blob: 83ad8b013b9894f06afd08f89743cb907e38b45a [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020042#include <linux/power_supply.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053043#include <linux/mhl_8334.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
Devin Kimd10f03c2012-06-25 17:05:13 -070045#include <asm/mach-types.h>
46
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053047#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080048#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053049#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053050#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053051
52#define MSM_USB_BASE (motg->regs)
53#define DRIVER_NAME "msm_otg"
54
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053055#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053056#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053057#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
58#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
59#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
60#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
61
62#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
63#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
64#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
65#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
66
Mayank Rana248698c2012-04-19 00:03:16 +053067#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080068#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053069#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
70
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053071static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053073static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053074static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053075static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076
Anji jonnala11aa5c42011-05-04 10:19:48 +053077static struct regulator *hsusb_3p3;
78static struct regulator *hsusb_1p8;
79static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053080static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +053081static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053082static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053083
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053084static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053085static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +053086{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053087#ifdef CONFIG_USB_MSM_ACA
88 return true;
89#else
90 return debug_aca_enabled;
91#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +053092}
93
Mayank Rana248698c2012-04-19 00:03:16 +053094static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
95 { /* VDD_CX CORNER Voting */
96 [VDD_NONE] = RPM_VREG_CORNER_NONE,
97 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
98 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
99 },
100 { /* VDD_CX Voltage Voting */
101 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
102 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
103 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
104 },
105};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530106
107static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
108{
109 int rc = 0;
110
111 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700112 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530113 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200114 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530115 return PTR_ERR(hsusb_3p3);
116 }
117
118 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
119 USB_PHY_3P3_VOL_MAX);
120 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700121 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530123 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530124 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700125 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200127 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530130 }
131 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
132 USB_PHY_1P8_VOL_MAX);
133 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700134 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530136 goto put_1p8;
137 }
138
139 return 0;
140 }
141
Anji jonnala11aa5c42011-05-04 10:19:48 +0530142put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144put_3p3_lpm:
145 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530146 return rc;
147}
148
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530149static int msm_hsusb_config_vddcx(int high)
150{
Mayank Rana248698c2012-04-19 00:03:16 +0530151 struct msm_otg *motg = the_msm_otg;
152 enum usb_vdd_type vdd_type = motg->vdd_type;
153 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530154 int min_vol;
155 int ret;
156
Mayank Rana248698c2012-04-19 00:03:16 +0530157 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530158 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
159 if (ret) {
160 pr_err("%s: unable to set the voltage for regulator "
161 "HSUSB_VDDCX\n", __func__);
162 return ret;
163 }
164
165 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
166
167 return ret;
168}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530169
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530171{
172 int ret = 0;
173
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530174 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530175 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
176 return -ENODEV;
177 }
178
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530179 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530180 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
181 return -ENODEV;
182 }
183
184 if (on) {
185 ret = regulator_set_optimum_mode(hsusb_1p8,
186 USB_PHY_1P8_HPM_LOAD);
187 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530189 "HSUSB_1p8\n", __func__);
190 return ret;
191 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192
193 ret = regulator_enable(hsusb_1p8);
194 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700195 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196 __func__);
197 regulator_set_optimum_mode(hsusb_1p8, 0);
198 return ret;
199 }
200
Anji jonnala11aa5c42011-05-04 10:19:48 +0530201 ret = regulator_set_optimum_mode(hsusb_3p3,
202 USB_PHY_3P3_HPM_LOAD);
203 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530205 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 regulator_set_optimum_mode(hsusb_1p8, 0);
207 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530208 return ret;
209 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210
211 ret = regulator_enable(hsusb_3p3);
212 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700213 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214 __func__);
215 regulator_set_optimum_mode(hsusb_3p3, 0);
216 regulator_set_optimum_mode(hsusb_1p8, 0);
217 regulator_disable(hsusb_1p8);
218 return ret;
219 }
220
Anji jonnala11aa5c42011-05-04 10:19:48 +0530221 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 ret = regulator_disable(hsusb_1p8);
223 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700224 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 __func__);
226 return ret;
227 }
228
229 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530230 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233
234 ret = regulator_disable(hsusb_3p3);
235 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700236 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237 __func__);
238 return ret;
239 }
240 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530241 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530243 "HSUSB_3p3\n", __func__);
244 }
245
246 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
247 return ret < 0 ? ret : 0;
248}
249
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530250static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
251{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530252 struct msm_otg_platform_data *pdata = motg->pdata;
253
254 if (!pdata->mhl_enable)
255 return;
256
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530257 if (!mhl_usb_hs_switch) {
258 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530259 return;
260 }
261
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530262 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530263 if (regulator_enable(mhl_usb_hs_switch))
264 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530265 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530266 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530267 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530268}
269
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200270static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530271{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200272 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530273 int cnt = 0;
274
275 /* initiate read operation */
276 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
277 USB_ULPI_VIEWPORT);
278
279 /* wait for completion */
280 while (cnt < ULPI_IO_TIMEOUT_USEC) {
281 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
282 break;
283 udelay(1);
284 cnt++;
285 }
286
287 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200288 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530289 readl(USB_ULPI_VIEWPORT));
290 return -ETIMEDOUT;
291 }
292 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
293}
294
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200295static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530296{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200297 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530298 int cnt = 0;
299
300 /* initiate write operation */
301 writel(ULPI_RUN | ULPI_WRITE |
302 ULPI_ADDR(reg) | ULPI_DATA(val),
303 USB_ULPI_VIEWPORT);
304
305 /* wait for completion */
306 while (cnt < ULPI_IO_TIMEOUT_USEC) {
307 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
308 break;
309 udelay(1);
310 cnt++;
311 }
312
313 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200314 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530315 return -ETIMEDOUT;
316 }
317 return 0;
318}
319
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200320static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530321 .read = ulpi_read,
322 .write = ulpi_write,
323};
324
325static void ulpi_init(struct msm_otg *motg)
326{
327 struct msm_otg_platform_data *pdata = motg->pdata;
328 int *seq = pdata->phy_init_seq;
329
330 if (!seq)
331 return;
332
333 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200334 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530335 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200336 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530337 seq += 2;
338 }
339}
340
341static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
342{
343 int ret;
344
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530345 if (IS_ERR(motg->clk))
346 return 0;
347
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530348 if (assert) {
349 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
350 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200351 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530352 } else {
353 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
354 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200355 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530356 }
357 return ret;
358}
359
360static int msm_otg_phy_clk_reset(struct msm_otg *motg)
361{
362 int ret;
363
Amit Blay02eff132011-09-21 16:46:24 +0300364 if (IS_ERR(motg->phy_reset_clk))
365 return 0;
366
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530367 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
368 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200369 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530370 return ret;
371 }
372 usleep_range(10000, 12000);
373 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
374 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200375 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530376 return ret;
377}
378
379static int msm_otg_phy_reset(struct msm_otg *motg)
380{
381 u32 val;
382 int ret;
383 int retries;
384
385 ret = msm_otg_link_clk_reset(motg, 1);
386 if (ret)
387 return ret;
388 ret = msm_otg_phy_clk_reset(motg);
389 if (ret)
390 return ret;
391 ret = msm_otg_link_clk_reset(motg, 0);
392 if (ret)
393 return ret;
394
395 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
396 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
397
398 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200399 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530400 ULPI_CLR(ULPI_FUNC_CTRL));
401 if (!ret)
402 break;
403 ret = msm_otg_phy_clk_reset(motg);
404 if (ret)
405 return ret;
406 }
407 if (!retries)
408 return -ETIMEDOUT;
409
410 /* This reset calibrates the phy, if the above write succeeded */
411 ret = msm_otg_phy_clk_reset(motg);
412 if (ret)
413 return ret;
414
415 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200416 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530417 if (ret != -ETIMEDOUT)
418 break;
419 ret = msm_otg_phy_clk_reset(motg);
420 if (ret)
421 return ret;
422 }
423 if (!retries)
424 return -ETIMEDOUT;
425
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200426 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530427 return 0;
428}
429
430#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530431static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530433 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530434
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530435 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530436 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530437 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530438 break;
439 udelay(1);
440 cnt++;
441 }
442 if (cnt >= LINK_RESET_TIMEOUT_USEC)
443 return -ETIMEDOUT;
444
445 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530446 writel_relaxed(0x80000000, USB_PORTSC);
447 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530448 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530449
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530450 return 0;
451}
452
Steve Mucklef132c6c2012-06-06 18:30:57 -0700453static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530454{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700455 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530456 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530457 int ret;
458 u32 val = 0;
459 u32 ulpi_val = 0;
460
Ofir Cohen4da266f2012-01-03 10:19:29 +0200461 /*
462 * USB PHY and Link reset also reset the USB BAM.
463 * Thus perform reset operation only once to avoid
464 * USB BAM reset on other cases e.g. USB cable disconnections.
465 */
466 if (pdata->disable_reset_on_disconnect) {
467 if (motg->reset_counter)
468 return 0;
469 else
470 motg->reset_counter++;
471 }
472
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530473 if (!IS_ERR(motg->clk))
474 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530475 ret = msm_otg_phy_reset(motg);
476 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700477 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530478 return ret;
479 }
480
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530481 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530482 ret = msm_otg_link_reset(motg);
483 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700484 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530485 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530486 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530487 msleep(100);
488
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530489 ulpi_init(motg);
490
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 /* Ensure that RESET operation is completed before turning off clock */
492 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530493
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530494 if (!IS_ERR(motg->clk))
495 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530496
497 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530498 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530499 if (pdata->mode == USB_OTG) {
500 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
501 val |= OTGSC_IDIE | OTGSC_BSVIE;
502 } else if (pdata->mode == USB_PERIPHERAL) {
503 ulpi_val = ULPI_INT_SESS_VALID;
504 val |= OTGSC_BSVIE;
505 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530506 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200507 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
508 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530509 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700510 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530511 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530512 /* Enable PMIC pull-up */
513 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530514 }
515
516 return 0;
517}
518
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530519static const char *timer_string(int bit)
520{
521 switch (bit) {
522 case A_WAIT_VRISE: return "a_wait_vrise";
523 case A_WAIT_VFALL: return "a_wait_vfall";
524 case B_SRP_FAIL: return "b_srp_fail";
525 case A_WAIT_BCON: return "a_wait_bcon";
526 case A_AIDL_BDIS: return "a_aidl_bdis";
527 case A_BIDL_ADIS: return "a_bidl_adis";
528 case B_ASE0_BRST: return "b_ase0_brst";
529 case A_TST_MAINT: return "a_tst_maint";
530 case B_TST_SRP: return "b_tst_srp";
531 case B_TST_CONFIG: return "b_tst_config";
532 default: return "UNDEFINED";
533 }
534}
535
536static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
537{
538 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
539
540 switch (motg->active_tmout) {
541 case A_WAIT_VRISE:
542 /* TODO: use vbus_vld interrupt */
543 set_bit(A_VBUS_VLD, &motg->inputs);
544 break;
545 case A_TST_MAINT:
546 /* OTG PET: End session after TA_TST_MAINT */
547 set_bit(A_BUS_DROP, &motg->inputs);
548 break;
549 case B_TST_SRP:
550 /*
551 * OTG PET: Initiate SRP after TB_TST_SRP of
552 * previous session end.
553 */
554 set_bit(B_BUS_REQ, &motg->inputs);
555 break;
556 case B_TST_CONFIG:
557 clear_bit(A_CONN, &motg->inputs);
558 break;
559 default:
560 set_bit(motg->active_tmout, &motg->tmouts);
561 }
562
563 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
564 queue_work(system_nrt_wq, &motg->sm_work);
565 return HRTIMER_NORESTART;
566}
567
568static void msm_otg_del_timer(struct msm_otg *motg)
569{
570 int bit = motg->active_tmout;
571
572 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
573 div_s64(ktime_to_us(hrtimer_get_remaining(
574 &motg->timer)), 1000));
575 hrtimer_cancel(&motg->timer);
576 clear_bit(bit, &motg->tmouts);
577}
578
579static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
580{
581 clear_bit(bit, &motg->tmouts);
582 motg->active_tmout = bit;
583 pr_debug("starting %s timer\n", timer_string(bit));
584 hrtimer_start(&motg->timer,
585 ktime_set(time / 1000, (time % 1000) * 1000000),
586 HRTIMER_MODE_REL);
587}
588
589static void msm_otg_init_timer(struct msm_otg *motg)
590{
591 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
592 motg->timer.function = msm_otg_timer_func;
593}
594
Steve Mucklef132c6c2012-06-06 18:30:57 -0700595static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530596{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700597 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530598
Steve Mucklef132c6c2012-06-06 18:30:57 -0700599 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530600 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700601 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530602 return -EINVAL;
603 }
604
605 pr_debug("A-Host: HNP initiated\n");
606 clear_bit(A_BUS_REQ, &motg->inputs);
607 queue_work(system_nrt_wq, &motg->sm_work);
608 return 0;
609}
610
Steve Mucklef132c6c2012-06-06 18:30:57 -0700611static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530612{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700613 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530614 u32 val;
615 int ret = 0;
616
Steve Mucklef132c6c2012-06-06 18:30:57 -0700617 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530618 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700619 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530620 ret = -EINVAL;
621 goto out;
622 }
623
624 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
625 pr_debug("initial conditions of SRP are not met. Try again"
626 "after some time\n");
627 ret = -EAGAIN;
628 goto out;
629 }
630
631 pr_debug("B-Device SRP started\n");
632
633 /*
634 * PHY won't pull D+ high unless it detects Vbus valid.
635 * Since by definition, SRP is only done when Vbus is not valid,
636 * software work-around needs to be used to spoof the PHY into
637 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
638 * VBUSVLDEXT register bits.
639 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700640 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530641 /*
642 * Harware auto assist data pulsing: Data pulse is given
643 * for 7msec; wait for vbus
644 */
645 val = readl_relaxed(USB_OTGSC);
646 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
647
648 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
649out:
650 return ret;
651}
652
Steve Mucklef132c6c2012-06-06 18:30:57 -0700653static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530654{
655 struct usb_hcd *hcd = bus_to_hcd(otg->host);
656 struct usb_device *rhub = otg->host->root_hub;
657
658 if (enable) {
659 pm_runtime_disable(&rhub->dev);
660 rhub->state = USB_STATE_NOTATTACHED;
661 hcd->driver->bus_suspend(hcd);
662 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
663 } else {
664 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700665 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530666 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
667 }
668}
669
Steve Mucklef132c6c2012-06-06 18:30:57 -0700670static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530671{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700672 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530673
Amit Blay6fa647a2012-05-24 14:12:08 +0300674 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530675 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530676
Jack Pham69e621d2012-06-25 18:48:07 -0700677 if (atomic_read(&motg->in_lpm) == suspend)
678 return 0;
679
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530680 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700681 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530682 case OTG_STATE_A_WAIT_BCON:
683 if (TA_WAIT_BCON > 0)
684 break;
685 /* fall through */
686 case OTG_STATE_A_HOST:
687 pr_debug("host bus suspend\n");
688 clear_bit(A_BUS_REQ, &motg->inputs);
689 queue_work(system_nrt_wq, &motg->sm_work);
690 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300691 case OTG_STATE_B_PERIPHERAL:
692 pr_debug("peripheral bus suspend\n");
693 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
694 break;
695 set_bit(A_BUS_SUSPEND, &motg->inputs);
696 queue_work(system_nrt_wq, &motg->sm_work);
697 break;
698
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530699 default:
700 break;
701 }
702 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700703 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530704 case OTG_STATE_A_SUSPEND:
705 /* Remote wakeup or resume */
706 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700707 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700708
709 /* ensure hardware is not in low power mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700710 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530711 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300712 case OTG_STATE_B_PERIPHERAL:
713 pr_debug("peripheral bus resume\n");
714 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
715 break;
716 clear_bit(A_BUS_SUSPEND, &motg->inputs);
717 queue_work(system_nrt_wq, &motg->sm_work);
718 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530719 default:
720 break;
721 }
722 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530723 return 0;
724}
725
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530726#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530727#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
728
729#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530730static int msm_otg_suspend(struct msm_otg *motg)
731{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200732 struct usb_phy *phy = &motg->phy;
733 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530734 struct msm_otg_platform_data *pdata = motg->pdata;
735 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300736 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530737 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800738 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530739 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530740
741 if (atomic_read(&motg->in_lpm))
742 return 0;
743
744 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530745 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
746 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700747 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300748 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
749 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530750 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530751 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530752 * Chipidea 45-nm PHY suspend sequence:
753 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530754 * Interrupt Latch Register auto-clear feature is not present
755 * in all PHY versions. Latch register is clear on read type.
756 * Clear latch register to avoid spurious wakeup from
757 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530758 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530759 * PHY comparators are disabled when PHY enters into low power
760 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
761 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
762 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530763 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530764 * PLL is not turned off when PHY enters into low power mode (LPM).
765 * Disable PLL for maximum power savings.
766 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530767
768 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200769 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530770 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200771 ulpi_write(phy, 0x01, 0x30);
772 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530773 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530774
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700775
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530776 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530777 * PHY may take some time or even fail to enter into low power
778 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
779 * in failure case.
780 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530781 portsc = readl_relaxed(USB_PORTSC);
782 if (!(portsc & PORTSC_PHCD)) {
783 writel_relaxed(portsc | PORTSC_PHCD,
784 USB_PORTSC);
785 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
786 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
787 break;
788 udelay(1);
789 cnt++;
790 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530791 }
792
793 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200794 dev_err(phy->dev, "Unable to suspend PHY\n");
795 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530796 enable_irq(motg->irq);
797 return -ETIMEDOUT;
798 }
799
800 /*
801 * PHY has capability to generate interrupt asynchronously in low
802 * power mode (LPM). This interrupt is level triggered. So USB IRQ
803 * line must be disabled till async interrupt enable bit is cleared
804 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
805 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530806 *
807 * PHY retention mode is disallowed while entering to LPM with wall
808 * charger connected. But PHY is put into suspend mode. Hence
809 * enable asynchronous interrupt to detect charger disconnection when
810 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530811 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530812 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300813 if (host_bus_suspend || device_bus_suspend ||
814 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530815 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
816 else
817 cmd_val |= ULPI_STP_CTRL;
818 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530819
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530820 /*
821 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
822 * PHY retention and collapse can not happen with VDP_SRC enabled.
823 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300824 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
825 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200826 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
827 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
828 /* Enable PHY HV interrupts to wake MPM/Link */
829 phy_ctrl_val |=
830 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530831
Amit Blay58b31472011-11-18 09:39:39 +0200832 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530834 }
835
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700836 /* Ensure that above operation is completed before turning off clocks */
837 mb();
Amit Blay9b6e58b2012-06-18 13:12:49 +0300838 if (!motg->pdata->core_clk_always_on_workaround) {
839 clk_disable_unprepare(motg->pclk);
840 clk_disable_unprepare(motg->core_clk);
841 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530842
Anji jonnala7da3f262011-12-02 17:22:14 -0800843 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530844 if (!host_bus_suspend) {
845 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
846 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700847 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530848 "TCXO D0 buffer%d\n", __func__, ret);
849 else
850 motg->lpm_flags |= XO_SHUTDOWN;
851 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800852
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530853 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
854 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 msm_hsusb_ldo_enable(motg, 0);
856 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530857 }
858
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530859 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530861 msm_hsusb_mhl_switch_enable(motg, 0);
862 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863
Steve Mucklef132c6c2012-06-06 18:30:57 -0700864 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530865 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 if (motg->pdata->pmic_id_irq)
867 enable_irq_wake(motg->pdata->pmic_id_irq);
868 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530869 if (bus)
870 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
871
872 atomic_set(&motg->in_lpm, 1);
873 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530875
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200876 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530877
878 return 0;
879}
880
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530881static int msm_otg_resume(struct msm_otg *motg)
882{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200883 struct usb_phy *phy = &motg->phy;
884 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530885 int cnt = 0;
886 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200887 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800888 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530889
890 if (!atomic_read(&motg->in_lpm))
891 return 0;
892
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530894
Anji jonnala7da3f262011-12-02 17:22:14 -0800895 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530896 if (motg->lpm_flags & XO_SHUTDOWN) {
897 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
898 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700899 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530900 "TCXO D0 buffer%d\n", __func__, ret);
901 motg->lpm_flags &= ~XO_SHUTDOWN;
902 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530903
Amit Blay9b6e58b2012-06-18 13:12:49 +0300904 if (!motg->pdata->core_clk_always_on_workaround) {
905 clk_prepare_enable(motg->core_clk);
906 clk_prepare_enable(motg->pclk);
907 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530908
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
910 msm_hsusb_ldo_enable(motg, 1);
911 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
912 }
913
914 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530915 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530916 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200917 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
918 phy_ctrl_val |= PHY_RETEN;
919 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
920 /* Disable PHY HV interrupts */
921 phy_ctrl_val &=
922 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
923 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700924 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530925 }
926
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530927 temp = readl(USB_USBCMD);
928 temp &= ~ASYNC_INTR_CTRL;
929 temp &= ~ULPI_STP_CTRL;
930 writel(temp, USB_USBCMD);
931
932 /*
933 * PHY comes out of low power mode (LPM) in case of wakeup
934 * from asynchronous interrupt.
935 */
936 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
937 goto skip_phy_resume;
938
939 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
940 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
941 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
942 break;
943 udelay(1);
944 cnt++;
945 }
946
947 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
948 /*
949 * This is a fatal error. Reset the link and
950 * PHY. USB state can not be restored. Re-insertion
951 * of USB cable is the only way to get USB working.
952 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200953 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530954 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200955 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530956 }
957
958skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700959 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530960 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 if (motg->pdata->pmic_id_irq)
962 disable_irq_wake(motg->pdata->pmic_id_irq);
963 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964 if (bus)
965 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
966
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530967 atomic_set(&motg->in_lpm, 0);
968
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530969 if (motg->async_int) {
970 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530971 enable_irq(motg->irq);
972 }
973
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200974 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530975
976 return 0;
977}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530978#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530979
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -0700980static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
981{
982 if (!psy)
983 goto psy_not_supported;
984
985 if (host_mode)
986 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
987 else
988 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
989
990psy_not_supported:
991 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
992 return -ENXIO;
993}
994
David Keitel081a3e22012-04-18 12:37:07 -0700995static int msm_otg_notify_chg_type(struct msm_otg *motg)
996{
997 static int charger_type;
998 /*
999 * TODO
1000 * Unify OTG driver charger types and power supply charger types
1001 */
1002 if (charger_type == motg->chg_type)
1003 return 0;
1004
1005 if (motg->chg_type == USB_SDP_CHARGER)
1006 charger_type = POWER_SUPPLY_TYPE_USB;
1007 else if (motg->chg_type == USB_CDP_CHARGER)
1008 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301009 else if (motg->chg_type == USB_DCP_CHARGER ||
1010 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001011 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1012 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1013 motg->chg_type == USB_ACA_A_CHARGER ||
1014 motg->chg_type == USB_ACA_B_CHARGER ||
1015 motg->chg_type == USB_ACA_C_CHARGER))
1016 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1017 else
1018 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1019
1020 return pm8921_set_usb_power_supply_type(charger_type);
1021}
1022
Amit Blay0f7edf72012-01-15 10:11:27 +02001023static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1024{
Amit Blay0f7edf72012-01-15 10:11:27 +02001025
Amit Blay0f7edf72012-01-15 10:11:27 +02001026 if (!psy)
1027 goto psy_not_supported;
1028
1029 if (motg->cur_power == 0 && mA > 0) {
1030 /* Enable charging */
1031 if (power_supply_set_online(psy, true))
1032 goto psy_not_supported;
1033 } else if (motg->cur_power > 0 && mA == 0) {
1034 /* Disable charging */
1035 if (power_supply_set_online(psy, false))
1036 goto psy_not_supported;
1037 return 0;
1038 }
1039 /* Set max current limit */
1040 if (power_supply_set_current_limit(psy, 1000*mA))
1041 goto psy_not_supported;
1042
1043 return 0;
1044
1045psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001046 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001047 return -ENXIO;
1048}
1049
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301050static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1051{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001052 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301053
1054 if (g && g->is_a_peripheral)
1055 return;
1056
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301057 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1058 motg->chg_type == USB_ACA_A_CHARGER ||
1059 motg->chg_type == USB_ACA_B_CHARGER ||
1060 motg->chg_type == USB_ACA_C_CHARGER) &&
1061 mA > IDEV_ACA_CHG_LIMIT)
1062 mA = IDEV_ACA_CHG_LIMIT;
1063
David Keitel081a3e22012-04-18 12:37:07 -07001064 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001065 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001066 "Failed notifying %d charger type to PMIC\n",
1067 motg->chg_type);
1068
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301069 if (motg->cur_power == mA)
1070 return;
1071
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001072 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001073
1074 /*
1075 * Use Power Supply API if supported, otherwise fallback
1076 * to legacy pm8921 API.
1077 */
1078 if (msm_otg_notify_power_supply(motg, mA))
1079 pm8921_charger_vbus_draw(mA);
1080
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301081 motg->cur_power = mA;
1082}
1083
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001084static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301085{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001086 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301087
1088 /*
1089 * Gadget driver uses set_power method to notify about the
1090 * available current based on suspend/configured states.
1091 *
1092 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1093 * states when CDP/ACA is connected.
1094 */
1095 if (motg->chg_type == USB_SDP_CHARGER)
1096 msm_otg_notify_charger(motg, mA);
1097
1098 return 0;
1099}
1100
Steve Mucklef132c6c2012-06-06 18:30:57 -07001101static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301102{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001103 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301104 struct msm_otg_platform_data *pdata = motg->pdata;
1105 struct usb_hcd *hcd;
1106
1107 if (!otg->host)
1108 return;
1109
1110 hcd = bus_to_hcd(otg->host);
1111
1112 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001113 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301114
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301115 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001116 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301117 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1118
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301119 /*
1120 * Some boards have a switch cotrolled by gpio
1121 * to enable/disable internal HUB. Enable internal
1122 * HUB before kicking the host.
1123 */
1124 if (pdata->setup_gpio)
1125 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301126 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301127 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001128 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301129
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301130 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301131 /* HCD core reset all bits of PORTSC. select ULPI phy */
1132 writel_relaxed(0x80000000, USB_PORTSC);
1133
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301134 if (pdata->setup_gpio)
1135 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301136
1137 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001138 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301139 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301140 }
1141}
1142
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143static int msm_otg_usbdev_notify(struct notifier_block *self,
1144 unsigned long action, void *priv)
1145{
1146 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001147 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301148 struct usb_device *udev = priv;
1149
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301150 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1151 goto out;
1152
Steve Mucklef132c6c2012-06-06 18:30:57 -07001153 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301154 goto out;
1155 /*
1156 * Interested in devices connected directly to the root hub.
1157 * ACA dock can supply IDEV_CHG irrespective devices connected
1158 * on the accessory port.
1159 */
1160 if (!udev->parent || udev->parent->parent ||
1161 motg->chg_type == USB_ACA_DOCK_CHARGER)
1162 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163
1164 switch (action) {
1165 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301166 if (aca_enabled())
1167 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001168 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301169 pr_debug("B_CONN set\n");
1170 set_bit(B_CONN, &motg->inputs);
1171 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001172 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301173 /*
1174 * OTG PET: A-device must end session within
1175 * 10 sec after PET enumeration.
1176 */
1177 if (udev->quirks & USB_QUIRK_OTG_PET)
1178 msm_otg_start_timer(motg, TA_TST_MAINT,
1179 A_TST_MAINT);
1180 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301181 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001183 if (udev->actconfig)
1184 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1185 else
1186 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001187 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301188 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301189 break;
1190 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001191 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1192 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301193 pr_debug("B_CONN clear\n");
1194 clear_bit(B_CONN, &motg->inputs);
1195 /*
1196 * OTG PET: A-device must end session after
1197 * PET disconnection if it is enumerated
1198 * with bcdDevice[0] = 1. USB core sets
1199 * bus->otg_vbus_off for us. clear it here.
1200 */
1201 if (udev->bus->otg_vbus_off) {
1202 udev->bus->otg_vbus_off = 0;
1203 set_bit(A_BUS_DROP, &motg->inputs);
1204 }
1205 queue_work(system_nrt_wq, &motg->sm_work);
1206 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 default:
1208 break;
1209 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301210 if (test_bit(ID_A, &motg->inputs))
1211 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1212 motg->mA_port);
1213out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214 return NOTIFY_OK;
1215}
1216
Mayank Ranae3926882011-12-26 09:47:54 +05301217static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1218{
1219 int ret;
1220 static bool vbus_is_on;
1221
1222 if (vbus_is_on == on)
1223 return;
1224
1225 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301226 ret = motg->pdata->vbus_power(on);
1227 if (!ret)
1228 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301229 return;
1230 }
1231
1232 if (!vbus_otg) {
1233 pr_err("vbus_otg is NULL.");
1234 return;
1235 }
1236
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001237 /*
1238 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001239 * from usb before turning on the boost.
1240 * if exiting host mode disable the boost before enabling to draw
1241 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001242 */
Mayank Ranae3926882011-12-26 09:47:54 +05301243 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001244 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301245 ret = regulator_enable(vbus_otg);
1246 if (ret) {
1247 pr_err("unable to enable vbus_otg\n");
1248 return;
1249 }
1250 vbus_is_on = true;
1251 } else {
1252 ret = regulator_disable(vbus_otg);
1253 if (ret) {
1254 pr_err("unable to disable vbus_otg\n");
1255 return;
1256 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001257 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301258 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301259 }
1260}
1261
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001262static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301263{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001264 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301265 struct usb_hcd *hcd;
1266
1267 /*
1268 * Fail host registration if this board can support
1269 * only peripheral configuration.
1270 */
1271 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001272 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301273 return -ENODEV;
1274 }
1275
Devin Kimd10f03c2012-06-25 17:05:13 -07001276 if (!machine_is_apq8064_mako()) {
1277 if (!motg->pdata->vbus_power && host) {
1278 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1279 if (IS_ERR(vbus_otg)) {
1280 pr_err("Unable to get vbus_otg\n");
1281 return -ENODEV;
1282 }
Mayank Ranae3926882011-12-26 09:47:54 +05301283 }
1284 }
1285
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301286 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001287 if (otg->phy->state == OTG_STATE_A_HOST) {
1288 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301290 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301291 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301292 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001293 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301294 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301295 } else {
1296 otg->host = NULL;
1297 }
1298
1299 return 0;
1300 }
1301
1302 hcd = bus_to_hcd(host);
1303 hcd->power_budget = motg->pdata->power_budget;
1304
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301305#ifdef CONFIG_USB_OTG
1306 host->otg_port = 1;
1307#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001308 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1309 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301310 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001311 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301312
1313 /*
1314 * Kick the state machine work, if peripheral is not supported
1315 * or peripheral is already registered with us.
1316 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301317 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001318 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301319 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301320 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301321
1322 return 0;
1323}
1324
Steve Mucklef132c6c2012-06-06 18:30:57 -07001325static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301326{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301327 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001328 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301329 struct msm_otg_platform_data *pdata = motg->pdata;
1330
1331 if (!otg->gadget)
1332 return;
1333
1334 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001335 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301336 /*
1337 * Some boards have a switch cotrolled by gpio
1338 * to enable/disable internal HUB. Disable internal
1339 * HUB before kicking the gadget.
1340 */
1341 if (pdata->setup_gpio)
1342 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001343
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301344 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301345 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301346 ret = msm_bus_scale_client_update_request(
1347 motg->bus_perf_client, 1);
1348 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001349 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301350 "bus bandwidth %d\n", __func__, ret);
1351 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301352 usb_gadget_vbus_connect(otg->gadget);
1353 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001354 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301355 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301356 /* Configure BUS performance parameters to default */
1357 if (motg->bus_perf_client) {
1358 ret = msm_bus_scale_client_update_request(
1359 motg->bus_perf_client, 0);
1360 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001361 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301362 "for bus bw %d\n", __func__, ret);
1363 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301364 if (pdata->setup_gpio)
1365 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1366 }
1367
1368}
1369
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001370static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301371 struct usb_gadget *gadget)
1372{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001373 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301374
1375 /*
1376 * Fail peripheral registration if this board can support
1377 * only host configuration.
1378 */
1379 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001380 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301381 return -ENODEV;
1382 }
1383
1384 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001385 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1386 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301387 msm_otg_start_peripheral(otg, 0);
1388 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001389 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301390 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301391 } else {
1392 otg->gadget = NULL;
1393 }
1394
1395 return 0;
1396 }
1397 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001398 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301399
1400 /*
1401 * Kick the state machine work, if host is not supported
1402 * or host is already registered with us.
1403 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301404 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001405 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301406 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301407 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301408
1409 return 0;
1410}
1411
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301412static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1413 void (*callback)(int on))
1414{
1415 struct usb_phy *phy = &motg->phy;
1416 int ret;
1417
1418 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1419 !motg->pdata->pmic_id_irq) {
1420 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1421 return -ENODEV;
1422 }
1423
1424 if (!motg->pdata->mhl_dev_name) {
1425 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1426 return -ENODEV;
1427 }
1428
1429 if (callback)
1430 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1431 callback);
1432 else
1433 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1434
1435 if (ret)
1436 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1437 motg->pdata->mhl_dev_name, ret);
1438 else
1439 motg->mhl_enabled = true;
1440
1441 return ret;
1442}
1443
1444static void msm_otg_mhl_notify_online(int on)
1445{
1446 struct msm_otg *motg = the_msm_otg;
1447 struct usb_phy *phy = &motg->phy;
1448 bool queue = false;
1449
1450 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1451
1452 if (on) {
1453 set_bit(MHL, &motg->inputs);
1454 } else {
1455 clear_bit(MHL, &motg->inputs);
1456 queue = true;
1457 }
1458
1459 if (queue && phy->state != OTG_STATE_UNDEFINED)
1460 schedule_work(&motg->sm_work);
1461}
1462
1463static bool msm_otg_is_mhl(struct msm_otg *motg)
1464{
1465 struct usb_phy *phy = &motg->phy;
1466 int is_mhl, ret;
1467
1468 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1469 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1470 /*
1471 * MHL driver calls our callback saying that MHL connected
1472 * if RID_GND is detected. But at later part of discovery
1473 * it may figure out MHL is not connected and returns
1474 * false. Hence clear MHL input here.
1475 */
1476 clear_bit(MHL, &motg->inputs);
1477 dev_dbg(phy->dev, "MHL device not found\n");
1478 return false;
1479 }
1480
1481 set_bit(MHL, &motg->inputs);
1482 dev_dbg(phy->dev, "MHL device found\n");
1483 return true;
1484}
1485
1486static bool msm_chg_mhl_detect(struct msm_otg *motg)
1487{
1488 bool ret, id;
1489 unsigned long flags;
1490
1491 if (!motg->mhl_enabled)
1492 return false;
1493
1494 local_irq_save(flags);
1495 id = irq_read_line(motg->pdata->pmic_id_irq);
1496 local_irq_restore(flags);
1497
1498 if (id)
1499 return false;
1500
1501 mhl_det_in_progress = true;
1502 ret = msm_otg_is_mhl(motg);
1503 mhl_det_in_progress = false;
1504
1505 return ret;
1506}
1507
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001508static bool msm_chg_aca_detect(struct msm_otg *motg)
1509{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001510 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001511 u32 int_sts;
1512 bool ret = false;
1513
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301514 if (!aca_enabled())
1515 goto out;
1516
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001517 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1518 goto out;
1519
Steve Mucklef132c6c2012-06-06 18:30:57 -07001520 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001521 switch (int_sts & 0x1C) {
1522 case 0x08:
1523 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001524 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001525 motg->chg_type = USB_ACA_A_CHARGER;
1526 motg->chg_state = USB_CHG_STATE_DETECTED;
1527 clear_bit(ID_B, &motg->inputs);
1528 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301529 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001530 ret = true;
1531 }
1532 break;
1533 case 0x0C:
1534 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001535 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001536 motg->chg_type = USB_ACA_B_CHARGER;
1537 motg->chg_state = USB_CHG_STATE_DETECTED;
1538 clear_bit(ID_A, &motg->inputs);
1539 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301540 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001541 ret = true;
1542 }
1543 break;
1544 case 0x10:
1545 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001546 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001547 motg->chg_type = USB_ACA_C_CHARGER;
1548 motg->chg_state = USB_CHG_STATE_DETECTED;
1549 clear_bit(ID_A, &motg->inputs);
1550 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301551 set_bit(ID, &motg->inputs);
1552 ret = true;
1553 }
1554 break;
1555 case 0x04:
1556 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001557 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301558 motg->chg_type = USB_INVALID_CHARGER;
1559 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1560 clear_bit(ID_A, &motg->inputs);
1561 clear_bit(ID_B, &motg->inputs);
1562 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001563 ret = true;
1564 }
1565 break;
1566 default:
1567 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1568 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301569 test_and_clear_bit(ID_C, &motg->inputs) |
1570 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001571 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001572 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001573 motg->chg_type = USB_INVALID_CHARGER;
1574 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1575 }
1576 }
1577out:
1578 return ret;
1579}
1580
1581static void msm_chg_enable_aca_det(struct msm_otg *motg)
1582{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001583 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001584
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301585 if (!aca_enabled())
1586 return;
1587
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001588 switch (motg->pdata->phy_type) {
1589 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301590 /* Disable ID_GND in link and PHY */
1591 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1592 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001593 ulpi_write(phy, 0x01, 0x0C);
1594 ulpi_write(phy, 0x10, 0x0F);
1595 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301596 /* Disable PMIC ID pull-up */
1597 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301598 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001599 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301600 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001601 break;
1602 default:
1603 break;
1604 }
1605}
1606
1607static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1608{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001609 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001610
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301611 if (!aca_enabled())
1612 return;
1613
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001614 switch (motg->pdata->phy_type) {
1615 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301616 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001617 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301618 break;
1619 default:
1620 break;
1621 }
1622}
1623
1624static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1625{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001626 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301627
1628 if (!aca_enabled())
1629 return;
1630
1631 switch (motg->pdata->phy_type) {
1632 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001633 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001634 break;
1635 default:
1636 break;
1637 }
1638}
1639
1640static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1641{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001642 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001643 bool ret = false;
1644
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301645 if (!aca_enabled())
1646 return ret;
1647
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001648 switch (motg->pdata->phy_type) {
1649 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001650 if (ulpi_read(phy, 0x91) & 1) {
1651 dev_dbg(phy->dev, "RID change\n");
1652 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001653 ret = msm_chg_aca_detect(motg);
1654 }
1655 default:
1656 break;
1657 }
1658 return ret;
1659}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301660
1661static void msm_otg_id_timer_func(unsigned long data)
1662{
1663 struct msm_otg *motg = (struct msm_otg *) data;
1664
1665 if (!aca_enabled())
1666 return;
1667
1668 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001669 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301670 return;
1671 }
1672
Steve Mucklef132c6c2012-06-06 18:30:57 -07001673 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301674 goto out;
1675
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301676 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001677 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301678 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301679 }
1680
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301681out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301682 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1683 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1684}
1685
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301686static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1687{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001688 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301689 u32 chg_det;
1690 bool ret = false;
1691
1692 switch (motg->pdata->phy_type) {
1693 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001694 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301695 ret = chg_det & (1 << 4);
1696 break;
1697 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001698 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301699 ret = chg_det & 1;
1700 break;
1701 default:
1702 break;
1703 }
1704 return ret;
1705}
1706
1707static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1708{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001709 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301710 u32 chg_det;
1711
1712 switch (motg->pdata->phy_type) {
1713 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001714 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301715 /* Turn off charger block */
1716 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001717 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301718 udelay(20);
1719 /* control chg block via ULPI */
1720 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001721 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301722 /* put it in host mode for enabling D- source */
1723 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001724 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301725 /* Turn on chg detect block */
1726 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001727 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301728 udelay(20);
1729 /* enable chg detection */
1730 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001731 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301732 break;
1733 case SNPS_28NM_INTEGRATED_PHY:
1734 /*
1735 * Configure DM as current source, DP as current sink
1736 * and enable battery charging comparators.
1737 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001738 ulpi_write(phy, 0x8, 0x85);
1739 ulpi_write(phy, 0x2, 0x85);
1740 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301741 break;
1742 default:
1743 break;
1744 }
1745}
1746
1747static bool msm_chg_check_primary_det(struct msm_otg *motg)
1748{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001749 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301750 u32 chg_det;
1751 bool ret = false;
1752
1753 switch (motg->pdata->phy_type) {
1754 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001755 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301756 ret = chg_det & (1 << 4);
1757 break;
1758 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001759 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301760 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301761 /* Turn off VDP_SRC */
1762 ulpi_write(phy, 0x3, 0x86);
1763 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301764 break;
1765 default:
1766 break;
1767 }
1768 return ret;
1769}
1770
1771static void msm_chg_enable_primary_det(struct msm_otg *motg)
1772{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001773 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301774 u32 chg_det;
1775
1776 switch (motg->pdata->phy_type) {
1777 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001778 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301779 /* enable chg detection */
1780 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001781 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301782 break;
1783 case SNPS_28NM_INTEGRATED_PHY:
1784 /*
1785 * Configure DP as current source, DM as current sink
1786 * and enable battery charging comparators.
1787 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001788 ulpi_write(phy, 0x2, 0x85);
1789 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301790 break;
1791 default:
1792 break;
1793 }
1794}
1795
1796static bool msm_chg_check_dcd(struct msm_otg *motg)
1797{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001798 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301799 u32 line_state;
1800 bool ret = false;
1801
1802 switch (motg->pdata->phy_type) {
1803 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001804 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301805 ret = !(line_state & 1);
1806 break;
1807 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001808 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301809 ret = line_state & 2;
1810 break;
1811 default:
1812 break;
1813 }
1814 return ret;
1815}
1816
1817static void msm_chg_disable_dcd(struct msm_otg *motg)
1818{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001819 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301820 u32 chg_det;
1821
1822 switch (motg->pdata->phy_type) {
1823 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001824 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301825 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001826 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301827 break;
1828 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001829 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301830 break;
1831 default:
1832 break;
1833 }
1834}
1835
1836static void msm_chg_enable_dcd(struct msm_otg *motg)
1837{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001838 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301839 u32 chg_det;
1840
1841 switch (motg->pdata->phy_type) {
1842 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001843 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301844 /* Turn on D+ current source */
1845 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001846 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301847 break;
1848 case SNPS_28NM_INTEGRATED_PHY:
1849 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001850 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301851 break;
1852 default:
1853 break;
1854 }
1855}
1856
1857static void msm_chg_block_on(struct msm_otg *motg)
1858{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001859 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301860 u32 func_ctrl, chg_det;
1861
1862 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001863 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301864 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1865 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001866 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301867
1868 switch (motg->pdata->phy_type) {
1869 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001870 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301871 /* control chg block via ULPI */
1872 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001873 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301874 /* Turn on chg detect block */
1875 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001876 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301877 udelay(20);
1878 break;
1879 case SNPS_28NM_INTEGRATED_PHY:
1880 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001881 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301882 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001883 ulpi_write(phy, 0x1F, 0x92);
1884 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301885 udelay(100);
1886 break;
1887 default:
1888 break;
1889 }
1890}
1891
1892static void msm_chg_block_off(struct msm_otg *motg)
1893{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001894 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301895 u32 func_ctrl, chg_det;
1896
1897 switch (motg->pdata->phy_type) {
1898 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001899 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301900 /* Turn off charger block */
1901 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001902 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301903 break;
1904 case SNPS_28NM_INTEGRATED_PHY:
1905 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001906 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301907 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001908 ulpi_write(phy, 0x1F, 0x92);
1909 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301910 break;
1911 default:
1912 break;
1913 }
1914
1915 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001916 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301917 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1918 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001919 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301920}
1921
Anji jonnalad270e2d2011-08-09 11:28:32 +05301922static const char *chg_to_string(enum usb_chg_type chg_type)
1923{
1924 switch (chg_type) {
1925 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1926 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1927 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1928 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1929 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1930 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1931 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301932 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05301933 default: return "INVALID_CHARGER";
1934 }
1935}
1936
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301937#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1938#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301939#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1940#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301941static void msm_chg_detect_work(struct work_struct *w)
1942{
1943 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001944 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301945 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301946 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301947 unsigned long delay;
1948
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001949 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301950
1951 if (test_bit(MHL, &motg->inputs)) {
1952 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
1953 return;
1954 }
1955
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301956 switch (motg->chg_state) {
1957 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301958 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301959 if (motg->pdata->enable_dcd)
1960 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001961 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301962 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1963 motg->dcd_retries = 0;
1964 delay = MSM_CHG_DCD_POLL_TIME;
1965 break;
1966 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301967 if (msm_chg_mhl_detect(motg)) {
1968 msm_chg_block_off(motg);
1969 motg->chg_state = USB_CHG_STATE_DETECTED;
1970 motg->chg_type = USB_INVALID_CHARGER;
1971 queue_work(system_nrt_wq, &motg->sm_work);
1972 return;
1973 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001974 is_aca = msm_chg_aca_detect(motg);
1975 if (is_aca) {
1976 /*
1977 * ID_A can be ACA dock too. continue
1978 * primary detection after DCD.
1979 */
1980 if (test_bit(ID_A, &motg->inputs)) {
1981 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1982 } else {
1983 delay = 0;
1984 break;
1985 }
1986 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301987 if (motg->pdata->enable_dcd)
1988 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301989 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1990 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301991 if (motg->pdata->enable_dcd)
1992 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301993 msm_chg_enable_primary_det(motg);
1994 delay = MSM_CHG_PRIMARY_DET_TIME;
1995 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1996 } else {
1997 delay = MSM_CHG_DCD_POLL_TIME;
1998 }
1999 break;
2000 case USB_CHG_STATE_DCD_DONE:
2001 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302002 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2003 dm_vlgc = line_state & PORTSC_LS_DM;
2004 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302005 if (test_bit(ID_A, &motg->inputs)) {
2006 motg->chg_type = USB_ACA_DOCK_CHARGER;
2007 motg->chg_state = USB_CHG_STATE_DETECTED;
2008 delay = 0;
2009 break;
2010 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302011 if (line_state) { /* DP > VLGC */
2012 motg->chg_type = USB_PROPRIETARY_CHARGER;
2013 motg->chg_state = USB_CHG_STATE_DETECTED;
2014 delay = 0;
2015 } else {
2016 msm_chg_enable_secondary_det(motg);
2017 delay = MSM_CHG_SECONDARY_DET_TIME;
2018 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2019 }
2020 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302021 if (test_bit(ID_A, &motg->inputs)) {
2022 motg->chg_type = USB_ACA_A_CHARGER;
2023 motg->chg_state = USB_CHG_STATE_DETECTED;
2024 delay = 0;
2025 break;
2026 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302027
2028 if (line_state) /* DP > VLGC or/and DM > VLGC */
2029 motg->chg_type = USB_PROPRIETARY_CHARGER;
2030 else
2031 motg->chg_type = USB_SDP_CHARGER;
2032
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302033 motg->chg_state = USB_CHG_STATE_DETECTED;
2034 delay = 0;
2035 }
2036 break;
2037 case USB_CHG_STATE_PRIMARY_DONE:
2038 vout = msm_chg_check_secondary_det(motg);
2039 if (vout)
2040 motg->chg_type = USB_DCP_CHARGER;
2041 else
2042 motg->chg_type = USB_CDP_CHARGER;
2043 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2044 /* fall through */
2045 case USB_CHG_STATE_SECONDARY_DONE:
2046 motg->chg_state = USB_CHG_STATE_DETECTED;
2047 case USB_CHG_STATE_DETECTED:
2048 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002049 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302050 /*
2051 * Spurious interrupt is seen after enabling ACA detection
2052 * due to which charger detection fails in case of PET.
2053 * Add delay of 100 microsec to avoid that.
2054 */
2055 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002056 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002057 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302058 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302059 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302060 return;
2061 default:
2062 return;
2063 }
2064
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302065 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066}
2067
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302068/*
2069 * We support OTG, Peripheral only and Host only configurations. In case
2070 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2071 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2072 * enabled when switch is controlled by user and default mode is supplied
2073 * by board file, which can be changed by userspace later.
2074 */
2075static void msm_otg_init_sm(struct msm_otg *motg)
2076{
2077 struct msm_otg_platform_data *pdata = motg->pdata;
2078 u32 otgsc = readl(USB_OTGSC);
2079
2080 switch (pdata->mode) {
2081 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002082 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302083 if (pdata->default_mode == USB_HOST) {
2084 clear_bit(ID, &motg->inputs);
2085 } else if (pdata->default_mode == USB_PERIPHERAL) {
2086 set_bit(ID, &motg->inputs);
2087 set_bit(B_SESS_VLD, &motg->inputs);
2088 } else {
2089 set_bit(ID, &motg->inputs);
2090 clear_bit(B_SESS_VLD, &motg->inputs);
2091 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302092 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302093 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302094 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302095 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302096 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302097 set_bit(A_BUS_REQ, &motg->inputs);
2098 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002099 if (otgsc & OTGSC_BSV)
2100 set_bit(B_SESS_VLD, &motg->inputs);
2101 else
2102 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302103 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302104 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07002105 unsigned long flags;
2106 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302107 if (irq_read_line(pdata->pmic_id_irq))
2108 set_bit(ID, &motg->inputs);
2109 else
2110 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07002111 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302112 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302113 /*
2114 * VBUS initial state is reported after PMIC
2115 * driver initialization. Wait for it.
2116 */
2117 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302118 }
2119 break;
2120 case USB_HOST:
2121 clear_bit(ID, &motg->inputs);
2122 break;
2123 case USB_PERIPHERAL:
2124 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302125 if (pdata->otg_control == OTG_PHY_CONTROL) {
2126 if (otgsc & OTGSC_BSV)
2127 set_bit(B_SESS_VLD, &motg->inputs);
2128 else
2129 clear_bit(B_SESS_VLD, &motg->inputs);
2130 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2131 /*
2132 * VBUS initial state is reported after PMIC
2133 * driver initialization. Wait for it.
2134 */
2135 wait_for_completion(&pmic_vbus_init);
2136 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302137 break;
2138 default:
2139 break;
2140 }
2141}
2142
2143static void msm_otg_sm_work(struct work_struct *w)
2144{
2145 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002146 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302147 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302148
Steve Mucklef132c6c2012-06-06 18:30:57 -07002149 pm_runtime_resume(otg->phy->dev);
2150 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002151 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302152 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002153 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302154 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302155 psy = power_supply_get_by_name("usb");
2156 if (!psy)
2157 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002158 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302159 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2160 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002161 pm_runtime_put_noidle(otg->phy->dev);
2162 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302163 break;
2164 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302165 /* FALL THROUGH */
2166 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302167 if (test_bit(MHL, &motg->inputs)) {
2168 /* allow LPM */
2169 pm_runtime_put_noidle(otg->phy->dev);
2170 pm_runtime_suspend(otg->phy->dev);
2171 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302173 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302174 if (msm_chg_mhl_detect(motg)) {
2175 work = 1;
2176 break;
2177 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302178 clear_bit(B_BUS_REQ, &motg->inputs);
2179 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002180 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302181 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302182 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302183 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302184 switch (motg->chg_state) {
2185 case USB_CHG_STATE_UNDEFINED:
2186 msm_chg_detect_work(&motg->chg_work.work);
2187 break;
2188 case USB_CHG_STATE_DETECTED:
2189 switch (motg->chg_type) {
2190 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302191 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002192 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302193 /* fall through */
2194 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302195 msm_otg_notify_charger(motg,
2196 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002197 pm_runtime_put_noidle(otg->phy->dev);
2198 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302199 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302200 case USB_ACA_B_CHARGER:
2201 msm_otg_notify_charger(motg,
2202 IDEV_ACA_CHG_MAX);
2203 /*
2204 * (ID_B --> ID_C) PHY_ALT interrupt can
2205 * not be detected in LPM.
2206 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302207 break;
2208 case USB_CDP_CHARGER:
2209 msm_otg_notify_charger(motg,
2210 IDEV_CHG_MAX);
2211 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002212 otg->phy->state =
2213 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302214 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302215 case USB_ACA_C_CHARGER:
2216 msm_otg_notify_charger(motg,
2217 IDEV_ACA_CHG_MAX);
2218 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002219 otg->phy->state =
2220 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302221 break;
2222 case USB_SDP_CHARGER:
roy.park99cef212012-07-26 17:00:40 -07002223 msm_otg_notify_charger(motg,
2224 IDEV_CHG_MIN);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302225 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002226 otg->phy->state =
2227 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302228 break;
2229 default:
2230 break;
2231 }
2232 break;
2233 default:
2234 break;
2235 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302236 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2237 pr_debug("b_sess_end && b_bus_req\n");
2238 if (msm_otg_start_srp(otg) < 0) {
2239 clear_bit(B_BUS_REQ, &motg->inputs);
2240 work = 1;
2241 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302242 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002243 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302244 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2245 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302246 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302247 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302248 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302249 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2250 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302251 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002252 msm_otg_reset(otg->phy);
2253 pm_runtime_put_noidle(otg->phy->dev);
2254 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302255 }
2256 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302257 case OTG_STATE_B_SRP_INIT:
2258 if (!test_bit(ID, &motg->inputs) ||
2259 test_bit(ID_A, &motg->inputs) ||
2260 test_bit(ID_C, &motg->inputs) ||
2261 (test_bit(B_SESS_VLD, &motg->inputs) &&
2262 !test_bit(ID_B, &motg->inputs))) {
2263 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2264 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002265 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302266 /*
2267 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2268 * bits after SRP initiation.
2269 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002270 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302271 work = 1;
2272 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2273 pr_debug("b_srp_fail\n");
2274 pr_info("A-device did not respond to SRP\n");
2275 clear_bit(B_BUS_REQ, &motg->inputs);
2276 clear_bit(B_SRP_FAIL, &motg->tmouts);
2277 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002278 ulpi_write(otg->phy, 0x0, 0x98);
2279 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302280 motg->b_last_se0_sess = jiffies;
2281 work = 1;
2282 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302283 break;
2284 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302285 if (!test_bit(ID, &motg->inputs) ||
2286 test_bit(ID_A, &motg->inputs) ||
2287 test_bit(ID_B, &motg->inputs) ||
2288 !test_bit(B_SESS_VLD, &motg->inputs)) {
2289 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302290 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2291 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302292 msm_otg_notify_charger(motg, 0);
2293 srp_reqd = otg->gadget->otg_srp_reqd;
2294 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302295 if (test_bit(ID_B, &motg->inputs))
2296 clear_bit(ID_B, &motg->inputs);
2297 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002298 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302299 motg->b_last_se0_sess = jiffies;
2300 if (srp_reqd)
2301 msm_otg_start_timer(motg,
2302 TB_TST_SRP, B_TST_SRP);
2303 else
2304 work = 1;
2305 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2306 otg->gadget->b_hnp_enable &&
2307 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2308 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2309 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2310 /* D+ pullup should not be disconnected within 4msec
2311 * after A device suspends the bus. Otherwise PET will
2312 * fail the compliance test.
2313 */
2314 udelay(1000);
2315 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002316 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302317 /*
2318 * start HCD even before A-device enable
2319 * pull-up to meet HNP timings.
2320 */
2321 otg->host->is_b_host = 1;
2322 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002323 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2324 test_bit(B_SESS_VLD, &motg->inputs)) {
2325 pr_debug("a_bus_suspend && b_sess_vld\n");
2326 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002327 pm_runtime_put_noidle(otg->phy->dev);
2328 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002329 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002330 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302331 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002332 }
2333 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302334 case OTG_STATE_B_WAIT_ACON:
2335 if (!test_bit(ID, &motg->inputs) ||
2336 test_bit(ID_A, &motg->inputs) ||
2337 test_bit(ID_B, &motg->inputs) ||
2338 !test_bit(B_SESS_VLD, &motg->inputs)) {
2339 pr_debug("!id || id_a/b || !b_sess_vld\n");
2340 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302341 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302342 * A-device is physically disconnected during
2343 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302344 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302345 msm_otg_start_host(otg, 0);
2346 otg->host->is_b_host = 0;
2347
2348 clear_bit(B_BUS_REQ, &motg->inputs);
2349 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2350 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002351 otg->phy->state = OTG_STATE_B_IDLE;
2352 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302353 work = 1;
2354 } else if (test_bit(A_CONN, &motg->inputs)) {
2355 pr_debug("a_conn\n");
2356 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002357 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302358 /*
2359 * PET disconnects D+ pullup after reset is generated
2360 * by B device in B_HOST role which is not detected by
2361 * B device. As workaorund , start timer of 300msec
2362 * and stop timer if A device is enumerated else clear
2363 * A_CONN.
2364 */
2365 msm_otg_start_timer(motg, TB_TST_CONFIG,
2366 B_TST_CONFIG);
2367 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2368 pr_debug("b_ase0_brst_tmout\n");
2369 pr_info("B HNP fail:No response from A device\n");
2370 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002371 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302372 otg->host->is_b_host = 0;
2373 clear_bit(B_ASE0_BRST, &motg->tmouts);
2374 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2375 clear_bit(B_BUS_REQ, &motg->inputs);
2376 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002377 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302378 work = 1;
2379 } else if (test_bit(ID_C, &motg->inputs)) {
2380 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2381 }
2382 break;
2383 case OTG_STATE_B_HOST:
2384 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2385 !test_bit(A_CONN, &motg->inputs) ||
2386 !test_bit(B_SESS_VLD, &motg->inputs)) {
2387 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2388 clear_bit(A_CONN, &motg->inputs);
2389 clear_bit(B_BUS_REQ, &motg->inputs);
2390 msm_otg_start_host(otg, 0);
2391 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002392 otg->phy->state = OTG_STATE_B_IDLE;
2393 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302394 work = 1;
2395 } else if (test_bit(ID_C, &motg->inputs)) {
2396 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2397 }
2398 break;
2399 case OTG_STATE_A_IDLE:
2400 otg->default_a = 1;
2401 if (test_bit(ID, &motg->inputs) &&
2402 !test_bit(ID_A, &motg->inputs)) {
2403 pr_debug("id && !id_a\n");
2404 otg->default_a = 0;
2405 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002406 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302407 del_timer_sync(&motg->id_timer);
2408 msm_otg_link_reset(motg);
2409 msm_chg_enable_aca_intr(motg);
2410 msm_otg_notify_charger(motg, 0);
2411 work = 1;
2412 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2413 (test_bit(A_SRP_DET, &motg->inputs) ||
2414 test_bit(A_BUS_REQ, &motg->inputs))) {
2415 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2416
2417 clear_bit(A_SRP_DET, &motg->inputs);
2418 /* Disable SRP detection */
2419 writel_relaxed((readl_relaxed(USB_OTGSC) &
2420 ~OTGSC_INTSTS_MASK) &
2421 ~OTGSC_DPIE, USB_OTGSC);
2422
Steve Mucklef132c6c2012-06-06 18:30:57 -07002423 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302424 /* VBUS should not be supplied before end of SRP pulse
2425 * generated by PET, if not complaince test fail.
2426 */
2427 usleep_range(10000, 12000);
2428 /* ACA: ID_A: Stop charging untill enumeration */
2429 if (test_bit(ID_A, &motg->inputs))
2430 msm_otg_notify_charger(motg, 0);
2431 else
2432 msm_hsusb_vbus_power(motg, 1);
2433 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2434 } else {
2435 pr_debug("No session requested\n");
2436 clear_bit(A_BUS_DROP, &motg->inputs);
2437 if (test_bit(ID_A, &motg->inputs)) {
2438 msm_otg_notify_charger(motg,
2439 IDEV_ACA_CHG_MAX);
2440 } else if (!test_bit(ID, &motg->inputs)) {
2441 msm_otg_notify_charger(motg, 0);
2442 /*
2443 * A-device is not providing power on VBUS.
2444 * Enable SRP detection.
2445 */
2446 writel_relaxed(0x13, USB_USBMODE);
2447 writel_relaxed((readl_relaxed(USB_OTGSC) &
2448 ~OTGSC_INTSTS_MASK) |
2449 OTGSC_DPIE, USB_OTGSC);
2450 mb();
2451 }
2452 }
2453 break;
2454 case OTG_STATE_A_WAIT_VRISE:
2455 if ((test_bit(ID, &motg->inputs) &&
2456 !test_bit(ID_A, &motg->inputs)) ||
2457 test_bit(A_BUS_DROP, &motg->inputs) ||
2458 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2459 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2460 clear_bit(A_BUS_REQ, &motg->inputs);
2461 msm_otg_del_timer(motg);
2462 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002463 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302464 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2465 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2466 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002467 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302468 if (TA_WAIT_BCON > 0)
2469 msm_otg_start_timer(motg, TA_WAIT_BCON,
2470 A_WAIT_BCON);
2471 msm_otg_start_host(otg, 1);
2472 msm_chg_enable_aca_det(motg);
2473 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302474 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302475 if (msm_chg_check_aca_intr(motg))
2476 work = 1;
2477 }
2478 break;
2479 case OTG_STATE_A_WAIT_BCON:
2480 if ((test_bit(ID, &motg->inputs) &&
2481 !test_bit(ID_A, &motg->inputs)) ||
2482 test_bit(A_BUS_DROP, &motg->inputs) ||
2483 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2484 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2485 "a_wait_bcon_tmout\n");
2486 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2487 pr_info("Device No Response\n");
2488 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2489 }
2490 msm_otg_del_timer(motg);
2491 clear_bit(A_BUS_REQ, &motg->inputs);
2492 clear_bit(B_CONN, &motg->inputs);
2493 msm_otg_start_host(otg, 0);
2494 /*
2495 * ACA: ID_A with NO accessory, just the A plug is
2496 * attached to ACA: Use IDCHG_MAX for charging
2497 */
2498 if (test_bit(ID_A, &motg->inputs))
2499 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2500 else
2501 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002502 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302503 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2504 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2505 pr_debug("!a_vbus_vld\n");
2506 clear_bit(B_CONN, &motg->inputs);
2507 msm_otg_del_timer(motg);
2508 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002509 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2510 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302511 } else if (test_bit(ID_A, &motg->inputs)) {
2512 msm_hsusb_vbus_power(motg, 0);
2513 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2514 /*
2515 * If TA_WAIT_BCON is infinite, we don;t
2516 * turn off VBUS. Enter low power mode.
2517 */
2518 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002519 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302520 } else if (!test_bit(ID, &motg->inputs)) {
2521 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302522 }
2523 break;
2524 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302525 if ((test_bit(ID, &motg->inputs) &&
2526 !test_bit(ID_A, &motg->inputs)) ||
2527 test_bit(A_BUS_DROP, &motg->inputs)) {
2528 pr_debug("id_a/b/c || a_bus_drop\n");
2529 clear_bit(B_CONN, &motg->inputs);
2530 clear_bit(A_BUS_REQ, &motg->inputs);
2531 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002532 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302533 msm_otg_start_host(otg, 0);
2534 if (!test_bit(ID_A, &motg->inputs))
2535 msm_hsusb_vbus_power(motg, 0);
2536 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2537 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2538 pr_debug("!a_vbus_vld\n");
2539 clear_bit(B_CONN, &motg->inputs);
2540 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002541 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302542 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002543 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302544 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2545 /*
2546 * a_bus_req is de-asserted when root hub is
2547 * suspended or HNP is in progress.
2548 */
2549 pr_debug("!a_bus_req\n");
2550 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002551 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302552 if (otg->host->b_hnp_enable)
2553 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2554 A_AIDL_BDIS);
2555 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002556 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302557 } else if (!test_bit(B_CONN, &motg->inputs)) {
2558 pr_debug("!b_conn\n");
2559 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002560 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302561 if (TA_WAIT_BCON > 0)
2562 msm_otg_start_timer(motg, TA_WAIT_BCON,
2563 A_WAIT_BCON);
2564 if (msm_chg_check_aca_intr(motg))
2565 work = 1;
2566 } else if (test_bit(ID_A, &motg->inputs)) {
2567 msm_otg_del_timer(motg);
2568 msm_hsusb_vbus_power(motg, 0);
2569 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2570 msm_otg_notify_charger(motg,
2571 IDEV_ACA_CHG_MAX);
2572 else
2573 msm_otg_notify_charger(motg,
2574 IDEV_CHG_MIN - motg->mA_port);
2575 } else if (!test_bit(ID, &motg->inputs)) {
2576 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2577 motg->chg_type = USB_INVALID_CHARGER;
2578 msm_otg_notify_charger(motg, 0);
2579 msm_hsusb_vbus_power(motg, 1);
2580 }
2581 break;
2582 case OTG_STATE_A_SUSPEND:
2583 if ((test_bit(ID, &motg->inputs) &&
2584 !test_bit(ID_A, &motg->inputs)) ||
2585 test_bit(A_BUS_DROP, &motg->inputs) ||
2586 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2587 pr_debug("id_a/b/c || a_bus_drop ||"
2588 "a_aidl_bdis_tmout\n");
2589 msm_otg_del_timer(motg);
2590 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002591 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302592 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002593 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302594 if (!test_bit(ID_A, &motg->inputs))
2595 msm_hsusb_vbus_power(motg, 0);
2596 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2597 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2598 pr_debug("!a_vbus_vld\n");
2599 msm_otg_del_timer(motg);
2600 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002601 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302602 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002603 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302604 } else if (!test_bit(B_CONN, &motg->inputs) &&
2605 otg->host->b_hnp_enable) {
2606 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002607 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302608 msm_otg_host_hnp_enable(otg, 1);
2609 otg->gadget->is_a_peripheral = 1;
2610 msm_otg_start_peripheral(otg, 1);
2611 } else if (!test_bit(B_CONN, &motg->inputs) &&
2612 !otg->host->b_hnp_enable) {
2613 pr_debug("!b_conn && !b_hnp_enable");
2614 /*
2615 * bus request is dropped during suspend.
2616 * acquire again for next device.
2617 */
2618 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002619 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302620 if (TA_WAIT_BCON > 0)
2621 msm_otg_start_timer(motg, TA_WAIT_BCON,
2622 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002623 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302624 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002625 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302626 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002627 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002628 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302629 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302630 }
2631 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302632 case OTG_STATE_A_PERIPHERAL:
2633 if ((test_bit(ID, &motg->inputs) &&
2634 !test_bit(ID_A, &motg->inputs)) ||
2635 test_bit(A_BUS_DROP, &motg->inputs)) {
2636 pr_debug("id _f/b/c || a_bus_drop\n");
2637 /* Clear BIDL_ADIS timer */
2638 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002639 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302640 msm_otg_start_peripheral(otg, 0);
2641 otg->gadget->is_a_peripheral = 0;
2642 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002643 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302644 if (!test_bit(ID_A, &motg->inputs))
2645 msm_hsusb_vbus_power(motg, 0);
2646 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2647 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2648 pr_debug("!a_vbus_vld\n");
2649 /* Clear BIDL_ADIS timer */
2650 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002651 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302652 msm_otg_start_peripheral(otg, 0);
2653 otg->gadget->is_a_peripheral = 0;
2654 msm_otg_start_host(otg, 0);
2655 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2656 pr_debug("a_bidl_adis_tmout\n");
2657 msm_otg_start_peripheral(otg, 0);
2658 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002659 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302660 set_bit(A_BUS_REQ, &motg->inputs);
2661 msm_otg_host_hnp_enable(otg, 0);
2662 if (TA_WAIT_BCON > 0)
2663 msm_otg_start_timer(motg, TA_WAIT_BCON,
2664 A_WAIT_BCON);
2665 } else if (test_bit(ID_A, &motg->inputs)) {
2666 msm_hsusb_vbus_power(motg, 0);
2667 msm_otg_notify_charger(motg,
2668 IDEV_CHG_MIN - motg->mA_port);
2669 } else if (!test_bit(ID, &motg->inputs)) {
2670 msm_otg_notify_charger(motg, 0);
2671 msm_hsusb_vbus_power(motg, 1);
2672 }
2673 break;
2674 case OTG_STATE_A_WAIT_VFALL:
2675 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2676 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002677 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302678 work = 1;
2679 }
2680 break;
2681 case OTG_STATE_A_VBUS_ERR:
2682 if ((test_bit(ID, &motg->inputs) &&
2683 !test_bit(ID_A, &motg->inputs)) ||
2684 test_bit(A_BUS_DROP, &motg->inputs) ||
2685 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002686 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302687 if (!test_bit(ID_A, &motg->inputs))
2688 msm_hsusb_vbus_power(motg, 0);
2689 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2690 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2691 motg->chg_type = USB_INVALID_CHARGER;
2692 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302693 }
2694 break;
2695 default:
2696 break;
2697 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302698 if (work)
2699 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302700}
2701
2702static irqreturn_t msm_otg_irq(int irq, void *data)
2703{
2704 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002705 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302706 u32 otgsc = 0, usbsts, pc;
2707 bool work = 0;
2708 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302709
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302710 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302711 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302712 disable_irq_nosync(irq);
2713 motg->async_int = 1;
Jack Pham5ca279b2012-05-14 18:42:54 -07002714 if (atomic_read(&motg->pm_suspended))
2715 motg->sm_work_pending = true;
2716 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002717 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302718 return IRQ_HANDLED;
2719 }
2720
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002721 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302722 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302723
2724 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302725 return IRQ_NONE;
2726
2727 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302728 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302729 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302730 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302731 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302732 pr_debug("Id clear\n");
2733 /*
2734 * Assert a_bus_req to supply power on
2735 * VBUS when Micro/Mini-A cable is connected
2736 * with out user intervention.
2737 */
2738 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302739 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302740 msm_chg_enable_aca_det(motg);
2741 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302742 writel_relaxed(otgsc, USB_OTGSC);
2743 work = 1;
2744 } else if (otgsc & OTGSC_DPIS) {
2745 pr_debug("DPIS detected\n");
2746 writel_relaxed(otgsc, USB_OTGSC);
2747 set_bit(A_SRP_DET, &motg->inputs);
2748 set_bit(A_BUS_REQ, &motg->inputs);
2749 work = 1;
2750 } else if (otgsc & OTGSC_BSVIS) {
2751 writel_relaxed(otgsc, USB_OTGSC);
2752 /*
2753 * BSV interrupt comes when operating as an A-device
2754 * (VBUS on/off).
2755 * But, handle BSV when charger is removed from ACA in ID_A
2756 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002757 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302758 !test_bit(ID_A, &motg->inputs))
2759 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302760 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302761 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302762 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302763 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302764 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302765 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002766 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2767
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302768 msm_chg_check_aca_intr(motg);
2769 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302770 work = 1;
2771 } else if (usbsts & STS_PCI) {
2772 pc = readl_relaxed(USB_PORTSC);
2773 pr_debug("portsc = %x\n", pc);
2774 ret = IRQ_NONE;
2775 /*
2776 * HCD Acks PCI interrupt. We use this to switch
2777 * between different OTG states.
2778 */
2779 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002780 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302781 case OTG_STATE_A_SUSPEND:
2782 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2783 !(pc & PORTSC_CCS)) {
2784 pr_debug("B_CONN clear\n");
2785 clear_bit(B_CONN, &motg->inputs);
2786 msm_otg_del_timer(motg);
2787 }
2788 break;
2789 case OTG_STATE_A_PERIPHERAL:
2790 /*
2791 * A-peripheral observed activity on bus.
2792 * clear A_BIDL_ADIS timer.
2793 */
2794 msm_otg_del_timer(motg);
2795 work = 0;
2796 break;
2797 case OTG_STATE_B_WAIT_ACON:
2798 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2799 pr_debug("A_CONN set\n");
2800 set_bit(A_CONN, &motg->inputs);
2801 /* Clear ASE0_BRST timer */
2802 msm_otg_del_timer(motg);
2803 }
2804 break;
2805 case OTG_STATE_B_HOST:
2806 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2807 pr_debug("A_CONN clear\n");
2808 clear_bit(A_CONN, &motg->inputs);
2809 msm_otg_del_timer(motg);
2810 }
2811 break;
2812 case OTG_STATE_A_WAIT_BCON:
2813 if (TA_WAIT_BCON < 0)
2814 set_bit(A_BUS_REQ, &motg->inputs);
2815 default:
2816 work = 0;
2817 break;
2818 }
2819 } else if (usbsts & STS_URI) {
2820 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002821 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302822 case OTG_STATE_A_PERIPHERAL:
2823 /*
2824 * A-peripheral observed activity on bus.
2825 * clear A_BIDL_ADIS timer.
2826 */
2827 msm_otg_del_timer(motg);
2828 work = 0;
2829 break;
2830 default:
2831 work = 0;
2832 break;
2833 }
2834 } else if (usbsts & STS_SLI) {
2835 ret = IRQ_NONE;
2836 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002837 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302838 case OTG_STATE_B_PERIPHERAL:
2839 if (otg->gadget->b_hnp_enable) {
2840 set_bit(A_BUS_SUSPEND, &motg->inputs);
2841 set_bit(B_BUS_REQ, &motg->inputs);
2842 work = 1;
2843 }
2844 break;
2845 case OTG_STATE_A_PERIPHERAL:
2846 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2847 A_BIDL_ADIS);
2848 break;
2849 default:
2850 break;
2851 }
2852 } else if ((usbsts & PHY_ALT_INT)) {
2853 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2854 if (msm_chg_check_aca_intr(motg))
2855 work = 1;
2856 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302857 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302858 if (work)
2859 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302860
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302861 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002862}
2863
2864static void msm_otg_set_vbus_state(int online)
2865{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302866 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002867 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05302868 struct usb_otg *otg = motg->phy.otg;
2869
2870 /* In A Host Mode, ignore received BSV interrupts */
2871 if (otg->phy->state >= OTG_STATE_A_IDLE)
2872 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002873
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302874 if (online) {
2875 pr_debug("PMIC: BSV set\n");
2876 set_bit(B_SESS_VLD, &motg->inputs);
2877 } else {
2878 pr_debug("PMIC: BSV clear\n");
2879 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302880 }
2881
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302882 if (!init) {
2883 init = true;
2884 complete(&pmic_vbus_init);
2885 pr_debug("PMIC: BSV init complete\n");
2886 return;
2887 }
2888
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302889 if (test_bit(MHL, &motg->inputs) ||
2890 mhl_det_in_progress) {
2891 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
2892 return;
2893 }
2894
Jack Pham5ca279b2012-05-14 18:42:54 -07002895 if (atomic_read(&motg->pm_suspended))
2896 motg->sm_work_pending = true;
2897 else
2898 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002899}
2900
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302901static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002902{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302903 struct msm_otg *motg = container_of(w, struct msm_otg,
2904 pmic_id_status_work.work);
2905 int work = 0;
2906 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002907
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302908 local_irq_save(flags);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302909 if (irq_read_line(motg->pdata->pmic_id_irq)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302910 if (!test_and_set_bit(ID, &motg->inputs)) {
2911 pr_debug("PMIC: ID set\n");
2912 work = 1;
2913 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302914 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302915 if (test_and_clear_bit(ID, &motg->inputs)) {
2916 pr_debug("PMIC: ID clear\n");
2917 set_bit(A_BUS_REQ, &motg->inputs);
2918 work = 1;
2919 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302920 }
2921
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302922 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07002923 if (atomic_read(&motg->pm_suspended))
2924 motg->sm_work_pending = true;
2925 else
2926 queue_work(system_nrt_wq, &motg->sm_work);
2927 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302928 local_irq_restore(flags);
2929
2930}
2931
2932#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
2933static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2934{
2935 struct msm_otg *motg = data;
2936
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302937 if (test_bit(MHL, &motg->inputs) ||
2938 mhl_det_in_progress) {
2939 pr_debug("PMIC: Id interrupt ignored in MHL\n");
2940 return IRQ_HANDLED;
2941 }
2942
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302943 if (!aca_id_turned_on)
2944 /*schedule delayed work for 5msec for ID line state to settle*/
2945 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
2946 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002947
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302948 return IRQ_HANDLED;
2949}
2950
2951static int msm_otg_mode_show(struct seq_file *s, void *unused)
2952{
2953 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002954 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302955
Steve Mucklef132c6c2012-06-06 18:30:57 -07002956 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302957 case OTG_STATE_A_HOST:
2958 seq_printf(s, "host\n");
2959 break;
2960 case OTG_STATE_B_PERIPHERAL:
2961 seq_printf(s, "peripheral\n");
2962 break;
2963 default:
2964 seq_printf(s, "none\n");
2965 break;
2966 }
2967
2968 return 0;
2969}
2970
2971static int msm_otg_mode_open(struct inode *inode, struct file *file)
2972{
2973 return single_open(file, msm_otg_mode_show, inode->i_private);
2974}
2975
2976static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2977 size_t count, loff_t *ppos)
2978{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302979 struct seq_file *s = file->private_data;
2980 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302981 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07002982 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302983 int status = count;
2984 enum usb_mode_type req_mode;
2985
2986 memset(buf, 0x00, sizeof(buf));
2987
2988 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2989 status = -EFAULT;
2990 goto out;
2991 }
2992
2993 if (!strncmp(buf, "host", 4)) {
2994 req_mode = USB_HOST;
2995 } else if (!strncmp(buf, "peripheral", 10)) {
2996 req_mode = USB_PERIPHERAL;
2997 } else if (!strncmp(buf, "none", 4)) {
2998 req_mode = USB_NONE;
2999 } else {
3000 status = -EINVAL;
3001 goto out;
3002 }
3003
3004 switch (req_mode) {
3005 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003006 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303007 case OTG_STATE_A_HOST:
3008 case OTG_STATE_B_PERIPHERAL:
3009 set_bit(ID, &motg->inputs);
3010 clear_bit(B_SESS_VLD, &motg->inputs);
3011 break;
3012 default:
3013 goto out;
3014 }
3015 break;
3016 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003017 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303018 case OTG_STATE_B_IDLE:
3019 case OTG_STATE_A_HOST:
3020 set_bit(ID, &motg->inputs);
3021 set_bit(B_SESS_VLD, &motg->inputs);
3022 break;
3023 default:
3024 goto out;
3025 }
3026 break;
3027 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003028 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303029 case OTG_STATE_B_IDLE:
3030 case OTG_STATE_B_PERIPHERAL:
3031 clear_bit(ID, &motg->inputs);
3032 break;
3033 default:
3034 goto out;
3035 }
3036 break;
3037 default:
3038 goto out;
3039 }
3040
Steve Mucklef132c6c2012-06-06 18:30:57 -07003041 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303042 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303043out:
3044 return status;
3045}
3046
3047const struct file_operations msm_otg_mode_fops = {
3048 .open = msm_otg_mode_open,
3049 .read = seq_read,
3050 .write = msm_otg_mode_write,
3051 .llseek = seq_lseek,
3052 .release = single_release,
3053};
3054
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303055static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3056{
3057 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003058 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303059
Steve Mucklef132c6c2012-06-06 18:30:57 -07003060 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303061 return 0;
3062}
3063
3064static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3065{
3066 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3067}
3068
3069const struct file_operations msm_otg_state_fops = {
3070 .open = msm_otg_otg_state_open,
3071 .read = seq_read,
3072 .llseek = seq_lseek,
3073 .release = single_release,
3074};
3075
Anji jonnalad270e2d2011-08-09 11:28:32 +05303076static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3077{
3078 struct msm_otg *motg = s->private;
3079
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303080 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303081 return 0;
3082}
3083
3084static int msm_otg_chg_open(struct inode *inode, struct file *file)
3085{
3086 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3087}
3088
3089const struct file_operations msm_otg_chg_fops = {
3090 .open = msm_otg_chg_open,
3091 .read = seq_read,
3092 .llseek = seq_lseek,
3093 .release = single_release,
3094};
3095
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303096static int msm_otg_aca_show(struct seq_file *s, void *unused)
3097{
3098 if (debug_aca_enabled)
3099 seq_printf(s, "enabled\n");
3100 else
3101 seq_printf(s, "disabled\n");
3102
3103 return 0;
3104}
3105
3106static int msm_otg_aca_open(struct inode *inode, struct file *file)
3107{
3108 return single_open(file, msm_otg_aca_show, inode->i_private);
3109}
3110
3111static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3112 size_t count, loff_t *ppos)
3113{
3114 char buf[8];
3115
3116 memset(buf, 0x00, sizeof(buf));
3117
3118 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3119 return -EFAULT;
3120
3121 if (!strncmp(buf, "enable", 6))
3122 debug_aca_enabled = true;
3123 else
3124 debug_aca_enabled = false;
3125
3126 return count;
3127}
3128
3129const struct file_operations msm_otg_aca_fops = {
3130 .open = msm_otg_aca_open,
3131 .read = seq_read,
3132 .write = msm_otg_aca_write,
3133 .llseek = seq_lseek,
3134 .release = single_release,
3135};
3136
Manu Gautam8bdcc592012-03-06 11:26:06 +05303137static int msm_otg_bus_show(struct seq_file *s, void *unused)
3138{
3139 if (debug_bus_voting_enabled)
3140 seq_printf(s, "enabled\n");
3141 else
3142 seq_printf(s, "disabled\n");
3143
3144 return 0;
3145}
3146
3147static int msm_otg_bus_open(struct inode *inode, struct file *file)
3148{
3149 return single_open(file, msm_otg_bus_show, inode->i_private);
3150}
3151
3152static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3153 size_t count, loff_t *ppos)
3154{
3155 char buf[8];
3156 int ret;
3157 struct seq_file *s = file->private_data;
3158 struct msm_otg *motg = s->private;
3159
3160 memset(buf, 0x00, sizeof(buf));
3161
3162 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3163 return -EFAULT;
3164
3165 if (!strncmp(buf, "enable", 6)) {
3166 /* Do not vote here. Let OTG statemachine decide when to vote */
3167 debug_bus_voting_enabled = true;
3168 } else {
3169 debug_bus_voting_enabled = false;
3170 if (motg->bus_perf_client) {
3171 ret = msm_bus_scale_client_update_request(
3172 motg->bus_perf_client, 0);
3173 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003174 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303175 "for bus bw %d\n", __func__, ret);
3176 }
3177 }
3178
3179 return count;
3180}
3181
3182const struct file_operations msm_otg_bus_fops = {
3183 .open = msm_otg_bus_open,
3184 .read = seq_read,
3185 .write = msm_otg_bus_write,
3186 .llseek = seq_lseek,
3187 .release = single_release,
3188};
3189
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303190static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303191
3192static int msm_otg_debugfs_init(struct msm_otg *motg)
3193{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303194 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303195
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303196 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3197
3198 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3199 return -ENODEV;
3200
Anji jonnalad270e2d2011-08-09 11:28:32 +05303201 if (motg->pdata->mode == USB_OTG &&
3202 motg->pdata->otg_control == OTG_USER_CONTROL) {
3203
Manu Gautam8bdcc592012-03-06 11:26:06 +05303204 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303205 S_IWUSR, msm_otg_dbg_root, motg,
3206 &msm_otg_mode_fops);
3207
Manu Gautam8bdcc592012-03-06 11:26:06 +05303208 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303209 debugfs_remove(msm_otg_dbg_root);
3210 msm_otg_dbg_root = NULL;
3211 return -ENODEV;
3212 }
3213 }
3214
Manu Gautam8bdcc592012-03-06 11:26:06 +05303215 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303216 msm_otg_dbg_root, motg,
3217 &msm_otg_chg_fops);
3218
Manu Gautam8bdcc592012-03-06 11:26:06 +05303219 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303220 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303221 return -ENODEV;
3222 }
3223
Manu Gautam8bdcc592012-03-06 11:26:06 +05303224 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303225 msm_otg_dbg_root, motg,
3226 &msm_otg_aca_fops);
3227
Manu Gautam8bdcc592012-03-06 11:26:06 +05303228 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303229 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303230 return -ENODEV;
3231 }
3232
Manu Gautam8bdcc592012-03-06 11:26:06 +05303233 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3234 msm_otg_dbg_root, motg,
3235 &msm_otg_bus_fops);
3236
3237 if (!msm_otg_dentry) {
3238 debugfs_remove_recursive(msm_otg_dbg_root);
3239 return -ENODEV;
3240 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303241
3242 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3243 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3244
3245 if (!msm_otg_dentry) {
3246 debugfs_remove_recursive(msm_otg_dbg_root);
3247 return -ENODEV;
3248 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303249 return 0;
3250}
3251
3252static void msm_otg_debugfs_cleanup(void)
3253{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303254 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303255}
3256
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303257static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3258static struct platform_device *msm_otg_add_pdev(
3259 struct platform_device *ofdev, const char *name)
3260{
3261 struct platform_device *pdev;
3262 const struct resource *res = ofdev->resource;
3263 unsigned int num = ofdev->num_resources;
3264 int retval;
3265
3266 pdev = platform_device_alloc(name, -1);
3267 if (!pdev) {
3268 retval = -ENOMEM;
3269 goto error;
3270 }
3271
3272 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3273 pdev->dev.dma_mask = &msm_otg_dma_mask;
3274
3275 if (num) {
3276 retval = platform_device_add_resources(pdev, res, num);
3277 if (retval)
3278 goto error;
3279 }
3280
3281 retval = platform_device_add(pdev);
3282 if (retval)
3283 goto error;
3284
3285 return pdev;
3286
3287error:
3288 platform_device_put(pdev);
3289 return ERR_PTR(retval);
3290}
3291
3292static int msm_otg_setup_devices(struct platform_device *ofdev,
3293 enum usb_mode_type mode, bool init)
3294{
3295 const char *gadget_name = "msm_hsusb";
3296 const char *host_name = "msm_hsusb_host";
3297 static struct platform_device *gadget_pdev;
3298 static struct platform_device *host_pdev;
3299 int retval = 0;
3300
3301 if (!init) {
3302 if (gadget_pdev)
3303 platform_device_unregister(gadget_pdev);
3304 if (host_pdev)
3305 platform_device_unregister(host_pdev);
3306 return 0;
3307 }
3308
3309 switch (mode) {
3310 case USB_OTG:
3311 /* fall through */
3312 case USB_PERIPHERAL:
3313 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3314 if (IS_ERR(gadget_pdev)) {
3315 retval = PTR_ERR(gadget_pdev);
3316 break;
3317 }
3318 if (mode == USB_PERIPHERAL)
3319 break;
3320 /* fall through */
3321 case USB_HOST:
3322 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3323 if (IS_ERR(host_pdev)) {
3324 retval = PTR_ERR(host_pdev);
3325 if (mode == USB_OTG)
3326 platform_device_unregister(gadget_pdev);
3327 }
3328 break;
3329 default:
3330 break;
3331 }
3332
3333 return retval;
3334}
3335
3336struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3337{
3338 struct device_node *node = pdev->dev.of_node;
3339 struct msm_otg_platform_data *pdata;
3340 int len = 0;
3341
3342 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3343 if (!pdata) {
3344 pr_err("unable to allocate platform data\n");
3345 return NULL;
3346 }
3347 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3348 if (len) {
3349 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3350 if (!pdata->phy_init_seq)
3351 return NULL;
3352 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3353 pdata->phy_init_seq,
3354 len/sizeof(*pdata->phy_init_seq));
3355 }
3356 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3357 &pdata->power_budget);
3358 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3359 &pdata->mode);
3360 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3361 &pdata->otg_control);
3362 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3363 &pdata->default_mode);
3364 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3365 &pdata->phy_type);
3366 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3367 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303368 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303369}
3370
3371static int __init msm_otg_probe(struct platform_device *pdev)
3372{
3373 int ret = 0;
3374 struct resource *res;
3375 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003376 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303377 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303378
3379 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303380
3381 if (pdev->dev.of_node) {
3382 dev_dbg(&pdev->dev, "device tree enabled\n");
3383 pdata = msm_otg_dt_to_pdata(pdev);
3384 if (!pdata)
3385 return -ENOMEM;
3386 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3387 if (ret) {
3388 dev_err(&pdev->dev, "devices setup failed\n");
3389 return ret;
3390 }
3391 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303392 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3393 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303394 } else {
3395 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303396 }
3397
3398 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3399 if (!motg) {
3400 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3401 return -ENOMEM;
3402 }
3403
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003404 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3405 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003406 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3407 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303408 goto free_motg;
3409 }
3410
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003411 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303412 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003413 phy = &motg->phy;
3414 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303415
3416 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303417 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3418 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3419 * PMIC can detect ACA ID_GND and generate an interrupt.
3420 */
3421 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3422 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3423 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003424 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303425 }
3426
Ofir Cohen4da266f2012-01-03 10:19:29 +02003427 /* initialize reset counter */
3428 motg->reset_counter = 0;
3429
Amit Blay02eff132011-09-21 16:46:24 +03003430 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003431 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003432 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003433 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303434
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303435 /*
3436 * Targets on which link uses asynchronous reset methodology,
3437 * free running clock is not required during the reset.
3438 */
Manu Gautam5143b252012-01-05 19:25:23 -08003439 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303440 if (IS_ERR(motg->clk))
3441 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3442 else
3443 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303444
3445 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003446 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303447 * CORE CLK must be running at >55Mhz for correct HSUSB
3448 * operation and USB core cannot tolerate frequency changes on
3449 * CORE CLK. For such USB cores, vote for maximum clk frequency
3450 * on pclk source
3451 */
Manu Gautam5143b252012-01-05 19:25:23 -08003452 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3453 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303454 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003455 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303456 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003457 goto put_clk;
3458 }
3459 clk_set_rate(motg->core_clk, INT_MAX);
3460
3461 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3462 if (IS_ERR(motg->pclk)) {
3463 dev_err(&pdev->dev, "failed to get iface_clk\n");
3464 ret = PTR_ERR(motg->pclk);
3465 goto put_core_clk;
3466 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303467
3468 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3469 if (!res) {
3470 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3471 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003472 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303473 }
3474
3475 motg->regs = ioremap(res->start, resource_size(res));
3476 if (!motg->regs) {
3477 dev_err(&pdev->dev, "ioremap failed\n");
3478 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003479 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303480 }
3481 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3482
3483 motg->irq = platform_get_irq(pdev, 0);
3484 if (!motg->irq) {
3485 dev_err(&pdev->dev, "platform_get_irq failed\n");
3486 ret = -ENODEV;
3487 goto free_regs;
3488 }
3489
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003490 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003491 if (IS_ERR(motg->xo_handle)) {
3492 dev_err(&pdev->dev, "%s not able to get the handle "
3493 "to vote for TCXO D0 buffer\n", __func__);
3494 ret = PTR_ERR(motg->xo_handle);
3495 goto free_regs;
3496 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303497
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003498 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003499 if (ret) {
3500 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3501 "D0 buffer%d\n", __func__, ret);
3502 goto free_xo_handle;
3503 }
3504
Manu Gautam28b1bac2012-01-30 16:43:06 +05303505 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303506
Mayank Rana248698c2012-04-19 00:03:16 +05303507 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003508 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303509 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003510 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303511 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003512 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003513 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303514 goto devote_xo_handle;
3515 }
3516 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303517 }
3518
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003519 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303520 if (ret) {
3521 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303522 goto devote_xo_handle;
3523 }
3524
3525 ret = regulator_enable(hsusb_vddcx);
3526 if (ret) {
3527 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3528 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303529 }
3530
3531 ret = msm_hsusb_ldo_init(motg, 1);
3532 if (ret) {
3533 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303534 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303535 }
3536
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303537 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303538 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3539 "mhl_usb_hs_switch");
3540 if (IS_ERR(mhl_usb_hs_switch)) {
3541 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003542 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303543 goto free_ldo_init;
3544 }
3545 }
3546
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003547 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303548 if (ret) {
3549 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003550 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303551 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303552 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303553
3554 writel(0, USB_USBINTR);
3555 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003556 /* Ensure that above STOREs are completed before enabling interrupts */
3557 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303558
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303559 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3560 if (ret)
3561 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003562 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303563 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303564 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303565 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303566 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303567 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3568 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303569 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3570 "msm_otg", motg);
3571 if (ret) {
3572 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003573 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303574 }
3575
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003576 phy->init = msm_otg_reset;
3577 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003578 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303579
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003580 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303581
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003582 phy->otg->phy = &motg->phy;
3583 phy->otg->set_host = msm_otg_set_host;
3584 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003585 phy->otg->start_hnp = msm_otg_start_hnp;
3586 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003587
3588 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303589 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003590 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303591 goto free_irq;
3592 }
3593
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303594 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303595 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003596 if (motg->pdata->pmic_id_irq) {
3597 ret = request_irq(motg->pdata->pmic_id_irq,
3598 msm_pmic_id_irq,
3599 IRQF_TRIGGER_RISING |
3600 IRQF_TRIGGER_FALLING,
3601 "msm_otg", motg);
3602 if (ret) {
3603 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003604 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003605 }
3606 } else {
3607 ret = -ENODEV;
3608 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003609 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003610 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303611 }
3612
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303613 msm_hsusb_mhl_switch_enable(motg, 1);
3614
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303615 platform_set_drvdata(pdev, motg);
3616 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003617 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303618
Anji jonnalad270e2d2011-08-09 11:28:32 +05303619 ret = msm_otg_debugfs_init(motg);
3620 if (ret)
3621 dev_dbg(&pdev->dev, "mode debugfs file is"
3622 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303623
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003624 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3625 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3626
Amit Blay58b31472011-11-18 09:39:39 +02003627 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3628 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303629 (!(motg->pdata->mode == USB_OTG) ||
3630 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003631 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303632 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003633
Amit Blay58b31472011-11-18 09:39:39 +02003634 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3635 motg->caps = ALLOW_PHY_RETENTION;
3636 }
3637
Amit Blay6fa647a2012-05-24 14:12:08 +03003638 if (motg->pdata->enable_lpm_on_dev_suspend)
3639 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3640
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003641 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303642 pm_runtime_set_active(&pdev->dev);
3643 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303644
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303645 if (motg->pdata->bus_scale_table) {
3646 motg->bus_perf_client =
3647 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3648 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003649 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303650 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303651 else
3652 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303653 }
3654
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303655 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003656
Steve Mucklef132c6c2012-06-06 18:30:57 -07003657remove_phy:
3658 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303659free_irq:
3660 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003661destroy_wlock:
3662 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303663 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003664 msm_hsusb_ldo_enable(motg, 0);
3665free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303666 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303667free_hsusb_vddcx:
3668 regulator_disable(hsusb_vddcx);
3669free_config_vddcx:
3670 regulator_set_voltage(hsusb_vddcx,
3671 vdd_val[motg->vdd_type][VDD_NONE],
3672 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003673devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303674 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003675 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003676free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003677 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303678free_regs:
3679 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003680put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303681 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303682put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003683 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303684put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303685 if (!IS_ERR(motg->clk))
3686 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003687 if (!IS_ERR(motg->phy_reset_clk))
3688 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003689free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003690 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303691free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303692 kfree(motg);
3693 return ret;
3694}
3695
3696static int __devexit msm_otg_remove(struct platform_device *pdev)
3697{
3698 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003699 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303700 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303701
3702 if (otg->host || otg->gadget)
3703 return -EBUSY;
3704
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303705 if (pdev->dev.of_node)
3706 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003707 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3708 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303709 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303710 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303711 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303712 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303713 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303714
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303715 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303716
3717 device_init_wakeup(&pdev->dev, 0);
3718 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003719 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303720
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303721 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003722 if (motg->pdata->pmic_id_irq)
3723 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003724 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303725 free_irq(motg->irq, motg);
3726
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303727 /*
3728 * Put PHY in low power mode.
3729 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003730 ulpi_read(otg->phy, 0x14);
3731 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303732
3733 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3734 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3735 if (readl(USB_PORTSC) & PORTSC_PHCD)
3736 break;
3737 udelay(1);
3738 cnt++;
3739 }
3740 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003741 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303742
Manu Gautam28b1bac2012-01-30 16:43:06 +05303743 clk_disable_unprepare(motg->pclk);
3744 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003745 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003746 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303747 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303748 regulator_disable(hsusb_vddcx);
3749 regulator_set_voltage(hsusb_vddcx,
3750 vdd_val[motg->vdd_type][VDD_NONE],
3751 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303752
3753 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303754 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303755
Amit Blay02eff132011-09-21 16:46:24 +03003756 if (!IS_ERR(motg->phy_reset_clk))
3757 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303758 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303759 if (!IS_ERR(motg->clk))
3760 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003761 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303762
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303763 if (motg->bus_perf_client)
3764 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303765
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003766 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303767 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303768 return 0;
3769}
3770
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303771#ifdef CONFIG_PM_RUNTIME
3772static int msm_otg_runtime_idle(struct device *dev)
3773{
3774 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003775 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303776
3777 dev_dbg(dev, "OTG runtime idle\n");
3778
Steve Mucklef132c6c2012-06-06 18:30:57 -07003779 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303780 return -EAGAIN;
3781 else
3782 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303783}
3784
3785static int msm_otg_runtime_suspend(struct device *dev)
3786{
3787 struct msm_otg *motg = dev_get_drvdata(dev);
3788
3789 dev_dbg(dev, "OTG runtime suspend\n");
3790 return msm_otg_suspend(motg);
3791}
3792
3793static int msm_otg_runtime_resume(struct device *dev)
3794{
3795 struct msm_otg *motg = dev_get_drvdata(dev);
3796
3797 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303798 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303799 return msm_otg_resume(motg);
3800}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303801#endif
3802
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303803#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303804static int msm_otg_pm_suspend(struct device *dev)
3805{
Jack Pham5ca279b2012-05-14 18:42:54 -07003806 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303807 struct msm_otg *motg = dev_get_drvdata(dev);
3808
3809 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003810
3811 atomic_set(&motg->pm_suspended, 1);
3812 ret = msm_otg_suspend(motg);
3813 if (ret)
3814 atomic_set(&motg->pm_suspended, 0);
3815
3816 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303817}
3818
3819static int msm_otg_pm_resume(struct device *dev)
3820{
Jack Pham5ca279b2012-05-14 18:42:54 -07003821 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303822 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303823
3824 dev_dbg(dev, "OTG PM resume\n");
3825
Jack Pham5ca279b2012-05-14 18:42:54 -07003826 atomic_set(&motg->pm_suspended, 0);
3827 if (motg->sm_work_pending) {
3828 motg->sm_work_pending = false;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303829
Jack Pham5ca279b2012-05-14 18:42:54 -07003830 pm_runtime_get_noresume(dev);
3831 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303832
Jack Pham5ca279b2012-05-14 18:42:54 -07003833 /* Update runtime PM status */
3834 pm_runtime_disable(dev);
3835 pm_runtime_set_active(dev);
3836 pm_runtime_enable(dev);
3837
3838 queue_work(system_nrt_wq, &motg->sm_work);
3839 }
3840
3841 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303842}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303843#endif
3844
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303845#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303846static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303847 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3848 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3849 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303850};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303851#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303852
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303853static struct of_device_id msm_otg_dt_match[] = {
3854 { .compatible = "qcom,hsusb-otg",
3855 },
3856 {}
3857};
3858
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303859static struct platform_driver msm_otg_driver = {
3860 .remove = __devexit_p(msm_otg_remove),
3861 .driver = {
3862 .name = DRIVER_NAME,
3863 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303864#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303865 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303866#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303867 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303868 },
3869};
3870
3871static int __init msm_otg_init(void)
3872{
3873 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3874}
3875
3876static void __exit msm_otg_exit(void)
3877{
3878 platform_driver_unregister(&msm_otg_driver);
3879}
3880
3881module_init(msm_otg_init);
3882module_exit(msm_otg_exit);
3883
3884MODULE_LICENSE("GPL v2");
3885MODULE_DESCRIPTION("MSM USB transceiver driver");