blob: 3ad05b0633fc50db4f8ccc2476e10424489e6d93 [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. 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
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053090static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053092static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053093static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053094static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095
Anji jonnala11aa5c42011-05-04 10:19:48 +053096static struct regulator *hsusb_3p3;
97static struct regulator *hsusb_1p8;
98static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053099static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530100static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +0530101static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530102
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530103static bool aca_id_turned_on;
David Keitel272ce522012-08-17 16:25:24 -0700104static bool legacy_power_supply;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530105static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530106{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530107#ifdef CONFIG_USB_MSM_ACA
108 return true;
109#else
110 return debug_aca_enabled;
111#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +0530112}
113
Mayank Rana248698c2012-04-19 00:03:16 +0530114static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
115 { /* VDD_CX CORNER Voting */
116 [VDD_NONE] = RPM_VREG_CORNER_NONE,
117 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
118 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
119 },
120 { /* VDD_CX Voltage Voting */
121 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
122 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
123 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
124 },
125};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126
127static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
128{
129 int rc = 0;
130
131 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700132 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200134 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530135 return PTR_ERR(hsusb_3p3);
136 }
137
138 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
139 USB_PHY_3P3_VOL_MAX);
140 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700141 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530143 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700145 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530146 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200147 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530150 }
151 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
152 USB_PHY_1P8_VOL_MAX);
153 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700154 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700155 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530156 goto put_1p8;
157 }
158
159 return 0;
160 }
161
Anji jonnala11aa5c42011-05-04 10:19:48 +0530162put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164put_3p3_lpm:
165 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530166 return rc;
167}
168
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530169static int msm_hsusb_config_vddcx(int high)
170{
Mayank Rana248698c2012-04-19 00:03:16 +0530171 struct msm_otg *motg = the_msm_otg;
172 enum usb_vdd_type vdd_type = motg->vdd_type;
173 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530174 int min_vol;
175 int ret;
176
Mayank Rana248698c2012-04-19 00:03:16 +0530177 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530178 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
179 if (ret) {
180 pr_err("%s: unable to set the voltage for regulator "
181 "HSUSB_VDDCX\n", __func__);
182 return ret;
183 }
184
185 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
186
187 return ret;
188}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530189
Amit Blay81801aa2012-09-19 12:08:12 +0200190static int msm_hsusb_ldo_enable(struct msm_otg *motg,
191 enum msm_otg_phy_reg_mode mode)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530192{
193 int ret = 0;
194
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530195 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530196 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
197 return -ENODEV;
198 }
199
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530200 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530201 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
202 return -ENODEV;
203 }
204
Amit Blay81801aa2012-09-19 12:08:12 +0200205 switch (mode) {
206 case USB_PHY_REG_ON:
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207 ret = regulator_set_optimum_mode(hsusb_1p8,
208 USB_PHY_1P8_HPM_LOAD);
209 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530211 "HSUSB_1p8\n", __func__);
212 return ret;
213 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214
215 ret = regulator_enable(hsusb_1p8);
216 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700217 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 __func__);
219 regulator_set_optimum_mode(hsusb_1p8, 0);
220 return ret;
221 }
222
Anji jonnala11aa5c42011-05-04 10:19:48 +0530223 ret = regulator_set_optimum_mode(hsusb_3p3,
224 USB_PHY_3P3_HPM_LOAD);
225 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530227 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 regulator_set_optimum_mode(hsusb_1p8, 0);
229 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530230 return ret;
231 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232
233 ret = regulator_enable(hsusb_3p3);
234 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700235 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 __func__);
237 regulator_set_optimum_mode(hsusb_3p3, 0);
238 regulator_set_optimum_mode(hsusb_1p8, 0);
239 regulator_disable(hsusb_1p8);
240 return ret;
241 }
242
Amit Blay81801aa2012-09-19 12:08:12 +0200243 break;
244
245 case USB_PHY_REG_OFF:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 ret = regulator_disable(hsusb_1p8);
247 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700248 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 __func__);
250 return ret;
251 }
252
253 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530254 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530256 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257
258 ret = regulator_disable(hsusb_3p3);
259 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700260 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 __func__);
262 return ret;
263 }
264 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530265 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530267 "HSUSB_3p3\n", __func__);
Amit Blay81801aa2012-09-19 12:08:12 +0200268
269 break;
270
271 case USB_PHY_REG_LPM_ON:
272 ret = regulator_set_optimum_mode(hsusb_1p8,
273 USB_PHY_1P8_LPM_LOAD);
274 if (ret < 0) {
275 pr_err("%s: Unable to set LPM of the regulator: HSUSB_1p8\n",
276 __func__);
277 return ret;
278 }
279
280 ret = regulator_set_optimum_mode(hsusb_3p3,
281 USB_PHY_3P3_LPM_LOAD);
282 if (ret < 0) {
283 pr_err("%s: Unable to set LPM of the regulator: HSUSB_3p3\n",
284 __func__);
285 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
286 return ret;
287 }
288
289 break;
290
291 case USB_PHY_REG_LPM_OFF:
292 ret = regulator_set_optimum_mode(hsusb_1p8,
293 USB_PHY_1P8_HPM_LOAD);
294 if (ret < 0) {
295 pr_err("%s: Unable to set HPM of the regulator: HSUSB_1p8\n",
296 __func__);
297 return ret;
298 }
299
300 ret = regulator_set_optimum_mode(hsusb_3p3,
301 USB_PHY_3P3_HPM_LOAD);
302 if (ret < 0) {
303 pr_err("%s: Unable to set HPM of the regulator: HSUSB_3p3\n",
304 __func__);
305 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
306 return ret;
307 }
308
309 break;
310
311 default:
312 pr_err("%s: Unsupported mode (%d).", __func__, mode);
313 return -ENOTSUPP;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530314 }
315
Amit Blay81801aa2012-09-19 12:08:12 +0200316 pr_debug("%s: USB reg mode (%d) (OFF/HPM/LPM)\n", __func__, mode);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530317 return ret < 0 ? ret : 0;
318}
319
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530320static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
321{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530322 struct msm_otg_platform_data *pdata = motg->pdata;
323
324 if (!pdata->mhl_enable)
325 return;
326
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530327 if (!mhl_usb_hs_switch) {
328 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530329 return;
330 }
331
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530332 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530333 if (regulator_enable(mhl_usb_hs_switch))
334 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530335 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530336 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530337 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530338}
339
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200340static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530341{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200342 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530343 int cnt = 0;
344
345 /* initiate read operation */
346 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
347 USB_ULPI_VIEWPORT);
348
349 /* wait for completion */
350 while (cnt < ULPI_IO_TIMEOUT_USEC) {
351 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
352 break;
353 udelay(1);
354 cnt++;
355 }
356
357 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200358 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530359 readl(USB_ULPI_VIEWPORT));
360 return -ETIMEDOUT;
361 }
362 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
363}
364
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200365static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530366{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200367 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530368 int cnt = 0;
369
370 /* initiate write operation */
371 writel(ULPI_RUN | ULPI_WRITE |
372 ULPI_ADDR(reg) | ULPI_DATA(val),
373 USB_ULPI_VIEWPORT);
374
375 /* wait for completion */
376 while (cnt < ULPI_IO_TIMEOUT_USEC) {
377 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
378 break;
379 udelay(1);
380 cnt++;
381 }
382
383 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200384 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530385 return -ETIMEDOUT;
386 }
387 return 0;
388}
389
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200390static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530391 .read = ulpi_read,
392 .write = ulpi_write,
393};
394
395static void ulpi_init(struct msm_otg *motg)
396{
397 struct msm_otg_platform_data *pdata = motg->pdata;
Mayank Rana443f9e42012-09-21 18:32:39 +0530398 int aseq[10];
399 int *seq = NULL;
400
401 if (override_phy_init) {
402 pr_debug("%s(): HUSB PHY Init:%s\n", __func__,
403 override_phy_init);
404 get_options(override_phy_init, ARRAY_SIZE(aseq), aseq);
405 seq = &aseq[1];
406 } else {
407 seq = pdata->phy_init_seq;
408 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530409
410 if (!seq)
411 return;
412
413 while (seq[0] >= 0) {
Mayank Rana443f9e42012-09-21 18:32:39 +0530414 if (override_phy_init)
415 pr_debug("ulpi: write 0x%02x to 0x%02x\n",
416 seq[0], seq[1]);
417
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200418 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530419 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200420 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530421 seq += 2;
422 }
423}
424
425static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
426{
427 int ret;
428
429 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530430 if (!IS_ERR(motg->clk)) {
431 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
432 } else {
433 /* Using asynchronous block reset to the hardware */
434 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
435 clk_disable_unprepare(motg->pclk);
436 clk_disable_unprepare(motg->core_clk);
437 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
438 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530439 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200440 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530441 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530442 if (!IS_ERR(motg->clk)) {
443 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
444 } else {
445 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
446 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
447 ndelay(200);
448 clk_prepare_enable(motg->core_clk);
449 clk_prepare_enable(motg->pclk);
450 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530451 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200452 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530453 }
454 return ret;
455}
456
457static int msm_otg_phy_clk_reset(struct msm_otg *motg)
458{
459 int ret;
460
Amit Blay02eff132011-09-21 16:46:24 +0300461 if (IS_ERR(motg->phy_reset_clk))
462 return 0;
463
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530464 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
465 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200466 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530467 return ret;
468 }
469 usleep_range(10000, 12000);
470 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
471 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200472 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530473 return ret;
474}
475
476static int msm_otg_phy_reset(struct msm_otg *motg)
477{
478 u32 val;
479 int ret;
480 int retries;
481
482 ret = msm_otg_link_clk_reset(motg, 1);
483 if (ret)
484 return ret;
485 ret = msm_otg_phy_clk_reset(motg);
486 if (ret)
487 return ret;
Bar Weinere7129cb2012-11-28 08:44:14 +0200488 /* 10 usec delay is required according to spec */
489 if (IS_ERR(motg->phy_reset_clk))
490 usleep_range(10, 12);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530491 ret = msm_otg_link_clk_reset(motg, 0);
492 if (ret)
493 return ret;
494
495 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
496 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
497
498 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200499 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530500 ULPI_CLR(ULPI_FUNC_CTRL));
501 if (!ret)
502 break;
503 ret = msm_otg_phy_clk_reset(motg);
504 if (ret)
505 return ret;
506 }
507 if (!retries)
508 return -ETIMEDOUT;
509
510 /* This reset calibrates the phy, if the above write succeeded */
511 ret = msm_otg_phy_clk_reset(motg);
512 if (ret)
513 return ret;
514
515 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200516 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530517 if (ret != -ETIMEDOUT)
518 break;
519 ret = msm_otg_phy_clk_reset(motg);
520 if (ret)
521 return ret;
522 }
523 if (!retries)
524 return -ETIMEDOUT;
525
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200526 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530527 return 0;
528}
529
530#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530531static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530532{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530533 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530534
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530535 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530536 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530537 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530538 break;
539 udelay(1);
540 cnt++;
541 }
542 if (cnt >= LINK_RESET_TIMEOUT_USEC)
543 return -ETIMEDOUT;
544
545 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530546 writel_relaxed(0x80000000, USB_PORTSC);
547 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530548 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530549
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530550 return 0;
551}
552
Steve Mucklef132c6c2012-06-06 18:30:57 -0700553static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530554{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700555 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530556 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530557 int ret;
558 u32 val = 0;
559 u32 ulpi_val = 0;
560
Ofir Cohen4da266f2012-01-03 10:19:29 +0200561 /*
562 * USB PHY and Link reset also reset the USB BAM.
563 * Thus perform reset operation only once to avoid
564 * USB BAM reset on other cases e.g. USB cable disconnections.
565 */
566 if (pdata->disable_reset_on_disconnect) {
567 if (motg->reset_counter)
568 return 0;
569 else
570 motg->reset_counter++;
571 }
572
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530573 if (!IS_ERR(motg->clk))
574 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530575 ret = msm_otg_phy_reset(motg);
576 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700577 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530578 return ret;
579 }
580
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530581 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530582 ret = msm_otg_link_reset(motg);
583 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700584 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530585 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530586 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530587 msleep(100);
588
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530589 ulpi_init(motg);
590
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 /* Ensure that RESET operation is completed before turning off clock */
592 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530593
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530594 if (!IS_ERR(motg->clk))
595 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530596
597 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530598 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530599 if (pdata->mode == USB_OTG) {
600 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
601 val |= OTGSC_IDIE | OTGSC_BSVIE;
602 } else if (pdata->mode == USB_PERIPHERAL) {
603 ulpi_val = ULPI_INT_SESS_VALID;
604 val |= OTGSC_BSVIE;
605 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530606 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200607 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
608 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530609 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700610 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530611 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530612 /* Enable PMIC pull-up */
613 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530614 }
615
616 return 0;
617}
618
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530619static const char *timer_string(int bit)
620{
621 switch (bit) {
622 case A_WAIT_VRISE: return "a_wait_vrise";
623 case A_WAIT_VFALL: return "a_wait_vfall";
624 case B_SRP_FAIL: return "b_srp_fail";
625 case A_WAIT_BCON: return "a_wait_bcon";
626 case A_AIDL_BDIS: return "a_aidl_bdis";
627 case A_BIDL_ADIS: return "a_bidl_adis";
628 case B_ASE0_BRST: return "b_ase0_brst";
629 case A_TST_MAINT: return "a_tst_maint";
630 case B_TST_SRP: return "b_tst_srp";
631 case B_TST_CONFIG: return "b_tst_config";
632 default: return "UNDEFINED";
633 }
634}
635
636static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
637{
638 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
639
640 switch (motg->active_tmout) {
641 case A_WAIT_VRISE:
642 /* TODO: use vbus_vld interrupt */
643 set_bit(A_VBUS_VLD, &motg->inputs);
644 break;
645 case A_TST_MAINT:
646 /* OTG PET: End session after TA_TST_MAINT */
647 set_bit(A_BUS_DROP, &motg->inputs);
648 break;
649 case B_TST_SRP:
650 /*
651 * OTG PET: Initiate SRP after TB_TST_SRP of
652 * previous session end.
653 */
654 set_bit(B_BUS_REQ, &motg->inputs);
655 break;
656 case B_TST_CONFIG:
657 clear_bit(A_CONN, &motg->inputs);
658 break;
659 default:
660 set_bit(motg->active_tmout, &motg->tmouts);
661 }
662
663 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
664 queue_work(system_nrt_wq, &motg->sm_work);
665 return HRTIMER_NORESTART;
666}
667
668static void msm_otg_del_timer(struct msm_otg *motg)
669{
670 int bit = motg->active_tmout;
671
672 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
673 div_s64(ktime_to_us(hrtimer_get_remaining(
674 &motg->timer)), 1000));
675 hrtimer_cancel(&motg->timer);
676 clear_bit(bit, &motg->tmouts);
677}
678
679static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
680{
681 clear_bit(bit, &motg->tmouts);
682 motg->active_tmout = bit;
683 pr_debug("starting %s timer\n", timer_string(bit));
684 hrtimer_start(&motg->timer,
685 ktime_set(time / 1000, (time % 1000) * 1000000),
686 HRTIMER_MODE_REL);
687}
688
689static void msm_otg_init_timer(struct msm_otg *motg)
690{
691 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
692 motg->timer.function = msm_otg_timer_func;
693}
694
Steve Mucklef132c6c2012-06-06 18:30:57 -0700695static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530696{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700697 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530698
Steve Mucklef132c6c2012-06-06 18:30:57 -0700699 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530700 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700701 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530702 return -EINVAL;
703 }
704
705 pr_debug("A-Host: HNP initiated\n");
706 clear_bit(A_BUS_REQ, &motg->inputs);
707 queue_work(system_nrt_wq, &motg->sm_work);
708 return 0;
709}
710
Steve Mucklef132c6c2012-06-06 18:30:57 -0700711static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530712{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700713 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530714 u32 val;
715 int ret = 0;
716
Steve Mucklef132c6c2012-06-06 18:30:57 -0700717 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530718 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700719 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530720 ret = -EINVAL;
721 goto out;
722 }
723
724 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
725 pr_debug("initial conditions of SRP are not met. Try again"
726 "after some time\n");
727 ret = -EAGAIN;
728 goto out;
729 }
730
731 pr_debug("B-Device SRP started\n");
732
733 /*
734 * PHY won't pull D+ high unless it detects Vbus valid.
735 * Since by definition, SRP is only done when Vbus is not valid,
736 * software work-around needs to be used to spoof the PHY into
737 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
738 * VBUSVLDEXT register bits.
739 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700740 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530741 /*
742 * Harware auto assist data pulsing: Data pulse is given
743 * for 7msec; wait for vbus
744 */
745 val = readl_relaxed(USB_OTGSC);
746 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
747
748 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
749out:
750 return ret;
751}
752
Steve Mucklef132c6c2012-06-06 18:30:57 -0700753static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530754{
755 struct usb_hcd *hcd = bus_to_hcd(otg->host);
756 struct usb_device *rhub = otg->host->root_hub;
757
758 if (enable) {
759 pm_runtime_disable(&rhub->dev);
760 rhub->state = USB_STATE_NOTATTACHED;
761 hcd->driver->bus_suspend(hcd);
762 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
763 } else {
764 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700765 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530766 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
767 }
768}
769
Steve Mucklef132c6c2012-06-06 18:30:57 -0700770static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530771{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700772 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530773
Amit Blay6fa647a2012-05-24 14:12:08 +0300774 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530775 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530776
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530777 /*
778 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
779 * during bus suspend/resume. Update the relevant state
780 * machine inputs and trigger LPM entry/exit. Checking
781 * in_lpm flag would avoid unnecessary work scheduling.
782 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530783 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700784 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530785 case OTG_STATE_A_WAIT_BCON:
786 if (TA_WAIT_BCON > 0)
787 break;
788 /* fall through */
789 case OTG_STATE_A_HOST:
790 pr_debug("host bus suspend\n");
791 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530792 if (!atomic_read(&motg->in_lpm))
793 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530794 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300795 case OTG_STATE_B_PERIPHERAL:
796 pr_debug("peripheral bus suspend\n");
797 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
798 break;
799 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530800 if (!atomic_read(&motg->in_lpm))
801 queue_delayed_work(system_nrt_wq,
802 &motg->suspend_work,
803 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300804 break;
805
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530806 default:
807 break;
808 }
809 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700810 switch (phy->state) {
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530811 case OTG_STATE_A_WAIT_BCON:
812 /* Remote wakeup or resume */
813 set_bit(A_BUS_REQ, &motg->inputs);
814 /* ensure hardware is not in low power mode */
815 if (atomic_read(&motg->in_lpm))
816 pm_runtime_resume(phy->dev);
817 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530818 case OTG_STATE_A_SUSPEND:
819 /* Remote wakeup or resume */
820 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700821 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700822
823 /* ensure hardware is not in low power mode */
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530824 if (atomic_read(&motg->in_lpm))
825 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530826 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300827 case OTG_STATE_B_PERIPHERAL:
828 pr_debug("peripheral bus resume\n");
829 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
830 break;
831 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530832 if (atomic_read(&motg->in_lpm))
833 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300834 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530835 default:
836 break;
837 }
838 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530839 return 0;
840}
841
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530842#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530843#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
844
845#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530846static int msm_otg_suspend(struct msm_otg *motg)
847{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200848 struct usb_phy *phy = &motg->phy;
849 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530850 struct msm_otg_platform_data *pdata = motg->pdata;
851 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300852 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530853 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800854 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530855 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530856
857 if (atomic_read(&motg->in_lpm))
858 return 0;
859
860 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530861 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
862 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700863 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300864 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
865 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530866 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700867
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530868 /*
869 * Abort suspend when,
870 * 1. charging detection in progress due to cable plug-in
871 * 2. host mode activation in progress due to Micro-A cable insertion
872 */
873
874 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
875 !dcp) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham502bea32012-08-13 15:34:20 -0700876 enable_irq(motg->irq);
877 return -EBUSY;
878 }
879
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530880 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530881 * Chipidea 45-nm PHY suspend sequence:
882 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530883 * Interrupt Latch Register auto-clear feature is not present
884 * in all PHY versions. Latch register is clear on read type.
885 * Clear latch register to avoid spurious wakeup from
886 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530887 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530888 * PHY comparators are disabled when PHY enters into low power
889 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
890 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
891 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530892 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530893 * PLL is not turned off when PHY enters into low power mode (LPM).
894 * Disable PLL for maximum power savings.
895 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530896
897 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200898 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530899 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200900 ulpi_write(phy, 0x01, 0x30);
901 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530902 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530903
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530905 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530906 * PHY may take some time or even fail to enter into low power
907 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
908 * in failure case.
909 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530910 portsc = readl_relaxed(USB_PORTSC);
911 if (!(portsc & PORTSC_PHCD)) {
912 writel_relaxed(portsc | PORTSC_PHCD,
913 USB_PORTSC);
914 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
915 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
916 break;
917 udelay(1);
918 cnt++;
919 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530920 }
921
922 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200923 dev_err(phy->dev, "Unable to suspend PHY\n");
924 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530925 enable_irq(motg->irq);
926 return -ETIMEDOUT;
927 }
928
929 /*
930 * PHY has capability to generate interrupt asynchronously in low
931 * power mode (LPM). This interrupt is level triggered. So USB IRQ
932 * line must be disabled till async interrupt enable bit is cleared
933 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
934 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530935 *
936 * PHY retention mode is disallowed while entering to LPM with wall
937 * charger connected. But PHY is put into suspend mode. Hence
938 * enable asynchronous interrupt to detect charger disconnection when
939 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530940 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530941 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300942 if (host_bus_suspend || device_bus_suspend ||
943 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530944 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
945 else
946 cmd_val |= ULPI_STP_CTRL;
947 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530948
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530949 /*
950 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
951 * PHY retention and collapse can not happen with VDP_SRC enabled.
952 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300953 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
954 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200955 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530956 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +0200957 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530958 if ((motg->pdata->mode == USB_OTG) ||
959 (motg->pdata->mode == USB_HOST))
960 phy_ctrl_val |= (PHY_IDHV_INTEN |
961 PHY_OTGSESSVLDHV_INTEN);
962 else
963 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
964 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530965
Amit Blay58b31472011-11-18 09:39:39 +0200966 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530968 }
969
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970 /* Ensure that above operation is completed before turning off clocks */
971 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300972 /* Consider clocks on workaround flag only in case of bus suspend */
973 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
974 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
975 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300976 clk_disable_unprepare(motg->pclk);
977 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300978 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300979 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530980
Anji jonnala7da3f262011-12-02 17:22:14 -0800981 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530982 if (!host_bus_suspend) {
983 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
984 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700985 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530986 "TCXO D0 buffer%d\n", __func__, ret);
987 else
988 motg->lpm_flags |= XO_SHUTDOWN;
989 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800990
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530991 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
992 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +0200993 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +0200995 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
996 !host_bus_suspend && !device_bus_suspend && !dcp) {
997 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
998 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530999 }
1000
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301001 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301003 msm_hsusb_mhl_switch_enable(motg, 0);
1004 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005
Steve Mucklef132c6c2012-06-06 18:30:57 -07001006 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301007 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001008 if (motg->async_irq)
1009 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 if (motg->pdata->pmic_id_irq)
1011 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001012 if (pdata->otg_control == OTG_PHY_CONTROL &&
1013 pdata->mpm_otgsessvld_int)
1014 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301016 if (bus)
1017 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1018
1019 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001020 /* Enable ASYNC IRQ (if present) during LPM */
1021 if (motg->async_irq)
1022 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301023 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301025
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001026 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301027
1028 return 0;
1029}
1030
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301031static int msm_otg_resume(struct msm_otg *motg)
1032{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001033 struct usb_phy *phy = &motg->phy;
1034 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001035 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301036 int cnt = 0;
1037 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001038 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001039 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301040
1041 if (!atomic_read(&motg->in_lpm))
1042 return 0;
1043
Jack Pham8978b892012-10-17 16:31:39 -07001044 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001045 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301046
Anji jonnala7da3f262011-12-02 17:22:14 -08001047 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301048 if (motg->lpm_flags & XO_SHUTDOWN) {
1049 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1050 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -07001051 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301052 "TCXO D0 buffer%d\n", __func__, ret);
1053 motg->lpm_flags &= ~XO_SHUTDOWN;
1054 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301055
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001056 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001057 clk_prepare_enable(motg->core_clk);
1058 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001059 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001060 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301061
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001062 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001063 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001065 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1066 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1067 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 }
1069
1070 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301071 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301072 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001073 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1074 phy_ctrl_val |= PHY_RETEN;
1075 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1076 /* Disable PHY HV interrupts */
1077 phy_ctrl_val &=
1078 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
1079 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301081 }
1082
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301083 temp = readl(USB_USBCMD);
1084 temp &= ~ASYNC_INTR_CTRL;
1085 temp &= ~ULPI_STP_CTRL;
1086 writel(temp, USB_USBCMD);
1087
1088 /*
1089 * PHY comes out of low power mode (LPM) in case of wakeup
1090 * from asynchronous interrupt.
1091 */
1092 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1093 goto skip_phy_resume;
1094
1095 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1096 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1097 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1098 break;
1099 udelay(1);
1100 cnt++;
1101 }
1102
1103 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1104 /*
1105 * This is a fatal error. Reset the link and
1106 * PHY. USB state can not be restored. Re-insertion
1107 * of USB cable is the only way to get USB working.
1108 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001109 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301110 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001111 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301112 }
1113
1114skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001115 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301116 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001117 if (motg->async_irq)
1118 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001119 if (motg->pdata->pmic_id_irq)
1120 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001121 if (pdata->otg_control == OTG_PHY_CONTROL &&
1122 pdata->mpm_otgsessvld_int)
1123 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001124 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301125 if (bus)
1126 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1127
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301128 atomic_set(&motg->in_lpm, 0);
1129
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301130 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001131 /* Match the disable_irq call from ISR */
1132 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301133 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301134 }
Jack Pham8978b892012-10-17 16:31:39 -07001135 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301136
Manu Gautamf8c45642012-08-10 10:20:56 -07001137 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1138 if (motg->async_irq)
1139 disable_irq(motg->async_irq);
1140
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001141 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301142
1143 return 0;
1144}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301145#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301146
David Keitel272ce522012-08-17 16:25:24 -07001147static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001148{
Jack Pham0c695282012-10-19 18:13:03 -07001149 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001150 pr_err("No USB power supply registered!\n");
1151 return;
1152 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001153
Jack Pham0c695282012-10-19 18:13:03 -07001154 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001155 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301156 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001157 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301158 } else {
David Keitel272ce522012-08-17 16:25:24 -07001159 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301160 /*
1161 * VBUS comparator is disabled by PMIC charging driver
1162 * when SYSTEM scope is selected. For ID_GND->ID_A
1163 * transition, give 50 msec delay so that PMIC charger
1164 * driver detect the VBUS and ready for accepting
1165 * charging current value from USB.
1166 */
1167 if (test_bit(ID_A, &motg->inputs))
1168 msleep(50);
1169 }
David Keitel272ce522012-08-17 16:25:24 -07001170 } else {
1171 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001172 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001173 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001174}
1175
David Keitel081a3e22012-04-18 12:37:07 -07001176static int msm_otg_notify_chg_type(struct msm_otg *motg)
1177{
1178 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001179
David Keitel081a3e22012-04-18 12:37:07 -07001180 /*
1181 * TODO
1182 * Unify OTG driver charger types and power supply charger types
1183 */
1184 if (charger_type == motg->chg_type)
1185 return 0;
1186
1187 if (motg->chg_type == USB_SDP_CHARGER)
1188 charger_type = POWER_SUPPLY_TYPE_USB;
1189 else if (motg->chg_type == USB_CDP_CHARGER)
1190 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301191 else if (motg->chg_type == USB_DCP_CHARGER ||
1192 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001193 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1194 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1195 motg->chg_type == USB_ACA_A_CHARGER ||
1196 motg->chg_type == USB_ACA_B_CHARGER ||
1197 motg->chg_type == USB_ACA_C_CHARGER))
1198 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1199 else
1200 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1201
Jack Pham0c695282012-10-19 18:13:03 -07001202 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001203 pr_err("No USB power supply registered!\n");
1204 return -EINVAL;
1205 }
1206
1207 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001208 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001209 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001210}
1211
Amit Blay0f7edf72012-01-15 10:11:27 +02001212static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1213{
Jack Pham0c695282012-10-19 18:13:03 -07001214 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001215 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1216 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001217 }
1218
David Keitel272ce522012-08-17 16:25:24 -07001219 if (motg->cur_power == 0 && mA > 2) {
1220 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001221 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001222 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001223 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001224 goto psy_error;
1225 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1226 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001227 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001228 goto psy_error;
1229 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001230 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001231 goto psy_error;
1232 }
Jack Pham0c695282012-10-19 18:13:03 -07001233 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001234 return 0;
1235
David Keitel272ce522012-08-17 16:25:24 -07001236psy_error:
1237 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001238 return -ENXIO;
1239}
1240
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301241static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1242{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001243 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301244
1245 if (g && g->is_a_peripheral)
1246 return;
1247
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301248 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1249 motg->chg_type == USB_ACA_A_CHARGER ||
1250 motg->chg_type == USB_ACA_B_CHARGER ||
1251 motg->chg_type == USB_ACA_C_CHARGER) &&
1252 mA > IDEV_ACA_CHG_LIMIT)
1253 mA = IDEV_ACA_CHG_LIMIT;
1254
David Keitel081a3e22012-04-18 12:37:07 -07001255 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001256 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001257 "Failed notifying %d charger type to PMIC\n",
1258 motg->chg_type);
1259
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301260 if (motg->cur_power == mA)
1261 return;
1262
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001263 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001264
1265 /*
1266 * Use Power Supply API if supported, otherwise fallback
1267 * to legacy pm8921 API.
1268 */
1269 if (msm_otg_notify_power_supply(motg, mA))
1270 pm8921_charger_vbus_draw(mA);
1271
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301272 motg->cur_power = mA;
1273}
1274
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001275static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301276{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001277 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301278
1279 /*
1280 * Gadget driver uses set_power method to notify about the
1281 * available current based on suspend/configured states.
1282 *
1283 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1284 * states when CDP/ACA is connected.
1285 */
1286 if (motg->chg_type == USB_SDP_CHARGER)
1287 msm_otg_notify_charger(motg, mA);
1288
1289 return 0;
1290}
1291
Steve Mucklef132c6c2012-06-06 18:30:57 -07001292static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301293{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001294 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301295 struct msm_otg_platform_data *pdata = motg->pdata;
1296 struct usb_hcd *hcd;
1297
1298 if (!otg->host)
1299 return;
1300
1301 hcd = bus_to_hcd(otg->host);
1302
1303 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001304 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301305
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301306 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001307 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301308 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1309
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301310 /*
1311 * Some boards have a switch cotrolled by gpio
1312 * to enable/disable internal HUB. Enable internal
1313 * HUB before kicking the host.
1314 */
1315 if (pdata->setup_gpio)
1316 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301317 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301318 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001319 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301320
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301321 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301322 /* HCD core reset all bits of PORTSC. select ULPI phy */
1323 writel_relaxed(0x80000000, USB_PORTSC);
1324
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301325 if (pdata->setup_gpio)
1326 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301327
1328 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001329 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301330 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301331 }
1332}
1333
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001334static int msm_otg_usbdev_notify(struct notifier_block *self,
1335 unsigned long action, void *priv)
1336{
1337 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001338 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301339 struct usb_device *udev = priv;
1340
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301341 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1342 goto out;
1343
Steve Mucklef132c6c2012-06-06 18:30:57 -07001344 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301345 goto out;
1346 /*
1347 * Interested in devices connected directly to the root hub.
1348 * ACA dock can supply IDEV_CHG irrespective devices connected
1349 * on the accessory port.
1350 */
1351 if (!udev->parent || udev->parent->parent ||
1352 motg->chg_type == USB_ACA_DOCK_CHARGER)
1353 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001354
1355 switch (action) {
1356 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301357 if (aca_enabled())
1358 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001359 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301360 pr_debug("B_CONN set\n");
1361 set_bit(B_CONN, &motg->inputs);
1362 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001363 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301364 /*
1365 * OTG PET: A-device must end session within
1366 * 10 sec after PET enumeration.
1367 */
1368 if (udev->quirks & USB_QUIRK_OTG_PET)
1369 msm_otg_start_timer(motg, TA_TST_MAINT,
1370 A_TST_MAINT);
1371 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301372 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001373 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374 if (udev->actconfig)
1375 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1376 else
1377 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001378 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301379 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301380 break;
1381 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001382 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1383 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301384 pr_debug("B_CONN clear\n");
1385 clear_bit(B_CONN, &motg->inputs);
1386 /*
1387 * OTG PET: A-device must end session after
1388 * PET disconnection if it is enumerated
1389 * with bcdDevice[0] = 1. USB core sets
1390 * bus->otg_vbus_off for us. clear it here.
1391 */
1392 if (udev->bus->otg_vbus_off) {
1393 udev->bus->otg_vbus_off = 0;
1394 set_bit(A_BUS_DROP, &motg->inputs);
1395 }
1396 queue_work(system_nrt_wq, &motg->sm_work);
1397 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001398 default:
1399 break;
1400 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301401 if (test_bit(ID_A, &motg->inputs))
1402 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1403 motg->mA_port);
1404out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001405 return NOTIFY_OK;
1406}
1407
Mayank Ranae3926882011-12-26 09:47:54 +05301408static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1409{
1410 int ret;
1411 static bool vbus_is_on;
1412
1413 if (vbus_is_on == on)
1414 return;
1415
1416 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301417 ret = motg->pdata->vbus_power(on);
1418 if (!ret)
1419 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301420 return;
1421 }
1422
1423 if (!vbus_otg) {
1424 pr_err("vbus_otg is NULL.");
1425 return;
1426 }
1427
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001428 /*
1429 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001430 * from usb before turning on the boost.
1431 * if exiting host mode disable the boost before enabling to draw
1432 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001433 */
Mayank Ranae3926882011-12-26 09:47:54 +05301434 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001435 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301436 ret = regulator_enable(vbus_otg);
1437 if (ret) {
1438 pr_err("unable to enable vbus_otg\n");
1439 return;
1440 }
1441 vbus_is_on = true;
1442 } else {
1443 ret = regulator_disable(vbus_otg);
1444 if (ret) {
1445 pr_err("unable to disable vbus_otg\n");
1446 return;
1447 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001448 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301449 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301450 }
1451}
1452
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001453static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301454{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001455 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301456 struct usb_hcd *hcd;
1457
1458 /*
1459 * Fail host registration if this board can support
1460 * only peripheral configuration.
1461 */
1462 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001463 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301464 return -ENODEV;
1465 }
1466
Mayank Ranae3926882011-12-26 09:47:54 +05301467 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001468 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301469 if (IS_ERR(vbus_otg)) {
1470 pr_err("Unable to get vbus_otg\n");
1471 return -ENODEV;
1472 }
1473 }
1474
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301475 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001476 if (otg->phy->state == OTG_STATE_A_HOST) {
1477 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301479 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301480 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301481 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001482 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301483 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301484 } else {
1485 otg->host = NULL;
1486 }
1487
1488 return 0;
1489 }
1490
1491 hcd = bus_to_hcd(host);
1492 hcd->power_budget = motg->pdata->power_budget;
1493
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301494#ifdef CONFIG_USB_OTG
1495 host->otg_port = 1;
1496#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001497 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1498 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301499 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001500 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301501
1502 /*
1503 * Kick the state machine work, if peripheral is not supported
1504 * or peripheral is already registered with us.
1505 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301506 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001507 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301508 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301509 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301510
1511 return 0;
1512}
1513
Steve Mucklef132c6c2012-06-06 18:30:57 -07001514static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301515{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301516 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001517 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301518 struct msm_otg_platform_data *pdata = motg->pdata;
1519
1520 if (!otg->gadget)
1521 return;
1522
1523 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001524 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301525 /*
1526 * Some boards have a switch cotrolled by gpio
1527 * to enable/disable internal HUB. Disable internal
1528 * HUB before kicking the gadget.
1529 */
1530 if (pdata->setup_gpio)
1531 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001532
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301533 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301534 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301535 ret = msm_bus_scale_client_update_request(
1536 motg->bus_perf_client, 1);
1537 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001538 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301539 "bus bandwidth %d\n", __func__, ret);
1540 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301541 usb_gadget_vbus_connect(otg->gadget);
1542 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001543 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301544 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301545 /* Configure BUS performance parameters to default */
1546 if (motg->bus_perf_client) {
1547 ret = msm_bus_scale_client_update_request(
1548 motg->bus_perf_client, 0);
1549 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001550 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301551 "for bus bw %d\n", __func__, ret);
1552 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301553 if (pdata->setup_gpio)
1554 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1555 }
1556
1557}
1558
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001559static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301560 struct usb_gadget *gadget)
1561{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001562 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301563
1564 /*
1565 * Fail peripheral registration if this board can support
1566 * only host configuration.
1567 */
1568 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001569 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301570 return -ENODEV;
1571 }
1572
1573 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001574 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1575 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301576 msm_otg_start_peripheral(otg, 0);
1577 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001578 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301579 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301580 } else {
1581 otg->gadget = NULL;
1582 }
1583
1584 return 0;
1585 }
1586 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001587 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301588
1589 /*
1590 * Kick the state machine work, if host is not supported
1591 * or host is already registered with us.
1592 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301593 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001594 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301595 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301596 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301597
1598 return 0;
1599}
1600
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301601static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1602{
1603 unsigned long flags;
1604 int id;
1605
1606 if (!motg->pdata->pmic_id_irq)
1607 return -ENODEV;
1608
1609 local_irq_save(flags);
1610 id = irq_read_line(motg->pdata->pmic_id_irq);
1611 local_irq_restore(flags);
1612
1613 /*
1614 * If we can not read ID line state for some reason, treat
1615 * it as float. This would prevent MHL discovery and kicking
1616 * host mode unnecessarily.
1617 */
1618 return !!id;
1619}
1620
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301621static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1622 void (*callback)(int on))
1623{
1624 struct usb_phy *phy = &motg->phy;
1625 int ret;
1626
Manoj Raoa7bddd12012-08-27 20:36:45 -07001627 if (!motg->pdata->mhl_enable) {
1628 dev_dbg(phy->dev, "MHL feature not enabled\n");
1629 return -ENODEV;
1630 }
1631
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301632 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1633 !motg->pdata->pmic_id_irq) {
1634 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1635 return -ENODEV;
1636 }
1637
1638 if (!motg->pdata->mhl_dev_name) {
1639 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1640 return -ENODEV;
1641 }
1642
1643 if (callback)
1644 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1645 callback);
1646 else
1647 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1648
1649 if (ret)
1650 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1651 motg->pdata->mhl_dev_name, ret);
1652 else
1653 motg->mhl_enabled = true;
1654
1655 return ret;
1656}
1657
1658static void msm_otg_mhl_notify_online(int on)
1659{
1660 struct msm_otg *motg = the_msm_otg;
1661 struct usb_phy *phy = &motg->phy;
1662 bool queue = false;
1663
1664 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1665
1666 if (on) {
1667 set_bit(MHL, &motg->inputs);
1668 } else {
1669 clear_bit(MHL, &motg->inputs);
1670 queue = true;
1671 }
1672
1673 if (queue && phy->state != OTG_STATE_UNDEFINED)
1674 schedule_work(&motg->sm_work);
1675}
1676
1677static bool msm_otg_is_mhl(struct msm_otg *motg)
1678{
1679 struct usb_phy *phy = &motg->phy;
1680 int is_mhl, ret;
1681
1682 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1683 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1684 /*
1685 * MHL driver calls our callback saying that MHL connected
1686 * if RID_GND is detected. But at later part of discovery
1687 * it may figure out MHL is not connected and returns
1688 * false. Hence clear MHL input here.
1689 */
1690 clear_bit(MHL, &motg->inputs);
1691 dev_dbg(phy->dev, "MHL device not found\n");
1692 return false;
1693 }
1694
1695 set_bit(MHL, &motg->inputs);
1696 dev_dbg(phy->dev, "MHL device found\n");
1697 return true;
1698}
1699
1700static bool msm_chg_mhl_detect(struct msm_otg *motg)
1701{
1702 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301703
1704 if (!motg->mhl_enabled)
1705 return false;
1706
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301707 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301708
1709 if (id)
1710 return false;
1711
1712 mhl_det_in_progress = true;
1713 ret = msm_otg_is_mhl(motg);
1714 mhl_det_in_progress = false;
1715
1716 return ret;
1717}
1718
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301719static void msm_otg_chg_check_timer_func(unsigned long data)
1720{
1721 struct msm_otg *motg = (struct msm_otg *) data;
1722 struct usb_otg *otg = motg->phy.otg;
1723
1724 if (atomic_read(&motg->in_lpm) ||
1725 !test_bit(B_SESS_VLD, &motg->inputs) ||
1726 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1727 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1728 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1729 return;
1730 }
1731
1732 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1733 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1734 set_bit(B_FALSE_SDP, &motg->inputs);
1735 queue_work(system_nrt_wq, &motg->sm_work);
1736 }
1737}
1738
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001739static bool msm_chg_aca_detect(struct msm_otg *motg)
1740{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001741 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742 u32 int_sts;
1743 bool ret = false;
1744
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301745 if (!aca_enabled())
1746 goto out;
1747
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001748 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1749 goto out;
1750
Steve Mucklef132c6c2012-06-06 18:30:57 -07001751 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001752 switch (int_sts & 0x1C) {
1753 case 0x08:
1754 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001755 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001756 motg->chg_type = USB_ACA_A_CHARGER;
1757 motg->chg_state = USB_CHG_STATE_DETECTED;
1758 clear_bit(ID_B, &motg->inputs);
1759 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301760 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001761 ret = true;
1762 }
1763 break;
1764 case 0x0C:
1765 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001766 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001767 motg->chg_type = USB_ACA_B_CHARGER;
1768 motg->chg_state = USB_CHG_STATE_DETECTED;
1769 clear_bit(ID_A, &motg->inputs);
1770 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301771 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001772 ret = true;
1773 }
1774 break;
1775 case 0x10:
1776 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001777 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001778 motg->chg_type = USB_ACA_C_CHARGER;
1779 motg->chg_state = USB_CHG_STATE_DETECTED;
1780 clear_bit(ID_A, &motg->inputs);
1781 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301782 set_bit(ID, &motg->inputs);
1783 ret = true;
1784 }
1785 break;
1786 case 0x04:
1787 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001788 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301789 motg->chg_type = USB_INVALID_CHARGER;
1790 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1791 clear_bit(ID_A, &motg->inputs);
1792 clear_bit(ID_B, &motg->inputs);
1793 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001794 ret = true;
1795 }
1796 break;
1797 default:
1798 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1799 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301800 test_and_clear_bit(ID_C, &motg->inputs) |
1801 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001802 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001803 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001804 motg->chg_type = USB_INVALID_CHARGER;
1805 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1806 }
1807 }
1808out:
1809 return ret;
1810}
1811
1812static void msm_chg_enable_aca_det(struct msm_otg *motg)
1813{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001814 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001815
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301816 if (!aca_enabled())
1817 return;
1818
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001819 switch (motg->pdata->phy_type) {
1820 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301821 /* Disable ID_GND in link and PHY */
1822 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1823 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001824 ulpi_write(phy, 0x01, 0x0C);
1825 ulpi_write(phy, 0x10, 0x0F);
1826 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301827 /* Disable PMIC ID pull-up */
1828 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301829 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001830 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301831 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001832 break;
1833 default:
1834 break;
1835 }
1836}
1837
1838static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1839{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001840 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001841
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301842 if (!aca_enabled())
1843 return;
1844
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001845 switch (motg->pdata->phy_type) {
1846 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301847 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001848 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301849 break;
1850 default:
1851 break;
1852 }
1853}
1854
1855static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1856{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001857 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301858
1859 if (!aca_enabled())
1860 return;
1861
1862 switch (motg->pdata->phy_type) {
1863 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001864 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001865 break;
1866 default:
1867 break;
1868 }
1869}
1870
1871static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1872{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001873 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001874 bool ret = false;
1875
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301876 if (!aca_enabled())
1877 return ret;
1878
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001879 switch (motg->pdata->phy_type) {
1880 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001881 if (ulpi_read(phy, 0x91) & 1) {
1882 dev_dbg(phy->dev, "RID change\n");
1883 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001884 ret = msm_chg_aca_detect(motg);
1885 }
1886 default:
1887 break;
1888 }
1889 return ret;
1890}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301891
1892static void msm_otg_id_timer_func(unsigned long data)
1893{
1894 struct msm_otg *motg = (struct msm_otg *) data;
1895
1896 if (!aca_enabled())
1897 return;
1898
1899 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001900 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301901 return;
1902 }
1903
Steve Mucklef132c6c2012-06-06 18:30:57 -07001904 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301905 goto out;
1906
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301907 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001908 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301909 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301910 }
1911
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301912out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301913 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1914 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1915}
1916
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301917static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1918{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001919 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301920 u32 chg_det;
1921 bool ret = false;
1922
1923 switch (motg->pdata->phy_type) {
1924 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001925 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301926 ret = chg_det & (1 << 4);
1927 break;
1928 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001929 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301930 ret = chg_det & 1;
1931 break;
1932 default:
1933 break;
1934 }
1935 return ret;
1936}
1937
1938static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1939{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001940 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301941 u32 chg_det;
1942
1943 switch (motg->pdata->phy_type) {
1944 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001945 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301946 /* Turn off charger block */
1947 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001948 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301949 udelay(20);
1950 /* control chg block via ULPI */
1951 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001952 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301953 /* put it in host mode for enabling D- source */
1954 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001955 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301956 /* Turn on chg detect block */
1957 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001958 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301959 udelay(20);
1960 /* enable chg detection */
1961 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001962 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301963 break;
1964 case SNPS_28NM_INTEGRATED_PHY:
1965 /*
1966 * Configure DM as current source, DP as current sink
1967 * and enable battery charging comparators.
1968 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001969 ulpi_write(phy, 0x8, 0x85);
1970 ulpi_write(phy, 0x2, 0x85);
1971 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301972 break;
1973 default:
1974 break;
1975 }
1976}
1977
1978static bool msm_chg_check_primary_det(struct msm_otg *motg)
1979{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001980 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301981 u32 chg_det;
1982 bool ret = false;
1983
1984 switch (motg->pdata->phy_type) {
1985 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001986 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301987 ret = chg_det & (1 << 4);
1988 break;
1989 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001990 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301991 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301992 /* Turn off VDP_SRC */
1993 ulpi_write(phy, 0x3, 0x86);
1994 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301995 break;
1996 default:
1997 break;
1998 }
1999 return ret;
2000}
2001
2002static void msm_chg_enable_primary_det(struct msm_otg *motg)
2003{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002004 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302005 u32 chg_det;
2006
2007 switch (motg->pdata->phy_type) {
2008 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002009 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302010 /* enable chg detection */
2011 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002012 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302013 break;
2014 case SNPS_28NM_INTEGRATED_PHY:
2015 /*
2016 * Configure DP as current source, DM as current sink
2017 * and enable battery charging comparators.
2018 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002019 ulpi_write(phy, 0x2, 0x85);
2020 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302021 break;
2022 default:
2023 break;
2024 }
2025}
2026
2027static bool msm_chg_check_dcd(struct msm_otg *motg)
2028{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002029 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302030 u32 line_state;
2031 bool ret = false;
2032
2033 switch (motg->pdata->phy_type) {
2034 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002035 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302036 ret = !(line_state & 1);
2037 break;
2038 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002039 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302040 ret = line_state & 2;
2041 break;
2042 default:
2043 break;
2044 }
2045 return ret;
2046}
2047
2048static void msm_chg_disable_dcd(struct msm_otg *motg)
2049{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002050 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302051 u32 chg_det;
2052
2053 switch (motg->pdata->phy_type) {
2054 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002055 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302056 chg_det &= ~(1 << 5);
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:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002060 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302061 break;
2062 default:
2063 break;
2064 }
2065}
2066
2067static void msm_chg_enable_dcd(struct msm_otg *motg)
2068{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002069 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302070 u32 chg_det;
2071
2072 switch (motg->pdata->phy_type) {
2073 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002074 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302075 /* Turn on D+ current source */
2076 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002077 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302078 break;
2079 case SNPS_28NM_INTEGRATED_PHY:
2080 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002081 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302082 break;
2083 default:
2084 break;
2085 }
2086}
2087
2088static void msm_chg_block_on(struct msm_otg *motg)
2089{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002090 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302091 u32 func_ctrl, chg_det;
2092
2093 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002094 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302095 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2096 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002097 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302098
2099 switch (motg->pdata->phy_type) {
2100 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002101 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302102 /* control chg block via ULPI */
2103 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002104 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302105 /* Turn on chg detect block */
2106 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002107 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302108 udelay(20);
2109 break;
2110 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302111 /* disable DP and DM pull down resistors */
2112 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302113 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002114 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302115 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002116 ulpi_write(phy, 0x1F, 0x92);
2117 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302118 udelay(100);
2119 break;
2120 default:
2121 break;
2122 }
2123}
2124
2125static void msm_chg_block_off(struct msm_otg *motg)
2126{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002127 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302128 u32 func_ctrl, chg_det;
2129
2130 switch (motg->pdata->phy_type) {
2131 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002132 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302133 /* Turn off charger block */
2134 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002135 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302136 break;
2137 case SNPS_28NM_INTEGRATED_PHY:
2138 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002139 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302140 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002141 ulpi_write(phy, 0x1F, 0x92);
2142 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302143 break;
2144 default:
2145 break;
2146 }
2147
2148 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002149 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302150 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2151 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002152 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302153}
2154
Anji jonnalad270e2d2011-08-09 11:28:32 +05302155static const char *chg_to_string(enum usb_chg_type chg_type)
2156{
2157 switch (chg_type) {
2158 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2159 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2160 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2161 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2162 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2163 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2164 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302165 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302166 default: return "INVALID_CHARGER";
2167 }
2168}
2169
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302170#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
2171#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302172#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2173#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302174static void msm_chg_detect_work(struct work_struct *w)
2175{
2176 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002177 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302178 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302179 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302180 unsigned long delay;
2181
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002182 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302183
2184 if (test_bit(MHL, &motg->inputs)) {
2185 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2186 return;
2187 }
2188
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302189 switch (motg->chg_state) {
2190 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302191 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302192 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002193 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302194 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2195 motg->dcd_retries = 0;
2196 delay = MSM_CHG_DCD_POLL_TIME;
2197 break;
2198 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302199 if (msm_chg_mhl_detect(motg)) {
2200 msm_chg_block_off(motg);
2201 motg->chg_state = USB_CHG_STATE_DETECTED;
2202 motg->chg_type = USB_INVALID_CHARGER;
2203 queue_work(system_nrt_wq, &motg->sm_work);
2204 return;
2205 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002206 is_aca = msm_chg_aca_detect(motg);
2207 if (is_aca) {
2208 /*
2209 * ID_A can be ACA dock too. continue
2210 * primary detection after DCD.
2211 */
2212 if (test_bit(ID_A, &motg->inputs)) {
2213 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2214 } else {
2215 delay = 0;
2216 break;
2217 }
2218 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302219 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302220 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2221 if (is_dcd || tmout) {
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302222 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302223 msm_chg_enable_primary_det(motg);
2224 delay = MSM_CHG_PRIMARY_DET_TIME;
2225 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2226 } else {
2227 delay = MSM_CHG_DCD_POLL_TIME;
2228 }
2229 break;
2230 case USB_CHG_STATE_DCD_DONE:
2231 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302232 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2233 dm_vlgc = line_state & PORTSC_LS_DM;
2234 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302235 if (test_bit(ID_A, &motg->inputs)) {
2236 motg->chg_type = USB_ACA_DOCK_CHARGER;
2237 motg->chg_state = USB_CHG_STATE_DETECTED;
2238 delay = 0;
2239 break;
2240 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302241 if (line_state) { /* DP > VLGC */
2242 motg->chg_type = USB_PROPRIETARY_CHARGER;
2243 motg->chg_state = USB_CHG_STATE_DETECTED;
2244 delay = 0;
2245 } else {
2246 msm_chg_enable_secondary_det(motg);
2247 delay = MSM_CHG_SECONDARY_DET_TIME;
2248 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2249 }
2250 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302251 if (test_bit(ID_A, &motg->inputs)) {
2252 motg->chg_type = USB_ACA_A_CHARGER;
2253 motg->chg_state = USB_CHG_STATE_DETECTED;
2254 delay = 0;
2255 break;
2256 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302257
2258 if (line_state) /* DP > VLGC or/and DM > VLGC */
2259 motg->chg_type = USB_PROPRIETARY_CHARGER;
2260 else
2261 motg->chg_type = USB_SDP_CHARGER;
2262
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302263 motg->chg_state = USB_CHG_STATE_DETECTED;
2264 delay = 0;
2265 }
2266 break;
2267 case USB_CHG_STATE_PRIMARY_DONE:
2268 vout = msm_chg_check_secondary_det(motg);
2269 if (vout)
2270 motg->chg_type = USB_DCP_CHARGER;
2271 else
2272 motg->chg_type = USB_CDP_CHARGER;
2273 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2274 /* fall through */
2275 case USB_CHG_STATE_SECONDARY_DONE:
2276 motg->chg_state = USB_CHG_STATE_DETECTED;
2277 case USB_CHG_STATE_DETECTED:
2278 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002279 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302280 /*
2281 * Spurious interrupt is seen after enabling ACA detection
2282 * due to which charger detection fails in case of PET.
2283 * Add delay of 100 microsec to avoid that.
2284 */
2285 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002286 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002287 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302288 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302289 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302290 return;
2291 default:
2292 return;
2293 }
2294
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302295 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302296}
2297
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302298/*
2299 * We support OTG, Peripheral only and Host only configurations. In case
2300 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2301 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2302 * enabled when switch is controlled by user and default mode is supplied
2303 * by board file, which can be changed by userspace later.
2304 */
2305static void msm_otg_init_sm(struct msm_otg *motg)
2306{
2307 struct msm_otg_platform_data *pdata = motg->pdata;
2308 u32 otgsc = readl(USB_OTGSC);
2309
2310 switch (pdata->mode) {
2311 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002312 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302313 if (pdata->default_mode == USB_HOST) {
2314 clear_bit(ID, &motg->inputs);
2315 } else if (pdata->default_mode == USB_PERIPHERAL) {
2316 set_bit(ID, &motg->inputs);
2317 set_bit(B_SESS_VLD, &motg->inputs);
2318 } else {
2319 set_bit(ID, &motg->inputs);
2320 clear_bit(B_SESS_VLD, &motg->inputs);
2321 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302322 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302323 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302324 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302325 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302326 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302327 set_bit(A_BUS_REQ, &motg->inputs);
2328 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002329 if (otgsc & OTGSC_BSV)
2330 set_bit(B_SESS_VLD, &motg->inputs);
2331 else
2332 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302333 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302334 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302335 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302336 set_bit(ID, &motg->inputs);
2337 else
2338 clear_bit(ID, &motg->inputs);
2339 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302340 /*
2341 * VBUS initial state is reported after PMIC
2342 * driver initialization. Wait for it.
2343 */
2344 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302345 }
2346 break;
2347 case USB_HOST:
2348 clear_bit(ID, &motg->inputs);
2349 break;
2350 case USB_PERIPHERAL:
2351 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302352 if (pdata->otg_control == OTG_PHY_CONTROL) {
2353 if (otgsc & OTGSC_BSV)
2354 set_bit(B_SESS_VLD, &motg->inputs);
2355 else
2356 clear_bit(B_SESS_VLD, &motg->inputs);
2357 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2358 /*
2359 * VBUS initial state is reported after PMIC
2360 * driver initialization. Wait for it.
2361 */
2362 wait_for_completion(&pmic_vbus_init);
2363 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302364 break;
2365 default:
2366 break;
2367 }
2368}
2369
2370static void msm_otg_sm_work(struct work_struct *w)
2371{
2372 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002373 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302374 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302375
Steve Mucklef132c6c2012-06-06 18:30:57 -07002376 pm_runtime_resume(otg->phy->dev);
2377 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002378 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302379 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002380 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302381 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002382 if (!psy && legacy_power_supply) {
2383 psy = power_supply_get_by_name("usb");
2384
2385 if (!psy)
2386 pr_err("couldn't get usb power supply\n");
2387 }
2388
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002389 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302390 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2391 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002392 pm_runtime_put_noidle(otg->phy->dev);
2393 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302394 break;
2395 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302396 /* FALL THROUGH */
2397 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302398 if (test_bit(MHL, &motg->inputs)) {
2399 /* allow LPM */
2400 pm_runtime_put_noidle(otg->phy->dev);
2401 pm_runtime_suspend(otg->phy->dev);
2402 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002403 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302404 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302405 if (msm_chg_mhl_detect(motg)) {
2406 work = 1;
2407 break;
2408 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302409 clear_bit(B_BUS_REQ, &motg->inputs);
2410 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002411 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302412 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302413 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302414 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302415 switch (motg->chg_state) {
2416 case USB_CHG_STATE_UNDEFINED:
2417 msm_chg_detect_work(&motg->chg_work.work);
2418 break;
2419 case USB_CHG_STATE_DETECTED:
2420 switch (motg->chg_type) {
2421 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302422 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002423 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302424 /* fall through */
2425 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302426 msm_otg_notify_charger(motg,
2427 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002428 pm_runtime_put_noidle(otg->phy->dev);
2429 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302430 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302431 case USB_ACA_B_CHARGER:
2432 msm_otg_notify_charger(motg,
2433 IDEV_ACA_CHG_MAX);
2434 /*
2435 * (ID_B --> ID_C) PHY_ALT interrupt can
2436 * not be detected in LPM.
2437 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302438 break;
2439 case USB_CDP_CHARGER:
2440 msm_otg_notify_charger(motg,
2441 IDEV_CHG_MAX);
2442 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002443 otg->phy->state =
2444 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302445 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302446 case USB_ACA_C_CHARGER:
2447 msm_otg_notify_charger(motg,
2448 IDEV_ACA_CHG_MAX);
2449 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002450 otg->phy->state =
2451 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302452 break;
2453 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302454 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002455 otg->phy->state =
2456 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302457 mod_timer(&motg->chg_check_timer,
2458 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302459 break;
2460 default:
2461 break;
2462 }
2463 break;
2464 default:
2465 break;
2466 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302467 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2468 pr_debug("b_sess_end && b_bus_req\n");
2469 if (msm_otg_start_srp(otg) < 0) {
2470 clear_bit(B_BUS_REQ, &motg->inputs);
2471 work = 1;
2472 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302473 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002474 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302475 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2476 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302477 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302478 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302479 del_timer_sync(&motg->chg_check_timer);
2480 clear_bit(B_FALSE_SDP, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302481 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302482 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2483 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302484 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002485 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302486 /*
2487 * There is a small window where ID interrupt
2488 * is not monitored during ID detection circuit
2489 * switch from ACA to PMIC. Check ID state
2490 * before entering into low power mode.
2491 */
2492 if (!msm_otg_read_pmic_id_state(motg)) {
2493 pr_debug("process missed ID intr\n");
2494 clear_bit(ID, &motg->inputs);
2495 work = 1;
2496 break;
2497 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002498 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002499 /*
2500 * Only if autosuspend was enabled in probe, it will be
2501 * used here. Otherwise, no delay will be used.
2502 */
2503 pm_runtime_mark_last_busy(otg->phy->dev);
2504 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302505 }
2506 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302507 case OTG_STATE_B_SRP_INIT:
2508 if (!test_bit(ID, &motg->inputs) ||
2509 test_bit(ID_A, &motg->inputs) ||
2510 test_bit(ID_C, &motg->inputs) ||
2511 (test_bit(B_SESS_VLD, &motg->inputs) &&
2512 !test_bit(ID_B, &motg->inputs))) {
2513 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2514 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002515 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302516 /*
2517 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2518 * bits after SRP initiation.
2519 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002520 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302521 work = 1;
2522 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2523 pr_debug("b_srp_fail\n");
2524 pr_info("A-device did not respond to SRP\n");
2525 clear_bit(B_BUS_REQ, &motg->inputs);
2526 clear_bit(B_SRP_FAIL, &motg->tmouts);
2527 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002528 ulpi_write(otg->phy, 0x0, 0x98);
2529 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302530 motg->b_last_se0_sess = jiffies;
2531 work = 1;
2532 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302533 break;
2534 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302535 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2536 test_bit(B_FALSE_SDP, &motg->inputs)) {
2537 pr_debug("B_FALSE_SDP\n");
2538 msm_otg_start_peripheral(otg, 0);
2539 motg->chg_type = USB_DCP_CHARGER;
2540 clear_bit(B_FALSE_SDP, &motg->inputs);
2541 otg->phy->state = OTG_STATE_B_IDLE;
2542 work = 1;
2543 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302544 test_bit(ID_A, &motg->inputs) ||
2545 test_bit(ID_B, &motg->inputs) ||
2546 !test_bit(B_SESS_VLD, &motg->inputs)) {
2547 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302548 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2549 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302550 msm_otg_notify_charger(motg, 0);
2551 srp_reqd = otg->gadget->otg_srp_reqd;
2552 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302553 if (test_bit(ID_B, &motg->inputs))
2554 clear_bit(ID_B, &motg->inputs);
2555 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002556 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302557 motg->b_last_se0_sess = jiffies;
2558 if (srp_reqd)
2559 msm_otg_start_timer(motg,
2560 TB_TST_SRP, B_TST_SRP);
2561 else
2562 work = 1;
2563 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2564 otg->gadget->b_hnp_enable &&
2565 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2566 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2567 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2568 /* D+ pullup should not be disconnected within 4msec
2569 * after A device suspends the bus. Otherwise PET will
2570 * fail the compliance test.
2571 */
2572 udelay(1000);
2573 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002574 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302575 /*
2576 * start HCD even before A-device enable
2577 * pull-up to meet HNP timings.
2578 */
2579 otg->host->is_b_host = 1;
2580 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002581 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2582 test_bit(B_SESS_VLD, &motg->inputs)) {
2583 pr_debug("a_bus_suspend && b_sess_vld\n");
2584 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002585 pm_runtime_put_noidle(otg->phy->dev);
2586 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002587 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002588 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302589 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590 }
2591 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302592 case OTG_STATE_B_WAIT_ACON:
2593 if (!test_bit(ID, &motg->inputs) ||
2594 test_bit(ID_A, &motg->inputs) ||
2595 test_bit(ID_B, &motg->inputs) ||
2596 !test_bit(B_SESS_VLD, &motg->inputs)) {
2597 pr_debug("!id || id_a/b || !b_sess_vld\n");
2598 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302599 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302600 * A-device is physically disconnected during
2601 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302602 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302603 msm_otg_start_host(otg, 0);
2604 otg->host->is_b_host = 0;
2605
2606 clear_bit(B_BUS_REQ, &motg->inputs);
2607 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2608 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002609 otg->phy->state = OTG_STATE_B_IDLE;
2610 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302611 work = 1;
2612 } else if (test_bit(A_CONN, &motg->inputs)) {
2613 pr_debug("a_conn\n");
2614 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002615 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302616 /*
2617 * PET disconnects D+ pullup after reset is generated
2618 * by B device in B_HOST role which is not detected by
2619 * B device. As workaorund , start timer of 300msec
2620 * and stop timer if A device is enumerated else clear
2621 * A_CONN.
2622 */
2623 msm_otg_start_timer(motg, TB_TST_CONFIG,
2624 B_TST_CONFIG);
2625 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2626 pr_debug("b_ase0_brst_tmout\n");
2627 pr_info("B HNP fail:No response from A device\n");
2628 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002629 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302630 otg->host->is_b_host = 0;
2631 clear_bit(B_ASE0_BRST, &motg->tmouts);
2632 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2633 clear_bit(B_BUS_REQ, &motg->inputs);
2634 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002635 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302636 work = 1;
2637 } else if (test_bit(ID_C, &motg->inputs)) {
2638 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2639 }
2640 break;
2641 case OTG_STATE_B_HOST:
2642 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2643 !test_bit(A_CONN, &motg->inputs) ||
2644 !test_bit(B_SESS_VLD, &motg->inputs)) {
2645 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2646 clear_bit(A_CONN, &motg->inputs);
2647 clear_bit(B_BUS_REQ, &motg->inputs);
2648 msm_otg_start_host(otg, 0);
2649 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002650 otg->phy->state = OTG_STATE_B_IDLE;
2651 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302652 work = 1;
2653 } else if (test_bit(ID_C, &motg->inputs)) {
2654 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2655 }
2656 break;
2657 case OTG_STATE_A_IDLE:
2658 otg->default_a = 1;
2659 if (test_bit(ID, &motg->inputs) &&
2660 !test_bit(ID_A, &motg->inputs)) {
2661 pr_debug("id && !id_a\n");
2662 otg->default_a = 0;
2663 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002664 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302665 del_timer_sync(&motg->id_timer);
2666 msm_otg_link_reset(motg);
2667 msm_chg_enable_aca_intr(motg);
2668 msm_otg_notify_charger(motg, 0);
2669 work = 1;
2670 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2671 (test_bit(A_SRP_DET, &motg->inputs) ||
2672 test_bit(A_BUS_REQ, &motg->inputs))) {
2673 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2674
2675 clear_bit(A_SRP_DET, &motg->inputs);
2676 /* Disable SRP detection */
2677 writel_relaxed((readl_relaxed(USB_OTGSC) &
2678 ~OTGSC_INTSTS_MASK) &
2679 ~OTGSC_DPIE, USB_OTGSC);
2680
Steve Mucklef132c6c2012-06-06 18:30:57 -07002681 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302682 /* VBUS should not be supplied before end of SRP pulse
2683 * generated by PET, if not complaince test fail.
2684 */
2685 usleep_range(10000, 12000);
2686 /* ACA: ID_A: Stop charging untill enumeration */
2687 if (test_bit(ID_A, &motg->inputs))
2688 msm_otg_notify_charger(motg, 0);
2689 else
2690 msm_hsusb_vbus_power(motg, 1);
2691 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2692 } else {
2693 pr_debug("No session requested\n");
2694 clear_bit(A_BUS_DROP, &motg->inputs);
2695 if (test_bit(ID_A, &motg->inputs)) {
2696 msm_otg_notify_charger(motg,
2697 IDEV_ACA_CHG_MAX);
2698 } else if (!test_bit(ID, &motg->inputs)) {
2699 msm_otg_notify_charger(motg, 0);
2700 /*
2701 * A-device is not providing power on VBUS.
2702 * Enable SRP detection.
2703 */
2704 writel_relaxed(0x13, USB_USBMODE);
2705 writel_relaxed((readl_relaxed(USB_OTGSC) &
2706 ~OTGSC_INTSTS_MASK) |
2707 OTGSC_DPIE, USB_OTGSC);
2708 mb();
2709 }
2710 }
2711 break;
2712 case OTG_STATE_A_WAIT_VRISE:
2713 if ((test_bit(ID, &motg->inputs) &&
2714 !test_bit(ID_A, &motg->inputs)) ||
2715 test_bit(A_BUS_DROP, &motg->inputs) ||
2716 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2717 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2718 clear_bit(A_BUS_REQ, &motg->inputs);
2719 msm_otg_del_timer(motg);
2720 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002721 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302722 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2723 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2724 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002725 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302726 if (TA_WAIT_BCON > 0)
2727 msm_otg_start_timer(motg, TA_WAIT_BCON,
2728 A_WAIT_BCON);
2729 msm_otg_start_host(otg, 1);
2730 msm_chg_enable_aca_det(motg);
2731 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302732 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302733 if (msm_chg_check_aca_intr(motg))
2734 work = 1;
2735 }
2736 break;
2737 case OTG_STATE_A_WAIT_BCON:
2738 if ((test_bit(ID, &motg->inputs) &&
2739 !test_bit(ID_A, &motg->inputs)) ||
2740 test_bit(A_BUS_DROP, &motg->inputs) ||
2741 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2742 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2743 "a_wait_bcon_tmout\n");
2744 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2745 pr_info("Device No Response\n");
2746 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2747 }
2748 msm_otg_del_timer(motg);
2749 clear_bit(A_BUS_REQ, &motg->inputs);
2750 clear_bit(B_CONN, &motg->inputs);
2751 msm_otg_start_host(otg, 0);
2752 /*
2753 * ACA: ID_A with NO accessory, just the A plug is
2754 * attached to ACA: Use IDCHG_MAX for charging
2755 */
2756 if (test_bit(ID_A, &motg->inputs))
2757 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2758 else
2759 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002760 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302761 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2762 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2763 pr_debug("!a_vbus_vld\n");
2764 clear_bit(B_CONN, &motg->inputs);
2765 msm_otg_del_timer(motg);
2766 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002767 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2768 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302769 } else if (test_bit(ID_A, &motg->inputs)) {
2770 msm_hsusb_vbus_power(motg, 0);
2771 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2772 /*
2773 * If TA_WAIT_BCON is infinite, we don;t
2774 * turn off VBUS. Enter low power mode.
2775 */
2776 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002777 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302778 } else if (!test_bit(ID, &motg->inputs)) {
2779 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302780 }
2781 break;
2782 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302783 if ((test_bit(ID, &motg->inputs) &&
2784 !test_bit(ID_A, &motg->inputs)) ||
2785 test_bit(A_BUS_DROP, &motg->inputs)) {
2786 pr_debug("id_a/b/c || a_bus_drop\n");
2787 clear_bit(B_CONN, &motg->inputs);
2788 clear_bit(A_BUS_REQ, &motg->inputs);
2789 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002790 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302791 msm_otg_start_host(otg, 0);
2792 if (!test_bit(ID_A, &motg->inputs))
2793 msm_hsusb_vbus_power(motg, 0);
2794 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2795 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2796 pr_debug("!a_vbus_vld\n");
2797 clear_bit(B_CONN, &motg->inputs);
2798 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002799 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302800 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002801 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302802 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2803 /*
2804 * a_bus_req is de-asserted when root hub is
2805 * suspended or HNP is in progress.
2806 */
2807 pr_debug("!a_bus_req\n");
2808 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002809 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302810 if (otg->host->b_hnp_enable)
2811 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2812 A_AIDL_BDIS);
2813 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002814 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302815 } else if (!test_bit(B_CONN, &motg->inputs)) {
2816 pr_debug("!b_conn\n");
2817 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002818 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302819 if (TA_WAIT_BCON > 0)
2820 msm_otg_start_timer(motg, TA_WAIT_BCON,
2821 A_WAIT_BCON);
2822 if (msm_chg_check_aca_intr(motg))
2823 work = 1;
2824 } else if (test_bit(ID_A, &motg->inputs)) {
2825 msm_otg_del_timer(motg);
2826 msm_hsusb_vbus_power(motg, 0);
2827 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2828 msm_otg_notify_charger(motg,
2829 IDEV_ACA_CHG_MAX);
2830 else
2831 msm_otg_notify_charger(motg,
2832 IDEV_CHG_MIN - motg->mA_port);
2833 } else if (!test_bit(ID, &motg->inputs)) {
2834 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2835 motg->chg_type = USB_INVALID_CHARGER;
2836 msm_otg_notify_charger(motg, 0);
2837 msm_hsusb_vbus_power(motg, 1);
2838 }
2839 break;
2840 case OTG_STATE_A_SUSPEND:
2841 if ((test_bit(ID, &motg->inputs) &&
2842 !test_bit(ID_A, &motg->inputs)) ||
2843 test_bit(A_BUS_DROP, &motg->inputs) ||
2844 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2845 pr_debug("id_a/b/c || a_bus_drop ||"
2846 "a_aidl_bdis_tmout\n");
2847 msm_otg_del_timer(motg);
2848 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002849 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302850 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002851 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302852 if (!test_bit(ID_A, &motg->inputs))
2853 msm_hsusb_vbus_power(motg, 0);
2854 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2855 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2856 pr_debug("!a_vbus_vld\n");
2857 msm_otg_del_timer(motg);
2858 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002859 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302860 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002861 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302862 } else if (!test_bit(B_CONN, &motg->inputs) &&
2863 otg->host->b_hnp_enable) {
2864 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002865 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302866 msm_otg_host_hnp_enable(otg, 1);
2867 otg->gadget->is_a_peripheral = 1;
2868 msm_otg_start_peripheral(otg, 1);
2869 } else if (!test_bit(B_CONN, &motg->inputs) &&
2870 !otg->host->b_hnp_enable) {
2871 pr_debug("!b_conn && !b_hnp_enable");
2872 /*
2873 * bus request is dropped during suspend.
2874 * acquire again for next device.
2875 */
2876 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002877 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302878 if (TA_WAIT_BCON > 0)
2879 msm_otg_start_timer(motg, TA_WAIT_BCON,
2880 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002881 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302882 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002883 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302884 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002885 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002886 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302887 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302888 }
2889 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302890 case OTG_STATE_A_PERIPHERAL:
2891 if ((test_bit(ID, &motg->inputs) &&
2892 !test_bit(ID_A, &motg->inputs)) ||
2893 test_bit(A_BUS_DROP, &motg->inputs)) {
2894 pr_debug("id _f/b/c || a_bus_drop\n");
2895 /* Clear BIDL_ADIS timer */
2896 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002897 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302898 msm_otg_start_peripheral(otg, 0);
2899 otg->gadget->is_a_peripheral = 0;
2900 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002901 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302902 if (!test_bit(ID_A, &motg->inputs))
2903 msm_hsusb_vbus_power(motg, 0);
2904 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2905 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2906 pr_debug("!a_vbus_vld\n");
2907 /* Clear BIDL_ADIS timer */
2908 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002909 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302910 msm_otg_start_peripheral(otg, 0);
2911 otg->gadget->is_a_peripheral = 0;
2912 msm_otg_start_host(otg, 0);
2913 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2914 pr_debug("a_bidl_adis_tmout\n");
2915 msm_otg_start_peripheral(otg, 0);
2916 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002917 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302918 set_bit(A_BUS_REQ, &motg->inputs);
2919 msm_otg_host_hnp_enable(otg, 0);
2920 if (TA_WAIT_BCON > 0)
2921 msm_otg_start_timer(motg, TA_WAIT_BCON,
2922 A_WAIT_BCON);
2923 } else if (test_bit(ID_A, &motg->inputs)) {
2924 msm_hsusb_vbus_power(motg, 0);
2925 msm_otg_notify_charger(motg,
2926 IDEV_CHG_MIN - motg->mA_port);
2927 } else if (!test_bit(ID, &motg->inputs)) {
2928 msm_otg_notify_charger(motg, 0);
2929 msm_hsusb_vbus_power(motg, 1);
2930 }
2931 break;
2932 case OTG_STATE_A_WAIT_VFALL:
2933 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2934 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002935 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302936 work = 1;
2937 }
2938 break;
2939 case OTG_STATE_A_VBUS_ERR:
2940 if ((test_bit(ID, &motg->inputs) &&
2941 !test_bit(ID_A, &motg->inputs)) ||
2942 test_bit(A_BUS_DROP, &motg->inputs) ||
2943 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002944 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302945 if (!test_bit(ID_A, &motg->inputs))
2946 msm_hsusb_vbus_power(motg, 0);
2947 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2948 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2949 motg->chg_type = USB_INVALID_CHARGER;
2950 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302951 }
2952 break;
2953 default:
2954 break;
2955 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302956 if (work)
2957 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302958}
2959
Amit Blayd0fe07b2012-09-05 16:42:09 +03002960static void msm_otg_suspend_work(struct work_struct *w)
2961{
2962 struct msm_otg *motg =
2963 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05302964
2965 /* This work is only for device bus suspend */
2966 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
2967 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03002968}
2969
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302970static irqreturn_t msm_otg_irq(int irq, void *data)
2971{
2972 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002973 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302974 u32 otgsc = 0, usbsts, pc;
2975 bool work = 0;
2976 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302977
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302978 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002979 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302980 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002981 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07002982 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002983 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302984 return IRQ_HANDLED;
2985 }
2986
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002987 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302988 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302989
2990 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302991 return IRQ_NONE;
2992
2993 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302994 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302995 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302996 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302997 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302998 pr_debug("Id clear\n");
2999 /*
3000 * Assert a_bus_req to supply power on
3001 * VBUS when Micro/Mini-A cable is connected
3002 * with out user intervention.
3003 */
3004 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303005 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303006 msm_chg_enable_aca_det(motg);
3007 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303008 writel_relaxed(otgsc, USB_OTGSC);
3009 work = 1;
3010 } else if (otgsc & OTGSC_DPIS) {
3011 pr_debug("DPIS detected\n");
3012 writel_relaxed(otgsc, USB_OTGSC);
3013 set_bit(A_SRP_DET, &motg->inputs);
3014 set_bit(A_BUS_REQ, &motg->inputs);
3015 work = 1;
3016 } else if (otgsc & OTGSC_BSVIS) {
3017 writel_relaxed(otgsc, USB_OTGSC);
3018 /*
3019 * BSV interrupt comes when operating as an A-device
3020 * (VBUS on/off).
3021 * But, handle BSV when charger is removed from ACA in ID_A
3022 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003023 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303024 !test_bit(ID_A, &motg->inputs))
3025 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303026 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303027 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303028 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303029 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303030 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303031 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003032 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3033
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303034 msm_chg_check_aca_intr(motg);
3035 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303036 work = 1;
3037 } else if (usbsts & STS_PCI) {
3038 pc = readl_relaxed(USB_PORTSC);
3039 pr_debug("portsc = %x\n", pc);
3040 ret = IRQ_NONE;
3041 /*
3042 * HCD Acks PCI interrupt. We use this to switch
3043 * between different OTG states.
3044 */
3045 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003046 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303047 case OTG_STATE_A_SUSPEND:
3048 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3049 !(pc & PORTSC_CCS)) {
3050 pr_debug("B_CONN clear\n");
3051 clear_bit(B_CONN, &motg->inputs);
3052 msm_otg_del_timer(motg);
3053 }
3054 break;
3055 case OTG_STATE_A_PERIPHERAL:
3056 /*
3057 * A-peripheral observed activity on bus.
3058 * clear A_BIDL_ADIS timer.
3059 */
3060 msm_otg_del_timer(motg);
3061 work = 0;
3062 break;
3063 case OTG_STATE_B_WAIT_ACON:
3064 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3065 pr_debug("A_CONN set\n");
3066 set_bit(A_CONN, &motg->inputs);
3067 /* Clear ASE0_BRST timer */
3068 msm_otg_del_timer(motg);
3069 }
3070 break;
3071 case OTG_STATE_B_HOST:
3072 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3073 pr_debug("A_CONN clear\n");
3074 clear_bit(A_CONN, &motg->inputs);
3075 msm_otg_del_timer(motg);
3076 }
3077 break;
3078 case OTG_STATE_A_WAIT_BCON:
3079 if (TA_WAIT_BCON < 0)
3080 set_bit(A_BUS_REQ, &motg->inputs);
3081 default:
3082 work = 0;
3083 break;
3084 }
3085 } else if (usbsts & STS_URI) {
3086 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003087 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303088 case OTG_STATE_A_PERIPHERAL:
3089 /*
3090 * A-peripheral observed activity on bus.
3091 * clear A_BIDL_ADIS timer.
3092 */
3093 msm_otg_del_timer(motg);
3094 work = 0;
3095 break;
3096 default:
3097 work = 0;
3098 break;
3099 }
3100 } else if (usbsts & STS_SLI) {
3101 ret = IRQ_NONE;
3102 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003103 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303104 case OTG_STATE_B_PERIPHERAL:
3105 if (otg->gadget->b_hnp_enable) {
3106 set_bit(A_BUS_SUSPEND, &motg->inputs);
3107 set_bit(B_BUS_REQ, &motg->inputs);
3108 work = 1;
3109 }
3110 break;
3111 case OTG_STATE_A_PERIPHERAL:
3112 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3113 A_BIDL_ADIS);
3114 break;
3115 default:
3116 break;
3117 }
3118 } else if ((usbsts & PHY_ALT_INT)) {
3119 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3120 if (msm_chg_check_aca_intr(motg))
3121 work = 1;
3122 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303123 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303124 if (work)
3125 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303126
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303127 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003128}
3129
3130static void msm_otg_set_vbus_state(int online)
3131{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303132 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003133 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303134
Chiranjeevi Velempati2e1048b2012-11-08 09:54:33 +05303135 /* Ignore received BSV interrupts, if ID pin is GND */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303136 if (!test_bit(ID, &motg->inputs)) {
3137 /*
3138 * state machine work waits for initial VBUS
3139 * completion in UNDEFINED state. Process
3140 * the initial VBUS event in ID_GND state.
3141 */
3142 if (init)
3143 return;
3144 goto complete;
3145 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003146
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303147 if (online) {
3148 pr_debug("PMIC: BSV set\n");
3149 set_bit(B_SESS_VLD, &motg->inputs);
3150 } else {
3151 pr_debug("PMIC: BSV clear\n");
3152 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303153 }
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303154complete:
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303155 if (!init) {
3156 init = true;
3157 complete(&pmic_vbus_init);
3158 pr_debug("PMIC: BSV init complete\n");
3159 return;
3160 }
3161
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303162 if (test_bit(MHL, &motg->inputs) ||
3163 mhl_det_in_progress) {
3164 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3165 return;
3166 }
3167
Jack Pham5ca279b2012-05-14 18:42:54 -07003168 if (atomic_read(&motg->pm_suspended))
3169 motg->sm_work_pending = true;
3170 else
3171 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003172}
3173
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303174static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003175{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303176 struct msm_otg *motg = container_of(w, struct msm_otg,
3177 pmic_id_status_work.work);
3178 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003179
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303180 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303181 if (!test_and_set_bit(ID, &motg->inputs)) {
3182 pr_debug("PMIC: ID set\n");
3183 work = 1;
3184 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303185 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303186 if (test_and_clear_bit(ID, &motg->inputs)) {
3187 pr_debug("PMIC: ID clear\n");
3188 set_bit(A_BUS_REQ, &motg->inputs);
3189 work = 1;
3190 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303191 }
3192
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303193 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003194 if (atomic_read(&motg->pm_suspended))
3195 motg->sm_work_pending = true;
3196 else
3197 queue_work(system_nrt_wq, &motg->sm_work);
3198 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303199
3200}
3201
3202#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3203static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3204{
3205 struct msm_otg *motg = data;
3206
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303207 if (test_bit(MHL, &motg->inputs) ||
3208 mhl_det_in_progress) {
3209 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3210 return IRQ_HANDLED;
3211 }
3212
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303213 if (!aca_id_turned_on)
3214 /*schedule delayed work for 5msec for ID line state to settle*/
3215 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3216 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003217
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303218 return IRQ_HANDLED;
3219}
3220
3221static int msm_otg_mode_show(struct seq_file *s, void *unused)
3222{
3223 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003224 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303225
Steve Mucklef132c6c2012-06-06 18:30:57 -07003226 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303227 case OTG_STATE_A_HOST:
3228 seq_printf(s, "host\n");
3229 break;
3230 case OTG_STATE_B_PERIPHERAL:
3231 seq_printf(s, "peripheral\n");
3232 break;
3233 default:
3234 seq_printf(s, "none\n");
3235 break;
3236 }
3237
3238 return 0;
3239}
3240
3241static int msm_otg_mode_open(struct inode *inode, struct file *file)
3242{
3243 return single_open(file, msm_otg_mode_show, inode->i_private);
3244}
3245
3246static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3247 size_t count, loff_t *ppos)
3248{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303249 struct seq_file *s = file->private_data;
3250 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303251 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003252 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303253 int status = count;
3254 enum usb_mode_type req_mode;
3255
3256 memset(buf, 0x00, sizeof(buf));
3257
3258 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3259 status = -EFAULT;
3260 goto out;
3261 }
3262
3263 if (!strncmp(buf, "host", 4)) {
3264 req_mode = USB_HOST;
3265 } else if (!strncmp(buf, "peripheral", 10)) {
3266 req_mode = USB_PERIPHERAL;
3267 } else if (!strncmp(buf, "none", 4)) {
3268 req_mode = USB_NONE;
3269 } else {
3270 status = -EINVAL;
3271 goto out;
3272 }
3273
3274 switch (req_mode) {
3275 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003276 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303277 case OTG_STATE_A_HOST:
3278 case OTG_STATE_B_PERIPHERAL:
3279 set_bit(ID, &motg->inputs);
3280 clear_bit(B_SESS_VLD, &motg->inputs);
3281 break;
3282 default:
3283 goto out;
3284 }
3285 break;
3286 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003287 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303288 case OTG_STATE_B_IDLE:
3289 case OTG_STATE_A_HOST:
3290 set_bit(ID, &motg->inputs);
3291 set_bit(B_SESS_VLD, &motg->inputs);
3292 break;
3293 default:
3294 goto out;
3295 }
3296 break;
3297 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003298 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303299 case OTG_STATE_B_IDLE:
3300 case OTG_STATE_B_PERIPHERAL:
3301 clear_bit(ID, &motg->inputs);
3302 break;
3303 default:
3304 goto out;
3305 }
3306 break;
3307 default:
3308 goto out;
3309 }
3310
Steve Mucklef132c6c2012-06-06 18:30:57 -07003311 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303312 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303313out:
3314 return status;
3315}
3316
3317const struct file_operations msm_otg_mode_fops = {
3318 .open = msm_otg_mode_open,
3319 .read = seq_read,
3320 .write = msm_otg_mode_write,
3321 .llseek = seq_lseek,
3322 .release = single_release,
3323};
3324
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303325static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3326{
3327 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003328 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303329
Steve Mucklef132c6c2012-06-06 18:30:57 -07003330 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303331 return 0;
3332}
3333
3334static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3335{
3336 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3337}
3338
3339const struct file_operations msm_otg_state_fops = {
3340 .open = msm_otg_otg_state_open,
3341 .read = seq_read,
3342 .llseek = seq_lseek,
3343 .release = single_release,
3344};
3345
Anji jonnalad270e2d2011-08-09 11:28:32 +05303346static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3347{
3348 struct msm_otg *motg = s->private;
3349
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303350 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303351 return 0;
3352}
3353
3354static int msm_otg_chg_open(struct inode *inode, struct file *file)
3355{
3356 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3357}
3358
3359const struct file_operations msm_otg_chg_fops = {
3360 .open = msm_otg_chg_open,
3361 .read = seq_read,
3362 .llseek = seq_lseek,
3363 .release = single_release,
3364};
3365
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303366static int msm_otg_aca_show(struct seq_file *s, void *unused)
3367{
3368 if (debug_aca_enabled)
3369 seq_printf(s, "enabled\n");
3370 else
3371 seq_printf(s, "disabled\n");
3372
3373 return 0;
3374}
3375
3376static int msm_otg_aca_open(struct inode *inode, struct file *file)
3377{
3378 return single_open(file, msm_otg_aca_show, inode->i_private);
3379}
3380
3381static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3382 size_t count, loff_t *ppos)
3383{
3384 char buf[8];
3385
3386 memset(buf, 0x00, sizeof(buf));
3387
3388 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3389 return -EFAULT;
3390
3391 if (!strncmp(buf, "enable", 6))
3392 debug_aca_enabled = true;
3393 else
3394 debug_aca_enabled = false;
3395
3396 return count;
3397}
3398
3399const struct file_operations msm_otg_aca_fops = {
3400 .open = msm_otg_aca_open,
3401 .read = seq_read,
3402 .write = msm_otg_aca_write,
3403 .llseek = seq_lseek,
3404 .release = single_release,
3405};
3406
Manu Gautam8bdcc592012-03-06 11:26:06 +05303407static int msm_otg_bus_show(struct seq_file *s, void *unused)
3408{
3409 if (debug_bus_voting_enabled)
3410 seq_printf(s, "enabled\n");
3411 else
3412 seq_printf(s, "disabled\n");
3413
3414 return 0;
3415}
3416
3417static int msm_otg_bus_open(struct inode *inode, struct file *file)
3418{
3419 return single_open(file, msm_otg_bus_show, inode->i_private);
3420}
3421
3422static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3423 size_t count, loff_t *ppos)
3424{
3425 char buf[8];
3426 int ret;
3427 struct seq_file *s = file->private_data;
3428 struct msm_otg *motg = s->private;
3429
3430 memset(buf, 0x00, sizeof(buf));
3431
3432 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3433 return -EFAULT;
3434
3435 if (!strncmp(buf, "enable", 6)) {
3436 /* Do not vote here. Let OTG statemachine decide when to vote */
3437 debug_bus_voting_enabled = true;
3438 } else {
3439 debug_bus_voting_enabled = false;
3440 if (motg->bus_perf_client) {
3441 ret = msm_bus_scale_client_update_request(
3442 motg->bus_perf_client, 0);
3443 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003444 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303445 "for bus bw %d\n", __func__, ret);
3446 }
3447 }
3448
3449 return count;
3450}
3451
David Keitel272ce522012-08-17 16:25:24 -07003452static int otg_power_get_property_usb(struct power_supply *psy,
3453 enum power_supply_property psp,
3454 union power_supply_propval *val)
3455{
Jack Pham0c695282012-10-19 18:13:03 -07003456 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003457 switch (psp) {
3458 case POWER_SUPPLY_PROP_SCOPE:
3459 if (motg->host_mode)
3460 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3461 else
3462 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3463 break;
3464 case POWER_SUPPLY_PROP_CURRENT_MAX:
3465 val->intval = motg->current_max;
3466 break;
3467 /* Reflect USB enumeration */
3468 case POWER_SUPPLY_PROP_PRESENT:
3469 case POWER_SUPPLY_PROP_ONLINE:
3470 val->intval = motg->online;
3471 break;
3472 default:
3473 return -EINVAL;
3474 }
3475 return 0;
3476}
3477
3478static int otg_power_set_property_usb(struct power_supply *psy,
3479 enum power_supply_property psp,
3480 const union power_supply_propval *val)
3481{
Jack Pham0c695282012-10-19 18:13:03 -07003482 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003483
3484 switch (psp) {
3485 /* Process PMIC notification in PRESENT prop */
3486 case POWER_SUPPLY_PROP_PRESENT:
3487 msm_otg_set_vbus_state(val->intval);
3488 break;
3489 /* The ONLINE property reflects if usb has enumerated */
3490 case POWER_SUPPLY_PROP_ONLINE:
3491 motg->online = val->intval;
3492 break;
3493 case POWER_SUPPLY_PROP_CURRENT_MAX:
3494 motg->current_max = val->intval;
3495 break;
3496 default:
3497 return -EINVAL;
3498 }
3499
3500 power_supply_changed(&motg->usb_psy);
3501 return 0;
3502}
3503
David Collinsd79acc52012-11-26 14:59:00 -08003504static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3505 enum power_supply_property psp)
3506{
3507 switch (psp) {
3508 case POWER_SUPPLY_PROP_PRESENT:
3509 case POWER_SUPPLY_PROP_ONLINE:
3510 case POWER_SUPPLY_PROP_CURRENT_MAX:
3511 return 1;
3512 default:
3513 break;
3514 }
3515
3516 return 0;
3517}
3518
David Keitel272ce522012-08-17 16:25:24 -07003519static char *otg_pm_power_supplied_to[] = {
3520 "battery",
3521};
3522
3523static enum power_supply_property otg_pm_power_props_usb[] = {
3524 POWER_SUPPLY_PROP_PRESENT,
3525 POWER_SUPPLY_PROP_ONLINE,
3526 POWER_SUPPLY_PROP_CURRENT_MAX,
3527 POWER_SUPPLY_PROP_SCOPE,
3528};
3529
Manu Gautam8bdcc592012-03-06 11:26:06 +05303530const struct file_operations msm_otg_bus_fops = {
3531 .open = msm_otg_bus_open,
3532 .read = seq_read,
3533 .write = msm_otg_bus_write,
3534 .llseek = seq_lseek,
3535 .release = single_release,
3536};
3537
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303538static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303539
3540static int msm_otg_debugfs_init(struct msm_otg *motg)
3541{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303542 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303543
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303544 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3545
3546 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3547 return -ENODEV;
3548
Anji jonnalad270e2d2011-08-09 11:28:32 +05303549 if (motg->pdata->mode == USB_OTG &&
3550 motg->pdata->otg_control == OTG_USER_CONTROL) {
3551
Manu Gautam8bdcc592012-03-06 11:26:06 +05303552 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303553 S_IWUSR, msm_otg_dbg_root, motg,
3554 &msm_otg_mode_fops);
3555
Manu Gautam8bdcc592012-03-06 11:26:06 +05303556 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303557 debugfs_remove(msm_otg_dbg_root);
3558 msm_otg_dbg_root = NULL;
3559 return -ENODEV;
3560 }
3561 }
3562
Manu Gautam8bdcc592012-03-06 11:26:06 +05303563 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303564 msm_otg_dbg_root, motg,
3565 &msm_otg_chg_fops);
3566
Manu Gautam8bdcc592012-03-06 11:26:06 +05303567 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303568 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303569 return -ENODEV;
3570 }
3571
Manu Gautam8bdcc592012-03-06 11:26:06 +05303572 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303573 msm_otg_dbg_root, motg,
3574 &msm_otg_aca_fops);
3575
Manu Gautam8bdcc592012-03-06 11:26:06 +05303576 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303577 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303578 return -ENODEV;
3579 }
3580
Manu Gautam8bdcc592012-03-06 11:26:06 +05303581 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3582 msm_otg_dbg_root, motg,
3583 &msm_otg_bus_fops);
3584
3585 if (!msm_otg_dentry) {
3586 debugfs_remove_recursive(msm_otg_dbg_root);
3587 return -ENODEV;
3588 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303589
3590 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3591 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3592
3593 if (!msm_otg_dentry) {
3594 debugfs_remove_recursive(msm_otg_dbg_root);
3595 return -ENODEV;
3596 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303597 return 0;
3598}
3599
3600static void msm_otg_debugfs_cleanup(void)
3601{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303602 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303603}
3604
Manu Gautam0ddbd922012-09-21 17:17:38 +05303605#define MSM_OTG_CMD_ID 0x09
3606#define MSM_OTG_DEVICE_ID 0x04
3607#define MSM_OTG_VMID_IDX 0xFF
3608#define MSM_OTG_MEM_TYPE 0x02
3609struct msm_otg_scm_cmd_buf {
3610 unsigned int device_id;
3611 unsigned int vmid_idx;
3612 unsigned int mem_type;
3613} __attribute__ ((__packed__));
3614
3615static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3616{
3617 int ret;
3618 struct msm_otg_platform_data *pdata = motg->pdata;
3619 struct msm_otg_scm_cmd_buf cmd_buf;
3620
3621 if (!pdata->pnoc_errata_fix)
3622 return;
3623
3624 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3625
3626 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3627 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3628 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3629
3630 ret = scm_call(SCM_SVC_CP, MSM_OTG_CMD_ID, &cmd_buf,
3631 sizeof(cmd_buf), NULL, 0);
3632
3633 if (ret)
3634 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3635}
3636
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303637static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3638static struct platform_device *msm_otg_add_pdev(
3639 struct platform_device *ofdev, const char *name)
3640{
3641 struct platform_device *pdev;
3642 const struct resource *res = ofdev->resource;
3643 unsigned int num = ofdev->num_resources;
3644 int retval;
3645
3646 pdev = platform_device_alloc(name, -1);
3647 if (!pdev) {
3648 retval = -ENOMEM;
3649 goto error;
3650 }
3651
3652 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3653 pdev->dev.dma_mask = &msm_otg_dma_mask;
3654
3655 if (num) {
3656 retval = platform_device_add_resources(pdev, res, num);
3657 if (retval)
3658 goto error;
3659 }
3660
3661 retval = platform_device_add(pdev);
3662 if (retval)
3663 goto error;
3664
3665 return pdev;
3666
3667error:
3668 platform_device_put(pdev);
3669 return ERR_PTR(retval);
3670}
3671
3672static int msm_otg_setup_devices(struct platform_device *ofdev,
3673 enum usb_mode_type mode, bool init)
3674{
3675 const char *gadget_name = "msm_hsusb";
3676 const char *host_name = "msm_hsusb_host";
3677 static struct platform_device *gadget_pdev;
3678 static struct platform_device *host_pdev;
3679 int retval = 0;
3680
3681 if (!init) {
3682 if (gadget_pdev)
3683 platform_device_unregister(gadget_pdev);
3684 if (host_pdev)
3685 platform_device_unregister(host_pdev);
3686 return 0;
3687 }
3688
3689 switch (mode) {
3690 case USB_OTG:
3691 /* fall through */
3692 case USB_PERIPHERAL:
3693 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3694 if (IS_ERR(gadget_pdev)) {
3695 retval = PTR_ERR(gadget_pdev);
3696 break;
3697 }
3698 if (mode == USB_PERIPHERAL)
3699 break;
3700 /* fall through */
3701 case USB_HOST:
3702 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3703 if (IS_ERR(host_pdev)) {
3704 retval = PTR_ERR(host_pdev);
3705 if (mode == USB_OTG)
3706 platform_device_unregister(gadget_pdev);
3707 }
3708 break;
3709 default:
3710 break;
3711 }
3712
3713 return retval;
3714}
3715
David Keitel272ce522012-08-17 16:25:24 -07003716static int msm_otg_register_power_supply(struct platform_device *pdev,
3717 struct msm_otg *motg)
3718{
3719 int ret;
3720
3721 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3722 if (ret < 0) {
3723 dev_err(motg->phy.dev,
3724 "%s:power_supply_register usb failed\n",
3725 __func__);
3726 return ret;
3727 }
3728
3729 legacy_power_supply = false;
3730 return 0;
3731}
3732
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303733struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3734{
3735 struct device_node *node = pdev->dev.of_node;
3736 struct msm_otg_platform_data *pdata;
3737 int len = 0;
3738
3739 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3740 if (!pdata) {
3741 pr_err("unable to allocate platform data\n");
3742 return NULL;
3743 }
3744 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3745 if (len) {
3746 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3747 if (!pdata->phy_init_seq)
3748 return NULL;
3749 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3750 pdata->phy_init_seq,
3751 len/sizeof(*pdata->phy_init_seq));
3752 }
3753 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3754 &pdata->power_budget);
3755 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3756 &pdata->mode);
3757 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3758 &pdata->otg_control);
3759 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3760 &pdata->default_mode);
3761 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3762 &pdata->phy_type);
3763 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3764 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303765 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3766 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05303767 pdata->pnoc_errata_fix = of_property_read_bool(node,
3768 "qcom,hsusb-otg-pnoc-errata-fix");
Manu Gautambd53fba2012-07-31 16:13:06 +05303769
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303770 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303771}
3772
3773static int __init msm_otg_probe(struct platform_device *pdev)
3774{
Manu Gautamf8c45642012-08-10 10:20:56 -07003775 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303776 struct resource *res;
3777 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003778 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303779 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303780
3781 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303782
3783 if (pdev->dev.of_node) {
3784 dev_dbg(&pdev->dev, "device tree enabled\n");
3785 pdata = msm_otg_dt_to_pdata(pdev);
3786 if (!pdata)
3787 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003788
3789 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3790 if (!pdata->bus_scale_table)
3791 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3792
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303793 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3794 if (ret) {
3795 dev_err(&pdev->dev, "devices setup failed\n");
3796 return ret;
3797 }
3798 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303799 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3800 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303801 } else {
3802 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303803 }
3804
3805 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3806 if (!motg) {
3807 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3808 return -ENOMEM;
3809 }
3810
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003811 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3812 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003813 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3814 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303815 goto free_motg;
3816 }
3817
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003818 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303819 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003820 phy = &motg->phy;
3821 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303822
3823 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303824 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3825 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3826 * PMIC can detect ACA ID_GND and generate an interrupt.
3827 */
3828 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3829 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3830 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003831 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303832 }
3833
Ofir Cohen4da266f2012-01-03 10:19:29 +02003834 /* initialize reset counter */
3835 motg->reset_counter = 0;
3836
Amit Blay02eff132011-09-21 16:46:24 +03003837 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003838 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003839 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003840 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303841
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303842 /*
3843 * Targets on which link uses asynchronous reset methodology,
3844 * free running clock is not required during the reset.
3845 */
Manu Gautam5143b252012-01-05 19:25:23 -08003846 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303847 if (IS_ERR(motg->clk))
3848 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3849 else
3850 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303851
3852 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003853 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303854 * CORE CLK must be running at >55Mhz for correct HSUSB
3855 * operation and USB core cannot tolerate frequency changes on
3856 * CORE CLK. For such USB cores, vote for maximum clk frequency
3857 * on pclk source
3858 */
Manu Gautam5143b252012-01-05 19:25:23 -08003859 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3860 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303861 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003862 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303863 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003864 goto put_clk;
3865 }
3866 clk_set_rate(motg->core_clk, INT_MAX);
3867
3868 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3869 if (IS_ERR(motg->pclk)) {
3870 dev_err(&pdev->dev, "failed to get iface_clk\n");
3871 ret = PTR_ERR(motg->pclk);
3872 goto put_core_clk;
3873 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303874
3875 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3876 if (!res) {
3877 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3878 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003879 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303880 }
3881
3882 motg->regs = ioremap(res->start, resource_size(res));
3883 if (!motg->regs) {
3884 dev_err(&pdev->dev, "ioremap failed\n");
3885 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003886 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303887 }
3888 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3889
3890 motg->irq = platform_get_irq(pdev, 0);
3891 if (!motg->irq) {
3892 dev_err(&pdev->dev, "platform_get_irq failed\n");
3893 ret = -ENODEV;
3894 goto free_regs;
3895 }
3896
Manu Gautamf8c45642012-08-10 10:20:56 -07003897 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3898 if (motg->async_irq < 0) {
3899 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3900 motg->async_irq = 0;
3901 }
3902
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003903 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003904 if (IS_ERR(motg->xo_handle)) {
3905 dev_err(&pdev->dev, "%s not able to get the handle "
3906 "to vote for TCXO D0 buffer\n", __func__);
3907 ret = PTR_ERR(motg->xo_handle);
3908 goto free_regs;
3909 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303910
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003911 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003912 if (ret) {
3913 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3914 "D0 buffer%d\n", __func__, ret);
3915 goto free_xo_handle;
3916 }
3917
Manu Gautam28b1bac2012-01-30 16:43:06 +05303918 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303919
Mayank Rana248698c2012-04-19 00:03:16 +05303920 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003921 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303922 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003923 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303924 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003925 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003926 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303927 goto devote_xo_handle;
3928 }
3929 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303930 }
3931
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003932 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303933 if (ret) {
3934 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303935 goto devote_xo_handle;
3936 }
3937
3938 ret = regulator_enable(hsusb_vddcx);
3939 if (ret) {
3940 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3941 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303942 }
3943
3944 ret = msm_hsusb_ldo_init(motg, 1);
3945 if (ret) {
3946 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303947 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303948 }
3949
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303950 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303951 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3952 "mhl_usb_hs_switch");
3953 if (IS_ERR(mhl_usb_hs_switch)) {
3954 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003955 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303956 goto free_ldo_init;
3957 }
3958 }
3959
Amit Blay81801aa2012-09-19 12:08:12 +02003960 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303961 if (ret) {
3962 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003963 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303964 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303965 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303966
Manu Gautam0ddbd922012-09-21 17:17:38 +05303967 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
3968 msm_otg_pnoc_errata_fix(motg);
3969
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303970 writel(0, USB_USBINTR);
3971 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003972 /* Ensure that above STOREs are completed before enabling interrupts */
3973 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303974
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303975 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3976 if (ret)
3977 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003978 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303979 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303980 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303981 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303982 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003983 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303984 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3985 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05303986 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
3987 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303988 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3989 "msm_otg", motg);
3990 if (ret) {
3991 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003992 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303993 }
3994
Manu Gautamf8c45642012-08-10 10:20:56 -07003995 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05303996 ret = request_irq(motg->async_irq, msm_otg_irq,
3997 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07003998 if (ret) {
3999 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4000 goto free_irq;
4001 }
4002 disable_irq(motg->async_irq);
4003 }
4004
Jack Pham87f202f2012-08-06 00:24:22 -07004005 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4006 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4007
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004008 phy->init = msm_otg_reset;
4009 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004010 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304011
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004012 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304013
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004014 phy->otg->phy = &motg->phy;
4015 phy->otg->set_host = msm_otg_set_host;
4016 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004017 phy->otg->start_hnp = msm_otg_start_hnp;
4018 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004019
4020 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304021 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004022 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004023 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304024 }
4025
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304026 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304027 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004028 if (motg->pdata->pmic_id_irq) {
4029 ret = request_irq(motg->pdata->pmic_id_irq,
4030 msm_pmic_id_irq,
4031 IRQF_TRIGGER_RISING |
4032 IRQF_TRIGGER_FALLING,
4033 "msm_otg", motg);
4034 if (ret) {
4035 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004036 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004037 }
4038 } else {
4039 ret = -ENODEV;
4040 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004041 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004042 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304043 }
4044
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304045 msm_hsusb_mhl_switch_enable(motg, 1);
4046
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304047 platform_set_drvdata(pdev, motg);
4048 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004049 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304050
Anji jonnalad270e2d2011-08-09 11:28:32 +05304051 ret = msm_otg_debugfs_init(motg);
4052 if (ret)
4053 dev_dbg(&pdev->dev, "mode debugfs file is"
4054 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304055
Amit Blay58b31472011-11-18 09:39:39 +02004056 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4057 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304058 (!(motg->pdata->mode == USB_OTG) ||
4059 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004060 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304061 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004062
Amit Blay58b31472011-11-18 09:39:39 +02004063 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004064 motg->caps = ALLOW_PHY_RETENTION |
4065 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02004066 }
4067
Amit Blay6fa647a2012-05-24 14:12:08 +03004068 if (motg->pdata->enable_lpm_on_dev_suspend)
4069 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4070
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004071 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304072 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004073 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304074
Amit Blayd6f38282012-10-29 13:13:46 +02004075 if (motg->pdata->delay_lpm_on_disconnect) {
4076 pm_runtime_set_autosuspend_delay(&pdev->dev,
4077 lpm_disconnect_thresh);
4078 pm_runtime_use_autosuspend(&pdev->dev);
4079 }
4080
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304081 if (motg->pdata->bus_scale_table) {
4082 motg->bus_perf_client =
4083 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4084 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004085 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304086 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05304087 else
4088 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304089 }
4090
David Keitel272ce522012-08-17 16:25:24 -07004091 motg->usb_psy.name = "usb";
4092 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4093 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4094 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4095 motg->usb_psy.properties = otg_pm_power_props_usb;
4096 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4097 motg->usb_psy.get_property = otg_power_get_property_usb;
4098 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004099 motg->usb_psy.property_is_writeable
4100 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004101
Jack Pham0c695282012-10-19 18:13:03 -07004102 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004103 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004104 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4105 legacy_power_supply = true;
4106 } else {
4107 /* otherwise register our own power supply */
4108 if (!msm_otg_register_power_supply(pdev, motg))
4109 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004110 }
4111
Jack Pham0c695282012-10-19 18:13:03 -07004112 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4113 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4114
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304115 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004116
Steve Mucklef132c6c2012-06-06 18:30:57 -07004117remove_phy:
4118 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004119free_async_irq:
4120 if (motg->async_irq)
4121 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304122free_irq:
4123 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004124destroy_wlock:
4125 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304126 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004127 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004128free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304129 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304130free_hsusb_vddcx:
4131 regulator_disable(hsusb_vddcx);
4132free_config_vddcx:
4133 regulator_set_voltage(hsusb_vddcx,
4134 vdd_val[motg->vdd_type][VDD_NONE],
4135 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004136devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304137 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004138 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004139free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004140 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304141free_regs:
4142 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004143put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304144 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304145put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004146 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304147put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304148 if (!IS_ERR(motg->clk))
4149 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004150 if (!IS_ERR(motg->phy_reset_clk))
4151 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004152free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004153 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304154free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304155 kfree(motg);
4156 return ret;
4157}
4158
4159static int __devexit msm_otg_remove(struct platform_device *pdev)
4160{
4161 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004162 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304163 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304164
4165 if (otg->host || otg->gadget)
4166 return -EBUSY;
4167
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304168 if (pdev->dev.of_node)
4169 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004170 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4171 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304172 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304173 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304174 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304175 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004176 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304177 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304178
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304179 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304180
4181 device_init_wakeup(&pdev->dev, 0);
4182 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004183 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304184
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304185 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004186 if (motg->pdata->pmic_id_irq)
4187 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004188 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304189 free_irq(motg->irq, motg);
4190
Jack Pham87f202f2012-08-06 00:24:22 -07004191 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4192 motg->pdata->mpm_otgsessvld_int)
4193 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4194
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304195 /*
4196 * Put PHY in low power mode.
4197 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07004198 ulpi_read(otg->phy, 0x14);
4199 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304200
4201 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4202 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4203 if (readl(USB_PORTSC) & PORTSC_PHCD)
4204 break;
4205 udelay(1);
4206 cnt++;
4207 }
4208 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004209 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304210
Manu Gautam28b1bac2012-01-30 16:43:06 +05304211 clk_disable_unprepare(motg->pclk);
4212 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004213 msm_xo_put(motg->xo_handle);
Amit Blay81801aa2012-09-19 12:08:12 +02004214 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304215 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304216 regulator_disable(hsusb_vddcx);
4217 regulator_set_voltage(hsusb_vddcx,
4218 vdd_val[motg->vdd_type][VDD_NONE],
4219 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304220
4221 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304222 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304223
Amit Blay02eff132011-09-21 16:46:24 +03004224 if (!IS_ERR(motg->phy_reset_clk))
4225 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304226 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304227 if (!IS_ERR(motg->clk))
4228 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004229 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304230
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304231 if (motg->bus_perf_client)
4232 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304233
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004234 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304235 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304236 return 0;
4237}
4238
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304239#ifdef CONFIG_PM_RUNTIME
4240static int msm_otg_runtime_idle(struct device *dev)
4241{
4242 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004243 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304244
4245 dev_dbg(dev, "OTG runtime idle\n");
4246
Steve Mucklef132c6c2012-06-06 18:30:57 -07004247 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304248 return -EAGAIN;
4249 else
4250 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304251}
4252
4253static int msm_otg_runtime_suspend(struct device *dev)
4254{
4255 struct msm_otg *motg = dev_get_drvdata(dev);
4256
4257 dev_dbg(dev, "OTG runtime suspend\n");
4258 return msm_otg_suspend(motg);
4259}
4260
4261static int msm_otg_runtime_resume(struct device *dev)
4262{
4263 struct msm_otg *motg = dev_get_drvdata(dev);
4264
4265 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304266 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304267 return msm_otg_resume(motg);
4268}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304269#endif
4270
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304271#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304272static int msm_otg_pm_suspend(struct device *dev)
4273{
Jack Pham5ca279b2012-05-14 18:42:54 -07004274 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304275 struct msm_otg *motg = dev_get_drvdata(dev);
4276
4277 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004278
4279 atomic_set(&motg->pm_suspended, 1);
4280 ret = msm_otg_suspend(motg);
4281 if (ret)
4282 atomic_set(&motg->pm_suspended, 0);
4283
4284 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304285}
4286
4287static int msm_otg_pm_resume(struct device *dev)
4288{
Jack Pham5ca279b2012-05-14 18:42:54 -07004289 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304290 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304291
4292 dev_dbg(dev, "OTG PM resume\n");
4293
Jack Pham5ca279b2012-05-14 18:42:54 -07004294 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004295 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004296 pm_runtime_get_noresume(dev);
4297 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304298
Jack Pham5ca279b2012-05-14 18:42:54 -07004299 /* Update runtime PM status */
4300 pm_runtime_disable(dev);
4301 pm_runtime_set_active(dev);
4302 pm_runtime_enable(dev);
4303
Jack Phamc7edb172012-08-13 15:32:39 -07004304 if (motg->sm_work_pending) {
4305 motg->sm_work_pending = false;
4306 queue_work(system_nrt_wq, &motg->sm_work);
4307 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004308 }
4309
4310 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304311}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304312#endif
4313
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304314#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304315static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304316 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4317 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4318 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304319};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304320#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304321
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304322static struct of_device_id msm_otg_dt_match[] = {
4323 { .compatible = "qcom,hsusb-otg",
4324 },
4325 {}
4326};
4327
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304328static struct platform_driver msm_otg_driver = {
4329 .remove = __devexit_p(msm_otg_remove),
4330 .driver = {
4331 .name = DRIVER_NAME,
4332 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304333#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304334 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304335#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304336 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304337 },
4338};
4339
4340static int __init msm_otg_init(void)
4341{
4342 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4343}
4344
4345static void __exit msm_otg_exit(void)
4346{
4347 platform_driver_unregister(&msm_otg_driver);
4348}
4349
4350module_init(msm_otg_init);
4351module_exit(msm_otg_exit);
4352
4353MODULE_LICENSE("GPL v2");
4354MODULE_DESCRIPTION("MSM USB transceiver driver");