blob: 5055dcf103345f4f73a3a8ce025fc27fc5e290c9 [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;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530864 bool host_bus_suspend, device_bus_suspend, dcp, prop_charger;
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())
Lena Salman05b544f2013-05-13 15:49:10 +0300873 return -EBUSY;
Lena Salmanabde35d2013-04-25 15:29:43 +0300874
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;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530882 prop_charger = motg->chg_type == USB_PROPRIETARY_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700883
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530884 /*
885 * Abort suspend when,
886 * 1. charging detection in progress due to cable plug-in
887 * 2. host mode activation in progress due to Micro-A cable insertion
888 */
889
890 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530891 !dcp && !prop_charger) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham502bea32012-08-13 15:34:20 -0700892 enable_irq(motg->irq);
893 return -EBUSY;
894 }
895
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530896 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530897 * Chipidea 45-nm PHY suspend sequence:
898 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530899 * Interrupt Latch Register auto-clear feature is not present
900 * in all PHY versions. Latch register is clear on read type.
901 * Clear latch register to avoid spurious wakeup from
902 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530903 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530904 * PHY comparators are disabled when PHY enters into low power
905 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
906 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
907 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530908 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530909 * PLL is not turned off when PHY enters into low power mode (LPM).
910 * Disable PLL for maximum power savings.
911 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530912
913 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200914 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530915 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200916 ulpi_write(phy, 0x01, 0x30);
917 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530918 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530919
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530921 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530922 * PHY may take some time or even fail to enter into low power
923 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
924 * in failure case.
925 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530926 portsc = readl_relaxed(USB_PORTSC);
927 if (!(portsc & PORTSC_PHCD)) {
928 writel_relaxed(portsc | PORTSC_PHCD,
929 USB_PORTSC);
930 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
931 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
932 break;
933 udelay(1);
934 cnt++;
935 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530936 }
937
938 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200939 dev_err(phy->dev, "Unable to suspend PHY\n");
940 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530941 enable_irq(motg->irq);
942 return -ETIMEDOUT;
943 }
944
945 /*
946 * PHY has capability to generate interrupt asynchronously in low
947 * power mode (LPM). This interrupt is level triggered. So USB IRQ
948 * line must be disabled till async interrupt enable bit is cleared
949 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
950 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530951 *
952 * PHY retention mode is disallowed while entering to LPM with wall
953 * charger connected. But PHY is put into suspend mode. Hence
954 * enable asynchronous interrupt to detect charger disconnection when
955 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530956 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530957 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300958 if (host_bus_suspend || device_bus_suspend ||
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530959 (motg->pdata->otg_control == OTG_PHY_CONTROL))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530960 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
961 else
962 cmd_val |= ULPI_STP_CTRL;
963 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530965 /*
966 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
967 * PHY retention and collapse can not happen with VDP_SRC enabled.
968 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300969 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
970 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200971 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530972 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +0200973 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530974 if ((motg->pdata->mode == USB_OTG) ||
975 (motg->pdata->mode == USB_HOST))
976 phy_ctrl_val |= (PHY_IDHV_INTEN |
977 PHY_OTGSESSVLDHV_INTEN);
978 else
979 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
980 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530981
Amit Blay58b31472011-11-18 09:39:39 +0200982 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530984 }
985
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 /* Ensure that above operation is completed before turning off clocks */
987 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300988 /* Consider clocks on workaround flag only in case of bus suspend */
989 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
990 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
991 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300992 clk_disable_unprepare(motg->pclk);
993 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300994 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300995 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530996
Anji jonnala7da3f262011-12-02 17:22:14 -0800997 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530998 if (!host_bus_suspend) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +0530999 if (!IS_ERR(motg->xo_clk)) {
1000 clk_disable_unprepare(motg->xo_clk);
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301001 motg->lpm_flags |= XO_SHUTDOWN;
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301002 } else {
1003 ret = msm_xo_mode_vote(motg->xo_handle,
1004 MSM_XO_MODE_OFF);
1005 if (ret)
1006 dev_err(phy->dev, "%s fail to devote XO %d\n",
1007 __func__, ret);
1008 else
1009 motg->lpm_flags |= XO_SHUTDOWN;
1010 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301011 }
Anji jonnala7da3f262011-12-02 17:22:14 -08001012
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301013 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
1014 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +02001015 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001017 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
1018 !host_bus_suspend && !device_bus_suspend && !dcp) {
1019 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
1020 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +05301021 }
1022
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301023 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301025 msm_hsusb_mhl_switch_enable(motg, 0);
1026 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027
Steve Mucklef132c6c2012-06-06 18:30:57 -07001028 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001029 if (motg->async_irq)
1030 enable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001031 else
1032 enable_irq_wake(motg->irq);
1033
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034 if (motg->pdata->pmic_id_irq)
1035 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001036 if (pdata->otg_control == OTG_PHY_CONTROL &&
1037 pdata->mpm_otgsessvld_int)
1038 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301040 if (bus)
1041 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1042
1043 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001044 /* Enable ASYNC IRQ (if present) during LPM */
1045 if (motg->async_irq)
1046 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301047 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301049
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001050 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301051
1052 return 0;
1053}
1054
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301055static int msm_otg_resume(struct msm_otg *motg)
1056{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001057 struct usb_phy *phy = &motg->phy;
1058 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001059 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301060 int cnt = 0;
1061 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001062 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001063 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301064
1065 if (!atomic_read(&motg->in_lpm))
1066 return 0;
1067
Jack Pham8978b892012-10-17 16:31:39 -07001068 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301070
Anji jonnala7da3f262011-12-02 17:22:14 -08001071 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301072 if (motg->lpm_flags & XO_SHUTDOWN) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301073 if (!IS_ERR(motg->xo_clk)) {
1074 clk_prepare_enable(motg->xo_clk);
1075 } else {
1076 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1077 if (ret)
1078 dev_err(phy->dev, "%s fail to vote for XO %d\n",
1079 __func__, ret);
1080 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301081 motg->lpm_flags &= ~XO_SHUTDOWN;
1082 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301083
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001084 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001085 clk_prepare_enable(motg->core_clk);
1086 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001087 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001088 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301089
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001091 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001092 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001093 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1094 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1095 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096 }
1097
1098 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301099 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301100 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001101 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1102 phy_ctrl_val |= PHY_RETEN;
1103 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1104 /* Disable PHY HV interrupts */
1105 phy_ctrl_val &=
1106 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
1107 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301109 }
1110
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301111 temp = readl(USB_USBCMD);
1112 temp &= ~ASYNC_INTR_CTRL;
1113 temp &= ~ULPI_STP_CTRL;
1114 writel(temp, USB_USBCMD);
1115
1116 /*
1117 * PHY comes out of low power mode (LPM) in case of wakeup
1118 * from asynchronous interrupt.
1119 */
1120 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1121 goto skip_phy_resume;
1122
1123 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1124 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1125 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1126 break;
1127 udelay(1);
1128 cnt++;
1129 }
1130
1131 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1132 /*
1133 * This is a fatal error. Reset the link and
1134 * PHY. USB state can not be restored. Re-insertion
1135 * of USB cable is the only way to get USB working.
1136 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001137 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301138 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001139 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301140 }
1141
1142skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001143 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001144 if (motg->async_irq)
1145 disable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001146 else
1147 disable_irq_wake(motg->irq);
1148
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 if (motg->pdata->pmic_id_irq)
1150 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001151 if (pdata->otg_control == OTG_PHY_CONTROL &&
1152 pdata->mpm_otgsessvld_int)
1153 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301155 if (bus)
1156 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1157
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301158 atomic_set(&motg->in_lpm, 0);
1159
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301160 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001161 /* Match the disable_irq call from ISR */
1162 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301163 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301164 }
Jack Pham8978b892012-10-17 16:31:39 -07001165 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301166
Manu Gautamf8c45642012-08-10 10:20:56 -07001167 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1168 if (motg->async_irq)
1169 disable_irq(motg->async_irq);
1170
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001171 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301172
1173 return 0;
1174}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301175#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301176
David Keitel272ce522012-08-17 16:25:24 -07001177static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001178{
Jack Pham0c695282012-10-19 18:13:03 -07001179 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001180 pr_err("No USB power supply registered!\n");
1181 return;
1182 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001183
Jack Pham0c695282012-10-19 18:13:03 -07001184 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001185 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301186 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001187 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301188 } else {
David Keitel272ce522012-08-17 16:25:24 -07001189 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301190 /*
1191 * VBUS comparator is disabled by PMIC charging driver
1192 * when SYSTEM scope is selected. For ID_GND->ID_A
1193 * transition, give 50 msec delay so that PMIC charger
1194 * driver detect the VBUS and ready for accepting
1195 * charging current value from USB.
1196 */
1197 if (test_bit(ID_A, &motg->inputs))
1198 msleep(50);
1199 }
David Keitel272ce522012-08-17 16:25:24 -07001200 } else {
1201 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001202 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001203 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001204}
1205
David Keitel081a3e22012-04-18 12:37:07 -07001206static int msm_otg_notify_chg_type(struct msm_otg *motg)
1207{
1208 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001209
David Keitel081a3e22012-04-18 12:37:07 -07001210 /*
1211 * TODO
1212 * Unify OTG driver charger types and power supply charger types
1213 */
1214 if (charger_type == motg->chg_type)
1215 return 0;
1216
1217 if (motg->chg_type == USB_SDP_CHARGER)
1218 charger_type = POWER_SUPPLY_TYPE_USB;
1219 else if (motg->chg_type == USB_CDP_CHARGER)
1220 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301221 else if (motg->chg_type == USB_DCP_CHARGER ||
1222 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001223 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1224 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1225 motg->chg_type == USB_ACA_A_CHARGER ||
1226 motg->chg_type == USB_ACA_B_CHARGER ||
1227 motg->chg_type == USB_ACA_C_CHARGER))
1228 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1229 else
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301230 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001231
Jack Pham0c695282012-10-19 18:13:03 -07001232 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001233 pr_err("No USB power supply registered!\n");
1234 return -EINVAL;
1235 }
1236
1237 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001238 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001239 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001240}
1241
Amit Blay0f7edf72012-01-15 10:11:27 +02001242static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1243{
Jack Pham0c695282012-10-19 18:13:03 -07001244 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001245 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1246 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001247 }
1248
David Keitel272ce522012-08-17 16:25:24 -07001249 if (motg->cur_power == 0 && mA > 2) {
1250 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001251 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001252 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001253 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001254 goto psy_error;
1255 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1256 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001257 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001258 goto psy_error;
1259 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001260 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001261 goto psy_error;
Manu Gautamfca298c2013-06-05 15:11:54 +05301262 } else {
1263 if (power_supply_set_online(psy, true))
1264 goto psy_error;
1265 /* Current has changed (100/2 --> 500) */
1266 if (power_supply_set_current_limit(psy, 1000*mA))
1267 goto psy_error;
David Keitel272ce522012-08-17 16:25:24 -07001268 }
Manu Gautamfca298c2013-06-05 15:11:54 +05301269
Jack Pham0c695282012-10-19 18:13:03 -07001270 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001271 return 0;
1272
David Keitel272ce522012-08-17 16:25:24 -07001273psy_error:
1274 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001275 return -ENXIO;
1276}
1277
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301278static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1279{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001280 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301281
1282 if (g && g->is_a_peripheral)
1283 return;
1284
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301285 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1286 motg->chg_type == USB_ACA_A_CHARGER ||
1287 motg->chg_type == USB_ACA_B_CHARGER ||
1288 motg->chg_type == USB_ACA_C_CHARGER) &&
1289 mA > IDEV_ACA_CHG_LIMIT)
1290 mA = IDEV_ACA_CHG_LIMIT;
1291
David Keitel081a3e22012-04-18 12:37:07 -07001292 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001293 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001294 "Failed notifying %d charger type to PMIC\n",
1295 motg->chg_type);
1296
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301297 if (motg->cur_power == mA)
1298 return;
1299
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001300 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001301
1302 /*
1303 * Use Power Supply API if supported, otherwise fallback
1304 * to legacy pm8921 API.
1305 */
1306 if (msm_otg_notify_power_supply(motg, mA))
1307 pm8921_charger_vbus_draw(mA);
1308
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301309 motg->cur_power = mA;
1310}
1311
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001312static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301313{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001314 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301315
1316 /*
1317 * Gadget driver uses set_power method to notify about the
1318 * available current based on suspend/configured states.
1319 *
1320 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1321 * states when CDP/ACA is connected.
1322 */
1323 if (motg->chg_type == USB_SDP_CHARGER)
1324 msm_otg_notify_charger(motg, mA);
1325
1326 return 0;
1327}
1328
Steve Mucklef132c6c2012-06-06 18:30:57 -07001329static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301330{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001331 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301332 struct msm_otg_platform_data *pdata = motg->pdata;
1333 struct usb_hcd *hcd;
1334
1335 if (!otg->host)
1336 return;
1337
1338 hcd = bus_to_hcd(otg->host);
1339
1340 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001341 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301342
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301343 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001344 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301345 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1346
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301347 /*
1348 * Some boards have a switch cotrolled by gpio
1349 * to enable/disable internal HUB. Enable internal
1350 * HUB before kicking the host.
1351 */
1352 if (pdata->setup_gpio)
1353 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301354 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301355 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001356 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301357
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301358 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301359 /* HCD core reset all bits of PORTSC. select ULPI phy */
1360 writel_relaxed(0x80000000, USB_PORTSC);
1361
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301362 if (pdata->setup_gpio)
1363 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301364
1365 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001366 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301367 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301368 }
1369}
1370
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001371static int msm_otg_usbdev_notify(struct notifier_block *self,
1372 unsigned long action, void *priv)
1373{
1374 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001375 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301376 struct usb_device *udev = priv;
1377
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301378 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1379 goto out;
1380
Steve Mucklef132c6c2012-06-06 18:30:57 -07001381 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301382 goto out;
1383 /*
1384 * Interested in devices connected directly to the root hub.
1385 * ACA dock can supply IDEV_CHG irrespective devices connected
1386 * on the accessory port.
1387 */
1388 if (!udev->parent || udev->parent->parent ||
1389 motg->chg_type == USB_ACA_DOCK_CHARGER)
1390 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001391
1392 switch (action) {
1393 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301394 if (aca_enabled())
1395 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001396 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301397 pr_debug("B_CONN set\n");
1398 set_bit(B_CONN, &motg->inputs);
1399 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001400 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301401 /*
1402 * OTG PET: A-device must end session within
1403 * 10 sec after PET enumeration.
1404 */
1405 if (udev->quirks & USB_QUIRK_OTG_PET)
1406 msm_otg_start_timer(motg, TA_TST_MAINT,
1407 A_TST_MAINT);
1408 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301409 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001411 if (udev->actconfig)
1412 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1413 else
1414 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001415 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301416 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301417 break;
1418 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001419 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1420 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301421 pr_debug("B_CONN clear\n");
1422 clear_bit(B_CONN, &motg->inputs);
1423 /*
1424 * OTG PET: A-device must end session after
1425 * PET disconnection if it is enumerated
1426 * with bcdDevice[0] = 1. USB core sets
1427 * bus->otg_vbus_off for us. clear it here.
1428 */
1429 if (udev->bus->otg_vbus_off) {
1430 udev->bus->otg_vbus_off = 0;
1431 set_bit(A_BUS_DROP, &motg->inputs);
1432 }
1433 queue_work(system_nrt_wq, &motg->sm_work);
1434 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435 default:
1436 break;
1437 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301438 if (test_bit(ID_A, &motg->inputs))
1439 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1440 motg->mA_port);
1441out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001442 return NOTIFY_OK;
1443}
1444
Mayank Ranae3926882011-12-26 09:47:54 +05301445static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1446{
1447 int ret;
1448 static bool vbus_is_on;
1449
1450 if (vbus_is_on == on)
1451 return;
1452
1453 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301454 ret = motg->pdata->vbus_power(on);
1455 if (!ret)
1456 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301457 return;
1458 }
1459
1460 if (!vbus_otg) {
1461 pr_err("vbus_otg is NULL.");
1462 return;
1463 }
1464
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001465 /*
1466 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001467 * from usb before turning on the boost.
1468 * if exiting host mode disable the boost before enabling to draw
1469 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001470 */
Mayank Ranae3926882011-12-26 09:47:54 +05301471 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001472 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301473 ret = regulator_enable(vbus_otg);
1474 if (ret) {
1475 pr_err("unable to enable vbus_otg\n");
1476 return;
1477 }
1478 vbus_is_on = true;
1479 } else {
1480 ret = regulator_disable(vbus_otg);
1481 if (ret) {
1482 pr_err("unable to disable vbus_otg\n");
1483 return;
1484 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001485 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301486 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301487 }
1488}
1489
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001490static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301491{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001492 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301493 struct usb_hcd *hcd;
1494
1495 /*
1496 * Fail host registration if this board can support
1497 * only peripheral configuration.
1498 */
1499 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001500 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301501 return -ENODEV;
1502 }
1503
Mayank Ranae3926882011-12-26 09:47:54 +05301504 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001505 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301506 if (IS_ERR(vbus_otg)) {
1507 pr_err("Unable to get vbus_otg\n");
Manu Gautam139a8132013-06-04 16:46:50 +05301508 return PTR_ERR(vbus_otg);
Mayank Ranae3926882011-12-26 09:47:54 +05301509 }
1510 }
1511
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301512 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001513 if (otg->phy->state == OTG_STATE_A_HOST) {
1514 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301516 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301517 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301518 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001519 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301520 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301521 } else {
1522 otg->host = NULL;
1523 }
1524
1525 return 0;
1526 }
1527
1528 hcd = bus_to_hcd(host);
1529 hcd->power_budget = motg->pdata->power_budget;
1530
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301531#ifdef CONFIG_USB_OTG
1532 host->otg_port = 1;
1533#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001534 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1535 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301536 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001537 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301538
1539 /*
1540 * Kick the state machine work, if peripheral is not supported
1541 * or peripheral is already registered with us.
1542 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301543 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001544 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301545 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301546 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301547
1548 return 0;
1549}
1550
Steve Mucklef132c6c2012-06-06 18:30:57 -07001551static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301552{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301553 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001554 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301555 struct msm_otg_platform_data *pdata = motg->pdata;
1556
1557 if (!otg->gadget)
1558 return;
1559
1560 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001561 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301562 /*
1563 * Some boards have a switch cotrolled by gpio
1564 * to enable/disable internal HUB. Disable internal
1565 * HUB before kicking the gadget.
1566 */
1567 if (pdata->setup_gpio)
1568 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001569
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301570 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301571 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301572 ret = msm_bus_scale_client_update_request(
1573 motg->bus_perf_client, 1);
1574 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001575 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301576 "bus bandwidth %d\n", __func__, ret);
1577 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301578 usb_gadget_vbus_connect(otg->gadget);
1579 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001580 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301581 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301582 /* Configure BUS performance parameters to default */
1583 if (motg->bus_perf_client) {
1584 ret = msm_bus_scale_client_update_request(
1585 motg->bus_perf_client, 0);
1586 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001587 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301588 "for bus bw %d\n", __func__, ret);
1589 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301590 if (pdata->setup_gpio)
1591 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1592 }
1593
1594}
1595
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001596static int msm_otg_set_peripheral(struct usb_otg *otg,
Stephen Boyd9850acb2013-01-28 14:11:20 -08001597 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301598{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001599 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301600
1601 /*
1602 * Fail peripheral registration if this board can support
1603 * only host configuration.
1604 */
1605 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001606 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301607 return -ENODEV;
1608 }
1609
1610 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001611 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1612 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301613 msm_otg_start_peripheral(otg, 0);
1614 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001615 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301616 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301617 } else {
1618 otg->gadget = NULL;
1619 }
1620
1621 return 0;
1622 }
1623 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001624 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301625
1626 /*
1627 * Kick the state machine work, if host is not supported
1628 * or host is already registered with us.
1629 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301630 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001631 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301632 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301633 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301634
1635 return 0;
1636}
1637
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301638static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1639{
1640 unsigned long flags;
1641 int id;
1642
1643 if (!motg->pdata->pmic_id_irq)
1644 return -ENODEV;
1645
1646 local_irq_save(flags);
1647 id = irq_read_line(motg->pdata->pmic_id_irq);
1648 local_irq_restore(flags);
1649
1650 /*
1651 * If we can not read ID line state for some reason, treat
1652 * it as float. This would prevent MHL discovery and kicking
1653 * host mode unnecessarily.
1654 */
1655 return !!id;
1656}
1657
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301658static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1659 void (*callback)(int on))
1660{
1661 struct usb_phy *phy = &motg->phy;
1662 int ret;
1663
Manoj Raoa7bddd12012-08-27 20:36:45 -07001664 if (!motg->pdata->mhl_enable) {
1665 dev_dbg(phy->dev, "MHL feature not enabled\n");
1666 return -ENODEV;
1667 }
1668
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301669 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1670 !motg->pdata->pmic_id_irq) {
1671 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1672 return -ENODEV;
1673 }
1674
1675 if (!motg->pdata->mhl_dev_name) {
1676 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1677 return -ENODEV;
1678 }
1679
1680 if (callback)
1681 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1682 callback);
1683 else
1684 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1685
1686 if (ret)
1687 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1688 motg->pdata->mhl_dev_name, ret);
1689 else
1690 motg->mhl_enabled = true;
1691
1692 return ret;
1693}
1694
1695static void msm_otg_mhl_notify_online(int on)
1696{
1697 struct msm_otg *motg = the_msm_otg;
1698 struct usb_phy *phy = &motg->phy;
1699 bool queue = false;
1700
1701 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1702
1703 if (on) {
1704 set_bit(MHL, &motg->inputs);
1705 } else {
1706 clear_bit(MHL, &motg->inputs);
1707 queue = true;
1708 }
1709
1710 if (queue && phy->state != OTG_STATE_UNDEFINED)
1711 schedule_work(&motg->sm_work);
1712}
1713
1714static bool msm_otg_is_mhl(struct msm_otg *motg)
1715{
1716 struct usb_phy *phy = &motg->phy;
1717 int is_mhl, ret;
1718
1719 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1720 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1721 /*
1722 * MHL driver calls our callback saying that MHL connected
1723 * if RID_GND is detected. But at later part of discovery
1724 * it may figure out MHL is not connected and returns
1725 * false. Hence clear MHL input here.
1726 */
1727 clear_bit(MHL, &motg->inputs);
1728 dev_dbg(phy->dev, "MHL device not found\n");
1729 return false;
1730 }
1731
1732 set_bit(MHL, &motg->inputs);
1733 dev_dbg(phy->dev, "MHL device found\n");
1734 return true;
1735}
1736
1737static bool msm_chg_mhl_detect(struct msm_otg *motg)
1738{
1739 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301740
1741 if (!motg->mhl_enabled)
1742 return false;
1743
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301744 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301745
1746 if (id)
1747 return false;
1748
1749 mhl_det_in_progress = true;
1750 ret = msm_otg_is_mhl(motg);
1751 mhl_det_in_progress = false;
1752
1753 return ret;
1754}
1755
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301756static void msm_otg_chg_check_timer_func(unsigned long data)
1757{
1758 struct msm_otg *motg = (struct msm_otg *) data;
1759 struct usb_otg *otg = motg->phy.otg;
1760
1761 if (atomic_read(&motg->in_lpm) ||
1762 !test_bit(B_SESS_VLD, &motg->inputs) ||
1763 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1764 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1765 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1766 return;
1767 }
1768
1769 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1770 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1771 set_bit(B_FALSE_SDP, &motg->inputs);
1772 queue_work(system_nrt_wq, &motg->sm_work);
1773 }
1774}
1775
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776static bool msm_chg_aca_detect(struct msm_otg *motg)
1777{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001778 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001779 u32 int_sts;
1780 bool ret = false;
1781
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301782 if (!aca_enabled())
1783 goto out;
1784
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001785 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1786 goto out;
1787
Steve Mucklef132c6c2012-06-06 18:30:57 -07001788 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001789 switch (int_sts & 0x1C) {
1790 case 0x08:
1791 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001792 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001793 motg->chg_type = USB_ACA_A_CHARGER;
1794 motg->chg_state = USB_CHG_STATE_DETECTED;
1795 clear_bit(ID_B, &motg->inputs);
1796 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301797 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001798 ret = true;
1799 }
1800 break;
1801 case 0x0C:
1802 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001803 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001804 motg->chg_type = USB_ACA_B_CHARGER;
1805 motg->chg_state = USB_CHG_STATE_DETECTED;
1806 clear_bit(ID_A, &motg->inputs);
1807 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301808 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001809 ret = true;
1810 }
1811 break;
1812 case 0x10:
1813 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001814 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001815 motg->chg_type = USB_ACA_C_CHARGER;
1816 motg->chg_state = USB_CHG_STATE_DETECTED;
1817 clear_bit(ID_A, &motg->inputs);
1818 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301819 set_bit(ID, &motg->inputs);
1820 ret = true;
1821 }
1822 break;
1823 case 0x04:
1824 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001825 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301826 motg->chg_type = USB_INVALID_CHARGER;
1827 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1828 clear_bit(ID_A, &motg->inputs);
1829 clear_bit(ID_B, &motg->inputs);
1830 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001831 ret = true;
1832 }
1833 break;
1834 default:
1835 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1836 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301837 test_and_clear_bit(ID_C, &motg->inputs) |
1838 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001839 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001840 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001841 motg->chg_type = USB_INVALID_CHARGER;
1842 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1843 }
1844 }
1845out:
1846 return ret;
1847}
1848
1849static void msm_chg_enable_aca_det(struct msm_otg *motg)
1850{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001851 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001852
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301853 if (!aca_enabled())
1854 return;
1855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001856 switch (motg->pdata->phy_type) {
1857 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301858 /* Disable ID_GND in link and PHY */
1859 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1860 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001861 ulpi_write(phy, 0x01, 0x0C);
1862 ulpi_write(phy, 0x10, 0x0F);
1863 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301864 /* Disable PMIC ID pull-up */
1865 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301866 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001867 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301868 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001869 break;
1870 default:
1871 break;
1872 }
1873}
1874
1875static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1876{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001877 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001878
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301879 if (!aca_enabled())
1880 return;
1881
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882 switch (motg->pdata->phy_type) {
1883 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301884 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001885 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301886 break;
1887 default:
1888 break;
1889 }
1890}
1891
1892static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1893{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001894 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301895
1896 if (!aca_enabled())
1897 return;
1898
1899 switch (motg->pdata->phy_type) {
1900 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001901 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001902 break;
1903 default:
1904 break;
1905 }
1906}
1907
1908static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1909{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001910 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001911 bool ret = false;
1912
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301913 if (!aca_enabled())
1914 return ret;
1915
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001916 switch (motg->pdata->phy_type) {
1917 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001918 if (ulpi_read(phy, 0x91) & 1) {
1919 dev_dbg(phy->dev, "RID change\n");
1920 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001921 ret = msm_chg_aca_detect(motg);
1922 }
1923 default:
1924 break;
1925 }
1926 return ret;
1927}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301928
1929static void msm_otg_id_timer_func(unsigned long data)
1930{
1931 struct msm_otg *motg = (struct msm_otg *) data;
1932
1933 if (!aca_enabled())
1934 return;
1935
1936 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001937 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301938 return;
1939 }
1940
Steve Mucklef132c6c2012-06-06 18:30:57 -07001941 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301942 goto out;
1943
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301944 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001945 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301946 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301947 }
1948
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301949out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301950 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1951 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1952}
1953
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301954static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1955{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001956 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301957 u32 chg_det;
1958 bool ret = false;
1959
1960 switch (motg->pdata->phy_type) {
1961 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001962 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301963 ret = chg_det & (1 << 4);
1964 break;
1965 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001966 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301967 ret = chg_det & 1;
1968 break;
1969 default:
1970 break;
1971 }
1972 return ret;
1973}
1974
1975static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1976{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001977 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301978 u32 chg_det;
1979
1980 switch (motg->pdata->phy_type) {
1981 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001982 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301983 /* Turn off charger block */
1984 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001985 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301986 udelay(20);
1987 /* control chg block via ULPI */
1988 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001989 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301990 /* put it in host mode for enabling D- source */
1991 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001992 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301993 /* Turn on chg detect block */
1994 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001995 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301996 udelay(20);
1997 /* enable chg detection */
1998 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001999 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302000 break;
2001 case SNPS_28NM_INTEGRATED_PHY:
2002 /*
2003 * Configure DM as current source, DP as current sink
2004 * and enable battery charging comparators.
2005 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002006 ulpi_write(phy, 0x8, 0x85);
2007 ulpi_write(phy, 0x2, 0x85);
2008 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302009 break;
2010 default:
2011 break;
2012 }
2013}
2014
2015static bool msm_chg_check_primary_det(struct msm_otg *motg)
2016{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002017 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302018 u32 chg_det;
2019 bool ret = false;
2020
2021 switch (motg->pdata->phy_type) {
2022 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002023 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302024 ret = chg_det & (1 << 4);
2025 break;
2026 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002027 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302028 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302029 /* Turn off VDP_SRC */
2030 ulpi_write(phy, 0x3, 0x86);
2031 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302032 break;
2033 default:
2034 break;
2035 }
2036 return ret;
2037}
2038
2039static void msm_chg_enable_primary_det(struct msm_otg *motg)
2040{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002041 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302042 u32 chg_det;
2043
2044 switch (motg->pdata->phy_type) {
2045 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002046 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302047 /* enable chg detection */
2048 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002049 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302050 break;
2051 case SNPS_28NM_INTEGRATED_PHY:
2052 /*
2053 * Configure DP as current source, DM as current sink
2054 * and enable battery charging comparators.
2055 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002056 ulpi_write(phy, 0x2, 0x85);
2057 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302058 break;
2059 default:
2060 break;
2061 }
2062}
2063
2064static bool msm_chg_check_dcd(struct msm_otg *motg)
2065{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002066 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302067 u32 line_state;
2068 bool ret = false;
2069
2070 switch (motg->pdata->phy_type) {
2071 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002072 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302073 ret = !(line_state & 1);
2074 break;
2075 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002076 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302077 ret = line_state & 2;
2078 break;
2079 default:
2080 break;
2081 }
2082 return ret;
2083}
2084
2085static void msm_chg_disable_dcd(struct msm_otg *motg)
2086{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002087 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302088 u32 chg_det;
2089
2090 switch (motg->pdata->phy_type) {
2091 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002092 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302093 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002094 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302095 break;
2096 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002097 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302098 break;
2099 default:
2100 break;
2101 }
2102}
2103
2104static void msm_chg_enable_dcd(struct msm_otg *motg)
2105{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002106 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302107 u32 chg_det;
2108
2109 switch (motg->pdata->phy_type) {
2110 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002111 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302112 /* Turn on D+ current source */
2113 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002114 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302115 break;
2116 case SNPS_28NM_INTEGRATED_PHY:
2117 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002118 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302119 break;
2120 default:
2121 break;
2122 }
2123}
2124
2125static void msm_chg_block_on(struct msm_otg *motg)
2126{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002127 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302128 u32 func_ctrl, chg_det;
2129
2130 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002131 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302132 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2133 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002134 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302135
2136 switch (motg->pdata->phy_type) {
2137 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002138 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302139 /* control chg block via ULPI */
2140 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002141 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302142 /* Turn on chg detect block */
2143 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002144 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302145 udelay(20);
2146 break;
2147 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302148 /* disable DP and DM pull down resistors */
2149 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302150 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002151 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302152 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002153 ulpi_write(phy, 0x1F, 0x92);
2154 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302155 udelay(100);
2156 break;
2157 default:
2158 break;
2159 }
2160}
2161
2162static void msm_chg_block_off(struct msm_otg *motg)
2163{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002164 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302165 u32 func_ctrl, chg_det;
2166
2167 switch (motg->pdata->phy_type) {
2168 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002169 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302170 /* Turn off charger block */
2171 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002172 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302173 break;
2174 case SNPS_28NM_INTEGRATED_PHY:
2175 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002176 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302177 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002178 ulpi_write(phy, 0x1F, 0x92);
2179 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302180 break;
2181 default:
2182 break;
2183 }
2184
2185 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002186 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302187 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2188 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002189 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302190}
2191
Anji jonnalad270e2d2011-08-09 11:28:32 +05302192static const char *chg_to_string(enum usb_chg_type chg_type)
2193{
2194 switch (chg_type) {
2195 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2196 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2197 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2198 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2199 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2200 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2201 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302202 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302203 default: return "INVALID_CHARGER";
2204 }
2205}
2206
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302207#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2208#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302209#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2210#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302211static void msm_chg_detect_work(struct work_struct *w)
2212{
2213 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002214 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302215 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302216 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302217 unsigned long delay;
2218
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002219 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302220
2221 if (test_bit(MHL, &motg->inputs)) {
2222 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2223 return;
2224 }
2225
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302226 switch (motg->chg_state) {
2227 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302228 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302229 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002230 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302231 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302232 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302233 delay = MSM_CHG_DCD_POLL_TIME;
2234 break;
2235 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302236 if (msm_chg_mhl_detect(motg)) {
2237 msm_chg_block_off(motg);
2238 motg->chg_state = USB_CHG_STATE_DETECTED;
2239 motg->chg_type = USB_INVALID_CHARGER;
2240 queue_work(system_nrt_wq, &motg->sm_work);
2241 return;
2242 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002243 is_aca = msm_chg_aca_detect(motg);
2244 if (is_aca) {
2245 /*
2246 * ID_A can be ACA dock too. continue
2247 * primary detection after DCD.
2248 */
2249 if (test_bit(ID_A, &motg->inputs)) {
2250 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2251 } else {
2252 delay = 0;
2253 break;
2254 }
2255 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302256 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302257 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2258 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302259 if (is_dcd || tmout) {
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302260 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302261 msm_chg_enable_primary_det(motg);
2262 delay = MSM_CHG_PRIMARY_DET_TIME;
2263 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2264 } else {
2265 delay = MSM_CHG_DCD_POLL_TIME;
2266 }
2267 break;
2268 case USB_CHG_STATE_DCD_DONE:
2269 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302270 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2271 dm_vlgc = line_state & PORTSC_LS_DM;
2272 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302273 if (test_bit(ID_A, &motg->inputs)) {
2274 motg->chg_type = USB_ACA_DOCK_CHARGER;
2275 motg->chg_state = USB_CHG_STATE_DETECTED;
2276 delay = 0;
2277 break;
2278 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302279 if (line_state) { /* DP > VLGC */
2280 motg->chg_type = USB_PROPRIETARY_CHARGER;
2281 motg->chg_state = USB_CHG_STATE_DETECTED;
2282 delay = 0;
2283 } else {
2284 msm_chg_enable_secondary_det(motg);
2285 delay = MSM_CHG_SECONDARY_DET_TIME;
2286 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2287 }
2288 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302289 if (test_bit(ID_A, &motg->inputs)) {
2290 motg->chg_type = USB_ACA_A_CHARGER;
2291 motg->chg_state = USB_CHG_STATE_DETECTED;
2292 delay = 0;
2293 break;
2294 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302295
2296 if (line_state) /* DP > VLGC or/and DM > VLGC */
2297 motg->chg_type = USB_PROPRIETARY_CHARGER;
2298 else
2299 motg->chg_type = USB_SDP_CHARGER;
2300
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302301 motg->chg_state = USB_CHG_STATE_DETECTED;
2302 delay = 0;
2303 }
2304 break;
2305 case USB_CHG_STATE_PRIMARY_DONE:
2306 vout = msm_chg_check_secondary_det(motg);
2307 if (vout)
2308 motg->chg_type = USB_DCP_CHARGER;
2309 else
2310 motg->chg_type = USB_CDP_CHARGER;
2311 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2312 /* fall through */
2313 case USB_CHG_STATE_SECONDARY_DONE:
2314 motg->chg_state = USB_CHG_STATE_DETECTED;
2315 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302316 /*
2317 * Notify the charger type to power supply
2318 * owner as soon as we determine the charger.
2319 */
2320 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302321 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002322 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302323 /*
2324 * Spurious interrupt is seen after enabling ACA detection
2325 * due to which charger detection fails in case of PET.
2326 * Add delay of 100 microsec to avoid that.
2327 */
2328 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002329 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002330 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302331 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302332 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302333 return;
2334 default:
2335 return;
2336 }
2337
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302338 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302339}
2340
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302341/*
2342 * We support OTG, Peripheral only and Host only configurations. In case
2343 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2344 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2345 * enabled when switch is controlled by user and default mode is supplied
2346 * by board file, which can be changed by userspace later.
2347 */
2348static void msm_otg_init_sm(struct msm_otg *motg)
2349{
2350 struct msm_otg_platform_data *pdata = motg->pdata;
2351 u32 otgsc = readl(USB_OTGSC);
2352
2353 switch (pdata->mode) {
2354 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002355 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302356 if (pdata->default_mode == USB_HOST) {
2357 clear_bit(ID, &motg->inputs);
2358 } else if (pdata->default_mode == USB_PERIPHERAL) {
2359 set_bit(ID, &motg->inputs);
2360 set_bit(B_SESS_VLD, &motg->inputs);
2361 } else {
2362 set_bit(ID, &motg->inputs);
2363 clear_bit(B_SESS_VLD, &motg->inputs);
2364 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302365 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302366 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302367 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302368 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302369 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302370 set_bit(A_BUS_REQ, &motg->inputs);
2371 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372 if (otgsc & OTGSC_BSV)
2373 set_bit(B_SESS_VLD, &motg->inputs);
2374 else
2375 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302376 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302377 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302378 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302379 set_bit(ID, &motg->inputs);
2380 else
2381 clear_bit(ID, &motg->inputs);
2382 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302383 /*
2384 * VBUS initial state is reported after PMIC
2385 * driver initialization. Wait for it.
2386 */
2387 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302388 }
2389 break;
2390 case USB_HOST:
2391 clear_bit(ID, &motg->inputs);
2392 break;
2393 case USB_PERIPHERAL:
2394 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302395 if (pdata->otg_control == OTG_PHY_CONTROL) {
2396 if (otgsc & OTGSC_BSV)
2397 set_bit(B_SESS_VLD, &motg->inputs);
2398 else
2399 clear_bit(B_SESS_VLD, &motg->inputs);
2400 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2401 /*
2402 * VBUS initial state is reported after PMIC
2403 * driver initialization. Wait for it.
2404 */
2405 wait_for_completion(&pmic_vbus_init);
2406 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302407 break;
2408 default:
2409 break;
2410 }
2411}
2412
2413static void msm_otg_sm_work(struct work_struct *w)
2414{
2415 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002416 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302417 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302418
Steve Mucklef132c6c2012-06-06 18:30:57 -07002419 pm_runtime_resume(otg->phy->dev);
2420 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002421 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302422 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002423 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302424 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002425 if (!psy && legacy_power_supply) {
2426 psy = power_supply_get_by_name("usb");
2427
2428 if (!psy)
2429 pr_err("couldn't get usb power supply\n");
2430 }
2431
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002432 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302433 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2434 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002435 pm_runtime_put_noidle(otg->phy->dev);
2436 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302437 break;
2438 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302439 /* FALL THROUGH */
2440 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302441 if (test_bit(MHL, &motg->inputs)) {
2442 /* allow LPM */
2443 pm_runtime_put_noidle(otg->phy->dev);
2444 pm_runtime_suspend(otg->phy->dev);
2445 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002446 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302447 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302448 if (msm_chg_mhl_detect(motg)) {
2449 work = 1;
2450 break;
2451 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302452 clear_bit(B_BUS_REQ, &motg->inputs);
2453 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002454 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302455 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302456 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302457 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302458 switch (motg->chg_state) {
2459 case USB_CHG_STATE_UNDEFINED:
2460 msm_chg_detect_work(&motg->chg_work.work);
2461 break;
2462 case USB_CHG_STATE_DETECTED:
2463 switch (motg->chg_type) {
2464 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302465 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002466 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302467 /* fall through */
2468 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302469 msm_otg_notify_charger(motg,
2470 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002471 pm_runtime_put_noidle(otg->phy->dev);
2472 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302473 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302474 case USB_ACA_B_CHARGER:
2475 msm_otg_notify_charger(motg,
2476 IDEV_ACA_CHG_MAX);
2477 /*
2478 * (ID_B --> ID_C) PHY_ALT interrupt can
2479 * not be detected in LPM.
2480 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302481 break;
2482 case USB_CDP_CHARGER:
2483 msm_otg_notify_charger(motg,
2484 IDEV_CHG_MAX);
2485 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002486 otg->phy->state =
2487 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302488 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302489 case USB_ACA_C_CHARGER:
2490 msm_otg_notify_charger(motg,
2491 IDEV_ACA_CHG_MAX);
2492 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002493 otg->phy->state =
2494 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302495 break;
2496 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302497 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002498 otg->phy->state =
2499 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302500 mod_timer(&motg->chg_check_timer,
2501 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302502 break;
2503 default:
2504 break;
2505 }
2506 break;
2507 default:
2508 break;
2509 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302510 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2511 pr_debug("b_sess_end && b_bus_req\n");
2512 if (msm_otg_start_srp(otg) < 0) {
2513 clear_bit(B_BUS_REQ, &motg->inputs);
2514 work = 1;
2515 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302516 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002517 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302518 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2519 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302520 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302521 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302522 del_timer_sync(&motg->chg_check_timer);
2523 clear_bit(B_FALSE_SDP, &motg->inputs);
Mayank Rana8ab00352013-01-23 19:26:21 +05302524 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302525 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302526 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2527 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302528 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002529 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302530 /*
2531 * There is a small window where ID interrupt
2532 * is not monitored during ID detection circuit
2533 * switch from ACA to PMIC. Check ID state
2534 * before entering into low power mode.
2535 */
2536 if (!msm_otg_read_pmic_id_state(motg)) {
2537 pr_debug("process missed ID intr\n");
2538 clear_bit(ID, &motg->inputs);
2539 work = 1;
2540 break;
2541 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002542 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002543 /*
2544 * Only if autosuspend was enabled in probe, it will be
2545 * used here. Otherwise, no delay will be used.
2546 */
2547 pm_runtime_mark_last_busy(otg->phy->dev);
2548 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302549 }
2550 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302551 case OTG_STATE_B_SRP_INIT:
2552 if (!test_bit(ID, &motg->inputs) ||
2553 test_bit(ID_A, &motg->inputs) ||
2554 test_bit(ID_C, &motg->inputs) ||
2555 (test_bit(B_SESS_VLD, &motg->inputs) &&
2556 !test_bit(ID_B, &motg->inputs))) {
2557 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2558 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002559 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302560 /*
2561 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2562 * bits after SRP initiation.
2563 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002564 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302565 work = 1;
2566 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2567 pr_debug("b_srp_fail\n");
2568 pr_info("A-device did not respond to SRP\n");
2569 clear_bit(B_BUS_REQ, &motg->inputs);
2570 clear_bit(B_SRP_FAIL, &motg->tmouts);
2571 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002572 ulpi_write(otg->phy, 0x0, 0x98);
2573 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302574 motg->b_last_se0_sess = jiffies;
2575 work = 1;
2576 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302577 break;
2578 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302579 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2580 test_bit(B_FALSE_SDP, &motg->inputs)) {
2581 pr_debug("B_FALSE_SDP\n");
2582 msm_otg_start_peripheral(otg, 0);
2583 motg->chg_type = USB_DCP_CHARGER;
2584 clear_bit(B_FALSE_SDP, &motg->inputs);
2585 otg->phy->state = OTG_STATE_B_IDLE;
2586 work = 1;
2587 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302588 test_bit(ID_A, &motg->inputs) ||
2589 test_bit(ID_B, &motg->inputs) ||
2590 !test_bit(B_SESS_VLD, &motg->inputs)) {
2591 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302592 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2593 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302594 msm_otg_notify_charger(motg, 0);
2595 srp_reqd = otg->gadget->otg_srp_reqd;
2596 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302597 if (test_bit(ID_B, &motg->inputs))
2598 clear_bit(ID_B, &motg->inputs);
2599 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002600 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302601 motg->b_last_se0_sess = jiffies;
2602 if (srp_reqd)
2603 msm_otg_start_timer(motg,
2604 TB_TST_SRP, B_TST_SRP);
2605 else
2606 work = 1;
2607 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2608 otg->gadget->b_hnp_enable &&
2609 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2610 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2611 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2612 /* D+ pullup should not be disconnected within 4msec
2613 * after A device suspends the bus. Otherwise PET will
2614 * fail the compliance test.
2615 */
2616 udelay(1000);
2617 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002618 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302619 /*
2620 * start HCD even before A-device enable
2621 * pull-up to meet HNP timings.
2622 */
2623 otg->host->is_b_host = 1;
2624 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002625 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2626 test_bit(B_SESS_VLD, &motg->inputs)) {
2627 pr_debug("a_bus_suspend && b_sess_vld\n");
2628 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002629 pm_runtime_put_noidle(otg->phy->dev);
2630 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002631 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002632 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302633 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002634 }
2635 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302636 case OTG_STATE_B_WAIT_ACON:
2637 if (!test_bit(ID, &motg->inputs) ||
2638 test_bit(ID_A, &motg->inputs) ||
2639 test_bit(ID_B, &motg->inputs) ||
2640 !test_bit(B_SESS_VLD, &motg->inputs)) {
2641 pr_debug("!id || id_a/b || !b_sess_vld\n");
2642 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302643 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302644 * A-device is physically disconnected during
2645 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302646 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302647 msm_otg_start_host(otg, 0);
2648 otg->host->is_b_host = 0;
2649
2650 clear_bit(B_BUS_REQ, &motg->inputs);
2651 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2652 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002653 otg->phy->state = OTG_STATE_B_IDLE;
2654 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302655 work = 1;
2656 } else if (test_bit(A_CONN, &motg->inputs)) {
2657 pr_debug("a_conn\n");
2658 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002659 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302660 /*
2661 * PET disconnects D+ pullup after reset is generated
2662 * by B device in B_HOST role which is not detected by
2663 * B device. As workaorund , start timer of 300msec
2664 * and stop timer if A device is enumerated else clear
2665 * A_CONN.
2666 */
2667 msm_otg_start_timer(motg, TB_TST_CONFIG,
2668 B_TST_CONFIG);
2669 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2670 pr_debug("b_ase0_brst_tmout\n");
2671 pr_info("B HNP fail:No response from A device\n");
2672 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002673 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302674 otg->host->is_b_host = 0;
2675 clear_bit(B_ASE0_BRST, &motg->tmouts);
2676 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2677 clear_bit(B_BUS_REQ, &motg->inputs);
2678 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002679 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302680 work = 1;
2681 } else if (test_bit(ID_C, &motg->inputs)) {
2682 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2683 }
2684 break;
2685 case OTG_STATE_B_HOST:
2686 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2687 !test_bit(A_CONN, &motg->inputs) ||
2688 !test_bit(B_SESS_VLD, &motg->inputs)) {
2689 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2690 clear_bit(A_CONN, &motg->inputs);
2691 clear_bit(B_BUS_REQ, &motg->inputs);
2692 msm_otg_start_host(otg, 0);
2693 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002694 otg->phy->state = OTG_STATE_B_IDLE;
2695 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302696 work = 1;
2697 } else if (test_bit(ID_C, &motg->inputs)) {
2698 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2699 }
2700 break;
2701 case OTG_STATE_A_IDLE:
2702 otg->default_a = 1;
2703 if (test_bit(ID, &motg->inputs) &&
2704 !test_bit(ID_A, &motg->inputs)) {
2705 pr_debug("id && !id_a\n");
2706 otg->default_a = 0;
2707 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002708 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302709 del_timer_sync(&motg->id_timer);
2710 msm_otg_link_reset(motg);
2711 msm_chg_enable_aca_intr(motg);
2712 msm_otg_notify_charger(motg, 0);
2713 work = 1;
2714 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2715 (test_bit(A_SRP_DET, &motg->inputs) ||
2716 test_bit(A_BUS_REQ, &motg->inputs))) {
2717 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2718
2719 clear_bit(A_SRP_DET, &motg->inputs);
2720 /* Disable SRP detection */
2721 writel_relaxed((readl_relaxed(USB_OTGSC) &
2722 ~OTGSC_INTSTS_MASK) &
2723 ~OTGSC_DPIE, USB_OTGSC);
2724
Steve Mucklef132c6c2012-06-06 18:30:57 -07002725 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302726 /* VBUS should not be supplied before end of SRP pulse
2727 * generated by PET, if not complaince test fail.
2728 */
2729 usleep_range(10000, 12000);
2730 /* ACA: ID_A: Stop charging untill enumeration */
2731 if (test_bit(ID_A, &motg->inputs))
2732 msm_otg_notify_charger(motg, 0);
2733 else
2734 msm_hsusb_vbus_power(motg, 1);
2735 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2736 } else {
2737 pr_debug("No session requested\n");
2738 clear_bit(A_BUS_DROP, &motg->inputs);
2739 if (test_bit(ID_A, &motg->inputs)) {
2740 msm_otg_notify_charger(motg,
2741 IDEV_ACA_CHG_MAX);
2742 } else if (!test_bit(ID, &motg->inputs)) {
2743 msm_otg_notify_charger(motg, 0);
2744 /*
2745 * A-device is not providing power on VBUS.
2746 * Enable SRP detection.
2747 */
2748 writel_relaxed(0x13, USB_USBMODE);
2749 writel_relaxed((readl_relaxed(USB_OTGSC) &
2750 ~OTGSC_INTSTS_MASK) |
2751 OTGSC_DPIE, USB_OTGSC);
2752 mb();
2753 }
2754 }
2755 break;
2756 case OTG_STATE_A_WAIT_VRISE:
2757 if ((test_bit(ID, &motg->inputs) &&
2758 !test_bit(ID_A, &motg->inputs)) ||
2759 test_bit(A_BUS_DROP, &motg->inputs) ||
2760 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2761 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2762 clear_bit(A_BUS_REQ, &motg->inputs);
2763 msm_otg_del_timer(motg);
2764 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002765 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302766 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2767 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2768 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002769 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302770 if (TA_WAIT_BCON > 0)
2771 msm_otg_start_timer(motg, TA_WAIT_BCON,
2772 A_WAIT_BCON);
2773 msm_otg_start_host(otg, 1);
2774 msm_chg_enable_aca_det(motg);
2775 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302776 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302777 if (msm_chg_check_aca_intr(motg))
2778 work = 1;
2779 }
2780 break;
2781 case OTG_STATE_A_WAIT_BCON:
2782 if ((test_bit(ID, &motg->inputs) &&
2783 !test_bit(ID_A, &motg->inputs)) ||
2784 test_bit(A_BUS_DROP, &motg->inputs) ||
2785 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2786 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2787 "a_wait_bcon_tmout\n");
2788 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2789 pr_info("Device No Response\n");
2790 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2791 }
2792 msm_otg_del_timer(motg);
2793 clear_bit(A_BUS_REQ, &motg->inputs);
2794 clear_bit(B_CONN, &motg->inputs);
2795 msm_otg_start_host(otg, 0);
2796 /*
2797 * ACA: ID_A with NO accessory, just the A plug is
2798 * attached to ACA: Use IDCHG_MAX for charging
2799 */
2800 if (test_bit(ID_A, &motg->inputs))
2801 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2802 else
2803 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002804 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302805 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2806 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2807 pr_debug("!a_vbus_vld\n");
2808 clear_bit(B_CONN, &motg->inputs);
2809 msm_otg_del_timer(motg);
2810 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002811 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2812 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302813 } else if (test_bit(ID_A, &motg->inputs)) {
2814 msm_hsusb_vbus_power(motg, 0);
2815 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2816 /*
2817 * If TA_WAIT_BCON is infinite, we don;t
2818 * turn off VBUS. Enter low power mode.
2819 */
2820 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002821 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302822 } else if (!test_bit(ID, &motg->inputs)) {
2823 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302824 }
2825 break;
2826 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302827 if ((test_bit(ID, &motg->inputs) &&
2828 !test_bit(ID_A, &motg->inputs)) ||
2829 test_bit(A_BUS_DROP, &motg->inputs)) {
2830 pr_debug("id_a/b/c || a_bus_drop\n");
2831 clear_bit(B_CONN, &motg->inputs);
2832 clear_bit(A_BUS_REQ, &motg->inputs);
2833 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002834 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302835 msm_otg_start_host(otg, 0);
2836 if (!test_bit(ID_A, &motg->inputs))
2837 msm_hsusb_vbus_power(motg, 0);
2838 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2839 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2840 pr_debug("!a_vbus_vld\n");
2841 clear_bit(B_CONN, &motg->inputs);
2842 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002843 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302844 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002845 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302846 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2847 /*
2848 * a_bus_req is de-asserted when root hub is
2849 * suspended or HNP is in progress.
2850 */
2851 pr_debug("!a_bus_req\n");
2852 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002853 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302854 if (otg->host->b_hnp_enable)
2855 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2856 A_AIDL_BDIS);
2857 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002858 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302859 } else if (!test_bit(B_CONN, &motg->inputs)) {
2860 pr_debug("!b_conn\n");
2861 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002862 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302863 if (TA_WAIT_BCON > 0)
2864 msm_otg_start_timer(motg, TA_WAIT_BCON,
2865 A_WAIT_BCON);
2866 if (msm_chg_check_aca_intr(motg))
2867 work = 1;
2868 } else if (test_bit(ID_A, &motg->inputs)) {
2869 msm_otg_del_timer(motg);
2870 msm_hsusb_vbus_power(motg, 0);
2871 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2872 msm_otg_notify_charger(motg,
2873 IDEV_ACA_CHG_MAX);
2874 else
2875 msm_otg_notify_charger(motg,
2876 IDEV_CHG_MIN - motg->mA_port);
2877 } else if (!test_bit(ID, &motg->inputs)) {
2878 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2879 motg->chg_type = USB_INVALID_CHARGER;
2880 msm_otg_notify_charger(motg, 0);
2881 msm_hsusb_vbus_power(motg, 1);
2882 }
2883 break;
2884 case OTG_STATE_A_SUSPEND:
2885 if ((test_bit(ID, &motg->inputs) &&
2886 !test_bit(ID_A, &motg->inputs)) ||
2887 test_bit(A_BUS_DROP, &motg->inputs) ||
2888 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2889 pr_debug("id_a/b/c || a_bus_drop ||"
2890 "a_aidl_bdis_tmout\n");
2891 msm_otg_del_timer(motg);
2892 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002893 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302894 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002895 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302896 if (!test_bit(ID_A, &motg->inputs))
2897 msm_hsusb_vbus_power(motg, 0);
2898 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2899 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2900 pr_debug("!a_vbus_vld\n");
2901 msm_otg_del_timer(motg);
2902 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002903 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302904 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002905 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302906 } else if (!test_bit(B_CONN, &motg->inputs) &&
2907 otg->host->b_hnp_enable) {
2908 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002909 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302910 msm_otg_host_hnp_enable(otg, 1);
2911 otg->gadget->is_a_peripheral = 1;
2912 msm_otg_start_peripheral(otg, 1);
2913 } else if (!test_bit(B_CONN, &motg->inputs) &&
2914 !otg->host->b_hnp_enable) {
2915 pr_debug("!b_conn && !b_hnp_enable");
2916 /*
2917 * bus request is dropped during suspend.
2918 * acquire again for next device.
2919 */
2920 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002921 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302922 if (TA_WAIT_BCON > 0)
2923 msm_otg_start_timer(motg, TA_WAIT_BCON,
2924 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002925 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302926 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002927 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302928 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002929 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002930 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302931 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302932 }
2933 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302934 case OTG_STATE_A_PERIPHERAL:
2935 if ((test_bit(ID, &motg->inputs) &&
2936 !test_bit(ID_A, &motg->inputs)) ||
2937 test_bit(A_BUS_DROP, &motg->inputs)) {
2938 pr_debug("id _f/b/c || a_bus_drop\n");
2939 /* Clear BIDL_ADIS timer */
2940 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002941 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302942 msm_otg_start_peripheral(otg, 0);
2943 otg->gadget->is_a_peripheral = 0;
2944 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002945 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302946 if (!test_bit(ID_A, &motg->inputs))
2947 msm_hsusb_vbus_power(motg, 0);
2948 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2949 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2950 pr_debug("!a_vbus_vld\n");
2951 /* Clear BIDL_ADIS timer */
2952 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002953 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302954 msm_otg_start_peripheral(otg, 0);
2955 otg->gadget->is_a_peripheral = 0;
2956 msm_otg_start_host(otg, 0);
2957 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2958 pr_debug("a_bidl_adis_tmout\n");
2959 msm_otg_start_peripheral(otg, 0);
2960 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002961 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302962 set_bit(A_BUS_REQ, &motg->inputs);
2963 msm_otg_host_hnp_enable(otg, 0);
2964 if (TA_WAIT_BCON > 0)
2965 msm_otg_start_timer(motg, TA_WAIT_BCON,
2966 A_WAIT_BCON);
2967 } else if (test_bit(ID_A, &motg->inputs)) {
2968 msm_hsusb_vbus_power(motg, 0);
2969 msm_otg_notify_charger(motg,
2970 IDEV_CHG_MIN - motg->mA_port);
2971 } else if (!test_bit(ID, &motg->inputs)) {
2972 msm_otg_notify_charger(motg, 0);
2973 msm_hsusb_vbus_power(motg, 1);
2974 }
2975 break;
2976 case OTG_STATE_A_WAIT_VFALL:
2977 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2978 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002979 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302980 work = 1;
2981 }
2982 break;
2983 case OTG_STATE_A_VBUS_ERR:
2984 if ((test_bit(ID, &motg->inputs) &&
2985 !test_bit(ID_A, &motg->inputs)) ||
2986 test_bit(A_BUS_DROP, &motg->inputs) ||
2987 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002988 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302989 if (!test_bit(ID_A, &motg->inputs))
2990 msm_hsusb_vbus_power(motg, 0);
2991 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2992 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2993 motg->chg_type = USB_INVALID_CHARGER;
2994 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302995 }
2996 break;
2997 default:
2998 break;
2999 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303000 if (work)
3001 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303002}
3003
Amit Blayd0fe07b2012-09-05 16:42:09 +03003004static void msm_otg_suspend_work(struct work_struct *w)
3005{
3006 struct msm_otg *motg =
3007 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05303008
3009 /* This work is only for device bus suspend */
3010 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
3011 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003012}
3013
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303014static irqreturn_t msm_otg_irq(int irq, void *data)
3015{
3016 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003017 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303018 u32 otgsc = 0, usbsts, pc;
3019 bool work = 0;
3020 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303021
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303022 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07003023 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303024 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07003025 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07003026 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07003027 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303028 return IRQ_HANDLED;
3029 }
3030
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003031 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303032 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303033
3034 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303035 return IRQ_NONE;
3036
3037 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303038 if (otgsc & OTGSC_ID) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003039 dev_dbg(otg->phy->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303040 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303041 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003042 dev_dbg(otg->phy->dev, "ID clear\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303043 /*
3044 * Assert a_bus_req to supply power on
3045 * VBUS when Micro/Mini-A cable is connected
3046 * with out user intervention.
3047 */
3048 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303049 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303050 msm_chg_enable_aca_det(motg);
3051 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303052 writel_relaxed(otgsc, USB_OTGSC);
3053 work = 1;
3054 } else if (otgsc & OTGSC_DPIS) {
3055 pr_debug("DPIS detected\n");
3056 writel_relaxed(otgsc, USB_OTGSC);
3057 set_bit(A_SRP_DET, &motg->inputs);
3058 set_bit(A_BUS_REQ, &motg->inputs);
3059 work = 1;
Vamsi Krishnaef6e1bf2013-03-02 15:36:17 -08003060 } else if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303061 writel_relaxed(otgsc, USB_OTGSC);
3062 /*
3063 * BSV interrupt comes when operating as an A-device
3064 * (VBUS on/off).
3065 * But, handle BSV when charger is removed from ACA in ID_A
3066 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003067 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303068 !test_bit(ID_A, &motg->inputs))
3069 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303070 if (otgsc & OTGSC_BSV) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003071 dev_dbg(otg->phy->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303072 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303073 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003074 dev_dbg(otg->phy->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303075 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003076 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3077
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303078 msm_chg_check_aca_intr(motg);
3079 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303080 work = 1;
3081 } else if (usbsts & STS_PCI) {
3082 pc = readl_relaxed(USB_PORTSC);
3083 pr_debug("portsc = %x\n", pc);
3084 ret = IRQ_NONE;
3085 /*
3086 * HCD Acks PCI interrupt. We use this to switch
3087 * between different OTG states.
3088 */
3089 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003090 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303091 case OTG_STATE_A_SUSPEND:
3092 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3093 !(pc & PORTSC_CCS)) {
3094 pr_debug("B_CONN clear\n");
3095 clear_bit(B_CONN, &motg->inputs);
3096 msm_otg_del_timer(motg);
3097 }
3098 break;
3099 case OTG_STATE_A_PERIPHERAL:
3100 /*
3101 * A-peripheral observed activity on bus.
3102 * clear A_BIDL_ADIS timer.
3103 */
3104 msm_otg_del_timer(motg);
3105 work = 0;
3106 break;
3107 case OTG_STATE_B_WAIT_ACON:
3108 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3109 pr_debug("A_CONN set\n");
3110 set_bit(A_CONN, &motg->inputs);
3111 /* Clear ASE0_BRST timer */
3112 msm_otg_del_timer(motg);
3113 }
3114 break;
3115 case OTG_STATE_B_HOST:
3116 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3117 pr_debug("A_CONN clear\n");
3118 clear_bit(A_CONN, &motg->inputs);
3119 msm_otg_del_timer(motg);
3120 }
3121 break;
3122 case OTG_STATE_A_WAIT_BCON:
3123 if (TA_WAIT_BCON < 0)
3124 set_bit(A_BUS_REQ, &motg->inputs);
3125 default:
3126 work = 0;
3127 break;
3128 }
3129 } else if (usbsts & STS_URI) {
3130 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003131 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303132 case OTG_STATE_A_PERIPHERAL:
3133 /*
3134 * A-peripheral observed activity on bus.
3135 * clear A_BIDL_ADIS timer.
3136 */
3137 msm_otg_del_timer(motg);
3138 work = 0;
3139 break;
3140 default:
3141 work = 0;
3142 break;
3143 }
3144 } else if (usbsts & STS_SLI) {
3145 ret = IRQ_NONE;
3146 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003147 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303148 case OTG_STATE_B_PERIPHERAL:
3149 if (otg->gadget->b_hnp_enable) {
3150 set_bit(A_BUS_SUSPEND, &motg->inputs);
3151 set_bit(B_BUS_REQ, &motg->inputs);
3152 work = 1;
3153 }
3154 break;
3155 case OTG_STATE_A_PERIPHERAL:
3156 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3157 A_BIDL_ADIS);
3158 break;
3159 default:
3160 break;
3161 }
3162 } else if ((usbsts & PHY_ALT_INT)) {
3163 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3164 if (msm_chg_check_aca_intr(motg))
3165 work = 1;
3166 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303167 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303168 if (work)
3169 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303170
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303171 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003172}
3173
3174static void msm_otg_set_vbus_state(int online)
3175{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303176 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003177 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303178
Vamsi Krishna945b4a92013-03-02 15:31:16 -08003179 if (online) {
3180 pr_debug("PMIC: BSV set\n");
3181 set_bit(B_SESS_VLD, &motg->inputs);
3182 } else {
3183 pr_debug("PMIC: BSV clear\n");
3184 clear_bit(B_SESS_VLD, &motg->inputs);
3185 }
3186
3187 /* do not queue state m/c work if id is grounded */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303188 if (!test_bit(ID, &motg->inputs)) {
3189 /*
3190 * state machine work waits for initial VBUS
3191 * completion in UNDEFINED state. Process
3192 * the initial VBUS event in ID_GND state.
3193 */
3194 if (init)
3195 return;
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303196 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003197
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303198 if (!init) {
3199 init = true;
3200 complete(&pmic_vbus_init);
3201 pr_debug("PMIC: BSV init complete\n");
3202 return;
3203 }
3204
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303205 if (test_bit(MHL, &motg->inputs) ||
3206 mhl_det_in_progress) {
3207 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3208 return;
3209 }
3210
Jack Pham5ca279b2012-05-14 18:42:54 -07003211 if (atomic_read(&motg->pm_suspended))
3212 motg->sm_work_pending = true;
3213 else
3214 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003215}
3216
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303217static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003218{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303219 struct msm_otg *motg = container_of(w, struct msm_otg,
3220 pmic_id_status_work.work);
3221 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003222
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303223 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303224 if (!test_and_set_bit(ID, &motg->inputs)) {
3225 pr_debug("PMIC: ID set\n");
3226 work = 1;
3227 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303228 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303229 if (test_and_clear_bit(ID, &motg->inputs)) {
3230 pr_debug("PMIC: ID clear\n");
3231 set_bit(A_BUS_REQ, &motg->inputs);
3232 work = 1;
3233 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303234 }
3235
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303236 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003237 if (atomic_read(&motg->pm_suspended))
3238 motg->sm_work_pending = true;
3239 else
3240 queue_work(system_nrt_wq, &motg->sm_work);
3241 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303242
3243}
3244
3245#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3246static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3247{
3248 struct msm_otg *motg = data;
3249
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303250 if (test_bit(MHL, &motg->inputs) ||
3251 mhl_det_in_progress) {
3252 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3253 return IRQ_HANDLED;
3254 }
3255
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303256 if (!aca_id_turned_on)
3257 /*schedule delayed work for 5msec for ID line state to settle*/
3258 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3259 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003260
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303261 return IRQ_HANDLED;
3262}
3263
3264static int msm_otg_mode_show(struct seq_file *s, void *unused)
3265{
3266 struct msm_otg *motg = s->private;
Stephen Boyd9850acb2013-01-28 14:11:20 -08003267 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303268
Stephen Boyd9850acb2013-01-28 14:11:20 -08003269 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303270 case OTG_STATE_A_HOST:
3271 seq_printf(s, "host\n");
3272 break;
3273 case OTG_STATE_B_PERIPHERAL:
3274 seq_printf(s, "peripheral\n");
3275 break;
3276 default:
3277 seq_printf(s, "none\n");
3278 break;
3279 }
3280
3281 return 0;
3282}
3283
3284static int msm_otg_mode_open(struct inode *inode, struct file *file)
3285{
3286 return single_open(file, msm_otg_mode_show, inode->i_private);
3287}
3288
3289static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3290 size_t count, loff_t *ppos)
3291{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303292 struct seq_file *s = file->private_data;
3293 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303294 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003295 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303296 int status = count;
3297 enum usb_mode_type req_mode;
3298
3299 memset(buf, 0x00, sizeof(buf));
3300
3301 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3302 status = -EFAULT;
3303 goto out;
3304 }
3305
3306 if (!strncmp(buf, "host", 4)) {
3307 req_mode = USB_HOST;
3308 } else if (!strncmp(buf, "peripheral", 10)) {
3309 req_mode = USB_PERIPHERAL;
3310 } else if (!strncmp(buf, "none", 4)) {
3311 req_mode = USB_NONE;
3312 } else {
3313 status = -EINVAL;
3314 goto out;
3315 }
3316
3317 switch (req_mode) {
3318 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003319 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303320 case OTG_STATE_A_HOST:
3321 case OTG_STATE_B_PERIPHERAL:
3322 set_bit(ID, &motg->inputs);
3323 clear_bit(B_SESS_VLD, &motg->inputs);
3324 break;
3325 default:
3326 goto out;
3327 }
3328 break;
3329 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003330 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303331 case OTG_STATE_B_IDLE:
3332 case OTG_STATE_A_HOST:
3333 set_bit(ID, &motg->inputs);
3334 set_bit(B_SESS_VLD, &motg->inputs);
3335 break;
3336 default:
3337 goto out;
3338 }
3339 break;
3340 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003341 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303342 case OTG_STATE_B_IDLE:
3343 case OTG_STATE_B_PERIPHERAL:
3344 clear_bit(ID, &motg->inputs);
3345 break;
3346 default:
3347 goto out;
3348 }
3349 break;
3350 default:
3351 goto out;
3352 }
3353
Steve Mucklef132c6c2012-06-06 18:30:57 -07003354 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303355 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303356out:
3357 return status;
3358}
3359
3360const struct file_operations msm_otg_mode_fops = {
3361 .open = msm_otg_mode_open,
3362 .read = seq_read,
3363 .write = msm_otg_mode_write,
3364 .llseek = seq_lseek,
3365 .release = single_release,
3366};
3367
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303368static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3369{
3370 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003371 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303372
Steve Mucklef132c6c2012-06-06 18:30:57 -07003373 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303374 return 0;
3375}
3376
3377static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3378{
3379 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3380}
3381
3382const struct file_operations msm_otg_state_fops = {
3383 .open = msm_otg_otg_state_open,
3384 .read = seq_read,
3385 .llseek = seq_lseek,
3386 .release = single_release,
3387};
3388
Anji jonnalad270e2d2011-08-09 11:28:32 +05303389static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3390{
3391 struct msm_otg *motg = s->private;
3392
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303393 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303394 return 0;
3395}
3396
3397static int msm_otg_chg_open(struct inode *inode, struct file *file)
3398{
3399 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3400}
3401
3402const struct file_operations msm_otg_chg_fops = {
3403 .open = msm_otg_chg_open,
3404 .read = seq_read,
3405 .llseek = seq_lseek,
3406 .release = single_release,
3407};
3408
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303409static int msm_otg_aca_show(struct seq_file *s, void *unused)
3410{
3411 if (debug_aca_enabled)
3412 seq_printf(s, "enabled\n");
3413 else
3414 seq_printf(s, "disabled\n");
3415
3416 return 0;
3417}
3418
3419static int msm_otg_aca_open(struct inode *inode, struct file *file)
3420{
3421 return single_open(file, msm_otg_aca_show, inode->i_private);
3422}
3423
3424static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3425 size_t count, loff_t *ppos)
3426{
3427 char buf[8];
3428
3429 memset(buf, 0x00, sizeof(buf));
3430
3431 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3432 return -EFAULT;
3433
3434 if (!strncmp(buf, "enable", 6))
3435 debug_aca_enabled = true;
3436 else
3437 debug_aca_enabled = false;
3438
3439 return count;
3440}
3441
3442const struct file_operations msm_otg_aca_fops = {
3443 .open = msm_otg_aca_open,
3444 .read = seq_read,
3445 .write = msm_otg_aca_write,
3446 .llseek = seq_lseek,
3447 .release = single_release,
3448};
3449
Manu Gautam8bdcc592012-03-06 11:26:06 +05303450static int msm_otg_bus_show(struct seq_file *s, void *unused)
3451{
3452 if (debug_bus_voting_enabled)
3453 seq_printf(s, "enabled\n");
3454 else
3455 seq_printf(s, "disabled\n");
3456
3457 return 0;
3458}
3459
3460static int msm_otg_bus_open(struct inode *inode, struct file *file)
3461{
3462 return single_open(file, msm_otg_bus_show, inode->i_private);
3463}
3464
3465static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3466 size_t count, loff_t *ppos)
3467{
3468 char buf[8];
3469 int ret;
3470 struct seq_file *s = file->private_data;
3471 struct msm_otg *motg = s->private;
3472
3473 memset(buf, 0x00, sizeof(buf));
3474
3475 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3476 return -EFAULT;
3477
3478 if (!strncmp(buf, "enable", 6)) {
3479 /* Do not vote here. Let OTG statemachine decide when to vote */
3480 debug_bus_voting_enabled = true;
3481 } else {
3482 debug_bus_voting_enabled = false;
3483 if (motg->bus_perf_client) {
3484 ret = msm_bus_scale_client_update_request(
3485 motg->bus_perf_client, 0);
3486 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003487 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303488 "for bus bw %d\n", __func__, ret);
3489 }
3490 }
3491
3492 return count;
3493}
3494
David Keitel272ce522012-08-17 16:25:24 -07003495static int otg_power_get_property_usb(struct power_supply *psy,
3496 enum power_supply_property psp,
3497 union power_supply_propval *val)
3498{
Jack Pham0c695282012-10-19 18:13:03 -07003499 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003500 switch (psp) {
3501 case POWER_SUPPLY_PROP_SCOPE:
3502 if (motg->host_mode)
3503 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3504 else
3505 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3506 break;
3507 case POWER_SUPPLY_PROP_CURRENT_MAX:
3508 val->intval = motg->current_max;
3509 break;
3510 /* Reflect USB enumeration */
3511 case POWER_SUPPLY_PROP_PRESENT:
3512 case POWER_SUPPLY_PROP_ONLINE:
3513 val->intval = motg->online;
3514 break;
3515 default:
3516 return -EINVAL;
3517 }
3518 return 0;
3519}
3520
3521static int otg_power_set_property_usb(struct power_supply *psy,
3522 enum power_supply_property psp,
3523 const union power_supply_propval *val)
3524{
Jack Pham0c695282012-10-19 18:13:03 -07003525 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003526
3527 switch (psp) {
3528 /* Process PMIC notification in PRESENT prop */
3529 case POWER_SUPPLY_PROP_PRESENT:
3530 msm_otg_set_vbus_state(val->intval);
3531 break;
3532 /* The ONLINE property reflects if usb has enumerated */
3533 case POWER_SUPPLY_PROP_ONLINE:
3534 motg->online = val->intval;
3535 break;
3536 case POWER_SUPPLY_PROP_CURRENT_MAX:
3537 motg->current_max = val->intval;
3538 break;
3539 default:
3540 return -EINVAL;
3541 }
3542
3543 power_supply_changed(&motg->usb_psy);
3544 return 0;
3545}
3546
David Collinsd79acc52012-11-26 14:59:00 -08003547static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3548 enum power_supply_property psp)
3549{
3550 switch (psp) {
3551 case POWER_SUPPLY_PROP_PRESENT:
3552 case POWER_SUPPLY_PROP_ONLINE:
3553 case POWER_SUPPLY_PROP_CURRENT_MAX:
3554 return 1;
3555 default:
3556 break;
3557 }
3558
3559 return 0;
3560}
3561
David Keitel272ce522012-08-17 16:25:24 -07003562static char *otg_pm_power_supplied_to[] = {
3563 "battery",
3564};
3565
3566static enum power_supply_property otg_pm_power_props_usb[] = {
3567 POWER_SUPPLY_PROP_PRESENT,
3568 POWER_SUPPLY_PROP_ONLINE,
3569 POWER_SUPPLY_PROP_CURRENT_MAX,
3570 POWER_SUPPLY_PROP_SCOPE,
3571};
3572
Manu Gautam8bdcc592012-03-06 11:26:06 +05303573const struct file_operations msm_otg_bus_fops = {
3574 .open = msm_otg_bus_open,
3575 .read = seq_read,
3576 .write = msm_otg_bus_write,
3577 .llseek = seq_lseek,
3578 .release = single_release,
3579};
3580
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303581static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303582
3583static int msm_otg_debugfs_init(struct msm_otg *motg)
3584{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303585 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303586
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303587 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3588
3589 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3590 return -ENODEV;
3591
Anji jonnalad270e2d2011-08-09 11:28:32 +05303592 if (motg->pdata->mode == USB_OTG &&
3593 motg->pdata->otg_control == OTG_USER_CONTROL) {
3594
Manu Gautam8bdcc592012-03-06 11:26:06 +05303595 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303596 S_IWUSR, msm_otg_dbg_root, motg,
3597 &msm_otg_mode_fops);
3598
Manu Gautam8bdcc592012-03-06 11:26:06 +05303599 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303600 debugfs_remove(msm_otg_dbg_root);
3601 msm_otg_dbg_root = NULL;
3602 return -ENODEV;
3603 }
3604 }
3605
Manu Gautam8bdcc592012-03-06 11:26:06 +05303606 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303607 msm_otg_dbg_root, motg,
3608 &msm_otg_chg_fops);
3609
Manu Gautam8bdcc592012-03-06 11:26:06 +05303610 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303611 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303612 return -ENODEV;
3613 }
3614
Manu Gautam8bdcc592012-03-06 11:26:06 +05303615 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303616 msm_otg_dbg_root, motg,
3617 &msm_otg_aca_fops);
3618
Manu Gautam8bdcc592012-03-06 11:26:06 +05303619 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303620 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303621 return -ENODEV;
3622 }
3623
Manu Gautam8bdcc592012-03-06 11:26:06 +05303624 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3625 msm_otg_dbg_root, motg,
3626 &msm_otg_bus_fops);
3627
3628 if (!msm_otg_dentry) {
3629 debugfs_remove_recursive(msm_otg_dbg_root);
3630 return -ENODEV;
3631 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303632
3633 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3634 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3635
3636 if (!msm_otg_dentry) {
3637 debugfs_remove_recursive(msm_otg_dbg_root);
3638 return -ENODEV;
3639 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303640 return 0;
3641}
3642
3643static void msm_otg_debugfs_cleanup(void)
3644{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303645 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303646}
3647
Manu Gautam0ddbd922012-09-21 17:17:38 +05303648#define MSM_OTG_CMD_ID 0x09
3649#define MSM_OTG_DEVICE_ID 0x04
3650#define MSM_OTG_VMID_IDX 0xFF
3651#define MSM_OTG_MEM_TYPE 0x02
3652struct msm_otg_scm_cmd_buf {
3653 unsigned int device_id;
3654 unsigned int vmid_idx;
3655 unsigned int mem_type;
3656} __attribute__ ((__packed__));
3657
3658static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3659{
3660 int ret;
3661 struct msm_otg_platform_data *pdata = motg->pdata;
3662 struct msm_otg_scm_cmd_buf cmd_buf;
3663
3664 if (!pdata->pnoc_errata_fix)
3665 return;
3666
3667 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3668
3669 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3670 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3671 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3672
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -07003673 ret = scm_call(SCM_SVC_MP, MSM_OTG_CMD_ID, &cmd_buf,
Manu Gautam0ddbd922012-09-21 17:17:38 +05303674 sizeof(cmd_buf), NULL, 0);
3675
3676 if (ret)
3677 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3678}
3679
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303680static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3681static struct platform_device *msm_otg_add_pdev(
3682 struct platform_device *ofdev, const char *name)
3683{
3684 struct platform_device *pdev;
3685 const struct resource *res = ofdev->resource;
3686 unsigned int num = ofdev->num_resources;
3687 int retval;
3688
3689 pdev = platform_device_alloc(name, -1);
3690 if (!pdev) {
3691 retval = -ENOMEM;
3692 goto error;
3693 }
3694
3695 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3696 pdev->dev.dma_mask = &msm_otg_dma_mask;
3697
3698 if (num) {
3699 retval = platform_device_add_resources(pdev, res, num);
3700 if (retval)
3701 goto error;
3702 }
3703
3704 retval = platform_device_add(pdev);
3705 if (retval)
3706 goto error;
3707
3708 return pdev;
3709
3710error:
3711 platform_device_put(pdev);
3712 return ERR_PTR(retval);
3713}
3714
3715static int msm_otg_setup_devices(struct platform_device *ofdev,
3716 enum usb_mode_type mode, bool init)
3717{
3718 const char *gadget_name = "msm_hsusb";
3719 const char *host_name = "msm_hsusb_host";
3720 static struct platform_device *gadget_pdev;
3721 static struct platform_device *host_pdev;
3722 int retval = 0;
3723
3724 if (!init) {
3725 if (gadget_pdev)
3726 platform_device_unregister(gadget_pdev);
3727 if (host_pdev)
3728 platform_device_unregister(host_pdev);
3729 return 0;
3730 }
3731
3732 switch (mode) {
3733 case USB_OTG:
3734 /* fall through */
3735 case USB_PERIPHERAL:
3736 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3737 if (IS_ERR(gadget_pdev)) {
3738 retval = PTR_ERR(gadget_pdev);
3739 break;
3740 }
3741 if (mode == USB_PERIPHERAL)
3742 break;
3743 /* fall through */
3744 case USB_HOST:
3745 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3746 if (IS_ERR(host_pdev)) {
3747 retval = PTR_ERR(host_pdev);
3748 if (mode == USB_OTG)
3749 platform_device_unregister(gadget_pdev);
3750 }
3751 break;
3752 default:
3753 break;
3754 }
3755
3756 return retval;
3757}
3758
David Keitel272ce522012-08-17 16:25:24 -07003759static int msm_otg_register_power_supply(struct platform_device *pdev,
3760 struct msm_otg *motg)
3761{
3762 int ret;
3763
3764 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3765 if (ret < 0) {
3766 dev_err(motg->phy.dev,
3767 "%s:power_supply_register usb failed\n",
3768 __func__);
3769 return ret;
3770 }
3771
3772 legacy_power_supply = false;
3773 return 0;
3774}
3775
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303776struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3777{
3778 struct device_node *node = pdev->dev.of_node;
3779 struct msm_otg_platform_data *pdata;
3780 int len = 0;
3781
3782 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3783 if (!pdata) {
3784 pr_err("unable to allocate platform data\n");
3785 return NULL;
3786 }
3787 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3788 if (len) {
3789 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3790 if (!pdata->phy_init_seq)
3791 return NULL;
3792 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3793 pdata->phy_init_seq,
3794 len/sizeof(*pdata->phy_init_seq));
3795 }
3796 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3797 &pdata->power_budget);
3798 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3799 &pdata->mode);
3800 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3801 &pdata->otg_control);
3802 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3803 &pdata->default_mode);
3804 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3805 &pdata->phy_type);
Manu Gautambd53fba2012-07-31 16:13:06 +05303806 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3807 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05303808 pdata->pnoc_errata_fix = of_property_read_bool(node,
3809 "qcom,hsusb-otg-pnoc-errata-fix");
Ido Shayevitza7114b92013-01-13 13:34:47 +02003810 pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
3811 "qcom,hsusb-otg-lpm-on-dev-suspend");
Ido Shayevitz26193352013-01-21 23:16:54 +02003812 pdata->core_clk_always_on_workaround = of_property_read_bool(node,
3813 "qcom,hsusb-otg-clk-always-on-workaround");
Shimrit Malichiffab5b02013-03-10 11:06:16 +02003814 pdata->delay_lpm_on_disconnect = of_property_read_bool(node,
3815 "qcom,hsusb-otg-delay-lpm");
Lena Salmanabde35d2013-04-25 15:29:43 +03003816 pdata->delay_lpm_hndshk_on_disconnect = of_property_read_bool(node,
3817 "qcom,hsusb-otg-delay-lpm-hndshk-on-disconnect");
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08003818 pdata->dp_manual_pullup = of_property_read_bool(node,
3819 "qcom,dp-manual-pullup");
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05303820 pdata->enable_sec_phy = of_property_read_bool(node,
3821 "qcom,usb2-enable-hsphy2");
Manu Gautambd53fba2012-07-31 16:13:06 +05303822
Manu Gautam0e0c53f2013-04-04 10:55:20 +05303823 pdata->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
3824 if (pdata->pmic_id_irq < 0)
3825 pdata->pmic_id_irq = 0;
3826
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303827 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303828}
3829
3830static int __init msm_otg_probe(struct platform_device *pdev)
3831{
Manu Gautamf8c45642012-08-10 10:20:56 -07003832 int ret = 0;
Mayank Rana0f286cf2013-02-27 11:43:27 +05303833 int len = 0;
3834 u32 tmp[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303835 struct resource *res;
3836 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003837 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303838 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303839
3840 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303841
3842 if (pdev->dev.of_node) {
3843 dev_dbg(&pdev->dev, "device tree enabled\n");
3844 pdata = msm_otg_dt_to_pdata(pdev);
3845 if (!pdata)
3846 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003847
3848 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3849 if (!pdata->bus_scale_table)
3850 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3851
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303852 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3853 if (ret) {
3854 dev_err(&pdev->dev, "devices setup failed\n");
3855 return ret;
3856 }
3857 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303858 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3859 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303860 } else {
3861 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303862 }
3863
3864 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3865 if (!motg) {
3866 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3867 return -ENOMEM;
3868 }
3869
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003870 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3871 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003872 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3873 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303874 goto free_motg;
3875 }
3876
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003877 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303878 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003879 phy = &motg->phy;
3880 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303881
3882 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303883 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3884 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3885 * PMIC can detect ACA ID_GND and generate an interrupt.
3886 */
3887 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3888 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3889 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003890 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303891 }
3892
Ofir Cohen4da266f2012-01-03 10:19:29 +02003893 /* initialize reset counter */
3894 motg->reset_counter = 0;
3895
Amit Blay02eff132011-09-21 16:46:24 +03003896 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003897 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003898 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003899 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303900
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303901 /*
3902 * Targets on which link uses asynchronous reset methodology,
3903 * free running clock is not required during the reset.
3904 */
Manu Gautam5143b252012-01-05 19:25:23 -08003905 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303906 if (IS_ERR(motg->clk))
3907 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3908 else
3909 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303910
3911 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003912 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303913 * CORE CLK must be running at >55Mhz for correct HSUSB
3914 * operation and USB core cannot tolerate frequency changes on
3915 * CORE CLK. For such USB cores, vote for maximum clk frequency
3916 * on pclk source
3917 */
Manu Gautam5143b252012-01-05 19:25:23 -08003918 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3919 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303920 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003921 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303922 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003923 goto put_clk;
3924 }
Mayank Rana3eaf28d2013-03-27 14:04:04 +05303925
3926 /*
3927 * Get Max supported clk frequency for USB Core CLK and request
3928 * to set the same.
3929 */
3930 motg->core_clk_rate = clk_round_rate(motg->core_clk, LONG_MAX);
3931 if (IS_ERR_VALUE(motg->core_clk_rate)) {
3932 dev_err(&pdev->dev, "fail to get core clk max freq.\n");
3933 } else {
3934 ret = clk_set_rate(motg->core_clk, motg->core_clk_rate);
3935 if (ret)
3936 dev_err(&pdev->dev, "fail to set core_clk freq:%d\n",
3937 ret);
3938 }
Manu Gautam5143b252012-01-05 19:25:23 -08003939
3940 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3941 if (IS_ERR(motg->pclk)) {
3942 dev_err(&pdev->dev, "failed to get iface_clk\n");
3943 ret = PTR_ERR(motg->pclk);
3944 goto put_core_clk;
3945 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303946
3947 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3948 if (!res) {
3949 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3950 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003951 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303952 }
3953
3954 motg->regs = ioremap(res->start, resource_size(res));
3955 if (!motg->regs) {
3956 dev_err(&pdev->dev, "ioremap failed\n");
3957 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003958 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303959 }
3960 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3961
3962 motg->irq = platform_get_irq(pdev, 0);
3963 if (!motg->irq) {
3964 dev_err(&pdev->dev, "platform_get_irq failed\n");
3965 ret = -ENODEV;
3966 goto free_regs;
3967 }
3968
Manu Gautamf8c45642012-08-10 10:20:56 -07003969 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3970 if (motg->async_irq < 0) {
3971 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3972 motg->async_irq = 0;
3973 }
3974
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05303975 motg->xo_clk = clk_get(&pdev->dev, "xo");
3976 if (IS_ERR(motg->xo_clk)) {
3977 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
3978 if (IS_ERR(motg->xo_handle)) {
3979 dev_err(&pdev->dev, "%s fail to get handle for TCXO\n",
3980 __func__);
3981 ret = PTR_ERR(motg->xo_handle);
3982 goto free_regs;
3983 } else {
3984 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
3985 if (ret) {
3986 dev_err(&pdev->dev, "%s XO voting failed %d\n",
3987 __func__, ret);
3988 goto free_xo_handle;
3989 }
3990 }
3991 } else {
3992 ret = clk_prepare_enable(motg->xo_clk);
3993 if (ret) {
3994 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
3995 __func__, ret);
3996 goto free_xo_handle;
3997 }
Anji jonnala7da3f262011-12-02 17:22:14 -08003998 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303999
Anji jonnala7da3f262011-12-02 17:22:14 -08004000
Manu Gautam28b1bac2012-01-30 16:43:06 +05304001 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304002
Mayank Rana248698c2012-04-19 00:03:16 +05304003 motg->vdd_type = VDDCX_CORNER;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304004 hsusb_vdd = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
4005 if (IS_ERR(hsusb_vdd)) {
4006 hsusb_vdd = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
4007 if (IS_ERR(hsusb_vdd)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004008 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304009 ret = PTR_ERR(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304010 goto devote_xo_handle;
4011 }
4012 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304013 }
4014
Mayank Rana0f286cf2013-02-27 11:43:27 +05304015 if (pdev->dev.of_node) {
4016 of_get_property(pdev->dev.of_node,
4017 "qcom,vdd-voltage-level",
4018 &len);
4019 if (len == sizeof(tmp)) {
4020 of_property_read_u32_array(pdev->dev.of_node,
4021 "qcom,vdd-voltage-level",
4022 tmp, len/sizeof(*tmp));
4023 vdd_val[motg->vdd_type][0] = tmp[0];
4024 vdd_val[motg->vdd_type][1] = tmp[1];
4025 vdd_val[motg->vdd_type][2] = tmp[2];
4026 } else {
4027 dev_dbg(&pdev->dev, "Using default hsusb vdd config.\n");
4028 }
4029 }
4030
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004031 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304032 if (ret) {
4033 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05304034 goto devote_xo_handle;
4035 }
4036
Mayank Rana0f286cf2013-02-27 11:43:27 +05304037 ret = regulator_enable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304038 if (ret) {
4039 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
4040 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304041 }
4042
4043 ret = msm_hsusb_ldo_init(motg, 1);
4044 if (ret) {
4045 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304046 goto free_hsusb_vdd;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304047 }
4048
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304049 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05304050 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
4051 "mhl_usb_hs_switch");
4052 if (IS_ERR(mhl_usb_hs_switch)) {
4053 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07004054 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304055 goto free_ldo_init;
4056 }
4057 }
4058
Amit Blay81801aa2012-09-19 12:08:12 +02004059 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304060 if (ret) {
4061 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004062 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304063 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05304064 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304065
Manu Gautam0ddbd922012-09-21 17:17:38 +05304066 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
4067 msm_otg_pnoc_errata_fix(motg);
4068
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304069 writel(0, USB_USBINTR);
4070 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004071 /* Ensure that above STOREs are completed before enabling interrupts */
4072 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304073
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304074 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
4075 if (ret)
4076 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004077 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05304078 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304079 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304080 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304081 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004082 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304083 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
4084 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05304085 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
4086 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304087 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
4088 "msm_otg", motg);
4089 if (ret) {
4090 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004091 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304092 }
4093
Manu Gautamf8c45642012-08-10 10:20:56 -07004094 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304095 ret = request_irq(motg->async_irq, msm_otg_irq,
4096 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004097 if (ret) {
4098 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4099 goto free_irq;
4100 }
4101 disable_irq(motg->async_irq);
4102 }
4103
Jack Pham87f202f2012-08-06 00:24:22 -07004104 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4105 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4106
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004107 phy->init = msm_otg_reset;
4108 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004109 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304110
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004111 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304112
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004113 phy->otg->phy = &motg->phy;
4114 phy->otg->set_host = msm_otg_set_host;
4115 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004116 phy->otg->start_hnp = msm_otg_start_hnp;
4117 phy->otg->start_srp = msm_otg_start_srp;
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004118 if (pdata->dp_manual_pullup)
4119 phy->flags |= ENABLE_DP_MANUAL_PULLUP;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004120
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304121 if (pdata->enable_sec_phy)
4122 phy->flags |= ENABLE_SECONDARY_PHY;
4123
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004124 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304125 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004126 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004127 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304128 }
4129
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304130 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304131 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004132 if (motg->pdata->pmic_id_irq) {
4133 ret = request_irq(motg->pdata->pmic_id_irq,
4134 msm_pmic_id_irq,
4135 IRQF_TRIGGER_RISING |
4136 IRQF_TRIGGER_FALLING,
4137 "msm_otg", motg);
4138 if (ret) {
4139 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004140 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004141 }
4142 } else {
4143 ret = -ENODEV;
4144 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004145 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004146 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304147 }
4148
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304149 msm_hsusb_mhl_switch_enable(motg, 1);
4150
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304151 platform_set_drvdata(pdev, motg);
4152 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004153 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304154
Anji jonnalad270e2d2011-08-09 11:28:32 +05304155 ret = msm_otg_debugfs_init(motg);
4156 if (ret)
4157 dev_dbg(&pdev->dev, "mode debugfs file is"
4158 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304159
Amit Blay58b31472011-11-18 09:39:39 +02004160 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4161 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304162 (!(motg->pdata->mode == USB_OTG) ||
4163 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004164 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304165 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004166
Amit Blay58b31472011-11-18 09:39:39 +02004167 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004168 motg->caps = ALLOW_PHY_RETENTION |
4169 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02004170 }
4171
Amit Blay6fa647a2012-05-24 14:12:08 +03004172 if (motg->pdata->enable_lpm_on_dev_suspend)
4173 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4174
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004175 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304176 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004177 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304178
Amit Blayd6f38282012-10-29 13:13:46 +02004179 if (motg->pdata->delay_lpm_on_disconnect) {
4180 pm_runtime_set_autosuspend_delay(&pdev->dev,
4181 lpm_disconnect_thresh);
4182 pm_runtime_use_autosuspend(&pdev->dev);
4183 }
4184
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304185 if (motg->pdata->bus_scale_table) {
4186 motg->bus_perf_client =
4187 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4188 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004189 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304190 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05304191 else
4192 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304193 }
4194
David Keitel272ce522012-08-17 16:25:24 -07004195 motg->usb_psy.name = "usb";
4196 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4197 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4198 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4199 motg->usb_psy.properties = otg_pm_power_props_usb;
4200 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4201 motg->usb_psy.get_property = otg_power_get_property_usb;
4202 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004203 motg->usb_psy.property_is_writeable
4204 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004205
Jack Pham0c695282012-10-19 18:13:03 -07004206 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004207 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004208 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4209 legacy_power_supply = true;
4210 } else {
4211 /* otherwise register our own power supply */
4212 if (!msm_otg_register_power_supply(pdev, motg))
4213 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004214 }
4215
Jack Pham0c695282012-10-19 18:13:03 -07004216 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4217 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4218
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304219 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004220
Steve Mucklef132c6c2012-06-06 18:30:57 -07004221remove_phy:
4222 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004223free_async_irq:
4224 if (motg->async_irq)
4225 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304226free_irq:
4227 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004228destroy_wlock:
4229 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304230 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004231 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004232free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304233 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304234free_hsusb_vdd:
4235 regulator_disable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304236free_config_vddcx:
Mayank Rana0f286cf2013-02-27 11:43:27 +05304237 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304238 vdd_val[motg->vdd_type][VDD_NONE],
4239 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004240devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304241 clk_disable_unprepare(motg->pclk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304242 if (!IS_ERR(motg->xo_clk))
4243 clk_disable_unprepare(motg->xo_clk);
4244 else
4245 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004246free_xo_handle:
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304247 if (!IS_ERR(motg->xo_clk))
4248 clk_put(motg->xo_clk);
4249 else
4250 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304251free_regs:
4252 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004253put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304254 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304255put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004256 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304257put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304258 if (!IS_ERR(motg->clk))
4259 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004260 if (!IS_ERR(motg->phy_reset_clk))
4261 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004262free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004263 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304264free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304265 kfree(motg);
4266 return ret;
4267}
4268
4269static int __devexit msm_otg_remove(struct platform_device *pdev)
4270{
4271 struct msm_otg *motg = platform_get_drvdata(pdev);
Stephen Boyd9850acb2013-01-28 14:11:20 -08004272 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304273 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304274
Stephen Boyd9850acb2013-01-28 14:11:20 -08004275 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304276 return -EBUSY;
4277
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304278 if (pdev->dev.of_node)
4279 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004280 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4281 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304282 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304283 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304284 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304285 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004286 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304287 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304288
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304289 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304290
4291 device_init_wakeup(&pdev->dev, 0);
4292 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004293 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304294
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304295 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004296 if (motg->pdata->pmic_id_irq)
4297 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004298 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304299 free_irq(motg->irq, motg);
4300
Jack Pham87f202f2012-08-06 00:24:22 -07004301 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4302 motg->pdata->mpm_otgsessvld_int)
4303 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4304
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304305 /*
4306 * Put PHY in low power mode.
4307 */
Stephen Boyd9850acb2013-01-28 14:11:20 -08004308 ulpi_read(phy, 0x14);
4309 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304310
4311 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4312 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4313 if (readl(USB_PORTSC) & PORTSC_PHCD)
4314 break;
4315 udelay(1);
4316 cnt++;
4317 }
4318 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Stephen Boyd9850acb2013-01-28 14:11:20 -08004319 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304320
Manu Gautam28b1bac2012-01-30 16:43:06 +05304321 clk_disable_unprepare(motg->pclk);
4322 clk_disable_unprepare(motg->core_clk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304323 if (!IS_ERR(motg->xo_clk)) {
4324 clk_disable_unprepare(motg->xo_clk);
4325 clk_put(motg->xo_clk);
4326 } else {
4327 msm_xo_put(motg->xo_handle);
4328 }
Amit Blay81801aa2012-09-19 12:08:12 +02004329 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304330 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304331 regulator_disable(hsusb_vdd);
4332 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304333 vdd_val[motg->vdd_type][VDD_NONE],
4334 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304335
4336 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304337 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304338
Amit Blay02eff132011-09-21 16:46:24 +03004339 if (!IS_ERR(motg->phy_reset_clk))
4340 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304341 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304342 if (!IS_ERR(motg->clk))
4343 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004344 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304345
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304346 if (motg->bus_perf_client)
4347 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304348
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004349 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304350 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304351 return 0;
4352}
4353
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304354#ifdef CONFIG_PM_RUNTIME
4355static int msm_otg_runtime_idle(struct device *dev)
4356{
4357 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004358 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304359
4360 dev_dbg(dev, "OTG runtime idle\n");
4361
Steve Mucklef132c6c2012-06-06 18:30:57 -07004362 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304363 return -EAGAIN;
4364 else
4365 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304366}
4367
4368static int msm_otg_runtime_suspend(struct device *dev)
4369{
4370 struct msm_otg *motg = dev_get_drvdata(dev);
4371
4372 dev_dbg(dev, "OTG runtime suspend\n");
4373 return msm_otg_suspend(motg);
4374}
4375
4376static int msm_otg_runtime_resume(struct device *dev)
4377{
4378 struct msm_otg *motg = dev_get_drvdata(dev);
4379
4380 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304381 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304382 return msm_otg_resume(motg);
4383}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304384#endif
4385
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304386#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304387static int msm_otg_pm_suspend(struct device *dev)
4388{
Jack Pham5ca279b2012-05-14 18:42:54 -07004389 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304390 struct msm_otg *motg = dev_get_drvdata(dev);
4391
4392 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004393
4394 atomic_set(&motg->pm_suspended, 1);
4395 ret = msm_otg_suspend(motg);
4396 if (ret)
4397 atomic_set(&motg->pm_suspended, 0);
4398
4399 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304400}
4401
4402static int msm_otg_pm_resume(struct device *dev)
4403{
Jack Pham5ca279b2012-05-14 18:42:54 -07004404 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304405 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304406
4407 dev_dbg(dev, "OTG PM resume\n");
4408
Jack Pham5ca279b2012-05-14 18:42:54 -07004409 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004410 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004411 pm_runtime_get_noresume(dev);
4412 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304413
Jack Pham5ca279b2012-05-14 18:42:54 -07004414 /* Update runtime PM status */
4415 pm_runtime_disable(dev);
4416 pm_runtime_set_active(dev);
4417 pm_runtime_enable(dev);
4418
Jack Phamc7edb172012-08-13 15:32:39 -07004419 if (motg->sm_work_pending) {
4420 motg->sm_work_pending = false;
4421 queue_work(system_nrt_wq, &motg->sm_work);
4422 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004423 }
4424
4425 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304426}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304427#endif
4428
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304429#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304430static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304431 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4432 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4433 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304434};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304435#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304436
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304437static struct of_device_id msm_otg_dt_match[] = {
4438 { .compatible = "qcom,hsusb-otg",
4439 },
4440 {}
4441};
4442
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304443static struct platform_driver msm_otg_driver = {
4444 .remove = __devexit_p(msm_otg_remove),
4445 .driver = {
4446 .name = DRIVER_NAME,
4447 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304448#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304449 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304450#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304451 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304452 },
4453};
4454
4455static int __init msm_otg_init(void)
4456{
4457 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4458}
4459
4460static void __exit msm_otg_exit(void)
4461{
4462 platform_driver_unregister(&msm_otg_driver);
4463}
4464
4465module_init(msm_otg_init);
4466module_exit(msm_otg_exit);
4467
4468MODULE_LICENSE("GPL v2");
4469MODULE_DESCRIPTION("MSM USB transceiver driver");