blob: d4d7ee908b1c409b2d88a36f09eb81afaf589011 [file] [log] [blame]
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05301/* Copyright (c) 2009-2013, Linux Foundation. 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>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053042#include <linux/mhl_8334.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053043
Manu Gautam0ddbd922012-09-21 17:17:38 +053044#include <mach/scm.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070046#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080047#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053048#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053049#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053050
51#define MSM_USB_BASE (motg->regs)
52#define DRIVER_NAME "msm_otg"
53
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053054#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondeti458d8792012-09-28 14:45:18 +053055#define CHG_RECHECK_DELAY (jiffies + msecs_to_jiffies(2000))
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
Amit Blayd0fe07b2012-09-05 16:42:09 +030071#define USB_SUSPEND_DELAY_TIME (500 * HZ/1000) /* 500 msec */
72
Amit Blay81801aa2012-09-19 12:08:12 +020073enum msm_otg_phy_reg_mode {
74 USB_PHY_REG_OFF,
75 USB_PHY_REG_ON,
76 USB_PHY_REG_LPM_ON,
77 USB_PHY_REG_LPM_OFF,
78};
79
Mayank Rana443f9e42012-09-21 18:32:39 +053080static char *override_phy_init;
81module_param(override_phy_init, charp, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(override_phy_init,
83 "Override HSUSB PHY Init Settings");
84
Amit Blayd6f38282012-10-29 13:13:46 +020085unsigned int lpm_disconnect_thresh = 1000;
86module_param(lpm_disconnect_thresh , uint, S_IRUGO | S_IWUSR);
87MODULE_PARM_DESC(lpm_disconnect_thresh,
88 "Delay before entering LPM on USB disconnect");
89
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053090static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053092static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053093static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053094static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095
Anji jonnala11aa5c42011-05-04 10:19:48 +053096static struct regulator *hsusb_3p3;
97static struct regulator *hsusb_1p8;
Mayank Rana0f286cf2013-02-27 11:43:27 +053098static struct regulator *hsusb_vdd;
Mayank Ranae3926882011-12-26 09:47:54 +053099static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530100static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +0530101static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530102
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530103static bool aca_id_turned_on;
David Keitel272ce522012-08-17 16:25:24 -0700104static bool legacy_power_supply;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530105static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530106{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530107#ifdef CONFIG_USB_MSM_ACA
108 return true;
109#else
110 return debug_aca_enabled;
111#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +0530112}
113
Mayank Rana0f286cf2013-02-27 11:43:27 +0530114static int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
Mayank Rana248698c2012-04-19 00:03:16 +0530115 { /* VDD_CX CORNER Voting */
116 [VDD_NONE] = RPM_VREG_CORNER_NONE,
117 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
118 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
119 },
120 { /* VDD_CX Voltage Voting */
121 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
122 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
123 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
124 },
125};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126
127static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
128{
129 int rc = 0;
130
131 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700132 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200134 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530135 return PTR_ERR(hsusb_3p3);
136 }
137
138 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
139 USB_PHY_3P3_VOL_MAX);
140 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700141 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530143 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700145 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530146 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200147 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530150 }
151 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
152 USB_PHY_1P8_VOL_MAX);
153 if (rc) {
Stephen Boyd9850acb2013-01-28 14:11:20 -0800154 dev_err(motg->phy.dev, "unable to set voltage level "
155 "for hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530156 goto put_1p8;
157 }
158
159 return 0;
160 }
161
Anji jonnala11aa5c42011-05-04 10:19:48 +0530162put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164put_3p3_lpm:
165 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530166 return rc;
167}
168
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530169static int msm_hsusb_config_vddcx(int high)
170{
Mayank Rana248698c2012-04-19 00:03:16 +0530171 struct msm_otg *motg = the_msm_otg;
172 enum usb_vdd_type vdd_type = motg->vdd_type;
173 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530174 int min_vol;
175 int ret;
176
Mayank Rana248698c2012-04-19 00:03:16 +0530177 min_vol = vdd_val[vdd_type][!!high];
Mayank Rana0f286cf2013-02-27 11:43:27 +0530178 ret = regulator_set_voltage(hsusb_vdd, min_vol, max_vol);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530179 if (ret) {
180 pr_err("%s: unable to set the voltage for regulator "
181 "HSUSB_VDDCX\n", __func__);
182 return ret;
183 }
184
185 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
186
187 return ret;
188}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530189
Amit Blay81801aa2012-09-19 12:08:12 +0200190static int msm_hsusb_ldo_enable(struct msm_otg *motg,
191 enum msm_otg_phy_reg_mode mode)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530192{
193 int ret = 0;
194
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530195 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530196 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
197 return -ENODEV;
198 }
199
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530200 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530201 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
202 return -ENODEV;
203 }
204
Amit Blay81801aa2012-09-19 12:08:12 +0200205 switch (mode) {
206 case USB_PHY_REG_ON:
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207 ret = regulator_set_optimum_mode(hsusb_1p8,
208 USB_PHY_1P8_HPM_LOAD);
209 if (ret < 0) {
Stephen Boyd9850acb2013-01-28 14:11:20 -0800210 pr_err("%s: Unable to set HPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530211 "HSUSB_1p8\n", __func__);
212 return ret;
213 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214
215 ret = regulator_enable(hsusb_1p8);
216 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700217 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 __func__);
219 regulator_set_optimum_mode(hsusb_1p8, 0);
220 return ret;
221 }
222
Anji jonnala11aa5c42011-05-04 10:19:48 +0530223 ret = regulator_set_optimum_mode(hsusb_3p3,
224 USB_PHY_3P3_HPM_LOAD);
225 if (ret < 0) {
Stephen Boyd9850acb2013-01-28 14:11:20 -0800226 pr_err("%s: Unable to set HPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530227 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 regulator_set_optimum_mode(hsusb_1p8, 0);
229 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530230 return ret;
231 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232
233 ret = regulator_enable(hsusb_3p3);
234 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700235 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 __func__);
237 regulator_set_optimum_mode(hsusb_3p3, 0);
238 regulator_set_optimum_mode(hsusb_1p8, 0);
239 regulator_disable(hsusb_1p8);
240 return ret;
241 }
242
Amit Blay81801aa2012-09-19 12:08:12 +0200243 break;
244
245 case USB_PHY_REG_OFF:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 ret = regulator_disable(hsusb_1p8);
247 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700248 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 __func__);
250 return ret;
251 }
252
253 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530254 if (ret < 0)
Stephen Boyd9850acb2013-01-28 14:11:20 -0800255 pr_err("%s: Unable to set LPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530256 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257
258 ret = regulator_disable(hsusb_3p3);
259 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700260 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 __func__);
262 return ret;
263 }
264 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530265 if (ret < 0)
Stephen Boyd9850acb2013-01-28 14:11:20 -0800266 pr_err("%s: Unable to set LPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530267 "HSUSB_3p3\n", __func__);
Amit Blay81801aa2012-09-19 12:08:12 +0200268
269 break;
270
271 case USB_PHY_REG_LPM_ON:
272 ret = regulator_set_optimum_mode(hsusb_1p8,
273 USB_PHY_1P8_LPM_LOAD);
274 if (ret < 0) {
275 pr_err("%s: Unable to set LPM of the regulator: HSUSB_1p8\n",
276 __func__);
277 return ret;
278 }
279
280 ret = regulator_set_optimum_mode(hsusb_3p3,
281 USB_PHY_3P3_LPM_LOAD);
282 if (ret < 0) {
283 pr_err("%s: Unable to set LPM of the regulator: HSUSB_3p3\n",
284 __func__);
285 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
286 return ret;
287 }
288
289 break;
290
291 case USB_PHY_REG_LPM_OFF:
292 ret = regulator_set_optimum_mode(hsusb_1p8,
293 USB_PHY_1P8_HPM_LOAD);
294 if (ret < 0) {
295 pr_err("%s: Unable to set HPM of the regulator: HSUSB_1p8\n",
296 __func__);
297 return ret;
298 }
299
300 ret = regulator_set_optimum_mode(hsusb_3p3,
301 USB_PHY_3P3_HPM_LOAD);
302 if (ret < 0) {
303 pr_err("%s: Unable to set HPM of the regulator: HSUSB_3p3\n",
304 __func__);
305 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
306 return ret;
307 }
308
309 break;
310
311 default:
312 pr_err("%s: Unsupported mode (%d).", __func__, mode);
313 return -ENOTSUPP;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530314 }
315
Amit Blay81801aa2012-09-19 12:08:12 +0200316 pr_debug("%s: USB reg mode (%d) (OFF/HPM/LPM)\n", __func__, mode);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530317 return ret < 0 ? ret : 0;
318}
319
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530320static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
321{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530322 struct msm_otg_platform_data *pdata = motg->pdata;
323
324 if (!pdata->mhl_enable)
325 return;
326
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530327 if (!mhl_usb_hs_switch) {
328 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530329 return;
330 }
331
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530332 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530333 if (regulator_enable(mhl_usb_hs_switch))
334 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530335 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530336 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530337 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530338}
339
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200340static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530341{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200342 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530343 int cnt = 0;
344
345 /* initiate read operation */
346 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
347 USB_ULPI_VIEWPORT);
348
349 /* wait for completion */
350 while (cnt < ULPI_IO_TIMEOUT_USEC) {
351 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
352 break;
353 udelay(1);
354 cnt++;
355 }
356
357 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200358 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530359 readl(USB_ULPI_VIEWPORT));
360 return -ETIMEDOUT;
361 }
362 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
363}
364
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200365static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530366{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200367 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530368 int cnt = 0;
369
370 /* initiate write operation */
371 writel(ULPI_RUN | ULPI_WRITE |
372 ULPI_ADDR(reg) | ULPI_DATA(val),
373 USB_ULPI_VIEWPORT);
374
375 /* wait for completion */
376 while (cnt < ULPI_IO_TIMEOUT_USEC) {
377 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
378 break;
379 udelay(1);
380 cnt++;
381 }
382
383 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200384 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530385 return -ETIMEDOUT;
386 }
387 return 0;
388}
389
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200390static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530391 .read = ulpi_read,
392 .write = ulpi_write,
393};
394
395static void ulpi_init(struct msm_otg *motg)
396{
397 struct msm_otg_platform_data *pdata = motg->pdata;
Mayank Rana443f9e42012-09-21 18:32:39 +0530398 int aseq[10];
399 int *seq = NULL;
400
401 if (override_phy_init) {
402 pr_debug("%s(): HUSB PHY Init:%s\n", __func__,
403 override_phy_init);
404 get_options(override_phy_init, ARRAY_SIZE(aseq), aseq);
405 seq = &aseq[1];
406 } else {
407 seq = pdata->phy_init_seq;
408 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530409
410 if (!seq)
411 return;
412
413 while (seq[0] >= 0) {
Mayank Rana443f9e42012-09-21 18:32:39 +0530414 if (override_phy_init)
415 pr_debug("ulpi: write 0x%02x to 0x%02x\n",
416 seq[0], seq[1]);
417
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200418 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530419 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200420 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530421 seq += 2;
422 }
423}
424
425static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
426{
427 int ret;
428
429 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530430 if (!IS_ERR(motg->clk)) {
431 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
432 } else {
433 /* Using asynchronous block reset to the hardware */
434 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
435 clk_disable_unprepare(motg->pclk);
436 clk_disable_unprepare(motg->core_clk);
437 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
438 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530439 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200440 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530441 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530442 if (!IS_ERR(motg->clk)) {
443 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
444 } else {
445 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
446 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
447 ndelay(200);
448 clk_prepare_enable(motg->core_clk);
449 clk_prepare_enable(motg->pclk);
450 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530451 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200452 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530453 }
454 return ret;
455}
456
457static int msm_otg_phy_clk_reset(struct msm_otg *motg)
458{
459 int ret;
460
Amit Blay02eff132011-09-21 16:46:24 +0300461 if (IS_ERR(motg->phy_reset_clk))
462 return 0;
463
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530464 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
465 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200466 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530467 return ret;
468 }
469 usleep_range(10000, 12000);
470 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
471 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200472 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530473 return ret;
474}
475
476static int msm_otg_phy_reset(struct msm_otg *motg)
477{
478 u32 val;
479 int ret;
480 int retries;
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530481 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530482
483 ret = msm_otg_link_clk_reset(motg, 1);
484 if (ret)
485 return ret;
486 ret = msm_otg_phy_clk_reset(motg);
487 if (ret)
488 return ret;
Amit Blay58dc2bc2013-01-24 12:28:03 +0200489
490 /*
491 * 10 usec delay is required according to spec. Using larger value
492 * since the exact value proved to not work 100% of the time.
493 */
Bar Weinere7129cb2012-11-28 08:44:14 +0200494 if (IS_ERR(motg->phy_reset_clk))
Amit Blay58dc2bc2013-01-24 12:28:03 +0200495 usleep_range(100, 120);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530496 ret = msm_otg_link_clk_reset(motg, 0);
497 if (ret)
498 return ret;
499
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530500 if (pdata && pdata->enable_sec_phy)
501 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
502 USB_PHY_CTRL2);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530503 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
504 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
505
506 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200507 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530508 ULPI_CLR(ULPI_FUNC_CTRL));
509 if (!ret)
510 break;
511 ret = msm_otg_phy_clk_reset(motg);
512 if (ret)
513 return ret;
514 }
515 if (!retries)
516 return -ETIMEDOUT;
517
518 /* This reset calibrates the phy, if the above write succeeded */
519 ret = msm_otg_phy_clk_reset(motg);
520 if (ret)
521 return ret;
522
523 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200524 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530525 if (ret != -ETIMEDOUT)
526 break;
527 ret = msm_otg_phy_clk_reset(motg);
528 if (ret)
529 return ret;
530 }
531 if (!retries)
532 return -ETIMEDOUT;
533
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200534 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530535 return 0;
536}
537
538#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530539static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530540{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530541 int cnt = 0;
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530542 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530543
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530544 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530545 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530546 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530547 break;
548 udelay(1);
549 cnt++;
550 }
551 if (cnt >= LINK_RESET_TIMEOUT_USEC)
552 return -ETIMEDOUT;
553
554 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530555 writel_relaxed(0x80000000, USB_PORTSC);
556 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530557 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530558
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530559 if (pdata && pdata->enable_sec_phy)
560 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
561 USB_PHY_CTRL2);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530562 return 0;
563}
564
Steve Mucklef132c6c2012-06-06 18:30:57 -0700565static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530566{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700567 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530568 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530569 int ret;
570 u32 val = 0;
571 u32 ulpi_val = 0;
572
Ofir Cohen4da266f2012-01-03 10:19:29 +0200573 /*
574 * USB PHY and Link reset also reset the USB BAM.
575 * Thus perform reset operation only once to avoid
576 * USB BAM reset on other cases e.g. USB cable disconnections.
577 */
578 if (pdata->disable_reset_on_disconnect) {
579 if (motg->reset_counter)
580 return 0;
581 else
582 motg->reset_counter++;
583 }
584
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530585 if (!IS_ERR(motg->clk))
586 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530587 ret = msm_otg_phy_reset(motg);
588 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700589 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530590 return ret;
591 }
592
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530593 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530594 ret = msm_otg_link_reset(motg);
595 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700596 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530597 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530598 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530599 msleep(100);
600
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530601 ulpi_init(motg);
602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 /* Ensure that RESET operation is completed before turning off clock */
604 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530605
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530606 if (!IS_ERR(motg->clk))
607 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530608
609 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530610 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530611 if (pdata->mode == USB_OTG) {
612 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
613 val |= OTGSC_IDIE | OTGSC_BSVIE;
614 } else if (pdata->mode == USB_PERIPHERAL) {
615 ulpi_val = ULPI_INT_SESS_VALID;
616 val |= OTGSC_BSVIE;
617 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530618 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200619 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
620 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530621 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700622 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530623 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530624 /* Enable PMIC pull-up */
625 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530626 }
627
628 return 0;
629}
630
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530631static const char *timer_string(int bit)
632{
633 switch (bit) {
634 case A_WAIT_VRISE: return "a_wait_vrise";
635 case A_WAIT_VFALL: return "a_wait_vfall";
636 case B_SRP_FAIL: return "b_srp_fail";
637 case A_WAIT_BCON: return "a_wait_bcon";
638 case A_AIDL_BDIS: return "a_aidl_bdis";
639 case A_BIDL_ADIS: return "a_bidl_adis";
640 case B_ASE0_BRST: return "b_ase0_brst";
641 case A_TST_MAINT: return "a_tst_maint";
642 case B_TST_SRP: return "b_tst_srp";
643 case B_TST_CONFIG: return "b_tst_config";
644 default: return "UNDEFINED";
645 }
646}
647
648static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
649{
650 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
651
652 switch (motg->active_tmout) {
653 case A_WAIT_VRISE:
654 /* TODO: use vbus_vld interrupt */
655 set_bit(A_VBUS_VLD, &motg->inputs);
656 break;
657 case A_TST_MAINT:
658 /* OTG PET: End session after TA_TST_MAINT */
659 set_bit(A_BUS_DROP, &motg->inputs);
660 break;
661 case B_TST_SRP:
662 /*
663 * OTG PET: Initiate SRP after TB_TST_SRP of
664 * previous session end.
665 */
666 set_bit(B_BUS_REQ, &motg->inputs);
667 break;
668 case B_TST_CONFIG:
669 clear_bit(A_CONN, &motg->inputs);
670 break;
671 default:
672 set_bit(motg->active_tmout, &motg->tmouts);
673 }
674
675 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
676 queue_work(system_nrt_wq, &motg->sm_work);
677 return HRTIMER_NORESTART;
678}
679
680static void msm_otg_del_timer(struct msm_otg *motg)
681{
682 int bit = motg->active_tmout;
683
684 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
685 div_s64(ktime_to_us(hrtimer_get_remaining(
686 &motg->timer)), 1000));
687 hrtimer_cancel(&motg->timer);
688 clear_bit(bit, &motg->tmouts);
689}
690
691static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
692{
693 clear_bit(bit, &motg->tmouts);
694 motg->active_tmout = bit;
695 pr_debug("starting %s timer\n", timer_string(bit));
696 hrtimer_start(&motg->timer,
697 ktime_set(time / 1000, (time % 1000) * 1000000),
698 HRTIMER_MODE_REL);
699}
700
701static void msm_otg_init_timer(struct msm_otg *motg)
702{
703 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
704 motg->timer.function = msm_otg_timer_func;
705}
706
Steve Mucklef132c6c2012-06-06 18:30:57 -0700707static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530708{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700709 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530710
Steve Mucklef132c6c2012-06-06 18:30:57 -0700711 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530712 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700713 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530714 return -EINVAL;
715 }
716
717 pr_debug("A-Host: HNP initiated\n");
718 clear_bit(A_BUS_REQ, &motg->inputs);
719 queue_work(system_nrt_wq, &motg->sm_work);
720 return 0;
721}
722
Steve Mucklef132c6c2012-06-06 18:30:57 -0700723static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530724{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700725 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530726 u32 val;
727 int ret = 0;
728
Steve Mucklef132c6c2012-06-06 18:30:57 -0700729 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530730 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700731 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530732 ret = -EINVAL;
733 goto out;
734 }
735
736 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
737 pr_debug("initial conditions of SRP are not met. Try again"
738 "after some time\n");
739 ret = -EAGAIN;
740 goto out;
741 }
742
743 pr_debug("B-Device SRP started\n");
744
745 /*
746 * PHY won't pull D+ high unless it detects Vbus valid.
747 * Since by definition, SRP is only done when Vbus is not valid,
748 * software work-around needs to be used to spoof the PHY into
749 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
750 * VBUSVLDEXT register bits.
751 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700752 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530753 /*
754 * Harware auto assist data pulsing: Data pulse is given
755 * for 7msec; wait for vbus
756 */
757 val = readl_relaxed(USB_OTGSC);
758 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
759
760 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
761out:
762 return ret;
763}
764
Steve Mucklef132c6c2012-06-06 18:30:57 -0700765static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530766{
767 struct usb_hcd *hcd = bus_to_hcd(otg->host);
768 struct usb_device *rhub = otg->host->root_hub;
769
770 if (enable) {
771 pm_runtime_disable(&rhub->dev);
772 rhub->state = USB_STATE_NOTATTACHED;
773 hcd->driver->bus_suspend(hcd);
774 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
775 } else {
776 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700777 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530778 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
779 }
780}
781
Steve Mucklef132c6c2012-06-06 18:30:57 -0700782static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530783{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700784 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530785
Amit Blay6fa647a2012-05-24 14:12:08 +0300786 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530787 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530788
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530789 /*
790 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
791 * during bus suspend/resume. Update the relevant state
792 * machine inputs and trigger LPM entry/exit. Checking
793 * in_lpm flag would avoid unnecessary work scheduling.
794 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530795 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700796 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530797 case OTG_STATE_A_WAIT_BCON:
798 if (TA_WAIT_BCON > 0)
799 break;
800 /* fall through */
801 case OTG_STATE_A_HOST:
802 pr_debug("host bus suspend\n");
803 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530804 if (!atomic_read(&motg->in_lpm))
805 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530806 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300807 case OTG_STATE_B_PERIPHERAL:
808 pr_debug("peripheral bus suspend\n");
809 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
810 break;
811 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530812 if (!atomic_read(&motg->in_lpm))
813 queue_delayed_work(system_nrt_wq,
814 &motg->suspend_work,
815 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300816 break;
817
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530818 default:
819 break;
820 }
821 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700822 switch (phy->state) {
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530823 case OTG_STATE_A_WAIT_BCON:
824 /* Remote wakeup or resume */
825 set_bit(A_BUS_REQ, &motg->inputs);
826 /* ensure hardware is not in low power mode */
827 if (atomic_read(&motg->in_lpm))
828 pm_runtime_resume(phy->dev);
829 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530830 case OTG_STATE_A_SUSPEND:
831 /* Remote wakeup or resume */
832 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700833 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700834
835 /* ensure hardware is not in low power mode */
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530836 if (atomic_read(&motg->in_lpm))
837 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530838 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300839 case OTG_STATE_B_PERIPHERAL:
840 pr_debug("peripheral bus resume\n");
841 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
842 break;
843 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530844 if (atomic_read(&motg->in_lpm))
845 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300846 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530847 default:
848 break;
849 }
850 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530851 return 0;
852}
853
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530854#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530855#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
856
857#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530858static int msm_otg_suspend(struct msm_otg *motg)
859{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200860 struct usb_phy *phy = &motg->phy;
861 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530862 struct msm_otg_platform_data *pdata = motg->pdata;
863 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300864 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530865 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800866 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530867 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530868
869 if (atomic_read(&motg->in_lpm))
870 return 0;
871
Lena Salmanabde35d2013-04-25 15:29:43 +0300872 if (motg->pdata->delay_lpm_hndshk_on_disconnect && !msm_bam_lpm_ok())
873 return 0;
874
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530875 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530876 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
877 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700878 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300879 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
880 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530881 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700882
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530883 /*
884 * Abort suspend when,
885 * 1. charging detection in progress due to cable plug-in
886 * 2. host mode activation in progress due to Micro-A cable insertion
887 */
888
889 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
890 !dcp) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham502bea32012-08-13 15:34:20 -0700891 enable_irq(motg->irq);
892 return -EBUSY;
893 }
894
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530895 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530896 * Chipidea 45-nm PHY suspend sequence:
897 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530898 * Interrupt Latch Register auto-clear feature is not present
899 * in all PHY versions. Latch register is clear on read type.
900 * Clear latch register to avoid spurious wakeup from
901 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530902 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530903 * PHY comparators are disabled when PHY enters into low power
904 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
905 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
906 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530907 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530908 * PLL is not turned off when PHY enters into low power mode (LPM).
909 * Disable PLL for maximum power savings.
910 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530911
912 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200913 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530914 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200915 ulpi_write(phy, 0x01, 0x30);
916 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530917 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530918
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530920 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530921 * PHY may take some time or even fail to enter into low power
922 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
923 * in failure case.
924 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530925 portsc = readl_relaxed(USB_PORTSC);
926 if (!(portsc & PORTSC_PHCD)) {
927 writel_relaxed(portsc | PORTSC_PHCD,
928 USB_PORTSC);
929 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
930 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
931 break;
932 udelay(1);
933 cnt++;
934 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530935 }
936
937 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200938 dev_err(phy->dev, "Unable to suspend PHY\n");
939 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530940 enable_irq(motg->irq);
941 return -ETIMEDOUT;
942 }
943
944 /*
945 * PHY has capability to generate interrupt asynchronously in low
946 * power mode (LPM). This interrupt is level triggered. So USB IRQ
947 * line must be disabled till async interrupt enable bit is cleared
948 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
949 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530950 *
951 * PHY retention mode is disallowed while entering to LPM with wall
952 * charger connected. But PHY is put into suspend mode. Hence
953 * enable asynchronous interrupt to detect charger disconnection when
954 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530955 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530956 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300957 if (host_bus_suspend || device_bus_suspend ||
958 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530959 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
960 else
961 cmd_val |= ULPI_STP_CTRL;
962 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530963
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530964 /*
965 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
966 * PHY retention and collapse can not happen with VDP_SRC enabled.
967 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300968 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
969 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200970 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530971 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +0200972 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530973 if ((motg->pdata->mode == USB_OTG) ||
974 (motg->pdata->mode == USB_HOST))
975 phy_ctrl_val |= (PHY_IDHV_INTEN |
976 PHY_OTGSESSVLDHV_INTEN);
977 else
978 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
979 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530980
Amit Blay58b31472011-11-18 09:39:39 +0200981 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530983 }
984
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985 /* Ensure that above operation is completed before turning off clocks */
986 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300987 /* Consider clocks on workaround flag only in case of bus suspend */
988 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
989 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
990 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300991 clk_disable_unprepare(motg->pclk);
992 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300993 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300994 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530995
Anji jonnala7da3f262011-12-02 17:22:14 -0800996 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530997 if (!host_bus_suspend) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +0530998 if (!IS_ERR(motg->xo_clk)) {
999 clk_disable_unprepare(motg->xo_clk);
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301000 motg->lpm_flags |= XO_SHUTDOWN;
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301001 } else {
1002 ret = msm_xo_mode_vote(motg->xo_handle,
1003 MSM_XO_MODE_OFF);
1004 if (ret)
1005 dev_err(phy->dev, "%s fail to devote XO %d\n",
1006 __func__, ret);
1007 else
1008 motg->lpm_flags |= XO_SHUTDOWN;
1009 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301010 }
Anji jonnala7da3f262011-12-02 17:22:14 -08001011
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301012 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
1013 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +02001014 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001016 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
1017 !host_bus_suspend && !device_bus_suspend && !dcp) {
1018 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
1019 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +05301020 }
1021
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301022 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001023 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301024 msm_hsusb_mhl_switch_enable(motg, 0);
1025 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026
Steve Mucklef132c6c2012-06-06 18:30:57 -07001027 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001028 if (motg->async_irq)
1029 enable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001030 else
1031 enable_irq_wake(motg->irq);
1032
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001033 if (motg->pdata->pmic_id_irq)
1034 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001035 if (pdata->otg_control == OTG_PHY_CONTROL &&
1036 pdata->mpm_otgsessvld_int)
1037 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301039 if (bus)
1040 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1041
1042 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001043 /* Enable ASYNC IRQ (if present) during LPM */
1044 if (motg->async_irq)
1045 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301046 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301048
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001049 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301050
1051 return 0;
1052}
1053
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301054static int msm_otg_resume(struct msm_otg *motg)
1055{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001056 struct usb_phy *phy = &motg->phy;
1057 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001058 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301059 int cnt = 0;
1060 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001061 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001062 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301063
1064 if (!atomic_read(&motg->in_lpm))
1065 return 0;
1066
Jack Pham8978b892012-10-17 16:31:39 -07001067 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301069
Anji jonnala7da3f262011-12-02 17:22:14 -08001070 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301071 if (motg->lpm_flags & XO_SHUTDOWN) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301072 if (!IS_ERR(motg->xo_clk)) {
1073 clk_prepare_enable(motg->xo_clk);
1074 } else {
1075 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1076 if (ret)
1077 dev_err(phy->dev, "%s fail to vote for XO %d\n",
1078 __func__, ret);
1079 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301080 motg->lpm_flags &= ~XO_SHUTDOWN;
1081 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301082
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001083 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001084 clk_prepare_enable(motg->core_clk);
1085 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001086 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001087 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301088
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001090 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001092 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1093 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1094 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001095 }
1096
1097 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301098 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301099 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001100 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1101 phy_ctrl_val |= PHY_RETEN;
1102 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1103 /* Disable PHY HV interrupts */
1104 phy_ctrl_val &=
1105 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
1106 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001107 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301108 }
1109
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301110 temp = readl(USB_USBCMD);
1111 temp &= ~ASYNC_INTR_CTRL;
1112 temp &= ~ULPI_STP_CTRL;
1113 writel(temp, USB_USBCMD);
1114
1115 /*
1116 * PHY comes out of low power mode (LPM) in case of wakeup
1117 * from asynchronous interrupt.
1118 */
1119 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1120 goto skip_phy_resume;
1121
1122 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1123 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1124 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1125 break;
1126 udelay(1);
1127 cnt++;
1128 }
1129
1130 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1131 /*
1132 * This is a fatal error. Reset the link and
1133 * PHY. USB state can not be restored. Re-insertion
1134 * of USB cable is the only way to get USB working.
1135 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001136 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301137 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001138 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301139 }
1140
1141skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001142 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001143 if (motg->async_irq)
1144 disable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001145 else
1146 disable_irq_wake(motg->irq);
1147
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 if (motg->pdata->pmic_id_irq)
1149 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001150 if (pdata->otg_control == OTG_PHY_CONTROL &&
1151 pdata->mpm_otgsessvld_int)
1152 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301154 if (bus)
1155 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1156
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301157 atomic_set(&motg->in_lpm, 0);
1158
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301159 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001160 /* Match the disable_irq call from ISR */
1161 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301162 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301163 }
Jack Pham8978b892012-10-17 16:31:39 -07001164 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301165
Manu Gautamf8c45642012-08-10 10:20:56 -07001166 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1167 if (motg->async_irq)
1168 disable_irq(motg->async_irq);
1169
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001170 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301171
1172 return 0;
1173}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301174#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301175
David Keitel272ce522012-08-17 16:25:24 -07001176static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001177{
Jack Pham0c695282012-10-19 18:13:03 -07001178 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001179 pr_err("No USB power supply registered!\n");
1180 return;
1181 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001182
Jack Pham0c695282012-10-19 18:13:03 -07001183 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001184 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301185 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001186 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301187 } else {
David Keitel272ce522012-08-17 16:25:24 -07001188 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301189 /*
1190 * VBUS comparator is disabled by PMIC charging driver
1191 * when SYSTEM scope is selected. For ID_GND->ID_A
1192 * transition, give 50 msec delay so that PMIC charger
1193 * driver detect the VBUS and ready for accepting
1194 * charging current value from USB.
1195 */
1196 if (test_bit(ID_A, &motg->inputs))
1197 msleep(50);
1198 }
David Keitel272ce522012-08-17 16:25:24 -07001199 } else {
1200 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001201 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001202 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001203}
1204
David Keitel081a3e22012-04-18 12:37:07 -07001205static int msm_otg_notify_chg_type(struct msm_otg *motg)
1206{
1207 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001208
David Keitel081a3e22012-04-18 12:37:07 -07001209 /*
1210 * TODO
1211 * Unify OTG driver charger types and power supply charger types
1212 */
1213 if (charger_type == motg->chg_type)
1214 return 0;
1215
1216 if (motg->chg_type == USB_SDP_CHARGER)
1217 charger_type = POWER_SUPPLY_TYPE_USB;
1218 else if (motg->chg_type == USB_CDP_CHARGER)
1219 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301220 else if (motg->chg_type == USB_DCP_CHARGER ||
1221 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001222 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1223 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1224 motg->chg_type == USB_ACA_A_CHARGER ||
1225 motg->chg_type == USB_ACA_B_CHARGER ||
1226 motg->chg_type == USB_ACA_C_CHARGER))
1227 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1228 else
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301229 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001230
Jack Pham0c695282012-10-19 18:13:03 -07001231 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001232 pr_err("No USB power supply registered!\n");
1233 return -EINVAL;
1234 }
1235
1236 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001237 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001238 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001239}
1240
Amit Blay0f7edf72012-01-15 10:11:27 +02001241static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1242{
Jack Pham0c695282012-10-19 18:13:03 -07001243 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001244 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1245 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001246 }
1247
David Keitel272ce522012-08-17 16:25:24 -07001248 if (motg->cur_power == 0 && mA > 2) {
1249 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001250 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001251 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001252 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001253 goto psy_error;
1254 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1255 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001256 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001257 goto psy_error;
1258 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001259 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001260 goto psy_error;
1261 }
Jack Pham0c695282012-10-19 18:13:03 -07001262 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001263 return 0;
1264
David Keitel272ce522012-08-17 16:25:24 -07001265psy_error:
1266 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001267 return -ENXIO;
1268}
1269
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301270static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1271{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001272 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301273
1274 if (g && g->is_a_peripheral)
1275 return;
1276
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301277 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1278 motg->chg_type == USB_ACA_A_CHARGER ||
1279 motg->chg_type == USB_ACA_B_CHARGER ||
1280 motg->chg_type == USB_ACA_C_CHARGER) &&
1281 mA > IDEV_ACA_CHG_LIMIT)
1282 mA = IDEV_ACA_CHG_LIMIT;
1283
David Keitel081a3e22012-04-18 12:37:07 -07001284 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001285 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001286 "Failed notifying %d charger type to PMIC\n",
1287 motg->chg_type);
1288
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301289 if (motg->cur_power == mA)
1290 return;
1291
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001292 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001293
1294 /*
1295 * Use Power Supply API if supported, otherwise fallback
1296 * to legacy pm8921 API.
1297 */
1298 if (msm_otg_notify_power_supply(motg, mA))
1299 pm8921_charger_vbus_draw(mA);
1300
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301301 motg->cur_power = mA;
1302}
1303
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001304static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301305{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001306 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301307
1308 /*
1309 * Gadget driver uses set_power method to notify about the
1310 * available current based on suspend/configured states.
1311 *
1312 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1313 * states when CDP/ACA is connected.
1314 */
1315 if (motg->chg_type == USB_SDP_CHARGER)
1316 msm_otg_notify_charger(motg, mA);
1317
1318 return 0;
1319}
1320
Steve Mucklef132c6c2012-06-06 18:30:57 -07001321static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301322{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001323 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301324 struct msm_otg_platform_data *pdata = motg->pdata;
1325 struct usb_hcd *hcd;
1326
1327 if (!otg->host)
1328 return;
1329
1330 hcd = bus_to_hcd(otg->host);
1331
1332 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001333 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301334
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301335 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001336 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301337 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1338
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301339 /*
1340 * Some boards have a switch cotrolled by gpio
1341 * to enable/disable internal HUB. Enable internal
1342 * HUB before kicking the host.
1343 */
1344 if (pdata->setup_gpio)
1345 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301346 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301347 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001348 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301349
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301350 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301351 /* HCD core reset all bits of PORTSC. select ULPI phy */
1352 writel_relaxed(0x80000000, USB_PORTSC);
1353
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301354 if (pdata->setup_gpio)
1355 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301356
1357 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001358 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301359 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301360 }
1361}
1362
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001363static int msm_otg_usbdev_notify(struct notifier_block *self,
1364 unsigned long action, void *priv)
1365{
1366 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001367 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301368 struct usb_device *udev = priv;
1369
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301370 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1371 goto out;
1372
Steve Mucklef132c6c2012-06-06 18:30:57 -07001373 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301374 goto out;
1375 /*
1376 * Interested in devices connected directly to the root hub.
1377 * ACA dock can supply IDEV_CHG irrespective devices connected
1378 * on the accessory port.
1379 */
1380 if (!udev->parent || udev->parent->parent ||
1381 motg->chg_type == USB_ACA_DOCK_CHARGER)
1382 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001383
1384 switch (action) {
1385 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301386 if (aca_enabled())
1387 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001388 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301389 pr_debug("B_CONN set\n");
1390 set_bit(B_CONN, &motg->inputs);
1391 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001392 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301393 /*
1394 * OTG PET: A-device must end session within
1395 * 10 sec after PET enumeration.
1396 */
1397 if (udev->quirks & USB_QUIRK_OTG_PET)
1398 msm_otg_start_timer(motg, TA_TST_MAINT,
1399 A_TST_MAINT);
1400 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301401 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001403 if (udev->actconfig)
1404 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1405 else
1406 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001407 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301408 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301409 break;
1410 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001411 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1412 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301413 pr_debug("B_CONN clear\n");
1414 clear_bit(B_CONN, &motg->inputs);
1415 /*
1416 * OTG PET: A-device must end session after
1417 * PET disconnection if it is enumerated
1418 * with bcdDevice[0] = 1. USB core sets
1419 * bus->otg_vbus_off for us. clear it here.
1420 */
1421 if (udev->bus->otg_vbus_off) {
1422 udev->bus->otg_vbus_off = 0;
1423 set_bit(A_BUS_DROP, &motg->inputs);
1424 }
1425 queue_work(system_nrt_wq, &motg->sm_work);
1426 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001427 default:
1428 break;
1429 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301430 if (test_bit(ID_A, &motg->inputs))
1431 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1432 motg->mA_port);
1433out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001434 return NOTIFY_OK;
1435}
1436
Mayank Ranae3926882011-12-26 09:47:54 +05301437static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1438{
1439 int ret;
1440 static bool vbus_is_on;
1441
1442 if (vbus_is_on == on)
1443 return;
1444
1445 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301446 ret = motg->pdata->vbus_power(on);
1447 if (!ret)
1448 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301449 return;
1450 }
1451
1452 if (!vbus_otg) {
1453 pr_err("vbus_otg is NULL.");
1454 return;
1455 }
1456
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001457 /*
1458 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001459 * from usb before turning on the boost.
1460 * if exiting host mode disable the boost before enabling to draw
1461 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001462 */
Mayank Ranae3926882011-12-26 09:47:54 +05301463 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001464 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301465 ret = regulator_enable(vbus_otg);
1466 if (ret) {
1467 pr_err("unable to enable vbus_otg\n");
1468 return;
1469 }
1470 vbus_is_on = true;
1471 } else {
1472 ret = regulator_disable(vbus_otg);
1473 if (ret) {
1474 pr_err("unable to disable vbus_otg\n");
1475 return;
1476 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001477 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301478 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301479 }
1480}
1481
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001482static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301483{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001484 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301485 struct usb_hcd *hcd;
1486
1487 /*
1488 * Fail host registration if this board can support
1489 * only peripheral configuration.
1490 */
1491 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001492 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301493 return -ENODEV;
1494 }
1495
Mayank Ranae3926882011-12-26 09:47:54 +05301496 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001497 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301498 if (IS_ERR(vbus_otg)) {
1499 pr_err("Unable to get vbus_otg\n");
1500 return -ENODEV;
1501 }
1502 }
1503
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301504 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001505 if (otg->phy->state == OTG_STATE_A_HOST) {
1506 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301508 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301509 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301510 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001511 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301512 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301513 } else {
1514 otg->host = NULL;
1515 }
1516
1517 return 0;
1518 }
1519
1520 hcd = bus_to_hcd(host);
1521 hcd->power_budget = motg->pdata->power_budget;
1522
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301523#ifdef CONFIG_USB_OTG
1524 host->otg_port = 1;
1525#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001526 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1527 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301528 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001529 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301530
1531 /*
1532 * Kick the state machine work, if peripheral is not supported
1533 * or peripheral is already registered with us.
1534 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301535 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001536 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301537 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301538 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301539
1540 return 0;
1541}
1542
Steve Mucklef132c6c2012-06-06 18:30:57 -07001543static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301544{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301545 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001546 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301547 struct msm_otg_platform_data *pdata = motg->pdata;
1548
1549 if (!otg->gadget)
1550 return;
1551
1552 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001553 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301554 /*
1555 * Some boards have a switch cotrolled by gpio
1556 * to enable/disable internal HUB. Disable internal
1557 * HUB before kicking the gadget.
1558 */
1559 if (pdata->setup_gpio)
1560 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001561
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301562 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301563 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301564 ret = msm_bus_scale_client_update_request(
1565 motg->bus_perf_client, 1);
1566 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001567 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301568 "bus bandwidth %d\n", __func__, ret);
1569 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301570 usb_gadget_vbus_connect(otg->gadget);
1571 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001572 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301573 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301574 /* Configure BUS performance parameters to default */
1575 if (motg->bus_perf_client) {
1576 ret = msm_bus_scale_client_update_request(
1577 motg->bus_perf_client, 0);
1578 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001579 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301580 "for bus bw %d\n", __func__, ret);
1581 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301582 if (pdata->setup_gpio)
1583 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1584 }
1585
1586}
1587
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001588static int msm_otg_set_peripheral(struct usb_otg *otg,
Stephen Boyd9850acb2013-01-28 14:11:20 -08001589 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301590{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001591 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301592
1593 /*
1594 * Fail peripheral registration if this board can support
1595 * only host configuration.
1596 */
1597 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001598 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301599 return -ENODEV;
1600 }
1601
1602 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001603 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1604 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301605 msm_otg_start_peripheral(otg, 0);
1606 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001607 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301608 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301609 } else {
1610 otg->gadget = NULL;
1611 }
1612
1613 return 0;
1614 }
1615 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001616 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301617
1618 /*
1619 * Kick the state machine work, if host is not supported
1620 * or host is already registered with us.
1621 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301622 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001623 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301624 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301625 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301626
1627 return 0;
1628}
1629
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301630static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1631{
1632 unsigned long flags;
1633 int id;
1634
1635 if (!motg->pdata->pmic_id_irq)
1636 return -ENODEV;
1637
1638 local_irq_save(flags);
1639 id = irq_read_line(motg->pdata->pmic_id_irq);
1640 local_irq_restore(flags);
1641
1642 /*
1643 * If we can not read ID line state for some reason, treat
1644 * it as float. This would prevent MHL discovery and kicking
1645 * host mode unnecessarily.
1646 */
1647 return !!id;
1648}
1649
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301650static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1651 void (*callback)(int on))
1652{
1653 struct usb_phy *phy = &motg->phy;
1654 int ret;
1655
Manoj Raoa7bddd12012-08-27 20:36:45 -07001656 if (!motg->pdata->mhl_enable) {
1657 dev_dbg(phy->dev, "MHL feature not enabled\n");
1658 return -ENODEV;
1659 }
1660
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301661 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1662 !motg->pdata->pmic_id_irq) {
1663 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1664 return -ENODEV;
1665 }
1666
1667 if (!motg->pdata->mhl_dev_name) {
1668 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1669 return -ENODEV;
1670 }
1671
1672 if (callback)
1673 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1674 callback);
1675 else
1676 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1677
1678 if (ret)
1679 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1680 motg->pdata->mhl_dev_name, ret);
1681 else
1682 motg->mhl_enabled = true;
1683
1684 return ret;
1685}
1686
1687static void msm_otg_mhl_notify_online(int on)
1688{
1689 struct msm_otg *motg = the_msm_otg;
1690 struct usb_phy *phy = &motg->phy;
1691 bool queue = false;
1692
1693 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1694
1695 if (on) {
1696 set_bit(MHL, &motg->inputs);
1697 } else {
1698 clear_bit(MHL, &motg->inputs);
1699 queue = true;
1700 }
1701
1702 if (queue && phy->state != OTG_STATE_UNDEFINED)
1703 schedule_work(&motg->sm_work);
1704}
1705
1706static bool msm_otg_is_mhl(struct msm_otg *motg)
1707{
1708 struct usb_phy *phy = &motg->phy;
1709 int is_mhl, ret;
1710
1711 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1712 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1713 /*
1714 * MHL driver calls our callback saying that MHL connected
1715 * if RID_GND is detected. But at later part of discovery
1716 * it may figure out MHL is not connected and returns
1717 * false. Hence clear MHL input here.
1718 */
1719 clear_bit(MHL, &motg->inputs);
1720 dev_dbg(phy->dev, "MHL device not found\n");
1721 return false;
1722 }
1723
1724 set_bit(MHL, &motg->inputs);
1725 dev_dbg(phy->dev, "MHL device found\n");
1726 return true;
1727}
1728
1729static bool msm_chg_mhl_detect(struct msm_otg *motg)
1730{
1731 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301732
1733 if (!motg->mhl_enabled)
1734 return false;
1735
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301736 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301737
1738 if (id)
1739 return false;
1740
1741 mhl_det_in_progress = true;
1742 ret = msm_otg_is_mhl(motg);
1743 mhl_det_in_progress = false;
1744
1745 return ret;
1746}
1747
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301748static void msm_otg_chg_check_timer_func(unsigned long data)
1749{
1750 struct msm_otg *motg = (struct msm_otg *) data;
1751 struct usb_otg *otg = motg->phy.otg;
1752
1753 if (atomic_read(&motg->in_lpm) ||
1754 !test_bit(B_SESS_VLD, &motg->inputs) ||
1755 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1756 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1757 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1758 return;
1759 }
1760
1761 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1762 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1763 set_bit(B_FALSE_SDP, &motg->inputs);
1764 queue_work(system_nrt_wq, &motg->sm_work);
1765 }
1766}
1767
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001768static bool msm_chg_aca_detect(struct msm_otg *motg)
1769{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001770 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001771 u32 int_sts;
1772 bool ret = false;
1773
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301774 if (!aca_enabled())
1775 goto out;
1776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1778 goto out;
1779
Steve Mucklef132c6c2012-06-06 18:30:57 -07001780 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001781 switch (int_sts & 0x1C) {
1782 case 0x08:
1783 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001784 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001785 motg->chg_type = USB_ACA_A_CHARGER;
1786 motg->chg_state = USB_CHG_STATE_DETECTED;
1787 clear_bit(ID_B, &motg->inputs);
1788 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301789 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790 ret = true;
1791 }
1792 break;
1793 case 0x0C:
1794 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001795 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001796 motg->chg_type = USB_ACA_B_CHARGER;
1797 motg->chg_state = USB_CHG_STATE_DETECTED;
1798 clear_bit(ID_A, &motg->inputs);
1799 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301800 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001801 ret = true;
1802 }
1803 break;
1804 case 0x10:
1805 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001806 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807 motg->chg_type = USB_ACA_C_CHARGER;
1808 motg->chg_state = USB_CHG_STATE_DETECTED;
1809 clear_bit(ID_A, &motg->inputs);
1810 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301811 set_bit(ID, &motg->inputs);
1812 ret = true;
1813 }
1814 break;
1815 case 0x04:
1816 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001817 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301818 motg->chg_type = USB_INVALID_CHARGER;
1819 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1820 clear_bit(ID_A, &motg->inputs);
1821 clear_bit(ID_B, &motg->inputs);
1822 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823 ret = true;
1824 }
1825 break;
1826 default:
1827 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1828 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301829 test_and_clear_bit(ID_C, &motg->inputs) |
1830 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001831 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001832 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001833 motg->chg_type = USB_INVALID_CHARGER;
1834 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1835 }
1836 }
1837out:
1838 return ret;
1839}
1840
1841static void msm_chg_enable_aca_det(struct msm_otg *motg)
1842{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001843 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001844
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301845 if (!aca_enabled())
1846 return;
1847
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001848 switch (motg->pdata->phy_type) {
1849 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301850 /* Disable ID_GND in link and PHY */
1851 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1852 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001853 ulpi_write(phy, 0x01, 0x0C);
1854 ulpi_write(phy, 0x10, 0x0F);
1855 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301856 /* Disable PMIC ID pull-up */
1857 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301858 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001859 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301860 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001861 break;
1862 default:
1863 break;
1864 }
1865}
1866
1867static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1868{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001869 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001870
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301871 if (!aca_enabled())
1872 return;
1873
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001874 switch (motg->pdata->phy_type) {
1875 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301876 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001877 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301878 break;
1879 default:
1880 break;
1881 }
1882}
1883
1884static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1885{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001886 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301887
1888 if (!aca_enabled())
1889 return;
1890
1891 switch (motg->pdata->phy_type) {
1892 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001893 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001894 break;
1895 default:
1896 break;
1897 }
1898}
1899
1900static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1901{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001902 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001903 bool ret = false;
1904
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301905 if (!aca_enabled())
1906 return ret;
1907
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 switch (motg->pdata->phy_type) {
1909 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001910 if (ulpi_read(phy, 0x91) & 1) {
1911 dev_dbg(phy->dev, "RID change\n");
1912 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001913 ret = msm_chg_aca_detect(motg);
1914 }
1915 default:
1916 break;
1917 }
1918 return ret;
1919}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301920
1921static void msm_otg_id_timer_func(unsigned long data)
1922{
1923 struct msm_otg *motg = (struct msm_otg *) data;
1924
1925 if (!aca_enabled())
1926 return;
1927
1928 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001929 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301930 return;
1931 }
1932
Steve Mucklef132c6c2012-06-06 18:30:57 -07001933 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301934 goto out;
1935
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301936 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001937 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301938 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301939 }
1940
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301941out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301942 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1943 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1944}
1945
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301946static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1947{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001948 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301949 u32 chg_det;
1950 bool ret = false;
1951
1952 switch (motg->pdata->phy_type) {
1953 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001954 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301955 ret = chg_det & (1 << 4);
1956 break;
1957 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001958 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301959 ret = chg_det & 1;
1960 break;
1961 default:
1962 break;
1963 }
1964 return ret;
1965}
1966
1967static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1968{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001969 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301970 u32 chg_det;
1971
1972 switch (motg->pdata->phy_type) {
1973 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001974 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301975 /* Turn off charger block */
1976 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001977 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301978 udelay(20);
1979 /* control chg block via ULPI */
1980 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001981 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301982 /* put it in host mode for enabling D- source */
1983 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001984 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301985 /* Turn on chg detect block */
1986 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001987 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301988 udelay(20);
1989 /* enable chg detection */
1990 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001991 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301992 break;
1993 case SNPS_28NM_INTEGRATED_PHY:
1994 /*
1995 * Configure DM as current source, DP as current sink
1996 * and enable battery charging comparators.
1997 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001998 ulpi_write(phy, 0x8, 0x85);
1999 ulpi_write(phy, 0x2, 0x85);
2000 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302001 break;
2002 default:
2003 break;
2004 }
2005}
2006
2007static bool msm_chg_check_primary_det(struct msm_otg *motg)
2008{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002009 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302010 u32 chg_det;
2011 bool ret = false;
2012
2013 switch (motg->pdata->phy_type) {
2014 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002015 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302016 ret = chg_det & (1 << 4);
2017 break;
2018 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002019 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302020 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302021 /* Turn off VDP_SRC */
2022 ulpi_write(phy, 0x3, 0x86);
2023 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302024 break;
2025 default:
2026 break;
2027 }
2028 return ret;
2029}
2030
2031static void msm_chg_enable_primary_det(struct msm_otg *motg)
2032{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002033 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302034 u32 chg_det;
2035
2036 switch (motg->pdata->phy_type) {
2037 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002038 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302039 /* enable chg detection */
2040 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002041 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302042 break;
2043 case SNPS_28NM_INTEGRATED_PHY:
2044 /*
2045 * Configure DP as current source, DM as current sink
2046 * and enable battery charging comparators.
2047 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002048 ulpi_write(phy, 0x2, 0x85);
2049 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302050 break;
2051 default:
2052 break;
2053 }
2054}
2055
2056static bool msm_chg_check_dcd(struct msm_otg *motg)
2057{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002058 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302059 u32 line_state;
2060 bool ret = false;
2061
2062 switch (motg->pdata->phy_type) {
2063 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002064 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302065 ret = !(line_state & 1);
2066 break;
2067 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002068 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302069 ret = line_state & 2;
2070 break;
2071 default:
2072 break;
2073 }
2074 return ret;
2075}
2076
2077static void msm_chg_disable_dcd(struct msm_otg *motg)
2078{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002079 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302080 u32 chg_det;
2081
2082 switch (motg->pdata->phy_type) {
2083 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002084 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302085 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002086 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302087 break;
2088 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002089 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302090 break;
2091 default:
2092 break;
2093 }
2094}
2095
2096static void msm_chg_enable_dcd(struct msm_otg *motg)
2097{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002098 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302099 u32 chg_det;
2100
2101 switch (motg->pdata->phy_type) {
2102 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002103 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302104 /* Turn on D+ current source */
2105 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002106 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302107 break;
2108 case SNPS_28NM_INTEGRATED_PHY:
2109 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002110 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302111 break;
2112 default:
2113 break;
2114 }
2115}
2116
2117static void msm_chg_block_on(struct msm_otg *motg)
2118{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002119 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302120 u32 func_ctrl, chg_det;
2121
2122 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002123 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302124 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2125 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002126 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302127
2128 switch (motg->pdata->phy_type) {
2129 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002130 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302131 /* control chg block via ULPI */
2132 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002133 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302134 /* Turn on chg detect block */
2135 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002136 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302137 udelay(20);
2138 break;
2139 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302140 /* disable DP and DM pull down resistors */
2141 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302142 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002143 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302144 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002145 ulpi_write(phy, 0x1F, 0x92);
2146 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302147 udelay(100);
2148 break;
2149 default:
2150 break;
2151 }
2152}
2153
2154static void msm_chg_block_off(struct msm_otg *motg)
2155{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002156 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302157 u32 func_ctrl, chg_det;
2158
2159 switch (motg->pdata->phy_type) {
2160 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002161 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302162 /* Turn off charger block */
2163 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002164 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302165 break;
2166 case SNPS_28NM_INTEGRATED_PHY:
2167 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002168 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302169 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002170 ulpi_write(phy, 0x1F, 0x92);
2171 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302172 break;
2173 default:
2174 break;
2175 }
2176
2177 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002178 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302179 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2180 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002181 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302182}
2183
Anji jonnalad270e2d2011-08-09 11:28:32 +05302184static const char *chg_to_string(enum usb_chg_type chg_type)
2185{
2186 switch (chg_type) {
2187 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2188 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2189 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2190 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2191 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2192 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2193 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302194 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302195 default: return "INVALID_CHARGER";
2196 }
2197}
2198
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302199#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2200#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302201#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2202#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302203static void msm_chg_detect_work(struct work_struct *w)
2204{
2205 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002206 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302207 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302208 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302209 unsigned long delay;
2210
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002211 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302212
2213 if (test_bit(MHL, &motg->inputs)) {
2214 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2215 return;
2216 }
2217
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302218 switch (motg->chg_state) {
2219 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302220 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302221 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002222 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302223 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302224 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302225 delay = MSM_CHG_DCD_POLL_TIME;
2226 break;
2227 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302228 if (msm_chg_mhl_detect(motg)) {
2229 msm_chg_block_off(motg);
2230 motg->chg_state = USB_CHG_STATE_DETECTED;
2231 motg->chg_type = USB_INVALID_CHARGER;
2232 queue_work(system_nrt_wq, &motg->sm_work);
2233 return;
2234 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002235 is_aca = msm_chg_aca_detect(motg);
2236 if (is_aca) {
2237 /*
2238 * ID_A can be ACA dock too. continue
2239 * primary detection after DCD.
2240 */
2241 if (test_bit(ID_A, &motg->inputs)) {
2242 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2243 } else {
2244 delay = 0;
2245 break;
2246 }
2247 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302248 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302249 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2250 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302251 if (is_dcd || tmout) {
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302252 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302253 msm_chg_enable_primary_det(motg);
2254 delay = MSM_CHG_PRIMARY_DET_TIME;
2255 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2256 } else {
2257 delay = MSM_CHG_DCD_POLL_TIME;
2258 }
2259 break;
2260 case USB_CHG_STATE_DCD_DONE:
2261 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302262 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2263 dm_vlgc = line_state & PORTSC_LS_DM;
2264 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302265 if (test_bit(ID_A, &motg->inputs)) {
2266 motg->chg_type = USB_ACA_DOCK_CHARGER;
2267 motg->chg_state = USB_CHG_STATE_DETECTED;
2268 delay = 0;
2269 break;
2270 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302271 if (line_state) { /* DP > VLGC */
2272 motg->chg_type = USB_PROPRIETARY_CHARGER;
2273 motg->chg_state = USB_CHG_STATE_DETECTED;
2274 delay = 0;
2275 } else {
2276 msm_chg_enable_secondary_det(motg);
2277 delay = MSM_CHG_SECONDARY_DET_TIME;
2278 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2279 }
2280 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302281 if (test_bit(ID_A, &motg->inputs)) {
2282 motg->chg_type = USB_ACA_A_CHARGER;
2283 motg->chg_state = USB_CHG_STATE_DETECTED;
2284 delay = 0;
2285 break;
2286 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302287
2288 if (line_state) /* DP > VLGC or/and DM > VLGC */
2289 motg->chg_type = USB_PROPRIETARY_CHARGER;
2290 else
2291 motg->chg_type = USB_SDP_CHARGER;
2292
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302293 motg->chg_state = USB_CHG_STATE_DETECTED;
2294 delay = 0;
2295 }
2296 break;
2297 case USB_CHG_STATE_PRIMARY_DONE:
2298 vout = msm_chg_check_secondary_det(motg);
2299 if (vout)
2300 motg->chg_type = USB_DCP_CHARGER;
2301 else
2302 motg->chg_type = USB_CDP_CHARGER;
2303 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2304 /* fall through */
2305 case USB_CHG_STATE_SECONDARY_DONE:
2306 motg->chg_state = USB_CHG_STATE_DETECTED;
2307 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302308 /*
2309 * Notify the charger type to power supply
2310 * owner as soon as we determine the charger.
2311 */
2312 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302313 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002314 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302315 /*
2316 * Spurious interrupt is seen after enabling ACA detection
2317 * due to which charger detection fails in case of PET.
2318 * Add delay of 100 microsec to avoid that.
2319 */
2320 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002321 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002322 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302323 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302324 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302325 return;
2326 default:
2327 return;
2328 }
2329
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302330 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302331}
2332
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302333/*
2334 * We support OTG, Peripheral only and Host only configurations. In case
2335 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2336 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2337 * enabled when switch is controlled by user and default mode is supplied
2338 * by board file, which can be changed by userspace later.
2339 */
2340static void msm_otg_init_sm(struct msm_otg *motg)
2341{
2342 struct msm_otg_platform_data *pdata = motg->pdata;
2343 u32 otgsc = readl(USB_OTGSC);
2344
2345 switch (pdata->mode) {
2346 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002347 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302348 if (pdata->default_mode == USB_HOST) {
2349 clear_bit(ID, &motg->inputs);
2350 } else if (pdata->default_mode == USB_PERIPHERAL) {
2351 set_bit(ID, &motg->inputs);
2352 set_bit(B_SESS_VLD, &motg->inputs);
2353 } else {
2354 set_bit(ID, &motg->inputs);
2355 clear_bit(B_SESS_VLD, &motg->inputs);
2356 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302357 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302358 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302359 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302360 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302361 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302362 set_bit(A_BUS_REQ, &motg->inputs);
2363 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002364 if (otgsc & OTGSC_BSV)
2365 set_bit(B_SESS_VLD, &motg->inputs);
2366 else
2367 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302368 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302369 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302370 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302371 set_bit(ID, &motg->inputs);
2372 else
2373 clear_bit(ID, &motg->inputs);
2374 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302375 /*
2376 * VBUS initial state is reported after PMIC
2377 * driver initialization. Wait for it.
2378 */
2379 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302380 }
2381 break;
2382 case USB_HOST:
2383 clear_bit(ID, &motg->inputs);
2384 break;
2385 case USB_PERIPHERAL:
2386 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302387 if (pdata->otg_control == OTG_PHY_CONTROL) {
2388 if (otgsc & OTGSC_BSV)
2389 set_bit(B_SESS_VLD, &motg->inputs);
2390 else
2391 clear_bit(B_SESS_VLD, &motg->inputs);
2392 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2393 /*
2394 * VBUS initial state is reported after PMIC
2395 * driver initialization. Wait for it.
2396 */
2397 wait_for_completion(&pmic_vbus_init);
2398 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302399 break;
2400 default:
2401 break;
2402 }
2403}
2404
2405static void msm_otg_sm_work(struct work_struct *w)
2406{
2407 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002408 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302409 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302410
Steve Mucklef132c6c2012-06-06 18:30:57 -07002411 pm_runtime_resume(otg->phy->dev);
2412 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002413 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302414 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002415 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302416 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002417 if (!psy && legacy_power_supply) {
2418 psy = power_supply_get_by_name("usb");
2419
2420 if (!psy)
2421 pr_err("couldn't get usb power supply\n");
2422 }
2423
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002424 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302425 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2426 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002427 pm_runtime_put_noidle(otg->phy->dev);
2428 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302429 break;
2430 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302431 /* FALL THROUGH */
2432 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302433 if (test_bit(MHL, &motg->inputs)) {
2434 /* allow LPM */
2435 pm_runtime_put_noidle(otg->phy->dev);
2436 pm_runtime_suspend(otg->phy->dev);
2437 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002438 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302439 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302440 if (msm_chg_mhl_detect(motg)) {
2441 work = 1;
2442 break;
2443 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302444 clear_bit(B_BUS_REQ, &motg->inputs);
2445 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002446 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302447 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302448 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302449 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302450 switch (motg->chg_state) {
2451 case USB_CHG_STATE_UNDEFINED:
2452 msm_chg_detect_work(&motg->chg_work.work);
2453 break;
2454 case USB_CHG_STATE_DETECTED:
2455 switch (motg->chg_type) {
2456 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302457 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002458 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302459 /* fall through */
2460 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302461 msm_otg_notify_charger(motg,
2462 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002463 pm_runtime_put_noidle(otg->phy->dev);
2464 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302465 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302466 case USB_ACA_B_CHARGER:
2467 msm_otg_notify_charger(motg,
2468 IDEV_ACA_CHG_MAX);
2469 /*
2470 * (ID_B --> ID_C) PHY_ALT interrupt can
2471 * not be detected in LPM.
2472 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302473 break;
2474 case USB_CDP_CHARGER:
2475 msm_otg_notify_charger(motg,
2476 IDEV_CHG_MAX);
2477 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002478 otg->phy->state =
2479 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302480 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302481 case USB_ACA_C_CHARGER:
2482 msm_otg_notify_charger(motg,
2483 IDEV_ACA_CHG_MAX);
2484 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002485 otg->phy->state =
2486 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302487 break;
2488 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302489 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002490 otg->phy->state =
2491 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302492 mod_timer(&motg->chg_check_timer,
2493 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302494 break;
2495 default:
2496 break;
2497 }
2498 break;
2499 default:
2500 break;
2501 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302502 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2503 pr_debug("b_sess_end && b_bus_req\n");
2504 if (msm_otg_start_srp(otg) < 0) {
2505 clear_bit(B_BUS_REQ, &motg->inputs);
2506 work = 1;
2507 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302508 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002509 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302510 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2511 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302512 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302513 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302514 del_timer_sync(&motg->chg_check_timer);
2515 clear_bit(B_FALSE_SDP, &motg->inputs);
Mayank Rana8ab00352013-01-23 19:26:21 +05302516 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302517 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302518 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2519 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302520 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002521 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302522 /*
2523 * There is a small window where ID interrupt
2524 * is not monitored during ID detection circuit
2525 * switch from ACA to PMIC. Check ID state
2526 * before entering into low power mode.
2527 */
2528 if (!msm_otg_read_pmic_id_state(motg)) {
2529 pr_debug("process missed ID intr\n");
2530 clear_bit(ID, &motg->inputs);
2531 work = 1;
2532 break;
2533 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002534 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002535 /*
2536 * Only if autosuspend was enabled in probe, it will be
2537 * used here. Otherwise, no delay will be used.
2538 */
2539 pm_runtime_mark_last_busy(otg->phy->dev);
2540 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302541 }
2542 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302543 case OTG_STATE_B_SRP_INIT:
2544 if (!test_bit(ID, &motg->inputs) ||
2545 test_bit(ID_A, &motg->inputs) ||
2546 test_bit(ID_C, &motg->inputs) ||
2547 (test_bit(B_SESS_VLD, &motg->inputs) &&
2548 !test_bit(ID_B, &motg->inputs))) {
2549 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2550 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002551 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302552 /*
2553 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2554 * bits after SRP initiation.
2555 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002556 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302557 work = 1;
2558 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2559 pr_debug("b_srp_fail\n");
2560 pr_info("A-device did not respond to SRP\n");
2561 clear_bit(B_BUS_REQ, &motg->inputs);
2562 clear_bit(B_SRP_FAIL, &motg->tmouts);
2563 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002564 ulpi_write(otg->phy, 0x0, 0x98);
2565 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302566 motg->b_last_se0_sess = jiffies;
2567 work = 1;
2568 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302569 break;
2570 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302571 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2572 test_bit(B_FALSE_SDP, &motg->inputs)) {
2573 pr_debug("B_FALSE_SDP\n");
2574 msm_otg_start_peripheral(otg, 0);
2575 motg->chg_type = USB_DCP_CHARGER;
2576 clear_bit(B_FALSE_SDP, &motg->inputs);
2577 otg->phy->state = OTG_STATE_B_IDLE;
2578 work = 1;
2579 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302580 test_bit(ID_A, &motg->inputs) ||
2581 test_bit(ID_B, &motg->inputs) ||
2582 !test_bit(B_SESS_VLD, &motg->inputs)) {
2583 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302584 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2585 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302586 msm_otg_notify_charger(motg, 0);
2587 srp_reqd = otg->gadget->otg_srp_reqd;
2588 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302589 if (test_bit(ID_B, &motg->inputs))
2590 clear_bit(ID_B, &motg->inputs);
2591 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002592 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302593 motg->b_last_se0_sess = jiffies;
2594 if (srp_reqd)
2595 msm_otg_start_timer(motg,
2596 TB_TST_SRP, B_TST_SRP);
2597 else
2598 work = 1;
2599 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2600 otg->gadget->b_hnp_enable &&
2601 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2602 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2603 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2604 /* D+ pullup should not be disconnected within 4msec
2605 * after A device suspends the bus. Otherwise PET will
2606 * fail the compliance test.
2607 */
2608 udelay(1000);
2609 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002610 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302611 /*
2612 * start HCD even before A-device enable
2613 * pull-up to meet HNP timings.
2614 */
2615 otg->host->is_b_host = 1;
2616 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002617 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2618 test_bit(B_SESS_VLD, &motg->inputs)) {
2619 pr_debug("a_bus_suspend && b_sess_vld\n");
2620 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002621 pm_runtime_put_noidle(otg->phy->dev);
2622 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002623 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002624 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302625 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002626 }
2627 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302628 case OTG_STATE_B_WAIT_ACON:
2629 if (!test_bit(ID, &motg->inputs) ||
2630 test_bit(ID_A, &motg->inputs) ||
2631 test_bit(ID_B, &motg->inputs) ||
2632 !test_bit(B_SESS_VLD, &motg->inputs)) {
2633 pr_debug("!id || id_a/b || !b_sess_vld\n");
2634 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302635 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302636 * A-device is physically disconnected during
2637 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302638 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302639 msm_otg_start_host(otg, 0);
2640 otg->host->is_b_host = 0;
2641
2642 clear_bit(B_BUS_REQ, &motg->inputs);
2643 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2644 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002645 otg->phy->state = OTG_STATE_B_IDLE;
2646 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302647 work = 1;
2648 } else if (test_bit(A_CONN, &motg->inputs)) {
2649 pr_debug("a_conn\n");
2650 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002651 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302652 /*
2653 * PET disconnects D+ pullup after reset is generated
2654 * by B device in B_HOST role which is not detected by
2655 * B device. As workaorund , start timer of 300msec
2656 * and stop timer if A device is enumerated else clear
2657 * A_CONN.
2658 */
2659 msm_otg_start_timer(motg, TB_TST_CONFIG,
2660 B_TST_CONFIG);
2661 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2662 pr_debug("b_ase0_brst_tmout\n");
2663 pr_info("B HNP fail:No response from A device\n");
2664 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002665 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302666 otg->host->is_b_host = 0;
2667 clear_bit(B_ASE0_BRST, &motg->tmouts);
2668 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2669 clear_bit(B_BUS_REQ, &motg->inputs);
2670 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002671 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302672 work = 1;
2673 } else if (test_bit(ID_C, &motg->inputs)) {
2674 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2675 }
2676 break;
2677 case OTG_STATE_B_HOST:
2678 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2679 !test_bit(A_CONN, &motg->inputs) ||
2680 !test_bit(B_SESS_VLD, &motg->inputs)) {
2681 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2682 clear_bit(A_CONN, &motg->inputs);
2683 clear_bit(B_BUS_REQ, &motg->inputs);
2684 msm_otg_start_host(otg, 0);
2685 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002686 otg->phy->state = OTG_STATE_B_IDLE;
2687 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302688 work = 1;
2689 } else if (test_bit(ID_C, &motg->inputs)) {
2690 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2691 }
2692 break;
2693 case OTG_STATE_A_IDLE:
2694 otg->default_a = 1;
2695 if (test_bit(ID, &motg->inputs) &&
2696 !test_bit(ID_A, &motg->inputs)) {
2697 pr_debug("id && !id_a\n");
2698 otg->default_a = 0;
2699 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002700 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302701 del_timer_sync(&motg->id_timer);
2702 msm_otg_link_reset(motg);
2703 msm_chg_enable_aca_intr(motg);
2704 msm_otg_notify_charger(motg, 0);
2705 work = 1;
2706 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2707 (test_bit(A_SRP_DET, &motg->inputs) ||
2708 test_bit(A_BUS_REQ, &motg->inputs))) {
2709 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2710
2711 clear_bit(A_SRP_DET, &motg->inputs);
2712 /* Disable SRP detection */
2713 writel_relaxed((readl_relaxed(USB_OTGSC) &
2714 ~OTGSC_INTSTS_MASK) &
2715 ~OTGSC_DPIE, USB_OTGSC);
2716
Steve Mucklef132c6c2012-06-06 18:30:57 -07002717 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302718 /* VBUS should not be supplied before end of SRP pulse
2719 * generated by PET, if not complaince test fail.
2720 */
2721 usleep_range(10000, 12000);
2722 /* ACA: ID_A: Stop charging untill enumeration */
2723 if (test_bit(ID_A, &motg->inputs))
2724 msm_otg_notify_charger(motg, 0);
2725 else
2726 msm_hsusb_vbus_power(motg, 1);
2727 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2728 } else {
2729 pr_debug("No session requested\n");
2730 clear_bit(A_BUS_DROP, &motg->inputs);
2731 if (test_bit(ID_A, &motg->inputs)) {
2732 msm_otg_notify_charger(motg,
2733 IDEV_ACA_CHG_MAX);
2734 } else if (!test_bit(ID, &motg->inputs)) {
2735 msm_otg_notify_charger(motg, 0);
2736 /*
2737 * A-device is not providing power on VBUS.
2738 * Enable SRP detection.
2739 */
2740 writel_relaxed(0x13, USB_USBMODE);
2741 writel_relaxed((readl_relaxed(USB_OTGSC) &
2742 ~OTGSC_INTSTS_MASK) |
2743 OTGSC_DPIE, USB_OTGSC);
2744 mb();
2745 }
2746 }
2747 break;
2748 case OTG_STATE_A_WAIT_VRISE:
2749 if ((test_bit(ID, &motg->inputs) &&
2750 !test_bit(ID_A, &motg->inputs)) ||
2751 test_bit(A_BUS_DROP, &motg->inputs) ||
2752 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2753 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2754 clear_bit(A_BUS_REQ, &motg->inputs);
2755 msm_otg_del_timer(motg);
2756 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002757 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302758 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2759 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2760 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002761 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302762 if (TA_WAIT_BCON > 0)
2763 msm_otg_start_timer(motg, TA_WAIT_BCON,
2764 A_WAIT_BCON);
2765 msm_otg_start_host(otg, 1);
2766 msm_chg_enable_aca_det(motg);
2767 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302768 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302769 if (msm_chg_check_aca_intr(motg))
2770 work = 1;
2771 }
2772 break;
2773 case OTG_STATE_A_WAIT_BCON:
2774 if ((test_bit(ID, &motg->inputs) &&
2775 !test_bit(ID_A, &motg->inputs)) ||
2776 test_bit(A_BUS_DROP, &motg->inputs) ||
2777 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2778 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2779 "a_wait_bcon_tmout\n");
2780 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2781 pr_info("Device No Response\n");
2782 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2783 }
2784 msm_otg_del_timer(motg);
2785 clear_bit(A_BUS_REQ, &motg->inputs);
2786 clear_bit(B_CONN, &motg->inputs);
2787 msm_otg_start_host(otg, 0);
2788 /*
2789 * ACA: ID_A with NO accessory, just the A plug is
2790 * attached to ACA: Use IDCHG_MAX for charging
2791 */
2792 if (test_bit(ID_A, &motg->inputs))
2793 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2794 else
2795 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002796 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302797 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2798 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2799 pr_debug("!a_vbus_vld\n");
2800 clear_bit(B_CONN, &motg->inputs);
2801 msm_otg_del_timer(motg);
2802 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002803 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2804 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302805 } else if (test_bit(ID_A, &motg->inputs)) {
2806 msm_hsusb_vbus_power(motg, 0);
2807 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2808 /*
2809 * If TA_WAIT_BCON is infinite, we don;t
2810 * turn off VBUS. Enter low power mode.
2811 */
2812 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002813 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302814 } else if (!test_bit(ID, &motg->inputs)) {
2815 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302816 }
2817 break;
2818 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302819 if ((test_bit(ID, &motg->inputs) &&
2820 !test_bit(ID_A, &motg->inputs)) ||
2821 test_bit(A_BUS_DROP, &motg->inputs)) {
2822 pr_debug("id_a/b/c || a_bus_drop\n");
2823 clear_bit(B_CONN, &motg->inputs);
2824 clear_bit(A_BUS_REQ, &motg->inputs);
2825 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002826 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302827 msm_otg_start_host(otg, 0);
2828 if (!test_bit(ID_A, &motg->inputs))
2829 msm_hsusb_vbus_power(motg, 0);
2830 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2831 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2832 pr_debug("!a_vbus_vld\n");
2833 clear_bit(B_CONN, &motg->inputs);
2834 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002835 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302836 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002837 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302838 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2839 /*
2840 * a_bus_req is de-asserted when root hub is
2841 * suspended or HNP is in progress.
2842 */
2843 pr_debug("!a_bus_req\n");
2844 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002845 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302846 if (otg->host->b_hnp_enable)
2847 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2848 A_AIDL_BDIS);
2849 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002850 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302851 } else if (!test_bit(B_CONN, &motg->inputs)) {
2852 pr_debug("!b_conn\n");
2853 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002854 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302855 if (TA_WAIT_BCON > 0)
2856 msm_otg_start_timer(motg, TA_WAIT_BCON,
2857 A_WAIT_BCON);
2858 if (msm_chg_check_aca_intr(motg))
2859 work = 1;
2860 } else if (test_bit(ID_A, &motg->inputs)) {
2861 msm_otg_del_timer(motg);
2862 msm_hsusb_vbus_power(motg, 0);
2863 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2864 msm_otg_notify_charger(motg,
2865 IDEV_ACA_CHG_MAX);
2866 else
2867 msm_otg_notify_charger(motg,
2868 IDEV_CHG_MIN - motg->mA_port);
2869 } else if (!test_bit(ID, &motg->inputs)) {
2870 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2871 motg->chg_type = USB_INVALID_CHARGER;
2872 msm_otg_notify_charger(motg, 0);
2873 msm_hsusb_vbus_power(motg, 1);
2874 }
2875 break;
2876 case OTG_STATE_A_SUSPEND:
2877 if ((test_bit(ID, &motg->inputs) &&
2878 !test_bit(ID_A, &motg->inputs)) ||
2879 test_bit(A_BUS_DROP, &motg->inputs) ||
2880 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2881 pr_debug("id_a/b/c || a_bus_drop ||"
2882 "a_aidl_bdis_tmout\n");
2883 msm_otg_del_timer(motg);
2884 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002885 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302886 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002887 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302888 if (!test_bit(ID_A, &motg->inputs))
2889 msm_hsusb_vbus_power(motg, 0);
2890 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2891 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2892 pr_debug("!a_vbus_vld\n");
2893 msm_otg_del_timer(motg);
2894 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002895 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302896 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002897 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302898 } else if (!test_bit(B_CONN, &motg->inputs) &&
2899 otg->host->b_hnp_enable) {
2900 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002901 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302902 msm_otg_host_hnp_enable(otg, 1);
2903 otg->gadget->is_a_peripheral = 1;
2904 msm_otg_start_peripheral(otg, 1);
2905 } else if (!test_bit(B_CONN, &motg->inputs) &&
2906 !otg->host->b_hnp_enable) {
2907 pr_debug("!b_conn && !b_hnp_enable");
2908 /*
2909 * bus request is dropped during suspend.
2910 * acquire again for next device.
2911 */
2912 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002913 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302914 if (TA_WAIT_BCON > 0)
2915 msm_otg_start_timer(motg, TA_WAIT_BCON,
2916 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002917 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302918 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002919 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302920 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002921 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002922 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302923 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302924 }
2925 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302926 case OTG_STATE_A_PERIPHERAL:
2927 if ((test_bit(ID, &motg->inputs) &&
2928 !test_bit(ID_A, &motg->inputs)) ||
2929 test_bit(A_BUS_DROP, &motg->inputs)) {
2930 pr_debug("id _f/b/c || a_bus_drop\n");
2931 /* Clear BIDL_ADIS timer */
2932 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002933 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302934 msm_otg_start_peripheral(otg, 0);
2935 otg->gadget->is_a_peripheral = 0;
2936 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002937 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302938 if (!test_bit(ID_A, &motg->inputs))
2939 msm_hsusb_vbus_power(motg, 0);
2940 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2941 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2942 pr_debug("!a_vbus_vld\n");
2943 /* Clear BIDL_ADIS timer */
2944 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002945 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302946 msm_otg_start_peripheral(otg, 0);
2947 otg->gadget->is_a_peripheral = 0;
2948 msm_otg_start_host(otg, 0);
2949 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2950 pr_debug("a_bidl_adis_tmout\n");
2951 msm_otg_start_peripheral(otg, 0);
2952 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002953 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302954 set_bit(A_BUS_REQ, &motg->inputs);
2955 msm_otg_host_hnp_enable(otg, 0);
2956 if (TA_WAIT_BCON > 0)
2957 msm_otg_start_timer(motg, TA_WAIT_BCON,
2958 A_WAIT_BCON);
2959 } else if (test_bit(ID_A, &motg->inputs)) {
2960 msm_hsusb_vbus_power(motg, 0);
2961 msm_otg_notify_charger(motg,
2962 IDEV_CHG_MIN - motg->mA_port);
2963 } else if (!test_bit(ID, &motg->inputs)) {
2964 msm_otg_notify_charger(motg, 0);
2965 msm_hsusb_vbus_power(motg, 1);
2966 }
2967 break;
2968 case OTG_STATE_A_WAIT_VFALL:
2969 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2970 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002971 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302972 work = 1;
2973 }
2974 break;
2975 case OTG_STATE_A_VBUS_ERR:
2976 if ((test_bit(ID, &motg->inputs) &&
2977 !test_bit(ID_A, &motg->inputs)) ||
2978 test_bit(A_BUS_DROP, &motg->inputs) ||
2979 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002980 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302981 if (!test_bit(ID_A, &motg->inputs))
2982 msm_hsusb_vbus_power(motg, 0);
2983 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2984 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2985 motg->chg_type = USB_INVALID_CHARGER;
2986 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302987 }
2988 break;
2989 default:
2990 break;
2991 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302992 if (work)
2993 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302994}
2995
Amit Blayd0fe07b2012-09-05 16:42:09 +03002996static void msm_otg_suspend_work(struct work_struct *w)
2997{
2998 struct msm_otg *motg =
2999 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05303000
3001 /* This work is only for device bus suspend */
3002 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
3003 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003004}
3005
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303006static irqreturn_t msm_otg_irq(int irq, void *data)
3007{
3008 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003009 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303010 u32 otgsc = 0, usbsts, pc;
3011 bool work = 0;
3012 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303013
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303014 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07003015 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303016 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07003017 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07003018 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07003019 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303020 return IRQ_HANDLED;
3021 }
3022
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003023 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303024 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303025
3026 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303027 return IRQ_NONE;
3028
3029 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303030 if (otgsc & OTGSC_ID) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003031 dev_dbg(otg->phy->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303032 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303033 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003034 dev_dbg(otg->phy->dev, "ID clear\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303035 /*
3036 * Assert a_bus_req to supply power on
3037 * VBUS when Micro/Mini-A cable is connected
3038 * with out user intervention.
3039 */
3040 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303041 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303042 msm_chg_enable_aca_det(motg);
3043 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303044 writel_relaxed(otgsc, USB_OTGSC);
3045 work = 1;
3046 } else if (otgsc & OTGSC_DPIS) {
3047 pr_debug("DPIS detected\n");
3048 writel_relaxed(otgsc, USB_OTGSC);
3049 set_bit(A_SRP_DET, &motg->inputs);
3050 set_bit(A_BUS_REQ, &motg->inputs);
3051 work = 1;
Vamsi Krishnaef6e1bf2013-03-02 15:36:17 -08003052 } else if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303053 writel_relaxed(otgsc, USB_OTGSC);
3054 /*
3055 * BSV interrupt comes when operating as an A-device
3056 * (VBUS on/off).
3057 * But, handle BSV when charger is removed from ACA in ID_A
3058 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003059 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303060 !test_bit(ID_A, &motg->inputs))
3061 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303062 if (otgsc & OTGSC_BSV) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003063 dev_dbg(otg->phy->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303064 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303065 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003066 dev_dbg(otg->phy->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303067 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003068 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3069
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303070 msm_chg_check_aca_intr(motg);
3071 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303072 work = 1;
3073 } else if (usbsts & STS_PCI) {
3074 pc = readl_relaxed(USB_PORTSC);
3075 pr_debug("portsc = %x\n", pc);
3076 ret = IRQ_NONE;
3077 /*
3078 * HCD Acks PCI interrupt. We use this to switch
3079 * between different OTG states.
3080 */
3081 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003082 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303083 case OTG_STATE_A_SUSPEND:
3084 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3085 !(pc & PORTSC_CCS)) {
3086 pr_debug("B_CONN clear\n");
3087 clear_bit(B_CONN, &motg->inputs);
3088 msm_otg_del_timer(motg);
3089 }
3090 break;
3091 case OTG_STATE_A_PERIPHERAL:
3092 /*
3093 * A-peripheral observed activity on bus.
3094 * clear A_BIDL_ADIS timer.
3095 */
3096 msm_otg_del_timer(motg);
3097 work = 0;
3098 break;
3099 case OTG_STATE_B_WAIT_ACON:
3100 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3101 pr_debug("A_CONN set\n");
3102 set_bit(A_CONN, &motg->inputs);
3103 /* Clear ASE0_BRST timer */
3104 msm_otg_del_timer(motg);
3105 }
3106 break;
3107 case OTG_STATE_B_HOST:
3108 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3109 pr_debug("A_CONN clear\n");
3110 clear_bit(A_CONN, &motg->inputs);
3111 msm_otg_del_timer(motg);
3112 }
3113 break;
3114 case OTG_STATE_A_WAIT_BCON:
3115 if (TA_WAIT_BCON < 0)
3116 set_bit(A_BUS_REQ, &motg->inputs);
3117 default:
3118 work = 0;
3119 break;
3120 }
3121 } else if (usbsts & STS_URI) {
3122 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003123 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303124 case OTG_STATE_A_PERIPHERAL:
3125 /*
3126 * A-peripheral observed activity on bus.
3127 * clear A_BIDL_ADIS timer.
3128 */
3129 msm_otg_del_timer(motg);
3130 work = 0;
3131 break;
3132 default:
3133 work = 0;
3134 break;
3135 }
3136 } else if (usbsts & STS_SLI) {
3137 ret = IRQ_NONE;
3138 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003139 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303140 case OTG_STATE_B_PERIPHERAL:
3141 if (otg->gadget->b_hnp_enable) {
3142 set_bit(A_BUS_SUSPEND, &motg->inputs);
3143 set_bit(B_BUS_REQ, &motg->inputs);
3144 work = 1;
3145 }
3146 break;
3147 case OTG_STATE_A_PERIPHERAL:
3148 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3149 A_BIDL_ADIS);
3150 break;
3151 default:
3152 break;
3153 }
3154 } else if ((usbsts & PHY_ALT_INT)) {
3155 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3156 if (msm_chg_check_aca_intr(motg))
3157 work = 1;
3158 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303159 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303160 if (work)
3161 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303162
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303163 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003164}
3165
3166static void msm_otg_set_vbus_state(int online)
3167{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303168 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003169 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303170
Vamsi Krishna945b4a92013-03-02 15:31:16 -08003171 if (online) {
3172 pr_debug("PMIC: BSV set\n");
3173 set_bit(B_SESS_VLD, &motg->inputs);
3174 } else {
3175 pr_debug("PMIC: BSV clear\n");
3176 clear_bit(B_SESS_VLD, &motg->inputs);
3177 }
3178
3179 /* do not queue state m/c work if id is grounded */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303180 if (!test_bit(ID, &motg->inputs)) {
3181 /*
3182 * state machine work waits for initial VBUS
3183 * completion in UNDEFINED state. Process
3184 * the initial VBUS event in ID_GND state.
3185 */
3186 if (init)
3187 return;
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303188 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003189
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303190 if (!init) {
3191 init = true;
3192 complete(&pmic_vbus_init);
3193 pr_debug("PMIC: BSV init complete\n");
3194 return;
3195 }
3196
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303197 if (test_bit(MHL, &motg->inputs) ||
3198 mhl_det_in_progress) {
3199 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3200 return;
3201 }
3202
Jack Pham5ca279b2012-05-14 18:42:54 -07003203 if (atomic_read(&motg->pm_suspended))
3204 motg->sm_work_pending = true;
3205 else
3206 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003207}
3208
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303209static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003210{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303211 struct msm_otg *motg = container_of(w, struct msm_otg,
3212 pmic_id_status_work.work);
3213 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003214
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303215 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303216 if (!test_and_set_bit(ID, &motg->inputs)) {
3217 pr_debug("PMIC: ID set\n");
3218 work = 1;
3219 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303220 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303221 if (test_and_clear_bit(ID, &motg->inputs)) {
3222 pr_debug("PMIC: ID clear\n");
3223 set_bit(A_BUS_REQ, &motg->inputs);
3224 work = 1;
3225 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303226 }
3227
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303228 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003229 if (atomic_read(&motg->pm_suspended))
3230 motg->sm_work_pending = true;
3231 else
3232 queue_work(system_nrt_wq, &motg->sm_work);
3233 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303234
3235}
3236
3237#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3238static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3239{
3240 struct msm_otg *motg = data;
3241
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303242 if (test_bit(MHL, &motg->inputs) ||
3243 mhl_det_in_progress) {
3244 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3245 return IRQ_HANDLED;
3246 }
3247
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303248 if (!aca_id_turned_on)
3249 /*schedule delayed work for 5msec for ID line state to settle*/
3250 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3251 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003252
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303253 return IRQ_HANDLED;
3254}
3255
3256static int msm_otg_mode_show(struct seq_file *s, void *unused)
3257{
3258 struct msm_otg *motg = s->private;
Stephen Boyd9850acb2013-01-28 14:11:20 -08003259 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303260
Stephen Boyd9850acb2013-01-28 14:11:20 -08003261 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303262 case OTG_STATE_A_HOST:
3263 seq_printf(s, "host\n");
3264 break;
3265 case OTG_STATE_B_PERIPHERAL:
3266 seq_printf(s, "peripheral\n");
3267 break;
3268 default:
3269 seq_printf(s, "none\n");
3270 break;
3271 }
3272
3273 return 0;
3274}
3275
3276static int msm_otg_mode_open(struct inode *inode, struct file *file)
3277{
3278 return single_open(file, msm_otg_mode_show, inode->i_private);
3279}
3280
3281static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3282 size_t count, loff_t *ppos)
3283{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303284 struct seq_file *s = file->private_data;
3285 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303286 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003287 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303288 int status = count;
3289 enum usb_mode_type req_mode;
3290
3291 memset(buf, 0x00, sizeof(buf));
3292
3293 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3294 status = -EFAULT;
3295 goto out;
3296 }
3297
3298 if (!strncmp(buf, "host", 4)) {
3299 req_mode = USB_HOST;
3300 } else if (!strncmp(buf, "peripheral", 10)) {
3301 req_mode = USB_PERIPHERAL;
3302 } else if (!strncmp(buf, "none", 4)) {
3303 req_mode = USB_NONE;
3304 } else {
3305 status = -EINVAL;
3306 goto out;
3307 }
3308
3309 switch (req_mode) {
3310 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003311 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303312 case OTG_STATE_A_HOST:
3313 case OTG_STATE_B_PERIPHERAL:
3314 set_bit(ID, &motg->inputs);
3315 clear_bit(B_SESS_VLD, &motg->inputs);
3316 break;
3317 default:
3318 goto out;
3319 }
3320 break;
3321 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003322 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303323 case OTG_STATE_B_IDLE:
3324 case OTG_STATE_A_HOST:
3325 set_bit(ID, &motg->inputs);
3326 set_bit(B_SESS_VLD, &motg->inputs);
3327 break;
3328 default:
3329 goto out;
3330 }
3331 break;
3332 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003333 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303334 case OTG_STATE_B_IDLE:
3335 case OTG_STATE_B_PERIPHERAL:
3336 clear_bit(ID, &motg->inputs);
3337 break;
3338 default:
3339 goto out;
3340 }
3341 break;
3342 default:
3343 goto out;
3344 }
3345
Steve Mucklef132c6c2012-06-06 18:30:57 -07003346 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303347 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303348out:
3349 return status;
3350}
3351
3352const struct file_operations msm_otg_mode_fops = {
3353 .open = msm_otg_mode_open,
3354 .read = seq_read,
3355 .write = msm_otg_mode_write,
3356 .llseek = seq_lseek,
3357 .release = single_release,
3358};
3359
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303360static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3361{
3362 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003363 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303364
Steve Mucklef132c6c2012-06-06 18:30:57 -07003365 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303366 return 0;
3367}
3368
3369static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3370{
3371 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3372}
3373
3374const struct file_operations msm_otg_state_fops = {
3375 .open = msm_otg_otg_state_open,
3376 .read = seq_read,
3377 .llseek = seq_lseek,
3378 .release = single_release,
3379};
3380
Anji jonnalad270e2d2011-08-09 11:28:32 +05303381static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3382{
3383 struct msm_otg *motg = s->private;
3384
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303385 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303386 return 0;
3387}
3388
3389static int msm_otg_chg_open(struct inode *inode, struct file *file)
3390{
3391 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3392}
3393
3394const struct file_operations msm_otg_chg_fops = {
3395 .open = msm_otg_chg_open,
3396 .read = seq_read,
3397 .llseek = seq_lseek,
3398 .release = single_release,
3399};
3400
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303401static int msm_otg_aca_show(struct seq_file *s, void *unused)
3402{
3403 if (debug_aca_enabled)
3404 seq_printf(s, "enabled\n");
3405 else
3406 seq_printf(s, "disabled\n");
3407
3408 return 0;
3409}
3410
3411static int msm_otg_aca_open(struct inode *inode, struct file *file)
3412{
3413 return single_open(file, msm_otg_aca_show, inode->i_private);
3414}
3415
3416static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3417 size_t count, loff_t *ppos)
3418{
3419 char buf[8];
3420
3421 memset(buf, 0x00, sizeof(buf));
3422
3423 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3424 return -EFAULT;
3425
3426 if (!strncmp(buf, "enable", 6))
3427 debug_aca_enabled = true;
3428 else
3429 debug_aca_enabled = false;
3430
3431 return count;
3432}
3433
3434const struct file_operations msm_otg_aca_fops = {
3435 .open = msm_otg_aca_open,
3436 .read = seq_read,
3437 .write = msm_otg_aca_write,
3438 .llseek = seq_lseek,
3439 .release = single_release,
3440};
3441
Manu Gautam8bdcc592012-03-06 11:26:06 +05303442static int msm_otg_bus_show(struct seq_file *s, void *unused)
3443{
3444 if (debug_bus_voting_enabled)
3445 seq_printf(s, "enabled\n");
3446 else
3447 seq_printf(s, "disabled\n");
3448
3449 return 0;
3450}
3451
3452static int msm_otg_bus_open(struct inode *inode, struct file *file)
3453{
3454 return single_open(file, msm_otg_bus_show, inode->i_private);
3455}
3456
3457static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3458 size_t count, loff_t *ppos)
3459{
3460 char buf[8];
3461 int ret;
3462 struct seq_file *s = file->private_data;
3463 struct msm_otg *motg = s->private;
3464
3465 memset(buf, 0x00, sizeof(buf));
3466
3467 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3468 return -EFAULT;
3469
3470 if (!strncmp(buf, "enable", 6)) {
3471 /* Do not vote here. Let OTG statemachine decide when to vote */
3472 debug_bus_voting_enabled = true;
3473 } else {
3474 debug_bus_voting_enabled = false;
3475 if (motg->bus_perf_client) {
3476 ret = msm_bus_scale_client_update_request(
3477 motg->bus_perf_client, 0);
3478 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003479 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303480 "for bus bw %d\n", __func__, ret);
3481 }
3482 }
3483
3484 return count;
3485}
3486
David Keitel272ce522012-08-17 16:25:24 -07003487static int otg_power_get_property_usb(struct power_supply *psy,
3488 enum power_supply_property psp,
3489 union power_supply_propval *val)
3490{
Jack Pham0c695282012-10-19 18:13:03 -07003491 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003492 switch (psp) {
3493 case POWER_SUPPLY_PROP_SCOPE:
3494 if (motg->host_mode)
3495 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3496 else
3497 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3498 break;
3499 case POWER_SUPPLY_PROP_CURRENT_MAX:
3500 val->intval = motg->current_max;
3501 break;
3502 /* Reflect USB enumeration */
3503 case POWER_SUPPLY_PROP_PRESENT:
3504 case POWER_SUPPLY_PROP_ONLINE:
3505 val->intval = motg->online;
3506 break;
3507 default:
3508 return -EINVAL;
3509 }
3510 return 0;
3511}
3512
3513static int otg_power_set_property_usb(struct power_supply *psy,
3514 enum power_supply_property psp,
3515 const union power_supply_propval *val)
3516{
Jack Pham0c695282012-10-19 18:13:03 -07003517 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003518
3519 switch (psp) {
3520 /* Process PMIC notification in PRESENT prop */
3521 case POWER_SUPPLY_PROP_PRESENT:
3522 msm_otg_set_vbus_state(val->intval);
3523 break;
3524 /* The ONLINE property reflects if usb has enumerated */
3525 case POWER_SUPPLY_PROP_ONLINE:
3526 motg->online = val->intval;
3527 break;
3528 case POWER_SUPPLY_PROP_CURRENT_MAX:
3529 motg->current_max = val->intval;
3530 break;
3531 default:
3532 return -EINVAL;
3533 }
3534
3535 power_supply_changed(&motg->usb_psy);
3536 return 0;
3537}
3538
David Collinsd79acc52012-11-26 14:59:00 -08003539static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3540 enum power_supply_property psp)
3541{
3542 switch (psp) {
3543 case POWER_SUPPLY_PROP_PRESENT:
3544 case POWER_SUPPLY_PROP_ONLINE:
3545 case POWER_SUPPLY_PROP_CURRENT_MAX:
3546 return 1;
3547 default:
3548 break;
3549 }
3550
3551 return 0;
3552}
3553
David Keitel272ce522012-08-17 16:25:24 -07003554static char *otg_pm_power_supplied_to[] = {
3555 "battery",
3556};
3557
3558static enum power_supply_property otg_pm_power_props_usb[] = {
3559 POWER_SUPPLY_PROP_PRESENT,
3560 POWER_SUPPLY_PROP_ONLINE,
3561 POWER_SUPPLY_PROP_CURRENT_MAX,
3562 POWER_SUPPLY_PROP_SCOPE,
3563};
3564
Manu Gautam8bdcc592012-03-06 11:26:06 +05303565const struct file_operations msm_otg_bus_fops = {
3566 .open = msm_otg_bus_open,
3567 .read = seq_read,
3568 .write = msm_otg_bus_write,
3569 .llseek = seq_lseek,
3570 .release = single_release,
3571};
3572
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303573static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303574
3575static int msm_otg_debugfs_init(struct msm_otg *motg)
3576{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303577 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303578
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303579 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3580
3581 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3582 return -ENODEV;
3583
Anji jonnalad270e2d2011-08-09 11:28:32 +05303584 if (motg->pdata->mode == USB_OTG &&
3585 motg->pdata->otg_control == OTG_USER_CONTROL) {
3586
Manu Gautam8bdcc592012-03-06 11:26:06 +05303587 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303588 S_IWUSR, msm_otg_dbg_root, motg,
3589 &msm_otg_mode_fops);
3590
Manu Gautam8bdcc592012-03-06 11:26:06 +05303591 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303592 debugfs_remove(msm_otg_dbg_root);
3593 msm_otg_dbg_root = NULL;
3594 return -ENODEV;
3595 }
3596 }
3597
Manu Gautam8bdcc592012-03-06 11:26:06 +05303598 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303599 msm_otg_dbg_root, motg,
3600 &msm_otg_chg_fops);
3601
Manu Gautam8bdcc592012-03-06 11:26:06 +05303602 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303603 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303604 return -ENODEV;
3605 }
3606
Manu Gautam8bdcc592012-03-06 11:26:06 +05303607 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303608 msm_otg_dbg_root, motg,
3609 &msm_otg_aca_fops);
3610
Manu Gautam8bdcc592012-03-06 11:26:06 +05303611 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303612 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303613 return -ENODEV;
3614 }
3615
Manu Gautam8bdcc592012-03-06 11:26:06 +05303616 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3617 msm_otg_dbg_root, motg,
3618 &msm_otg_bus_fops);
3619
3620 if (!msm_otg_dentry) {
3621 debugfs_remove_recursive(msm_otg_dbg_root);
3622 return -ENODEV;
3623 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303624
3625 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3626 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3627
3628 if (!msm_otg_dentry) {
3629 debugfs_remove_recursive(msm_otg_dbg_root);
3630 return -ENODEV;
3631 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303632 return 0;
3633}
3634
3635static void msm_otg_debugfs_cleanup(void)
3636{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303637 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303638}
3639
Manu Gautam0ddbd922012-09-21 17:17:38 +05303640#define MSM_OTG_CMD_ID 0x09
3641#define MSM_OTG_DEVICE_ID 0x04
3642#define MSM_OTG_VMID_IDX 0xFF
3643#define MSM_OTG_MEM_TYPE 0x02
3644struct msm_otg_scm_cmd_buf {
3645 unsigned int device_id;
3646 unsigned int vmid_idx;
3647 unsigned int mem_type;
3648} __attribute__ ((__packed__));
3649
3650static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3651{
3652 int ret;
3653 struct msm_otg_platform_data *pdata = motg->pdata;
3654 struct msm_otg_scm_cmd_buf cmd_buf;
3655
3656 if (!pdata->pnoc_errata_fix)
3657 return;
3658
3659 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3660
3661 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3662 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3663 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3664
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -07003665 ret = scm_call(SCM_SVC_MP, MSM_OTG_CMD_ID, &cmd_buf,
Manu Gautam0ddbd922012-09-21 17:17:38 +05303666 sizeof(cmd_buf), NULL, 0);
3667
3668 if (ret)
3669 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3670}
3671
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303672static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3673static struct platform_device *msm_otg_add_pdev(
3674 struct platform_device *ofdev, const char *name)
3675{
3676 struct platform_device *pdev;
3677 const struct resource *res = ofdev->resource;
3678 unsigned int num = ofdev->num_resources;
3679 int retval;
3680
3681 pdev = platform_device_alloc(name, -1);
3682 if (!pdev) {
3683 retval = -ENOMEM;
3684 goto error;
3685 }
3686
3687 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3688 pdev->dev.dma_mask = &msm_otg_dma_mask;
3689
3690 if (num) {
3691 retval = platform_device_add_resources(pdev, res, num);
3692 if (retval)
3693 goto error;
3694 }
3695
3696 retval = platform_device_add(pdev);
3697 if (retval)
3698 goto error;
3699
3700 return pdev;
3701
3702error:
3703 platform_device_put(pdev);
3704 return ERR_PTR(retval);
3705}
3706
3707static int msm_otg_setup_devices(struct platform_device *ofdev,
3708 enum usb_mode_type mode, bool init)
3709{
3710 const char *gadget_name = "msm_hsusb";
3711 const char *host_name = "msm_hsusb_host";
3712 static struct platform_device *gadget_pdev;
3713 static struct platform_device *host_pdev;
3714 int retval = 0;
3715
3716 if (!init) {
3717 if (gadget_pdev)
3718 platform_device_unregister(gadget_pdev);
3719 if (host_pdev)
3720 platform_device_unregister(host_pdev);
3721 return 0;
3722 }
3723
3724 switch (mode) {
3725 case USB_OTG:
3726 /* fall through */
3727 case USB_PERIPHERAL:
3728 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3729 if (IS_ERR(gadget_pdev)) {
3730 retval = PTR_ERR(gadget_pdev);
3731 break;
3732 }
3733 if (mode == USB_PERIPHERAL)
3734 break;
3735 /* fall through */
3736 case USB_HOST:
3737 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3738 if (IS_ERR(host_pdev)) {
3739 retval = PTR_ERR(host_pdev);
3740 if (mode == USB_OTG)
3741 platform_device_unregister(gadget_pdev);
3742 }
3743 break;
3744 default:
3745 break;
3746 }
3747
3748 return retval;
3749}
3750
David Keitel272ce522012-08-17 16:25:24 -07003751static int msm_otg_register_power_supply(struct platform_device *pdev,
3752 struct msm_otg *motg)
3753{
3754 int ret;
3755
3756 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3757 if (ret < 0) {
3758 dev_err(motg->phy.dev,
3759 "%s:power_supply_register usb failed\n",
3760 __func__);
3761 return ret;
3762 }
3763
3764 legacy_power_supply = false;
3765 return 0;
3766}
3767
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303768struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3769{
3770 struct device_node *node = pdev->dev.of_node;
3771 struct msm_otg_platform_data *pdata;
3772 int len = 0;
3773
3774 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3775 if (!pdata) {
3776 pr_err("unable to allocate platform data\n");
3777 return NULL;
3778 }
3779 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3780 if (len) {
3781 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3782 if (!pdata->phy_init_seq)
3783 return NULL;
3784 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3785 pdata->phy_init_seq,
3786 len/sizeof(*pdata->phy_init_seq));
3787 }
3788 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3789 &pdata->power_budget);
3790 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3791 &pdata->mode);
3792 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3793 &pdata->otg_control);
3794 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3795 &pdata->default_mode);
3796 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3797 &pdata->phy_type);
Manu Gautambd53fba2012-07-31 16:13:06 +05303798 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3799 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05303800 pdata->pnoc_errata_fix = of_property_read_bool(node,
3801 "qcom,hsusb-otg-pnoc-errata-fix");
Ido Shayevitza7114b92013-01-13 13:34:47 +02003802 pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
3803 "qcom,hsusb-otg-lpm-on-dev-suspend");
Ido Shayevitz26193352013-01-21 23:16:54 +02003804 pdata->core_clk_always_on_workaround = of_property_read_bool(node,
3805 "qcom,hsusb-otg-clk-always-on-workaround");
Shimrit Malichiffab5b02013-03-10 11:06:16 +02003806 pdata->delay_lpm_on_disconnect = of_property_read_bool(node,
3807 "qcom,hsusb-otg-delay-lpm");
Lena Salmanabde35d2013-04-25 15:29:43 +03003808 pdata->delay_lpm_hndshk_on_disconnect = of_property_read_bool(node,
3809 "qcom,hsusb-otg-delay-lpm-hndshk-on-disconnect");
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08003810 pdata->dp_manual_pullup = of_property_read_bool(node,
3811 "qcom,dp-manual-pullup");
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05303812 pdata->enable_sec_phy = of_property_read_bool(node,
3813 "qcom,usb2-enable-hsphy2");
Manu Gautambd53fba2012-07-31 16:13:06 +05303814
Manu Gautam0e0c53f2013-04-04 10:55:20 +05303815 pdata->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
3816 if (pdata->pmic_id_irq < 0)
3817 pdata->pmic_id_irq = 0;
3818
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303819 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303820}
3821
3822static int __init msm_otg_probe(struct platform_device *pdev)
3823{
Manu Gautamf8c45642012-08-10 10:20:56 -07003824 int ret = 0;
Mayank Rana0f286cf2013-02-27 11:43:27 +05303825 int len = 0;
3826 u32 tmp[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303827 struct resource *res;
3828 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003829 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303830 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303831
3832 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303833
3834 if (pdev->dev.of_node) {
3835 dev_dbg(&pdev->dev, "device tree enabled\n");
3836 pdata = msm_otg_dt_to_pdata(pdev);
3837 if (!pdata)
3838 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003839
3840 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3841 if (!pdata->bus_scale_table)
3842 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3843
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303844 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3845 if (ret) {
3846 dev_err(&pdev->dev, "devices setup failed\n");
3847 return ret;
3848 }
3849 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303850 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3851 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303852 } else {
3853 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303854 }
3855
3856 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3857 if (!motg) {
3858 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3859 return -ENOMEM;
3860 }
3861
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003862 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3863 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003864 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3865 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303866 goto free_motg;
3867 }
3868
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003869 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303870 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003871 phy = &motg->phy;
3872 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303873
3874 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303875 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3876 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3877 * PMIC can detect ACA ID_GND and generate an interrupt.
3878 */
3879 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3880 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3881 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003882 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303883 }
3884
Ofir Cohen4da266f2012-01-03 10:19:29 +02003885 /* initialize reset counter */
3886 motg->reset_counter = 0;
3887
Amit Blay02eff132011-09-21 16:46:24 +03003888 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003889 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003890 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003891 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303892
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303893 /*
3894 * Targets on which link uses asynchronous reset methodology,
3895 * free running clock is not required during the reset.
3896 */
Manu Gautam5143b252012-01-05 19:25:23 -08003897 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303898 if (IS_ERR(motg->clk))
3899 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3900 else
3901 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303902
3903 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003904 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303905 * CORE CLK must be running at >55Mhz for correct HSUSB
3906 * operation and USB core cannot tolerate frequency changes on
3907 * CORE CLK. For such USB cores, vote for maximum clk frequency
3908 * on pclk source
3909 */
Manu Gautam5143b252012-01-05 19:25:23 -08003910 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3911 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303912 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003913 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303914 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003915 goto put_clk;
3916 }
Mayank Rana3eaf28d2013-03-27 14:04:04 +05303917
3918 /*
3919 * Get Max supported clk frequency for USB Core CLK and request
3920 * to set the same.
3921 */
3922 motg->core_clk_rate = clk_round_rate(motg->core_clk, LONG_MAX);
3923 if (IS_ERR_VALUE(motg->core_clk_rate)) {
3924 dev_err(&pdev->dev, "fail to get core clk max freq.\n");
3925 } else {
3926 ret = clk_set_rate(motg->core_clk, motg->core_clk_rate);
3927 if (ret)
3928 dev_err(&pdev->dev, "fail to set core_clk freq:%d\n",
3929 ret);
3930 }
Manu Gautam5143b252012-01-05 19:25:23 -08003931
3932 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3933 if (IS_ERR(motg->pclk)) {
3934 dev_err(&pdev->dev, "failed to get iface_clk\n");
3935 ret = PTR_ERR(motg->pclk);
3936 goto put_core_clk;
3937 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303938
3939 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3940 if (!res) {
3941 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3942 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003943 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303944 }
3945
3946 motg->regs = ioremap(res->start, resource_size(res));
3947 if (!motg->regs) {
3948 dev_err(&pdev->dev, "ioremap failed\n");
3949 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003950 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303951 }
3952 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3953
3954 motg->irq = platform_get_irq(pdev, 0);
3955 if (!motg->irq) {
3956 dev_err(&pdev->dev, "platform_get_irq failed\n");
3957 ret = -ENODEV;
3958 goto free_regs;
3959 }
3960
Manu Gautamf8c45642012-08-10 10:20:56 -07003961 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3962 if (motg->async_irq < 0) {
3963 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3964 motg->async_irq = 0;
3965 }
3966
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05303967 motg->xo_clk = clk_get(&pdev->dev, "xo");
3968 if (IS_ERR(motg->xo_clk)) {
3969 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
3970 if (IS_ERR(motg->xo_handle)) {
3971 dev_err(&pdev->dev, "%s fail to get handle for TCXO\n",
3972 __func__);
3973 ret = PTR_ERR(motg->xo_handle);
3974 goto free_regs;
3975 } else {
3976 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
3977 if (ret) {
3978 dev_err(&pdev->dev, "%s XO voting failed %d\n",
3979 __func__, ret);
3980 goto free_xo_handle;
3981 }
3982 }
3983 } else {
3984 ret = clk_prepare_enable(motg->xo_clk);
3985 if (ret) {
3986 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
3987 __func__, ret);
3988 goto free_xo_handle;
3989 }
Anji jonnala7da3f262011-12-02 17:22:14 -08003990 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303991
Anji jonnala7da3f262011-12-02 17:22:14 -08003992
Manu Gautam28b1bac2012-01-30 16:43:06 +05303993 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303994
Mayank Rana248698c2012-04-19 00:03:16 +05303995 motg->vdd_type = VDDCX_CORNER;
Mayank Rana0f286cf2013-02-27 11:43:27 +05303996 hsusb_vdd = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
3997 if (IS_ERR(hsusb_vdd)) {
3998 hsusb_vdd = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
3999 if (IS_ERR(hsusb_vdd)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004000 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304001 ret = PTR_ERR(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304002 goto devote_xo_handle;
4003 }
4004 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304005 }
4006
Mayank Rana0f286cf2013-02-27 11:43:27 +05304007 if (pdev->dev.of_node) {
4008 of_get_property(pdev->dev.of_node,
4009 "qcom,vdd-voltage-level",
4010 &len);
4011 if (len == sizeof(tmp)) {
4012 of_property_read_u32_array(pdev->dev.of_node,
4013 "qcom,vdd-voltage-level",
4014 tmp, len/sizeof(*tmp));
4015 vdd_val[motg->vdd_type][0] = tmp[0];
4016 vdd_val[motg->vdd_type][1] = tmp[1];
4017 vdd_val[motg->vdd_type][2] = tmp[2];
4018 } else {
4019 dev_dbg(&pdev->dev, "Using default hsusb vdd config.\n");
4020 }
4021 }
4022
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004023 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304024 if (ret) {
4025 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05304026 goto devote_xo_handle;
4027 }
4028
Mayank Rana0f286cf2013-02-27 11:43:27 +05304029 ret = regulator_enable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304030 if (ret) {
4031 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
4032 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304033 }
4034
4035 ret = msm_hsusb_ldo_init(motg, 1);
4036 if (ret) {
4037 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304038 goto free_hsusb_vdd;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304039 }
4040
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304041 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05304042 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
4043 "mhl_usb_hs_switch");
4044 if (IS_ERR(mhl_usb_hs_switch)) {
4045 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07004046 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304047 goto free_ldo_init;
4048 }
4049 }
4050
Amit Blay81801aa2012-09-19 12:08:12 +02004051 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304052 if (ret) {
4053 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004054 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304055 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05304056 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304057
Manu Gautam0ddbd922012-09-21 17:17:38 +05304058 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
4059 msm_otg_pnoc_errata_fix(motg);
4060
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304061 writel(0, USB_USBINTR);
4062 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004063 /* Ensure that above STOREs are completed before enabling interrupts */
4064 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304065
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304066 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
4067 if (ret)
4068 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004069 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05304070 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304071 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304072 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304073 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004074 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304075 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
4076 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05304077 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
4078 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304079 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
4080 "msm_otg", motg);
4081 if (ret) {
4082 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004083 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304084 }
4085
Manu Gautamf8c45642012-08-10 10:20:56 -07004086 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304087 ret = request_irq(motg->async_irq, msm_otg_irq,
4088 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004089 if (ret) {
4090 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4091 goto free_irq;
4092 }
4093 disable_irq(motg->async_irq);
4094 }
4095
Jack Pham87f202f2012-08-06 00:24:22 -07004096 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4097 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4098
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004099 phy->init = msm_otg_reset;
4100 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004101 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304102
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004103 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304104
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004105 phy->otg->phy = &motg->phy;
4106 phy->otg->set_host = msm_otg_set_host;
4107 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004108 phy->otg->start_hnp = msm_otg_start_hnp;
4109 phy->otg->start_srp = msm_otg_start_srp;
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004110 if (pdata->dp_manual_pullup)
4111 phy->flags |= ENABLE_DP_MANUAL_PULLUP;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004112
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304113 if (pdata->enable_sec_phy)
4114 phy->flags |= ENABLE_SECONDARY_PHY;
4115
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004116 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304117 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004118 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004119 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304120 }
4121
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304122 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304123 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004124 if (motg->pdata->pmic_id_irq) {
4125 ret = request_irq(motg->pdata->pmic_id_irq,
4126 msm_pmic_id_irq,
4127 IRQF_TRIGGER_RISING |
4128 IRQF_TRIGGER_FALLING,
4129 "msm_otg", motg);
4130 if (ret) {
4131 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004132 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004133 }
4134 } else {
4135 ret = -ENODEV;
4136 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004137 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004138 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304139 }
4140
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304141 msm_hsusb_mhl_switch_enable(motg, 1);
4142
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304143 platform_set_drvdata(pdev, motg);
4144 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004145 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304146
Anji jonnalad270e2d2011-08-09 11:28:32 +05304147 ret = msm_otg_debugfs_init(motg);
4148 if (ret)
4149 dev_dbg(&pdev->dev, "mode debugfs file is"
4150 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304151
Amit Blay58b31472011-11-18 09:39:39 +02004152 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4153 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304154 (!(motg->pdata->mode == USB_OTG) ||
4155 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004156 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304157 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004158
Amit Blay58b31472011-11-18 09:39:39 +02004159 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004160 motg->caps = ALLOW_PHY_RETENTION |
4161 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02004162 }
4163
Amit Blay6fa647a2012-05-24 14:12:08 +03004164 if (motg->pdata->enable_lpm_on_dev_suspend)
4165 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4166
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004167 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304168 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004169 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304170
Amit Blayd6f38282012-10-29 13:13:46 +02004171 if (motg->pdata->delay_lpm_on_disconnect) {
4172 pm_runtime_set_autosuspend_delay(&pdev->dev,
4173 lpm_disconnect_thresh);
4174 pm_runtime_use_autosuspend(&pdev->dev);
4175 }
4176
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304177 if (motg->pdata->bus_scale_table) {
4178 motg->bus_perf_client =
4179 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4180 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004181 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304182 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05304183 else
4184 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304185 }
4186
David Keitel272ce522012-08-17 16:25:24 -07004187 motg->usb_psy.name = "usb";
4188 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4189 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4190 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4191 motg->usb_psy.properties = otg_pm_power_props_usb;
4192 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4193 motg->usb_psy.get_property = otg_power_get_property_usb;
4194 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004195 motg->usb_psy.property_is_writeable
4196 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004197
Jack Pham0c695282012-10-19 18:13:03 -07004198 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004199 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004200 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4201 legacy_power_supply = true;
4202 } else {
4203 /* otherwise register our own power supply */
4204 if (!msm_otg_register_power_supply(pdev, motg))
4205 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004206 }
4207
Jack Pham0c695282012-10-19 18:13:03 -07004208 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4209 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4210
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304211 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004212
Steve Mucklef132c6c2012-06-06 18:30:57 -07004213remove_phy:
4214 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004215free_async_irq:
4216 if (motg->async_irq)
4217 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304218free_irq:
4219 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004220destroy_wlock:
4221 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304222 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004223 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004224free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304225 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304226free_hsusb_vdd:
4227 regulator_disable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304228free_config_vddcx:
Mayank Rana0f286cf2013-02-27 11:43:27 +05304229 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304230 vdd_val[motg->vdd_type][VDD_NONE],
4231 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004232devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304233 clk_disable_unprepare(motg->pclk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304234 if (!IS_ERR(motg->xo_clk))
4235 clk_disable_unprepare(motg->xo_clk);
4236 else
4237 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004238free_xo_handle:
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304239 if (!IS_ERR(motg->xo_clk))
4240 clk_put(motg->xo_clk);
4241 else
4242 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304243free_regs:
4244 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004245put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304246 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304247put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004248 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304249put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304250 if (!IS_ERR(motg->clk))
4251 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004252 if (!IS_ERR(motg->phy_reset_clk))
4253 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004254free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004255 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304256free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304257 kfree(motg);
4258 return ret;
4259}
4260
4261static int __devexit msm_otg_remove(struct platform_device *pdev)
4262{
4263 struct msm_otg *motg = platform_get_drvdata(pdev);
Stephen Boyd9850acb2013-01-28 14:11:20 -08004264 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304265 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304266
Stephen Boyd9850acb2013-01-28 14:11:20 -08004267 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304268 return -EBUSY;
4269
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304270 if (pdev->dev.of_node)
4271 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004272 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4273 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304274 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304275 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304276 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304277 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004278 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304279 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304280
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304281 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304282
4283 device_init_wakeup(&pdev->dev, 0);
4284 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004285 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304286
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304287 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004288 if (motg->pdata->pmic_id_irq)
4289 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004290 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304291 free_irq(motg->irq, motg);
4292
Jack Pham87f202f2012-08-06 00:24:22 -07004293 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4294 motg->pdata->mpm_otgsessvld_int)
4295 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4296
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304297 /*
4298 * Put PHY in low power mode.
4299 */
Stephen Boyd9850acb2013-01-28 14:11:20 -08004300 ulpi_read(phy, 0x14);
4301 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304302
4303 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4304 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4305 if (readl(USB_PORTSC) & PORTSC_PHCD)
4306 break;
4307 udelay(1);
4308 cnt++;
4309 }
4310 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Stephen Boyd9850acb2013-01-28 14:11:20 -08004311 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304312
Manu Gautam28b1bac2012-01-30 16:43:06 +05304313 clk_disable_unprepare(motg->pclk);
4314 clk_disable_unprepare(motg->core_clk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304315 if (!IS_ERR(motg->xo_clk)) {
4316 clk_disable_unprepare(motg->xo_clk);
4317 clk_put(motg->xo_clk);
4318 } else {
4319 msm_xo_put(motg->xo_handle);
4320 }
Amit Blay81801aa2012-09-19 12:08:12 +02004321 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304322 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304323 regulator_disable(hsusb_vdd);
4324 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304325 vdd_val[motg->vdd_type][VDD_NONE],
4326 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304327
4328 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304329 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304330
Amit Blay02eff132011-09-21 16:46:24 +03004331 if (!IS_ERR(motg->phy_reset_clk))
4332 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304333 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304334 if (!IS_ERR(motg->clk))
4335 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004336 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304337
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304338 if (motg->bus_perf_client)
4339 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304340
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004341 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304342 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304343 return 0;
4344}
4345
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304346#ifdef CONFIG_PM_RUNTIME
4347static int msm_otg_runtime_idle(struct device *dev)
4348{
4349 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004350 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304351
4352 dev_dbg(dev, "OTG runtime idle\n");
4353
Steve Mucklef132c6c2012-06-06 18:30:57 -07004354 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304355 return -EAGAIN;
4356 else
4357 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304358}
4359
4360static int msm_otg_runtime_suspend(struct device *dev)
4361{
4362 struct msm_otg *motg = dev_get_drvdata(dev);
4363
4364 dev_dbg(dev, "OTG runtime suspend\n");
4365 return msm_otg_suspend(motg);
4366}
4367
4368static int msm_otg_runtime_resume(struct device *dev)
4369{
4370 struct msm_otg *motg = dev_get_drvdata(dev);
4371
4372 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304373 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304374 return msm_otg_resume(motg);
4375}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304376#endif
4377
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304378#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304379static int msm_otg_pm_suspend(struct device *dev)
4380{
Jack Pham5ca279b2012-05-14 18:42:54 -07004381 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304382 struct msm_otg *motg = dev_get_drvdata(dev);
4383
4384 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004385
4386 atomic_set(&motg->pm_suspended, 1);
4387 ret = msm_otg_suspend(motg);
4388 if (ret)
4389 atomic_set(&motg->pm_suspended, 0);
4390
4391 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304392}
4393
4394static int msm_otg_pm_resume(struct device *dev)
4395{
Jack Pham5ca279b2012-05-14 18:42:54 -07004396 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304397 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304398
4399 dev_dbg(dev, "OTG PM resume\n");
4400
Jack Pham5ca279b2012-05-14 18:42:54 -07004401 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004402 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004403 pm_runtime_get_noresume(dev);
4404 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304405
Jack Pham5ca279b2012-05-14 18:42:54 -07004406 /* Update runtime PM status */
4407 pm_runtime_disable(dev);
4408 pm_runtime_set_active(dev);
4409 pm_runtime_enable(dev);
4410
Jack Phamc7edb172012-08-13 15:32:39 -07004411 if (motg->sm_work_pending) {
4412 motg->sm_work_pending = false;
4413 queue_work(system_nrt_wq, &motg->sm_work);
4414 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004415 }
4416
4417 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304418}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304419#endif
4420
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304421#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304422static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304423 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4424 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4425 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304426};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304427#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304428
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304429static struct of_device_id msm_otg_dt_match[] = {
4430 { .compatible = "qcom,hsusb-otg",
4431 },
4432 {}
4433};
4434
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304435static struct platform_driver msm_otg_driver = {
4436 .remove = __devexit_p(msm_otg_remove),
4437 .driver = {
4438 .name = DRIVER_NAME,
4439 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304440#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304441 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304442#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304443 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304444 },
4445};
4446
4447static int __init msm_otg_init(void)
4448{
4449 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4450}
4451
4452static void __exit msm_otg_exit(void)
4453{
4454 platform_driver_unregister(&msm_otg_driver);
4455}
4456
4457module_init(msm_otg_init);
4458module_exit(msm_otg_exit);
4459
4460MODULE_LICENSE("GPL v2");
4461MODULE_DESCRIPTION("MSM USB transceiver driver");