blob: 2da4b6c24f09bd373e7a696fe0ee55299bb8253d [file] [log] [blame]
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05301/* Copyright (c) 2009-2013, Linux Foundation. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053042#include <linux/mhl_8334.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053043
Manu Gautam0ddbd922012-09-21 17:17:38 +053044#include <mach/scm.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070046#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080047#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053048#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053049#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053050
51#define MSM_USB_BASE (motg->regs)
52#define DRIVER_NAME "msm_otg"
53
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053054#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondeti458d8792012-09-28 14:45:18 +053055#define CHG_RECHECK_DELAY (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053056#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053057#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
58#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
59#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
60#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
61
62#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
63#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
64#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
65#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
66
Mayank Rana248698c2012-04-19 00:03:16 +053067#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080068#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053069#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
70
Amit Blayd0fe07b2012-09-05 16:42:09 +030071#define USB_SUSPEND_DELAY_TIME (500 * HZ/1000) /* 500 msec */
72
Amit Blay81801aa2012-09-19 12:08:12 +020073enum msm_otg_phy_reg_mode {
74 USB_PHY_REG_OFF,
75 USB_PHY_REG_ON,
76 USB_PHY_REG_LPM_ON,
77 USB_PHY_REG_LPM_OFF,
78};
79
Mayank Rana443f9e42012-09-21 18:32:39 +053080static char *override_phy_init;
81module_param(override_phy_init, charp, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(override_phy_init,
83 "Override HSUSB PHY Init Settings");
84
Amit Blayd6f38282012-10-29 13:13:46 +020085unsigned int lpm_disconnect_thresh = 1000;
86module_param(lpm_disconnect_thresh , uint, S_IRUGO | S_IWUSR);
87MODULE_PARM_DESC(lpm_disconnect_thresh,
88 "Delay before entering LPM on USB disconnect");
89
zhenhuahb6d5edc2013-06-19 14:37:32 +080090static bool floated_charger_enable;
91module_param(floated_charger_enable , bool, S_IRUGO | S_IWUSR);
92MODULE_PARM_DESC(floated_charger_enable,
93 "Whether to enable floated charger");
94
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053095static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053097static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053098static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053099static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100
Anji jonnala11aa5c42011-05-04 10:19:48 +0530101static struct regulator *hsusb_3p3;
102static struct regulator *hsusb_1p8;
Mayank Rana0f286cf2013-02-27 11:43:27 +0530103static struct regulator *hsusb_vdd;
Mayank Ranae3926882011-12-26 09:47:54 +0530104static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530105static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +0530106static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530107
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530108static bool aca_id_turned_on;
David Keitel272ce522012-08-17 16:25:24 -0700109static bool legacy_power_supply;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530110static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530111{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530112#ifdef CONFIG_USB_MSM_ACA
113 return true;
114#else
115 return debug_aca_enabled;
116#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +0530117}
118
Mayank Rana0f286cf2013-02-27 11:43:27 +0530119static int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
Mayank Rana248698c2012-04-19 00:03:16 +0530120 { /* VDD_CX CORNER Voting */
121 [VDD_NONE] = RPM_VREG_CORNER_NONE,
122 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
123 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
124 },
125 { /* VDD_CX Voltage Voting */
126 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
127 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
128 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
129 },
130};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530131
132static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
133{
134 int rc = 0;
135
136 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700137 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200139 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530140 return PTR_ERR(hsusb_3p3);
141 }
142
143 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
144 USB_PHY_3P3_VOL_MAX);
145 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700146 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700147 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530148 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530149 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700150 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530151 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200152 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530153 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700154 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530155 }
156 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
157 USB_PHY_1P8_VOL_MAX);
158 if (rc) {
Stephen Boyd9850acb2013-01-28 14:11:20 -0800159 dev_err(motg->phy.dev, "unable to set voltage level "
160 "for hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530161 goto put_1p8;
162 }
163
164 return 0;
165 }
166
Anji jonnala11aa5c42011-05-04 10:19:48 +0530167put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169put_3p3_lpm:
170 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530171 return rc;
172}
173
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530174static int msm_hsusb_config_vddcx(int high)
175{
Mayank Rana248698c2012-04-19 00:03:16 +0530176 struct msm_otg *motg = the_msm_otg;
177 enum usb_vdd_type vdd_type = motg->vdd_type;
178 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530179 int min_vol;
180 int ret;
181
Mayank Rana248698c2012-04-19 00:03:16 +0530182 min_vol = vdd_val[vdd_type][!!high];
Mayank Rana0f286cf2013-02-27 11:43:27 +0530183 ret = regulator_set_voltage(hsusb_vdd, min_vol, max_vol);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530184 if (ret) {
185 pr_err("%s: unable to set the voltage for regulator "
186 "HSUSB_VDDCX\n", __func__);
187 return ret;
188 }
189
190 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
191
192 return ret;
193}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530194
Amit Blay81801aa2012-09-19 12:08:12 +0200195static int msm_hsusb_ldo_enable(struct msm_otg *motg,
196 enum msm_otg_phy_reg_mode mode)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530197{
198 int ret = 0;
199
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530200 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530201 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
202 return -ENODEV;
203 }
204
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530205 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530206 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
207 return -ENODEV;
208 }
209
Amit Blay81801aa2012-09-19 12:08:12 +0200210 switch (mode) {
211 case USB_PHY_REG_ON:
Anji jonnala11aa5c42011-05-04 10:19:48 +0530212 ret = regulator_set_optimum_mode(hsusb_1p8,
213 USB_PHY_1P8_HPM_LOAD);
214 if (ret < 0) {
Stephen Boyd9850acb2013-01-28 14:11:20 -0800215 pr_err("%s: Unable to set HPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530216 "HSUSB_1p8\n", __func__);
217 return ret;
218 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219
220 ret = regulator_enable(hsusb_1p8);
221 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700222 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 __func__);
224 regulator_set_optimum_mode(hsusb_1p8, 0);
225 return ret;
226 }
227
Anji jonnala11aa5c42011-05-04 10:19:48 +0530228 ret = regulator_set_optimum_mode(hsusb_3p3,
229 USB_PHY_3P3_HPM_LOAD);
230 if (ret < 0) {
Stephen Boyd9850acb2013-01-28 14:11:20 -0800231 pr_err("%s: Unable to set HPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 regulator_set_optimum_mode(hsusb_1p8, 0);
234 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530235 return ret;
236 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700237
238 ret = regulator_enable(hsusb_3p3);
239 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700240 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241 __func__);
242 regulator_set_optimum_mode(hsusb_3p3, 0);
243 regulator_set_optimum_mode(hsusb_1p8, 0);
244 regulator_disable(hsusb_1p8);
245 return ret;
246 }
247
Amit Blay81801aa2012-09-19 12:08:12 +0200248 break;
249
250 case USB_PHY_REG_OFF:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251 ret = regulator_disable(hsusb_1p8);
252 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700253 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700254 __func__);
255 return ret;
256 }
257
258 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530259 if (ret < 0)
Stephen Boyd9850acb2013-01-28 14:11:20 -0800260 pr_err("%s: Unable to set LPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530261 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262
263 ret = regulator_disable(hsusb_3p3);
264 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700265 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 __func__);
267 return ret;
268 }
269 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530270 if (ret < 0)
Stephen Boyd9850acb2013-01-28 14:11:20 -0800271 pr_err("%s: Unable to set LPM of the regulator "
Anji jonnala11aa5c42011-05-04 10:19:48 +0530272 "HSUSB_3p3\n", __func__);
Amit Blay81801aa2012-09-19 12:08:12 +0200273
274 break;
275
276 case USB_PHY_REG_LPM_ON:
277 ret = regulator_set_optimum_mode(hsusb_1p8,
278 USB_PHY_1P8_LPM_LOAD);
279 if (ret < 0) {
280 pr_err("%s: Unable to set LPM of the regulator: HSUSB_1p8\n",
281 __func__);
282 return ret;
283 }
284
285 ret = regulator_set_optimum_mode(hsusb_3p3,
286 USB_PHY_3P3_LPM_LOAD);
287 if (ret < 0) {
288 pr_err("%s: Unable to set LPM of the regulator: HSUSB_3p3\n",
289 __func__);
290 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
291 return ret;
292 }
293
294 break;
295
296 case USB_PHY_REG_LPM_OFF:
297 ret = regulator_set_optimum_mode(hsusb_1p8,
298 USB_PHY_1P8_HPM_LOAD);
299 if (ret < 0) {
300 pr_err("%s: Unable to set HPM of the regulator: HSUSB_1p8\n",
301 __func__);
302 return ret;
303 }
304
305 ret = regulator_set_optimum_mode(hsusb_3p3,
306 USB_PHY_3P3_HPM_LOAD);
307 if (ret < 0) {
308 pr_err("%s: Unable to set HPM of the regulator: HSUSB_3p3\n",
309 __func__);
310 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
311 return ret;
312 }
313
314 break;
315
316 default:
317 pr_err("%s: Unsupported mode (%d).", __func__, mode);
318 return -ENOTSUPP;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530319 }
320
Amit Blay81801aa2012-09-19 12:08:12 +0200321 pr_debug("%s: USB reg mode (%d) (OFF/HPM/LPM)\n", __func__, mode);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530322 return ret < 0 ? ret : 0;
323}
324
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530325static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
326{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530327 struct msm_otg_platform_data *pdata = motg->pdata;
328
329 if (!pdata->mhl_enable)
330 return;
331
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530332 if (!mhl_usb_hs_switch) {
333 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530334 return;
335 }
336
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530337 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530338 if (regulator_enable(mhl_usb_hs_switch))
339 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530340 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530341 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530342 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530343}
344
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200345static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530346{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200347 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530348 int cnt = 0;
349
350 /* initiate read operation */
351 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
352 USB_ULPI_VIEWPORT);
353
354 /* wait for completion */
355 while (cnt < ULPI_IO_TIMEOUT_USEC) {
356 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
357 break;
358 udelay(1);
359 cnt++;
360 }
361
362 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200363 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530364 readl(USB_ULPI_VIEWPORT));
365 return -ETIMEDOUT;
366 }
367 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
368}
369
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200370static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530371{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200372 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530373 int cnt = 0;
374
375 /* initiate write operation */
376 writel(ULPI_RUN | ULPI_WRITE |
377 ULPI_ADDR(reg) | ULPI_DATA(val),
378 USB_ULPI_VIEWPORT);
379
380 /* wait for completion */
381 while (cnt < ULPI_IO_TIMEOUT_USEC) {
382 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
383 break;
384 udelay(1);
385 cnt++;
386 }
387
388 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200389 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530390 return -ETIMEDOUT;
391 }
392 return 0;
393}
394
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200395static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530396 .read = ulpi_read,
397 .write = ulpi_write,
398};
399
400static void ulpi_init(struct msm_otg *motg)
401{
402 struct msm_otg_platform_data *pdata = motg->pdata;
Mayank Rana443f9e42012-09-21 18:32:39 +0530403 int aseq[10];
404 int *seq = NULL;
405
406 if (override_phy_init) {
407 pr_debug("%s(): HUSB PHY Init:%s\n", __func__,
408 override_phy_init);
409 get_options(override_phy_init, ARRAY_SIZE(aseq), aseq);
410 seq = &aseq[1];
411 } else {
412 seq = pdata->phy_init_seq;
413 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530414
415 if (!seq)
416 return;
417
418 while (seq[0] >= 0) {
Mayank Rana443f9e42012-09-21 18:32:39 +0530419 if (override_phy_init)
420 pr_debug("ulpi: write 0x%02x to 0x%02x\n",
421 seq[0], seq[1]);
422
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200423 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530424 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200425 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530426 seq += 2;
427 }
428}
429
430static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
431{
432 int ret;
433
434 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530435 if (!IS_ERR(motg->clk)) {
436 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
437 } else {
438 /* Using asynchronous block reset to the hardware */
439 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
440 clk_disable_unprepare(motg->pclk);
441 clk_disable_unprepare(motg->core_clk);
442 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
443 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530444 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200445 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530446 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530447 if (!IS_ERR(motg->clk)) {
448 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
449 } else {
450 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
451 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
452 ndelay(200);
453 clk_prepare_enable(motg->core_clk);
454 clk_prepare_enable(motg->pclk);
455 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530456 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200457 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530458 }
459 return ret;
460}
461
462static int msm_otg_phy_clk_reset(struct msm_otg *motg)
463{
464 int ret;
465
Amit Blay02eff132011-09-21 16:46:24 +0300466 if (IS_ERR(motg->phy_reset_clk))
467 return 0;
468
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530469 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
470 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200471 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530472 return ret;
473 }
474 usleep_range(10000, 12000);
475 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
476 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200477 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530478 return ret;
479}
480
481static int msm_otg_phy_reset(struct msm_otg *motg)
482{
483 u32 val;
484 int ret;
485 int retries;
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530486 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530487
488 ret = msm_otg_link_clk_reset(motg, 1);
489 if (ret)
490 return ret;
491 ret = msm_otg_phy_clk_reset(motg);
492 if (ret)
493 return ret;
Amit Blay58dc2bc2013-01-24 12:28:03 +0200494
495 /*
496 * 10 usec delay is required according to spec. Using larger value
497 * since the exact value proved to not work 100% of the time.
498 */
Bar Weinere7129cb2012-11-28 08:44:14 +0200499 if (IS_ERR(motg->phy_reset_clk))
Amit Blay58dc2bc2013-01-24 12:28:03 +0200500 usleep_range(100, 120);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530501 ret = msm_otg_link_clk_reset(motg, 0);
502 if (ret)
503 return ret;
504
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530505 if (pdata && pdata->enable_sec_phy)
506 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
507 USB_PHY_CTRL2);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530508 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
509 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
510
511 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200512 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530513 ULPI_CLR(ULPI_FUNC_CTRL));
514 if (!ret)
515 break;
516 ret = msm_otg_phy_clk_reset(motg);
517 if (ret)
518 return ret;
519 }
520 if (!retries)
521 return -ETIMEDOUT;
522
523 /* This reset calibrates the phy, if the above write succeeded */
524 ret = msm_otg_phy_clk_reset(motg);
525 if (ret)
526 return ret;
527
528 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200529 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530530 if (ret != -ETIMEDOUT)
531 break;
532 ret = msm_otg_phy_clk_reset(motg);
533 if (ret)
534 return ret;
535 }
536 if (!retries)
537 return -ETIMEDOUT;
538
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200539 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530540 return 0;
541}
542
543#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530544static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530545{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530546 int cnt = 0;
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530547 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530548
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530549 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530550 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530551 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530552 break;
553 udelay(1);
554 cnt++;
555 }
556 if (cnt >= LINK_RESET_TIMEOUT_USEC)
557 return -ETIMEDOUT;
558
559 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530560 writel_relaxed(0x80000000, USB_PORTSC);
561 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530562 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530563
Manu Gautam0fd2d0e2013-03-26 18:09:11 +0530564 if (pdata && pdata->enable_sec_phy)
565 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
566 USB_PHY_CTRL2);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530567 return 0;
568}
569
Steve Mucklef132c6c2012-06-06 18:30:57 -0700570static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530571{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700572 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530573 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530574 int ret;
575 u32 val = 0;
576 u32 ulpi_val = 0;
577
Ofir Cohen4da266f2012-01-03 10:19:29 +0200578 /*
579 * USB PHY and Link reset also reset the USB BAM.
580 * Thus perform reset operation only once to avoid
581 * USB BAM reset on other cases e.g. USB cable disconnections.
582 */
583 if (pdata->disable_reset_on_disconnect) {
584 if (motg->reset_counter)
585 return 0;
586 else
587 motg->reset_counter++;
588 }
589
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530590 if (!IS_ERR(motg->clk))
591 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530592 ret = msm_otg_phy_reset(motg);
593 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700594 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530595 return ret;
596 }
597
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530598 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530599 ret = msm_otg_link_reset(motg);
600 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700601 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530602 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530603 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530604 msleep(100);
605
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530606 ulpi_init(motg);
607
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700608 /* Ensure that RESET operation is completed before turning off clock */
609 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530610
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530611 if (!IS_ERR(motg->clk))
612 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530613
614 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530615 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530616 if (pdata->mode == USB_OTG) {
617 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
618 val |= OTGSC_IDIE | OTGSC_BSVIE;
619 } else if (pdata->mode == USB_PERIPHERAL) {
620 ulpi_val = ULPI_INT_SESS_VALID;
621 val |= OTGSC_BSVIE;
622 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530623 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200624 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
625 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530626 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700627 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530628 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530629 /* Enable PMIC pull-up */
630 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530631 }
632
633 return 0;
634}
635
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530636static const char *timer_string(int bit)
637{
638 switch (bit) {
639 case A_WAIT_VRISE: return "a_wait_vrise";
640 case A_WAIT_VFALL: return "a_wait_vfall";
641 case B_SRP_FAIL: return "b_srp_fail";
642 case A_WAIT_BCON: return "a_wait_bcon";
643 case A_AIDL_BDIS: return "a_aidl_bdis";
644 case A_BIDL_ADIS: return "a_bidl_adis";
645 case B_ASE0_BRST: return "b_ase0_brst";
646 case A_TST_MAINT: return "a_tst_maint";
647 case B_TST_SRP: return "b_tst_srp";
648 case B_TST_CONFIG: return "b_tst_config";
649 default: return "UNDEFINED";
650 }
651}
652
653static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
654{
655 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
656
657 switch (motg->active_tmout) {
658 case A_WAIT_VRISE:
659 /* TODO: use vbus_vld interrupt */
660 set_bit(A_VBUS_VLD, &motg->inputs);
661 break;
662 case A_TST_MAINT:
663 /* OTG PET: End session after TA_TST_MAINT */
664 set_bit(A_BUS_DROP, &motg->inputs);
665 break;
666 case B_TST_SRP:
667 /*
668 * OTG PET: Initiate SRP after TB_TST_SRP of
669 * previous session end.
670 */
671 set_bit(B_BUS_REQ, &motg->inputs);
672 break;
673 case B_TST_CONFIG:
674 clear_bit(A_CONN, &motg->inputs);
675 break;
676 default:
677 set_bit(motg->active_tmout, &motg->tmouts);
678 }
679
680 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
681 queue_work(system_nrt_wq, &motg->sm_work);
682 return HRTIMER_NORESTART;
683}
684
685static void msm_otg_del_timer(struct msm_otg *motg)
686{
687 int bit = motg->active_tmout;
688
689 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
690 div_s64(ktime_to_us(hrtimer_get_remaining(
691 &motg->timer)), 1000));
692 hrtimer_cancel(&motg->timer);
693 clear_bit(bit, &motg->tmouts);
694}
695
696static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
697{
698 clear_bit(bit, &motg->tmouts);
699 motg->active_tmout = bit;
700 pr_debug("starting %s timer\n", timer_string(bit));
701 hrtimer_start(&motg->timer,
702 ktime_set(time / 1000, (time % 1000) * 1000000),
703 HRTIMER_MODE_REL);
704}
705
706static void msm_otg_init_timer(struct msm_otg *motg)
707{
708 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
709 motg->timer.function = msm_otg_timer_func;
710}
711
Steve Mucklef132c6c2012-06-06 18:30:57 -0700712static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530713{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700714 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530715
Steve Mucklef132c6c2012-06-06 18:30:57 -0700716 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530717 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700718 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530719 return -EINVAL;
720 }
721
722 pr_debug("A-Host: HNP initiated\n");
723 clear_bit(A_BUS_REQ, &motg->inputs);
724 queue_work(system_nrt_wq, &motg->sm_work);
725 return 0;
726}
727
Steve Mucklef132c6c2012-06-06 18:30:57 -0700728static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530729{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700730 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530731 u32 val;
732 int ret = 0;
733
Steve Mucklef132c6c2012-06-06 18:30:57 -0700734 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530735 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700736 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530737 ret = -EINVAL;
738 goto out;
739 }
740
741 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
742 pr_debug("initial conditions of SRP are not met. Try again"
743 "after some time\n");
744 ret = -EAGAIN;
745 goto out;
746 }
747
748 pr_debug("B-Device SRP started\n");
749
750 /*
751 * PHY won't pull D+ high unless it detects Vbus valid.
752 * Since by definition, SRP is only done when Vbus is not valid,
753 * software work-around needs to be used to spoof the PHY into
754 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
755 * VBUSVLDEXT register bits.
756 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700757 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530758 /*
759 * Harware auto assist data pulsing: Data pulse is given
760 * for 7msec; wait for vbus
761 */
762 val = readl_relaxed(USB_OTGSC);
763 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
764
765 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
766out:
767 return ret;
768}
769
Steve Mucklef132c6c2012-06-06 18:30:57 -0700770static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530771{
772 struct usb_hcd *hcd = bus_to_hcd(otg->host);
773 struct usb_device *rhub = otg->host->root_hub;
774
775 if (enable) {
776 pm_runtime_disable(&rhub->dev);
777 rhub->state = USB_STATE_NOTATTACHED;
778 hcd->driver->bus_suspend(hcd);
779 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
780 } else {
781 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700782 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530783 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
784 }
785}
786
Steve Mucklef132c6c2012-06-06 18:30:57 -0700787static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530788{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700789 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530790
Amit Blay6fa647a2012-05-24 14:12:08 +0300791 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530792 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530793
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530794 /*
795 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
796 * during bus suspend/resume. Update the relevant state
797 * machine inputs and trigger LPM entry/exit. Checking
798 * in_lpm flag would avoid unnecessary work scheduling.
799 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530800 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700801 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530802 case OTG_STATE_A_WAIT_BCON:
803 if (TA_WAIT_BCON > 0)
804 break;
805 /* fall through */
806 case OTG_STATE_A_HOST:
807 pr_debug("host bus suspend\n");
808 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530809 if (!atomic_read(&motg->in_lpm))
810 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530811 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300812 case OTG_STATE_B_PERIPHERAL:
813 pr_debug("peripheral bus suspend\n");
814 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
815 break;
816 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530817 if (!atomic_read(&motg->in_lpm))
818 queue_delayed_work(system_nrt_wq,
819 &motg->suspend_work,
820 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300821 break;
822
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530823 default:
824 break;
825 }
826 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700827 switch (phy->state) {
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530828 case OTG_STATE_A_WAIT_BCON:
829 /* Remote wakeup or resume */
830 set_bit(A_BUS_REQ, &motg->inputs);
831 /* ensure hardware is not in low power mode */
832 if (atomic_read(&motg->in_lpm))
833 pm_runtime_resume(phy->dev);
834 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530835 case OTG_STATE_A_SUSPEND:
836 /* Remote wakeup or resume */
837 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700838 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700839
840 /* ensure hardware is not in low power mode */
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530841 if (atomic_read(&motg->in_lpm))
842 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530843 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300844 case OTG_STATE_B_PERIPHERAL:
845 pr_debug("peripheral bus resume\n");
846 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
847 break;
848 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530849 if (atomic_read(&motg->in_lpm))
850 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300851 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530852 default:
853 break;
854 }
855 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530856 return 0;
857}
858
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530859#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530860#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
861
862#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530863static int msm_otg_suspend(struct msm_otg *motg)
864{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200865 struct usb_phy *phy = &motg->phy;
866 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530867 struct msm_otg_platform_data *pdata = motg->pdata;
868 int cnt = 0;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530869 bool host_bus_suspend, device_bus_suspend, dcp, prop_charger;
zhenhuahb6d5edc2013-06-19 14:37:32 +0800870 bool floated_charger;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530871 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800872 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530873 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530874
875 if (atomic_read(&motg->in_lpm))
876 return 0;
877
Lena Salmanabde35d2013-04-25 15:29:43 +0300878 if (motg->pdata->delay_lpm_hndshk_on_disconnect && !msm_bam_lpm_ok())
Lena Salman05b544f2013-05-13 15:49:10 +0300879 return -EBUSY;
Lena Salmanabde35d2013-04-25 15:29:43 +0300880
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530881 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530882 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
883 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700884 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300885 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
886 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530887 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530888 prop_charger = motg->chg_type == USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +0800889 floated_charger = motg->chg_type == USB_FLOATED_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700890
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530891 /*
892 * Abort suspend when,
893 * 1. charging detection in progress due to cable plug-in
894 * 2. host mode activation in progress due to Micro-A cable insertion
895 */
896
897 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
zhenhuahb6d5edc2013-06-19 14:37:32 +0800898 !dcp && !prop_charger && !floated_charger) ||
899 test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham502bea32012-08-13 15:34:20 -0700900 enable_irq(motg->irq);
901 return -EBUSY;
902 }
903
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530904 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530905 * Chipidea 45-nm PHY suspend sequence:
906 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530907 * Interrupt Latch Register auto-clear feature is not present
908 * in all PHY versions. Latch register is clear on read type.
909 * Clear latch register to avoid spurious wakeup from
910 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530911 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530912 * PHY comparators are disabled when PHY enters into low power
913 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
914 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
915 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530916 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530917 * PLL is not turned off when PHY enters into low power mode (LPM).
918 * Disable PLL for maximum power savings.
919 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530920
921 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200922 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530923 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200924 ulpi_write(phy, 0x01, 0x30);
925 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530926 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530927
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530929 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530930 * PHY may take some time or even fail to enter into low power
931 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
932 * in failure case.
933 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530934 portsc = readl_relaxed(USB_PORTSC);
935 if (!(portsc & PORTSC_PHCD)) {
936 writel_relaxed(portsc | PORTSC_PHCD,
937 USB_PORTSC);
938 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
939 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
940 break;
941 udelay(1);
942 cnt++;
943 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530944 }
945
946 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200947 dev_err(phy->dev, "Unable to suspend PHY\n");
948 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530949 enable_irq(motg->irq);
950 return -ETIMEDOUT;
951 }
952
953 /*
954 * PHY has capability to generate interrupt asynchronously in low
955 * power mode (LPM). This interrupt is level triggered. So USB IRQ
956 * line must be disabled till async interrupt enable bit is cleared
957 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
958 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530959 *
960 * PHY retention mode is disallowed while entering to LPM with wall
961 * charger connected. But PHY is put into suspend mode. Hence
962 * enable asynchronous interrupt to detect charger disconnection when
963 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530965 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300966 if (host_bus_suspend || device_bus_suspend ||
Pavankumar Kondeti70970b72013-05-16 13:37:24 +0530967 (motg->pdata->otg_control == OTG_PHY_CONTROL))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530968 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
969 else
970 cmd_val |= ULPI_STP_CTRL;
971 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530972
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530973 /*
974 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
975 * PHY retention and collapse can not happen with VDP_SRC enabled.
976 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300977 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
978 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200979 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530980 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +0200981 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530982 if ((motg->pdata->mode == USB_OTG) ||
983 (motg->pdata->mode == USB_HOST))
984 phy_ctrl_val |= (PHY_IDHV_INTEN |
985 PHY_OTGSESSVLDHV_INTEN);
986 else
987 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
988 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530989
Amit Blay58b31472011-11-18 09:39:39 +0200990 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530992 }
993
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 /* Ensure that above operation is completed before turning off clocks */
995 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300996 /* Consider clocks on workaround flag only in case of bus suspend */
997 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
998 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
999 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001000 clk_disable_unprepare(motg->pclk);
1001 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001002 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001003 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301004
Anji jonnala7da3f262011-12-02 17:22:14 -08001005 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301006 if (!host_bus_suspend) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301007 if (!IS_ERR(motg->xo_clk)) {
1008 clk_disable_unprepare(motg->xo_clk);
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301009 motg->lpm_flags |= XO_SHUTDOWN;
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301010 } else {
1011 ret = msm_xo_mode_vote(motg->xo_handle,
1012 MSM_XO_MODE_OFF);
1013 if (ret)
1014 dev_err(phy->dev, "%s fail to devote XO %d\n",
1015 __func__, ret);
1016 else
1017 motg->lpm_flags |= XO_SHUTDOWN;
1018 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301019 }
Anji jonnala7da3f262011-12-02 17:22:14 -08001020
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301021 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
1022 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +02001023 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001025 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
1026 !host_bus_suspend && !device_bus_suspend && !dcp) {
1027 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
1028 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +05301029 }
1030
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301031 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301033 msm_hsusb_mhl_switch_enable(motg, 0);
1034 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035
Steve Mucklef132c6c2012-06-06 18:30:57 -07001036 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001037 if (motg->async_irq)
1038 enable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001039 else
1040 enable_irq_wake(motg->irq);
1041
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042 if (motg->pdata->pmic_id_irq)
1043 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001044 if (pdata->otg_control == OTG_PHY_CONTROL &&
1045 pdata->mpm_otgsessvld_int)
1046 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301048 if (bus)
1049 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1050
1051 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001052 /* Enable ASYNC IRQ (if present) during LPM */
1053 if (motg->async_irq)
1054 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301055 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001056 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301057
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001058 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301059
1060 return 0;
1061}
1062
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301063static int msm_otg_resume(struct msm_otg *motg)
1064{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001065 struct usb_phy *phy = &motg->phy;
1066 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001067 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301068 int cnt = 0;
1069 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001070 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001071 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301072
1073 if (!atomic_read(&motg->in_lpm))
1074 return 0;
1075
Jack Pham8978b892012-10-17 16:31:39 -07001076 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301078
Anji jonnala7da3f262011-12-02 17:22:14 -08001079 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301080 if (motg->lpm_flags & XO_SHUTDOWN) {
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05301081 if (!IS_ERR(motg->xo_clk)) {
1082 clk_prepare_enable(motg->xo_clk);
1083 } else {
1084 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1085 if (ret)
1086 dev_err(phy->dev, "%s fail to vote for XO %d\n",
1087 __func__, ret);
1088 }
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301089 motg->lpm_flags &= ~XO_SHUTDOWN;
1090 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301091
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001092 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001093 clk_prepare_enable(motg->core_clk);
1094 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001095 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001096 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301097
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001098 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001099 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001100 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001101 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1102 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1103 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001104 }
1105
1106 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301107 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301108 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001109 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1110 phy_ctrl_val |= PHY_RETEN;
1111 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1112 /* Disable PHY HV interrupts */
1113 phy_ctrl_val &=
1114 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
1115 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001116 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301117 }
1118
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301119 temp = readl(USB_USBCMD);
1120 temp &= ~ASYNC_INTR_CTRL;
1121 temp &= ~ULPI_STP_CTRL;
1122 writel(temp, USB_USBCMD);
1123
1124 /*
1125 * PHY comes out of low power mode (LPM) in case of wakeup
1126 * from asynchronous interrupt.
1127 */
1128 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1129 goto skip_phy_resume;
1130
1131 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1132 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1133 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1134 break;
1135 udelay(1);
1136 cnt++;
1137 }
1138
1139 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1140 /*
1141 * This is a fatal error. Reset the link and
1142 * PHY. USB state can not be restored. Re-insertion
1143 * of USB cable is the only way to get USB working.
1144 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001145 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301146 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001147 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301148 }
1149
1150skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001151 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001152 if (motg->async_irq)
1153 disable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001154 else
1155 disable_irq_wake(motg->irq);
1156
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 if (motg->pdata->pmic_id_irq)
1158 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001159 if (pdata->otg_control == OTG_PHY_CONTROL &&
1160 pdata->mpm_otgsessvld_int)
1161 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301163 if (bus)
1164 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1165
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301166 atomic_set(&motg->in_lpm, 0);
1167
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301168 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001169 /* Match the disable_irq call from ISR */
1170 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301171 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301172 }
Jack Pham8978b892012-10-17 16:31:39 -07001173 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301174
Manu Gautamf8c45642012-08-10 10:20:56 -07001175 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1176 if (motg->async_irq)
1177 disable_irq(motg->async_irq);
1178
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001179 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301180
1181 return 0;
1182}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301183#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301184
David Keitel272ce522012-08-17 16:25:24 -07001185static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001186{
Jack Pham0c695282012-10-19 18:13:03 -07001187 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001188 pr_err("No USB power supply registered!\n");
1189 return;
1190 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001191
Jack Pham0c695282012-10-19 18:13:03 -07001192 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001193 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301194 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001195 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301196 } else {
David Keitel272ce522012-08-17 16:25:24 -07001197 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301198 /*
1199 * VBUS comparator is disabled by PMIC charging driver
1200 * when SYSTEM scope is selected. For ID_GND->ID_A
1201 * transition, give 50 msec delay so that PMIC charger
1202 * driver detect the VBUS and ready for accepting
1203 * charging current value from USB.
1204 */
1205 if (test_bit(ID_A, &motg->inputs))
1206 msleep(50);
1207 }
David Keitel272ce522012-08-17 16:25:24 -07001208 } else {
1209 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001210 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001211 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001212}
1213
David Keitel081a3e22012-04-18 12:37:07 -07001214static int msm_otg_notify_chg_type(struct msm_otg *motg)
1215{
1216 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001217
David Keitel081a3e22012-04-18 12:37:07 -07001218 /*
1219 * TODO
1220 * Unify OTG driver charger types and power supply charger types
1221 */
1222 if (charger_type == motg->chg_type)
1223 return 0;
1224
1225 if (motg->chg_type == USB_SDP_CHARGER)
1226 charger_type = POWER_SUPPLY_TYPE_USB;
1227 else if (motg->chg_type == USB_CDP_CHARGER)
1228 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301229 else if (motg->chg_type == USB_DCP_CHARGER ||
1230 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001231 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1232 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1233 motg->chg_type == USB_ACA_A_CHARGER ||
1234 motg->chg_type == USB_ACA_B_CHARGER ||
1235 motg->chg_type == USB_ACA_C_CHARGER))
1236 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1237 else
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301238 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001239
Jack Pham0c695282012-10-19 18:13:03 -07001240 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001241 pr_err("No USB power supply registered!\n");
1242 return -EINVAL;
1243 }
1244
1245 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001246 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001247 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001248}
1249
Amit Blay0f7edf72012-01-15 10:11:27 +02001250static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1251{
Jack Pham0c695282012-10-19 18:13:03 -07001252 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001253 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1254 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001255 }
1256
David Keitel272ce522012-08-17 16:25:24 -07001257 if (motg->cur_power == 0 && mA > 2) {
1258 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001259 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001260 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001261 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001262 goto psy_error;
1263 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1264 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001265 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001266 goto psy_error;
1267 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001268 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001269 goto psy_error;
Manu Gautamfca298c2013-06-05 15:11:54 +05301270 } else {
1271 if (power_supply_set_online(psy, true))
1272 goto psy_error;
1273 /* Current has changed (100/2 --> 500) */
1274 if (power_supply_set_current_limit(psy, 1000*mA))
1275 goto psy_error;
David Keitel272ce522012-08-17 16:25:24 -07001276 }
Manu Gautamfca298c2013-06-05 15:11:54 +05301277
Jack Pham0c695282012-10-19 18:13:03 -07001278 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001279 return 0;
1280
David Keitel272ce522012-08-17 16:25:24 -07001281psy_error:
1282 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001283 return -ENXIO;
1284}
1285
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301286static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1287{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001288 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301289
1290 if (g && g->is_a_peripheral)
1291 return;
1292
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301293 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1294 motg->chg_type == USB_ACA_A_CHARGER ||
1295 motg->chg_type == USB_ACA_B_CHARGER ||
1296 motg->chg_type == USB_ACA_C_CHARGER) &&
1297 mA > IDEV_ACA_CHG_LIMIT)
1298 mA = IDEV_ACA_CHG_LIMIT;
1299
David Keitel081a3e22012-04-18 12:37:07 -07001300 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001301 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001302 "Failed notifying %d charger type to PMIC\n",
1303 motg->chg_type);
1304
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301305 if (motg->cur_power == mA)
1306 return;
1307
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001308 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001309
1310 /*
1311 * Use Power Supply API if supported, otherwise fallback
1312 * to legacy pm8921 API.
1313 */
1314 if (msm_otg_notify_power_supply(motg, mA))
1315 pm8921_charger_vbus_draw(mA);
1316
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301317 motg->cur_power = mA;
1318}
1319
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001320static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301321{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001322 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301323
1324 /*
1325 * Gadget driver uses set_power method to notify about the
1326 * available current based on suspend/configured states.
1327 *
1328 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1329 * states when CDP/ACA is connected.
1330 */
1331 if (motg->chg_type == USB_SDP_CHARGER)
1332 msm_otg_notify_charger(motg, mA);
1333
1334 return 0;
1335}
1336
Steve Mucklef132c6c2012-06-06 18:30:57 -07001337static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301338{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001339 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301340 struct msm_otg_platform_data *pdata = motg->pdata;
1341 struct usb_hcd *hcd;
1342
1343 if (!otg->host)
1344 return;
1345
1346 hcd = bus_to_hcd(otg->host);
1347
1348 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001349 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301350
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301351 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001352 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301353 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1354
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301355 /*
1356 * Some boards have a switch cotrolled by gpio
1357 * to enable/disable internal HUB. Enable internal
1358 * HUB before kicking the host.
1359 */
1360 if (pdata->setup_gpio)
1361 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301362 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301363 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001364 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301365
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301366 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301367 /* HCD core reset all bits of PORTSC. select ULPI phy */
1368 writel_relaxed(0x80000000, USB_PORTSC);
1369
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301370 if (pdata->setup_gpio)
1371 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301372
1373 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001374 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301375 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301376 }
1377}
1378
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001379static int msm_otg_usbdev_notify(struct notifier_block *self,
1380 unsigned long action, void *priv)
1381{
1382 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001383 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301384 struct usb_device *udev = priv;
1385
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301386 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1387 goto out;
1388
Steve Mucklef132c6c2012-06-06 18:30:57 -07001389 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301390 goto out;
1391 /*
1392 * Interested in devices connected directly to the root hub.
1393 * ACA dock can supply IDEV_CHG irrespective devices connected
1394 * on the accessory port.
1395 */
1396 if (!udev->parent || udev->parent->parent ||
1397 motg->chg_type == USB_ACA_DOCK_CHARGER)
1398 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001399
1400 switch (action) {
1401 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301402 if (aca_enabled())
1403 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001404 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301405 pr_debug("B_CONN set\n");
1406 set_bit(B_CONN, &motg->inputs);
1407 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001408 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301409 /*
1410 * OTG PET: A-device must end session within
1411 * 10 sec after PET enumeration.
1412 */
1413 if (udev->quirks & USB_QUIRK_OTG_PET)
1414 msm_otg_start_timer(motg, TA_TST_MAINT,
1415 A_TST_MAINT);
1416 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301417 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001418 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419 if (udev->actconfig)
1420 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1421 else
1422 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001423 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301424 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301425 break;
1426 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001427 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1428 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301429 pr_debug("B_CONN clear\n");
1430 clear_bit(B_CONN, &motg->inputs);
1431 /*
1432 * OTG PET: A-device must end session after
1433 * PET disconnection if it is enumerated
1434 * with bcdDevice[0] = 1. USB core sets
1435 * bus->otg_vbus_off for us. clear it here.
1436 */
1437 if (udev->bus->otg_vbus_off) {
1438 udev->bus->otg_vbus_off = 0;
1439 set_bit(A_BUS_DROP, &motg->inputs);
1440 }
1441 queue_work(system_nrt_wq, &motg->sm_work);
1442 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001443 default:
1444 break;
1445 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301446 if (test_bit(ID_A, &motg->inputs))
1447 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1448 motg->mA_port);
1449out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001450 return NOTIFY_OK;
1451}
1452
Mayank Ranae3926882011-12-26 09:47:54 +05301453static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1454{
1455 int ret;
1456 static bool vbus_is_on;
1457
1458 if (vbus_is_on == on)
1459 return;
1460
1461 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301462 ret = motg->pdata->vbus_power(on);
1463 if (!ret)
1464 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301465 return;
1466 }
1467
1468 if (!vbus_otg) {
1469 pr_err("vbus_otg is NULL.");
1470 return;
1471 }
1472
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001473 /*
1474 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001475 * from usb before turning on the boost.
1476 * if exiting host mode disable the boost before enabling to draw
1477 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001478 */
Mayank Ranae3926882011-12-26 09:47:54 +05301479 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001480 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301481 ret = regulator_enable(vbus_otg);
1482 if (ret) {
1483 pr_err("unable to enable vbus_otg\n");
1484 return;
1485 }
1486 vbus_is_on = true;
1487 } else {
1488 ret = regulator_disable(vbus_otg);
1489 if (ret) {
1490 pr_err("unable to disable vbus_otg\n");
1491 return;
1492 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001493 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301494 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301495 }
1496}
1497
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001498static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301499{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001500 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301501 struct usb_hcd *hcd;
1502
1503 /*
1504 * Fail host registration if this board can support
1505 * only peripheral configuration.
1506 */
1507 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001508 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301509 return -ENODEV;
1510 }
1511
Mayank Ranae3926882011-12-26 09:47:54 +05301512 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001513 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301514 if (IS_ERR(vbus_otg)) {
1515 pr_err("Unable to get vbus_otg\n");
Manu Gautam139a8132013-06-04 16:46:50 +05301516 return PTR_ERR(vbus_otg);
Mayank Ranae3926882011-12-26 09:47:54 +05301517 }
1518 }
1519
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301520 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001521 if (otg->phy->state == OTG_STATE_A_HOST) {
1522 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001523 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301524 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301525 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301526 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001527 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301528 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301529 } else {
1530 otg->host = NULL;
1531 }
1532
1533 return 0;
1534 }
1535
1536 hcd = bus_to_hcd(host);
1537 hcd->power_budget = motg->pdata->power_budget;
1538
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301539#ifdef CONFIG_USB_OTG
1540 host->otg_port = 1;
1541#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001542 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1543 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301544 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001545 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301546
1547 /*
1548 * Kick the state machine work, if peripheral is not supported
1549 * or peripheral is already registered with us.
1550 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301551 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001552 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301553 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301554 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301555
1556 return 0;
1557}
1558
Steve Mucklef132c6c2012-06-06 18:30:57 -07001559static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301560{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301561 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001562 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301563 struct msm_otg_platform_data *pdata = motg->pdata;
1564
1565 if (!otg->gadget)
1566 return;
1567
1568 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001569 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301570 /*
1571 * Some boards have a switch cotrolled by gpio
1572 * to enable/disable internal HUB. Disable internal
1573 * HUB before kicking the gadget.
1574 */
1575 if (pdata->setup_gpio)
1576 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001577
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301578 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301579 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301580 ret = msm_bus_scale_client_update_request(
1581 motg->bus_perf_client, 1);
1582 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001583 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301584 "bus bandwidth %d\n", __func__, ret);
1585 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301586 usb_gadget_vbus_connect(otg->gadget);
1587 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001588 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301589 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301590 /* Configure BUS performance parameters to default */
1591 if (motg->bus_perf_client) {
1592 ret = msm_bus_scale_client_update_request(
1593 motg->bus_perf_client, 0);
1594 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001595 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301596 "for bus bw %d\n", __func__, ret);
1597 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301598 if (pdata->setup_gpio)
1599 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1600 }
1601
1602}
1603
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001604static int msm_otg_set_peripheral(struct usb_otg *otg,
Stephen Boyd9850acb2013-01-28 14:11:20 -08001605 struct usb_gadget *gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301606{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001607 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301608
1609 /*
1610 * Fail peripheral registration if this board can support
1611 * only host configuration.
1612 */
1613 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001614 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301615 return -ENODEV;
1616 }
1617
1618 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001619 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1620 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301621 msm_otg_start_peripheral(otg, 0);
1622 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001623 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301624 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301625 } else {
1626 otg->gadget = NULL;
1627 }
1628
1629 return 0;
1630 }
1631 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001632 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301633
1634 /*
1635 * Kick the state machine work, if host is not supported
1636 * or host is already registered with us.
1637 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301638 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001639 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301640 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301641 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301642
1643 return 0;
1644}
1645
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301646static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1647{
1648 unsigned long flags;
1649 int id;
1650
1651 if (!motg->pdata->pmic_id_irq)
1652 return -ENODEV;
1653
1654 local_irq_save(flags);
1655 id = irq_read_line(motg->pdata->pmic_id_irq);
1656 local_irq_restore(flags);
1657
1658 /*
1659 * If we can not read ID line state for some reason, treat
1660 * it as float. This would prevent MHL discovery and kicking
1661 * host mode unnecessarily.
1662 */
1663 return !!id;
1664}
1665
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301666static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1667 void (*callback)(int on))
1668{
1669 struct usb_phy *phy = &motg->phy;
1670 int ret;
1671
Manoj Raoa7bddd12012-08-27 20:36:45 -07001672 if (!motg->pdata->mhl_enable) {
1673 dev_dbg(phy->dev, "MHL feature not enabled\n");
1674 return -ENODEV;
1675 }
1676
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301677 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1678 !motg->pdata->pmic_id_irq) {
1679 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1680 return -ENODEV;
1681 }
1682
1683 if (!motg->pdata->mhl_dev_name) {
1684 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1685 return -ENODEV;
1686 }
1687
1688 if (callback)
1689 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1690 callback);
1691 else
1692 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1693
1694 if (ret)
1695 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1696 motg->pdata->mhl_dev_name, ret);
1697 else
1698 motg->mhl_enabled = true;
1699
1700 return ret;
1701}
1702
1703static void msm_otg_mhl_notify_online(int on)
1704{
1705 struct msm_otg *motg = the_msm_otg;
1706 struct usb_phy *phy = &motg->phy;
1707 bool queue = false;
1708
1709 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1710
1711 if (on) {
1712 set_bit(MHL, &motg->inputs);
1713 } else {
1714 clear_bit(MHL, &motg->inputs);
1715 queue = true;
1716 }
1717
1718 if (queue && phy->state != OTG_STATE_UNDEFINED)
1719 schedule_work(&motg->sm_work);
1720}
1721
1722static bool msm_otg_is_mhl(struct msm_otg *motg)
1723{
1724 struct usb_phy *phy = &motg->phy;
1725 int is_mhl, ret;
1726
1727 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1728 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1729 /*
1730 * MHL driver calls our callback saying that MHL connected
1731 * if RID_GND is detected. But at later part of discovery
1732 * it may figure out MHL is not connected and returns
1733 * false. Hence clear MHL input here.
1734 */
1735 clear_bit(MHL, &motg->inputs);
1736 dev_dbg(phy->dev, "MHL device not found\n");
1737 return false;
1738 }
1739
1740 set_bit(MHL, &motg->inputs);
1741 dev_dbg(phy->dev, "MHL device found\n");
1742 return true;
1743}
1744
1745static bool msm_chg_mhl_detect(struct msm_otg *motg)
1746{
1747 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301748
1749 if (!motg->mhl_enabled)
1750 return false;
1751
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301752 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301753
1754 if (id)
1755 return false;
1756
1757 mhl_det_in_progress = true;
1758 ret = msm_otg_is_mhl(motg);
1759 mhl_det_in_progress = false;
1760
1761 return ret;
1762}
1763
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301764static void msm_otg_chg_check_timer_func(unsigned long data)
1765{
1766 struct msm_otg *motg = (struct msm_otg *) data;
1767 struct usb_otg *otg = motg->phy.otg;
1768
1769 if (atomic_read(&motg->in_lpm) ||
1770 !test_bit(B_SESS_VLD, &motg->inputs) ||
1771 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1772 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1773 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1774 return;
1775 }
1776
1777 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1778 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1779 set_bit(B_FALSE_SDP, &motg->inputs);
1780 queue_work(system_nrt_wq, &motg->sm_work);
1781 }
1782}
1783
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001784static bool msm_chg_aca_detect(struct msm_otg *motg)
1785{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001786 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001787 u32 int_sts;
1788 bool ret = false;
1789
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301790 if (!aca_enabled())
1791 goto out;
1792
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001793 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1794 goto out;
1795
Steve Mucklef132c6c2012-06-06 18:30:57 -07001796 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001797 switch (int_sts & 0x1C) {
1798 case 0x08:
1799 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001800 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001801 motg->chg_type = USB_ACA_A_CHARGER;
1802 motg->chg_state = USB_CHG_STATE_DETECTED;
1803 clear_bit(ID_B, &motg->inputs);
1804 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301805 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001806 ret = true;
1807 }
1808 break;
1809 case 0x0C:
1810 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001811 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001812 motg->chg_type = USB_ACA_B_CHARGER;
1813 motg->chg_state = USB_CHG_STATE_DETECTED;
1814 clear_bit(ID_A, &motg->inputs);
1815 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301816 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001817 ret = true;
1818 }
1819 break;
1820 case 0x10:
1821 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001822 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823 motg->chg_type = USB_ACA_C_CHARGER;
1824 motg->chg_state = USB_CHG_STATE_DETECTED;
1825 clear_bit(ID_A, &motg->inputs);
1826 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301827 set_bit(ID, &motg->inputs);
1828 ret = true;
1829 }
1830 break;
1831 case 0x04:
1832 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001833 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301834 motg->chg_type = USB_INVALID_CHARGER;
1835 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1836 clear_bit(ID_A, &motg->inputs);
1837 clear_bit(ID_B, &motg->inputs);
1838 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001839 ret = true;
1840 }
1841 break;
1842 default:
1843 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1844 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301845 test_and_clear_bit(ID_C, &motg->inputs) |
1846 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001847 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001848 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 motg->chg_type = USB_INVALID_CHARGER;
1850 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1851 }
1852 }
1853out:
1854 return ret;
1855}
1856
1857static void msm_chg_enable_aca_det(struct msm_otg *motg)
1858{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001859 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001860
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301861 if (!aca_enabled())
1862 return;
1863
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001864 switch (motg->pdata->phy_type) {
1865 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301866 /* Disable ID_GND in link and PHY */
1867 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1868 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001869 ulpi_write(phy, 0x01, 0x0C);
1870 ulpi_write(phy, 0x10, 0x0F);
1871 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301872 /* Disable PMIC ID pull-up */
1873 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301874 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001875 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301876 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001877 break;
1878 default:
1879 break;
1880 }
1881}
1882
1883static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1884{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001885 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301887 if (!aca_enabled())
1888 return;
1889
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001890 switch (motg->pdata->phy_type) {
1891 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301892 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001893 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301894 break;
1895 default:
1896 break;
1897 }
1898}
1899
1900static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1901{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001902 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301903
1904 if (!aca_enabled())
1905 return;
1906
1907 switch (motg->pdata->phy_type) {
1908 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001909 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001910 break;
1911 default:
1912 break;
1913 }
1914}
1915
1916static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1917{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001918 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001919 bool ret = false;
1920
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301921 if (!aca_enabled())
1922 return ret;
1923
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 switch (motg->pdata->phy_type) {
1925 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001926 if (ulpi_read(phy, 0x91) & 1) {
1927 dev_dbg(phy->dev, "RID change\n");
1928 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929 ret = msm_chg_aca_detect(motg);
1930 }
1931 default:
1932 break;
1933 }
1934 return ret;
1935}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301936
1937static void msm_otg_id_timer_func(unsigned long data)
1938{
1939 struct msm_otg *motg = (struct msm_otg *) data;
1940
1941 if (!aca_enabled())
1942 return;
1943
1944 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001945 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301946 return;
1947 }
1948
Steve Mucklef132c6c2012-06-06 18:30:57 -07001949 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301950 goto out;
1951
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301952 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001953 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301954 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301955 }
1956
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301957out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301958 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1959 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1960}
1961
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301962static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1963{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001964 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301965 u32 chg_det;
1966 bool ret = false;
1967
1968 switch (motg->pdata->phy_type) {
1969 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001970 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301971 ret = chg_det & (1 << 4);
1972 break;
1973 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001974 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301975 ret = chg_det & 1;
1976 break;
1977 default:
1978 break;
1979 }
1980 return ret;
1981}
1982
1983static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1984{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001985 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301986 u32 chg_det;
1987
1988 switch (motg->pdata->phy_type) {
1989 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001990 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301991 /* Turn off charger block */
1992 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001993 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301994 udelay(20);
1995 /* control chg block via ULPI */
1996 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001997 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301998 /* put it in host mode for enabling D- source */
1999 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002000 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302001 /* Turn on chg detect block */
2002 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002003 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302004 udelay(20);
2005 /* enable chg detection */
2006 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002007 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302008 break;
2009 case SNPS_28NM_INTEGRATED_PHY:
2010 /*
2011 * Configure DM as current source, DP as current sink
2012 * and enable battery charging comparators.
2013 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002014 ulpi_write(phy, 0x8, 0x85);
2015 ulpi_write(phy, 0x2, 0x85);
2016 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302017 break;
2018 default:
2019 break;
2020 }
2021}
2022
2023static bool msm_chg_check_primary_det(struct msm_otg *motg)
2024{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002025 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302026 u32 chg_det;
2027 bool ret = false;
2028
2029 switch (motg->pdata->phy_type) {
2030 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002031 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302032 ret = chg_det & (1 << 4);
2033 break;
2034 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002035 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302036 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302037 /* Turn off VDP_SRC */
2038 ulpi_write(phy, 0x3, 0x86);
2039 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302040 break;
2041 default:
2042 break;
2043 }
2044 return ret;
2045}
2046
2047static void msm_chg_enable_primary_det(struct msm_otg *motg)
2048{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002049 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302050 u32 chg_det;
2051
2052 switch (motg->pdata->phy_type) {
2053 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002054 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302055 /* enable chg detection */
2056 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002057 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302058 break;
2059 case SNPS_28NM_INTEGRATED_PHY:
2060 /*
2061 * Configure DP as current source, DM as current sink
2062 * and enable battery charging comparators.
2063 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002064 ulpi_write(phy, 0x2, 0x85);
2065 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066 break;
2067 default:
2068 break;
2069 }
2070}
2071
2072static bool msm_chg_check_dcd(struct msm_otg *motg)
2073{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002074 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302075 u32 line_state;
2076 bool ret = false;
2077
2078 switch (motg->pdata->phy_type) {
2079 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002080 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302081 ret = !(line_state & 1);
2082 break;
2083 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002084 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302085 ret = line_state & 2;
2086 break;
2087 default:
2088 break;
2089 }
2090 return ret;
2091}
2092
2093static void msm_chg_disable_dcd(struct msm_otg *motg)
2094{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002095 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302096 u32 chg_det;
2097
2098 switch (motg->pdata->phy_type) {
2099 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002100 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302101 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002102 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302103 break;
2104 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002105 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302106 break;
2107 default:
2108 break;
2109 }
2110}
2111
2112static void msm_chg_enable_dcd(struct msm_otg *motg)
2113{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002114 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302115 u32 chg_det;
2116
2117 switch (motg->pdata->phy_type) {
2118 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002119 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302120 /* Turn on D+ current source */
2121 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002122 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302123 break;
2124 case SNPS_28NM_INTEGRATED_PHY:
2125 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002126 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302127 break;
2128 default:
2129 break;
2130 }
2131}
2132
2133static void msm_chg_block_on(struct msm_otg *motg)
2134{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002135 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302136 u32 func_ctrl, chg_det;
2137
2138 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002139 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302140 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2141 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002142 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302143
2144 switch (motg->pdata->phy_type) {
2145 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002146 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302147 /* control chg block via ULPI */
2148 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002149 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302150 /* Turn on chg detect block */
2151 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002152 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302153 udelay(20);
2154 break;
2155 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302156 /* disable DP and DM pull down resistors */
2157 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302158 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002159 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302160 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002161 ulpi_write(phy, 0x1F, 0x92);
2162 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302163 udelay(100);
2164 break;
2165 default:
2166 break;
2167 }
2168}
2169
2170static void msm_chg_block_off(struct msm_otg *motg)
2171{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002172 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302173 u32 func_ctrl, chg_det;
2174
2175 switch (motg->pdata->phy_type) {
2176 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002177 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302178 /* Turn off charger block */
2179 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002180 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302181 break;
2182 case SNPS_28NM_INTEGRATED_PHY:
2183 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002184 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302185 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002186 ulpi_write(phy, 0x1F, 0x92);
2187 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302188 break;
2189 default:
2190 break;
2191 }
2192
2193 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002194 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302195 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2196 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002197 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302198}
2199
Anji jonnalad270e2d2011-08-09 11:28:32 +05302200static const char *chg_to_string(enum usb_chg_type chg_type)
2201{
2202 switch (chg_type) {
2203 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2204 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2205 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2206 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2207 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2208 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2209 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302210 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
zhenhuahb6d5edc2013-06-19 14:37:32 +08002211 case USB_FLOATED_CHARGER: return "USB_FLOATED_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302212 default: return "INVALID_CHARGER";
2213 }
2214}
2215
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302216#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2217#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302218#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2219#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302220static void msm_chg_detect_work(struct work_struct *w)
2221{
2222 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002223 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302224 bool is_dcd = false, tmout, vout, is_aca;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002225 static bool dcd;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302226 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302227 unsigned long delay;
2228
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002229 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302230
2231 if (test_bit(MHL, &motg->inputs)) {
2232 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2233 return;
2234 }
2235
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302236 switch (motg->chg_state) {
2237 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302238 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302239 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002240 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302241 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302242 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302243 delay = MSM_CHG_DCD_POLL_TIME;
2244 break;
2245 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302246 if (msm_chg_mhl_detect(motg)) {
2247 msm_chg_block_off(motg);
2248 motg->chg_state = USB_CHG_STATE_DETECTED;
2249 motg->chg_type = USB_INVALID_CHARGER;
2250 queue_work(system_nrt_wq, &motg->sm_work);
2251 return;
2252 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002253 is_aca = msm_chg_aca_detect(motg);
2254 if (is_aca) {
2255 /*
2256 * ID_A can be ACA dock too. continue
2257 * primary detection after DCD.
2258 */
2259 if (test_bit(ID_A, &motg->inputs)) {
2260 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2261 } else {
2262 delay = 0;
2263 break;
2264 }
2265 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302266 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302267 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2268 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302269 if (is_dcd || tmout) {
zhenhuahb6d5edc2013-06-19 14:37:32 +08002270 if (is_dcd)
2271 dcd = true;
2272 else
2273 dcd = false;
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302274 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302275 msm_chg_enable_primary_det(motg);
2276 delay = MSM_CHG_PRIMARY_DET_TIME;
2277 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2278 } else {
2279 delay = MSM_CHG_DCD_POLL_TIME;
2280 }
2281 break;
2282 case USB_CHG_STATE_DCD_DONE:
2283 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302284 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2285 dm_vlgc = line_state & PORTSC_LS_DM;
2286 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302287 if (test_bit(ID_A, &motg->inputs)) {
2288 motg->chg_type = USB_ACA_DOCK_CHARGER;
2289 motg->chg_state = USB_CHG_STATE_DETECTED;
2290 delay = 0;
2291 break;
2292 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302293 if (line_state) { /* DP > VLGC */
2294 motg->chg_type = USB_PROPRIETARY_CHARGER;
2295 motg->chg_state = USB_CHG_STATE_DETECTED;
2296 delay = 0;
2297 } else {
2298 msm_chg_enable_secondary_det(motg);
2299 delay = MSM_CHG_SECONDARY_DET_TIME;
2300 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2301 }
2302 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302303 if (test_bit(ID_A, &motg->inputs)) {
2304 motg->chg_type = USB_ACA_A_CHARGER;
2305 motg->chg_state = USB_CHG_STATE_DETECTED;
2306 delay = 0;
2307 break;
2308 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302309
2310 if (line_state) /* DP > VLGC or/and DM > VLGC */
2311 motg->chg_type = USB_PROPRIETARY_CHARGER;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002312 else if (!dcd && floated_charger_enable)
2313 motg->chg_type = USB_FLOATED_CHARGER;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302314 else
2315 motg->chg_type = USB_SDP_CHARGER;
2316
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302317 motg->chg_state = USB_CHG_STATE_DETECTED;
2318 delay = 0;
2319 }
2320 break;
2321 case USB_CHG_STATE_PRIMARY_DONE:
2322 vout = msm_chg_check_secondary_det(motg);
2323 if (vout)
2324 motg->chg_type = USB_DCP_CHARGER;
2325 else
2326 motg->chg_type = USB_CDP_CHARGER;
2327 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2328 /* fall through */
2329 case USB_CHG_STATE_SECONDARY_DONE:
2330 motg->chg_state = USB_CHG_STATE_DETECTED;
2331 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302332 /*
2333 * Notify the charger type to power supply
2334 * owner as soon as we determine the charger.
2335 */
2336 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302337 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002338 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302339 /*
2340 * Spurious interrupt is seen after enabling ACA detection
2341 * due to which charger detection fails in case of PET.
2342 * Add delay of 100 microsec to avoid that.
2343 */
2344 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002345 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002346 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302347 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302348 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302349 return;
2350 default:
2351 return;
2352 }
2353
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302354 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302355}
2356
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302357/*
2358 * We support OTG, Peripheral only and Host only configurations. In case
2359 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2360 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2361 * enabled when switch is controlled by user and default mode is supplied
2362 * by board file, which can be changed by userspace later.
2363 */
2364static void msm_otg_init_sm(struct msm_otg *motg)
2365{
2366 struct msm_otg_platform_data *pdata = motg->pdata;
2367 u32 otgsc = readl(USB_OTGSC);
2368
2369 switch (pdata->mode) {
2370 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002371 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302372 if (pdata->default_mode == USB_HOST) {
2373 clear_bit(ID, &motg->inputs);
2374 } else if (pdata->default_mode == USB_PERIPHERAL) {
2375 set_bit(ID, &motg->inputs);
2376 set_bit(B_SESS_VLD, &motg->inputs);
2377 } else {
2378 set_bit(ID, &motg->inputs);
2379 clear_bit(B_SESS_VLD, &motg->inputs);
2380 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302381 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302382 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302383 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302384 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302385 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302386 set_bit(A_BUS_REQ, &motg->inputs);
2387 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002388 if (otgsc & OTGSC_BSV)
2389 set_bit(B_SESS_VLD, &motg->inputs);
2390 else
2391 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302392 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302393 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302394 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302395 set_bit(ID, &motg->inputs);
2396 else
2397 clear_bit(ID, &motg->inputs);
2398 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302399 /*
2400 * VBUS initial state is reported after PMIC
2401 * driver initialization. Wait for it.
2402 */
2403 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302404 }
2405 break;
2406 case USB_HOST:
2407 clear_bit(ID, &motg->inputs);
2408 break;
2409 case USB_PERIPHERAL:
2410 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302411 if (pdata->otg_control == OTG_PHY_CONTROL) {
2412 if (otgsc & OTGSC_BSV)
2413 set_bit(B_SESS_VLD, &motg->inputs);
2414 else
2415 clear_bit(B_SESS_VLD, &motg->inputs);
2416 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2417 /*
2418 * VBUS initial state is reported after PMIC
2419 * driver initialization. Wait for it.
2420 */
2421 wait_for_completion(&pmic_vbus_init);
2422 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302423 break;
2424 default:
2425 break;
2426 }
2427}
2428
2429static void msm_otg_sm_work(struct work_struct *w)
2430{
2431 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002432 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302433 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302434
Steve Mucklef132c6c2012-06-06 18:30:57 -07002435 pm_runtime_resume(otg->phy->dev);
2436 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002437 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302438 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002439 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302440 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002441 if (!psy && legacy_power_supply) {
2442 psy = power_supply_get_by_name("usb");
2443
2444 if (!psy)
2445 pr_err("couldn't get usb power supply\n");
2446 }
2447
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002448 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302449 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2450 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002451 pm_runtime_put_noidle(otg->phy->dev);
2452 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302453 break;
2454 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302455 /* FALL THROUGH */
2456 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302457 if (test_bit(MHL, &motg->inputs)) {
2458 /* allow LPM */
2459 pm_runtime_put_noidle(otg->phy->dev);
2460 pm_runtime_suspend(otg->phy->dev);
2461 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002462 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302463 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302464 if (msm_chg_mhl_detect(motg)) {
2465 work = 1;
2466 break;
2467 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302468 clear_bit(B_BUS_REQ, &motg->inputs);
2469 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002470 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302471 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302472 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302473 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302474 switch (motg->chg_state) {
2475 case USB_CHG_STATE_UNDEFINED:
2476 msm_chg_detect_work(&motg->chg_work.work);
2477 break;
2478 case USB_CHG_STATE_DETECTED:
2479 switch (motg->chg_type) {
2480 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302481 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002482 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302483 /* fall through */
2484 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302485 msm_otg_notify_charger(motg,
2486 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002487 pm_runtime_put_noidle(otg->phy->dev);
2488 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302489 break;
zhenhuahb6d5edc2013-06-19 14:37:32 +08002490 case USB_FLOATED_CHARGER:
2491 msm_otg_notify_charger(motg,
2492 IDEV_CHG_MAX);
2493 pm_runtime_put_noidle(otg->phy->dev);
2494 pm_runtime_suspend(otg->phy->dev);
2495 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302496 case USB_ACA_B_CHARGER:
2497 msm_otg_notify_charger(motg,
2498 IDEV_ACA_CHG_MAX);
2499 /*
2500 * (ID_B --> ID_C) PHY_ALT interrupt can
2501 * not be detected in LPM.
2502 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302503 break;
2504 case USB_CDP_CHARGER:
2505 msm_otg_notify_charger(motg,
2506 IDEV_CHG_MAX);
2507 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002508 otg->phy->state =
2509 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302510 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302511 case USB_ACA_C_CHARGER:
2512 msm_otg_notify_charger(motg,
2513 IDEV_ACA_CHG_MAX);
2514 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002515 otg->phy->state =
2516 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302517 break;
2518 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302519 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002520 otg->phy->state =
2521 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302522 mod_timer(&motg->chg_check_timer,
2523 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302524 break;
2525 default:
2526 break;
2527 }
2528 break;
2529 default:
2530 break;
2531 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302532 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2533 pr_debug("b_sess_end && b_bus_req\n");
2534 if (msm_otg_start_srp(otg) < 0) {
2535 clear_bit(B_BUS_REQ, &motg->inputs);
2536 work = 1;
2537 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302538 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002539 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302540 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2541 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302542 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302543 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302544 del_timer_sync(&motg->chg_check_timer);
2545 clear_bit(B_FALSE_SDP, &motg->inputs);
Mayank Rana8ab00352013-01-23 19:26:21 +05302546 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302547 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302548 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2549 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302550 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002551 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302552 /*
2553 * There is a small window where ID interrupt
2554 * is not monitored during ID detection circuit
2555 * switch from ACA to PMIC. Check ID state
2556 * before entering into low power mode.
2557 */
2558 if (!msm_otg_read_pmic_id_state(motg)) {
2559 pr_debug("process missed ID intr\n");
2560 clear_bit(ID, &motg->inputs);
2561 work = 1;
2562 break;
2563 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002564 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002565 /*
2566 * Only if autosuspend was enabled in probe, it will be
2567 * used here. Otherwise, no delay will be used.
2568 */
2569 pm_runtime_mark_last_busy(otg->phy->dev);
2570 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302571 }
2572 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302573 case OTG_STATE_B_SRP_INIT:
2574 if (!test_bit(ID, &motg->inputs) ||
2575 test_bit(ID_A, &motg->inputs) ||
2576 test_bit(ID_C, &motg->inputs) ||
2577 (test_bit(B_SESS_VLD, &motg->inputs) &&
2578 !test_bit(ID_B, &motg->inputs))) {
2579 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2580 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002581 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302582 /*
2583 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2584 * bits after SRP initiation.
2585 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002586 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302587 work = 1;
2588 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2589 pr_debug("b_srp_fail\n");
2590 pr_info("A-device did not respond to SRP\n");
2591 clear_bit(B_BUS_REQ, &motg->inputs);
2592 clear_bit(B_SRP_FAIL, &motg->tmouts);
2593 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002594 ulpi_write(otg->phy, 0x0, 0x98);
2595 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302596 motg->b_last_se0_sess = jiffies;
2597 work = 1;
2598 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302599 break;
2600 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302601 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2602 test_bit(B_FALSE_SDP, &motg->inputs)) {
2603 pr_debug("B_FALSE_SDP\n");
2604 msm_otg_start_peripheral(otg, 0);
2605 motg->chg_type = USB_DCP_CHARGER;
2606 clear_bit(B_FALSE_SDP, &motg->inputs);
2607 otg->phy->state = OTG_STATE_B_IDLE;
2608 work = 1;
2609 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302610 test_bit(ID_A, &motg->inputs) ||
2611 test_bit(ID_B, &motg->inputs) ||
2612 !test_bit(B_SESS_VLD, &motg->inputs)) {
2613 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302614 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2615 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302616 msm_otg_notify_charger(motg, 0);
2617 srp_reqd = otg->gadget->otg_srp_reqd;
2618 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302619 if (test_bit(ID_B, &motg->inputs))
2620 clear_bit(ID_B, &motg->inputs);
2621 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002622 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302623 motg->b_last_se0_sess = jiffies;
2624 if (srp_reqd)
2625 msm_otg_start_timer(motg,
2626 TB_TST_SRP, B_TST_SRP);
2627 else
2628 work = 1;
2629 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2630 otg->gadget->b_hnp_enable &&
2631 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2632 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2633 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2634 /* D+ pullup should not be disconnected within 4msec
2635 * after A device suspends the bus. Otherwise PET will
2636 * fail the compliance test.
2637 */
2638 udelay(1000);
2639 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002640 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302641 /*
2642 * start HCD even before A-device enable
2643 * pull-up to meet HNP timings.
2644 */
2645 otg->host->is_b_host = 1;
2646 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002647 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2648 test_bit(B_SESS_VLD, &motg->inputs)) {
2649 pr_debug("a_bus_suspend && b_sess_vld\n");
2650 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002651 pm_runtime_put_noidle(otg->phy->dev);
2652 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002653 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002654 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302655 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002656 }
2657 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302658 case OTG_STATE_B_WAIT_ACON:
2659 if (!test_bit(ID, &motg->inputs) ||
2660 test_bit(ID_A, &motg->inputs) ||
2661 test_bit(ID_B, &motg->inputs) ||
2662 !test_bit(B_SESS_VLD, &motg->inputs)) {
2663 pr_debug("!id || id_a/b || !b_sess_vld\n");
2664 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302665 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302666 * A-device is physically disconnected during
2667 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302668 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302669 msm_otg_start_host(otg, 0);
2670 otg->host->is_b_host = 0;
2671
2672 clear_bit(B_BUS_REQ, &motg->inputs);
2673 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2674 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002675 otg->phy->state = OTG_STATE_B_IDLE;
2676 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302677 work = 1;
2678 } else if (test_bit(A_CONN, &motg->inputs)) {
2679 pr_debug("a_conn\n");
2680 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002681 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302682 /*
2683 * PET disconnects D+ pullup after reset is generated
2684 * by B device in B_HOST role which is not detected by
2685 * B device. As workaorund , start timer of 300msec
2686 * and stop timer if A device is enumerated else clear
2687 * A_CONN.
2688 */
2689 msm_otg_start_timer(motg, TB_TST_CONFIG,
2690 B_TST_CONFIG);
2691 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2692 pr_debug("b_ase0_brst_tmout\n");
2693 pr_info("B HNP fail:No response from A device\n");
2694 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002695 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302696 otg->host->is_b_host = 0;
2697 clear_bit(B_ASE0_BRST, &motg->tmouts);
2698 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2699 clear_bit(B_BUS_REQ, &motg->inputs);
2700 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002701 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302702 work = 1;
2703 } else if (test_bit(ID_C, &motg->inputs)) {
2704 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2705 }
2706 break;
2707 case OTG_STATE_B_HOST:
2708 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2709 !test_bit(A_CONN, &motg->inputs) ||
2710 !test_bit(B_SESS_VLD, &motg->inputs)) {
2711 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2712 clear_bit(A_CONN, &motg->inputs);
2713 clear_bit(B_BUS_REQ, &motg->inputs);
2714 msm_otg_start_host(otg, 0);
2715 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002716 otg->phy->state = OTG_STATE_B_IDLE;
2717 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302718 work = 1;
2719 } else if (test_bit(ID_C, &motg->inputs)) {
2720 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2721 }
2722 break;
2723 case OTG_STATE_A_IDLE:
2724 otg->default_a = 1;
2725 if (test_bit(ID, &motg->inputs) &&
2726 !test_bit(ID_A, &motg->inputs)) {
2727 pr_debug("id && !id_a\n");
2728 otg->default_a = 0;
2729 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002730 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302731 del_timer_sync(&motg->id_timer);
2732 msm_otg_link_reset(motg);
2733 msm_chg_enable_aca_intr(motg);
2734 msm_otg_notify_charger(motg, 0);
2735 work = 1;
2736 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2737 (test_bit(A_SRP_DET, &motg->inputs) ||
2738 test_bit(A_BUS_REQ, &motg->inputs))) {
2739 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2740
2741 clear_bit(A_SRP_DET, &motg->inputs);
2742 /* Disable SRP detection */
2743 writel_relaxed((readl_relaxed(USB_OTGSC) &
2744 ~OTGSC_INTSTS_MASK) &
2745 ~OTGSC_DPIE, USB_OTGSC);
2746
Steve Mucklef132c6c2012-06-06 18:30:57 -07002747 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302748 /* VBUS should not be supplied before end of SRP pulse
2749 * generated by PET, if not complaince test fail.
2750 */
2751 usleep_range(10000, 12000);
2752 /* ACA: ID_A: Stop charging untill enumeration */
2753 if (test_bit(ID_A, &motg->inputs))
2754 msm_otg_notify_charger(motg, 0);
2755 else
2756 msm_hsusb_vbus_power(motg, 1);
2757 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2758 } else {
2759 pr_debug("No session requested\n");
2760 clear_bit(A_BUS_DROP, &motg->inputs);
2761 if (test_bit(ID_A, &motg->inputs)) {
2762 msm_otg_notify_charger(motg,
2763 IDEV_ACA_CHG_MAX);
2764 } else if (!test_bit(ID, &motg->inputs)) {
2765 msm_otg_notify_charger(motg, 0);
2766 /*
2767 * A-device is not providing power on VBUS.
2768 * Enable SRP detection.
2769 */
2770 writel_relaxed(0x13, USB_USBMODE);
2771 writel_relaxed((readl_relaxed(USB_OTGSC) &
2772 ~OTGSC_INTSTS_MASK) |
2773 OTGSC_DPIE, USB_OTGSC);
2774 mb();
2775 }
2776 }
2777 break;
2778 case OTG_STATE_A_WAIT_VRISE:
2779 if ((test_bit(ID, &motg->inputs) &&
2780 !test_bit(ID_A, &motg->inputs)) ||
2781 test_bit(A_BUS_DROP, &motg->inputs) ||
2782 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2783 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2784 clear_bit(A_BUS_REQ, &motg->inputs);
2785 msm_otg_del_timer(motg);
2786 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002787 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302788 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2789 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2790 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002791 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302792 if (TA_WAIT_BCON > 0)
2793 msm_otg_start_timer(motg, TA_WAIT_BCON,
2794 A_WAIT_BCON);
2795 msm_otg_start_host(otg, 1);
2796 msm_chg_enable_aca_det(motg);
2797 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302798 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302799 if (msm_chg_check_aca_intr(motg))
2800 work = 1;
2801 }
2802 break;
2803 case OTG_STATE_A_WAIT_BCON:
2804 if ((test_bit(ID, &motg->inputs) &&
2805 !test_bit(ID_A, &motg->inputs)) ||
2806 test_bit(A_BUS_DROP, &motg->inputs) ||
2807 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2808 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2809 "a_wait_bcon_tmout\n");
2810 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2811 pr_info("Device No Response\n");
2812 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2813 }
2814 msm_otg_del_timer(motg);
2815 clear_bit(A_BUS_REQ, &motg->inputs);
2816 clear_bit(B_CONN, &motg->inputs);
2817 msm_otg_start_host(otg, 0);
2818 /*
2819 * ACA: ID_A with NO accessory, just the A plug is
2820 * attached to ACA: Use IDCHG_MAX for charging
2821 */
2822 if (test_bit(ID_A, &motg->inputs))
2823 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2824 else
2825 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002826 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302827 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2828 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2829 pr_debug("!a_vbus_vld\n");
2830 clear_bit(B_CONN, &motg->inputs);
2831 msm_otg_del_timer(motg);
2832 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002833 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2834 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302835 } else if (test_bit(ID_A, &motg->inputs)) {
2836 msm_hsusb_vbus_power(motg, 0);
2837 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2838 /*
2839 * If TA_WAIT_BCON is infinite, we don;t
2840 * turn off VBUS. Enter low power mode.
2841 */
2842 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002843 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302844 } else if (!test_bit(ID, &motg->inputs)) {
2845 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302846 }
2847 break;
2848 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302849 if ((test_bit(ID, &motg->inputs) &&
2850 !test_bit(ID_A, &motg->inputs)) ||
2851 test_bit(A_BUS_DROP, &motg->inputs)) {
2852 pr_debug("id_a/b/c || a_bus_drop\n");
2853 clear_bit(B_CONN, &motg->inputs);
2854 clear_bit(A_BUS_REQ, &motg->inputs);
2855 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002856 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302857 msm_otg_start_host(otg, 0);
2858 if (!test_bit(ID_A, &motg->inputs))
2859 msm_hsusb_vbus_power(motg, 0);
2860 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2861 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2862 pr_debug("!a_vbus_vld\n");
2863 clear_bit(B_CONN, &motg->inputs);
2864 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002865 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302866 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002867 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302868 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2869 /*
2870 * a_bus_req is de-asserted when root hub is
2871 * suspended or HNP is in progress.
2872 */
2873 pr_debug("!a_bus_req\n");
2874 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002875 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302876 if (otg->host->b_hnp_enable)
2877 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2878 A_AIDL_BDIS);
2879 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002880 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302881 } else if (!test_bit(B_CONN, &motg->inputs)) {
2882 pr_debug("!b_conn\n");
2883 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002884 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302885 if (TA_WAIT_BCON > 0)
2886 msm_otg_start_timer(motg, TA_WAIT_BCON,
2887 A_WAIT_BCON);
2888 if (msm_chg_check_aca_intr(motg))
2889 work = 1;
2890 } else if (test_bit(ID_A, &motg->inputs)) {
2891 msm_otg_del_timer(motg);
2892 msm_hsusb_vbus_power(motg, 0);
2893 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2894 msm_otg_notify_charger(motg,
2895 IDEV_ACA_CHG_MAX);
2896 else
2897 msm_otg_notify_charger(motg,
2898 IDEV_CHG_MIN - motg->mA_port);
2899 } else if (!test_bit(ID, &motg->inputs)) {
2900 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2901 motg->chg_type = USB_INVALID_CHARGER;
2902 msm_otg_notify_charger(motg, 0);
2903 msm_hsusb_vbus_power(motg, 1);
2904 }
2905 break;
2906 case OTG_STATE_A_SUSPEND:
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_AIDL_BDIS, &motg->tmouts)) {
2911 pr_debug("id_a/b/c || a_bus_drop ||"
2912 "a_aidl_bdis_tmout\n");
2913 msm_otg_del_timer(motg);
2914 clear_bit(B_CONN, &motg->inputs);
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_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002917 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302918 if (!test_bit(ID_A, &motg->inputs))
2919 msm_hsusb_vbus_power(motg, 0);
2920 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2921 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2922 pr_debug("!a_vbus_vld\n");
2923 msm_otg_del_timer(motg);
2924 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002925 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302926 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002927 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302928 } else if (!test_bit(B_CONN, &motg->inputs) &&
2929 otg->host->b_hnp_enable) {
2930 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002931 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302932 msm_otg_host_hnp_enable(otg, 1);
2933 otg->gadget->is_a_peripheral = 1;
2934 msm_otg_start_peripheral(otg, 1);
2935 } else if (!test_bit(B_CONN, &motg->inputs) &&
2936 !otg->host->b_hnp_enable) {
2937 pr_debug("!b_conn && !b_hnp_enable");
2938 /*
2939 * bus request is dropped during suspend.
2940 * acquire again for next device.
2941 */
2942 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002943 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302944 if (TA_WAIT_BCON > 0)
2945 msm_otg_start_timer(motg, TA_WAIT_BCON,
2946 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002947 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302948 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002949 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302950 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002951 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002952 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302953 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302954 }
2955 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302956 case OTG_STATE_A_PERIPHERAL:
2957 if ((test_bit(ID, &motg->inputs) &&
2958 !test_bit(ID_A, &motg->inputs)) ||
2959 test_bit(A_BUS_DROP, &motg->inputs)) {
2960 pr_debug("id _f/b/c || a_bus_drop\n");
2961 /* Clear BIDL_ADIS timer */
2962 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002963 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302964 msm_otg_start_peripheral(otg, 0);
2965 otg->gadget->is_a_peripheral = 0;
2966 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002967 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302968 if (!test_bit(ID_A, &motg->inputs))
2969 msm_hsusb_vbus_power(motg, 0);
2970 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2971 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2972 pr_debug("!a_vbus_vld\n");
2973 /* Clear BIDL_ADIS timer */
2974 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002975 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302976 msm_otg_start_peripheral(otg, 0);
2977 otg->gadget->is_a_peripheral = 0;
2978 msm_otg_start_host(otg, 0);
2979 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2980 pr_debug("a_bidl_adis_tmout\n");
2981 msm_otg_start_peripheral(otg, 0);
2982 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002983 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302984 set_bit(A_BUS_REQ, &motg->inputs);
2985 msm_otg_host_hnp_enable(otg, 0);
2986 if (TA_WAIT_BCON > 0)
2987 msm_otg_start_timer(motg, TA_WAIT_BCON,
2988 A_WAIT_BCON);
2989 } else if (test_bit(ID_A, &motg->inputs)) {
2990 msm_hsusb_vbus_power(motg, 0);
2991 msm_otg_notify_charger(motg,
2992 IDEV_CHG_MIN - motg->mA_port);
2993 } else if (!test_bit(ID, &motg->inputs)) {
2994 msm_otg_notify_charger(motg, 0);
2995 msm_hsusb_vbus_power(motg, 1);
2996 }
2997 break;
2998 case OTG_STATE_A_WAIT_VFALL:
2999 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
3000 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003001 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303002 work = 1;
3003 }
3004 break;
3005 case OTG_STATE_A_VBUS_ERR:
3006 if ((test_bit(ID, &motg->inputs) &&
3007 !test_bit(ID_A, &motg->inputs)) ||
3008 test_bit(A_BUS_DROP, &motg->inputs) ||
3009 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003010 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303011 if (!test_bit(ID_A, &motg->inputs))
3012 msm_hsusb_vbus_power(motg, 0);
3013 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
3014 motg->chg_state = USB_CHG_STATE_UNDEFINED;
3015 motg->chg_type = USB_INVALID_CHARGER;
3016 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303017 }
3018 break;
3019 default:
3020 break;
3021 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303022 if (work)
3023 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303024}
3025
Amit Blayd0fe07b2012-09-05 16:42:09 +03003026static void msm_otg_suspend_work(struct work_struct *w)
3027{
3028 struct msm_otg *motg =
3029 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05303030
3031 /* This work is only for device bus suspend */
3032 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
3033 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003034}
3035
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303036static irqreturn_t msm_otg_irq(int irq, void *data)
3037{
3038 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003039 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303040 u32 otgsc = 0, usbsts, pc;
3041 bool work = 0;
3042 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303043
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303044 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07003045 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303046 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07003047 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07003048 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07003049 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303050 return IRQ_HANDLED;
3051 }
3052
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003053 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303054 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303055
3056 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303057 return IRQ_NONE;
3058
3059 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303060 if (otgsc & OTGSC_ID) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003061 dev_dbg(otg->phy->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303062 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303063 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003064 dev_dbg(otg->phy->dev, "ID clear\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303065 /*
3066 * Assert a_bus_req to supply power on
3067 * VBUS when Micro/Mini-A cable is connected
3068 * with out user intervention.
3069 */
3070 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303071 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303072 msm_chg_enable_aca_det(motg);
3073 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303074 writel_relaxed(otgsc, USB_OTGSC);
3075 work = 1;
3076 } else if (otgsc & OTGSC_DPIS) {
3077 pr_debug("DPIS detected\n");
3078 writel_relaxed(otgsc, USB_OTGSC);
3079 set_bit(A_SRP_DET, &motg->inputs);
3080 set_bit(A_BUS_REQ, &motg->inputs);
3081 work = 1;
Vamsi Krishnaef6e1bf2013-03-02 15:36:17 -08003082 } else if ((otgsc & OTGSC_BSVIE) && (otgsc & OTGSC_BSVIS)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303083 writel_relaxed(otgsc, USB_OTGSC);
3084 /*
3085 * BSV interrupt comes when operating as an A-device
3086 * (VBUS on/off).
3087 * But, handle BSV when charger is removed from ACA in ID_A
3088 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003089 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303090 !test_bit(ID_A, &motg->inputs))
3091 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303092 if (otgsc & OTGSC_BSV) {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003093 dev_dbg(otg->phy->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303094 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303095 } else {
Stephen Boyd9850acb2013-01-28 14:11:20 -08003096 dev_dbg(otg->phy->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303097 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003098 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3099
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303100 msm_chg_check_aca_intr(motg);
3101 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303102 work = 1;
3103 } else if (usbsts & STS_PCI) {
3104 pc = readl_relaxed(USB_PORTSC);
3105 pr_debug("portsc = %x\n", pc);
3106 ret = IRQ_NONE;
3107 /*
3108 * HCD Acks PCI interrupt. We use this to switch
3109 * between different OTG states.
3110 */
3111 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003112 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303113 case OTG_STATE_A_SUSPEND:
3114 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3115 !(pc & PORTSC_CCS)) {
3116 pr_debug("B_CONN clear\n");
3117 clear_bit(B_CONN, &motg->inputs);
3118 msm_otg_del_timer(motg);
3119 }
3120 break;
3121 case OTG_STATE_A_PERIPHERAL:
3122 /*
3123 * A-peripheral observed activity on bus.
3124 * clear A_BIDL_ADIS timer.
3125 */
3126 msm_otg_del_timer(motg);
3127 work = 0;
3128 break;
3129 case OTG_STATE_B_WAIT_ACON:
3130 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3131 pr_debug("A_CONN set\n");
3132 set_bit(A_CONN, &motg->inputs);
3133 /* Clear ASE0_BRST timer */
3134 msm_otg_del_timer(motg);
3135 }
3136 break;
3137 case OTG_STATE_B_HOST:
3138 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3139 pr_debug("A_CONN clear\n");
3140 clear_bit(A_CONN, &motg->inputs);
3141 msm_otg_del_timer(motg);
3142 }
3143 break;
3144 case OTG_STATE_A_WAIT_BCON:
3145 if (TA_WAIT_BCON < 0)
3146 set_bit(A_BUS_REQ, &motg->inputs);
3147 default:
3148 work = 0;
3149 break;
3150 }
3151 } else if (usbsts & STS_URI) {
3152 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003153 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303154 case OTG_STATE_A_PERIPHERAL:
3155 /*
3156 * A-peripheral observed activity on bus.
3157 * clear A_BIDL_ADIS timer.
3158 */
3159 msm_otg_del_timer(motg);
3160 work = 0;
3161 break;
3162 default:
3163 work = 0;
3164 break;
3165 }
3166 } else if (usbsts & STS_SLI) {
3167 ret = IRQ_NONE;
3168 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003169 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303170 case OTG_STATE_B_PERIPHERAL:
3171 if (otg->gadget->b_hnp_enable) {
3172 set_bit(A_BUS_SUSPEND, &motg->inputs);
3173 set_bit(B_BUS_REQ, &motg->inputs);
3174 work = 1;
3175 }
3176 break;
3177 case OTG_STATE_A_PERIPHERAL:
3178 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3179 A_BIDL_ADIS);
3180 break;
3181 default:
3182 break;
3183 }
3184 } else if ((usbsts & PHY_ALT_INT)) {
3185 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3186 if (msm_chg_check_aca_intr(motg))
3187 work = 1;
3188 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303189 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303190 if (work)
3191 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303192
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303193 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003194}
3195
3196static void msm_otg_set_vbus_state(int online)
3197{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303198 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003199 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303200
Vamsi Krishna945b4a92013-03-02 15:31:16 -08003201 if (online) {
3202 pr_debug("PMIC: BSV set\n");
3203 set_bit(B_SESS_VLD, &motg->inputs);
3204 } else {
3205 pr_debug("PMIC: BSV clear\n");
3206 clear_bit(B_SESS_VLD, &motg->inputs);
3207 }
3208
3209 /* do not queue state m/c work if id is grounded */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303210 if (!test_bit(ID, &motg->inputs)) {
3211 /*
3212 * state machine work waits for initial VBUS
3213 * completion in UNDEFINED state. Process
3214 * the initial VBUS event in ID_GND state.
3215 */
3216 if (init)
3217 return;
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303218 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003219
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303220 if (!init) {
3221 init = true;
3222 complete(&pmic_vbus_init);
3223 pr_debug("PMIC: BSV init complete\n");
3224 return;
3225 }
3226
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303227 if (test_bit(MHL, &motg->inputs) ||
3228 mhl_det_in_progress) {
3229 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3230 return;
3231 }
3232
Jack Pham5ca279b2012-05-14 18:42:54 -07003233 if (atomic_read(&motg->pm_suspended))
3234 motg->sm_work_pending = true;
3235 else
3236 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003237}
3238
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303239static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003240{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303241 struct msm_otg *motg = container_of(w, struct msm_otg,
3242 pmic_id_status_work.work);
3243 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003244
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303245 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303246 if (!test_and_set_bit(ID, &motg->inputs)) {
3247 pr_debug("PMIC: ID set\n");
3248 work = 1;
3249 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303250 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303251 if (test_and_clear_bit(ID, &motg->inputs)) {
3252 pr_debug("PMIC: ID clear\n");
3253 set_bit(A_BUS_REQ, &motg->inputs);
3254 work = 1;
3255 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303256 }
3257
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303258 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003259 if (atomic_read(&motg->pm_suspended))
3260 motg->sm_work_pending = true;
3261 else
3262 queue_work(system_nrt_wq, &motg->sm_work);
3263 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303264
3265}
3266
3267#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3268static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3269{
3270 struct msm_otg *motg = data;
3271
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303272 if (test_bit(MHL, &motg->inputs) ||
3273 mhl_det_in_progress) {
3274 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3275 return IRQ_HANDLED;
3276 }
3277
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303278 if (!aca_id_turned_on)
3279 /*schedule delayed work for 5msec for ID line state to settle*/
3280 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3281 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003282
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303283 return IRQ_HANDLED;
3284}
3285
3286static int msm_otg_mode_show(struct seq_file *s, void *unused)
3287{
3288 struct msm_otg *motg = s->private;
Stephen Boyd9850acb2013-01-28 14:11:20 -08003289 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303290
Stephen Boyd9850acb2013-01-28 14:11:20 -08003291 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303292 case OTG_STATE_A_HOST:
3293 seq_printf(s, "host\n");
3294 break;
3295 case OTG_STATE_B_PERIPHERAL:
3296 seq_printf(s, "peripheral\n");
3297 break;
3298 default:
3299 seq_printf(s, "none\n");
3300 break;
3301 }
3302
3303 return 0;
3304}
3305
3306static int msm_otg_mode_open(struct inode *inode, struct file *file)
3307{
3308 return single_open(file, msm_otg_mode_show, inode->i_private);
3309}
3310
3311static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3312 size_t count, loff_t *ppos)
3313{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303314 struct seq_file *s = file->private_data;
3315 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303316 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003317 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303318 int status = count;
3319 enum usb_mode_type req_mode;
3320
3321 memset(buf, 0x00, sizeof(buf));
3322
3323 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3324 status = -EFAULT;
3325 goto out;
3326 }
3327
3328 if (!strncmp(buf, "host", 4)) {
3329 req_mode = USB_HOST;
3330 } else if (!strncmp(buf, "peripheral", 10)) {
3331 req_mode = USB_PERIPHERAL;
3332 } else if (!strncmp(buf, "none", 4)) {
3333 req_mode = USB_NONE;
3334 } else {
3335 status = -EINVAL;
3336 goto out;
3337 }
3338
3339 switch (req_mode) {
3340 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003341 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303342 case OTG_STATE_A_HOST:
3343 case OTG_STATE_B_PERIPHERAL:
3344 set_bit(ID, &motg->inputs);
3345 clear_bit(B_SESS_VLD, &motg->inputs);
3346 break;
3347 default:
3348 goto out;
3349 }
3350 break;
3351 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003352 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303353 case OTG_STATE_B_IDLE:
3354 case OTG_STATE_A_HOST:
3355 set_bit(ID, &motg->inputs);
3356 set_bit(B_SESS_VLD, &motg->inputs);
3357 break;
3358 default:
3359 goto out;
3360 }
3361 break;
3362 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003363 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303364 case OTG_STATE_B_IDLE:
3365 case OTG_STATE_B_PERIPHERAL:
3366 clear_bit(ID, &motg->inputs);
3367 break;
3368 default:
3369 goto out;
3370 }
3371 break;
3372 default:
3373 goto out;
3374 }
3375
Steve Mucklef132c6c2012-06-06 18:30:57 -07003376 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303377 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303378out:
3379 return status;
3380}
3381
3382const struct file_operations msm_otg_mode_fops = {
3383 .open = msm_otg_mode_open,
3384 .read = seq_read,
3385 .write = msm_otg_mode_write,
3386 .llseek = seq_lseek,
3387 .release = single_release,
3388};
3389
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303390static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3391{
3392 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003393 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303394
Steve Mucklef132c6c2012-06-06 18:30:57 -07003395 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303396 return 0;
3397}
3398
3399static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3400{
3401 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3402}
3403
3404const struct file_operations msm_otg_state_fops = {
3405 .open = msm_otg_otg_state_open,
3406 .read = seq_read,
3407 .llseek = seq_lseek,
3408 .release = single_release,
3409};
3410
Anji jonnalad270e2d2011-08-09 11:28:32 +05303411static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3412{
3413 struct msm_otg *motg = s->private;
3414
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303415 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303416 return 0;
3417}
3418
3419static int msm_otg_chg_open(struct inode *inode, struct file *file)
3420{
3421 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3422}
3423
3424const struct file_operations msm_otg_chg_fops = {
3425 .open = msm_otg_chg_open,
3426 .read = seq_read,
3427 .llseek = seq_lseek,
3428 .release = single_release,
3429};
3430
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303431static int msm_otg_aca_show(struct seq_file *s, void *unused)
3432{
3433 if (debug_aca_enabled)
3434 seq_printf(s, "enabled\n");
3435 else
3436 seq_printf(s, "disabled\n");
3437
3438 return 0;
3439}
3440
3441static int msm_otg_aca_open(struct inode *inode, struct file *file)
3442{
3443 return single_open(file, msm_otg_aca_show, inode->i_private);
3444}
3445
3446static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3447 size_t count, loff_t *ppos)
3448{
3449 char buf[8];
3450
3451 memset(buf, 0x00, sizeof(buf));
3452
3453 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3454 return -EFAULT;
3455
3456 if (!strncmp(buf, "enable", 6))
3457 debug_aca_enabled = true;
3458 else
3459 debug_aca_enabled = false;
3460
3461 return count;
3462}
3463
3464const struct file_operations msm_otg_aca_fops = {
3465 .open = msm_otg_aca_open,
3466 .read = seq_read,
3467 .write = msm_otg_aca_write,
3468 .llseek = seq_lseek,
3469 .release = single_release,
3470};
3471
Manu Gautam8bdcc592012-03-06 11:26:06 +05303472static int msm_otg_bus_show(struct seq_file *s, void *unused)
3473{
3474 if (debug_bus_voting_enabled)
3475 seq_printf(s, "enabled\n");
3476 else
3477 seq_printf(s, "disabled\n");
3478
3479 return 0;
3480}
3481
3482static int msm_otg_bus_open(struct inode *inode, struct file *file)
3483{
3484 return single_open(file, msm_otg_bus_show, inode->i_private);
3485}
3486
3487static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3488 size_t count, loff_t *ppos)
3489{
3490 char buf[8];
3491 int ret;
3492 struct seq_file *s = file->private_data;
3493 struct msm_otg *motg = s->private;
3494
3495 memset(buf, 0x00, sizeof(buf));
3496
3497 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3498 return -EFAULT;
3499
3500 if (!strncmp(buf, "enable", 6)) {
3501 /* Do not vote here. Let OTG statemachine decide when to vote */
3502 debug_bus_voting_enabled = true;
3503 } else {
3504 debug_bus_voting_enabled = false;
3505 if (motg->bus_perf_client) {
3506 ret = msm_bus_scale_client_update_request(
3507 motg->bus_perf_client, 0);
3508 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003509 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303510 "for bus bw %d\n", __func__, ret);
3511 }
3512 }
3513
3514 return count;
3515}
3516
David Keitel272ce522012-08-17 16:25:24 -07003517static int otg_power_get_property_usb(struct power_supply *psy,
3518 enum power_supply_property psp,
3519 union power_supply_propval *val)
3520{
Jack Pham0c695282012-10-19 18:13:03 -07003521 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003522 switch (psp) {
3523 case POWER_SUPPLY_PROP_SCOPE:
3524 if (motg->host_mode)
3525 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3526 else
3527 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3528 break;
3529 case POWER_SUPPLY_PROP_CURRENT_MAX:
3530 val->intval = motg->current_max;
3531 break;
3532 /* Reflect USB enumeration */
3533 case POWER_SUPPLY_PROP_PRESENT:
3534 case POWER_SUPPLY_PROP_ONLINE:
3535 val->intval = motg->online;
3536 break;
3537 default:
3538 return -EINVAL;
3539 }
3540 return 0;
3541}
3542
3543static int otg_power_set_property_usb(struct power_supply *psy,
3544 enum power_supply_property psp,
3545 const union power_supply_propval *val)
3546{
Jack Pham0c695282012-10-19 18:13:03 -07003547 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003548
3549 switch (psp) {
3550 /* Process PMIC notification in PRESENT prop */
3551 case POWER_SUPPLY_PROP_PRESENT:
3552 msm_otg_set_vbus_state(val->intval);
3553 break;
3554 /* The ONLINE property reflects if usb has enumerated */
3555 case POWER_SUPPLY_PROP_ONLINE:
3556 motg->online = val->intval;
3557 break;
3558 case POWER_SUPPLY_PROP_CURRENT_MAX:
3559 motg->current_max = val->intval;
3560 break;
3561 default:
3562 return -EINVAL;
3563 }
3564
3565 power_supply_changed(&motg->usb_psy);
3566 return 0;
3567}
3568
David Collinsd79acc52012-11-26 14:59:00 -08003569static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3570 enum power_supply_property psp)
3571{
3572 switch (psp) {
3573 case POWER_SUPPLY_PROP_PRESENT:
3574 case POWER_SUPPLY_PROP_ONLINE:
3575 case POWER_SUPPLY_PROP_CURRENT_MAX:
3576 return 1;
3577 default:
3578 break;
3579 }
3580
3581 return 0;
3582}
3583
David Keitel272ce522012-08-17 16:25:24 -07003584static char *otg_pm_power_supplied_to[] = {
3585 "battery",
3586};
3587
3588static enum power_supply_property otg_pm_power_props_usb[] = {
3589 POWER_SUPPLY_PROP_PRESENT,
3590 POWER_SUPPLY_PROP_ONLINE,
3591 POWER_SUPPLY_PROP_CURRENT_MAX,
3592 POWER_SUPPLY_PROP_SCOPE,
3593};
3594
Manu Gautam8bdcc592012-03-06 11:26:06 +05303595const struct file_operations msm_otg_bus_fops = {
3596 .open = msm_otg_bus_open,
3597 .read = seq_read,
3598 .write = msm_otg_bus_write,
3599 .llseek = seq_lseek,
3600 .release = single_release,
3601};
3602
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303603static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303604
3605static int msm_otg_debugfs_init(struct msm_otg *motg)
3606{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303607 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303608
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303609 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3610
3611 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3612 return -ENODEV;
3613
Anji jonnalad270e2d2011-08-09 11:28:32 +05303614 if (motg->pdata->mode == USB_OTG &&
3615 motg->pdata->otg_control == OTG_USER_CONTROL) {
3616
Manu Gautam8bdcc592012-03-06 11:26:06 +05303617 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303618 S_IWUSR, msm_otg_dbg_root, motg,
3619 &msm_otg_mode_fops);
3620
Manu Gautam8bdcc592012-03-06 11:26:06 +05303621 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303622 debugfs_remove(msm_otg_dbg_root);
3623 msm_otg_dbg_root = NULL;
3624 return -ENODEV;
3625 }
3626 }
3627
Manu Gautam8bdcc592012-03-06 11:26:06 +05303628 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303629 msm_otg_dbg_root, motg,
3630 &msm_otg_chg_fops);
3631
Manu Gautam8bdcc592012-03-06 11:26:06 +05303632 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303633 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303634 return -ENODEV;
3635 }
3636
Manu Gautam8bdcc592012-03-06 11:26:06 +05303637 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303638 msm_otg_dbg_root, motg,
3639 &msm_otg_aca_fops);
3640
Manu Gautam8bdcc592012-03-06 11:26:06 +05303641 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303642 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303643 return -ENODEV;
3644 }
3645
Manu Gautam8bdcc592012-03-06 11:26:06 +05303646 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3647 msm_otg_dbg_root, motg,
3648 &msm_otg_bus_fops);
3649
3650 if (!msm_otg_dentry) {
3651 debugfs_remove_recursive(msm_otg_dbg_root);
3652 return -ENODEV;
3653 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303654
3655 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3656 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3657
3658 if (!msm_otg_dentry) {
3659 debugfs_remove_recursive(msm_otg_dbg_root);
3660 return -ENODEV;
3661 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303662 return 0;
3663}
3664
3665static void msm_otg_debugfs_cleanup(void)
3666{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303667 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303668}
3669
Manu Gautam0ddbd922012-09-21 17:17:38 +05303670#define MSM_OTG_CMD_ID 0x09
3671#define MSM_OTG_DEVICE_ID 0x04
3672#define MSM_OTG_VMID_IDX 0xFF
3673#define MSM_OTG_MEM_TYPE 0x02
3674struct msm_otg_scm_cmd_buf {
3675 unsigned int device_id;
3676 unsigned int vmid_idx;
3677 unsigned int mem_type;
3678} __attribute__ ((__packed__));
3679
3680static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3681{
3682 int ret;
3683 struct msm_otg_platform_data *pdata = motg->pdata;
3684 struct msm_otg_scm_cmd_buf cmd_buf;
3685
3686 if (!pdata->pnoc_errata_fix)
3687 return;
3688
3689 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3690
3691 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3692 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3693 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3694
Syed Rameez Mustafa6ab6af32013-03-18 12:53:11 -07003695 ret = scm_call(SCM_SVC_MP, MSM_OTG_CMD_ID, &cmd_buf,
Manu Gautam0ddbd922012-09-21 17:17:38 +05303696 sizeof(cmd_buf), NULL, 0);
3697
3698 if (ret)
3699 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3700}
3701
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303702static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3703static struct platform_device *msm_otg_add_pdev(
3704 struct platform_device *ofdev, const char *name)
3705{
3706 struct platform_device *pdev;
3707 const struct resource *res = ofdev->resource;
3708 unsigned int num = ofdev->num_resources;
3709 int retval;
3710
3711 pdev = platform_device_alloc(name, -1);
3712 if (!pdev) {
3713 retval = -ENOMEM;
3714 goto error;
3715 }
3716
3717 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3718 pdev->dev.dma_mask = &msm_otg_dma_mask;
3719
3720 if (num) {
3721 retval = platform_device_add_resources(pdev, res, num);
3722 if (retval)
3723 goto error;
3724 }
3725
3726 retval = platform_device_add(pdev);
3727 if (retval)
3728 goto error;
3729
3730 return pdev;
3731
3732error:
3733 platform_device_put(pdev);
3734 return ERR_PTR(retval);
3735}
3736
3737static int msm_otg_setup_devices(struct platform_device *ofdev,
3738 enum usb_mode_type mode, bool init)
3739{
3740 const char *gadget_name = "msm_hsusb";
3741 const char *host_name = "msm_hsusb_host";
3742 static struct platform_device *gadget_pdev;
3743 static struct platform_device *host_pdev;
3744 int retval = 0;
3745
3746 if (!init) {
3747 if (gadget_pdev)
3748 platform_device_unregister(gadget_pdev);
3749 if (host_pdev)
3750 platform_device_unregister(host_pdev);
3751 return 0;
3752 }
3753
3754 switch (mode) {
3755 case USB_OTG:
3756 /* fall through */
3757 case USB_PERIPHERAL:
3758 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3759 if (IS_ERR(gadget_pdev)) {
3760 retval = PTR_ERR(gadget_pdev);
3761 break;
3762 }
3763 if (mode == USB_PERIPHERAL)
3764 break;
3765 /* fall through */
3766 case USB_HOST:
3767 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3768 if (IS_ERR(host_pdev)) {
3769 retval = PTR_ERR(host_pdev);
3770 if (mode == USB_OTG)
3771 platform_device_unregister(gadget_pdev);
3772 }
3773 break;
3774 default:
3775 break;
3776 }
3777
3778 return retval;
3779}
3780
David Keitel272ce522012-08-17 16:25:24 -07003781static int msm_otg_register_power_supply(struct platform_device *pdev,
3782 struct msm_otg *motg)
3783{
3784 int ret;
3785
3786 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3787 if (ret < 0) {
3788 dev_err(motg->phy.dev,
3789 "%s:power_supply_register usb failed\n",
3790 __func__);
3791 return ret;
3792 }
3793
3794 legacy_power_supply = false;
3795 return 0;
3796}
3797
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303798struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3799{
3800 struct device_node *node = pdev->dev.of_node;
3801 struct msm_otg_platform_data *pdata;
3802 int len = 0;
3803
3804 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3805 if (!pdata) {
3806 pr_err("unable to allocate platform data\n");
3807 return NULL;
3808 }
3809 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3810 if (len) {
3811 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3812 if (!pdata->phy_init_seq)
3813 return NULL;
3814 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3815 pdata->phy_init_seq,
3816 len/sizeof(*pdata->phy_init_seq));
3817 }
3818 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3819 &pdata->power_budget);
3820 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3821 &pdata->mode);
3822 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3823 &pdata->otg_control);
3824 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3825 &pdata->default_mode);
3826 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3827 &pdata->phy_type);
Manu Gautambd53fba2012-07-31 16:13:06 +05303828 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3829 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05303830 pdata->pnoc_errata_fix = of_property_read_bool(node,
3831 "qcom,hsusb-otg-pnoc-errata-fix");
Ido Shayevitza7114b92013-01-13 13:34:47 +02003832 pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
3833 "qcom,hsusb-otg-lpm-on-dev-suspend");
Ido Shayevitz26193352013-01-21 23:16:54 +02003834 pdata->core_clk_always_on_workaround = of_property_read_bool(node,
3835 "qcom,hsusb-otg-clk-always-on-workaround");
Shimrit Malichiffab5b02013-03-10 11:06:16 +02003836 pdata->delay_lpm_on_disconnect = of_property_read_bool(node,
3837 "qcom,hsusb-otg-delay-lpm");
Lena Salmanabde35d2013-04-25 15:29:43 +03003838 pdata->delay_lpm_hndshk_on_disconnect = of_property_read_bool(node,
3839 "qcom,hsusb-otg-delay-lpm-hndshk-on-disconnect");
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08003840 pdata->dp_manual_pullup = of_property_read_bool(node,
3841 "qcom,dp-manual-pullup");
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05303842 pdata->enable_sec_phy = of_property_read_bool(node,
3843 "qcom,usb2-enable-hsphy2");
Manu Gautambd53fba2012-07-31 16:13:06 +05303844
Manu Gautam0e0c53f2013-04-04 10:55:20 +05303845 pdata->pmic_id_irq = platform_get_irq_byname(pdev, "pmic_id_irq");
3846 if (pdata->pmic_id_irq < 0)
3847 pdata->pmic_id_irq = 0;
3848
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303849 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303850}
3851
3852static int __init msm_otg_probe(struct platform_device *pdev)
3853{
Manu Gautamf8c45642012-08-10 10:20:56 -07003854 int ret = 0;
Mayank Rana0f286cf2013-02-27 11:43:27 +05303855 int len = 0;
3856 u32 tmp[3];
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303857 struct resource *res;
3858 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003859 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303860 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303861
3862 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303863
3864 if (pdev->dev.of_node) {
3865 dev_dbg(&pdev->dev, "device tree enabled\n");
3866 pdata = msm_otg_dt_to_pdata(pdev);
3867 if (!pdata)
3868 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003869
3870 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3871 if (!pdata->bus_scale_table)
3872 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3873
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303874 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3875 if (ret) {
3876 dev_err(&pdev->dev, "devices setup failed\n");
3877 return ret;
3878 }
3879 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303880 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3881 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303882 } else {
3883 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303884 }
3885
3886 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3887 if (!motg) {
3888 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3889 return -ENOMEM;
3890 }
3891
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003892 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3893 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003894 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3895 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303896 goto free_motg;
3897 }
3898
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003899 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303900 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003901 phy = &motg->phy;
3902 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303903
3904 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303905 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3906 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3907 * PMIC can detect ACA ID_GND and generate an interrupt.
3908 */
3909 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3910 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3911 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003912 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303913 }
3914
Ofir Cohen4da266f2012-01-03 10:19:29 +02003915 /* initialize reset counter */
3916 motg->reset_counter = 0;
3917
Amit Blay02eff132011-09-21 16:46:24 +03003918 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003919 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003920 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003921 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303922
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303923 /*
3924 * Targets on which link uses asynchronous reset methodology,
3925 * free running clock is not required during the reset.
3926 */
Manu Gautam5143b252012-01-05 19:25:23 -08003927 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303928 if (IS_ERR(motg->clk))
3929 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3930 else
3931 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303932
3933 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003934 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303935 * CORE CLK must be running at >55Mhz for correct HSUSB
3936 * operation and USB core cannot tolerate frequency changes on
3937 * CORE CLK. For such USB cores, vote for maximum clk frequency
3938 * on pclk source
3939 */
Manu Gautam5143b252012-01-05 19:25:23 -08003940 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3941 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303942 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003943 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303944 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003945 goto put_clk;
3946 }
Mayank Rana3eaf28d2013-03-27 14:04:04 +05303947
3948 /*
3949 * Get Max supported clk frequency for USB Core CLK and request
3950 * to set the same.
3951 */
3952 motg->core_clk_rate = clk_round_rate(motg->core_clk, LONG_MAX);
3953 if (IS_ERR_VALUE(motg->core_clk_rate)) {
3954 dev_err(&pdev->dev, "fail to get core clk max freq.\n");
3955 } else {
3956 ret = clk_set_rate(motg->core_clk, motg->core_clk_rate);
3957 if (ret)
3958 dev_err(&pdev->dev, "fail to set core_clk freq:%d\n",
3959 ret);
3960 }
Manu Gautam5143b252012-01-05 19:25:23 -08003961
3962 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3963 if (IS_ERR(motg->pclk)) {
3964 dev_err(&pdev->dev, "failed to get iface_clk\n");
3965 ret = PTR_ERR(motg->pclk);
3966 goto put_core_clk;
3967 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303968
3969 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3970 if (!res) {
3971 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3972 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003973 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303974 }
3975
3976 motg->regs = ioremap(res->start, resource_size(res));
3977 if (!motg->regs) {
3978 dev_err(&pdev->dev, "ioremap failed\n");
3979 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003980 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303981 }
3982 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3983
3984 motg->irq = platform_get_irq(pdev, 0);
3985 if (!motg->irq) {
3986 dev_err(&pdev->dev, "platform_get_irq failed\n");
3987 ret = -ENODEV;
3988 goto free_regs;
3989 }
3990
Manu Gautamf8c45642012-08-10 10:20:56 -07003991 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3992 if (motg->async_irq < 0) {
3993 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3994 motg->async_irq = 0;
3995 }
3996
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05303997 motg->xo_clk = clk_get(&pdev->dev, "xo");
3998 if (IS_ERR(motg->xo_clk)) {
3999 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
4000 if (IS_ERR(motg->xo_handle)) {
4001 dev_err(&pdev->dev, "%s fail to get handle for TCXO\n",
4002 __func__);
4003 ret = PTR_ERR(motg->xo_handle);
4004 goto free_regs;
4005 } else {
4006 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
4007 if (ret) {
4008 dev_err(&pdev->dev, "%s XO voting failed %d\n",
4009 __func__, ret);
4010 goto free_xo_handle;
4011 }
4012 }
4013 } else {
4014 ret = clk_prepare_enable(motg->xo_clk);
4015 if (ret) {
4016 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
4017 __func__, ret);
4018 goto free_xo_handle;
4019 }
Anji jonnala7da3f262011-12-02 17:22:14 -08004020 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05304021
Anji jonnala7da3f262011-12-02 17:22:14 -08004022
Manu Gautam28b1bac2012-01-30 16:43:06 +05304023 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304024
Mayank Rana248698c2012-04-19 00:03:16 +05304025 motg->vdd_type = VDDCX_CORNER;
Mayank Rana0f286cf2013-02-27 11:43:27 +05304026 hsusb_vdd = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
4027 if (IS_ERR(hsusb_vdd)) {
4028 hsusb_vdd = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
4029 if (IS_ERR(hsusb_vdd)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07004030 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304031 ret = PTR_ERR(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304032 goto devote_xo_handle;
4033 }
4034 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304035 }
4036
Mayank Rana0f286cf2013-02-27 11:43:27 +05304037 if (pdev->dev.of_node) {
4038 of_get_property(pdev->dev.of_node,
4039 "qcom,vdd-voltage-level",
4040 &len);
4041 if (len == sizeof(tmp)) {
4042 of_property_read_u32_array(pdev->dev.of_node,
4043 "qcom,vdd-voltage-level",
4044 tmp, len/sizeof(*tmp));
4045 vdd_val[motg->vdd_type][0] = tmp[0];
4046 vdd_val[motg->vdd_type][1] = tmp[1];
4047 vdd_val[motg->vdd_type][2] = tmp[2];
4048 } else {
4049 dev_dbg(&pdev->dev, "Using default hsusb vdd config.\n");
4050 }
4051 }
4052
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004053 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304054 if (ret) {
4055 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05304056 goto devote_xo_handle;
4057 }
4058
Mayank Rana0f286cf2013-02-27 11:43:27 +05304059 ret = regulator_enable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304060 if (ret) {
4061 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
4062 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304063 }
4064
4065 ret = msm_hsusb_ldo_init(motg, 1);
4066 if (ret) {
4067 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana0f286cf2013-02-27 11:43:27 +05304068 goto free_hsusb_vdd;
Anji jonnala11aa5c42011-05-04 10:19:48 +05304069 }
4070
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304071 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05304072 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
4073 "mhl_usb_hs_switch");
4074 if (IS_ERR(mhl_usb_hs_switch)) {
4075 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07004076 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05304077 goto free_ldo_init;
4078 }
4079 }
4080
Amit Blay81801aa2012-09-19 12:08:12 +02004081 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304082 if (ret) {
4083 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004084 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304085 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05304086 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304087
Manu Gautam0ddbd922012-09-21 17:17:38 +05304088 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
4089 msm_otg_pnoc_errata_fix(motg);
4090
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304091 writel(0, USB_USBINTR);
4092 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004093 /* Ensure that above STOREs are completed before enabling interrupts */
4094 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304095
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304096 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
4097 if (ret)
4098 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004099 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05304100 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304101 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304102 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304103 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004104 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304105 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
4106 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05304107 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
4108 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304109 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
4110 "msm_otg", motg);
4111 if (ret) {
4112 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004113 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304114 }
4115
Manu Gautamf8c45642012-08-10 10:20:56 -07004116 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304117 ret = request_irq(motg->async_irq, msm_otg_irq,
4118 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004119 if (ret) {
4120 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4121 goto free_irq;
4122 }
4123 disable_irq(motg->async_irq);
4124 }
4125
Jack Pham87f202f2012-08-06 00:24:22 -07004126 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4127 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4128
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004129 phy->init = msm_otg_reset;
4130 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004131 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304132
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004133 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304134
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004135 phy->otg->phy = &motg->phy;
4136 phy->otg->set_host = msm_otg_set_host;
4137 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004138 phy->otg->start_hnp = msm_otg_start_hnp;
4139 phy->otg->start_srp = msm_otg_start_srp;
Vamsi Krishna1a1684b2013-03-02 16:14:52 -08004140 if (pdata->dp_manual_pullup)
4141 phy->flags |= ENABLE_DP_MANUAL_PULLUP;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004142
Manu Gautam0fd2d0e2013-03-26 18:09:11 +05304143 if (pdata->enable_sec_phy)
4144 phy->flags |= ENABLE_SECONDARY_PHY;
4145
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004146 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304147 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004148 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004149 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304150 }
4151
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304152 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304153 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004154 if (motg->pdata->pmic_id_irq) {
4155 ret = request_irq(motg->pdata->pmic_id_irq,
4156 msm_pmic_id_irq,
4157 IRQF_TRIGGER_RISING |
4158 IRQF_TRIGGER_FALLING,
4159 "msm_otg", motg);
4160 if (ret) {
4161 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004162 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004163 }
4164 } else {
4165 ret = -ENODEV;
4166 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004167 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004168 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304169 }
4170
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304171 msm_hsusb_mhl_switch_enable(motg, 1);
4172
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304173 platform_set_drvdata(pdev, motg);
4174 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004175 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304176
Anji jonnalad270e2d2011-08-09 11:28:32 +05304177 ret = msm_otg_debugfs_init(motg);
4178 if (ret)
4179 dev_dbg(&pdev->dev, "mode debugfs file is"
4180 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304181
Amit Blay58b31472011-11-18 09:39:39 +02004182 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4183 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304184 (!(motg->pdata->mode == USB_OTG) ||
4185 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004186 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304187 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004188
Amit Blay58b31472011-11-18 09:39:39 +02004189 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004190 motg->caps = ALLOW_PHY_RETENTION |
4191 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02004192 }
4193
Amit Blay6fa647a2012-05-24 14:12:08 +03004194 if (motg->pdata->enable_lpm_on_dev_suspend)
4195 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4196
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004197 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304198 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004199 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304200
Amit Blayd6f38282012-10-29 13:13:46 +02004201 if (motg->pdata->delay_lpm_on_disconnect) {
4202 pm_runtime_set_autosuspend_delay(&pdev->dev,
4203 lpm_disconnect_thresh);
4204 pm_runtime_use_autosuspend(&pdev->dev);
4205 }
4206
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304207 if (motg->pdata->bus_scale_table) {
4208 motg->bus_perf_client =
4209 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4210 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004211 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304212 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05304213 else
4214 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304215 }
4216
David Keitel272ce522012-08-17 16:25:24 -07004217 motg->usb_psy.name = "usb";
4218 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4219 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4220 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4221 motg->usb_psy.properties = otg_pm_power_props_usb;
4222 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4223 motg->usb_psy.get_property = otg_power_get_property_usb;
4224 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004225 motg->usb_psy.property_is_writeable
4226 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004227
Jack Pham0c695282012-10-19 18:13:03 -07004228 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004229 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004230 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4231 legacy_power_supply = true;
4232 } else {
4233 /* otherwise register our own power supply */
4234 if (!msm_otg_register_power_supply(pdev, motg))
4235 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004236 }
4237
Jack Pham0c695282012-10-19 18:13:03 -07004238 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4239 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4240
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304241 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004242
Steve Mucklef132c6c2012-06-06 18:30:57 -07004243remove_phy:
4244 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004245free_async_irq:
4246 if (motg->async_irq)
4247 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304248free_irq:
4249 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004250destroy_wlock:
4251 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304252 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004253 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004254free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304255 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304256free_hsusb_vdd:
4257 regulator_disable(hsusb_vdd);
Mayank Rana248698c2012-04-19 00:03:16 +05304258free_config_vddcx:
Mayank Rana0f286cf2013-02-27 11:43:27 +05304259 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304260 vdd_val[motg->vdd_type][VDD_NONE],
4261 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004262devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304263 clk_disable_unprepare(motg->pclk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304264 if (!IS_ERR(motg->xo_clk))
4265 clk_disable_unprepare(motg->xo_clk);
4266 else
4267 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004268free_xo_handle:
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304269 if (!IS_ERR(motg->xo_clk))
4270 clk_put(motg->xo_clk);
4271 else
4272 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304273free_regs:
4274 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004275put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304276 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304277put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004278 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304279put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304280 if (!IS_ERR(motg->clk))
4281 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004282 if (!IS_ERR(motg->phy_reset_clk))
4283 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004284free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004285 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304286free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304287 kfree(motg);
4288 return ret;
4289}
4290
4291static int __devexit msm_otg_remove(struct platform_device *pdev)
4292{
4293 struct msm_otg *motg = platform_get_drvdata(pdev);
Stephen Boyd9850acb2013-01-28 14:11:20 -08004294 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304295 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304296
Stephen Boyd9850acb2013-01-28 14:11:20 -08004297 if (phy->otg->host || phy->otg->gadget)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304298 return -EBUSY;
4299
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304300 if (pdev->dev.of_node)
4301 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004302 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4303 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304304 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304305 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304306 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304307 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004308 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304309 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304310
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304311 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304312
4313 device_init_wakeup(&pdev->dev, 0);
4314 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004315 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304316
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304317 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004318 if (motg->pdata->pmic_id_irq)
4319 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004320 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304321 free_irq(motg->irq, motg);
4322
Jack Pham87f202f2012-08-06 00:24:22 -07004323 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4324 motg->pdata->mpm_otgsessvld_int)
4325 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4326
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304327 /*
4328 * Put PHY in low power mode.
4329 */
Stephen Boyd9850acb2013-01-28 14:11:20 -08004330 ulpi_read(phy, 0x14);
4331 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304332
4333 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4334 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4335 if (readl(USB_PORTSC) & PORTSC_PHCD)
4336 break;
4337 udelay(1);
4338 cnt++;
4339 }
4340 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Stephen Boyd9850acb2013-01-28 14:11:20 -08004341 dev_err(phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304342
Manu Gautam28b1bac2012-01-30 16:43:06 +05304343 clk_disable_unprepare(motg->pclk);
4344 clk_disable_unprepare(motg->core_clk);
Vijayavardhan Vennapusa0cab6172013-02-20 19:51:15 +05304345 if (!IS_ERR(motg->xo_clk)) {
4346 clk_disable_unprepare(motg->xo_clk);
4347 clk_put(motg->xo_clk);
4348 } else {
4349 msm_xo_put(motg->xo_handle);
4350 }
Amit Blay81801aa2012-09-19 12:08:12 +02004351 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304352 msm_hsusb_ldo_init(motg, 0);
Mayank Rana0f286cf2013-02-27 11:43:27 +05304353 regulator_disable(hsusb_vdd);
4354 regulator_set_voltage(hsusb_vdd,
Mayank Rana248698c2012-04-19 00:03:16 +05304355 vdd_val[motg->vdd_type][VDD_NONE],
4356 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304357
4358 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304359 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304360
Amit Blay02eff132011-09-21 16:46:24 +03004361 if (!IS_ERR(motg->phy_reset_clk))
4362 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304363 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304364 if (!IS_ERR(motg->clk))
4365 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004366 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304367
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304368 if (motg->bus_perf_client)
4369 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304370
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004371 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304372 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304373 return 0;
4374}
4375
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304376#ifdef CONFIG_PM_RUNTIME
4377static int msm_otg_runtime_idle(struct device *dev)
4378{
4379 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004380 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304381
4382 dev_dbg(dev, "OTG runtime idle\n");
4383
Steve Mucklef132c6c2012-06-06 18:30:57 -07004384 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304385 return -EAGAIN;
4386 else
4387 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304388}
4389
4390static int msm_otg_runtime_suspend(struct device *dev)
4391{
4392 struct msm_otg *motg = dev_get_drvdata(dev);
4393
4394 dev_dbg(dev, "OTG runtime suspend\n");
4395 return msm_otg_suspend(motg);
4396}
4397
4398static int msm_otg_runtime_resume(struct device *dev)
4399{
4400 struct msm_otg *motg = dev_get_drvdata(dev);
4401
4402 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304403 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304404 return msm_otg_resume(motg);
4405}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304406#endif
4407
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304408#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304409static int msm_otg_pm_suspend(struct device *dev)
4410{
Jack Pham5ca279b2012-05-14 18:42:54 -07004411 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304412 struct msm_otg *motg = dev_get_drvdata(dev);
4413
4414 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004415
4416 atomic_set(&motg->pm_suspended, 1);
4417 ret = msm_otg_suspend(motg);
4418 if (ret)
4419 atomic_set(&motg->pm_suspended, 0);
4420
4421 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304422}
4423
4424static int msm_otg_pm_resume(struct device *dev)
4425{
Jack Pham5ca279b2012-05-14 18:42:54 -07004426 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304427 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304428
4429 dev_dbg(dev, "OTG PM resume\n");
4430
Jack Pham5ca279b2012-05-14 18:42:54 -07004431 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004432 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004433 pm_runtime_get_noresume(dev);
4434 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304435
Jack Pham5ca279b2012-05-14 18:42:54 -07004436 /* Update runtime PM status */
4437 pm_runtime_disable(dev);
4438 pm_runtime_set_active(dev);
4439 pm_runtime_enable(dev);
4440
Jack Phamc7edb172012-08-13 15:32:39 -07004441 if (motg->sm_work_pending) {
4442 motg->sm_work_pending = false;
4443 queue_work(system_nrt_wq, &motg->sm_work);
4444 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004445 }
4446
4447 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304448}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304449#endif
4450
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304451#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304452static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304453 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4454 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4455 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304456};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304457#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304458
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304459static struct of_device_id msm_otg_dt_match[] = {
4460 { .compatible = "qcom,hsusb-otg",
4461 },
4462 {}
4463};
4464
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304465static struct platform_driver msm_otg_driver = {
4466 .remove = __devexit_p(msm_otg_remove),
4467 .driver = {
4468 .name = DRIVER_NAME,
4469 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304470#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304471 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304472#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304473 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304474 },
4475};
4476
4477static int __init msm_otg_init(void)
4478{
4479 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4480}
4481
4482static void __exit msm_otg_exit(void)
4483{
4484 platform_driver_unregister(&msm_otg_driver);
4485}
4486
4487module_init(msm_otg_init);
4488module_exit(msm_otg_exit);
4489
4490MODULE_LICENSE("GPL v2");
4491MODULE_DESCRIPTION("MSM USB transceiver driver");