blob: 4bd98f7068c6c706c5ed5d1dc593e482dd73a360 [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));
Mayank Ranab95838b2013-07-31 14:36:35 +0530366 dev_err(phy->dev, "PORTSC: %08x USBCMD: %08x\n",
367 readl_relaxed(USB_PORTSC), readl_relaxed(USB_USBCMD));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530368 return -ETIMEDOUT;
369 }
370 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
371}
372
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200373static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530374{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200375 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530376 int cnt = 0;
377
378 /* initiate write operation */
379 writel(ULPI_RUN | ULPI_WRITE |
380 ULPI_ADDR(reg) | ULPI_DATA(val),
381 USB_ULPI_VIEWPORT);
382
383 /* wait for completion */
384 while (cnt < ULPI_IO_TIMEOUT_USEC) {
385 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
386 break;
387 udelay(1);
388 cnt++;
389 }
390
391 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200392 dev_err(phy->dev, "ulpi_write: timeout\n");
Mayank Ranab95838b2013-07-31 14:36:35 +0530393 dev_err(phy->dev, "PORTSC: %08x USBCMD: %08x\n",
394 readl_relaxed(USB_PORTSC), readl_relaxed(USB_USBCMD));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530395 return -ETIMEDOUT;
396 }
397 return 0;
398}
399
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200400static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530401 .read = ulpi_read,
402 .write = ulpi_write,
403};
404
405static void ulpi_init(struct msm_otg *motg)
406{
407 struct msm_otg_platform_data *pdata = motg->pdata;
Mayank Rana443f9e42012-09-21 18:32:39 +0530408 int aseq[10];
409 int *seq = NULL;
410
411 if (override_phy_init) {
412 pr_debug("%s(): HUSB PHY Init:%s\n", __func__,
413 override_phy_init);
414 get_options(override_phy_init, ARRAY_SIZE(aseq), aseq);
415 seq = &aseq[1];
416 } else {
417 seq = pdata->phy_init_seq;
418 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530419
420 if (!seq)
421 return;
422
423 while (seq[0] >= 0) {
Mayank Rana443f9e42012-09-21 18:32:39 +0530424 if (override_phy_init)
425 pr_debug("ulpi: write 0x%02x to 0x%02x\n",
426 seq[0], seq[1]);
427
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200428 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530429 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200430 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530431 seq += 2;
432 }
433}
434
435static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
436{
437 int ret;
438
439 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530440 if (!IS_ERR(motg->clk)) {
441 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
442 } else {
443 /* Using asynchronous block reset to the hardware */
444 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
445 clk_disable_unprepare(motg->pclk);
446 clk_disable_unprepare(motg->core_clk);
447 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
448 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530449 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200450 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530451 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530452 if (!IS_ERR(motg->clk)) {
453 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
454 } else {
455 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
456 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
457 ndelay(200);
458 clk_prepare_enable(motg->core_clk);
459 clk_prepare_enable(motg->pclk);
460 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530461 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200462 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530463 }
464 return ret;
465}
466
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530467static int msm_otg_phy_reset(struct msm_otg *motg)
468{
469 u32 val;
470 int ret;
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530471 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530472
473 ret = msm_otg_link_clk_reset(motg, 1);
474 if (ret)
475 return ret;
Amit Blay58dc2bc2013-01-24 12:28:03 +0200476
Mayank Ranab95838b2013-07-31 14:36:35 +0530477 /* wait for 1ms delay as suggested in HPG. */
478 usleep_range(1000, 1200);
479
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530480 ret = msm_otg_link_clk_reset(motg, 0);
481 if (ret)
482 return ret;
483
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530484 if (pdata && pdata->enable_sec_phy)
485 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
486 USB_PHY_CTRL2);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530487 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
488 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
489
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200490 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530491 return 0;
492}
493
494#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530495static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530496{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530497 int cnt = 0;
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530498 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530499
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530500 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530501 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530502 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530503 break;
504 udelay(1);
505 cnt++;
506 }
507 if (cnt >= LINK_RESET_TIMEOUT_USEC)
508 return -ETIMEDOUT;
509
510 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530511 writel_relaxed(0x80000000, USB_PORTSC);
512 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530513 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530514
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530515 if (pdata && pdata->enable_sec_phy)
516 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
517 USB_PHY_CTRL2);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530518 return 0;
519}
520
Mayank Ranab95838b2013-07-31 14:36:35 +0530521static void usb_phy_reset(struct msm_otg *motg)
522{
523 u32 val;
524
525 if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY)
526 return;
527
528 /* Assert USB PHY_PON */
529 val = readl_relaxed(USB_PHY_CTRL);
530 val &= ~PHY_POR_BIT_MASK;
531 val |= PHY_POR_ASSERT;
532 writel_relaxed(val, USB_PHY_CTRL);
533
534 /* wait for minimum 10 microseconds as suggested in HPG. */
535 usleep_range(10, 15);
536
537 /* Deassert USB PHY_PON */
538 val = readl_relaxed(USB_PHY_CTRL);
539 val &= ~PHY_POR_BIT_MASK;
540 val |= PHY_POR_DEASSERT;
541 writel_relaxed(val, USB_PHY_CTRL);
542
543 /* Ensure that RESET operation is completed. */
544 mb();
545}
546
Steve Mucklef132c6c2012-06-06 18:30:57 -0700547static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530548{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700549 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530550 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551 int ret;
552 u32 val = 0;
553 u32 ulpi_val = 0;
554
Ofir Cohen4da266f2012-01-03 10:19:29 +0200555 /*
556 * USB PHY and Link reset also reset the USB BAM.
557 * Thus perform reset operation only once to avoid
558 * USB BAM reset on other cases e.g. USB cable disconnections.
559 */
560 if (pdata->disable_reset_on_disconnect) {
561 if (motg->reset_counter)
562 return 0;
563 else
564 motg->reset_counter++;
565 }
566
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530567 if (!IS_ERR(motg->clk))
568 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530569 ret = msm_otg_phy_reset(motg);
570 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700571 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530572 return ret;
573 }
574
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530575 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530576 ret = msm_otg_link_reset(motg);
577 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700578 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530579 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530580 }
Mayank Ranab95838b2013-07-31 14:36:35 +0530581
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530582 msleep(100);
583
Mayank Ranab95838b2013-07-31 14:36:35 +0530584 /* Reset USB PHY after performing USB Link RESET */
585 usb_phy_reset(motg);
586
587 /* Program USB PHY Override registers. */
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530588 ulpi_init(motg);
589
Mayank Ranab95838b2013-07-31 14:36:35 +0530590 /*
591 * It is recommended in HPG to reset USB PHY after programming
592 * USB PHY Override registers.
593 */
594 usb_phy_reset(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530595
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530596 if (!IS_ERR(motg->clk))
597 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530598
599 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530600 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530601 if (pdata->mode == USB_OTG) {
602 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
603 val |= OTGSC_IDIE | OTGSC_BSVIE;
604 } else if (pdata->mode == USB_PERIPHERAL) {
605 ulpi_val = ULPI_INT_SESS_VALID;
606 val |= OTGSC_BSVIE;
607 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530608 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200609 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
610 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530611 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700612 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530613 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530614 /* Enable PMIC pull-up */
615 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530616 }
617
618 return 0;
619}
620
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530621static const char *timer_string(int bit)
622{
623 switch (bit) {
624 case A_WAIT_VRISE: return "a_wait_vrise";
625 case A_WAIT_VFALL: return "a_wait_vfall";
626 case B_SRP_FAIL: return "b_srp_fail";
627 case A_WAIT_BCON: return "a_wait_bcon";
628 case A_AIDL_BDIS: return "a_aidl_bdis";
629 case A_BIDL_ADIS: return "a_bidl_adis";
630 case B_ASE0_BRST: return "b_ase0_brst";
631 case A_TST_MAINT: return "a_tst_maint";
632 case B_TST_SRP: return "b_tst_srp";
633 case B_TST_CONFIG: return "b_tst_config";
634 default: return "UNDEFINED";
635 }
636}
637
638static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
639{
640 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
641
642 switch (motg->active_tmout) {
643 case A_WAIT_VRISE:
644 /* TODO: use vbus_vld interrupt */
645 set_bit(A_VBUS_VLD, &motg->inputs);
646 break;
647 case A_TST_MAINT:
648 /* OTG PET: End session after TA_TST_MAINT */
649 set_bit(A_BUS_DROP, &motg->inputs);
650 break;
651 case B_TST_SRP:
652 /*
653 * OTG PET: Initiate SRP after TB_TST_SRP of
654 * previous session end.
655 */
656 set_bit(B_BUS_REQ, &motg->inputs);
657 break;
658 case B_TST_CONFIG:
659 clear_bit(A_CONN, &motg->inputs);
660 break;
661 default:
662 set_bit(motg->active_tmout, &motg->tmouts);
663 }
664
665 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
666 queue_work(system_nrt_wq, &motg->sm_work);
667 return HRTIMER_NORESTART;
668}
669
670static void msm_otg_del_timer(struct msm_otg *motg)
671{
672 int bit = motg->active_tmout;
673
674 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
675 div_s64(ktime_to_us(hrtimer_get_remaining(
676 &motg->timer)), 1000));
677 hrtimer_cancel(&motg->timer);
678 clear_bit(bit, &motg->tmouts);
679}
680
681static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
682{
683 clear_bit(bit, &motg->tmouts);
684 motg->active_tmout = bit;
685 pr_debug("starting %s timer\n", timer_string(bit));
686 hrtimer_start(&motg->timer,
687 ktime_set(time / 1000, (time % 1000) * 1000000),
688 HRTIMER_MODE_REL);
689}
690
691static void msm_otg_init_timer(struct msm_otg *motg)
692{
693 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
694 motg->timer.function = msm_otg_timer_func;
695}
696
Steve Mucklef132c6c2012-06-06 18:30:57 -0700697static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530698{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700699 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530700
Steve Mucklef132c6c2012-06-06 18:30:57 -0700701 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530702 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700703 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530704 return -EINVAL;
705 }
706
707 pr_debug("A-Host: HNP initiated\n");
708 clear_bit(A_BUS_REQ, &motg->inputs);
709 queue_work(system_nrt_wq, &motg->sm_work);
710 return 0;
711}
712
Steve Mucklef132c6c2012-06-06 18:30:57 -0700713static int msm_otg_start_srp(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 u32 val;
717 int ret = 0;
718
Steve Mucklef132c6c2012-06-06 18:30:57 -0700719 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530720 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700721 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530722 ret = -EINVAL;
723 goto out;
724 }
725
726 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
727 pr_debug("initial conditions of SRP are not met. Try again"
728 "after some time\n");
729 ret = -EAGAIN;
730 goto out;
731 }
732
733 pr_debug("B-Device SRP started\n");
734
735 /*
736 * PHY won't pull D+ high unless it detects Vbus valid.
737 * Since by definition, SRP is only done when Vbus is not valid,
738 * software work-around needs to be used to spoof the PHY into
739 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
740 * VBUSVLDEXT register bits.
741 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700742 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530743 /*
744 * Harware auto assist data pulsing: Data pulse is given
745 * for 7msec; wait for vbus
746 */
747 val = readl_relaxed(USB_OTGSC);
748 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
749
750 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
751out:
752 return ret;
753}
754
Steve Mucklef132c6c2012-06-06 18:30:57 -0700755static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530756{
757 struct usb_hcd *hcd = bus_to_hcd(otg->host);
758 struct usb_device *rhub = otg->host->root_hub;
759
760 if (enable) {
761 pm_runtime_disable(&rhub->dev);
762 rhub->state = USB_STATE_NOTATTACHED;
763 hcd->driver->bus_suspend(hcd);
764 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
765 } else {
766 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700767 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530768 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
769 }
770}
771
Steve Mucklef132c6c2012-06-06 18:30:57 -0700772static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530773{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700774 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530775
Amit Blay6fa647a2012-05-24 14:12:08 +0300776 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530777 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530778
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530779 /*
780 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
781 * during bus suspend/resume. Update the relevant state
782 * machine inputs and trigger LPM entry/exit. Checking
783 * in_lpm flag would avoid unnecessary work scheduling.
784 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530785 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700786 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530787 case OTG_STATE_A_WAIT_BCON:
788 if (TA_WAIT_BCON > 0)
789 break;
790 /* fall through */
791 case OTG_STATE_A_HOST:
792 pr_debug("host bus suspend\n");
793 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530794 if (!atomic_read(&motg->in_lpm))
795 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530796 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300797 case OTG_STATE_B_PERIPHERAL:
798 pr_debug("peripheral bus suspend\n");
799 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
800 break;
801 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530802 if (!atomic_read(&motg->in_lpm))
803 queue_delayed_work(system_nrt_wq,
804 &motg->suspend_work,
805 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300806 break;
807
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530808 default:
809 break;
810 }
811 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700812 switch (phy->state) {
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530813 case OTG_STATE_A_WAIT_BCON:
814 /* Remote wakeup or resume */
815 set_bit(A_BUS_REQ, &motg->inputs);
816 /* ensure hardware is not in low power mode */
817 if (atomic_read(&motg->in_lpm))
818 pm_runtime_resume(phy->dev);
819 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530820 case OTG_STATE_A_SUSPEND:
821 /* Remote wakeup or resume */
822 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700823 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700824
825 /* ensure hardware is not in low power mode */
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530826 if (atomic_read(&motg->in_lpm))
827 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530828 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300829 case OTG_STATE_B_PERIPHERAL:
830 pr_debug("peripheral bus resume\n");
831 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
832 break;
833 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530834 if (atomic_read(&motg->in_lpm))
835 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300836 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530837 default:
838 break;
839 }
840 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530841 return 0;
842}
843
Manu Gautame3a39082013-06-11 10:42:56 +0530844static void msm_otg_bus_vote(struct msm_otg *motg, enum usb_bus_vote vote)
845{
846 int ret;
847 struct msm_otg_platform_data *pdata = motg->pdata;
848
849 /* Check if target allows min_vote to be same as no_vote */
Ido Shayevitzcf050e22013-07-22 17:31:48 +0300850 if (pdata->bus_scale_table &&
851 vote >= pdata->bus_scale_table->num_usecases)
Manu Gautame3a39082013-06-11 10:42:56 +0530852 vote = USB_NO_PERF_VOTE;
853
854 if (motg->bus_perf_client) {
855 ret = msm_bus_scale_client_update_request(
856 motg->bus_perf_client, vote);
857 if (ret)
858 dev_err(motg->phy.dev, "%s: Failed to vote (%d)\n"
859 "for bus bw %d\n", __func__, vote, ret);
860 }
861}
862
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530863#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530864#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
865
866#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530867static int msm_otg_suspend(struct msm_otg *motg)
868{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200869 struct usb_phy *phy = &motg->phy;
870 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530871 struct msm_otg_platform_data *pdata = motg->pdata;
872 int cnt = 0;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530873 bool host_bus_suspend, device_bus_suspend, dcp, prop_charger;
zhenhuahb6d5edc2013-06-19 14:37:32 +0800874 bool floated_charger;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530875 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800876 unsigned ret;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +0530877 u32 portsc, config2;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530878
879 if (atomic_read(&motg->in_lpm))
880 return 0;
881
Lena Salmanabde35d2013-04-25 15:29:43 +0300882 if (motg->pdata->delay_lpm_hndshk_on_disconnect && !msm_bam_lpm_ok())
Lena Salman05b544f2013-05-13 15:49:10 +0300883 return -EBUSY;
Lena Salmanabde35d2013-04-25 15:29:43 +0300884
Sujeet Kumarceee6e82013-09-06 11:22:49 +0530885 motg->ui_enabled = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530886 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530887 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
888 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700889 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300890 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
891 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530892 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530893 prop_charger = motg->chg_type == USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +0800894 floated_charger = motg->chg_type == USB_FLOATED_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700895
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +0530896 /* Enable line state difference wakeup fix for only device and host
897 * bus suspend scenarios. Otherwise PHY can not be suspended when
898 * a charger that pulls DP/DM high is connected.
899 */
900 config2 = readl_relaxed(USB_GENCONFIG2);
901 if (device_bus_suspend)
902 config2 |= GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
903 else
904 config2 &= ~GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
905 writel_relaxed(config2, USB_GENCONFIG2);
906
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530907 /*
908 * Abort suspend when,
909 * 1. charging detection in progress due to cable plug-in
910 * 2. host mode activation in progress due to Micro-A cable insertion
911 */
912
913 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
zhenhuahb6d5edc2013-06-19 14:37:32 +0800914 !dcp && !prop_charger && !floated_charger) ||
915 test_bit(A_BUS_REQ, &motg->inputs)) {
Sujeet Kumarceee6e82013-09-06 11:22:49 +0530916 motg->ui_enabled = 1;
Jack Pham502bea32012-08-13 15:34:20 -0700917 enable_irq(motg->irq);
918 return -EBUSY;
919 }
920
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530921 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530922 * Chipidea 45-nm PHY suspend sequence:
923 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530924 * Interrupt Latch Register auto-clear feature is not present
925 * in all PHY versions. Latch register is clear on read type.
926 * Clear latch register to avoid spurious wakeup from
927 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530928 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530929 * PHY comparators are disabled when PHY enters into low power
930 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
931 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
932 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530933 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530934 * PLL is not turned off when PHY enters into low power mode (LPM).
935 * Disable PLL for maximum power savings.
936 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530937
938 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200939 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530940 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200941 ulpi_write(phy, 0x01, 0x30);
942 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530943 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530944
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530946 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530947 * PHY may take some time or even fail to enter into low power
948 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
949 * in failure case.
950 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530951 portsc = readl_relaxed(USB_PORTSC);
952 if (!(portsc & PORTSC_PHCD)) {
953 writel_relaxed(portsc | PORTSC_PHCD,
954 USB_PORTSC);
955 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
956 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
957 break;
958 udelay(1);
959 cnt++;
960 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530961 }
962
963 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200964 dev_err(phy->dev, "Unable to suspend PHY\n");
965 msm_otg_reset(phy);
Sujeet Kumarceee6e82013-09-06 11:22:49 +0530966 motg->ui_enabled = 1;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530967 enable_irq(motg->irq);
968 return -ETIMEDOUT;
969 }
970
971 /*
972 * PHY has capability to generate interrupt asynchronously in low
973 * power mode (LPM). This interrupt is level triggered. So USB IRQ
974 * line must be disabled till async interrupt enable bit is cleared
975 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
976 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530977 *
978 * PHY retention mode is disallowed while entering to LPM with wall
979 * charger connected. But PHY is put into suspend mode. Hence
980 * enable asynchronous interrupt to detect charger disconnection when
981 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530982 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530983 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300984 if (host_bus_suspend || device_bus_suspend ||
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530985 (motg->pdata->otg_control == OTG_PHY_CONTROL))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530986 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
987 else
988 cmd_val |= ULPI_STP_CTRL;
989 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530990
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530991 /*
992 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
993 * PHY retention and collapse can not happen with VDP_SRC enabled.
994 */
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +0530995 if (motg->caps & ALLOW_PHY_RETENTION && !device_bus_suspend && !dcp &&
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +0530996 (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION)
997 && (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS))))) {
Amit Blay58b31472011-11-18 09:39:39 +0200998 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530999 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +02001000 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05301001 if ((motg->pdata->mode == USB_OTG) ||
1002 (motg->pdata->mode == USB_HOST))
1003 phy_ctrl_val |= (PHY_IDHV_INTEN |
1004 PHY_OTGSESSVLDHV_INTEN);
1005 else
1006 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
1007 }
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301008 if (host_bus_suspend)
1009 phy_ctrl_val |= PHY_CLAMP_DPDMSE_EN;
Amit Blay58b31472011-11-18 09:39:39 +02001010 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301012 }
1013
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 /* Ensure that above operation is completed before turning off clocks */
1015 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001016 /* Consider clocks on workaround flag only in case of bus suspend */
1017 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
1018 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
1019 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001020 clk_disable_unprepare(motg->pclk);
1021 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001022 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001023 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301024
Anji jonnala7da3f262011-12-02 17:22:14 -08001025 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05301026 if (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION) &&
1027 (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS)))) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301028 if (!IS_ERR(motg->xo_clk)) {
1029 clk_disable_unprepare(motg->xo_clk);
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301030 motg->lpm_flags |= XO_SHUTDOWN;
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301031 } else {
1032 ret = msm_xo_mode_vote(motg->xo_handle,
1033 MSM_XO_MODE_OFF);
1034 if (ret)
1035 dev_err(phy->dev, "%s fail to devote XO %d\n",
1036 __func__, ret);
1037 else
1038 motg->lpm_flags |= XO_SHUTDOWN;
1039 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301040 }
Anji jonnala7da3f262011-12-02 17:22:14 -08001041
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301042 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
1043 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +02001044 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001046 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
1047 !host_bus_suspend && !device_bus_suspend && !dcp) {
1048 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
1049 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +05301050 }
1051
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301052 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301054 msm_hsusb_mhl_switch_enable(motg, 0);
1055 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056
Steve Mucklef132c6c2012-06-06 18:30:57 -07001057 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001058 if (motg->async_irq)
1059 enable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001060 else
1061 enable_irq_wake(motg->irq);
1062
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001063 if (motg->pdata->pmic_id_irq)
1064 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001065 if (pdata->otg_control == OTG_PHY_CONTROL &&
1066 pdata->mpm_otgsessvld_int)
1067 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301068 if (host_bus_suspend && pdata->mpm_dpshv_int)
1069 msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 1);
1070 if (host_bus_suspend && pdata->mpm_dmshv_int)
1071 msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301073 if (bus)
1074 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1075
Manu Gautame3a39082013-06-11 10:42:56 +05301076 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
1077
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301078 motg->host_bus_suspend = host_bus_suspend;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301079 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001080 /* Enable ASYNC IRQ (if present) during LPM */
1081 if (motg->async_irq)
1082 enable_irq(motg->async_irq);
Sujeet Kumarceee6e82013-09-06 11:22:49 +05301083
1084 /* XO shutdown during idle , non wakeable irqs must be disabled */
1085 if (device_bus_suspend || host_bus_suspend || !motg->async_irq) {
1086 motg->ui_enabled = 1;
1087 enable_irq(motg->irq);
1088 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301090
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001091 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301092
1093 return 0;
1094}
1095
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301096static int msm_otg_resume(struct msm_otg *motg)
1097{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001098 struct usb_phy *phy = &motg->phy;
1099 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001100 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301101 int cnt = 0;
1102 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001103 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001104 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301105
1106 if (!atomic_read(&motg->in_lpm))
1107 return 0;
1108
Ido Shayevitz44ca03c2013-07-09 15:57:21 +03001109 if (motg->pdata->delay_lpm_hndshk_on_disconnect)
1110 msm_bam_notify_lpm_resume();
1111
Sujeet Kumarceee6e82013-09-06 11:22:49 +05301112 if (motg->ui_enabled) {
1113 motg->ui_enabled = 0;
1114 disable_irq(motg->irq);
1115 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001116 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301117
Manu Gautame3a39082013-06-11 10:42:56 +05301118 /* Some platforms require BUS vote to enable/disable clocks */
1119 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
1120
Anji jonnala7da3f262011-12-02 17:22:14 -08001121 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301122 if (motg->lpm_flags & XO_SHUTDOWN) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301123 if (!IS_ERR(motg->xo_clk)) {
1124 clk_prepare_enable(motg->xo_clk);
1125 } else {
1126 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1127 if (ret)
1128 dev_err(phy->dev, "%s fail to vote for XO %d\n",
1129 __func__, ret);
1130 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301131 motg->lpm_flags &= ~XO_SHUTDOWN;
1132 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301133
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001134 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001135 clk_prepare_enable(motg->core_clk);
1136 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001137 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001138 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301139
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001141 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001143 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1144 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1145 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001146 }
1147
1148 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301149 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301150 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001151 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1152 phy_ctrl_val |= PHY_RETEN;
1153 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1154 /* Disable PHY HV interrupts */
1155 phy_ctrl_val &=
1156 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301157 phy_ctrl_val &= ~(PHY_CLAMP_DPDMSE_EN);
Amit Blay58b31472011-11-18 09:39:39 +02001158 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301160 }
1161
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301162 temp = readl(USB_USBCMD);
1163 temp &= ~ASYNC_INTR_CTRL;
1164 temp &= ~ULPI_STP_CTRL;
1165 writel(temp, USB_USBCMD);
1166
1167 /*
1168 * PHY comes out of low power mode (LPM) in case of wakeup
1169 * from asynchronous interrupt.
1170 */
1171 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1172 goto skip_phy_resume;
1173
1174 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1175 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1176 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1177 break;
1178 udelay(1);
1179 cnt++;
1180 }
1181
1182 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1183 /*
1184 * This is a fatal error. Reset the link and
1185 * PHY. USB state can not be restored. Re-insertion
1186 * of USB cable is the only way to get USB working.
1187 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001188 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301189 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001190 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301191 }
1192
1193skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001194 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001195 if (motg->async_irq)
1196 disable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001197 else
1198 disable_irq_wake(motg->irq);
1199
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 if (motg->pdata->pmic_id_irq)
1201 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001202 if (pdata->otg_control == OTG_PHY_CONTROL &&
1203 pdata->mpm_otgsessvld_int)
1204 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301205 if (motg->host_bus_suspend && pdata->mpm_dpshv_int)
1206 msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 0);
1207 if (motg->host_bus_suspend && pdata->mpm_dmshv_int)
1208 msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301210 if (bus)
1211 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1212
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301213 atomic_set(&motg->in_lpm, 0);
1214
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301215 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001216 /* Match the disable_irq call from ISR */
1217 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301218 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301219 }
Sujeet Kumarceee6e82013-09-06 11:22:49 +05301220 motg->ui_enabled = 1;
Jack Pham8978b892012-10-17 16:31:39 -07001221 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301222
Manu Gautamf8c45642012-08-10 10:20:56 -07001223 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1224 if (motg->async_irq)
1225 disable_irq(motg->async_irq);
1226
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001227 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301228
1229 return 0;
1230}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301231#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301232
David Keitel272ce522012-08-17 16:25:24 -07001233static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001234{
Jack Pham0c695282012-10-19 18:13:03 -07001235 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001236 pr_err("No USB power supply registered!\n");
1237 return;
1238 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001239
Jack Pham0c695282012-10-19 18:13:03 -07001240 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001241 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301242 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001243 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301244 } else {
David Keitel272ce522012-08-17 16:25:24 -07001245 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301246 /*
1247 * VBUS comparator is disabled by PMIC charging driver
1248 * when SYSTEM scope is selected. For ID_GND->ID_A
1249 * transition, give 50 msec delay so that PMIC charger
1250 * driver detect the VBUS and ready for accepting
1251 * charging current value from USB.
1252 */
1253 if (test_bit(ID_A, &motg->inputs))
1254 msleep(50);
1255 }
David Keitel272ce522012-08-17 16:25:24 -07001256 } else {
1257 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001258 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001259 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001260}
1261
David Keitel081a3e22012-04-18 12:37:07 -07001262static int msm_otg_notify_chg_type(struct msm_otg *motg)
1263{
1264 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001265
David Keitel081a3e22012-04-18 12:37:07 -07001266 /*
1267 * TODO
1268 * Unify OTG driver charger types and power supply charger types
1269 */
1270 if (charger_type == motg->chg_type)
1271 return 0;
1272
1273 if (motg->chg_type == USB_SDP_CHARGER)
1274 charger_type = POWER_SUPPLY_TYPE_USB;
1275 else if (motg->chg_type == USB_CDP_CHARGER)
1276 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301277 else if (motg->chg_type == USB_DCP_CHARGER ||
1278 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001279 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1280 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1281 motg->chg_type == USB_ACA_A_CHARGER ||
1282 motg->chg_type == USB_ACA_B_CHARGER ||
1283 motg->chg_type == USB_ACA_C_CHARGER))
1284 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1285 else
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301286 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001287
Jack Pham0c695282012-10-19 18:13:03 -07001288 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001289 pr_err("No USB power supply registered!\n");
1290 return -EINVAL;
1291 }
1292
1293 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001294 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001295 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001296}
1297
Amit Blay0f7edf72012-01-15 10:11:27 +02001298static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1299{
Jack Pham0c695282012-10-19 18:13:03 -07001300 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001301 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1302 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001303 }
1304
David Keitel272ce522012-08-17 16:25:24 -07001305 if (motg->cur_power == 0 && mA > 2) {
1306 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001307 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001308 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001309 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001310 goto psy_error;
1311 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1312 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001313 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001314 goto psy_error;
1315 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001316 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001317 goto psy_error;
Manu Gautamfca298c2013-06-05 15:11:54 +05301318 } else {
1319 if (power_supply_set_online(psy, true))
1320 goto psy_error;
1321 /* Current has changed (100/2 --> 500) */
1322 if (power_supply_set_current_limit(psy, 1000*mA))
1323 goto psy_error;
David Keitel272ce522012-08-17 16:25:24 -07001324 }
Manu Gautamfca298c2013-06-05 15:11:54 +05301325
Jack Pham0c695282012-10-19 18:13:03 -07001326 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001327 return 0;
1328
David Keitel272ce522012-08-17 16:25:24 -07001329psy_error:
1330 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001331 return -ENXIO;
1332}
1333
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301334static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1335{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001336 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301337
1338 if (g && g->is_a_peripheral)
1339 return;
1340
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301341 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1342 motg->chg_type == USB_ACA_A_CHARGER ||
1343 motg->chg_type == USB_ACA_B_CHARGER ||
1344 motg->chg_type == USB_ACA_C_CHARGER) &&
1345 mA > IDEV_ACA_CHG_LIMIT)
1346 mA = IDEV_ACA_CHG_LIMIT;
1347
David Keitel081a3e22012-04-18 12:37:07 -07001348 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001349 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001350 "Failed notifying %d charger type to PMIC\n",
1351 motg->chg_type);
1352
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301353 if (motg->cur_power == mA)
1354 return;
1355
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001356 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001357
1358 /*
1359 * Use Power Supply API if supported, otherwise fallback
1360 * to legacy pm8921 API.
1361 */
1362 if (msm_otg_notify_power_supply(motg, mA))
1363 pm8921_charger_vbus_draw(mA);
1364
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301365 motg->cur_power = mA;
1366}
1367
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001368static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301369{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001370 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301371
1372 /*
1373 * Gadget driver uses set_power method to notify about the
1374 * available current based on suspend/configured states.
1375 *
1376 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1377 * states when CDP/ACA is connected.
1378 */
1379 if (motg->chg_type == USB_SDP_CHARGER)
1380 msm_otg_notify_charger(motg, mA);
1381
1382 return 0;
1383}
1384
Steve Mucklef132c6c2012-06-06 18:30:57 -07001385static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301386{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001387 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301388 struct msm_otg_platform_data *pdata = motg->pdata;
1389 struct usb_hcd *hcd;
1390
1391 if (!otg->host)
1392 return;
1393
1394 hcd = bus_to_hcd(otg->host);
1395
1396 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001397 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301398
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301399 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001400 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301401 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1402
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301403 /*
1404 * Some boards have a switch cotrolled by gpio
1405 * to enable/disable internal HUB. Enable internal
1406 * HUB before kicking the host.
1407 */
1408 if (pdata->setup_gpio)
1409 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301410 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301411 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001412 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301413
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301414 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301415 /* HCD core reset all bits of PORTSC. select ULPI phy */
1416 writel_relaxed(0x80000000, USB_PORTSC);
1417
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301418 if (pdata->setup_gpio)
1419 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301420
1421 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001422 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301423 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301424 }
1425}
1426
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001427static int msm_otg_usbdev_notify(struct notifier_block *self,
1428 unsigned long action, void *priv)
1429{
1430 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001431 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301432 struct usb_device *udev = priv;
1433
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301434 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1435 goto out;
1436
Steve Mucklef132c6c2012-06-06 18:30:57 -07001437 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301438 goto out;
1439 /*
1440 * Interested in devices connected directly to the root hub.
1441 * ACA dock can supply IDEV_CHG irrespective devices connected
1442 * on the accessory port.
1443 */
1444 if (!udev->parent || udev->parent->parent ||
1445 motg->chg_type == USB_ACA_DOCK_CHARGER)
1446 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001447
1448 switch (action) {
1449 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301450 if (aca_enabled())
1451 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001452 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301453 pr_debug("B_CONN set\n");
1454 set_bit(B_CONN, &motg->inputs);
1455 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001456 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301457 /*
1458 * OTG PET: A-device must end session within
1459 * 10 sec after PET enumeration.
1460 */
1461 if (udev->quirks & USB_QUIRK_OTG_PET)
1462 msm_otg_start_timer(motg, TA_TST_MAINT,
1463 A_TST_MAINT);
1464 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301465 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001466 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001467 if (udev->actconfig)
1468 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1469 else
1470 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001471 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301472 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301473 break;
1474 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001475 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1476 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301477 pr_debug("B_CONN clear\n");
1478 clear_bit(B_CONN, &motg->inputs);
1479 /*
1480 * OTG PET: A-device must end session after
1481 * PET disconnection if it is enumerated
1482 * with bcdDevice[0] = 1. USB core sets
1483 * bus->otg_vbus_off for us. clear it here.
1484 */
1485 if (udev->bus->otg_vbus_off) {
1486 udev->bus->otg_vbus_off = 0;
1487 set_bit(A_BUS_DROP, &motg->inputs);
1488 }
1489 queue_work(system_nrt_wq, &motg->sm_work);
1490 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491 default:
1492 break;
1493 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301494 if (test_bit(ID_A, &motg->inputs))
1495 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1496 motg->mA_port);
1497out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001498 return NOTIFY_OK;
1499}
1500
Mayank Ranae3926882011-12-26 09:47:54 +05301501static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1502{
1503 int ret;
1504 static bool vbus_is_on;
1505
1506 if (vbus_is_on == on)
1507 return;
1508
1509 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301510 ret = motg->pdata->vbus_power(on);
1511 if (!ret)
1512 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301513 return;
1514 }
1515
1516 if (!vbus_otg) {
1517 pr_err("vbus_otg is NULL.");
1518 return;
1519 }
1520
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001521 /*
1522 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001523 * from usb before turning on the boost.
1524 * if exiting host mode disable the boost before enabling to draw
1525 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001526 */
Mayank Ranae3926882011-12-26 09:47:54 +05301527 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001528 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301529 ret = regulator_enable(vbus_otg);
1530 if (ret) {
1531 pr_err("unable to enable vbus_otg\n");
1532 return;
1533 }
1534 vbus_is_on = true;
1535 } else {
1536 ret = regulator_disable(vbus_otg);
1537 if (ret) {
1538 pr_err("unable to disable vbus_otg\n");
1539 return;
1540 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001541 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301542 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301543 }
1544}
1545
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001546static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301547{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001548 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301549 struct usb_hcd *hcd;
1550
1551 /*
1552 * Fail host registration if this board can support
1553 * only peripheral configuration.
1554 */
1555 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001556 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301557 return -ENODEV;
1558 }
1559
Mayank Ranae3926882011-12-26 09:47:54 +05301560 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001561 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301562 if (IS_ERR(vbus_otg)) {
1563 pr_err("Unable to get vbus_otg\n");
Manu Gautam139a8132013-06-04 16:46:50 +05301564 return PTR_ERR(vbus_otg);
Mayank Ranae3926882011-12-26 09:47:54 +05301565 }
1566 }
1567
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301568 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001569 if (otg->phy->state == OTG_STATE_A_HOST) {
1570 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001571 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301572 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301573 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301574 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001575 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301576 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301577 } else {
1578 otg->host = NULL;
1579 }
1580
1581 return 0;
1582 }
1583
1584 hcd = bus_to_hcd(host);
1585 hcd->power_budget = motg->pdata->power_budget;
1586
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301587#ifdef CONFIG_USB_OTG
1588 host->otg_port = 1;
1589#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001590 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1591 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301592 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001593 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301594
1595 /*
1596 * Kick the state machine work, if peripheral is not supported
1597 * or peripheral is already registered with us.
1598 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301599 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001600 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301601 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301602 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301603
1604 return 0;
1605}
1606
Steve Mucklef132c6c2012-06-06 18:30:57 -07001607static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301608{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001609 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301610 struct msm_otg_platform_data *pdata = motg->pdata;
1611
1612 if (!otg->gadget)
1613 return;
1614
1615 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001616 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301617 /*
1618 * Some boards have a switch cotrolled by gpio
1619 * to enable/disable internal HUB. Disable internal
1620 * HUB before kicking the gadget.
1621 */
1622 if (pdata->setup_gpio)
1623 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001624
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301625 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautame3a39082013-06-11 10:42:56 +05301626 if (debug_bus_voting_enabled)
1627 msm_otg_bus_vote(motg, USB_MAX_PERF_VOTE);
1628
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301629 usb_gadget_vbus_connect(otg->gadget);
1630 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001631 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301632 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301633 /* Configure BUS performance parameters to default */
Manu Gautame3a39082013-06-11 10:42:56 +05301634 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301635 if (pdata->setup_gpio)
1636 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1637 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301638}
1639
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001640static int msm_otg_set_peripheral(struct usb_otg *otg,
Stephen Boyd9850acb2013-01-28 14:11:20 -08001641 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301642{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001643 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301644
1645 /*
1646 * Fail peripheral registration if this board can support
1647 * only host configuration.
1648 */
1649 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001650 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301651 return -ENODEV;
1652 }
1653
1654 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001655 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1656 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301657 msm_otg_start_peripheral(otg, 0);
1658 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001659 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301660 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301661 } else {
1662 otg->gadget = NULL;
1663 }
1664
1665 return 0;
1666 }
1667 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001668 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301669
1670 /*
1671 * Kick the state machine work, if host is not supported
1672 * or host is already registered with us.
1673 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301674 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001675 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301676 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301677 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301678
1679 return 0;
1680}
1681
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301682static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1683{
1684 unsigned long flags;
1685 int id;
1686
1687 if (!motg->pdata->pmic_id_irq)
1688 return -ENODEV;
1689
1690 local_irq_save(flags);
1691 id = irq_read_line(motg->pdata->pmic_id_irq);
1692 local_irq_restore(flags);
1693
1694 /*
1695 * If we can not read ID line state for some reason, treat
1696 * it as float. This would prevent MHL discovery and kicking
1697 * host mode unnecessarily.
1698 */
1699 return !!id;
1700}
1701
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301702static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1703 void (*callback)(int on))
1704{
1705 struct usb_phy *phy = &motg->phy;
1706 int ret;
1707
Manoj Raoa7bddd12012-08-27 20:36:45 -07001708 if (!motg->pdata->mhl_enable) {
1709 dev_dbg(phy->dev, "MHL feature not enabled\n");
1710 return -ENODEV;
1711 }
1712
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301713 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1714 !motg->pdata->pmic_id_irq) {
1715 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1716 return -ENODEV;
1717 }
1718
1719 if (!motg->pdata->mhl_dev_name) {
1720 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1721 return -ENODEV;
1722 }
1723
1724 if (callback)
1725 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1726 callback);
1727 else
1728 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1729
1730 if (ret)
1731 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1732 motg->pdata->mhl_dev_name, ret);
1733 else
1734 motg->mhl_enabled = true;
1735
1736 return ret;
1737}
1738
1739static void msm_otg_mhl_notify_online(int on)
1740{
1741 struct msm_otg *motg = the_msm_otg;
1742 struct usb_phy *phy = &motg->phy;
1743 bool queue = false;
1744
1745 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1746
1747 if (on) {
1748 set_bit(MHL, &motg->inputs);
1749 } else {
1750 clear_bit(MHL, &motg->inputs);
1751 queue = true;
1752 }
1753
1754 if (queue && phy->state != OTG_STATE_UNDEFINED)
1755 schedule_work(&motg->sm_work);
1756}
1757
1758static bool msm_otg_is_mhl(struct msm_otg *motg)
1759{
1760 struct usb_phy *phy = &motg->phy;
1761 int is_mhl, ret;
1762
1763 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1764 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1765 /*
1766 * MHL driver calls our callback saying that MHL connected
1767 * if RID_GND is detected. But at later part of discovery
1768 * it may figure out MHL is not connected and returns
1769 * false. Hence clear MHL input here.
1770 */
1771 clear_bit(MHL, &motg->inputs);
1772 dev_dbg(phy->dev, "MHL device not found\n");
1773 return false;
1774 }
1775
1776 set_bit(MHL, &motg->inputs);
1777 dev_dbg(phy->dev, "MHL device found\n");
1778 return true;
1779}
1780
1781static bool msm_chg_mhl_detect(struct msm_otg *motg)
1782{
1783 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301784
1785 if (!motg->mhl_enabled)
1786 return false;
1787
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301788 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301789
1790 if (id)
1791 return false;
1792
1793 mhl_det_in_progress = true;
1794 ret = msm_otg_is_mhl(motg);
1795 mhl_det_in_progress = false;
1796
1797 return ret;
1798}
1799
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301800static void msm_otg_chg_check_timer_func(unsigned long data)
1801{
1802 struct msm_otg *motg = (struct msm_otg *) data;
1803 struct usb_otg *otg = motg->phy.otg;
1804
1805 if (atomic_read(&motg->in_lpm) ||
1806 !test_bit(B_SESS_VLD, &motg->inputs) ||
1807 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1808 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1809 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1810 return;
1811 }
1812
1813 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1814 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1815 set_bit(B_FALSE_SDP, &motg->inputs);
1816 queue_work(system_nrt_wq, &motg->sm_work);
1817 }
1818}
1819
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001820static bool msm_chg_aca_detect(struct msm_otg *motg)
1821{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001822 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823 u32 int_sts;
1824 bool ret = false;
1825
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301826 if (!aca_enabled())
1827 goto out;
1828
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001829 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1830 goto out;
1831
Steve Mucklef132c6c2012-06-06 18:30:57 -07001832 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001833 switch (int_sts & 0x1C) {
1834 case 0x08:
1835 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001836 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001837 motg->chg_type = USB_ACA_A_CHARGER;
1838 motg->chg_state = USB_CHG_STATE_DETECTED;
1839 clear_bit(ID_B, &motg->inputs);
1840 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301841 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 ret = true;
1843 }
1844 break;
1845 case 0x0C:
1846 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001847 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001848 motg->chg_type = USB_ACA_B_CHARGER;
1849 motg->chg_state = USB_CHG_STATE_DETECTED;
1850 clear_bit(ID_A, &motg->inputs);
1851 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301852 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001853 ret = true;
1854 }
1855 break;
1856 case 0x10:
1857 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001858 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001859 motg->chg_type = USB_ACA_C_CHARGER;
1860 motg->chg_state = USB_CHG_STATE_DETECTED;
1861 clear_bit(ID_A, &motg->inputs);
1862 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301863 set_bit(ID, &motg->inputs);
1864 ret = true;
1865 }
1866 break;
1867 case 0x04:
1868 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001869 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301870 motg->chg_type = USB_INVALID_CHARGER;
1871 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1872 clear_bit(ID_A, &motg->inputs);
1873 clear_bit(ID_B, &motg->inputs);
1874 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001875 ret = true;
1876 }
1877 break;
1878 default:
1879 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1880 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301881 test_and_clear_bit(ID_C, &motg->inputs) |
1882 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001883 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001884 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001885 motg->chg_type = USB_INVALID_CHARGER;
1886 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1887 }
1888 }
1889out:
1890 return ret;
1891}
1892
1893static void msm_chg_enable_aca_det(struct msm_otg *motg)
1894{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001895 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001896
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301897 if (!aca_enabled())
1898 return;
1899
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001900 switch (motg->pdata->phy_type) {
1901 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301902 /* Disable ID_GND in link and PHY */
1903 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1904 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001905 ulpi_write(phy, 0x01, 0x0C);
1906 ulpi_write(phy, 0x10, 0x0F);
1907 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301908 /* Disable PMIC ID pull-up */
1909 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301910 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001911 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301912 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001913 break;
1914 default:
1915 break;
1916 }
1917}
1918
1919static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1920{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001921 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301923 if (!aca_enabled())
1924 return;
1925
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001926 switch (motg->pdata->phy_type) {
1927 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301928 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001929 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301930 break;
1931 default:
1932 break;
1933 }
1934}
1935
1936static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1937{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001938 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301939
1940 if (!aca_enabled())
1941 return;
1942
1943 switch (motg->pdata->phy_type) {
1944 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001945 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001946 break;
1947 default:
1948 break;
1949 }
1950}
1951
1952static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1953{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001954 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001955 bool ret = false;
1956
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301957 if (!aca_enabled())
1958 return ret;
1959
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001960 switch (motg->pdata->phy_type) {
1961 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001962 if (ulpi_read(phy, 0x91) & 1) {
1963 dev_dbg(phy->dev, "RID change\n");
1964 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001965 ret = msm_chg_aca_detect(motg);
1966 }
1967 default:
1968 break;
1969 }
1970 return ret;
1971}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301972
1973static void msm_otg_id_timer_func(unsigned long data)
1974{
1975 struct msm_otg *motg = (struct msm_otg *) data;
1976
1977 if (!aca_enabled())
1978 return;
1979
1980 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001981 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301982 return;
1983 }
1984
Steve Mucklef132c6c2012-06-06 18:30:57 -07001985 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301986 goto out;
1987
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301988 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001989 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301990 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301991 }
1992
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301993out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301994 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1995 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1996}
1997
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301998static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1999{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002000 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302001 u32 chg_det;
2002 bool ret = false;
2003
2004 switch (motg->pdata->phy_type) {
2005 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002006 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302007 ret = chg_det & (1 << 4);
2008 break;
2009 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002010 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302011 ret = chg_det & 1;
2012 break;
2013 default:
2014 break;
2015 }
2016 return ret;
2017}
2018
2019static void msm_chg_enable_secondary_det(struct msm_otg *motg)
2020{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002021 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302022 u32 chg_det;
2023
2024 switch (motg->pdata->phy_type) {
2025 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002026 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302027 /* Turn off charger block */
2028 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002029 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302030 udelay(20);
2031 /* control chg block via ULPI */
2032 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002033 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302034 /* put it in host mode for enabling D- source */
2035 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002036 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302037 /* Turn on chg detect block */
2038 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002039 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302040 udelay(20);
2041 /* enable chg detection */
2042 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002043 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302044 break;
2045 case SNPS_28NM_INTEGRATED_PHY:
2046 /*
2047 * Configure DM as current source, DP as current sink
2048 * and enable battery charging comparators.
2049 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002050 ulpi_write(phy, 0x8, 0x85);
2051 ulpi_write(phy, 0x2, 0x85);
2052 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302053 break;
2054 default:
2055 break;
2056 }
2057}
2058
2059static bool msm_chg_check_primary_det(struct msm_otg *motg)
2060{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002061 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302062 u32 chg_det;
2063 bool ret = false;
2064
2065 switch (motg->pdata->phy_type) {
2066 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002067 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302068 ret = chg_det & (1 << 4);
2069 break;
2070 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002071 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302072 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302073 /* Turn off VDP_SRC */
2074 ulpi_write(phy, 0x3, 0x86);
2075 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302076 break;
2077 default:
2078 break;
2079 }
2080 return ret;
2081}
2082
2083static void msm_chg_enable_primary_det(struct msm_otg *motg)
2084{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002085 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302086 u32 chg_det;
2087
2088 switch (motg->pdata->phy_type) {
2089 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002090 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302091 /* enable chg detection */
2092 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002093 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302094 break;
2095 case SNPS_28NM_INTEGRATED_PHY:
2096 /*
2097 * Configure DP as current source, DM as current sink
2098 * and enable battery charging comparators.
2099 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002100 ulpi_write(phy, 0x2, 0x85);
2101 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302102 break;
2103 default:
2104 break;
2105 }
2106}
2107
2108static bool msm_chg_check_dcd(struct msm_otg *motg)
2109{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002110 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302111 u32 line_state;
2112 bool ret = false;
2113
2114 switch (motg->pdata->phy_type) {
2115 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002116 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302117 ret = !(line_state & 1);
2118 break;
2119 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002120 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302121 ret = line_state & 2;
2122 break;
2123 default:
2124 break;
2125 }
2126 return ret;
2127}
2128
2129static void msm_chg_disable_dcd(struct msm_otg *motg)
2130{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002131 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302132 u32 chg_det;
2133
2134 switch (motg->pdata->phy_type) {
2135 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002136 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302137 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002138 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302139 break;
2140 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002141 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302142 break;
2143 default:
2144 break;
2145 }
2146}
2147
2148static void msm_chg_enable_dcd(struct msm_otg *motg)
2149{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002150 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302151 u32 chg_det;
2152
2153 switch (motg->pdata->phy_type) {
2154 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002155 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302156 /* Turn on D+ current source */
2157 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002158 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302159 break;
2160 case SNPS_28NM_INTEGRATED_PHY:
2161 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002162 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302163 break;
2164 default:
2165 break;
2166 }
2167}
2168
2169static void msm_chg_block_on(struct msm_otg *motg)
2170{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002171 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302172 u32 func_ctrl, chg_det;
2173
2174 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002175 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302176 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2177 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002178 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302179
2180 switch (motg->pdata->phy_type) {
2181 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002182 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302183 /* control chg block via ULPI */
2184 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002185 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302186 /* Turn on chg detect block */
2187 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002188 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302189 udelay(20);
2190 break;
2191 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302192 /* disable DP and DM pull down resistors */
2193 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302194 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002195 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302196 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002197 ulpi_write(phy, 0x1F, 0x92);
2198 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302199 udelay(100);
2200 break;
2201 default:
2202 break;
2203 }
2204}
2205
2206static void msm_chg_block_off(struct msm_otg *motg)
2207{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002208 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302209 u32 func_ctrl, chg_det;
2210
2211 switch (motg->pdata->phy_type) {
2212 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002213 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302214 /* Turn off charger block */
2215 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002216 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302217 break;
2218 case SNPS_28NM_INTEGRATED_PHY:
2219 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002220 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302221 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002222 ulpi_write(phy, 0x1F, 0x92);
2223 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetic4c69762013-07-08 15:03:26 +05302224 /* re-enable DP and DM pull down resistors */
2225 ulpi_write(phy, 0x6, 0xB);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302226 break;
2227 default:
2228 break;
2229 }
2230
2231 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002232 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302233 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2234 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002235 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302236}
2237
Anji jonnalad270e2d2011-08-09 11:28:32 +05302238static const char *chg_to_string(enum usb_chg_type chg_type)
2239{
2240 switch (chg_type) {
2241 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2242 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2243 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2244 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2245 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2246 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2247 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302248 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
zhenhuahb6d5edc2013-06-19 14:37:32 +08002249 case USB_FLOATED_CHARGER: return "USB_FLOATED_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302250 default: return "INVALID_CHARGER";
2251 }
2252}
2253
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302254#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2255#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302256#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2257#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302258static void msm_chg_detect_work(struct work_struct *w)
2259{
2260 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002261 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302262 bool is_dcd = false, tmout, vout, is_aca;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002263 static bool dcd;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302264 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302265 unsigned long delay;
2266
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002267 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302268
2269 if (test_bit(MHL, &motg->inputs)) {
2270 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2271 return;
2272 }
2273
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302274 switch (motg->chg_state) {
2275 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302276 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302277 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002278 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302279 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302280 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302281 delay = MSM_CHG_DCD_POLL_TIME;
2282 break;
2283 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302284 if (msm_chg_mhl_detect(motg)) {
2285 msm_chg_block_off(motg);
2286 motg->chg_state = USB_CHG_STATE_DETECTED;
2287 motg->chg_type = USB_INVALID_CHARGER;
2288 queue_work(system_nrt_wq, &motg->sm_work);
2289 return;
2290 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002291 is_aca = msm_chg_aca_detect(motg);
2292 if (is_aca) {
2293 /*
2294 * ID_A can be ACA dock too. continue
2295 * primary detection after DCD.
2296 */
2297 if (test_bit(ID_A, &motg->inputs)) {
2298 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2299 } else {
2300 delay = 0;
2301 break;
2302 }
2303 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302304 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302305 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2306 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302307 if (is_dcd || tmout) {
zhenhuahb6d5edc2013-06-19 14:37:32 +08002308 if (is_dcd)
2309 dcd = true;
2310 else
2311 dcd = false;
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302312 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302313 msm_chg_enable_primary_det(motg);
2314 delay = MSM_CHG_PRIMARY_DET_TIME;
2315 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2316 } else {
2317 delay = MSM_CHG_DCD_POLL_TIME;
2318 }
2319 break;
2320 case USB_CHG_STATE_DCD_DONE:
2321 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302322 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2323 dm_vlgc = line_state & PORTSC_LS_DM;
2324 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302325 if (test_bit(ID_A, &motg->inputs)) {
2326 motg->chg_type = USB_ACA_DOCK_CHARGER;
2327 motg->chg_state = USB_CHG_STATE_DETECTED;
2328 delay = 0;
2329 break;
2330 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302331 if (line_state) { /* DP > VLGC */
2332 motg->chg_type = USB_PROPRIETARY_CHARGER;
2333 motg->chg_state = USB_CHG_STATE_DETECTED;
2334 delay = 0;
2335 } else {
2336 msm_chg_enable_secondary_det(motg);
2337 delay = MSM_CHG_SECONDARY_DET_TIME;
2338 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2339 }
2340 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302341 if (test_bit(ID_A, &motg->inputs)) {
2342 motg->chg_type = USB_ACA_A_CHARGER;
2343 motg->chg_state = USB_CHG_STATE_DETECTED;
2344 delay = 0;
2345 break;
2346 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302347
2348 if (line_state) /* DP > VLGC or/and DM > VLGC */
2349 motg->chg_type = USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002350 else if (!dcd && floated_charger_enable)
2351 motg->chg_type = USB_FLOATED_CHARGER;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302352 else
2353 motg->chg_type = USB_SDP_CHARGER;
2354
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302355 motg->chg_state = USB_CHG_STATE_DETECTED;
2356 delay = 0;
2357 }
2358 break;
2359 case USB_CHG_STATE_PRIMARY_DONE:
2360 vout = msm_chg_check_secondary_det(motg);
2361 if (vout)
2362 motg->chg_type = USB_DCP_CHARGER;
2363 else
2364 motg->chg_type = USB_CDP_CHARGER;
2365 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2366 /* fall through */
2367 case USB_CHG_STATE_SECONDARY_DONE:
2368 motg->chg_state = USB_CHG_STATE_DETECTED;
2369 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302370 /*
2371 * Notify the charger type to power supply
2372 * owner as soon as we determine the charger.
2373 */
2374 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302375 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002376 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302377 /*
2378 * Spurious interrupt is seen after enabling ACA detection
2379 * due to which charger detection fails in case of PET.
2380 * Add delay of 100 microsec to avoid that.
2381 */
2382 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002383 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002384 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302385 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302386 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302387 return;
2388 default:
2389 return;
2390 }
2391
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302392 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302393}
2394
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302395/*
2396 * We support OTG, Peripheral only and Host only configurations. In case
2397 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2398 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2399 * enabled when switch is controlled by user and default mode is supplied
2400 * by board file, which can be changed by userspace later.
2401 */
2402static void msm_otg_init_sm(struct msm_otg *motg)
2403{
2404 struct msm_otg_platform_data *pdata = motg->pdata;
2405 u32 otgsc = readl(USB_OTGSC);
2406
2407 switch (pdata->mode) {
2408 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002409 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302410 if (pdata->default_mode == USB_HOST) {
2411 clear_bit(ID, &motg->inputs);
2412 } else if (pdata->default_mode == USB_PERIPHERAL) {
2413 set_bit(ID, &motg->inputs);
2414 set_bit(B_SESS_VLD, &motg->inputs);
2415 } else {
2416 set_bit(ID, &motg->inputs);
2417 clear_bit(B_SESS_VLD, &motg->inputs);
2418 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302419 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302420 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302421 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302422 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302423 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302424 set_bit(A_BUS_REQ, &motg->inputs);
2425 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002426 if (otgsc & OTGSC_BSV)
2427 set_bit(B_SESS_VLD, &motg->inputs);
2428 else
2429 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302430 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302431 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302432 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302433 set_bit(ID, &motg->inputs);
2434 else
2435 clear_bit(ID, &motg->inputs);
2436 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302437 /*
2438 * VBUS initial state is reported after PMIC
2439 * driver initialization. Wait for it.
2440 */
2441 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302442 }
2443 break;
2444 case USB_HOST:
2445 clear_bit(ID, &motg->inputs);
2446 break;
2447 case USB_PERIPHERAL:
2448 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302449 if (pdata->otg_control == OTG_PHY_CONTROL) {
2450 if (otgsc & OTGSC_BSV)
2451 set_bit(B_SESS_VLD, &motg->inputs);
2452 else
2453 clear_bit(B_SESS_VLD, &motg->inputs);
2454 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2455 /*
2456 * VBUS initial state is reported after PMIC
2457 * driver initialization. Wait for it.
2458 */
2459 wait_for_completion(&pmic_vbus_init);
2460 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302461 break;
2462 default:
2463 break;
2464 }
2465}
2466
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302467static void msm_otg_wait_for_ext_chg_done(struct msm_otg *motg)
2468{
2469 struct usb_phy *phy = &motg->phy;
2470 unsigned long t;
2471
2472 /*
2473 * Defer next cable connect event till external charger
2474 * detection is completed.
2475 */
2476
2477 if (motg->ext_chg_active) {
2478
2479 pr_debug("before msm_otg ext chg wait\n");
2480
2481 t = wait_for_completion_timeout(&motg->ext_chg_wait,
2482 msecs_to_jiffies(3000));
2483 if (!t)
2484 pr_err("msm_otg ext chg wait timeout\n");
2485 else
2486 pr_debug("msm_otg ext chg wait done\n");
2487 }
2488
2489 if (motg->ext_chg_opened) {
2490 if (phy->flags & ENABLE_DP_MANUAL_PULLUP) {
2491 ulpi_write(phy, ULPI_MISC_A_VBUSVLDEXT |
2492 ULPI_MISC_A_VBUSVLDEXTSEL,
2493 ULPI_CLR(ULPI_MISC_A));
2494 }
2495 /* clear charging register bits */
2496 ulpi_write(phy, 0x3F, 0x86);
2497 /* re-enable DP and DM pull-down resistors*/
2498 ulpi_write(phy, 0x6, 0xB);
2499 }
2500}
2501
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302502static void msm_otg_sm_work(struct work_struct *w)
2503{
2504 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002505 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302506 bool work = 0, srp_reqd, dcp;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302507
Steve Mucklef132c6c2012-06-06 18:30:57 -07002508 pm_runtime_resume(otg->phy->dev);
2509 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002510 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302511 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002512 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302513 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002514 if (!psy && legacy_power_supply) {
2515 psy = power_supply_get_by_name("usb");
2516
2517 if (!psy)
2518 pr_err("couldn't get usb power supply\n");
2519 }
2520
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002521 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302522 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2523 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002524 pm_runtime_put_noidle(otg->phy->dev);
2525 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302526 break;
2527 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302528 /* FALL THROUGH */
2529 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302530 if (test_bit(MHL, &motg->inputs)) {
2531 /* allow LPM */
2532 pm_runtime_put_noidle(otg->phy->dev);
2533 pm_runtime_suspend(otg->phy->dev);
2534 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002535 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302536 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302537 if (msm_chg_mhl_detect(motg)) {
2538 work = 1;
2539 break;
2540 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302541 clear_bit(B_BUS_REQ, &motg->inputs);
2542 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002543 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302544 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302545 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302546 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302547 switch (motg->chg_state) {
2548 case USB_CHG_STATE_UNDEFINED:
2549 msm_chg_detect_work(&motg->chg_work.work);
2550 break;
2551 case USB_CHG_STATE_DETECTED:
2552 switch (motg->chg_type) {
2553 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302554 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002555 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302556 if (motg->ext_chg_opened) {
2557 init_completion(
2558 &motg->ext_chg_wait);
2559 motg->ext_chg_active = true;
2560 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302561 /* fall through */
2562 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302563 msm_otg_notify_charger(motg,
2564 IDEV_CHG_MAX);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302565 pm_runtime_put_sync(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302566 break;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002567 case USB_FLOATED_CHARGER:
2568 msm_otg_notify_charger(motg,
2569 IDEV_CHG_MAX);
2570 pm_runtime_put_noidle(otg->phy->dev);
2571 pm_runtime_suspend(otg->phy->dev);
2572 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302573 case USB_ACA_B_CHARGER:
2574 msm_otg_notify_charger(motg,
2575 IDEV_ACA_CHG_MAX);
2576 /*
2577 * (ID_B --> ID_C) PHY_ALT interrupt can
2578 * not be detected in LPM.
2579 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302580 break;
2581 case USB_CDP_CHARGER:
2582 msm_otg_notify_charger(motg,
2583 IDEV_CHG_MAX);
2584 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002585 otg->phy->state =
2586 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302587 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302588 case USB_ACA_C_CHARGER:
2589 msm_otg_notify_charger(motg,
2590 IDEV_ACA_CHG_MAX);
2591 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002592 otg->phy->state =
2593 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302594 break;
2595 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302596 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002597 otg->phy->state =
2598 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302599 mod_timer(&motg->chg_check_timer,
2600 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302601 break;
2602 default:
2603 break;
2604 }
2605 break;
2606 default:
2607 break;
2608 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302609 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2610 pr_debug("b_sess_end && b_bus_req\n");
2611 if (msm_otg_start_srp(otg) < 0) {
2612 clear_bit(B_BUS_REQ, &motg->inputs);
2613 work = 1;
2614 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302615 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002616 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302617 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2618 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302619 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302620 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302621 del_timer_sync(&motg->chg_check_timer);
2622 clear_bit(B_FALSE_SDP, &motg->inputs);
Mayank Rana8ab00352013-01-23 19:26:21 +05302623 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302624 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302625 dcp = (motg->chg_type == USB_DCP_CHARGER);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302626 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2627 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302628 msm_otg_notify_charger(motg, 0);
Manu Gautam6604b682013-09-04 17:50:17 +05302629 if (dcp) {
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302630 msm_otg_wait_for_ext_chg_done(motg);
Manu Gautam6604b682013-09-04 17:50:17 +05302631 /* Turn off VDP_SRC */
2632 ulpi_write(otg->phy, 0x2, 0x86);
2633 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002634 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302635 /*
2636 * There is a small window where ID interrupt
2637 * is not monitored during ID detection circuit
2638 * switch from ACA to PMIC. Check ID state
2639 * before entering into low power mode.
2640 */
2641 if (!msm_otg_read_pmic_id_state(motg)) {
2642 pr_debug("process missed ID intr\n");
2643 clear_bit(ID, &motg->inputs);
2644 work = 1;
2645 break;
2646 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002647 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002648 /*
2649 * Only if autosuspend was enabled in probe, it will be
2650 * used here. Otherwise, no delay will be used.
2651 */
2652 pm_runtime_mark_last_busy(otg->phy->dev);
2653 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302654 }
2655 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302656 case OTG_STATE_B_SRP_INIT:
2657 if (!test_bit(ID, &motg->inputs) ||
2658 test_bit(ID_A, &motg->inputs) ||
2659 test_bit(ID_C, &motg->inputs) ||
2660 (test_bit(B_SESS_VLD, &motg->inputs) &&
2661 !test_bit(ID_B, &motg->inputs))) {
2662 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2663 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002664 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302665 /*
2666 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2667 * bits after SRP initiation.
2668 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002669 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302670 work = 1;
2671 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2672 pr_debug("b_srp_fail\n");
2673 pr_info("A-device did not respond to SRP\n");
2674 clear_bit(B_BUS_REQ, &motg->inputs);
2675 clear_bit(B_SRP_FAIL, &motg->tmouts);
2676 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002677 ulpi_write(otg->phy, 0x0, 0x98);
2678 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302679 motg->b_last_se0_sess = jiffies;
2680 work = 1;
2681 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302682 break;
2683 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302684 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2685 test_bit(B_FALSE_SDP, &motg->inputs)) {
2686 pr_debug("B_FALSE_SDP\n");
2687 msm_otg_start_peripheral(otg, 0);
2688 motg->chg_type = USB_DCP_CHARGER;
2689 clear_bit(B_FALSE_SDP, &motg->inputs);
2690 otg->phy->state = OTG_STATE_B_IDLE;
2691 work = 1;
2692 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302693 test_bit(ID_A, &motg->inputs) ||
2694 test_bit(ID_B, &motg->inputs) ||
2695 !test_bit(B_SESS_VLD, &motg->inputs)) {
2696 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302697 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2698 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302699 msm_otg_notify_charger(motg, 0);
2700 srp_reqd = otg->gadget->otg_srp_reqd;
2701 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302702 if (test_bit(ID_B, &motg->inputs))
2703 clear_bit(ID_B, &motg->inputs);
2704 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002705 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302706 motg->b_last_se0_sess = jiffies;
2707 if (srp_reqd)
2708 msm_otg_start_timer(motg,
2709 TB_TST_SRP, B_TST_SRP);
2710 else
2711 work = 1;
2712 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2713 otg->gadget->b_hnp_enable &&
2714 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2715 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2716 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2717 /* D+ pullup should not be disconnected within 4msec
2718 * after A device suspends the bus. Otherwise PET will
2719 * fail the compliance test.
2720 */
2721 udelay(1000);
2722 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002723 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302724 /*
2725 * start HCD even before A-device enable
2726 * pull-up to meet HNP timings.
2727 */
2728 otg->host->is_b_host = 1;
2729 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002730 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2731 test_bit(B_SESS_VLD, &motg->inputs)) {
2732 pr_debug("a_bus_suspend && b_sess_vld\n");
2733 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002734 pm_runtime_put_noidle(otg->phy->dev);
2735 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002736 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002737 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302738 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002739 }
2740 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302741 case OTG_STATE_B_WAIT_ACON:
2742 if (!test_bit(ID, &motg->inputs) ||
2743 test_bit(ID_A, &motg->inputs) ||
2744 test_bit(ID_B, &motg->inputs) ||
2745 !test_bit(B_SESS_VLD, &motg->inputs)) {
2746 pr_debug("!id || id_a/b || !b_sess_vld\n");
2747 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302748 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302749 * A-device is physically disconnected during
2750 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302751 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302752 msm_otg_start_host(otg, 0);
2753 otg->host->is_b_host = 0;
2754
2755 clear_bit(B_BUS_REQ, &motg->inputs);
2756 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2757 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002758 otg->phy->state = OTG_STATE_B_IDLE;
2759 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302760 work = 1;
2761 } else if (test_bit(A_CONN, &motg->inputs)) {
2762 pr_debug("a_conn\n");
2763 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002764 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302765 /*
2766 * PET disconnects D+ pullup after reset is generated
2767 * by B device in B_HOST role which is not detected by
2768 * B device. As workaorund , start timer of 300msec
2769 * and stop timer if A device is enumerated else clear
2770 * A_CONN.
2771 */
2772 msm_otg_start_timer(motg, TB_TST_CONFIG,
2773 B_TST_CONFIG);
2774 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2775 pr_debug("b_ase0_brst_tmout\n");
2776 pr_info("B HNP fail:No response from A device\n");
2777 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002778 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302779 otg->host->is_b_host = 0;
2780 clear_bit(B_ASE0_BRST, &motg->tmouts);
2781 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2782 clear_bit(B_BUS_REQ, &motg->inputs);
2783 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002784 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302785 work = 1;
2786 } else if (test_bit(ID_C, &motg->inputs)) {
2787 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2788 }
2789 break;
2790 case OTG_STATE_B_HOST:
2791 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2792 !test_bit(A_CONN, &motg->inputs) ||
2793 !test_bit(B_SESS_VLD, &motg->inputs)) {
2794 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2795 clear_bit(A_CONN, &motg->inputs);
2796 clear_bit(B_BUS_REQ, &motg->inputs);
2797 msm_otg_start_host(otg, 0);
2798 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002799 otg->phy->state = OTG_STATE_B_IDLE;
2800 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302801 work = 1;
2802 } else if (test_bit(ID_C, &motg->inputs)) {
2803 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2804 }
2805 break;
2806 case OTG_STATE_A_IDLE:
2807 otg->default_a = 1;
2808 if (test_bit(ID, &motg->inputs) &&
2809 !test_bit(ID_A, &motg->inputs)) {
2810 pr_debug("id && !id_a\n");
2811 otg->default_a = 0;
2812 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002813 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302814 del_timer_sync(&motg->id_timer);
2815 msm_otg_link_reset(motg);
2816 msm_chg_enable_aca_intr(motg);
2817 msm_otg_notify_charger(motg, 0);
2818 work = 1;
2819 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2820 (test_bit(A_SRP_DET, &motg->inputs) ||
2821 test_bit(A_BUS_REQ, &motg->inputs))) {
2822 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2823
2824 clear_bit(A_SRP_DET, &motg->inputs);
2825 /* Disable SRP detection */
2826 writel_relaxed((readl_relaxed(USB_OTGSC) &
2827 ~OTGSC_INTSTS_MASK) &
2828 ~OTGSC_DPIE, USB_OTGSC);
2829
Steve Mucklef132c6c2012-06-06 18:30:57 -07002830 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302831 /* VBUS should not be supplied before end of SRP pulse
2832 * generated by PET, if not complaince test fail.
2833 */
2834 usleep_range(10000, 12000);
2835 /* ACA: ID_A: Stop charging untill enumeration */
2836 if (test_bit(ID_A, &motg->inputs))
2837 msm_otg_notify_charger(motg, 0);
2838 else
2839 msm_hsusb_vbus_power(motg, 1);
2840 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2841 } else {
2842 pr_debug("No session requested\n");
2843 clear_bit(A_BUS_DROP, &motg->inputs);
2844 if (test_bit(ID_A, &motg->inputs)) {
2845 msm_otg_notify_charger(motg,
2846 IDEV_ACA_CHG_MAX);
2847 } else if (!test_bit(ID, &motg->inputs)) {
2848 msm_otg_notify_charger(motg, 0);
2849 /*
2850 * A-device is not providing power on VBUS.
2851 * Enable SRP detection.
2852 */
2853 writel_relaxed(0x13, USB_USBMODE);
2854 writel_relaxed((readl_relaxed(USB_OTGSC) &
2855 ~OTGSC_INTSTS_MASK) |
2856 OTGSC_DPIE, USB_OTGSC);
2857 mb();
2858 }
2859 }
2860 break;
2861 case OTG_STATE_A_WAIT_VRISE:
2862 if ((test_bit(ID, &motg->inputs) &&
2863 !test_bit(ID_A, &motg->inputs)) ||
2864 test_bit(A_BUS_DROP, &motg->inputs) ||
2865 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2866 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2867 clear_bit(A_BUS_REQ, &motg->inputs);
2868 msm_otg_del_timer(motg);
2869 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002870 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302871 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2872 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2873 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002874 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302875 if (TA_WAIT_BCON > 0)
2876 msm_otg_start_timer(motg, TA_WAIT_BCON,
2877 A_WAIT_BCON);
Sujeet Kumarebaef0c2013-06-03 15:54:25 +05302878
2879 /* Clear BSV in host mode */
2880 clear_bit(B_SESS_VLD, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302881 msm_otg_start_host(otg, 1);
2882 msm_chg_enable_aca_det(motg);
2883 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302884 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302885 if (msm_chg_check_aca_intr(motg))
2886 work = 1;
2887 }
2888 break;
2889 case OTG_STATE_A_WAIT_BCON:
2890 if ((test_bit(ID, &motg->inputs) &&
2891 !test_bit(ID_A, &motg->inputs)) ||
2892 test_bit(A_BUS_DROP, &motg->inputs) ||
2893 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2894 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2895 "a_wait_bcon_tmout\n");
2896 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2897 pr_info("Device No Response\n");
2898 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2899 }
2900 msm_otg_del_timer(motg);
2901 clear_bit(A_BUS_REQ, &motg->inputs);
2902 clear_bit(B_CONN, &motg->inputs);
2903 msm_otg_start_host(otg, 0);
2904 /*
2905 * ACA: ID_A with NO accessory, just the A plug is
2906 * attached to ACA: Use IDCHG_MAX for charging
2907 */
2908 if (test_bit(ID_A, &motg->inputs))
2909 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2910 else
2911 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002912 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302913 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2914 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2915 pr_debug("!a_vbus_vld\n");
2916 clear_bit(B_CONN, &motg->inputs);
2917 msm_otg_del_timer(motg);
2918 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002919 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2920 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302921 } else if (test_bit(ID_A, &motg->inputs)) {
2922 msm_hsusb_vbus_power(motg, 0);
2923 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2924 /*
2925 * If TA_WAIT_BCON is infinite, we don;t
2926 * turn off VBUS. Enter low power mode.
2927 */
2928 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002929 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302930 } else if (!test_bit(ID, &motg->inputs)) {
2931 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302932 }
2933 break;
2934 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302935 if ((test_bit(ID, &motg->inputs) &&
2936 !test_bit(ID_A, &motg->inputs)) ||
2937 test_bit(A_BUS_DROP, &motg->inputs)) {
2938 pr_debug("id_a/b/c || a_bus_drop\n");
2939 clear_bit(B_CONN, &motg->inputs);
2940 clear_bit(A_BUS_REQ, &motg->inputs);
2941 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002942 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302943 msm_otg_start_host(otg, 0);
2944 if (!test_bit(ID_A, &motg->inputs))
2945 msm_hsusb_vbus_power(motg, 0);
2946 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2947 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2948 pr_debug("!a_vbus_vld\n");
2949 clear_bit(B_CONN, &motg->inputs);
2950 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002951 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302952 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002953 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302954 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2955 /*
2956 * a_bus_req is de-asserted when root hub is
2957 * suspended or HNP is in progress.
2958 */
2959 pr_debug("!a_bus_req\n");
2960 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002961 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302962 if (otg->host->b_hnp_enable)
2963 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2964 A_AIDL_BDIS);
2965 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002966 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302967 } else if (!test_bit(B_CONN, &motg->inputs)) {
2968 pr_debug("!b_conn\n");
2969 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002970 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302971 if (TA_WAIT_BCON > 0)
2972 msm_otg_start_timer(motg, TA_WAIT_BCON,
2973 A_WAIT_BCON);
2974 if (msm_chg_check_aca_intr(motg))
2975 work = 1;
2976 } else if (test_bit(ID_A, &motg->inputs)) {
2977 msm_otg_del_timer(motg);
2978 msm_hsusb_vbus_power(motg, 0);
2979 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2980 msm_otg_notify_charger(motg,
2981 IDEV_ACA_CHG_MAX);
2982 else
2983 msm_otg_notify_charger(motg,
2984 IDEV_CHG_MIN - motg->mA_port);
2985 } else if (!test_bit(ID, &motg->inputs)) {
2986 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2987 motg->chg_type = USB_INVALID_CHARGER;
2988 msm_otg_notify_charger(motg, 0);
2989 msm_hsusb_vbus_power(motg, 1);
2990 }
2991 break;
2992 case OTG_STATE_A_SUSPEND:
2993 if ((test_bit(ID, &motg->inputs) &&
2994 !test_bit(ID_A, &motg->inputs)) ||
2995 test_bit(A_BUS_DROP, &motg->inputs) ||
2996 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2997 pr_debug("id_a/b/c || a_bus_drop ||"
2998 "a_aidl_bdis_tmout\n");
2999 msm_otg_del_timer(motg);
3000 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003001 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303002 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003003 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303004 if (!test_bit(ID_A, &motg->inputs))
3005 msm_hsusb_vbus_power(motg, 0);
3006 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3007 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
3008 pr_debug("!a_vbus_vld\n");
3009 msm_otg_del_timer(motg);
3010 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003011 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303012 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003013 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303014 } else if (!test_bit(B_CONN, &motg->inputs) &&
3015 otg->host->b_hnp_enable) {
3016 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003017 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303018 msm_otg_host_hnp_enable(otg, 1);
3019 otg->gadget->is_a_peripheral = 1;
3020 msm_otg_start_peripheral(otg, 1);
3021 } else if (!test_bit(B_CONN, &motg->inputs) &&
3022 !otg->host->b_hnp_enable) {
3023 pr_debug("!b_conn && !b_hnp_enable");
3024 /*
3025 * bus request is dropped during suspend.
3026 * acquire again for next device.
3027 */
3028 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003029 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303030 if (TA_WAIT_BCON > 0)
3031 msm_otg_start_timer(motg, TA_WAIT_BCON,
3032 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003033 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05303034 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003035 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303036 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003037 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003038 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05303039 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303040 }
3041 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303042 case OTG_STATE_A_PERIPHERAL:
3043 if ((test_bit(ID, &motg->inputs) &&
3044 !test_bit(ID_A, &motg->inputs)) ||
3045 test_bit(A_BUS_DROP, &motg->inputs)) {
3046 pr_debug("id _f/b/c || a_bus_drop\n");
3047 /* Clear BIDL_ADIS timer */
3048 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003049 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303050 msm_otg_start_peripheral(otg, 0);
3051 otg->gadget->is_a_peripheral = 0;
3052 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003053 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303054 if (!test_bit(ID_A, &motg->inputs))
3055 msm_hsusb_vbus_power(motg, 0);
3056 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3057 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
3058 pr_debug("!a_vbus_vld\n");
3059 /* Clear BIDL_ADIS timer */
3060 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003061 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303062 msm_otg_start_peripheral(otg, 0);
3063 otg->gadget->is_a_peripheral = 0;
3064 msm_otg_start_host(otg, 0);
3065 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
3066 pr_debug("a_bidl_adis_tmout\n");
3067 msm_otg_start_peripheral(otg, 0);
3068 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003069 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303070 set_bit(A_BUS_REQ, &motg->inputs);
3071 msm_otg_host_hnp_enable(otg, 0);
3072 if (TA_WAIT_BCON > 0)
3073 msm_otg_start_timer(motg, TA_WAIT_BCON,
3074 A_WAIT_BCON);
3075 } else if (test_bit(ID_A, &motg->inputs)) {
3076 msm_hsusb_vbus_power(motg, 0);
3077 msm_otg_notify_charger(motg,
3078 IDEV_CHG_MIN - motg->mA_port);
3079 } else if (!test_bit(ID, &motg->inputs)) {
3080 msm_otg_notify_charger(motg, 0);
3081 msm_hsusb_vbus_power(motg, 1);
3082 }
3083 break;
3084 case OTG_STATE_A_WAIT_VFALL:
3085 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
3086 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003087 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303088 work = 1;
3089 }
3090 break;
3091 case OTG_STATE_A_VBUS_ERR:
3092 if ((test_bit(ID, &motg->inputs) &&
3093 !test_bit(ID_A, &motg->inputs)) ||
3094 test_bit(A_BUS_DROP, &motg->inputs) ||
3095 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003096 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303097 if (!test_bit(ID_A, &motg->inputs))
3098 msm_hsusb_vbus_power(motg, 0);
3099 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3100 motg->chg_state = USB_CHG_STATE_UNDEFINED;
3101 motg->chg_type = USB_INVALID_CHARGER;
3102 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303103 }
3104 break;
3105 default:
3106 break;
3107 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303108 if (work)
3109 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303110}
3111
Amit Blayd0fe07b2012-09-05 16:42:09 +03003112static void msm_otg_suspend_work(struct work_struct *w)
3113{
3114 struct msm_otg *motg =
3115 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05303116
3117 /* This work is only for device bus suspend */
3118 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
3119 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003120}
3121
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303122static irqreturn_t msm_otg_irq(int irq, void *data)
3123{
3124 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003125 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303126 u32 otgsc = 0, usbsts, pc;
3127 bool work = 0;
3128 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303129
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303130 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07003131 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303132 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07003133 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07003134 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07003135 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303136 return IRQ_HANDLED;
3137 }
3138
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003139 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303140 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303141
3142 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303143 return IRQ_NONE;
3144
3145 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303146 if (otgsc & OTGSC_ID) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003147 dev_dbg(otg->phy->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303148 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303149 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003150 dev_dbg(otg->phy->dev, "ID clear\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303151 /*
3152 * Assert a_bus_req to supply power on
3153 * VBUS when Micro/Mini-A cable is connected
3154 * with out user intervention.
3155 */
3156 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303157 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303158 msm_chg_enable_aca_det(motg);
3159 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303160 writel_relaxed(otgsc, USB_OTGSC);
3161 work = 1;
3162 } else if (otgsc & OTGSC_DPIS) {
3163 pr_debug("DPIS detected\n");
3164 writel_relaxed(otgsc, USB_OTGSC);
3165 set_bit(A_SRP_DET, &motg->inputs);
3166 set_bit(A_BUS_REQ, &motg->inputs);
3167 work = 1;
Vamsi Krishnaef6e1bf2013-03-02 15:36:17 -08003168 } else if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303169 writel_relaxed(otgsc, USB_OTGSC);
3170 /*
3171 * BSV interrupt comes when operating as an A-device
3172 * (VBUS on/off).
3173 * But, handle BSV when charger is removed from ACA in ID_A
3174 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003175 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303176 !test_bit(ID_A, &motg->inputs))
3177 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303178 if (otgsc & OTGSC_BSV) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003179 dev_dbg(otg->phy->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303180 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303181 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003182 dev_dbg(otg->phy->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303183 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003184 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3185
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303186 msm_chg_check_aca_intr(motg);
3187 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303188 work = 1;
3189 } else if (usbsts & STS_PCI) {
3190 pc = readl_relaxed(USB_PORTSC);
3191 pr_debug("portsc = %x\n", pc);
3192 ret = IRQ_NONE;
3193 /*
3194 * HCD Acks PCI interrupt. We use this to switch
3195 * between different OTG states.
3196 */
3197 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003198 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303199 case OTG_STATE_A_SUSPEND:
3200 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3201 !(pc & PORTSC_CCS)) {
3202 pr_debug("B_CONN clear\n");
3203 clear_bit(B_CONN, &motg->inputs);
3204 msm_otg_del_timer(motg);
3205 }
3206 break;
3207 case OTG_STATE_A_PERIPHERAL:
3208 /*
3209 * A-peripheral observed activity on bus.
3210 * clear A_BIDL_ADIS timer.
3211 */
3212 msm_otg_del_timer(motg);
3213 work = 0;
3214 break;
3215 case OTG_STATE_B_WAIT_ACON:
3216 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3217 pr_debug("A_CONN set\n");
3218 set_bit(A_CONN, &motg->inputs);
3219 /* Clear ASE0_BRST timer */
3220 msm_otg_del_timer(motg);
3221 }
3222 break;
3223 case OTG_STATE_B_HOST:
3224 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3225 pr_debug("A_CONN clear\n");
3226 clear_bit(A_CONN, &motg->inputs);
3227 msm_otg_del_timer(motg);
3228 }
3229 break;
3230 case OTG_STATE_A_WAIT_BCON:
3231 if (TA_WAIT_BCON < 0)
3232 set_bit(A_BUS_REQ, &motg->inputs);
3233 default:
3234 work = 0;
3235 break;
3236 }
3237 } else if (usbsts & STS_URI) {
3238 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003239 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303240 case OTG_STATE_A_PERIPHERAL:
3241 /*
3242 * A-peripheral observed activity on bus.
3243 * clear A_BIDL_ADIS timer.
3244 */
3245 msm_otg_del_timer(motg);
3246 work = 0;
3247 break;
3248 default:
3249 work = 0;
3250 break;
3251 }
3252 } else if (usbsts & STS_SLI) {
3253 ret = IRQ_NONE;
3254 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003255 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303256 case OTG_STATE_B_PERIPHERAL:
3257 if (otg->gadget->b_hnp_enable) {
3258 set_bit(A_BUS_SUSPEND, &motg->inputs);
3259 set_bit(B_BUS_REQ, &motg->inputs);
3260 work = 1;
3261 }
3262 break;
3263 case OTG_STATE_A_PERIPHERAL:
3264 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3265 A_BIDL_ADIS);
3266 break;
3267 default:
3268 break;
3269 }
3270 } else if ((usbsts & PHY_ALT_INT)) {
3271 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3272 if (msm_chg_check_aca_intr(motg))
3273 work = 1;
3274 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303275 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303276 if (work)
3277 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303278
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303279 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003280}
3281
3282static void msm_otg_set_vbus_state(int online)
3283{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303284 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003285 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303286
Vamsi Krishna945b4a92013-03-02 15:31:16 -08003287 if (online) {
3288 pr_debug("PMIC: BSV set\n");
3289 set_bit(B_SESS_VLD, &motg->inputs);
3290 } else {
3291 pr_debug("PMIC: BSV clear\n");
3292 clear_bit(B_SESS_VLD, &motg->inputs);
3293 }
3294
3295 /* do not queue state m/c work if id is grounded */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303296 if (!test_bit(ID, &motg->inputs)) {
3297 /*
3298 * state machine work waits for initial VBUS
3299 * completion in UNDEFINED state. Process
3300 * the initial VBUS event in ID_GND state.
3301 */
3302 if (init)
3303 return;
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303304 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003305
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303306 if (!init) {
3307 init = true;
3308 complete(&pmic_vbus_init);
3309 pr_debug("PMIC: BSV init complete\n");
3310 return;
3311 }
3312
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303313 if (test_bit(MHL, &motg->inputs) ||
3314 mhl_det_in_progress) {
3315 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3316 return;
3317 }
3318
Jack Pham5ca279b2012-05-14 18:42:54 -07003319 if (atomic_read(&motg->pm_suspended))
3320 motg->sm_work_pending = true;
3321 else
3322 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003323}
3324
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303325static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003326{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303327 struct msm_otg *motg = container_of(w, struct msm_otg,
3328 pmic_id_status_work.work);
3329 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003330
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303331 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303332 if (!test_and_set_bit(ID, &motg->inputs)) {
3333 pr_debug("PMIC: ID set\n");
3334 work = 1;
3335 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303336 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303337 if (test_and_clear_bit(ID, &motg->inputs)) {
3338 pr_debug("PMIC: ID clear\n");
3339 set_bit(A_BUS_REQ, &motg->inputs);
3340 work = 1;
3341 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303342 }
3343
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303344 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003345 if (atomic_read(&motg->pm_suspended))
3346 motg->sm_work_pending = true;
3347 else
3348 queue_work(system_nrt_wq, &motg->sm_work);
3349 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303350
3351}
3352
3353#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3354static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3355{
3356 struct msm_otg *motg = data;
3357
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303358 if (test_bit(MHL, &motg->inputs) ||
3359 mhl_det_in_progress) {
3360 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3361 return IRQ_HANDLED;
3362 }
3363
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303364 if (!aca_id_turned_on)
3365 /*schedule delayed work for 5msec for ID line state to settle*/
3366 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3367 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003368
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303369 return IRQ_HANDLED;
3370}
3371
3372static int msm_otg_mode_show(struct seq_file *s, void *unused)
3373{
3374 struct msm_otg *motg = s->private;
Stephen Boyd9850acb2013-01-28 14:11:20 -08003375 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303376
Stephen Boyd9850acb2013-01-28 14:11:20 -08003377 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303378 case OTG_STATE_A_HOST:
3379 seq_printf(s, "host\n");
3380 break;
3381 case OTG_STATE_B_PERIPHERAL:
3382 seq_printf(s, "peripheral\n");
3383 break;
3384 default:
3385 seq_printf(s, "none\n");
3386 break;
3387 }
3388
3389 return 0;
3390}
3391
3392static int msm_otg_mode_open(struct inode *inode, struct file *file)
3393{
3394 return single_open(file, msm_otg_mode_show, inode->i_private);
3395}
3396
3397static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3398 size_t count, loff_t *ppos)
3399{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303400 struct seq_file *s = file->private_data;
3401 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303402 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003403 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303404 int status = count;
3405 enum usb_mode_type req_mode;
3406
3407 memset(buf, 0x00, sizeof(buf));
3408
3409 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3410 status = -EFAULT;
3411 goto out;
3412 }
3413
3414 if (!strncmp(buf, "host", 4)) {
3415 req_mode = USB_HOST;
3416 } else if (!strncmp(buf, "peripheral", 10)) {
3417 req_mode = USB_PERIPHERAL;
3418 } else if (!strncmp(buf, "none", 4)) {
3419 req_mode = USB_NONE;
3420 } else {
3421 status = -EINVAL;
3422 goto out;
3423 }
3424
3425 switch (req_mode) {
3426 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003427 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303428 case OTG_STATE_A_HOST:
3429 case OTG_STATE_B_PERIPHERAL:
3430 set_bit(ID, &motg->inputs);
3431 clear_bit(B_SESS_VLD, &motg->inputs);
3432 break;
3433 default:
3434 goto out;
3435 }
3436 break;
3437 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003438 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303439 case OTG_STATE_B_IDLE:
3440 case OTG_STATE_A_HOST:
3441 set_bit(ID, &motg->inputs);
3442 set_bit(B_SESS_VLD, &motg->inputs);
3443 break;
3444 default:
3445 goto out;
3446 }
3447 break;
3448 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003449 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303450 case OTG_STATE_B_IDLE:
3451 case OTG_STATE_B_PERIPHERAL:
3452 clear_bit(ID, &motg->inputs);
3453 break;
3454 default:
3455 goto out;
3456 }
3457 break;
3458 default:
3459 goto out;
3460 }
3461
Steve Mucklef132c6c2012-06-06 18:30:57 -07003462 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303463 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303464out:
3465 return status;
3466}
3467
3468const struct file_operations msm_otg_mode_fops = {
3469 .open = msm_otg_mode_open,
3470 .read = seq_read,
3471 .write = msm_otg_mode_write,
3472 .llseek = seq_lseek,
3473 .release = single_release,
3474};
3475
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303476static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3477{
3478 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003479 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303480
Steve Mucklef132c6c2012-06-06 18:30:57 -07003481 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303482 return 0;
3483}
3484
3485static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3486{
3487 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3488}
3489
3490const struct file_operations msm_otg_state_fops = {
3491 .open = msm_otg_otg_state_open,
3492 .read = seq_read,
3493 .llseek = seq_lseek,
3494 .release = single_release,
3495};
3496
Anji jonnalad270e2d2011-08-09 11:28:32 +05303497static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3498{
3499 struct msm_otg *motg = s->private;
3500
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303501 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303502 return 0;
3503}
3504
3505static int msm_otg_chg_open(struct inode *inode, struct file *file)
3506{
3507 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3508}
3509
3510const struct file_operations msm_otg_chg_fops = {
3511 .open = msm_otg_chg_open,
3512 .read = seq_read,
3513 .llseek = seq_lseek,
3514 .release = single_release,
3515};
3516
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303517static int msm_otg_aca_show(struct seq_file *s, void *unused)
3518{
3519 if (debug_aca_enabled)
3520 seq_printf(s, "enabled\n");
3521 else
3522 seq_printf(s, "disabled\n");
3523
3524 return 0;
3525}
3526
3527static int msm_otg_aca_open(struct inode *inode, struct file *file)
3528{
3529 return single_open(file, msm_otg_aca_show, inode->i_private);
3530}
3531
3532static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3533 size_t count, loff_t *ppos)
3534{
3535 char buf[8];
3536
3537 memset(buf, 0x00, sizeof(buf));
3538
3539 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3540 return -EFAULT;
3541
3542 if (!strncmp(buf, "enable", 6))
3543 debug_aca_enabled = true;
3544 else
3545 debug_aca_enabled = false;
3546
3547 return count;
3548}
3549
3550const struct file_operations msm_otg_aca_fops = {
3551 .open = msm_otg_aca_open,
3552 .read = seq_read,
3553 .write = msm_otg_aca_write,
3554 .llseek = seq_lseek,
3555 .release = single_release,
3556};
3557
Manu Gautam8bdcc592012-03-06 11:26:06 +05303558static int msm_otg_bus_show(struct seq_file *s, void *unused)
3559{
3560 if (debug_bus_voting_enabled)
3561 seq_printf(s, "enabled\n");
3562 else
3563 seq_printf(s, "disabled\n");
3564
3565 return 0;
3566}
3567
3568static int msm_otg_bus_open(struct inode *inode, struct file *file)
3569{
3570 return single_open(file, msm_otg_bus_show, inode->i_private);
3571}
3572
3573static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3574 size_t count, loff_t *ppos)
3575{
3576 char buf[8];
Manu Gautam8bdcc592012-03-06 11:26:06 +05303577 struct seq_file *s = file->private_data;
3578 struct msm_otg *motg = s->private;
3579
3580 memset(buf, 0x00, sizeof(buf));
3581
3582 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3583 return -EFAULT;
3584
3585 if (!strncmp(buf, "enable", 6)) {
3586 /* Do not vote here. Let OTG statemachine decide when to vote */
3587 debug_bus_voting_enabled = true;
3588 } else {
3589 debug_bus_voting_enabled = false;
Manu Gautame3a39082013-06-11 10:42:56 +05303590 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303591 }
3592
3593 return count;
3594}
3595
David Keitel272ce522012-08-17 16:25:24 -07003596static int otg_power_get_property_usb(struct power_supply *psy,
3597 enum power_supply_property psp,
3598 union power_supply_propval *val)
3599{
Jack Pham0c695282012-10-19 18:13:03 -07003600 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003601 switch (psp) {
3602 case POWER_SUPPLY_PROP_SCOPE:
3603 if (motg->host_mode)
3604 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3605 else
3606 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3607 break;
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303608 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
3609 val->intval = motg->voltage_max;
3610 break;
David Keitel272ce522012-08-17 16:25:24 -07003611 case POWER_SUPPLY_PROP_CURRENT_MAX:
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303612 val->intval = motg->current_max;
David Keitel272ce522012-08-17 16:25:24 -07003613 break;
3614 /* Reflect USB enumeration */
3615 case POWER_SUPPLY_PROP_PRESENT:
3616 case POWER_SUPPLY_PROP_ONLINE:
3617 val->intval = motg->online;
3618 break;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303619 case POWER_SUPPLY_PROP_TYPE:
3620 val->intval = psy->type;
3621 break;
David Keitel272ce522012-08-17 16:25:24 -07003622 default:
3623 return -EINVAL;
3624 }
3625 return 0;
3626}
3627
3628static int otg_power_set_property_usb(struct power_supply *psy,
3629 enum power_supply_property psp,
3630 const union power_supply_propval *val)
3631{
Jack Pham0c695282012-10-19 18:13:03 -07003632 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003633
3634 switch (psp) {
3635 /* Process PMIC notification in PRESENT prop */
3636 case POWER_SUPPLY_PROP_PRESENT:
3637 msm_otg_set_vbus_state(val->intval);
3638 break;
3639 /* The ONLINE property reflects if usb has enumerated */
3640 case POWER_SUPPLY_PROP_ONLINE:
3641 motg->online = val->intval;
3642 break;
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303643 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
3644 motg->voltage_max = val->intval;
3645 break;
David Keitel272ce522012-08-17 16:25:24 -07003646 case POWER_SUPPLY_PROP_CURRENT_MAX:
3647 motg->current_max = val->intval;
3648 break;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303649 case POWER_SUPPLY_PROP_TYPE:
3650 psy->type = val->intval;
3651 break;
David Keitel272ce522012-08-17 16:25:24 -07003652 default:
3653 return -EINVAL;
3654 }
3655
3656 power_supply_changed(&motg->usb_psy);
3657 return 0;
3658}
3659
David Collinsd79acc52012-11-26 14:59:00 -08003660static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3661 enum power_supply_property psp)
3662{
3663 switch (psp) {
3664 case POWER_SUPPLY_PROP_PRESENT:
3665 case POWER_SUPPLY_PROP_ONLINE:
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303666 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
David Collinsd79acc52012-11-26 14:59:00 -08003667 case POWER_SUPPLY_PROP_CURRENT_MAX:
3668 return 1;
3669 default:
3670 break;
3671 }
3672
3673 return 0;
3674}
3675
David Keitel272ce522012-08-17 16:25:24 -07003676static char *otg_pm_power_supplied_to[] = {
3677 "battery",
3678};
3679
3680static enum power_supply_property otg_pm_power_props_usb[] = {
3681 POWER_SUPPLY_PROP_PRESENT,
3682 POWER_SUPPLY_PROP_ONLINE,
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303683 POWER_SUPPLY_PROP_VOLTAGE_MAX,
David Keitel272ce522012-08-17 16:25:24 -07003684 POWER_SUPPLY_PROP_CURRENT_MAX,
3685 POWER_SUPPLY_PROP_SCOPE,
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303686 POWER_SUPPLY_PROP_TYPE,
David Keitel272ce522012-08-17 16:25:24 -07003687};
3688
Manu Gautam8bdcc592012-03-06 11:26:06 +05303689const struct file_operations msm_otg_bus_fops = {
3690 .open = msm_otg_bus_open,
3691 .read = seq_read,
3692 .write = msm_otg_bus_write,
3693 .llseek = seq_lseek,
3694 .release = single_release,
3695};
3696
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303697static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303698
3699static int msm_otg_debugfs_init(struct msm_otg *motg)
3700{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303701 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303702
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303703 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3704
3705 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3706 return -ENODEV;
3707
Anji jonnalad270e2d2011-08-09 11:28:32 +05303708 if (motg->pdata->mode == USB_OTG &&
3709 motg->pdata->otg_control == OTG_USER_CONTROL) {
3710
Manu Gautam8bdcc592012-03-06 11:26:06 +05303711 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303712 S_IWUSR, msm_otg_dbg_root, motg,
3713 &msm_otg_mode_fops);
3714
Manu Gautam8bdcc592012-03-06 11:26:06 +05303715 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303716 debugfs_remove(msm_otg_dbg_root);
3717 msm_otg_dbg_root = NULL;
3718 return -ENODEV;
3719 }
3720 }
3721
Manu Gautam8bdcc592012-03-06 11:26:06 +05303722 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303723 msm_otg_dbg_root, motg,
3724 &msm_otg_chg_fops);
3725
Manu Gautam8bdcc592012-03-06 11:26:06 +05303726 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303727 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303728 return -ENODEV;
3729 }
3730
Manu Gautam8bdcc592012-03-06 11:26:06 +05303731 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303732 msm_otg_dbg_root, motg,
3733 &msm_otg_aca_fops);
3734
Manu Gautam8bdcc592012-03-06 11:26:06 +05303735 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303736 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303737 return -ENODEV;
3738 }
3739
Manu Gautam8bdcc592012-03-06 11:26:06 +05303740 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3741 msm_otg_dbg_root, motg,
3742 &msm_otg_bus_fops);
3743
3744 if (!msm_otg_dentry) {
3745 debugfs_remove_recursive(msm_otg_dbg_root);
3746 return -ENODEV;
3747 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303748
3749 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3750 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3751
3752 if (!msm_otg_dentry) {
3753 debugfs_remove_recursive(msm_otg_dbg_root);
3754 return -ENODEV;
3755 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303756 return 0;
3757}
3758
3759static void msm_otg_debugfs_cleanup(void)
3760{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303761 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303762}
3763
Manu Gautam0ddbd922012-09-21 17:17:38 +05303764#define MSM_OTG_CMD_ID 0x09
3765#define MSM_OTG_DEVICE_ID 0x04
3766#define MSM_OTG_VMID_IDX 0xFF
3767#define MSM_OTG_MEM_TYPE 0x02
3768struct msm_otg_scm_cmd_buf {
3769 unsigned int device_id;
3770 unsigned int vmid_idx;
3771 unsigned int mem_type;
3772} __attribute__ ((__packed__));
3773
3774static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3775{
3776 int ret;
3777 struct msm_otg_platform_data *pdata = motg->pdata;
3778 struct msm_otg_scm_cmd_buf cmd_buf;
3779
3780 if (!pdata->pnoc_errata_fix)
3781 return;
3782
3783 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3784
3785 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3786 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3787 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3788
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -07003789 ret = scm_call(SCM_SVC_MP, MSM_OTG_CMD_ID, &cmd_buf,
Manu Gautam0ddbd922012-09-21 17:17:38 +05303790 sizeof(cmd_buf), NULL, 0);
3791
3792 if (ret)
3793 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3794}
3795
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303796static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3797static struct platform_device *msm_otg_add_pdev(
3798 struct platform_device *ofdev, const char *name)
3799{
3800 struct platform_device *pdev;
3801 const struct resource *res = ofdev->resource;
3802 unsigned int num = ofdev->num_resources;
3803 int retval;
Amit Blayb4f14532013-06-17 22:33:57 +03003804 struct ci13xxx_platform_data ci_pdata;
3805 struct msm_otg_platform_data *otg_pdata;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303806
3807 pdev = platform_device_alloc(name, -1);
3808 if (!pdev) {
3809 retval = -ENOMEM;
3810 goto error;
3811 }
3812
3813 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3814 pdev->dev.dma_mask = &msm_otg_dma_mask;
3815
3816 if (num) {
3817 retval = platform_device_add_resources(pdev, res, num);
3818 if (retval)
3819 goto error;
3820 }
3821
Amit Blayb4f14532013-06-17 22:33:57 +03003822 if (!strcmp(name, "msm_hsusb")) {
3823 otg_pdata =
3824 (struct msm_otg_platform_data *)
3825 ofdev->dev.platform_data;
3826 ci_pdata.log2_itc = otg_pdata->log2_itc;
3827 ci_pdata.usb_core_id = 0;
Shimrit Malichi5a2d5b52013-06-20 19:04:28 +03003828 ci_pdata.l1_supported = otg_pdata->l1_supported;
Amit Blayb4f14532013-06-17 22:33:57 +03003829 retval = platform_device_add_data(pdev, &ci_pdata,
3830 sizeof(ci_pdata));
3831 if (retval)
3832 goto error;
3833 }
3834
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303835 retval = platform_device_add(pdev);
3836 if (retval)
3837 goto error;
3838
3839 return pdev;
3840
3841error:
3842 platform_device_put(pdev);
3843 return ERR_PTR(retval);
3844}
3845
3846static int msm_otg_setup_devices(struct platform_device *ofdev,
3847 enum usb_mode_type mode, bool init)
3848{
3849 const char *gadget_name = "msm_hsusb";
3850 const char *host_name = "msm_hsusb_host";
3851 static struct platform_device *gadget_pdev;
3852 static struct platform_device *host_pdev;
3853 int retval = 0;
3854
3855 if (!init) {
3856 if (gadget_pdev)
3857 platform_device_unregister(gadget_pdev);
3858 if (host_pdev)
3859 platform_device_unregister(host_pdev);
3860 return 0;
3861 }
3862
3863 switch (mode) {
3864 case USB_OTG:
3865 /* fall through */
3866 case USB_PERIPHERAL:
3867 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3868 if (IS_ERR(gadget_pdev)) {
3869 retval = PTR_ERR(gadget_pdev);
3870 break;
3871 }
3872 if (mode == USB_PERIPHERAL)
3873 break;
3874 /* fall through */
3875 case USB_HOST:
3876 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3877 if (IS_ERR(host_pdev)) {
3878 retval = PTR_ERR(host_pdev);
3879 if (mode == USB_OTG)
3880 platform_device_unregister(gadget_pdev);
3881 }
3882 break;
3883 default:
3884 break;
3885 }
3886
3887 return retval;
3888}
3889
David Keitel272ce522012-08-17 16:25:24 -07003890static int msm_otg_register_power_supply(struct platform_device *pdev,
3891 struct msm_otg *motg)
3892{
3893 int ret;
3894
3895 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3896 if (ret < 0) {
3897 dev_err(motg->phy.dev,
3898 "%s:power_supply_register usb failed\n",
3899 __func__);
3900 return ret;
3901 }
3902
3903 legacy_power_supply = false;
3904 return 0;
3905}
3906
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303907static int msm_otg_ext_chg_open(struct inode *inode, struct file *file)
3908{
3909 struct msm_otg *motg = the_msm_otg;
3910
3911 pr_debug("msm_otg ext chg open\n");
3912
3913 motg->ext_chg_opened = true;
3914 file->private_data = (void *)motg;
3915 return 0;
3916}
3917
3918static long
3919msm_otg_ext_chg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3920{
3921 struct msm_otg *motg = file->private_data;
3922 struct msm_usb_chg_info info = {0};
3923 int ret = 0, val;
3924
3925 switch (cmd) {
3926 case MSM_USB_EXT_CHG_INFO:
3927 info.chg_block_type = USB_CHG_BLOCK_ULPI;
3928 info.page_offset = motg->io_res->start & ~PAGE_MASK;
3929 /* mmap() works on PAGE granularity */
3930 info.length = PAGE_SIZE;
3931
3932 if (copy_to_user((void __user *)arg, &info, sizeof(info))) {
3933 pr_err("%s: copy to user failed\n\n", __func__);
3934 ret = -EFAULT;
3935 }
3936 break;
3937 case MSM_USB_EXT_CHG_BLOCK_LPM:
3938 if (get_user(val, (int __user *)arg)) {
3939 pr_err("%s: get_user failed\n\n", __func__);
3940 ret = -EFAULT;
3941 break;
3942 }
3943 pr_debug("%s: LPM block request %d\n", __func__, val);
3944 if (val) { /* block LPM */
3945 if (motg->chg_type == USB_DCP_CHARGER) {
3946 /*
3947 * If device is already suspended, resume it.
3948 * The PM usage counter is incremented in
3949 * runtime resume method. if device is not
3950 * suspended, cancel the scheduled suspend
3951 * and increment the PM usage counter.
3952 */
3953 if (pm_runtime_suspended(motg->phy.dev))
3954 pm_runtime_resume(motg->phy.dev);
3955 else
3956 pm_runtime_get_sync(motg->phy.dev);
3957 } else {
3958 motg->ext_chg_active = false;
3959 complete(&motg->ext_chg_wait);
3960 ret = -ENODEV;
3961 }
3962 } else {
3963 motg->ext_chg_active = false;
3964 complete(&motg->ext_chg_wait);
3965 pm_runtime_put(motg->phy.dev);
3966 }
3967 break;
3968 default:
3969 ret = -EINVAL;
3970 }
3971
3972 return ret;
3973}
3974
3975static int msm_otg_ext_chg_mmap(struct file *file, struct vm_area_struct *vma)
3976{
3977 struct msm_otg *motg = file->private_data;
3978 unsigned long vsize = vma->vm_end - vma->vm_start;
3979 int ret;
3980
3981 if (vma->vm_pgoff || vsize > PAGE_SIZE)
3982 return -EINVAL;
3983
3984 vma->vm_pgoff = __phys_to_pfn(motg->io_res->start);
3985 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
3986
3987 ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
3988 vsize, vma->vm_page_prot);
3989 if (ret < 0) {
3990 pr_err("%s: failed with return val %d\n", __func__, ret);
3991 return ret;
3992 }
3993
3994 return 0;
3995}
3996
3997static int msm_otg_ext_chg_release(struct inode *inode, struct file *file)
3998{
3999 struct msm_otg *motg = file->private_data;
4000
4001 pr_debug("msm_otg ext chg release\n");
4002
4003 motg->ext_chg_opened = false;
4004
4005 return 0;
4006}
4007
4008static const struct file_operations msm_otg_ext_chg_fops = {
4009 .owner = THIS_MODULE,
4010 .open = msm_otg_ext_chg_open,
4011 .unlocked_ioctl = msm_otg_ext_chg_ioctl,
4012 .mmap = msm_otg_ext_chg_mmap,
4013 .release = msm_otg_ext_chg_release,
4014};
4015
4016static int msm_otg_setup_ext_chg_cdev(struct msm_otg *motg)
4017{
4018 int ret;
4019
4020 if (motg->pdata->enable_sec_phy || motg->pdata->mode == USB_HOST ||
4021 motg->pdata->otg_control != OTG_PMIC_CONTROL ||
4022 psy != &motg->usb_psy) {
4023 pr_debug("usb ext chg is not supported by msm otg\n");
4024 return -ENODEV;
4025 }
4026
4027 ret = alloc_chrdev_region(&motg->ext_chg_dev, 0, 1, "usb_ext_chg");
4028 if (ret < 0) {
4029 pr_err("Fail to allocate usb ext char dev region\n");
4030 return ret;
4031 }
4032 motg->ext_chg_class = class_create(THIS_MODULE, "msm_ext_chg");
4033 if (ret < 0) {
4034 pr_err("Fail to create usb ext chg class\n");
4035 goto unreg_chrdev;
4036 }
4037 cdev_init(&motg->ext_chg_cdev, &msm_otg_ext_chg_fops);
4038 motg->ext_chg_cdev.owner = THIS_MODULE;
4039
4040 ret = cdev_add(&motg->ext_chg_cdev, motg->ext_chg_dev, 1);
4041 if (ret < 0) {
4042 pr_err("Fail to add usb ext chg cdev\n");
4043 goto destroy_class;
4044 }
4045 motg->ext_chg_device = device_create(motg->ext_chg_class,
4046 NULL, motg->ext_chg_dev, NULL,
4047 "usb_ext_chg");
4048 if (IS_ERR(motg->ext_chg_device)) {
4049 pr_err("Fail to create usb ext chg device\n");
4050 ret = PTR_ERR(motg->ext_chg_device);
4051 motg->ext_chg_device = NULL;
4052 goto del_cdev;
4053 }
4054
4055 init_completion(&motg->ext_chg_wait);
4056 pr_debug("msm otg ext chg cdev setup success\n");
4057 return 0;
4058
4059del_cdev:
4060 cdev_del(&motg->ext_chg_cdev);
4061destroy_class:
4062 class_destroy(motg->ext_chg_class);
4063unreg_chrdev:
4064 unregister_chrdev_region(motg->ext_chg_dev, 1);
4065
4066 return ret;
4067}
4068
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304069static ssize_t dpdm_pulldown_enable_show(struct device *dev,
4070 struct device_attribute *attr, char *buf)
4071{
4072 struct msm_otg *motg = the_msm_otg;
4073 struct msm_otg_platform_data *pdata = motg->pdata;
4074
4075 return snprintf(buf, PAGE_SIZE, "%s\n", pdata->dpdm_pulldown_added ?
4076 "enabled" : "disabled");
4077}
4078
4079static ssize_t dpdm_pulldown_enable_store(struct device *dev,
4080 struct device_attribute *attr, const char
4081 *buf, size_t size)
4082{
4083 struct msm_otg *motg = the_msm_otg;
4084 struct msm_otg_platform_data *pdata = motg->pdata;
4085
4086 if (!strnicmp(buf, "enable", 6)) {
4087 pdata->dpdm_pulldown_added = true;
4088 return size;
4089 } else if (!strnicmp(buf, "disable", 7)) {
4090 pdata->dpdm_pulldown_added = false;
4091 return size;
4092 }
4093
4094 return -EINVAL;
4095}
4096
4097static DEVICE_ATTR(dpdm_pulldown_enable, S_IRUGO | S_IWUSR,
4098 dpdm_pulldown_enable_show, dpdm_pulldown_enable_store);
4099
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304100struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
4101{
4102 struct device_node *node = pdev->dev.of_node;
4103 struct msm_otg_platform_data *pdata;
4104 int len = 0;
4105
4106 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
4107 if (!pdata) {
4108 pr_err("unable to allocate platform data\n");
4109 return NULL;
4110 }
4111 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
4112 if (len) {
4113 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
4114 if (!pdata->phy_init_seq)
4115 return NULL;
4116 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
4117 pdata->phy_init_seq,
4118 len/sizeof(*pdata->phy_init_seq));
4119 }
4120 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
4121 &pdata->power_budget);
4122 of_property_read_u32(node, "qcom,hsusb-otg-mode",
4123 &pdata->mode);
4124 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
4125 &pdata->otg_control);
4126 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
4127 &pdata->default_mode);
4128 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
4129 &pdata->phy_type);
Manu Gautambd53fba2012-07-31 16:13:06 +05304130 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
4131 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05304132 pdata->pnoc_errata_fix = of_property_read_bool(node,
4133 "qcom,hsusb-otg-pnoc-errata-fix");
Ido Shayevitza7114b92013-01-13 13:34:47 +02004134 pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
4135 "qcom,hsusb-otg-lpm-on-dev-suspend");
Ido Shayevitz26193352013-01-21 23:16:54 +02004136 pdata->core_clk_always_on_workaround = of_property_read_bool(node,
4137 "qcom,hsusb-otg-clk-always-on-workaround");
Shimrit Malichiffab5b02013-03-10 11:06:16 +02004138 pdata->delay_lpm_on_disconnect = of_property_read_bool(node,
4139 "qcom,hsusb-otg-delay-lpm");
Lena Salmanabde35d2013-04-25 15:29:43 +03004140 pdata->delay_lpm_hndshk_on_disconnect = of_property_read_bool(node,
4141 "qcom,hsusb-otg-delay-lpm-hndshk-on-disconnect");
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004142 pdata->dp_manual_pullup = of_property_read_bool(node,
4143 "qcom,dp-manual-pullup");
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304144 pdata->enable_sec_phy = of_property_read_bool(node,
4145 "qcom,usb2-enable-hsphy2");
Amit Blayb4f14532013-06-17 22:33:57 +03004146 of_property_read_u32(node, "qcom,hsusb-log2-itc",
4147 &pdata->log2_itc);
Manu Gautambd53fba2012-07-31 16:13:06 +05304148
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304149 of_property_read_u32(node, "qcom,hsusb-otg-mpm-dpsehv-int",
4150 &pdata->mpm_dpshv_int);
4151 of_property_read_u32(node, "qcom,hsusb-otg-mpm-dmsehv-int",
4152 &pdata->mpm_dmshv_int);
Manu Gautam0e0c53f2013-04-04 10:55:20 +05304153 pdata->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
4154 if (pdata->pmic_id_irq < 0)
4155 pdata->pmic_id_irq = 0;
4156
Shimrit Malichi5a2d5b52013-06-20 19:04:28 +03004157 pdata->l1_supported = of_property_read_bool(node,
4158 "qcom,hsusb-l1-supported");
4159
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304160 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304161}
4162
4163static int __init msm_otg_probe(struct platform_device *pdev)
4164{
Manu Gautamf8c45642012-08-10 10:20:56 -07004165 int ret = 0;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304166 int len = 0;
4167 u32 tmp[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304168 struct resource *res;
4169 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004170 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304171 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304172
4173 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304174
4175 if (pdev->dev.of_node) {
4176 dev_dbg(&pdev->dev, "device tree enabled\n");
4177 pdata = msm_otg_dt_to_pdata(pdev);
4178 if (!pdata)
4179 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07004180
4181 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
4182 if (!pdata->bus_scale_table)
4183 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
4184
Amit Blayb4f14532013-06-17 22:33:57 +03004185 pdev->dev.platform_data = pdata;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304186 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
4187 if (ret) {
4188 dev_err(&pdev->dev, "devices setup failed\n");
4189 return ret;
4190 }
4191 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304192 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
4193 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304194 } else {
4195 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304196 }
4197
Manu Gautame3a39082013-06-11 10:42:56 +05304198 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304199 if (!motg) {
4200 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
4201 return -ENOMEM;
4202 }
4203
Manu Gautame3a39082013-06-11 10:42:56 +05304204 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
4205 GFP_KERNEL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004206 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004207 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
Manu Gautame3a39082013-06-11 10:42:56 +05304208 return -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304209 }
4210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004211 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304212 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004213 phy = &motg->phy;
4214 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05304215
Manu Gautame3a39082013-06-11 10:42:56 +05304216 if (motg->pdata->bus_scale_table) {
4217 motg->bus_perf_client =
4218 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4219 if (!motg->bus_perf_client) {
4220 dev_err(motg->phy.dev, "%s: Failed to register BUS\n"
4221 "scaling client!!\n", __func__);
4222 } else {
4223 debug_bus_voting_enabled = true;
4224 /* Some platforms require BUS vote to control clocks */
4225 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
4226 }
4227 }
4228
Anji jonnala0f73cac2011-05-04 10:19:46 +05304229 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304230 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
4231 * PHY treat ACA ID_GND as float and no interrupt is generated. But
4232 * PMIC can detect ACA ID_GND and generate an interrupt.
4233 */
4234 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
4235 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
4236 ret = -EINVAL;
Manu Gautame3a39082013-06-11 10:42:56 +05304237 goto devote_bus_bw;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304238 }
4239
Ofir Cohen4da266f2012-01-03 10:19:29 +02004240 /* initialize reset counter */
4241 motg->reset_counter = 0;
4242
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304243 /*
4244 * Targets on which link uses asynchronous reset methodology,
4245 * free running clock is not required during the reset.
4246 */
Manu Gautam5143b252012-01-05 19:25:23 -08004247 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304248 if (IS_ERR(motg->clk))
4249 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
4250 else
4251 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05304252
4253 /*
Manu Gautam5143b252012-01-05 19:25:23 -08004254 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05304255 * CORE CLK must be running at >55Mhz for correct HSUSB
4256 * operation and USB core cannot tolerate frequency changes on
4257 * CORE CLK. For such USB cores, vote for maximum clk frequency
4258 * on pclk source
4259 */
Manu Gautam5143b252012-01-05 19:25:23 -08004260 motg->core_clk = clk_get(&pdev->dev, "core_clk");
4261 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304262 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08004263 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05304264 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004265 goto put_clk;
4266 }
Mayank Rana3eaf28d2013-03-27 14:04:04 +05304267
4268 /*
4269 * Get Max supported clk frequency for USB Core CLK and request
4270 * to set the same.
4271 */
4272 motg->core_clk_rate = clk_round_rate(motg->core_clk, LONG_MAX);
4273 if (IS_ERR_VALUE(motg->core_clk_rate)) {
4274 dev_err(&pdev->dev, "fail to get core clk max freq.\n");
4275 } else {
4276 ret = clk_set_rate(motg->core_clk, motg->core_clk_rate);
4277 if (ret)
4278 dev_err(&pdev->dev, "fail to set core_clk freq:%d\n",
4279 ret);
4280 }
Manu Gautam5143b252012-01-05 19:25:23 -08004281
4282 motg->pclk = clk_get(&pdev->dev, "iface_clk");
4283 if (IS_ERR(motg->pclk)) {
4284 dev_err(&pdev->dev, "failed to get iface_clk\n");
4285 ret = PTR_ERR(motg->pclk);
4286 goto put_core_clk;
4287 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304288
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304289 /*
4290 * On few platforms USB PHY is fed with sleep clk.
4291 * Hence don't fail probe.
4292 */
4293 motg->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
4294 if (IS_ERR(motg->sleep_clk)) {
4295 dev_dbg(&pdev->dev, "failed to get sleep_clk\n");
4296 } else {
4297 ret = clk_prepare_enable(motg->sleep_clk);
4298 if (ret) {
4299 dev_err(&pdev->dev, "%s failed to vote sleep_clk%d\n",
4300 __func__, ret);
4301 goto put_pclk;
4302 }
4303 }
4304
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304305 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4306 if (!res) {
4307 dev_err(&pdev->dev, "failed to get platform resource mem\n");
4308 ret = -ENODEV;
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304309 goto disable_sleep_clk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304310 }
4311
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304312 motg->io_res = res;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304313 motg->regs = ioremap(res->start, resource_size(res));
4314 if (!motg->regs) {
4315 dev_err(&pdev->dev, "ioremap failed\n");
4316 ret = -ENOMEM;
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304317 goto disable_sleep_clk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304318 }
4319 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
4320
4321 motg->irq = platform_get_irq(pdev, 0);
4322 if (!motg->irq) {
4323 dev_err(&pdev->dev, "platform_get_irq failed\n");
4324 ret = -ENODEV;
4325 goto free_regs;
4326 }
4327
Manu Gautamf8c45642012-08-10 10:20:56 -07004328 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
4329 if (motg->async_irq < 0) {
4330 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
4331 motg->async_irq = 0;
4332 }
4333
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304334 motg->xo_clk = clk_get(&pdev->dev, "xo");
4335 if (IS_ERR(motg->xo_clk)) {
4336 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
4337 if (IS_ERR(motg->xo_handle)) {
4338 dev_err(&pdev->dev, "%s fail to get handle for TCXO\n",
4339 __func__);
4340 ret = PTR_ERR(motg->xo_handle);
4341 goto free_regs;
4342 } else {
4343 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
4344 if (ret) {
4345 dev_err(&pdev->dev, "%s XO voting failed %d\n",
4346 __func__, ret);
4347 goto free_xo_handle;
4348 }
4349 }
4350 } else {
4351 ret = clk_prepare_enable(motg->xo_clk);
4352 if (ret) {
4353 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
4354 __func__, ret);
4355 goto free_xo_handle;
4356 }
Anji jonnala7da3f262011-12-02 17:22:14 -08004357 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05304358
Anji jonnala7da3f262011-12-02 17:22:14 -08004359
Manu Gautam28b1bac2012-01-30 16:43:06 +05304360 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304361
Mayank Rana248698c2012-04-19 00:03:16 +05304362 motg->vdd_type = VDDCX_CORNER;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304363 hsusb_vdd = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
4364 if (IS_ERR(hsusb_vdd)) {
4365 hsusb_vdd = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
4366 if (IS_ERR(hsusb_vdd)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004367 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304368 ret = PTR_ERR(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304369 goto devote_xo_handle;
4370 }
4371 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304372 }
4373
Mayank Rana0f286cf2013-02-27 11:43:27 +05304374 if (pdev->dev.of_node) {
4375 of_get_property(pdev->dev.of_node,
4376 "qcom,vdd-voltage-level",
4377 &len);
4378 if (len == sizeof(tmp)) {
4379 of_property_read_u32_array(pdev->dev.of_node,
4380 "qcom,vdd-voltage-level",
4381 tmp, len/sizeof(*tmp));
4382 vdd_val[motg->vdd_type][0] = tmp[0];
4383 vdd_val[motg->vdd_type][1] = tmp[1];
4384 vdd_val[motg->vdd_type][2] = tmp[2];
4385 } else {
4386 dev_dbg(&pdev->dev, "Using default hsusb vdd config.\n");
4387 }
4388 }
4389
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004390 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304391 if (ret) {
4392 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05304393 goto devote_xo_handle;
4394 }
4395
Mayank Rana0f286cf2013-02-27 11:43:27 +05304396 ret = regulator_enable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304397 if (ret) {
4398 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
4399 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304400 }
4401
4402 ret = msm_hsusb_ldo_init(motg, 1);
4403 if (ret) {
4404 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304405 goto free_hsusb_vdd;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304406 }
4407
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304408 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05304409 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
4410 "mhl_usb_hs_switch");
4411 if (IS_ERR(mhl_usb_hs_switch)) {
4412 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07004413 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304414 goto free_ldo_init;
4415 }
4416 }
4417
Amit Blay81801aa2012-09-19 12:08:12 +02004418 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304419 if (ret) {
4420 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004421 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304422 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05304423 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304424
Manu Gautam0ddbd922012-09-21 17:17:38 +05304425 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
4426 msm_otg_pnoc_errata_fix(motg);
4427
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304428 writel(0, USB_USBINTR);
4429 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004430 /* Ensure that above STOREs are completed before enabling interrupts */
4431 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304432
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304433 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
4434 if (ret)
4435 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004436 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05304437 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304438 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304439 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304440 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004441 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304442 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
4443 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05304444 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
4445 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304446 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
4447 "msm_otg", motg);
4448 if (ret) {
4449 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004450 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304451 }
4452
Manu Gautamf8c45642012-08-10 10:20:56 -07004453 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304454 ret = request_irq(motg->async_irq, msm_otg_irq,
4455 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004456 if (ret) {
4457 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4458 goto free_irq;
4459 }
4460 disable_irq(motg->async_irq);
4461 }
4462
Jack Pham87f202f2012-08-06 00:24:22 -07004463 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4464 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4465
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304466 if (pdata->mpm_dpshv_int)
4467 msm_mpm_enable_pin(pdata->mpm_dpshv_int, 1);
4468 if (pdata->mpm_dmshv_int)
4469 msm_mpm_enable_pin(pdata->mpm_dmshv_int, 1);
4470
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004471 phy->init = msm_otg_reset;
4472 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004473 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304474
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004475 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304476
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004477 phy->otg->phy = &motg->phy;
4478 phy->otg->set_host = msm_otg_set_host;
4479 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004480 phy->otg->start_hnp = msm_otg_start_hnp;
4481 phy->otg->start_srp = msm_otg_start_srp;
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004482 if (pdata->dp_manual_pullup)
4483 phy->flags |= ENABLE_DP_MANUAL_PULLUP;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004484
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304485 if (pdata->enable_sec_phy)
4486 phy->flags |= ENABLE_SECONDARY_PHY;
4487
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004488 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304489 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004490 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004491 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304492 }
4493
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304494 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304495 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004496 if (motg->pdata->pmic_id_irq) {
4497 ret = request_irq(motg->pdata->pmic_id_irq,
4498 msm_pmic_id_irq,
4499 IRQF_TRIGGER_RISING |
4500 IRQF_TRIGGER_FALLING,
4501 "msm_otg", motg);
4502 if (ret) {
4503 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004504 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004505 }
4506 } else {
4507 ret = -ENODEV;
4508 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004509 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004510 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304511 }
4512
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304513 msm_hsusb_mhl_switch_enable(motg, 1);
4514
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304515 platform_set_drvdata(pdev, motg);
4516 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004517 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304518
Anji jonnalad270e2d2011-08-09 11:28:32 +05304519 ret = msm_otg_debugfs_init(motg);
4520 if (ret)
4521 dev_dbg(&pdev->dev, "mode debugfs file is"
4522 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304523
Amit Blay58b31472011-11-18 09:39:39 +02004524 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4525 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304526 (!(motg->pdata->mode == USB_OTG) ||
4527 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004528 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304529 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004530
Amit Blay58b31472011-11-18 09:39:39 +02004531 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004532 motg->caps = ALLOW_PHY_RETENTION |
4533 ALLOW_PHY_REGULATORS_LPM;
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304534
4535 if (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int)
4536 motg->caps |= ALLOW_HOST_PHY_RETENTION;
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304537 device_create_file(&pdev->dev,
4538 &dev_attr_dpdm_pulldown_enable);
Amit Blay58b31472011-11-18 09:39:39 +02004539 }
4540
Amit Blay6fa647a2012-05-24 14:12:08 +03004541 if (motg->pdata->enable_lpm_on_dev_suspend)
4542 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4543
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004544 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304545 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004546 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304547
Amit Blayd6f38282012-10-29 13:13:46 +02004548 if (motg->pdata->delay_lpm_on_disconnect) {
4549 pm_runtime_set_autosuspend_delay(&pdev->dev,
4550 lpm_disconnect_thresh);
4551 pm_runtime_use_autosuspend(&pdev->dev);
4552 }
4553
David Keitel272ce522012-08-17 16:25:24 -07004554 motg->usb_psy.name = "usb";
4555 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4556 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4557 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4558 motg->usb_psy.properties = otg_pm_power_props_usb;
4559 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4560 motg->usb_psy.get_property = otg_power_get_property_usb;
4561 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004562 motg->usb_psy.property_is_writeable
4563 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004564
Jack Pham0c695282012-10-19 18:13:03 -07004565 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004566 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004567 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4568 legacy_power_supply = true;
4569 } else {
4570 /* otherwise register our own power supply */
4571 if (!msm_otg_register_power_supply(pdev, motg))
4572 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004573 }
4574
Jack Pham0c695282012-10-19 18:13:03 -07004575 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4576 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4577
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304578 ret = msm_otg_setup_ext_chg_cdev(motg);
4579 if (ret)
4580 dev_dbg(&pdev->dev, "fail to setup cdev\n");
4581
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304582 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004583
Steve Mucklef132c6c2012-06-06 18:30:57 -07004584remove_phy:
4585 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004586free_async_irq:
4587 if (motg->async_irq)
4588 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304589free_irq:
4590 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004591destroy_wlock:
4592 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304593 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004594 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004595free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304596 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304597free_hsusb_vdd:
4598 regulator_disable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304599free_config_vddcx:
Mayank Rana0f286cf2013-02-27 11:43:27 +05304600 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304601 vdd_val[motg->vdd_type][VDD_NONE],
4602 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004603devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304604 clk_disable_unprepare(motg->pclk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304605 if (!IS_ERR(motg->xo_clk))
4606 clk_disable_unprepare(motg->xo_clk);
4607 else
4608 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004609free_xo_handle:
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304610 if (!IS_ERR(motg->xo_clk))
4611 clk_put(motg->xo_clk);
4612 else
4613 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304614free_regs:
4615 iounmap(motg->regs);
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304616disable_sleep_clk:
4617 if (!IS_ERR(motg->sleep_clk))
4618 clk_disable_unprepare(motg->sleep_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004619put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304620 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304621put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004622 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304623put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304624 if (!IS_ERR(motg->clk))
4625 clk_put(motg->clk);
Manu Gautame3a39082013-06-11 10:42:56 +05304626devote_bus_bw:
4627 if (motg->bus_perf_client) {
4628 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
4629 msm_bus_scale_unregister_client(motg->bus_perf_client);
4630 }
4631
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304632 return ret;
4633}
4634
4635static int __devexit msm_otg_remove(struct platform_device *pdev)
4636{
4637 struct msm_otg *motg = platform_get_drvdata(pdev);
Stephen Boyd9850acb2013-01-28 14:11:20 -08004638 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304639 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304640
Stephen Boyd9850acb2013-01-28 14:11:20 -08004641 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304642 return -EBUSY;
4643
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304644 if (!motg->ext_chg_device) {
4645 device_destroy(motg->ext_chg_class, motg->ext_chg_dev);
4646 cdev_del(&motg->ext_chg_cdev);
4647 class_destroy(motg->ext_chg_class);
4648 unregister_chrdev_region(motg->ext_chg_dev, 1);
4649 }
4650
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304651 if (pdev->dev.of_node)
4652 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004653 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4654 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304655 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304656 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304657 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304658 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004659 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304660 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304661
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304662 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304663
4664 device_init_wakeup(&pdev->dev, 0);
4665 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004666 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304667
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304668 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004669 if (motg->pdata->pmic_id_irq)
4670 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004671 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304672 free_irq(motg->irq, motg);
4673
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304674 if ((motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) &&
4675 (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int))
4676 device_remove_file(&pdev->dev,
4677 &dev_attr_dpdm_pulldown_enable);
Jack Pham87f202f2012-08-06 00:24:22 -07004678 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4679 motg->pdata->mpm_otgsessvld_int)
4680 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4681
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304682 if (motg->pdata->mpm_dpshv_int)
4683 msm_mpm_enable_pin(motg->pdata->mpm_dpshv_int, 0);
4684 if (motg->pdata->mpm_dmshv_int)
4685 msm_mpm_enable_pin(motg->pdata->mpm_dmshv_int, 0);
4686
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304687 /*
4688 * Put PHY in low power mode.
4689 */
Stephen Boyd9850acb2013-01-28 14:11:20 -08004690 ulpi_read(phy, 0x14);
4691 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304692
4693 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4694 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4695 if (readl(USB_PORTSC) & PORTSC_PHCD)
4696 break;
4697 udelay(1);
4698 cnt++;
4699 }
4700 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Stephen Boyd9850acb2013-01-28 14:11:20 -08004701 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304702
Manu Gautam28b1bac2012-01-30 16:43:06 +05304703 clk_disable_unprepare(motg->pclk);
4704 clk_disable_unprepare(motg->core_clk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304705 if (!IS_ERR(motg->xo_clk)) {
4706 clk_disable_unprepare(motg->xo_clk);
4707 clk_put(motg->xo_clk);
4708 } else {
4709 msm_xo_put(motg->xo_handle);
4710 }
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304711
4712 if (!IS_ERR(motg->sleep_clk))
4713 clk_disable_unprepare(motg->sleep_clk);
4714
Amit Blay81801aa2012-09-19 12:08:12 +02004715 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304716 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304717 regulator_disable(hsusb_vdd);
4718 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304719 vdd_val[motg->vdd_type][VDD_NONE],
4720 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304721
4722 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304723 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304724
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304725 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304726 if (!IS_ERR(motg->clk))
4727 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004728 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304729
Manu Gautame3a39082013-06-11 10:42:56 +05304730 if (motg->bus_perf_client) {
4731 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304732 msm_bus_scale_unregister_client(motg->bus_perf_client);
Manu Gautame3a39082013-06-11 10:42:56 +05304733 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304734
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304735 return 0;
4736}
4737
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304738#ifdef CONFIG_PM_RUNTIME
4739static int msm_otg_runtime_idle(struct device *dev)
4740{
4741 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004742 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304743
4744 dev_dbg(dev, "OTG runtime idle\n");
4745
Steve Mucklef132c6c2012-06-06 18:30:57 -07004746 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304747 return -EAGAIN;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304748
4749 if (motg->ext_chg_active) {
4750 dev_dbg(dev, "Deferring LPM\n");
4751 /*
4752 * Charger detection may happen in user space.
4753 * Delay entering LPM by 3 sec. Otherwise we
4754 * have to exit LPM when user space begins
4755 * charger detection.
4756 *
4757 * This timer will be canceled when user space
4758 * votes against LPM by incrementing PM usage
4759 * counter. We enter low power mode when
4760 * PM usage counter is decremented.
4761 */
4762 pm_schedule_suspend(dev, 3000);
4763 return -EAGAIN;
4764 }
4765
4766 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304767}
4768
4769static int msm_otg_runtime_suspend(struct device *dev)
4770{
4771 struct msm_otg *motg = dev_get_drvdata(dev);
4772
4773 dev_dbg(dev, "OTG runtime suspend\n");
4774 return msm_otg_suspend(motg);
4775}
4776
4777static int msm_otg_runtime_resume(struct device *dev)
4778{
4779 struct msm_otg *motg = dev_get_drvdata(dev);
4780
4781 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304782 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304783 return msm_otg_resume(motg);
4784}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304785#endif
4786
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304787#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304788static int msm_otg_pm_suspend(struct device *dev)
4789{
Jack Pham5ca279b2012-05-14 18:42:54 -07004790 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304791 struct msm_otg *motg = dev_get_drvdata(dev);
4792
4793 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004794
4795 atomic_set(&motg->pm_suspended, 1);
4796 ret = msm_otg_suspend(motg);
4797 if (ret)
4798 atomic_set(&motg->pm_suspended, 0);
4799
4800 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304801}
4802
4803static int msm_otg_pm_resume(struct device *dev)
4804{
Jack Pham5ca279b2012-05-14 18:42:54 -07004805 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304806 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304807
4808 dev_dbg(dev, "OTG PM resume\n");
4809
Jack Pham5ca279b2012-05-14 18:42:54 -07004810 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004811 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004812 pm_runtime_get_noresume(dev);
4813 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304814
Jack Pham5ca279b2012-05-14 18:42:54 -07004815 /* Update runtime PM status */
4816 pm_runtime_disable(dev);
4817 pm_runtime_set_active(dev);
4818 pm_runtime_enable(dev);
4819
Jack Phamc7edb172012-08-13 15:32:39 -07004820 if (motg->sm_work_pending) {
4821 motg->sm_work_pending = false;
4822 queue_work(system_nrt_wq, &motg->sm_work);
4823 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004824 }
4825
4826 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304827}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304828#endif
4829
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304830#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304831static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304832 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4833 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4834 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304835};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304836#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304837
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304838static struct of_device_id msm_otg_dt_match[] = {
4839 { .compatible = "qcom,hsusb-otg",
4840 },
4841 {}
4842};
4843
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304844static struct platform_driver msm_otg_driver = {
4845 .remove = __devexit_p(msm_otg_remove),
4846 .driver = {
4847 .name = DRIVER_NAME,
4848 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304849#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304850 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304851#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304852 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304853 },
4854};
4855
4856static int __init msm_otg_init(void)
4857{
4858 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4859}
4860
4861static void __exit msm_otg_exit(void)
4862{
4863 platform_driver_unregister(&msm_otg_driver);
4864}
4865
4866module_init(msm_otg_init);
4867module_exit(msm_otg_exit);
4868
4869MODULE_LICENSE("GPL v2");
4870MODULE_DESCRIPTION("MSM USB transceiver driver");