blob: 4cf47036e91a751e9aa432c41a2e269eff765f46 [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
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530885 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530886 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
887 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700888 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300889 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
890 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530891 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530892 prop_charger = motg->chg_type == USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +0800893 floated_charger = motg->chg_type == USB_FLOATED_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700894
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +0530895 /* Enable line state difference wakeup fix for only device and host
896 * bus suspend scenarios. Otherwise PHY can not be suspended when
897 * a charger that pulls DP/DM high is connected.
898 */
899 config2 = readl_relaxed(USB_GENCONFIG2);
900 if (device_bus_suspend)
901 config2 |= GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
902 else
903 config2 &= ~GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
904 writel_relaxed(config2, USB_GENCONFIG2);
905
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530906 /*
907 * Abort suspend when,
908 * 1. charging detection in progress due to cable plug-in
909 * 2. host mode activation in progress due to Micro-A cable insertion
910 */
911
912 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
zhenhuahb6d5edc2013-06-19 14:37:32 +0800913 !dcp && !prop_charger && !floated_charger) ||
914 test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham502bea32012-08-13 15:34:20 -0700915 enable_irq(motg->irq);
916 return -EBUSY;
917 }
918
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530919 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530920 * Chipidea 45-nm PHY suspend sequence:
921 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530922 * Interrupt Latch Register auto-clear feature is not present
923 * in all PHY versions. Latch register is clear on read type.
924 * Clear latch register to avoid spurious wakeup from
925 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530926 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530927 * PHY comparators are disabled when PHY enters into low power
928 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
929 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
930 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530931 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530932 * PLL is not turned off when PHY enters into low power mode (LPM).
933 * Disable PLL for maximum power savings.
934 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530935
936 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200937 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530938 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200939 ulpi_write(phy, 0x01, 0x30);
940 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530941 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530942
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530944 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530945 * PHY may take some time or even fail to enter into low power
946 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
947 * in failure case.
948 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530949 portsc = readl_relaxed(USB_PORTSC);
950 if (!(portsc & PORTSC_PHCD)) {
951 writel_relaxed(portsc | PORTSC_PHCD,
952 USB_PORTSC);
953 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
954 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
955 break;
956 udelay(1);
957 cnt++;
958 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530959 }
960
961 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200962 dev_err(phy->dev, "Unable to suspend PHY\n");
963 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964 enable_irq(motg->irq);
965 return -ETIMEDOUT;
966 }
967
968 /*
969 * PHY has capability to generate interrupt asynchronously in low
970 * power mode (LPM). This interrupt is level triggered. So USB IRQ
971 * line must be disabled till async interrupt enable bit is cleared
972 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
973 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530974 *
975 * PHY retention mode is disallowed while entering to LPM with wall
976 * charger connected. But PHY is put into suspend mode. Hence
977 * enable asynchronous interrupt to detect charger disconnection when
978 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530979 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530980 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300981 if (host_bus_suspend || device_bus_suspend ||
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530982 (motg->pdata->otg_control == OTG_PHY_CONTROL))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530983 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
984 else
985 cmd_val |= ULPI_STP_CTRL;
986 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530987
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530988 /*
989 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
990 * PHY retention and collapse can not happen with VDP_SRC enabled.
991 */
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +0530992 if (motg->caps & ALLOW_PHY_RETENTION && !device_bus_suspend && !dcp &&
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +0530993 (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION)
994 && (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS))))) {
Amit Blay58b31472011-11-18 09:39:39 +0200995 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530996 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +0200997 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530998 if ((motg->pdata->mode == USB_OTG) ||
999 (motg->pdata->mode == USB_HOST))
1000 phy_ctrl_val |= (PHY_IDHV_INTEN |
1001 PHY_OTGSESSVLDHV_INTEN);
1002 else
1003 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
1004 }
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301005 if (host_bus_suspend)
1006 phy_ctrl_val |= PHY_CLAMP_DPDMSE_EN;
Amit Blay58b31472011-11-18 09:39:39 +02001007 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301009 }
1010
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011 /* Ensure that above operation is completed before turning off clocks */
1012 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001013 /* Consider clocks on workaround flag only in case of bus suspend */
1014 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
1015 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
1016 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001017 clk_disable_unprepare(motg->pclk);
1018 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001019 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001020 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301021
Anji jonnala7da3f262011-12-02 17:22:14 -08001022 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05301023 if (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION) &&
1024 (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS)))) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301025 if (!IS_ERR(motg->xo_clk)) {
1026 clk_disable_unprepare(motg->xo_clk);
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301027 motg->lpm_flags |= XO_SHUTDOWN;
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301028 } else {
1029 ret = msm_xo_mode_vote(motg->xo_handle,
1030 MSM_XO_MODE_OFF);
1031 if (ret)
1032 dev_err(phy->dev, "%s fail to devote XO %d\n",
1033 __func__, ret);
1034 else
1035 motg->lpm_flags |= XO_SHUTDOWN;
1036 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301037 }
Anji jonnala7da3f262011-12-02 17:22:14 -08001038
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301039 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
1040 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +02001041 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001043 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
1044 !host_bus_suspend && !device_bus_suspend && !dcp) {
1045 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
1046 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +05301047 }
1048
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301049 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301051 msm_hsusb_mhl_switch_enable(motg, 0);
1052 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001053
Steve Mucklef132c6c2012-06-06 18:30:57 -07001054 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001055 if (motg->async_irq)
1056 enable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001057 else
1058 enable_irq_wake(motg->irq);
1059
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001060 if (motg->pdata->pmic_id_irq)
1061 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001062 if (pdata->otg_control == OTG_PHY_CONTROL &&
1063 pdata->mpm_otgsessvld_int)
1064 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301065 if (host_bus_suspend && pdata->mpm_dpshv_int)
1066 msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 1);
1067 if (host_bus_suspend && pdata->mpm_dmshv_int)
1068 msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001069 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301070 if (bus)
1071 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1072
Manu Gautame3a39082013-06-11 10:42:56 +05301073 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
1074
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301075 motg->host_bus_suspend = host_bus_suspend;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301076 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001077 /* Enable ASYNC IRQ (if present) during LPM */
1078 if (motg->async_irq)
1079 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301080 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001081 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301082
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001083 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301084
1085 return 0;
1086}
1087
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301088static int msm_otg_resume(struct msm_otg *motg)
1089{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001090 struct usb_phy *phy = &motg->phy;
1091 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001092 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301093 int cnt = 0;
1094 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001095 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001096 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301097
1098 if (!atomic_read(&motg->in_lpm))
1099 return 0;
1100
Ido Shayevitz44ca03c2013-07-09 15:57:21 +03001101 if (motg->pdata->delay_lpm_hndshk_on_disconnect)
1102 msm_bam_notify_lpm_resume();
1103
Jack Pham8978b892012-10-17 16:31:39 -07001104 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001105 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301106
Manu Gautame3a39082013-06-11 10:42:56 +05301107 /* Some platforms require BUS vote to enable/disable clocks */
1108 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
1109
Anji jonnala7da3f262011-12-02 17:22:14 -08001110 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301111 if (motg->lpm_flags & XO_SHUTDOWN) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301112 if (!IS_ERR(motg->xo_clk)) {
1113 clk_prepare_enable(motg->xo_clk);
1114 } else {
1115 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1116 if (ret)
1117 dev_err(phy->dev, "%s fail to vote for XO %d\n",
1118 __func__, ret);
1119 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301120 motg->lpm_flags &= ~XO_SHUTDOWN;
1121 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301122
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001123 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001124 clk_prepare_enable(motg->core_clk);
1125 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001126 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001127 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301128
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001129 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001130 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001132 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1133 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1134 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135 }
1136
1137 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301138 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301139 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001140 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1141 phy_ctrl_val |= PHY_RETEN;
1142 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1143 /* Disable PHY HV interrupts */
1144 phy_ctrl_val &=
1145 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301146 phy_ctrl_val &= ~(PHY_CLAMP_DPDMSE_EN);
Amit Blay58b31472011-11-18 09:39:39 +02001147 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301149 }
1150
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301151 temp = readl(USB_USBCMD);
1152 temp &= ~ASYNC_INTR_CTRL;
1153 temp &= ~ULPI_STP_CTRL;
1154 writel(temp, USB_USBCMD);
1155
1156 /*
1157 * PHY comes out of low power mode (LPM) in case of wakeup
1158 * from asynchronous interrupt.
1159 */
1160 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1161 goto skip_phy_resume;
1162
1163 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1164 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1165 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1166 break;
1167 udelay(1);
1168 cnt++;
1169 }
1170
1171 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1172 /*
1173 * This is a fatal error. Reset the link and
1174 * PHY. USB state can not be restored. Re-insertion
1175 * of USB cable is the only way to get USB working.
1176 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001177 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301178 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001179 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301180 }
1181
1182skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001183 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001184 if (motg->async_irq)
1185 disable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001186 else
1187 disable_irq_wake(motg->irq);
1188
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 if (motg->pdata->pmic_id_irq)
1190 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001191 if (pdata->otg_control == OTG_PHY_CONTROL &&
1192 pdata->mpm_otgsessvld_int)
1193 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301194 if (motg->host_bus_suspend && pdata->mpm_dpshv_int)
1195 msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 0);
1196 if (motg->host_bus_suspend && pdata->mpm_dmshv_int)
1197 msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301199 if (bus)
1200 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1201
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301202 atomic_set(&motg->in_lpm, 0);
1203
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301204 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001205 /* Match the disable_irq call from ISR */
1206 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301207 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301208 }
Jack Pham8978b892012-10-17 16:31:39 -07001209 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301210
Manu Gautamf8c45642012-08-10 10:20:56 -07001211 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1212 if (motg->async_irq)
1213 disable_irq(motg->async_irq);
1214
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001215 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301216
1217 return 0;
1218}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301219#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301220
David Keitel272ce522012-08-17 16:25:24 -07001221static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001222{
Jack Pham0c695282012-10-19 18:13:03 -07001223 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001224 pr_err("No USB power supply registered!\n");
1225 return;
1226 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001227
Jack Pham0c695282012-10-19 18:13:03 -07001228 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001229 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301230 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001231 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301232 } else {
David Keitel272ce522012-08-17 16:25:24 -07001233 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301234 /*
1235 * VBUS comparator is disabled by PMIC charging driver
1236 * when SYSTEM scope is selected. For ID_GND->ID_A
1237 * transition, give 50 msec delay so that PMIC charger
1238 * driver detect the VBUS and ready for accepting
1239 * charging current value from USB.
1240 */
1241 if (test_bit(ID_A, &motg->inputs))
1242 msleep(50);
1243 }
David Keitel272ce522012-08-17 16:25:24 -07001244 } else {
1245 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001246 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001247 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001248}
1249
David Keitel081a3e22012-04-18 12:37:07 -07001250static int msm_otg_notify_chg_type(struct msm_otg *motg)
1251{
1252 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001253
David Keitel081a3e22012-04-18 12:37:07 -07001254 /*
1255 * TODO
1256 * Unify OTG driver charger types and power supply charger types
1257 */
1258 if (charger_type == motg->chg_type)
1259 return 0;
1260
1261 if (motg->chg_type == USB_SDP_CHARGER)
1262 charger_type = POWER_SUPPLY_TYPE_USB;
1263 else if (motg->chg_type == USB_CDP_CHARGER)
1264 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301265 else if (motg->chg_type == USB_DCP_CHARGER ||
1266 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001267 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1268 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1269 motg->chg_type == USB_ACA_A_CHARGER ||
1270 motg->chg_type == USB_ACA_B_CHARGER ||
1271 motg->chg_type == USB_ACA_C_CHARGER))
1272 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1273 else
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301274 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001275
Jack Pham0c695282012-10-19 18:13:03 -07001276 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001277 pr_err("No USB power supply registered!\n");
1278 return -EINVAL;
1279 }
1280
1281 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001282 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001283 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001284}
1285
Amit Blay0f7edf72012-01-15 10:11:27 +02001286static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1287{
Jack Pham0c695282012-10-19 18:13:03 -07001288 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001289 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1290 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001291 }
1292
David Keitel272ce522012-08-17 16:25:24 -07001293 if (motg->cur_power == 0 && mA > 2) {
1294 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001295 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001296 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001297 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001298 goto psy_error;
1299 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1300 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001301 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001302 goto psy_error;
1303 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001304 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001305 goto psy_error;
Manu Gautamfca298c2013-06-05 15:11:54 +05301306 } else {
1307 if (power_supply_set_online(psy, true))
1308 goto psy_error;
1309 /* Current has changed (100/2 --> 500) */
1310 if (power_supply_set_current_limit(psy, 1000*mA))
1311 goto psy_error;
David Keitel272ce522012-08-17 16:25:24 -07001312 }
Manu Gautamfca298c2013-06-05 15:11:54 +05301313
Jack Pham0c695282012-10-19 18:13:03 -07001314 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001315 return 0;
1316
David Keitel272ce522012-08-17 16:25:24 -07001317psy_error:
1318 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001319 return -ENXIO;
1320}
1321
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301322static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1323{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001324 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301325
1326 if (g && g->is_a_peripheral)
1327 return;
1328
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301329 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1330 motg->chg_type == USB_ACA_A_CHARGER ||
1331 motg->chg_type == USB_ACA_B_CHARGER ||
1332 motg->chg_type == USB_ACA_C_CHARGER) &&
1333 mA > IDEV_ACA_CHG_LIMIT)
1334 mA = IDEV_ACA_CHG_LIMIT;
1335
David Keitel081a3e22012-04-18 12:37:07 -07001336 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001337 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001338 "Failed notifying %d charger type to PMIC\n",
1339 motg->chg_type);
1340
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301341 if (motg->cur_power == mA)
1342 return;
1343
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001344 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001345
1346 /*
1347 * Use Power Supply API if supported, otherwise fallback
1348 * to legacy pm8921 API.
1349 */
1350 if (msm_otg_notify_power_supply(motg, mA))
1351 pm8921_charger_vbus_draw(mA);
1352
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301353 motg->cur_power = mA;
1354}
1355
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001356static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301357{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001358 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301359
1360 /*
1361 * Gadget driver uses set_power method to notify about the
1362 * available current based on suspend/configured states.
1363 *
1364 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1365 * states when CDP/ACA is connected.
1366 */
1367 if (motg->chg_type == USB_SDP_CHARGER)
1368 msm_otg_notify_charger(motg, mA);
1369
1370 return 0;
1371}
1372
Steve Mucklef132c6c2012-06-06 18:30:57 -07001373static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301374{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001375 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301376 struct msm_otg_platform_data *pdata = motg->pdata;
1377 struct usb_hcd *hcd;
1378
1379 if (!otg->host)
1380 return;
1381
1382 hcd = bus_to_hcd(otg->host);
1383
1384 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001385 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301386
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301387 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001388 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301389 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1390
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301391 /*
1392 * Some boards have a switch cotrolled by gpio
1393 * to enable/disable internal HUB. Enable internal
1394 * HUB before kicking the host.
1395 */
1396 if (pdata->setup_gpio)
1397 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301398 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301399 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001400 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301401
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301402 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301403 /* HCD core reset all bits of PORTSC. select ULPI phy */
1404 writel_relaxed(0x80000000, USB_PORTSC);
1405
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301406 if (pdata->setup_gpio)
1407 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301408
1409 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001410 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301411 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301412 }
1413}
1414
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415static int msm_otg_usbdev_notify(struct notifier_block *self,
1416 unsigned long action, void *priv)
1417{
1418 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001419 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301420 struct usb_device *udev = priv;
1421
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301422 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1423 goto out;
1424
Steve Mucklef132c6c2012-06-06 18:30:57 -07001425 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301426 goto out;
1427 /*
1428 * Interested in devices connected directly to the root hub.
1429 * ACA dock can supply IDEV_CHG irrespective devices connected
1430 * on the accessory port.
1431 */
1432 if (!udev->parent || udev->parent->parent ||
1433 motg->chg_type == USB_ACA_DOCK_CHARGER)
1434 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435
1436 switch (action) {
1437 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301438 if (aca_enabled())
1439 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001440 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301441 pr_debug("B_CONN set\n");
1442 set_bit(B_CONN, &motg->inputs);
1443 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001444 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301445 /*
1446 * OTG PET: A-device must end session within
1447 * 10 sec after PET enumeration.
1448 */
1449 if (udev->quirks & USB_QUIRK_OTG_PET)
1450 msm_otg_start_timer(motg, TA_TST_MAINT,
1451 A_TST_MAINT);
1452 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301453 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001454 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455 if (udev->actconfig)
1456 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1457 else
1458 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001459 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301460 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301461 break;
1462 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001463 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1464 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301465 pr_debug("B_CONN clear\n");
1466 clear_bit(B_CONN, &motg->inputs);
1467 /*
1468 * OTG PET: A-device must end session after
1469 * PET disconnection if it is enumerated
1470 * with bcdDevice[0] = 1. USB core sets
1471 * bus->otg_vbus_off for us. clear it here.
1472 */
1473 if (udev->bus->otg_vbus_off) {
1474 udev->bus->otg_vbus_off = 0;
1475 set_bit(A_BUS_DROP, &motg->inputs);
1476 }
1477 queue_work(system_nrt_wq, &motg->sm_work);
1478 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001479 default:
1480 break;
1481 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301482 if (test_bit(ID_A, &motg->inputs))
1483 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1484 motg->mA_port);
1485out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001486 return NOTIFY_OK;
1487}
1488
Mayank Ranae3926882011-12-26 09:47:54 +05301489static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1490{
1491 int ret;
1492 static bool vbus_is_on;
1493
1494 if (vbus_is_on == on)
1495 return;
1496
1497 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301498 ret = motg->pdata->vbus_power(on);
1499 if (!ret)
1500 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301501 return;
1502 }
1503
1504 if (!vbus_otg) {
1505 pr_err("vbus_otg is NULL.");
1506 return;
1507 }
1508
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001509 /*
1510 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001511 * from usb before turning on the boost.
1512 * if exiting host mode disable the boost before enabling to draw
1513 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001514 */
Mayank Ranae3926882011-12-26 09:47:54 +05301515 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001516 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301517 ret = regulator_enable(vbus_otg);
1518 if (ret) {
1519 pr_err("unable to enable vbus_otg\n");
1520 return;
1521 }
1522 vbus_is_on = true;
1523 } else {
1524 ret = regulator_disable(vbus_otg);
1525 if (ret) {
1526 pr_err("unable to disable vbus_otg\n");
1527 return;
1528 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001529 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301530 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301531 }
1532}
1533
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001534static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301535{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001536 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301537 struct usb_hcd *hcd;
1538
1539 /*
1540 * Fail host registration if this board can support
1541 * only peripheral configuration.
1542 */
1543 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001544 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301545 return -ENODEV;
1546 }
1547
Mayank Ranae3926882011-12-26 09:47:54 +05301548 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001549 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301550 if (IS_ERR(vbus_otg)) {
1551 pr_err("Unable to get vbus_otg\n");
Manu Gautam139a8132013-06-04 16:46:50 +05301552 return PTR_ERR(vbus_otg);
Mayank Ranae3926882011-12-26 09:47:54 +05301553 }
1554 }
1555
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301556 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001557 if (otg->phy->state == OTG_STATE_A_HOST) {
1558 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001559 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301560 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301561 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301562 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001563 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301564 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301565 } else {
1566 otg->host = NULL;
1567 }
1568
1569 return 0;
1570 }
1571
1572 hcd = bus_to_hcd(host);
1573 hcd->power_budget = motg->pdata->power_budget;
1574
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301575#ifdef CONFIG_USB_OTG
1576 host->otg_port = 1;
1577#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001578 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1579 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301580 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001581 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301582
1583 /*
1584 * Kick the state machine work, if peripheral is not supported
1585 * or peripheral is already registered with us.
1586 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301587 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001588 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301589 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301590 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301591
1592 return 0;
1593}
1594
Steve Mucklef132c6c2012-06-06 18:30:57 -07001595static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301596{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001597 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301598 struct msm_otg_platform_data *pdata = motg->pdata;
1599
1600 if (!otg->gadget)
1601 return;
1602
1603 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001604 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301605 /*
1606 * Some boards have a switch cotrolled by gpio
1607 * to enable/disable internal HUB. Disable internal
1608 * HUB before kicking the gadget.
1609 */
1610 if (pdata->setup_gpio)
1611 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001612
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301613 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautame3a39082013-06-11 10:42:56 +05301614 if (debug_bus_voting_enabled)
1615 msm_otg_bus_vote(motg, USB_MAX_PERF_VOTE);
1616
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301617 usb_gadget_vbus_connect(otg->gadget);
1618 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001619 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301620 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301621 /* Configure BUS performance parameters to default */
Manu Gautame3a39082013-06-11 10:42:56 +05301622 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301623 if (pdata->setup_gpio)
1624 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1625 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301626}
1627
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001628static int msm_otg_set_peripheral(struct usb_otg *otg,
Stephen Boyd9850acb2013-01-28 14:11:20 -08001629 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301630{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001631 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301632
1633 /*
1634 * Fail peripheral registration if this board can support
1635 * only host configuration.
1636 */
1637 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001638 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301639 return -ENODEV;
1640 }
1641
1642 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001643 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1644 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301645 msm_otg_start_peripheral(otg, 0);
1646 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001647 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301648 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301649 } else {
1650 otg->gadget = NULL;
1651 }
1652
1653 return 0;
1654 }
1655 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001656 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301657
1658 /*
1659 * Kick the state machine work, if host is not supported
1660 * or host is already registered with us.
1661 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301662 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001663 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301664 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301665 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301666
1667 return 0;
1668}
1669
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301670static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1671{
1672 unsigned long flags;
1673 int id;
1674
1675 if (!motg->pdata->pmic_id_irq)
1676 return -ENODEV;
1677
1678 local_irq_save(flags);
1679 id = irq_read_line(motg->pdata->pmic_id_irq);
1680 local_irq_restore(flags);
1681
1682 /*
1683 * If we can not read ID line state for some reason, treat
1684 * it as float. This would prevent MHL discovery and kicking
1685 * host mode unnecessarily.
1686 */
1687 return !!id;
1688}
1689
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301690static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1691 void (*callback)(int on))
1692{
1693 struct usb_phy *phy = &motg->phy;
1694 int ret;
1695
Manoj Raoa7bddd12012-08-27 20:36:45 -07001696 if (!motg->pdata->mhl_enable) {
1697 dev_dbg(phy->dev, "MHL feature not enabled\n");
1698 return -ENODEV;
1699 }
1700
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301701 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1702 !motg->pdata->pmic_id_irq) {
1703 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1704 return -ENODEV;
1705 }
1706
1707 if (!motg->pdata->mhl_dev_name) {
1708 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1709 return -ENODEV;
1710 }
1711
1712 if (callback)
1713 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1714 callback);
1715 else
1716 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1717
1718 if (ret)
1719 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1720 motg->pdata->mhl_dev_name, ret);
1721 else
1722 motg->mhl_enabled = true;
1723
1724 return ret;
1725}
1726
1727static void msm_otg_mhl_notify_online(int on)
1728{
1729 struct msm_otg *motg = the_msm_otg;
1730 struct usb_phy *phy = &motg->phy;
1731 bool queue = false;
1732
1733 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1734
1735 if (on) {
1736 set_bit(MHL, &motg->inputs);
1737 } else {
1738 clear_bit(MHL, &motg->inputs);
1739 queue = true;
1740 }
1741
1742 if (queue && phy->state != OTG_STATE_UNDEFINED)
1743 schedule_work(&motg->sm_work);
1744}
1745
1746static bool msm_otg_is_mhl(struct msm_otg *motg)
1747{
1748 struct usb_phy *phy = &motg->phy;
1749 int is_mhl, ret;
1750
1751 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1752 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1753 /*
1754 * MHL driver calls our callback saying that MHL connected
1755 * if RID_GND is detected. But at later part of discovery
1756 * it may figure out MHL is not connected and returns
1757 * false. Hence clear MHL input here.
1758 */
1759 clear_bit(MHL, &motg->inputs);
1760 dev_dbg(phy->dev, "MHL device not found\n");
1761 return false;
1762 }
1763
1764 set_bit(MHL, &motg->inputs);
1765 dev_dbg(phy->dev, "MHL device found\n");
1766 return true;
1767}
1768
1769static bool msm_chg_mhl_detect(struct msm_otg *motg)
1770{
1771 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301772
1773 if (!motg->mhl_enabled)
1774 return false;
1775
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301776 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301777
1778 if (id)
1779 return false;
1780
1781 mhl_det_in_progress = true;
1782 ret = msm_otg_is_mhl(motg);
1783 mhl_det_in_progress = false;
1784
1785 return ret;
1786}
1787
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301788static void msm_otg_chg_check_timer_func(unsigned long data)
1789{
1790 struct msm_otg *motg = (struct msm_otg *) data;
1791 struct usb_otg *otg = motg->phy.otg;
1792
1793 if (atomic_read(&motg->in_lpm) ||
1794 !test_bit(B_SESS_VLD, &motg->inputs) ||
1795 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1796 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1797 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1798 return;
1799 }
1800
1801 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1802 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1803 set_bit(B_FALSE_SDP, &motg->inputs);
1804 queue_work(system_nrt_wq, &motg->sm_work);
1805 }
1806}
1807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001808static bool msm_chg_aca_detect(struct msm_otg *motg)
1809{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001810 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001811 u32 int_sts;
1812 bool ret = false;
1813
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301814 if (!aca_enabled())
1815 goto out;
1816
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001817 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1818 goto out;
1819
Steve Mucklef132c6c2012-06-06 18:30:57 -07001820 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001821 switch (int_sts & 0x1C) {
1822 case 0x08:
1823 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001824 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001825 motg->chg_type = USB_ACA_A_CHARGER;
1826 motg->chg_state = USB_CHG_STATE_DETECTED;
1827 clear_bit(ID_B, &motg->inputs);
1828 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301829 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001830 ret = true;
1831 }
1832 break;
1833 case 0x0C:
1834 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001835 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001836 motg->chg_type = USB_ACA_B_CHARGER;
1837 motg->chg_state = USB_CHG_STATE_DETECTED;
1838 clear_bit(ID_A, &motg->inputs);
1839 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301840 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001841 ret = true;
1842 }
1843 break;
1844 case 0x10:
1845 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001846 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001847 motg->chg_type = USB_ACA_C_CHARGER;
1848 motg->chg_state = USB_CHG_STATE_DETECTED;
1849 clear_bit(ID_A, &motg->inputs);
1850 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301851 set_bit(ID, &motg->inputs);
1852 ret = true;
1853 }
1854 break;
1855 case 0x04:
1856 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001857 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301858 motg->chg_type = USB_INVALID_CHARGER;
1859 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1860 clear_bit(ID_A, &motg->inputs);
1861 clear_bit(ID_B, &motg->inputs);
1862 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001863 ret = true;
1864 }
1865 break;
1866 default:
1867 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1868 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301869 test_and_clear_bit(ID_C, &motg->inputs) |
1870 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001872 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001873 motg->chg_type = USB_INVALID_CHARGER;
1874 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1875 }
1876 }
1877out:
1878 return ret;
1879}
1880
1881static void msm_chg_enable_aca_det(struct msm_otg *motg)
1882{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001883 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001884
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301885 if (!aca_enabled())
1886 return;
1887
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001888 switch (motg->pdata->phy_type) {
1889 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301890 /* Disable ID_GND in link and PHY */
1891 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1892 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001893 ulpi_write(phy, 0x01, 0x0C);
1894 ulpi_write(phy, 0x10, 0x0F);
1895 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301896 /* Disable PMIC ID pull-up */
1897 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301898 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001899 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301900 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001901 break;
1902 default:
1903 break;
1904 }
1905}
1906
1907static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1908{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001909 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001910
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301911 if (!aca_enabled())
1912 return;
1913
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001914 switch (motg->pdata->phy_type) {
1915 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301916 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001917 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301918 break;
1919 default:
1920 break;
1921 }
1922}
1923
1924static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1925{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001926 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301927
1928 if (!aca_enabled())
1929 return;
1930
1931 switch (motg->pdata->phy_type) {
1932 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001933 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934 break;
1935 default:
1936 break;
1937 }
1938}
1939
1940static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1941{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001942 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001943 bool ret = false;
1944
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301945 if (!aca_enabled())
1946 return ret;
1947
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001948 switch (motg->pdata->phy_type) {
1949 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001950 if (ulpi_read(phy, 0x91) & 1) {
1951 dev_dbg(phy->dev, "RID change\n");
1952 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953 ret = msm_chg_aca_detect(motg);
1954 }
1955 default:
1956 break;
1957 }
1958 return ret;
1959}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301960
1961static void msm_otg_id_timer_func(unsigned long data)
1962{
1963 struct msm_otg *motg = (struct msm_otg *) data;
1964
1965 if (!aca_enabled())
1966 return;
1967
1968 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001969 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301970 return;
1971 }
1972
Steve Mucklef132c6c2012-06-06 18:30:57 -07001973 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301974 goto out;
1975
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301976 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001977 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301978 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301979 }
1980
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301981out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301982 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1983 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1984}
1985
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301986static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1987{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001988 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301989 u32 chg_det;
1990 bool ret = false;
1991
1992 switch (motg->pdata->phy_type) {
1993 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001994 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301995 ret = chg_det & (1 << 4);
1996 break;
1997 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001998 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301999 ret = chg_det & 1;
2000 break;
2001 default:
2002 break;
2003 }
2004 return ret;
2005}
2006
2007static void msm_chg_enable_secondary_det(struct msm_otg *motg)
2008{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002009 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302010 u32 chg_det;
2011
2012 switch (motg->pdata->phy_type) {
2013 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002014 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302015 /* Turn off charger block */
2016 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002017 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302018 udelay(20);
2019 /* control chg block via ULPI */
2020 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002021 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302022 /* put it in host mode for enabling D- source */
2023 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002024 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302025 /* Turn on chg detect block */
2026 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002027 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302028 udelay(20);
2029 /* enable chg detection */
2030 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002031 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302032 break;
2033 case SNPS_28NM_INTEGRATED_PHY:
2034 /*
2035 * Configure DM as current source, DP as current sink
2036 * and enable battery charging comparators.
2037 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002038 ulpi_write(phy, 0x8, 0x85);
2039 ulpi_write(phy, 0x2, 0x85);
2040 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302041 break;
2042 default:
2043 break;
2044 }
2045}
2046
2047static bool msm_chg_check_primary_det(struct msm_otg *motg)
2048{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002049 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302050 u32 chg_det;
2051 bool ret = false;
2052
2053 switch (motg->pdata->phy_type) {
2054 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002055 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302056 ret = chg_det & (1 << 4);
2057 break;
2058 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002059 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302060 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302061 /* Turn off VDP_SRC */
2062 ulpi_write(phy, 0x3, 0x86);
2063 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302064 break;
2065 default:
2066 break;
2067 }
2068 return ret;
2069}
2070
2071static void msm_chg_enable_primary_det(struct msm_otg *motg)
2072{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002073 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302074 u32 chg_det;
2075
2076 switch (motg->pdata->phy_type) {
2077 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002078 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302079 /* enable chg detection */
2080 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002081 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302082 break;
2083 case SNPS_28NM_INTEGRATED_PHY:
2084 /*
2085 * Configure DP as current source, DM as current sink
2086 * and enable battery charging comparators.
2087 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002088 ulpi_write(phy, 0x2, 0x85);
2089 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302090 break;
2091 default:
2092 break;
2093 }
2094}
2095
2096static bool msm_chg_check_dcd(struct msm_otg *motg)
2097{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002098 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302099 u32 line_state;
2100 bool ret = false;
2101
2102 switch (motg->pdata->phy_type) {
2103 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002104 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302105 ret = !(line_state & 1);
2106 break;
2107 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002108 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302109 ret = line_state & 2;
2110 break;
2111 default:
2112 break;
2113 }
2114 return ret;
2115}
2116
2117static void msm_chg_disable_dcd(struct msm_otg *motg)
2118{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002119 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302120 u32 chg_det;
2121
2122 switch (motg->pdata->phy_type) {
2123 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002124 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302125 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002126 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302127 break;
2128 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002129 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302130 break;
2131 default:
2132 break;
2133 }
2134}
2135
2136static void msm_chg_enable_dcd(struct msm_otg *motg)
2137{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002138 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302139 u32 chg_det;
2140
2141 switch (motg->pdata->phy_type) {
2142 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002143 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302144 /* Turn on D+ current source */
2145 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002146 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302147 break;
2148 case SNPS_28NM_INTEGRATED_PHY:
2149 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002150 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302151 break;
2152 default:
2153 break;
2154 }
2155}
2156
2157static void msm_chg_block_on(struct msm_otg *motg)
2158{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002159 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302160 u32 func_ctrl, chg_det;
2161
2162 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002163 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302164 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2165 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002166 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302167
2168 switch (motg->pdata->phy_type) {
2169 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002170 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302171 /* control chg block via ULPI */
2172 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002173 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302174 /* Turn on chg detect block */
2175 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002176 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302177 udelay(20);
2178 break;
2179 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302180 /* disable DP and DM pull down resistors */
2181 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302182 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002183 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302184 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002185 ulpi_write(phy, 0x1F, 0x92);
2186 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302187 udelay(100);
2188 break;
2189 default:
2190 break;
2191 }
2192}
2193
2194static void msm_chg_block_off(struct msm_otg *motg)
2195{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002196 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302197 u32 func_ctrl, chg_det;
2198
2199 switch (motg->pdata->phy_type) {
2200 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002201 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302202 /* Turn off charger block */
2203 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002204 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302205 break;
2206 case SNPS_28NM_INTEGRATED_PHY:
2207 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002208 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302209 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002210 ulpi_write(phy, 0x1F, 0x92);
2211 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetic4c69762013-07-08 15:03:26 +05302212 /* re-enable DP and DM pull down resistors */
2213 ulpi_write(phy, 0x6, 0xB);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302214 break;
2215 default:
2216 break;
2217 }
2218
2219 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002220 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302221 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2222 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002223 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302224}
2225
Anji jonnalad270e2d2011-08-09 11:28:32 +05302226static const char *chg_to_string(enum usb_chg_type chg_type)
2227{
2228 switch (chg_type) {
2229 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2230 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2231 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2232 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2233 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2234 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2235 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302236 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
zhenhuahb6d5edc2013-06-19 14:37:32 +08002237 case USB_FLOATED_CHARGER: return "USB_FLOATED_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302238 default: return "INVALID_CHARGER";
2239 }
2240}
2241
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302242#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2243#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302244#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2245#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302246static void msm_chg_detect_work(struct work_struct *w)
2247{
2248 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002249 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302250 bool is_dcd = false, tmout, vout, is_aca;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002251 static bool dcd;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302252 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302253 unsigned long delay;
2254
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002255 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302256
2257 if (test_bit(MHL, &motg->inputs)) {
2258 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2259 return;
2260 }
2261
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302262 switch (motg->chg_state) {
2263 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302264 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302265 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002266 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302267 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302268 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302269 delay = MSM_CHG_DCD_POLL_TIME;
2270 break;
2271 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302272 if (msm_chg_mhl_detect(motg)) {
2273 msm_chg_block_off(motg);
2274 motg->chg_state = USB_CHG_STATE_DETECTED;
2275 motg->chg_type = USB_INVALID_CHARGER;
2276 queue_work(system_nrt_wq, &motg->sm_work);
2277 return;
2278 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002279 is_aca = msm_chg_aca_detect(motg);
2280 if (is_aca) {
2281 /*
2282 * ID_A can be ACA dock too. continue
2283 * primary detection after DCD.
2284 */
2285 if (test_bit(ID_A, &motg->inputs)) {
2286 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2287 } else {
2288 delay = 0;
2289 break;
2290 }
2291 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302292 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302293 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2294 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302295 if (is_dcd || tmout) {
zhenhuahb6d5edc2013-06-19 14:37:32 +08002296 if (is_dcd)
2297 dcd = true;
2298 else
2299 dcd = false;
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302300 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302301 msm_chg_enable_primary_det(motg);
2302 delay = MSM_CHG_PRIMARY_DET_TIME;
2303 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2304 } else {
2305 delay = MSM_CHG_DCD_POLL_TIME;
2306 }
2307 break;
2308 case USB_CHG_STATE_DCD_DONE:
2309 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302310 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2311 dm_vlgc = line_state & PORTSC_LS_DM;
2312 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302313 if (test_bit(ID_A, &motg->inputs)) {
2314 motg->chg_type = USB_ACA_DOCK_CHARGER;
2315 motg->chg_state = USB_CHG_STATE_DETECTED;
2316 delay = 0;
2317 break;
2318 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302319 if (line_state) { /* DP > VLGC */
2320 motg->chg_type = USB_PROPRIETARY_CHARGER;
2321 motg->chg_state = USB_CHG_STATE_DETECTED;
2322 delay = 0;
2323 } else {
2324 msm_chg_enable_secondary_det(motg);
2325 delay = MSM_CHG_SECONDARY_DET_TIME;
2326 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2327 }
2328 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302329 if (test_bit(ID_A, &motg->inputs)) {
2330 motg->chg_type = USB_ACA_A_CHARGER;
2331 motg->chg_state = USB_CHG_STATE_DETECTED;
2332 delay = 0;
2333 break;
2334 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302335
2336 if (line_state) /* DP > VLGC or/and DM > VLGC */
2337 motg->chg_type = USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002338 else if (!dcd && floated_charger_enable)
2339 motg->chg_type = USB_FLOATED_CHARGER;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302340 else
2341 motg->chg_type = USB_SDP_CHARGER;
2342
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302343 motg->chg_state = USB_CHG_STATE_DETECTED;
2344 delay = 0;
2345 }
2346 break;
2347 case USB_CHG_STATE_PRIMARY_DONE:
2348 vout = msm_chg_check_secondary_det(motg);
2349 if (vout)
2350 motg->chg_type = USB_DCP_CHARGER;
2351 else
2352 motg->chg_type = USB_CDP_CHARGER;
2353 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2354 /* fall through */
2355 case USB_CHG_STATE_SECONDARY_DONE:
2356 motg->chg_state = USB_CHG_STATE_DETECTED;
2357 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302358 /*
2359 * Notify the charger type to power supply
2360 * owner as soon as we determine the charger.
2361 */
2362 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302363 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002364 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302365 /*
2366 * Spurious interrupt is seen after enabling ACA detection
2367 * due to which charger detection fails in case of PET.
2368 * Add delay of 100 microsec to avoid that.
2369 */
2370 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002371 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002372 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302373 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302374 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302375 return;
2376 default:
2377 return;
2378 }
2379
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302380 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302381}
2382
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302383/*
2384 * We support OTG, Peripheral only and Host only configurations. In case
2385 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2386 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2387 * enabled when switch is controlled by user and default mode is supplied
2388 * by board file, which can be changed by userspace later.
2389 */
2390static void msm_otg_init_sm(struct msm_otg *motg)
2391{
2392 struct msm_otg_platform_data *pdata = motg->pdata;
2393 u32 otgsc = readl(USB_OTGSC);
2394
2395 switch (pdata->mode) {
2396 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302398 if (pdata->default_mode == USB_HOST) {
2399 clear_bit(ID, &motg->inputs);
2400 } else if (pdata->default_mode == USB_PERIPHERAL) {
2401 set_bit(ID, &motg->inputs);
2402 set_bit(B_SESS_VLD, &motg->inputs);
2403 } else {
2404 set_bit(ID, &motg->inputs);
2405 clear_bit(B_SESS_VLD, &motg->inputs);
2406 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302407 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302408 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302409 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302410 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302411 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302412 set_bit(A_BUS_REQ, &motg->inputs);
2413 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002414 if (otgsc & OTGSC_BSV)
2415 set_bit(B_SESS_VLD, &motg->inputs);
2416 else
2417 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302418 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302419 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302420 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302421 set_bit(ID, &motg->inputs);
2422 else
2423 clear_bit(ID, &motg->inputs);
2424 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302425 /*
2426 * VBUS initial state is reported after PMIC
2427 * driver initialization. Wait for it.
2428 */
2429 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302430 }
2431 break;
2432 case USB_HOST:
2433 clear_bit(ID, &motg->inputs);
2434 break;
2435 case USB_PERIPHERAL:
2436 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302437 if (pdata->otg_control == OTG_PHY_CONTROL) {
2438 if (otgsc & OTGSC_BSV)
2439 set_bit(B_SESS_VLD, &motg->inputs);
2440 else
2441 clear_bit(B_SESS_VLD, &motg->inputs);
2442 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2443 /*
2444 * VBUS initial state is reported after PMIC
2445 * driver initialization. Wait for it.
2446 */
2447 wait_for_completion(&pmic_vbus_init);
2448 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302449 break;
2450 default:
2451 break;
2452 }
2453}
2454
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302455static void msm_otg_wait_for_ext_chg_done(struct msm_otg *motg)
2456{
2457 struct usb_phy *phy = &motg->phy;
2458 unsigned long t;
2459
2460 /*
2461 * Defer next cable connect event till external charger
2462 * detection is completed.
2463 */
2464
2465 if (motg->ext_chg_active) {
2466
2467 pr_debug("before msm_otg ext chg wait\n");
2468
2469 t = wait_for_completion_timeout(&motg->ext_chg_wait,
2470 msecs_to_jiffies(3000));
2471 if (!t)
2472 pr_err("msm_otg ext chg wait timeout\n");
2473 else
2474 pr_debug("msm_otg ext chg wait done\n");
2475 }
2476
2477 if (motg->ext_chg_opened) {
2478 if (phy->flags & ENABLE_DP_MANUAL_PULLUP) {
2479 ulpi_write(phy, ULPI_MISC_A_VBUSVLDEXT |
2480 ULPI_MISC_A_VBUSVLDEXTSEL,
2481 ULPI_CLR(ULPI_MISC_A));
2482 }
2483 /* clear charging register bits */
2484 ulpi_write(phy, 0x3F, 0x86);
2485 /* re-enable DP and DM pull-down resistors*/
2486 ulpi_write(phy, 0x6, 0xB);
2487 }
2488}
2489
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302490static void msm_otg_sm_work(struct work_struct *w)
2491{
2492 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002493 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302494 bool work = 0, srp_reqd, dcp;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302495
Steve Mucklef132c6c2012-06-06 18:30:57 -07002496 pm_runtime_resume(otg->phy->dev);
2497 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002498 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302499 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002500 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302501 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002502 if (!psy && legacy_power_supply) {
2503 psy = power_supply_get_by_name("usb");
2504
2505 if (!psy)
2506 pr_err("couldn't get usb power supply\n");
2507 }
2508
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002509 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302510 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2511 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002512 pm_runtime_put_noidle(otg->phy->dev);
2513 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302514 break;
2515 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302516 /* FALL THROUGH */
2517 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302518 if (test_bit(MHL, &motg->inputs)) {
2519 /* allow LPM */
2520 pm_runtime_put_noidle(otg->phy->dev);
2521 pm_runtime_suspend(otg->phy->dev);
2522 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002523 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302524 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302525 if (msm_chg_mhl_detect(motg)) {
2526 work = 1;
2527 break;
2528 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302529 clear_bit(B_BUS_REQ, &motg->inputs);
2530 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002531 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302532 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302533 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302534 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302535 switch (motg->chg_state) {
2536 case USB_CHG_STATE_UNDEFINED:
2537 msm_chg_detect_work(&motg->chg_work.work);
2538 break;
2539 case USB_CHG_STATE_DETECTED:
2540 switch (motg->chg_type) {
2541 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302542 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002543 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302544 if (motg->ext_chg_opened) {
2545 init_completion(
2546 &motg->ext_chg_wait);
2547 motg->ext_chg_active = true;
2548 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302549 /* fall through */
2550 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302551 msm_otg_notify_charger(motg,
2552 IDEV_CHG_MAX);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302553 pm_runtime_put_sync(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302554 break;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002555 case USB_FLOATED_CHARGER:
2556 msm_otg_notify_charger(motg,
2557 IDEV_CHG_MAX);
2558 pm_runtime_put_noidle(otg->phy->dev);
2559 pm_runtime_suspend(otg->phy->dev);
2560 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302561 case USB_ACA_B_CHARGER:
2562 msm_otg_notify_charger(motg,
2563 IDEV_ACA_CHG_MAX);
2564 /*
2565 * (ID_B --> ID_C) PHY_ALT interrupt can
2566 * not be detected in LPM.
2567 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302568 break;
2569 case USB_CDP_CHARGER:
2570 msm_otg_notify_charger(motg,
2571 IDEV_CHG_MAX);
2572 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002573 otg->phy->state =
2574 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302575 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302576 case USB_ACA_C_CHARGER:
2577 msm_otg_notify_charger(motg,
2578 IDEV_ACA_CHG_MAX);
2579 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002580 otg->phy->state =
2581 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302582 break;
2583 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302584 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002585 otg->phy->state =
2586 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302587 mod_timer(&motg->chg_check_timer,
2588 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302589 break;
2590 default:
2591 break;
2592 }
2593 break;
2594 default:
2595 break;
2596 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302597 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2598 pr_debug("b_sess_end && b_bus_req\n");
2599 if (msm_otg_start_srp(otg) < 0) {
2600 clear_bit(B_BUS_REQ, &motg->inputs);
2601 work = 1;
2602 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302603 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002604 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302605 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2606 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302607 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302608 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302609 del_timer_sync(&motg->chg_check_timer);
2610 clear_bit(B_FALSE_SDP, &motg->inputs);
Mayank Rana8ab00352013-01-23 19:26:21 +05302611 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302612 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302613 dcp = (motg->chg_type == USB_DCP_CHARGER);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302614 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2615 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302616 msm_otg_notify_charger(motg, 0);
Manu Gautam6604b682013-09-04 17:50:17 +05302617 if (dcp) {
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302618 msm_otg_wait_for_ext_chg_done(motg);
Manu Gautam6604b682013-09-04 17:50:17 +05302619 /* Turn off VDP_SRC */
2620 ulpi_write(otg->phy, 0x2, 0x86);
2621 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002622 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302623 /*
2624 * There is a small window where ID interrupt
2625 * is not monitored during ID detection circuit
2626 * switch from ACA to PMIC. Check ID state
2627 * before entering into low power mode.
2628 */
2629 if (!msm_otg_read_pmic_id_state(motg)) {
2630 pr_debug("process missed ID intr\n");
2631 clear_bit(ID, &motg->inputs);
2632 work = 1;
2633 break;
2634 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002635 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002636 /*
2637 * Only if autosuspend was enabled in probe, it will be
2638 * used here. Otherwise, no delay will be used.
2639 */
2640 pm_runtime_mark_last_busy(otg->phy->dev);
2641 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302642 }
2643 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302644 case OTG_STATE_B_SRP_INIT:
2645 if (!test_bit(ID, &motg->inputs) ||
2646 test_bit(ID_A, &motg->inputs) ||
2647 test_bit(ID_C, &motg->inputs) ||
2648 (test_bit(B_SESS_VLD, &motg->inputs) &&
2649 !test_bit(ID_B, &motg->inputs))) {
2650 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2651 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002652 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302653 /*
2654 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2655 * bits after SRP initiation.
2656 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002657 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302658 work = 1;
2659 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2660 pr_debug("b_srp_fail\n");
2661 pr_info("A-device did not respond to SRP\n");
2662 clear_bit(B_BUS_REQ, &motg->inputs);
2663 clear_bit(B_SRP_FAIL, &motg->tmouts);
2664 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002665 ulpi_write(otg->phy, 0x0, 0x98);
2666 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302667 motg->b_last_se0_sess = jiffies;
2668 work = 1;
2669 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302670 break;
2671 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302672 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2673 test_bit(B_FALSE_SDP, &motg->inputs)) {
2674 pr_debug("B_FALSE_SDP\n");
2675 msm_otg_start_peripheral(otg, 0);
2676 motg->chg_type = USB_DCP_CHARGER;
2677 clear_bit(B_FALSE_SDP, &motg->inputs);
2678 otg->phy->state = OTG_STATE_B_IDLE;
2679 work = 1;
2680 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302681 test_bit(ID_A, &motg->inputs) ||
2682 test_bit(ID_B, &motg->inputs) ||
2683 !test_bit(B_SESS_VLD, &motg->inputs)) {
2684 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302685 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2686 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302687 msm_otg_notify_charger(motg, 0);
2688 srp_reqd = otg->gadget->otg_srp_reqd;
2689 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302690 if (test_bit(ID_B, &motg->inputs))
2691 clear_bit(ID_B, &motg->inputs);
2692 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002693 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302694 motg->b_last_se0_sess = jiffies;
2695 if (srp_reqd)
2696 msm_otg_start_timer(motg,
2697 TB_TST_SRP, B_TST_SRP);
2698 else
2699 work = 1;
2700 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2701 otg->gadget->b_hnp_enable &&
2702 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2703 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2704 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2705 /* D+ pullup should not be disconnected within 4msec
2706 * after A device suspends the bus. Otherwise PET will
2707 * fail the compliance test.
2708 */
2709 udelay(1000);
2710 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002711 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302712 /*
2713 * start HCD even before A-device enable
2714 * pull-up to meet HNP timings.
2715 */
2716 otg->host->is_b_host = 1;
2717 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002718 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2719 test_bit(B_SESS_VLD, &motg->inputs)) {
2720 pr_debug("a_bus_suspend && b_sess_vld\n");
2721 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002722 pm_runtime_put_noidle(otg->phy->dev);
2723 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002724 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002725 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302726 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002727 }
2728 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302729 case OTG_STATE_B_WAIT_ACON:
2730 if (!test_bit(ID, &motg->inputs) ||
2731 test_bit(ID_A, &motg->inputs) ||
2732 test_bit(ID_B, &motg->inputs) ||
2733 !test_bit(B_SESS_VLD, &motg->inputs)) {
2734 pr_debug("!id || id_a/b || !b_sess_vld\n");
2735 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302736 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302737 * A-device is physically disconnected during
2738 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302739 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302740 msm_otg_start_host(otg, 0);
2741 otg->host->is_b_host = 0;
2742
2743 clear_bit(B_BUS_REQ, &motg->inputs);
2744 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2745 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002746 otg->phy->state = OTG_STATE_B_IDLE;
2747 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302748 work = 1;
2749 } else if (test_bit(A_CONN, &motg->inputs)) {
2750 pr_debug("a_conn\n");
2751 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002752 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302753 /*
2754 * PET disconnects D+ pullup after reset is generated
2755 * by B device in B_HOST role which is not detected by
2756 * B device. As workaorund , start timer of 300msec
2757 * and stop timer if A device is enumerated else clear
2758 * A_CONN.
2759 */
2760 msm_otg_start_timer(motg, TB_TST_CONFIG,
2761 B_TST_CONFIG);
2762 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2763 pr_debug("b_ase0_brst_tmout\n");
2764 pr_info("B HNP fail:No response from A device\n");
2765 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002766 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302767 otg->host->is_b_host = 0;
2768 clear_bit(B_ASE0_BRST, &motg->tmouts);
2769 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2770 clear_bit(B_BUS_REQ, &motg->inputs);
2771 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002772 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302773 work = 1;
2774 } else if (test_bit(ID_C, &motg->inputs)) {
2775 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2776 }
2777 break;
2778 case OTG_STATE_B_HOST:
2779 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2780 !test_bit(A_CONN, &motg->inputs) ||
2781 !test_bit(B_SESS_VLD, &motg->inputs)) {
2782 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2783 clear_bit(A_CONN, &motg->inputs);
2784 clear_bit(B_BUS_REQ, &motg->inputs);
2785 msm_otg_start_host(otg, 0);
2786 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002787 otg->phy->state = OTG_STATE_B_IDLE;
2788 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302789 work = 1;
2790 } else if (test_bit(ID_C, &motg->inputs)) {
2791 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2792 }
2793 break;
2794 case OTG_STATE_A_IDLE:
2795 otg->default_a = 1;
2796 if (test_bit(ID, &motg->inputs) &&
2797 !test_bit(ID_A, &motg->inputs)) {
2798 pr_debug("id && !id_a\n");
2799 otg->default_a = 0;
2800 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002801 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302802 del_timer_sync(&motg->id_timer);
2803 msm_otg_link_reset(motg);
2804 msm_chg_enable_aca_intr(motg);
2805 msm_otg_notify_charger(motg, 0);
2806 work = 1;
2807 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2808 (test_bit(A_SRP_DET, &motg->inputs) ||
2809 test_bit(A_BUS_REQ, &motg->inputs))) {
2810 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2811
2812 clear_bit(A_SRP_DET, &motg->inputs);
2813 /* Disable SRP detection */
2814 writel_relaxed((readl_relaxed(USB_OTGSC) &
2815 ~OTGSC_INTSTS_MASK) &
2816 ~OTGSC_DPIE, USB_OTGSC);
2817
Steve Mucklef132c6c2012-06-06 18:30:57 -07002818 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302819 /* VBUS should not be supplied before end of SRP pulse
2820 * generated by PET, if not complaince test fail.
2821 */
2822 usleep_range(10000, 12000);
2823 /* ACA: ID_A: Stop charging untill enumeration */
2824 if (test_bit(ID_A, &motg->inputs))
2825 msm_otg_notify_charger(motg, 0);
2826 else
2827 msm_hsusb_vbus_power(motg, 1);
2828 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2829 } else {
2830 pr_debug("No session requested\n");
2831 clear_bit(A_BUS_DROP, &motg->inputs);
2832 if (test_bit(ID_A, &motg->inputs)) {
2833 msm_otg_notify_charger(motg,
2834 IDEV_ACA_CHG_MAX);
2835 } else if (!test_bit(ID, &motg->inputs)) {
2836 msm_otg_notify_charger(motg, 0);
2837 /*
2838 * A-device is not providing power on VBUS.
2839 * Enable SRP detection.
2840 */
2841 writel_relaxed(0x13, USB_USBMODE);
2842 writel_relaxed((readl_relaxed(USB_OTGSC) &
2843 ~OTGSC_INTSTS_MASK) |
2844 OTGSC_DPIE, USB_OTGSC);
2845 mb();
2846 }
2847 }
2848 break;
2849 case OTG_STATE_A_WAIT_VRISE:
2850 if ((test_bit(ID, &motg->inputs) &&
2851 !test_bit(ID_A, &motg->inputs)) ||
2852 test_bit(A_BUS_DROP, &motg->inputs) ||
2853 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2854 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2855 clear_bit(A_BUS_REQ, &motg->inputs);
2856 msm_otg_del_timer(motg);
2857 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002858 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302859 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2860 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2861 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002862 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302863 if (TA_WAIT_BCON > 0)
2864 msm_otg_start_timer(motg, TA_WAIT_BCON,
2865 A_WAIT_BCON);
Sujeet Kumarebaef0c2013-06-03 15:54:25 +05302866
2867 /* Clear BSV in host mode */
2868 clear_bit(B_SESS_VLD, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302869 msm_otg_start_host(otg, 1);
2870 msm_chg_enable_aca_det(motg);
2871 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302872 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302873 if (msm_chg_check_aca_intr(motg))
2874 work = 1;
2875 }
2876 break;
2877 case OTG_STATE_A_WAIT_BCON:
2878 if ((test_bit(ID, &motg->inputs) &&
2879 !test_bit(ID_A, &motg->inputs)) ||
2880 test_bit(A_BUS_DROP, &motg->inputs) ||
2881 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2882 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2883 "a_wait_bcon_tmout\n");
2884 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2885 pr_info("Device No Response\n");
2886 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2887 }
2888 msm_otg_del_timer(motg);
2889 clear_bit(A_BUS_REQ, &motg->inputs);
2890 clear_bit(B_CONN, &motg->inputs);
2891 msm_otg_start_host(otg, 0);
2892 /*
2893 * ACA: ID_A with NO accessory, just the A plug is
2894 * attached to ACA: Use IDCHG_MAX for charging
2895 */
2896 if (test_bit(ID_A, &motg->inputs))
2897 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2898 else
2899 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002900 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302901 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2902 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2903 pr_debug("!a_vbus_vld\n");
2904 clear_bit(B_CONN, &motg->inputs);
2905 msm_otg_del_timer(motg);
2906 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002907 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2908 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302909 } else if (test_bit(ID_A, &motg->inputs)) {
2910 msm_hsusb_vbus_power(motg, 0);
2911 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2912 /*
2913 * If TA_WAIT_BCON is infinite, we don;t
2914 * turn off VBUS. Enter low power mode.
2915 */
2916 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002917 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302918 } else if (!test_bit(ID, &motg->inputs)) {
2919 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302920 }
2921 break;
2922 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302923 if ((test_bit(ID, &motg->inputs) &&
2924 !test_bit(ID_A, &motg->inputs)) ||
2925 test_bit(A_BUS_DROP, &motg->inputs)) {
2926 pr_debug("id_a/b/c || a_bus_drop\n");
2927 clear_bit(B_CONN, &motg->inputs);
2928 clear_bit(A_BUS_REQ, &motg->inputs);
2929 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002930 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302931 msm_otg_start_host(otg, 0);
2932 if (!test_bit(ID_A, &motg->inputs))
2933 msm_hsusb_vbus_power(motg, 0);
2934 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2935 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2936 pr_debug("!a_vbus_vld\n");
2937 clear_bit(B_CONN, &motg->inputs);
2938 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002939 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302940 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002941 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302942 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2943 /*
2944 * a_bus_req is de-asserted when root hub is
2945 * suspended or HNP is in progress.
2946 */
2947 pr_debug("!a_bus_req\n");
2948 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002949 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302950 if (otg->host->b_hnp_enable)
2951 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2952 A_AIDL_BDIS);
2953 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002954 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302955 } else if (!test_bit(B_CONN, &motg->inputs)) {
2956 pr_debug("!b_conn\n");
2957 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002958 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302959 if (TA_WAIT_BCON > 0)
2960 msm_otg_start_timer(motg, TA_WAIT_BCON,
2961 A_WAIT_BCON);
2962 if (msm_chg_check_aca_intr(motg))
2963 work = 1;
2964 } else if (test_bit(ID_A, &motg->inputs)) {
2965 msm_otg_del_timer(motg);
2966 msm_hsusb_vbus_power(motg, 0);
2967 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2968 msm_otg_notify_charger(motg,
2969 IDEV_ACA_CHG_MAX);
2970 else
2971 msm_otg_notify_charger(motg,
2972 IDEV_CHG_MIN - motg->mA_port);
2973 } else if (!test_bit(ID, &motg->inputs)) {
2974 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2975 motg->chg_type = USB_INVALID_CHARGER;
2976 msm_otg_notify_charger(motg, 0);
2977 msm_hsusb_vbus_power(motg, 1);
2978 }
2979 break;
2980 case OTG_STATE_A_SUSPEND:
2981 if ((test_bit(ID, &motg->inputs) &&
2982 !test_bit(ID_A, &motg->inputs)) ||
2983 test_bit(A_BUS_DROP, &motg->inputs) ||
2984 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2985 pr_debug("id_a/b/c || a_bus_drop ||"
2986 "a_aidl_bdis_tmout\n");
2987 msm_otg_del_timer(motg);
2988 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002989 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302990 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002991 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302992 if (!test_bit(ID_A, &motg->inputs))
2993 msm_hsusb_vbus_power(motg, 0);
2994 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2995 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2996 pr_debug("!a_vbus_vld\n");
2997 msm_otg_del_timer(motg);
2998 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002999 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303000 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003001 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303002 } else if (!test_bit(B_CONN, &motg->inputs) &&
3003 otg->host->b_hnp_enable) {
3004 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003005 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303006 msm_otg_host_hnp_enable(otg, 1);
3007 otg->gadget->is_a_peripheral = 1;
3008 msm_otg_start_peripheral(otg, 1);
3009 } else if (!test_bit(B_CONN, &motg->inputs) &&
3010 !otg->host->b_hnp_enable) {
3011 pr_debug("!b_conn && !b_hnp_enable");
3012 /*
3013 * bus request is dropped during suspend.
3014 * acquire again for next device.
3015 */
3016 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003017 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303018 if (TA_WAIT_BCON > 0)
3019 msm_otg_start_timer(motg, TA_WAIT_BCON,
3020 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003021 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05303022 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003023 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303024 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003025 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003026 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05303027 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303028 }
3029 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303030 case OTG_STATE_A_PERIPHERAL:
3031 if ((test_bit(ID, &motg->inputs) &&
3032 !test_bit(ID_A, &motg->inputs)) ||
3033 test_bit(A_BUS_DROP, &motg->inputs)) {
3034 pr_debug("id _f/b/c || a_bus_drop\n");
3035 /* Clear BIDL_ADIS timer */
3036 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003037 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303038 msm_otg_start_peripheral(otg, 0);
3039 otg->gadget->is_a_peripheral = 0;
3040 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003041 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303042 if (!test_bit(ID_A, &motg->inputs))
3043 msm_hsusb_vbus_power(motg, 0);
3044 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3045 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
3046 pr_debug("!a_vbus_vld\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_VBUS_ERR;
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);
3053 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
3054 pr_debug("a_bidl_adis_tmout\n");
3055 msm_otg_start_peripheral(otg, 0);
3056 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003057 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303058 set_bit(A_BUS_REQ, &motg->inputs);
3059 msm_otg_host_hnp_enable(otg, 0);
3060 if (TA_WAIT_BCON > 0)
3061 msm_otg_start_timer(motg, TA_WAIT_BCON,
3062 A_WAIT_BCON);
3063 } else if (test_bit(ID_A, &motg->inputs)) {
3064 msm_hsusb_vbus_power(motg, 0);
3065 msm_otg_notify_charger(motg,
3066 IDEV_CHG_MIN - motg->mA_port);
3067 } else if (!test_bit(ID, &motg->inputs)) {
3068 msm_otg_notify_charger(motg, 0);
3069 msm_hsusb_vbus_power(motg, 1);
3070 }
3071 break;
3072 case OTG_STATE_A_WAIT_VFALL:
3073 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
3074 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003075 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303076 work = 1;
3077 }
3078 break;
3079 case OTG_STATE_A_VBUS_ERR:
3080 if ((test_bit(ID, &motg->inputs) &&
3081 !test_bit(ID_A, &motg->inputs)) ||
3082 test_bit(A_BUS_DROP, &motg->inputs) ||
3083 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003084 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303085 if (!test_bit(ID_A, &motg->inputs))
3086 msm_hsusb_vbus_power(motg, 0);
3087 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3088 motg->chg_state = USB_CHG_STATE_UNDEFINED;
3089 motg->chg_type = USB_INVALID_CHARGER;
3090 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303091 }
3092 break;
3093 default:
3094 break;
3095 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303096 if (work)
3097 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303098}
3099
Amit Blayd0fe07b2012-09-05 16:42:09 +03003100static void msm_otg_suspend_work(struct work_struct *w)
3101{
3102 struct msm_otg *motg =
3103 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05303104
3105 /* This work is only for device bus suspend */
3106 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
3107 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003108}
3109
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303110static irqreturn_t msm_otg_irq(int irq, void *data)
3111{
3112 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003113 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303114 u32 otgsc = 0, usbsts, pc;
3115 bool work = 0;
3116 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303117
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303118 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07003119 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303120 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07003121 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07003122 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07003123 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303124 return IRQ_HANDLED;
3125 }
3126
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003127 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303128 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303129
3130 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303131 return IRQ_NONE;
3132
3133 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303134 if (otgsc & OTGSC_ID) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003135 dev_dbg(otg->phy->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303136 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303137 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003138 dev_dbg(otg->phy->dev, "ID clear\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303139 /*
3140 * Assert a_bus_req to supply power on
3141 * VBUS when Micro/Mini-A cable is connected
3142 * with out user intervention.
3143 */
3144 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303145 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303146 msm_chg_enable_aca_det(motg);
3147 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303148 writel_relaxed(otgsc, USB_OTGSC);
3149 work = 1;
3150 } else if (otgsc & OTGSC_DPIS) {
3151 pr_debug("DPIS detected\n");
3152 writel_relaxed(otgsc, USB_OTGSC);
3153 set_bit(A_SRP_DET, &motg->inputs);
3154 set_bit(A_BUS_REQ, &motg->inputs);
3155 work = 1;
Vamsi Krishnaef6e1bf2013-03-02 15:36:17 -08003156 } else if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303157 writel_relaxed(otgsc, USB_OTGSC);
3158 /*
3159 * BSV interrupt comes when operating as an A-device
3160 * (VBUS on/off).
3161 * But, handle BSV when charger is removed from ACA in ID_A
3162 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003163 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303164 !test_bit(ID_A, &motg->inputs))
3165 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303166 if (otgsc & OTGSC_BSV) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003167 dev_dbg(otg->phy->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303168 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303169 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003170 dev_dbg(otg->phy->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303171 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003172 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3173
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303174 msm_chg_check_aca_intr(motg);
3175 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303176 work = 1;
3177 } else if (usbsts & STS_PCI) {
3178 pc = readl_relaxed(USB_PORTSC);
3179 pr_debug("portsc = %x\n", pc);
3180 ret = IRQ_NONE;
3181 /*
3182 * HCD Acks PCI interrupt. We use this to switch
3183 * between different OTG states.
3184 */
3185 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003186 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303187 case OTG_STATE_A_SUSPEND:
3188 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3189 !(pc & PORTSC_CCS)) {
3190 pr_debug("B_CONN clear\n");
3191 clear_bit(B_CONN, &motg->inputs);
3192 msm_otg_del_timer(motg);
3193 }
3194 break;
3195 case OTG_STATE_A_PERIPHERAL:
3196 /*
3197 * A-peripheral observed activity on bus.
3198 * clear A_BIDL_ADIS timer.
3199 */
3200 msm_otg_del_timer(motg);
3201 work = 0;
3202 break;
3203 case OTG_STATE_B_WAIT_ACON:
3204 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3205 pr_debug("A_CONN set\n");
3206 set_bit(A_CONN, &motg->inputs);
3207 /* Clear ASE0_BRST timer */
3208 msm_otg_del_timer(motg);
3209 }
3210 break;
3211 case OTG_STATE_B_HOST:
3212 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3213 pr_debug("A_CONN clear\n");
3214 clear_bit(A_CONN, &motg->inputs);
3215 msm_otg_del_timer(motg);
3216 }
3217 break;
3218 case OTG_STATE_A_WAIT_BCON:
3219 if (TA_WAIT_BCON < 0)
3220 set_bit(A_BUS_REQ, &motg->inputs);
3221 default:
3222 work = 0;
3223 break;
3224 }
3225 } else if (usbsts & STS_URI) {
3226 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003227 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303228 case OTG_STATE_A_PERIPHERAL:
3229 /*
3230 * A-peripheral observed activity on bus.
3231 * clear A_BIDL_ADIS timer.
3232 */
3233 msm_otg_del_timer(motg);
3234 work = 0;
3235 break;
3236 default:
3237 work = 0;
3238 break;
3239 }
3240 } else if (usbsts & STS_SLI) {
3241 ret = IRQ_NONE;
3242 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003243 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303244 case OTG_STATE_B_PERIPHERAL:
3245 if (otg->gadget->b_hnp_enable) {
3246 set_bit(A_BUS_SUSPEND, &motg->inputs);
3247 set_bit(B_BUS_REQ, &motg->inputs);
3248 work = 1;
3249 }
3250 break;
3251 case OTG_STATE_A_PERIPHERAL:
3252 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3253 A_BIDL_ADIS);
3254 break;
3255 default:
3256 break;
3257 }
3258 } else if ((usbsts & PHY_ALT_INT)) {
3259 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3260 if (msm_chg_check_aca_intr(motg))
3261 work = 1;
3262 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303263 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303264 if (work)
3265 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303266
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303267 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003268}
3269
3270static void msm_otg_set_vbus_state(int online)
3271{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303272 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003273 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303274
Vamsi Krishna945b4a92013-03-02 15:31:16 -08003275 if (online) {
3276 pr_debug("PMIC: BSV set\n");
3277 set_bit(B_SESS_VLD, &motg->inputs);
3278 } else {
3279 pr_debug("PMIC: BSV clear\n");
3280 clear_bit(B_SESS_VLD, &motg->inputs);
3281 }
3282
3283 /* do not queue state m/c work if id is grounded */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303284 if (!test_bit(ID, &motg->inputs)) {
3285 /*
3286 * state machine work waits for initial VBUS
3287 * completion in UNDEFINED state. Process
3288 * the initial VBUS event in ID_GND state.
3289 */
3290 if (init)
3291 return;
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303292 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003293
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303294 if (!init) {
3295 init = true;
3296 complete(&pmic_vbus_init);
3297 pr_debug("PMIC: BSV init complete\n");
3298 return;
3299 }
3300
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303301 if (test_bit(MHL, &motg->inputs) ||
3302 mhl_det_in_progress) {
3303 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3304 return;
3305 }
3306
Jack Pham5ca279b2012-05-14 18:42:54 -07003307 if (atomic_read(&motg->pm_suspended))
3308 motg->sm_work_pending = true;
3309 else
3310 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003311}
3312
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303313static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003314{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303315 struct msm_otg *motg = container_of(w, struct msm_otg,
3316 pmic_id_status_work.work);
3317 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003318
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303319 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303320 if (!test_and_set_bit(ID, &motg->inputs)) {
3321 pr_debug("PMIC: ID set\n");
3322 work = 1;
3323 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303324 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303325 if (test_and_clear_bit(ID, &motg->inputs)) {
3326 pr_debug("PMIC: ID clear\n");
3327 set_bit(A_BUS_REQ, &motg->inputs);
3328 work = 1;
3329 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303330 }
3331
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303332 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003333 if (atomic_read(&motg->pm_suspended))
3334 motg->sm_work_pending = true;
3335 else
3336 queue_work(system_nrt_wq, &motg->sm_work);
3337 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303338
3339}
3340
3341#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3342static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3343{
3344 struct msm_otg *motg = data;
3345
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303346 if (test_bit(MHL, &motg->inputs) ||
3347 mhl_det_in_progress) {
3348 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3349 return IRQ_HANDLED;
3350 }
3351
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303352 if (!aca_id_turned_on)
3353 /*schedule delayed work for 5msec for ID line state to settle*/
3354 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3355 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003356
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303357 return IRQ_HANDLED;
3358}
3359
3360static int msm_otg_mode_show(struct seq_file *s, void *unused)
3361{
3362 struct msm_otg *motg = s->private;
Stephen Boyd9850acb2013-01-28 14:11:20 -08003363 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303364
Stephen Boyd9850acb2013-01-28 14:11:20 -08003365 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303366 case OTG_STATE_A_HOST:
3367 seq_printf(s, "host\n");
3368 break;
3369 case OTG_STATE_B_PERIPHERAL:
3370 seq_printf(s, "peripheral\n");
3371 break;
3372 default:
3373 seq_printf(s, "none\n");
3374 break;
3375 }
3376
3377 return 0;
3378}
3379
3380static int msm_otg_mode_open(struct inode *inode, struct file *file)
3381{
3382 return single_open(file, msm_otg_mode_show, inode->i_private);
3383}
3384
3385static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3386 size_t count, loff_t *ppos)
3387{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303388 struct seq_file *s = file->private_data;
3389 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303390 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003391 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303392 int status = count;
3393 enum usb_mode_type req_mode;
3394
3395 memset(buf, 0x00, sizeof(buf));
3396
3397 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3398 status = -EFAULT;
3399 goto out;
3400 }
3401
3402 if (!strncmp(buf, "host", 4)) {
3403 req_mode = USB_HOST;
3404 } else if (!strncmp(buf, "peripheral", 10)) {
3405 req_mode = USB_PERIPHERAL;
3406 } else if (!strncmp(buf, "none", 4)) {
3407 req_mode = USB_NONE;
3408 } else {
3409 status = -EINVAL;
3410 goto out;
3411 }
3412
3413 switch (req_mode) {
3414 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003415 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303416 case OTG_STATE_A_HOST:
3417 case OTG_STATE_B_PERIPHERAL:
3418 set_bit(ID, &motg->inputs);
3419 clear_bit(B_SESS_VLD, &motg->inputs);
3420 break;
3421 default:
3422 goto out;
3423 }
3424 break;
3425 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003426 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303427 case OTG_STATE_B_IDLE:
3428 case OTG_STATE_A_HOST:
3429 set_bit(ID, &motg->inputs);
3430 set_bit(B_SESS_VLD, &motg->inputs);
3431 break;
3432 default:
3433 goto out;
3434 }
3435 break;
3436 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003437 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303438 case OTG_STATE_B_IDLE:
3439 case OTG_STATE_B_PERIPHERAL:
3440 clear_bit(ID, &motg->inputs);
3441 break;
3442 default:
3443 goto out;
3444 }
3445 break;
3446 default:
3447 goto out;
3448 }
3449
Steve Mucklef132c6c2012-06-06 18:30:57 -07003450 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303451 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303452out:
3453 return status;
3454}
3455
3456const struct file_operations msm_otg_mode_fops = {
3457 .open = msm_otg_mode_open,
3458 .read = seq_read,
3459 .write = msm_otg_mode_write,
3460 .llseek = seq_lseek,
3461 .release = single_release,
3462};
3463
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303464static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3465{
3466 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003467 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303468
Steve Mucklef132c6c2012-06-06 18:30:57 -07003469 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303470 return 0;
3471}
3472
3473static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3474{
3475 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3476}
3477
3478const struct file_operations msm_otg_state_fops = {
3479 .open = msm_otg_otg_state_open,
3480 .read = seq_read,
3481 .llseek = seq_lseek,
3482 .release = single_release,
3483};
3484
Anji jonnalad270e2d2011-08-09 11:28:32 +05303485static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3486{
3487 struct msm_otg *motg = s->private;
3488
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303489 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303490 return 0;
3491}
3492
3493static int msm_otg_chg_open(struct inode *inode, struct file *file)
3494{
3495 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3496}
3497
3498const struct file_operations msm_otg_chg_fops = {
3499 .open = msm_otg_chg_open,
3500 .read = seq_read,
3501 .llseek = seq_lseek,
3502 .release = single_release,
3503};
3504
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303505static int msm_otg_aca_show(struct seq_file *s, void *unused)
3506{
3507 if (debug_aca_enabled)
3508 seq_printf(s, "enabled\n");
3509 else
3510 seq_printf(s, "disabled\n");
3511
3512 return 0;
3513}
3514
3515static int msm_otg_aca_open(struct inode *inode, struct file *file)
3516{
3517 return single_open(file, msm_otg_aca_show, inode->i_private);
3518}
3519
3520static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3521 size_t count, loff_t *ppos)
3522{
3523 char buf[8];
3524
3525 memset(buf, 0x00, sizeof(buf));
3526
3527 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3528 return -EFAULT;
3529
3530 if (!strncmp(buf, "enable", 6))
3531 debug_aca_enabled = true;
3532 else
3533 debug_aca_enabled = false;
3534
3535 return count;
3536}
3537
3538const struct file_operations msm_otg_aca_fops = {
3539 .open = msm_otg_aca_open,
3540 .read = seq_read,
3541 .write = msm_otg_aca_write,
3542 .llseek = seq_lseek,
3543 .release = single_release,
3544};
3545
Manu Gautam8bdcc592012-03-06 11:26:06 +05303546static int msm_otg_bus_show(struct seq_file *s, void *unused)
3547{
3548 if (debug_bus_voting_enabled)
3549 seq_printf(s, "enabled\n");
3550 else
3551 seq_printf(s, "disabled\n");
3552
3553 return 0;
3554}
3555
3556static int msm_otg_bus_open(struct inode *inode, struct file *file)
3557{
3558 return single_open(file, msm_otg_bus_show, inode->i_private);
3559}
3560
3561static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3562 size_t count, loff_t *ppos)
3563{
3564 char buf[8];
Manu Gautam8bdcc592012-03-06 11:26:06 +05303565 struct seq_file *s = file->private_data;
3566 struct msm_otg *motg = s->private;
3567
3568 memset(buf, 0x00, sizeof(buf));
3569
3570 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3571 return -EFAULT;
3572
3573 if (!strncmp(buf, "enable", 6)) {
3574 /* Do not vote here. Let OTG statemachine decide when to vote */
3575 debug_bus_voting_enabled = true;
3576 } else {
3577 debug_bus_voting_enabled = false;
Manu Gautame3a39082013-06-11 10:42:56 +05303578 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303579 }
3580
3581 return count;
3582}
3583
David Keitel272ce522012-08-17 16:25:24 -07003584static int otg_power_get_property_usb(struct power_supply *psy,
3585 enum power_supply_property psp,
3586 union power_supply_propval *val)
3587{
Jack Pham0c695282012-10-19 18:13:03 -07003588 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003589 switch (psp) {
3590 case POWER_SUPPLY_PROP_SCOPE:
3591 if (motg->host_mode)
3592 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3593 else
3594 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3595 break;
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303596 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
3597 val->intval = motg->voltage_max;
3598 break;
David Keitel272ce522012-08-17 16:25:24 -07003599 case POWER_SUPPLY_PROP_CURRENT_MAX:
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303600 val->intval = motg->current_max;
David Keitel272ce522012-08-17 16:25:24 -07003601 break;
3602 /* Reflect USB enumeration */
3603 case POWER_SUPPLY_PROP_PRESENT:
3604 case POWER_SUPPLY_PROP_ONLINE:
3605 val->intval = motg->online;
3606 break;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303607 case POWER_SUPPLY_PROP_TYPE:
3608 val->intval = psy->type;
3609 break;
Wu Fenglin04ae6de2013-09-09 19:15:06 +08003610 case POWER_SUPPLY_PROP_HEALTH:
3611 val->intval = motg->usbin_health;
3612 break;
David Keitel272ce522012-08-17 16:25:24 -07003613 default:
3614 return -EINVAL;
3615 }
3616 return 0;
3617}
3618
3619static int otg_power_set_property_usb(struct power_supply *psy,
3620 enum power_supply_property psp,
3621 const union power_supply_propval *val)
3622{
Jack Pham0c695282012-10-19 18:13:03 -07003623 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003624
3625 switch (psp) {
3626 /* Process PMIC notification in PRESENT prop */
3627 case POWER_SUPPLY_PROP_PRESENT:
3628 msm_otg_set_vbus_state(val->intval);
3629 break;
3630 /* The ONLINE property reflects if usb has enumerated */
3631 case POWER_SUPPLY_PROP_ONLINE:
3632 motg->online = val->intval;
3633 break;
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303634 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
3635 motg->voltage_max = val->intval;
3636 break;
David Keitel272ce522012-08-17 16:25:24 -07003637 case POWER_SUPPLY_PROP_CURRENT_MAX:
3638 motg->current_max = val->intval;
3639 break;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303640 case POWER_SUPPLY_PROP_TYPE:
3641 psy->type = val->intval;
3642 break;
Wu Fenglin04ae6de2013-09-09 19:15:06 +08003643 case POWER_SUPPLY_PROP_HEALTH:
3644 motg->usbin_health = val->intval;
3645 break;
David Keitel272ce522012-08-17 16:25:24 -07003646 default:
3647 return -EINVAL;
3648 }
3649
3650 power_supply_changed(&motg->usb_psy);
3651 return 0;
3652}
3653
David Collinsd79acc52012-11-26 14:59:00 -08003654static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3655 enum power_supply_property psp)
3656{
3657 switch (psp) {
Wu Fenglin04ae6de2013-09-09 19:15:06 +08003658 case POWER_SUPPLY_PROP_HEALTH:
David Collinsd79acc52012-11-26 14:59:00 -08003659 case POWER_SUPPLY_PROP_PRESENT:
3660 case POWER_SUPPLY_PROP_ONLINE:
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303661 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
David Collinsd79acc52012-11-26 14:59:00 -08003662 case POWER_SUPPLY_PROP_CURRENT_MAX:
3663 return 1;
3664 default:
3665 break;
3666 }
3667
3668 return 0;
3669}
3670
David Keitel272ce522012-08-17 16:25:24 -07003671static char *otg_pm_power_supplied_to[] = {
3672 "battery",
3673};
3674
3675static enum power_supply_property otg_pm_power_props_usb[] = {
Wu Fenglin04ae6de2013-09-09 19:15:06 +08003676 POWER_SUPPLY_PROP_HEALTH,
David Keitel272ce522012-08-17 16:25:24 -07003677 POWER_SUPPLY_PROP_PRESENT,
3678 POWER_SUPPLY_PROP_ONLINE,
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303679 POWER_SUPPLY_PROP_VOLTAGE_MAX,
David Keitel272ce522012-08-17 16:25:24 -07003680 POWER_SUPPLY_PROP_CURRENT_MAX,
3681 POWER_SUPPLY_PROP_SCOPE,
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303682 POWER_SUPPLY_PROP_TYPE,
David Keitel272ce522012-08-17 16:25:24 -07003683};
3684
Manu Gautam8bdcc592012-03-06 11:26:06 +05303685const struct file_operations msm_otg_bus_fops = {
3686 .open = msm_otg_bus_open,
3687 .read = seq_read,
3688 .write = msm_otg_bus_write,
3689 .llseek = seq_lseek,
3690 .release = single_release,
3691};
3692
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303693static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303694
3695static int msm_otg_debugfs_init(struct msm_otg *motg)
3696{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303697 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303698
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303699 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3700
3701 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3702 return -ENODEV;
3703
Anji jonnalad270e2d2011-08-09 11:28:32 +05303704 if (motg->pdata->mode == USB_OTG &&
3705 motg->pdata->otg_control == OTG_USER_CONTROL) {
3706
Manu Gautam8bdcc592012-03-06 11:26:06 +05303707 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303708 S_IWUSR, msm_otg_dbg_root, motg,
3709 &msm_otg_mode_fops);
3710
Manu Gautam8bdcc592012-03-06 11:26:06 +05303711 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303712 debugfs_remove(msm_otg_dbg_root);
3713 msm_otg_dbg_root = NULL;
3714 return -ENODEV;
3715 }
3716 }
3717
Manu Gautam8bdcc592012-03-06 11:26:06 +05303718 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303719 msm_otg_dbg_root, motg,
3720 &msm_otg_chg_fops);
3721
Manu Gautam8bdcc592012-03-06 11:26:06 +05303722 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303723 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303724 return -ENODEV;
3725 }
3726
Manu Gautam8bdcc592012-03-06 11:26:06 +05303727 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303728 msm_otg_dbg_root, motg,
3729 &msm_otg_aca_fops);
3730
Manu Gautam8bdcc592012-03-06 11:26:06 +05303731 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303732 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303733 return -ENODEV;
3734 }
3735
Manu Gautam8bdcc592012-03-06 11:26:06 +05303736 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3737 msm_otg_dbg_root, motg,
3738 &msm_otg_bus_fops);
3739
3740 if (!msm_otg_dentry) {
3741 debugfs_remove_recursive(msm_otg_dbg_root);
3742 return -ENODEV;
3743 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303744
3745 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3746 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3747
3748 if (!msm_otg_dentry) {
3749 debugfs_remove_recursive(msm_otg_dbg_root);
3750 return -ENODEV;
3751 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303752 return 0;
3753}
3754
3755static void msm_otg_debugfs_cleanup(void)
3756{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303757 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303758}
3759
Manu Gautam0ddbd922012-09-21 17:17:38 +05303760#define MSM_OTG_CMD_ID 0x09
3761#define MSM_OTG_DEVICE_ID 0x04
3762#define MSM_OTG_VMID_IDX 0xFF
3763#define MSM_OTG_MEM_TYPE 0x02
3764struct msm_otg_scm_cmd_buf {
3765 unsigned int device_id;
3766 unsigned int vmid_idx;
3767 unsigned int mem_type;
3768} __attribute__ ((__packed__));
3769
3770static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3771{
3772 int ret;
3773 struct msm_otg_platform_data *pdata = motg->pdata;
3774 struct msm_otg_scm_cmd_buf cmd_buf;
3775
3776 if (!pdata->pnoc_errata_fix)
3777 return;
3778
3779 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3780
3781 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3782 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3783 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3784
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -07003785 ret = scm_call(SCM_SVC_MP, MSM_OTG_CMD_ID, &cmd_buf,
Manu Gautam0ddbd922012-09-21 17:17:38 +05303786 sizeof(cmd_buf), NULL, 0);
3787
3788 if (ret)
3789 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3790}
3791
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303792static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3793static struct platform_device *msm_otg_add_pdev(
3794 struct platform_device *ofdev, const char *name)
3795{
3796 struct platform_device *pdev;
3797 const struct resource *res = ofdev->resource;
3798 unsigned int num = ofdev->num_resources;
3799 int retval;
Amit Blayb4f14532013-06-17 22:33:57 +03003800 struct ci13xxx_platform_data ci_pdata;
3801 struct msm_otg_platform_data *otg_pdata;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303802
3803 pdev = platform_device_alloc(name, -1);
3804 if (!pdev) {
3805 retval = -ENOMEM;
3806 goto error;
3807 }
3808
3809 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3810 pdev->dev.dma_mask = &msm_otg_dma_mask;
3811
3812 if (num) {
3813 retval = platform_device_add_resources(pdev, res, num);
3814 if (retval)
3815 goto error;
3816 }
3817
Amit Blayb4f14532013-06-17 22:33:57 +03003818 if (!strcmp(name, "msm_hsusb")) {
3819 otg_pdata =
3820 (struct msm_otg_platform_data *)
3821 ofdev->dev.platform_data;
3822 ci_pdata.log2_itc = otg_pdata->log2_itc;
3823 ci_pdata.usb_core_id = 0;
Shimrit Malichi5a2d5b52013-06-20 19:04:28 +03003824 ci_pdata.l1_supported = otg_pdata->l1_supported;
Amit Blayb4f14532013-06-17 22:33:57 +03003825 retval = platform_device_add_data(pdev, &ci_pdata,
3826 sizeof(ci_pdata));
3827 if (retval)
3828 goto error;
3829 }
3830
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303831 retval = platform_device_add(pdev);
3832 if (retval)
3833 goto error;
3834
3835 return pdev;
3836
3837error:
3838 platform_device_put(pdev);
3839 return ERR_PTR(retval);
3840}
3841
3842static int msm_otg_setup_devices(struct platform_device *ofdev,
3843 enum usb_mode_type mode, bool init)
3844{
3845 const char *gadget_name = "msm_hsusb";
3846 const char *host_name = "msm_hsusb_host";
3847 static struct platform_device *gadget_pdev;
3848 static struct platform_device *host_pdev;
3849 int retval = 0;
3850
3851 if (!init) {
3852 if (gadget_pdev)
3853 platform_device_unregister(gadget_pdev);
3854 if (host_pdev)
3855 platform_device_unregister(host_pdev);
3856 return 0;
3857 }
3858
3859 switch (mode) {
3860 case USB_OTG:
3861 /* fall through */
3862 case USB_PERIPHERAL:
3863 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3864 if (IS_ERR(gadget_pdev)) {
3865 retval = PTR_ERR(gadget_pdev);
3866 break;
3867 }
3868 if (mode == USB_PERIPHERAL)
3869 break;
3870 /* fall through */
3871 case USB_HOST:
3872 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3873 if (IS_ERR(host_pdev)) {
3874 retval = PTR_ERR(host_pdev);
3875 if (mode == USB_OTG)
3876 platform_device_unregister(gadget_pdev);
3877 }
3878 break;
3879 default:
3880 break;
3881 }
3882
3883 return retval;
3884}
3885
David Keitel272ce522012-08-17 16:25:24 -07003886static int msm_otg_register_power_supply(struct platform_device *pdev,
3887 struct msm_otg *motg)
3888{
3889 int ret;
3890
3891 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3892 if (ret < 0) {
3893 dev_err(motg->phy.dev,
3894 "%s:power_supply_register usb failed\n",
3895 __func__);
3896 return ret;
3897 }
3898
3899 legacy_power_supply = false;
3900 return 0;
3901}
3902
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303903static int msm_otg_ext_chg_open(struct inode *inode, struct file *file)
3904{
3905 struct msm_otg *motg = the_msm_otg;
3906
3907 pr_debug("msm_otg ext chg open\n");
3908
3909 motg->ext_chg_opened = true;
3910 file->private_data = (void *)motg;
3911 return 0;
3912}
3913
3914static long
3915msm_otg_ext_chg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3916{
3917 struct msm_otg *motg = file->private_data;
3918 struct msm_usb_chg_info info = {0};
3919 int ret = 0, val;
3920
3921 switch (cmd) {
3922 case MSM_USB_EXT_CHG_INFO:
3923 info.chg_block_type = USB_CHG_BLOCK_ULPI;
3924 info.page_offset = motg->io_res->start & ~PAGE_MASK;
3925 /* mmap() works on PAGE granularity */
3926 info.length = PAGE_SIZE;
3927
3928 if (copy_to_user((void __user *)arg, &info, sizeof(info))) {
3929 pr_err("%s: copy to user failed\n\n", __func__);
3930 ret = -EFAULT;
3931 }
3932 break;
3933 case MSM_USB_EXT_CHG_BLOCK_LPM:
3934 if (get_user(val, (int __user *)arg)) {
3935 pr_err("%s: get_user failed\n\n", __func__);
3936 ret = -EFAULT;
3937 break;
3938 }
3939 pr_debug("%s: LPM block request %d\n", __func__, val);
3940 if (val) { /* block LPM */
3941 if (motg->chg_type == USB_DCP_CHARGER) {
3942 /*
3943 * If device is already suspended, resume it.
3944 * The PM usage counter is incremented in
3945 * runtime resume method. if device is not
3946 * suspended, cancel the scheduled suspend
3947 * and increment the PM usage counter.
3948 */
3949 if (pm_runtime_suspended(motg->phy.dev))
3950 pm_runtime_resume(motg->phy.dev);
3951 else
3952 pm_runtime_get_sync(motg->phy.dev);
3953 } else {
3954 motg->ext_chg_active = false;
3955 complete(&motg->ext_chg_wait);
3956 ret = -ENODEV;
3957 }
3958 } else {
3959 motg->ext_chg_active = false;
3960 complete(&motg->ext_chg_wait);
3961 pm_runtime_put(motg->phy.dev);
3962 }
3963 break;
3964 default:
3965 ret = -EINVAL;
3966 }
3967
3968 return ret;
3969}
3970
3971static int msm_otg_ext_chg_mmap(struct file *file, struct vm_area_struct *vma)
3972{
3973 struct msm_otg *motg = file->private_data;
3974 unsigned long vsize = vma->vm_end - vma->vm_start;
3975 int ret;
3976
3977 if (vma->vm_pgoff || vsize > PAGE_SIZE)
3978 return -EINVAL;
3979
3980 vma->vm_pgoff = __phys_to_pfn(motg->io_res->start);
3981 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
3982
3983 ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
3984 vsize, vma->vm_page_prot);
3985 if (ret < 0) {
3986 pr_err("%s: failed with return val %d\n", __func__, ret);
3987 return ret;
3988 }
3989
3990 return 0;
3991}
3992
3993static int msm_otg_ext_chg_release(struct inode *inode, struct file *file)
3994{
3995 struct msm_otg *motg = file->private_data;
3996
3997 pr_debug("msm_otg ext chg release\n");
3998
3999 motg->ext_chg_opened = false;
4000
4001 return 0;
4002}
4003
4004static const struct file_operations msm_otg_ext_chg_fops = {
4005 .owner = THIS_MODULE,
4006 .open = msm_otg_ext_chg_open,
4007 .unlocked_ioctl = msm_otg_ext_chg_ioctl,
4008 .mmap = msm_otg_ext_chg_mmap,
4009 .release = msm_otg_ext_chg_release,
4010};
4011
4012static int msm_otg_setup_ext_chg_cdev(struct msm_otg *motg)
4013{
4014 int ret;
4015
4016 if (motg->pdata->enable_sec_phy || motg->pdata->mode == USB_HOST ||
4017 motg->pdata->otg_control != OTG_PMIC_CONTROL ||
4018 psy != &motg->usb_psy) {
4019 pr_debug("usb ext chg is not supported by msm otg\n");
4020 return -ENODEV;
4021 }
4022
4023 ret = alloc_chrdev_region(&motg->ext_chg_dev, 0, 1, "usb_ext_chg");
4024 if (ret < 0) {
4025 pr_err("Fail to allocate usb ext char dev region\n");
4026 return ret;
4027 }
4028 motg->ext_chg_class = class_create(THIS_MODULE, "msm_ext_chg");
4029 if (ret < 0) {
4030 pr_err("Fail to create usb ext chg class\n");
4031 goto unreg_chrdev;
4032 }
4033 cdev_init(&motg->ext_chg_cdev, &msm_otg_ext_chg_fops);
4034 motg->ext_chg_cdev.owner = THIS_MODULE;
4035
4036 ret = cdev_add(&motg->ext_chg_cdev, motg->ext_chg_dev, 1);
4037 if (ret < 0) {
4038 pr_err("Fail to add usb ext chg cdev\n");
4039 goto destroy_class;
4040 }
4041 motg->ext_chg_device = device_create(motg->ext_chg_class,
4042 NULL, motg->ext_chg_dev, NULL,
4043 "usb_ext_chg");
4044 if (IS_ERR(motg->ext_chg_device)) {
4045 pr_err("Fail to create usb ext chg device\n");
4046 ret = PTR_ERR(motg->ext_chg_device);
4047 motg->ext_chg_device = NULL;
4048 goto del_cdev;
4049 }
4050
4051 init_completion(&motg->ext_chg_wait);
4052 pr_debug("msm otg ext chg cdev setup success\n");
4053 return 0;
4054
4055del_cdev:
4056 cdev_del(&motg->ext_chg_cdev);
4057destroy_class:
4058 class_destroy(motg->ext_chg_class);
4059unreg_chrdev:
4060 unregister_chrdev_region(motg->ext_chg_dev, 1);
4061
4062 return ret;
4063}
4064
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304065static ssize_t dpdm_pulldown_enable_show(struct device *dev,
4066 struct device_attribute *attr, char *buf)
4067{
4068 struct msm_otg *motg = the_msm_otg;
4069 struct msm_otg_platform_data *pdata = motg->pdata;
4070
4071 return snprintf(buf, PAGE_SIZE, "%s\n", pdata->dpdm_pulldown_added ?
4072 "enabled" : "disabled");
4073}
4074
4075static ssize_t dpdm_pulldown_enable_store(struct device *dev,
4076 struct device_attribute *attr, const char
4077 *buf, size_t size)
4078{
4079 struct msm_otg *motg = the_msm_otg;
4080 struct msm_otg_platform_data *pdata = motg->pdata;
4081
4082 if (!strnicmp(buf, "enable", 6)) {
4083 pdata->dpdm_pulldown_added = true;
4084 return size;
4085 } else if (!strnicmp(buf, "disable", 7)) {
4086 pdata->dpdm_pulldown_added = false;
4087 return size;
4088 }
4089
4090 return -EINVAL;
4091}
4092
4093static DEVICE_ATTR(dpdm_pulldown_enable, S_IRUGO | S_IWUSR,
4094 dpdm_pulldown_enable_show, dpdm_pulldown_enable_store);
4095
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304096struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
4097{
4098 struct device_node *node = pdev->dev.of_node;
4099 struct msm_otg_platform_data *pdata;
4100 int len = 0;
4101
4102 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
4103 if (!pdata) {
4104 pr_err("unable to allocate platform data\n");
4105 return NULL;
4106 }
4107 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
4108 if (len) {
4109 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
4110 if (!pdata->phy_init_seq)
4111 return NULL;
4112 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
4113 pdata->phy_init_seq,
4114 len/sizeof(*pdata->phy_init_seq));
4115 }
4116 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
4117 &pdata->power_budget);
4118 of_property_read_u32(node, "qcom,hsusb-otg-mode",
4119 &pdata->mode);
4120 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
4121 &pdata->otg_control);
4122 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
4123 &pdata->default_mode);
4124 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
4125 &pdata->phy_type);
Manu Gautambd53fba2012-07-31 16:13:06 +05304126 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
4127 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05304128 pdata->pnoc_errata_fix = of_property_read_bool(node,
4129 "qcom,hsusb-otg-pnoc-errata-fix");
Ido Shayevitza7114b92013-01-13 13:34:47 +02004130 pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
4131 "qcom,hsusb-otg-lpm-on-dev-suspend");
Ido Shayevitz26193352013-01-21 23:16:54 +02004132 pdata->core_clk_always_on_workaround = of_property_read_bool(node,
4133 "qcom,hsusb-otg-clk-always-on-workaround");
Shimrit Malichiffab5b02013-03-10 11:06:16 +02004134 pdata->delay_lpm_on_disconnect = of_property_read_bool(node,
4135 "qcom,hsusb-otg-delay-lpm");
Lena Salmanabde35d2013-04-25 15:29:43 +03004136 pdata->delay_lpm_hndshk_on_disconnect = of_property_read_bool(node,
4137 "qcom,hsusb-otg-delay-lpm-hndshk-on-disconnect");
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004138 pdata->dp_manual_pullup = of_property_read_bool(node,
4139 "qcom,dp-manual-pullup");
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304140 pdata->enable_sec_phy = of_property_read_bool(node,
4141 "qcom,usb2-enable-hsphy2");
Amit Blayb4f14532013-06-17 22:33:57 +03004142 of_property_read_u32(node, "qcom,hsusb-log2-itc",
4143 &pdata->log2_itc);
Manu Gautambd53fba2012-07-31 16:13:06 +05304144
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304145 of_property_read_u32(node, "qcom,hsusb-otg-mpm-dpsehv-int",
4146 &pdata->mpm_dpshv_int);
4147 of_property_read_u32(node, "qcom,hsusb-otg-mpm-dmsehv-int",
4148 &pdata->mpm_dmshv_int);
Manu Gautam0e0c53f2013-04-04 10:55:20 +05304149 pdata->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
4150 if (pdata->pmic_id_irq < 0)
4151 pdata->pmic_id_irq = 0;
4152
Shimrit Malichi5a2d5b52013-06-20 19:04:28 +03004153 pdata->l1_supported = of_property_read_bool(node,
4154 "qcom,hsusb-l1-supported");
4155
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304156 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304157}
4158
4159static int __init msm_otg_probe(struct platform_device *pdev)
4160{
Manu Gautamf8c45642012-08-10 10:20:56 -07004161 int ret = 0;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304162 int len = 0;
4163 u32 tmp[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304164 struct resource *res;
4165 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004166 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304167 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304168
4169 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304170
4171 if (pdev->dev.of_node) {
4172 dev_dbg(&pdev->dev, "device tree enabled\n");
4173 pdata = msm_otg_dt_to_pdata(pdev);
4174 if (!pdata)
4175 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07004176
4177 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
4178 if (!pdata->bus_scale_table)
4179 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
4180
Amit Blayb4f14532013-06-17 22:33:57 +03004181 pdev->dev.platform_data = pdata;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304182 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
4183 if (ret) {
4184 dev_err(&pdev->dev, "devices setup failed\n");
4185 return ret;
4186 }
4187 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304188 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
4189 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304190 } else {
4191 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304192 }
4193
Manu Gautame3a39082013-06-11 10:42:56 +05304194 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304195 if (!motg) {
4196 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
4197 return -ENOMEM;
4198 }
4199
Manu Gautame3a39082013-06-11 10:42:56 +05304200 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
4201 GFP_KERNEL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004202 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004203 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
Manu Gautame3a39082013-06-11 10:42:56 +05304204 return -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304205 }
4206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004207 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304208 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004209 phy = &motg->phy;
4210 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05304211
Manu Gautame3a39082013-06-11 10:42:56 +05304212 if (motg->pdata->bus_scale_table) {
4213 motg->bus_perf_client =
4214 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4215 if (!motg->bus_perf_client) {
4216 dev_err(motg->phy.dev, "%s: Failed to register BUS\n"
4217 "scaling client!!\n", __func__);
4218 } else {
4219 debug_bus_voting_enabled = true;
4220 /* Some platforms require BUS vote to control clocks */
4221 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
4222 }
4223 }
4224
Anji jonnala0f73cac2011-05-04 10:19:46 +05304225 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304226 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
4227 * PHY treat ACA ID_GND as float and no interrupt is generated. But
4228 * PMIC can detect ACA ID_GND and generate an interrupt.
4229 */
4230 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
4231 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
4232 ret = -EINVAL;
Manu Gautame3a39082013-06-11 10:42:56 +05304233 goto devote_bus_bw;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304234 }
4235
Ofir Cohen4da266f2012-01-03 10:19:29 +02004236 /* initialize reset counter */
4237 motg->reset_counter = 0;
4238
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304239 /*
4240 * Targets on which link uses asynchronous reset methodology,
4241 * free running clock is not required during the reset.
4242 */
Manu Gautam5143b252012-01-05 19:25:23 -08004243 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304244 if (IS_ERR(motg->clk))
4245 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
4246 else
4247 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05304248
4249 /*
Manu Gautam5143b252012-01-05 19:25:23 -08004250 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05304251 * CORE CLK must be running at >55Mhz for correct HSUSB
4252 * operation and USB core cannot tolerate frequency changes on
4253 * CORE CLK. For such USB cores, vote for maximum clk frequency
4254 * on pclk source
4255 */
Manu Gautam5143b252012-01-05 19:25:23 -08004256 motg->core_clk = clk_get(&pdev->dev, "core_clk");
4257 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304258 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08004259 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05304260 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004261 goto put_clk;
4262 }
Mayank Rana3eaf28d2013-03-27 14:04:04 +05304263
4264 /*
4265 * Get Max supported clk frequency for USB Core CLK and request
4266 * to set the same.
4267 */
4268 motg->core_clk_rate = clk_round_rate(motg->core_clk, LONG_MAX);
4269 if (IS_ERR_VALUE(motg->core_clk_rate)) {
4270 dev_err(&pdev->dev, "fail to get core clk max freq.\n");
4271 } else {
4272 ret = clk_set_rate(motg->core_clk, motg->core_clk_rate);
4273 if (ret)
4274 dev_err(&pdev->dev, "fail to set core_clk freq:%d\n",
4275 ret);
4276 }
Manu Gautam5143b252012-01-05 19:25:23 -08004277
4278 motg->pclk = clk_get(&pdev->dev, "iface_clk");
4279 if (IS_ERR(motg->pclk)) {
4280 dev_err(&pdev->dev, "failed to get iface_clk\n");
4281 ret = PTR_ERR(motg->pclk);
4282 goto put_core_clk;
4283 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304284
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304285 /*
4286 * On few platforms USB PHY is fed with sleep clk.
4287 * Hence don't fail probe.
4288 */
4289 motg->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
4290 if (IS_ERR(motg->sleep_clk)) {
4291 dev_dbg(&pdev->dev, "failed to get sleep_clk\n");
4292 } else {
4293 ret = clk_prepare_enable(motg->sleep_clk);
4294 if (ret) {
4295 dev_err(&pdev->dev, "%s failed to vote sleep_clk%d\n",
4296 __func__, ret);
4297 goto put_pclk;
4298 }
4299 }
4300
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304301 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4302 if (!res) {
4303 dev_err(&pdev->dev, "failed to get platform resource mem\n");
4304 ret = -ENODEV;
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304305 goto disable_sleep_clk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304306 }
4307
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304308 motg->io_res = res;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304309 motg->regs = ioremap(res->start, resource_size(res));
4310 if (!motg->regs) {
4311 dev_err(&pdev->dev, "ioremap failed\n");
4312 ret = -ENOMEM;
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304313 goto disable_sleep_clk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304314 }
4315 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
4316
4317 motg->irq = platform_get_irq(pdev, 0);
4318 if (!motg->irq) {
4319 dev_err(&pdev->dev, "platform_get_irq failed\n");
4320 ret = -ENODEV;
4321 goto free_regs;
4322 }
4323
Manu Gautamf8c45642012-08-10 10:20:56 -07004324 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
4325 if (motg->async_irq < 0) {
4326 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
4327 motg->async_irq = 0;
4328 }
4329
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304330 motg->xo_clk = clk_get(&pdev->dev, "xo");
4331 if (IS_ERR(motg->xo_clk)) {
4332 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
4333 if (IS_ERR(motg->xo_handle)) {
4334 dev_err(&pdev->dev, "%s fail to get handle for TCXO\n",
4335 __func__);
4336 ret = PTR_ERR(motg->xo_handle);
4337 goto free_regs;
4338 } else {
4339 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
4340 if (ret) {
4341 dev_err(&pdev->dev, "%s XO voting failed %d\n",
4342 __func__, ret);
4343 goto free_xo_handle;
4344 }
4345 }
4346 } else {
4347 ret = clk_prepare_enable(motg->xo_clk);
4348 if (ret) {
4349 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
4350 __func__, ret);
4351 goto free_xo_handle;
4352 }
Anji jonnala7da3f262011-12-02 17:22:14 -08004353 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05304354
Anji jonnala7da3f262011-12-02 17:22:14 -08004355
Manu Gautam28b1bac2012-01-30 16:43:06 +05304356 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304357
Mayank Rana248698c2012-04-19 00:03:16 +05304358 motg->vdd_type = VDDCX_CORNER;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304359 hsusb_vdd = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
4360 if (IS_ERR(hsusb_vdd)) {
4361 hsusb_vdd = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
4362 if (IS_ERR(hsusb_vdd)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004363 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304364 ret = PTR_ERR(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304365 goto devote_xo_handle;
4366 }
4367 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304368 }
4369
Mayank Rana0f286cf2013-02-27 11:43:27 +05304370 if (pdev->dev.of_node) {
4371 of_get_property(pdev->dev.of_node,
4372 "qcom,vdd-voltage-level",
4373 &len);
4374 if (len == sizeof(tmp)) {
4375 of_property_read_u32_array(pdev->dev.of_node,
4376 "qcom,vdd-voltage-level",
4377 tmp, len/sizeof(*tmp));
4378 vdd_val[motg->vdd_type][0] = tmp[0];
4379 vdd_val[motg->vdd_type][1] = tmp[1];
4380 vdd_val[motg->vdd_type][2] = tmp[2];
4381 } else {
4382 dev_dbg(&pdev->dev, "Using default hsusb vdd config.\n");
4383 }
4384 }
4385
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004386 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304387 if (ret) {
4388 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05304389 goto devote_xo_handle;
4390 }
4391
Mayank Rana0f286cf2013-02-27 11:43:27 +05304392 ret = regulator_enable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304393 if (ret) {
4394 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
4395 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304396 }
4397
4398 ret = msm_hsusb_ldo_init(motg, 1);
4399 if (ret) {
4400 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304401 goto free_hsusb_vdd;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304402 }
4403
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304404 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05304405 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
4406 "mhl_usb_hs_switch");
4407 if (IS_ERR(mhl_usb_hs_switch)) {
4408 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07004409 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304410 goto free_ldo_init;
4411 }
4412 }
4413
Amit Blay81801aa2012-09-19 12:08:12 +02004414 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304415 if (ret) {
4416 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004417 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304418 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05304419 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304420
Manu Gautam0ddbd922012-09-21 17:17:38 +05304421 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
4422 msm_otg_pnoc_errata_fix(motg);
4423
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304424 writel(0, USB_USBINTR);
4425 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004426 /* Ensure that above STOREs are completed before enabling interrupts */
4427 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304428
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304429 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
4430 if (ret)
4431 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004432 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05304433 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304434 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304435 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304436 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004437 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304438 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
4439 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05304440 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
4441 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304442 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
4443 "msm_otg", motg);
4444 if (ret) {
4445 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004446 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304447 }
4448
Manu Gautamf8c45642012-08-10 10:20:56 -07004449 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304450 ret = request_irq(motg->async_irq, msm_otg_irq,
4451 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004452 if (ret) {
4453 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4454 goto free_irq;
4455 }
4456 disable_irq(motg->async_irq);
4457 }
4458
Jack Pham87f202f2012-08-06 00:24:22 -07004459 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4460 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4461
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304462 if (pdata->mpm_dpshv_int)
4463 msm_mpm_enable_pin(pdata->mpm_dpshv_int, 1);
4464 if (pdata->mpm_dmshv_int)
4465 msm_mpm_enable_pin(pdata->mpm_dmshv_int, 1);
4466
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004467 phy->init = msm_otg_reset;
4468 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004469 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304470
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004471 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304472
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004473 phy->otg->phy = &motg->phy;
4474 phy->otg->set_host = msm_otg_set_host;
4475 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004476 phy->otg->start_hnp = msm_otg_start_hnp;
4477 phy->otg->start_srp = msm_otg_start_srp;
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004478 if (pdata->dp_manual_pullup)
4479 phy->flags |= ENABLE_DP_MANUAL_PULLUP;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004480
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304481 if (pdata->enable_sec_phy)
4482 phy->flags |= ENABLE_SECONDARY_PHY;
4483
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004484 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304485 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004486 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004487 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304488 }
4489
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304490 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304491 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004492 if (motg->pdata->pmic_id_irq) {
4493 ret = request_irq(motg->pdata->pmic_id_irq,
4494 msm_pmic_id_irq,
4495 IRQF_TRIGGER_RISING |
4496 IRQF_TRIGGER_FALLING,
4497 "msm_otg", motg);
4498 if (ret) {
4499 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004500 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004501 }
4502 } else {
4503 ret = -ENODEV;
4504 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004505 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004506 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304507 }
4508
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304509 msm_hsusb_mhl_switch_enable(motg, 1);
4510
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304511 platform_set_drvdata(pdev, motg);
4512 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004513 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304514
Anji jonnalad270e2d2011-08-09 11:28:32 +05304515 ret = msm_otg_debugfs_init(motg);
4516 if (ret)
4517 dev_dbg(&pdev->dev, "mode debugfs file is"
4518 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304519
Amit Blay58b31472011-11-18 09:39:39 +02004520 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4521 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304522 (!(motg->pdata->mode == USB_OTG) ||
4523 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004524 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304525 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004526
Amit Blay58b31472011-11-18 09:39:39 +02004527 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004528 motg->caps = ALLOW_PHY_RETENTION |
4529 ALLOW_PHY_REGULATORS_LPM;
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304530
4531 if (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int)
4532 motg->caps |= ALLOW_HOST_PHY_RETENTION;
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304533 device_create_file(&pdev->dev,
4534 &dev_attr_dpdm_pulldown_enable);
Amit Blay58b31472011-11-18 09:39:39 +02004535 }
4536
Amit Blay6fa647a2012-05-24 14:12:08 +03004537 if (motg->pdata->enable_lpm_on_dev_suspend)
4538 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4539
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004540 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304541 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004542 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304543
Amit Blayd6f38282012-10-29 13:13:46 +02004544 if (motg->pdata->delay_lpm_on_disconnect) {
4545 pm_runtime_set_autosuspend_delay(&pdev->dev,
4546 lpm_disconnect_thresh);
4547 pm_runtime_use_autosuspend(&pdev->dev);
4548 }
4549
David Keitel272ce522012-08-17 16:25:24 -07004550 motg->usb_psy.name = "usb";
4551 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4552 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4553 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4554 motg->usb_psy.properties = otg_pm_power_props_usb;
4555 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4556 motg->usb_psy.get_property = otg_power_get_property_usb;
4557 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004558 motg->usb_psy.property_is_writeable
4559 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004560
Jack Pham0c695282012-10-19 18:13:03 -07004561 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004562 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004563 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4564 legacy_power_supply = true;
4565 } else {
4566 /* otherwise register our own power supply */
4567 if (!msm_otg_register_power_supply(pdev, motg))
4568 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004569 }
4570
Jack Pham0c695282012-10-19 18:13:03 -07004571 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4572 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4573
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304574 ret = msm_otg_setup_ext_chg_cdev(motg);
4575 if (ret)
4576 dev_dbg(&pdev->dev, "fail to setup cdev\n");
4577
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304578 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004579
Steve Mucklef132c6c2012-06-06 18:30:57 -07004580remove_phy:
4581 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004582free_async_irq:
4583 if (motg->async_irq)
4584 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304585free_irq:
4586 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004587destroy_wlock:
4588 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304589 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004590 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004591free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304592 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304593free_hsusb_vdd:
4594 regulator_disable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304595free_config_vddcx:
Mayank Rana0f286cf2013-02-27 11:43:27 +05304596 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304597 vdd_val[motg->vdd_type][VDD_NONE],
4598 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004599devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304600 clk_disable_unprepare(motg->pclk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304601 if (!IS_ERR(motg->xo_clk))
4602 clk_disable_unprepare(motg->xo_clk);
4603 else
4604 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004605free_xo_handle:
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304606 if (!IS_ERR(motg->xo_clk))
4607 clk_put(motg->xo_clk);
4608 else
4609 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304610free_regs:
4611 iounmap(motg->regs);
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304612disable_sleep_clk:
4613 if (!IS_ERR(motg->sleep_clk))
4614 clk_disable_unprepare(motg->sleep_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004615put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304616 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304617put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004618 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304619put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304620 if (!IS_ERR(motg->clk))
4621 clk_put(motg->clk);
Manu Gautame3a39082013-06-11 10:42:56 +05304622devote_bus_bw:
4623 if (motg->bus_perf_client) {
4624 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
4625 msm_bus_scale_unregister_client(motg->bus_perf_client);
4626 }
4627
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304628 return ret;
4629}
4630
4631static int __devexit msm_otg_remove(struct platform_device *pdev)
4632{
4633 struct msm_otg *motg = platform_get_drvdata(pdev);
Stephen Boyd9850acb2013-01-28 14:11:20 -08004634 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304635 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304636
Stephen Boyd9850acb2013-01-28 14:11:20 -08004637 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304638 return -EBUSY;
4639
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304640 if (!motg->ext_chg_device) {
4641 device_destroy(motg->ext_chg_class, motg->ext_chg_dev);
4642 cdev_del(&motg->ext_chg_cdev);
4643 class_destroy(motg->ext_chg_class);
4644 unregister_chrdev_region(motg->ext_chg_dev, 1);
4645 }
4646
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304647 if (pdev->dev.of_node)
4648 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004649 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4650 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304651 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304652 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304653 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304654 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004655 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304656 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304657
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304658 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304659
4660 device_init_wakeup(&pdev->dev, 0);
4661 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004662 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304663
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304664 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004665 if (motg->pdata->pmic_id_irq)
4666 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004667 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304668 free_irq(motg->irq, motg);
4669
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304670 if ((motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) &&
4671 (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int))
4672 device_remove_file(&pdev->dev,
4673 &dev_attr_dpdm_pulldown_enable);
Jack Pham87f202f2012-08-06 00:24:22 -07004674 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4675 motg->pdata->mpm_otgsessvld_int)
4676 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4677
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304678 if (motg->pdata->mpm_dpshv_int)
4679 msm_mpm_enable_pin(motg->pdata->mpm_dpshv_int, 0);
4680 if (motg->pdata->mpm_dmshv_int)
4681 msm_mpm_enable_pin(motg->pdata->mpm_dmshv_int, 0);
4682
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304683 /*
4684 * Put PHY in low power mode.
4685 */
Stephen Boyd9850acb2013-01-28 14:11:20 -08004686 ulpi_read(phy, 0x14);
4687 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304688
4689 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4690 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4691 if (readl(USB_PORTSC) & PORTSC_PHCD)
4692 break;
4693 udelay(1);
4694 cnt++;
4695 }
4696 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Stephen Boyd9850acb2013-01-28 14:11:20 -08004697 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304698
Manu Gautam28b1bac2012-01-30 16:43:06 +05304699 clk_disable_unprepare(motg->pclk);
4700 clk_disable_unprepare(motg->core_clk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304701 if (!IS_ERR(motg->xo_clk)) {
4702 clk_disable_unprepare(motg->xo_clk);
4703 clk_put(motg->xo_clk);
4704 } else {
4705 msm_xo_put(motg->xo_handle);
4706 }
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304707
4708 if (!IS_ERR(motg->sleep_clk))
4709 clk_disable_unprepare(motg->sleep_clk);
4710
Amit Blay81801aa2012-09-19 12:08:12 +02004711 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304712 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304713 regulator_disable(hsusb_vdd);
4714 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304715 vdd_val[motg->vdd_type][VDD_NONE],
4716 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304717
4718 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304719 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304720
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304721 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304722 if (!IS_ERR(motg->clk))
4723 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004724 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304725
Manu Gautame3a39082013-06-11 10:42:56 +05304726 if (motg->bus_perf_client) {
4727 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304728 msm_bus_scale_unregister_client(motg->bus_perf_client);
Manu Gautame3a39082013-06-11 10:42:56 +05304729 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304730
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304731 return 0;
4732}
4733
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304734#ifdef CONFIG_PM_RUNTIME
4735static int msm_otg_runtime_idle(struct device *dev)
4736{
4737 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004738 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304739
4740 dev_dbg(dev, "OTG runtime idle\n");
4741
Steve Mucklef132c6c2012-06-06 18:30:57 -07004742 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304743 return -EAGAIN;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304744
4745 if (motg->ext_chg_active) {
4746 dev_dbg(dev, "Deferring LPM\n");
4747 /*
4748 * Charger detection may happen in user space.
4749 * Delay entering LPM by 3 sec. Otherwise we
4750 * have to exit LPM when user space begins
4751 * charger detection.
4752 *
4753 * This timer will be canceled when user space
4754 * votes against LPM by incrementing PM usage
4755 * counter. We enter low power mode when
4756 * PM usage counter is decremented.
4757 */
4758 pm_schedule_suspend(dev, 3000);
4759 return -EAGAIN;
4760 }
4761
4762 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304763}
4764
4765static int msm_otg_runtime_suspend(struct device *dev)
4766{
4767 struct msm_otg *motg = dev_get_drvdata(dev);
4768
4769 dev_dbg(dev, "OTG runtime suspend\n");
4770 return msm_otg_suspend(motg);
4771}
4772
4773static int msm_otg_runtime_resume(struct device *dev)
4774{
4775 struct msm_otg *motg = dev_get_drvdata(dev);
4776
4777 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304778 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304779 return msm_otg_resume(motg);
4780}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304781#endif
4782
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304783#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304784static int msm_otg_pm_suspend(struct device *dev)
4785{
Jack Pham5ca279b2012-05-14 18:42:54 -07004786 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304787 struct msm_otg *motg = dev_get_drvdata(dev);
4788
4789 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004790
4791 atomic_set(&motg->pm_suspended, 1);
4792 ret = msm_otg_suspend(motg);
4793 if (ret)
4794 atomic_set(&motg->pm_suspended, 0);
4795
4796 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304797}
4798
4799static int msm_otg_pm_resume(struct device *dev)
4800{
Jack Pham5ca279b2012-05-14 18:42:54 -07004801 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304802 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304803
4804 dev_dbg(dev, "OTG PM resume\n");
4805
Jack Pham5ca279b2012-05-14 18:42:54 -07004806 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004807 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004808 pm_runtime_get_noresume(dev);
4809 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304810
Jack Pham5ca279b2012-05-14 18:42:54 -07004811 /* Update runtime PM status */
4812 pm_runtime_disable(dev);
4813 pm_runtime_set_active(dev);
4814 pm_runtime_enable(dev);
4815
Jack Phamc7edb172012-08-13 15:32:39 -07004816 if (motg->sm_work_pending) {
4817 motg->sm_work_pending = false;
4818 queue_work(system_nrt_wq, &motg->sm_work);
4819 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004820 }
4821
4822 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304823}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304824#endif
4825
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304826#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304827static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304828 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4829 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4830 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304831};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304832#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304833
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304834static struct of_device_id msm_otg_dt_match[] = {
4835 { .compatible = "qcom,hsusb-otg",
4836 },
4837 {}
4838};
4839
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304840static struct platform_driver msm_otg_driver = {
4841 .remove = __devexit_p(msm_otg_remove),
4842 .driver = {
4843 .name = DRIVER_NAME,
4844 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304845#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304846 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304847#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304848 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304849 },
4850};
4851
4852static int __init msm_otg_init(void)
4853{
4854 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4855}
4856
4857static void __exit msm_otg_exit(void)
4858{
4859 platform_driver_unregister(&msm_otg_driver);
4860}
4861
4862module_init(msm_otg_init);
4863module_exit(msm_otg_exit);
4864
4865MODULE_LICENSE("GPL v2");
4866MODULE_DESCRIPTION("MSM USB transceiver driver");