blob: 06e3a1b90cc91dd3b945d2d6f99e3c0f50ca0243 [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>
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +053039#include <linux/usb/msm_ext_chg.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053040#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053042#include <linux/mfd/pm8xxx/misc.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053043#include <linux/mhl_8334.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
Manu Gautam0ddbd922012-09-21 17:17:38 +053045#include <mach/scm.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053046#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070047#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080048#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053049#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053050#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053051
52#define MSM_USB_BASE (motg->regs)
53#define DRIVER_NAME "msm_otg"
54
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053055#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondeti458d8792012-09-28 14:45:18 +053056#define CHG_RECHECK_DELAY (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053057#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053058#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
59#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
60#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
61#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
62
63#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
64#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
65#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
66#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
67
Mayank Rana248698c2012-04-19 00:03:16 +053068#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080069#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053070#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
71
Amit Blayd0fe07b2012-09-05 16:42:09 +030072#define USB_SUSPEND_DELAY_TIME (500 * HZ/1000) /* 500 msec */
73
Amit Blay81801aa2012-09-19 12:08:12 +020074enum msm_otg_phy_reg_mode {
75 USB_PHY_REG_OFF,
76 USB_PHY_REG_ON,
77 USB_PHY_REG_LPM_ON,
78 USB_PHY_REG_LPM_OFF,
79};
80
Mayank Rana443f9e42012-09-21 18:32:39 +053081static char *override_phy_init;
82module_param(override_phy_init, charp, S_IRUGO|S_IWUSR);
83MODULE_PARM_DESC(override_phy_init,
84 "Override HSUSB PHY Init Settings");
85
Amit Blayd6f38282012-10-29 13:13:46 +020086unsigned int lpm_disconnect_thresh = 1000;
87module_param(lpm_disconnect_thresh , uint, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(lpm_disconnect_thresh,
89 "Delay before entering LPM on USB disconnect");
90
zhenhuahb6d5edc2013-06-19 14:37:32 +080091static bool floated_charger_enable;
92module_param(floated_charger_enable , bool, S_IRUGO | S_IWUSR);
93MODULE_PARM_DESC(floated_charger_enable,
94 "Whether to enable floated charger");
95
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053096static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053098static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053099static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530100static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101
Anji jonnala11aa5c42011-05-04 10:19:48 +0530102static struct regulator *hsusb_3p3;
103static struct regulator *hsusb_1p8;
Mayank Rana0f286cf2013-02-27 11:43:27 +0530104static struct regulator *hsusb_vdd;
Mayank Ranae3926882011-12-26 09:47:54 +0530105static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530106static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +0530107static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530108
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530109static bool aca_id_turned_on;
David Keitel272ce522012-08-17 16:25:24 -0700110static bool legacy_power_supply;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530111static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530112{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530113#ifdef CONFIG_USB_MSM_ACA
114 return true;
115#else
116 return debug_aca_enabled;
117#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +0530118}
119
Mayank Rana0f286cf2013-02-27 11:43:27 +0530120static int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
Mayank Rana248698c2012-04-19 00:03:16 +0530121 { /* VDD_CX CORNER Voting */
122 [VDD_NONE] = RPM_VREG_CORNER_NONE,
123 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
124 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
125 },
126 { /* VDD_CX Voltage Voting */
127 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
128 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
129 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
130 },
131};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132
133static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
134{
135 int rc = 0;
136
137 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700138 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530139 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200140 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530141 return PTR_ERR(hsusb_3p3);
142 }
143
144 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
145 USB_PHY_3P3_VOL_MAX);
146 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700147 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700148 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530149 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530150 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700151 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530152 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200153 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530154 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700155 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530156 }
157 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
158 USB_PHY_1P8_VOL_MAX);
159 if (rc) {
Stephen Boyd9850acb2013-01-28 14:11:20 -0800160 dev_err(motg->phy.dev, "unable to set voltage level "
161 "for hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530162 goto put_1p8;
163 }
164
165 return 0;
166 }
167
Anji jonnala11aa5c42011-05-04 10:19:48 +0530168put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170put_3p3_lpm:
171 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530172 return rc;
173}
174
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530175static int msm_hsusb_config_vddcx(int high)
176{
Mayank Rana248698c2012-04-19 00:03:16 +0530177 struct msm_otg *motg = the_msm_otg;
178 enum usb_vdd_type vdd_type = motg->vdd_type;
179 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530180 int min_vol;
181 int ret;
182
Mayank Rana248698c2012-04-19 00:03:16 +0530183 min_vol = vdd_val[vdd_type][!!high];
Mayank Rana0f286cf2013-02-27 11:43:27 +0530184 ret = regulator_set_voltage(hsusb_vdd, min_vol, max_vol);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530185 if (ret) {
186 pr_err("%s: unable to set the voltage for regulator "
187 "HSUSB_VDDCX\n", __func__);
188 return ret;
189 }
190
191 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
192
193 return ret;
194}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530195
Amit Blay81801aa2012-09-19 12:08:12 +0200196static int msm_hsusb_ldo_enable(struct msm_otg *motg,
197 enum msm_otg_phy_reg_mode mode)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530198{
199 int ret = 0;
200
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530201 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530202 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
203 return -ENODEV;
204 }
205
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530206 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
208 return -ENODEV;
209 }
210
Amit Blay81801aa2012-09-19 12:08:12 +0200211 switch (mode) {
212 case USB_PHY_REG_ON:
Anji jonnala11aa5c42011-05-04 10:19:48 +0530213 ret = regulator_set_optimum_mode(hsusb_1p8,
214 USB_PHY_1P8_HPM_LOAD);
215 if (ret < 0) {
Stephen Boyd9850acb2013-01-28 14:11:20 -0800216 pr_err("%s: Unable to set HPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530217 "HSUSB_1p8\n", __func__);
218 return ret;
219 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220
221 ret = regulator_enable(hsusb_1p8);
222 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700223 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 __func__);
225 regulator_set_optimum_mode(hsusb_1p8, 0);
226 return ret;
227 }
228
Anji jonnala11aa5c42011-05-04 10:19:48 +0530229 ret = regulator_set_optimum_mode(hsusb_3p3,
230 USB_PHY_3P3_HPM_LOAD);
231 if (ret < 0) {
Stephen Boyd9850acb2013-01-28 14:11:20 -0800232 pr_err("%s: Unable to set HPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530233 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 regulator_set_optimum_mode(hsusb_1p8, 0);
235 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530236 return ret;
237 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238
239 ret = regulator_enable(hsusb_3p3);
240 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700241 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242 __func__);
243 regulator_set_optimum_mode(hsusb_3p3, 0);
244 regulator_set_optimum_mode(hsusb_1p8, 0);
245 regulator_disable(hsusb_1p8);
246 return ret;
247 }
248
Amit Blay81801aa2012-09-19 12:08:12 +0200249 break;
250
251 case USB_PHY_REG_OFF:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 ret = regulator_disable(hsusb_1p8);
253 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700254 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 __func__);
256 return ret;
257 }
258
259 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530260 if (ret < 0)
Stephen Boyd9850acb2013-01-28 14:11:20 -0800261 pr_err("%s: Unable to set LPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530262 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263
264 ret = regulator_disable(hsusb_3p3);
265 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700266 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 __func__);
268 return ret;
269 }
270 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530271 if (ret < 0)
Stephen Boyd9850acb2013-01-28 14:11:20 -0800272 pr_err("%s: Unable to set LPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530273 "HSUSB_3p3\n", __func__);
Amit Blay81801aa2012-09-19 12:08:12 +0200274
275 break;
276
277 case USB_PHY_REG_LPM_ON:
278 ret = regulator_set_optimum_mode(hsusb_1p8,
279 USB_PHY_1P8_LPM_LOAD);
280 if (ret < 0) {
281 pr_err("%s: Unable to set LPM of the regulator: HSUSB_1p8\n",
282 __func__);
283 return ret;
284 }
285
286 ret = regulator_set_optimum_mode(hsusb_3p3,
287 USB_PHY_3P3_LPM_LOAD);
288 if (ret < 0) {
289 pr_err("%s: Unable to set LPM of the regulator: HSUSB_3p3\n",
290 __func__);
291 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
292 return ret;
293 }
294
295 break;
296
297 case USB_PHY_REG_LPM_OFF:
298 ret = regulator_set_optimum_mode(hsusb_1p8,
299 USB_PHY_1P8_HPM_LOAD);
300 if (ret < 0) {
301 pr_err("%s: Unable to set HPM of the regulator: HSUSB_1p8\n",
302 __func__);
303 return ret;
304 }
305
306 ret = regulator_set_optimum_mode(hsusb_3p3,
307 USB_PHY_3P3_HPM_LOAD);
308 if (ret < 0) {
309 pr_err("%s: Unable to set HPM of the regulator: HSUSB_3p3\n",
310 __func__);
311 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
312 return ret;
313 }
314
315 break;
316
317 default:
318 pr_err("%s: Unsupported mode (%d).", __func__, mode);
319 return -ENOTSUPP;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530320 }
321
Amit Blay81801aa2012-09-19 12:08:12 +0200322 pr_debug("%s: USB reg mode (%d) (OFF/HPM/LPM)\n", __func__, mode);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530323 return ret < 0 ? ret : 0;
324}
325
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530326static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
327{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530328 struct msm_otg_platform_data *pdata = motg->pdata;
329
330 if (!pdata->mhl_enable)
331 return;
332
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530333 if (!mhl_usb_hs_switch) {
334 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530335 return;
336 }
337
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530338 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530339 if (regulator_enable(mhl_usb_hs_switch))
340 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530341 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530342 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530343 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530344}
345
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200346static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530347{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200348 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530349 int cnt = 0;
350
351 /* initiate read operation */
352 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
353 USB_ULPI_VIEWPORT);
354
355 /* wait for completion */
356 while (cnt < ULPI_IO_TIMEOUT_USEC) {
357 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
358 break;
359 udelay(1);
360 cnt++;
361 }
362
363 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200364 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530365 readl(USB_ULPI_VIEWPORT));
366 return -ETIMEDOUT;
367 }
368 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
369}
370
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200371static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530372{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200373 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530374 int cnt = 0;
375
376 /* initiate write operation */
377 writel(ULPI_RUN | ULPI_WRITE |
378 ULPI_ADDR(reg) | ULPI_DATA(val),
379 USB_ULPI_VIEWPORT);
380
381 /* wait for completion */
382 while (cnt < ULPI_IO_TIMEOUT_USEC) {
383 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
384 break;
385 udelay(1);
386 cnt++;
387 }
388
389 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200390 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530391 return -ETIMEDOUT;
392 }
393 return 0;
394}
395
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200396static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530397 .read = ulpi_read,
398 .write = ulpi_write,
399};
400
401static void ulpi_init(struct msm_otg *motg)
402{
403 struct msm_otg_platform_data *pdata = motg->pdata;
Mayank Rana443f9e42012-09-21 18:32:39 +0530404 int aseq[10];
405 int *seq = NULL;
406
407 if (override_phy_init) {
408 pr_debug("%s(): HUSB PHY Init:%s\n", __func__,
409 override_phy_init);
410 get_options(override_phy_init, ARRAY_SIZE(aseq), aseq);
411 seq = &aseq[1];
412 } else {
413 seq = pdata->phy_init_seq;
414 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530415
416 if (!seq)
417 return;
418
419 while (seq[0] >= 0) {
Mayank Rana443f9e42012-09-21 18:32:39 +0530420 if (override_phy_init)
421 pr_debug("ulpi: write 0x%02x to 0x%02x\n",
422 seq[0], seq[1]);
423
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200424 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530425 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200426 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530427 seq += 2;
428 }
429}
430
431static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
432{
433 int ret;
434
435 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530436 if (!IS_ERR(motg->clk)) {
437 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
438 } else {
439 /* Using asynchronous block reset to the hardware */
440 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
441 clk_disable_unprepare(motg->pclk);
442 clk_disable_unprepare(motg->core_clk);
443 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
444 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530445 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200446 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530447 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530448 if (!IS_ERR(motg->clk)) {
449 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
450 } else {
451 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
452 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
453 ndelay(200);
454 clk_prepare_enable(motg->core_clk);
455 clk_prepare_enable(motg->pclk);
456 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530457 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200458 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530459 }
460 return ret;
461}
462
463static int msm_otg_phy_clk_reset(struct msm_otg *motg)
464{
465 int ret;
466
Amit Blay02eff132011-09-21 16:46:24 +0300467 if (IS_ERR(motg->phy_reset_clk))
468 return 0;
469
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530470 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
471 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200472 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530473 return ret;
474 }
475 usleep_range(10000, 12000);
476 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
477 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200478 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530479 return ret;
480}
481
482static int msm_otg_phy_reset(struct msm_otg *motg)
483{
484 u32 val;
485 int ret;
486 int retries;
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530487 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530488
489 ret = msm_otg_link_clk_reset(motg, 1);
490 if (ret)
491 return ret;
492 ret = msm_otg_phy_clk_reset(motg);
493 if (ret)
494 return ret;
Amit Blay58dc2bc2013-01-24 12:28:03 +0200495
496 /*
497 * 10 usec delay is required according to spec. Using larger value
498 * since the exact value proved to not work 100% of the time.
499 */
Bar Weinere7129cb2012-11-28 08:44:14 +0200500 if (IS_ERR(motg->phy_reset_clk))
Amit Blay58dc2bc2013-01-24 12:28:03 +0200501 usleep_range(100, 120);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530502 ret = msm_otg_link_clk_reset(motg, 0);
503 if (ret)
504 return ret;
505
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530506 if (pdata && pdata->enable_sec_phy)
507 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
508 USB_PHY_CTRL2);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530509 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
510 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
511
512 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200513 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530514 ULPI_CLR(ULPI_FUNC_CTRL));
515 if (!ret)
516 break;
517 ret = msm_otg_phy_clk_reset(motg);
518 if (ret)
519 return ret;
520 }
521 if (!retries)
522 return -ETIMEDOUT;
523
524 /* This reset calibrates the phy, if the above write succeeded */
525 ret = msm_otg_phy_clk_reset(motg);
526 if (ret)
527 return ret;
528
529 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200530 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530531 if (ret != -ETIMEDOUT)
532 break;
533 ret = msm_otg_phy_clk_reset(motg);
534 if (ret)
535 return ret;
536 }
537 if (!retries)
538 return -ETIMEDOUT;
539
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200540 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530541 return 0;
542}
543
544#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530545static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530546{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530547 int cnt = 0;
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530548 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530549
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530550 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530552 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530553 break;
554 udelay(1);
555 cnt++;
556 }
557 if (cnt >= LINK_RESET_TIMEOUT_USEC)
558 return -ETIMEDOUT;
559
560 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530561 writel_relaxed(0x80000000, USB_PORTSC);
562 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530563 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530564
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530565 if (pdata && pdata->enable_sec_phy)
566 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
567 USB_PHY_CTRL2);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530568 return 0;
569}
570
Steve Mucklef132c6c2012-06-06 18:30:57 -0700571static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530572{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700573 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530574 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530575 int ret;
576 u32 val = 0;
577 u32 ulpi_val = 0;
578
Ofir Cohen4da266f2012-01-03 10:19:29 +0200579 /*
580 * USB PHY and Link reset also reset the USB BAM.
581 * Thus perform reset operation only once to avoid
582 * USB BAM reset on other cases e.g. USB cable disconnections.
583 */
584 if (pdata->disable_reset_on_disconnect) {
585 if (motg->reset_counter)
586 return 0;
587 else
588 motg->reset_counter++;
589 }
590
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530591 if (!IS_ERR(motg->clk))
592 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530593 ret = msm_otg_phy_reset(motg);
594 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700595 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530596 return ret;
597 }
598
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530599 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530600 ret = msm_otg_link_reset(motg);
601 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700602 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530603 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530604 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530605 msleep(100);
606
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530607 ulpi_init(motg);
608
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700609 /* Ensure that RESET operation is completed before turning off clock */
610 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530611
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530612 if (!IS_ERR(motg->clk))
613 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530614
615 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530616 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530617 if (pdata->mode == USB_OTG) {
618 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
619 val |= OTGSC_IDIE | OTGSC_BSVIE;
620 } else if (pdata->mode == USB_PERIPHERAL) {
621 ulpi_val = ULPI_INT_SESS_VALID;
622 val |= OTGSC_BSVIE;
623 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530624 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200625 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
626 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530627 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700628 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530629 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530630 /* Enable PMIC pull-up */
631 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530632 }
633
634 return 0;
635}
636
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530637static const char *timer_string(int bit)
638{
639 switch (bit) {
640 case A_WAIT_VRISE: return "a_wait_vrise";
641 case A_WAIT_VFALL: return "a_wait_vfall";
642 case B_SRP_FAIL: return "b_srp_fail";
643 case A_WAIT_BCON: return "a_wait_bcon";
644 case A_AIDL_BDIS: return "a_aidl_bdis";
645 case A_BIDL_ADIS: return "a_bidl_adis";
646 case B_ASE0_BRST: return "b_ase0_brst";
647 case A_TST_MAINT: return "a_tst_maint";
648 case B_TST_SRP: return "b_tst_srp";
649 case B_TST_CONFIG: return "b_tst_config";
650 default: return "UNDEFINED";
651 }
652}
653
654static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
655{
656 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
657
658 switch (motg->active_tmout) {
659 case A_WAIT_VRISE:
660 /* TODO: use vbus_vld interrupt */
661 set_bit(A_VBUS_VLD, &motg->inputs);
662 break;
663 case A_TST_MAINT:
664 /* OTG PET: End session after TA_TST_MAINT */
665 set_bit(A_BUS_DROP, &motg->inputs);
666 break;
667 case B_TST_SRP:
668 /*
669 * OTG PET: Initiate SRP after TB_TST_SRP of
670 * previous session end.
671 */
672 set_bit(B_BUS_REQ, &motg->inputs);
673 break;
674 case B_TST_CONFIG:
675 clear_bit(A_CONN, &motg->inputs);
676 break;
677 default:
678 set_bit(motg->active_tmout, &motg->tmouts);
679 }
680
681 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
682 queue_work(system_nrt_wq, &motg->sm_work);
683 return HRTIMER_NORESTART;
684}
685
686static void msm_otg_del_timer(struct msm_otg *motg)
687{
688 int bit = motg->active_tmout;
689
690 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
691 div_s64(ktime_to_us(hrtimer_get_remaining(
692 &motg->timer)), 1000));
693 hrtimer_cancel(&motg->timer);
694 clear_bit(bit, &motg->tmouts);
695}
696
697static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
698{
699 clear_bit(bit, &motg->tmouts);
700 motg->active_tmout = bit;
701 pr_debug("starting %s timer\n", timer_string(bit));
702 hrtimer_start(&motg->timer,
703 ktime_set(time / 1000, (time % 1000) * 1000000),
704 HRTIMER_MODE_REL);
705}
706
707static void msm_otg_init_timer(struct msm_otg *motg)
708{
709 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
710 motg->timer.function = msm_otg_timer_func;
711}
712
Steve Mucklef132c6c2012-06-06 18:30:57 -0700713static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530714{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700715 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530716
Steve Mucklef132c6c2012-06-06 18:30:57 -0700717 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530718 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700719 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530720 return -EINVAL;
721 }
722
723 pr_debug("A-Host: HNP initiated\n");
724 clear_bit(A_BUS_REQ, &motg->inputs);
725 queue_work(system_nrt_wq, &motg->sm_work);
726 return 0;
727}
728
Steve Mucklef132c6c2012-06-06 18:30:57 -0700729static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530730{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700731 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530732 u32 val;
733 int ret = 0;
734
Steve Mucklef132c6c2012-06-06 18:30:57 -0700735 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530736 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700737 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530738 ret = -EINVAL;
739 goto out;
740 }
741
742 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
743 pr_debug("initial conditions of SRP are not met. Try again"
744 "after some time\n");
745 ret = -EAGAIN;
746 goto out;
747 }
748
749 pr_debug("B-Device SRP started\n");
750
751 /*
752 * PHY won't pull D+ high unless it detects Vbus valid.
753 * Since by definition, SRP is only done when Vbus is not valid,
754 * software work-around needs to be used to spoof the PHY into
755 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
756 * VBUSVLDEXT register bits.
757 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700758 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530759 /*
760 * Harware auto assist data pulsing: Data pulse is given
761 * for 7msec; wait for vbus
762 */
763 val = readl_relaxed(USB_OTGSC);
764 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
765
766 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
767out:
768 return ret;
769}
770
Steve Mucklef132c6c2012-06-06 18:30:57 -0700771static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530772{
773 struct usb_hcd *hcd = bus_to_hcd(otg->host);
774 struct usb_device *rhub = otg->host->root_hub;
775
776 if (enable) {
777 pm_runtime_disable(&rhub->dev);
778 rhub->state = USB_STATE_NOTATTACHED;
779 hcd->driver->bus_suspend(hcd);
780 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
781 } else {
782 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700783 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530784 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
785 }
786}
787
Steve Mucklef132c6c2012-06-06 18:30:57 -0700788static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530789{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700790 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530791
Amit Blay6fa647a2012-05-24 14:12:08 +0300792 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530793 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530794
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530795 /*
796 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
797 * during bus suspend/resume. Update the relevant state
798 * machine inputs and trigger LPM entry/exit. Checking
799 * in_lpm flag would avoid unnecessary work scheduling.
800 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530801 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700802 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530803 case OTG_STATE_A_WAIT_BCON:
804 if (TA_WAIT_BCON > 0)
805 break;
806 /* fall through */
807 case OTG_STATE_A_HOST:
808 pr_debug("host bus suspend\n");
809 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530810 if (!atomic_read(&motg->in_lpm))
811 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530812 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300813 case OTG_STATE_B_PERIPHERAL:
814 pr_debug("peripheral bus suspend\n");
815 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
816 break;
817 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530818 if (!atomic_read(&motg->in_lpm))
819 queue_delayed_work(system_nrt_wq,
820 &motg->suspend_work,
821 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300822 break;
823
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530824 default:
825 break;
826 }
827 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700828 switch (phy->state) {
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530829 case OTG_STATE_A_WAIT_BCON:
830 /* Remote wakeup or resume */
831 set_bit(A_BUS_REQ, &motg->inputs);
832 /* ensure hardware is not in low power mode */
833 if (atomic_read(&motg->in_lpm))
834 pm_runtime_resume(phy->dev);
835 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530836 case OTG_STATE_A_SUSPEND:
837 /* Remote wakeup or resume */
838 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700839 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700840
841 /* ensure hardware is not in low power mode */
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530842 if (atomic_read(&motg->in_lpm))
843 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530844 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300845 case OTG_STATE_B_PERIPHERAL:
846 pr_debug("peripheral bus resume\n");
847 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
848 break;
849 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530850 if (atomic_read(&motg->in_lpm))
851 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300852 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530853 default:
854 break;
855 }
856 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530857 return 0;
858}
859
Manu Gautame3a39082013-06-11 10:42:56 +0530860static void msm_otg_bus_vote(struct msm_otg *motg, enum usb_bus_vote vote)
861{
862 int ret;
863 struct msm_otg_platform_data *pdata = motg->pdata;
864
865 /* Check if target allows min_vote to be same as no_vote */
Ido Shayevitzcf050e22013-07-22 17:31:48 +0300866 if (pdata->bus_scale_table &&
867 vote >= pdata->bus_scale_table->num_usecases)
Manu Gautame3a39082013-06-11 10:42:56 +0530868 vote = USB_NO_PERF_VOTE;
869
870 if (motg->bus_perf_client) {
871 ret = msm_bus_scale_client_update_request(
872 motg->bus_perf_client, vote);
873 if (ret)
874 dev_err(motg->phy.dev, "%s: Failed to vote (%d)\n"
875 "for bus bw %d\n", __func__, vote, ret);
876 }
877}
878
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530879#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530880#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
881
882#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530883static int msm_otg_suspend(struct msm_otg *motg)
884{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200885 struct usb_phy *phy = &motg->phy;
886 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530887 struct msm_otg_platform_data *pdata = motg->pdata;
888 int cnt = 0;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530889 bool host_bus_suspend, device_bus_suspend, dcp, prop_charger;
zhenhuahb6d5edc2013-06-19 14:37:32 +0800890 bool floated_charger;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530891 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800892 unsigned ret;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +0530893 u32 portsc, config2;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530894
895 if (atomic_read(&motg->in_lpm))
896 return 0;
897
Lena Salmanabde35d2013-04-25 15:29:43 +0300898 if (motg->pdata->delay_lpm_hndshk_on_disconnect && !msm_bam_lpm_ok())
Lena Salman05b544f2013-05-13 15:49:10 +0300899 return -EBUSY;
Lena Salmanabde35d2013-04-25 15:29:43 +0300900
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530901 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530902 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
903 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700904 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300905 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
906 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530907 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530908 prop_charger = motg->chg_type == USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +0800909 floated_charger = motg->chg_type == USB_FLOATED_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700910
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +0530911 /* Enable line state difference wakeup fix for only device and host
912 * bus suspend scenarios. Otherwise PHY can not be suspended when
913 * a charger that pulls DP/DM high is connected.
914 */
915 config2 = readl_relaxed(USB_GENCONFIG2);
916 if (device_bus_suspend)
917 config2 |= GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
918 else
919 config2 &= ~GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
920 writel_relaxed(config2, USB_GENCONFIG2);
921
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530922 /*
923 * Abort suspend when,
924 * 1. charging detection in progress due to cable plug-in
925 * 2. host mode activation in progress due to Micro-A cable insertion
926 */
927
928 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
zhenhuahb6d5edc2013-06-19 14:37:32 +0800929 !dcp && !prop_charger && !floated_charger) ||
930 test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham502bea32012-08-13 15:34:20 -0700931 enable_irq(motg->irq);
932 return -EBUSY;
933 }
934
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530935 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530936 * Chipidea 45-nm PHY suspend sequence:
937 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530938 * Interrupt Latch Register auto-clear feature is not present
939 * in all PHY versions. Latch register is clear on read type.
940 * Clear latch register to avoid spurious wakeup from
941 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530942 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530943 * PHY comparators are disabled when PHY enters into low power
944 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
945 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
946 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530947 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530948 * PLL is not turned off when PHY enters into low power mode (LPM).
949 * Disable PLL for maximum power savings.
950 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530951
952 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200953 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530954 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200955 ulpi_write(phy, 0x01, 0x30);
956 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530957 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530958
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700959
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530960 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530961 * PHY may take some time or even fail to enter into low power
962 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
963 * in failure case.
964 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530965 portsc = readl_relaxed(USB_PORTSC);
966 if (!(portsc & PORTSC_PHCD)) {
967 writel_relaxed(portsc | PORTSC_PHCD,
968 USB_PORTSC);
969 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
970 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
971 break;
972 udelay(1);
973 cnt++;
974 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530975 }
976
977 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200978 dev_err(phy->dev, "Unable to suspend PHY\n");
979 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530980 enable_irq(motg->irq);
981 return -ETIMEDOUT;
982 }
983
984 /*
985 * PHY has capability to generate interrupt asynchronously in low
986 * power mode (LPM). This interrupt is level triggered. So USB IRQ
987 * line must be disabled till async interrupt enable bit is cleared
988 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
989 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530990 *
991 * PHY retention mode is disallowed while entering to LPM with wall
992 * charger connected. But PHY is put into suspend mode. Hence
993 * enable asynchronous interrupt to detect charger disconnection when
994 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530995 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530996 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300997 if (host_bus_suspend || device_bus_suspend ||
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530998 (motg->pdata->otg_control == OTG_PHY_CONTROL))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530999 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
1000 else
1001 cmd_val |= ULPI_STP_CTRL;
1002 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301003
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301004 /*
1005 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
1006 * PHY retention and collapse can not happen with VDP_SRC enabled.
1007 */
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301008 if (motg->caps & ALLOW_PHY_RETENTION && !device_bus_suspend && !dcp &&
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05301009 (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION)
1010 && (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS))))) {
Amit Blay58b31472011-11-18 09:39:39 +02001011 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05301012 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +02001013 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05301014 if ((motg->pdata->mode == USB_OTG) ||
1015 (motg->pdata->mode == USB_HOST))
1016 phy_ctrl_val |= (PHY_IDHV_INTEN |
1017 PHY_OTGSESSVLDHV_INTEN);
1018 else
1019 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
1020 }
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301021 if (host_bus_suspend)
1022 phy_ctrl_val |= PHY_CLAMP_DPDMSE_EN;
Amit Blay58b31472011-11-18 09:39:39 +02001023 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301025 }
1026
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001027 /* Ensure that above operation is completed before turning off clocks */
1028 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001029 /* Consider clocks on workaround flag only in case of bus suspend */
1030 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
1031 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
1032 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001033 clk_disable_unprepare(motg->pclk);
1034 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001035 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001036 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301037
Anji jonnala7da3f262011-12-02 17:22:14 -08001038 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05301039 if (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION) &&
1040 (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS)))) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301041 if (!IS_ERR(motg->xo_clk)) {
1042 clk_disable_unprepare(motg->xo_clk);
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301043 motg->lpm_flags |= XO_SHUTDOWN;
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301044 } else {
1045 ret = msm_xo_mode_vote(motg->xo_handle,
1046 MSM_XO_MODE_OFF);
1047 if (ret)
1048 dev_err(phy->dev, "%s fail to devote XO %d\n",
1049 __func__, ret);
1050 else
1051 motg->lpm_flags |= XO_SHUTDOWN;
1052 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301053 }
Anji jonnala7da3f262011-12-02 17:22:14 -08001054
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301055 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
1056 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +02001057 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001058 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001059 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
1060 !host_bus_suspend && !device_bus_suspend && !dcp) {
1061 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
1062 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +05301063 }
1064
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301065 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301067 msm_hsusb_mhl_switch_enable(motg, 0);
1068 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069
Steve Mucklef132c6c2012-06-06 18:30:57 -07001070 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001071 if (motg->async_irq)
1072 enable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001073 else
1074 enable_irq_wake(motg->irq);
1075
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076 if (motg->pdata->pmic_id_irq)
1077 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001078 if (pdata->otg_control == OTG_PHY_CONTROL &&
1079 pdata->mpm_otgsessvld_int)
1080 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301081 if (host_bus_suspend && pdata->mpm_dpshv_int)
1082 msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 1);
1083 if (host_bus_suspend && pdata->mpm_dmshv_int)
1084 msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001085 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301086 if (bus)
1087 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1088
Manu Gautame3a39082013-06-11 10:42:56 +05301089 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
1090
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301091 motg->host_bus_suspend = host_bus_suspend;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301092 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001093 /* Enable ASYNC IRQ (if present) during LPM */
1094 if (motg->async_irq)
1095 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301096 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301098
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001099 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301100
1101 return 0;
1102}
1103
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301104static int msm_otg_resume(struct msm_otg *motg)
1105{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001106 struct usb_phy *phy = &motg->phy;
1107 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001108 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301109 int cnt = 0;
1110 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001111 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001112 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301113
1114 if (!atomic_read(&motg->in_lpm))
1115 return 0;
1116
Ido Shayevitz44ca03c2013-07-09 15:57:21 +03001117 if (motg->pdata->delay_lpm_hndshk_on_disconnect)
1118 msm_bam_notify_lpm_resume();
1119
Jack Pham8978b892012-10-17 16:31:39 -07001120 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301122
Manu Gautame3a39082013-06-11 10:42:56 +05301123 /* Some platforms require BUS vote to enable/disable clocks */
1124 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
1125
Anji jonnala7da3f262011-12-02 17:22:14 -08001126 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301127 if (motg->lpm_flags & XO_SHUTDOWN) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301128 if (!IS_ERR(motg->xo_clk)) {
1129 clk_prepare_enable(motg->xo_clk);
1130 } else {
1131 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1132 if (ret)
1133 dev_err(phy->dev, "%s fail to vote for XO %d\n",
1134 __func__, ret);
1135 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301136 motg->lpm_flags &= ~XO_SHUTDOWN;
1137 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301138
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001139 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001140 clk_prepare_enable(motg->core_clk);
1141 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001142 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001143 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301144
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001146 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001147 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001148 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1149 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1150 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151 }
1152
1153 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301154 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301155 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001156 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1157 phy_ctrl_val |= PHY_RETEN;
1158 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1159 /* Disable PHY HV interrupts */
1160 phy_ctrl_val &=
1161 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301162 phy_ctrl_val &= ~(PHY_CLAMP_DPDMSE_EN);
Amit Blay58b31472011-11-18 09:39:39 +02001163 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301165 }
1166
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301167 temp = readl(USB_USBCMD);
1168 temp &= ~ASYNC_INTR_CTRL;
1169 temp &= ~ULPI_STP_CTRL;
1170 writel(temp, USB_USBCMD);
1171
1172 /*
1173 * PHY comes out of low power mode (LPM) in case of wakeup
1174 * from asynchronous interrupt.
1175 */
1176 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1177 goto skip_phy_resume;
1178
1179 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1180 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1181 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1182 break;
1183 udelay(1);
1184 cnt++;
1185 }
1186
1187 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1188 /*
1189 * This is a fatal error. Reset the link and
1190 * PHY. USB state can not be restored. Re-insertion
1191 * of USB cable is the only way to get USB working.
1192 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001193 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301194 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001195 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301196 }
1197
1198skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001199 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001200 if (motg->async_irq)
1201 disable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001202 else
1203 disable_irq_wake(motg->irq);
1204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205 if (motg->pdata->pmic_id_irq)
1206 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001207 if (pdata->otg_control == OTG_PHY_CONTROL &&
1208 pdata->mpm_otgsessvld_int)
1209 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301210 if (motg->host_bus_suspend && pdata->mpm_dpshv_int)
1211 msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 0);
1212 if (motg->host_bus_suspend && pdata->mpm_dmshv_int)
1213 msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301215 if (bus)
1216 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1217
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301218 atomic_set(&motg->in_lpm, 0);
1219
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301220 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001221 /* Match the disable_irq call from ISR */
1222 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301223 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301224 }
Jack Pham8978b892012-10-17 16:31:39 -07001225 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301226
Manu Gautamf8c45642012-08-10 10:20:56 -07001227 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1228 if (motg->async_irq)
1229 disable_irq(motg->async_irq);
1230
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001231 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301232
1233 return 0;
1234}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301235#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301236
David Keitel272ce522012-08-17 16:25:24 -07001237static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001238{
Jack Pham0c695282012-10-19 18:13:03 -07001239 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001240 pr_err("No USB power supply registered!\n");
1241 return;
1242 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001243
Jack Pham0c695282012-10-19 18:13:03 -07001244 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001245 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301246 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001247 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301248 } else {
David Keitel272ce522012-08-17 16:25:24 -07001249 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301250 /*
1251 * VBUS comparator is disabled by PMIC charging driver
1252 * when SYSTEM scope is selected. For ID_GND->ID_A
1253 * transition, give 50 msec delay so that PMIC charger
1254 * driver detect the VBUS and ready for accepting
1255 * charging current value from USB.
1256 */
1257 if (test_bit(ID_A, &motg->inputs))
1258 msleep(50);
1259 }
David Keitel272ce522012-08-17 16:25:24 -07001260 } else {
1261 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001262 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001263 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001264}
1265
David Keitel081a3e22012-04-18 12:37:07 -07001266static int msm_otg_notify_chg_type(struct msm_otg *motg)
1267{
1268 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001269
David Keitel081a3e22012-04-18 12:37:07 -07001270 /*
1271 * TODO
1272 * Unify OTG driver charger types and power supply charger types
1273 */
1274 if (charger_type == motg->chg_type)
1275 return 0;
1276
1277 if (motg->chg_type == USB_SDP_CHARGER)
1278 charger_type = POWER_SUPPLY_TYPE_USB;
1279 else if (motg->chg_type == USB_CDP_CHARGER)
1280 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301281 else if (motg->chg_type == USB_DCP_CHARGER ||
1282 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001283 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1284 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1285 motg->chg_type == USB_ACA_A_CHARGER ||
1286 motg->chg_type == USB_ACA_B_CHARGER ||
1287 motg->chg_type == USB_ACA_C_CHARGER))
1288 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1289 else
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301290 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001291
Jack Pham0c695282012-10-19 18:13:03 -07001292 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001293 pr_err("No USB power supply registered!\n");
1294 return -EINVAL;
1295 }
1296
1297 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001298 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001299 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001300}
1301
Amit Blay0f7edf72012-01-15 10:11:27 +02001302static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1303{
Jack Pham0c695282012-10-19 18:13:03 -07001304 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001305 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1306 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001307 }
1308
David Keitel272ce522012-08-17 16:25:24 -07001309 if (motg->cur_power == 0 && mA > 2) {
1310 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001311 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001312 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001313 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001314 goto psy_error;
1315 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1316 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001317 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001318 goto psy_error;
1319 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001320 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001321 goto psy_error;
Manu Gautamfca298c2013-06-05 15:11:54 +05301322 } else {
1323 if (power_supply_set_online(psy, true))
1324 goto psy_error;
1325 /* Current has changed (100/2 --> 500) */
1326 if (power_supply_set_current_limit(psy, 1000*mA))
1327 goto psy_error;
David Keitel272ce522012-08-17 16:25:24 -07001328 }
Manu Gautamfca298c2013-06-05 15:11:54 +05301329
Jack Pham0c695282012-10-19 18:13:03 -07001330 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001331 return 0;
1332
David Keitel272ce522012-08-17 16:25:24 -07001333psy_error:
1334 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001335 return -ENXIO;
1336}
1337
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301338static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1339{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001340 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301341
1342 if (g && g->is_a_peripheral)
1343 return;
1344
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301345 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1346 motg->chg_type == USB_ACA_A_CHARGER ||
1347 motg->chg_type == USB_ACA_B_CHARGER ||
1348 motg->chg_type == USB_ACA_C_CHARGER) &&
1349 mA > IDEV_ACA_CHG_LIMIT)
1350 mA = IDEV_ACA_CHG_LIMIT;
1351
David Keitel081a3e22012-04-18 12:37:07 -07001352 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001353 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001354 "Failed notifying %d charger type to PMIC\n",
1355 motg->chg_type);
1356
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301357 if (motg->cur_power == mA)
1358 return;
1359
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001360 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001361
1362 /*
1363 * Use Power Supply API if supported, otherwise fallback
1364 * to legacy pm8921 API.
1365 */
1366 if (msm_otg_notify_power_supply(motg, mA))
1367 pm8921_charger_vbus_draw(mA);
1368
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301369 motg->cur_power = mA;
1370}
1371
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001372static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301373{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001374 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301375
1376 /*
1377 * Gadget driver uses set_power method to notify about the
1378 * available current based on suspend/configured states.
1379 *
1380 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1381 * states when CDP/ACA is connected.
1382 */
1383 if (motg->chg_type == USB_SDP_CHARGER)
1384 msm_otg_notify_charger(motg, mA);
1385
1386 return 0;
1387}
1388
Steve Mucklef132c6c2012-06-06 18:30:57 -07001389static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301390{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001391 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301392 struct msm_otg_platform_data *pdata = motg->pdata;
1393 struct usb_hcd *hcd;
1394
1395 if (!otg->host)
1396 return;
1397
1398 hcd = bus_to_hcd(otg->host);
1399
1400 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001401 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301402
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301403 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001404 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301405 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1406
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301407 /*
1408 * Some boards have a switch cotrolled by gpio
1409 * to enable/disable internal HUB. Enable internal
1410 * HUB before kicking the host.
1411 */
1412 if (pdata->setup_gpio)
1413 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301414 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301415 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001416 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301417
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301418 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301419 /* HCD core reset all bits of PORTSC. select ULPI phy */
1420 writel_relaxed(0x80000000, USB_PORTSC);
1421
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301422 if (pdata->setup_gpio)
1423 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301424
1425 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001426 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301427 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301428 }
1429}
1430
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431static int msm_otg_usbdev_notify(struct notifier_block *self,
1432 unsigned long action, void *priv)
1433{
1434 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001435 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301436 struct usb_device *udev = priv;
1437
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301438 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1439 goto out;
1440
Steve Mucklef132c6c2012-06-06 18:30:57 -07001441 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301442 goto out;
1443 /*
1444 * Interested in devices connected directly to the root hub.
1445 * ACA dock can supply IDEV_CHG irrespective devices connected
1446 * on the accessory port.
1447 */
1448 if (!udev->parent || udev->parent->parent ||
1449 motg->chg_type == USB_ACA_DOCK_CHARGER)
1450 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001451
1452 switch (action) {
1453 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301454 if (aca_enabled())
1455 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001456 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301457 pr_debug("B_CONN set\n");
1458 set_bit(B_CONN, &motg->inputs);
1459 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001460 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301461 /*
1462 * OTG PET: A-device must end session within
1463 * 10 sec after PET enumeration.
1464 */
1465 if (udev->quirks & USB_QUIRK_OTG_PET)
1466 msm_otg_start_timer(motg, TA_TST_MAINT,
1467 A_TST_MAINT);
1468 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301469 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001470 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001471 if (udev->actconfig)
1472 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1473 else
1474 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001475 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301476 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301477 break;
1478 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001479 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1480 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301481 pr_debug("B_CONN clear\n");
1482 clear_bit(B_CONN, &motg->inputs);
1483 /*
1484 * OTG PET: A-device must end session after
1485 * PET disconnection if it is enumerated
1486 * with bcdDevice[0] = 1. USB core sets
1487 * bus->otg_vbus_off for us. clear it here.
1488 */
1489 if (udev->bus->otg_vbus_off) {
1490 udev->bus->otg_vbus_off = 0;
1491 set_bit(A_BUS_DROP, &motg->inputs);
1492 }
1493 queue_work(system_nrt_wq, &motg->sm_work);
1494 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001495 default:
1496 break;
1497 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301498 if (test_bit(ID_A, &motg->inputs))
1499 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1500 motg->mA_port);
1501out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001502 return NOTIFY_OK;
1503}
1504
Mayank Ranae3926882011-12-26 09:47:54 +05301505static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1506{
1507 int ret;
1508 static bool vbus_is_on;
1509
1510 if (vbus_is_on == on)
1511 return;
1512
1513 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301514 ret = motg->pdata->vbus_power(on);
1515 if (!ret)
1516 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301517 return;
1518 }
1519
1520 if (!vbus_otg) {
1521 pr_err("vbus_otg is NULL.");
1522 return;
1523 }
1524
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001525 /*
1526 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001527 * from usb before turning on the boost.
1528 * if exiting host mode disable the boost before enabling to draw
1529 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001530 */
Mayank Ranae3926882011-12-26 09:47:54 +05301531 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001532 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301533 ret = regulator_enable(vbus_otg);
1534 if (ret) {
1535 pr_err("unable to enable vbus_otg\n");
1536 return;
1537 }
1538 vbus_is_on = true;
1539 } else {
1540 ret = regulator_disable(vbus_otg);
1541 if (ret) {
1542 pr_err("unable to disable vbus_otg\n");
1543 return;
1544 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001545 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301546 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301547 }
1548}
1549
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001550static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301551{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001552 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301553 struct usb_hcd *hcd;
1554
1555 /*
1556 * Fail host registration if this board can support
1557 * only peripheral configuration.
1558 */
1559 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001560 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301561 return -ENODEV;
1562 }
1563
Mayank Ranae3926882011-12-26 09:47:54 +05301564 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001565 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301566 if (IS_ERR(vbus_otg)) {
1567 pr_err("Unable to get vbus_otg\n");
Manu Gautam139a8132013-06-04 16:46:50 +05301568 return PTR_ERR(vbus_otg);
Mayank Ranae3926882011-12-26 09:47:54 +05301569 }
1570 }
1571
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301572 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001573 if (otg->phy->state == OTG_STATE_A_HOST) {
1574 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001575 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301576 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301577 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301578 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001579 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301580 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301581 } else {
1582 otg->host = NULL;
1583 }
1584
1585 return 0;
1586 }
1587
1588 hcd = bus_to_hcd(host);
1589 hcd->power_budget = motg->pdata->power_budget;
1590
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301591#ifdef CONFIG_USB_OTG
1592 host->otg_port = 1;
1593#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001594 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1595 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301596 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001597 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301598
1599 /*
1600 * Kick the state machine work, if peripheral is not supported
1601 * or peripheral is already registered with us.
1602 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301603 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001604 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301605 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301606 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301607
1608 return 0;
1609}
1610
Steve Mucklef132c6c2012-06-06 18:30:57 -07001611static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301612{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001613 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301614 struct msm_otg_platform_data *pdata = motg->pdata;
1615
1616 if (!otg->gadget)
1617 return;
1618
1619 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001620 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301621 /*
1622 * Some boards have a switch cotrolled by gpio
1623 * to enable/disable internal HUB. Disable internal
1624 * HUB before kicking the gadget.
1625 */
1626 if (pdata->setup_gpio)
1627 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001628
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301629 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautame3a39082013-06-11 10:42:56 +05301630 if (debug_bus_voting_enabled)
1631 msm_otg_bus_vote(motg, USB_MAX_PERF_VOTE);
1632
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301633 usb_gadget_vbus_connect(otg->gadget);
1634 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001635 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301636 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301637 /* Configure BUS performance parameters to default */
Manu Gautame3a39082013-06-11 10:42:56 +05301638 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301639 if (pdata->setup_gpio)
1640 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1641 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301642}
1643
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001644static int msm_otg_set_peripheral(struct usb_otg *otg,
Stephen Boyd9850acb2013-01-28 14:11:20 -08001645 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301646{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001647 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301648
1649 /*
1650 * Fail peripheral registration if this board can support
1651 * only host configuration.
1652 */
1653 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001654 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301655 return -ENODEV;
1656 }
1657
1658 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001659 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1660 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301661 msm_otg_start_peripheral(otg, 0);
1662 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001663 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301664 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301665 } else {
1666 otg->gadget = NULL;
1667 }
1668
1669 return 0;
1670 }
1671 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001672 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301673
1674 /*
1675 * Kick the state machine work, if host is not supported
1676 * or host is already registered with us.
1677 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301678 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001679 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301680 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301681 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301682
1683 return 0;
1684}
1685
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301686static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1687{
1688 unsigned long flags;
1689 int id;
1690
1691 if (!motg->pdata->pmic_id_irq)
1692 return -ENODEV;
1693
1694 local_irq_save(flags);
1695 id = irq_read_line(motg->pdata->pmic_id_irq);
1696 local_irq_restore(flags);
1697
1698 /*
1699 * If we can not read ID line state for some reason, treat
1700 * it as float. This would prevent MHL discovery and kicking
1701 * host mode unnecessarily.
1702 */
1703 return !!id;
1704}
1705
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301706static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1707 void (*callback)(int on))
1708{
1709 struct usb_phy *phy = &motg->phy;
1710 int ret;
1711
Manoj Raoa7bddd12012-08-27 20:36:45 -07001712 if (!motg->pdata->mhl_enable) {
1713 dev_dbg(phy->dev, "MHL feature not enabled\n");
1714 return -ENODEV;
1715 }
1716
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301717 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1718 !motg->pdata->pmic_id_irq) {
1719 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1720 return -ENODEV;
1721 }
1722
1723 if (!motg->pdata->mhl_dev_name) {
1724 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1725 return -ENODEV;
1726 }
1727
1728 if (callback)
1729 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1730 callback);
1731 else
1732 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1733
1734 if (ret)
1735 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1736 motg->pdata->mhl_dev_name, ret);
1737 else
1738 motg->mhl_enabled = true;
1739
1740 return ret;
1741}
1742
1743static void msm_otg_mhl_notify_online(int on)
1744{
1745 struct msm_otg *motg = the_msm_otg;
1746 struct usb_phy *phy = &motg->phy;
1747 bool queue = false;
1748
1749 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1750
1751 if (on) {
1752 set_bit(MHL, &motg->inputs);
1753 } else {
1754 clear_bit(MHL, &motg->inputs);
1755 queue = true;
1756 }
1757
1758 if (queue && phy->state != OTG_STATE_UNDEFINED)
1759 schedule_work(&motg->sm_work);
1760}
1761
1762static bool msm_otg_is_mhl(struct msm_otg *motg)
1763{
1764 struct usb_phy *phy = &motg->phy;
1765 int is_mhl, ret;
1766
1767 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1768 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1769 /*
1770 * MHL driver calls our callback saying that MHL connected
1771 * if RID_GND is detected. But at later part of discovery
1772 * it may figure out MHL is not connected and returns
1773 * false. Hence clear MHL input here.
1774 */
1775 clear_bit(MHL, &motg->inputs);
1776 dev_dbg(phy->dev, "MHL device not found\n");
1777 return false;
1778 }
1779
1780 set_bit(MHL, &motg->inputs);
1781 dev_dbg(phy->dev, "MHL device found\n");
1782 return true;
1783}
1784
1785static bool msm_chg_mhl_detect(struct msm_otg *motg)
1786{
1787 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301788
1789 if (!motg->mhl_enabled)
1790 return false;
1791
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301792 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301793
1794 if (id)
1795 return false;
1796
1797 mhl_det_in_progress = true;
1798 ret = msm_otg_is_mhl(motg);
1799 mhl_det_in_progress = false;
1800
1801 return ret;
1802}
1803
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301804static void msm_otg_chg_check_timer_func(unsigned long data)
1805{
1806 struct msm_otg *motg = (struct msm_otg *) data;
1807 struct usb_otg *otg = motg->phy.otg;
1808
1809 if (atomic_read(&motg->in_lpm) ||
1810 !test_bit(B_SESS_VLD, &motg->inputs) ||
1811 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1812 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1813 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1814 return;
1815 }
1816
1817 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1818 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1819 set_bit(B_FALSE_SDP, &motg->inputs);
1820 queue_work(system_nrt_wq, &motg->sm_work);
1821 }
1822}
1823
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001824static bool msm_chg_aca_detect(struct msm_otg *motg)
1825{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001826 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001827 u32 int_sts;
1828 bool ret = false;
1829
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301830 if (!aca_enabled())
1831 goto out;
1832
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001833 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1834 goto out;
1835
Steve Mucklef132c6c2012-06-06 18:30:57 -07001836 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001837 switch (int_sts & 0x1C) {
1838 case 0x08:
1839 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001840 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001841 motg->chg_type = USB_ACA_A_CHARGER;
1842 motg->chg_state = USB_CHG_STATE_DETECTED;
1843 clear_bit(ID_B, &motg->inputs);
1844 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301845 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001846 ret = true;
1847 }
1848 break;
1849 case 0x0C:
1850 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001851 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001852 motg->chg_type = USB_ACA_B_CHARGER;
1853 motg->chg_state = USB_CHG_STATE_DETECTED;
1854 clear_bit(ID_A, &motg->inputs);
1855 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301856 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 ret = true;
1858 }
1859 break;
1860 case 0x10:
1861 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001862 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863 motg->chg_type = USB_ACA_C_CHARGER;
1864 motg->chg_state = USB_CHG_STATE_DETECTED;
1865 clear_bit(ID_A, &motg->inputs);
1866 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301867 set_bit(ID, &motg->inputs);
1868 ret = true;
1869 }
1870 break;
1871 case 0x04:
1872 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001873 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301874 motg->chg_type = USB_INVALID_CHARGER;
1875 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1876 clear_bit(ID_A, &motg->inputs);
1877 clear_bit(ID_B, &motg->inputs);
1878 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001879 ret = true;
1880 }
1881 break;
1882 default:
1883 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1884 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301885 test_and_clear_bit(ID_C, &motg->inputs) |
1886 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001887 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001888 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001889 motg->chg_type = USB_INVALID_CHARGER;
1890 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1891 }
1892 }
1893out:
1894 return ret;
1895}
1896
1897static void msm_chg_enable_aca_det(struct msm_otg *motg)
1898{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001899 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001900
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301901 if (!aca_enabled())
1902 return;
1903
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001904 switch (motg->pdata->phy_type) {
1905 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301906 /* Disable ID_GND in link and PHY */
1907 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1908 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001909 ulpi_write(phy, 0x01, 0x0C);
1910 ulpi_write(phy, 0x10, 0x0F);
1911 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301912 /* Disable PMIC ID pull-up */
1913 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301914 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001915 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301916 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001917 break;
1918 default:
1919 break;
1920 }
1921}
1922
1923static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1924{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001925 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001926
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301927 if (!aca_enabled())
1928 return;
1929
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930 switch (motg->pdata->phy_type) {
1931 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301932 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001933 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301934 break;
1935 default:
1936 break;
1937 }
1938}
1939
1940static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1941{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001942 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301943
1944 if (!aca_enabled())
1945 return;
1946
1947 switch (motg->pdata->phy_type) {
1948 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001949 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001950 break;
1951 default:
1952 break;
1953 }
1954}
1955
1956static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1957{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001958 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001959 bool ret = false;
1960
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301961 if (!aca_enabled())
1962 return ret;
1963
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001964 switch (motg->pdata->phy_type) {
1965 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001966 if (ulpi_read(phy, 0x91) & 1) {
1967 dev_dbg(phy->dev, "RID change\n");
1968 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001969 ret = msm_chg_aca_detect(motg);
1970 }
1971 default:
1972 break;
1973 }
1974 return ret;
1975}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301976
1977static void msm_otg_id_timer_func(unsigned long data)
1978{
1979 struct msm_otg *motg = (struct msm_otg *) data;
1980
1981 if (!aca_enabled())
1982 return;
1983
1984 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001985 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301986 return;
1987 }
1988
Steve Mucklef132c6c2012-06-06 18:30:57 -07001989 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301990 goto out;
1991
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301992 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001993 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301994 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301995 }
1996
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301997out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301998 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1999 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
2000}
2001
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302002static bool msm_chg_check_secondary_det(struct msm_otg *motg)
2003{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002004 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302005 u32 chg_det;
2006 bool ret = false;
2007
2008 switch (motg->pdata->phy_type) {
2009 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002010 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302011 ret = chg_det & (1 << 4);
2012 break;
2013 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002014 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302015 ret = chg_det & 1;
2016 break;
2017 default:
2018 break;
2019 }
2020 return ret;
2021}
2022
2023static void msm_chg_enable_secondary_det(struct msm_otg *motg)
2024{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002025 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302026 u32 chg_det;
2027
2028 switch (motg->pdata->phy_type) {
2029 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002030 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302031 /* Turn off charger block */
2032 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002033 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302034 udelay(20);
2035 /* control chg block via ULPI */
2036 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002037 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302038 /* put it in host mode for enabling D- source */
2039 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002040 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302041 /* Turn on chg detect block */
2042 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002043 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302044 udelay(20);
2045 /* enable chg detection */
2046 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002047 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302048 break;
2049 case SNPS_28NM_INTEGRATED_PHY:
2050 /*
2051 * Configure DM as current source, DP as current sink
2052 * and enable battery charging comparators.
2053 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002054 ulpi_write(phy, 0x8, 0x85);
2055 ulpi_write(phy, 0x2, 0x85);
2056 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302057 break;
2058 default:
2059 break;
2060 }
2061}
2062
2063static bool msm_chg_check_primary_det(struct msm_otg *motg)
2064{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002065 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066 u32 chg_det;
2067 bool ret = false;
2068
2069 switch (motg->pdata->phy_type) {
2070 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002071 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302072 ret = chg_det & (1 << 4);
2073 break;
2074 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002075 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302076 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302077 /* Turn off VDP_SRC */
2078 ulpi_write(phy, 0x3, 0x86);
2079 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302080 break;
2081 default:
2082 break;
2083 }
2084 return ret;
2085}
2086
2087static void msm_chg_enable_primary_det(struct msm_otg *motg)
2088{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002089 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302090 u32 chg_det;
2091
2092 switch (motg->pdata->phy_type) {
2093 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002094 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302095 /* enable chg detection */
2096 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002097 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302098 break;
2099 case SNPS_28NM_INTEGRATED_PHY:
2100 /*
2101 * Configure DP as current source, DM as current sink
2102 * and enable battery charging comparators.
2103 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002104 ulpi_write(phy, 0x2, 0x85);
2105 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302106 break;
2107 default:
2108 break;
2109 }
2110}
2111
2112static bool msm_chg_check_dcd(struct msm_otg *motg)
2113{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002114 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302115 u32 line_state;
2116 bool ret = false;
2117
2118 switch (motg->pdata->phy_type) {
2119 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002120 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302121 ret = !(line_state & 1);
2122 break;
2123 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002124 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302125 ret = line_state & 2;
2126 break;
2127 default:
2128 break;
2129 }
2130 return ret;
2131}
2132
2133static void msm_chg_disable_dcd(struct msm_otg *motg)
2134{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002135 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302136 u32 chg_det;
2137
2138 switch (motg->pdata->phy_type) {
2139 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002140 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302141 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002142 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302143 break;
2144 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002145 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302146 break;
2147 default:
2148 break;
2149 }
2150}
2151
2152static void msm_chg_enable_dcd(struct msm_otg *motg)
2153{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002154 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302155 u32 chg_det;
2156
2157 switch (motg->pdata->phy_type) {
2158 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002159 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302160 /* Turn on D+ current source */
2161 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002162 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302163 break;
2164 case SNPS_28NM_INTEGRATED_PHY:
2165 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002166 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302167 break;
2168 default:
2169 break;
2170 }
2171}
2172
2173static void msm_chg_block_on(struct msm_otg *motg)
2174{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002175 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302176 u32 func_ctrl, chg_det;
2177
2178 /* put the controller in non-driving 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_NONDRIVING;
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 switch (motg->pdata->phy_type) {
2185 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002186 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302187 /* control chg block via ULPI */
2188 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002189 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302190 /* Turn on chg detect block */
2191 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002192 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302193 udelay(20);
2194 break;
2195 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302196 /* disable DP and DM pull down resistors */
2197 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302198 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002199 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302200 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002201 ulpi_write(phy, 0x1F, 0x92);
2202 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302203 udelay(100);
2204 break;
2205 default:
2206 break;
2207 }
2208}
2209
2210static void msm_chg_block_off(struct msm_otg *motg)
2211{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002212 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302213 u32 func_ctrl, chg_det;
2214
2215 switch (motg->pdata->phy_type) {
2216 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002217 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302218 /* Turn off charger block */
2219 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002220 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302221 break;
2222 case SNPS_28NM_INTEGRATED_PHY:
2223 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002224 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302225 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002226 ulpi_write(phy, 0x1F, 0x92);
2227 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetic4c69762013-07-08 15:03:26 +05302228 /* re-enable DP and DM pull down resistors */
2229 ulpi_write(phy, 0x6, 0xB);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302230 break;
2231 default:
2232 break;
2233 }
2234
2235 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002236 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302237 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2238 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002239 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302240}
2241
Anji jonnalad270e2d2011-08-09 11:28:32 +05302242static const char *chg_to_string(enum usb_chg_type chg_type)
2243{
2244 switch (chg_type) {
2245 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2246 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2247 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2248 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2249 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2250 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2251 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302252 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
zhenhuahb6d5edc2013-06-19 14:37:32 +08002253 case USB_FLOATED_CHARGER: return "USB_FLOATED_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302254 default: return "INVALID_CHARGER";
2255 }
2256}
2257
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302258#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2259#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302260#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2261#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302262static void msm_chg_detect_work(struct work_struct *w)
2263{
2264 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002265 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302266 bool is_dcd = false, tmout, vout, is_aca;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002267 static bool dcd;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302268 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302269 unsigned long delay;
2270
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002271 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302272
2273 if (test_bit(MHL, &motg->inputs)) {
2274 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2275 return;
2276 }
2277
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302278 switch (motg->chg_state) {
2279 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302280 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302281 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002282 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302283 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302284 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302285 delay = MSM_CHG_DCD_POLL_TIME;
2286 break;
2287 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302288 if (msm_chg_mhl_detect(motg)) {
2289 msm_chg_block_off(motg);
2290 motg->chg_state = USB_CHG_STATE_DETECTED;
2291 motg->chg_type = USB_INVALID_CHARGER;
2292 queue_work(system_nrt_wq, &motg->sm_work);
2293 return;
2294 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002295 is_aca = msm_chg_aca_detect(motg);
2296 if (is_aca) {
2297 /*
2298 * ID_A can be ACA dock too. continue
2299 * primary detection after DCD.
2300 */
2301 if (test_bit(ID_A, &motg->inputs)) {
2302 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2303 } else {
2304 delay = 0;
2305 break;
2306 }
2307 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302308 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302309 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2310 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302311 if (is_dcd || tmout) {
zhenhuahb6d5edc2013-06-19 14:37:32 +08002312 if (is_dcd)
2313 dcd = true;
2314 else
2315 dcd = false;
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302316 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302317 msm_chg_enable_primary_det(motg);
2318 delay = MSM_CHG_PRIMARY_DET_TIME;
2319 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2320 } else {
2321 delay = MSM_CHG_DCD_POLL_TIME;
2322 }
2323 break;
2324 case USB_CHG_STATE_DCD_DONE:
2325 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302326 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2327 dm_vlgc = line_state & PORTSC_LS_DM;
2328 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302329 if (test_bit(ID_A, &motg->inputs)) {
2330 motg->chg_type = USB_ACA_DOCK_CHARGER;
2331 motg->chg_state = USB_CHG_STATE_DETECTED;
2332 delay = 0;
2333 break;
2334 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302335 if (line_state) { /* DP > VLGC */
2336 motg->chg_type = USB_PROPRIETARY_CHARGER;
2337 motg->chg_state = USB_CHG_STATE_DETECTED;
2338 delay = 0;
2339 } else {
2340 msm_chg_enable_secondary_det(motg);
2341 delay = MSM_CHG_SECONDARY_DET_TIME;
2342 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2343 }
2344 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302345 if (test_bit(ID_A, &motg->inputs)) {
2346 motg->chg_type = USB_ACA_A_CHARGER;
2347 motg->chg_state = USB_CHG_STATE_DETECTED;
2348 delay = 0;
2349 break;
2350 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302351
2352 if (line_state) /* DP > VLGC or/and DM > VLGC */
2353 motg->chg_type = USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002354 else if (!dcd && floated_charger_enable)
2355 motg->chg_type = USB_FLOATED_CHARGER;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302356 else
2357 motg->chg_type = USB_SDP_CHARGER;
2358
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302359 motg->chg_state = USB_CHG_STATE_DETECTED;
2360 delay = 0;
2361 }
2362 break;
2363 case USB_CHG_STATE_PRIMARY_DONE:
2364 vout = msm_chg_check_secondary_det(motg);
2365 if (vout)
2366 motg->chg_type = USB_DCP_CHARGER;
2367 else
2368 motg->chg_type = USB_CDP_CHARGER;
2369 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2370 /* fall through */
2371 case USB_CHG_STATE_SECONDARY_DONE:
2372 motg->chg_state = USB_CHG_STATE_DETECTED;
2373 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302374 /*
2375 * Notify the charger type to power supply
2376 * owner as soon as we determine the charger.
2377 */
2378 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302379 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002380 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302381 /*
2382 * Spurious interrupt is seen after enabling ACA detection
2383 * due to which charger detection fails in case of PET.
2384 * Add delay of 100 microsec to avoid that.
2385 */
2386 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002387 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002388 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302389 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302390 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302391 return;
2392 default:
2393 return;
2394 }
2395
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302396 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302397}
2398
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302399/*
2400 * We support OTG, Peripheral only and Host only configurations. In case
2401 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2402 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2403 * enabled when switch is controlled by user and default mode is supplied
2404 * by board file, which can be changed by userspace later.
2405 */
2406static void msm_otg_init_sm(struct msm_otg *motg)
2407{
2408 struct msm_otg_platform_data *pdata = motg->pdata;
2409 u32 otgsc = readl(USB_OTGSC);
2410
2411 switch (pdata->mode) {
2412 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302414 if (pdata->default_mode == USB_HOST) {
2415 clear_bit(ID, &motg->inputs);
2416 } else if (pdata->default_mode == USB_PERIPHERAL) {
2417 set_bit(ID, &motg->inputs);
2418 set_bit(B_SESS_VLD, &motg->inputs);
2419 } else {
2420 set_bit(ID, &motg->inputs);
2421 clear_bit(B_SESS_VLD, &motg->inputs);
2422 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302423 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302424 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302425 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302426 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302427 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302428 set_bit(A_BUS_REQ, &motg->inputs);
2429 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002430 if (otgsc & OTGSC_BSV)
2431 set_bit(B_SESS_VLD, &motg->inputs);
2432 else
2433 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302434 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302435 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302436 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302437 set_bit(ID, &motg->inputs);
2438 else
2439 clear_bit(ID, &motg->inputs);
2440 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302441 /*
2442 * VBUS initial state is reported after PMIC
2443 * driver initialization. Wait for it.
2444 */
2445 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302446 }
2447 break;
2448 case USB_HOST:
2449 clear_bit(ID, &motg->inputs);
2450 break;
2451 case USB_PERIPHERAL:
2452 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302453 if (pdata->otg_control == OTG_PHY_CONTROL) {
2454 if (otgsc & OTGSC_BSV)
2455 set_bit(B_SESS_VLD, &motg->inputs);
2456 else
2457 clear_bit(B_SESS_VLD, &motg->inputs);
2458 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2459 /*
2460 * VBUS initial state is reported after PMIC
2461 * driver initialization. Wait for it.
2462 */
2463 wait_for_completion(&pmic_vbus_init);
2464 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302465 break;
2466 default:
2467 break;
2468 }
2469}
2470
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302471static void msm_otg_wait_for_ext_chg_done(struct msm_otg *motg)
2472{
2473 struct usb_phy *phy = &motg->phy;
2474 unsigned long t;
2475
2476 /*
2477 * Defer next cable connect event till external charger
2478 * detection is completed.
2479 */
2480
2481 if (motg->ext_chg_active) {
2482
2483 pr_debug("before msm_otg ext chg wait\n");
2484
2485 t = wait_for_completion_timeout(&motg->ext_chg_wait,
2486 msecs_to_jiffies(3000));
2487 if (!t)
2488 pr_err("msm_otg ext chg wait timeout\n");
2489 else
2490 pr_debug("msm_otg ext chg wait done\n");
2491 }
2492
2493 if (motg->ext_chg_opened) {
2494 if (phy->flags & ENABLE_DP_MANUAL_PULLUP) {
2495 ulpi_write(phy, ULPI_MISC_A_VBUSVLDEXT |
2496 ULPI_MISC_A_VBUSVLDEXTSEL,
2497 ULPI_CLR(ULPI_MISC_A));
2498 }
2499 /* clear charging register bits */
2500 ulpi_write(phy, 0x3F, 0x86);
2501 /* re-enable DP and DM pull-down resistors*/
2502 ulpi_write(phy, 0x6, 0xB);
2503 }
2504}
2505
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302506static void msm_otg_sm_work(struct work_struct *w)
2507{
2508 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002509 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302510 bool work = 0, srp_reqd, dcp;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302511
Steve Mucklef132c6c2012-06-06 18:30:57 -07002512 pm_runtime_resume(otg->phy->dev);
2513 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002514 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302515 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002516 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302517 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002518 if (!psy && legacy_power_supply) {
2519 psy = power_supply_get_by_name("usb");
2520
2521 if (!psy)
2522 pr_err("couldn't get usb power supply\n");
2523 }
2524
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002525 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302526 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2527 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002528 pm_runtime_put_noidle(otg->phy->dev);
2529 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302530 break;
2531 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302532 /* FALL THROUGH */
2533 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302534 if (test_bit(MHL, &motg->inputs)) {
2535 /* allow LPM */
2536 pm_runtime_put_noidle(otg->phy->dev);
2537 pm_runtime_suspend(otg->phy->dev);
2538 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002539 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302540 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302541 if (msm_chg_mhl_detect(motg)) {
2542 work = 1;
2543 break;
2544 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302545 clear_bit(B_BUS_REQ, &motg->inputs);
2546 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002547 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302548 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302549 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302550 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302551 switch (motg->chg_state) {
2552 case USB_CHG_STATE_UNDEFINED:
2553 msm_chg_detect_work(&motg->chg_work.work);
2554 break;
2555 case USB_CHG_STATE_DETECTED:
2556 switch (motg->chg_type) {
2557 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302558 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002559 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302560 if (motg->ext_chg_opened) {
2561 init_completion(
2562 &motg->ext_chg_wait);
2563 motg->ext_chg_active = true;
2564 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302565 /* fall through */
2566 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302567 msm_otg_notify_charger(motg,
2568 IDEV_CHG_MAX);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302569 pm_runtime_put_sync(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302570 break;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002571 case USB_FLOATED_CHARGER:
2572 msm_otg_notify_charger(motg,
2573 IDEV_CHG_MAX);
2574 pm_runtime_put_noidle(otg->phy->dev);
2575 pm_runtime_suspend(otg->phy->dev);
2576 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302577 case USB_ACA_B_CHARGER:
2578 msm_otg_notify_charger(motg,
2579 IDEV_ACA_CHG_MAX);
2580 /*
2581 * (ID_B --> ID_C) PHY_ALT interrupt can
2582 * not be detected in LPM.
2583 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302584 break;
2585 case USB_CDP_CHARGER:
2586 msm_otg_notify_charger(motg,
2587 IDEV_CHG_MAX);
2588 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002589 otg->phy->state =
2590 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302591 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302592 case USB_ACA_C_CHARGER:
2593 msm_otg_notify_charger(motg,
2594 IDEV_ACA_CHG_MAX);
2595 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002596 otg->phy->state =
2597 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302598 break;
2599 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302600 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002601 otg->phy->state =
2602 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302603 mod_timer(&motg->chg_check_timer,
2604 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302605 break;
2606 default:
2607 break;
2608 }
2609 break;
2610 default:
2611 break;
2612 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302613 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2614 pr_debug("b_sess_end && b_bus_req\n");
2615 if (msm_otg_start_srp(otg) < 0) {
2616 clear_bit(B_BUS_REQ, &motg->inputs);
2617 work = 1;
2618 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302619 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002620 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302621 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2622 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302623 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302624 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302625 del_timer_sync(&motg->chg_check_timer);
2626 clear_bit(B_FALSE_SDP, &motg->inputs);
Mayank Rana8ab00352013-01-23 19:26:21 +05302627 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302628 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302629 dcp = (motg->chg_type == USB_DCP_CHARGER);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302630 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2631 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302632 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302633 if (dcp)
2634 msm_otg_wait_for_ext_chg_done(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002635 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302636 /*
2637 * There is a small window where ID interrupt
2638 * is not monitored during ID detection circuit
2639 * switch from ACA to PMIC. Check ID state
2640 * before entering into low power mode.
2641 */
2642 if (!msm_otg_read_pmic_id_state(motg)) {
2643 pr_debug("process missed ID intr\n");
2644 clear_bit(ID, &motg->inputs);
2645 work = 1;
2646 break;
2647 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002648 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002649 /*
2650 * Only if autosuspend was enabled in probe, it will be
2651 * used here. Otherwise, no delay will be used.
2652 */
2653 pm_runtime_mark_last_busy(otg->phy->dev);
2654 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302655 }
2656 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302657 case OTG_STATE_B_SRP_INIT:
2658 if (!test_bit(ID, &motg->inputs) ||
2659 test_bit(ID_A, &motg->inputs) ||
2660 test_bit(ID_C, &motg->inputs) ||
2661 (test_bit(B_SESS_VLD, &motg->inputs) &&
2662 !test_bit(ID_B, &motg->inputs))) {
2663 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2664 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002665 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302666 /*
2667 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2668 * bits after SRP initiation.
2669 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002670 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302671 work = 1;
2672 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2673 pr_debug("b_srp_fail\n");
2674 pr_info("A-device did not respond to SRP\n");
2675 clear_bit(B_BUS_REQ, &motg->inputs);
2676 clear_bit(B_SRP_FAIL, &motg->tmouts);
2677 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002678 ulpi_write(otg->phy, 0x0, 0x98);
2679 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302680 motg->b_last_se0_sess = jiffies;
2681 work = 1;
2682 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302683 break;
2684 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302685 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2686 test_bit(B_FALSE_SDP, &motg->inputs)) {
2687 pr_debug("B_FALSE_SDP\n");
2688 msm_otg_start_peripheral(otg, 0);
2689 motg->chg_type = USB_DCP_CHARGER;
2690 clear_bit(B_FALSE_SDP, &motg->inputs);
2691 otg->phy->state = OTG_STATE_B_IDLE;
2692 work = 1;
2693 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302694 test_bit(ID_A, &motg->inputs) ||
2695 test_bit(ID_B, &motg->inputs) ||
2696 !test_bit(B_SESS_VLD, &motg->inputs)) {
2697 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302698 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2699 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302700 msm_otg_notify_charger(motg, 0);
2701 srp_reqd = otg->gadget->otg_srp_reqd;
2702 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302703 if (test_bit(ID_B, &motg->inputs))
2704 clear_bit(ID_B, &motg->inputs);
2705 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002706 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302707 motg->b_last_se0_sess = jiffies;
2708 if (srp_reqd)
2709 msm_otg_start_timer(motg,
2710 TB_TST_SRP, B_TST_SRP);
2711 else
2712 work = 1;
2713 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2714 otg->gadget->b_hnp_enable &&
2715 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2716 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2717 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2718 /* D+ pullup should not be disconnected within 4msec
2719 * after A device suspends the bus. Otherwise PET will
2720 * fail the compliance test.
2721 */
2722 udelay(1000);
2723 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002724 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302725 /*
2726 * start HCD even before A-device enable
2727 * pull-up to meet HNP timings.
2728 */
2729 otg->host->is_b_host = 1;
2730 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002731 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2732 test_bit(B_SESS_VLD, &motg->inputs)) {
2733 pr_debug("a_bus_suspend && b_sess_vld\n");
2734 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002735 pm_runtime_put_noidle(otg->phy->dev);
2736 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002737 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002738 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302739 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002740 }
2741 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302742 case OTG_STATE_B_WAIT_ACON:
2743 if (!test_bit(ID, &motg->inputs) ||
2744 test_bit(ID_A, &motg->inputs) ||
2745 test_bit(ID_B, &motg->inputs) ||
2746 !test_bit(B_SESS_VLD, &motg->inputs)) {
2747 pr_debug("!id || id_a/b || !b_sess_vld\n");
2748 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302749 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302750 * A-device is physically disconnected during
2751 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302752 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302753 msm_otg_start_host(otg, 0);
2754 otg->host->is_b_host = 0;
2755
2756 clear_bit(B_BUS_REQ, &motg->inputs);
2757 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2758 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002759 otg->phy->state = OTG_STATE_B_IDLE;
2760 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302761 work = 1;
2762 } else if (test_bit(A_CONN, &motg->inputs)) {
2763 pr_debug("a_conn\n");
2764 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002765 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302766 /*
2767 * PET disconnects D+ pullup after reset is generated
2768 * by B device in B_HOST role which is not detected by
2769 * B device. As workaorund , start timer of 300msec
2770 * and stop timer if A device is enumerated else clear
2771 * A_CONN.
2772 */
2773 msm_otg_start_timer(motg, TB_TST_CONFIG,
2774 B_TST_CONFIG);
2775 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2776 pr_debug("b_ase0_brst_tmout\n");
2777 pr_info("B HNP fail:No response from A device\n");
2778 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002779 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302780 otg->host->is_b_host = 0;
2781 clear_bit(B_ASE0_BRST, &motg->tmouts);
2782 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2783 clear_bit(B_BUS_REQ, &motg->inputs);
2784 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002785 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302786 work = 1;
2787 } else if (test_bit(ID_C, &motg->inputs)) {
2788 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2789 }
2790 break;
2791 case OTG_STATE_B_HOST:
2792 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2793 !test_bit(A_CONN, &motg->inputs) ||
2794 !test_bit(B_SESS_VLD, &motg->inputs)) {
2795 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2796 clear_bit(A_CONN, &motg->inputs);
2797 clear_bit(B_BUS_REQ, &motg->inputs);
2798 msm_otg_start_host(otg, 0);
2799 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002800 otg->phy->state = OTG_STATE_B_IDLE;
2801 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302802 work = 1;
2803 } else if (test_bit(ID_C, &motg->inputs)) {
2804 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2805 }
2806 break;
2807 case OTG_STATE_A_IDLE:
2808 otg->default_a = 1;
2809 if (test_bit(ID, &motg->inputs) &&
2810 !test_bit(ID_A, &motg->inputs)) {
2811 pr_debug("id && !id_a\n");
2812 otg->default_a = 0;
2813 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002814 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302815 del_timer_sync(&motg->id_timer);
2816 msm_otg_link_reset(motg);
2817 msm_chg_enable_aca_intr(motg);
2818 msm_otg_notify_charger(motg, 0);
2819 work = 1;
2820 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2821 (test_bit(A_SRP_DET, &motg->inputs) ||
2822 test_bit(A_BUS_REQ, &motg->inputs))) {
2823 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2824
2825 clear_bit(A_SRP_DET, &motg->inputs);
2826 /* Disable SRP detection */
2827 writel_relaxed((readl_relaxed(USB_OTGSC) &
2828 ~OTGSC_INTSTS_MASK) &
2829 ~OTGSC_DPIE, USB_OTGSC);
2830
Steve Mucklef132c6c2012-06-06 18:30:57 -07002831 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302832 /* VBUS should not be supplied before end of SRP pulse
2833 * generated by PET, if not complaince test fail.
2834 */
2835 usleep_range(10000, 12000);
2836 /* ACA: ID_A: Stop charging untill enumeration */
2837 if (test_bit(ID_A, &motg->inputs))
2838 msm_otg_notify_charger(motg, 0);
2839 else
2840 msm_hsusb_vbus_power(motg, 1);
2841 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2842 } else {
2843 pr_debug("No session requested\n");
2844 clear_bit(A_BUS_DROP, &motg->inputs);
2845 if (test_bit(ID_A, &motg->inputs)) {
2846 msm_otg_notify_charger(motg,
2847 IDEV_ACA_CHG_MAX);
2848 } else if (!test_bit(ID, &motg->inputs)) {
2849 msm_otg_notify_charger(motg, 0);
2850 /*
2851 * A-device is not providing power on VBUS.
2852 * Enable SRP detection.
2853 */
2854 writel_relaxed(0x13, USB_USBMODE);
2855 writel_relaxed((readl_relaxed(USB_OTGSC) &
2856 ~OTGSC_INTSTS_MASK) |
2857 OTGSC_DPIE, USB_OTGSC);
2858 mb();
2859 }
2860 }
2861 break;
2862 case OTG_STATE_A_WAIT_VRISE:
2863 if ((test_bit(ID, &motg->inputs) &&
2864 !test_bit(ID_A, &motg->inputs)) ||
2865 test_bit(A_BUS_DROP, &motg->inputs) ||
2866 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2867 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2868 clear_bit(A_BUS_REQ, &motg->inputs);
2869 msm_otg_del_timer(motg);
2870 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002871 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302872 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2873 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2874 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002875 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302876 if (TA_WAIT_BCON > 0)
2877 msm_otg_start_timer(motg, TA_WAIT_BCON,
2878 A_WAIT_BCON);
Sujeet Kumarebaef0c2013-06-03 15:54:25 +05302879
2880 /* Clear BSV in host mode */
2881 clear_bit(B_SESS_VLD, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302882 msm_otg_start_host(otg, 1);
2883 msm_chg_enable_aca_det(motg);
2884 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302885 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302886 if (msm_chg_check_aca_intr(motg))
2887 work = 1;
2888 }
2889 break;
2890 case OTG_STATE_A_WAIT_BCON:
2891 if ((test_bit(ID, &motg->inputs) &&
2892 !test_bit(ID_A, &motg->inputs)) ||
2893 test_bit(A_BUS_DROP, &motg->inputs) ||
2894 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2895 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2896 "a_wait_bcon_tmout\n");
2897 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2898 pr_info("Device No Response\n");
2899 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2900 }
2901 msm_otg_del_timer(motg);
2902 clear_bit(A_BUS_REQ, &motg->inputs);
2903 clear_bit(B_CONN, &motg->inputs);
2904 msm_otg_start_host(otg, 0);
2905 /*
2906 * ACA: ID_A with NO accessory, just the A plug is
2907 * attached to ACA: Use IDCHG_MAX for charging
2908 */
2909 if (test_bit(ID_A, &motg->inputs))
2910 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2911 else
2912 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002913 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302914 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2915 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2916 pr_debug("!a_vbus_vld\n");
2917 clear_bit(B_CONN, &motg->inputs);
2918 msm_otg_del_timer(motg);
2919 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002920 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2921 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302922 } else if (test_bit(ID_A, &motg->inputs)) {
2923 msm_hsusb_vbus_power(motg, 0);
2924 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2925 /*
2926 * If TA_WAIT_BCON is infinite, we don;t
2927 * turn off VBUS. Enter low power mode.
2928 */
2929 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002930 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302931 } else if (!test_bit(ID, &motg->inputs)) {
2932 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302933 }
2934 break;
2935 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302936 if ((test_bit(ID, &motg->inputs) &&
2937 !test_bit(ID_A, &motg->inputs)) ||
2938 test_bit(A_BUS_DROP, &motg->inputs)) {
2939 pr_debug("id_a/b/c || a_bus_drop\n");
2940 clear_bit(B_CONN, &motg->inputs);
2941 clear_bit(A_BUS_REQ, &motg->inputs);
2942 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002943 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302944 msm_otg_start_host(otg, 0);
2945 if (!test_bit(ID_A, &motg->inputs))
2946 msm_hsusb_vbus_power(motg, 0);
2947 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2948 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2949 pr_debug("!a_vbus_vld\n");
2950 clear_bit(B_CONN, &motg->inputs);
2951 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002952 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302953 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002954 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302955 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2956 /*
2957 * a_bus_req is de-asserted when root hub is
2958 * suspended or HNP is in progress.
2959 */
2960 pr_debug("!a_bus_req\n");
2961 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002962 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302963 if (otg->host->b_hnp_enable)
2964 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2965 A_AIDL_BDIS);
2966 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002967 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302968 } else if (!test_bit(B_CONN, &motg->inputs)) {
2969 pr_debug("!b_conn\n");
2970 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002971 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302972 if (TA_WAIT_BCON > 0)
2973 msm_otg_start_timer(motg, TA_WAIT_BCON,
2974 A_WAIT_BCON);
2975 if (msm_chg_check_aca_intr(motg))
2976 work = 1;
2977 } else if (test_bit(ID_A, &motg->inputs)) {
2978 msm_otg_del_timer(motg);
2979 msm_hsusb_vbus_power(motg, 0);
2980 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2981 msm_otg_notify_charger(motg,
2982 IDEV_ACA_CHG_MAX);
2983 else
2984 msm_otg_notify_charger(motg,
2985 IDEV_CHG_MIN - motg->mA_port);
2986 } else if (!test_bit(ID, &motg->inputs)) {
2987 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2988 motg->chg_type = USB_INVALID_CHARGER;
2989 msm_otg_notify_charger(motg, 0);
2990 msm_hsusb_vbus_power(motg, 1);
2991 }
2992 break;
2993 case OTG_STATE_A_SUSPEND:
2994 if ((test_bit(ID, &motg->inputs) &&
2995 !test_bit(ID_A, &motg->inputs)) ||
2996 test_bit(A_BUS_DROP, &motg->inputs) ||
2997 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2998 pr_debug("id_a/b/c || a_bus_drop ||"
2999 "a_aidl_bdis_tmout\n");
3000 msm_otg_del_timer(motg);
3001 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003002 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303003 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003004 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303005 if (!test_bit(ID_A, &motg->inputs))
3006 msm_hsusb_vbus_power(motg, 0);
3007 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3008 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
3009 pr_debug("!a_vbus_vld\n");
3010 msm_otg_del_timer(motg);
3011 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003012 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303013 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003014 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303015 } else if (!test_bit(B_CONN, &motg->inputs) &&
3016 otg->host->b_hnp_enable) {
3017 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003018 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303019 msm_otg_host_hnp_enable(otg, 1);
3020 otg->gadget->is_a_peripheral = 1;
3021 msm_otg_start_peripheral(otg, 1);
3022 } else if (!test_bit(B_CONN, &motg->inputs) &&
3023 !otg->host->b_hnp_enable) {
3024 pr_debug("!b_conn && !b_hnp_enable");
3025 /*
3026 * bus request is dropped during suspend.
3027 * acquire again for next device.
3028 */
3029 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003030 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303031 if (TA_WAIT_BCON > 0)
3032 msm_otg_start_timer(motg, TA_WAIT_BCON,
3033 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003034 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05303035 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003036 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303037 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003038 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003039 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05303040 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303041 }
3042 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303043 case OTG_STATE_A_PERIPHERAL:
3044 if ((test_bit(ID, &motg->inputs) &&
3045 !test_bit(ID_A, &motg->inputs)) ||
3046 test_bit(A_BUS_DROP, &motg->inputs)) {
3047 pr_debug("id _f/b/c || a_bus_drop\n");
3048 /* Clear BIDL_ADIS timer */
3049 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003050 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303051 msm_otg_start_peripheral(otg, 0);
3052 otg->gadget->is_a_peripheral = 0;
3053 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003054 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303055 if (!test_bit(ID_A, &motg->inputs))
3056 msm_hsusb_vbus_power(motg, 0);
3057 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3058 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
3059 pr_debug("!a_vbus_vld\n");
3060 /* Clear BIDL_ADIS timer */
3061 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003062 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303063 msm_otg_start_peripheral(otg, 0);
3064 otg->gadget->is_a_peripheral = 0;
3065 msm_otg_start_host(otg, 0);
3066 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
3067 pr_debug("a_bidl_adis_tmout\n");
3068 msm_otg_start_peripheral(otg, 0);
3069 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003070 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303071 set_bit(A_BUS_REQ, &motg->inputs);
3072 msm_otg_host_hnp_enable(otg, 0);
3073 if (TA_WAIT_BCON > 0)
3074 msm_otg_start_timer(motg, TA_WAIT_BCON,
3075 A_WAIT_BCON);
3076 } else if (test_bit(ID_A, &motg->inputs)) {
3077 msm_hsusb_vbus_power(motg, 0);
3078 msm_otg_notify_charger(motg,
3079 IDEV_CHG_MIN - motg->mA_port);
3080 } else if (!test_bit(ID, &motg->inputs)) {
3081 msm_otg_notify_charger(motg, 0);
3082 msm_hsusb_vbus_power(motg, 1);
3083 }
3084 break;
3085 case OTG_STATE_A_WAIT_VFALL:
3086 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
3087 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003088 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303089 work = 1;
3090 }
3091 break;
3092 case OTG_STATE_A_VBUS_ERR:
3093 if ((test_bit(ID, &motg->inputs) &&
3094 !test_bit(ID_A, &motg->inputs)) ||
3095 test_bit(A_BUS_DROP, &motg->inputs) ||
3096 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003097 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303098 if (!test_bit(ID_A, &motg->inputs))
3099 msm_hsusb_vbus_power(motg, 0);
3100 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3101 motg->chg_state = USB_CHG_STATE_UNDEFINED;
3102 motg->chg_type = USB_INVALID_CHARGER;
3103 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303104 }
3105 break;
3106 default:
3107 break;
3108 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303109 if (work)
3110 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303111}
3112
Amit Blayd0fe07b2012-09-05 16:42:09 +03003113static void msm_otg_suspend_work(struct work_struct *w)
3114{
3115 struct msm_otg *motg =
3116 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05303117
3118 /* This work is only for device bus suspend */
3119 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
3120 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003121}
3122
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303123static irqreturn_t msm_otg_irq(int irq, void *data)
3124{
3125 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003126 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303127 u32 otgsc = 0, usbsts, pc;
3128 bool work = 0;
3129 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303130
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303131 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07003132 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303133 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07003134 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07003135 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07003136 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303137 return IRQ_HANDLED;
3138 }
3139
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003140 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303141 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303142
3143 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303144 return IRQ_NONE;
3145
3146 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303147 if (otgsc & OTGSC_ID) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003148 dev_dbg(otg->phy->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303149 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303150 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003151 dev_dbg(otg->phy->dev, "ID clear\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303152 /*
3153 * Assert a_bus_req to supply power on
3154 * VBUS when Micro/Mini-A cable is connected
3155 * with out user intervention.
3156 */
3157 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303158 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303159 msm_chg_enable_aca_det(motg);
3160 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303161 writel_relaxed(otgsc, USB_OTGSC);
3162 work = 1;
3163 } else if (otgsc & OTGSC_DPIS) {
3164 pr_debug("DPIS detected\n");
3165 writel_relaxed(otgsc, USB_OTGSC);
3166 set_bit(A_SRP_DET, &motg->inputs);
3167 set_bit(A_BUS_REQ, &motg->inputs);
3168 work = 1;
Vamsi Krishnaef6e1bf2013-03-02 15:36:17 -08003169 } else if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303170 writel_relaxed(otgsc, USB_OTGSC);
3171 /*
3172 * BSV interrupt comes when operating as an A-device
3173 * (VBUS on/off).
3174 * But, handle BSV when charger is removed from ACA in ID_A
3175 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003176 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303177 !test_bit(ID_A, &motg->inputs))
3178 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303179 if (otgsc & OTGSC_BSV) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003180 dev_dbg(otg->phy->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303181 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303182 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003183 dev_dbg(otg->phy->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303184 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003185 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3186
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303187 msm_chg_check_aca_intr(motg);
3188 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303189 work = 1;
3190 } else if (usbsts & STS_PCI) {
3191 pc = readl_relaxed(USB_PORTSC);
3192 pr_debug("portsc = %x\n", pc);
3193 ret = IRQ_NONE;
3194 /*
3195 * HCD Acks PCI interrupt. We use this to switch
3196 * between different OTG states.
3197 */
3198 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003199 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303200 case OTG_STATE_A_SUSPEND:
3201 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3202 !(pc & PORTSC_CCS)) {
3203 pr_debug("B_CONN clear\n");
3204 clear_bit(B_CONN, &motg->inputs);
3205 msm_otg_del_timer(motg);
3206 }
3207 break;
3208 case OTG_STATE_A_PERIPHERAL:
3209 /*
3210 * A-peripheral observed activity on bus.
3211 * clear A_BIDL_ADIS timer.
3212 */
3213 msm_otg_del_timer(motg);
3214 work = 0;
3215 break;
3216 case OTG_STATE_B_WAIT_ACON:
3217 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3218 pr_debug("A_CONN set\n");
3219 set_bit(A_CONN, &motg->inputs);
3220 /* Clear ASE0_BRST timer */
3221 msm_otg_del_timer(motg);
3222 }
3223 break;
3224 case OTG_STATE_B_HOST:
3225 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3226 pr_debug("A_CONN clear\n");
3227 clear_bit(A_CONN, &motg->inputs);
3228 msm_otg_del_timer(motg);
3229 }
3230 break;
3231 case OTG_STATE_A_WAIT_BCON:
3232 if (TA_WAIT_BCON < 0)
3233 set_bit(A_BUS_REQ, &motg->inputs);
3234 default:
3235 work = 0;
3236 break;
3237 }
3238 } else if (usbsts & STS_URI) {
3239 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003240 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303241 case OTG_STATE_A_PERIPHERAL:
3242 /*
3243 * A-peripheral observed activity on bus.
3244 * clear A_BIDL_ADIS timer.
3245 */
3246 msm_otg_del_timer(motg);
3247 work = 0;
3248 break;
3249 default:
3250 work = 0;
3251 break;
3252 }
3253 } else if (usbsts & STS_SLI) {
3254 ret = IRQ_NONE;
3255 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003256 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303257 case OTG_STATE_B_PERIPHERAL:
3258 if (otg->gadget->b_hnp_enable) {
3259 set_bit(A_BUS_SUSPEND, &motg->inputs);
3260 set_bit(B_BUS_REQ, &motg->inputs);
3261 work = 1;
3262 }
3263 break;
3264 case OTG_STATE_A_PERIPHERAL:
3265 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3266 A_BIDL_ADIS);
3267 break;
3268 default:
3269 break;
3270 }
3271 } else if ((usbsts & PHY_ALT_INT)) {
3272 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3273 if (msm_chg_check_aca_intr(motg))
3274 work = 1;
3275 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303276 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303277 if (work)
3278 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303279
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303280 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003281}
3282
3283static void msm_otg_set_vbus_state(int online)
3284{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303285 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003286 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303287
Vamsi Krishna945b4a92013-03-02 15:31:16 -08003288 if (online) {
3289 pr_debug("PMIC: BSV set\n");
3290 set_bit(B_SESS_VLD, &motg->inputs);
3291 } else {
3292 pr_debug("PMIC: BSV clear\n");
3293 clear_bit(B_SESS_VLD, &motg->inputs);
3294 }
3295
3296 /* do not queue state m/c work if id is grounded */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303297 if (!test_bit(ID, &motg->inputs)) {
3298 /*
3299 * state machine work waits for initial VBUS
3300 * completion in UNDEFINED state. Process
3301 * the initial VBUS event in ID_GND state.
3302 */
3303 if (init)
3304 return;
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303305 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003306
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303307 if (!init) {
3308 init = true;
3309 complete(&pmic_vbus_init);
3310 pr_debug("PMIC: BSV init complete\n");
3311 return;
3312 }
3313
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303314 if (test_bit(MHL, &motg->inputs) ||
3315 mhl_det_in_progress) {
3316 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3317 return;
3318 }
3319
Jack Pham5ca279b2012-05-14 18:42:54 -07003320 if (atomic_read(&motg->pm_suspended))
3321 motg->sm_work_pending = true;
3322 else
3323 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003324}
3325
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303326static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003327{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303328 struct msm_otg *motg = container_of(w, struct msm_otg,
3329 pmic_id_status_work.work);
3330 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003331
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303332 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303333 if (!test_and_set_bit(ID, &motg->inputs)) {
3334 pr_debug("PMIC: ID set\n");
3335 work = 1;
3336 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303337 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303338 if (test_and_clear_bit(ID, &motg->inputs)) {
3339 pr_debug("PMIC: ID clear\n");
3340 set_bit(A_BUS_REQ, &motg->inputs);
3341 work = 1;
3342 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303343 }
3344
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303345 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003346 if (atomic_read(&motg->pm_suspended))
3347 motg->sm_work_pending = true;
3348 else
3349 queue_work(system_nrt_wq, &motg->sm_work);
3350 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303351
3352}
3353
3354#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3355static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3356{
3357 struct msm_otg *motg = data;
3358
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303359 if (test_bit(MHL, &motg->inputs) ||
3360 mhl_det_in_progress) {
3361 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3362 return IRQ_HANDLED;
3363 }
3364
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303365 if (!aca_id_turned_on)
3366 /*schedule delayed work for 5msec for ID line state to settle*/
3367 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3368 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003369
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303370 return IRQ_HANDLED;
3371}
3372
3373static int msm_otg_mode_show(struct seq_file *s, void *unused)
3374{
3375 struct msm_otg *motg = s->private;
Stephen Boyd9850acb2013-01-28 14:11:20 -08003376 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303377
Stephen Boyd9850acb2013-01-28 14:11:20 -08003378 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303379 case OTG_STATE_A_HOST:
3380 seq_printf(s, "host\n");
3381 break;
3382 case OTG_STATE_B_PERIPHERAL:
3383 seq_printf(s, "peripheral\n");
3384 break;
3385 default:
3386 seq_printf(s, "none\n");
3387 break;
3388 }
3389
3390 return 0;
3391}
3392
3393static int msm_otg_mode_open(struct inode *inode, struct file *file)
3394{
3395 return single_open(file, msm_otg_mode_show, inode->i_private);
3396}
3397
3398static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3399 size_t count, loff_t *ppos)
3400{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303401 struct seq_file *s = file->private_data;
3402 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303403 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003404 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303405 int status = count;
3406 enum usb_mode_type req_mode;
3407
3408 memset(buf, 0x00, sizeof(buf));
3409
3410 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3411 status = -EFAULT;
3412 goto out;
3413 }
3414
3415 if (!strncmp(buf, "host", 4)) {
3416 req_mode = USB_HOST;
3417 } else if (!strncmp(buf, "peripheral", 10)) {
3418 req_mode = USB_PERIPHERAL;
3419 } else if (!strncmp(buf, "none", 4)) {
3420 req_mode = USB_NONE;
3421 } else {
3422 status = -EINVAL;
3423 goto out;
3424 }
3425
3426 switch (req_mode) {
3427 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003428 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303429 case OTG_STATE_A_HOST:
3430 case OTG_STATE_B_PERIPHERAL:
3431 set_bit(ID, &motg->inputs);
3432 clear_bit(B_SESS_VLD, &motg->inputs);
3433 break;
3434 default:
3435 goto out;
3436 }
3437 break;
3438 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003439 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303440 case OTG_STATE_B_IDLE:
3441 case OTG_STATE_A_HOST:
3442 set_bit(ID, &motg->inputs);
3443 set_bit(B_SESS_VLD, &motg->inputs);
3444 break;
3445 default:
3446 goto out;
3447 }
3448 break;
3449 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003450 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303451 case OTG_STATE_B_IDLE:
3452 case OTG_STATE_B_PERIPHERAL:
3453 clear_bit(ID, &motg->inputs);
3454 break;
3455 default:
3456 goto out;
3457 }
3458 break;
3459 default:
3460 goto out;
3461 }
3462
Steve Mucklef132c6c2012-06-06 18:30:57 -07003463 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303464 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303465out:
3466 return status;
3467}
3468
3469const struct file_operations msm_otg_mode_fops = {
3470 .open = msm_otg_mode_open,
3471 .read = seq_read,
3472 .write = msm_otg_mode_write,
3473 .llseek = seq_lseek,
3474 .release = single_release,
3475};
3476
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303477static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3478{
3479 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003480 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303481
Steve Mucklef132c6c2012-06-06 18:30:57 -07003482 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303483 return 0;
3484}
3485
3486static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3487{
3488 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3489}
3490
3491const struct file_operations msm_otg_state_fops = {
3492 .open = msm_otg_otg_state_open,
3493 .read = seq_read,
3494 .llseek = seq_lseek,
3495 .release = single_release,
3496};
3497
Anji jonnalad270e2d2011-08-09 11:28:32 +05303498static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3499{
3500 struct msm_otg *motg = s->private;
3501
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303502 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303503 return 0;
3504}
3505
3506static int msm_otg_chg_open(struct inode *inode, struct file *file)
3507{
3508 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3509}
3510
3511const struct file_operations msm_otg_chg_fops = {
3512 .open = msm_otg_chg_open,
3513 .read = seq_read,
3514 .llseek = seq_lseek,
3515 .release = single_release,
3516};
3517
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303518static int msm_otg_aca_show(struct seq_file *s, void *unused)
3519{
3520 if (debug_aca_enabled)
3521 seq_printf(s, "enabled\n");
3522 else
3523 seq_printf(s, "disabled\n");
3524
3525 return 0;
3526}
3527
3528static int msm_otg_aca_open(struct inode *inode, struct file *file)
3529{
3530 return single_open(file, msm_otg_aca_show, inode->i_private);
3531}
3532
3533static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3534 size_t count, loff_t *ppos)
3535{
3536 char buf[8];
3537
3538 memset(buf, 0x00, sizeof(buf));
3539
3540 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3541 return -EFAULT;
3542
3543 if (!strncmp(buf, "enable", 6))
3544 debug_aca_enabled = true;
3545 else
3546 debug_aca_enabled = false;
3547
3548 return count;
3549}
3550
3551const struct file_operations msm_otg_aca_fops = {
3552 .open = msm_otg_aca_open,
3553 .read = seq_read,
3554 .write = msm_otg_aca_write,
3555 .llseek = seq_lseek,
3556 .release = single_release,
3557};
3558
Manu Gautam8bdcc592012-03-06 11:26:06 +05303559static int msm_otg_bus_show(struct seq_file *s, void *unused)
3560{
3561 if (debug_bus_voting_enabled)
3562 seq_printf(s, "enabled\n");
3563 else
3564 seq_printf(s, "disabled\n");
3565
3566 return 0;
3567}
3568
3569static int msm_otg_bus_open(struct inode *inode, struct file *file)
3570{
3571 return single_open(file, msm_otg_bus_show, inode->i_private);
3572}
3573
3574static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3575 size_t count, loff_t *ppos)
3576{
3577 char buf[8];
Manu Gautam8bdcc592012-03-06 11:26:06 +05303578 struct seq_file *s = file->private_data;
3579 struct msm_otg *motg = s->private;
3580
3581 memset(buf, 0x00, sizeof(buf));
3582
3583 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3584 return -EFAULT;
3585
3586 if (!strncmp(buf, "enable", 6)) {
3587 /* Do not vote here. Let OTG statemachine decide when to vote */
3588 debug_bus_voting_enabled = true;
3589 } else {
3590 debug_bus_voting_enabled = false;
Manu Gautame3a39082013-06-11 10:42:56 +05303591 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303592 }
3593
3594 return count;
3595}
3596
David Keitel272ce522012-08-17 16:25:24 -07003597static int otg_power_get_property_usb(struct power_supply *psy,
3598 enum power_supply_property psp,
3599 union power_supply_propval *val)
3600{
Jack Pham0c695282012-10-19 18:13:03 -07003601 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003602 switch (psp) {
3603 case POWER_SUPPLY_PROP_SCOPE:
3604 if (motg->host_mode)
3605 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3606 else
3607 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3608 break;
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303609 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
3610 val->intval = motg->voltage_max;
3611 break;
David Keitel272ce522012-08-17 16:25:24 -07003612 case POWER_SUPPLY_PROP_CURRENT_MAX:
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303613 val->intval = motg->current_max;
David Keitel272ce522012-08-17 16:25:24 -07003614 break;
3615 /* Reflect USB enumeration */
3616 case POWER_SUPPLY_PROP_PRESENT:
3617 case POWER_SUPPLY_PROP_ONLINE:
3618 val->intval = motg->online;
3619 break;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303620 case POWER_SUPPLY_PROP_TYPE:
3621 val->intval = psy->type;
3622 break;
David Keitel272ce522012-08-17 16:25:24 -07003623 default:
3624 return -EINVAL;
3625 }
3626 return 0;
3627}
3628
3629static int otg_power_set_property_usb(struct power_supply *psy,
3630 enum power_supply_property psp,
3631 const union power_supply_propval *val)
3632{
Jack Pham0c695282012-10-19 18:13:03 -07003633 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003634
3635 switch (psp) {
3636 /* Process PMIC notification in PRESENT prop */
3637 case POWER_SUPPLY_PROP_PRESENT:
3638 msm_otg_set_vbus_state(val->intval);
3639 break;
3640 /* The ONLINE property reflects if usb has enumerated */
3641 case POWER_SUPPLY_PROP_ONLINE:
3642 motg->online = val->intval;
3643 break;
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303644 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
3645 motg->voltage_max = val->intval;
3646 break;
David Keitel272ce522012-08-17 16:25:24 -07003647 case POWER_SUPPLY_PROP_CURRENT_MAX:
3648 motg->current_max = val->intval;
3649 break;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303650 case POWER_SUPPLY_PROP_TYPE:
3651 psy->type = val->intval;
3652 break;
David Keitel272ce522012-08-17 16:25:24 -07003653 default:
3654 return -EINVAL;
3655 }
3656
3657 power_supply_changed(&motg->usb_psy);
3658 return 0;
3659}
3660
David Collinsd79acc52012-11-26 14:59:00 -08003661static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3662 enum power_supply_property psp)
3663{
3664 switch (psp) {
3665 case POWER_SUPPLY_PROP_PRESENT:
3666 case POWER_SUPPLY_PROP_ONLINE:
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303667 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
David Collinsd79acc52012-11-26 14:59:00 -08003668 case POWER_SUPPLY_PROP_CURRENT_MAX:
3669 return 1;
3670 default:
3671 break;
3672 }
3673
3674 return 0;
3675}
3676
David Keitel272ce522012-08-17 16:25:24 -07003677static char *otg_pm_power_supplied_to[] = {
3678 "battery",
3679};
3680
3681static enum power_supply_property otg_pm_power_props_usb[] = {
3682 POWER_SUPPLY_PROP_PRESENT,
3683 POWER_SUPPLY_PROP_ONLINE,
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303684 POWER_SUPPLY_PROP_VOLTAGE_MAX,
David Keitel272ce522012-08-17 16:25:24 -07003685 POWER_SUPPLY_PROP_CURRENT_MAX,
3686 POWER_SUPPLY_PROP_SCOPE,
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303687 POWER_SUPPLY_PROP_TYPE,
David Keitel272ce522012-08-17 16:25:24 -07003688};
3689
Manu Gautam8bdcc592012-03-06 11:26:06 +05303690const struct file_operations msm_otg_bus_fops = {
3691 .open = msm_otg_bus_open,
3692 .read = seq_read,
3693 .write = msm_otg_bus_write,
3694 .llseek = seq_lseek,
3695 .release = single_release,
3696};
3697
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303698static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303699
3700static int msm_otg_debugfs_init(struct msm_otg *motg)
3701{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303702 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303703
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303704 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3705
3706 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3707 return -ENODEV;
3708
Anji jonnalad270e2d2011-08-09 11:28:32 +05303709 if (motg->pdata->mode == USB_OTG &&
3710 motg->pdata->otg_control == OTG_USER_CONTROL) {
3711
Manu Gautam8bdcc592012-03-06 11:26:06 +05303712 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303713 S_IWUSR, msm_otg_dbg_root, motg,
3714 &msm_otg_mode_fops);
3715
Manu Gautam8bdcc592012-03-06 11:26:06 +05303716 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303717 debugfs_remove(msm_otg_dbg_root);
3718 msm_otg_dbg_root = NULL;
3719 return -ENODEV;
3720 }
3721 }
3722
Manu Gautam8bdcc592012-03-06 11:26:06 +05303723 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303724 msm_otg_dbg_root, motg,
3725 &msm_otg_chg_fops);
3726
Manu Gautam8bdcc592012-03-06 11:26:06 +05303727 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303728 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303729 return -ENODEV;
3730 }
3731
Manu Gautam8bdcc592012-03-06 11:26:06 +05303732 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303733 msm_otg_dbg_root, motg,
3734 &msm_otg_aca_fops);
3735
Manu Gautam8bdcc592012-03-06 11:26:06 +05303736 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303737 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303738 return -ENODEV;
3739 }
3740
Manu Gautam8bdcc592012-03-06 11:26:06 +05303741 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3742 msm_otg_dbg_root, motg,
3743 &msm_otg_bus_fops);
3744
3745 if (!msm_otg_dentry) {
3746 debugfs_remove_recursive(msm_otg_dbg_root);
3747 return -ENODEV;
3748 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303749
3750 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3751 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3752
3753 if (!msm_otg_dentry) {
3754 debugfs_remove_recursive(msm_otg_dbg_root);
3755 return -ENODEV;
3756 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303757 return 0;
3758}
3759
3760static void msm_otg_debugfs_cleanup(void)
3761{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303762 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303763}
3764
Manu Gautam0ddbd922012-09-21 17:17:38 +05303765#define MSM_OTG_CMD_ID 0x09
3766#define MSM_OTG_DEVICE_ID 0x04
3767#define MSM_OTG_VMID_IDX 0xFF
3768#define MSM_OTG_MEM_TYPE 0x02
3769struct msm_otg_scm_cmd_buf {
3770 unsigned int device_id;
3771 unsigned int vmid_idx;
3772 unsigned int mem_type;
3773} __attribute__ ((__packed__));
3774
3775static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3776{
3777 int ret;
3778 struct msm_otg_platform_data *pdata = motg->pdata;
3779 struct msm_otg_scm_cmd_buf cmd_buf;
3780
3781 if (!pdata->pnoc_errata_fix)
3782 return;
3783
3784 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3785
3786 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3787 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3788 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3789
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -07003790 ret = scm_call(SCM_SVC_MP, MSM_OTG_CMD_ID, &cmd_buf,
Manu Gautam0ddbd922012-09-21 17:17:38 +05303791 sizeof(cmd_buf), NULL, 0);
3792
3793 if (ret)
3794 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3795}
3796
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303797static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3798static struct platform_device *msm_otg_add_pdev(
3799 struct platform_device *ofdev, const char *name)
3800{
3801 struct platform_device *pdev;
3802 const struct resource *res = ofdev->resource;
3803 unsigned int num = ofdev->num_resources;
3804 int retval;
Amit Blayb4f14532013-06-17 22:33:57 +03003805 struct ci13xxx_platform_data ci_pdata;
3806 struct msm_otg_platform_data *otg_pdata;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303807
3808 pdev = platform_device_alloc(name, -1);
3809 if (!pdev) {
3810 retval = -ENOMEM;
3811 goto error;
3812 }
3813
3814 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3815 pdev->dev.dma_mask = &msm_otg_dma_mask;
3816
3817 if (num) {
3818 retval = platform_device_add_resources(pdev, res, num);
3819 if (retval)
3820 goto error;
3821 }
3822
Amit Blayb4f14532013-06-17 22:33:57 +03003823 if (!strcmp(name, "msm_hsusb")) {
3824 otg_pdata =
3825 (struct msm_otg_platform_data *)
3826 ofdev->dev.platform_data;
3827 ci_pdata.log2_itc = otg_pdata->log2_itc;
3828 ci_pdata.usb_core_id = 0;
Shimrit Malichi5a2d5b52013-06-20 19:04:28 +03003829 ci_pdata.l1_supported = otg_pdata->l1_supported;
Amit Blayb4f14532013-06-17 22:33:57 +03003830 retval = platform_device_add_data(pdev, &ci_pdata,
3831 sizeof(ci_pdata));
3832 if (retval)
3833 goto error;
3834 }
3835
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303836 retval = platform_device_add(pdev);
3837 if (retval)
3838 goto error;
3839
3840 return pdev;
3841
3842error:
3843 platform_device_put(pdev);
3844 return ERR_PTR(retval);
3845}
3846
3847static int msm_otg_setup_devices(struct platform_device *ofdev,
3848 enum usb_mode_type mode, bool init)
3849{
3850 const char *gadget_name = "msm_hsusb";
3851 const char *host_name = "msm_hsusb_host";
3852 static struct platform_device *gadget_pdev;
3853 static struct platform_device *host_pdev;
3854 int retval = 0;
3855
3856 if (!init) {
3857 if (gadget_pdev)
3858 platform_device_unregister(gadget_pdev);
3859 if (host_pdev)
3860 platform_device_unregister(host_pdev);
3861 return 0;
3862 }
3863
3864 switch (mode) {
3865 case USB_OTG:
3866 /* fall through */
3867 case USB_PERIPHERAL:
3868 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3869 if (IS_ERR(gadget_pdev)) {
3870 retval = PTR_ERR(gadget_pdev);
3871 break;
3872 }
3873 if (mode == USB_PERIPHERAL)
3874 break;
3875 /* fall through */
3876 case USB_HOST:
3877 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3878 if (IS_ERR(host_pdev)) {
3879 retval = PTR_ERR(host_pdev);
3880 if (mode == USB_OTG)
3881 platform_device_unregister(gadget_pdev);
3882 }
3883 break;
3884 default:
3885 break;
3886 }
3887
3888 return retval;
3889}
3890
David Keitel272ce522012-08-17 16:25:24 -07003891static int msm_otg_register_power_supply(struct platform_device *pdev,
3892 struct msm_otg *motg)
3893{
3894 int ret;
3895
3896 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3897 if (ret < 0) {
3898 dev_err(motg->phy.dev,
3899 "%s:power_supply_register usb failed\n",
3900 __func__);
3901 return ret;
3902 }
3903
3904 legacy_power_supply = false;
3905 return 0;
3906}
3907
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303908static int msm_otg_ext_chg_open(struct inode *inode, struct file *file)
3909{
3910 struct msm_otg *motg = the_msm_otg;
3911
3912 pr_debug("msm_otg ext chg open\n");
3913
3914 motg->ext_chg_opened = true;
3915 file->private_data = (void *)motg;
3916 return 0;
3917}
3918
3919static long
3920msm_otg_ext_chg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3921{
3922 struct msm_otg *motg = file->private_data;
3923 struct msm_usb_chg_info info = {0};
3924 int ret = 0, val;
3925
3926 switch (cmd) {
3927 case MSM_USB_EXT_CHG_INFO:
3928 info.chg_block_type = USB_CHG_BLOCK_ULPI;
3929 info.page_offset = motg->io_res->start & ~PAGE_MASK;
3930 /* mmap() works on PAGE granularity */
3931 info.length = PAGE_SIZE;
3932
3933 if (copy_to_user((void __user *)arg, &info, sizeof(info))) {
3934 pr_err("%s: copy to user failed\n\n", __func__);
3935 ret = -EFAULT;
3936 }
3937 break;
3938 case MSM_USB_EXT_CHG_BLOCK_LPM:
3939 if (get_user(val, (int __user *)arg)) {
3940 pr_err("%s: get_user failed\n\n", __func__);
3941 ret = -EFAULT;
3942 break;
3943 }
3944 pr_debug("%s: LPM block request %d\n", __func__, val);
3945 if (val) { /* block LPM */
3946 if (motg->chg_type == USB_DCP_CHARGER) {
3947 /*
3948 * If device is already suspended, resume it.
3949 * The PM usage counter is incremented in
3950 * runtime resume method. if device is not
3951 * suspended, cancel the scheduled suspend
3952 * and increment the PM usage counter.
3953 */
3954 if (pm_runtime_suspended(motg->phy.dev))
3955 pm_runtime_resume(motg->phy.dev);
3956 else
3957 pm_runtime_get_sync(motg->phy.dev);
3958 } else {
3959 motg->ext_chg_active = false;
3960 complete(&motg->ext_chg_wait);
3961 ret = -ENODEV;
3962 }
3963 } else {
3964 motg->ext_chg_active = false;
3965 complete(&motg->ext_chg_wait);
3966 pm_runtime_put(motg->phy.dev);
3967 }
3968 break;
3969 default:
3970 ret = -EINVAL;
3971 }
3972
3973 return ret;
3974}
3975
3976static int msm_otg_ext_chg_mmap(struct file *file, struct vm_area_struct *vma)
3977{
3978 struct msm_otg *motg = file->private_data;
3979 unsigned long vsize = vma->vm_end - vma->vm_start;
3980 int ret;
3981
3982 if (vma->vm_pgoff || vsize > PAGE_SIZE)
3983 return -EINVAL;
3984
3985 vma->vm_pgoff = __phys_to_pfn(motg->io_res->start);
3986 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
3987
3988 ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
3989 vsize, vma->vm_page_prot);
3990 if (ret < 0) {
3991 pr_err("%s: failed with return val %d\n", __func__, ret);
3992 return ret;
3993 }
3994
3995 return 0;
3996}
3997
3998static int msm_otg_ext_chg_release(struct inode *inode, struct file *file)
3999{
4000 struct msm_otg *motg = file->private_data;
4001
4002 pr_debug("msm_otg ext chg release\n");
4003
4004 motg->ext_chg_opened = false;
4005
4006 return 0;
4007}
4008
4009static const struct file_operations msm_otg_ext_chg_fops = {
4010 .owner = THIS_MODULE,
4011 .open = msm_otg_ext_chg_open,
4012 .unlocked_ioctl = msm_otg_ext_chg_ioctl,
4013 .mmap = msm_otg_ext_chg_mmap,
4014 .release = msm_otg_ext_chg_release,
4015};
4016
4017static int msm_otg_setup_ext_chg_cdev(struct msm_otg *motg)
4018{
4019 int ret;
4020
4021 if (motg->pdata->enable_sec_phy || motg->pdata->mode == USB_HOST ||
4022 motg->pdata->otg_control != OTG_PMIC_CONTROL ||
4023 psy != &motg->usb_psy) {
4024 pr_debug("usb ext chg is not supported by msm otg\n");
4025 return -ENODEV;
4026 }
4027
4028 ret = alloc_chrdev_region(&motg->ext_chg_dev, 0, 1, "usb_ext_chg");
4029 if (ret < 0) {
4030 pr_err("Fail to allocate usb ext char dev region\n");
4031 return ret;
4032 }
4033 motg->ext_chg_class = class_create(THIS_MODULE, "msm_ext_chg");
4034 if (ret < 0) {
4035 pr_err("Fail to create usb ext chg class\n");
4036 goto unreg_chrdev;
4037 }
4038 cdev_init(&motg->ext_chg_cdev, &msm_otg_ext_chg_fops);
4039 motg->ext_chg_cdev.owner = THIS_MODULE;
4040
4041 ret = cdev_add(&motg->ext_chg_cdev, motg->ext_chg_dev, 1);
4042 if (ret < 0) {
4043 pr_err("Fail to add usb ext chg cdev\n");
4044 goto destroy_class;
4045 }
4046 motg->ext_chg_device = device_create(motg->ext_chg_class,
4047 NULL, motg->ext_chg_dev, NULL,
4048 "usb_ext_chg");
4049 if (IS_ERR(motg->ext_chg_device)) {
4050 pr_err("Fail to create usb ext chg device\n");
4051 ret = PTR_ERR(motg->ext_chg_device);
4052 motg->ext_chg_device = NULL;
4053 goto del_cdev;
4054 }
4055
4056 init_completion(&motg->ext_chg_wait);
4057 pr_debug("msm otg ext chg cdev setup success\n");
4058 return 0;
4059
4060del_cdev:
4061 cdev_del(&motg->ext_chg_cdev);
4062destroy_class:
4063 class_destroy(motg->ext_chg_class);
4064unreg_chrdev:
4065 unregister_chrdev_region(motg->ext_chg_dev, 1);
4066
4067 return ret;
4068}
4069
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304070static ssize_t dpdm_pulldown_enable_show(struct device *dev,
4071 struct device_attribute *attr, char *buf)
4072{
4073 struct msm_otg *motg = the_msm_otg;
4074 struct msm_otg_platform_data *pdata = motg->pdata;
4075
4076 return snprintf(buf, PAGE_SIZE, "%s\n", pdata->dpdm_pulldown_added ?
4077 "enabled" : "disabled");
4078}
4079
4080static ssize_t dpdm_pulldown_enable_store(struct device *dev,
4081 struct device_attribute *attr, const char
4082 *buf, size_t size)
4083{
4084 struct msm_otg *motg = the_msm_otg;
4085 struct msm_otg_platform_data *pdata = motg->pdata;
4086
4087 if (!strnicmp(buf, "enable", 6)) {
4088 pdata->dpdm_pulldown_added = true;
4089 return size;
4090 } else if (!strnicmp(buf, "disable", 7)) {
4091 pdata->dpdm_pulldown_added = false;
4092 return size;
4093 }
4094
4095 return -EINVAL;
4096}
4097
4098static DEVICE_ATTR(dpdm_pulldown_enable, S_IRUGO | S_IWUSR,
4099 dpdm_pulldown_enable_show, dpdm_pulldown_enable_store);
4100
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304101struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
4102{
4103 struct device_node *node = pdev->dev.of_node;
4104 struct msm_otg_platform_data *pdata;
4105 int len = 0;
4106
4107 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
4108 if (!pdata) {
4109 pr_err("unable to allocate platform data\n");
4110 return NULL;
4111 }
4112 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
4113 if (len) {
4114 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
4115 if (!pdata->phy_init_seq)
4116 return NULL;
4117 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
4118 pdata->phy_init_seq,
4119 len/sizeof(*pdata->phy_init_seq));
4120 }
4121 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
4122 &pdata->power_budget);
4123 of_property_read_u32(node, "qcom,hsusb-otg-mode",
4124 &pdata->mode);
4125 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
4126 &pdata->otg_control);
4127 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
4128 &pdata->default_mode);
4129 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
4130 &pdata->phy_type);
Manu Gautambd53fba2012-07-31 16:13:06 +05304131 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
4132 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05304133 pdata->pnoc_errata_fix = of_property_read_bool(node,
4134 "qcom,hsusb-otg-pnoc-errata-fix");
Ido Shayevitza7114b92013-01-13 13:34:47 +02004135 pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
4136 "qcom,hsusb-otg-lpm-on-dev-suspend");
Ido Shayevitz26193352013-01-21 23:16:54 +02004137 pdata->core_clk_always_on_workaround = of_property_read_bool(node,
4138 "qcom,hsusb-otg-clk-always-on-workaround");
Shimrit Malichiffab5b02013-03-10 11:06:16 +02004139 pdata->delay_lpm_on_disconnect = of_property_read_bool(node,
4140 "qcom,hsusb-otg-delay-lpm");
Lena Salmanabde35d2013-04-25 15:29:43 +03004141 pdata->delay_lpm_hndshk_on_disconnect = of_property_read_bool(node,
4142 "qcom,hsusb-otg-delay-lpm-hndshk-on-disconnect");
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004143 pdata->dp_manual_pullup = of_property_read_bool(node,
4144 "qcom,dp-manual-pullup");
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304145 pdata->enable_sec_phy = of_property_read_bool(node,
4146 "qcom,usb2-enable-hsphy2");
Amit Blayb4f14532013-06-17 22:33:57 +03004147 of_property_read_u32(node, "qcom,hsusb-log2-itc",
4148 &pdata->log2_itc);
Manu Gautambd53fba2012-07-31 16:13:06 +05304149
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304150 of_property_read_u32(node, "qcom,hsusb-otg-mpm-dpsehv-int",
4151 &pdata->mpm_dpshv_int);
4152 of_property_read_u32(node, "qcom,hsusb-otg-mpm-dmsehv-int",
4153 &pdata->mpm_dmshv_int);
Manu Gautam0e0c53f2013-04-04 10:55:20 +05304154 pdata->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
4155 if (pdata->pmic_id_irq < 0)
4156 pdata->pmic_id_irq = 0;
4157
Shimrit Malichi5a2d5b52013-06-20 19:04:28 +03004158 pdata->l1_supported = of_property_read_bool(node,
4159 "qcom,hsusb-l1-supported");
4160
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304161 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304162}
4163
4164static int __init msm_otg_probe(struct platform_device *pdev)
4165{
Manu Gautamf8c45642012-08-10 10:20:56 -07004166 int ret = 0;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304167 int len = 0;
4168 u32 tmp[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304169 struct resource *res;
4170 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004171 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304172 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304173
4174 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304175
4176 if (pdev->dev.of_node) {
4177 dev_dbg(&pdev->dev, "device tree enabled\n");
4178 pdata = msm_otg_dt_to_pdata(pdev);
4179 if (!pdata)
4180 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07004181
4182 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
4183 if (!pdata->bus_scale_table)
4184 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
4185
Amit Blayb4f14532013-06-17 22:33:57 +03004186 pdev->dev.platform_data = pdata;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304187 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
4188 if (ret) {
4189 dev_err(&pdev->dev, "devices setup failed\n");
4190 return ret;
4191 }
4192 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304193 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
4194 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304195 } else {
4196 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304197 }
4198
Manu Gautame3a39082013-06-11 10:42:56 +05304199 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304200 if (!motg) {
4201 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
4202 return -ENOMEM;
4203 }
4204
Manu Gautame3a39082013-06-11 10:42:56 +05304205 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
4206 GFP_KERNEL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004207 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004208 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
Manu Gautame3a39082013-06-11 10:42:56 +05304209 return -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304210 }
4211
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004212 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304213 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004214 phy = &motg->phy;
4215 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05304216
Manu Gautame3a39082013-06-11 10:42:56 +05304217 if (motg->pdata->bus_scale_table) {
4218 motg->bus_perf_client =
4219 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4220 if (!motg->bus_perf_client) {
4221 dev_err(motg->phy.dev, "%s: Failed to register BUS\n"
4222 "scaling client!!\n", __func__);
4223 } else {
4224 debug_bus_voting_enabled = true;
4225 /* Some platforms require BUS vote to control clocks */
4226 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
4227 }
4228 }
4229
Anji jonnala0f73cac2011-05-04 10:19:46 +05304230 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304231 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
4232 * PHY treat ACA ID_GND as float and no interrupt is generated. But
4233 * PMIC can detect ACA ID_GND and generate an interrupt.
4234 */
4235 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
4236 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
4237 ret = -EINVAL;
Manu Gautame3a39082013-06-11 10:42:56 +05304238 goto devote_bus_bw;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304239 }
4240
Ofir Cohen4da266f2012-01-03 10:19:29 +02004241 /* initialize reset counter */
4242 motg->reset_counter = 0;
4243
Amit Blay02eff132011-09-21 16:46:24 +03004244 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08004245 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03004246 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08004247 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304248
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304249 /*
4250 * Targets on which link uses asynchronous reset methodology,
4251 * free running clock is not required during the reset.
4252 */
Manu Gautam5143b252012-01-05 19:25:23 -08004253 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304254 if (IS_ERR(motg->clk))
4255 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
4256 else
4257 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05304258
4259 /*
Manu Gautam5143b252012-01-05 19:25:23 -08004260 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05304261 * CORE CLK must be running at >55Mhz for correct HSUSB
4262 * operation and USB core cannot tolerate frequency changes on
4263 * CORE CLK. For such USB cores, vote for maximum clk frequency
4264 * on pclk source
4265 */
Manu Gautam5143b252012-01-05 19:25:23 -08004266 motg->core_clk = clk_get(&pdev->dev, "core_clk");
4267 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304268 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08004269 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05304270 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004271 goto put_clk;
4272 }
Mayank Rana3eaf28d2013-03-27 14:04:04 +05304273
4274 /*
4275 * Get Max supported clk frequency for USB Core CLK and request
4276 * to set the same.
4277 */
4278 motg->core_clk_rate = clk_round_rate(motg->core_clk, LONG_MAX);
4279 if (IS_ERR_VALUE(motg->core_clk_rate)) {
4280 dev_err(&pdev->dev, "fail to get core clk max freq.\n");
4281 } else {
4282 ret = clk_set_rate(motg->core_clk, motg->core_clk_rate);
4283 if (ret)
4284 dev_err(&pdev->dev, "fail to set core_clk freq:%d\n",
4285 ret);
4286 }
Manu Gautam5143b252012-01-05 19:25:23 -08004287
4288 motg->pclk = clk_get(&pdev->dev, "iface_clk");
4289 if (IS_ERR(motg->pclk)) {
4290 dev_err(&pdev->dev, "failed to get iface_clk\n");
4291 ret = PTR_ERR(motg->pclk);
4292 goto put_core_clk;
4293 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304294
4295 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4296 if (!res) {
4297 dev_err(&pdev->dev, "failed to get platform resource mem\n");
4298 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08004299 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304300 }
4301
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304302 motg->io_res = res;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304303 motg->regs = ioremap(res->start, resource_size(res));
4304 if (!motg->regs) {
4305 dev_err(&pdev->dev, "ioremap failed\n");
4306 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08004307 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304308 }
4309 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
4310
4311 motg->irq = platform_get_irq(pdev, 0);
4312 if (!motg->irq) {
4313 dev_err(&pdev->dev, "platform_get_irq failed\n");
4314 ret = -ENODEV;
4315 goto free_regs;
4316 }
4317
Manu Gautamf8c45642012-08-10 10:20:56 -07004318 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
4319 if (motg->async_irq < 0) {
4320 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
4321 motg->async_irq = 0;
4322 }
4323
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304324 motg->xo_clk = clk_get(&pdev->dev, "xo");
4325 if (IS_ERR(motg->xo_clk)) {
4326 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
4327 if (IS_ERR(motg->xo_handle)) {
4328 dev_err(&pdev->dev, "%s fail to get handle for TCXO\n",
4329 __func__);
4330 ret = PTR_ERR(motg->xo_handle);
4331 goto free_regs;
4332 } else {
4333 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
4334 if (ret) {
4335 dev_err(&pdev->dev, "%s XO voting failed %d\n",
4336 __func__, ret);
4337 goto free_xo_handle;
4338 }
4339 }
4340 } else {
4341 ret = clk_prepare_enable(motg->xo_clk);
4342 if (ret) {
4343 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
4344 __func__, ret);
4345 goto free_xo_handle;
4346 }
Anji jonnala7da3f262011-12-02 17:22:14 -08004347 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05304348
Anji jonnala7da3f262011-12-02 17:22:14 -08004349
Manu Gautam28b1bac2012-01-30 16:43:06 +05304350 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304351
Mayank Rana248698c2012-04-19 00:03:16 +05304352 motg->vdd_type = VDDCX_CORNER;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304353 hsusb_vdd = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
4354 if (IS_ERR(hsusb_vdd)) {
4355 hsusb_vdd = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
4356 if (IS_ERR(hsusb_vdd)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004357 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304358 ret = PTR_ERR(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304359 goto devote_xo_handle;
4360 }
4361 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304362 }
4363
Mayank Rana0f286cf2013-02-27 11:43:27 +05304364 if (pdev->dev.of_node) {
4365 of_get_property(pdev->dev.of_node,
4366 "qcom,vdd-voltage-level",
4367 &len);
4368 if (len == sizeof(tmp)) {
4369 of_property_read_u32_array(pdev->dev.of_node,
4370 "qcom,vdd-voltage-level",
4371 tmp, len/sizeof(*tmp));
4372 vdd_val[motg->vdd_type][0] = tmp[0];
4373 vdd_val[motg->vdd_type][1] = tmp[1];
4374 vdd_val[motg->vdd_type][2] = tmp[2];
4375 } else {
4376 dev_dbg(&pdev->dev, "Using default hsusb vdd config.\n");
4377 }
4378 }
4379
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004380 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304381 if (ret) {
4382 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05304383 goto devote_xo_handle;
4384 }
4385
Mayank Rana0f286cf2013-02-27 11:43:27 +05304386 ret = regulator_enable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304387 if (ret) {
4388 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
4389 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304390 }
4391
4392 ret = msm_hsusb_ldo_init(motg, 1);
4393 if (ret) {
4394 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304395 goto free_hsusb_vdd;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304396 }
4397
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304398 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05304399 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
4400 "mhl_usb_hs_switch");
4401 if (IS_ERR(mhl_usb_hs_switch)) {
4402 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07004403 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304404 goto free_ldo_init;
4405 }
4406 }
4407
Amit Blay81801aa2012-09-19 12:08:12 +02004408 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304409 if (ret) {
4410 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004411 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304412 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05304413 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304414
Manu Gautam0ddbd922012-09-21 17:17:38 +05304415 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
4416 msm_otg_pnoc_errata_fix(motg);
4417
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304418 writel(0, USB_USBINTR);
4419 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004420 /* Ensure that above STOREs are completed before enabling interrupts */
4421 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304422
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304423 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
4424 if (ret)
4425 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004426 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05304427 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304428 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304429 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304430 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004431 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304432 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
4433 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05304434 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
4435 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304436 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
4437 "msm_otg", motg);
4438 if (ret) {
4439 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004440 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304441 }
4442
Manu Gautamf8c45642012-08-10 10:20:56 -07004443 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304444 ret = request_irq(motg->async_irq, msm_otg_irq,
4445 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004446 if (ret) {
4447 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4448 goto free_irq;
4449 }
4450 disable_irq(motg->async_irq);
4451 }
4452
Jack Pham87f202f2012-08-06 00:24:22 -07004453 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4454 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4455
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304456 if (pdata->mpm_dpshv_int)
4457 msm_mpm_enable_pin(pdata->mpm_dpshv_int, 1);
4458 if (pdata->mpm_dmshv_int)
4459 msm_mpm_enable_pin(pdata->mpm_dmshv_int, 1);
4460
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004461 phy->init = msm_otg_reset;
4462 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004463 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304464
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004465 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304466
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004467 phy->otg->phy = &motg->phy;
4468 phy->otg->set_host = msm_otg_set_host;
4469 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004470 phy->otg->start_hnp = msm_otg_start_hnp;
4471 phy->otg->start_srp = msm_otg_start_srp;
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004472 if (pdata->dp_manual_pullup)
4473 phy->flags |= ENABLE_DP_MANUAL_PULLUP;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004474
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304475 if (pdata->enable_sec_phy)
4476 phy->flags |= ENABLE_SECONDARY_PHY;
4477
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004478 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304479 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004480 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004481 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304482 }
4483
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304484 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304485 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004486 if (motg->pdata->pmic_id_irq) {
4487 ret = request_irq(motg->pdata->pmic_id_irq,
4488 msm_pmic_id_irq,
4489 IRQF_TRIGGER_RISING |
4490 IRQF_TRIGGER_FALLING,
4491 "msm_otg", motg);
4492 if (ret) {
4493 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004494 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004495 }
4496 } else {
4497 ret = -ENODEV;
4498 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004499 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004500 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304501 }
4502
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304503 msm_hsusb_mhl_switch_enable(motg, 1);
4504
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304505 platform_set_drvdata(pdev, motg);
4506 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004507 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304508
Anji jonnalad270e2d2011-08-09 11:28:32 +05304509 ret = msm_otg_debugfs_init(motg);
4510 if (ret)
4511 dev_dbg(&pdev->dev, "mode debugfs file is"
4512 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304513
Amit Blay58b31472011-11-18 09:39:39 +02004514 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4515 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304516 (!(motg->pdata->mode == USB_OTG) ||
4517 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004518 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304519 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004520
Amit Blay58b31472011-11-18 09:39:39 +02004521 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004522 motg->caps = ALLOW_PHY_RETENTION |
4523 ALLOW_PHY_REGULATORS_LPM;
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304524
4525 if (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int)
4526 motg->caps |= ALLOW_HOST_PHY_RETENTION;
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304527 device_create_file(&pdev->dev,
4528 &dev_attr_dpdm_pulldown_enable);
Amit Blay58b31472011-11-18 09:39:39 +02004529 }
4530
Amit Blay6fa647a2012-05-24 14:12:08 +03004531 if (motg->pdata->enable_lpm_on_dev_suspend)
4532 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4533
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004534 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304535 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004536 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304537
Amit Blayd6f38282012-10-29 13:13:46 +02004538 if (motg->pdata->delay_lpm_on_disconnect) {
4539 pm_runtime_set_autosuspend_delay(&pdev->dev,
4540 lpm_disconnect_thresh);
4541 pm_runtime_use_autosuspend(&pdev->dev);
4542 }
4543
David Keitel272ce522012-08-17 16:25:24 -07004544 motg->usb_psy.name = "usb";
4545 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4546 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4547 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4548 motg->usb_psy.properties = otg_pm_power_props_usb;
4549 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4550 motg->usb_psy.get_property = otg_power_get_property_usb;
4551 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004552 motg->usb_psy.property_is_writeable
4553 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004554
Jack Pham0c695282012-10-19 18:13:03 -07004555 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004556 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004557 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4558 legacy_power_supply = true;
4559 } else {
4560 /* otherwise register our own power supply */
4561 if (!msm_otg_register_power_supply(pdev, motg))
4562 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004563 }
4564
Jack Pham0c695282012-10-19 18:13:03 -07004565 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4566 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4567
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304568 ret = msm_otg_setup_ext_chg_cdev(motg);
4569 if (ret)
4570 dev_dbg(&pdev->dev, "fail to setup cdev\n");
4571
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304572 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004573
Steve Mucklef132c6c2012-06-06 18:30:57 -07004574remove_phy:
4575 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004576free_async_irq:
4577 if (motg->async_irq)
4578 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304579free_irq:
4580 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004581destroy_wlock:
4582 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304583 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004584 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004585free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304586 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304587free_hsusb_vdd:
4588 regulator_disable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304589free_config_vddcx:
Mayank Rana0f286cf2013-02-27 11:43:27 +05304590 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304591 vdd_val[motg->vdd_type][VDD_NONE],
4592 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004593devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304594 clk_disable_unprepare(motg->pclk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304595 if (!IS_ERR(motg->xo_clk))
4596 clk_disable_unprepare(motg->xo_clk);
4597 else
4598 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004599free_xo_handle:
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304600 if (!IS_ERR(motg->xo_clk))
4601 clk_put(motg->xo_clk);
4602 else
4603 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304604free_regs:
4605 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004606put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304607 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304608put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004609 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304610put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304611 if (!IS_ERR(motg->clk))
4612 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004613 if (!IS_ERR(motg->phy_reset_clk))
4614 clk_put(motg->phy_reset_clk);
Manu Gautame3a39082013-06-11 10:42:56 +05304615devote_bus_bw:
4616 if (motg->bus_perf_client) {
4617 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
4618 msm_bus_scale_unregister_client(motg->bus_perf_client);
4619 }
4620
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304621 return ret;
4622}
4623
4624static int __devexit msm_otg_remove(struct platform_device *pdev)
4625{
4626 struct msm_otg *motg = platform_get_drvdata(pdev);
Stephen Boyd9850acb2013-01-28 14:11:20 -08004627 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304628 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304629
Stephen Boyd9850acb2013-01-28 14:11:20 -08004630 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304631 return -EBUSY;
4632
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304633 if (!motg->ext_chg_device) {
4634 device_destroy(motg->ext_chg_class, motg->ext_chg_dev);
4635 cdev_del(&motg->ext_chg_cdev);
4636 class_destroy(motg->ext_chg_class);
4637 unregister_chrdev_region(motg->ext_chg_dev, 1);
4638 }
4639
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304640 if (pdev->dev.of_node)
4641 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004642 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4643 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304644 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304645 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304646 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304647 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004648 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304649 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304650
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304651 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304652
4653 device_init_wakeup(&pdev->dev, 0);
4654 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004655 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304656
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304657 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004658 if (motg->pdata->pmic_id_irq)
4659 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004660 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304661 free_irq(motg->irq, motg);
4662
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304663 if ((motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) &&
4664 (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int))
4665 device_remove_file(&pdev->dev,
4666 &dev_attr_dpdm_pulldown_enable);
Jack Pham87f202f2012-08-06 00:24:22 -07004667 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4668 motg->pdata->mpm_otgsessvld_int)
4669 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4670
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304671 if (motg->pdata->mpm_dpshv_int)
4672 msm_mpm_enable_pin(motg->pdata->mpm_dpshv_int, 0);
4673 if (motg->pdata->mpm_dmshv_int)
4674 msm_mpm_enable_pin(motg->pdata->mpm_dmshv_int, 0);
4675
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304676 /*
4677 * Put PHY in low power mode.
4678 */
Stephen Boyd9850acb2013-01-28 14:11:20 -08004679 ulpi_read(phy, 0x14);
4680 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304681
4682 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4683 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4684 if (readl(USB_PORTSC) & PORTSC_PHCD)
4685 break;
4686 udelay(1);
4687 cnt++;
4688 }
4689 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Stephen Boyd9850acb2013-01-28 14:11:20 -08004690 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304691
Manu Gautam28b1bac2012-01-30 16:43:06 +05304692 clk_disable_unprepare(motg->pclk);
4693 clk_disable_unprepare(motg->core_clk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304694 if (!IS_ERR(motg->xo_clk)) {
4695 clk_disable_unprepare(motg->xo_clk);
4696 clk_put(motg->xo_clk);
4697 } else {
4698 msm_xo_put(motg->xo_handle);
4699 }
Amit Blay81801aa2012-09-19 12:08:12 +02004700 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304701 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304702 regulator_disable(hsusb_vdd);
4703 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304704 vdd_val[motg->vdd_type][VDD_NONE],
4705 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304706
4707 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304708 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304709
Amit Blay02eff132011-09-21 16:46:24 +03004710 if (!IS_ERR(motg->phy_reset_clk))
4711 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304712 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304713 if (!IS_ERR(motg->clk))
4714 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004715 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304716
Manu Gautame3a39082013-06-11 10:42:56 +05304717 if (motg->bus_perf_client) {
4718 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304719 msm_bus_scale_unregister_client(motg->bus_perf_client);
Manu Gautame3a39082013-06-11 10:42:56 +05304720 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304721
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304722 return 0;
4723}
4724
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304725#ifdef CONFIG_PM_RUNTIME
4726static int msm_otg_runtime_idle(struct device *dev)
4727{
4728 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004729 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304730
4731 dev_dbg(dev, "OTG runtime idle\n");
4732
Steve Mucklef132c6c2012-06-06 18:30:57 -07004733 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304734 return -EAGAIN;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304735
4736 if (motg->ext_chg_active) {
4737 dev_dbg(dev, "Deferring LPM\n");
4738 /*
4739 * Charger detection may happen in user space.
4740 * Delay entering LPM by 3 sec. Otherwise we
4741 * have to exit LPM when user space begins
4742 * charger detection.
4743 *
4744 * This timer will be canceled when user space
4745 * votes against LPM by incrementing PM usage
4746 * counter. We enter low power mode when
4747 * PM usage counter is decremented.
4748 */
4749 pm_schedule_suspend(dev, 3000);
4750 return -EAGAIN;
4751 }
4752
4753 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304754}
4755
4756static int msm_otg_runtime_suspend(struct device *dev)
4757{
4758 struct msm_otg *motg = dev_get_drvdata(dev);
4759
4760 dev_dbg(dev, "OTG runtime suspend\n");
4761 return msm_otg_suspend(motg);
4762}
4763
4764static int msm_otg_runtime_resume(struct device *dev)
4765{
4766 struct msm_otg *motg = dev_get_drvdata(dev);
4767
4768 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304769 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304770 return msm_otg_resume(motg);
4771}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304772#endif
4773
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304774#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304775static int msm_otg_pm_suspend(struct device *dev)
4776{
Jack Pham5ca279b2012-05-14 18:42:54 -07004777 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304778 struct msm_otg *motg = dev_get_drvdata(dev);
4779
4780 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004781
4782 atomic_set(&motg->pm_suspended, 1);
4783 ret = msm_otg_suspend(motg);
4784 if (ret)
4785 atomic_set(&motg->pm_suspended, 0);
4786
4787 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304788}
4789
4790static int msm_otg_pm_resume(struct device *dev)
4791{
Jack Pham5ca279b2012-05-14 18:42:54 -07004792 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304793 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304794
4795 dev_dbg(dev, "OTG PM resume\n");
4796
Jack Pham5ca279b2012-05-14 18:42:54 -07004797 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004798 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004799 pm_runtime_get_noresume(dev);
4800 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304801
Jack Pham5ca279b2012-05-14 18:42:54 -07004802 /* Update runtime PM status */
4803 pm_runtime_disable(dev);
4804 pm_runtime_set_active(dev);
4805 pm_runtime_enable(dev);
4806
Jack Phamc7edb172012-08-13 15:32:39 -07004807 if (motg->sm_work_pending) {
4808 motg->sm_work_pending = false;
4809 queue_work(system_nrt_wq, &motg->sm_work);
4810 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004811 }
4812
4813 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304814}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304815#endif
4816
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304817#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304818static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304819 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4820 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4821 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304822};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304823#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304824
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304825static struct of_device_id msm_otg_dt_match[] = {
4826 { .compatible = "qcom,hsusb-otg",
4827 },
4828 {}
4829};
4830
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304831static struct platform_driver msm_otg_driver = {
4832 .remove = __devexit_p(msm_otg_remove),
4833 .driver = {
4834 .name = DRIVER_NAME,
4835 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304836#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304837 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304838#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304839 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304840 },
4841};
4842
4843static int __init msm_otg_init(void)
4844{
4845 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4846}
4847
4848static void __exit msm_otg_exit(void)
4849{
4850 platform_driver_unregister(&msm_otg_driver);
4851}
4852
4853module_init(msm_otg_init);
4854module_exit(msm_otg_exit);
4855
4856MODULE_LICENSE("GPL v2");
4857MODULE_DESCRIPTION("MSM USB transceiver driver");