blob: 8287ad7200df731d5b57c20fb0ff2cb35b6c3c79 [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
Mayank Rana34f6f3e2013-11-11 12:40:00 +0530473 /*
474 * AHB2AHB Bypass mode shouldn't be enable before doing
475 * async clock reset. If it is enable, disable the same.
476 */
477 val = readl_relaxed(USB_AHBMODE);
478 if (val & AHB2AHB_BYPASS) {
479 pr_err("%s(): AHB2AHB_BYPASS SET: AHBMODE:%x\n",
480 __func__, val);
481 val &= ~AHB2AHB_BYPASS_BIT_MASK;
482 writel_relaxed(val | AHB2AHB_BYPASS_CLEAR, USB_AHBMODE);
483 pr_err("%s(): AHBMODE: %x\n", __func__,
484 readl_relaxed(USB_AHBMODE));
485 }
486
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530487 ret = msm_otg_link_clk_reset(motg, 1);
488 if (ret)
489 return ret;
Amit Blay58dc2bc2013-01-24 12:28:03 +0200490
Mayank Ranab95838b2013-07-31 14:36:35 +0530491 /* wait for 1ms delay as suggested in HPG. */
492 usleep_range(1000, 1200);
493
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530494 ret = msm_otg_link_clk_reset(motg, 0);
495 if (ret)
496 return ret;
497
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530498 if (pdata && pdata->enable_sec_phy)
499 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
500 USB_PHY_CTRL2);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530501 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
502 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
503
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200504 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530505 return 0;
506}
507
508#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530509static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530510{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530511 int cnt = 0;
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530512 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530513
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530514 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530515 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530516 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530517 break;
518 udelay(1);
519 cnt++;
520 }
521 if (cnt >= LINK_RESET_TIMEOUT_USEC)
522 return -ETIMEDOUT;
523
524 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530525 writel_relaxed(0x80000000, USB_PORTSC);
526 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530527 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530528
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530529 if (pdata && pdata->enable_sec_phy)
530 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
531 USB_PHY_CTRL2);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530532 return 0;
533}
534
Mayank Ranab95838b2013-07-31 14:36:35 +0530535static void usb_phy_reset(struct msm_otg *motg)
536{
537 u32 val;
538
539 if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY)
540 return;
541
542 /* Assert USB PHY_PON */
543 val = readl_relaxed(USB_PHY_CTRL);
544 val &= ~PHY_POR_BIT_MASK;
545 val |= PHY_POR_ASSERT;
546 writel_relaxed(val, USB_PHY_CTRL);
547
548 /* wait for minimum 10 microseconds as suggested in HPG. */
549 usleep_range(10, 15);
550
551 /* Deassert USB PHY_PON */
552 val = readl_relaxed(USB_PHY_CTRL);
553 val &= ~PHY_POR_BIT_MASK;
554 val |= PHY_POR_DEASSERT;
555 writel_relaxed(val, USB_PHY_CTRL);
556
557 /* Ensure that RESET operation is completed. */
558 mb();
559}
560
Steve Mucklef132c6c2012-06-06 18:30:57 -0700561static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530562{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700563 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530564 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530565 int ret;
566 u32 val = 0;
567 u32 ulpi_val = 0;
568
Ofir Cohen4da266f2012-01-03 10:19:29 +0200569 /*
570 * USB PHY and Link reset also reset the USB BAM.
571 * Thus perform reset operation only once to avoid
572 * USB BAM reset on other cases e.g. USB cable disconnections.
573 */
574 if (pdata->disable_reset_on_disconnect) {
575 if (motg->reset_counter)
576 return 0;
577 else
578 motg->reset_counter++;
579 }
580
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530581 if (!IS_ERR(motg->clk))
582 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530583 ret = msm_otg_phy_reset(motg);
584 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700585 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530586 return ret;
587 }
588
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530589 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530590 ret = msm_otg_link_reset(motg);
591 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700592 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530593 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530594 }
Mayank Ranab95838b2013-07-31 14:36:35 +0530595
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530596 msleep(100);
597
Mayank Ranab95838b2013-07-31 14:36:35 +0530598 /* Reset USB PHY after performing USB Link RESET */
599 usb_phy_reset(motg);
600
601 /* Program USB PHY Override registers. */
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530602 ulpi_init(motg);
603
Mayank Ranab95838b2013-07-31 14:36:35 +0530604 /*
605 * It is recommended in HPG to reset USB PHY after programming
606 * USB PHY Override registers.
607 */
608 usb_phy_reset(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530609
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530610 if (!IS_ERR(motg->clk))
611 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530612
613 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530614 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530615 if (pdata->mode == USB_OTG) {
616 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
617 val |= OTGSC_IDIE | OTGSC_BSVIE;
618 } else if (pdata->mode == USB_PERIPHERAL) {
619 ulpi_val = ULPI_INT_SESS_VALID;
620 val |= OTGSC_BSVIE;
621 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530622 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200623 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
624 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530625 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700626 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530627 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530628 /* Enable PMIC pull-up */
629 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530630 }
631
Saket Saurabhb06a8c62013-11-25 15:17:23 +0530632 if (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED)
633 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU),
634 USB_OTGSC);
635
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530636 return 0;
637}
638
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530639static const char *timer_string(int bit)
640{
641 switch (bit) {
642 case A_WAIT_VRISE: return "a_wait_vrise";
643 case A_WAIT_VFALL: return "a_wait_vfall";
644 case B_SRP_FAIL: return "b_srp_fail";
645 case A_WAIT_BCON: return "a_wait_bcon";
646 case A_AIDL_BDIS: return "a_aidl_bdis";
647 case A_BIDL_ADIS: return "a_bidl_adis";
648 case B_ASE0_BRST: return "b_ase0_brst";
649 case A_TST_MAINT: return "a_tst_maint";
650 case B_TST_SRP: return "b_tst_srp";
651 case B_TST_CONFIG: return "b_tst_config";
652 default: return "UNDEFINED";
653 }
654}
655
656static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
657{
658 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
659
660 switch (motg->active_tmout) {
661 case A_WAIT_VRISE:
662 /* TODO: use vbus_vld interrupt */
663 set_bit(A_VBUS_VLD, &motg->inputs);
664 break;
665 case A_TST_MAINT:
666 /* OTG PET: End session after TA_TST_MAINT */
667 set_bit(A_BUS_DROP, &motg->inputs);
668 break;
669 case B_TST_SRP:
670 /*
671 * OTG PET: Initiate SRP after TB_TST_SRP of
672 * previous session end.
673 */
674 set_bit(B_BUS_REQ, &motg->inputs);
675 break;
676 case B_TST_CONFIG:
677 clear_bit(A_CONN, &motg->inputs);
678 break;
679 default:
680 set_bit(motg->active_tmout, &motg->tmouts);
681 }
682
683 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
684 queue_work(system_nrt_wq, &motg->sm_work);
685 return HRTIMER_NORESTART;
686}
687
688static void msm_otg_del_timer(struct msm_otg *motg)
689{
690 int bit = motg->active_tmout;
691
692 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
693 div_s64(ktime_to_us(hrtimer_get_remaining(
694 &motg->timer)), 1000));
695 hrtimer_cancel(&motg->timer);
696 clear_bit(bit, &motg->tmouts);
697}
698
699static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
700{
701 clear_bit(bit, &motg->tmouts);
702 motg->active_tmout = bit;
703 pr_debug("starting %s timer\n", timer_string(bit));
704 hrtimer_start(&motg->timer,
705 ktime_set(time / 1000, (time % 1000) * 1000000),
706 HRTIMER_MODE_REL);
707}
708
709static void msm_otg_init_timer(struct msm_otg *motg)
710{
711 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
712 motg->timer.function = msm_otg_timer_func;
713}
714
Steve Mucklef132c6c2012-06-06 18:30:57 -0700715static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530716{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700717 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530718
Steve Mucklef132c6c2012-06-06 18:30:57 -0700719 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530720 pr_err("HNP 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 return -EINVAL;
723 }
724
725 pr_debug("A-Host: HNP initiated\n");
726 clear_bit(A_BUS_REQ, &motg->inputs);
727 queue_work(system_nrt_wq, &motg->sm_work);
728 return 0;
729}
730
Steve Mucklef132c6c2012-06-06 18:30:57 -0700731static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530732{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700733 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530734 u32 val;
735 int ret = 0;
736
Steve Mucklef132c6c2012-06-06 18:30:57 -0700737 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530738 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700739 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530740 ret = -EINVAL;
741 goto out;
742 }
743
744 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
745 pr_debug("initial conditions of SRP are not met. Try again"
746 "after some time\n");
747 ret = -EAGAIN;
748 goto out;
749 }
750
751 pr_debug("B-Device SRP started\n");
752
753 /*
754 * PHY won't pull D+ high unless it detects Vbus valid.
755 * Since by definition, SRP is only done when Vbus is not valid,
756 * software work-around needs to be used to spoof the PHY into
757 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
758 * VBUSVLDEXT register bits.
759 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700760 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530761 /*
762 * Harware auto assist data pulsing: Data pulse is given
763 * for 7msec; wait for vbus
764 */
765 val = readl_relaxed(USB_OTGSC);
766 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
767
768 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
769out:
770 return ret;
771}
772
Steve Mucklef132c6c2012-06-06 18:30:57 -0700773static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530774{
775 struct usb_hcd *hcd = bus_to_hcd(otg->host);
776 struct usb_device *rhub = otg->host->root_hub;
777
778 if (enable) {
779 pm_runtime_disable(&rhub->dev);
780 rhub->state = USB_STATE_NOTATTACHED;
781 hcd->driver->bus_suspend(hcd);
782 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
783 } else {
784 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700785 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530786 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
787 }
788}
789
Steve Mucklef132c6c2012-06-06 18:30:57 -0700790static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530791{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700792 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530793
Amit Blay6fa647a2012-05-24 14:12:08 +0300794 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530795 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530796
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530797 /*
798 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
799 * during bus suspend/resume. Update the relevant state
800 * machine inputs and trigger LPM entry/exit. Checking
801 * in_lpm flag would avoid unnecessary work scheduling.
802 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530803 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700804 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530805 case OTG_STATE_A_WAIT_BCON:
806 if (TA_WAIT_BCON > 0)
807 break;
808 /* fall through */
809 case OTG_STATE_A_HOST:
810 pr_debug("host bus suspend\n");
811 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530812 if (!atomic_read(&motg->in_lpm))
813 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530814 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300815 case OTG_STATE_B_PERIPHERAL:
816 pr_debug("peripheral bus suspend\n");
817 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
818 break;
819 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530820 if (!atomic_read(&motg->in_lpm))
821 queue_delayed_work(system_nrt_wq,
822 &motg->suspend_work,
823 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300824 break;
825
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530826 default:
827 break;
828 }
829 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700830 switch (phy->state) {
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530831 case OTG_STATE_A_WAIT_BCON:
832 /* Remote wakeup or resume */
833 set_bit(A_BUS_REQ, &motg->inputs);
834 /* ensure hardware is not in low power mode */
835 if (atomic_read(&motg->in_lpm))
836 pm_runtime_resume(phy->dev);
837 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530838 case OTG_STATE_A_SUSPEND:
839 /* Remote wakeup or resume */
840 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700841 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700842
843 /* ensure hardware is not in low power mode */
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530844 if (atomic_read(&motg->in_lpm))
845 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530846 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300847 case OTG_STATE_B_PERIPHERAL:
848 pr_debug("peripheral bus resume\n");
849 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
850 break;
851 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530852 if (atomic_read(&motg->in_lpm))
853 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300854 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530855 default:
856 break;
857 }
858 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530859 return 0;
860}
861
Manu Gautame3a39082013-06-11 10:42:56 +0530862static void msm_otg_bus_vote(struct msm_otg *motg, enum usb_bus_vote vote)
863{
864 int ret;
865 struct msm_otg_platform_data *pdata = motg->pdata;
866
867 /* Check if target allows min_vote to be same as no_vote */
Ido Shayevitzcf050e22013-07-22 17:31:48 +0300868 if (pdata->bus_scale_table &&
869 vote >= pdata->bus_scale_table->num_usecases)
Manu Gautame3a39082013-06-11 10:42:56 +0530870 vote = USB_NO_PERF_VOTE;
871
872 if (motg->bus_perf_client) {
873 ret = msm_bus_scale_client_update_request(
874 motg->bus_perf_client, vote);
875 if (ret)
876 dev_err(motg->phy.dev, "%s: Failed to vote (%d)\n"
877 "for bus bw %d\n", __func__, vote, ret);
878 }
879}
880
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530881#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530882#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
883
884#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530885static int msm_otg_suspend(struct msm_otg *motg)
886{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200887 struct usb_phy *phy = &motg->phy;
888 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530889 struct msm_otg_platform_data *pdata = motg->pdata;
890 int cnt = 0;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530891 bool host_bus_suspend, device_bus_suspend, dcp, prop_charger;
zhenhuahb6d5edc2013-06-19 14:37:32 +0800892 bool floated_charger;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530893 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800894 unsigned ret;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +0530895 u32 portsc, config2;
Saket Saurabhb06a8c62013-11-25 15:17:23 +0530896 u32 func_ctrl;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530897
898 if (atomic_read(&motg->in_lpm))
899 return 0;
900
Lena Salmanabde35d2013-04-25 15:29:43 +0300901 if (motg->pdata->delay_lpm_hndshk_on_disconnect && !msm_bam_lpm_ok())
Lena Salman05b544f2013-05-13 15:49:10 +0300902 return -EBUSY;
Lena Salmanabde35d2013-04-25 15:29:43 +0300903
Sujeet Kumarceee6e82013-09-06 11:22:49 +0530904 motg->ui_enabled = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530905 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530906 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
907 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700908 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300909 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
910 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530911 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530912 prop_charger = motg->chg_type == USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +0800913 floated_charger = motg->chg_type == USB_FLOATED_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700914
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +0530915 /* Enable line state difference wakeup fix for only device and host
916 * bus suspend scenarios. Otherwise PHY can not be suspended when
917 * a charger that pulls DP/DM high is connected.
918 */
919 config2 = readl_relaxed(USB_GENCONFIG2);
920 if (device_bus_suspend)
921 config2 |= GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
922 else
923 config2 &= ~GENCFG2_LINESTATE_DIFF_WAKEUP_EN;
924 writel_relaxed(config2, USB_GENCONFIG2);
925
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530926 /*
927 * Abort suspend when,
928 * 1. charging detection in progress due to cable plug-in
929 * 2. host mode activation in progress due to Micro-A cable insertion
930 */
931
932 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
zhenhuahb6d5edc2013-06-19 14:37:32 +0800933 !dcp && !prop_charger && !floated_charger) ||
934 test_bit(A_BUS_REQ, &motg->inputs)) {
Sujeet Kumarceee6e82013-09-06 11:22:49 +0530935 motg->ui_enabled = 1;
Jack Pham502bea32012-08-13 15:34:20 -0700936 enable_irq(motg->irq);
937 return -EBUSY;
938 }
939
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530940 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530941 * Chipidea 45-nm PHY suspend sequence:
942 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530943 * Interrupt Latch Register auto-clear feature is not present
944 * in all PHY versions. Latch register is clear on read type.
945 * Clear latch register to avoid spurious wakeup from
946 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530947 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530948 * PHY comparators are disabled when PHY enters into low power
949 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
950 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
951 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530952 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530953 * PLL is not turned off when PHY enters into low power mode (LPM).
954 * Disable PLL for maximum power savings.
955 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530956
957 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200958 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530959 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200960 ulpi_write(phy, 0x01, 0x30);
961 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530962 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530963
Saket Saurabhb06a8c62013-11-25 15:17:23 +0530964 if (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED) {
965 /* put the controller in non-driving mode */
966 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
967 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
968 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
969 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
970 ulpi_write(phy, ULPI_IFC_CTRL_AUTORESUME,
971 ULPI_CLR(ULPI_IFC_CTRL));
972 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530974 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530975 * PHY may take some time or even fail to enter into low power
976 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
977 * in failure case.
978 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530979 portsc = readl_relaxed(USB_PORTSC);
980 if (!(portsc & PORTSC_PHCD)) {
981 writel_relaxed(portsc | PORTSC_PHCD,
982 USB_PORTSC);
983 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
984 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
985 break;
986 udelay(1);
987 cnt++;
988 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530989 }
990
991 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200992 dev_err(phy->dev, "Unable to suspend PHY\n");
993 msm_otg_reset(phy);
Sujeet Kumarceee6e82013-09-06 11:22:49 +0530994 motg->ui_enabled = 1;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530995 enable_irq(motg->irq);
996 return -ETIMEDOUT;
997 }
998
999 /*
1000 * PHY has capability to generate interrupt asynchronously in low
1001 * power mode (LPM). This interrupt is level triggered. So USB IRQ
1002 * line must be disabled till async interrupt enable bit is cleared
1003 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
1004 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +05301005 *
1006 * PHY retention mode is disallowed while entering to LPM with wall
1007 * charger connected. But PHY is put into suspend mode. Hence
1008 * enable asynchronous interrupt to detect charger disconnection when
1009 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301010 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301011 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +03001012 if (host_bus_suspend || device_bus_suspend ||
Pavankumar Kondeti70970b72013-05-16 13:37:24 +05301013 (motg->pdata->otg_control == OTG_PHY_CONTROL))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301014 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
1015 else
1016 cmd_val |= ULPI_STP_CTRL;
1017 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301018
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301019 /*
1020 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
1021 * PHY retention and collapse can not happen with VDP_SRC enabled.
1022 */
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301023 if (motg->caps & ALLOW_PHY_RETENTION && !device_bus_suspend && !dcp &&
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05301024 (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION)
1025 && (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS))))) {
Amit Blay58b31472011-11-18 09:39:39 +02001026 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05301027 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +02001028 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05301029 if ((motg->pdata->mode == USB_OTG) ||
1030 (motg->pdata->mode == USB_HOST))
1031 phy_ctrl_val |= (PHY_IDHV_INTEN |
1032 PHY_OTGSESSVLDHV_INTEN);
1033 else
1034 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
1035 }
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301036 if (host_bus_suspend)
1037 phy_ctrl_val |= PHY_CLAMP_DPDMSE_EN;
Saket Saurabhb06a8c62013-11-25 15:17:23 +05301038
1039 if (!(motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED)) {
1040 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
1041 motg->lpm_flags |= PHY_RETENTIONED;
1042 } else {
1043 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
1044 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301045 }
1046
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 /* Ensure that above operation is completed before turning off clocks */
1048 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001049 /* Consider clocks on workaround flag only in case of bus suspend */
1050 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
1051 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
1052 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001053 clk_disable_unprepare(motg->pclk);
1054 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001055 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001056 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301057
Anji jonnala7da3f262011-12-02 17:22:14 -08001058 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05301059 if (!host_bus_suspend || ((motg->caps & ALLOW_HOST_PHY_RETENTION) &&
1060 (pdata->dpdm_pulldown_added || !(portsc & PORTSC_CCS)))) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301061 if (!IS_ERR(motg->xo_clk)) {
1062 clk_disable_unprepare(motg->xo_clk);
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301063 motg->lpm_flags |= XO_SHUTDOWN;
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301064 } else {
1065 ret = msm_xo_mode_vote(motg->xo_handle,
1066 MSM_XO_MODE_OFF);
1067 if (ret)
1068 dev_err(phy->dev, "%s fail to devote XO %d\n",
1069 __func__, ret);
1070 else
1071 motg->lpm_flags |= XO_SHUTDOWN;
1072 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301073 }
Anji jonnala7da3f262011-12-02 17:22:14 -08001074
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301075 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
1076 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +02001077 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001078 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001079 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
1080 !host_bus_suspend && !device_bus_suspend && !dcp) {
1081 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
1082 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +05301083 }
1084
Saket Saurabhb06a8c62013-11-25 15:17:23 +05301085 if (motg->lpm_flags & PHY_RETENTIONED ||
1086 (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301088 msm_hsusb_mhl_switch_enable(motg, 0);
1089 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090
Steve Mucklef132c6c2012-06-06 18:30:57 -07001091 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001092 if (motg->async_irq)
1093 enable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001094 else
1095 enable_irq_wake(motg->irq);
1096
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 if (motg->pdata->pmic_id_irq)
1098 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001099 if (pdata->otg_control == OTG_PHY_CONTROL &&
1100 pdata->mpm_otgsessvld_int)
1101 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301102 if (host_bus_suspend && pdata->mpm_dpshv_int)
1103 msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 1);
1104 if (host_bus_suspend && pdata->mpm_dmshv_int)
1105 msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301107 if (bus)
1108 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1109
Manu Gautame3a39082013-06-11 10:42:56 +05301110 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
1111
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301112 motg->host_bus_suspend = host_bus_suspend;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301113 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001114 /* Enable ASYNC IRQ (if present) during LPM */
1115 if (motg->async_irq)
1116 enable_irq(motg->async_irq);
Sujeet Kumarceee6e82013-09-06 11:22:49 +05301117
1118 /* XO shutdown during idle , non wakeable irqs must be disabled */
1119 if (device_bus_suspend || host_bus_suspend || !motg->async_irq) {
1120 motg->ui_enabled = 1;
1121 enable_irq(motg->irq);
1122 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301124
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001125 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301126
1127 return 0;
1128}
1129
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301130static int msm_otg_resume(struct msm_otg *motg)
1131{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001132 struct usb_phy *phy = &motg->phy;
1133 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001134 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301135 int cnt = 0;
1136 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001137 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001138 unsigned ret;
Saket Saurabhb06a8c62013-11-25 15:17:23 +05301139 u32 func_ctrl;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301140
1141 if (!atomic_read(&motg->in_lpm))
1142 return 0;
1143
Ido Shayevitz44ca03c2013-07-09 15:57:21 +03001144 if (motg->pdata->delay_lpm_hndshk_on_disconnect)
1145 msm_bam_notify_lpm_resume();
1146
Sujeet Kumarceee6e82013-09-06 11:22:49 +05301147 if (motg->ui_enabled) {
1148 motg->ui_enabled = 0;
1149 disable_irq(motg->irq);
1150 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001151 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301152
Manu Gautame3a39082013-06-11 10:42:56 +05301153 /* Some platforms require BUS vote to enable/disable clocks */
1154 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
1155
Anji jonnala7da3f262011-12-02 17:22:14 -08001156 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301157 if (motg->lpm_flags & XO_SHUTDOWN) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301158 if (!IS_ERR(motg->xo_clk)) {
1159 clk_prepare_enable(motg->xo_clk);
1160 } else {
1161 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1162 if (ret)
1163 dev_err(phy->dev, "%s fail to vote for XO %d\n",
1164 __func__, ret);
1165 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301166 motg->lpm_flags &= ~XO_SHUTDOWN;
1167 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301168
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001169 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001170 clk_prepare_enable(motg->core_clk);
1171 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001172 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001173 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301174
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001175 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001176 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001178 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1179 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1180 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001181 }
1182
Saket Saurabhb06a8c62013-11-25 15:17:23 +05301183 if (motg->lpm_flags & PHY_RETENTIONED ||
1184 (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED)) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301185 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301186 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001187 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1188 phy_ctrl_val |= PHY_RETEN;
1189 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1190 /* Disable PHY HV interrupts */
1191 phy_ctrl_val &=
1192 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301193 phy_ctrl_val &= ~(PHY_CLAMP_DPDMSE_EN);
Amit Blay58b31472011-11-18 09:39:39 +02001194 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001195 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301196 }
1197
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301198 temp = readl(USB_USBCMD);
1199 temp &= ~ASYNC_INTR_CTRL;
1200 temp &= ~ULPI_STP_CTRL;
1201 writel(temp, USB_USBCMD);
1202
1203 /*
1204 * PHY comes out of low power mode (LPM) in case of wakeup
1205 * from asynchronous interrupt.
1206 */
1207 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1208 goto skip_phy_resume;
1209
1210 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1211 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1212 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1213 break;
1214 udelay(1);
1215 cnt++;
1216 }
1217
1218 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1219 /*
1220 * This is a fatal error. Reset the link and
1221 * PHY. USB state can not be restored. Re-insertion
1222 * of USB cable is the only way to get USB working.
1223 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001224 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301225 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001226 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301227 }
1228
1229skip_phy_resume:
Saket Saurabhb06a8c62013-11-25 15:17:23 +05301230 if (motg->caps & ALLOW_VDD_MIN_WITH_RETENTION_DISABLED) {
1231 /* put the controller in normal mode */
1232 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
1233 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1234 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1235 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
1236 }
1237
Steve Mucklef132c6c2012-06-06 18:30:57 -07001238 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001239 if (motg->async_irq)
1240 disable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001241 else
1242 disable_irq_wake(motg->irq);
1243
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001244 if (motg->pdata->pmic_id_irq)
1245 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001246 if (pdata->otg_control == OTG_PHY_CONTROL &&
1247 pdata->mpm_otgsessvld_int)
1248 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05301249 if (motg->host_bus_suspend && pdata->mpm_dpshv_int)
1250 msm_mpm_set_pin_wake(pdata->mpm_dpshv_int, 0);
1251 if (motg->host_bus_suspend && pdata->mpm_dmshv_int)
1252 msm_mpm_set_pin_wake(pdata->mpm_dmshv_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001253 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301254 if (bus)
1255 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1256
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301257 atomic_set(&motg->in_lpm, 0);
1258
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301259 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001260 /* Match the disable_irq call from ISR */
1261 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301262 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301263 }
Sujeet Kumarceee6e82013-09-06 11:22:49 +05301264 motg->ui_enabled = 1;
Jack Pham8978b892012-10-17 16:31:39 -07001265 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301266
Manu Gautamf8c45642012-08-10 10:20:56 -07001267 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1268 if (motg->async_irq)
1269 disable_irq(motg->async_irq);
1270
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001271 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301272
1273 return 0;
1274}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301275#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301276
David Keitel272ce522012-08-17 16:25:24 -07001277static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001278{
Jack Pham0c695282012-10-19 18:13:03 -07001279 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001280 pr_err("No USB power supply registered!\n");
1281 return;
1282 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001283
Jack Pham0c695282012-10-19 18:13:03 -07001284 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001285 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301286 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001287 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301288 } else {
David Keitel272ce522012-08-17 16:25:24 -07001289 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301290 /*
1291 * VBUS comparator is disabled by PMIC charging driver
1292 * when SYSTEM scope is selected. For ID_GND->ID_A
1293 * transition, give 50 msec delay so that PMIC charger
1294 * driver detect the VBUS and ready for accepting
1295 * charging current value from USB.
1296 */
1297 if (test_bit(ID_A, &motg->inputs))
1298 msleep(50);
1299 }
David Keitel272ce522012-08-17 16:25:24 -07001300 } else {
1301 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001302 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001303 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001304}
1305
David Keitel081a3e22012-04-18 12:37:07 -07001306static int msm_otg_notify_chg_type(struct msm_otg *motg)
1307{
1308 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001309
David Keitel081a3e22012-04-18 12:37:07 -07001310 /*
1311 * TODO
1312 * Unify OTG driver charger types and power supply charger types
1313 */
1314 if (charger_type == motg->chg_type)
1315 return 0;
1316
1317 if (motg->chg_type == USB_SDP_CHARGER)
1318 charger_type = POWER_SUPPLY_TYPE_USB;
1319 else if (motg->chg_type == USB_CDP_CHARGER)
1320 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301321 else if (motg->chg_type == USB_DCP_CHARGER ||
Zhenhua Huang504ce782013-11-14 12:29:58 +08001322 motg->chg_type == USB_PROPRIETARY_CHARGER ||
1323 motg->chg_type == USB_FLOATED_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001324 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1325 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1326 motg->chg_type == USB_ACA_A_CHARGER ||
1327 motg->chg_type == USB_ACA_B_CHARGER ||
1328 motg->chg_type == USB_ACA_C_CHARGER))
1329 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1330 else
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301331 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001332
Jack Pham0c695282012-10-19 18:13:03 -07001333 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001334 pr_err("No USB power supply registered!\n");
1335 return -EINVAL;
1336 }
1337
1338 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001339 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001340 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001341}
1342
Amit Blay0f7edf72012-01-15 10:11:27 +02001343static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1344{
Jack Pham0c695282012-10-19 18:13:03 -07001345 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001346 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1347 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001348 }
1349
David Keitel272ce522012-08-17 16:25:24 -07001350 if (motg->cur_power == 0 && mA > 2) {
1351 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001352 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001353 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001354 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001355 goto psy_error;
1356 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1357 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001358 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001359 goto psy_error;
1360 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001361 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001362 goto psy_error;
Manu Gautamfca298c2013-06-05 15:11:54 +05301363 } else {
1364 if (power_supply_set_online(psy, true))
1365 goto psy_error;
1366 /* Current has changed (100/2 --> 500) */
1367 if (power_supply_set_current_limit(psy, 1000*mA))
1368 goto psy_error;
David Keitel272ce522012-08-17 16:25:24 -07001369 }
Manu Gautamfca298c2013-06-05 15:11:54 +05301370
Jack Pham0c695282012-10-19 18:13:03 -07001371 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001372 return 0;
1373
David Keitel272ce522012-08-17 16:25:24 -07001374psy_error:
1375 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001376 return -ENXIO;
1377}
1378
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301379static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1380{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001381 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301382
1383 if (g && g->is_a_peripheral)
1384 return;
1385
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301386 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1387 motg->chg_type == USB_ACA_A_CHARGER ||
1388 motg->chg_type == USB_ACA_B_CHARGER ||
1389 motg->chg_type == USB_ACA_C_CHARGER) &&
1390 mA > IDEV_ACA_CHG_LIMIT)
1391 mA = IDEV_ACA_CHG_LIMIT;
1392
David Keitel081a3e22012-04-18 12:37:07 -07001393 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001394 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001395 "Failed notifying %d charger type to PMIC\n",
1396 motg->chg_type);
1397
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301398 if (motg->cur_power == mA)
1399 return;
1400
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001401 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001402
1403 /*
1404 * Use Power Supply API if supported, otherwise fallback
1405 * to legacy pm8921 API.
1406 */
1407 if (msm_otg_notify_power_supply(motg, mA))
1408 pm8921_charger_vbus_draw(mA);
1409
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301410 motg->cur_power = mA;
1411}
1412
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001413static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301414{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001415 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301416
1417 /*
1418 * Gadget driver uses set_power method to notify about the
1419 * available current based on suspend/configured states.
1420 *
1421 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1422 * states when CDP/ACA is connected.
1423 */
1424 if (motg->chg_type == USB_SDP_CHARGER)
1425 msm_otg_notify_charger(motg, mA);
1426
1427 return 0;
1428}
1429
Steve Mucklef132c6c2012-06-06 18:30:57 -07001430static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301431{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001432 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301433 struct msm_otg_platform_data *pdata = motg->pdata;
1434 struct usb_hcd *hcd;
1435
1436 if (!otg->host)
1437 return;
1438
1439 hcd = bus_to_hcd(otg->host);
1440
1441 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001442 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301443
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301444 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001445 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301446 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1447
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301448 /*
1449 * Some boards have a switch cotrolled by gpio
1450 * to enable/disable internal HUB. Enable internal
1451 * HUB before kicking the host.
1452 */
1453 if (pdata->setup_gpio)
1454 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301455 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301456 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001457 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301458
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301459 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301460 /* HCD core reset all bits of PORTSC. select ULPI phy */
1461 writel_relaxed(0x80000000, USB_PORTSC);
1462
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301463 if (pdata->setup_gpio)
1464 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301465
1466 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001467 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301468 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301469 }
1470}
1471
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001472static int msm_otg_usbdev_notify(struct notifier_block *self,
1473 unsigned long action, void *priv)
1474{
1475 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001476 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301477 struct usb_device *udev = priv;
1478
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301479 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1480 goto out;
1481
Steve Mucklef132c6c2012-06-06 18:30:57 -07001482 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301483 goto out;
1484 /*
1485 * Interested in devices connected directly to the root hub.
1486 * ACA dock can supply IDEV_CHG irrespective devices connected
1487 * on the accessory port.
1488 */
1489 if (!udev->parent || udev->parent->parent ||
1490 motg->chg_type == USB_ACA_DOCK_CHARGER)
1491 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001492
1493 switch (action) {
1494 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301495 if (aca_enabled())
1496 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001497 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301498 pr_debug("B_CONN set\n");
1499 set_bit(B_CONN, &motg->inputs);
1500 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001501 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301502 /*
1503 * OTG PET: A-device must end session within
1504 * 10 sec after PET enumeration.
1505 */
1506 if (udev->quirks & USB_QUIRK_OTG_PET)
1507 msm_otg_start_timer(motg, TA_TST_MAINT,
1508 A_TST_MAINT);
1509 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301510 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001511 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001512 if (udev->actconfig)
1513 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1514 else
1515 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001516 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301517 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301518 break;
1519 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001520 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1521 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301522 pr_debug("B_CONN clear\n");
1523 clear_bit(B_CONN, &motg->inputs);
1524 /*
1525 * OTG PET: A-device must end session after
1526 * PET disconnection if it is enumerated
1527 * with bcdDevice[0] = 1. USB core sets
1528 * bus->otg_vbus_off for us. clear it here.
1529 */
1530 if (udev->bus->otg_vbus_off) {
1531 udev->bus->otg_vbus_off = 0;
1532 set_bit(A_BUS_DROP, &motg->inputs);
1533 }
1534 queue_work(system_nrt_wq, &motg->sm_work);
1535 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001536 default:
1537 break;
1538 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301539 if (test_bit(ID_A, &motg->inputs))
1540 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1541 motg->mA_port);
1542out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001543 return NOTIFY_OK;
1544}
1545
Mayank Ranae3926882011-12-26 09:47:54 +05301546static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1547{
1548 int ret;
1549 static bool vbus_is_on;
1550
1551 if (vbus_is_on == on)
1552 return;
1553
1554 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301555 ret = motg->pdata->vbus_power(on);
1556 if (!ret)
1557 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301558 return;
1559 }
1560
1561 if (!vbus_otg) {
1562 pr_err("vbus_otg is NULL.");
1563 return;
1564 }
1565
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001566 /*
1567 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001568 * from usb before turning on the boost.
1569 * if exiting host mode disable the boost before enabling to draw
1570 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001571 */
Mayank Ranae3926882011-12-26 09:47:54 +05301572 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001573 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301574 ret = regulator_enable(vbus_otg);
1575 if (ret) {
1576 pr_err("unable to enable vbus_otg\n");
1577 return;
1578 }
1579 vbus_is_on = true;
1580 } else {
1581 ret = regulator_disable(vbus_otg);
1582 if (ret) {
1583 pr_err("unable to disable vbus_otg\n");
1584 return;
1585 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001586 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301587 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301588 }
1589}
1590
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001591static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301592{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001593 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301594 struct usb_hcd *hcd;
1595
1596 /*
1597 * Fail host registration if this board can support
1598 * only peripheral configuration.
1599 */
1600 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001601 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301602 return -ENODEV;
1603 }
1604
Mayank Ranae3926882011-12-26 09:47:54 +05301605 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001606 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301607 if (IS_ERR(vbus_otg)) {
1608 pr_err("Unable to get vbus_otg\n");
Manu Gautam139a8132013-06-04 16:46:50 +05301609 return PTR_ERR(vbus_otg);
Mayank Ranae3926882011-12-26 09:47:54 +05301610 }
1611 }
1612
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301613 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001614 if (otg->phy->state == OTG_STATE_A_HOST) {
1615 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001616 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301617 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301618 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301619 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001620 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301621 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301622 } else {
1623 otg->host = NULL;
1624 }
1625
1626 return 0;
1627 }
1628
1629 hcd = bus_to_hcd(host);
1630 hcd->power_budget = motg->pdata->power_budget;
1631
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301632#ifdef CONFIG_USB_OTG
1633 host->otg_port = 1;
1634#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001635 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1636 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301637 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001638 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301639
1640 /*
1641 * Kick the state machine work, if peripheral is not supported
1642 * or peripheral is already registered with us.
1643 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301644 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001645 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301646 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301647 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301648
1649 return 0;
1650}
1651
Steve Mucklef132c6c2012-06-06 18:30:57 -07001652static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301653{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001654 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301655 struct msm_otg_platform_data *pdata = motg->pdata;
1656
1657 if (!otg->gadget)
1658 return;
1659
1660 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001661 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301662 /*
1663 * Some boards have a switch cotrolled by gpio
1664 * to enable/disable internal HUB. Disable internal
1665 * HUB before kicking the gadget.
1666 */
1667 if (pdata->setup_gpio)
1668 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001669
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301670 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautame3a39082013-06-11 10:42:56 +05301671 if (debug_bus_voting_enabled)
1672 msm_otg_bus_vote(motg, USB_MAX_PERF_VOTE);
1673
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301674 usb_gadget_vbus_connect(otg->gadget);
1675 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001676 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301677 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301678 /* Configure BUS performance parameters to default */
Manu Gautame3a39082013-06-11 10:42:56 +05301679 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301680 if (pdata->setup_gpio)
1681 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1682 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301683}
1684
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001685static int msm_otg_set_peripheral(struct usb_otg *otg,
Stephen Boyd9850acb2013-01-28 14:11:20 -08001686 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301687{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001688 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301689
1690 /*
1691 * Fail peripheral registration if this board can support
1692 * only host configuration.
1693 */
1694 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001695 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301696 return -ENODEV;
1697 }
1698
1699 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001700 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1701 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301702 msm_otg_start_peripheral(otg, 0);
1703 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001704 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301705 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301706 } else {
1707 otg->gadget = NULL;
1708 }
1709
1710 return 0;
1711 }
1712 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001713 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301714
1715 /*
1716 * Kick the state machine work, if host is not supported
1717 * or host is already registered with us.
1718 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301719 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001720 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301721 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301722 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301723
1724 return 0;
1725}
1726
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301727static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1728{
1729 unsigned long flags;
1730 int id;
1731
1732 if (!motg->pdata->pmic_id_irq)
1733 return -ENODEV;
1734
1735 local_irq_save(flags);
1736 id = irq_read_line(motg->pdata->pmic_id_irq);
1737 local_irq_restore(flags);
1738
1739 /*
1740 * If we can not read ID line state for some reason, treat
1741 * it as float. This would prevent MHL discovery and kicking
1742 * host mode unnecessarily.
1743 */
1744 return !!id;
1745}
1746
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301747static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1748 void (*callback)(int on))
1749{
1750 struct usb_phy *phy = &motg->phy;
1751 int ret;
1752
Manoj Raoa7bddd12012-08-27 20:36:45 -07001753 if (!motg->pdata->mhl_enable) {
1754 dev_dbg(phy->dev, "MHL feature not enabled\n");
1755 return -ENODEV;
1756 }
1757
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301758 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1759 !motg->pdata->pmic_id_irq) {
1760 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1761 return -ENODEV;
1762 }
1763
1764 if (!motg->pdata->mhl_dev_name) {
1765 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1766 return -ENODEV;
1767 }
1768
1769 if (callback)
1770 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1771 callback);
1772 else
1773 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1774
1775 if (ret)
1776 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1777 motg->pdata->mhl_dev_name, ret);
1778 else
1779 motg->mhl_enabled = true;
1780
1781 return ret;
1782}
1783
1784static void msm_otg_mhl_notify_online(int on)
1785{
1786 struct msm_otg *motg = the_msm_otg;
1787 struct usb_phy *phy = &motg->phy;
1788 bool queue = false;
1789
1790 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1791
1792 if (on) {
1793 set_bit(MHL, &motg->inputs);
1794 } else {
1795 clear_bit(MHL, &motg->inputs);
1796 queue = true;
1797 }
1798
1799 if (queue && phy->state != OTG_STATE_UNDEFINED)
1800 schedule_work(&motg->sm_work);
1801}
1802
1803static bool msm_otg_is_mhl(struct msm_otg *motg)
1804{
1805 struct usb_phy *phy = &motg->phy;
1806 int is_mhl, ret;
1807
1808 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1809 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1810 /*
1811 * MHL driver calls our callback saying that MHL connected
1812 * if RID_GND is detected. But at later part of discovery
1813 * it may figure out MHL is not connected and returns
1814 * false. Hence clear MHL input here.
1815 */
1816 clear_bit(MHL, &motg->inputs);
1817 dev_dbg(phy->dev, "MHL device not found\n");
1818 return false;
1819 }
1820
1821 set_bit(MHL, &motg->inputs);
1822 dev_dbg(phy->dev, "MHL device found\n");
1823 return true;
1824}
1825
1826static bool msm_chg_mhl_detect(struct msm_otg *motg)
1827{
1828 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301829
1830 if (!motg->mhl_enabled)
1831 return false;
1832
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301833 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301834
1835 if (id)
1836 return false;
1837
1838 mhl_det_in_progress = true;
1839 ret = msm_otg_is_mhl(motg);
1840 mhl_det_in_progress = false;
1841
1842 return ret;
1843}
1844
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301845static void msm_otg_chg_check_timer_func(unsigned long data)
1846{
1847 struct msm_otg *motg = (struct msm_otg *) data;
1848 struct usb_otg *otg = motg->phy.otg;
1849
1850 if (atomic_read(&motg->in_lpm) ||
1851 !test_bit(B_SESS_VLD, &motg->inputs) ||
1852 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1853 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1854 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1855 return;
1856 }
1857
1858 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1859 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1860 set_bit(B_FALSE_SDP, &motg->inputs);
1861 queue_work(system_nrt_wq, &motg->sm_work);
1862 }
1863}
1864
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001865static bool msm_chg_aca_detect(struct msm_otg *motg)
1866{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001867 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001868 u32 int_sts;
1869 bool ret = false;
1870
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301871 if (!aca_enabled())
1872 goto out;
1873
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001874 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1875 goto out;
1876
Steve Mucklef132c6c2012-06-06 18:30:57 -07001877 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001878 switch (int_sts & 0x1C) {
1879 case 0x08:
1880 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001881 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882 motg->chg_type = USB_ACA_A_CHARGER;
1883 motg->chg_state = USB_CHG_STATE_DETECTED;
1884 clear_bit(ID_B, &motg->inputs);
1885 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301886 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001887 ret = true;
1888 }
1889 break;
1890 case 0x0C:
1891 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001892 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 motg->chg_type = USB_ACA_B_CHARGER;
1894 motg->chg_state = USB_CHG_STATE_DETECTED;
1895 clear_bit(ID_A, &motg->inputs);
1896 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301897 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001898 ret = true;
1899 }
1900 break;
1901 case 0x10:
1902 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001903 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001904 motg->chg_type = USB_ACA_C_CHARGER;
1905 motg->chg_state = USB_CHG_STATE_DETECTED;
1906 clear_bit(ID_A, &motg->inputs);
1907 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301908 set_bit(ID, &motg->inputs);
1909 ret = true;
1910 }
1911 break;
1912 case 0x04:
1913 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001914 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301915 motg->chg_type = USB_INVALID_CHARGER;
1916 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1917 clear_bit(ID_A, &motg->inputs);
1918 clear_bit(ID_B, &motg->inputs);
1919 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001920 ret = true;
1921 }
1922 break;
1923 default:
1924 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1925 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301926 test_and_clear_bit(ID_C, &motg->inputs) |
1927 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001928 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001929 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930 motg->chg_type = USB_INVALID_CHARGER;
1931 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1932 }
1933 }
1934out:
1935 return ret;
1936}
1937
1938static void msm_chg_enable_aca_det(struct msm_otg *motg)
1939{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001940 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001941
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301942 if (!aca_enabled())
1943 return;
1944
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001945 switch (motg->pdata->phy_type) {
1946 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301947 /* Disable ID_GND in link and PHY */
1948 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1949 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001950 ulpi_write(phy, 0x01, 0x0C);
1951 ulpi_write(phy, 0x10, 0x0F);
1952 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301953 /* Disable PMIC ID pull-up */
1954 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301955 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001956 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301957 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001958 break;
1959 default:
1960 break;
1961 }
1962}
1963
1964static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1965{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001966 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001967
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301968 if (!aca_enabled())
1969 return;
1970
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001971 switch (motg->pdata->phy_type) {
1972 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301973 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001974 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301975 break;
1976 default:
1977 break;
1978 }
1979}
1980
1981static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1982{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001983 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301984
1985 if (!aca_enabled())
1986 return;
1987
1988 switch (motg->pdata->phy_type) {
1989 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001990 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001991 break;
1992 default:
1993 break;
1994 }
1995}
1996
1997static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1998{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001999 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002000 bool ret = false;
2001
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302002 if (!aca_enabled())
2003 return ret;
2004
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002005 switch (motg->pdata->phy_type) {
2006 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002007 if (ulpi_read(phy, 0x91) & 1) {
2008 dev_dbg(phy->dev, "RID change\n");
2009 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002010 ret = msm_chg_aca_detect(motg);
2011 }
2012 default:
2013 break;
2014 }
2015 return ret;
2016}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302017
2018static void msm_otg_id_timer_func(unsigned long data)
2019{
2020 struct msm_otg *motg = (struct msm_otg *) data;
2021
2022 if (!aca_enabled())
2023 return;
2024
2025 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002026 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302027 return;
2028 }
2029
Steve Mucklef132c6c2012-06-06 18:30:57 -07002030 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302031 goto out;
2032
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302033 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002034 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302035 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302036 }
2037
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302038out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302039 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
2040 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
2041}
2042
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302043static bool msm_chg_check_secondary_det(struct msm_otg *motg)
2044{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002045 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302046 u32 chg_det;
2047 bool ret = false;
2048
2049 switch (motg->pdata->phy_type) {
2050 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002051 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302052 ret = chg_det & (1 << 4);
2053 break;
2054 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002055 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302056 ret = chg_det & 1;
2057 break;
2058 default:
2059 break;
2060 }
2061 return ret;
2062}
2063
2064static void msm_chg_enable_secondary_det(struct msm_otg *motg)
2065{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002066 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302067 u32 chg_det;
2068
2069 switch (motg->pdata->phy_type) {
2070 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002071 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302072 /* Turn off charger block */
2073 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002074 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302075 udelay(20);
2076 /* control chg block via ULPI */
2077 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002078 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302079 /* put it in host mode for enabling D- source */
2080 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002081 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302082 /* Turn on chg detect block */
2083 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002084 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302085 udelay(20);
2086 /* enable chg detection */
2087 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002088 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302089 break;
2090 case SNPS_28NM_INTEGRATED_PHY:
2091 /*
2092 * Configure DM as current source, DP as current sink
2093 * and enable battery charging comparators.
2094 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002095 ulpi_write(phy, 0x8, 0x85);
2096 ulpi_write(phy, 0x2, 0x85);
2097 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302098 break;
2099 default:
2100 break;
2101 }
2102}
2103
2104static bool msm_chg_check_primary_det(struct msm_otg *motg)
2105{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002106 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302107 u32 chg_det;
2108 bool ret = false;
2109
2110 switch (motg->pdata->phy_type) {
2111 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002112 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302113 ret = chg_det & (1 << 4);
2114 break;
2115 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002116 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302117 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302118 /* Turn off VDP_SRC */
2119 ulpi_write(phy, 0x3, 0x86);
2120 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302121 break;
2122 default:
2123 break;
2124 }
2125 return ret;
2126}
2127
2128static void msm_chg_enable_primary_det(struct msm_otg *motg)
2129{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002130 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302131 u32 chg_det;
2132
2133 switch (motg->pdata->phy_type) {
2134 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002135 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302136 /* enable chg detection */
2137 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002138 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302139 break;
2140 case SNPS_28NM_INTEGRATED_PHY:
2141 /*
2142 * Configure DP as current source, DM as current sink
2143 * and enable battery charging comparators.
2144 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002145 ulpi_write(phy, 0x2, 0x85);
2146 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302147 break;
2148 default:
2149 break;
2150 }
2151}
2152
2153static bool msm_chg_check_dcd(struct msm_otg *motg)
2154{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002155 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302156 u32 line_state;
2157 bool ret = false;
2158
2159 switch (motg->pdata->phy_type) {
2160 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002161 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302162 ret = !(line_state & 1);
2163 break;
2164 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002165 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302166 ret = line_state & 2;
2167 break;
2168 default:
2169 break;
2170 }
2171 return ret;
2172}
2173
2174static void msm_chg_disable_dcd(struct msm_otg *motg)
2175{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002176 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302177 u32 chg_det;
2178
2179 switch (motg->pdata->phy_type) {
2180 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002181 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302182 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002183 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302184 break;
2185 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002186 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302187 break;
2188 default:
2189 break;
2190 }
2191}
2192
2193static void msm_chg_enable_dcd(struct msm_otg *motg)
2194{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002195 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302196 u32 chg_det;
2197
2198 switch (motg->pdata->phy_type) {
2199 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002200 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302201 /* Turn on D+ current source */
2202 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002203 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302204 break;
2205 case SNPS_28NM_INTEGRATED_PHY:
2206 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002207 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302208 break;
2209 default:
2210 break;
2211 }
2212}
2213
2214static void msm_chg_block_on(struct msm_otg *motg)
2215{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002216 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302217 u32 func_ctrl, chg_det;
2218
2219 /* put the controller in non-driving 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_NONDRIVING;
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 switch (motg->pdata->phy_type) {
2226 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002227 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302228 /* control chg block via ULPI */
2229 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002230 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302231 /* Turn on chg detect block */
2232 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002233 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302234 udelay(20);
2235 break;
2236 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302237 /* disable DP and DM pull down resistors */
2238 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302239 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002240 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302241 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002242 ulpi_write(phy, 0x1F, 0x92);
2243 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302244 udelay(100);
2245 break;
2246 default:
2247 break;
2248 }
2249}
2250
2251static void msm_chg_block_off(struct msm_otg *motg)
2252{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002253 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302254 u32 func_ctrl, chg_det;
2255
2256 switch (motg->pdata->phy_type) {
2257 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002258 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302259 /* Turn off charger block */
2260 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002261 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302262 break;
2263 case SNPS_28NM_INTEGRATED_PHY:
2264 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002265 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302266 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002267 ulpi_write(phy, 0x1F, 0x92);
2268 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetic4c69762013-07-08 15:03:26 +05302269 /* re-enable DP and DM pull down resistors */
2270 ulpi_write(phy, 0x6, 0xB);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302271 break;
2272 default:
2273 break;
2274 }
2275
2276 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002277 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302278 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2279 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002280 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302281}
2282
Anji jonnalad270e2d2011-08-09 11:28:32 +05302283static const char *chg_to_string(enum usb_chg_type chg_type)
2284{
2285 switch (chg_type) {
2286 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2287 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2288 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2289 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2290 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2291 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2292 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302293 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
zhenhuahb6d5edc2013-06-19 14:37:32 +08002294 case USB_FLOATED_CHARGER: return "USB_FLOATED_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302295 default: return "INVALID_CHARGER";
2296 }
2297}
2298
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302299#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2300#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302301#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2302#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302303static void msm_chg_detect_work(struct work_struct *w)
2304{
2305 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002306 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302307 bool is_dcd = false, tmout, vout, is_aca;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002308 static bool dcd;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302309 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302310 unsigned long delay;
2311
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002312 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302313
2314 if (test_bit(MHL, &motg->inputs)) {
2315 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2316 return;
2317 }
2318
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302319 switch (motg->chg_state) {
2320 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302321 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302322 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002323 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302324 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302325 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302326 delay = MSM_CHG_DCD_POLL_TIME;
2327 break;
2328 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302329 if (msm_chg_mhl_detect(motg)) {
2330 msm_chg_block_off(motg);
2331 motg->chg_state = USB_CHG_STATE_DETECTED;
2332 motg->chg_type = USB_INVALID_CHARGER;
2333 queue_work(system_nrt_wq, &motg->sm_work);
2334 return;
2335 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002336 is_aca = msm_chg_aca_detect(motg);
2337 if (is_aca) {
2338 /*
2339 * ID_A can be ACA dock too. continue
2340 * primary detection after DCD.
2341 */
2342 if (test_bit(ID_A, &motg->inputs)) {
2343 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2344 } else {
2345 delay = 0;
2346 break;
2347 }
2348 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302349 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302350 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2351 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302352 if (is_dcd || tmout) {
zhenhuahb6d5edc2013-06-19 14:37:32 +08002353 if (is_dcd)
2354 dcd = true;
2355 else
2356 dcd = false;
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302357 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302358 msm_chg_enable_primary_det(motg);
2359 delay = MSM_CHG_PRIMARY_DET_TIME;
2360 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2361 } else {
2362 delay = MSM_CHG_DCD_POLL_TIME;
2363 }
2364 break;
2365 case USB_CHG_STATE_DCD_DONE:
2366 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302367 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2368 dm_vlgc = line_state & PORTSC_LS_DM;
2369 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302370 if (test_bit(ID_A, &motg->inputs)) {
2371 motg->chg_type = USB_ACA_DOCK_CHARGER;
2372 motg->chg_state = USB_CHG_STATE_DETECTED;
2373 delay = 0;
2374 break;
2375 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302376 if (line_state) { /* DP > VLGC */
2377 motg->chg_type = USB_PROPRIETARY_CHARGER;
2378 motg->chg_state = USB_CHG_STATE_DETECTED;
2379 delay = 0;
2380 } else {
2381 msm_chg_enable_secondary_det(motg);
2382 delay = MSM_CHG_SECONDARY_DET_TIME;
2383 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2384 }
2385 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302386 if (test_bit(ID_A, &motg->inputs)) {
2387 motg->chg_type = USB_ACA_A_CHARGER;
2388 motg->chg_state = USB_CHG_STATE_DETECTED;
2389 delay = 0;
2390 break;
2391 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302392
2393 if (line_state) /* DP > VLGC or/and DM > VLGC */
2394 motg->chg_type = USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002395 else if (!dcd && floated_charger_enable)
2396 motg->chg_type = USB_FLOATED_CHARGER;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302397 else
2398 motg->chg_type = USB_SDP_CHARGER;
2399
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302400 motg->chg_state = USB_CHG_STATE_DETECTED;
2401 delay = 0;
2402 }
2403 break;
2404 case USB_CHG_STATE_PRIMARY_DONE:
2405 vout = msm_chg_check_secondary_det(motg);
2406 if (vout)
2407 motg->chg_type = USB_DCP_CHARGER;
2408 else
2409 motg->chg_type = USB_CDP_CHARGER;
2410 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2411 /* fall through */
2412 case USB_CHG_STATE_SECONDARY_DONE:
2413 motg->chg_state = USB_CHG_STATE_DETECTED;
2414 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302415 /*
2416 * Notify the charger type to power supply
2417 * owner as soon as we determine the charger.
2418 */
2419 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302420 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002421 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302422 /*
2423 * Spurious interrupt is seen after enabling ACA detection
2424 * due to which charger detection fails in case of PET.
2425 * Add delay of 100 microsec to avoid that.
2426 */
2427 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002428 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002429 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302430 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302431 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302432 return;
2433 default:
2434 return;
2435 }
2436
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302437 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302438}
2439
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302440/*
2441 * We support OTG, Peripheral only and Host only configurations. In case
2442 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2443 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2444 * enabled when switch is controlled by user and default mode is supplied
2445 * by board file, which can be changed by userspace later.
2446 */
2447static void msm_otg_init_sm(struct msm_otg *motg)
2448{
2449 struct msm_otg_platform_data *pdata = motg->pdata;
2450 u32 otgsc = readl(USB_OTGSC);
2451
2452 switch (pdata->mode) {
2453 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002454 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302455 if (pdata->default_mode == USB_HOST) {
2456 clear_bit(ID, &motg->inputs);
2457 } else if (pdata->default_mode == USB_PERIPHERAL) {
2458 set_bit(ID, &motg->inputs);
2459 set_bit(B_SESS_VLD, &motg->inputs);
2460 } else {
2461 set_bit(ID, &motg->inputs);
2462 clear_bit(B_SESS_VLD, &motg->inputs);
2463 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302464 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302465 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302466 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302467 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302468 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302469 set_bit(A_BUS_REQ, &motg->inputs);
2470 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002471 if (otgsc & OTGSC_BSV)
2472 set_bit(B_SESS_VLD, &motg->inputs);
2473 else
2474 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302475 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302476 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302477 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302478 set_bit(ID, &motg->inputs);
2479 else
2480 clear_bit(ID, &motg->inputs);
2481 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302482 /*
2483 * VBUS initial state is reported after PMIC
2484 * driver initialization. Wait for it.
2485 */
2486 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302487 }
2488 break;
2489 case USB_HOST:
2490 clear_bit(ID, &motg->inputs);
2491 break;
2492 case USB_PERIPHERAL:
2493 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302494 if (pdata->otg_control == OTG_PHY_CONTROL) {
2495 if (otgsc & OTGSC_BSV)
2496 set_bit(B_SESS_VLD, &motg->inputs);
2497 else
2498 clear_bit(B_SESS_VLD, &motg->inputs);
2499 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2500 /*
2501 * VBUS initial state is reported after PMIC
2502 * driver initialization. Wait for it.
2503 */
2504 wait_for_completion(&pmic_vbus_init);
2505 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302506 break;
2507 default:
2508 break;
2509 }
2510}
2511
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302512static void msm_otg_wait_for_ext_chg_done(struct msm_otg *motg)
2513{
2514 struct usb_phy *phy = &motg->phy;
2515 unsigned long t;
2516
2517 /*
2518 * Defer next cable connect event till external charger
2519 * detection is completed.
2520 */
2521
2522 if (motg->ext_chg_active) {
2523
2524 pr_debug("before msm_otg ext chg wait\n");
2525
2526 t = wait_for_completion_timeout(&motg->ext_chg_wait,
2527 msecs_to_jiffies(3000));
2528 if (!t)
2529 pr_err("msm_otg ext chg wait timeout\n");
2530 else
2531 pr_debug("msm_otg ext chg wait done\n");
2532 }
2533
2534 if (motg->ext_chg_opened) {
2535 if (phy->flags & ENABLE_DP_MANUAL_PULLUP) {
2536 ulpi_write(phy, ULPI_MISC_A_VBUSVLDEXT |
2537 ULPI_MISC_A_VBUSVLDEXTSEL,
2538 ULPI_CLR(ULPI_MISC_A));
2539 }
2540 /* clear charging register bits */
2541 ulpi_write(phy, 0x3F, 0x86);
2542 /* re-enable DP and DM pull-down resistors*/
2543 ulpi_write(phy, 0x6, 0xB);
2544 }
2545}
2546
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302547static void msm_otg_sm_work(struct work_struct *w)
2548{
2549 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002550 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302551 bool work = 0, srp_reqd, dcp;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302552
Steve Mucklef132c6c2012-06-06 18:30:57 -07002553 pm_runtime_resume(otg->phy->dev);
2554 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002555 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302556 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002557 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302558 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002559 if (!psy && legacy_power_supply) {
2560 psy = power_supply_get_by_name("usb");
2561
2562 if (!psy)
2563 pr_err("couldn't get usb power supply\n");
2564 }
2565
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002566 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302567 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2568 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002569 pm_runtime_put_noidle(otg->phy->dev);
2570 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302571 break;
2572 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302573 /* FALL THROUGH */
2574 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302575 if (test_bit(MHL, &motg->inputs)) {
2576 /* allow LPM */
2577 pm_runtime_put_noidle(otg->phy->dev);
2578 pm_runtime_suspend(otg->phy->dev);
2579 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002580 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302581 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302582 if (msm_chg_mhl_detect(motg)) {
2583 work = 1;
2584 break;
2585 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302586 clear_bit(B_BUS_REQ, &motg->inputs);
2587 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002588 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302589 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302590 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302591 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302592 switch (motg->chg_state) {
2593 case USB_CHG_STATE_UNDEFINED:
2594 msm_chg_detect_work(&motg->chg_work.work);
2595 break;
2596 case USB_CHG_STATE_DETECTED:
2597 switch (motg->chg_type) {
2598 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302599 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002600 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302601 if (motg->ext_chg_opened) {
2602 init_completion(
2603 &motg->ext_chg_wait);
2604 motg->ext_chg_active = true;
2605 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302606 /* fall through */
2607 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302608 msm_otg_notify_charger(motg,
2609 IDEV_CHG_MAX);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302610 pm_runtime_put_sync(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302611 break;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002612 case USB_FLOATED_CHARGER:
2613 msm_otg_notify_charger(motg,
2614 IDEV_CHG_MAX);
2615 pm_runtime_put_noidle(otg->phy->dev);
2616 pm_runtime_suspend(otg->phy->dev);
2617 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302618 case USB_ACA_B_CHARGER:
2619 msm_otg_notify_charger(motg,
2620 IDEV_ACA_CHG_MAX);
2621 /*
2622 * (ID_B --> ID_C) PHY_ALT interrupt can
2623 * not be detected in LPM.
2624 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302625 break;
2626 case USB_CDP_CHARGER:
2627 msm_otg_notify_charger(motg,
2628 IDEV_CHG_MAX);
2629 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002630 otg->phy->state =
2631 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302632 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302633 case USB_ACA_C_CHARGER:
2634 msm_otg_notify_charger(motg,
2635 IDEV_ACA_CHG_MAX);
2636 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002637 otg->phy->state =
2638 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302639 break;
2640 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302641 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002642 otg->phy->state =
2643 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302644 mod_timer(&motg->chg_check_timer,
2645 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302646 break;
2647 default:
2648 break;
2649 }
2650 break;
2651 default:
2652 break;
2653 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302654 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2655 pr_debug("b_sess_end && b_bus_req\n");
2656 if (msm_otg_start_srp(otg) < 0) {
2657 clear_bit(B_BUS_REQ, &motg->inputs);
2658 work = 1;
2659 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302660 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002661 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302662 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2663 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302664 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302665 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302666 del_timer_sync(&motg->chg_check_timer);
2667 clear_bit(B_FALSE_SDP, &motg->inputs);
Mayank Rana8ab00352013-01-23 19:26:21 +05302668 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302669 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302670 dcp = (motg->chg_type == USB_DCP_CHARGER);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302671 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2672 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302673 msm_otg_notify_charger(motg, 0);
Manu Gautam6604b682013-09-04 17:50:17 +05302674 if (dcp) {
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05302675 msm_otg_wait_for_ext_chg_done(motg);
Manu Gautam6604b682013-09-04 17:50:17 +05302676 /* Turn off VDP_SRC */
2677 ulpi_write(otg->phy, 0x2, 0x86);
2678 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002679 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302680 /*
2681 * There is a small window where ID interrupt
2682 * is not monitored during ID detection circuit
2683 * switch from ACA to PMIC. Check ID state
2684 * before entering into low power mode.
2685 */
2686 if (!msm_otg_read_pmic_id_state(motg)) {
2687 pr_debug("process missed ID intr\n");
2688 clear_bit(ID, &motg->inputs);
2689 work = 1;
2690 break;
2691 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002692 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002693 /*
2694 * Only if autosuspend was enabled in probe, it will be
2695 * used here. Otherwise, no delay will be used.
2696 */
2697 pm_runtime_mark_last_busy(otg->phy->dev);
2698 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302699 }
2700 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302701 case OTG_STATE_B_SRP_INIT:
2702 if (!test_bit(ID, &motg->inputs) ||
2703 test_bit(ID_A, &motg->inputs) ||
2704 test_bit(ID_C, &motg->inputs) ||
2705 (test_bit(B_SESS_VLD, &motg->inputs) &&
2706 !test_bit(ID_B, &motg->inputs))) {
2707 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2708 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002709 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302710 /*
2711 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2712 * bits after SRP initiation.
2713 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002714 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302715 work = 1;
2716 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2717 pr_debug("b_srp_fail\n");
2718 pr_info("A-device did not respond to SRP\n");
2719 clear_bit(B_BUS_REQ, &motg->inputs);
2720 clear_bit(B_SRP_FAIL, &motg->tmouts);
2721 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002722 ulpi_write(otg->phy, 0x0, 0x98);
2723 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302724 motg->b_last_se0_sess = jiffies;
2725 work = 1;
2726 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302727 break;
2728 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302729 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2730 test_bit(B_FALSE_SDP, &motg->inputs)) {
2731 pr_debug("B_FALSE_SDP\n");
2732 msm_otg_start_peripheral(otg, 0);
2733 motg->chg_type = USB_DCP_CHARGER;
2734 clear_bit(B_FALSE_SDP, &motg->inputs);
2735 otg->phy->state = OTG_STATE_B_IDLE;
2736 work = 1;
2737 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302738 test_bit(ID_A, &motg->inputs) ||
2739 test_bit(ID_B, &motg->inputs) ||
2740 !test_bit(B_SESS_VLD, &motg->inputs)) {
2741 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302742 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2743 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302744 msm_otg_notify_charger(motg, 0);
2745 srp_reqd = otg->gadget->otg_srp_reqd;
2746 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302747 if (test_bit(ID_B, &motg->inputs))
2748 clear_bit(ID_B, &motg->inputs);
2749 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002750 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302751 motg->b_last_se0_sess = jiffies;
2752 if (srp_reqd)
2753 msm_otg_start_timer(motg,
2754 TB_TST_SRP, B_TST_SRP);
2755 else
2756 work = 1;
2757 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2758 otg->gadget->b_hnp_enable &&
2759 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2760 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2761 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2762 /* D+ pullup should not be disconnected within 4msec
2763 * after A device suspends the bus. Otherwise PET will
2764 * fail the compliance test.
2765 */
2766 udelay(1000);
2767 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002768 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302769 /*
2770 * start HCD even before A-device enable
2771 * pull-up to meet HNP timings.
2772 */
2773 otg->host->is_b_host = 1;
2774 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002775 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2776 test_bit(B_SESS_VLD, &motg->inputs)) {
2777 pr_debug("a_bus_suspend && b_sess_vld\n");
2778 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002779 pm_runtime_put_noidle(otg->phy->dev);
2780 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002781 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002782 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302783 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002784 }
2785 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302786 case OTG_STATE_B_WAIT_ACON:
2787 if (!test_bit(ID, &motg->inputs) ||
2788 test_bit(ID_A, &motg->inputs) ||
2789 test_bit(ID_B, &motg->inputs) ||
2790 !test_bit(B_SESS_VLD, &motg->inputs)) {
2791 pr_debug("!id || id_a/b || !b_sess_vld\n");
2792 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302793 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302794 * A-device is physically disconnected during
2795 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302796 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302797 msm_otg_start_host(otg, 0);
2798 otg->host->is_b_host = 0;
2799
2800 clear_bit(B_BUS_REQ, &motg->inputs);
2801 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2802 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002803 otg->phy->state = OTG_STATE_B_IDLE;
2804 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302805 work = 1;
2806 } else if (test_bit(A_CONN, &motg->inputs)) {
2807 pr_debug("a_conn\n");
2808 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002809 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302810 /*
2811 * PET disconnects D+ pullup after reset is generated
2812 * by B device in B_HOST role which is not detected by
2813 * B device. As workaorund , start timer of 300msec
2814 * and stop timer if A device is enumerated else clear
2815 * A_CONN.
2816 */
2817 msm_otg_start_timer(motg, TB_TST_CONFIG,
2818 B_TST_CONFIG);
2819 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2820 pr_debug("b_ase0_brst_tmout\n");
2821 pr_info("B HNP fail:No response from A device\n");
2822 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002823 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302824 otg->host->is_b_host = 0;
2825 clear_bit(B_ASE0_BRST, &motg->tmouts);
2826 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2827 clear_bit(B_BUS_REQ, &motg->inputs);
2828 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002829 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302830 work = 1;
2831 } else if (test_bit(ID_C, &motg->inputs)) {
2832 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2833 }
2834 break;
2835 case OTG_STATE_B_HOST:
2836 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2837 !test_bit(A_CONN, &motg->inputs) ||
2838 !test_bit(B_SESS_VLD, &motg->inputs)) {
2839 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2840 clear_bit(A_CONN, &motg->inputs);
2841 clear_bit(B_BUS_REQ, &motg->inputs);
2842 msm_otg_start_host(otg, 0);
2843 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002844 otg->phy->state = OTG_STATE_B_IDLE;
2845 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302846 work = 1;
2847 } else if (test_bit(ID_C, &motg->inputs)) {
2848 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2849 }
2850 break;
2851 case OTG_STATE_A_IDLE:
2852 otg->default_a = 1;
2853 if (test_bit(ID, &motg->inputs) &&
2854 !test_bit(ID_A, &motg->inputs)) {
2855 pr_debug("id && !id_a\n");
2856 otg->default_a = 0;
2857 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002858 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302859 del_timer_sync(&motg->id_timer);
2860 msm_otg_link_reset(motg);
2861 msm_chg_enable_aca_intr(motg);
2862 msm_otg_notify_charger(motg, 0);
2863 work = 1;
2864 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2865 (test_bit(A_SRP_DET, &motg->inputs) ||
2866 test_bit(A_BUS_REQ, &motg->inputs))) {
2867 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2868
2869 clear_bit(A_SRP_DET, &motg->inputs);
2870 /* Disable SRP detection */
2871 writel_relaxed((readl_relaxed(USB_OTGSC) &
2872 ~OTGSC_INTSTS_MASK) &
2873 ~OTGSC_DPIE, USB_OTGSC);
2874
Steve Mucklef132c6c2012-06-06 18:30:57 -07002875 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302876 /* VBUS should not be supplied before end of SRP pulse
2877 * generated by PET, if not complaince test fail.
2878 */
2879 usleep_range(10000, 12000);
2880 /* ACA: ID_A: Stop charging untill enumeration */
2881 if (test_bit(ID_A, &motg->inputs))
2882 msm_otg_notify_charger(motg, 0);
2883 else
2884 msm_hsusb_vbus_power(motg, 1);
2885 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2886 } else {
2887 pr_debug("No session requested\n");
2888 clear_bit(A_BUS_DROP, &motg->inputs);
2889 if (test_bit(ID_A, &motg->inputs)) {
2890 msm_otg_notify_charger(motg,
2891 IDEV_ACA_CHG_MAX);
2892 } else if (!test_bit(ID, &motg->inputs)) {
2893 msm_otg_notify_charger(motg, 0);
2894 /*
2895 * A-device is not providing power on VBUS.
2896 * Enable SRP detection.
2897 */
2898 writel_relaxed(0x13, USB_USBMODE);
2899 writel_relaxed((readl_relaxed(USB_OTGSC) &
2900 ~OTGSC_INTSTS_MASK) |
2901 OTGSC_DPIE, USB_OTGSC);
2902 mb();
2903 }
2904 }
2905 break;
2906 case OTG_STATE_A_WAIT_VRISE:
2907 if ((test_bit(ID, &motg->inputs) &&
2908 !test_bit(ID_A, &motg->inputs)) ||
2909 test_bit(A_BUS_DROP, &motg->inputs) ||
2910 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2911 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2912 clear_bit(A_BUS_REQ, &motg->inputs);
2913 msm_otg_del_timer(motg);
2914 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002915 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302916 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2917 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2918 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002919 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302920 if (TA_WAIT_BCON > 0)
2921 msm_otg_start_timer(motg, TA_WAIT_BCON,
2922 A_WAIT_BCON);
Sujeet Kumarebaef0c2013-06-03 15:54:25 +05302923
2924 /* Clear BSV in host mode */
2925 clear_bit(B_SESS_VLD, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302926 msm_otg_start_host(otg, 1);
2927 msm_chg_enable_aca_det(motg);
2928 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302929 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302930 if (msm_chg_check_aca_intr(motg))
2931 work = 1;
2932 }
2933 break;
2934 case OTG_STATE_A_WAIT_BCON:
2935 if ((test_bit(ID, &motg->inputs) &&
2936 !test_bit(ID_A, &motg->inputs)) ||
2937 test_bit(A_BUS_DROP, &motg->inputs) ||
2938 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2939 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2940 "a_wait_bcon_tmout\n");
2941 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2942 pr_info("Device No Response\n");
2943 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2944 }
2945 msm_otg_del_timer(motg);
2946 clear_bit(A_BUS_REQ, &motg->inputs);
2947 clear_bit(B_CONN, &motg->inputs);
2948 msm_otg_start_host(otg, 0);
2949 /*
2950 * ACA: ID_A with NO accessory, just the A plug is
2951 * attached to ACA: Use IDCHG_MAX for charging
2952 */
2953 if (test_bit(ID_A, &motg->inputs))
2954 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2955 else
2956 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002957 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302958 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2959 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2960 pr_debug("!a_vbus_vld\n");
2961 clear_bit(B_CONN, &motg->inputs);
2962 msm_otg_del_timer(motg);
2963 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002964 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2965 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302966 } else if (test_bit(ID_A, &motg->inputs)) {
2967 msm_hsusb_vbus_power(motg, 0);
2968 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2969 /*
2970 * If TA_WAIT_BCON is infinite, we don;t
2971 * turn off VBUS. Enter low power mode.
2972 */
2973 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002974 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302975 } else if (!test_bit(ID, &motg->inputs)) {
2976 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302977 }
2978 break;
2979 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302980 if ((test_bit(ID, &motg->inputs) &&
2981 !test_bit(ID_A, &motg->inputs)) ||
2982 test_bit(A_BUS_DROP, &motg->inputs)) {
2983 pr_debug("id_a/b/c || a_bus_drop\n");
2984 clear_bit(B_CONN, &motg->inputs);
2985 clear_bit(A_BUS_REQ, &motg->inputs);
2986 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002987 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302988 msm_otg_start_host(otg, 0);
2989 if (!test_bit(ID_A, &motg->inputs))
2990 msm_hsusb_vbus_power(motg, 0);
2991 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2992 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2993 pr_debug("!a_vbus_vld\n");
2994 clear_bit(B_CONN, &motg->inputs);
2995 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002996 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302997 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002998 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302999 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
3000 /*
3001 * a_bus_req is de-asserted when root hub is
3002 * suspended or HNP is in progress.
3003 */
3004 pr_debug("!a_bus_req\n");
3005 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003006 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303007 if (otg->host->b_hnp_enable)
3008 msm_otg_start_timer(motg, TA_AIDL_BDIS,
3009 A_AIDL_BDIS);
3010 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07003011 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303012 } else if (!test_bit(B_CONN, &motg->inputs)) {
3013 pr_debug("!b_conn\n");
3014 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003015 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303016 if (TA_WAIT_BCON > 0)
3017 msm_otg_start_timer(motg, TA_WAIT_BCON,
3018 A_WAIT_BCON);
3019 if (msm_chg_check_aca_intr(motg))
3020 work = 1;
3021 } else if (test_bit(ID_A, &motg->inputs)) {
3022 msm_otg_del_timer(motg);
3023 msm_hsusb_vbus_power(motg, 0);
3024 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
3025 msm_otg_notify_charger(motg,
3026 IDEV_ACA_CHG_MAX);
3027 else
3028 msm_otg_notify_charger(motg,
3029 IDEV_CHG_MIN - motg->mA_port);
3030 } else if (!test_bit(ID, &motg->inputs)) {
3031 motg->chg_state = USB_CHG_STATE_UNDEFINED;
3032 motg->chg_type = USB_INVALID_CHARGER;
3033 msm_otg_notify_charger(motg, 0);
3034 msm_hsusb_vbus_power(motg, 1);
3035 }
3036 break;
3037 case OTG_STATE_A_SUSPEND:
3038 if ((test_bit(ID, &motg->inputs) &&
3039 !test_bit(ID_A, &motg->inputs)) ||
3040 test_bit(A_BUS_DROP, &motg->inputs) ||
3041 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
3042 pr_debug("id_a/b/c || a_bus_drop ||"
3043 "a_aidl_bdis_tmout\n");
3044 msm_otg_del_timer(motg);
3045 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003046 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303047 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003048 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303049 if (!test_bit(ID_A, &motg->inputs))
3050 msm_hsusb_vbus_power(motg, 0);
3051 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3052 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
3053 pr_debug("!a_vbus_vld\n");
3054 msm_otg_del_timer(motg);
3055 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003056 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303057 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003058 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303059 } else if (!test_bit(B_CONN, &motg->inputs) &&
3060 otg->host->b_hnp_enable) {
3061 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003062 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303063 msm_otg_host_hnp_enable(otg, 1);
3064 otg->gadget->is_a_peripheral = 1;
3065 msm_otg_start_peripheral(otg, 1);
3066 } else if (!test_bit(B_CONN, &motg->inputs) &&
3067 !otg->host->b_hnp_enable) {
3068 pr_debug("!b_conn && !b_hnp_enable");
3069 /*
3070 * bus request is dropped during suspend.
3071 * acquire again for next device.
3072 */
3073 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003074 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303075 if (TA_WAIT_BCON > 0)
3076 msm_otg_start_timer(motg, TA_WAIT_BCON,
3077 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003078 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05303079 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003080 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303081 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003082 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003083 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05303084 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303085 }
3086 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303087 case OTG_STATE_A_PERIPHERAL:
3088 if ((test_bit(ID, &motg->inputs) &&
3089 !test_bit(ID_A, &motg->inputs)) ||
3090 test_bit(A_BUS_DROP, &motg->inputs)) {
3091 pr_debug("id _f/b/c || a_bus_drop\n");
3092 /* Clear BIDL_ADIS timer */
3093 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003094 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303095 msm_otg_start_peripheral(otg, 0);
3096 otg->gadget->is_a_peripheral = 0;
3097 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003098 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303099 if (!test_bit(ID_A, &motg->inputs))
3100 msm_hsusb_vbus_power(motg, 0);
3101 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3102 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
3103 pr_debug("!a_vbus_vld\n");
3104 /* Clear BIDL_ADIS timer */
3105 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003106 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303107 msm_otg_start_peripheral(otg, 0);
3108 otg->gadget->is_a_peripheral = 0;
3109 msm_otg_start_host(otg, 0);
3110 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
3111 pr_debug("a_bidl_adis_tmout\n");
3112 msm_otg_start_peripheral(otg, 0);
3113 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003114 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303115 set_bit(A_BUS_REQ, &motg->inputs);
3116 msm_otg_host_hnp_enable(otg, 0);
3117 if (TA_WAIT_BCON > 0)
3118 msm_otg_start_timer(motg, TA_WAIT_BCON,
3119 A_WAIT_BCON);
3120 } else if (test_bit(ID_A, &motg->inputs)) {
3121 msm_hsusb_vbus_power(motg, 0);
3122 msm_otg_notify_charger(motg,
3123 IDEV_CHG_MIN - motg->mA_port);
3124 } else if (!test_bit(ID, &motg->inputs)) {
3125 msm_otg_notify_charger(motg, 0);
3126 msm_hsusb_vbus_power(motg, 1);
3127 }
3128 break;
3129 case OTG_STATE_A_WAIT_VFALL:
3130 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
3131 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003132 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303133 work = 1;
3134 }
3135 break;
3136 case OTG_STATE_A_VBUS_ERR:
3137 if ((test_bit(ID, &motg->inputs) &&
3138 !test_bit(ID_A, &motg->inputs)) ||
3139 test_bit(A_BUS_DROP, &motg->inputs) ||
3140 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003141 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303142 if (!test_bit(ID_A, &motg->inputs))
3143 msm_hsusb_vbus_power(motg, 0);
3144 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3145 motg->chg_state = USB_CHG_STATE_UNDEFINED;
3146 motg->chg_type = USB_INVALID_CHARGER;
3147 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303148 }
3149 break;
3150 default:
3151 break;
3152 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303153 if (work)
3154 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303155}
3156
Amit Blayd0fe07b2012-09-05 16:42:09 +03003157static void msm_otg_suspend_work(struct work_struct *w)
3158{
3159 struct msm_otg *motg =
3160 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05303161
3162 /* This work is only for device bus suspend */
3163 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
3164 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003165}
3166
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303167static irqreturn_t msm_otg_irq(int irq, void *data)
3168{
3169 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003170 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303171 u32 otgsc = 0, usbsts, pc;
3172 bool work = 0;
3173 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303174
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303175 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07003176 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303177 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07003178 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07003179 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07003180 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303181 return IRQ_HANDLED;
3182 }
3183
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003184 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303185 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303186
3187 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303188 return IRQ_NONE;
3189
3190 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303191 if (otgsc & OTGSC_ID) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003192 dev_dbg(otg->phy->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303193 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303194 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003195 dev_dbg(otg->phy->dev, "ID clear\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303196 /*
3197 * Assert a_bus_req to supply power on
3198 * VBUS when Micro/Mini-A cable is connected
3199 * with out user intervention.
3200 */
3201 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303202 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303203 msm_chg_enable_aca_det(motg);
3204 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303205 writel_relaxed(otgsc, USB_OTGSC);
3206 work = 1;
3207 } else if (otgsc & OTGSC_DPIS) {
3208 pr_debug("DPIS detected\n");
3209 writel_relaxed(otgsc, USB_OTGSC);
3210 set_bit(A_SRP_DET, &motg->inputs);
3211 set_bit(A_BUS_REQ, &motg->inputs);
3212 work = 1;
Vamsi Krishnaef6e1bf2013-03-02 15:36:17 -08003213 } else if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303214 writel_relaxed(otgsc, USB_OTGSC);
3215 /*
3216 * BSV interrupt comes when operating as an A-device
3217 * (VBUS on/off).
3218 * But, handle BSV when charger is removed from ACA in ID_A
3219 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003220 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303221 !test_bit(ID_A, &motg->inputs))
3222 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303223 if (otgsc & OTGSC_BSV) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003224 dev_dbg(otg->phy->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303225 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303226 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003227 dev_dbg(otg->phy->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303228 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003229 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3230
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303231 msm_chg_check_aca_intr(motg);
3232 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303233 work = 1;
3234 } else if (usbsts & STS_PCI) {
3235 pc = readl_relaxed(USB_PORTSC);
3236 pr_debug("portsc = %x\n", pc);
3237 ret = IRQ_NONE;
3238 /*
3239 * HCD Acks PCI interrupt. We use this to switch
3240 * between different OTG states.
3241 */
3242 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003243 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303244 case OTG_STATE_A_SUSPEND:
3245 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3246 !(pc & PORTSC_CCS)) {
3247 pr_debug("B_CONN clear\n");
3248 clear_bit(B_CONN, &motg->inputs);
3249 msm_otg_del_timer(motg);
3250 }
3251 break;
3252 case OTG_STATE_A_PERIPHERAL:
3253 /*
3254 * A-peripheral observed activity on bus.
3255 * clear A_BIDL_ADIS timer.
3256 */
3257 msm_otg_del_timer(motg);
3258 work = 0;
3259 break;
3260 case OTG_STATE_B_WAIT_ACON:
3261 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3262 pr_debug("A_CONN set\n");
3263 set_bit(A_CONN, &motg->inputs);
3264 /* Clear ASE0_BRST timer */
3265 msm_otg_del_timer(motg);
3266 }
3267 break;
3268 case OTG_STATE_B_HOST:
3269 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3270 pr_debug("A_CONN clear\n");
3271 clear_bit(A_CONN, &motg->inputs);
3272 msm_otg_del_timer(motg);
3273 }
3274 break;
3275 case OTG_STATE_A_WAIT_BCON:
3276 if (TA_WAIT_BCON < 0)
3277 set_bit(A_BUS_REQ, &motg->inputs);
3278 default:
3279 work = 0;
3280 break;
3281 }
3282 } else if (usbsts & STS_URI) {
3283 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003284 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303285 case OTG_STATE_A_PERIPHERAL:
3286 /*
3287 * A-peripheral observed activity on bus.
3288 * clear A_BIDL_ADIS timer.
3289 */
3290 msm_otg_del_timer(motg);
3291 work = 0;
3292 break;
3293 default:
3294 work = 0;
3295 break;
3296 }
3297 } else if (usbsts & STS_SLI) {
3298 ret = IRQ_NONE;
3299 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003300 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303301 case OTG_STATE_B_PERIPHERAL:
3302 if (otg->gadget->b_hnp_enable) {
3303 set_bit(A_BUS_SUSPEND, &motg->inputs);
3304 set_bit(B_BUS_REQ, &motg->inputs);
3305 work = 1;
3306 }
3307 break;
3308 case OTG_STATE_A_PERIPHERAL:
3309 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3310 A_BIDL_ADIS);
3311 break;
3312 default:
3313 break;
3314 }
3315 } else if ((usbsts & PHY_ALT_INT)) {
3316 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3317 if (msm_chg_check_aca_intr(motg))
3318 work = 1;
3319 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303320 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303321 if (work)
3322 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303323
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303324 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003325}
3326
3327static void msm_otg_set_vbus_state(int online)
3328{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303329 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003330 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303331
Vamsi Krishna945b4a92013-03-02 15:31:16 -08003332 if (online) {
3333 pr_debug("PMIC: BSV set\n");
3334 set_bit(B_SESS_VLD, &motg->inputs);
3335 } else {
3336 pr_debug("PMIC: BSV clear\n");
3337 clear_bit(B_SESS_VLD, &motg->inputs);
3338 }
3339
3340 /* do not queue state m/c work if id is grounded */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303341 if (!test_bit(ID, &motg->inputs)) {
3342 /*
3343 * state machine work waits for initial VBUS
3344 * completion in UNDEFINED state. Process
3345 * the initial VBUS event in ID_GND state.
3346 */
3347 if (init)
3348 return;
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303349 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003350
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303351 if (!init) {
3352 init = true;
3353 complete(&pmic_vbus_init);
3354 pr_debug("PMIC: BSV init complete\n");
3355 return;
3356 }
3357
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303358 if (test_bit(MHL, &motg->inputs) ||
3359 mhl_det_in_progress) {
3360 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3361 return;
3362 }
3363
Jack Pham5ca279b2012-05-14 18:42:54 -07003364 if (atomic_read(&motg->pm_suspended))
3365 motg->sm_work_pending = true;
3366 else
3367 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003368}
3369
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303370static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003371{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303372 struct msm_otg *motg = container_of(w, struct msm_otg,
3373 pmic_id_status_work.work);
3374 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003375
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303376 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303377 if (!test_and_set_bit(ID, &motg->inputs)) {
3378 pr_debug("PMIC: ID set\n");
3379 work = 1;
3380 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303381 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303382 if (test_and_clear_bit(ID, &motg->inputs)) {
3383 pr_debug("PMIC: ID clear\n");
3384 set_bit(A_BUS_REQ, &motg->inputs);
3385 work = 1;
3386 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303387 }
3388
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303389 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003390 if (atomic_read(&motg->pm_suspended))
3391 motg->sm_work_pending = true;
3392 else
3393 queue_work(system_nrt_wq, &motg->sm_work);
3394 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303395
3396}
3397
3398#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3399static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3400{
3401 struct msm_otg *motg = data;
3402
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303403 if (test_bit(MHL, &motg->inputs) ||
3404 mhl_det_in_progress) {
3405 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3406 return IRQ_HANDLED;
3407 }
3408
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303409 if (!aca_id_turned_on)
3410 /*schedule delayed work for 5msec for ID line state to settle*/
3411 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3412 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003413
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303414 return IRQ_HANDLED;
3415}
3416
3417static int msm_otg_mode_show(struct seq_file *s, void *unused)
3418{
3419 struct msm_otg *motg = s->private;
Stephen Boyd9850acb2013-01-28 14:11:20 -08003420 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303421
Stephen Boyd9850acb2013-01-28 14:11:20 -08003422 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303423 case OTG_STATE_A_HOST:
3424 seq_printf(s, "host\n");
3425 break;
3426 case OTG_STATE_B_PERIPHERAL:
3427 seq_printf(s, "peripheral\n");
3428 break;
3429 default:
3430 seq_printf(s, "none\n");
3431 break;
3432 }
3433
3434 return 0;
3435}
3436
3437static int msm_otg_mode_open(struct inode *inode, struct file *file)
3438{
3439 return single_open(file, msm_otg_mode_show, inode->i_private);
3440}
3441
3442static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3443 size_t count, loff_t *ppos)
3444{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303445 struct seq_file *s = file->private_data;
3446 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303447 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003448 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303449 int status = count;
3450 enum usb_mode_type req_mode;
3451
3452 memset(buf, 0x00, sizeof(buf));
3453
3454 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3455 status = -EFAULT;
3456 goto out;
3457 }
3458
3459 if (!strncmp(buf, "host", 4)) {
3460 req_mode = USB_HOST;
3461 } else if (!strncmp(buf, "peripheral", 10)) {
3462 req_mode = USB_PERIPHERAL;
3463 } else if (!strncmp(buf, "none", 4)) {
3464 req_mode = USB_NONE;
3465 } else {
3466 status = -EINVAL;
3467 goto out;
3468 }
3469
3470 switch (req_mode) {
3471 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003472 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303473 case OTG_STATE_A_HOST:
3474 case OTG_STATE_B_PERIPHERAL:
3475 set_bit(ID, &motg->inputs);
3476 clear_bit(B_SESS_VLD, &motg->inputs);
3477 break;
3478 default:
3479 goto out;
3480 }
3481 break;
3482 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003483 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303484 case OTG_STATE_B_IDLE:
3485 case OTG_STATE_A_HOST:
3486 set_bit(ID, &motg->inputs);
3487 set_bit(B_SESS_VLD, &motg->inputs);
3488 break;
3489 default:
3490 goto out;
3491 }
3492 break;
3493 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003494 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303495 case OTG_STATE_B_IDLE:
3496 case OTG_STATE_B_PERIPHERAL:
3497 clear_bit(ID, &motg->inputs);
3498 break;
3499 default:
3500 goto out;
3501 }
3502 break;
3503 default:
3504 goto out;
3505 }
3506
Steve Mucklef132c6c2012-06-06 18:30:57 -07003507 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303508 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303509out:
3510 return status;
3511}
3512
3513const struct file_operations msm_otg_mode_fops = {
3514 .open = msm_otg_mode_open,
3515 .read = seq_read,
3516 .write = msm_otg_mode_write,
3517 .llseek = seq_lseek,
3518 .release = single_release,
3519};
3520
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303521static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3522{
3523 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003524 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303525
Steve Mucklef132c6c2012-06-06 18:30:57 -07003526 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303527 return 0;
3528}
3529
3530static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3531{
3532 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3533}
3534
3535const struct file_operations msm_otg_state_fops = {
3536 .open = msm_otg_otg_state_open,
3537 .read = seq_read,
3538 .llseek = seq_lseek,
3539 .release = single_release,
3540};
3541
Anji jonnalad270e2d2011-08-09 11:28:32 +05303542static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3543{
3544 struct msm_otg *motg = s->private;
3545
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303546 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303547 return 0;
3548}
3549
3550static int msm_otg_chg_open(struct inode *inode, struct file *file)
3551{
3552 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3553}
3554
3555const struct file_operations msm_otg_chg_fops = {
3556 .open = msm_otg_chg_open,
3557 .read = seq_read,
3558 .llseek = seq_lseek,
3559 .release = single_release,
3560};
3561
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303562static int msm_otg_aca_show(struct seq_file *s, void *unused)
3563{
3564 if (debug_aca_enabled)
3565 seq_printf(s, "enabled\n");
3566 else
3567 seq_printf(s, "disabled\n");
3568
3569 return 0;
3570}
3571
3572static int msm_otg_aca_open(struct inode *inode, struct file *file)
3573{
3574 return single_open(file, msm_otg_aca_show, inode->i_private);
3575}
3576
3577static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3578 size_t count, loff_t *ppos)
3579{
3580 char buf[8];
3581
3582 memset(buf, 0x00, sizeof(buf));
3583
3584 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3585 return -EFAULT;
3586
3587 if (!strncmp(buf, "enable", 6))
3588 debug_aca_enabled = true;
3589 else
3590 debug_aca_enabled = false;
3591
3592 return count;
3593}
3594
3595const struct file_operations msm_otg_aca_fops = {
3596 .open = msm_otg_aca_open,
3597 .read = seq_read,
3598 .write = msm_otg_aca_write,
3599 .llseek = seq_lseek,
3600 .release = single_release,
3601};
3602
Manu Gautam8bdcc592012-03-06 11:26:06 +05303603static int msm_otg_bus_show(struct seq_file *s, void *unused)
3604{
3605 if (debug_bus_voting_enabled)
3606 seq_printf(s, "enabled\n");
3607 else
3608 seq_printf(s, "disabled\n");
3609
3610 return 0;
3611}
3612
3613static int msm_otg_bus_open(struct inode *inode, struct file *file)
3614{
3615 return single_open(file, msm_otg_bus_show, inode->i_private);
3616}
3617
3618static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3619 size_t count, loff_t *ppos)
3620{
3621 char buf[8];
Manu Gautam8bdcc592012-03-06 11:26:06 +05303622 struct seq_file *s = file->private_data;
3623 struct msm_otg *motg = s->private;
3624
3625 memset(buf, 0x00, sizeof(buf));
3626
3627 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3628 return -EFAULT;
3629
3630 if (!strncmp(buf, "enable", 6)) {
3631 /* Do not vote here. Let OTG statemachine decide when to vote */
3632 debug_bus_voting_enabled = true;
3633 } else {
3634 debug_bus_voting_enabled = false;
Manu Gautame3a39082013-06-11 10:42:56 +05303635 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303636 }
3637
3638 return count;
3639}
3640
David Keitel272ce522012-08-17 16:25:24 -07003641static int otg_power_get_property_usb(struct power_supply *psy,
3642 enum power_supply_property psp,
3643 union power_supply_propval *val)
3644{
Jack Pham0c695282012-10-19 18:13:03 -07003645 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003646 switch (psp) {
3647 case POWER_SUPPLY_PROP_SCOPE:
3648 if (motg->host_mode)
3649 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3650 else
3651 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3652 break;
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303653 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
3654 val->intval = motg->voltage_max;
3655 break;
David Keitel272ce522012-08-17 16:25:24 -07003656 case POWER_SUPPLY_PROP_CURRENT_MAX:
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303657 val->intval = motg->current_max;
David Keitel272ce522012-08-17 16:25:24 -07003658 break;
3659 /* Reflect USB enumeration */
3660 case POWER_SUPPLY_PROP_PRESENT:
3661 case POWER_SUPPLY_PROP_ONLINE:
3662 val->intval = motg->online;
3663 break;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303664 case POWER_SUPPLY_PROP_TYPE:
3665 val->intval = psy->type;
3666 break;
Wu Fenglin04ae6de2013-09-09 19:15:06 +08003667 case POWER_SUPPLY_PROP_HEALTH:
3668 val->intval = motg->usbin_health;
3669 break;
David Keitel272ce522012-08-17 16:25:24 -07003670 default:
3671 return -EINVAL;
3672 }
3673 return 0;
3674}
3675
3676static int otg_power_set_property_usb(struct power_supply *psy,
3677 enum power_supply_property psp,
3678 const union power_supply_propval *val)
3679{
Jack Pham0c695282012-10-19 18:13:03 -07003680 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003681
3682 switch (psp) {
3683 /* Process PMIC notification in PRESENT prop */
3684 case POWER_SUPPLY_PROP_PRESENT:
3685 msm_otg_set_vbus_state(val->intval);
3686 break;
3687 /* The ONLINE property reflects if usb has enumerated */
3688 case POWER_SUPPLY_PROP_ONLINE:
3689 motg->online = val->intval;
3690 break;
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303691 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
3692 motg->voltage_max = val->intval;
3693 break;
David Keitel272ce522012-08-17 16:25:24 -07003694 case POWER_SUPPLY_PROP_CURRENT_MAX:
3695 motg->current_max = val->intval;
3696 break;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303697 case POWER_SUPPLY_PROP_TYPE:
3698 psy->type = val->intval;
3699 break;
Wu Fenglin04ae6de2013-09-09 19:15:06 +08003700 case POWER_SUPPLY_PROP_HEALTH:
3701 motg->usbin_health = val->intval;
3702 break;
David Keitel272ce522012-08-17 16:25:24 -07003703 default:
3704 return -EINVAL;
3705 }
3706
3707 power_supply_changed(&motg->usb_psy);
3708 return 0;
3709}
3710
David Collinsd79acc52012-11-26 14:59:00 -08003711static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3712 enum power_supply_property psp)
3713{
3714 switch (psp) {
Wu Fenglin04ae6de2013-09-09 19:15:06 +08003715 case POWER_SUPPLY_PROP_HEALTH:
David Collinsd79acc52012-11-26 14:59:00 -08003716 case POWER_SUPPLY_PROP_PRESENT:
3717 case POWER_SUPPLY_PROP_ONLINE:
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303718 case POWER_SUPPLY_PROP_VOLTAGE_MAX:
David Collinsd79acc52012-11-26 14:59:00 -08003719 case POWER_SUPPLY_PROP_CURRENT_MAX:
3720 return 1;
3721 default:
3722 break;
3723 }
3724
3725 return 0;
3726}
3727
David Keitel272ce522012-08-17 16:25:24 -07003728static char *otg_pm_power_supplied_to[] = {
3729 "battery",
3730};
3731
3732static enum power_supply_property otg_pm_power_props_usb[] = {
Wu Fenglin04ae6de2013-09-09 19:15:06 +08003733 POWER_SUPPLY_PROP_HEALTH,
David Keitel272ce522012-08-17 16:25:24 -07003734 POWER_SUPPLY_PROP_PRESENT,
3735 POWER_SUPPLY_PROP_ONLINE,
Pavankumar Kondeti254e38d2013-07-16 11:13:05 +05303736 POWER_SUPPLY_PROP_VOLTAGE_MAX,
David Keitel272ce522012-08-17 16:25:24 -07003737 POWER_SUPPLY_PROP_CURRENT_MAX,
3738 POWER_SUPPLY_PROP_SCOPE,
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303739 POWER_SUPPLY_PROP_TYPE,
David Keitel272ce522012-08-17 16:25:24 -07003740};
3741
Manu Gautam8bdcc592012-03-06 11:26:06 +05303742const struct file_operations msm_otg_bus_fops = {
3743 .open = msm_otg_bus_open,
3744 .read = seq_read,
3745 .write = msm_otg_bus_write,
3746 .llseek = seq_lseek,
3747 .release = single_release,
3748};
3749
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303750static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303751
3752static int msm_otg_debugfs_init(struct msm_otg *motg)
3753{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303754 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303755
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303756 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3757
3758 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3759 return -ENODEV;
3760
Anji jonnalad270e2d2011-08-09 11:28:32 +05303761 if (motg->pdata->mode == USB_OTG &&
3762 motg->pdata->otg_control == OTG_USER_CONTROL) {
3763
Manu Gautam8bdcc592012-03-06 11:26:06 +05303764 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303765 S_IWUSR, msm_otg_dbg_root, motg,
3766 &msm_otg_mode_fops);
3767
Manu Gautam8bdcc592012-03-06 11:26:06 +05303768 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303769 debugfs_remove(msm_otg_dbg_root);
3770 msm_otg_dbg_root = NULL;
3771 return -ENODEV;
3772 }
3773 }
3774
Manu Gautam8bdcc592012-03-06 11:26:06 +05303775 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303776 msm_otg_dbg_root, motg,
3777 &msm_otg_chg_fops);
3778
Manu Gautam8bdcc592012-03-06 11:26:06 +05303779 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303780 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303781 return -ENODEV;
3782 }
3783
Manu Gautam8bdcc592012-03-06 11:26:06 +05303784 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303785 msm_otg_dbg_root, motg,
3786 &msm_otg_aca_fops);
3787
Manu Gautam8bdcc592012-03-06 11:26:06 +05303788 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303789 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303790 return -ENODEV;
3791 }
3792
Manu Gautam8bdcc592012-03-06 11:26:06 +05303793 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3794 msm_otg_dbg_root, motg,
3795 &msm_otg_bus_fops);
3796
3797 if (!msm_otg_dentry) {
3798 debugfs_remove_recursive(msm_otg_dbg_root);
3799 return -ENODEV;
3800 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303801
3802 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3803 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3804
3805 if (!msm_otg_dentry) {
3806 debugfs_remove_recursive(msm_otg_dbg_root);
3807 return -ENODEV;
3808 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303809 return 0;
3810}
3811
3812static void msm_otg_debugfs_cleanup(void)
3813{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303814 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303815}
3816
Manu Gautam0ddbd922012-09-21 17:17:38 +05303817#define MSM_OTG_CMD_ID 0x09
3818#define MSM_OTG_DEVICE_ID 0x04
3819#define MSM_OTG_VMID_IDX 0xFF
3820#define MSM_OTG_MEM_TYPE 0x02
3821struct msm_otg_scm_cmd_buf {
3822 unsigned int device_id;
3823 unsigned int vmid_idx;
3824 unsigned int mem_type;
3825} __attribute__ ((__packed__));
3826
3827static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3828{
3829 int ret;
3830 struct msm_otg_platform_data *pdata = motg->pdata;
3831 struct msm_otg_scm_cmd_buf cmd_buf;
3832
3833 if (!pdata->pnoc_errata_fix)
3834 return;
3835
3836 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3837
3838 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3839 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3840 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3841
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -07003842 ret = scm_call(SCM_SVC_MP, MSM_OTG_CMD_ID, &cmd_buf,
Manu Gautam0ddbd922012-09-21 17:17:38 +05303843 sizeof(cmd_buf), NULL, 0);
3844
3845 if (ret)
3846 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3847}
3848
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303849static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3850static struct platform_device *msm_otg_add_pdev(
3851 struct platform_device *ofdev, const char *name)
3852{
3853 struct platform_device *pdev;
3854 const struct resource *res = ofdev->resource;
3855 unsigned int num = ofdev->num_resources;
3856 int retval;
Amit Blayb4f14532013-06-17 22:33:57 +03003857 struct ci13xxx_platform_data ci_pdata;
3858 struct msm_otg_platform_data *otg_pdata;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303859
3860 pdev = platform_device_alloc(name, -1);
3861 if (!pdev) {
3862 retval = -ENOMEM;
3863 goto error;
3864 }
3865
3866 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3867 pdev->dev.dma_mask = &msm_otg_dma_mask;
3868
3869 if (num) {
3870 retval = platform_device_add_resources(pdev, res, num);
3871 if (retval)
3872 goto error;
3873 }
3874
Amit Blayb4f14532013-06-17 22:33:57 +03003875 if (!strcmp(name, "msm_hsusb")) {
3876 otg_pdata =
3877 (struct msm_otg_platform_data *)
3878 ofdev->dev.platform_data;
3879 ci_pdata.log2_itc = otg_pdata->log2_itc;
3880 ci_pdata.usb_core_id = 0;
Shimrit Malichi5a2d5b52013-06-20 19:04:28 +03003881 ci_pdata.l1_supported = otg_pdata->l1_supported;
Mayank Rana34f6f3e2013-11-11 12:40:00 +05303882 ci_pdata.enable_ahb2ahb_bypass =
3883 otg_pdata->enable_ahb2ahb_bypass;
Amit Blayb4f14532013-06-17 22:33:57 +03003884 retval = platform_device_add_data(pdev, &ci_pdata,
3885 sizeof(ci_pdata));
3886 if (retval)
3887 goto error;
3888 }
3889
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303890 retval = platform_device_add(pdev);
3891 if (retval)
3892 goto error;
3893
3894 return pdev;
3895
3896error:
3897 platform_device_put(pdev);
3898 return ERR_PTR(retval);
3899}
3900
3901static int msm_otg_setup_devices(struct platform_device *ofdev,
3902 enum usb_mode_type mode, bool init)
3903{
3904 const char *gadget_name = "msm_hsusb";
3905 const char *host_name = "msm_hsusb_host";
3906 static struct platform_device *gadget_pdev;
3907 static struct platform_device *host_pdev;
3908 int retval = 0;
3909
3910 if (!init) {
3911 if (gadget_pdev)
3912 platform_device_unregister(gadget_pdev);
3913 if (host_pdev)
3914 platform_device_unregister(host_pdev);
3915 return 0;
3916 }
3917
3918 switch (mode) {
3919 case USB_OTG:
3920 /* fall through */
3921 case USB_PERIPHERAL:
3922 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3923 if (IS_ERR(gadget_pdev)) {
3924 retval = PTR_ERR(gadget_pdev);
3925 break;
3926 }
3927 if (mode == USB_PERIPHERAL)
3928 break;
3929 /* fall through */
3930 case USB_HOST:
3931 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3932 if (IS_ERR(host_pdev)) {
3933 retval = PTR_ERR(host_pdev);
3934 if (mode == USB_OTG)
3935 platform_device_unregister(gadget_pdev);
3936 }
3937 break;
3938 default:
3939 break;
3940 }
3941
3942 return retval;
3943}
3944
David Keitel272ce522012-08-17 16:25:24 -07003945static int msm_otg_register_power_supply(struct platform_device *pdev,
3946 struct msm_otg *motg)
3947{
3948 int ret;
3949
3950 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3951 if (ret < 0) {
3952 dev_err(motg->phy.dev,
3953 "%s:power_supply_register usb failed\n",
3954 __func__);
3955 return ret;
3956 }
3957
3958 legacy_power_supply = false;
3959 return 0;
3960}
3961
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05303962static int msm_otg_ext_chg_open(struct inode *inode, struct file *file)
3963{
3964 struct msm_otg *motg = the_msm_otg;
3965
3966 pr_debug("msm_otg ext chg open\n");
3967
3968 motg->ext_chg_opened = true;
3969 file->private_data = (void *)motg;
3970 return 0;
3971}
3972
3973static long
3974msm_otg_ext_chg_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3975{
3976 struct msm_otg *motg = file->private_data;
3977 struct msm_usb_chg_info info = {0};
3978 int ret = 0, val;
3979
3980 switch (cmd) {
3981 case MSM_USB_EXT_CHG_INFO:
3982 info.chg_block_type = USB_CHG_BLOCK_ULPI;
3983 info.page_offset = motg->io_res->start & ~PAGE_MASK;
3984 /* mmap() works on PAGE granularity */
3985 info.length = PAGE_SIZE;
3986
3987 if (copy_to_user((void __user *)arg, &info, sizeof(info))) {
3988 pr_err("%s: copy to user failed\n\n", __func__);
3989 ret = -EFAULT;
3990 }
3991 break;
3992 case MSM_USB_EXT_CHG_BLOCK_LPM:
3993 if (get_user(val, (int __user *)arg)) {
3994 pr_err("%s: get_user failed\n\n", __func__);
3995 ret = -EFAULT;
3996 break;
3997 }
3998 pr_debug("%s: LPM block request %d\n", __func__, val);
3999 if (val) { /* block LPM */
4000 if (motg->chg_type == USB_DCP_CHARGER) {
4001 /*
4002 * If device is already suspended, resume it.
4003 * The PM usage counter is incremented in
4004 * runtime resume method. if device is not
4005 * suspended, cancel the scheduled suspend
4006 * and increment the PM usage counter.
4007 */
4008 if (pm_runtime_suspended(motg->phy.dev))
4009 pm_runtime_resume(motg->phy.dev);
4010 else
4011 pm_runtime_get_sync(motg->phy.dev);
4012 } else {
4013 motg->ext_chg_active = false;
4014 complete(&motg->ext_chg_wait);
4015 ret = -ENODEV;
4016 }
4017 } else {
4018 motg->ext_chg_active = false;
4019 complete(&motg->ext_chg_wait);
4020 pm_runtime_put(motg->phy.dev);
4021 }
4022 break;
4023 default:
4024 ret = -EINVAL;
4025 }
4026
4027 return ret;
4028}
4029
4030static int msm_otg_ext_chg_mmap(struct file *file, struct vm_area_struct *vma)
4031{
4032 struct msm_otg *motg = file->private_data;
4033 unsigned long vsize = vma->vm_end - vma->vm_start;
4034 int ret;
4035
4036 if (vma->vm_pgoff || vsize > PAGE_SIZE)
4037 return -EINVAL;
4038
4039 vma->vm_pgoff = __phys_to_pfn(motg->io_res->start);
4040 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
4041
4042 ret = io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
4043 vsize, vma->vm_page_prot);
4044 if (ret < 0) {
4045 pr_err("%s: failed with return val %d\n", __func__, ret);
4046 return ret;
4047 }
4048
4049 return 0;
4050}
4051
4052static int msm_otg_ext_chg_release(struct inode *inode, struct file *file)
4053{
4054 struct msm_otg *motg = file->private_data;
4055
4056 pr_debug("msm_otg ext chg release\n");
4057
4058 motg->ext_chg_opened = false;
4059
4060 return 0;
4061}
4062
4063static const struct file_operations msm_otg_ext_chg_fops = {
4064 .owner = THIS_MODULE,
4065 .open = msm_otg_ext_chg_open,
4066 .unlocked_ioctl = msm_otg_ext_chg_ioctl,
4067 .mmap = msm_otg_ext_chg_mmap,
4068 .release = msm_otg_ext_chg_release,
4069};
4070
4071static int msm_otg_setup_ext_chg_cdev(struct msm_otg *motg)
4072{
4073 int ret;
4074
4075 if (motg->pdata->enable_sec_phy || motg->pdata->mode == USB_HOST ||
4076 motg->pdata->otg_control != OTG_PMIC_CONTROL ||
4077 psy != &motg->usb_psy) {
4078 pr_debug("usb ext chg is not supported by msm otg\n");
4079 return -ENODEV;
4080 }
4081
4082 ret = alloc_chrdev_region(&motg->ext_chg_dev, 0, 1, "usb_ext_chg");
4083 if (ret < 0) {
4084 pr_err("Fail to allocate usb ext char dev region\n");
4085 return ret;
4086 }
4087 motg->ext_chg_class = class_create(THIS_MODULE, "msm_ext_chg");
4088 if (ret < 0) {
4089 pr_err("Fail to create usb ext chg class\n");
4090 goto unreg_chrdev;
4091 }
4092 cdev_init(&motg->ext_chg_cdev, &msm_otg_ext_chg_fops);
4093 motg->ext_chg_cdev.owner = THIS_MODULE;
4094
4095 ret = cdev_add(&motg->ext_chg_cdev, motg->ext_chg_dev, 1);
4096 if (ret < 0) {
4097 pr_err("Fail to add usb ext chg cdev\n");
4098 goto destroy_class;
4099 }
4100 motg->ext_chg_device = device_create(motg->ext_chg_class,
4101 NULL, motg->ext_chg_dev, NULL,
4102 "usb_ext_chg");
4103 if (IS_ERR(motg->ext_chg_device)) {
4104 pr_err("Fail to create usb ext chg device\n");
4105 ret = PTR_ERR(motg->ext_chg_device);
4106 motg->ext_chg_device = NULL;
4107 goto del_cdev;
4108 }
4109
4110 init_completion(&motg->ext_chg_wait);
4111 pr_debug("msm otg ext chg cdev setup success\n");
4112 return 0;
4113
4114del_cdev:
4115 cdev_del(&motg->ext_chg_cdev);
4116destroy_class:
4117 class_destroy(motg->ext_chg_class);
4118unreg_chrdev:
4119 unregister_chrdev_region(motg->ext_chg_dev, 1);
4120
4121 return ret;
4122}
4123
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304124static ssize_t dpdm_pulldown_enable_show(struct device *dev,
4125 struct device_attribute *attr, char *buf)
4126{
4127 struct msm_otg *motg = the_msm_otg;
4128 struct msm_otg_platform_data *pdata = motg->pdata;
4129
4130 return snprintf(buf, PAGE_SIZE, "%s\n", pdata->dpdm_pulldown_added ?
4131 "enabled" : "disabled");
4132}
4133
4134static ssize_t dpdm_pulldown_enable_store(struct device *dev,
4135 struct device_attribute *attr, const char
4136 *buf, size_t size)
4137{
4138 struct msm_otg *motg = the_msm_otg;
4139 struct msm_otg_platform_data *pdata = motg->pdata;
4140
4141 if (!strnicmp(buf, "enable", 6)) {
4142 pdata->dpdm_pulldown_added = true;
4143 return size;
4144 } else if (!strnicmp(buf, "disable", 7)) {
4145 pdata->dpdm_pulldown_added = false;
4146 return size;
4147 }
4148
4149 return -EINVAL;
4150}
4151
4152static DEVICE_ATTR(dpdm_pulldown_enable, S_IRUGO | S_IWUSR,
4153 dpdm_pulldown_enable_show, dpdm_pulldown_enable_store);
4154
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304155struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
4156{
4157 struct device_node *node = pdev->dev.of_node;
4158 struct msm_otg_platform_data *pdata;
4159 int len = 0;
4160
4161 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
4162 if (!pdata) {
4163 pr_err("unable to allocate platform data\n");
4164 return NULL;
4165 }
4166 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
4167 if (len) {
4168 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
4169 if (!pdata->phy_init_seq)
4170 return NULL;
4171 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
4172 pdata->phy_init_seq,
4173 len/sizeof(*pdata->phy_init_seq));
4174 }
4175 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
4176 &pdata->power_budget);
4177 of_property_read_u32(node, "qcom,hsusb-otg-mode",
4178 &pdata->mode);
4179 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
4180 &pdata->otg_control);
4181 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
4182 &pdata->default_mode);
4183 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
4184 &pdata->phy_type);
Manu Gautambd53fba2012-07-31 16:13:06 +05304185 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
4186 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05304187 pdata->pnoc_errata_fix = of_property_read_bool(node,
4188 "qcom,hsusb-otg-pnoc-errata-fix");
Ido Shayevitza7114b92013-01-13 13:34:47 +02004189 pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
4190 "qcom,hsusb-otg-lpm-on-dev-suspend");
Ido Shayevitz26193352013-01-21 23:16:54 +02004191 pdata->core_clk_always_on_workaround = of_property_read_bool(node,
4192 "qcom,hsusb-otg-clk-always-on-workaround");
Shimrit Malichiffab5b02013-03-10 11:06:16 +02004193 pdata->delay_lpm_on_disconnect = of_property_read_bool(node,
4194 "qcom,hsusb-otg-delay-lpm");
Lena Salmanabde35d2013-04-25 15:29:43 +03004195 pdata->delay_lpm_hndshk_on_disconnect = of_property_read_bool(node,
4196 "qcom,hsusb-otg-delay-lpm-hndshk-on-disconnect");
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004197 pdata->dp_manual_pullup = of_property_read_bool(node,
4198 "qcom,dp-manual-pullup");
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304199 pdata->enable_sec_phy = of_property_read_bool(node,
4200 "qcom,usb2-enable-hsphy2");
Amit Blayb4f14532013-06-17 22:33:57 +03004201 of_property_read_u32(node, "qcom,hsusb-log2-itc",
4202 &pdata->log2_itc);
Manu Gautambd53fba2012-07-31 16:13:06 +05304203
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304204 of_property_read_u32(node, "qcom,hsusb-otg-mpm-dpsehv-int",
4205 &pdata->mpm_dpshv_int);
4206 of_property_read_u32(node, "qcom,hsusb-otg-mpm-dmsehv-int",
4207 &pdata->mpm_dmshv_int);
Manu Gautam0e0c53f2013-04-04 10:55:20 +05304208 pdata->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
4209 if (pdata->pmic_id_irq < 0)
4210 pdata->pmic_id_irq = 0;
4211
Shimrit Malichi5a2d5b52013-06-20 19:04:28 +03004212 pdata->l1_supported = of_property_read_bool(node,
4213 "qcom,hsusb-l1-supported");
Mayank Rana34f6f3e2013-11-11 12:40:00 +05304214 pdata->enable_ahb2ahb_bypass = of_property_read_bool(node,
4215 "qcom,ahb-async-bridge-bypass");
Saket Saurabhb06a8c62013-11-25 15:17:23 +05304216 pdata->disable_retention_with_vdd_min = of_property_read_bool(node,
4217 "qcom,disable-retention-with-vdd-min");
Shimrit Malichi5a2d5b52013-06-20 19:04:28 +03004218
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304219 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304220}
4221
4222static int __init msm_otg_probe(struct platform_device *pdev)
4223{
Manu Gautamf8c45642012-08-10 10:20:56 -07004224 int ret = 0;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304225 int len = 0;
4226 u32 tmp[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304227 struct resource *res;
4228 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004229 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304230 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304231
4232 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304233
4234 if (pdev->dev.of_node) {
4235 dev_dbg(&pdev->dev, "device tree enabled\n");
4236 pdata = msm_otg_dt_to_pdata(pdev);
4237 if (!pdata)
4238 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07004239
4240 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
4241 if (!pdata->bus_scale_table)
4242 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
4243
Amit Blayb4f14532013-06-17 22:33:57 +03004244 pdev->dev.platform_data = pdata;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304245 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
4246 if (ret) {
4247 dev_err(&pdev->dev, "devices setup failed\n");
4248 return ret;
4249 }
4250 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304251 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
4252 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304253 } else {
4254 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304255 }
4256
Manu Gautame3a39082013-06-11 10:42:56 +05304257 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304258 if (!motg) {
4259 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
4260 return -ENOMEM;
4261 }
4262
Manu Gautame3a39082013-06-11 10:42:56 +05304263 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
4264 GFP_KERNEL);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004265 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004266 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
Manu Gautame3a39082013-06-11 10:42:56 +05304267 return -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304268 }
4269
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004270 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304271 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004272 phy = &motg->phy;
4273 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05304274
Manu Gautame3a39082013-06-11 10:42:56 +05304275 if (motg->pdata->bus_scale_table) {
4276 motg->bus_perf_client =
4277 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4278 if (!motg->bus_perf_client) {
4279 dev_err(motg->phy.dev, "%s: Failed to register BUS\n"
4280 "scaling client!!\n", __func__);
4281 } else {
4282 debug_bus_voting_enabled = true;
4283 /* Some platforms require BUS vote to control clocks */
4284 msm_otg_bus_vote(motg, USB_MIN_PERF_VOTE);
4285 }
4286 }
4287
Anji jonnala0f73cac2011-05-04 10:19:46 +05304288 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304289 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
4290 * PHY treat ACA ID_GND as float and no interrupt is generated. But
4291 * PMIC can detect ACA ID_GND and generate an interrupt.
4292 */
4293 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
4294 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
4295 ret = -EINVAL;
Manu Gautame3a39082013-06-11 10:42:56 +05304296 goto devote_bus_bw;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304297 }
4298
Ofir Cohen4da266f2012-01-03 10:19:29 +02004299 /* initialize reset counter */
4300 motg->reset_counter = 0;
4301
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304302 /*
4303 * Targets on which link uses asynchronous reset methodology,
4304 * free running clock is not required during the reset.
4305 */
Manu Gautam5143b252012-01-05 19:25:23 -08004306 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304307 if (IS_ERR(motg->clk))
4308 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
4309 else
4310 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05304311
4312 /*
Manu Gautam5143b252012-01-05 19:25:23 -08004313 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05304314 * CORE CLK must be running at >55Mhz for correct HSUSB
4315 * operation and USB core cannot tolerate frequency changes on
4316 * CORE CLK. For such USB cores, vote for maximum clk frequency
4317 * on pclk source
4318 */
Manu Gautam5143b252012-01-05 19:25:23 -08004319 motg->core_clk = clk_get(&pdev->dev, "core_clk");
4320 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304321 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08004322 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05304323 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004324 goto put_clk;
4325 }
Mayank Rana3eaf28d2013-03-27 14:04:04 +05304326
4327 /*
4328 * Get Max supported clk frequency for USB Core CLK and request
4329 * to set the same.
4330 */
4331 motg->core_clk_rate = clk_round_rate(motg->core_clk, LONG_MAX);
4332 if (IS_ERR_VALUE(motg->core_clk_rate)) {
4333 dev_err(&pdev->dev, "fail to get core clk max freq.\n");
4334 } else {
4335 ret = clk_set_rate(motg->core_clk, motg->core_clk_rate);
4336 if (ret)
4337 dev_err(&pdev->dev, "fail to set core_clk freq:%d\n",
4338 ret);
4339 }
Manu Gautam5143b252012-01-05 19:25:23 -08004340
4341 motg->pclk = clk_get(&pdev->dev, "iface_clk");
4342 if (IS_ERR(motg->pclk)) {
4343 dev_err(&pdev->dev, "failed to get iface_clk\n");
4344 ret = PTR_ERR(motg->pclk);
4345 goto put_core_clk;
4346 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304347
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304348 /*
4349 * On few platforms USB PHY is fed with sleep clk.
4350 * Hence don't fail probe.
4351 */
4352 motg->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
4353 if (IS_ERR(motg->sleep_clk)) {
4354 dev_dbg(&pdev->dev, "failed to get sleep_clk\n");
4355 } else {
4356 ret = clk_prepare_enable(motg->sleep_clk);
4357 if (ret) {
4358 dev_err(&pdev->dev, "%s failed to vote sleep_clk%d\n",
4359 __func__, ret);
4360 goto put_pclk;
4361 }
4362 }
4363
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304364 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4365 if (!res) {
4366 dev_err(&pdev->dev, "failed to get platform resource mem\n");
4367 ret = -ENODEV;
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304368 goto disable_sleep_clk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304369 }
4370
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304371 motg->io_res = res;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304372 motg->regs = ioremap(res->start, resource_size(res));
4373 if (!motg->regs) {
4374 dev_err(&pdev->dev, "ioremap failed\n");
4375 ret = -ENOMEM;
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304376 goto disable_sleep_clk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304377 }
4378 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
4379
4380 motg->irq = platform_get_irq(pdev, 0);
4381 if (!motg->irq) {
4382 dev_err(&pdev->dev, "platform_get_irq failed\n");
4383 ret = -ENODEV;
4384 goto free_regs;
4385 }
4386
Manu Gautamf8c45642012-08-10 10:20:56 -07004387 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
4388 if (motg->async_irq < 0) {
4389 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
4390 motg->async_irq = 0;
4391 }
4392
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304393 motg->xo_clk = clk_get(&pdev->dev, "xo");
4394 if (IS_ERR(motg->xo_clk)) {
4395 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
4396 if (IS_ERR(motg->xo_handle)) {
4397 dev_err(&pdev->dev, "%s fail to get handle for TCXO\n",
4398 __func__);
4399 ret = PTR_ERR(motg->xo_handle);
4400 goto free_regs;
4401 } else {
4402 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
4403 if (ret) {
4404 dev_err(&pdev->dev, "%s XO voting failed %d\n",
4405 __func__, ret);
4406 goto free_xo_handle;
4407 }
4408 }
4409 } else {
4410 ret = clk_prepare_enable(motg->xo_clk);
4411 if (ret) {
4412 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
4413 __func__, ret);
4414 goto free_xo_handle;
4415 }
Anji jonnala7da3f262011-12-02 17:22:14 -08004416 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05304417
Anji jonnala7da3f262011-12-02 17:22:14 -08004418
Manu Gautam28b1bac2012-01-30 16:43:06 +05304419 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304420
Mayank Rana248698c2012-04-19 00:03:16 +05304421 motg->vdd_type = VDDCX_CORNER;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304422 hsusb_vdd = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
4423 if (IS_ERR(hsusb_vdd)) {
4424 hsusb_vdd = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
4425 if (IS_ERR(hsusb_vdd)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004426 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304427 ret = PTR_ERR(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304428 goto devote_xo_handle;
4429 }
4430 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304431 }
4432
Mayank Rana0f286cf2013-02-27 11:43:27 +05304433 if (pdev->dev.of_node) {
4434 of_get_property(pdev->dev.of_node,
4435 "qcom,vdd-voltage-level",
4436 &len);
4437 if (len == sizeof(tmp)) {
4438 of_property_read_u32_array(pdev->dev.of_node,
4439 "qcom,vdd-voltage-level",
4440 tmp, len/sizeof(*tmp));
4441 vdd_val[motg->vdd_type][0] = tmp[0];
4442 vdd_val[motg->vdd_type][1] = tmp[1];
4443 vdd_val[motg->vdd_type][2] = tmp[2];
4444 } else {
4445 dev_dbg(&pdev->dev, "Using default hsusb vdd config.\n");
4446 }
4447 }
4448
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004449 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304450 if (ret) {
4451 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05304452 goto devote_xo_handle;
4453 }
4454
Mayank Rana0f286cf2013-02-27 11:43:27 +05304455 ret = regulator_enable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304456 if (ret) {
4457 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
4458 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304459 }
4460
4461 ret = msm_hsusb_ldo_init(motg, 1);
4462 if (ret) {
4463 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304464 goto free_hsusb_vdd;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304465 }
4466
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304467 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05304468 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
4469 "mhl_usb_hs_switch");
4470 if (IS_ERR(mhl_usb_hs_switch)) {
4471 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07004472 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304473 goto free_ldo_init;
4474 }
4475 }
4476
Amit Blay81801aa2012-09-19 12:08:12 +02004477 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304478 if (ret) {
4479 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004480 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304481 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05304482 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304483
Manu Gautam0ddbd922012-09-21 17:17:38 +05304484 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
4485 msm_otg_pnoc_errata_fix(motg);
4486
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304487 writel(0, USB_USBINTR);
4488 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004489 /* Ensure that above STOREs are completed before enabling interrupts */
4490 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304491
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304492 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
4493 if (ret)
4494 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004495 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05304496 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304497 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304498 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304499 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004500 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304501 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
4502 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05304503 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
4504 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304505 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
4506 "msm_otg", motg);
4507 if (ret) {
4508 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004509 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304510 }
4511
Manu Gautamf8c45642012-08-10 10:20:56 -07004512 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304513 ret = request_irq(motg->async_irq, msm_otg_irq,
4514 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004515 if (ret) {
4516 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4517 goto free_irq;
4518 }
4519 disable_irq(motg->async_irq);
4520 }
4521
Jack Pham87f202f2012-08-06 00:24:22 -07004522 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4523 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4524
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304525 if (pdata->mpm_dpshv_int)
4526 msm_mpm_enable_pin(pdata->mpm_dpshv_int, 1);
4527 if (pdata->mpm_dmshv_int)
4528 msm_mpm_enable_pin(pdata->mpm_dmshv_int, 1);
4529
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004530 phy->init = msm_otg_reset;
4531 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004532 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304533
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004534 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304535
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004536 phy->otg->phy = &motg->phy;
4537 phy->otg->set_host = msm_otg_set_host;
4538 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004539 phy->otg->start_hnp = msm_otg_start_hnp;
4540 phy->otg->start_srp = msm_otg_start_srp;
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004541 if (pdata->dp_manual_pullup)
4542 phy->flags |= ENABLE_DP_MANUAL_PULLUP;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004543
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304544 if (pdata->enable_sec_phy)
4545 phy->flags |= ENABLE_SECONDARY_PHY;
4546
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004547 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304548 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004549 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004550 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304551 }
4552
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304553 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304554 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004555 if (motg->pdata->pmic_id_irq) {
4556 ret = request_irq(motg->pdata->pmic_id_irq,
4557 msm_pmic_id_irq,
4558 IRQF_TRIGGER_RISING |
4559 IRQF_TRIGGER_FALLING,
4560 "msm_otg", motg);
4561 if (ret) {
4562 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004563 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004564 }
4565 } else {
4566 ret = -ENODEV;
4567 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004568 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004569 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304570 }
4571
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304572 msm_hsusb_mhl_switch_enable(motg, 1);
4573
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304574 platform_set_drvdata(pdev, motg);
4575 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004576 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304577
Anji jonnalad270e2d2011-08-09 11:28:32 +05304578 ret = msm_otg_debugfs_init(motg);
4579 if (ret)
4580 dev_dbg(&pdev->dev, "mode debugfs file is"
4581 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304582
Amit Blay58b31472011-11-18 09:39:39 +02004583 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4584 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304585 (!(motg->pdata->mode == USB_OTG) ||
4586 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004587 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304588 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004589
Amit Blay58b31472011-11-18 09:39:39 +02004590 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004591 motg->caps = ALLOW_PHY_RETENTION |
4592 ALLOW_PHY_REGULATORS_LPM;
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304593
4594 if (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int)
4595 motg->caps |= ALLOW_HOST_PHY_RETENTION;
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304596 device_create_file(&pdev->dev,
4597 &dev_attr_dpdm_pulldown_enable);
Amit Blay58b31472011-11-18 09:39:39 +02004598 }
4599
Amit Blay6fa647a2012-05-24 14:12:08 +03004600 if (motg->pdata->enable_lpm_on_dev_suspend)
4601 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4602
Saket Saurabhb06a8c62013-11-25 15:17:23 +05304603 if (motg->pdata->disable_retention_with_vdd_min)
4604 motg->caps |= ALLOW_VDD_MIN_WITH_RETENTION_DISABLED;
4605
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004606 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304607 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004608 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304609
Amit Blayd6f38282012-10-29 13:13:46 +02004610 if (motg->pdata->delay_lpm_on_disconnect) {
4611 pm_runtime_set_autosuspend_delay(&pdev->dev,
4612 lpm_disconnect_thresh);
4613 pm_runtime_use_autosuspend(&pdev->dev);
4614 }
4615
David Keitel272ce522012-08-17 16:25:24 -07004616 motg->usb_psy.name = "usb";
4617 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4618 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4619 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4620 motg->usb_psy.properties = otg_pm_power_props_usb;
4621 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4622 motg->usb_psy.get_property = otg_power_get_property_usb;
4623 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004624 motg->usb_psy.property_is_writeable
4625 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004626
Jack Pham0c695282012-10-19 18:13:03 -07004627 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004628 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004629 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4630 legacy_power_supply = true;
4631 } else {
4632 /* otherwise register our own power supply */
4633 if (!msm_otg_register_power_supply(pdev, motg))
4634 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004635 }
4636
Jack Pham0c695282012-10-19 18:13:03 -07004637 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4638 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4639
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304640 ret = msm_otg_setup_ext_chg_cdev(motg);
4641 if (ret)
4642 dev_dbg(&pdev->dev, "fail to setup cdev\n");
4643
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304644 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004645
Steve Mucklef132c6c2012-06-06 18:30:57 -07004646remove_phy:
4647 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004648free_async_irq:
4649 if (motg->async_irq)
4650 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304651free_irq:
4652 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004653destroy_wlock:
4654 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304655 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004656 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004657free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304658 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304659free_hsusb_vdd:
4660 regulator_disable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304661free_config_vddcx:
Mayank Rana0f286cf2013-02-27 11:43:27 +05304662 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304663 vdd_val[motg->vdd_type][VDD_NONE],
4664 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004665devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304666 clk_disable_unprepare(motg->pclk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304667 if (!IS_ERR(motg->xo_clk))
4668 clk_disable_unprepare(motg->xo_clk);
4669 else
4670 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004671free_xo_handle:
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304672 if (!IS_ERR(motg->xo_clk))
4673 clk_put(motg->xo_clk);
4674 else
4675 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304676free_regs:
4677 iounmap(motg->regs);
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304678disable_sleep_clk:
4679 if (!IS_ERR(motg->sleep_clk))
4680 clk_disable_unprepare(motg->sleep_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004681put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304682 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304683put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004684 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304685put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304686 if (!IS_ERR(motg->clk))
4687 clk_put(motg->clk);
Manu Gautame3a39082013-06-11 10:42:56 +05304688devote_bus_bw:
4689 if (motg->bus_perf_client) {
4690 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
4691 msm_bus_scale_unregister_client(motg->bus_perf_client);
4692 }
4693
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304694 return ret;
4695}
4696
4697static int __devexit msm_otg_remove(struct platform_device *pdev)
4698{
4699 struct msm_otg *motg = platform_get_drvdata(pdev);
Stephen Boyd9850acb2013-01-28 14:11:20 -08004700 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304701 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304702
Stephen Boyd9850acb2013-01-28 14:11:20 -08004703 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304704 return -EBUSY;
4705
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304706 if (!motg->ext_chg_device) {
4707 device_destroy(motg->ext_chg_class, motg->ext_chg_dev);
4708 cdev_del(&motg->ext_chg_cdev);
4709 class_destroy(motg->ext_chg_class);
4710 unregister_chrdev_region(motg->ext_chg_dev, 1);
4711 }
4712
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304713 if (pdev->dev.of_node)
4714 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004715 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4716 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304717 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304718 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304719 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304720 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004721 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304722 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304723
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304724 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304725
4726 device_init_wakeup(&pdev->dev, 0);
4727 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004728 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304729
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304730 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004731 if (motg->pdata->pmic_id_irq)
4732 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004733 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304734 free_irq(motg->irq, motg);
4735
Vijayavardhan Vennapusa8997e332013-07-15 09:53:51 +05304736 if ((motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) &&
4737 (motg->pdata->mpm_dpshv_int || motg->pdata->mpm_dmshv_int))
4738 device_remove_file(&pdev->dev,
4739 &dev_attr_dpdm_pulldown_enable);
Jack Pham87f202f2012-08-06 00:24:22 -07004740 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4741 motg->pdata->mpm_otgsessvld_int)
4742 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4743
Vijayavardhan Vennapusa2239b2b2013-06-28 13:16:29 +05304744 if (motg->pdata->mpm_dpshv_int)
4745 msm_mpm_enable_pin(motg->pdata->mpm_dpshv_int, 0);
4746 if (motg->pdata->mpm_dmshv_int)
4747 msm_mpm_enable_pin(motg->pdata->mpm_dmshv_int, 0);
4748
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304749 /*
4750 * Put PHY in low power mode.
4751 */
Stephen Boyd9850acb2013-01-28 14:11:20 -08004752 ulpi_read(phy, 0x14);
4753 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304754
4755 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4756 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4757 if (readl(USB_PORTSC) & PORTSC_PHCD)
4758 break;
4759 udelay(1);
4760 cnt++;
4761 }
4762 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Stephen Boyd9850acb2013-01-28 14:11:20 -08004763 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304764
Manu Gautam28b1bac2012-01-30 16:43:06 +05304765 clk_disable_unprepare(motg->pclk);
4766 clk_disable_unprepare(motg->core_clk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304767 if (!IS_ERR(motg->xo_clk)) {
4768 clk_disable_unprepare(motg->xo_clk);
4769 clk_put(motg->xo_clk);
4770 } else {
4771 msm_xo_put(motg->xo_handle);
4772 }
Mayank Ranadd1f1dc2013-07-24 17:02:45 +05304773
4774 if (!IS_ERR(motg->sleep_clk))
4775 clk_disable_unprepare(motg->sleep_clk);
4776
Amit Blay81801aa2012-09-19 12:08:12 +02004777 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304778 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304779 regulator_disable(hsusb_vdd);
4780 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304781 vdd_val[motg->vdd_type][VDD_NONE],
4782 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304783
4784 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304785 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304786
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304787 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304788 if (!IS_ERR(motg->clk))
4789 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004790 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304791
Manu Gautame3a39082013-06-11 10:42:56 +05304792 if (motg->bus_perf_client) {
4793 msm_otg_bus_vote(motg, USB_NO_PERF_VOTE);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304794 msm_bus_scale_unregister_client(motg->bus_perf_client);
Manu Gautame3a39082013-06-11 10:42:56 +05304795 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304796
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304797 return 0;
4798}
4799
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304800#ifdef CONFIG_PM_RUNTIME
4801static int msm_otg_runtime_idle(struct device *dev)
4802{
4803 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004804 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304805
4806 dev_dbg(dev, "OTG runtime idle\n");
4807
Steve Mucklef132c6c2012-06-06 18:30:57 -07004808 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304809 return -EAGAIN;
Pavankumar Kondetif1ffbdd2013-06-27 13:31:36 +05304810
4811 if (motg->ext_chg_active) {
4812 dev_dbg(dev, "Deferring LPM\n");
4813 /*
4814 * Charger detection may happen in user space.
4815 * Delay entering LPM by 3 sec. Otherwise we
4816 * have to exit LPM when user space begins
4817 * charger detection.
4818 *
4819 * This timer will be canceled when user space
4820 * votes against LPM by incrementing PM usage
4821 * counter. We enter low power mode when
4822 * PM usage counter is decremented.
4823 */
4824 pm_schedule_suspend(dev, 3000);
4825 return -EAGAIN;
4826 }
4827
4828 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304829}
4830
4831static int msm_otg_runtime_suspend(struct device *dev)
4832{
4833 struct msm_otg *motg = dev_get_drvdata(dev);
4834
4835 dev_dbg(dev, "OTG runtime suspend\n");
4836 return msm_otg_suspend(motg);
4837}
4838
4839static int msm_otg_runtime_resume(struct device *dev)
4840{
4841 struct msm_otg *motg = dev_get_drvdata(dev);
4842
4843 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304844 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304845 return msm_otg_resume(motg);
4846}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304847#endif
4848
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304849#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304850static int msm_otg_pm_suspend(struct device *dev)
4851{
Jack Pham5ca279b2012-05-14 18:42:54 -07004852 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304853 struct msm_otg *motg = dev_get_drvdata(dev);
4854
4855 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004856
4857 atomic_set(&motg->pm_suspended, 1);
4858 ret = msm_otg_suspend(motg);
4859 if (ret)
4860 atomic_set(&motg->pm_suspended, 0);
4861
4862 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304863}
4864
4865static int msm_otg_pm_resume(struct device *dev)
4866{
Jack Pham5ca279b2012-05-14 18:42:54 -07004867 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304868 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304869
4870 dev_dbg(dev, "OTG PM resume\n");
4871
Jack Pham5ca279b2012-05-14 18:42:54 -07004872 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004873 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004874 pm_runtime_get_noresume(dev);
4875 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304876
Jack Pham5ca279b2012-05-14 18:42:54 -07004877 /* Update runtime PM status */
4878 pm_runtime_disable(dev);
4879 pm_runtime_set_active(dev);
4880 pm_runtime_enable(dev);
4881
Jack Phamc7edb172012-08-13 15:32:39 -07004882 if (motg->sm_work_pending) {
4883 motg->sm_work_pending = false;
4884 queue_work(system_nrt_wq, &motg->sm_work);
4885 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004886 }
4887
4888 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304889}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304890#endif
4891
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304892#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304893static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304894 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4895 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4896 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304897};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304898#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304899
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304900static struct of_device_id msm_otg_dt_match[] = {
4901 { .compatible = "qcom,hsusb-otg",
4902 },
4903 {}
4904};
4905
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304906static struct platform_driver msm_otg_driver = {
4907 .remove = __devexit_p(msm_otg_remove),
4908 .driver = {
4909 .name = DRIVER_NAME,
4910 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304911#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304912 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304913#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304914 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304915 },
4916};
4917
4918static int __init msm_otg_init(void)
4919{
4920 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4921}
4922
4923static void __exit msm_otg_exit(void)
4924{
4925 platform_driver_unregister(&msm_otg_driver);
4926}
4927
4928module_init(msm_otg_init);
4929module_exit(msm_otg_exit);
4930
4931MODULE_LICENSE("GPL v2");
4932MODULE_DESCRIPTION("MSM USB transceiver driver");