blob: 44a7aee427792376071d10cb686c25220faacd1a [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;
1262 }
Jack Pham0c695282012-10-19 18:13:03 -07001263 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001264 return 0;
1265
David Keitel272ce522012-08-17 16:25:24 -07001266psy_error:
1267 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001268 return -ENXIO;
1269}
1270
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301271static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1272{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001273 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301274
1275 if (g && g->is_a_peripheral)
1276 return;
1277
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301278 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1279 motg->chg_type == USB_ACA_A_CHARGER ||
1280 motg->chg_type == USB_ACA_B_CHARGER ||
1281 motg->chg_type == USB_ACA_C_CHARGER) &&
1282 mA > IDEV_ACA_CHG_LIMIT)
1283 mA = IDEV_ACA_CHG_LIMIT;
1284
David Keitel081a3e22012-04-18 12:37:07 -07001285 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001286 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001287 "Failed notifying %d charger type to PMIC\n",
1288 motg->chg_type);
1289
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301290 if (motg->cur_power == mA)
1291 return;
1292
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001293 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001294
1295 /*
1296 * Use Power Supply API if supported, otherwise fallback
1297 * to legacy pm8921 API.
1298 */
1299 if (msm_otg_notify_power_supply(motg, mA))
1300 pm8921_charger_vbus_draw(mA);
1301
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301302 motg->cur_power = mA;
1303}
1304
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001305static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301306{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001307 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301308
1309 /*
1310 * Gadget driver uses set_power method to notify about the
1311 * available current based on suspend/configured states.
1312 *
1313 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1314 * states when CDP/ACA is connected.
1315 */
1316 if (motg->chg_type == USB_SDP_CHARGER)
1317 msm_otg_notify_charger(motg, mA);
1318
1319 return 0;
1320}
1321
Steve Mucklef132c6c2012-06-06 18:30:57 -07001322static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301323{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001324 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301325 struct msm_otg_platform_data *pdata = motg->pdata;
1326 struct usb_hcd *hcd;
1327
1328 if (!otg->host)
1329 return;
1330
1331 hcd = bus_to_hcd(otg->host);
1332
1333 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001334 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301335
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301336 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001337 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301338 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1339
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301340 /*
1341 * Some boards have a switch cotrolled by gpio
1342 * to enable/disable internal HUB. Enable internal
1343 * HUB before kicking the host.
1344 */
1345 if (pdata->setup_gpio)
1346 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301347 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301348 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001349 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301350
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301351 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301352 /* HCD core reset all bits of PORTSC. select ULPI phy */
1353 writel_relaxed(0x80000000, USB_PORTSC);
1354
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301355 if (pdata->setup_gpio)
1356 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301357
1358 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001359 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301360 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301361 }
1362}
1363
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001364static int msm_otg_usbdev_notify(struct notifier_block *self,
1365 unsigned long action, void *priv)
1366{
1367 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001368 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301369 struct usb_device *udev = priv;
1370
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301371 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1372 goto out;
1373
Steve Mucklef132c6c2012-06-06 18:30:57 -07001374 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301375 goto out;
1376 /*
1377 * Interested in devices connected directly to the root hub.
1378 * ACA dock can supply IDEV_CHG irrespective devices connected
1379 * on the accessory port.
1380 */
1381 if (!udev->parent || udev->parent->parent ||
1382 motg->chg_type == USB_ACA_DOCK_CHARGER)
1383 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001384
1385 switch (action) {
1386 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301387 if (aca_enabled())
1388 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001389 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301390 pr_debug("B_CONN set\n");
1391 set_bit(B_CONN, &motg->inputs);
1392 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001393 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301394 /*
1395 * OTG PET: A-device must end session within
1396 * 10 sec after PET enumeration.
1397 */
1398 if (udev->quirks & USB_QUIRK_OTG_PET)
1399 msm_otg_start_timer(motg, TA_TST_MAINT,
1400 A_TST_MAINT);
1401 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301402 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001403 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001404 if (udev->actconfig)
1405 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1406 else
1407 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001408 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301409 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301410 break;
1411 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001412 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1413 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301414 pr_debug("B_CONN clear\n");
1415 clear_bit(B_CONN, &motg->inputs);
1416 /*
1417 * OTG PET: A-device must end session after
1418 * PET disconnection if it is enumerated
1419 * with bcdDevice[0] = 1. USB core sets
1420 * bus->otg_vbus_off for us. clear it here.
1421 */
1422 if (udev->bus->otg_vbus_off) {
1423 udev->bus->otg_vbus_off = 0;
1424 set_bit(A_BUS_DROP, &motg->inputs);
1425 }
1426 queue_work(system_nrt_wq, &motg->sm_work);
1427 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001428 default:
1429 break;
1430 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301431 if (test_bit(ID_A, &motg->inputs))
1432 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1433 motg->mA_port);
1434out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435 return NOTIFY_OK;
1436}
1437
Mayank Ranae3926882011-12-26 09:47:54 +05301438static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1439{
1440 int ret;
1441 static bool vbus_is_on;
1442
1443 if (vbus_is_on == on)
1444 return;
1445
1446 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301447 ret = motg->pdata->vbus_power(on);
1448 if (!ret)
1449 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301450 return;
1451 }
1452
1453 if (!vbus_otg) {
1454 pr_err("vbus_otg is NULL.");
1455 return;
1456 }
1457
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001458 /*
1459 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001460 * from usb before turning on the boost.
1461 * if exiting host mode disable the boost before enabling to draw
1462 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001463 */
Mayank Ranae3926882011-12-26 09:47:54 +05301464 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001465 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301466 ret = regulator_enable(vbus_otg);
1467 if (ret) {
1468 pr_err("unable to enable vbus_otg\n");
1469 return;
1470 }
1471 vbus_is_on = true;
1472 } else {
1473 ret = regulator_disable(vbus_otg);
1474 if (ret) {
1475 pr_err("unable to disable vbus_otg\n");
1476 return;
1477 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001478 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301479 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301480 }
1481}
1482
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001483static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301484{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001485 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301486 struct usb_hcd *hcd;
1487
1488 /*
1489 * Fail host registration if this board can support
1490 * only peripheral configuration.
1491 */
1492 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001493 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301494 return -ENODEV;
1495 }
1496
Mayank Ranae3926882011-12-26 09:47:54 +05301497 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001498 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301499 if (IS_ERR(vbus_otg)) {
1500 pr_err("Unable to get vbus_otg\n");
1501 return -ENODEV;
1502 }
1503 }
1504
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301505 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001506 if (otg->phy->state == OTG_STATE_A_HOST) {
1507 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001508 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301509 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301510 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301511 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001512 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301513 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301514 } else {
1515 otg->host = NULL;
1516 }
1517
1518 return 0;
1519 }
1520
1521 hcd = bus_to_hcd(host);
1522 hcd->power_budget = motg->pdata->power_budget;
1523
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301524#ifdef CONFIG_USB_OTG
1525 host->otg_port = 1;
1526#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001527 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1528 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301529 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001530 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301531
1532 /*
1533 * Kick the state machine work, if peripheral is not supported
1534 * or peripheral is already registered with us.
1535 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301536 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001537 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301538 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301539 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301540
1541 return 0;
1542}
1543
Steve Mucklef132c6c2012-06-06 18:30:57 -07001544static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301545{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301546 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001547 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301548 struct msm_otg_platform_data *pdata = motg->pdata;
1549
1550 if (!otg->gadget)
1551 return;
1552
1553 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001554 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301555 /*
1556 * Some boards have a switch cotrolled by gpio
1557 * to enable/disable internal HUB. Disable internal
1558 * HUB before kicking the gadget.
1559 */
1560 if (pdata->setup_gpio)
1561 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001562
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301563 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301564 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301565 ret = msm_bus_scale_client_update_request(
1566 motg->bus_perf_client, 1);
1567 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001568 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301569 "bus bandwidth %d\n", __func__, ret);
1570 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301571 usb_gadget_vbus_connect(otg->gadget);
1572 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001573 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301574 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301575 /* Configure BUS performance parameters to default */
1576 if (motg->bus_perf_client) {
1577 ret = msm_bus_scale_client_update_request(
1578 motg->bus_perf_client, 0);
1579 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001580 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301581 "for bus bw %d\n", __func__, ret);
1582 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301583 if (pdata->setup_gpio)
1584 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1585 }
1586
1587}
1588
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001589static int msm_otg_set_peripheral(struct usb_otg *otg,
Stephen Boyd9850acb2013-01-28 14:11:20 -08001590 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301591{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001592 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301593
1594 /*
1595 * Fail peripheral registration if this board can support
1596 * only host configuration.
1597 */
1598 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001599 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301600 return -ENODEV;
1601 }
1602
1603 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001604 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1605 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301606 msm_otg_start_peripheral(otg, 0);
1607 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001608 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301609 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301610 } else {
1611 otg->gadget = NULL;
1612 }
1613
1614 return 0;
1615 }
1616 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001617 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301618
1619 /*
1620 * Kick the state machine work, if host is not supported
1621 * or host is already registered with us.
1622 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301623 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001624 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301625 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301626 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301627
1628 return 0;
1629}
1630
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301631static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1632{
1633 unsigned long flags;
1634 int id;
1635
1636 if (!motg->pdata->pmic_id_irq)
1637 return -ENODEV;
1638
1639 local_irq_save(flags);
1640 id = irq_read_line(motg->pdata->pmic_id_irq);
1641 local_irq_restore(flags);
1642
1643 /*
1644 * If we can not read ID line state for some reason, treat
1645 * it as float. This would prevent MHL discovery and kicking
1646 * host mode unnecessarily.
1647 */
1648 return !!id;
1649}
1650
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301651static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1652 void (*callback)(int on))
1653{
1654 struct usb_phy *phy = &motg->phy;
1655 int ret;
1656
Manoj Raoa7bddd12012-08-27 20:36:45 -07001657 if (!motg->pdata->mhl_enable) {
1658 dev_dbg(phy->dev, "MHL feature not enabled\n");
1659 return -ENODEV;
1660 }
1661
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301662 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1663 !motg->pdata->pmic_id_irq) {
1664 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1665 return -ENODEV;
1666 }
1667
1668 if (!motg->pdata->mhl_dev_name) {
1669 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1670 return -ENODEV;
1671 }
1672
1673 if (callback)
1674 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1675 callback);
1676 else
1677 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1678
1679 if (ret)
1680 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1681 motg->pdata->mhl_dev_name, ret);
1682 else
1683 motg->mhl_enabled = true;
1684
1685 return ret;
1686}
1687
1688static void msm_otg_mhl_notify_online(int on)
1689{
1690 struct msm_otg *motg = the_msm_otg;
1691 struct usb_phy *phy = &motg->phy;
1692 bool queue = false;
1693
1694 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1695
1696 if (on) {
1697 set_bit(MHL, &motg->inputs);
1698 } else {
1699 clear_bit(MHL, &motg->inputs);
1700 queue = true;
1701 }
1702
1703 if (queue && phy->state != OTG_STATE_UNDEFINED)
1704 schedule_work(&motg->sm_work);
1705}
1706
1707static bool msm_otg_is_mhl(struct msm_otg *motg)
1708{
1709 struct usb_phy *phy = &motg->phy;
1710 int is_mhl, ret;
1711
1712 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1713 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1714 /*
1715 * MHL driver calls our callback saying that MHL connected
1716 * if RID_GND is detected. But at later part of discovery
1717 * it may figure out MHL is not connected and returns
1718 * false. Hence clear MHL input here.
1719 */
1720 clear_bit(MHL, &motg->inputs);
1721 dev_dbg(phy->dev, "MHL device not found\n");
1722 return false;
1723 }
1724
1725 set_bit(MHL, &motg->inputs);
1726 dev_dbg(phy->dev, "MHL device found\n");
1727 return true;
1728}
1729
1730static bool msm_chg_mhl_detect(struct msm_otg *motg)
1731{
1732 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301733
1734 if (!motg->mhl_enabled)
1735 return false;
1736
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301737 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301738
1739 if (id)
1740 return false;
1741
1742 mhl_det_in_progress = true;
1743 ret = msm_otg_is_mhl(motg);
1744 mhl_det_in_progress = false;
1745
1746 return ret;
1747}
1748
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301749static void msm_otg_chg_check_timer_func(unsigned long data)
1750{
1751 struct msm_otg *motg = (struct msm_otg *) data;
1752 struct usb_otg *otg = motg->phy.otg;
1753
1754 if (atomic_read(&motg->in_lpm) ||
1755 !test_bit(B_SESS_VLD, &motg->inputs) ||
1756 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1757 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1758 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1759 return;
1760 }
1761
1762 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1763 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1764 set_bit(B_FALSE_SDP, &motg->inputs);
1765 queue_work(system_nrt_wq, &motg->sm_work);
1766 }
1767}
1768
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769static bool msm_chg_aca_detect(struct msm_otg *motg)
1770{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001771 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001772 u32 int_sts;
1773 bool ret = false;
1774
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301775 if (!aca_enabled())
1776 goto out;
1777
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001778 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1779 goto out;
1780
Steve Mucklef132c6c2012-06-06 18:30:57 -07001781 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782 switch (int_sts & 0x1C) {
1783 case 0x08:
1784 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001785 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786 motg->chg_type = USB_ACA_A_CHARGER;
1787 motg->chg_state = USB_CHG_STATE_DETECTED;
1788 clear_bit(ID_B, &motg->inputs);
1789 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301790 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001791 ret = true;
1792 }
1793 break;
1794 case 0x0C:
1795 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001796 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797 motg->chg_type = USB_ACA_B_CHARGER;
1798 motg->chg_state = USB_CHG_STATE_DETECTED;
1799 clear_bit(ID_A, &motg->inputs);
1800 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301801 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001802 ret = true;
1803 }
1804 break;
1805 case 0x10:
1806 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001807 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001808 motg->chg_type = USB_ACA_C_CHARGER;
1809 motg->chg_state = USB_CHG_STATE_DETECTED;
1810 clear_bit(ID_A, &motg->inputs);
1811 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301812 set_bit(ID, &motg->inputs);
1813 ret = true;
1814 }
1815 break;
1816 case 0x04:
1817 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001818 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301819 motg->chg_type = USB_INVALID_CHARGER;
1820 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1821 clear_bit(ID_A, &motg->inputs);
1822 clear_bit(ID_B, &motg->inputs);
1823 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001824 ret = true;
1825 }
1826 break;
1827 default:
1828 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1829 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301830 test_and_clear_bit(ID_C, &motg->inputs) |
1831 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001832 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001833 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001834 motg->chg_type = USB_INVALID_CHARGER;
1835 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1836 }
1837 }
1838out:
1839 return ret;
1840}
1841
1842static void msm_chg_enable_aca_det(struct msm_otg *motg)
1843{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001844 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001845
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301846 if (!aca_enabled())
1847 return;
1848
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 switch (motg->pdata->phy_type) {
1850 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301851 /* Disable ID_GND in link and PHY */
1852 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1853 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001854 ulpi_write(phy, 0x01, 0x0C);
1855 ulpi_write(phy, 0x10, 0x0F);
1856 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301857 /* Disable PMIC ID pull-up */
1858 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301859 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001860 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301861 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001862 break;
1863 default:
1864 break;
1865 }
1866}
1867
1868static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1869{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001870 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301872 if (!aca_enabled())
1873 return;
1874
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001875 switch (motg->pdata->phy_type) {
1876 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301877 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001878 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301879 break;
1880 default:
1881 break;
1882 }
1883}
1884
1885static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1886{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001887 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301888
1889 if (!aca_enabled())
1890 return;
1891
1892 switch (motg->pdata->phy_type) {
1893 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001894 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895 break;
1896 default:
1897 break;
1898 }
1899}
1900
1901static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1902{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001903 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001904 bool ret = false;
1905
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301906 if (!aca_enabled())
1907 return ret;
1908
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001909 switch (motg->pdata->phy_type) {
1910 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001911 if (ulpi_read(phy, 0x91) & 1) {
1912 dev_dbg(phy->dev, "RID change\n");
1913 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001914 ret = msm_chg_aca_detect(motg);
1915 }
1916 default:
1917 break;
1918 }
1919 return ret;
1920}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301921
1922static void msm_otg_id_timer_func(unsigned long data)
1923{
1924 struct msm_otg *motg = (struct msm_otg *) data;
1925
1926 if (!aca_enabled())
1927 return;
1928
1929 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001930 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301931 return;
1932 }
1933
Steve Mucklef132c6c2012-06-06 18:30:57 -07001934 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301935 goto out;
1936
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301937 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001938 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301939 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301940 }
1941
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301942out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301943 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1944 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1945}
1946
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301947static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1948{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001949 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301950 u32 chg_det;
1951 bool ret = false;
1952
1953 switch (motg->pdata->phy_type) {
1954 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001955 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301956 ret = chg_det & (1 << 4);
1957 break;
1958 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001959 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301960 ret = chg_det & 1;
1961 break;
1962 default:
1963 break;
1964 }
1965 return ret;
1966}
1967
1968static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1969{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001970 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301971 u32 chg_det;
1972
1973 switch (motg->pdata->phy_type) {
1974 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001975 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301976 /* Turn off charger block */
1977 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001978 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301979 udelay(20);
1980 /* control chg block via ULPI */
1981 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001982 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301983 /* put it in host mode for enabling D- source */
1984 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001985 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301986 /* Turn on chg detect block */
1987 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001988 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301989 udelay(20);
1990 /* enable chg detection */
1991 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001992 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301993 break;
1994 case SNPS_28NM_INTEGRATED_PHY:
1995 /*
1996 * Configure DM as current source, DP as current sink
1997 * and enable battery charging comparators.
1998 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001999 ulpi_write(phy, 0x8, 0x85);
2000 ulpi_write(phy, 0x2, 0x85);
2001 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302002 break;
2003 default:
2004 break;
2005 }
2006}
2007
2008static bool msm_chg_check_primary_det(struct msm_otg *motg)
2009{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002010 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302011 u32 chg_det;
2012 bool ret = false;
2013
2014 switch (motg->pdata->phy_type) {
2015 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002016 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302017 ret = chg_det & (1 << 4);
2018 break;
2019 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002020 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302021 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302022 /* Turn off VDP_SRC */
2023 ulpi_write(phy, 0x3, 0x86);
2024 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302025 break;
2026 default:
2027 break;
2028 }
2029 return ret;
2030}
2031
2032static void msm_chg_enable_primary_det(struct msm_otg *motg)
2033{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002034 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302035 u32 chg_det;
2036
2037 switch (motg->pdata->phy_type) {
2038 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002039 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302040 /* enable chg detection */
2041 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002042 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302043 break;
2044 case SNPS_28NM_INTEGRATED_PHY:
2045 /*
2046 * Configure DP as current source, DM as current sink
2047 * and enable battery charging comparators.
2048 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002049 ulpi_write(phy, 0x2, 0x85);
2050 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302051 break;
2052 default:
2053 break;
2054 }
2055}
2056
2057static bool msm_chg_check_dcd(struct msm_otg *motg)
2058{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002059 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302060 u32 line_state;
2061 bool ret = false;
2062
2063 switch (motg->pdata->phy_type) {
2064 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002065 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066 ret = !(line_state & 1);
2067 break;
2068 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002069 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302070 ret = line_state & 2;
2071 break;
2072 default:
2073 break;
2074 }
2075 return ret;
2076}
2077
2078static void msm_chg_disable_dcd(struct msm_otg *motg)
2079{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002080 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302081 u32 chg_det;
2082
2083 switch (motg->pdata->phy_type) {
2084 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002085 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302086 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002087 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302088 break;
2089 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002090 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302091 break;
2092 default:
2093 break;
2094 }
2095}
2096
2097static void msm_chg_enable_dcd(struct msm_otg *motg)
2098{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002099 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302100 u32 chg_det;
2101
2102 switch (motg->pdata->phy_type) {
2103 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002104 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302105 /* Turn on D+ current source */
2106 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002107 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302108 break;
2109 case SNPS_28NM_INTEGRATED_PHY:
2110 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002111 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302112 break;
2113 default:
2114 break;
2115 }
2116}
2117
2118static void msm_chg_block_on(struct msm_otg *motg)
2119{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002120 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302121 u32 func_ctrl, chg_det;
2122
2123 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002124 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302125 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2126 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002127 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302128
2129 switch (motg->pdata->phy_type) {
2130 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002131 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302132 /* control chg block via ULPI */
2133 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002134 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302135 /* Turn on chg detect block */
2136 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002137 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302138 udelay(20);
2139 break;
2140 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302141 /* disable DP and DM pull down resistors */
2142 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302143 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002144 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302145 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002146 ulpi_write(phy, 0x1F, 0x92);
2147 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302148 udelay(100);
2149 break;
2150 default:
2151 break;
2152 }
2153}
2154
2155static void msm_chg_block_off(struct msm_otg *motg)
2156{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002157 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302158 u32 func_ctrl, chg_det;
2159
2160 switch (motg->pdata->phy_type) {
2161 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002162 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302163 /* Turn off charger block */
2164 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002165 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302166 break;
2167 case SNPS_28NM_INTEGRATED_PHY:
2168 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002169 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302170 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002171 ulpi_write(phy, 0x1F, 0x92);
2172 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302173 break;
2174 default:
2175 break;
2176 }
2177
2178 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002179 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302180 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2181 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002182 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302183}
2184
Anji jonnalad270e2d2011-08-09 11:28:32 +05302185static const char *chg_to_string(enum usb_chg_type chg_type)
2186{
2187 switch (chg_type) {
2188 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2189 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2190 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2191 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2192 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2193 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2194 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302195 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302196 default: return "INVALID_CHARGER";
2197 }
2198}
2199
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302200#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2201#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302202#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2203#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302204static void msm_chg_detect_work(struct work_struct *w)
2205{
2206 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002207 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302208 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302209 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302210 unsigned long delay;
2211
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002212 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302213
2214 if (test_bit(MHL, &motg->inputs)) {
2215 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2216 return;
2217 }
2218
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302219 switch (motg->chg_state) {
2220 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302221 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302222 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002223 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302224 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302225 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302226 delay = MSM_CHG_DCD_POLL_TIME;
2227 break;
2228 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302229 if (msm_chg_mhl_detect(motg)) {
2230 msm_chg_block_off(motg);
2231 motg->chg_state = USB_CHG_STATE_DETECTED;
2232 motg->chg_type = USB_INVALID_CHARGER;
2233 queue_work(system_nrt_wq, &motg->sm_work);
2234 return;
2235 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002236 is_aca = msm_chg_aca_detect(motg);
2237 if (is_aca) {
2238 /*
2239 * ID_A can be ACA dock too. continue
2240 * primary detection after DCD.
2241 */
2242 if (test_bit(ID_A, &motg->inputs)) {
2243 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2244 } else {
2245 delay = 0;
2246 break;
2247 }
2248 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302249 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302250 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2251 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302252 if (is_dcd || tmout) {
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302253 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302254 msm_chg_enable_primary_det(motg);
2255 delay = MSM_CHG_PRIMARY_DET_TIME;
2256 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2257 } else {
2258 delay = MSM_CHG_DCD_POLL_TIME;
2259 }
2260 break;
2261 case USB_CHG_STATE_DCD_DONE:
2262 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302263 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2264 dm_vlgc = line_state & PORTSC_LS_DM;
2265 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302266 if (test_bit(ID_A, &motg->inputs)) {
2267 motg->chg_type = USB_ACA_DOCK_CHARGER;
2268 motg->chg_state = USB_CHG_STATE_DETECTED;
2269 delay = 0;
2270 break;
2271 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302272 if (line_state) { /* DP > VLGC */
2273 motg->chg_type = USB_PROPRIETARY_CHARGER;
2274 motg->chg_state = USB_CHG_STATE_DETECTED;
2275 delay = 0;
2276 } else {
2277 msm_chg_enable_secondary_det(motg);
2278 delay = MSM_CHG_SECONDARY_DET_TIME;
2279 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2280 }
2281 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302282 if (test_bit(ID_A, &motg->inputs)) {
2283 motg->chg_type = USB_ACA_A_CHARGER;
2284 motg->chg_state = USB_CHG_STATE_DETECTED;
2285 delay = 0;
2286 break;
2287 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302288
2289 if (line_state) /* DP > VLGC or/and DM > VLGC */
2290 motg->chg_type = USB_PROPRIETARY_CHARGER;
2291 else
2292 motg->chg_type = USB_SDP_CHARGER;
2293
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302294 motg->chg_state = USB_CHG_STATE_DETECTED;
2295 delay = 0;
2296 }
2297 break;
2298 case USB_CHG_STATE_PRIMARY_DONE:
2299 vout = msm_chg_check_secondary_det(motg);
2300 if (vout)
2301 motg->chg_type = USB_DCP_CHARGER;
2302 else
2303 motg->chg_type = USB_CDP_CHARGER;
2304 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2305 /* fall through */
2306 case USB_CHG_STATE_SECONDARY_DONE:
2307 motg->chg_state = USB_CHG_STATE_DETECTED;
2308 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302309 /*
2310 * Notify the charger type to power supply
2311 * owner as soon as we determine the charger.
2312 */
2313 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302314 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002315 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302316 /*
2317 * Spurious interrupt is seen after enabling ACA detection
2318 * due to which charger detection fails in case of PET.
2319 * Add delay of 100 microsec to avoid that.
2320 */
2321 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002322 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002323 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302324 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302325 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302326 return;
2327 default:
2328 return;
2329 }
2330
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302331 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302332}
2333
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302334/*
2335 * We support OTG, Peripheral only and Host only configurations. In case
2336 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2337 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2338 * enabled when switch is controlled by user and default mode is supplied
2339 * by board file, which can be changed by userspace later.
2340 */
2341static void msm_otg_init_sm(struct msm_otg *motg)
2342{
2343 struct msm_otg_platform_data *pdata = motg->pdata;
2344 u32 otgsc = readl(USB_OTGSC);
2345
2346 switch (pdata->mode) {
2347 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002348 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302349 if (pdata->default_mode == USB_HOST) {
2350 clear_bit(ID, &motg->inputs);
2351 } else if (pdata->default_mode == USB_PERIPHERAL) {
2352 set_bit(ID, &motg->inputs);
2353 set_bit(B_SESS_VLD, &motg->inputs);
2354 } else {
2355 set_bit(ID, &motg->inputs);
2356 clear_bit(B_SESS_VLD, &motg->inputs);
2357 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302358 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302359 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302360 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302361 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302362 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302363 set_bit(A_BUS_REQ, &motg->inputs);
2364 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002365 if (otgsc & OTGSC_BSV)
2366 set_bit(B_SESS_VLD, &motg->inputs);
2367 else
2368 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302369 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302370 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302371 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302372 set_bit(ID, &motg->inputs);
2373 else
2374 clear_bit(ID, &motg->inputs);
2375 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302376 /*
2377 * VBUS initial state is reported after PMIC
2378 * driver initialization. Wait for it.
2379 */
2380 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302381 }
2382 break;
2383 case USB_HOST:
2384 clear_bit(ID, &motg->inputs);
2385 break;
2386 case USB_PERIPHERAL:
2387 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302388 if (pdata->otg_control == OTG_PHY_CONTROL) {
2389 if (otgsc & OTGSC_BSV)
2390 set_bit(B_SESS_VLD, &motg->inputs);
2391 else
2392 clear_bit(B_SESS_VLD, &motg->inputs);
2393 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2394 /*
2395 * VBUS initial state is reported after PMIC
2396 * driver initialization. Wait for it.
2397 */
2398 wait_for_completion(&pmic_vbus_init);
2399 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302400 break;
2401 default:
2402 break;
2403 }
2404}
2405
2406static void msm_otg_sm_work(struct work_struct *w)
2407{
2408 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002409 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302410 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302411
Steve Mucklef132c6c2012-06-06 18:30:57 -07002412 pm_runtime_resume(otg->phy->dev);
2413 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002414 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302415 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002416 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302417 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002418 if (!psy && legacy_power_supply) {
2419 psy = power_supply_get_by_name("usb");
2420
2421 if (!psy)
2422 pr_err("couldn't get usb power supply\n");
2423 }
2424
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002425 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302426 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2427 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002428 pm_runtime_put_noidle(otg->phy->dev);
2429 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302430 break;
2431 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302432 /* FALL THROUGH */
2433 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302434 if (test_bit(MHL, &motg->inputs)) {
2435 /* allow LPM */
2436 pm_runtime_put_noidle(otg->phy->dev);
2437 pm_runtime_suspend(otg->phy->dev);
2438 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002439 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302440 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302441 if (msm_chg_mhl_detect(motg)) {
2442 work = 1;
2443 break;
2444 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302445 clear_bit(B_BUS_REQ, &motg->inputs);
2446 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002447 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302448 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302449 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302450 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302451 switch (motg->chg_state) {
2452 case USB_CHG_STATE_UNDEFINED:
2453 msm_chg_detect_work(&motg->chg_work.work);
2454 break;
2455 case USB_CHG_STATE_DETECTED:
2456 switch (motg->chg_type) {
2457 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302458 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002459 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302460 /* fall through */
2461 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302462 msm_otg_notify_charger(motg,
2463 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002464 pm_runtime_put_noidle(otg->phy->dev);
2465 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302466 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302467 case USB_ACA_B_CHARGER:
2468 msm_otg_notify_charger(motg,
2469 IDEV_ACA_CHG_MAX);
2470 /*
2471 * (ID_B --> ID_C) PHY_ALT interrupt can
2472 * not be detected in LPM.
2473 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302474 break;
2475 case USB_CDP_CHARGER:
2476 msm_otg_notify_charger(motg,
2477 IDEV_CHG_MAX);
2478 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002479 otg->phy->state =
2480 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302481 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302482 case USB_ACA_C_CHARGER:
2483 msm_otg_notify_charger(motg,
2484 IDEV_ACA_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;
2489 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302490 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002491 otg->phy->state =
2492 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302493 mod_timer(&motg->chg_check_timer,
2494 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302495 break;
2496 default:
2497 break;
2498 }
2499 break;
2500 default:
2501 break;
2502 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302503 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2504 pr_debug("b_sess_end && b_bus_req\n");
2505 if (msm_otg_start_srp(otg) < 0) {
2506 clear_bit(B_BUS_REQ, &motg->inputs);
2507 work = 1;
2508 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302509 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002510 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302511 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2512 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302513 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302514 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302515 del_timer_sync(&motg->chg_check_timer);
2516 clear_bit(B_FALSE_SDP, &motg->inputs);
Mayank Rana8ab00352013-01-23 19:26:21 +05302517 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302518 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302519 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2520 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302521 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002522 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302523 /*
2524 * There is a small window where ID interrupt
2525 * is not monitored during ID detection circuit
2526 * switch from ACA to PMIC. Check ID state
2527 * before entering into low power mode.
2528 */
2529 if (!msm_otg_read_pmic_id_state(motg)) {
2530 pr_debug("process missed ID intr\n");
2531 clear_bit(ID, &motg->inputs);
2532 work = 1;
2533 break;
2534 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002535 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002536 /*
2537 * Only if autosuspend was enabled in probe, it will be
2538 * used here. Otherwise, no delay will be used.
2539 */
2540 pm_runtime_mark_last_busy(otg->phy->dev);
2541 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302542 }
2543 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302544 case OTG_STATE_B_SRP_INIT:
2545 if (!test_bit(ID, &motg->inputs) ||
2546 test_bit(ID_A, &motg->inputs) ||
2547 test_bit(ID_C, &motg->inputs) ||
2548 (test_bit(B_SESS_VLD, &motg->inputs) &&
2549 !test_bit(ID_B, &motg->inputs))) {
2550 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2551 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002552 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302553 /*
2554 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2555 * bits after SRP initiation.
2556 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002557 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302558 work = 1;
2559 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2560 pr_debug("b_srp_fail\n");
2561 pr_info("A-device did not respond to SRP\n");
2562 clear_bit(B_BUS_REQ, &motg->inputs);
2563 clear_bit(B_SRP_FAIL, &motg->tmouts);
2564 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002565 ulpi_write(otg->phy, 0x0, 0x98);
2566 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302567 motg->b_last_se0_sess = jiffies;
2568 work = 1;
2569 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302570 break;
2571 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302572 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2573 test_bit(B_FALSE_SDP, &motg->inputs)) {
2574 pr_debug("B_FALSE_SDP\n");
2575 msm_otg_start_peripheral(otg, 0);
2576 motg->chg_type = USB_DCP_CHARGER;
2577 clear_bit(B_FALSE_SDP, &motg->inputs);
2578 otg->phy->state = OTG_STATE_B_IDLE;
2579 work = 1;
2580 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302581 test_bit(ID_A, &motg->inputs) ||
2582 test_bit(ID_B, &motg->inputs) ||
2583 !test_bit(B_SESS_VLD, &motg->inputs)) {
2584 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302585 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2586 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302587 msm_otg_notify_charger(motg, 0);
2588 srp_reqd = otg->gadget->otg_srp_reqd;
2589 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302590 if (test_bit(ID_B, &motg->inputs))
2591 clear_bit(ID_B, &motg->inputs);
2592 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002593 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302594 motg->b_last_se0_sess = jiffies;
2595 if (srp_reqd)
2596 msm_otg_start_timer(motg,
2597 TB_TST_SRP, B_TST_SRP);
2598 else
2599 work = 1;
2600 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2601 otg->gadget->b_hnp_enable &&
2602 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2603 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2604 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2605 /* D+ pullup should not be disconnected within 4msec
2606 * after A device suspends the bus. Otherwise PET will
2607 * fail the compliance test.
2608 */
2609 udelay(1000);
2610 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002611 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302612 /*
2613 * start HCD even before A-device enable
2614 * pull-up to meet HNP timings.
2615 */
2616 otg->host->is_b_host = 1;
2617 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002618 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2619 test_bit(B_SESS_VLD, &motg->inputs)) {
2620 pr_debug("a_bus_suspend && b_sess_vld\n");
2621 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002622 pm_runtime_put_noidle(otg->phy->dev);
2623 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002624 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002625 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302626 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002627 }
2628 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302629 case OTG_STATE_B_WAIT_ACON:
2630 if (!test_bit(ID, &motg->inputs) ||
2631 test_bit(ID_A, &motg->inputs) ||
2632 test_bit(ID_B, &motg->inputs) ||
2633 !test_bit(B_SESS_VLD, &motg->inputs)) {
2634 pr_debug("!id || id_a/b || !b_sess_vld\n");
2635 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302636 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302637 * A-device is physically disconnected during
2638 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302639 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302640 msm_otg_start_host(otg, 0);
2641 otg->host->is_b_host = 0;
2642
2643 clear_bit(B_BUS_REQ, &motg->inputs);
2644 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2645 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002646 otg->phy->state = OTG_STATE_B_IDLE;
2647 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302648 work = 1;
2649 } else if (test_bit(A_CONN, &motg->inputs)) {
2650 pr_debug("a_conn\n");
2651 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002652 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302653 /*
2654 * PET disconnects D+ pullup after reset is generated
2655 * by B device in B_HOST role which is not detected by
2656 * B device. As workaorund , start timer of 300msec
2657 * and stop timer if A device is enumerated else clear
2658 * A_CONN.
2659 */
2660 msm_otg_start_timer(motg, TB_TST_CONFIG,
2661 B_TST_CONFIG);
2662 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2663 pr_debug("b_ase0_brst_tmout\n");
2664 pr_info("B HNP fail:No response from A device\n");
2665 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002666 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302667 otg->host->is_b_host = 0;
2668 clear_bit(B_ASE0_BRST, &motg->tmouts);
2669 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2670 clear_bit(B_BUS_REQ, &motg->inputs);
2671 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002672 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302673 work = 1;
2674 } else if (test_bit(ID_C, &motg->inputs)) {
2675 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2676 }
2677 break;
2678 case OTG_STATE_B_HOST:
2679 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2680 !test_bit(A_CONN, &motg->inputs) ||
2681 !test_bit(B_SESS_VLD, &motg->inputs)) {
2682 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2683 clear_bit(A_CONN, &motg->inputs);
2684 clear_bit(B_BUS_REQ, &motg->inputs);
2685 msm_otg_start_host(otg, 0);
2686 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002687 otg->phy->state = OTG_STATE_B_IDLE;
2688 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302689 work = 1;
2690 } else if (test_bit(ID_C, &motg->inputs)) {
2691 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2692 }
2693 break;
2694 case OTG_STATE_A_IDLE:
2695 otg->default_a = 1;
2696 if (test_bit(ID, &motg->inputs) &&
2697 !test_bit(ID_A, &motg->inputs)) {
2698 pr_debug("id && !id_a\n");
2699 otg->default_a = 0;
2700 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002701 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302702 del_timer_sync(&motg->id_timer);
2703 msm_otg_link_reset(motg);
2704 msm_chg_enable_aca_intr(motg);
2705 msm_otg_notify_charger(motg, 0);
2706 work = 1;
2707 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2708 (test_bit(A_SRP_DET, &motg->inputs) ||
2709 test_bit(A_BUS_REQ, &motg->inputs))) {
2710 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2711
2712 clear_bit(A_SRP_DET, &motg->inputs);
2713 /* Disable SRP detection */
2714 writel_relaxed((readl_relaxed(USB_OTGSC) &
2715 ~OTGSC_INTSTS_MASK) &
2716 ~OTGSC_DPIE, USB_OTGSC);
2717
Steve Mucklef132c6c2012-06-06 18:30:57 -07002718 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302719 /* VBUS should not be supplied before end of SRP pulse
2720 * generated by PET, if not complaince test fail.
2721 */
2722 usleep_range(10000, 12000);
2723 /* ACA: ID_A: Stop charging untill enumeration */
2724 if (test_bit(ID_A, &motg->inputs))
2725 msm_otg_notify_charger(motg, 0);
2726 else
2727 msm_hsusb_vbus_power(motg, 1);
2728 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2729 } else {
2730 pr_debug("No session requested\n");
2731 clear_bit(A_BUS_DROP, &motg->inputs);
2732 if (test_bit(ID_A, &motg->inputs)) {
2733 msm_otg_notify_charger(motg,
2734 IDEV_ACA_CHG_MAX);
2735 } else if (!test_bit(ID, &motg->inputs)) {
2736 msm_otg_notify_charger(motg, 0);
2737 /*
2738 * A-device is not providing power on VBUS.
2739 * Enable SRP detection.
2740 */
2741 writel_relaxed(0x13, USB_USBMODE);
2742 writel_relaxed((readl_relaxed(USB_OTGSC) &
2743 ~OTGSC_INTSTS_MASK) |
2744 OTGSC_DPIE, USB_OTGSC);
2745 mb();
2746 }
2747 }
2748 break;
2749 case OTG_STATE_A_WAIT_VRISE:
2750 if ((test_bit(ID, &motg->inputs) &&
2751 !test_bit(ID_A, &motg->inputs)) ||
2752 test_bit(A_BUS_DROP, &motg->inputs) ||
2753 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2754 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2755 clear_bit(A_BUS_REQ, &motg->inputs);
2756 msm_otg_del_timer(motg);
2757 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002758 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302759 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2760 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2761 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002762 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302763 if (TA_WAIT_BCON > 0)
2764 msm_otg_start_timer(motg, TA_WAIT_BCON,
2765 A_WAIT_BCON);
2766 msm_otg_start_host(otg, 1);
2767 msm_chg_enable_aca_det(motg);
2768 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302769 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302770 if (msm_chg_check_aca_intr(motg))
2771 work = 1;
2772 }
2773 break;
2774 case OTG_STATE_A_WAIT_BCON:
2775 if ((test_bit(ID, &motg->inputs) &&
2776 !test_bit(ID_A, &motg->inputs)) ||
2777 test_bit(A_BUS_DROP, &motg->inputs) ||
2778 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2779 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2780 "a_wait_bcon_tmout\n");
2781 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2782 pr_info("Device No Response\n");
2783 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2784 }
2785 msm_otg_del_timer(motg);
2786 clear_bit(A_BUS_REQ, &motg->inputs);
2787 clear_bit(B_CONN, &motg->inputs);
2788 msm_otg_start_host(otg, 0);
2789 /*
2790 * ACA: ID_A with NO accessory, just the A plug is
2791 * attached to ACA: Use IDCHG_MAX for charging
2792 */
2793 if (test_bit(ID_A, &motg->inputs))
2794 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2795 else
2796 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002797 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302798 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2799 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2800 pr_debug("!a_vbus_vld\n");
2801 clear_bit(B_CONN, &motg->inputs);
2802 msm_otg_del_timer(motg);
2803 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002804 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2805 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302806 } else if (test_bit(ID_A, &motg->inputs)) {
2807 msm_hsusb_vbus_power(motg, 0);
2808 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2809 /*
2810 * If TA_WAIT_BCON is infinite, we don;t
2811 * turn off VBUS. Enter low power mode.
2812 */
2813 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002814 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302815 } else if (!test_bit(ID, &motg->inputs)) {
2816 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302817 }
2818 break;
2819 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302820 if ((test_bit(ID, &motg->inputs) &&
2821 !test_bit(ID_A, &motg->inputs)) ||
2822 test_bit(A_BUS_DROP, &motg->inputs)) {
2823 pr_debug("id_a/b/c || a_bus_drop\n");
2824 clear_bit(B_CONN, &motg->inputs);
2825 clear_bit(A_BUS_REQ, &motg->inputs);
2826 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002827 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302828 msm_otg_start_host(otg, 0);
2829 if (!test_bit(ID_A, &motg->inputs))
2830 msm_hsusb_vbus_power(motg, 0);
2831 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2832 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2833 pr_debug("!a_vbus_vld\n");
2834 clear_bit(B_CONN, &motg->inputs);
2835 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002836 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302837 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002838 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302839 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2840 /*
2841 * a_bus_req is de-asserted when root hub is
2842 * suspended or HNP is in progress.
2843 */
2844 pr_debug("!a_bus_req\n");
2845 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002846 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302847 if (otg->host->b_hnp_enable)
2848 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2849 A_AIDL_BDIS);
2850 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002851 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302852 } else if (!test_bit(B_CONN, &motg->inputs)) {
2853 pr_debug("!b_conn\n");
2854 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002855 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302856 if (TA_WAIT_BCON > 0)
2857 msm_otg_start_timer(motg, TA_WAIT_BCON,
2858 A_WAIT_BCON);
2859 if (msm_chg_check_aca_intr(motg))
2860 work = 1;
2861 } else if (test_bit(ID_A, &motg->inputs)) {
2862 msm_otg_del_timer(motg);
2863 msm_hsusb_vbus_power(motg, 0);
2864 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2865 msm_otg_notify_charger(motg,
2866 IDEV_ACA_CHG_MAX);
2867 else
2868 msm_otg_notify_charger(motg,
2869 IDEV_CHG_MIN - motg->mA_port);
2870 } else if (!test_bit(ID, &motg->inputs)) {
2871 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2872 motg->chg_type = USB_INVALID_CHARGER;
2873 msm_otg_notify_charger(motg, 0);
2874 msm_hsusb_vbus_power(motg, 1);
2875 }
2876 break;
2877 case OTG_STATE_A_SUSPEND:
2878 if ((test_bit(ID, &motg->inputs) &&
2879 !test_bit(ID_A, &motg->inputs)) ||
2880 test_bit(A_BUS_DROP, &motg->inputs) ||
2881 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2882 pr_debug("id_a/b/c || a_bus_drop ||"
2883 "a_aidl_bdis_tmout\n");
2884 msm_otg_del_timer(motg);
2885 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002886 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302887 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002888 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302889 if (!test_bit(ID_A, &motg->inputs))
2890 msm_hsusb_vbus_power(motg, 0);
2891 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2892 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2893 pr_debug("!a_vbus_vld\n");
2894 msm_otg_del_timer(motg);
2895 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002896 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302897 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002898 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302899 } else if (!test_bit(B_CONN, &motg->inputs) &&
2900 otg->host->b_hnp_enable) {
2901 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002902 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302903 msm_otg_host_hnp_enable(otg, 1);
2904 otg->gadget->is_a_peripheral = 1;
2905 msm_otg_start_peripheral(otg, 1);
2906 } else if (!test_bit(B_CONN, &motg->inputs) &&
2907 !otg->host->b_hnp_enable) {
2908 pr_debug("!b_conn && !b_hnp_enable");
2909 /*
2910 * bus request is dropped during suspend.
2911 * acquire again for next device.
2912 */
2913 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002914 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302915 if (TA_WAIT_BCON > 0)
2916 msm_otg_start_timer(motg, TA_WAIT_BCON,
2917 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002918 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302919 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002920 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302921 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002922 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002923 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302924 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302925 }
2926 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302927 case OTG_STATE_A_PERIPHERAL:
2928 if ((test_bit(ID, &motg->inputs) &&
2929 !test_bit(ID_A, &motg->inputs)) ||
2930 test_bit(A_BUS_DROP, &motg->inputs)) {
2931 pr_debug("id _f/b/c || a_bus_drop\n");
2932 /* Clear BIDL_ADIS timer */
2933 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002934 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302935 msm_otg_start_peripheral(otg, 0);
2936 otg->gadget->is_a_peripheral = 0;
2937 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002938 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302939 if (!test_bit(ID_A, &motg->inputs))
2940 msm_hsusb_vbus_power(motg, 0);
2941 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2942 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2943 pr_debug("!a_vbus_vld\n");
2944 /* Clear BIDL_ADIS timer */
2945 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002946 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302947 msm_otg_start_peripheral(otg, 0);
2948 otg->gadget->is_a_peripheral = 0;
2949 msm_otg_start_host(otg, 0);
2950 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2951 pr_debug("a_bidl_adis_tmout\n");
2952 msm_otg_start_peripheral(otg, 0);
2953 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002954 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302955 set_bit(A_BUS_REQ, &motg->inputs);
2956 msm_otg_host_hnp_enable(otg, 0);
2957 if (TA_WAIT_BCON > 0)
2958 msm_otg_start_timer(motg, TA_WAIT_BCON,
2959 A_WAIT_BCON);
2960 } else if (test_bit(ID_A, &motg->inputs)) {
2961 msm_hsusb_vbus_power(motg, 0);
2962 msm_otg_notify_charger(motg,
2963 IDEV_CHG_MIN - motg->mA_port);
2964 } else if (!test_bit(ID, &motg->inputs)) {
2965 msm_otg_notify_charger(motg, 0);
2966 msm_hsusb_vbus_power(motg, 1);
2967 }
2968 break;
2969 case OTG_STATE_A_WAIT_VFALL:
2970 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2971 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002972 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302973 work = 1;
2974 }
2975 break;
2976 case OTG_STATE_A_VBUS_ERR:
2977 if ((test_bit(ID, &motg->inputs) &&
2978 !test_bit(ID_A, &motg->inputs)) ||
2979 test_bit(A_BUS_DROP, &motg->inputs) ||
2980 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002981 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302982 if (!test_bit(ID_A, &motg->inputs))
2983 msm_hsusb_vbus_power(motg, 0);
2984 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2985 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2986 motg->chg_type = USB_INVALID_CHARGER;
2987 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302988 }
2989 break;
2990 default:
2991 break;
2992 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302993 if (work)
2994 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302995}
2996
Amit Blayd0fe07b2012-09-05 16:42:09 +03002997static void msm_otg_suspend_work(struct work_struct *w)
2998{
2999 struct msm_otg *motg =
3000 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05303001
3002 /* This work is only for device bus suspend */
3003 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
3004 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003005}
3006
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303007static irqreturn_t msm_otg_irq(int irq, void *data)
3008{
3009 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003010 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303011 u32 otgsc = 0, usbsts, pc;
3012 bool work = 0;
3013 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303014
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303015 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07003016 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303017 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07003018 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07003019 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07003020 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303021 return IRQ_HANDLED;
3022 }
3023
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003024 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303025 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303026
3027 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303028 return IRQ_NONE;
3029
3030 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303031 if (otgsc & OTGSC_ID) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003032 dev_dbg(otg->phy->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303033 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303034 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003035 dev_dbg(otg->phy->dev, "ID clear\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303036 /*
3037 * Assert a_bus_req to supply power on
3038 * VBUS when Micro/Mini-A cable is connected
3039 * with out user intervention.
3040 */
3041 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303042 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303043 msm_chg_enable_aca_det(motg);
3044 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303045 writel_relaxed(otgsc, USB_OTGSC);
3046 work = 1;
3047 } else if (otgsc & OTGSC_DPIS) {
3048 pr_debug("DPIS detected\n");
3049 writel_relaxed(otgsc, USB_OTGSC);
3050 set_bit(A_SRP_DET, &motg->inputs);
3051 set_bit(A_BUS_REQ, &motg->inputs);
3052 work = 1;
Vamsi Krishnaef6e1bf2013-03-02 15:36:17 -08003053 } else if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303054 writel_relaxed(otgsc, USB_OTGSC);
3055 /*
3056 * BSV interrupt comes when operating as an A-device
3057 * (VBUS on/off).
3058 * But, handle BSV when charger is removed from ACA in ID_A
3059 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003060 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303061 !test_bit(ID_A, &motg->inputs))
3062 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303063 if (otgsc & OTGSC_BSV) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003064 dev_dbg(otg->phy->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303065 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303066 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003067 dev_dbg(otg->phy->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303068 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003069 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3070
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303071 msm_chg_check_aca_intr(motg);
3072 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303073 work = 1;
3074 } else if (usbsts & STS_PCI) {
3075 pc = readl_relaxed(USB_PORTSC);
3076 pr_debug("portsc = %x\n", pc);
3077 ret = IRQ_NONE;
3078 /*
3079 * HCD Acks PCI interrupt. We use this to switch
3080 * between different OTG states.
3081 */
3082 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003083 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303084 case OTG_STATE_A_SUSPEND:
3085 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3086 !(pc & PORTSC_CCS)) {
3087 pr_debug("B_CONN clear\n");
3088 clear_bit(B_CONN, &motg->inputs);
3089 msm_otg_del_timer(motg);
3090 }
3091 break;
3092 case OTG_STATE_A_PERIPHERAL:
3093 /*
3094 * A-peripheral observed activity on bus.
3095 * clear A_BIDL_ADIS timer.
3096 */
3097 msm_otg_del_timer(motg);
3098 work = 0;
3099 break;
3100 case OTG_STATE_B_WAIT_ACON:
3101 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3102 pr_debug("A_CONN set\n");
3103 set_bit(A_CONN, &motg->inputs);
3104 /* Clear ASE0_BRST timer */
3105 msm_otg_del_timer(motg);
3106 }
3107 break;
3108 case OTG_STATE_B_HOST:
3109 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3110 pr_debug("A_CONN clear\n");
3111 clear_bit(A_CONN, &motg->inputs);
3112 msm_otg_del_timer(motg);
3113 }
3114 break;
3115 case OTG_STATE_A_WAIT_BCON:
3116 if (TA_WAIT_BCON < 0)
3117 set_bit(A_BUS_REQ, &motg->inputs);
3118 default:
3119 work = 0;
3120 break;
3121 }
3122 } else if (usbsts & STS_URI) {
3123 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003124 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303125 case OTG_STATE_A_PERIPHERAL:
3126 /*
3127 * A-peripheral observed activity on bus.
3128 * clear A_BIDL_ADIS timer.
3129 */
3130 msm_otg_del_timer(motg);
3131 work = 0;
3132 break;
3133 default:
3134 work = 0;
3135 break;
3136 }
3137 } else if (usbsts & STS_SLI) {
3138 ret = IRQ_NONE;
3139 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003140 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303141 case OTG_STATE_B_PERIPHERAL:
3142 if (otg->gadget->b_hnp_enable) {
3143 set_bit(A_BUS_SUSPEND, &motg->inputs);
3144 set_bit(B_BUS_REQ, &motg->inputs);
3145 work = 1;
3146 }
3147 break;
3148 case OTG_STATE_A_PERIPHERAL:
3149 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3150 A_BIDL_ADIS);
3151 break;
3152 default:
3153 break;
3154 }
3155 } else if ((usbsts & PHY_ALT_INT)) {
3156 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3157 if (msm_chg_check_aca_intr(motg))
3158 work = 1;
3159 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303160 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303161 if (work)
3162 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303163
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303164 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003165}
3166
3167static void msm_otg_set_vbus_state(int online)
3168{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303169 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003170 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303171
Vamsi Krishna945b4a92013-03-02 15:31:16 -08003172 if (online) {
3173 pr_debug("PMIC: BSV set\n");
3174 set_bit(B_SESS_VLD, &motg->inputs);
3175 } else {
3176 pr_debug("PMIC: BSV clear\n");
3177 clear_bit(B_SESS_VLD, &motg->inputs);
3178 }
3179
3180 /* do not queue state m/c work if id is grounded */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303181 if (!test_bit(ID, &motg->inputs)) {
3182 /*
3183 * state machine work waits for initial VBUS
3184 * completion in UNDEFINED state. Process
3185 * the initial VBUS event in ID_GND state.
3186 */
3187 if (init)
3188 return;
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303189 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003190
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303191 if (!init) {
3192 init = true;
3193 complete(&pmic_vbus_init);
3194 pr_debug("PMIC: BSV init complete\n");
3195 return;
3196 }
3197
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303198 if (test_bit(MHL, &motg->inputs) ||
3199 mhl_det_in_progress) {
3200 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3201 return;
3202 }
3203
Jack Pham5ca279b2012-05-14 18:42:54 -07003204 if (atomic_read(&motg->pm_suspended))
3205 motg->sm_work_pending = true;
3206 else
3207 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003208}
3209
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303210static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003211{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303212 struct msm_otg *motg = container_of(w, struct msm_otg,
3213 pmic_id_status_work.work);
3214 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003215
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303216 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303217 if (!test_and_set_bit(ID, &motg->inputs)) {
3218 pr_debug("PMIC: ID set\n");
3219 work = 1;
3220 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303221 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303222 if (test_and_clear_bit(ID, &motg->inputs)) {
3223 pr_debug("PMIC: ID clear\n");
3224 set_bit(A_BUS_REQ, &motg->inputs);
3225 work = 1;
3226 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303227 }
3228
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303229 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003230 if (atomic_read(&motg->pm_suspended))
3231 motg->sm_work_pending = true;
3232 else
3233 queue_work(system_nrt_wq, &motg->sm_work);
3234 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303235
3236}
3237
3238#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3239static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3240{
3241 struct msm_otg *motg = data;
3242
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303243 if (test_bit(MHL, &motg->inputs) ||
3244 mhl_det_in_progress) {
3245 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3246 return IRQ_HANDLED;
3247 }
3248
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303249 if (!aca_id_turned_on)
3250 /*schedule delayed work for 5msec for ID line state to settle*/
3251 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3252 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003253
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303254 return IRQ_HANDLED;
3255}
3256
3257static int msm_otg_mode_show(struct seq_file *s, void *unused)
3258{
3259 struct msm_otg *motg = s->private;
Stephen Boyd9850acb2013-01-28 14:11:20 -08003260 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303261
Stephen Boyd9850acb2013-01-28 14:11:20 -08003262 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303263 case OTG_STATE_A_HOST:
3264 seq_printf(s, "host\n");
3265 break;
3266 case OTG_STATE_B_PERIPHERAL:
3267 seq_printf(s, "peripheral\n");
3268 break;
3269 default:
3270 seq_printf(s, "none\n");
3271 break;
3272 }
3273
3274 return 0;
3275}
3276
3277static int msm_otg_mode_open(struct inode *inode, struct file *file)
3278{
3279 return single_open(file, msm_otg_mode_show, inode->i_private);
3280}
3281
3282static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3283 size_t count, loff_t *ppos)
3284{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303285 struct seq_file *s = file->private_data;
3286 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303287 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003288 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303289 int status = count;
3290 enum usb_mode_type req_mode;
3291
3292 memset(buf, 0x00, sizeof(buf));
3293
3294 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3295 status = -EFAULT;
3296 goto out;
3297 }
3298
3299 if (!strncmp(buf, "host", 4)) {
3300 req_mode = USB_HOST;
3301 } else if (!strncmp(buf, "peripheral", 10)) {
3302 req_mode = USB_PERIPHERAL;
3303 } else if (!strncmp(buf, "none", 4)) {
3304 req_mode = USB_NONE;
3305 } else {
3306 status = -EINVAL;
3307 goto out;
3308 }
3309
3310 switch (req_mode) {
3311 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003312 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303313 case OTG_STATE_A_HOST:
3314 case OTG_STATE_B_PERIPHERAL:
3315 set_bit(ID, &motg->inputs);
3316 clear_bit(B_SESS_VLD, &motg->inputs);
3317 break;
3318 default:
3319 goto out;
3320 }
3321 break;
3322 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003323 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303324 case OTG_STATE_B_IDLE:
3325 case OTG_STATE_A_HOST:
3326 set_bit(ID, &motg->inputs);
3327 set_bit(B_SESS_VLD, &motg->inputs);
3328 break;
3329 default:
3330 goto out;
3331 }
3332 break;
3333 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003334 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303335 case OTG_STATE_B_IDLE:
3336 case OTG_STATE_B_PERIPHERAL:
3337 clear_bit(ID, &motg->inputs);
3338 break;
3339 default:
3340 goto out;
3341 }
3342 break;
3343 default:
3344 goto out;
3345 }
3346
Steve Mucklef132c6c2012-06-06 18:30:57 -07003347 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303348 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303349out:
3350 return status;
3351}
3352
3353const struct file_operations msm_otg_mode_fops = {
3354 .open = msm_otg_mode_open,
3355 .read = seq_read,
3356 .write = msm_otg_mode_write,
3357 .llseek = seq_lseek,
3358 .release = single_release,
3359};
3360
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303361static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3362{
3363 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003364 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303365
Steve Mucklef132c6c2012-06-06 18:30:57 -07003366 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303367 return 0;
3368}
3369
3370static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3371{
3372 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3373}
3374
3375const struct file_operations msm_otg_state_fops = {
3376 .open = msm_otg_otg_state_open,
3377 .read = seq_read,
3378 .llseek = seq_lseek,
3379 .release = single_release,
3380};
3381
Anji jonnalad270e2d2011-08-09 11:28:32 +05303382static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3383{
3384 struct msm_otg *motg = s->private;
3385
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303386 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303387 return 0;
3388}
3389
3390static int msm_otg_chg_open(struct inode *inode, struct file *file)
3391{
3392 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3393}
3394
3395const struct file_operations msm_otg_chg_fops = {
3396 .open = msm_otg_chg_open,
3397 .read = seq_read,
3398 .llseek = seq_lseek,
3399 .release = single_release,
3400};
3401
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303402static int msm_otg_aca_show(struct seq_file *s, void *unused)
3403{
3404 if (debug_aca_enabled)
3405 seq_printf(s, "enabled\n");
3406 else
3407 seq_printf(s, "disabled\n");
3408
3409 return 0;
3410}
3411
3412static int msm_otg_aca_open(struct inode *inode, struct file *file)
3413{
3414 return single_open(file, msm_otg_aca_show, inode->i_private);
3415}
3416
3417static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3418 size_t count, loff_t *ppos)
3419{
3420 char buf[8];
3421
3422 memset(buf, 0x00, sizeof(buf));
3423
3424 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3425 return -EFAULT;
3426
3427 if (!strncmp(buf, "enable", 6))
3428 debug_aca_enabled = true;
3429 else
3430 debug_aca_enabled = false;
3431
3432 return count;
3433}
3434
3435const struct file_operations msm_otg_aca_fops = {
3436 .open = msm_otg_aca_open,
3437 .read = seq_read,
3438 .write = msm_otg_aca_write,
3439 .llseek = seq_lseek,
3440 .release = single_release,
3441};
3442
Manu Gautam8bdcc592012-03-06 11:26:06 +05303443static int msm_otg_bus_show(struct seq_file *s, void *unused)
3444{
3445 if (debug_bus_voting_enabled)
3446 seq_printf(s, "enabled\n");
3447 else
3448 seq_printf(s, "disabled\n");
3449
3450 return 0;
3451}
3452
3453static int msm_otg_bus_open(struct inode *inode, struct file *file)
3454{
3455 return single_open(file, msm_otg_bus_show, inode->i_private);
3456}
3457
3458static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3459 size_t count, loff_t *ppos)
3460{
3461 char buf[8];
3462 int ret;
3463 struct seq_file *s = file->private_data;
3464 struct msm_otg *motg = s->private;
3465
3466 memset(buf, 0x00, sizeof(buf));
3467
3468 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3469 return -EFAULT;
3470
3471 if (!strncmp(buf, "enable", 6)) {
3472 /* Do not vote here. Let OTG statemachine decide when to vote */
3473 debug_bus_voting_enabled = true;
3474 } else {
3475 debug_bus_voting_enabled = false;
3476 if (motg->bus_perf_client) {
3477 ret = msm_bus_scale_client_update_request(
3478 motg->bus_perf_client, 0);
3479 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003480 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303481 "for bus bw %d\n", __func__, ret);
3482 }
3483 }
3484
3485 return count;
3486}
3487
David Keitel272ce522012-08-17 16:25:24 -07003488static int otg_power_get_property_usb(struct power_supply *psy,
3489 enum power_supply_property psp,
3490 union power_supply_propval *val)
3491{
Jack Pham0c695282012-10-19 18:13:03 -07003492 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003493 switch (psp) {
3494 case POWER_SUPPLY_PROP_SCOPE:
3495 if (motg->host_mode)
3496 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3497 else
3498 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3499 break;
3500 case POWER_SUPPLY_PROP_CURRENT_MAX:
3501 val->intval = motg->current_max;
3502 break;
3503 /* Reflect USB enumeration */
3504 case POWER_SUPPLY_PROP_PRESENT:
3505 case POWER_SUPPLY_PROP_ONLINE:
3506 val->intval = motg->online;
3507 break;
3508 default:
3509 return -EINVAL;
3510 }
3511 return 0;
3512}
3513
3514static int otg_power_set_property_usb(struct power_supply *psy,
3515 enum power_supply_property psp,
3516 const union power_supply_propval *val)
3517{
Jack Pham0c695282012-10-19 18:13:03 -07003518 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003519
3520 switch (psp) {
3521 /* Process PMIC notification in PRESENT prop */
3522 case POWER_SUPPLY_PROP_PRESENT:
3523 msm_otg_set_vbus_state(val->intval);
3524 break;
3525 /* The ONLINE property reflects if usb has enumerated */
3526 case POWER_SUPPLY_PROP_ONLINE:
3527 motg->online = val->intval;
3528 break;
3529 case POWER_SUPPLY_PROP_CURRENT_MAX:
3530 motg->current_max = val->intval;
3531 break;
3532 default:
3533 return -EINVAL;
3534 }
3535
3536 power_supply_changed(&motg->usb_psy);
3537 return 0;
3538}
3539
David Collinsd79acc52012-11-26 14:59:00 -08003540static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3541 enum power_supply_property psp)
3542{
3543 switch (psp) {
3544 case POWER_SUPPLY_PROP_PRESENT:
3545 case POWER_SUPPLY_PROP_ONLINE:
3546 case POWER_SUPPLY_PROP_CURRENT_MAX:
3547 return 1;
3548 default:
3549 break;
3550 }
3551
3552 return 0;
3553}
3554
David Keitel272ce522012-08-17 16:25:24 -07003555static char *otg_pm_power_supplied_to[] = {
3556 "battery",
3557};
3558
3559static enum power_supply_property otg_pm_power_props_usb[] = {
3560 POWER_SUPPLY_PROP_PRESENT,
3561 POWER_SUPPLY_PROP_ONLINE,
3562 POWER_SUPPLY_PROP_CURRENT_MAX,
3563 POWER_SUPPLY_PROP_SCOPE,
3564};
3565
Manu Gautam8bdcc592012-03-06 11:26:06 +05303566const struct file_operations msm_otg_bus_fops = {
3567 .open = msm_otg_bus_open,
3568 .read = seq_read,
3569 .write = msm_otg_bus_write,
3570 .llseek = seq_lseek,
3571 .release = single_release,
3572};
3573
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303574static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303575
3576static int msm_otg_debugfs_init(struct msm_otg *motg)
3577{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303578 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303579
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303580 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3581
3582 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3583 return -ENODEV;
3584
Anji jonnalad270e2d2011-08-09 11:28:32 +05303585 if (motg->pdata->mode == USB_OTG &&
3586 motg->pdata->otg_control == OTG_USER_CONTROL) {
3587
Manu Gautam8bdcc592012-03-06 11:26:06 +05303588 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303589 S_IWUSR, msm_otg_dbg_root, motg,
3590 &msm_otg_mode_fops);
3591
Manu Gautam8bdcc592012-03-06 11:26:06 +05303592 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303593 debugfs_remove(msm_otg_dbg_root);
3594 msm_otg_dbg_root = NULL;
3595 return -ENODEV;
3596 }
3597 }
3598
Manu Gautam8bdcc592012-03-06 11:26:06 +05303599 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303600 msm_otg_dbg_root, motg,
3601 &msm_otg_chg_fops);
3602
Manu Gautam8bdcc592012-03-06 11:26:06 +05303603 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303604 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303605 return -ENODEV;
3606 }
3607
Manu Gautam8bdcc592012-03-06 11:26:06 +05303608 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303609 msm_otg_dbg_root, motg,
3610 &msm_otg_aca_fops);
3611
Manu Gautam8bdcc592012-03-06 11:26:06 +05303612 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303613 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303614 return -ENODEV;
3615 }
3616
Manu Gautam8bdcc592012-03-06 11:26:06 +05303617 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3618 msm_otg_dbg_root, motg,
3619 &msm_otg_bus_fops);
3620
3621 if (!msm_otg_dentry) {
3622 debugfs_remove_recursive(msm_otg_dbg_root);
3623 return -ENODEV;
3624 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303625
3626 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3627 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3628
3629 if (!msm_otg_dentry) {
3630 debugfs_remove_recursive(msm_otg_dbg_root);
3631 return -ENODEV;
3632 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303633 return 0;
3634}
3635
3636static void msm_otg_debugfs_cleanup(void)
3637{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303638 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303639}
3640
Manu Gautam0ddbd922012-09-21 17:17:38 +05303641#define MSM_OTG_CMD_ID 0x09
3642#define MSM_OTG_DEVICE_ID 0x04
3643#define MSM_OTG_VMID_IDX 0xFF
3644#define MSM_OTG_MEM_TYPE 0x02
3645struct msm_otg_scm_cmd_buf {
3646 unsigned int device_id;
3647 unsigned int vmid_idx;
3648 unsigned int mem_type;
3649} __attribute__ ((__packed__));
3650
3651static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3652{
3653 int ret;
3654 struct msm_otg_platform_data *pdata = motg->pdata;
3655 struct msm_otg_scm_cmd_buf cmd_buf;
3656
3657 if (!pdata->pnoc_errata_fix)
3658 return;
3659
3660 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3661
3662 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3663 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3664 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3665
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -07003666 ret = scm_call(SCM_SVC_MP, MSM_OTG_CMD_ID, &cmd_buf,
Manu Gautam0ddbd922012-09-21 17:17:38 +05303667 sizeof(cmd_buf), NULL, 0);
3668
3669 if (ret)
3670 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3671}
3672
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303673static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3674static struct platform_device *msm_otg_add_pdev(
3675 struct platform_device *ofdev, const char *name)
3676{
3677 struct platform_device *pdev;
3678 const struct resource *res = ofdev->resource;
3679 unsigned int num = ofdev->num_resources;
3680 int retval;
3681
3682 pdev = platform_device_alloc(name, -1);
3683 if (!pdev) {
3684 retval = -ENOMEM;
3685 goto error;
3686 }
3687
3688 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3689 pdev->dev.dma_mask = &msm_otg_dma_mask;
3690
3691 if (num) {
3692 retval = platform_device_add_resources(pdev, res, num);
3693 if (retval)
3694 goto error;
3695 }
3696
3697 retval = platform_device_add(pdev);
3698 if (retval)
3699 goto error;
3700
3701 return pdev;
3702
3703error:
3704 platform_device_put(pdev);
3705 return ERR_PTR(retval);
3706}
3707
3708static int msm_otg_setup_devices(struct platform_device *ofdev,
3709 enum usb_mode_type mode, bool init)
3710{
3711 const char *gadget_name = "msm_hsusb";
3712 const char *host_name = "msm_hsusb_host";
3713 static struct platform_device *gadget_pdev;
3714 static struct platform_device *host_pdev;
3715 int retval = 0;
3716
3717 if (!init) {
3718 if (gadget_pdev)
3719 platform_device_unregister(gadget_pdev);
3720 if (host_pdev)
3721 platform_device_unregister(host_pdev);
3722 return 0;
3723 }
3724
3725 switch (mode) {
3726 case USB_OTG:
3727 /* fall through */
3728 case USB_PERIPHERAL:
3729 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3730 if (IS_ERR(gadget_pdev)) {
3731 retval = PTR_ERR(gadget_pdev);
3732 break;
3733 }
3734 if (mode == USB_PERIPHERAL)
3735 break;
3736 /* fall through */
3737 case USB_HOST:
3738 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3739 if (IS_ERR(host_pdev)) {
3740 retval = PTR_ERR(host_pdev);
3741 if (mode == USB_OTG)
3742 platform_device_unregister(gadget_pdev);
3743 }
3744 break;
3745 default:
3746 break;
3747 }
3748
3749 return retval;
3750}
3751
David Keitel272ce522012-08-17 16:25:24 -07003752static int msm_otg_register_power_supply(struct platform_device *pdev,
3753 struct msm_otg *motg)
3754{
3755 int ret;
3756
3757 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3758 if (ret < 0) {
3759 dev_err(motg->phy.dev,
3760 "%s:power_supply_register usb failed\n",
3761 __func__);
3762 return ret;
3763 }
3764
3765 legacy_power_supply = false;
3766 return 0;
3767}
3768
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303769struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3770{
3771 struct device_node *node = pdev->dev.of_node;
3772 struct msm_otg_platform_data *pdata;
3773 int len = 0;
3774
3775 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3776 if (!pdata) {
3777 pr_err("unable to allocate platform data\n");
3778 return NULL;
3779 }
3780 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3781 if (len) {
3782 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3783 if (!pdata->phy_init_seq)
3784 return NULL;
3785 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3786 pdata->phy_init_seq,
3787 len/sizeof(*pdata->phy_init_seq));
3788 }
3789 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3790 &pdata->power_budget);
3791 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3792 &pdata->mode);
3793 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3794 &pdata->otg_control);
3795 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3796 &pdata->default_mode);
3797 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3798 &pdata->phy_type);
Manu Gautambd53fba2012-07-31 16:13:06 +05303799 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3800 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05303801 pdata->pnoc_errata_fix = of_property_read_bool(node,
3802 "qcom,hsusb-otg-pnoc-errata-fix");
Ido Shayevitza7114b92013-01-13 13:34:47 +02003803 pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
3804 "qcom,hsusb-otg-lpm-on-dev-suspend");
Ido Shayevitz26193352013-01-21 23:16:54 +02003805 pdata->core_clk_always_on_workaround = of_property_read_bool(node,
3806 "qcom,hsusb-otg-clk-always-on-workaround");
Shimrit Malichiffab5b02013-03-10 11:06:16 +02003807 pdata->delay_lpm_on_disconnect = of_property_read_bool(node,
3808 "qcom,hsusb-otg-delay-lpm");
Lena Salmanabde35d2013-04-25 15:29:43 +03003809 pdata->delay_lpm_hndshk_on_disconnect = of_property_read_bool(node,
3810 "qcom,hsusb-otg-delay-lpm-hndshk-on-disconnect");
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08003811 pdata->dp_manual_pullup = of_property_read_bool(node,
3812 "qcom,dp-manual-pullup");
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05303813 pdata->enable_sec_phy = of_property_read_bool(node,
3814 "qcom,usb2-enable-hsphy2");
Manu Gautambd53fba2012-07-31 16:13:06 +05303815
Manu Gautam0e0c53f2013-04-04 10:55:20 +05303816 pdata->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
3817 if (pdata->pmic_id_irq < 0)
3818 pdata->pmic_id_irq = 0;
3819
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303820 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303821}
3822
3823static int __init msm_otg_probe(struct platform_device *pdev)
3824{
Manu Gautamf8c45642012-08-10 10:20:56 -07003825 int ret = 0;
Mayank Rana0f286cf2013-02-27 11:43:27 +05303826 int len = 0;
3827 u32 tmp[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303828 struct resource *res;
3829 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003830 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303831 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303832
3833 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303834
3835 if (pdev->dev.of_node) {
3836 dev_dbg(&pdev->dev, "device tree enabled\n");
3837 pdata = msm_otg_dt_to_pdata(pdev);
3838 if (!pdata)
3839 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003840
3841 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3842 if (!pdata->bus_scale_table)
3843 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3844
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303845 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3846 if (ret) {
3847 dev_err(&pdev->dev, "devices setup failed\n");
3848 return ret;
3849 }
3850 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303851 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3852 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303853 } else {
3854 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303855 }
3856
3857 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3858 if (!motg) {
3859 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3860 return -ENOMEM;
3861 }
3862
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003863 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3864 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003865 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3866 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303867 goto free_motg;
3868 }
3869
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003870 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303871 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003872 phy = &motg->phy;
3873 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303874
3875 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303876 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3877 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3878 * PMIC can detect ACA ID_GND and generate an interrupt.
3879 */
3880 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3881 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3882 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003883 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303884 }
3885
Ofir Cohen4da266f2012-01-03 10:19:29 +02003886 /* initialize reset counter */
3887 motg->reset_counter = 0;
3888
Amit Blay02eff132011-09-21 16:46:24 +03003889 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003890 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003891 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003892 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303893
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303894 /*
3895 * Targets on which link uses asynchronous reset methodology,
3896 * free running clock is not required during the reset.
3897 */
Manu Gautam5143b252012-01-05 19:25:23 -08003898 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303899 if (IS_ERR(motg->clk))
3900 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3901 else
3902 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303903
3904 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003905 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303906 * CORE CLK must be running at >55Mhz for correct HSUSB
3907 * operation and USB core cannot tolerate frequency changes on
3908 * CORE CLK. For such USB cores, vote for maximum clk frequency
3909 * on pclk source
3910 */
Manu Gautam5143b252012-01-05 19:25:23 -08003911 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3912 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303913 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003914 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303915 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003916 goto put_clk;
3917 }
Mayank Rana3eaf28d2013-03-27 14:04:04 +05303918
3919 /*
3920 * Get Max supported clk frequency for USB Core CLK and request
3921 * to set the same.
3922 */
3923 motg->core_clk_rate = clk_round_rate(motg->core_clk, LONG_MAX);
3924 if (IS_ERR_VALUE(motg->core_clk_rate)) {
3925 dev_err(&pdev->dev, "fail to get core clk max freq.\n");
3926 } else {
3927 ret = clk_set_rate(motg->core_clk, motg->core_clk_rate);
3928 if (ret)
3929 dev_err(&pdev->dev, "fail to set core_clk freq:%d\n",
3930 ret);
3931 }
Manu Gautam5143b252012-01-05 19:25:23 -08003932
3933 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3934 if (IS_ERR(motg->pclk)) {
3935 dev_err(&pdev->dev, "failed to get iface_clk\n");
3936 ret = PTR_ERR(motg->pclk);
3937 goto put_core_clk;
3938 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303939
3940 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3941 if (!res) {
3942 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3943 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003944 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303945 }
3946
3947 motg->regs = ioremap(res->start, resource_size(res));
3948 if (!motg->regs) {
3949 dev_err(&pdev->dev, "ioremap failed\n");
3950 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003951 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303952 }
3953 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3954
3955 motg->irq = platform_get_irq(pdev, 0);
3956 if (!motg->irq) {
3957 dev_err(&pdev->dev, "platform_get_irq failed\n");
3958 ret = -ENODEV;
3959 goto free_regs;
3960 }
3961
Manu Gautamf8c45642012-08-10 10:20:56 -07003962 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3963 if (motg->async_irq < 0) {
3964 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3965 motg->async_irq = 0;
3966 }
3967
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05303968 motg->xo_clk = clk_get(&pdev->dev, "xo");
3969 if (IS_ERR(motg->xo_clk)) {
3970 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
3971 if (IS_ERR(motg->xo_handle)) {
3972 dev_err(&pdev->dev, "%s fail to get handle for TCXO\n",
3973 __func__);
3974 ret = PTR_ERR(motg->xo_handle);
3975 goto free_regs;
3976 } else {
3977 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
3978 if (ret) {
3979 dev_err(&pdev->dev, "%s XO voting failed %d\n",
3980 __func__, ret);
3981 goto free_xo_handle;
3982 }
3983 }
3984 } else {
3985 ret = clk_prepare_enable(motg->xo_clk);
3986 if (ret) {
3987 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
3988 __func__, ret);
3989 goto free_xo_handle;
3990 }
Anji jonnala7da3f262011-12-02 17:22:14 -08003991 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303992
Anji jonnala7da3f262011-12-02 17:22:14 -08003993
Manu Gautam28b1bac2012-01-30 16:43:06 +05303994 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303995
Mayank Rana248698c2012-04-19 00:03:16 +05303996 motg->vdd_type = VDDCX_CORNER;
Mayank Rana0f286cf2013-02-27 11:43:27 +05303997 hsusb_vdd = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
3998 if (IS_ERR(hsusb_vdd)) {
3999 hsusb_vdd = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
4000 if (IS_ERR(hsusb_vdd)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004001 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304002 ret = PTR_ERR(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304003 goto devote_xo_handle;
4004 }
4005 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304006 }
4007
Mayank Rana0f286cf2013-02-27 11:43:27 +05304008 if (pdev->dev.of_node) {
4009 of_get_property(pdev->dev.of_node,
4010 "qcom,vdd-voltage-level",
4011 &len);
4012 if (len == sizeof(tmp)) {
4013 of_property_read_u32_array(pdev->dev.of_node,
4014 "qcom,vdd-voltage-level",
4015 tmp, len/sizeof(*tmp));
4016 vdd_val[motg->vdd_type][0] = tmp[0];
4017 vdd_val[motg->vdd_type][1] = tmp[1];
4018 vdd_val[motg->vdd_type][2] = tmp[2];
4019 } else {
4020 dev_dbg(&pdev->dev, "Using default hsusb vdd config.\n");
4021 }
4022 }
4023
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004024 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304025 if (ret) {
4026 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05304027 goto devote_xo_handle;
4028 }
4029
Mayank Rana0f286cf2013-02-27 11:43:27 +05304030 ret = regulator_enable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304031 if (ret) {
4032 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
4033 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304034 }
4035
4036 ret = msm_hsusb_ldo_init(motg, 1);
4037 if (ret) {
4038 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304039 goto free_hsusb_vdd;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304040 }
4041
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304042 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05304043 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
4044 "mhl_usb_hs_switch");
4045 if (IS_ERR(mhl_usb_hs_switch)) {
4046 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07004047 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304048 goto free_ldo_init;
4049 }
4050 }
4051
Amit Blay81801aa2012-09-19 12:08:12 +02004052 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304053 if (ret) {
4054 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004055 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304056 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05304057 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304058
Manu Gautam0ddbd922012-09-21 17:17:38 +05304059 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
4060 msm_otg_pnoc_errata_fix(motg);
4061
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304062 writel(0, USB_USBINTR);
4063 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004064 /* Ensure that above STOREs are completed before enabling interrupts */
4065 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304066
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304067 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
4068 if (ret)
4069 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004070 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05304071 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304072 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304073 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304074 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004075 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304076 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
4077 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05304078 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
4079 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304080 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
4081 "msm_otg", motg);
4082 if (ret) {
4083 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004084 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304085 }
4086
Manu Gautamf8c45642012-08-10 10:20:56 -07004087 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304088 ret = request_irq(motg->async_irq, msm_otg_irq,
4089 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004090 if (ret) {
4091 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4092 goto free_irq;
4093 }
4094 disable_irq(motg->async_irq);
4095 }
4096
Jack Pham87f202f2012-08-06 00:24:22 -07004097 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4098 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4099
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004100 phy->init = msm_otg_reset;
4101 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004102 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304103
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004104 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304105
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004106 phy->otg->phy = &motg->phy;
4107 phy->otg->set_host = msm_otg_set_host;
4108 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004109 phy->otg->start_hnp = msm_otg_start_hnp;
4110 phy->otg->start_srp = msm_otg_start_srp;
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004111 if (pdata->dp_manual_pullup)
4112 phy->flags |= ENABLE_DP_MANUAL_PULLUP;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004113
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304114 if (pdata->enable_sec_phy)
4115 phy->flags |= ENABLE_SECONDARY_PHY;
4116
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004117 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304118 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004119 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004120 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304121 }
4122
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304123 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304124 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004125 if (motg->pdata->pmic_id_irq) {
4126 ret = request_irq(motg->pdata->pmic_id_irq,
4127 msm_pmic_id_irq,
4128 IRQF_TRIGGER_RISING |
4129 IRQF_TRIGGER_FALLING,
4130 "msm_otg", motg);
4131 if (ret) {
4132 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004133 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004134 }
4135 } else {
4136 ret = -ENODEV;
4137 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004138 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004139 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304140 }
4141
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304142 msm_hsusb_mhl_switch_enable(motg, 1);
4143
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304144 platform_set_drvdata(pdev, motg);
4145 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004146 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304147
Anji jonnalad270e2d2011-08-09 11:28:32 +05304148 ret = msm_otg_debugfs_init(motg);
4149 if (ret)
4150 dev_dbg(&pdev->dev, "mode debugfs file is"
4151 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304152
Amit Blay58b31472011-11-18 09:39:39 +02004153 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4154 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304155 (!(motg->pdata->mode == USB_OTG) ||
4156 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004157 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304158 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004159
Amit Blay58b31472011-11-18 09:39:39 +02004160 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004161 motg->caps = ALLOW_PHY_RETENTION |
4162 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02004163 }
4164
Amit Blay6fa647a2012-05-24 14:12:08 +03004165 if (motg->pdata->enable_lpm_on_dev_suspend)
4166 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4167
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004168 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304169 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004170 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304171
Amit Blayd6f38282012-10-29 13:13:46 +02004172 if (motg->pdata->delay_lpm_on_disconnect) {
4173 pm_runtime_set_autosuspend_delay(&pdev->dev,
4174 lpm_disconnect_thresh);
4175 pm_runtime_use_autosuspend(&pdev->dev);
4176 }
4177
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304178 if (motg->pdata->bus_scale_table) {
4179 motg->bus_perf_client =
4180 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4181 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004182 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304183 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05304184 else
4185 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304186 }
4187
David Keitel272ce522012-08-17 16:25:24 -07004188 motg->usb_psy.name = "usb";
4189 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4190 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4191 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4192 motg->usb_psy.properties = otg_pm_power_props_usb;
4193 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4194 motg->usb_psy.get_property = otg_power_get_property_usb;
4195 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004196 motg->usb_psy.property_is_writeable
4197 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004198
Jack Pham0c695282012-10-19 18:13:03 -07004199 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004200 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004201 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4202 legacy_power_supply = true;
4203 } else {
4204 /* otherwise register our own power supply */
4205 if (!msm_otg_register_power_supply(pdev, motg))
4206 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004207 }
4208
Jack Pham0c695282012-10-19 18:13:03 -07004209 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4210 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4211
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304212 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004213
Steve Mucklef132c6c2012-06-06 18:30:57 -07004214remove_phy:
4215 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004216free_async_irq:
4217 if (motg->async_irq)
4218 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304219free_irq:
4220 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004221destroy_wlock:
4222 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304223 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004224 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004225free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304226 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304227free_hsusb_vdd:
4228 regulator_disable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304229free_config_vddcx:
Mayank Rana0f286cf2013-02-27 11:43:27 +05304230 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304231 vdd_val[motg->vdd_type][VDD_NONE],
4232 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004233devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304234 clk_disable_unprepare(motg->pclk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304235 if (!IS_ERR(motg->xo_clk))
4236 clk_disable_unprepare(motg->xo_clk);
4237 else
4238 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004239free_xo_handle:
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304240 if (!IS_ERR(motg->xo_clk))
4241 clk_put(motg->xo_clk);
4242 else
4243 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304244free_regs:
4245 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004246put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304247 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304248put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004249 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304250put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304251 if (!IS_ERR(motg->clk))
4252 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004253 if (!IS_ERR(motg->phy_reset_clk))
4254 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004255free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004256 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304257free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304258 kfree(motg);
4259 return ret;
4260}
4261
4262static int __devexit msm_otg_remove(struct platform_device *pdev)
4263{
4264 struct msm_otg *motg = platform_get_drvdata(pdev);
Stephen Boyd9850acb2013-01-28 14:11:20 -08004265 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304266 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304267
Stephen Boyd9850acb2013-01-28 14:11:20 -08004268 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304269 return -EBUSY;
4270
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304271 if (pdev->dev.of_node)
4272 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004273 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4274 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304275 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304276 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304277 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304278 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004279 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304280 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304281
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304282 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304283
4284 device_init_wakeup(&pdev->dev, 0);
4285 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004286 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304287
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304288 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004289 if (motg->pdata->pmic_id_irq)
4290 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004291 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304292 free_irq(motg->irq, motg);
4293
Jack Pham87f202f2012-08-06 00:24:22 -07004294 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4295 motg->pdata->mpm_otgsessvld_int)
4296 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4297
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304298 /*
4299 * Put PHY in low power mode.
4300 */
Stephen Boyd9850acb2013-01-28 14:11:20 -08004301 ulpi_read(phy, 0x14);
4302 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304303
4304 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4305 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4306 if (readl(USB_PORTSC) & PORTSC_PHCD)
4307 break;
4308 udelay(1);
4309 cnt++;
4310 }
4311 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Stephen Boyd9850acb2013-01-28 14:11:20 -08004312 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304313
Manu Gautam28b1bac2012-01-30 16:43:06 +05304314 clk_disable_unprepare(motg->pclk);
4315 clk_disable_unprepare(motg->core_clk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304316 if (!IS_ERR(motg->xo_clk)) {
4317 clk_disable_unprepare(motg->xo_clk);
4318 clk_put(motg->xo_clk);
4319 } else {
4320 msm_xo_put(motg->xo_handle);
4321 }
Amit Blay81801aa2012-09-19 12:08:12 +02004322 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304323 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304324 regulator_disable(hsusb_vdd);
4325 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304326 vdd_val[motg->vdd_type][VDD_NONE],
4327 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304328
4329 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304330 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304331
Amit Blay02eff132011-09-21 16:46:24 +03004332 if (!IS_ERR(motg->phy_reset_clk))
4333 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304334 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304335 if (!IS_ERR(motg->clk))
4336 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004337 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304338
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304339 if (motg->bus_perf_client)
4340 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304341
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004342 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304343 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304344 return 0;
4345}
4346
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304347#ifdef CONFIG_PM_RUNTIME
4348static int msm_otg_runtime_idle(struct device *dev)
4349{
4350 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004351 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304352
4353 dev_dbg(dev, "OTG runtime idle\n");
4354
Steve Mucklef132c6c2012-06-06 18:30:57 -07004355 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304356 return -EAGAIN;
4357 else
4358 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304359}
4360
4361static int msm_otg_runtime_suspend(struct device *dev)
4362{
4363 struct msm_otg *motg = dev_get_drvdata(dev);
4364
4365 dev_dbg(dev, "OTG runtime suspend\n");
4366 return msm_otg_suspend(motg);
4367}
4368
4369static int msm_otg_runtime_resume(struct device *dev)
4370{
4371 struct msm_otg *motg = dev_get_drvdata(dev);
4372
4373 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304374 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304375 return msm_otg_resume(motg);
4376}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304377#endif
4378
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304379#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304380static int msm_otg_pm_suspend(struct device *dev)
4381{
Jack Pham5ca279b2012-05-14 18:42:54 -07004382 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304383 struct msm_otg *motg = dev_get_drvdata(dev);
4384
4385 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004386
4387 atomic_set(&motg->pm_suspended, 1);
4388 ret = msm_otg_suspend(motg);
4389 if (ret)
4390 atomic_set(&motg->pm_suspended, 0);
4391
4392 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304393}
4394
4395static int msm_otg_pm_resume(struct device *dev)
4396{
Jack Pham5ca279b2012-05-14 18:42:54 -07004397 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304398 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304399
4400 dev_dbg(dev, "OTG PM resume\n");
4401
Jack Pham5ca279b2012-05-14 18:42:54 -07004402 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004403 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004404 pm_runtime_get_noresume(dev);
4405 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304406
Jack Pham5ca279b2012-05-14 18:42:54 -07004407 /* Update runtime PM status */
4408 pm_runtime_disable(dev);
4409 pm_runtime_set_active(dev);
4410 pm_runtime_enable(dev);
4411
Jack Phamc7edb172012-08-13 15:32:39 -07004412 if (motg->sm_work_pending) {
4413 motg->sm_work_pending = false;
4414 queue_work(system_nrt_wq, &motg->sm_work);
4415 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004416 }
4417
4418 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304419}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304420#endif
4421
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304422#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304423static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304424 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4425 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4426 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304427};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304428#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304429
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304430static struct of_device_id msm_otg_dt_match[] = {
4431 { .compatible = "qcom,hsusb-otg",
4432 },
4433 {}
4434};
4435
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304436static struct platform_driver msm_otg_driver = {
4437 .remove = __devexit_p(msm_otg_remove),
4438 .driver = {
4439 .name = DRIVER_NAME,
4440 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304441#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304442 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304443#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304444 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304445 },
4446};
4447
4448static int __init msm_otg_init(void)
4449{
4450 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4451}
4452
4453static void __exit msm_otg_exit(void)
4454{
4455 platform_driver_unregister(&msm_otg_driver);
4456}
4457
4458module_init(msm_otg_init);
4459module_exit(msm_otg_exit);
4460
4461MODULE_LICENSE("GPL v2");
4462MODULE_DESCRIPTION("MSM USB transceiver driver");