blob: c69071df6ed353f8301c3219e7e795bcf1b3b4cc [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)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001011 if (motg->async_irq)
1012 enable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001013 else
1014 enable_irq_wake(motg->irq);
1015
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 if (motg->pdata->pmic_id_irq)
1017 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001018 if (pdata->otg_control == OTG_PHY_CONTROL &&
1019 pdata->mpm_otgsessvld_int)
1020 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301022 if (bus)
1023 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1024
1025 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001026 /* Enable ASYNC IRQ (if present) during LPM */
1027 if (motg->async_irq)
1028 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301029 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001030 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301031
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001032 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301033
1034 return 0;
1035}
1036
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301037static int msm_otg_resume(struct msm_otg *motg)
1038{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001039 struct usb_phy *phy = &motg->phy;
1040 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001041 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301042 int cnt = 0;
1043 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001044 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001045 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301046
1047 if (!atomic_read(&motg->in_lpm))
1048 return 0;
1049
Jack Pham8978b892012-10-17 16:31:39 -07001050 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001051 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301052
Anji jonnala7da3f262011-12-02 17:22:14 -08001053 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301054 if (motg->lpm_flags & XO_SHUTDOWN) {
1055 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1056 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -07001057 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301058 "TCXO D0 buffer%d\n", __func__, ret);
1059 motg->lpm_flags &= ~XO_SHUTDOWN;
1060 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301061
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001062 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001063 clk_prepare_enable(motg->core_clk);
1064 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001065 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001066 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301067
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001068 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001069 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001070 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001071 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1072 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1073 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074 }
1075
1076 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301077 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301078 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001079 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1080 phy_ctrl_val |= PHY_RETEN;
1081 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1082 /* Disable PHY HV interrupts */
1083 phy_ctrl_val &=
1084 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
1085 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301087 }
1088
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301089 temp = readl(USB_USBCMD);
1090 temp &= ~ASYNC_INTR_CTRL;
1091 temp &= ~ULPI_STP_CTRL;
1092 writel(temp, USB_USBCMD);
1093
1094 /*
1095 * PHY comes out of low power mode (LPM) in case of wakeup
1096 * from asynchronous interrupt.
1097 */
1098 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1099 goto skip_phy_resume;
1100
1101 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1102 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1103 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1104 break;
1105 udelay(1);
1106 cnt++;
1107 }
1108
1109 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1110 /*
1111 * This is a fatal error. Reset the link and
1112 * PHY. USB state can not be restored. Re-insertion
1113 * of USB cable is the only way to get USB working.
1114 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001115 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301116 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001117 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301118 }
1119
1120skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001121 if (device_may_wakeup(phy->dev)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001122 if (motg->async_irq)
1123 disable_irq_wake(motg->async_irq);
Jack Phamd110a5a2013-02-21 13:34:48 -08001124 else
1125 disable_irq_wake(motg->irq);
1126
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127 if (motg->pdata->pmic_id_irq)
1128 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001129 if (pdata->otg_control == OTG_PHY_CONTROL &&
1130 pdata->mpm_otgsessvld_int)
1131 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301133 if (bus)
1134 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1135
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301136 atomic_set(&motg->in_lpm, 0);
1137
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301138 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001139 /* Match the disable_irq call from ISR */
1140 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301141 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301142 }
Jack Pham8978b892012-10-17 16:31:39 -07001143 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301144
Manu Gautamf8c45642012-08-10 10:20:56 -07001145 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1146 if (motg->async_irq)
1147 disable_irq(motg->async_irq);
1148
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001149 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301150
1151 return 0;
1152}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301153#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301154
David Keitel272ce522012-08-17 16:25:24 -07001155static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001156{
Jack Pham0c695282012-10-19 18:13:03 -07001157 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001158 pr_err("No USB power supply registered!\n");
1159 return;
1160 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001161
Jack Pham0c695282012-10-19 18:13:03 -07001162 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001163 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301164 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001165 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301166 } else {
David Keitel272ce522012-08-17 16:25:24 -07001167 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301168 /*
1169 * VBUS comparator is disabled by PMIC charging driver
1170 * when SYSTEM scope is selected. For ID_GND->ID_A
1171 * transition, give 50 msec delay so that PMIC charger
1172 * driver detect the VBUS and ready for accepting
1173 * charging current value from USB.
1174 */
1175 if (test_bit(ID_A, &motg->inputs))
1176 msleep(50);
1177 }
David Keitel272ce522012-08-17 16:25:24 -07001178 } else {
1179 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001180 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001181 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001182}
1183
David Keitel081a3e22012-04-18 12:37:07 -07001184static int msm_otg_notify_chg_type(struct msm_otg *motg)
1185{
1186 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001187
David Keitel081a3e22012-04-18 12:37:07 -07001188 /*
1189 * TODO
1190 * Unify OTG driver charger types and power supply charger types
1191 */
1192 if (charger_type == motg->chg_type)
1193 return 0;
1194
1195 if (motg->chg_type == USB_SDP_CHARGER)
1196 charger_type = POWER_SUPPLY_TYPE_USB;
1197 else if (motg->chg_type == USB_CDP_CHARGER)
1198 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301199 else if (motg->chg_type == USB_DCP_CHARGER ||
1200 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001201 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1202 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1203 motg->chg_type == USB_ACA_A_CHARGER ||
1204 motg->chg_type == USB_ACA_B_CHARGER ||
1205 motg->chg_type == USB_ACA_C_CHARGER))
1206 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1207 else
Anirudh Ghayal685a6a52013-01-08 18:28:55 +05301208 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001209
Jack Pham0c695282012-10-19 18:13:03 -07001210 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001211 pr_err("No USB power supply registered!\n");
1212 return -EINVAL;
1213 }
1214
1215 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001216 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001217 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001218}
1219
Amit Blay0f7edf72012-01-15 10:11:27 +02001220static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1221{
Jack Pham0c695282012-10-19 18:13:03 -07001222 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001223 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1224 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001225 }
1226
David Keitel272ce522012-08-17 16:25:24 -07001227 if (motg->cur_power == 0 && mA > 2) {
1228 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001229 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001230 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001231 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001232 goto psy_error;
1233 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1234 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001235 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001236 goto psy_error;
1237 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001238 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001239 goto psy_error;
1240 }
Jack Pham0c695282012-10-19 18:13:03 -07001241 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001242 return 0;
1243
David Keitel272ce522012-08-17 16:25:24 -07001244psy_error:
1245 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001246 return -ENXIO;
1247}
1248
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301249static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1250{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001251 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301252
1253 if (g && g->is_a_peripheral)
1254 return;
1255
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301256 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1257 motg->chg_type == USB_ACA_A_CHARGER ||
1258 motg->chg_type == USB_ACA_B_CHARGER ||
1259 motg->chg_type == USB_ACA_C_CHARGER) &&
1260 mA > IDEV_ACA_CHG_LIMIT)
1261 mA = IDEV_ACA_CHG_LIMIT;
1262
David Keitel081a3e22012-04-18 12:37:07 -07001263 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001264 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001265 "Failed notifying %d charger type to PMIC\n",
1266 motg->chg_type);
1267
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301268 if (motg->cur_power == mA)
1269 return;
1270
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001271 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001272
1273 /*
1274 * Use Power Supply API if supported, otherwise fallback
1275 * to legacy pm8921 API.
1276 */
1277 if (msm_otg_notify_power_supply(motg, mA))
1278 pm8921_charger_vbus_draw(mA);
1279
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301280 motg->cur_power = mA;
1281}
1282
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001283static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301284{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001285 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301286
1287 /*
1288 * Gadget driver uses set_power method to notify about the
1289 * available current based on suspend/configured states.
1290 *
1291 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1292 * states when CDP/ACA is connected.
1293 */
1294 if (motg->chg_type == USB_SDP_CHARGER)
1295 msm_otg_notify_charger(motg, mA);
1296
1297 return 0;
1298}
1299
Steve Mucklef132c6c2012-06-06 18:30:57 -07001300static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301301{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001302 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301303 struct msm_otg_platform_data *pdata = motg->pdata;
1304 struct usb_hcd *hcd;
1305
1306 if (!otg->host)
1307 return;
1308
1309 hcd = bus_to_hcd(otg->host);
1310
1311 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001312 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301313
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301314 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001315 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301316 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1317
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301318 /*
1319 * Some boards have a switch cotrolled by gpio
1320 * to enable/disable internal HUB. Enable internal
1321 * HUB before kicking the host.
1322 */
1323 if (pdata->setup_gpio)
1324 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301325 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301326 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001327 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301328
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301329 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301330 /* HCD core reset all bits of PORTSC. select ULPI phy */
1331 writel_relaxed(0x80000000, USB_PORTSC);
1332
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301333 if (pdata->setup_gpio)
1334 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301335
1336 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001337 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301338 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301339 }
1340}
1341
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001342static int msm_otg_usbdev_notify(struct notifier_block *self,
1343 unsigned long action, void *priv)
1344{
1345 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001346 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301347 struct usb_device *udev = priv;
1348
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301349 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1350 goto out;
1351
Steve Mucklef132c6c2012-06-06 18:30:57 -07001352 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301353 goto out;
1354 /*
1355 * Interested in devices connected directly to the root hub.
1356 * ACA dock can supply IDEV_CHG irrespective devices connected
1357 * on the accessory port.
1358 */
1359 if (!udev->parent || udev->parent->parent ||
1360 motg->chg_type == USB_ACA_DOCK_CHARGER)
1361 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001362
1363 switch (action) {
1364 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301365 if (aca_enabled())
1366 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001367 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301368 pr_debug("B_CONN set\n");
1369 set_bit(B_CONN, &motg->inputs);
1370 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001371 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301372 /*
1373 * OTG PET: A-device must end session within
1374 * 10 sec after PET enumeration.
1375 */
1376 if (udev->quirks & USB_QUIRK_OTG_PET)
1377 msm_otg_start_timer(motg, TA_TST_MAINT,
1378 A_TST_MAINT);
1379 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301380 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001381 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001382 if (udev->actconfig)
1383 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1384 else
1385 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001386 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301387 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301388 break;
1389 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001390 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1391 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301392 pr_debug("B_CONN clear\n");
1393 clear_bit(B_CONN, &motg->inputs);
1394 /*
1395 * OTG PET: A-device must end session after
1396 * PET disconnection if it is enumerated
1397 * with bcdDevice[0] = 1. USB core sets
1398 * bus->otg_vbus_off for us. clear it here.
1399 */
1400 if (udev->bus->otg_vbus_off) {
1401 udev->bus->otg_vbus_off = 0;
1402 set_bit(A_BUS_DROP, &motg->inputs);
1403 }
1404 queue_work(system_nrt_wq, &motg->sm_work);
1405 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001406 default:
1407 break;
1408 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301409 if (test_bit(ID_A, &motg->inputs))
1410 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1411 motg->mA_port);
1412out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001413 return NOTIFY_OK;
1414}
1415
Mayank Ranae3926882011-12-26 09:47:54 +05301416static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1417{
1418 int ret;
1419 static bool vbus_is_on;
1420
1421 if (vbus_is_on == on)
1422 return;
1423
1424 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301425 ret = motg->pdata->vbus_power(on);
1426 if (!ret)
1427 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301428 return;
1429 }
1430
1431 if (!vbus_otg) {
1432 pr_err("vbus_otg is NULL.");
1433 return;
1434 }
1435
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001436 /*
1437 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001438 * from usb before turning on the boost.
1439 * if exiting host mode disable the boost before enabling to draw
1440 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001441 */
Mayank Ranae3926882011-12-26 09:47:54 +05301442 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001443 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301444 ret = regulator_enable(vbus_otg);
1445 if (ret) {
1446 pr_err("unable to enable vbus_otg\n");
1447 return;
1448 }
1449 vbus_is_on = true;
1450 } else {
1451 ret = regulator_disable(vbus_otg);
1452 if (ret) {
1453 pr_err("unable to disable vbus_otg\n");
1454 return;
1455 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001456 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301457 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301458 }
1459}
1460
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001461static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301462{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001463 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301464 struct usb_hcd *hcd;
1465
1466 /*
1467 * Fail host registration if this board can support
1468 * only peripheral configuration.
1469 */
1470 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001471 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301472 return -ENODEV;
1473 }
1474
Mayank Ranae3926882011-12-26 09:47:54 +05301475 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001476 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301477 if (IS_ERR(vbus_otg)) {
1478 pr_err("Unable to get vbus_otg\n");
1479 return -ENODEV;
1480 }
1481 }
1482
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301483 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001484 if (otg->phy->state == OTG_STATE_A_HOST) {
1485 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001486 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301487 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301488 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301489 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001490 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301491 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301492 } else {
1493 otg->host = NULL;
1494 }
1495
1496 return 0;
1497 }
1498
1499 hcd = bus_to_hcd(host);
1500 hcd->power_budget = motg->pdata->power_budget;
1501
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301502#ifdef CONFIG_USB_OTG
1503 host->otg_port = 1;
1504#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001505 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1506 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301507 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001508 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301509
1510 /*
1511 * Kick the state machine work, if peripheral is not supported
1512 * or peripheral is already registered with us.
1513 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301514 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001515 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301516 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301517 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301518
1519 return 0;
1520}
1521
Steve Mucklef132c6c2012-06-06 18:30:57 -07001522static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301523{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301524 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001525 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301526 struct msm_otg_platform_data *pdata = motg->pdata;
1527
1528 if (!otg->gadget)
1529 return;
1530
1531 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001532 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301533 /*
1534 * Some boards have a switch cotrolled by gpio
1535 * to enable/disable internal HUB. Disable internal
1536 * HUB before kicking the gadget.
1537 */
1538 if (pdata->setup_gpio)
1539 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001540
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301541 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301542 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301543 ret = msm_bus_scale_client_update_request(
1544 motg->bus_perf_client, 1);
1545 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001546 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301547 "bus bandwidth %d\n", __func__, ret);
1548 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301549 usb_gadget_vbus_connect(otg->gadget);
1550 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001551 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301552 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301553 /* Configure BUS performance parameters to default */
1554 if (motg->bus_perf_client) {
1555 ret = msm_bus_scale_client_update_request(
1556 motg->bus_perf_client, 0);
1557 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001558 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301559 "for bus bw %d\n", __func__, ret);
1560 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301561 if (pdata->setup_gpio)
1562 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1563 }
1564
1565}
1566
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001567static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301568 struct usb_gadget *gadget)
1569{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001570 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301571
1572 /*
1573 * Fail peripheral registration if this board can support
1574 * only host configuration.
1575 */
1576 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001577 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301578 return -ENODEV;
1579 }
1580
1581 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001582 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1583 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301584 msm_otg_start_peripheral(otg, 0);
1585 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001586 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301587 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301588 } else {
1589 otg->gadget = NULL;
1590 }
1591
1592 return 0;
1593 }
1594 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001595 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301596
1597 /*
1598 * Kick the state machine work, if host is not supported
1599 * or host is already registered with us.
1600 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301601 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001602 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301603 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301604 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301605
1606 return 0;
1607}
1608
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301609static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1610{
1611 unsigned long flags;
1612 int id;
1613
1614 if (!motg->pdata->pmic_id_irq)
1615 return -ENODEV;
1616
1617 local_irq_save(flags);
1618 id = irq_read_line(motg->pdata->pmic_id_irq);
1619 local_irq_restore(flags);
1620
1621 /*
1622 * If we can not read ID line state for some reason, treat
1623 * it as float. This would prevent MHL discovery and kicking
1624 * host mode unnecessarily.
1625 */
1626 return !!id;
1627}
1628
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301629static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1630 void (*callback)(int on))
1631{
1632 struct usb_phy *phy = &motg->phy;
1633 int ret;
1634
Manoj Raoa7bddd12012-08-27 20:36:45 -07001635 if (!motg->pdata->mhl_enable) {
1636 dev_dbg(phy->dev, "MHL feature not enabled\n");
1637 return -ENODEV;
1638 }
1639
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301640 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1641 !motg->pdata->pmic_id_irq) {
1642 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1643 return -ENODEV;
1644 }
1645
1646 if (!motg->pdata->mhl_dev_name) {
1647 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1648 return -ENODEV;
1649 }
1650
1651 if (callback)
1652 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1653 callback);
1654 else
1655 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1656
1657 if (ret)
1658 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1659 motg->pdata->mhl_dev_name, ret);
1660 else
1661 motg->mhl_enabled = true;
1662
1663 return ret;
1664}
1665
1666static void msm_otg_mhl_notify_online(int on)
1667{
1668 struct msm_otg *motg = the_msm_otg;
1669 struct usb_phy *phy = &motg->phy;
1670 bool queue = false;
1671
1672 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1673
1674 if (on) {
1675 set_bit(MHL, &motg->inputs);
1676 } else {
1677 clear_bit(MHL, &motg->inputs);
1678 queue = true;
1679 }
1680
1681 if (queue && phy->state != OTG_STATE_UNDEFINED)
1682 schedule_work(&motg->sm_work);
1683}
1684
1685static bool msm_otg_is_mhl(struct msm_otg *motg)
1686{
1687 struct usb_phy *phy = &motg->phy;
1688 int is_mhl, ret;
1689
1690 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1691 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1692 /*
1693 * MHL driver calls our callback saying that MHL connected
1694 * if RID_GND is detected. But at later part of discovery
1695 * it may figure out MHL is not connected and returns
1696 * false. Hence clear MHL input here.
1697 */
1698 clear_bit(MHL, &motg->inputs);
1699 dev_dbg(phy->dev, "MHL device not found\n");
1700 return false;
1701 }
1702
1703 set_bit(MHL, &motg->inputs);
1704 dev_dbg(phy->dev, "MHL device found\n");
1705 return true;
1706}
1707
1708static bool msm_chg_mhl_detect(struct msm_otg *motg)
1709{
1710 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301711
1712 if (!motg->mhl_enabled)
1713 return false;
1714
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301715 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301716
1717 if (id)
1718 return false;
1719
1720 mhl_det_in_progress = true;
1721 ret = msm_otg_is_mhl(motg);
1722 mhl_det_in_progress = false;
1723
1724 return ret;
1725}
1726
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301727static void msm_otg_chg_check_timer_func(unsigned long data)
1728{
1729 struct msm_otg *motg = (struct msm_otg *) data;
1730 struct usb_otg *otg = motg->phy.otg;
1731
1732 if (atomic_read(&motg->in_lpm) ||
1733 !test_bit(B_SESS_VLD, &motg->inputs) ||
1734 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1735 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1736 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1737 return;
1738 }
1739
1740 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1741 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1742 set_bit(B_FALSE_SDP, &motg->inputs);
1743 queue_work(system_nrt_wq, &motg->sm_work);
1744 }
1745}
1746
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001747static bool msm_chg_aca_detect(struct msm_otg *motg)
1748{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001749 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001750 u32 int_sts;
1751 bool ret = false;
1752
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301753 if (!aca_enabled())
1754 goto out;
1755
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001756 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1757 goto out;
1758
Steve Mucklef132c6c2012-06-06 18:30:57 -07001759 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001760 switch (int_sts & 0x1C) {
1761 case 0x08:
1762 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001763 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764 motg->chg_type = USB_ACA_A_CHARGER;
1765 motg->chg_state = USB_CHG_STATE_DETECTED;
1766 clear_bit(ID_B, &motg->inputs);
1767 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301768 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769 ret = true;
1770 }
1771 break;
1772 case 0x0C:
1773 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001774 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001775 motg->chg_type = USB_ACA_B_CHARGER;
1776 motg->chg_state = USB_CHG_STATE_DETECTED;
1777 clear_bit(ID_A, &motg->inputs);
1778 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301779 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001780 ret = true;
1781 }
1782 break;
1783 case 0x10:
1784 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001785 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786 motg->chg_type = USB_ACA_C_CHARGER;
1787 motg->chg_state = USB_CHG_STATE_DETECTED;
1788 clear_bit(ID_A, &motg->inputs);
1789 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301790 set_bit(ID, &motg->inputs);
1791 ret = true;
1792 }
1793 break;
1794 case 0x04:
1795 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001796 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301797 motg->chg_type = USB_INVALID_CHARGER;
1798 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1799 clear_bit(ID_A, &motg->inputs);
1800 clear_bit(ID_B, &motg->inputs);
1801 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001802 ret = true;
1803 }
1804 break;
1805 default:
1806 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1807 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301808 test_and_clear_bit(ID_C, &motg->inputs) |
1809 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001810 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001811 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001812 motg->chg_type = USB_INVALID_CHARGER;
1813 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1814 }
1815 }
1816out:
1817 return ret;
1818}
1819
1820static void msm_chg_enable_aca_det(struct msm_otg *motg)
1821{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001822 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301824 if (!aca_enabled())
1825 return;
1826
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001827 switch (motg->pdata->phy_type) {
1828 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301829 /* Disable ID_GND in link and PHY */
1830 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1831 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001832 ulpi_write(phy, 0x01, 0x0C);
1833 ulpi_write(phy, 0x10, 0x0F);
1834 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301835 /* Disable PMIC ID pull-up */
1836 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301837 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001838 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301839 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001840 break;
1841 default:
1842 break;
1843 }
1844}
1845
1846static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1847{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001848 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301850 if (!aca_enabled())
1851 return;
1852
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001853 switch (motg->pdata->phy_type) {
1854 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301855 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001856 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301857 break;
1858 default:
1859 break;
1860 }
1861}
1862
1863static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1864{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001865 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301866
1867 if (!aca_enabled())
1868 return;
1869
1870 switch (motg->pdata->phy_type) {
1871 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001872 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001873 break;
1874 default:
1875 break;
1876 }
1877}
1878
1879static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1880{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001881 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001882 bool ret = false;
1883
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301884 if (!aca_enabled())
1885 return ret;
1886
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001887 switch (motg->pdata->phy_type) {
1888 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001889 if (ulpi_read(phy, 0x91) & 1) {
1890 dev_dbg(phy->dev, "RID change\n");
1891 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001892 ret = msm_chg_aca_detect(motg);
1893 }
1894 default:
1895 break;
1896 }
1897 return ret;
1898}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301899
1900static void msm_otg_id_timer_func(unsigned long data)
1901{
1902 struct msm_otg *motg = (struct msm_otg *) data;
1903
1904 if (!aca_enabled())
1905 return;
1906
1907 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001908 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301909 return;
1910 }
1911
Steve Mucklef132c6c2012-06-06 18:30:57 -07001912 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301913 goto out;
1914
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301915 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001916 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301917 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301918 }
1919
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301920out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301921 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1922 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1923}
1924
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301925static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1926{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001927 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301928 u32 chg_det;
1929 bool ret = false;
1930
1931 switch (motg->pdata->phy_type) {
1932 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001933 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301934 ret = chg_det & (1 << 4);
1935 break;
1936 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001937 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301938 ret = chg_det & 1;
1939 break;
1940 default:
1941 break;
1942 }
1943 return ret;
1944}
1945
1946static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1947{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001948 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301949 u32 chg_det;
1950
1951 switch (motg->pdata->phy_type) {
1952 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001953 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301954 /* Turn off charger block */
1955 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001956 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301957 udelay(20);
1958 /* control chg block via ULPI */
1959 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001960 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301961 /* put it in host mode for enabling D- source */
1962 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001963 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301964 /* Turn on chg detect block */
1965 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001966 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301967 udelay(20);
1968 /* enable chg detection */
1969 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001970 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301971 break;
1972 case SNPS_28NM_INTEGRATED_PHY:
1973 /*
1974 * Configure DM as current source, DP as current sink
1975 * and enable battery charging comparators.
1976 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001977 ulpi_write(phy, 0x8, 0x85);
1978 ulpi_write(phy, 0x2, 0x85);
1979 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301980 break;
1981 default:
1982 break;
1983 }
1984}
1985
1986static bool msm_chg_check_primary_det(struct msm_otg *motg)
1987{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001988 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301989 u32 chg_det;
1990 bool ret = false;
1991
1992 switch (motg->pdata->phy_type) {
1993 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001994 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301995 ret = chg_det & (1 << 4);
1996 break;
1997 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001998 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301999 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302000 /* Turn off VDP_SRC */
2001 ulpi_write(phy, 0x3, 0x86);
2002 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302003 break;
2004 default:
2005 break;
2006 }
2007 return ret;
2008}
2009
2010static void msm_chg_enable_primary_det(struct msm_otg *motg)
2011{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002012 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302013 u32 chg_det;
2014
2015 switch (motg->pdata->phy_type) {
2016 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002017 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302018 /* enable chg detection */
2019 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002020 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302021 break;
2022 case SNPS_28NM_INTEGRATED_PHY:
2023 /*
2024 * Configure DP as current source, DM as current sink
2025 * and enable battery charging comparators.
2026 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002027 ulpi_write(phy, 0x2, 0x85);
2028 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302029 break;
2030 default:
2031 break;
2032 }
2033}
2034
2035static bool msm_chg_check_dcd(struct msm_otg *motg)
2036{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002037 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302038 u32 line_state;
2039 bool ret = false;
2040
2041 switch (motg->pdata->phy_type) {
2042 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002043 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302044 ret = !(line_state & 1);
2045 break;
2046 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002047 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302048 ret = line_state & 2;
2049 break;
2050 default:
2051 break;
2052 }
2053 return ret;
2054}
2055
2056static void msm_chg_disable_dcd(struct msm_otg *motg)
2057{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002058 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302059 u32 chg_det;
2060
2061 switch (motg->pdata->phy_type) {
2062 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002063 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302064 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002065 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066 break;
2067 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002068 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302069 break;
2070 default:
2071 break;
2072 }
2073}
2074
2075static void msm_chg_enable_dcd(struct msm_otg *motg)
2076{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002077 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302078 u32 chg_det;
2079
2080 switch (motg->pdata->phy_type) {
2081 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002082 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302083 /* Turn on D+ current source */
2084 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002085 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302086 break;
2087 case SNPS_28NM_INTEGRATED_PHY:
2088 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002089 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302090 break;
2091 default:
2092 break;
2093 }
2094}
2095
2096static void msm_chg_block_on(struct msm_otg *motg)
2097{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002098 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302099 u32 func_ctrl, chg_det;
2100
2101 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002102 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302103 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2104 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002105 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302106
2107 switch (motg->pdata->phy_type) {
2108 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002109 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302110 /* control chg block via ULPI */
2111 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002112 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302113 /* Turn on chg detect block */
2114 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002115 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302116 udelay(20);
2117 break;
2118 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302119 /* disable DP and DM pull down resistors */
2120 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302121 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002122 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302123 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002124 ulpi_write(phy, 0x1F, 0x92);
2125 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302126 udelay(100);
2127 break;
2128 default:
2129 break;
2130 }
2131}
2132
2133static void msm_chg_block_off(struct msm_otg *motg)
2134{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002135 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302136 u32 func_ctrl, chg_det;
2137
2138 switch (motg->pdata->phy_type) {
2139 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002140 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302141 /* Turn off charger block */
2142 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002143 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302144 break;
2145 case SNPS_28NM_INTEGRATED_PHY:
2146 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002147 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302148 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002149 ulpi_write(phy, 0x1F, 0x92);
2150 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302151 break;
2152 default:
2153 break;
2154 }
2155
2156 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002157 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302158 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2159 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002160 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302161}
2162
Anji jonnalad270e2d2011-08-09 11:28:32 +05302163static const char *chg_to_string(enum usb_chg_type chg_type)
2164{
2165 switch (chg_type) {
2166 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2167 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2168 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2169 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2170 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2171 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2172 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302173 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302174 default: return "INVALID_CHARGER";
2175 }
2176}
2177
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302178#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2179#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302180#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2181#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302182static void msm_chg_detect_work(struct work_struct *w)
2183{
2184 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002185 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302186 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302187 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302188 unsigned long delay;
2189
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002190 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302191
2192 if (test_bit(MHL, &motg->inputs)) {
2193 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2194 return;
2195 }
2196
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302197 switch (motg->chg_state) {
2198 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302199 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302200 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002201 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302202 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302203 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302204 delay = MSM_CHG_DCD_POLL_TIME;
2205 break;
2206 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302207 if (msm_chg_mhl_detect(motg)) {
2208 msm_chg_block_off(motg);
2209 motg->chg_state = USB_CHG_STATE_DETECTED;
2210 motg->chg_type = USB_INVALID_CHARGER;
2211 queue_work(system_nrt_wq, &motg->sm_work);
2212 return;
2213 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002214 is_aca = msm_chg_aca_detect(motg);
2215 if (is_aca) {
2216 /*
2217 * ID_A can be ACA dock too. continue
2218 * primary detection after DCD.
2219 */
2220 if (test_bit(ID_A, &motg->inputs)) {
2221 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2222 } else {
2223 delay = 0;
2224 break;
2225 }
2226 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302227 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetiebb4a2d2013-01-04 12:28:10 +05302228 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2229 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302230 if (is_dcd || tmout) {
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302231 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302232 msm_chg_enable_primary_det(motg);
2233 delay = MSM_CHG_PRIMARY_DET_TIME;
2234 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2235 } else {
2236 delay = MSM_CHG_DCD_POLL_TIME;
2237 }
2238 break;
2239 case USB_CHG_STATE_DCD_DONE:
2240 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302241 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2242 dm_vlgc = line_state & PORTSC_LS_DM;
2243 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302244 if (test_bit(ID_A, &motg->inputs)) {
2245 motg->chg_type = USB_ACA_DOCK_CHARGER;
2246 motg->chg_state = USB_CHG_STATE_DETECTED;
2247 delay = 0;
2248 break;
2249 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302250 if (line_state) { /* DP > VLGC */
2251 motg->chg_type = USB_PROPRIETARY_CHARGER;
2252 motg->chg_state = USB_CHG_STATE_DETECTED;
2253 delay = 0;
2254 } else {
2255 msm_chg_enable_secondary_det(motg);
2256 delay = MSM_CHG_SECONDARY_DET_TIME;
2257 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2258 }
2259 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302260 if (test_bit(ID_A, &motg->inputs)) {
2261 motg->chg_type = USB_ACA_A_CHARGER;
2262 motg->chg_state = USB_CHG_STATE_DETECTED;
2263 delay = 0;
2264 break;
2265 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302266
2267 if (line_state) /* DP > VLGC or/and DM > VLGC */
2268 motg->chg_type = USB_PROPRIETARY_CHARGER;
2269 else
2270 motg->chg_type = USB_SDP_CHARGER;
2271
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302272 motg->chg_state = USB_CHG_STATE_DETECTED;
2273 delay = 0;
2274 }
2275 break;
2276 case USB_CHG_STATE_PRIMARY_DONE:
2277 vout = msm_chg_check_secondary_det(motg);
2278 if (vout)
2279 motg->chg_type = USB_DCP_CHARGER;
2280 else
2281 motg->chg_type = USB_CDP_CHARGER;
2282 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2283 /* fall through */
2284 case USB_CHG_STATE_SECONDARY_DONE:
2285 motg->chg_state = USB_CHG_STATE_DETECTED;
2286 case USB_CHG_STATE_DETECTED:
Pavankumar Kondetid7b6d1a2013-01-11 15:38:09 +05302287 /*
2288 * Notify the charger type to power supply
2289 * owner as soon as we determine the charger.
2290 */
2291 msm_otg_notify_chg_type(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302292 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002293 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302294 /*
2295 * Spurious interrupt is seen after enabling ACA detection
2296 * due to which charger detection fails in case of PET.
2297 * Add delay of 100 microsec to avoid that.
2298 */
2299 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002300 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002301 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302302 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302303 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302304 return;
2305 default:
2306 return;
2307 }
2308
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302309 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302310}
2311
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302312/*
2313 * We support OTG, Peripheral only and Host only configurations. In case
2314 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2315 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2316 * enabled when switch is controlled by user and default mode is supplied
2317 * by board file, which can be changed by userspace later.
2318 */
2319static void msm_otg_init_sm(struct msm_otg *motg)
2320{
2321 struct msm_otg_platform_data *pdata = motg->pdata;
2322 u32 otgsc = readl(USB_OTGSC);
2323
2324 switch (pdata->mode) {
2325 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002326 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302327 if (pdata->default_mode == USB_HOST) {
2328 clear_bit(ID, &motg->inputs);
2329 } else if (pdata->default_mode == USB_PERIPHERAL) {
2330 set_bit(ID, &motg->inputs);
2331 set_bit(B_SESS_VLD, &motg->inputs);
2332 } else {
2333 set_bit(ID, &motg->inputs);
2334 clear_bit(B_SESS_VLD, &motg->inputs);
2335 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302336 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302337 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302338 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302339 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302340 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302341 set_bit(A_BUS_REQ, &motg->inputs);
2342 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002343 if (otgsc & OTGSC_BSV)
2344 set_bit(B_SESS_VLD, &motg->inputs);
2345 else
2346 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302347 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302348 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302349 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302350 set_bit(ID, &motg->inputs);
2351 else
2352 clear_bit(ID, &motg->inputs);
2353 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302354 /*
2355 * VBUS initial state is reported after PMIC
2356 * driver initialization. Wait for it.
2357 */
2358 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302359 }
2360 break;
2361 case USB_HOST:
2362 clear_bit(ID, &motg->inputs);
2363 break;
2364 case USB_PERIPHERAL:
2365 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302366 if (pdata->otg_control == OTG_PHY_CONTROL) {
2367 if (otgsc & OTGSC_BSV)
2368 set_bit(B_SESS_VLD, &motg->inputs);
2369 else
2370 clear_bit(B_SESS_VLD, &motg->inputs);
2371 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2372 /*
2373 * VBUS initial state is reported after PMIC
2374 * driver initialization. Wait for it.
2375 */
2376 wait_for_completion(&pmic_vbus_init);
2377 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302378 break;
2379 default:
2380 break;
2381 }
2382}
2383
2384static void msm_otg_sm_work(struct work_struct *w)
2385{
2386 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002387 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302388 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302389
Steve Mucklef132c6c2012-06-06 18:30:57 -07002390 pm_runtime_resume(otg->phy->dev);
2391 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002392 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302393 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002394 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302395 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002396 if (!psy && legacy_power_supply) {
2397 psy = power_supply_get_by_name("usb");
2398
2399 if (!psy)
2400 pr_err("couldn't get usb power supply\n");
2401 }
2402
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002403 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302404 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2405 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002406 pm_runtime_put_noidle(otg->phy->dev);
2407 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302408 break;
2409 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302410 /* FALL THROUGH */
2411 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302412 if (test_bit(MHL, &motg->inputs)) {
2413 /* allow LPM */
2414 pm_runtime_put_noidle(otg->phy->dev);
2415 pm_runtime_suspend(otg->phy->dev);
2416 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002417 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302418 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302419 if (msm_chg_mhl_detect(motg)) {
2420 work = 1;
2421 break;
2422 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302423 clear_bit(B_BUS_REQ, &motg->inputs);
2424 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002425 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302426 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302427 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302428 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302429 switch (motg->chg_state) {
2430 case USB_CHG_STATE_UNDEFINED:
2431 msm_chg_detect_work(&motg->chg_work.work);
2432 break;
2433 case USB_CHG_STATE_DETECTED:
2434 switch (motg->chg_type) {
2435 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302436 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002437 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302438 /* fall through */
2439 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302440 msm_otg_notify_charger(motg,
2441 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002442 pm_runtime_put_noidle(otg->phy->dev);
2443 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302444 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302445 case USB_ACA_B_CHARGER:
2446 msm_otg_notify_charger(motg,
2447 IDEV_ACA_CHG_MAX);
2448 /*
2449 * (ID_B --> ID_C) PHY_ALT interrupt can
2450 * not be detected in LPM.
2451 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302452 break;
2453 case USB_CDP_CHARGER:
2454 msm_otg_notify_charger(motg,
2455 IDEV_CHG_MAX);
2456 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002457 otg->phy->state =
2458 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302459 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302460 case USB_ACA_C_CHARGER:
2461 msm_otg_notify_charger(motg,
2462 IDEV_ACA_CHG_MAX);
2463 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002464 otg->phy->state =
2465 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302466 break;
2467 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302468 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002469 otg->phy->state =
2470 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302471 mod_timer(&motg->chg_check_timer,
2472 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302473 break;
2474 default:
2475 break;
2476 }
2477 break;
2478 default:
2479 break;
2480 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302481 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2482 pr_debug("b_sess_end && b_bus_req\n");
2483 if (msm_otg_start_srp(otg) < 0) {
2484 clear_bit(B_BUS_REQ, &motg->inputs);
2485 work = 1;
2486 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302487 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002488 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302489 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2490 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302491 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302492 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302493 del_timer_sync(&motg->chg_check_timer);
2494 clear_bit(B_FALSE_SDP, &motg->inputs);
Mayank Rana8ab00352013-01-23 19:26:21 +05302495 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302496 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302497 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2498 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302499 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002500 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302501 /*
2502 * There is a small window where ID interrupt
2503 * is not monitored during ID detection circuit
2504 * switch from ACA to PMIC. Check ID state
2505 * before entering into low power mode.
2506 */
2507 if (!msm_otg_read_pmic_id_state(motg)) {
2508 pr_debug("process missed ID intr\n");
2509 clear_bit(ID, &motg->inputs);
2510 work = 1;
2511 break;
2512 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002513 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002514 /*
2515 * Only if autosuspend was enabled in probe, it will be
2516 * used here. Otherwise, no delay will be used.
2517 */
2518 pm_runtime_mark_last_busy(otg->phy->dev);
2519 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302520 }
2521 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302522 case OTG_STATE_B_SRP_INIT:
2523 if (!test_bit(ID, &motg->inputs) ||
2524 test_bit(ID_A, &motg->inputs) ||
2525 test_bit(ID_C, &motg->inputs) ||
2526 (test_bit(B_SESS_VLD, &motg->inputs) &&
2527 !test_bit(ID_B, &motg->inputs))) {
2528 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2529 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002530 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302531 /*
2532 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2533 * bits after SRP initiation.
2534 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002535 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302536 work = 1;
2537 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2538 pr_debug("b_srp_fail\n");
2539 pr_info("A-device did not respond to SRP\n");
2540 clear_bit(B_BUS_REQ, &motg->inputs);
2541 clear_bit(B_SRP_FAIL, &motg->tmouts);
2542 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002543 ulpi_write(otg->phy, 0x0, 0x98);
2544 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302545 motg->b_last_se0_sess = jiffies;
2546 work = 1;
2547 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302548 break;
2549 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302550 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2551 test_bit(B_FALSE_SDP, &motg->inputs)) {
2552 pr_debug("B_FALSE_SDP\n");
2553 msm_otg_start_peripheral(otg, 0);
2554 motg->chg_type = USB_DCP_CHARGER;
2555 clear_bit(B_FALSE_SDP, &motg->inputs);
2556 otg->phy->state = OTG_STATE_B_IDLE;
2557 work = 1;
2558 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302559 test_bit(ID_A, &motg->inputs) ||
2560 test_bit(ID_B, &motg->inputs) ||
2561 !test_bit(B_SESS_VLD, &motg->inputs)) {
2562 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302563 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2564 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302565 msm_otg_notify_charger(motg, 0);
2566 srp_reqd = otg->gadget->otg_srp_reqd;
2567 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302568 if (test_bit(ID_B, &motg->inputs))
2569 clear_bit(ID_B, &motg->inputs);
2570 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002571 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302572 motg->b_last_se0_sess = jiffies;
2573 if (srp_reqd)
2574 msm_otg_start_timer(motg,
2575 TB_TST_SRP, B_TST_SRP);
2576 else
2577 work = 1;
2578 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2579 otg->gadget->b_hnp_enable &&
2580 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2581 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2582 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2583 /* D+ pullup should not be disconnected within 4msec
2584 * after A device suspends the bus. Otherwise PET will
2585 * fail the compliance test.
2586 */
2587 udelay(1000);
2588 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002589 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302590 /*
2591 * start HCD even before A-device enable
2592 * pull-up to meet HNP timings.
2593 */
2594 otg->host->is_b_host = 1;
2595 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002596 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2597 test_bit(B_SESS_VLD, &motg->inputs)) {
2598 pr_debug("a_bus_suspend && b_sess_vld\n");
2599 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002600 pm_runtime_put_noidle(otg->phy->dev);
2601 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002602 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002603 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302604 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002605 }
2606 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302607 case OTG_STATE_B_WAIT_ACON:
2608 if (!test_bit(ID, &motg->inputs) ||
2609 test_bit(ID_A, &motg->inputs) ||
2610 test_bit(ID_B, &motg->inputs) ||
2611 !test_bit(B_SESS_VLD, &motg->inputs)) {
2612 pr_debug("!id || id_a/b || !b_sess_vld\n");
2613 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302614 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302615 * A-device is physically disconnected during
2616 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302617 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302618 msm_otg_start_host(otg, 0);
2619 otg->host->is_b_host = 0;
2620
2621 clear_bit(B_BUS_REQ, &motg->inputs);
2622 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2623 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002624 otg->phy->state = OTG_STATE_B_IDLE;
2625 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302626 work = 1;
2627 } else if (test_bit(A_CONN, &motg->inputs)) {
2628 pr_debug("a_conn\n");
2629 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002630 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302631 /*
2632 * PET disconnects D+ pullup after reset is generated
2633 * by B device in B_HOST role which is not detected by
2634 * B device. As workaorund , start timer of 300msec
2635 * and stop timer if A device is enumerated else clear
2636 * A_CONN.
2637 */
2638 msm_otg_start_timer(motg, TB_TST_CONFIG,
2639 B_TST_CONFIG);
2640 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2641 pr_debug("b_ase0_brst_tmout\n");
2642 pr_info("B HNP fail:No response from A device\n");
2643 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002644 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302645 otg->host->is_b_host = 0;
2646 clear_bit(B_ASE0_BRST, &motg->tmouts);
2647 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2648 clear_bit(B_BUS_REQ, &motg->inputs);
2649 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002650 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302651 work = 1;
2652 } else if (test_bit(ID_C, &motg->inputs)) {
2653 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2654 }
2655 break;
2656 case OTG_STATE_B_HOST:
2657 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2658 !test_bit(A_CONN, &motg->inputs) ||
2659 !test_bit(B_SESS_VLD, &motg->inputs)) {
2660 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2661 clear_bit(A_CONN, &motg->inputs);
2662 clear_bit(B_BUS_REQ, &motg->inputs);
2663 msm_otg_start_host(otg, 0);
2664 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002665 otg->phy->state = OTG_STATE_B_IDLE;
2666 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302667 work = 1;
2668 } else if (test_bit(ID_C, &motg->inputs)) {
2669 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2670 }
2671 break;
2672 case OTG_STATE_A_IDLE:
2673 otg->default_a = 1;
2674 if (test_bit(ID, &motg->inputs) &&
2675 !test_bit(ID_A, &motg->inputs)) {
2676 pr_debug("id && !id_a\n");
2677 otg->default_a = 0;
2678 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002679 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302680 del_timer_sync(&motg->id_timer);
2681 msm_otg_link_reset(motg);
2682 msm_chg_enable_aca_intr(motg);
2683 msm_otg_notify_charger(motg, 0);
2684 work = 1;
2685 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2686 (test_bit(A_SRP_DET, &motg->inputs) ||
2687 test_bit(A_BUS_REQ, &motg->inputs))) {
2688 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2689
2690 clear_bit(A_SRP_DET, &motg->inputs);
2691 /* Disable SRP detection */
2692 writel_relaxed((readl_relaxed(USB_OTGSC) &
2693 ~OTGSC_INTSTS_MASK) &
2694 ~OTGSC_DPIE, USB_OTGSC);
2695
Steve Mucklef132c6c2012-06-06 18:30:57 -07002696 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302697 /* VBUS should not be supplied before end of SRP pulse
2698 * generated by PET, if not complaince test fail.
2699 */
2700 usleep_range(10000, 12000);
2701 /* ACA: ID_A: Stop charging untill enumeration */
2702 if (test_bit(ID_A, &motg->inputs))
2703 msm_otg_notify_charger(motg, 0);
2704 else
2705 msm_hsusb_vbus_power(motg, 1);
2706 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2707 } else {
2708 pr_debug("No session requested\n");
2709 clear_bit(A_BUS_DROP, &motg->inputs);
2710 if (test_bit(ID_A, &motg->inputs)) {
2711 msm_otg_notify_charger(motg,
2712 IDEV_ACA_CHG_MAX);
2713 } else if (!test_bit(ID, &motg->inputs)) {
2714 msm_otg_notify_charger(motg, 0);
2715 /*
2716 * A-device is not providing power on VBUS.
2717 * Enable SRP detection.
2718 */
2719 writel_relaxed(0x13, USB_USBMODE);
2720 writel_relaxed((readl_relaxed(USB_OTGSC) &
2721 ~OTGSC_INTSTS_MASK) |
2722 OTGSC_DPIE, USB_OTGSC);
2723 mb();
2724 }
2725 }
2726 break;
2727 case OTG_STATE_A_WAIT_VRISE:
2728 if ((test_bit(ID, &motg->inputs) &&
2729 !test_bit(ID_A, &motg->inputs)) ||
2730 test_bit(A_BUS_DROP, &motg->inputs) ||
2731 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2732 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2733 clear_bit(A_BUS_REQ, &motg->inputs);
2734 msm_otg_del_timer(motg);
2735 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002736 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302737 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2738 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2739 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002740 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302741 if (TA_WAIT_BCON > 0)
2742 msm_otg_start_timer(motg, TA_WAIT_BCON,
2743 A_WAIT_BCON);
2744 msm_otg_start_host(otg, 1);
2745 msm_chg_enable_aca_det(motg);
2746 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302747 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302748 if (msm_chg_check_aca_intr(motg))
2749 work = 1;
2750 }
2751 break;
2752 case OTG_STATE_A_WAIT_BCON:
2753 if ((test_bit(ID, &motg->inputs) &&
2754 !test_bit(ID_A, &motg->inputs)) ||
2755 test_bit(A_BUS_DROP, &motg->inputs) ||
2756 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2757 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2758 "a_wait_bcon_tmout\n");
2759 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2760 pr_info("Device No Response\n");
2761 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2762 }
2763 msm_otg_del_timer(motg);
2764 clear_bit(A_BUS_REQ, &motg->inputs);
2765 clear_bit(B_CONN, &motg->inputs);
2766 msm_otg_start_host(otg, 0);
2767 /*
2768 * ACA: ID_A with NO accessory, just the A plug is
2769 * attached to ACA: Use IDCHG_MAX for charging
2770 */
2771 if (test_bit(ID_A, &motg->inputs))
2772 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2773 else
2774 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002775 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302776 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2777 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2778 pr_debug("!a_vbus_vld\n");
2779 clear_bit(B_CONN, &motg->inputs);
2780 msm_otg_del_timer(motg);
2781 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002782 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2783 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302784 } else if (test_bit(ID_A, &motg->inputs)) {
2785 msm_hsusb_vbus_power(motg, 0);
2786 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2787 /*
2788 * If TA_WAIT_BCON is infinite, we don;t
2789 * turn off VBUS. Enter low power mode.
2790 */
2791 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002792 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302793 } else if (!test_bit(ID, &motg->inputs)) {
2794 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302795 }
2796 break;
2797 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302798 if ((test_bit(ID, &motg->inputs) &&
2799 !test_bit(ID_A, &motg->inputs)) ||
2800 test_bit(A_BUS_DROP, &motg->inputs)) {
2801 pr_debug("id_a/b/c || a_bus_drop\n");
2802 clear_bit(B_CONN, &motg->inputs);
2803 clear_bit(A_BUS_REQ, &motg->inputs);
2804 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002805 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302806 msm_otg_start_host(otg, 0);
2807 if (!test_bit(ID_A, &motg->inputs))
2808 msm_hsusb_vbus_power(motg, 0);
2809 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2810 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2811 pr_debug("!a_vbus_vld\n");
2812 clear_bit(B_CONN, &motg->inputs);
2813 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002814 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302815 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002816 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302817 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2818 /*
2819 * a_bus_req is de-asserted when root hub is
2820 * suspended or HNP is in progress.
2821 */
2822 pr_debug("!a_bus_req\n");
2823 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002824 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302825 if (otg->host->b_hnp_enable)
2826 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2827 A_AIDL_BDIS);
2828 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002829 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302830 } else if (!test_bit(B_CONN, &motg->inputs)) {
2831 pr_debug("!b_conn\n");
2832 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002833 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302834 if (TA_WAIT_BCON > 0)
2835 msm_otg_start_timer(motg, TA_WAIT_BCON,
2836 A_WAIT_BCON);
2837 if (msm_chg_check_aca_intr(motg))
2838 work = 1;
2839 } else if (test_bit(ID_A, &motg->inputs)) {
2840 msm_otg_del_timer(motg);
2841 msm_hsusb_vbus_power(motg, 0);
2842 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2843 msm_otg_notify_charger(motg,
2844 IDEV_ACA_CHG_MAX);
2845 else
2846 msm_otg_notify_charger(motg,
2847 IDEV_CHG_MIN - motg->mA_port);
2848 } else if (!test_bit(ID, &motg->inputs)) {
2849 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2850 motg->chg_type = USB_INVALID_CHARGER;
2851 msm_otg_notify_charger(motg, 0);
2852 msm_hsusb_vbus_power(motg, 1);
2853 }
2854 break;
2855 case OTG_STATE_A_SUSPEND:
2856 if ((test_bit(ID, &motg->inputs) &&
2857 !test_bit(ID_A, &motg->inputs)) ||
2858 test_bit(A_BUS_DROP, &motg->inputs) ||
2859 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2860 pr_debug("id_a/b/c || a_bus_drop ||"
2861 "a_aidl_bdis_tmout\n");
2862 msm_otg_del_timer(motg);
2863 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002864 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302865 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002866 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302867 if (!test_bit(ID_A, &motg->inputs))
2868 msm_hsusb_vbus_power(motg, 0);
2869 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2870 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2871 pr_debug("!a_vbus_vld\n");
2872 msm_otg_del_timer(motg);
2873 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002874 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302875 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002876 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302877 } else if (!test_bit(B_CONN, &motg->inputs) &&
2878 otg->host->b_hnp_enable) {
2879 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002880 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302881 msm_otg_host_hnp_enable(otg, 1);
2882 otg->gadget->is_a_peripheral = 1;
2883 msm_otg_start_peripheral(otg, 1);
2884 } else if (!test_bit(B_CONN, &motg->inputs) &&
2885 !otg->host->b_hnp_enable) {
2886 pr_debug("!b_conn && !b_hnp_enable");
2887 /*
2888 * bus request is dropped during suspend.
2889 * acquire again for next device.
2890 */
2891 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002892 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302893 if (TA_WAIT_BCON > 0)
2894 msm_otg_start_timer(motg, TA_WAIT_BCON,
2895 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002896 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302897 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002898 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302899 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002900 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002901 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302902 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302903 }
2904 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302905 case OTG_STATE_A_PERIPHERAL:
2906 if ((test_bit(ID, &motg->inputs) &&
2907 !test_bit(ID_A, &motg->inputs)) ||
2908 test_bit(A_BUS_DROP, &motg->inputs)) {
2909 pr_debug("id _f/b/c || a_bus_drop\n");
2910 /* Clear BIDL_ADIS timer */
2911 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002912 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302913 msm_otg_start_peripheral(otg, 0);
2914 otg->gadget->is_a_peripheral = 0;
2915 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002916 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302917 if (!test_bit(ID_A, &motg->inputs))
2918 msm_hsusb_vbus_power(motg, 0);
2919 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2920 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2921 pr_debug("!a_vbus_vld\n");
2922 /* Clear BIDL_ADIS timer */
2923 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002924 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302925 msm_otg_start_peripheral(otg, 0);
2926 otg->gadget->is_a_peripheral = 0;
2927 msm_otg_start_host(otg, 0);
2928 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2929 pr_debug("a_bidl_adis_tmout\n");
2930 msm_otg_start_peripheral(otg, 0);
2931 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002932 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302933 set_bit(A_BUS_REQ, &motg->inputs);
2934 msm_otg_host_hnp_enable(otg, 0);
2935 if (TA_WAIT_BCON > 0)
2936 msm_otg_start_timer(motg, TA_WAIT_BCON,
2937 A_WAIT_BCON);
2938 } else if (test_bit(ID_A, &motg->inputs)) {
2939 msm_hsusb_vbus_power(motg, 0);
2940 msm_otg_notify_charger(motg,
2941 IDEV_CHG_MIN - motg->mA_port);
2942 } else if (!test_bit(ID, &motg->inputs)) {
2943 msm_otg_notify_charger(motg, 0);
2944 msm_hsusb_vbus_power(motg, 1);
2945 }
2946 break;
2947 case OTG_STATE_A_WAIT_VFALL:
2948 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2949 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002950 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302951 work = 1;
2952 }
2953 break;
2954 case OTG_STATE_A_VBUS_ERR:
2955 if ((test_bit(ID, &motg->inputs) &&
2956 !test_bit(ID_A, &motg->inputs)) ||
2957 test_bit(A_BUS_DROP, &motg->inputs) ||
2958 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002959 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302960 if (!test_bit(ID_A, &motg->inputs))
2961 msm_hsusb_vbus_power(motg, 0);
2962 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2963 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2964 motg->chg_type = USB_INVALID_CHARGER;
2965 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302966 }
2967 break;
2968 default:
2969 break;
2970 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302971 if (work)
2972 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302973}
2974
Amit Blayd0fe07b2012-09-05 16:42:09 +03002975static void msm_otg_suspend_work(struct work_struct *w)
2976{
2977 struct msm_otg *motg =
2978 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05302979
2980 /* This work is only for device bus suspend */
2981 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
2982 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03002983}
2984
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302985static irqreturn_t msm_otg_irq(int irq, void *data)
2986{
2987 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002988 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302989 u32 otgsc = 0, usbsts, pc;
2990 bool work = 0;
2991 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302992
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302993 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002994 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302995 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002996 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07002997 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002998 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302999 return IRQ_HANDLED;
3000 }
3001
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003002 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303003 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303004
3005 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303006 return IRQ_NONE;
3007
3008 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303009 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303010 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303011 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303012 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303013 pr_debug("Id clear\n");
3014 /*
3015 * Assert a_bus_req to supply power on
3016 * VBUS when Micro/Mini-A cable is connected
3017 * with out user intervention.
3018 */
3019 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303020 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303021 msm_chg_enable_aca_det(motg);
3022 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303023 writel_relaxed(otgsc, USB_OTGSC);
3024 work = 1;
3025 } else if (otgsc & OTGSC_DPIS) {
3026 pr_debug("DPIS detected\n");
3027 writel_relaxed(otgsc, USB_OTGSC);
3028 set_bit(A_SRP_DET, &motg->inputs);
3029 set_bit(A_BUS_REQ, &motg->inputs);
3030 work = 1;
3031 } else if (otgsc & OTGSC_BSVIS) {
3032 writel_relaxed(otgsc, USB_OTGSC);
3033 /*
3034 * BSV interrupt comes when operating as an A-device
3035 * (VBUS on/off).
3036 * But, handle BSV when charger is removed from ACA in ID_A
3037 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003038 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303039 !test_bit(ID_A, &motg->inputs))
3040 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303041 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303042 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303043 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303044 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303045 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303046 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003047 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3048
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303049 msm_chg_check_aca_intr(motg);
3050 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303051 work = 1;
3052 } else if (usbsts & STS_PCI) {
3053 pc = readl_relaxed(USB_PORTSC);
3054 pr_debug("portsc = %x\n", pc);
3055 ret = IRQ_NONE;
3056 /*
3057 * HCD Acks PCI interrupt. We use this to switch
3058 * between different OTG states.
3059 */
3060 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003061 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303062 case OTG_STATE_A_SUSPEND:
3063 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3064 !(pc & PORTSC_CCS)) {
3065 pr_debug("B_CONN clear\n");
3066 clear_bit(B_CONN, &motg->inputs);
3067 msm_otg_del_timer(motg);
3068 }
3069 break;
3070 case OTG_STATE_A_PERIPHERAL:
3071 /*
3072 * A-peripheral observed activity on bus.
3073 * clear A_BIDL_ADIS timer.
3074 */
3075 msm_otg_del_timer(motg);
3076 work = 0;
3077 break;
3078 case OTG_STATE_B_WAIT_ACON:
3079 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3080 pr_debug("A_CONN set\n");
3081 set_bit(A_CONN, &motg->inputs);
3082 /* Clear ASE0_BRST timer */
3083 msm_otg_del_timer(motg);
3084 }
3085 break;
3086 case OTG_STATE_B_HOST:
3087 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3088 pr_debug("A_CONN clear\n");
3089 clear_bit(A_CONN, &motg->inputs);
3090 msm_otg_del_timer(motg);
3091 }
3092 break;
3093 case OTG_STATE_A_WAIT_BCON:
3094 if (TA_WAIT_BCON < 0)
3095 set_bit(A_BUS_REQ, &motg->inputs);
3096 default:
3097 work = 0;
3098 break;
3099 }
3100 } else if (usbsts & STS_URI) {
3101 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003102 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303103 case OTG_STATE_A_PERIPHERAL:
3104 /*
3105 * A-peripheral observed activity on bus.
3106 * clear A_BIDL_ADIS timer.
3107 */
3108 msm_otg_del_timer(motg);
3109 work = 0;
3110 break;
3111 default:
3112 work = 0;
3113 break;
3114 }
3115 } else if (usbsts & STS_SLI) {
3116 ret = IRQ_NONE;
3117 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003118 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303119 case OTG_STATE_B_PERIPHERAL:
3120 if (otg->gadget->b_hnp_enable) {
3121 set_bit(A_BUS_SUSPEND, &motg->inputs);
3122 set_bit(B_BUS_REQ, &motg->inputs);
3123 work = 1;
3124 }
3125 break;
3126 case OTG_STATE_A_PERIPHERAL:
3127 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3128 A_BIDL_ADIS);
3129 break;
3130 default:
3131 break;
3132 }
3133 } else if ((usbsts & PHY_ALT_INT)) {
3134 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3135 if (msm_chg_check_aca_intr(motg))
3136 work = 1;
3137 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303138 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303139 if (work)
3140 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303141
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303142 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003143}
3144
3145static void msm_otg_set_vbus_state(int online)
3146{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303147 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003148 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303149
Chiranjeevi Velempati2e1048b2012-11-08 09:54:33 +05303150 /* Ignore received BSV interrupts, if ID pin is GND */
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303151 if (!test_bit(ID, &motg->inputs)) {
3152 /*
3153 * state machine work waits for initial VBUS
3154 * completion in UNDEFINED state. Process
3155 * the initial VBUS event in ID_GND state.
3156 */
3157 if (init)
3158 return;
3159 goto complete;
3160 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003161
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303162 if (online) {
3163 pr_debug("PMIC: BSV set\n");
3164 set_bit(B_SESS_VLD, &motg->inputs);
3165 } else {
3166 pr_debug("PMIC: BSV clear\n");
3167 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303168 }
Pavankumar Kondetibbaa46ff2012-12-09 20:21:02 +05303169complete:
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303170 if (!init) {
3171 init = true;
3172 complete(&pmic_vbus_init);
3173 pr_debug("PMIC: BSV init complete\n");
3174 return;
3175 }
3176
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303177 if (test_bit(MHL, &motg->inputs) ||
3178 mhl_det_in_progress) {
3179 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3180 return;
3181 }
3182
Jack Pham5ca279b2012-05-14 18:42:54 -07003183 if (atomic_read(&motg->pm_suspended))
3184 motg->sm_work_pending = true;
3185 else
3186 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003187}
3188
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303189static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003190{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303191 struct msm_otg *motg = container_of(w, struct msm_otg,
3192 pmic_id_status_work.work);
3193 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003194
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303195 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303196 if (!test_and_set_bit(ID, &motg->inputs)) {
3197 pr_debug("PMIC: ID set\n");
3198 work = 1;
3199 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303200 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303201 if (test_and_clear_bit(ID, &motg->inputs)) {
3202 pr_debug("PMIC: ID clear\n");
3203 set_bit(A_BUS_REQ, &motg->inputs);
3204 work = 1;
3205 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303206 }
3207
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303208 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003209 if (atomic_read(&motg->pm_suspended))
3210 motg->sm_work_pending = true;
3211 else
3212 queue_work(system_nrt_wq, &motg->sm_work);
3213 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303214
3215}
3216
3217#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3218static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3219{
3220 struct msm_otg *motg = data;
3221
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303222 if (test_bit(MHL, &motg->inputs) ||
3223 mhl_det_in_progress) {
3224 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3225 return IRQ_HANDLED;
3226 }
3227
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303228 if (!aca_id_turned_on)
3229 /*schedule delayed work for 5msec for ID line state to settle*/
3230 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3231 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003232
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303233 return IRQ_HANDLED;
3234}
3235
3236static int msm_otg_mode_show(struct seq_file *s, void *unused)
3237{
3238 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003239 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303240
Steve Mucklef132c6c2012-06-06 18:30:57 -07003241 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303242 case OTG_STATE_A_HOST:
3243 seq_printf(s, "host\n");
3244 break;
3245 case OTG_STATE_B_PERIPHERAL:
3246 seq_printf(s, "peripheral\n");
3247 break;
3248 default:
3249 seq_printf(s, "none\n");
3250 break;
3251 }
3252
3253 return 0;
3254}
3255
3256static int msm_otg_mode_open(struct inode *inode, struct file *file)
3257{
3258 return single_open(file, msm_otg_mode_show, inode->i_private);
3259}
3260
3261static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3262 size_t count, loff_t *ppos)
3263{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303264 struct seq_file *s = file->private_data;
3265 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303266 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003267 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303268 int status = count;
3269 enum usb_mode_type req_mode;
3270
3271 memset(buf, 0x00, sizeof(buf));
3272
3273 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3274 status = -EFAULT;
3275 goto out;
3276 }
3277
3278 if (!strncmp(buf, "host", 4)) {
3279 req_mode = USB_HOST;
3280 } else if (!strncmp(buf, "peripheral", 10)) {
3281 req_mode = USB_PERIPHERAL;
3282 } else if (!strncmp(buf, "none", 4)) {
3283 req_mode = USB_NONE;
3284 } else {
3285 status = -EINVAL;
3286 goto out;
3287 }
3288
3289 switch (req_mode) {
3290 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003291 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303292 case OTG_STATE_A_HOST:
3293 case OTG_STATE_B_PERIPHERAL:
3294 set_bit(ID, &motg->inputs);
3295 clear_bit(B_SESS_VLD, &motg->inputs);
3296 break;
3297 default:
3298 goto out;
3299 }
3300 break;
3301 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003302 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303303 case OTG_STATE_B_IDLE:
3304 case OTG_STATE_A_HOST:
3305 set_bit(ID, &motg->inputs);
3306 set_bit(B_SESS_VLD, &motg->inputs);
3307 break;
3308 default:
3309 goto out;
3310 }
3311 break;
3312 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003313 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303314 case OTG_STATE_B_IDLE:
3315 case OTG_STATE_B_PERIPHERAL:
3316 clear_bit(ID, &motg->inputs);
3317 break;
3318 default:
3319 goto out;
3320 }
3321 break;
3322 default:
3323 goto out;
3324 }
3325
Steve Mucklef132c6c2012-06-06 18:30:57 -07003326 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303327 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303328out:
3329 return status;
3330}
3331
3332const struct file_operations msm_otg_mode_fops = {
3333 .open = msm_otg_mode_open,
3334 .read = seq_read,
3335 .write = msm_otg_mode_write,
3336 .llseek = seq_lseek,
3337 .release = single_release,
3338};
3339
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303340static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3341{
3342 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003343 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303344
Steve Mucklef132c6c2012-06-06 18:30:57 -07003345 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303346 return 0;
3347}
3348
3349static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3350{
3351 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3352}
3353
3354const struct file_operations msm_otg_state_fops = {
3355 .open = msm_otg_otg_state_open,
3356 .read = seq_read,
3357 .llseek = seq_lseek,
3358 .release = single_release,
3359};
3360
Anji jonnalad270e2d2011-08-09 11:28:32 +05303361static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3362{
3363 struct msm_otg *motg = s->private;
3364
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303365 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303366 return 0;
3367}
3368
3369static int msm_otg_chg_open(struct inode *inode, struct file *file)
3370{
3371 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3372}
3373
3374const struct file_operations msm_otg_chg_fops = {
3375 .open = msm_otg_chg_open,
3376 .read = seq_read,
3377 .llseek = seq_lseek,
3378 .release = single_release,
3379};
3380
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303381static int msm_otg_aca_show(struct seq_file *s, void *unused)
3382{
3383 if (debug_aca_enabled)
3384 seq_printf(s, "enabled\n");
3385 else
3386 seq_printf(s, "disabled\n");
3387
3388 return 0;
3389}
3390
3391static int msm_otg_aca_open(struct inode *inode, struct file *file)
3392{
3393 return single_open(file, msm_otg_aca_show, inode->i_private);
3394}
3395
3396static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3397 size_t count, loff_t *ppos)
3398{
3399 char buf[8];
3400
3401 memset(buf, 0x00, sizeof(buf));
3402
3403 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3404 return -EFAULT;
3405
3406 if (!strncmp(buf, "enable", 6))
3407 debug_aca_enabled = true;
3408 else
3409 debug_aca_enabled = false;
3410
3411 return count;
3412}
3413
3414const struct file_operations msm_otg_aca_fops = {
3415 .open = msm_otg_aca_open,
3416 .read = seq_read,
3417 .write = msm_otg_aca_write,
3418 .llseek = seq_lseek,
3419 .release = single_release,
3420};
3421
Manu Gautam8bdcc592012-03-06 11:26:06 +05303422static int msm_otg_bus_show(struct seq_file *s, void *unused)
3423{
3424 if (debug_bus_voting_enabled)
3425 seq_printf(s, "enabled\n");
3426 else
3427 seq_printf(s, "disabled\n");
3428
3429 return 0;
3430}
3431
3432static int msm_otg_bus_open(struct inode *inode, struct file *file)
3433{
3434 return single_open(file, msm_otg_bus_show, inode->i_private);
3435}
3436
3437static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3438 size_t count, loff_t *ppos)
3439{
3440 char buf[8];
3441 int ret;
3442 struct seq_file *s = file->private_data;
3443 struct msm_otg *motg = s->private;
3444
3445 memset(buf, 0x00, sizeof(buf));
3446
3447 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3448 return -EFAULT;
3449
3450 if (!strncmp(buf, "enable", 6)) {
3451 /* Do not vote here. Let OTG statemachine decide when to vote */
3452 debug_bus_voting_enabled = true;
3453 } else {
3454 debug_bus_voting_enabled = false;
3455 if (motg->bus_perf_client) {
3456 ret = msm_bus_scale_client_update_request(
3457 motg->bus_perf_client, 0);
3458 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003459 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303460 "for bus bw %d\n", __func__, ret);
3461 }
3462 }
3463
3464 return count;
3465}
3466
David Keitel272ce522012-08-17 16:25:24 -07003467static int otg_power_get_property_usb(struct power_supply *psy,
3468 enum power_supply_property psp,
3469 union power_supply_propval *val)
3470{
Jack Pham0c695282012-10-19 18:13:03 -07003471 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003472 switch (psp) {
3473 case POWER_SUPPLY_PROP_SCOPE:
3474 if (motg->host_mode)
3475 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3476 else
3477 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3478 break;
3479 case POWER_SUPPLY_PROP_CURRENT_MAX:
3480 val->intval = motg->current_max;
3481 break;
3482 /* Reflect USB enumeration */
3483 case POWER_SUPPLY_PROP_PRESENT:
3484 case POWER_SUPPLY_PROP_ONLINE:
3485 val->intval = motg->online;
3486 break;
3487 default:
3488 return -EINVAL;
3489 }
3490 return 0;
3491}
3492
3493static int otg_power_set_property_usb(struct power_supply *psy,
3494 enum power_supply_property psp,
3495 const union power_supply_propval *val)
3496{
Jack Pham0c695282012-10-19 18:13:03 -07003497 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003498
3499 switch (psp) {
3500 /* Process PMIC notification in PRESENT prop */
3501 case POWER_SUPPLY_PROP_PRESENT:
3502 msm_otg_set_vbus_state(val->intval);
3503 break;
3504 /* The ONLINE property reflects if usb has enumerated */
3505 case POWER_SUPPLY_PROP_ONLINE:
3506 motg->online = val->intval;
3507 break;
3508 case POWER_SUPPLY_PROP_CURRENT_MAX:
3509 motg->current_max = val->intval;
3510 break;
3511 default:
3512 return -EINVAL;
3513 }
3514
3515 power_supply_changed(&motg->usb_psy);
3516 return 0;
3517}
3518
David Collinsd79acc52012-11-26 14:59:00 -08003519static int otg_power_property_is_writeable_usb(struct power_supply *psy,
3520 enum power_supply_property psp)
3521{
3522 switch (psp) {
3523 case POWER_SUPPLY_PROP_PRESENT:
3524 case POWER_SUPPLY_PROP_ONLINE:
3525 case POWER_SUPPLY_PROP_CURRENT_MAX:
3526 return 1;
3527 default:
3528 break;
3529 }
3530
3531 return 0;
3532}
3533
David Keitel272ce522012-08-17 16:25:24 -07003534static char *otg_pm_power_supplied_to[] = {
3535 "battery",
3536};
3537
3538static enum power_supply_property otg_pm_power_props_usb[] = {
3539 POWER_SUPPLY_PROP_PRESENT,
3540 POWER_SUPPLY_PROP_ONLINE,
3541 POWER_SUPPLY_PROP_CURRENT_MAX,
3542 POWER_SUPPLY_PROP_SCOPE,
3543};
3544
Manu Gautam8bdcc592012-03-06 11:26:06 +05303545const struct file_operations msm_otg_bus_fops = {
3546 .open = msm_otg_bus_open,
3547 .read = seq_read,
3548 .write = msm_otg_bus_write,
3549 .llseek = seq_lseek,
3550 .release = single_release,
3551};
3552
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303553static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303554
3555static int msm_otg_debugfs_init(struct msm_otg *motg)
3556{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303557 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303558
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303559 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3560
3561 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3562 return -ENODEV;
3563
Anji jonnalad270e2d2011-08-09 11:28:32 +05303564 if (motg->pdata->mode == USB_OTG &&
3565 motg->pdata->otg_control == OTG_USER_CONTROL) {
3566
Manu Gautam8bdcc592012-03-06 11:26:06 +05303567 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303568 S_IWUSR, msm_otg_dbg_root, motg,
3569 &msm_otg_mode_fops);
3570
Manu Gautam8bdcc592012-03-06 11:26:06 +05303571 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303572 debugfs_remove(msm_otg_dbg_root);
3573 msm_otg_dbg_root = NULL;
3574 return -ENODEV;
3575 }
3576 }
3577
Manu Gautam8bdcc592012-03-06 11:26:06 +05303578 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303579 msm_otg_dbg_root, motg,
3580 &msm_otg_chg_fops);
3581
Manu Gautam8bdcc592012-03-06 11:26:06 +05303582 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303583 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303584 return -ENODEV;
3585 }
3586
Manu Gautam8bdcc592012-03-06 11:26:06 +05303587 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303588 msm_otg_dbg_root, motg,
3589 &msm_otg_aca_fops);
3590
Manu Gautam8bdcc592012-03-06 11:26:06 +05303591 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303592 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303593 return -ENODEV;
3594 }
3595
Manu Gautam8bdcc592012-03-06 11:26:06 +05303596 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3597 msm_otg_dbg_root, motg,
3598 &msm_otg_bus_fops);
3599
3600 if (!msm_otg_dentry) {
3601 debugfs_remove_recursive(msm_otg_dbg_root);
3602 return -ENODEV;
3603 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303604
3605 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3606 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3607
3608 if (!msm_otg_dentry) {
3609 debugfs_remove_recursive(msm_otg_dbg_root);
3610 return -ENODEV;
3611 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303612 return 0;
3613}
3614
3615static void msm_otg_debugfs_cleanup(void)
3616{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303617 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303618}
3619
Manu Gautam0ddbd922012-09-21 17:17:38 +05303620#define MSM_OTG_CMD_ID 0x09
3621#define MSM_OTG_DEVICE_ID 0x04
3622#define MSM_OTG_VMID_IDX 0xFF
3623#define MSM_OTG_MEM_TYPE 0x02
3624struct msm_otg_scm_cmd_buf {
3625 unsigned int device_id;
3626 unsigned int vmid_idx;
3627 unsigned int mem_type;
3628} __attribute__ ((__packed__));
3629
3630static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3631{
3632 int ret;
3633 struct msm_otg_platform_data *pdata = motg->pdata;
3634 struct msm_otg_scm_cmd_buf cmd_buf;
3635
3636 if (!pdata->pnoc_errata_fix)
3637 return;
3638
3639 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3640
3641 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3642 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3643 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3644
3645 ret = scm_call(SCM_SVC_CP, MSM_OTG_CMD_ID, &cmd_buf,
3646 sizeof(cmd_buf), NULL, 0);
3647
3648 if (ret)
3649 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3650}
3651
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303652static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3653static struct platform_device *msm_otg_add_pdev(
3654 struct platform_device *ofdev, const char *name)
3655{
3656 struct platform_device *pdev;
3657 const struct resource *res = ofdev->resource;
3658 unsigned int num = ofdev->num_resources;
3659 int retval;
3660
3661 pdev = platform_device_alloc(name, -1);
3662 if (!pdev) {
3663 retval = -ENOMEM;
3664 goto error;
3665 }
3666
3667 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3668 pdev->dev.dma_mask = &msm_otg_dma_mask;
3669
3670 if (num) {
3671 retval = platform_device_add_resources(pdev, res, num);
3672 if (retval)
3673 goto error;
3674 }
3675
3676 retval = platform_device_add(pdev);
3677 if (retval)
3678 goto error;
3679
3680 return pdev;
3681
3682error:
3683 platform_device_put(pdev);
3684 return ERR_PTR(retval);
3685}
3686
3687static int msm_otg_setup_devices(struct platform_device *ofdev,
3688 enum usb_mode_type mode, bool init)
3689{
3690 const char *gadget_name = "msm_hsusb";
3691 const char *host_name = "msm_hsusb_host";
3692 static struct platform_device *gadget_pdev;
3693 static struct platform_device *host_pdev;
3694 int retval = 0;
3695
3696 if (!init) {
3697 if (gadget_pdev)
3698 platform_device_unregister(gadget_pdev);
3699 if (host_pdev)
3700 platform_device_unregister(host_pdev);
3701 return 0;
3702 }
3703
3704 switch (mode) {
3705 case USB_OTG:
3706 /* fall through */
3707 case USB_PERIPHERAL:
3708 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3709 if (IS_ERR(gadget_pdev)) {
3710 retval = PTR_ERR(gadget_pdev);
3711 break;
3712 }
3713 if (mode == USB_PERIPHERAL)
3714 break;
3715 /* fall through */
3716 case USB_HOST:
3717 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3718 if (IS_ERR(host_pdev)) {
3719 retval = PTR_ERR(host_pdev);
3720 if (mode == USB_OTG)
3721 platform_device_unregister(gadget_pdev);
3722 }
3723 break;
3724 default:
3725 break;
3726 }
3727
3728 return retval;
3729}
3730
David Keitel272ce522012-08-17 16:25:24 -07003731static int msm_otg_register_power_supply(struct platform_device *pdev,
3732 struct msm_otg *motg)
3733{
3734 int ret;
3735
3736 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3737 if (ret < 0) {
3738 dev_err(motg->phy.dev,
3739 "%s:power_supply_register usb failed\n",
3740 __func__);
3741 return ret;
3742 }
3743
3744 legacy_power_supply = false;
3745 return 0;
3746}
3747
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303748struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3749{
3750 struct device_node *node = pdev->dev.of_node;
3751 struct msm_otg_platform_data *pdata;
3752 int len = 0;
3753
3754 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3755 if (!pdata) {
3756 pr_err("unable to allocate platform data\n");
3757 return NULL;
3758 }
3759 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3760 if (len) {
3761 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3762 if (!pdata->phy_init_seq)
3763 return NULL;
3764 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3765 pdata->phy_init_seq,
3766 len/sizeof(*pdata->phy_init_seq));
3767 }
3768 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3769 &pdata->power_budget);
3770 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3771 &pdata->mode);
3772 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3773 &pdata->otg_control);
3774 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3775 &pdata->default_mode);
3776 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3777 &pdata->phy_type);
3778 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3779 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303780 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3781 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05303782 pdata->pnoc_errata_fix = of_property_read_bool(node,
3783 "qcom,hsusb-otg-pnoc-errata-fix");
Ido Shayevitza7114b92013-01-13 13:34:47 +02003784 pdata->enable_lpm_on_dev_suspend = of_property_read_bool(node,
3785 "qcom,hsusb-otg-lpm-on-dev-suspend");
Ido Shayevitz26193352013-01-21 23:16:54 +02003786 pdata->core_clk_always_on_workaround = of_property_read_bool(node,
3787 "qcom,hsusb-otg-clk-always-on-workaround");
Manu Gautambd53fba2012-07-31 16:13:06 +05303788
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303789 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303790}
3791
3792static int __init msm_otg_probe(struct platform_device *pdev)
3793{
Manu Gautamf8c45642012-08-10 10:20:56 -07003794 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303795 struct resource *res;
3796 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003797 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303798 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303799
3800 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303801
3802 if (pdev->dev.of_node) {
3803 dev_dbg(&pdev->dev, "device tree enabled\n");
3804 pdata = msm_otg_dt_to_pdata(pdev);
3805 if (!pdata)
3806 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003807
3808 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3809 if (!pdata->bus_scale_table)
3810 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3811
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303812 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3813 if (ret) {
3814 dev_err(&pdev->dev, "devices setup failed\n");
3815 return ret;
3816 }
3817 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303818 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3819 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303820 } else {
3821 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303822 }
3823
3824 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3825 if (!motg) {
3826 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3827 return -ENOMEM;
3828 }
3829
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003830 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3831 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003832 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3833 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303834 goto free_motg;
3835 }
3836
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003837 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303838 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003839 phy = &motg->phy;
3840 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303841
3842 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303843 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3844 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3845 * PMIC can detect ACA ID_GND and generate an interrupt.
3846 */
3847 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3848 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3849 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003850 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303851 }
3852
Ofir Cohen4da266f2012-01-03 10:19:29 +02003853 /* initialize reset counter */
3854 motg->reset_counter = 0;
3855
Amit Blay02eff132011-09-21 16:46:24 +03003856 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003857 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003858 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003859 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303860
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303861 /*
3862 * Targets on which link uses asynchronous reset methodology,
3863 * free running clock is not required during the reset.
3864 */
Manu Gautam5143b252012-01-05 19:25:23 -08003865 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303866 if (IS_ERR(motg->clk))
3867 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3868 else
3869 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303870
3871 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003872 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303873 * CORE CLK must be running at >55Mhz for correct HSUSB
3874 * operation and USB core cannot tolerate frequency changes on
3875 * CORE CLK. For such USB cores, vote for maximum clk frequency
3876 * on pclk source
3877 */
Manu Gautam5143b252012-01-05 19:25:23 -08003878 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3879 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303880 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003881 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303882 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003883 goto put_clk;
3884 }
3885 clk_set_rate(motg->core_clk, INT_MAX);
3886
3887 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3888 if (IS_ERR(motg->pclk)) {
3889 dev_err(&pdev->dev, "failed to get iface_clk\n");
3890 ret = PTR_ERR(motg->pclk);
3891 goto put_core_clk;
3892 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303893
3894 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3895 if (!res) {
3896 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3897 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003898 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303899 }
3900
3901 motg->regs = ioremap(res->start, resource_size(res));
3902 if (!motg->regs) {
3903 dev_err(&pdev->dev, "ioremap failed\n");
3904 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003905 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303906 }
3907 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3908
3909 motg->irq = platform_get_irq(pdev, 0);
3910 if (!motg->irq) {
3911 dev_err(&pdev->dev, "platform_get_irq failed\n");
3912 ret = -ENODEV;
3913 goto free_regs;
3914 }
3915
Manu Gautamf8c45642012-08-10 10:20:56 -07003916 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3917 if (motg->async_irq < 0) {
3918 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3919 motg->async_irq = 0;
3920 }
3921
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003922 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003923 if (IS_ERR(motg->xo_handle)) {
3924 dev_err(&pdev->dev, "%s not able to get the handle "
3925 "to vote for TCXO D0 buffer\n", __func__);
3926 ret = PTR_ERR(motg->xo_handle);
3927 goto free_regs;
3928 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303929
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003930 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003931 if (ret) {
3932 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3933 "D0 buffer%d\n", __func__, ret);
3934 goto free_xo_handle;
3935 }
3936
Manu Gautam28b1bac2012-01-30 16:43:06 +05303937 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303938
Mayank Rana248698c2012-04-19 00:03:16 +05303939 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003940 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303941 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003942 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303943 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003944 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003945 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303946 goto devote_xo_handle;
3947 }
3948 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303949 }
3950
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003951 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303952 if (ret) {
3953 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303954 goto devote_xo_handle;
3955 }
3956
3957 ret = regulator_enable(hsusb_vddcx);
3958 if (ret) {
3959 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3960 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303961 }
3962
3963 ret = msm_hsusb_ldo_init(motg, 1);
3964 if (ret) {
3965 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303966 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303967 }
3968
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303969 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303970 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3971 "mhl_usb_hs_switch");
3972 if (IS_ERR(mhl_usb_hs_switch)) {
3973 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003974 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303975 goto free_ldo_init;
3976 }
3977 }
3978
Amit Blay81801aa2012-09-19 12:08:12 +02003979 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303980 if (ret) {
3981 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003982 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303983 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303984 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303985
Manu Gautam0ddbd922012-09-21 17:17:38 +05303986 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
3987 msm_otg_pnoc_errata_fix(motg);
3988
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303989 writel(0, USB_USBINTR);
3990 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003991 /* Ensure that above STOREs are completed before enabling interrupts */
3992 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303993
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303994 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3995 if (ret)
3996 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003997 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303998 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303999 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304000 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304001 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004002 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304003 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
4004 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05304005 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
4006 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304007 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
4008 "msm_otg", motg);
4009 if (ret) {
4010 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004011 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304012 }
4013
Manu Gautamf8c45642012-08-10 10:20:56 -07004014 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05304015 ret = request_irq(motg->async_irq, msm_otg_irq,
4016 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07004017 if (ret) {
4018 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
4019 goto free_irq;
4020 }
4021 disable_irq(motg->async_irq);
4022 }
4023
Jack Pham87f202f2012-08-06 00:24:22 -07004024 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
4025 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
4026
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004027 phy->init = msm_otg_reset;
4028 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004029 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304030
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004031 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304032
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004033 phy->otg->phy = &motg->phy;
4034 phy->otg->set_host = msm_otg_set_host;
4035 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07004036 phy->otg->start_hnp = msm_otg_start_hnp;
4037 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004038
4039 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304040 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004041 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07004042 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304043 }
4044
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304045 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304046 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004047 if (motg->pdata->pmic_id_irq) {
4048 ret = request_irq(motg->pdata->pmic_id_irq,
4049 msm_pmic_id_irq,
4050 IRQF_TRIGGER_RISING |
4051 IRQF_TRIGGER_FALLING,
4052 "msm_otg", motg);
4053 if (ret) {
4054 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004055 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004056 }
4057 } else {
4058 ret = -ENODEV;
4059 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004060 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004061 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304062 }
4063
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304064 msm_hsusb_mhl_switch_enable(motg, 1);
4065
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304066 platform_set_drvdata(pdev, motg);
4067 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004068 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304069
Anji jonnalad270e2d2011-08-09 11:28:32 +05304070 ret = msm_otg_debugfs_init(motg);
4071 if (ret)
4072 dev_dbg(&pdev->dev, "mode debugfs file is"
4073 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304074
Amit Blay58b31472011-11-18 09:39:39 +02004075 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4076 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304077 (!(motg->pdata->mode == USB_OTG) ||
4078 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004079 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304080 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004081
Amit Blay58b31472011-11-18 09:39:39 +02004082 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004083 motg->caps = ALLOW_PHY_RETENTION |
4084 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02004085 }
4086
Amit Blay6fa647a2012-05-24 14:12:08 +03004087 if (motg->pdata->enable_lpm_on_dev_suspend)
4088 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4089
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004090 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304091 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004092 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304093
Amit Blayd6f38282012-10-29 13:13:46 +02004094 if (motg->pdata->delay_lpm_on_disconnect) {
4095 pm_runtime_set_autosuspend_delay(&pdev->dev,
4096 lpm_disconnect_thresh);
4097 pm_runtime_use_autosuspend(&pdev->dev);
4098 }
4099
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304100 if (motg->pdata->bus_scale_table) {
4101 motg->bus_perf_client =
4102 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4103 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004104 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304105 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05304106 else
4107 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304108 }
4109
David Keitel272ce522012-08-17 16:25:24 -07004110 motg->usb_psy.name = "usb";
4111 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4112 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4113 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4114 motg->usb_psy.properties = otg_pm_power_props_usb;
4115 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4116 motg->usb_psy.get_property = otg_power_get_property_usb;
4117 motg->usb_psy.set_property = otg_power_set_property_usb;
David Collinsd79acc52012-11-26 14:59:00 -08004118 motg->usb_psy.property_is_writeable
4119 = otg_power_property_is_writeable_usb;
David Keitel272ce522012-08-17 16:25:24 -07004120
Jack Pham0c695282012-10-19 18:13:03 -07004121 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004122 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004123 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4124 legacy_power_supply = true;
4125 } else {
4126 /* otherwise register our own power supply */
4127 if (!msm_otg_register_power_supply(pdev, motg))
4128 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004129 }
4130
Jack Pham0c695282012-10-19 18:13:03 -07004131 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4132 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4133
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304134 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004135
Steve Mucklef132c6c2012-06-06 18:30:57 -07004136remove_phy:
4137 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004138free_async_irq:
4139 if (motg->async_irq)
4140 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304141free_irq:
4142 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004143destroy_wlock:
4144 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304145 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004146 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004147free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304148 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304149free_hsusb_vddcx:
4150 regulator_disable(hsusb_vddcx);
4151free_config_vddcx:
4152 regulator_set_voltage(hsusb_vddcx,
4153 vdd_val[motg->vdd_type][VDD_NONE],
4154 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004155devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304156 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004157 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004158free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004159 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304160free_regs:
4161 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004162put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304163 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304164put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004165 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304166put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304167 if (!IS_ERR(motg->clk))
4168 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004169 if (!IS_ERR(motg->phy_reset_clk))
4170 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004171free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004172 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304173free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304174 kfree(motg);
4175 return ret;
4176}
4177
4178static int __devexit msm_otg_remove(struct platform_device *pdev)
4179{
4180 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004181 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304182 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304183
4184 if (otg->host || otg->gadget)
4185 return -EBUSY;
4186
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304187 if (pdev->dev.of_node)
4188 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004189 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4190 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304191 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304192 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304193 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304194 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004195 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304196 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304197
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304198 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304199
4200 device_init_wakeup(&pdev->dev, 0);
4201 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004202 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304203
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304204 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004205 if (motg->pdata->pmic_id_irq)
4206 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004207 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304208 free_irq(motg->irq, motg);
4209
Jack Pham87f202f2012-08-06 00:24:22 -07004210 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4211 motg->pdata->mpm_otgsessvld_int)
4212 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4213
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304214 /*
4215 * Put PHY in low power mode.
4216 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07004217 ulpi_read(otg->phy, 0x14);
4218 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304219
4220 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4221 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4222 if (readl(USB_PORTSC) & PORTSC_PHCD)
4223 break;
4224 udelay(1);
4225 cnt++;
4226 }
4227 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004228 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304229
Manu Gautam28b1bac2012-01-30 16:43:06 +05304230 clk_disable_unprepare(motg->pclk);
4231 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004232 msm_xo_put(motg->xo_handle);
Amit Blay81801aa2012-09-19 12:08:12 +02004233 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304234 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304235 regulator_disable(hsusb_vddcx);
4236 regulator_set_voltage(hsusb_vddcx,
4237 vdd_val[motg->vdd_type][VDD_NONE],
4238 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304239
4240 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304241 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304242
Amit Blay02eff132011-09-21 16:46:24 +03004243 if (!IS_ERR(motg->phy_reset_clk))
4244 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304245 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304246 if (!IS_ERR(motg->clk))
4247 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004248 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304249
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304250 if (motg->bus_perf_client)
4251 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304252
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004253 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304254 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304255 return 0;
4256}
4257
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304258#ifdef CONFIG_PM_RUNTIME
4259static int msm_otg_runtime_idle(struct device *dev)
4260{
4261 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004262 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304263
4264 dev_dbg(dev, "OTG runtime idle\n");
4265
Steve Mucklef132c6c2012-06-06 18:30:57 -07004266 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304267 return -EAGAIN;
4268 else
4269 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304270}
4271
4272static int msm_otg_runtime_suspend(struct device *dev)
4273{
4274 struct msm_otg *motg = dev_get_drvdata(dev);
4275
4276 dev_dbg(dev, "OTG runtime suspend\n");
4277 return msm_otg_suspend(motg);
4278}
4279
4280static int msm_otg_runtime_resume(struct device *dev)
4281{
4282 struct msm_otg *motg = dev_get_drvdata(dev);
4283
4284 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304285 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304286 return msm_otg_resume(motg);
4287}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304288#endif
4289
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304290#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304291static int msm_otg_pm_suspend(struct device *dev)
4292{
Jack Pham5ca279b2012-05-14 18:42:54 -07004293 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304294 struct msm_otg *motg = dev_get_drvdata(dev);
4295
4296 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004297
4298 atomic_set(&motg->pm_suspended, 1);
4299 ret = msm_otg_suspend(motg);
4300 if (ret)
4301 atomic_set(&motg->pm_suspended, 0);
4302
4303 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304304}
4305
4306static int msm_otg_pm_resume(struct device *dev)
4307{
Jack Pham5ca279b2012-05-14 18:42:54 -07004308 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304309 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304310
4311 dev_dbg(dev, "OTG PM resume\n");
4312
Jack Pham5ca279b2012-05-14 18:42:54 -07004313 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004314 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004315 pm_runtime_get_noresume(dev);
4316 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304317
Jack Pham5ca279b2012-05-14 18:42:54 -07004318 /* Update runtime PM status */
4319 pm_runtime_disable(dev);
4320 pm_runtime_set_active(dev);
4321 pm_runtime_enable(dev);
4322
Jack Phamc7edb172012-08-13 15:32:39 -07004323 if (motg->sm_work_pending) {
4324 motg->sm_work_pending = false;
4325 queue_work(system_nrt_wq, &motg->sm_work);
4326 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004327 }
4328
4329 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304330}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304331#endif
4332
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304333#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304334static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304335 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4336 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4337 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304338};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304339#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304340
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304341static struct of_device_id msm_otg_dt_match[] = {
4342 { .compatible = "qcom,hsusb-otg",
4343 },
4344 {}
4345};
4346
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304347static struct platform_driver msm_otg_driver = {
4348 .remove = __devexit_p(msm_otg_remove),
4349 .driver = {
4350 .name = DRIVER_NAME,
4351 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304352#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304353 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304354#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304355 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304356 },
4357};
4358
4359static int __init msm_otg_init(void)
4360{
4361 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4362}
4363
4364static void __exit msm_otg_exit(void)
4365{
4366 platform_driver_unregister(&msm_otg_driver);
4367}
4368
4369module_init(msm_otg_init);
4370module_exit(msm_otg_exit);
4371
4372MODULE_LICENSE("GPL v2");
4373MODULE_DESCRIPTION("MSM USB transceiver driver");