blob: b9210cfcf9203d85c1e3daad772d2ece3963eaed [file] [log] [blame]
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05301/* Copyright (c) 2009-2013, Linux Foundation. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053042#include <linux/mhl_8334.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053043
Manu Gautam0ddbd922012-09-21 17:17:38 +053044#include <mach/scm.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070046#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080047#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053048#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053049#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053050
51#define MSM_USB_BASE (motg->regs)
52#define DRIVER_NAME "msm_otg"
53
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053054#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondeti458d8792012-09-28 14:45:18 +053055#define CHG_RECHECK_DELAY (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053056#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053057#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
58#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
59#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
60#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
61
62#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
63#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
64#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
65#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
66
Mayank Rana248698c2012-04-19 00:03:16 +053067#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080068#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053069#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
70
Amit Blayd0fe07b2012-09-05 16:42:09 +030071#define USB_SUSPEND_DELAY_TIME (500 * HZ/1000) /* 500 msec */
72
Amit Blay81801aa2012-09-19 12:08:12 +020073enum msm_otg_phy_reg_mode {
74 USB_PHY_REG_OFF,
75 USB_PHY_REG_ON,
76 USB_PHY_REG_LPM_ON,
77 USB_PHY_REG_LPM_OFF,
78};
79
Mayank Rana443f9e42012-09-21 18:32:39 +053080static char *override_phy_init;
81module_param(override_phy_init, charp, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(override_phy_init,
83 "Override HSUSB PHY Init Settings");
84
Amit Blayd6f38282012-10-29 13:13:46 +020085unsigned int lpm_disconnect_thresh = 1000;
86module_param(lpm_disconnect_thresh , uint, S_IRUGO | S_IWUSR);
87MODULE_PARM_DESC(lpm_disconnect_thresh,
88 "Delay before entering LPM on USB disconnect");
89
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;
Amit Blay58dc2bc2013-01-24 12:28:03 +0200488
489 /*
490 * 10 usec delay is required according to spec. Using larger value
491 * since the exact value proved to not work 100% of the time.
492 */
Bar Weinere7129cb2012-11-28 08:44:14 +0200493 if (IS_ERR(motg->phy_reset_clk))
Amit Blay58dc2bc2013-01-24 12:28:03 +0200494 usleep_range(100, 120);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530495 ret = msm_otg_link_clk_reset(motg, 0);
496 if (ret)
497 return ret;
498
499 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
500 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
501
502 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200503 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530504 ULPI_CLR(ULPI_FUNC_CTRL));
505 if (!ret)
506 break;
507 ret = msm_otg_phy_clk_reset(motg);
508 if (ret)
509 return ret;
510 }
511 if (!retries)
512 return -ETIMEDOUT;
513
514 /* This reset calibrates the phy, if the above write succeeded */
515 ret = msm_otg_phy_clk_reset(motg);
516 if (ret)
517 return ret;
518
519 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200520 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530521 if (ret != -ETIMEDOUT)
522 break;
523 ret = msm_otg_phy_clk_reset(motg);
524 if (ret)
525 return ret;
526 }
527 if (!retries)
528 return -ETIMEDOUT;
529
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200530 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530531 return 0;
532}
533
534#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530535static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530536{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530537 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530538
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530539 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530540 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530541 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530542 break;
543 udelay(1);
544 cnt++;
545 }
546 if (cnt >= LINK_RESET_TIMEOUT_USEC)
547 return -ETIMEDOUT;
548
549 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530550 writel_relaxed(0x80000000, USB_PORTSC);
551 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530552 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530553
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530554 return 0;
555}
556
Steve Mucklef132c6c2012-06-06 18:30:57 -0700557static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530558{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700559 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530560 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530561 int ret;
562 u32 val = 0;
563 u32 ulpi_val = 0;
564
Ofir Cohen4da266f2012-01-03 10:19:29 +0200565 /*
566 * USB PHY and Link reset also reset the USB BAM.
567 * Thus perform reset operation only once to avoid
568 * USB BAM reset on other cases e.g. USB cable disconnections.
569 */
570 if (pdata->disable_reset_on_disconnect) {
571 if (motg->reset_counter)
572 return 0;
573 else
574 motg->reset_counter++;
575 }
576
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530577 if (!IS_ERR(motg->clk))
578 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530579 ret = msm_otg_phy_reset(motg);
580 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700581 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530582 return ret;
583 }
584
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530585 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530586 ret = msm_otg_link_reset(motg);
587 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700588 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530589 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530590 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530591 msleep(100);
592
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530593 ulpi_init(motg);
594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 /* Ensure that RESET operation is completed before turning off clock */
596 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530597
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530598 if (!IS_ERR(motg->clk))
599 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530600
601 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530602 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530603 if (pdata->mode == USB_OTG) {
604 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
605 val |= OTGSC_IDIE | OTGSC_BSVIE;
606 } else if (pdata->mode == USB_PERIPHERAL) {
607 ulpi_val = ULPI_INT_SESS_VALID;
608 val |= OTGSC_BSVIE;
609 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530610 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200611 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
612 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530613 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700614 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530615 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530616 /* Enable PMIC pull-up */
617 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530618 }
619
620 return 0;
621}
622
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530623static const char *timer_string(int bit)
624{
625 switch (bit) {
626 case A_WAIT_VRISE: return "a_wait_vrise";
627 case A_WAIT_VFALL: return "a_wait_vfall";
628 case B_SRP_FAIL: return "b_srp_fail";
629 case A_WAIT_BCON: return "a_wait_bcon";
630 case A_AIDL_BDIS: return "a_aidl_bdis";
631 case A_BIDL_ADIS: return "a_bidl_adis";
632 case B_ASE0_BRST: return "b_ase0_brst";
633 case A_TST_MAINT: return "a_tst_maint";
634 case B_TST_SRP: return "b_tst_srp";
635 case B_TST_CONFIG: return "b_tst_config";
636 default: return "UNDEFINED";
637 }
638}
639
640static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
641{
642 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
643
644 switch (motg->active_tmout) {
645 case A_WAIT_VRISE:
646 /* TODO: use vbus_vld interrupt */
647 set_bit(A_VBUS_VLD, &motg->inputs);
648 break;
649 case A_TST_MAINT:
650 /* OTG PET: End session after TA_TST_MAINT */
651 set_bit(A_BUS_DROP, &motg->inputs);
652 break;
653 case B_TST_SRP:
654 /*
655 * OTG PET: Initiate SRP after TB_TST_SRP of
656 * previous session end.
657 */
658 set_bit(B_BUS_REQ, &motg->inputs);
659 break;
660 case B_TST_CONFIG:
661 clear_bit(A_CONN, &motg->inputs);
662 break;
663 default:
664 set_bit(motg->active_tmout, &motg->tmouts);
665 }
666
667 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
668 queue_work(system_nrt_wq, &motg->sm_work);
669 return HRTIMER_NORESTART;
670}
671
672static void msm_otg_del_timer(struct msm_otg *motg)
673{
674 int bit = motg->active_tmout;
675
676 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
677 div_s64(ktime_to_us(hrtimer_get_remaining(
678 &motg->timer)), 1000));
679 hrtimer_cancel(&motg->timer);
680 clear_bit(bit, &motg->tmouts);
681}
682
683static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
684{
685 clear_bit(bit, &motg->tmouts);
686 motg->active_tmout = bit;
687 pr_debug("starting %s timer\n", timer_string(bit));
688 hrtimer_start(&motg->timer,
689 ktime_set(time / 1000, (time % 1000) * 1000000),
690 HRTIMER_MODE_REL);
691}
692
693static void msm_otg_init_timer(struct msm_otg *motg)
694{
695 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
696 motg->timer.function = msm_otg_timer_func;
697}
698
Steve Mucklef132c6c2012-06-06 18:30:57 -0700699static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530700{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700701 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530702
Steve Mucklef132c6c2012-06-06 18:30:57 -0700703 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530704 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700705 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530706 return -EINVAL;
707 }
708
709 pr_debug("A-Host: HNP initiated\n");
710 clear_bit(A_BUS_REQ, &motg->inputs);
711 queue_work(system_nrt_wq, &motg->sm_work);
712 return 0;
713}
714
Steve Mucklef132c6c2012-06-06 18:30:57 -0700715static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530716{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700717 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530718 u32 val;
719 int ret = 0;
720
Steve Mucklef132c6c2012-06-06 18:30:57 -0700721 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530722 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700723 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530724 ret = -EINVAL;
725 goto out;
726 }
727
728 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
729 pr_debug("initial conditions of SRP are not met. Try again"
730 "after some time\n");
731 ret = -EAGAIN;
732 goto out;
733 }
734
735 pr_debug("B-Device SRP started\n");
736
737 /*
738 * PHY won't pull D+ high unless it detects Vbus valid.
739 * Since by definition, SRP is only done when Vbus is not valid,
740 * software work-around needs to be used to spoof the PHY into
741 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
742 * VBUSVLDEXT register bits.
743 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700744 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530745 /*
746 * Harware auto assist data pulsing: Data pulse is given
747 * for 7msec; wait for vbus
748 */
749 val = readl_relaxed(USB_OTGSC);
750 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
751
752 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
753out:
754 return ret;
755}
756
Steve Mucklef132c6c2012-06-06 18:30:57 -0700757static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530758{
759 struct usb_hcd *hcd = bus_to_hcd(otg->host);
760 struct usb_device *rhub = otg->host->root_hub;
761
762 if (enable) {
763 pm_runtime_disable(&rhub->dev);
764 rhub->state = USB_STATE_NOTATTACHED;
765 hcd->driver->bus_suspend(hcd);
766 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
767 } else {
768 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700769 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530770 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
771 }
772}
773
Steve Mucklef132c6c2012-06-06 18:30:57 -0700774static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530775{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700776 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530777
Amit Blay6fa647a2012-05-24 14:12:08 +0300778 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530779 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530780
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530781 /*
782 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
783 * during bus suspend/resume. Update the relevant state
784 * machine inputs and trigger LPM entry/exit. Checking
785 * in_lpm flag would avoid unnecessary work scheduling.
786 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530787 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700788 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530789 case OTG_STATE_A_WAIT_BCON:
790 if (TA_WAIT_BCON > 0)
791 break;
792 /* fall through */
793 case OTG_STATE_A_HOST:
794 pr_debug("host bus suspend\n");
795 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530796 if (!atomic_read(&motg->in_lpm))
797 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530798 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300799 case OTG_STATE_B_PERIPHERAL:
800 pr_debug("peripheral bus suspend\n");
801 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
802 break;
803 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530804 if (!atomic_read(&motg->in_lpm))
805 queue_delayed_work(system_nrt_wq,
806 &motg->suspend_work,
807 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300808 break;
809
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530810 default:
811 break;
812 }
813 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700814 switch (phy->state) {
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530815 case OTG_STATE_A_WAIT_BCON:
816 /* Remote wakeup or resume */
817 set_bit(A_BUS_REQ, &motg->inputs);
818 /* ensure hardware is not in low power mode */
819 if (atomic_read(&motg->in_lpm))
820 pm_runtime_resume(phy->dev);
821 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530822 case OTG_STATE_A_SUSPEND:
823 /* Remote wakeup or resume */
824 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700825 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700826
827 /* ensure hardware is not in low power mode */
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530828 if (atomic_read(&motg->in_lpm))
829 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530830 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300831 case OTG_STATE_B_PERIPHERAL:
832 pr_debug("peripheral bus resume\n");
833 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
834 break;
835 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530836 if (atomic_read(&motg->in_lpm))
837 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300838 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530839 default:
840 break;
841 }
842 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530843 return 0;
844}
845
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530846#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530847#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
848
849#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530850static int msm_otg_suspend(struct msm_otg *motg)
851{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200852 struct usb_phy *phy = &motg->phy;
853 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530854 struct msm_otg_platform_data *pdata = motg->pdata;
855 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300856 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530857 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800858 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530859 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530860
861 if (atomic_read(&motg->in_lpm))
862 return 0;
863
864 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530865 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
866 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700867 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300868 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
869 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530870 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700871
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530872 /*
873 * Abort suspend when,
874 * 1. charging detection in progress due to cable plug-in
875 * 2. host mode activation in progress due to Micro-A cable insertion
876 */
877
878 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
879 !dcp) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham502bea32012-08-13 15:34:20 -0700880 enable_irq(motg->irq);
881 return -EBUSY;
882 }
883
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530884 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530885 * Chipidea 45-nm PHY suspend sequence:
886 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530887 * Interrupt Latch Register auto-clear feature is not present
888 * in all PHY versions. Latch register is clear on read type.
889 * Clear latch register to avoid spurious wakeup from
890 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530891 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530892 * PHY comparators are disabled when PHY enters into low power
893 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
894 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
895 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530896 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530897 * PLL is not turned off when PHY enters into low power mode (LPM).
898 * Disable PLL for maximum power savings.
899 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530900
901 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200902 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530903 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200904 ulpi_write(phy, 0x01, 0x30);
905 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530906 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530907
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530909 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530910 * PHY may take some time or even fail to enter into low power
911 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
912 * in failure case.
913 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530914 portsc = readl_relaxed(USB_PORTSC);
915 if (!(portsc & PORTSC_PHCD)) {
916 writel_relaxed(portsc | PORTSC_PHCD,
917 USB_PORTSC);
918 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
919 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
920 break;
921 udelay(1);
922 cnt++;
923 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530924 }
925
926 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200927 dev_err(phy->dev, "Unable to suspend PHY\n");
928 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530929 enable_irq(motg->irq);
930 return -ETIMEDOUT;
931 }
932
933 /*
934 * PHY has capability to generate interrupt asynchronously in low
935 * power mode (LPM). This interrupt is level triggered. So USB IRQ
936 * line must be disabled till async interrupt enable bit is cleared
937 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
938 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530939 *
940 * PHY retention mode is disallowed while entering to LPM with wall
941 * charger connected. But PHY is put into suspend mode. Hence
942 * enable asynchronous interrupt to detect charger disconnection when
943 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530944 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530945 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300946 if (host_bus_suspend || device_bus_suspend ||
947 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530948 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
949 else
950 cmd_val |= ULPI_STP_CTRL;
951 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530952
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530953 /*
954 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
955 * PHY retention and collapse can not happen with VDP_SRC enabled.
956 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300957 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
958 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200959 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530960 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +0200961 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530962 if ((motg->pdata->mode == USB_OTG) ||
963 (motg->pdata->mode == USB_HOST))
964 phy_ctrl_val |= (PHY_IDHV_INTEN |
965 PHY_OTGSESSVLDHV_INTEN);
966 else
967 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
968 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530969
Amit Blay58b31472011-11-18 09:39:39 +0200970 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530972 }
973
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974 /* Ensure that above operation is completed before turning off clocks */
975 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300976 /* Consider clocks on workaround flag only in case of bus suspend */
977 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
978 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
979 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300980 clk_disable_unprepare(motg->pclk);
981 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300982 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300983 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530984
Anji jonnala7da3f262011-12-02 17:22:14 -0800985 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530986 if (!host_bus_suspend) {
987 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
988 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700989 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530990 "TCXO D0 buffer%d\n", __func__, ret);
991 else
992 motg->lpm_flags |= XO_SHUTDOWN;
993 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800994
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530995 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
996 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +0200997 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700998 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +0200999 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
1000 !host_bus_suspend && !device_bus_suspend && !dcp) {
1001 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
1002 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +05301003 }
1004
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301005 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301007 msm_hsusb_mhl_switch_enable(motg, 0);
1008 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009
Steve Mucklef132c6c2012-06-06 18:30:57 -07001010 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301011 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001012 if (motg->async_irq)
1013 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 if (motg->pdata->pmic_id_irq)
1015 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001016 if (pdata->otg_control == OTG_PHY_CONTROL &&
1017 pdata->mpm_otgsessvld_int)
1018 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301020 if (bus)
1021 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1022
1023 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001024 /* Enable ASYNC IRQ (if present) during LPM */
1025 if (motg->async_irq)
1026 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301027 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301029
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001030 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301031
1032 return 0;
1033}
1034
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301035static int msm_otg_resume(struct msm_otg *motg)
1036{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001037 struct usb_phy *phy = &motg->phy;
1038 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001039 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301040 int cnt = 0;
1041 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001042 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001043 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301044
1045 if (!atomic_read(&motg->in_lpm))
1046 return 0;
1047
Jack Pham8978b892012-10-17 16:31:39 -07001048 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001049 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301050
Anji jonnala7da3f262011-12-02 17:22:14 -08001051 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301052 if (motg->lpm_flags & XO_SHUTDOWN) {
1053 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1054 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -07001055 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301056 "TCXO D0 buffer%d\n", __func__, ret);
1057 motg->lpm_flags &= ~XO_SHUTDOWN;
1058 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301059
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001060 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001061 clk_prepare_enable(motg->core_clk);
1062 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001063 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001064 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301065
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001066 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001067 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001069 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1070 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1071 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072 }
1073
1074 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301075 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301076 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001077 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1078 phy_ctrl_val |= PHY_RETEN;
1079 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1080 /* Disable PHY HV interrupts */
1081 phy_ctrl_val &=
1082 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
1083 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301085 }
1086
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301087 temp = readl(USB_USBCMD);
1088 temp &= ~ASYNC_INTR_CTRL;
1089 temp &= ~ULPI_STP_CTRL;
1090 writel(temp, USB_USBCMD);
1091
1092 /*
1093 * PHY comes out of low power mode (LPM) in case of wakeup
1094 * from asynchronous interrupt.
1095 */
1096 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1097 goto skip_phy_resume;
1098
1099 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1100 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1101 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1102 break;
1103 udelay(1);
1104 cnt++;
1105 }
1106
1107 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1108 /*
1109 * This is a fatal error. Reset the link and
1110 * PHY. USB state can not be restored. Re-insertion
1111 * of USB cable is the only way to get USB working.
1112 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001113 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301114 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001115 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301116 }
1117
1118skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001119 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301120 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001121 if (motg->async_irq)
1122 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123 if (motg->pdata->pmic_id_irq)
1124 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001125 if (pdata->otg_control == OTG_PHY_CONTROL &&
1126 pdata->mpm_otgsessvld_int)
1127 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001128 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301129 if (bus)
1130 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1131
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301132 atomic_set(&motg->in_lpm, 0);
1133
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301134 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001135 /* Match the disable_irq call from ISR */
1136 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301137 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301138 }
Jack Pham8978b892012-10-17 16:31:39 -07001139 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301140
Manu Gautamf8c45642012-08-10 10:20:56 -07001141 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1142 if (motg->async_irq)
1143 disable_irq(motg->async_irq);
1144
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001145 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301146
1147 return 0;
1148}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301149#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301150
David Keitel272ce522012-08-17 16:25:24 -07001151static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001152{
Jack Pham0c695282012-10-19 18:13:03 -07001153 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001154 pr_err("No USB power supply registered!\n");
1155 return;
1156 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001157
Jack Pham0c695282012-10-19 18:13:03 -07001158 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001159 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301160 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001161 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301162 } else {
David Keitel272ce522012-08-17 16:25:24 -07001163 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301164 /*
1165 * VBUS comparator is disabled by PMIC charging driver
1166 * when SYSTEM scope is selected. For ID_GND->ID_A
1167 * transition, give 50 msec delay so that PMIC charger
1168 * driver detect the VBUS and ready for accepting
1169 * charging current value from USB.
1170 */
1171 if (test_bit(ID_A, &motg->inputs))
1172 msleep(50);
1173 }
David Keitel272ce522012-08-17 16:25:24 -07001174 } else {
1175 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001176 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001177 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001178}
1179
David Keitel081a3e22012-04-18 12:37:07 -07001180static int msm_otg_notify_chg_type(struct msm_otg *motg)
1181{
1182 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001183
David Keitel081a3e22012-04-18 12:37:07 -07001184 /*
1185 * TODO
1186 * Unify OTG driver charger types and power supply charger types
1187 */
1188 if (charger_type == motg->chg_type)
1189 return 0;
1190
1191 if (motg->chg_type == USB_SDP_CHARGER)
1192 charger_type = POWER_SUPPLY_TYPE_USB;
1193 else if (motg->chg_type == USB_CDP_CHARGER)
1194 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301195 else if (motg->chg_type == USB_DCP_CHARGER ||
1196 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001197 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1198 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1199 motg->chg_type == USB_ACA_A_CHARGER ||
1200 motg->chg_type == USB_ACA_B_CHARGER ||
1201 motg->chg_type == USB_ACA_C_CHARGER))
1202 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1203 else
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301204 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001205
Jack Pham0c695282012-10-19 18:13:03 -07001206 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001207 pr_err("No USB power supply registered!\n");
1208 return -EINVAL;
1209 }
1210
1211 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001212 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001213 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001214}
1215
Amit Blay0f7edf72012-01-15 10:11:27 +02001216static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1217{
Jack Pham0c695282012-10-19 18:13:03 -07001218 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001219 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1220 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001221 }
1222
David Keitel272ce522012-08-17 16:25:24 -07001223 if (motg->cur_power == 0 && mA > 2) {
1224 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001225 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001226 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001227 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001228 goto psy_error;
1229 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1230 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001231 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001232 goto psy_error;
1233 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001234 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001235 goto psy_error;
1236 }
Jack Pham0c695282012-10-19 18:13:03 -07001237 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001238 return 0;
1239
David Keitel272ce522012-08-17 16:25:24 -07001240psy_error:
1241 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001242 return -ENXIO;
1243}
1244
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301245static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1246{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001247 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301248
1249 if (g && g->is_a_peripheral)
1250 return;
1251
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301252 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1253 motg->chg_type == USB_ACA_A_CHARGER ||
1254 motg->chg_type == USB_ACA_B_CHARGER ||
1255 motg->chg_type == USB_ACA_C_CHARGER) &&
1256 mA > IDEV_ACA_CHG_LIMIT)
1257 mA = IDEV_ACA_CHG_LIMIT;
1258
David Keitel081a3e22012-04-18 12:37:07 -07001259 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001260 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001261 "Failed notifying %d charger type to PMIC\n",
1262 motg->chg_type);
1263
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301264 if (motg->cur_power == mA)
1265 return;
1266
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001267 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001268
1269 /*
1270 * Use Power Supply API if supported, otherwise fallback
1271 * to legacy pm8921 API.
1272 */
1273 if (msm_otg_notify_power_supply(motg, mA))
1274 pm8921_charger_vbus_draw(mA);
1275
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301276 motg->cur_power = mA;
1277}
1278
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001279static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301280{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001281 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301282
1283 /*
1284 * Gadget driver uses set_power method to notify about the
1285 * available current based on suspend/configured states.
1286 *
1287 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1288 * states when CDP/ACA is connected.
1289 */
1290 if (motg->chg_type == USB_SDP_CHARGER)
1291 msm_otg_notify_charger(motg, mA);
1292
1293 return 0;
1294}
1295
Steve Mucklef132c6c2012-06-06 18:30:57 -07001296static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301297{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001298 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301299 struct msm_otg_platform_data *pdata = motg->pdata;
1300 struct usb_hcd *hcd;
1301
1302 if (!otg->host)
1303 return;
1304
1305 hcd = bus_to_hcd(otg->host);
1306
1307 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001308 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301309
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301310 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001311 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301312 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1313
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301314 /*
1315 * Some boards have a switch cotrolled by gpio
1316 * to enable/disable internal HUB. Enable internal
1317 * HUB before kicking the host.
1318 */
1319 if (pdata->setup_gpio)
1320 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301321 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301322 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001323 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301324
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301325 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301326 /* HCD core reset all bits of PORTSC. select ULPI phy */
1327 writel_relaxed(0x80000000, USB_PORTSC);
1328
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301329 if (pdata->setup_gpio)
1330 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301331
1332 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001333 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301334 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301335 }
1336}
1337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338static int msm_otg_usbdev_notify(struct notifier_block *self,
1339 unsigned long action, void *priv)
1340{
1341 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001342 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301343 struct usb_device *udev = priv;
1344
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301345 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1346 goto out;
1347
Steve Mucklef132c6c2012-06-06 18:30:57 -07001348 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301349 goto out;
1350 /*
1351 * Interested in devices connected directly to the root hub.
1352 * ACA dock can supply IDEV_CHG irrespective devices connected
1353 * on the accessory port.
1354 */
1355 if (!udev->parent || udev->parent->parent ||
1356 motg->chg_type == USB_ACA_DOCK_CHARGER)
1357 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001358
1359 switch (action) {
1360 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301361 if (aca_enabled())
1362 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001363 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301364 pr_debug("B_CONN set\n");
1365 set_bit(B_CONN, &motg->inputs);
1366 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001367 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301368 /*
1369 * OTG PET: A-device must end session within
1370 * 10 sec after PET enumeration.
1371 */
1372 if (udev->quirks & USB_QUIRK_OTG_PET)
1373 msm_otg_start_timer(motg, TA_TST_MAINT,
1374 A_TST_MAINT);
1375 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301376 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001378 if (udev->actconfig)
1379 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1380 else
1381 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001382 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301383 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301384 break;
1385 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001386 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1387 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301388 pr_debug("B_CONN clear\n");
1389 clear_bit(B_CONN, &motg->inputs);
1390 /*
1391 * OTG PET: A-device must end session after
1392 * PET disconnection if it is enumerated
1393 * with bcdDevice[0] = 1. USB core sets
1394 * bus->otg_vbus_off for us. clear it here.
1395 */
1396 if (udev->bus->otg_vbus_off) {
1397 udev->bus->otg_vbus_off = 0;
1398 set_bit(A_BUS_DROP, &motg->inputs);
1399 }
1400 queue_work(system_nrt_wq, &motg->sm_work);
1401 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402 default:
1403 break;
1404 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301405 if (test_bit(ID_A, &motg->inputs))
1406 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1407 motg->mA_port);
1408out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409 return NOTIFY_OK;
1410}
1411
Mayank Ranae3926882011-12-26 09:47:54 +05301412static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1413{
1414 int ret;
1415 static bool vbus_is_on;
1416
1417 if (vbus_is_on == on)
1418 return;
1419
1420 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301421 ret = motg->pdata->vbus_power(on);
1422 if (!ret)
1423 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301424 return;
1425 }
1426
1427 if (!vbus_otg) {
1428 pr_err("vbus_otg is NULL.");
1429 return;
1430 }
1431
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001432 /*
1433 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001434 * from usb before turning on the boost.
1435 * if exiting host mode disable the boost before enabling to draw
1436 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001437 */
Mayank Ranae3926882011-12-26 09:47:54 +05301438 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001439 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301440 ret = regulator_enable(vbus_otg);
1441 if (ret) {
1442 pr_err("unable to enable vbus_otg\n");
1443 return;
1444 }
1445 vbus_is_on = true;
1446 } else {
1447 ret = regulator_disable(vbus_otg);
1448 if (ret) {
1449 pr_err("unable to disable vbus_otg\n");
1450 return;
1451 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001452 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301453 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301454 }
1455}
1456
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001457static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301458{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001459 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301460 struct usb_hcd *hcd;
1461
1462 /*
1463 * Fail host registration if this board can support
1464 * only peripheral configuration.
1465 */
1466 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001467 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301468 return -ENODEV;
1469 }
1470
Mayank Ranae3926882011-12-26 09:47:54 +05301471 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001472 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301473 if (IS_ERR(vbus_otg)) {
1474 pr_err("Unable to get vbus_otg\n");
1475 return -ENODEV;
1476 }
1477 }
1478
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301479 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001480 if (otg->phy->state == OTG_STATE_A_HOST) {
1481 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001482 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301483 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301484 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301485 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001486 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301487 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301488 } else {
1489 otg->host = NULL;
1490 }
1491
1492 return 0;
1493 }
1494
1495 hcd = bus_to_hcd(host);
1496 hcd->power_budget = motg->pdata->power_budget;
1497
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301498#ifdef CONFIG_USB_OTG
1499 host->otg_port = 1;
1500#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001501 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1502 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301503 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001504 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301505
1506 /*
1507 * Kick the state machine work, if peripheral is not supported
1508 * or peripheral is already registered with us.
1509 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301510 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001511 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301512 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301513 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301514
1515 return 0;
1516}
1517
Steve Mucklef132c6c2012-06-06 18:30:57 -07001518static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301519{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301520 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001521 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301522 struct msm_otg_platform_data *pdata = motg->pdata;
1523
1524 if (!otg->gadget)
1525 return;
1526
1527 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001528 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301529 /*
1530 * Some boards have a switch cotrolled by gpio
1531 * to enable/disable internal HUB. Disable internal
1532 * HUB before kicking the gadget.
1533 */
1534 if (pdata->setup_gpio)
1535 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001536
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301537 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301538 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301539 ret = msm_bus_scale_client_update_request(
1540 motg->bus_perf_client, 1);
1541 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001542 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301543 "bus bandwidth %d\n", __func__, ret);
1544 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301545 usb_gadget_vbus_connect(otg->gadget);
1546 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001547 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301548 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301549 /* Configure BUS performance parameters to default */
1550 if (motg->bus_perf_client) {
1551 ret = msm_bus_scale_client_update_request(
1552 motg->bus_perf_client, 0);
1553 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001554 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301555 "for bus bw %d\n", __func__, ret);
1556 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301557 if (pdata->setup_gpio)
1558 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1559 }
1560
1561}
1562
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001563static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301564 struct usb_gadget *gadget)
1565{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001566 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301567
1568 /*
1569 * Fail peripheral registration if this board can support
1570 * only host configuration.
1571 */
1572 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001573 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301574 return -ENODEV;
1575 }
1576
1577 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001578 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1579 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301580 msm_otg_start_peripheral(otg, 0);
1581 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001582 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301583 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301584 } else {
1585 otg->gadget = NULL;
1586 }
1587
1588 return 0;
1589 }
1590 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001591 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301592
1593 /*
1594 * Kick the state machine work, if host is not supported
1595 * or host is already registered with us.
1596 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301597 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001598 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301599 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301600 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301601
1602 return 0;
1603}
1604
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301605static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1606{
1607 unsigned long flags;
1608 int id;
1609
1610 if (!motg->pdata->pmic_id_irq)
1611 return -ENODEV;
1612
1613 local_irq_save(flags);
1614 id = irq_read_line(motg->pdata->pmic_id_irq);
1615 local_irq_restore(flags);
1616
1617 /*
1618 * If we can not read ID line state for some reason, treat
1619 * it as float. This would prevent MHL discovery and kicking
1620 * host mode unnecessarily.
1621 */
1622 return !!id;
1623}
1624
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301625static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1626 void (*callback)(int on))
1627{
1628 struct usb_phy *phy = &motg->phy;
1629 int ret;
1630
Manoj Raoa7bddd12012-08-27 20:36:45 -07001631 if (!motg->pdata->mhl_enable) {
1632 dev_dbg(phy->dev, "MHL feature not enabled\n");
1633 return -ENODEV;
1634 }
1635
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301636 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1637 !motg->pdata->pmic_id_irq) {
1638 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1639 return -ENODEV;
1640 }
1641
1642 if (!motg->pdata->mhl_dev_name) {
1643 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1644 return -ENODEV;
1645 }
1646
1647 if (callback)
1648 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1649 callback);
1650 else
1651 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1652
1653 if (ret)
1654 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1655 motg->pdata->mhl_dev_name, ret);
1656 else
1657 motg->mhl_enabled = true;
1658
1659 return ret;
1660}
1661
1662static void msm_otg_mhl_notify_online(int on)
1663{
1664 struct msm_otg *motg = the_msm_otg;
1665 struct usb_phy *phy = &motg->phy;
1666 bool queue = false;
1667
1668 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1669
1670 if (on) {
1671 set_bit(MHL, &motg->inputs);
1672 } else {
1673 clear_bit(MHL, &motg->inputs);
1674 queue = true;
1675 }
1676
1677 if (queue && phy->state != OTG_STATE_UNDEFINED)
1678 schedule_work(&motg->sm_work);
1679}
1680
1681static bool msm_otg_is_mhl(struct msm_otg *motg)
1682{
1683 struct usb_phy *phy = &motg->phy;
1684 int is_mhl, ret;
1685
1686 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1687 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1688 /*
1689 * MHL driver calls our callback saying that MHL connected
1690 * if RID_GND is detected. But at later part of discovery
1691 * it may figure out MHL is not connected and returns
1692 * false. Hence clear MHL input here.
1693 */
1694 clear_bit(MHL, &motg->inputs);
1695 dev_dbg(phy->dev, "MHL device not found\n");
1696 return false;
1697 }
1698
1699 set_bit(MHL, &motg->inputs);
1700 dev_dbg(phy->dev, "MHL device found\n");
1701 return true;
1702}
1703
1704static bool msm_chg_mhl_detect(struct msm_otg *motg)
1705{
1706 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301707
1708 if (!motg->mhl_enabled)
1709 return false;
1710
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301711 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301712
1713 if (id)
1714 return false;
1715
1716 mhl_det_in_progress = true;
1717 ret = msm_otg_is_mhl(motg);
1718 mhl_det_in_progress = false;
1719
1720 return ret;
1721}
1722
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301723static void msm_otg_chg_check_timer_func(unsigned long data)
1724{
1725 struct msm_otg *motg = (struct msm_otg *) data;
1726 struct usb_otg *otg = motg->phy.otg;
1727
1728 if (atomic_read(&motg->in_lpm) ||
1729 !test_bit(B_SESS_VLD, &motg->inputs) ||
1730 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1731 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1732 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1733 return;
1734 }
1735
1736 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1737 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1738 set_bit(B_FALSE_SDP, &motg->inputs);
1739 queue_work(system_nrt_wq, &motg->sm_work);
1740 }
1741}
1742
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001743static bool msm_chg_aca_detect(struct msm_otg *motg)
1744{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001745 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001746 u32 int_sts;
1747 bool ret = false;
1748
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301749 if (!aca_enabled())
1750 goto out;
1751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001752 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1753 goto out;
1754
Steve Mucklef132c6c2012-06-06 18:30:57 -07001755 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001756 switch (int_sts & 0x1C) {
1757 case 0x08:
1758 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001759 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001760 motg->chg_type = USB_ACA_A_CHARGER;
1761 motg->chg_state = USB_CHG_STATE_DETECTED;
1762 clear_bit(ID_B, &motg->inputs);
1763 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301764 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001765 ret = true;
1766 }
1767 break;
1768 case 0x0C:
1769 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001770 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001771 motg->chg_type = USB_ACA_B_CHARGER;
1772 motg->chg_state = USB_CHG_STATE_DETECTED;
1773 clear_bit(ID_A, &motg->inputs);
1774 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301775 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776 ret = true;
1777 }
1778 break;
1779 case 0x10:
1780 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001781 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001782 motg->chg_type = USB_ACA_C_CHARGER;
1783 motg->chg_state = USB_CHG_STATE_DETECTED;
1784 clear_bit(ID_A, &motg->inputs);
1785 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301786 set_bit(ID, &motg->inputs);
1787 ret = true;
1788 }
1789 break;
1790 case 0x04:
1791 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001792 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301793 motg->chg_type = USB_INVALID_CHARGER;
1794 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1795 clear_bit(ID_A, &motg->inputs);
1796 clear_bit(ID_B, &motg->inputs);
1797 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001798 ret = true;
1799 }
1800 break;
1801 default:
1802 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1803 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301804 test_and_clear_bit(ID_C, &motg->inputs) |
1805 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001806 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001807 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001808 motg->chg_type = USB_INVALID_CHARGER;
1809 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1810 }
1811 }
1812out:
1813 return ret;
1814}
1815
1816static void msm_chg_enable_aca_det(struct msm_otg *motg)
1817{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001818 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001819
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301820 if (!aca_enabled())
1821 return;
1822
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823 switch (motg->pdata->phy_type) {
1824 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301825 /* Disable ID_GND in link and PHY */
1826 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1827 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001828 ulpi_write(phy, 0x01, 0x0C);
1829 ulpi_write(phy, 0x10, 0x0F);
1830 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301831 /* Disable PMIC ID pull-up */
1832 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301833 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001834 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301835 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001836 break;
1837 default:
1838 break;
1839 }
1840}
1841
1842static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1843{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001844 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001845
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301846 if (!aca_enabled())
1847 return;
1848
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 switch (motg->pdata->phy_type) {
1850 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301851 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001852 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301853 break;
1854 default:
1855 break;
1856 }
1857}
1858
1859static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1860{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001861 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301862
1863 if (!aca_enabled())
1864 return;
1865
1866 switch (motg->pdata->phy_type) {
1867 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001868 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001869 break;
1870 default:
1871 break;
1872 }
1873}
1874
1875static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1876{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001877 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001878 bool ret = false;
1879
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301880 if (!aca_enabled())
1881 return ret;
1882
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001883 switch (motg->pdata->phy_type) {
1884 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001885 if (ulpi_read(phy, 0x91) & 1) {
1886 dev_dbg(phy->dev, "RID change\n");
1887 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001888 ret = msm_chg_aca_detect(motg);
1889 }
1890 default:
1891 break;
1892 }
1893 return ret;
1894}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301895
1896static void msm_otg_id_timer_func(unsigned long data)
1897{
1898 struct msm_otg *motg = (struct msm_otg *) data;
1899
1900 if (!aca_enabled())
1901 return;
1902
1903 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001904 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301905 return;
1906 }
1907
Steve Mucklef132c6c2012-06-06 18:30:57 -07001908 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301909 goto out;
1910
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301911 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001912 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301913 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301914 }
1915
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301916out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301917 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1918 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1919}
1920
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301921static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1922{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001923 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301924 u32 chg_det;
1925 bool ret = false;
1926
1927 switch (motg->pdata->phy_type) {
1928 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001929 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301930 ret = chg_det & (1 << 4);
1931 break;
1932 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001933 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301934 ret = chg_det & 1;
1935 break;
1936 default:
1937 break;
1938 }
1939 return ret;
1940}
1941
1942static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1943{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001944 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301945 u32 chg_det;
1946
1947 switch (motg->pdata->phy_type) {
1948 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001949 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301950 /* Turn off charger block */
1951 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001952 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301953 udelay(20);
1954 /* control chg block via ULPI */
1955 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001956 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301957 /* put it in host mode for enabling D- source */
1958 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001959 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301960 /* Turn on chg detect block */
1961 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001962 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301963 udelay(20);
1964 /* enable chg detection */
1965 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001966 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301967 break;
1968 case SNPS_28NM_INTEGRATED_PHY:
1969 /*
1970 * Configure DM as current source, DP as current sink
1971 * and enable battery charging comparators.
1972 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001973 ulpi_write(phy, 0x8, 0x85);
1974 ulpi_write(phy, 0x2, 0x85);
1975 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301976 break;
1977 default:
1978 break;
1979 }
1980}
1981
1982static bool msm_chg_check_primary_det(struct msm_otg *motg)
1983{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001984 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301985 u32 chg_det;
1986 bool ret = false;
1987
1988 switch (motg->pdata->phy_type) {
1989 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001990 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301991 ret = chg_det & (1 << 4);
1992 break;
1993 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001994 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301995 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301996 /* Turn off VDP_SRC */
1997 ulpi_write(phy, 0x3, 0x86);
1998 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301999 break;
2000 default:
2001 break;
2002 }
2003 return ret;
2004}
2005
2006static void msm_chg_enable_primary_det(struct msm_otg *motg)
2007{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002008 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302009 u32 chg_det;
2010
2011 switch (motg->pdata->phy_type) {
2012 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002013 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302014 /* enable chg detection */
2015 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002016 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302017 break;
2018 case SNPS_28NM_INTEGRATED_PHY:
2019 /*
2020 * Configure DP as current source, DM as current sink
2021 * and enable battery charging comparators.
2022 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002023 ulpi_write(phy, 0x2, 0x85);
2024 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302025 break;
2026 default:
2027 break;
2028 }
2029}
2030
2031static bool msm_chg_check_dcd(struct msm_otg *motg)
2032{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002033 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302034 u32 line_state;
2035 bool ret = false;
2036
2037 switch (motg->pdata->phy_type) {
2038 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002039 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302040 ret = !(line_state & 1);
2041 break;
2042 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002043 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302044 ret = line_state & 2;
2045 break;
2046 default:
2047 break;
2048 }
2049 return ret;
2050}
2051
2052static void msm_chg_disable_dcd(struct msm_otg *motg)
2053{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002054 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302055 u32 chg_det;
2056
2057 switch (motg->pdata->phy_type) {
2058 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002059 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302060 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002061 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302062 break;
2063 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002064 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302065 break;
2066 default:
2067 break;
2068 }
2069}
2070
2071static void msm_chg_enable_dcd(struct msm_otg *motg)
2072{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002073 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302074 u32 chg_det;
2075
2076 switch (motg->pdata->phy_type) {
2077 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002078 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302079 /* Turn on D+ current source */
2080 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002081 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302082 break;
2083 case SNPS_28NM_INTEGRATED_PHY:
2084 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002085 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302086 break;
2087 default:
2088 break;
2089 }
2090}
2091
2092static void msm_chg_block_on(struct msm_otg *motg)
2093{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002094 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302095 u32 func_ctrl, chg_det;
2096
2097 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002098 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302099 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2100 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002101 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302102
2103 switch (motg->pdata->phy_type) {
2104 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002105 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302106 /* control chg block via ULPI */
2107 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002108 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302109 /* Turn on chg detect block */
2110 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002111 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302112 udelay(20);
2113 break;
2114 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302115 /* disable DP and DM pull down resistors */
2116 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302117 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002118 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302119 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002120 ulpi_write(phy, 0x1F, 0x92);
2121 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302122 udelay(100);
2123 break;
2124 default:
2125 break;
2126 }
2127}
2128
2129static void msm_chg_block_off(struct msm_otg *motg)
2130{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002131 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302132 u32 func_ctrl, chg_det;
2133
2134 switch (motg->pdata->phy_type) {
2135 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002136 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302137 /* Turn off charger block */
2138 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002139 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302140 break;
2141 case SNPS_28NM_INTEGRATED_PHY:
2142 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002143 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302144 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002145 ulpi_write(phy, 0x1F, 0x92);
2146 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302147 break;
2148 default:
2149 break;
2150 }
2151
2152 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002153 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302154 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2155 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002156 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302157}
2158
Anji jonnalad270e2d2011-08-09 11:28:32 +05302159static const char *chg_to_string(enum usb_chg_type chg_type)
2160{
2161 switch (chg_type) {
2162 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2163 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2164 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2165 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2166 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2167 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2168 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302169 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302170 default: return "INVALID_CHARGER";
2171 }
2172}
2173
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302174#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2175#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302176#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2177#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302178static void msm_chg_detect_work(struct work_struct *w)
2179{
2180 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002181 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302182 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302183 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302184 unsigned long delay;
2185
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002186 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302187
2188 if (test_bit(MHL, &motg->inputs)) {
2189 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2190 return;
2191 }
2192
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302193 switch (motg->chg_state) {
2194 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302195 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302196 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302198 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302199 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302200 delay = MSM_CHG_DCD_POLL_TIME;
2201 break;
2202 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302203 if (msm_chg_mhl_detect(motg)) {
2204 msm_chg_block_off(motg);
2205 motg->chg_state = USB_CHG_STATE_DETECTED;
2206 motg->chg_type = USB_INVALID_CHARGER;
2207 queue_work(system_nrt_wq, &motg->sm_work);
2208 return;
2209 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002210 is_aca = msm_chg_aca_detect(motg);
2211 if (is_aca) {
2212 /*
2213 * ID_A can be ACA dock too. continue
2214 * primary detection after DCD.
2215 */
2216 if (test_bit(ID_A, &motg->inputs)) {
2217 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2218 } else {
2219 delay = 0;
2220 break;
2221 }
2222 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302223 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302224 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2225 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302226 if (is_dcd || tmout) {
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302227 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302228 msm_chg_enable_primary_det(motg);
2229 delay = MSM_CHG_PRIMARY_DET_TIME;
2230 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2231 } else {
2232 delay = MSM_CHG_DCD_POLL_TIME;
2233 }
2234 break;
2235 case USB_CHG_STATE_DCD_DONE:
2236 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302237 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2238 dm_vlgc = line_state & PORTSC_LS_DM;
2239 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302240 if (test_bit(ID_A, &motg->inputs)) {
2241 motg->chg_type = USB_ACA_DOCK_CHARGER;
2242 motg->chg_state = USB_CHG_STATE_DETECTED;
2243 delay = 0;
2244 break;
2245 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302246 if (line_state) { /* DP > VLGC */
2247 motg->chg_type = USB_PROPRIETARY_CHARGER;
2248 motg->chg_state = USB_CHG_STATE_DETECTED;
2249 delay = 0;
2250 } else {
2251 msm_chg_enable_secondary_det(motg);
2252 delay = MSM_CHG_SECONDARY_DET_TIME;
2253 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2254 }
2255 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302256 if (test_bit(ID_A, &motg->inputs)) {
2257 motg->chg_type = USB_ACA_A_CHARGER;
2258 motg->chg_state = USB_CHG_STATE_DETECTED;
2259 delay = 0;
2260 break;
2261 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302262
2263 if (line_state) /* DP > VLGC or/and DM > VLGC */
2264 motg->chg_type = USB_PROPRIETARY_CHARGER;
2265 else
2266 motg->chg_type = USB_SDP_CHARGER;
2267
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302268 motg->chg_state = USB_CHG_STATE_DETECTED;
2269 delay = 0;
2270 }
2271 break;
2272 case USB_CHG_STATE_PRIMARY_DONE:
2273 vout = msm_chg_check_secondary_det(motg);
2274 if (vout)
2275 motg->chg_type = USB_DCP_CHARGER;
2276 else
2277 motg->chg_type = USB_CDP_CHARGER;
2278 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2279 /* fall through */
2280 case USB_CHG_STATE_SECONDARY_DONE:
2281 motg->chg_state = USB_CHG_STATE_DETECTED;
2282 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302283 /*
2284 * Notify the charger type to power supply
2285 * owner as soon as we determine the charger.
2286 */
2287 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302288 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002289 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302290 /*
2291 * Spurious interrupt is seen after enabling ACA detection
2292 * due to which charger detection fails in case of PET.
2293 * Add delay of 100 microsec to avoid that.
2294 */
2295 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002296 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002297 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302298 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302299 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302300 return;
2301 default:
2302 return;
2303 }
2304
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302305 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302306}
2307
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302308/*
2309 * We support OTG, Peripheral only and Host only configurations. In case
2310 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2311 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2312 * enabled when switch is controlled by user and default mode is supplied
2313 * by board file, which can be changed by userspace later.
2314 */
2315static void msm_otg_init_sm(struct msm_otg *motg)
2316{
2317 struct msm_otg_platform_data *pdata = motg->pdata;
2318 u32 otgsc = readl(USB_OTGSC);
2319
2320 switch (pdata->mode) {
2321 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002322 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302323 if (pdata->default_mode == USB_HOST) {
2324 clear_bit(ID, &motg->inputs);
2325 } else if (pdata->default_mode == USB_PERIPHERAL) {
2326 set_bit(ID, &motg->inputs);
2327 set_bit(B_SESS_VLD, &motg->inputs);
2328 } else {
2329 set_bit(ID, &motg->inputs);
2330 clear_bit(B_SESS_VLD, &motg->inputs);
2331 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302332 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302333 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302334 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302335 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302336 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302337 set_bit(A_BUS_REQ, &motg->inputs);
2338 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002339 if (otgsc & OTGSC_BSV)
2340 set_bit(B_SESS_VLD, &motg->inputs);
2341 else
2342 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302343 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302344 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302345 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302346 set_bit(ID, &motg->inputs);
2347 else
2348 clear_bit(ID, &motg->inputs);
2349 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302350 /*
2351 * VBUS initial state is reported after PMIC
2352 * driver initialization. Wait for it.
2353 */
2354 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302355 }
2356 break;
2357 case USB_HOST:
2358 clear_bit(ID, &motg->inputs);
2359 break;
2360 case USB_PERIPHERAL:
2361 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302362 if (pdata->otg_control == OTG_PHY_CONTROL) {
2363 if (otgsc & OTGSC_BSV)
2364 set_bit(B_SESS_VLD, &motg->inputs);
2365 else
2366 clear_bit(B_SESS_VLD, &motg->inputs);
2367 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2368 /*
2369 * VBUS initial state is reported after PMIC
2370 * driver initialization. Wait for it.
2371 */
2372 wait_for_completion(&pmic_vbus_init);
2373 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302374 break;
2375 default:
2376 break;
2377 }
2378}
2379
2380static void msm_otg_sm_work(struct work_struct *w)
2381{
2382 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002383 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302384 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302385
Steve Mucklef132c6c2012-06-06 18:30:57 -07002386 pm_runtime_resume(otg->phy->dev);
2387 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002388 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302389 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002390 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302391 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002392 if (!psy && legacy_power_supply) {
2393 psy = power_supply_get_by_name("usb");
2394
2395 if (!psy)
2396 pr_err("couldn't get usb power supply\n");
2397 }
2398
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002399 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302400 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2401 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002402 pm_runtime_put_noidle(otg->phy->dev);
2403 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302404 break;
2405 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302406 /* FALL THROUGH */
2407 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302408 if (test_bit(MHL, &motg->inputs)) {
2409 /* allow LPM */
2410 pm_runtime_put_noidle(otg->phy->dev);
2411 pm_runtime_suspend(otg->phy->dev);
2412 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302414 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302415 if (msm_chg_mhl_detect(motg)) {
2416 work = 1;
2417 break;
2418 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302419 clear_bit(B_BUS_REQ, &motg->inputs);
2420 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002421 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302422 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302423 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302424 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302425 switch (motg->chg_state) {
2426 case USB_CHG_STATE_UNDEFINED:
2427 msm_chg_detect_work(&motg->chg_work.work);
2428 break;
2429 case USB_CHG_STATE_DETECTED:
2430 switch (motg->chg_type) {
2431 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302432 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002433 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302434 /* fall through */
2435 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302436 msm_otg_notify_charger(motg,
2437 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002438 pm_runtime_put_noidle(otg->phy->dev);
2439 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302440 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302441 case USB_ACA_B_CHARGER:
2442 msm_otg_notify_charger(motg,
2443 IDEV_ACA_CHG_MAX);
2444 /*
2445 * (ID_B --> ID_C) PHY_ALT interrupt can
2446 * not be detected in LPM.
2447 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302448 break;
2449 case USB_CDP_CHARGER:
2450 msm_otg_notify_charger(motg,
2451 IDEV_CHG_MAX);
2452 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002453 otg->phy->state =
2454 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302455 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302456 case USB_ACA_C_CHARGER:
2457 msm_otg_notify_charger(motg,
2458 IDEV_ACA_CHG_MAX);
2459 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002460 otg->phy->state =
2461 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302462 break;
2463 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302464 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002465 otg->phy->state =
2466 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302467 mod_timer(&motg->chg_check_timer,
2468 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302469 break;
2470 default:
2471 break;
2472 }
2473 break;
2474 default:
2475 break;
2476 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302477 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2478 pr_debug("b_sess_end && b_bus_req\n");
2479 if (msm_otg_start_srp(otg) < 0) {
2480 clear_bit(B_BUS_REQ, &motg->inputs);
2481 work = 1;
2482 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302483 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002484 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302485 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2486 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302487 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302488 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302489 del_timer_sync(&motg->chg_check_timer);
2490 clear_bit(B_FALSE_SDP, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302491 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302492 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2493 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302494 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002495 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302496 /*
2497 * There is a small window where ID interrupt
2498 * is not monitored during ID detection circuit
2499 * switch from ACA to PMIC. Check ID state
2500 * before entering into low power mode.
2501 */
2502 if (!msm_otg_read_pmic_id_state(motg)) {
2503 pr_debug("process missed ID intr\n");
2504 clear_bit(ID, &motg->inputs);
2505 work = 1;
2506 break;
2507 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002508 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002509 /*
2510 * Only if autosuspend was enabled in probe, it will be
2511 * used here. Otherwise, no delay will be used.
2512 */
2513 pm_runtime_mark_last_busy(otg->phy->dev);
2514 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302515 }
2516 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302517 case OTG_STATE_B_SRP_INIT:
2518 if (!test_bit(ID, &motg->inputs) ||
2519 test_bit(ID_A, &motg->inputs) ||
2520 test_bit(ID_C, &motg->inputs) ||
2521 (test_bit(B_SESS_VLD, &motg->inputs) &&
2522 !test_bit(ID_B, &motg->inputs))) {
2523 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2524 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002525 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302526 /*
2527 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2528 * bits after SRP initiation.
2529 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002530 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302531 work = 1;
2532 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2533 pr_debug("b_srp_fail\n");
2534 pr_info("A-device did not respond to SRP\n");
2535 clear_bit(B_BUS_REQ, &motg->inputs);
2536 clear_bit(B_SRP_FAIL, &motg->tmouts);
2537 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002538 ulpi_write(otg->phy, 0x0, 0x98);
2539 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302540 motg->b_last_se0_sess = jiffies;
2541 work = 1;
2542 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302543 break;
2544 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302545 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2546 test_bit(B_FALSE_SDP, &motg->inputs)) {
2547 pr_debug("B_FALSE_SDP\n");
2548 msm_otg_start_peripheral(otg, 0);
2549 motg->chg_type = USB_DCP_CHARGER;
2550 clear_bit(B_FALSE_SDP, &motg->inputs);
2551 otg->phy->state = OTG_STATE_B_IDLE;
2552 work = 1;
2553 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302554 test_bit(ID_A, &motg->inputs) ||
2555 test_bit(ID_B, &motg->inputs) ||
2556 !test_bit(B_SESS_VLD, &motg->inputs)) {
2557 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302558 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2559 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302560 msm_otg_notify_charger(motg, 0);
2561 srp_reqd = otg->gadget->otg_srp_reqd;
2562 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302563 if (test_bit(ID_B, &motg->inputs))
2564 clear_bit(ID_B, &motg->inputs);
2565 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002566 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302567 motg->b_last_se0_sess = jiffies;
2568 if (srp_reqd)
2569 msm_otg_start_timer(motg,
2570 TB_TST_SRP, B_TST_SRP);
2571 else
2572 work = 1;
2573 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2574 otg->gadget->b_hnp_enable &&
2575 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2576 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2577 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2578 /* D+ pullup should not be disconnected within 4msec
2579 * after A device suspends the bus. Otherwise PET will
2580 * fail the compliance test.
2581 */
2582 udelay(1000);
2583 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002584 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302585 /*
2586 * start HCD even before A-device enable
2587 * pull-up to meet HNP timings.
2588 */
2589 otg->host->is_b_host = 1;
2590 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002591 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2592 test_bit(B_SESS_VLD, &motg->inputs)) {
2593 pr_debug("a_bus_suspend && b_sess_vld\n");
2594 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002595 pm_runtime_put_noidle(otg->phy->dev);
2596 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002597 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002598 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302599 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600 }
2601 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302602 case OTG_STATE_B_WAIT_ACON:
2603 if (!test_bit(ID, &motg->inputs) ||
2604 test_bit(ID_A, &motg->inputs) ||
2605 test_bit(ID_B, &motg->inputs) ||
2606 !test_bit(B_SESS_VLD, &motg->inputs)) {
2607 pr_debug("!id || id_a/b || !b_sess_vld\n");
2608 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302609 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302610 * A-device is physically disconnected during
2611 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302612 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302613 msm_otg_start_host(otg, 0);
2614 otg->host->is_b_host = 0;
2615
2616 clear_bit(B_BUS_REQ, &motg->inputs);
2617 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2618 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002619 otg->phy->state = OTG_STATE_B_IDLE;
2620 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302621 work = 1;
2622 } else if (test_bit(A_CONN, &motg->inputs)) {
2623 pr_debug("a_conn\n");
2624 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002625 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302626 /*
2627 * PET disconnects D+ pullup after reset is generated
2628 * by B device in B_HOST role which is not detected by
2629 * B device. As workaorund , start timer of 300msec
2630 * and stop timer if A device is enumerated else clear
2631 * A_CONN.
2632 */
2633 msm_otg_start_timer(motg, TB_TST_CONFIG,
2634 B_TST_CONFIG);
2635 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2636 pr_debug("b_ase0_brst_tmout\n");
2637 pr_info("B HNP fail:No response from A device\n");
2638 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002639 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302640 otg->host->is_b_host = 0;
2641 clear_bit(B_ASE0_BRST, &motg->tmouts);
2642 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2643 clear_bit(B_BUS_REQ, &motg->inputs);
2644 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002645 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302646 work = 1;
2647 } else if (test_bit(ID_C, &motg->inputs)) {
2648 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2649 }
2650 break;
2651 case OTG_STATE_B_HOST:
2652 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2653 !test_bit(A_CONN, &motg->inputs) ||
2654 !test_bit(B_SESS_VLD, &motg->inputs)) {
2655 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2656 clear_bit(A_CONN, &motg->inputs);
2657 clear_bit(B_BUS_REQ, &motg->inputs);
2658 msm_otg_start_host(otg, 0);
2659 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002660 otg->phy->state = OTG_STATE_B_IDLE;
2661 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302662 work = 1;
2663 } else if (test_bit(ID_C, &motg->inputs)) {
2664 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2665 }
2666 break;
2667 case OTG_STATE_A_IDLE:
2668 otg->default_a = 1;
2669 if (test_bit(ID, &motg->inputs) &&
2670 !test_bit(ID_A, &motg->inputs)) {
2671 pr_debug("id && !id_a\n");
2672 otg->default_a = 0;
2673 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002674 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302675 del_timer_sync(&motg->id_timer);
2676 msm_otg_link_reset(motg);
2677 msm_chg_enable_aca_intr(motg);
2678 msm_otg_notify_charger(motg, 0);
2679 work = 1;
2680 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2681 (test_bit(A_SRP_DET, &motg->inputs) ||
2682 test_bit(A_BUS_REQ, &motg->inputs))) {
2683 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2684
2685 clear_bit(A_SRP_DET, &motg->inputs);
2686 /* Disable SRP detection */
2687 writel_relaxed((readl_relaxed(USB_OTGSC) &
2688 ~OTGSC_INTSTS_MASK) &
2689 ~OTGSC_DPIE, USB_OTGSC);
2690
Steve Mucklef132c6c2012-06-06 18:30:57 -07002691 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302692 /* VBUS should not be supplied before end of SRP pulse
2693 * generated by PET, if not complaince test fail.
2694 */
2695 usleep_range(10000, 12000);
2696 /* ACA: ID_A: Stop charging untill enumeration */
2697 if (test_bit(ID_A, &motg->inputs))
2698 msm_otg_notify_charger(motg, 0);
2699 else
2700 msm_hsusb_vbus_power(motg, 1);
2701 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2702 } else {
2703 pr_debug("No session requested\n");
2704 clear_bit(A_BUS_DROP, &motg->inputs);
2705 if (test_bit(ID_A, &motg->inputs)) {
2706 msm_otg_notify_charger(motg,
2707 IDEV_ACA_CHG_MAX);
2708 } else if (!test_bit(ID, &motg->inputs)) {
2709 msm_otg_notify_charger(motg, 0);
2710 /*
2711 * A-device is not providing power on VBUS.
2712 * Enable SRP detection.
2713 */
2714 writel_relaxed(0x13, USB_USBMODE);
2715 writel_relaxed((readl_relaxed(USB_OTGSC) &
2716 ~OTGSC_INTSTS_MASK) |
2717 OTGSC_DPIE, USB_OTGSC);
2718 mb();
2719 }
2720 }
2721 break;
2722 case OTG_STATE_A_WAIT_VRISE:
2723 if ((test_bit(ID, &motg->inputs) &&
2724 !test_bit(ID_A, &motg->inputs)) ||
2725 test_bit(A_BUS_DROP, &motg->inputs) ||
2726 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2727 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2728 clear_bit(A_BUS_REQ, &motg->inputs);
2729 msm_otg_del_timer(motg);
2730 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002731 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302732 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2733 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2734 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002735 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302736 if (TA_WAIT_BCON > 0)
2737 msm_otg_start_timer(motg, TA_WAIT_BCON,
2738 A_WAIT_BCON);
2739 msm_otg_start_host(otg, 1);
2740 msm_chg_enable_aca_det(motg);
2741 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302742 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302743 if (msm_chg_check_aca_intr(motg))
2744 work = 1;
2745 }
2746 break;
2747 case OTG_STATE_A_WAIT_BCON:
2748 if ((test_bit(ID, &motg->inputs) &&
2749 !test_bit(ID_A, &motg->inputs)) ||
2750 test_bit(A_BUS_DROP, &motg->inputs) ||
2751 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2752 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2753 "a_wait_bcon_tmout\n");
2754 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2755 pr_info("Device No Response\n");
2756 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2757 }
2758 msm_otg_del_timer(motg);
2759 clear_bit(A_BUS_REQ, &motg->inputs);
2760 clear_bit(B_CONN, &motg->inputs);
2761 msm_otg_start_host(otg, 0);
2762 /*
2763 * ACA: ID_A with NO accessory, just the A plug is
2764 * attached to ACA: Use IDCHG_MAX for charging
2765 */
2766 if (test_bit(ID_A, &motg->inputs))
2767 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2768 else
2769 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002770 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302771 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2772 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2773 pr_debug("!a_vbus_vld\n");
2774 clear_bit(B_CONN, &motg->inputs);
2775 msm_otg_del_timer(motg);
2776 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002777 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2778 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302779 } else if (test_bit(ID_A, &motg->inputs)) {
2780 msm_hsusb_vbus_power(motg, 0);
2781 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2782 /*
2783 * If TA_WAIT_BCON is infinite, we don;t
2784 * turn off VBUS. Enter low power mode.
2785 */
2786 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002787 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302788 } else if (!test_bit(ID, &motg->inputs)) {
2789 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302790 }
2791 break;
2792 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302793 if ((test_bit(ID, &motg->inputs) &&
2794 !test_bit(ID_A, &motg->inputs)) ||
2795 test_bit(A_BUS_DROP, &motg->inputs)) {
2796 pr_debug("id_a/b/c || a_bus_drop\n");
2797 clear_bit(B_CONN, &motg->inputs);
2798 clear_bit(A_BUS_REQ, &motg->inputs);
2799 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002800 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302801 msm_otg_start_host(otg, 0);
2802 if (!test_bit(ID_A, &motg->inputs))
2803 msm_hsusb_vbus_power(motg, 0);
2804 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2805 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2806 pr_debug("!a_vbus_vld\n");
2807 clear_bit(B_CONN, &motg->inputs);
2808 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002809 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302810 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002811 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302812 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2813 /*
2814 * a_bus_req is de-asserted when root hub is
2815 * suspended or HNP is in progress.
2816 */
2817 pr_debug("!a_bus_req\n");
2818 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002819 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302820 if (otg->host->b_hnp_enable)
2821 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2822 A_AIDL_BDIS);
2823 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002824 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302825 } else if (!test_bit(B_CONN, &motg->inputs)) {
2826 pr_debug("!b_conn\n");
2827 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002828 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302829 if (TA_WAIT_BCON > 0)
2830 msm_otg_start_timer(motg, TA_WAIT_BCON,
2831 A_WAIT_BCON);
2832 if (msm_chg_check_aca_intr(motg))
2833 work = 1;
2834 } else if (test_bit(ID_A, &motg->inputs)) {
2835 msm_otg_del_timer(motg);
2836 msm_hsusb_vbus_power(motg, 0);
2837 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2838 msm_otg_notify_charger(motg,
2839 IDEV_ACA_CHG_MAX);
2840 else
2841 msm_otg_notify_charger(motg,
2842 IDEV_CHG_MIN - motg->mA_port);
2843 } else if (!test_bit(ID, &motg->inputs)) {
2844 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2845 motg->chg_type = USB_INVALID_CHARGER;
2846 msm_otg_notify_charger(motg, 0);
2847 msm_hsusb_vbus_power(motg, 1);
2848 }
2849 break;
2850 case OTG_STATE_A_SUSPEND:
2851 if ((test_bit(ID, &motg->inputs) &&
2852 !test_bit(ID_A, &motg->inputs)) ||
2853 test_bit(A_BUS_DROP, &motg->inputs) ||
2854 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2855 pr_debug("id_a/b/c || a_bus_drop ||"
2856 "a_aidl_bdis_tmout\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_WAIT_VFALL;
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 if (!test_bit(ID_A, &motg->inputs))
2863 msm_hsusb_vbus_power(motg, 0);
2864 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2865 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2866 pr_debug("!a_vbus_vld\n");
2867 msm_otg_del_timer(motg);
2868 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002869 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302870 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002871 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302872 } else if (!test_bit(B_CONN, &motg->inputs) &&
2873 otg->host->b_hnp_enable) {
2874 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002875 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302876 msm_otg_host_hnp_enable(otg, 1);
2877 otg->gadget->is_a_peripheral = 1;
2878 msm_otg_start_peripheral(otg, 1);
2879 } else if (!test_bit(B_CONN, &motg->inputs) &&
2880 !otg->host->b_hnp_enable) {
2881 pr_debug("!b_conn && !b_hnp_enable");
2882 /*
2883 * bus request is dropped during suspend.
2884 * acquire again for next device.
2885 */
2886 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002887 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302888 if (TA_WAIT_BCON > 0)
2889 msm_otg_start_timer(motg, TA_WAIT_BCON,
2890 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002891 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302892 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002893 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302894 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002895 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002896 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302897 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302898 }
2899 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302900 case OTG_STATE_A_PERIPHERAL:
2901 if ((test_bit(ID, &motg->inputs) &&
2902 !test_bit(ID_A, &motg->inputs)) ||
2903 test_bit(A_BUS_DROP, &motg->inputs)) {
2904 pr_debug("id _f/b/c || a_bus_drop\n");
2905 /* Clear BIDL_ADIS timer */
2906 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002907 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302908 msm_otg_start_peripheral(otg, 0);
2909 otg->gadget->is_a_peripheral = 0;
2910 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002911 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302912 if (!test_bit(ID_A, &motg->inputs))
2913 msm_hsusb_vbus_power(motg, 0);
2914 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2915 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2916 pr_debug("!a_vbus_vld\n");
2917 /* Clear BIDL_ADIS timer */
2918 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002919 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302920 msm_otg_start_peripheral(otg, 0);
2921 otg->gadget->is_a_peripheral = 0;
2922 msm_otg_start_host(otg, 0);
2923 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2924 pr_debug("a_bidl_adis_tmout\n");
2925 msm_otg_start_peripheral(otg, 0);
2926 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002927 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302928 set_bit(A_BUS_REQ, &motg->inputs);
2929 msm_otg_host_hnp_enable(otg, 0);
2930 if (TA_WAIT_BCON > 0)
2931 msm_otg_start_timer(motg, TA_WAIT_BCON,
2932 A_WAIT_BCON);
2933 } else if (test_bit(ID_A, &motg->inputs)) {
2934 msm_hsusb_vbus_power(motg, 0);
2935 msm_otg_notify_charger(motg,
2936 IDEV_CHG_MIN - motg->mA_port);
2937 } else if (!test_bit(ID, &motg->inputs)) {
2938 msm_otg_notify_charger(motg, 0);
2939 msm_hsusb_vbus_power(motg, 1);
2940 }
2941 break;
2942 case OTG_STATE_A_WAIT_VFALL:
2943 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2944 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002945 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302946 work = 1;
2947 }
2948 break;
2949 case OTG_STATE_A_VBUS_ERR:
2950 if ((test_bit(ID, &motg->inputs) &&
2951 !test_bit(ID_A, &motg->inputs)) ||
2952 test_bit(A_BUS_DROP, &motg->inputs) ||
2953 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002954 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302955 if (!test_bit(ID_A, &motg->inputs))
2956 msm_hsusb_vbus_power(motg, 0);
2957 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2958 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2959 motg->chg_type = USB_INVALID_CHARGER;
2960 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302961 }
2962 break;
2963 default:
2964 break;
2965 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302966 if (work)
2967 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302968}
2969
Amit Blayd0fe07b2012-09-05 16:42:09 +03002970static void msm_otg_suspend_work(struct work_struct *w)
2971{
2972 struct msm_otg *motg =
2973 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05302974
2975 /* This work is only for device bus suspend */
2976 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
2977 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03002978}
2979
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302980static irqreturn_t msm_otg_irq(int irq, void *data)
2981{
2982 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002983 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302984 u32 otgsc = 0, usbsts, pc;
2985 bool work = 0;
2986 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302987
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302988 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002989 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302990 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002991 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07002992 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002993 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302994 return IRQ_HANDLED;
2995 }
2996
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002997 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302998 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302999
3000 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303001 return IRQ_NONE;
3002
3003 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303004 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303005 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303006 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303007 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303008 pr_debug("Id clear\n");
3009 /*
3010 * Assert a_bus_req to supply power on
3011 * VBUS when Micro/Mini-A cable is connected
3012 * with out user intervention.
3013 */
3014 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303015 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303016 msm_chg_enable_aca_det(motg);
3017 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303018 writel_relaxed(otgsc, USB_OTGSC);
3019 work = 1;
3020 } else if (otgsc & OTGSC_DPIS) {
3021 pr_debug("DPIS detected\n");
3022 writel_relaxed(otgsc, USB_OTGSC);
3023 set_bit(A_SRP_DET, &motg->inputs);
3024 set_bit(A_BUS_REQ, &motg->inputs);
3025 work = 1;
3026 } else if (otgsc & OTGSC_BSVIS) {
3027 writel_relaxed(otgsc, USB_OTGSC);
3028 /*
3029 * BSV interrupt comes when operating as an A-device
3030 * (VBUS on/off).
3031 * But, handle BSV when charger is removed from ACA in ID_A
3032 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003033 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303034 !test_bit(ID_A, &motg->inputs))
3035 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303036 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303037 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303038 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303039 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303040 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303041 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003042 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3043
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303044 msm_chg_check_aca_intr(motg);
3045 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303046 work = 1;
3047 } else if (usbsts & STS_PCI) {
3048 pc = readl_relaxed(USB_PORTSC);
3049 pr_debug("portsc = %x\n", pc);
3050 ret = IRQ_NONE;
3051 /*
3052 * HCD Acks PCI interrupt. We use this to switch
3053 * between different OTG states.
3054 */
3055 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003056 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303057 case OTG_STATE_A_SUSPEND:
3058 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3059 !(pc & PORTSC_CCS)) {
3060 pr_debug("B_CONN clear\n");
3061 clear_bit(B_CONN, &motg->inputs);
3062 msm_otg_del_timer(motg);
3063 }
3064 break;
3065 case OTG_STATE_A_PERIPHERAL:
3066 /*
3067 * A-peripheral observed activity on bus.
3068 * clear A_BIDL_ADIS timer.
3069 */
3070 msm_otg_del_timer(motg);
3071 work = 0;
3072 break;
3073 case OTG_STATE_B_WAIT_ACON:
3074 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3075 pr_debug("A_CONN set\n");
3076 set_bit(A_CONN, &motg->inputs);
3077 /* Clear ASE0_BRST timer */
3078 msm_otg_del_timer(motg);
3079 }
3080 break;
3081 case OTG_STATE_B_HOST:
3082 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3083 pr_debug("A_CONN clear\n");
3084 clear_bit(A_CONN, &motg->inputs);
3085 msm_otg_del_timer(motg);
3086 }
3087 break;
3088 case OTG_STATE_A_WAIT_BCON:
3089 if (TA_WAIT_BCON < 0)
3090 set_bit(A_BUS_REQ, &motg->inputs);
3091 default:
3092 work = 0;
3093 break;
3094 }
3095 } else if (usbsts & STS_URI) {
3096 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003097 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303098 case OTG_STATE_A_PERIPHERAL:
3099 /*
3100 * A-peripheral observed activity on bus.
3101 * clear A_BIDL_ADIS timer.
3102 */
3103 msm_otg_del_timer(motg);
3104 work = 0;
3105 break;
3106 default:
3107 work = 0;
3108 break;
3109 }
3110 } else if (usbsts & STS_SLI) {
3111 ret = IRQ_NONE;
3112 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003113 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303114 case OTG_STATE_B_PERIPHERAL:
3115 if (otg->gadget->b_hnp_enable) {
3116 set_bit(A_BUS_SUSPEND, &motg->inputs);
3117 set_bit(B_BUS_REQ, &motg->inputs);
3118 work = 1;
3119 }
3120 break;
3121 case OTG_STATE_A_PERIPHERAL:
3122 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3123 A_BIDL_ADIS);
3124 break;
3125 default:
3126 break;
3127 }
3128 } else if ((usbsts & PHY_ALT_INT)) {
3129 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3130 if (msm_chg_check_aca_intr(motg))
3131 work = 1;
3132 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303133 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303134 if (work)
3135 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303136
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303137 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003138}
3139
3140static void msm_otg_set_vbus_state(int online)
3141{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303142 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003143 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303144
Chiranjeevi Velempati2e1048b2012-11-08 09:54:33 +05303145 /* Ignore received BSV interrupts, if ID pin is GND */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303146 if (!test_bit(ID, &motg->inputs)) {
3147 /*
3148 * state machine work waits for initial VBUS
3149 * completion in UNDEFINED state. Process
3150 * the initial VBUS event in ID_GND state.
3151 */
3152 if (init)
3153 return;
3154 goto complete;
3155 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003156
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303157 if (online) {
3158 pr_debug("PMIC: BSV set\n");
3159 set_bit(B_SESS_VLD, &motg->inputs);
3160 } else {
3161 pr_debug("PMIC: BSV clear\n");
3162 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303163 }
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303164complete:
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303165 if (!init) {
3166 init = true;
3167 complete(&pmic_vbus_init);
3168 pr_debug("PMIC: BSV init complete\n");
3169 return;
3170 }
3171
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303172 if (test_bit(MHL, &motg->inputs) ||
3173 mhl_det_in_progress) {
3174 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3175 return;
3176 }
3177
Jack Pham5ca279b2012-05-14 18:42:54 -07003178 if (atomic_read(&motg->pm_suspended))
3179 motg->sm_work_pending = true;
3180 else
3181 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003182}
3183
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303184static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003185{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303186 struct msm_otg *motg = container_of(w, struct msm_otg,
3187 pmic_id_status_work.work);
3188 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003189
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303190 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303191 if (!test_and_set_bit(ID, &motg->inputs)) {
3192 pr_debug("PMIC: ID set\n");
3193 work = 1;
3194 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303195 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303196 if (test_and_clear_bit(ID, &motg->inputs)) {
3197 pr_debug("PMIC: ID clear\n");
3198 set_bit(A_BUS_REQ, &motg->inputs);
3199 work = 1;
3200 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303201 }
3202
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303203 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003204 if (atomic_read(&motg->pm_suspended))
3205 motg->sm_work_pending = true;
3206 else
3207 queue_work(system_nrt_wq, &motg->sm_work);
3208 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303209
3210}
3211
3212#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3213static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3214{
3215 struct msm_otg *motg = data;
3216
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303217 if (test_bit(MHL, &motg->inputs) ||
3218 mhl_det_in_progress) {
3219 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3220 return IRQ_HANDLED;
3221 }
3222
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303223 if (!aca_id_turned_on)
3224 /*schedule delayed work for 5msec for ID line state to settle*/
3225 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3226 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003227
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303228 return IRQ_HANDLED;
3229}
3230
3231static int msm_otg_mode_show(struct seq_file *s, void *unused)
3232{
3233 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003234 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303235
Steve Mucklef132c6c2012-06-06 18:30:57 -07003236 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303237 case OTG_STATE_A_HOST:
3238 seq_printf(s, "host\n");
3239 break;
3240 case OTG_STATE_B_PERIPHERAL:
3241 seq_printf(s, "peripheral\n");
3242 break;
3243 default:
3244 seq_printf(s, "none\n");
3245 break;
3246 }
3247
3248 return 0;
3249}
3250
3251static int msm_otg_mode_open(struct inode *inode, struct file *file)
3252{
3253 return single_open(file, msm_otg_mode_show, inode->i_private);
3254}
3255
3256static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3257 size_t count, loff_t *ppos)
3258{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303259 struct seq_file *s = file->private_data;
3260 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303261 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003262 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303263 int status = count;
3264 enum usb_mode_type req_mode;
3265
3266 memset(buf, 0x00, sizeof(buf));
3267
3268 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3269 status = -EFAULT;
3270 goto out;
3271 }
3272
3273 if (!strncmp(buf, "host", 4)) {
3274 req_mode = USB_HOST;
3275 } else if (!strncmp(buf, "peripheral", 10)) {
3276 req_mode = USB_PERIPHERAL;
3277 } else if (!strncmp(buf, "none", 4)) {
3278 req_mode = USB_NONE;
3279 } else {
3280 status = -EINVAL;
3281 goto out;
3282 }
3283
3284 switch (req_mode) {
3285 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003286 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303287 case OTG_STATE_A_HOST:
3288 case OTG_STATE_B_PERIPHERAL:
3289 set_bit(ID, &motg->inputs);
3290 clear_bit(B_SESS_VLD, &motg->inputs);
3291 break;
3292 default:
3293 goto out;
3294 }
3295 break;
3296 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003297 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303298 case OTG_STATE_B_IDLE:
3299 case OTG_STATE_A_HOST:
3300 set_bit(ID, &motg->inputs);
3301 set_bit(B_SESS_VLD, &motg->inputs);
3302 break;
3303 default:
3304 goto out;
3305 }
3306 break;
3307 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003308 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303309 case OTG_STATE_B_IDLE:
3310 case OTG_STATE_B_PERIPHERAL:
3311 clear_bit(ID, &motg->inputs);
3312 break;
3313 default:
3314 goto out;
3315 }
3316 break;
3317 default:
3318 goto out;
3319 }
3320
Steve Mucklef132c6c2012-06-06 18:30:57 -07003321 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303322 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303323out:
3324 return status;
3325}
3326
3327const struct file_operations msm_otg_mode_fops = {
3328 .open = msm_otg_mode_open,
3329 .read = seq_read,
3330 .write = msm_otg_mode_write,
3331 .llseek = seq_lseek,
3332 .release = single_release,
3333};
3334
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303335static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3336{
3337 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003338 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303339
Steve Mucklef132c6c2012-06-06 18:30:57 -07003340 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303341 return 0;
3342}
3343
3344static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3345{
3346 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3347}
3348
3349const struct file_operations msm_otg_state_fops = {
3350 .open = msm_otg_otg_state_open,
3351 .read = seq_read,
3352 .llseek = seq_lseek,
3353 .release = single_release,
3354};
3355
Anji jonnalad270e2d2011-08-09 11:28:32 +05303356static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3357{
3358 struct msm_otg *motg = s->private;
3359
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303360 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303361 return 0;
3362}
3363
3364static int msm_otg_chg_open(struct inode *inode, struct file *file)
3365{
3366 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3367}
3368
3369const struct file_operations msm_otg_chg_fops = {
3370 .open = msm_otg_chg_open,
3371 .read = seq_read,
3372 .llseek = seq_lseek,
3373 .release = single_release,
3374};
3375
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303376static int msm_otg_aca_show(struct seq_file *s, void *unused)
3377{
3378 if (debug_aca_enabled)
3379 seq_printf(s, "enabled\n");
3380 else
3381 seq_printf(s, "disabled\n");
3382
3383 return 0;
3384}
3385
3386static int msm_otg_aca_open(struct inode *inode, struct file *file)
3387{
3388 return single_open(file, msm_otg_aca_show, inode->i_private);
3389}
3390
3391static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3392 size_t count, loff_t *ppos)
3393{
3394 char buf[8];
3395
3396 memset(buf, 0x00, sizeof(buf));
3397
3398 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3399 return -EFAULT;
3400
3401 if (!strncmp(buf, "enable", 6))
3402 debug_aca_enabled = true;
3403 else
3404 debug_aca_enabled = false;
3405
3406 return count;
3407}
3408
3409const struct file_operations msm_otg_aca_fops = {
3410 .open = msm_otg_aca_open,
3411 .read = seq_read,
3412 .write = msm_otg_aca_write,
3413 .llseek = seq_lseek,
3414 .release = single_release,
3415};
3416
Manu Gautam8bdcc592012-03-06 11:26:06 +05303417static int msm_otg_bus_show(struct seq_file *s, void *unused)
3418{
3419 if (debug_bus_voting_enabled)
3420 seq_printf(s, "enabled\n");
3421 else
3422 seq_printf(s, "disabled\n");
3423
3424 return 0;
3425}
3426
3427static int msm_otg_bus_open(struct inode *inode, struct file *file)
3428{
3429 return single_open(file, msm_otg_bus_show, inode->i_private);
3430}
3431
3432static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3433 size_t count, loff_t *ppos)
3434{
3435 char buf[8];
3436 int ret;
3437 struct seq_file *s = file->private_data;
3438 struct msm_otg *motg = s->private;
3439
3440 memset(buf, 0x00, sizeof(buf));
3441
3442 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3443 return -EFAULT;
3444
3445 if (!strncmp(buf, "enable", 6)) {
3446 /* Do not vote here. Let OTG statemachine decide when to vote */
3447 debug_bus_voting_enabled = true;
3448 } else {
3449 debug_bus_voting_enabled = false;
3450 if (motg->bus_perf_client) {
3451 ret = msm_bus_scale_client_update_request(
3452 motg->bus_perf_client, 0);
3453 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003454 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303455 "for bus bw %d\n", __func__, ret);
3456 }
3457 }
3458
3459 return count;
3460}
3461
David Keitel272ce522012-08-17 16:25:24 -07003462static int otg_power_get_property_usb(struct power_supply *psy,
3463 enum power_supply_property psp,
3464 union power_supply_propval *val)
3465{
Jack Pham0c695282012-10-19 18:13:03 -07003466 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003467 switch (psp) {
3468 case POWER_SUPPLY_PROP_SCOPE:
3469 if (motg->host_mode)
3470 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3471 else
3472 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3473 break;
3474 case POWER_SUPPLY_PROP_CURRENT_MAX:
3475 val->intval = motg->current_max;
3476 break;
3477 /* Reflect USB enumeration */
3478 case POWER_SUPPLY_PROP_PRESENT:
3479 case POWER_SUPPLY_PROP_ONLINE:
3480 val->intval = motg->online;
3481 break;
3482 default:
3483 return -EINVAL;
3484 }
3485 return 0;
3486}
3487
3488static int otg_power_set_property_usb(struct power_supply *psy,
3489 enum power_supply_property psp,
3490 const union power_supply_propval *val)
3491{
Jack Pham0c695282012-10-19 18:13:03 -07003492 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003493
3494 switch (psp) {
3495 /* Process PMIC notification in PRESENT prop */
3496 case POWER_SUPPLY_PROP_PRESENT:
3497 msm_otg_set_vbus_state(val->intval);
3498 break;
3499 /* The ONLINE property reflects if usb has enumerated */
3500 case POWER_SUPPLY_PROP_ONLINE:
3501 motg->online = val->intval;
3502 break;
3503 case POWER_SUPPLY_PROP_CURRENT_MAX:
3504 motg->current_max = val->intval;
3505 break;
3506 default:
3507 return -EINVAL;
3508 }
3509
3510 power_supply_changed(&motg->usb_psy);
3511 return 0;
3512}
3513
David Collinsd79acc52012-11-26 14:59:00 -08003514static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3515 enum power_supply_property psp)
3516{
3517 switch (psp) {
3518 case POWER_SUPPLY_PROP_PRESENT:
3519 case POWER_SUPPLY_PROP_ONLINE:
3520 case POWER_SUPPLY_PROP_CURRENT_MAX:
3521 return 1;
3522 default:
3523 break;
3524 }
3525
3526 return 0;
3527}
3528
David Keitel272ce522012-08-17 16:25:24 -07003529static char *otg_pm_power_supplied_to[] = {
3530 "battery",
3531};
3532
3533static enum power_supply_property otg_pm_power_props_usb[] = {
3534 POWER_SUPPLY_PROP_PRESENT,
3535 POWER_SUPPLY_PROP_ONLINE,
3536 POWER_SUPPLY_PROP_CURRENT_MAX,
3537 POWER_SUPPLY_PROP_SCOPE,
3538};
3539
Manu Gautam8bdcc592012-03-06 11:26:06 +05303540const struct file_operations msm_otg_bus_fops = {
3541 .open = msm_otg_bus_open,
3542 .read = seq_read,
3543 .write = msm_otg_bus_write,
3544 .llseek = seq_lseek,
3545 .release = single_release,
3546};
3547
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303548static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303549
3550static int msm_otg_debugfs_init(struct msm_otg *motg)
3551{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303552 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303553
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303554 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3555
3556 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3557 return -ENODEV;
3558
Anji jonnalad270e2d2011-08-09 11:28:32 +05303559 if (motg->pdata->mode == USB_OTG &&
3560 motg->pdata->otg_control == OTG_USER_CONTROL) {
3561
Manu Gautam8bdcc592012-03-06 11:26:06 +05303562 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303563 S_IWUSR, msm_otg_dbg_root, motg,
3564 &msm_otg_mode_fops);
3565
Manu Gautam8bdcc592012-03-06 11:26:06 +05303566 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303567 debugfs_remove(msm_otg_dbg_root);
3568 msm_otg_dbg_root = NULL;
3569 return -ENODEV;
3570 }
3571 }
3572
Manu Gautam8bdcc592012-03-06 11:26:06 +05303573 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303574 msm_otg_dbg_root, motg,
3575 &msm_otg_chg_fops);
3576
Manu Gautam8bdcc592012-03-06 11:26:06 +05303577 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303578 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303579 return -ENODEV;
3580 }
3581
Manu Gautam8bdcc592012-03-06 11:26:06 +05303582 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303583 msm_otg_dbg_root, motg,
3584 &msm_otg_aca_fops);
3585
Manu Gautam8bdcc592012-03-06 11:26:06 +05303586 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303587 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303588 return -ENODEV;
3589 }
3590
Manu Gautam8bdcc592012-03-06 11:26:06 +05303591 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3592 msm_otg_dbg_root, motg,
3593 &msm_otg_bus_fops);
3594
3595 if (!msm_otg_dentry) {
3596 debugfs_remove_recursive(msm_otg_dbg_root);
3597 return -ENODEV;
3598 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303599
3600 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3601 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3602
3603 if (!msm_otg_dentry) {
3604 debugfs_remove_recursive(msm_otg_dbg_root);
3605 return -ENODEV;
3606 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303607 return 0;
3608}
3609
3610static void msm_otg_debugfs_cleanup(void)
3611{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303612 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303613}
3614
Manu Gautam0ddbd922012-09-21 17:17:38 +05303615#define MSM_OTG_CMD_ID 0x09
3616#define MSM_OTG_DEVICE_ID 0x04
3617#define MSM_OTG_VMID_IDX 0xFF
3618#define MSM_OTG_MEM_TYPE 0x02
3619struct msm_otg_scm_cmd_buf {
3620 unsigned int device_id;
3621 unsigned int vmid_idx;
3622 unsigned int mem_type;
3623} __attribute__ ((__packed__));
3624
3625static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3626{
3627 int ret;
3628 struct msm_otg_platform_data *pdata = motg->pdata;
3629 struct msm_otg_scm_cmd_buf cmd_buf;
3630
3631 if (!pdata->pnoc_errata_fix)
3632 return;
3633
3634 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3635
3636 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3637 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3638 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3639
3640 ret = scm_call(SCM_SVC_CP, MSM_OTG_CMD_ID, &cmd_buf,
3641 sizeof(cmd_buf), NULL, 0);
3642
3643 if (ret)
3644 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3645}
3646
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303647static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3648static struct platform_device *msm_otg_add_pdev(
3649 struct platform_device *ofdev, const char *name)
3650{
3651 struct platform_device *pdev;
3652 const struct resource *res = ofdev->resource;
3653 unsigned int num = ofdev->num_resources;
3654 int retval;
3655
3656 pdev = platform_device_alloc(name, -1);
3657 if (!pdev) {
3658 retval = -ENOMEM;
3659 goto error;
3660 }
3661
3662 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3663 pdev->dev.dma_mask = &msm_otg_dma_mask;
3664
3665 if (num) {
3666 retval = platform_device_add_resources(pdev, res, num);
3667 if (retval)
3668 goto error;
3669 }
3670
3671 retval = platform_device_add(pdev);
3672 if (retval)
3673 goto error;
3674
3675 return pdev;
3676
3677error:
3678 platform_device_put(pdev);
3679 return ERR_PTR(retval);
3680}
3681
3682static int msm_otg_setup_devices(struct platform_device *ofdev,
3683 enum usb_mode_type mode, bool init)
3684{
3685 const char *gadget_name = "msm_hsusb";
3686 const char *host_name = "msm_hsusb_host";
3687 static struct platform_device *gadget_pdev;
3688 static struct platform_device *host_pdev;
3689 int retval = 0;
3690
3691 if (!init) {
3692 if (gadget_pdev)
3693 platform_device_unregister(gadget_pdev);
3694 if (host_pdev)
3695 platform_device_unregister(host_pdev);
3696 return 0;
3697 }
3698
3699 switch (mode) {
3700 case USB_OTG:
3701 /* fall through */
3702 case USB_PERIPHERAL:
3703 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3704 if (IS_ERR(gadget_pdev)) {
3705 retval = PTR_ERR(gadget_pdev);
3706 break;
3707 }
3708 if (mode == USB_PERIPHERAL)
3709 break;
3710 /* fall through */
3711 case USB_HOST:
3712 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3713 if (IS_ERR(host_pdev)) {
3714 retval = PTR_ERR(host_pdev);
3715 if (mode == USB_OTG)
3716 platform_device_unregister(gadget_pdev);
3717 }
3718 break;
3719 default:
3720 break;
3721 }
3722
3723 return retval;
3724}
3725
David Keitel272ce522012-08-17 16:25:24 -07003726static int msm_otg_register_power_supply(struct platform_device *pdev,
3727 struct msm_otg *motg)
3728{
3729 int ret;
3730
3731 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3732 if (ret < 0) {
3733 dev_err(motg->phy.dev,
3734 "%s:power_supply_register usb failed\n",
3735 __func__);
3736 return ret;
3737 }
3738
3739 legacy_power_supply = false;
3740 return 0;
3741}
3742
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303743struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3744{
3745 struct device_node *node = pdev->dev.of_node;
3746 struct msm_otg_platform_data *pdata;
3747 int len = 0;
3748
3749 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3750 if (!pdata) {
3751 pr_err("unable to allocate platform data\n");
3752 return NULL;
3753 }
3754 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3755 if (len) {
3756 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3757 if (!pdata->phy_init_seq)
3758 return NULL;
3759 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3760 pdata->phy_init_seq,
3761 len/sizeof(*pdata->phy_init_seq));
3762 }
3763 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3764 &pdata->power_budget);
3765 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3766 &pdata->mode);
3767 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3768 &pdata->otg_control);
3769 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3770 &pdata->default_mode);
3771 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3772 &pdata->phy_type);
3773 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3774 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303775 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3776 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05303777 pdata->pnoc_errata_fix = of_property_read_bool(node,
3778 "qcom,hsusb-otg-pnoc-errata-fix");
Manu Gautambd53fba2012-07-31 16:13:06 +05303779
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303780 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303781}
3782
3783static int __init msm_otg_probe(struct platform_device *pdev)
3784{
Manu Gautamf8c45642012-08-10 10:20:56 -07003785 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303786 struct resource *res;
3787 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003788 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303789 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303790
3791 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303792
3793 if (pdev->dev.of_node) {
3794 dev_dbg(&pdev->dev, "device tree enabled\n");
3795 pdata = msm_otg_dt_to_pdata(pdev);
3796 if (!pdata)
3797 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003798
3799 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3800 if (!pdata->bus_scale_table)
3801 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3802
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303803 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3804 if (ret) {
3805 dev_err(&pdev->dev, "devices setup failed\n");
3806 return ret;
3807 }
3808 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303809 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3810 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303811 } else {
3812 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303813 }
3814
3815 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3816 if (!motg) {
3817 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3818 return -ENOMEM;
3819 }
3820
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003821 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3822 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003823 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3824 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303825 goto free_motg;
3826 }
3827
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003828 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303829 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003830 phy = &motg->phy;
3831 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303832
3833 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303834 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3835 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3836 * PMIC can detect ACA ID_GND and generate an interrupt.
3837 */
3838 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3839 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3840 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003841 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303842 }
3843
Ofir Cohen4da266f2012-01-03 10:19:29 +02003844 /* initialize reset counter */
3845 motg->reset_counter = 0;
3846
Amit Blay02eff132011-09-21 16:46:24 +03003847 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003848 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003849 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003850 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303851
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303852 /*
3853 * Targets on which link uses asynchronous reset methodology,
3854 * free running clock is not required during the reset.
3855 */
Manu Gautam5143b252012-01-05 19:25:23 -08003856 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303857 if (IS_ERR(motg->clk))
3858 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3859 else
3860 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303861
3862 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003863 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303864 * CORE CLK must be running at >55Mhz for correct HSUSB
3865 * operation and USB core cannot tolerate frequency changes on
3866 * CORE CLK. For such USB cores, vote for maximum clk frequency
3867 * on pclk source
3868 */
Manu Gautam5143b252012-01-05 19:25:23 -08003869 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3870 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303871 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003872 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303873 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003874 goto put_clk;
3875 }
3876 clk_set_rate(motg->core_clk, INT_MAX);
3877
3878 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3879 if (IS_ERR(motg->pclk)) {
3880 dev_err(&pdev->dev, "failed to get iface_clk\n");
3881 ret = PTR_ERR(motg->pclk);
3882 goto put_core_clk;
3883 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303884
3885 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3886 if (!res) {
3887 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3888 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003889 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303890 }
3891
3892 motg->regs = ioremap(res->start, resource_size(res));
3893 if (!motg->regs) {
3894 dev_err(&pdev->dev, "ioremap failed\n");
3895 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003896 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303897 }
3898 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3899
3900 motg->irq = platform_get_irq(pdev, 0);
3901 if (!motg->irq) {
3902 dev_err(&pdev->dev, "platform_get_irq failed\n");
3903 ret = -ENODEV;
3904 goto free_regs;
3905 }
3906
Manu Gautamf8c45642012-08-10 10:20:56 -07003907 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3908 if (motg->async_irq < 0) {
3909 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3910 motg->async_irq = 0;
3911 }
3912
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003913 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003914 if (IS_ERR(motg->xo_handle)) {
3915 dev_err(&pdev->dev, "%s not able to get the handle "
3916 "to vote for TCXO D0 buffer\n", __func__);
3917 ret = PTR_ERR(motg->xo_handle);
3918 goto free_regs;
3919 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303920
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003921 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003922 if (ret) {
3923 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3924 "D0 buffer%d\n", __func__, ret);
3925 goto free_xo_handle;
3926 }
3927
Manu Gautam28b1bac2012-01-30 16:43:06 +05303928 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303929
Mayank Rana248698c2012-04-19 00:03:16 +05303930 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003931 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303932 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003933 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303934 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003935 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003936 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303937 goto devote_xo_handle;
3938 }
3939 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303940 }
3941
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003942 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303943 if (ret) {
3944 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303945 goto devote_xo_handle;
3946 }
3947
3948 ret = regulator_enable(hsusb_vddcx);
3949 if (ret) {
3950 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3951 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303952 }
3953
3954 ret = msm_hsusb_ldo_init(motg, 1);
3955 if (ret) {
3956 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303957 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303958 }
3959
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303960 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303961 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3962 "mhl_usb_hs_switch");
3963 if (IS_ERR(mhl_usb_hs_switch)) {
3964 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003965 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303966 goto free_ldo_init;
3967 }
3968 }
3969
Amit Blay81801aa2012-09-19 12:08:12 +02003970 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303971 if (ret) {
3972 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003973 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303974 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303975 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303976
Manu Gautam0ddbd922012-09-21 17:17:38 +05303977 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
3978 msm_otg_pnoc_errata_fix(motg);
3979
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303980 writel(0, USB_USBINTR);
3981 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003982 /* Ensure that above STOREs are completed before enabling interrupts */
3983 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303984
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303985 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3986 if (ret)
3987 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003988 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303989 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303990 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303991 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303992 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003993 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303994 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3995 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05303996 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
3997 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303998 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3999 "msm_otg", motg);
4000 if (ret) {
4001 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004002 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304003 }
4004
Manu Gautamf8c45642012-08-10 10:20:56 -07004005 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304006 ret = request_irq(motg->async_irq, msm_otg_irq,
4007 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004008 if (ret) {
4009 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4010 goto free_irq;
4011 }
4012 disable_irq(motg->async_irq);
4013 }
4014
Jack Pham87f202f2012-08-06 00:24:22 -07004015 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4016 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4017
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004018 phy->init = msm_otg_reset;
4019 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004020 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304021
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004022 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304023
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004024 phy->otg->phy = &motg->phy;
4025 phy->otg->set_host = msm_otg_set_host;
4026 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004027 phy->otg->start_hnp = msm_otg_start_hnp;
4028 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004029
4030 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304031 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004032 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004033 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304034 }
4035
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304036 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304037 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004038 if (motg->pdata->pmic_id_irq) {
4039 ret = request_irq(motg->pdata->pmic_id_irq,
4040 msm_pmic_id_irq,
4041 IRQF_TRIGGER_RISING |
4042 IRQF_TRIGGER_FALLING,
4043 "msm_otg", motg);
4044 if (ret) {
4045 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004046 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004047 }
4048 } else {
4049 ret = -ENODEV;
4050 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004051 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004052 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304053 }
4054
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304055 msm_hsusb_mhl_switch_enable(motg, 1);
4056
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304057 platform_set_drvdata(pdev, motg);
4058 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004059 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304060
Anji jonnalad270e2d2011-08-09 11:28:32 +05304061 ret = msm_otg_debugfs_init(motg);
4062 if (ret)
4063 dev_dbg(&pdev->dev, "mode debugfs file is"
4064 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304065
Amit Blay58b31472011-11-18 09:39:39 +02004066 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4067 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304068 (!(motg->pdata->mode == USB_OTG) ||
4069 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004070 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304071 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004072
Amit Blay58b31472011-11-18 09:39:39 +02004073 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004074 motg->caps = ALLOW_PHY_RETENTION |
4075 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02004076 }
4077
Amit Blay6fa647a2012-05-24 14:12:08 +03004078 if (motg->pdata->enable_lpm_on_dev_suspend)
4079 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4080
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004081 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304082 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004083 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304084
Amit Blayd6f38282012-10-29 13:13:46 +02004085 if (motg->pdata->delay_lpm_on_disconnect) {
4086 pm_runtime_set_autosuspend_delay(&pdev->dev,
4087 lpm_disconnect_thresh);
4088 pm_runtime_use_autosuspend(&pdev->dev);
4089 }
4090
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304091 if (motg->pdata->bus_scale_table) {
4092 motg->bus_perf_client =
4093 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4094 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004095 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304096 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05304097 else
4098 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304099 }
4100
David Keitel272ce522012-08-17 16:25:24 -07004101 motg->usb_psy.name = "usb";
4102 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4103 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4104 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4105 motg->usb_psy.properties = otg_pm_power_props_usb;
4106 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4107 motg->usb_psy.get_property = otg_power_get_property_usb;
4108 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004109 motg->usb_psy.property_is_writeable
4110 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004111
Jack Pham0c695282012-10-19 18:13:03 -07004112 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004113 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004114 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4115 legacy_power_supply = true;
4116 } else {
4117 /* otherwise register our own power supply */
4118 if (!msm_otg_register_power_supply(pdev, motg))
4119 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004120 }
4121
Jack Pham0c695282012-10-19 18:13:03 -07004122 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4123 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4124
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304125 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004126
Steve Mucklef132c6c2012-06-06 18:30:57 -07004127remove_phy:
4128 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004129free_async_irq:
4130 if (motg->async_irq)
4131 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304132free_irq:
4133 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004134destroy_wlock:
4135 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304136 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004137 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004138free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304139 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304140free_hsusb_vddcx:
4141 regulator_disable(hsusb_vddcx);
4142free_config_vddcx:
4143 regulator_set_voltage(hsusb_vddcx,
4144 vdd_val[motg->vdd_type][VDD_NONE],
4145 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004146devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304147 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004148 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004149free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004150 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304151free_regs:
4152 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004153put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304154 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304155put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004156 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304157put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304158 if (!IS_ERR(motg->clk))
4159 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004160 if (!IS_ERR(motg->phy_reset_clk))
4161 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004162free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004163 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304164free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304165 kfree(motg);
4166 return ret;
4167}
4168
4169static int __devexit msm_otg_remove(struct platform_device *pdev)
4170{
4171 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004172 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304173 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304174
4175 if (otg->host || otg->gadget)
4176 return -EBUSY;
4177
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304178 if (pdev->dev.of_node)
4179 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004180 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4181 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304182 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304183 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304184 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304185 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004186 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304187 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304188
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304189 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304190
4191 device_init_wakeup(&pdev->dev, 0);
4192 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004193 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304194
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304195 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004196 if (motg->pdata->pmic_id_irq)
4197 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004198 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304199 free_irq(motg->irq, motg);
4200
Jack Pham87f202f2012-08-06 00:24:22 -07004201 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4202 motg->pdata->mpm_otgsessvld_int)
4203 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4204
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304205 /*
4206 * Put PHY in low power mode.
4207 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07004208 ulpi_read(otg->phy, 0x14);
4209 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304210
4211 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4212 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4213 if (readl(USB_PORTSC) & PORTSC_PHCD)
4214 break;
4215 udelay(1);
4216 cnt++;
4217 }
4218 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004219 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304220
Manu Gautam28b1bac2012-01-30 16:43:06 +05304221 clk_disable_unprepare(motg->pclk);
4222 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004223 msm_xo_put(motg->xo_handle);
Amit Blay81801aa2012-09-19 12:08:12 +02004224 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304225 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304226 regulator_disable(hsusb_vddcx);
4227 regulator_set_voltage(hsusb_vddcx,
4228 vdd_val[motg->vdd_type][VDD_NONE],
4229 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304230
4231 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304232 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304233
Amit Blay02eff132011-09-21 16:46:24 +03004234 if (!IS_ERR(motg->phy_reset_clk))
4235 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304236 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304237 if (!IS_ERR(motg->clk))
4238 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004239 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304240
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304241 if (motg->bus_perf_client)
4242 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304243
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004244 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304245 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304246 return 0;
4247}
4248
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304249#ifdef CONFIG_PM_RUNTIME
4250static int msm_otg_runtime_idle(struct device *dev)
4251{
4252 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004253 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304254
4255 dev_dbg(dev, "OTG runtime idle\n");
4256
Steve Mucklef132c6c2012-06-06 18:30:57 -07004257 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304258 return -EAGAIN;
4259 else
4260 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304261}
4262
4263static int msm_otg_runtime_suspend(struct device *dev)
4264{
4265 struct msm_otg *motg = dev_get_drvdata(dev);
4266
4267 dev_dbg(dev, "OTG runtime suspend\n");
4268 return msm_otg_suspend(motg);
4269}
4270
4271static int msm_otg_runtime_resume(struct device *dev)
4272{
4273 struct msm_otg *motg = dev_get_drvdata(dev);
4274
4275 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304276 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304277 return msm_otg_resume(motg);
4278}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304279#endif
4280
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304281#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304282static int msm_otg_pm_suspend(struct device *dev)
4283{
Jack Pham5ca279b2012-05-14 18:42:54 -07004284 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304285 struct msm_otg *motg = dev_get_drvdata(dev);
4286
4287 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004288
4289 atomic_set(&motg->pm_suspended, 1);
4290 ret = msm_otg_suspend(motg);
4291 if (ret)
4292 atomic_set(&motg->pm_suspended, 0);
4293
4294 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304295}
4296
4297static int msm_otg_pm_resume(struct device *dev)
4298{
Jack Pham5ca279b2012-05-14 18:42:54 -07004299 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304300 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304301
4302 dev_dbg(dev, "OTG PM resume\n");
4303
Jack Pham5ca279b2012-05-14 18:42:54 -07004304 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004305 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004306 pm_runtime_get_noresume(dev);
4307 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304308
Jack Pham5ca279b2012-05-14 18:42:54 -07004309 /* Update runtime PM status */
4310 pm_runtime_disable(dev);
4311 pm_runtime_set_active(dev);
4312 pm_runtime_enable(dev);
4313
Jack Phamc7edb172012-08-13 15:32:39 -07004314 if (motg->sm_work_pending) {
4315 motg->sm_work_pending = false;
4316 queue_work(system_nrt_wq, &motg->sm_work);
4317 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004318 }
4319
4320 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304321}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304322#endif
4323
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304324#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304325static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304326 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4327 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4328 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304329};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304330#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304331
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304332static struct of_device_id msm_otg_dt_match[] = {
4333 { .compatible = "qcom,hsusb-otg",
4334 },
4335 {}
4336};
4337
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304338static struct platform_driver msm_otg_driver = {
4339 .remove = __devexit_p(msm_otg_remove),
4340 .driver = {
4341 .name = DRIVER_NAME,
4342 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304343#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304344 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304345#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304346 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304347 },
4348};
4349
4350static int __init msm_otg_init(void)
4351{
4352 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4353}
4354
4355static void __exit msm_otg_exit(void)
4356{
4357 platform_driver_unregister(&msm_otg_driver);
4358}
4359
4360module_init(msm_otg_init);
4361module_exit(msm_otg_exit);
4362
4363MODULE_LICENSE("GPL v2");
4364MODULE_DESCRIPTION("MSM USB transceiver driver");