blob: 51ad08212598c3c19936e478df30f1138744aaa9 [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Sridhar Parasuram3c67a412012-09-26 09:36:22 -070042#include <linux/power_supply.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053043#include <linux/mhl_8334.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
45#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
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053085static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070086static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053087static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053088static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053089static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070090
Anji jonnala11aa5c42011-05-04 10:19:48 +053091static struct regulator *hsusb_3p3;
92static struct regulator *hsusb_1p8;
93static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053094static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +053095static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053096static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053097
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053098static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053099static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530100{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530101#ifdef CONFIG_USB_MSM_ACA
102 return true;
103#else
104 return debug_aca_enabled;
105#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +0530106}
107
Mayank Rana248698c2012-04-19 00:03:16 +0530108static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
109 { /* VDD_CX CORNER Voting */
110 [VDD_NONE] = RPM_VREG_CORNER_NONE,
111 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
112 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
113 },
114 { /* VDD_CX Voltage Voting */
115 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
116 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
117 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
118 },
119};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530120
121static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
122{
123 int rc = 0;
124
125 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700126 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530127 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200128 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530129 return PTR_ERR(hsusb_3p3);
130 }
131
132 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
133 USB_PHY_3P3_VOL_MAX);
134 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700135 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530137 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700139 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530140 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200141 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530142 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144 }
145 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
146 USB_PHY_1P8_VOL_MAX);
147 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700148 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530150 goto put_1p8;
151 }
152
153 return 0;
154 }
155
Anji jonnala11aa5c42011-05-04 10:19:48 +0530156put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700158put_3p3_lpm:
159 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530160 return rc;
161}
162
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530163static int msm_hsusb_config_vddcx(int high)
164{
Mayank Rana248698c2012-04-19 00:03:16 +0530165 struct msm_otg *motg = the_msm_otg;
166 enum usb_vdd_type vdd_type = motg->vdd_type;
167 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530168 int min_vol;
169 int ret;
170
Mayank Rana248698c2012-04-19 00:03:16 +0530171 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530172 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
173 if (ret) {
174 pr_err("%s: unable to set the voltage for regulator "
175 "HSUSB_VDDCX\n", __func__);
176 return ret;
177 }
178
179 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
180
181 return ret;
182}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530183
Amit Blay81801aa2012-09-19 12:08:12 +0200184static int msm_hsusb_ldo_enable(struct msm_otg *motg,
185 enum msm_otg_phy_reg_mode mode)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530186{
187 int ret = 0;
188
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530189 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530190 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
191 return -ENODEV;
192 }
193
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530194 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530195 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
196 return -ENODEV;
197 }
198
Amit Blay81801aa2012-09-19 12:08:12 +0200199 switch (mode) {
200 case USB_PHY_REG_ON:
Anji jonnala11aa5c42011-05-04 10:19:48 +0530201 ret = regulator_set_optimum_mode(hsusb_1p8,
202 USB_PHY_1P8_HPM_LOAD);
203 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530205 "HSUSB_1p8\n", __func__);
206 return ret;
207 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208
209 ret = regulator_enable(hsusb_1p8);
210 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700211 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 __func__);
213 regulator_set_optimum_mode(hsusb_1p8, 0);
214 return ret;
215 }
216
Anji jonnala11aa5c42011-05-04 10:19:48 +0530217 ret = regulator_set_optimum_mode(hsusb_3p3,
218 USB_PHY_3P3_HPM_LOAD);
219 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530221 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222 regulator_set_optimum_mode(hsusb_1p8, 0);
223 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530224 return ret;
225 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226
227 ret = regulator_enable(hsusb_3p3);
228 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700229 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 __func__);
231 regulator_set_optimum_mode(hsusb_3p3, 0);
232 regulator_set_optimum_mode(hsusb_1p8, 0);
233 regulator_disable(hsusb_1p8);
234 return ret;
235 }
236
Amit Blay81801aa2012-09-19 12:08:12 +0200237 break;
238
239 case USB_PHY_REG_OFF:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 ret = regulator_disable(hsusb_1p8);
241 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700242 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700243 __func__);
244 return ret;
245 }
246
247 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530248 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530250 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700251
252 ret = regulator_disable(hsusb_3p3);
253 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700254 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 __func__);
256 return ret;
257 }
258 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530259 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700260 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530261 "HSUSB_3p3\n", __func__);
Amit Blay81801aa2012-09-19 12:08:12 +0200262
263 break;
264
265 case USB_PHY_REG_LPM_ON:
266 ret = regulator_set_optimum_mode(hsusb_1p8,
267 USB_PHY_1P8_LPM_LOAD);
268 if (ret < 0) {
269 pr_err("%s: Unable to set LPM of the regulator: HSUSB_1p8\n",
270 __func__);
271 return ret;
272 }
273
274 ret = regulator_set_optimum_mode(hsusb_3p3,
275 USB_PHY_3P3_LPM_LOAD);
276 if (ret < 0) {
277 pr_err("%s: Unable to set LPM of the regulator: HSUSB_3p3\n",
278 __func__);
279 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
280 return ret;
281 }
282
283 break;
284
285 case USB_PHY_REG_LPM_OFF:
286 ret = regulator_set_optimum_mode(hsusb_1p8,
287 USB_PHY_1P8_HPM_LOAD);
288 if (ret < 0) {
289 pr_err("%s: Unable to set HPM of the regulator: HSUSB_1p8\n",
290 __func__);
291 return ret;
292 }
293
294 ret = regulator_set_optimum_mode(hsusb_3p3,
295 USB_PHY_3P3_HPM_LOAD);
296 if (ret < 0) {
297 pr_err("%s: Unable to set HPM of the regulator: HSUSB_3p3\n",
298 __func__);
299 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
300 return ret;
301 }
302
303 break;
304
305 default:
306 pr_err("%s: Unsupported mode (%d).", __func__, mode);
307 return -ENOTSUPP;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530308 }
309
Amit Blay81801aa2012-09-19 12:08:12 +0200310 pr_debug("%s: USB reg mode (%d) (OFF/HPM/LPM)\n", __func__, mode);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530311 return ret < 0 ? ret : 0;
312}
313
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530314static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
315{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530316 struct msm_otg_platform_data *pdata = motg->pdata;
317
318 if (!pdata->mhl_enable)
319 return;
320
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530321 if (!mhl_usb_hs_switch) {
322 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530323 return;
324 }
325
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530326 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530327 if (regulator_enable(mhl_usb_hs_switch))
328 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530329 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530330 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530331 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530332}
333
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200334static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530335{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200336 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530337 int cnt = 0;
338
339 /* initiate read operation */
340 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
341 USB_ULPI_VIEWPORT);
342
343 /* wait for completion */
344 while (cnt < ULPI_IO_TIMEOUT_USEC) {
345 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
346 break;
347 udelay(1);
348 cnt++;
349 }
350
351 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200352 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530353 readl(USB_ULPI_VIEWPORT));
354 return -ETIMEDOUT;
355 }
356 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
357}
358
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200359static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530360{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200361 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530362 int cnt = 0;
363
364 /* initiate write operation */
365 writel(ULPI_RUN | ULPI_WRITE |
366 ULPI_ADDR(reg) | ULPI_DATA(val),
367 USB_ULPI_VIEWPORT);
368
369 /* wait for completion */
370 while (cnt < ULPI_IO_TIMEOUT_USEC) {
371 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
372 break;
373 udelay(1);
374 cnt++;
375 }
376
377 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200378 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530379 return -ETIMEDOUT;
380 }
381 return 0;
382}
383
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200384static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530385 .read = ulpi_read,
386 .write = ulpi_write,
387};
388
389static void ulpi_init(struct msm_otg *motg)
390{
391 struct msm_otg_platform_data *pdata = motg->pdata;
Mayank Rana443f9e42012-09-21 18:32:39 +0530392 int aseq[10];
393 int *seq = NULL;
394
395 if (override_phy_init) {
396 pr_debug("%s(): HUSB PHY Init:%s\n", __func__,
397 override_phy_init);
398 get_options(override_phy_init, ARRAY_SIZE(aseq), aseq);
399 seq = &aseq[1];
400 } else {
401 seq = pdata->phy_init_seq;
402 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530403
404 if (!seq)
405 return;
406
407 while (seq[0] >= 0) {
Mayank Rana443f9e42012-09-21 18:32:39 +0530408 if (override_phy_init)
409 pr_debug("ulpi: write 0x%02x to 0x%02x\n",
410 seq[0], seq[1]);
411
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200412 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530413 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200414 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530415 seq += 2;
416 }
417}
418
419static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
420{
421 int ret;
422
423 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530424 if (!IS_ERR(motg->clk)) {
425 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
426 } else {
427 /* Using asynchronous block reset to the hardware */
428 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
429 clk_disable_unprepare(motg->pclk);
430 clk_disable_unprepare(motg->core_clk);
431 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
432 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530433 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200434 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530435 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530436 if (!IS_ERR(motg->clk)) {
437 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
438 } else {
439 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
440 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
441 ndelay(200);
442 clk_prepare_enable(motg->core_clk);
443 clk_prepare_enable(motg->pclk);
444 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530445 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200446 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530447 }
448 return ret;
449}
450
451static int msm_otg_phy_clk_reset(struct msm_otg *motg)
452{
453 int ret;
454
Amit Blay02eff132011-09-21 16:46:24 +0300455 if (IS_ERR(motg->phy_reset_clk))
456 return 0;
457
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530458 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
459 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200460 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530461 return ret;
462 }
463 usleep_range(10000, 12000);
464 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
465 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200466 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530467 return ret;
468}
469
470static int msm_otg_phy_reset(struct msm_otg *motg)
471{
472 u32 val;
473 int ret;
474 int retries;
475
476 ret = msm_otg_link_clk_reset(motg, 1);
477 if (ret)
478 return ret;
479 ret = msm_otg_phy_clk_reset(motg);
480 if (ret)
481 return ret;
482 ret = msm_otg_link_clk_reset(motg, 0);
483 if (ret)
484 return ret;
485
486 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
487 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
488
489 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200490 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530491 ULPI_CLR(ULPI_FUNC_CTRL));
492 if (!ret)
493 break;
494 ret = msm_otg_phy_clk_reset(motg);
495 if (ret)
496 return ret;
497 }
498 if (!retries)
499 return -ETIMEDOUT;
500
501 /* This reset calibrates the phy, if the above write succeeded */
502 ret = msm_otg_phy_clk_reset(motg);
503 if (ret)
504 return ret;
505
506 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200507 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530508 if (ret != -ETIMEDOUT)
509 break;
510 ret = msm_otg_phy_clk_reset(motg);
511 if (ret)
512 return ret;
513 }
514 if (!retries)
515 return -ETIMEDOUT;
516
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200517 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530518 return 0;
519}
520
521#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530522static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530523{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530524 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530525
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530526 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530527 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530528 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530529 break;
530 udelay(1);
531 cnt++;
532 }
533 if (cnt >= LINK_RESET_TIMEOUT_USEC)
534 return -ETIMEDOUT;
535
536 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530537 writel_relaxed(0x80000000, USB_PORTSC);
538 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530539 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530540
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530541 return 0;
542}
543
Steve Mucklef132c6c2012-06-06 18:30:57 -0700544static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530545{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700546 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530547 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530548 int ret;
549 u32 val = 0;
550 u32 ulpi_val = 0;
551
Ofir Cohen4da266f2012-01-03 10:19:29 +0200552 /*
553 * USB PHY and Link reset also reset the USB BAM.
554 * Thus perform reset operation only once to avoid
555 * USB BAM reset on other cases e.g. USB cable disconnections.
556 */
557 if (pdata->disable_reset_on_disconnect) {
558 if (motg->reset_counter)
559 return 0;
560 else
561 motg->reset_counter++;
562 }
563
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530564 if (!IS_ERR(motg->clk))
565 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530566 ret = msm_otg_phy_reset(motg);
567 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700568 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530569 return ret;
570 }
571
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530572 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530573 ret = msm_otg_link_reset(motg);
574 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700575 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530576 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530577 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530578 msleep(100);
579
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530580 ulpi_init(motg);
581
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700582 /* Ensure that RESET operation is completed before turning off clock */
583 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530584
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530585 if (!IS_ERR(motg->clk))
586 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530587
588 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530589 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530590 if (pdata->mode == USB_OTG) {
591 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
592 val |= OTGSC_IDIE | OTGSC_BSVIE;
593 } else if (pdata->mode == USB_PERIPHERAL) {
594 ulpi_val = ULPI_INT_SESS_VALID;
595 val |= OTGSC_BSVIE;
596 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530597 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200598 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
599 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530600 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700601 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530602 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530603 /* Enable PMIC pull-up */
604 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530605 }
606
607 return 0;
608}
609
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530610static const char *timer_string(int bit)
611{
612 switch (bit) {
613 case A_WAIT_VRISE: return "a_wait_vrise";
614 case A_WAIT_VFALL: return "a_wait_vfall";
615 case B_SRP_FAIL: return "b_srp_fail";
616 case A_WAIT_BCON: return "a_wait_bcon";
617 case A_AIDL_BDIS: return "a_aidl_bdis";
618 case A_BIDL_ADIS: return "a_bidl_adis";
619 case B_ASE0_BRST: return "b_ase0_brst";
620 case A_TST_MAINT: return "a_tst_maint";
621 case B_TST_SRP: return "b_tst_srp";
622 case B_TST_CONFIG: return "b_tst_config";
623 default: return "UNDEFINED";
624 }
625}
626
627static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
628{
629 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
630
631 switch (motg->active_tmout) {
632 case A_WAIT_VRISE:
633 /* TODO: use vbus_vld interrupt */
634 set_bit(A_VBUS_VLD, &motg->inputs);
635 break;
636 case A_TST_MAINT:
637 /* OTG PET: End session after TA_TST_MAINT */
638 set_bit(A_BUS_DROP, &motg->inputs);
639 break;
640 case B_TST_SRP:
641 /*
642 * OTG PET: Initiate SRP after TB_TST_SRP of
643 * previous session end.
644 */
645 set_bit(B_BUS_REQ, &motg->inputs);
646 break;
647 case B_TST_CONFIG:
648 clear_bit(A_CONN, &motg->inputs);
649 break;
650 default:
651 set_bit(motg->active_tmout, &motg->tmouts);
652 }
653
654 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
655 queue_work(system_nrt_wq, &motg->sm_work);
656 return HRTIMER_NORESTART;
657}
658
659static void msm_otg_del_timer(struct msm_otg *motg)
660{
661 int bit = motg->active_tmout;
662
663 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
664 div_s64(ktime_to_us(hrtimer_get_remaining(
665 &motg->timer)), 1000));
666 hrtimer_cancel(&motg->timer);
667 clear_bit(bit, &motg->tmouts);
668}
669
670static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
671{
672 clear_bit(bit, &motg->tmouts);
673 motg->active_tmout = bit;
674 pr_debug("starting %s timer\n", timer_string(bit));
675 hrtimer_start(&motg->timer,
676 ktime_set(time / 1000, (time % 1000) * 1000000),
677 HRTIMER_MODE_REL);
678}
679
680static void msm_otg_init_timer(struct msm_otg *motg)
681{
682 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
683 motg->timer.function = msm_otg_timer_func;
684}
685
Steve Mucklef132c6c2012-06-06 18:30:57 -0700686static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530687{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700688 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530689
Steve Mucklef132c6c2012-06-06 18:30:57 -0700690 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530691 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700692 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530693 return -EINVAL;
694 }
695
696 pr_debug("A-Host: HNP initiated\n");
697 clear_bit(A_BUS_REQ, &motg->inputs);
698 queue_work(system_nrt_wq, &motg->sm_work);
699 return 0;
700}
701
Steve Mucklef132c6c2012-06-06 18:30:57 -0700702static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530703{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700704 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530705 u32 val;
706 int ret = 0;
707
Steve Mucklef132c6c2012-06-06 18:30:57 -0700708 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530709 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700710 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530711 ret = -EINVAL;
712 goto out;
713 }
714
715 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
716 pr_debug("initial conditions of SRP are not met. Try again"
717 "after some time\n");
718 ret = -EAGAIN;
719 goto out;
720 }
721
722 pr_debug("B-Device SRP started\n");
723
724 /*
725 * PHY won't pull D+ high unless it detects Vbus valid.
726 * Since by definition, SRP is only done when Vbus is not valid,
727 * software work-around needs to be used to spoof the PHY into
728 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
729 * VBUSVLDEXT register bits.
730 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700731 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530732 /*
733 * Harware auto assist data pulsing: Data pulse is given
734 * for 7msec; wait for vbus
735 */
736 val = readl_relaxed(USB_OTGSC);
737 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
738
739 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
740out:
741 return ret;
742}
743
Steve Mucklef132c6c2012-06-06 18:30:57 -0700744static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530745{
746 struct usb_hcd *hcd = bus_to_hcd(otg->host);
747 struct usb_device *rhub = otg->host->root_hub;
748
749 if (enable) {
750 pm_runtime_disable(&rhub->dev);
751 rhub->state = USB_STATE_NOTATTACHED;
752 hcd->driver->bus_suspend(hcd);
753 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
754 } else {
755 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700756 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530757 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
758 }
759}
760
Steve Mucklef132c6c2012-06-06 18:30:57 -0700761static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530762{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700763 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530764
Amit Blay6fa647a2012-05-24 14:12:08 +0300765 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530766 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530767
Amit Blayd0fe07b2012-09-05 16:42:09 +0300768 if (atomic_read(&motg->in_lpm) == suspend &&
769 !atomic_read(&motg->suspend_work_pending))
Jack Pham69e621d2012-06-25 18:48:07 -0700770 return 0;
771
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530772 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700773 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530774 case OTG_STATE_A_WAIT_BCON:
775 if (TA_WAIT_BCON > 0)
776 break;
777 /* fall through */
778 case OTG_STATE_A_HOST:
779 pr_debug("host bus suspend\n");
780 clear_bit(A_BUS_REQ, &motg->inputs);
781 queue_work(system_nrt_wq, &motg->sm_work);
782 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300783 case OTG_STATE_B_PERIPHERAL:
784 pr_debug("peripheral bus suspend\n");
785 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
786 break;
787 set_bit(A_BUS_SUSPEND, &motg->inputs);
Amit Blayd0fe07b2012-09-05 16:42:09 +0300788 atomic_set(&motg->suspend_work_pending, 1);
789 queue_delayed_work(system_nrt_wq, &motg->suspend_work,
790 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300791 break;
792
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530793 default:
794 break;
795 }
796 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700797 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530798 case OTG_STATE_A_SUSPEND:
799 /* Remote wakeup or resume */
800 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700801 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700802
803 /* ensure hardware is not in low power mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700804 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530805 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300806 case OTG_STATE_B_PERIPHERAL:
807 pr_debug("peripheral bus resume\n");
808 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
809 break;
810 clear_bit(A_BUS_SUSPEND, &motg->inputs);
811 queue_work(system_nrt_wq, &motg->sm_work);
812 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530813 default:
814 break;
815 }
816 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530817 return 0;
818}
819
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530820#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530821#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
822
823#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530824static int msm_otg_suspend(struct msm_otg *motg)
825{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200826 struct usb_phy *phy = &motg->phy;
827 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530828 struct msm_otg_platform_data *pdata = motg->pdata;
829 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300830 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530831 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800832 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530833 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530834
835 if (atomic_read(&motg->in_lpm))
836 return 0;
837
838 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530839 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
840 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700841 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300842 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
843 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530844 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700845
846 /* charging detection in progress due to cable plug-in */
847 if (test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
848 !dcp) {
849 enable_irq(motg->irq);
850 return -EBUSY;
851 }
852
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530853 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530854 * Chipidea 45-nm PHY suspend sequence:
855 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530856 * Interrupt Latch Register auto-clear feature is not present
857 * in all PHY versions. Latch register is clear on read type.
858 * Clear latch register to avoid spurious wakeup from
859 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530860 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530861 * PHY comparators are disabled when PHY enters into low power
862 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
863 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
864 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530865 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530866 * PLL is not turned off when PHY enters into low power mode (LPM).
867 * Disable PLL for maximum power savings.
868 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530869
870 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200871 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530872 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200873 ulpi_write(phy, 0x01, 0x30);
874 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530875 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530876
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530878 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530879 * PHY may take some time or even fail to enter into low power
880 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
881 * in failure case.
882 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530883 portsc = readl_relaxed(USB_PORTSC);
884 if (!(portsc & PORTSC_PHCD)) {
885 writel_relaxed(portsc | PORTSC_PHCD,
886 USB_PORTSC);
887 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
888 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
889 break;
890 udelay(1);
891 cnt++;
892 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530893 }
894
895 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200896 dev_err(phy->dev, "Unable to suspend PHY\n");
897 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530898 enable_irq(motg->irq);
899 return -ETIMEDOUT;
900 }
901
902 /*
903 * PHY has capability to generate interrupt asynchronously in low
904 * power mode (LPM). This interrupt is level triggered. So USB IRQ
905 * line must be disabled till async interrupt enable bit is cleared
906 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
907 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530908 *
909 * PHY retention mode is disallowed while entering to LPM with wall
910 * charger connected. But PHY is put into suspend mode. Hence
911 * enable asynchronous interrupt to detect charger disconnection when
912 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530913 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530914 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300915 if (host_bus_suspend || device_bus_suspend ||
916 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530917 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
918 else
919 cmd_val |= ULPI_STP_CTRL;
920 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530921
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530922 /*
923 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
924 * PHY retention and collapse can not happen with VDP_SRC enabled.
925 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300926 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
927 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200928 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530929 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +0200930 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530931 if ((motg->pdata->mode == USB_OTG) ||
932 (motg->pdata->mode == USB_HOST))
933 phy_ctrl_val |= (PHY_IDHV_INTEN |
934 PHY_OTGSESSVLDHV_INTEN);
935 else
936 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
937 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530938
Amit Blay58b31472011-11-18 09:39:39 +0200939 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530941 }
942
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943 /* Ensure that above operation is completed before turning off clocks */
944 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300945 /* Consider clocks on workaround flag only in case of bus suspend */
946 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
947 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
948 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300949 clk_disable_unprepare(motg->pclk);
950 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300951 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300952 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530953
Anji jonnala7da3f262011-12-02 17:22:14 -0800954 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530955 if (!host_bus_suspend) {
956 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
957 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700958 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530959 "TCXO D0 buffer%d\n", __func__, ret);
960 else
961 motg->lpm_flags |= XO_SHUTDOWN;
962 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800963
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530964 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
965 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +0200966 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +0200968 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
969 !host_bus_suspend && !device_bus_suspend && !dcp) {
970 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
971 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530972 }
973
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530974 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700975 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530976 msm_hsusb_mhl_switch_enable(motg, 0);
977 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978
Steve Mucklef132c6c2012-06-06 18:30:57 -0700979 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530980 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700981 if (motg->async_irq)
982 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700983 if (motg->pdata->pmic_id_irq)
984 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700985 if (pdata->otg_control == OTG_PHY_CONTROL &&
986 pdata->mpm_otgsessvld_int)
987 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530989 if (bus)
990 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
991
992 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -0700993 /* Enable ASYNC IRQ (if present) during LPM */
994 if (motg->async_irq)
995 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530996 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530998
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200999 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301000
1001 return 0;
1002}
1003
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301004static int msm_otg_resume(struct msm_otg *motg)
1005{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001006 struct usb_phy *phy = &motg->phy;
1007 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001008 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301009 int cnt = 0;
1010 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001011 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001012 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301013
1014 if (!atomic_read(&motg->in_lpm))
1015 return 0;
1016
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301018
Anji jonnala7da3f262011-12-02 17:22:14 -08001019 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301020 if (motg->lpm_flags & XO_SHUTDOWN) {
1021 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1022 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -07001023 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301024 "TCXO D0 buffer%d\n", __func__, ret);
1025 motg->lpm_flags &= ~XO_SHUTDOWN;
1026 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301027
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001028 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001029 clk_prepare_enable(motg->core_clk);
1030 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001031 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001032 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301033
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001035 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001036 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001037 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1038 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1039 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001040 }
1041
1042 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301043 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301044 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001045 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1046 phy_ctrl_val |= PHY_RETEN;
1047 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1048 /* Disable PHY HV interrupts */
1049 phy_ctrl_val &=
1050 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
1051 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301053 }
1054
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301055 temp = readl(USB_USBCMD);
1056 temp &= ~ASYNC_INTR_CTRL;
1057 temp &= ~ULPI_STP_CTRL;
1058 writel(temp, USB_USBCMD);
1059
1060 /*
1061 * PHY comes out of low power mode (LPM) in case of wakeup
1062 * from asynchronous interrupt.
1063 */
1064 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1065 goto skip_phy_resume;
1066
1067 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1068 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1069 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1070 break;
1071 udelay(1);
1072 cnt++;
1073 }
1074
1075 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1076 /*
1077 * This is a fatal error. Reset the link and
1078 * PHY. USB state can not be restored. Re-insertion
1079 * of USB cable is the only way to get USB working.
1080 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001081 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301082 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001083 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301084 }
1085
1086skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001087 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301088 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001089 if (motg->async_irq)
1090 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 if (motg->pdata->pmic_id_irq)
1092 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001093 if (pdata->otg_control == OTG_PHY_CONTROL &&
1094 pdata->mpm_otgsessvld_int)
1095 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301097 if (bus)
1098 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1099
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301100 atomic_set(&motg->in_lpm, 0);
1101
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301102 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001103 /* Match the disable_irq call from ISR */
1104 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301105 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301106 }
1107
Manu Gautamf8c45642012-08-10 10:20:56 -07001108 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1109 if (motg->async_irq)
1110 disable_irq(motg->async_irq);
1111
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001112 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301113
1114 return 0;
1115}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301116#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301117
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001118static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001119{
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001120 if (!psy)
1121 goto psy_not_supported;
1122
1123 if (host_mode)
1124 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1125 else
1126 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1127
1128psy_not_supported:
1129 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1130 return -ENXIO;
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001131}
1132
David Keitel081a3e22012-04-18 12:37:07 -07001133static int msm_otg_notify_chg_type(struct msm_otg *motg)
1134{
1135 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001136
David Keitel081a3e22012-04-18 12:37:07 -07001137 /*
1138 * TODO
1139 * Unify OTG driver charger types and power supply charger types
1140 */
1141 if (charger_type == motg->chg_type)
1142 return 0;
1143
1144 if (motg->chg_type == USB_SDP_CHARGER)
1145 charger_type = POWER_SUPPLY_TYPE_USB;
1146 else if (motg->chg_type == USB_CDP_CHARGER)
1147 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301148 else if (motg->chg_type == USB_DCP_CHARGER ||
1149 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001150 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1151 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1152 motg->chg_type == USB_ACA_A_CHARGER ||
1153 motg->chg_type == USB_ACA_B_CHARGER ||
1154 motg->chg_type == USB_ACA_C_CHARGER))
1155 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1156 else
1157 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1158
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001159 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001160 pr_err("No USB power supply registered!\n");
1161 return -EINVAL;
1162 }
1163
1164 pr_debug("setting usb power supply type %d\n", charger_type);
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001165 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001166 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001167}
1168
Amit Blay0f7edf72012-01-15 10:11:27 +02001169static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1170{
Amit Blay0f7edf72012-01-15 10:11:27 +02001171
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001172 if (!psy)
1173 goto psy_not_supported;
Amit Blay0f7edf72012-01-15 10:11:27 +02001174
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001175 if (motg->cur_power == 0 && mA > 0) {
David Keitelf5c5d602012-08-17 16:25:24 -07001176 /* Enable charging */
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001177 if (power_supply_set_online(psy, true))
1178 goto psy_not_supported;
1179 } else if (motg->cur_power > 0 && mA == 0) {
David Keitelf5c5d602012-08-17 16:25:24 -07001180 /* Disable charging */
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001181 if (power_supply_set_online(psy, false))
1182 goto psy_not_supported;
1183 return 0;
David Keitelf5c5d602012-08-17 16:25:24 -07001184 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001185 /* Set max current limit */
1186 if (power_supply_set_current_limit(psy, 1000*mA))
1187 goto psy_not_supported;
David Keitelf5c5d602012-08-17 16:25:24 -07001188
Amit Blay0f7edf72012-01-15 10:11:27 +02001189 return 0;
1190
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001191psy_not_supported:
1192 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001193 return -ENXIO;
1194}
1195
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301196static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1197{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001198 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301199
1200 if (g && g->is_a_peripheral)
1201 return;
1202
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301203 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1204 motg->chg_type == USB_ACA_A_CHARGER ||
1205 motg->chg_type == USB_ACA_B_CHARGER ||
1206 motg->chg_type == USB_ACA_C_CHARGER) &&
1207 mA > IDEV_ACA_CHG_LIMIT)
1208 mA = IDEV_ACA_CHG_LIMIT;
1209
David Keitel081a3e22012-04-18 12:37:07 -07001210 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001211 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001212 "Failed notifying %d charger type to PMIC\n",
1213 motg->chg_type);
1214
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301215 if (motg->cur_power == mA)
1216 return;
1217
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001218 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001219
1220 /*
1221 * Use Power Supply API if supported, otherwise fallback
1222 * to legacy pm8921 API.
1223 */
1224 if (msm_otg_notify_power_supply(motg, mA))
1225 pm8921_charger_vbus_draw(mA);
1226
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301227 motg->cur_power = mA;
1228}
1229
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001230static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301231{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001232 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301233
1234 /*
1235 * Gadget driver uses set_power method to notify about the
1236 * available current based on suspend/configured states.
1237 *
1238 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1239 * states when CDP/ACA is connected.
1240 */
1241 if (motg->chg_type == USB_SDP_CHARGER)
1242 msm_otg_notify_charger(motg, mA);
1243
1244 return 0;
1245}
1246
Steve Mucklef132c6c2012-06-06 18:30:57 -07001247static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301248{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001249 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301250 struct msm_otg_platform_data *pdata = motg->pdata;
1251 struct usb_hcd *hcd;
1252
1253 if (!otg->host)
1254 return;
1255
1256 hcd = bus_to_hcd(otg->host);
1257
1258 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001259 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301260
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301261 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001262 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301263 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1264
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301265 /*
1266 * Some boards have a switch cotrolled by gpio
1267 * to enable/disable internal HUB. Enable internal
1268 * HUB before kicking the host.
1269 */
1270 if (pdata->setup_gpio)
1271 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301272 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301273 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001274 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301275
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301276 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301277 /* HCD core reset all bits of PORTSC. select ULPI phy */
1278 writel_relaxed(0x80000000, USB_PORTSC);
1279
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301280 if (pdata->setup_gpio)
1281 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301282
1283 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001284 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301285 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301286 }
1287}
1288
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289static int msm_otg_usbdev_notify(struct notifier_block *self,
1290 unsigned long action, void *priv)
1291{
1292 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001293 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301294 struct usb_device *udev = priv;
1295
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301296 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1297 goto out;
1298
Steve Mucklef132c6c2012-06-06 18:30:57 -07001299 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301300 goto out;
1301 /*
1302 * Interested in devices connected directly to the root hub.
1303 * ACA dock can supply IDEV_CHG irrespective devices connected
1304 * on the accessory port.
1305 */
1306 if (!udev->parent || udev->parent->parent ||
1307 motg->chg_type == USB_ACA_DOCK_CHARGER)
1308 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309
1310 switch (action) {
1311 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301312 if (aca_enabled())
1313 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001314 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301315 pr_debug("B_CONN set\n");
1316 set_bit(B_CONN, &motg->inputs);
1317 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001318 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301319 /*
1320 * OTG PET: A-device must end session within
1321 * 10 sec after PET enumeration.
1322 */
1323 if (udev->quirks & USB_QUIRK_OTG_PET)
1324 msm_otg_start_timer(motg, TA_TST_MAINT,
1325 A_TST_MAINT);
1326 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301327 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001328 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329 if (udev->actconfig)
1330 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1331 else
1332 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001333 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301334 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301335 break;
1336 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001337 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1338 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301339 pr_debug("B_CONN clear\n");
1340 clear_bit(B_CONN, &motg->inputs);
1341 /*
1342 * OTG PET: A-device must end session after
1343 * PET disconnection if it is enumerated
1344 * with bcdDevice[0] = 1. USB core sets
1345 * bus->otg_vbus_off for us. clear it here.
1346 */
1347 if (udev->bus->otg_vbus_off) {
1348 udev->bus->otg_vbus_off = 0;
1349 set_bit(A_BUS_DROP, &motg->inputs);
1350 }
1351 queue_work(system_nrt_wq, &motg->sm_work);
1352 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001353 default:
1354 break;
1355 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301356 if (test_bit(ID_A, &motg->inputs))
1357 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1358 motg->mA_port);
1359out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001360 return NOTIFY_OK;
1361}
1362
Mayank Ranae3926882011-12-26 09:47:54 +05301363static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1364{
1365 int ret;
1366 static bool vbus_is_on;
1367
1368 if (vbus_is_on == on)
1369 return;
1370
1371 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301372 ret = motg->pdata->vbus_power(on);
1373 if (!ret)
1374 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301375 return;
1376 }
1377
1378 if (!vbus_otg) {
1379 pr_err("vbus_otg is NULL.");
1380 return;
1381 }
1382
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001383 /*
1384 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001385 * from usb before turning on the boost.
1386 * if exiting host mode disable the boost before enabling to draw
1387 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001388 */
Mayank Ranae3926882011-12-26 09:47:54 +05301389 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001390 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301391 ret = regulator_enable(vbus_otg);
1392 if (ret) {
1393 pr_err("unable to enable vbus_otg\n");
1394 return;
1395 }
1396 vbus_is_on = true;
1397 } else {
1398 ret = regulator_disable(vbus_otg);
1399 if (ret) {
1400 pr_err("unable to disable vbus_otg\n");
1401 return;
1402 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001403 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301404 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301405 }
1406}
1407
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001408static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301409{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001410 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301411 struct usb_hcd *hcd;
1412
1413 /*
1414 * Fail host registration if this board can support
1415 * only peripheral configuration.
1416 */
1417 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001418 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301419 return -ENODEV;
1420 }
1421
Mayank Ranae3926882011-12-26 09:47:54 +05301422 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001423 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301424 if (IS_ERR(vbus_otg)) {
1425 pr_err("Unable to get vbus_otg\n");
1426 return -ENODEV;
1427 }
1428 }
1429
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301430 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001431 if (otg->phy->state == OTG_STATE_A_HOST) {
1432 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301434 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301435 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301436 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001437 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301438 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301439 } else {
1440 otg->host = NULL;
1441 }
1442
1443 return 0;
1444 }
1445
1446 hcd = bus_to_hcd(host);
1447 hcd->power_budget = motg->pdata->power_budget;
1448
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301449#ifdef CONFIG_USB_OTG
1450 host->otg_port = 1;
1451#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001452 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1453 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301454 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001455 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301456
1457 /*
1458 * Kick the state machine work, if peripheral is not supported
1459 * or peripheral is already registered with us.
1460 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301461 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001462 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301463 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301464 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301465
1466 return 0;
1467}
1468
Steve Mucklef132c6c2012-06-06 18:30:57 -07001469static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301470{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301471 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001472 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301473 struct msm_otg_platform_data *pdata = motg->pdata;
1474
1475 if (!otg->gadget)
1476 return;
1477
1478 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001479 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301480 /*
1481 * Some boards have a switch cotrolled by gpio
1482 * to enable/disable internal HUB. Disable internal
1483 * HUB before kicking the gadget.
1484 */
1485 if (pdata->setup_gpio)
1486 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001487
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301488 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301489 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301490 ret = msm_bus_scale_client_update_request(
1491 motg->bus_perf_client, 1);
1492 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001493 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301494 "bus bandwidth %d\n", __func__, ret);
1495 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301496 usb_gadget_vbus_connect(otg->gadget);
1497 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001498 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301499 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301500 /* Configure BUS performance parameters to default */
1501 if (motg->bus_perf_client) {
1502 ret = msm_bus_scale_client_update_request(
1503 motg->bus_perf_client, 0);
1504 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001505 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301506 "for bus bw %d\n", __func__, ret);
1507 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301508 if (pdata->setup_gpio)
1509 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1510 }
1511
1512}
1513
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001514static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301515 struct usb_gadget *gadget)
1516{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001517 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301518
1519 /*
1520 * Fail peripheral registration if this board can support
1521 * only host configuration.
1522 */
1523 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001524 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301525 return -ENODEV;
1526 }
1527
1528 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001529 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1530 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301531 msm_otg_start_peripheral(otg, 0);
1532 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001533 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301534 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301535 } else {
1536 otg->gadget = NULL;
1537 }
1538
1539 return 0;
1540 }
1541 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001542 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301543
1544 /*
1545 * Kick the state machine work, if host is not supported
1546 * or host is already registered with us.
1547 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301548 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001549 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301550 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301551 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301552
1553 return 0;
1554}
1555
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301556static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1557 void (*callback)(int on))
1558{
1559 struct usb_phy *phy = &motg->phy;
1560 int ret;
1561
Manoj Raoa7bddd12012-08-27 20:36:45 -07001562 if (!motg->pdata->mhl_enable) {
1563 dev_dbg(phy->dev, "MHL feature not enabled\n");
1564 return -ENODEV;
1565 }
1566
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301567 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1568 !motg->pdata->pmic_id_irq) {
1569 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1570 return -ENODEV;
1571 }
1572
1573 if (!motg->pdata->mhl_dev_name) {
1574 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1575 return -ENODEV;
1576 }
1577
1578 if (callback)
1579 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1580 callback);
1581 else
1582 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1583
1584 if (ret)
1585 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1586 motg->pdata->mhl_dev_name, ret);
1587 else
1588 motg->mhl_enabled = true;
1589
1590 return ret;
1591}
1592
1593static void msm_otg_mhl_notify_online(int on)
1594{
1595 struct msm_otg *motg = the_msm_otg;
1596 struct usb_phy *phy = &motg->phy;
1597 bool queue = false;
1598
1599 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1600
1601 if (on) {
1602 set_bit(MHL, &motg->inputs);
1603 } else {
1604 clear_bit(MHL, &motg->inputs);
1605 queue = true;
1606 }
1607
1608 if (queue && phy->state != OTG_STATE_UNDEFINED)
1609 schedule_work(&motg->sm_work);
1610}
1611
1612static bool msm_otg_is_mhl(struct msm_otg *motg)
1613{
1614 struct usb_phy *phy = &motg->phy;
1615 int is_mhl, ret;
1616
1617 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1618 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1619 /*
1620 * MHL driver calls our callback saying that MHL connected
1621 * if RID_GND is detected. But at later part of discovery
1622 * it may figure out MHL is not connected and returns
1623 * false. Hence clear MHL input here.
1624 */
1625 clear_bit(MHL, &motg->inputs);
1626 dev_dbg(phy->dev, "MHL device not found\n");
1627 return false;
1628 }
1629
1630 set_bit(MHL, &motg->inputs);
1631 dev_dbg(phy->dev, "MHL device found\n");
1632 return true;
1633}
1634
1635static bool msm_chg_mhl_detect(struct msm_otg *motg)
1636{
1637 bool ret, id;
1638 unsigned long flags;
1639
1640 if (!motg->mhl_enabled)
1641 return false;
1642
1643 local_irq_save(flags);
1644 id = irq_read_line(motg->pdata->pmic_id_irq);
1645 local_irq_restore(flags);
1646
1647 if (id)
1648 return false;
1649
1650 mhl_det_in_progress = true;
1651 ret = msm_otg_is_mhl(motg);
1652 mhl_det_in_progress = false;
1653
1654 return ret;
1655}
1656
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301657static void msm_otg_chg_check_timer_func(unsigned long data)
1658{
1659 struct msm_otg *motg = (struct msm_otg *) data;
1660 struct usb_otg *otg = motg->phy.otg;
1661
1662 if (atomic_read(&motg->in_lpm) ||
1663 !test_bit(B_SESS_VLD, &motg->inputs) ||
1664 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1665 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1666 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1667 return;
1668 }
1669
1670 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1671 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1672 set_bit(B_FALSE_SDP, &motg->inputs);
1673 queue_work(system_nrt_wq, &motg->sm_work);
1674 }
1675}
1676
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001677static bool msm_chg_aca_detect(struct msm_otg *motg)
1678{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001679 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001680 u32 int_sts;
1681 bool ret = false;
1682
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301683 if (!aca_enabled())
1684 goto out;
1685
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001686 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1687 goto out;
1688
Steve Mucklef132c6c2012-06-06 18:30:57 -07001689 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001690 switch (int_sts & 0x1C) {
1691 case 0x08:
1692 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001693 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001694 motg->chg_type = USB_ACA_A_CHARGER;
1695 motg->chg_state = USB_CHG_STATE_DETECTED;
1696 clear_bit(ID_B, &motg->inputs);
1697 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301698 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001699 ret = true;
1700 }
1701 break;
1702 case 0x0C:
1703 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001704 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001705 motg->chg_type = USB_ACA_B_CHARGER;
1706 motg->chg_state = USB_CHG_STATE_DETECTED;
1707 clear_bit(ID_A, &motg->inputs);
1708 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301709 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001710 ret = true;
1711 }
1712 break;
1713 case 0x10:
1714 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001715 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001716 motg->chg_type = USB_ACA_C_CHARGER;
1717 motg->chg_state = USB_CHG_STATE_DETECTED;
1718 clear_bit(ID_A, &motg->inputs);
1719 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301720 set_bit(ID, &motg->inputs);
1721 ret = true;
1722 }
1723 break;
1724 case 0x04:
1725 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001726 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301727 motg->chg_type = USB_INVALID_CHARGER;
1728 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1729 clear_bit(ID_A, &motg->inputs);
1730 clear_bit(ID_B, &motg->inputs);
1731 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001732 ret = true;
1733 }
1734 break;
1735 default:
1736 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1737 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301738 test_and_clear_bit(ID_C, &motg->inputs) |
1739 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001740 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001741 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742 motg->chg_type = USB_INVALID_CHARGER;
1743 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1744 }
1745 }
1746out:
1747 return ret;
1748}
1749
1750static void msm_chg_enable_aca_det(struct msm_otg *motg)
1751{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001752 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001753
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301754 if (!aca_enabled())
1755 return;
1756
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001757 switch (motg->pdata->phy_type) {
1758 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301759 /* Disable ID_GND in link and PHY */
1760 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1761 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001762 ulpi_write(phy, 0x01, 0x0C);
1763 ulpi_write(phy, 0x10, 0x0F);
1764 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301765 /* Disable PMIC ID pull-up */
1766 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301767 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001768 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301769 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001770 break;
1771 default:
1772 break;
1773 }
1774}
1775
1776static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1777{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001778 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001779
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301780 if (!aca_enabled())
1781 return;
1782
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001783 switch (motg->pdata->phy_type) {
1784 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301785 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001786 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301787 break;
1788 default:
1789 break;
1790 }
1791}
1792
1793static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1794{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001795 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301796
1797 if (!aca_enabled())
1798 return;
1799
1800 switch (motg->pdata->phy_type) {
1801 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001802 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001803 break;
1804 default:
1805 break;
1806 }
1807}
1808
1809static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1810{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001811 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001812 bool ret = false;
1813
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301814 if (!aca_enabled())
1815 return ret;
1816
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001817 switch (motg->pdata->phy_type) {
1818 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001819 if (ulpi_read(phy, 0x91) & 1) {
1820 dev_dbg(phy->dev, "RID change\n");
1821 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001822 ret = msm_chg_aca_detect(motg);
1823 }
1824 default:
1825 break;
1826 }
1827 return ret;
1828}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301829
1830static void msm_otg_id_timer_func(unsigned long data)
1831{
1832 struct msm_otg *motg = (struct msm_otg *) data;
1833
1834 if (!aca_enabled())
1835 return;
1836
1837 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001838 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301839 return;
1840 }
1841
Steve Mucklef132c6c2012-06-06 18:30:57 -07001842 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301843 goto out;
1844
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301845 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001846 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301847 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301848 }
1849
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301850out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301851 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1852 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1853}
1854
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301855static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1856{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001857 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301858 u32 chg_det;
1859 bool ret = false;
1860
1861 switch (motg->pdata->phy_type) {
1862 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001863 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301864 ret = chg_det & (1 << 4);
1865 break;
1866 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001867 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301868 ret = chg_det & 1;
1869 break;
1870 default:
1871 break;
1872 }
1873 return ret;
1874}
1875
1876static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1877{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001878 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301879 u32 chg_det;
1880
1881 switch (motg->pdata->phy_type) {
1882 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001883 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301884 /* Turn off charger block */
1885 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001886 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301887 udelay(20);
1888 /* control chg block via ULPI */
1889 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001890 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301891 /* put it in host mode for enabling D- source */
1892 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001893 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301894 /* Turn on chg detect block */
1895 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001896 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301897 udelay(20);
1898 /* enable chg detection */
1899 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001900 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301901 break;
1902 case SNPS_28NM_INTEGRATED_PHY:
1903 /*
1904 * Configure DM as current source, DP as current sink
1905 * and enable battery charging comparators.
1906 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001907 ulpi_write(phy, 0x8, 0x85);
1908 ulpi_write(phy, 0x2, 0x85);
1909 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301910 break;
1911 default:
1912 break;
1913 }
1914}
1915
1916static bool msm_chg_check_primary_det(struct msm_otg *motg)
1917{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001918 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301919 u32 chg_det;
1920 bool ret = false;
1921
1922 switch (motg->pdata->phy_type) {
1923 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001924 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301925 ret = chg_det & (1 << 4);
1926 break;
1927 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001928 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301929 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301930 /* Turn off VDP_SRC */
1931 ulpi_write(phy, 0x3, 0x86);
1932 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301933 break;
1934 default:
1935 break;
1936 }
1937 return ret;
1938}
1939
1940static void msm_chg_enable_primary_det(struct msm_otg *motg)
1941{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001942 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301943 u32 chg_det;
1944
1945 switch (motg->pdata->phy_type) {
1946 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001947 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301948 /* enable chg detection */
1949 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001950 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301951 break;
1952 case SNPS_28NM_INTEGRATED_PHY:
1953 /*
1954 * Configure DP as current source, DM as current sink
1955 * and enable battery charging comparators.
1956 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001957 ulpi_write(phy, 0x2, 0x85);
1958 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301959 break;
1960 default:
1961 break;
1962 }
1963}
1964
1965static bool msm_chg_check_dcd(struct msm_otg *motg)
1966{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001967 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301968 u32 line_state;
1969 bool ret = false;
1970
1971 switch (motg->pdata->phy_type) {
1972 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001973 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301974 ret = !(line_state & 1);
1975 break;
1976 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001977 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301978 ret = line_state & 2;
1979 break;
1980 default:
1981 break;
1982 }
1983 return ret;
1984}
1985
1986static void msm_chg_disable_dcd(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
1991 switch (motg->pdata->phy_type) {
1992 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001993 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301994 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001995 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301996 break;
1997 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001998 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301999 break;
2000 default:
2001 break;
2002 }
2003}
2004
2005static void msm_chg_enable_dcd(struct msm_otg *motg)
2006{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002007 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302008 u32 chg_det;
2009
2010 switch (motg->pdata->phy_type) {
2011 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002012 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302013 /* Turn on D+ current source */
2014 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002015 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302016 break;
2017 case SNPS_28NM_INTEGRATED_PHY:
2018 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002019 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302020 break;
2021 default:
2022 break;
2023 }
2024}
2025
2026static void msm_chg_block_on(struct msm_otg *motg)
2027{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002028 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302029 u32 func_ctrl, chg_det;
2030
2031 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002032 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302033 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2034 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002035 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302036
2037 switch (motg->pdata->phy_type) {
2038 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002039 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302040 /* control chg block via ULPI */
2041 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002042 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302043 /* Turn on chg detect block */
2044 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002045 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302046 udelay(20);
2047 break;
2048 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302049 /* disable DP and DM pull down resistors */
2050 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302051 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002052 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302053 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002054 ulpi_write(phy, 0x1F, 0x92);
2055 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302056 udelay(100);
2057 break;
2058 default:
2059 break;
2060 }
2061}
2062
2063static void msm_chg_block_off(struct msm_otg *motg)
2064{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002065 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066 u32 func_ctrl, chg_det;
2067
2068 switch (motg->pdata->phy_type) {
2069 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002070 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302071 /* Turn off charger block */
2072 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002073 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302074 break;
2075 case SNPS_28NM_INTEGRATED_PHY:
2076 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002077 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302078 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002079 ulpi_write(phy, 0x1F, 0x92);
2080 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302081 break;
2082 default:
2083 break;
2084 }
2085
2086 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002087 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302088 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2089 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002090 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302091}
2092
Anji jonnalad270e2d2011-08-09 11:28:32 +05302093static const char *chg_to_string(enum usb_chg_type chg_type)
2094{
2095 switch (chg_type) {
2096 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2097 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2098 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2099 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2100 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2101 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2102 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302103 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302104 default: return "INVALID_CHARGER";
2105 }
2106}
2107
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302108#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
2109#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302110#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2111#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302112static void msm_chg_detect_work(struct work_struct *w)
2113{
2114 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002115 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302116 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302117 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302118 unsigned long delay;
2119
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002120 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302121
2122 if (test_bit(MHL, &motg->inputs)) {
2123 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2124 return;
2125 }
2126
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302127 switch (motg->chg_state) {
2128 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302129 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302130 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002131 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302132 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2133 motg->dcd_retries = 0;
2134 delay = MSM_CHG_DCD_POLL_TIME;
2135 break;
2136 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302137 if (msm_chg_mhl_detect(motg)) {
2138 msm_chg_block_off(motg);
2139 motg->chg_state = USB_CHG_STATE_DETECTED;
2140 motg->chg_type = USB_INVALID_CHARGER;
2141 queue_work(system_nrt_wq, &motg->sm_work);
2142 return;
2143 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002144 is_aca = msm_chg_aca_detect(motg);
2145 if (is_aca) {
2146 /*
2147 * ID_A can be ACA dock too. continue
2148 * primary detection after DCD.
2149 */
2150 if (test_bit(ID_A, &motg->inputs)) {
2151 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2152 } else {
2153 delay = 0;
2154 break;
2155 }
2156 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302157 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302158 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2159 if (is_dcd || tmout) {
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302160 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302161 msm_chg_enable_primary_det(motg);
2162 delay = MSM_CHG_PRIMARY_DET_TIME;
2163 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2164 } else {
2165 delay = MSM_CHG_DCD_POLL_TIME;
2166 }
2167 break;
2168 case USB_CHG_STATE_DCD_DONE:
2169 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302170 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2171 dm_vlgc = line_state & PORTSC_LS_DM;
2172 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302173 if (test_bit(ID_A, &motg->inputs)) {
2174 motg->chg_type = USB_ACA_DOCK_CHARGER;
2175 motg->chg_state = USB_CHG_STATE_DETECTED;
2176 delay = 0;
2177 break;
2178 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302179 if (line_state) { /* DP > VLGC */
2180 motg->chg_type = USB_PROPRIETARY_CHARGER;
2181 motg->chg_state = USB_CHG_STATE_DETECTED;
2182 delay = 0;
2183 } else {
2184 msm_chg_enable_secondary_det(motg);
2185 delay = MSM_CHG_SECONDARY_DET_TIME;
2186 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2187 }
2188 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302189 if (test_bit(ID_A, &motg->inputs)) {
2190 motg->chg_type = USB_ACA_A_CHARGER;
2191 motg->chg_state = USB_CHG_STATE_DETECTED;
2192 delay = 0;
2193 break;
2194 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302195
2196 if (line_state) /* DP > VLGC or/and DM > VLGC */
2197 motg->chg_type = USB_PROPRIETARY_CHARGER;
2198 else
2199 motg->chg_type = USB_SDP_CHARGER;
2200
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302201 motg->chg_state = USB_CHG_STATE_DETECTED;
2202 delay = 0;
2203 }
2204 break;
2205 case USB_CHG_STATE_PRIMARY_DONE:
2206 vout = msm_chg_check_secondary_det(motg);
2207 if (vout)
2208 motg->chg_type = USB_DCP_CHARGER;
2209 else
2210 motg->chg_type = USB_CDP_CHARGER;
2211 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2212 /* fall through */
2213 case USB_CHG_STATE_SECONDARY_DONE:
2214 motg->chg_state = USB_CHG_STATE_DETECTED;
2215 case USB_CHG_STATE_DETECTED:
2216 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002217 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302218 /*
2219 * Spurious interrupt is seen after enabling ACA detection
2220 * due to which charger detection fails in case of PET.
2221 * Add delay of 100 microsec to avoid that.
2222 */
2223 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002224 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002225 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302226 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302227 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302228 return;
2229 default:
2230 return;
2231 }
2232
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302233 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302234}
2235
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302236/*
2237 * We support OTG, Peripheral only and Host only configurations. In case
2238 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2239 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2240 * enabled when switch is controlled by user and default mode is supplied
2241 * by board file, which can be changed by userspace later.
2242 */
2243static void msm_otg_init_sm(struct msm_otg *motg)
2244{
2245 struct msm_otg_platform_data *pdata = motg->pdata;
2246 u32 otgsc = readl(USB_OTGSC);
2247
2248 switch (pdata->mode) {
2249 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002250 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302251 if (pdata->default_mode == USB_HOST) {
2252 clear_bit(ID, &motg->inputs);
2253 } else if (pdata->default_mode == USB_PERIPHERAL) {
2254 set_bit(ID, &motg->inputs);
2255 set_bit(B_SESS_VLD, &motg->inputs);
2256 } else {
2257 set_bit(ID, &motg->inputs);
2258 clear_bit(B_SESS_VLD, &motg->inputs);
2259 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302260 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302261 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302262 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302263 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302264 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302265 set_bit(A_BUS_REQ, &motg->inputs);
2266 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002267 if (otgsc & OTGSC_BSV)
2268 set_bit(B_SESS_VLD, &motg->inputs);
2269 else
2270 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302271 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302272 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07002273 unsigned long flags;
2274 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302275 if (irq_read_line(pdata->pmic_id_irq))
2276 set_bit(ID, &motg->inputs);
2277 else
2278 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07002279 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302280 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302281 /*
2282 * VBUS initial state is reported after PMIC
2283 * driver initialization. Wait for it.
2284 */
2285 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302286 }
2287 break;
2288 case USB_HOST:
2289 clear_bit(ID, &motg->inputs);
2290 break;
2291 case USB_PERIPHERAL:
2292 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302293 if (pdata->otg_control == OTG_PHY_CONTROL) {
2294 if (otgsc & OTGSC_BSV)
2295 set_bit(B_SESS_VLD, &motg->inputs);
2296 else
2297 clear_bit(B_SESS_VLD, &motg->inputs);
2298 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2299 /*
2300 * VBUS initial state is reported after PMIC
2301 * driver initialization. Wait for it.
2302 */
2303 wait_for_completion(&pmic_vbus_init);
2304 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302305 break;
2306 default:
2307 break;
2308 }
2309}
2310
2311static void msm_otg_sm_work(struct work_struct *w)
2312{
2313 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002314 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302315 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302316
Steve Mucklef132c6c2012-06-06 18:30:57 -07002317 pm_runtime_resume(otg->phy->dev);
2318 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002319 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302320 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002321 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302322 msm_otg_init_sm(motg);
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07002323 psy = power_supply_get_by_name("usb");
2324 if (!psy)
2325 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002326 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302327 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2328 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002329 pm_runtime_put_noidle(otg->phy->dev);
2330 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302331 break;
2332 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302333 /* FALL THROUGH */
2334 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302335 if (test_bit(MHL, &motg->inputs)) {
2336 /* allow LPM */
2337 pm_runtime_put_noidle(otg->phy->dev);
2338 pm_runtime_suspend(otg->phy->dev);
2339 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002340 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302341 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302342 if (msm_chg_mhl_detect(motg)) {
2343 work = 1;
2344 break;
2345 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302346 clear_bit(B_BUS_REQ, &motg->inputs);
2347 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002348 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302349 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302350 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302351 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302352 switch (motg->chg_state) {
2353 case USB_CHG_STATE_UNDEFINED:
2354 msm_chg_detect_work(&motg->chg_work.work);
2355 break;
2356 case USB_CHG_STATE_DETECTED:
2357 switch (motg->chg_type) {
2358 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302359 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002360 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302361 /* fall through */
2362 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302363 msm_otg_notify_charger(motg,
2364 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002365 pm_runtime_put_noidle(otg->phy->dev);
2366 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302367 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302368 case USB_ACA_B_CHARGER:
2369 msm_otg_notify_charger(motg,
2370 IDEV_ACA_CHG_MAX);
2371 /*
2372 * (ID_B --> ID_C) PHY_ALT interrupt can
2373 * not be detected in LPM.
2374 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302375 break;
2376 case USB_CDP_CHARGER:
2377 msm_otg_notify_charger(motg,
2378 IDEV_CHG_MAX);
2379 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002380 otg->phy->state =
2381 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302382 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302383 case USB_ACA_C_CHARGER:
2384 msm_otg_notify_charger(motg,
2385 IDEV_ACA_CHG_MAX);
2386 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002387 otg->phy->state =
2388 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302389 break;
2390 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302391 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002392 otg->phy->state =
2393 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302394 mod_timer(&motg->chg_check_timer,
2395 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302396 break;
2397 default:
2398 break;
2399 }
2400 break;
2401 default:
2402 break;
2403 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302404 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2405 pr_debug("b_sess_end && b_bus_req\n");
2406 if (msm_otg_start_srp(otg) < 0) {
2407 clear_bit(B_BUS_REQ, &motg->inputs);
2408 work = 1;
2409 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302410 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002411 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302412 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2413 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302414 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302415 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302416 del_timer_sync(&motg->chg_check_timer);
2417 clear_bit(B_FALSE_SDP, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302418 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302419 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2420 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302421 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002422 msm_otg_reset(otg->phy);
2423 pm_runtime_put_noidle(otg->phy->dev);
2424 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302425 }
2426 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302427 case OTG_STATE_B_SRP_INIT:
2428 if (!test_bit(ID, &motg->inputs) ||
2429 test_bit(ID_A, &motg->inputs) ||
2430 test_bit(ID_C, &motg->inputs) ||
2431 (test_bit(B_SESS_VLD, &motg->inputs) &&
2432 !test_bit(ID_B, &motg->inputs))) {
2433 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2434 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002435 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302436 /*
2437 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2438 * bits after SRP initiation.
2439 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002440 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302441 work = 1;
2442 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2443 pr_debug("b_srp_fail\n");
2444 pr_info("A-device did not respond to SRP\n");
2445 clear_bit(B_BUS_REQ, &motg->inputs);
2446 clear_bit(B_SRP_FAIL, &motg->tmouts);
2447 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002448 ulpi_write(otg->phy, 0x0, 0x98);
2449 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302450 motg->b_last_se0_sess = jiffies;
2451 work = 1;
2452 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302453 break;
2454 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302455 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2456 test_bit(B_FALSE_SDP, &motg->inputs)) {
2457 pr_debug("B_FALSE_SDP\n");
2458 msm_otg_start_peripheral(otg, 0);
2459 motg->chg_type = USB_DCP_CHARGER;
2460 clear_bit(B_FALSE_SDP, &motg->inputs);
2461 otg->phy->state = OTG_STATE_B_IDLE;
2462 work = 1;
2463 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302464 test_bit(ID_A, &motg->inputs) ||
2465 test_bit(ID_B, &motg->inputs) ||
2466 !test_bit(B_SESS_VLD, &motg->inputs)) {
2467 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302468 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2469 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302470 msm_otg_notify_charger(motg, 0);
2471 srp_reqd = otg->gadget->otg_srp_reqd;
2472 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302473 if (test_bit(ID_B, &motg->inputs))
2474 clear_bit(ID_B, &motg->inputs);
2475 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002476 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302477 motg->b_last_se0_sess = jiffies;
2478 if (srp_reqd)
2479 msm_otg_start_timer(motg,
2480 TB_TST_SRP, B_TST_SRP);
2481 else
2482 work = 1;
2483 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2484 otg->gadget->b_hnp_enable &&
2485 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2486 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2487 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2488 /* D+ pullup should not be disconnected within 4msec
2489 * after A device suspends the bus. Otherwise PET will
2490 * fail the compliance test.
2491 */
2492 udelay(1000);
2493 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002494 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302495 /*
2496 * start HCD even before A-device enable
2497 * pull-up to meet HNP timings.
2498 */
2499 otg->host->is_b_host = 1;
2500 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002501 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2502 test_bit(B_SESS_VLD, &motg->inputs)) {
2503 pr_debug("a_bus_suspend && b_sess_vld\n");
2504 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002505 pm_runtime_put_noidle(otg->phy->dev);
2506 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002507 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002508 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302509 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002510 }
2511 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302512 case OTG_STATE_B_WAIT_ACON:
2513 if (!test_bit(ID, &motg->inputs) ||
2514 test_bit(ID_A, &motg->inputs) ||
2515 test_bit(ID_B, &motg->inputs) ||
2516 !test_bit(B_SESS_VLD, &motg->inputs)) {
2517 pr_debug("!id || id_a/b || !b_sess_vld\n");
2518 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302519 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302520 * A-device is physically disconnected during
2521 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302522 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302523 msm_otg_start_host(otg, 0);
2524 otg->host->is_b_host = 0;
2525
2526 clear_bit(B_BUS_REQ, &motg->inputs);
2527 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2528 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002529 otg->phy->state = OTG_STATE_B_IDLE;
2530 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302531 work = 1;
2532 } else if (test_bit(A_CONN, &motg->inputs)) {
2533 pr_debug("a_conn\n");
2534 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002535 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302536 /*
2537 * PET disconnects D+ pullup after reset is generated
2538 * by B device in B_HOST role which is not detected by
2539 * B device. As workaorund , start timer of 300msec
2540 * and stop timer if A device is enumerated else clear
2541 * A_CONN.
2542 */
2543 msm_otg_start_timer(motg, TB_TST_CONFIG,
2544 B_TST_CONFIG);
2545 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2546 pr_debug("b_ase0_brst_tmout\n");
2547 pr_info("B HNP fail:No response from A device\n");
2548 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002549 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302550 otg->host->is_b_host = 0;
2551 clear_bit(B_ASE0_BRST, &motg->tmouts);
2552 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2553 clear_bit(B_BUS_REQ, &motg->inputs);
2554 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002555 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302556 work = 1;
2557 } else if (test_bit(ID_C, &motg->inputs)) {
2558 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2559 }
2560 break;
2561 case OTG_STATE_B_HOST:
2562 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2563 !test_bit(A_CONN, &motg->inputs) ||
2564 !test_bit(B_SESS_VLD, &motg->inputs)) {
2565 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2566 clear_bit(A_CONN, &motg->inputs);
2567 clear_bit(B_BUS_REQ, &motg->inputs);
2568 msm_otg_start_host(otg, 0);
2569 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002570 otg->phy->state = OTG_STATE_B_IDLE;
2571 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302572 work = 1;
2573 } else if (test_bit(ID_C, &motg->inputs)) {
2574 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2575 }
2576 break;
2577 case OTG_STATE_A_IDLE:
2578 otg->default_a = 1;
2579 if (test_bit(ID, &motg->inputs) &&
2580 !test_bit(ID_A, &motg->inputs)) {
2581 pr_debug("id && !id_a\n");
2582 otg->default_a = 0;
2583 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002584 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302585 del_timer_sync(&motg->id_timer);
2586 msm_otg_link_reset(motg);
2587 msm_chg_enable_aca_intr(motg);
2588 msm_otg_notify_charger(motg, 0);
2589 work = 1;
2590 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2591 (test_bit(A_SRP_DET, &motg->inputs) ||
2592 test_bit(A_BUS_REQ, &motg->inputs))) {
2593 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2594
2595 clear_bit(A_SRP_DET, &motg->inputs);
2596 /* Disable SRP detection */
2597 writel_relaxed((readl_relaxed(USB_OTGSC) &
2598 ~OTGSC_INTSTS_MASK) &
2599 ~OTGSC_DPIE, USB_OTGSC);
2600
Steve Mucklef132c6c2012-06-06 18:30:57 -07002601 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302602 /* VBUS should not be supplied before end of SRP pulse
2603 * generated by PET, if not complaince test fail.
2604 */
2605 usleep_range(10000, 12000);
2606 /* ACA: ID_A: Stop charging untill enumeration */
2607 if (test_bit(ID_A, &motg->inputs))
2608 msm_otg_notify_charger(motg, 0);
2609 else
2610 msm_hsusb_vbus_power(motg, 1);
2611 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2612 } else {
2613 pr_debug("No session requested\n");
2614 clear_bit(A_BUS_DROP, &motg->inputs);
2615 if (test_bit(ID_A, &motg->inputs)) {
2616 msm_otg_notify_charger(motg,
2617 IDEV_ACA_CHG_MAX);
2618 } else if (!test_bit(ID, &motg->inputs)) {
2619 msm_otg_notify_charger(motg, 0);
2620 /*
2621 * A-device is not providing power on VBUS.
2622 * Enable SRP detection.
2623 */
2624 writel_relaxed(0x13, USB_USBMODE);
2625 writel_relaxed((readl_relaxed(USB_OTGSC) &
2626 ~OTGSC_INTSTS_MASK) |
2627 OTGSC_DPIE, USB_OTGSC);
2628 mb();
2629 }
2630 }
2631 break;
2632 case OTG_STATE_A_WAIT_VRISE:
2633 if ((test_bit(ID, &motg->inputs) &&
2634 !test_bit(ID_A, &motg->inputs)) ||
2635 test_bit(A_BUS_DROP, &motg->inputs) ||
2636 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2637 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2638 clear_bit(A_BUS_REQ, &motg->inputs);
2639 msm_otg_del_timer(motg);
2640 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002641 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302642 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2643 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2644 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002645 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302646 if (TA_WAIT_BCON > 0)
2647 msm_otg_start_timer(motg, TA_WAIT_BCON,
2648 A_WAIT_BCON);
2649 msm_otg_start_host(otg, 1);
2650 msm_chg_enable_aca_det(motg);
2651 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302652 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302653 if (msm_chg_check_aca_intr(motg))
2654 work = 1;
2655 }
2656 break;
2657 case OTG_STATE_A_WAIT_BCON:
2658 if ((test_bit(ID, &motg->inputs) &&
2659 !test_bit(ID_A, &motg->inputs)) ||
2660 test_bit(A_BUS_DROP, &motg->inputs) ||
2661 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2662 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2663 "a_wait_bcon_tmout\n");
2664 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2665 pr_info("Device No Response\n");
2666 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2667 }
2668 msm_otg_del_timer(motg);
2669 clear_bit(A_BUS_REQ, &motg->inputs);
2670 clear_bit(B_CONN, &motg->inputs);
2671 msm_otg_start_host(otg, 0);
2672 /*
2673 * ACA: ID_A with NO accessory, just the A plug is
2674 * attached to ACA: Use IDCHG_MAX for charging
2675 */
2676 if (test_bit(ID_A, &motg->inputs))
2677 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2678 else
2679 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002680 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302681 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2682 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2683 pr_debug("!a_vbus_vld\n");
2684 clear_bit(B_CONN, &motg->inputs);
2685 msm_otg_del_timer(motg);
2686 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002687 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2688 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302689 } else if (test_bit(ID_A, &motg->inputs)) {
2690 msm_hsusb_vbus_power(motg, 0);
2691 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2692 /*
2693 * If TA_WAIT_BCON is infinite, we don;t
2694 * turn off VBUS. Enter low power mode.
2695 */
2696 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002697 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302698 } else if (!test_bit(ID, &motg->inputs)) {
2699 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302700 }
2701 break;
2702 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302703 if ((test_bit(ID, &motg->inputs) &&
2704 !test_bit(ID_A, &motg->inputs)) ||
2705 test_bit(A_BUS_DROP, &motg->inputs)) {
2706 pr_debug("id_a/b/c || a_bus_drop\n");
2707 clear_bit(B_CONN, &motg->inputs);
2708 clear_bit(A_BUS_REQ, &motg->inputs);
2709 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002710 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302711 msm_otg_start_host(otg, 0);
2712 if (!test_bit(ID_A, &motg->inputs))
2713 msm_hsusb_vbus_power(motg, 0);
2714 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2715 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2716 pr_debug("!a_vbus_vld\n");
2717 clear_bit(B_CONN, &motg->inputs);
2718 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002719 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302720 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002721 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302722 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2723 /*
2724 * a_bus_req is de-asserted when root hub is
2725 * suspended or HNP is in progress.
2726 */
2727 pr_debug("!a_bus_req\n");
2728 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002729 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302730 if (otg->host->b_hnp_enable)
2731 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2732 A_AIDL_BDIS);
2733 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002734 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302735 } else if (!test_bit(B_CONN, &motg->inputs)) {
2736 pr_debug("!b_conn\n");
2737 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002738 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302739 if (TA_WAIT_BCON > 0)
2740 msm_otg_start_timer(motg, TA_WAIT_BCON,
2741 A_WAIT_BCON);
2742 if (msm_chg_check_aca_intr(motg))
2743 work = 1;
2744 } else if (test_bit(ID_A, &motg->inputs)) {
2745 msm_otg_del_timer(motg);
2746 msm_hsusb_vbus_power(motg, 0);
2747 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2748 msm_otg_notify_charger(motg,
2749 IDEV_ACA_CHG_MAX);
2750 else
2751 msm_otg_notify_charger(motg,
2752 IDEV_CHG_MIN - motg->mA_port);
2753 } else if (!test_bit(ID, &motg->inputs)) {
2754 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2755 motg->chg_type = USB_INVALID_CHARGER;
2756 msm_otg_notify_charger(motg, 0);
2757 msm_hsusb_vbus_power(motg, 1);
2758 }
2759 break;
2760 case OTG_STATE_A_SUSPEND:
2761 if ((test_bit(ID, &motg->inputs) &&
2762 !test_bit(ID_A, &motg->inputs)) ||
2763 test_bit(A_BUS_DROP, &motg->inputs) ||
2764 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2765 pr_debug("id_a/b/c || a_bus_drop ||"
2766 "a_aidl_bdis_tmout\n");
2767 msm_otg_del_timer(motg);
2768 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002769 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302770 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002771 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302772 if (!test_bit(ID_A, &motg->inputs))
2773 msm_hsusb_vbus_power(motg, 0);
2774 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2775 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2776 pr_debug("!a_vbus_vld\n");
2777 msm_otg_del_timer(motg);
2778 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002779 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302780 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002781 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302782 } else if (!test_bit(B_CONN, &motg->inputs) &&
2783 otg->host->b_hnp_enable) {
2784 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002785 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302786 msm_otg_host_hnp_enable(otg, 1);
2787 otg->gadget->is_a_peripheral = 1;
2788 msm_otg_start_peripheral(otg, 1);
2789 } else if (!test_bit(B_CONN, &motg->inputs) &&
2790 !otg->host->b_hnp_enable) {
2791 pr_debug("!b_conn && !b_hnp_enable");
2792 /*
2793 * bus request is dropped during suspend.
2794 * acquire again for next device.
2795 */
2796 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002797 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302798 if (TA_WAIT_BCON > 0)
2799 msm_otg_start_timer(motg, TA_WAIT_BCON,
2800 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002801 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302802 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002803 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302804 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002805 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002806 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302807 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302808 }
2809 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302810 case OTG_STATE_A_PERIPHERAL:
2811 if ((test_bit(ID, &motg->inputs) &&
2812 !test_bit(ID_A, &motg->inputs)) ||
2813 test_bit(A_BUS_DROP, &motg->inputs)) {
2814 pr_debug("id _f/b/c || a_bus_drop\n");
2815 /* Clear BIDL_ADIS timer */
2816 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002817 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302818 msm_otg_start_peripheral(otg, 0);
2819 otg->gadget->is_a_peripheral = 0;
2820 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002821 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302822 if (!test_bit(ID_A, &motg->inputs))
2823 msm_hsusb_vbus_power(motg, 0);
2824 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2825 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2826 pr_debug("!a_vbus_vld\n");
2827 /* Clear BIDL_ADIS timer */
2828 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002829 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302830 msm_otg_start_peripheral(otg, 0);
2831 otg->gadget->is_a_peripheral = 0;
2832 msm_otg_start_host(otg, 0);
2833 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2834 pr_debug("a_bidl_adis_tmout\n");
2835 msm_otg_start_peripheral(otg, 0);
2836 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002837 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302838 set_bit(A_BUS_REQ, &motg->inputs);
2839 msm_otg_host_hnp_enable(otg, 0);
2840 if (TA_WAIT_BCON > 0)
2841 msm_otg_start_timer(motg, TA_WAIT_BCON,
2842 A_WAIT_BCON);
2843 } else if (test_bit(ID_A, &motg->inputs)) {
2844 msm_hsusb_vbus_power(motg, 0);
2845 msm_otg_notify_charger(motg,
2846 IDEV_CHG_MIN - motg->mA_port);
2847 } else if (!test_bit(ID, &motg->inputs)) {
2848 msm_otg_notify_charger(motg, 0);
2849 msm_hsusb_vbus_power(motg, 1);
2850 }
2851 break;
2852 case OTG_STATE_A_WAIT_VFALL:
2853 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2854 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002855 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302856 work = 1;
2857 }
2858 break;
2859 case OTG_STATE_A_VBUS_ERR:
2860 if ((test_bit(ID, &motg->inputs) &&
2861 !test_bit(ID_A, &motg->inputs)) ||
2862 test_bit(A_BUS_DROP, &motg->inputs) ||
2863 test_bit(A_CLR_ERR, &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 if (!test_bit(ID_A, &motg->inputs))
2866 msm_hsusb_vbus_power(motg, 0);
2867 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2868 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2869 motg->chg_type = USB_INVALID_CHARGER;
2870 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302871 }
2872 break;
2873 default:
2874 break;
2875 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302876 if (work)
2877 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302878}
2879
Amit Blayd0fe07b2012-09-05 16:42:09 +03002880static void msm_otg_suspend_work(struct work_struct *w)
2881{
2882 struct msm_otg *motg =
2883 container_of(w, struct msm_otg, suspend_work.work);
2884 atomic_set(&motg->suspend_work_pending, 0);
2885 msm_otg_sm_work(&motg->sm_work);
2886}
2887
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302888static irqreturn_t msm_otg_irq(int irq, void *data)
2889{
2890 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002891 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302892 u32 otgsc = 0, usbsts, pc;
2893 bool work = 0;
2894 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302895
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302896 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002897 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302898 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002899 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07002900 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002901 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302902 return IRQ_HANDLED;
2903 }
2904
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002905 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302906 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302907
2908 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302909 return IRQ_NONE;
2910
2911 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302912 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302913 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302914 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302915 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302916 pr_debug("Id clear\n");
2917 /*
2918 * Assert a_bus_req to supply power on
2919 * VBUS when Micro/Mini-A cable is connected
2920 * with out user intervention.
2921 */
2922 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302923 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302924 msm_chg_enable_aca_det(motg);
2925 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302926 writel_relaxed(otgsc, USB_OTGSC);
2927 work = 1;
2928 } else if (otgsc & OTGSC_DPIS) {
2929 pr_debug("DPIS detected\n");
2930 writel_relaxed(otgsc, USB_OTGSC);
2931 set_bit(A_SRP_DET, &motg->inputs);
2932 set_bit(A_BUS_REQ, &motg->inputs);
2933 work = 1;
2934 } else if (otgsc & OTGSC_BSVIS) {
2935 writel_relaxed(otgsc, USB_OTGSC);
2936 /*
2937 * BSV interrupt comes when operating as an A-device
2938 * (VBUS on/off).
2939 * But, handle BSV when charger is removed from ACA in ID_A
2940 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002941 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302942 !test_bit(ID_A, &motg->inputs))
2943 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302944 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302945 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302946 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302947 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302948 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302949 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002950 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2951
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302952 msm_chg_check_aca_intr(motg);
2953 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302954 work = 1;
2955 } else if (usbsts & STS_PCI) {
2956 pc = readl_relaxed(USB_PORTSC);
2957 pr_debug("portsc = %x\n", pc);
2958 ret = IRQ_NONE;
2959 /*
2960 * HCD Acks PCI interrupt. We use this to switch
2961 * between different OTG states.
2962 */
2963 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002964 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302965 case OTG_STATE_A_SUSPEND:
2966 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2967 !(pc & PORTSC_CCS)) {
2968 pr_debug("B_CONN clear\n");
2969 clear_bit(B_CONN, &motg->inputs);
2970 msm_otg_del_timer(motg);
2971 }
2972 break;
2973 case OTG_STATE_A_PERIPHERAL:
2974 /*
2975 * A-peripheral observed activity on bus.
2976 * clear A_BIDL_ADIS timer.
2977 */
2978 msm_otg_del_timer(motg);
2979 work = 0;
2980 break;
2981 case OTG_STATE_B_WAIT_ACON:
2982 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2983 pr_debug("A_CONN set\n");
2984 set_bit(A_CONN, &motg->inputs);
2985 /* Clear ASE0_BRST timer */
2986 msm_otg_del_timer(motg);
2987 }
2988 break;
2989 case OTG_STATE_B_HOST:
2990 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2991 pr_debug("A_CONN clear\n");
2992 clear_bit(A_CONN, &motg->inputs);
2993 msm_otg_del_timer(motg);
2994 }
2995 break;
2996 case OTG_STATE_A_WAIT_BCON:
2997 if (TA_WAIT_BCON < 0)
2998 set_bit(A_BUS_REQ, &motg->inputs);
2999 default:
3000 work = 0;
3001 break;
3002 }
3003 } else if (usbsts & STS_URI) {
3004 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003005 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303006 case OTG_STATE_A_PERIPHERAL:
3007 /*
3008 * A-peripheral observed activity on bus.
3009 * clear A_BIDL_ADIS timer.
3010 */
3011 msm_otg_del_timer(motg);
3012 work = 0;
3013 break;
3014 default:
3015 work = 0;
3016 break;
3017 }
3018 } else if (usbsts & STS_SLI) {
3019 ret = IRQ_NONE;
3020 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003021 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303022 case OTG_STATE_B_PERIPHERAL:
3023 if (otg->gadget->b_hnp_enable) {
3024 set_bit(A_BUS_SUSPEND, &motg->inputs);
3025 set_bit(B_BUS_REQ, &motg->inputs);
3026 work = 1;
3027 }
3028 break;
3029 case OTG_STATE_A_PERIPHERAL:
3030 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3031 A_BIDL_ADIS);
3032 break;
3033 default:
3034 break;
3035 }
3036 } else if ((usbsts & PHY_ALT_INT)) {
3037 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3038 if (msm_chg_check_aca_intr(motg))
3039 work = 1;
3040 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303041 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303042 if (work)
3043 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303044
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303045 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003046}
3047
3048static void msm_otg_set_vbus_state(int online)
3049{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303050 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003051 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303052 struct usb_otg *otg = motg->phy.otg;
3053
3054 /* In A Host Mode, ignore received BSV interrupts */
3055 if (otg->phy->state >= OTG_STATE_A_IDLE)
3056 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003057
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303058 if (online) {
3059 pr_debug("PMIC: BSV set\n");
3060 set_bit(B_SESS_VLD, &motg->inputs);
3061 } else {
3062 pr_debug("PMIC: BSV clear\n");
3063 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303064 }
3065
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303066 if (!init) {
3067 init = true;
3068 complete(&pmic_vbus_init);
3069 pr_debug("PMIC: BSV init complete\n");
3070 return;
3071 }
3072
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303073 if (test_bit(MHL, &motg->inputs) ||
3074 mhl_det_in_progress) {
3075 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3076 return;
3077 }
3078
Jack Pham5ca279b2012-05-14 18:42:54 -07003079 if (atomic_read(&motg->pm_suspended))
3080 motg->sm_work_pending = true;
3081 else
3082 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003083}
3084
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303085static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003086{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303087 struct msm_otg *motg = container_of(w, struct msm_otg,
3088 pmic_id_status_work.work);
3089 int work = 0;
3090 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003091
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303092 local_irq_save(flags);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303093 if (irq_read_line(motg->pdata->pmic_id_irq)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303094 if (!test_and_set_bit(ID, &motg->inputs)) {
3095 pr_debug("PMIC: ID set\n");
3096 work = 1;
3097 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303098 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303099 if (test_and_clear_bit(ID, &motg->inputs)) {
3100 pr_debug("PMIC: ID clear\n");
3101 set_bit(A_BUS_REQ, &motg->inputs);
3102 work = 1;
3103 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303104 }
3105
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303106 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003107 if (atomic_read(&motg->pm_suspended))
3108 motg->sm_work_pending = true;
3109 else
3110 queue_work(system_nrt_wq, &motg->sm_work);
3111 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303112 local_irq_restore(flags);
3113
3114}
3115
3116#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3117static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3118{
3119 struct msm_otg *motg = data;
3120
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303121 if (test_bit(MHL, &motg->inputs) ||
3122 mhl_det_in_progress) {
3123 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3124 return IRQ_HANDLED;
3125 }
3126
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303127 if (!aca_id_turned_on)
3128 /*schedule delayed work for 5msec for ID line state to settle*/
3129 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3130 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003131
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303132 return IRQ_HANDLED;
3133}
3134
3135static int msm_otg_mode_show(struct seq_file *s, void *unused)
3136{
3137 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003138 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303139
Steve Mucklef132c6c2012-06-06 18:30:57 -07003140 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303141 case OTG_STATE_A_HOST:
3142 seq_printf(s, "host\n");
3143 break;
3144 case OTG_STATE_B_PERIPHERAL:
3145 seq_printf(s, "peripheral\n");
3146 break;
3147 default:
3148 seq_printf(s, "none\n");
3149 break;
3150 }
3151
3152 return 0;
3153}
3154
3155static int msm_otg_mode_open(struct inode *inode, struct file *file)
3156{
3157 return single_open(file, msm_otg_mode_show, inode->i_private);
3158}
3159
3160static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3161 size_t count, loff_t *ppos)
3162{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303163 struct seq_file *s = file->private_data;
3164 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303165 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003166 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303167 int status = count;
3168 enum usb_mode_type req_mode;
3169
3170 memset(buf, 0x00, sizeof(buf));
3171
3172 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3173 status = -EFAULT;
3174 goto out;
3175 }
3176
3177 if (!strncmp(buf, "host", 4)) {
3178 req_mode = USB_HOST;
3179 } else if (!strncmp(buf, "peripheral", 10)) {
3180 req_mode = USB_PERIPHERAL;
3181 } else if (!strncmp(buf, "none", 4)) {
3182 req_mode = USB_NONE;
3183 } else {
3184 status = -EINVAL;
3185 goto out;
3186 }
3187
3188 switch (req_mode) {
3189 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003190 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303191 case OTG_STATE_A_HOST:
3192 case OTG_STATE_B_PERIPHERAL:
3193 set_bit(ID, &motg->inputs);
3194 clear_bit(B_SESS_VLD, &motg->inputs);
3195 break;
3196 default:
3197 goto out;
3198 }
3199 break;
3200 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003201 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303202 case OTG_STATE_B_IDLE:
3203 case OTG_STATE_A_HOST:
3204 set_bit(ID, &motg->inputs);
3205 set_bit(B_SESS_VLD, &motg->inputs);
3206 break;
3207 default:
3208 goto out;
3209 }
3210 break;
3211 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003212 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303213 case OTG_STATE_B_IDLE:
3214 case OTG_STATE_B_PERIPHERAL:
3215 clear_bit(ID, &motg->inputs);
3216 break;
3217 default:
3218 goto out;
3219 }
3220 break;
3221 default:
3222 goto out;
3223 }
3224
Steve Mucklef132c6c2012-06-06 18:30:57 -07003225 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303226 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303227out:
3228 return status;
3229}
3230
3231const struct file_operations msm_otg_mode_fops = {
3232 .open = msm_otg_mode_open,
3233 .read = seq_read,
3234 .write = msm_otg_mode_write,
3235 .llseek = seq_lseek,
3236 .release = single_release,
3237};
3238
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303239static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3240{
3241 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003242 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303243
Steve Mucklef132c6c2012-06-06 18:30:57 -07003244 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303245 return 0;
3246}
3247
3248static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3249{
3250 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3251}
3252
3253const struct file_operations msm_otg_state_fops = {
3254 .open = msm_otg_otg_state_open,
3255 .read = seq_read,
3256 .llseek = seq_lseek,
3257 .release = single_release,
3258};
3259
Anji jonnalad270e2d2011-08-09 11:28:32 +05303260static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3261{
3262 struct msm_otg *motg = s->private;
3263
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303264 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303265 return 0;
3266}
3267
3268static int msm_otg_chg_open(struct inode *inode, struct file *file)
3269{
3270 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3271}
3272
3273const struct file_operations msm_otg_chg_fops = {
3274 .open = msm_otg_chg_open,
3275 .read = seq_read,
3276 .llseek = seq_lseek,
3277 .release = single_release,
3278};
3279
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303280static int msm_otg_aca_show(struct seq_file *s, void *unused)
3281{
3282 if (debug_aca_enabled)
3283 seq_printf(s, "enabled\n");
3284 else
3285 seq_printf(s, "disabled\n");
3286
3287 return 0;
3288}
3289
3290static int msm_otg_aca_open(struct inode *inode, struct file *file)
3291{
3292 return single_open(file, msm_otg_aca_show, inode->i_private);
3293}
3294
3295static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3296 size_t count, loff_t *ppos)
3297{
3298 char buf[8];
3299
3300 memset(buf, 0x00, sizeof(buf));
3301
3302 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3303 return -EFAULT;
3304
3305 if (!strncmp(buf, "enable", 6))
3306 debug_aca_enabled = true;
3307 else
3308 debug_aca_enabled = false;
3309
3310 return count;
3311}
3312
3313const struct file_operations msm_otg_aca_fops = {
3314 .open = msm_otg_aca_open,
3315 .read = seq_read,
3316 .write = msm_otg_aca_write,
3317 .llseek = seq_lseek,
3318 .release = single_release,
3319};
3320
Manu Gautam8bdcc592012-03-06 11:26:06 +05303321static int msm_otg_bus_show(struct seq_file *s, void *unused)
3322{
3323 if (debug_bus_voting_enabled)
3324 seq_printf(s, "enabled\n");
3325 else
3326 seq_printf(s, "disabled\n");
3327
3328 return 0;
3329}
3330
3331static int msm_otg_bus_open(struct inode *inode, struct file *file)
3332{
3333 return single_open(file, msm_otg_bus_show, inode->i_private);
3334}
3335
3336static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3337 size_t count, loff_t *ppos)
3338{
3339 char buf[8];
3340 int ret;
3341 struct seq_file *s = file->private_data;
3342 struct msm_otg *motg = s->private;
3343
3344 memset(buf, 0x00, sizeof(buf));
3345
3346 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3347 return -EFAULT;
3348
3349 if (!strncmp(buf, "enable", 6)) {
3350 /* Do not vote here. Let OTG statemachine decide when to vote */
3351 debug_bus_voting_enabled = true;
3352 } else {
3353 debug_bus_voting_enabled = false;
3354 if (motg->bus_perf_client) {
3355 ret = msm_bus_scale_client_update_request(
3356 motg->bus_perf_client, 0);
3357 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003358 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303359 "for bus bw %d\n", __func__, ret);
3360 }
3361 }
3362
3363 return count;
3364}
3365
3366const struct file_operations msm_otg_bus_fops = {
3367 .open = msm_otg_bus_open,
3368 .read = seq_read,
3369 .write = msm_otg_bus_write,
3370 .llseek = seq_lseek,
3371 .release = single_release,
3372};
3373
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303374static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303375
3376static int msm_otg_debugfs_init(struct msm_otg *motg)
3377{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303378 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303379
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303380 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3381
3382 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3383 return -ENODEV;
3384
Anji jonnalad270e2d2011-08-09 11:28:32 +05303385 if (motg->pdata->mode == USB_OTG &&
3386 motg->pdata->otg_control == OTG_USER_CONTROL) {
3387
Manu Gautam8bdcc592012-03-06 11:26:06 +05303388 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303389 S_IWUSR, msm_otg_dbg_root, motg,
3390 &msm_otg_mode_fops);
3391
Manu Gautam8bdcc592012-03-06 11:26:06 +05303392 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303393 debugfs_remove(msm_otg_dbg_root);
3394 msm_otg_dbg_root = NULL;
3395 return -ENODEV;
3396 }
3397 }
3398
Manu Gautam8bdcc592012-03-06 11:26:06 +05303399 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303400 msm_otg_dbg_root, motg,
3401 &msm_otg_chg_fops);
3402
Manu Gautam8bdcc592012-03-06 11:26:06 +05303403 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303404 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303405 return -ENODEV;
3406 }
3407
Manu Gautam8bdcc592012-03-06 11:26:06 +05303408 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303409 msm_otg_dbg_root, motg,
3410 &msm_otg_aca_fops);
3411
Manu Gautam8bdcc592012-03-06 11:26:06 +05303412 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303413 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303414 return -ENODEV;
3415 }
3416
Manu Gautam8bdcc592012-03-06 11:26:06 +05303417 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3418 msm_otg_dbg_root, motg,
3419 &msm_otg_bus_fops);
3420
3421 if (!msm_otg_dentry) {
3422 debugfs_remove_recursive(msm_otg_dbg_root);
3423 return -ENODEV;
3424 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303425
3426 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3427 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3428
3429 if (!msm_otg_dentry) {
3430 debugfs_remove_recursive(msm_otg_dbg_root);
3431 return -ENODEV;
3432 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303433 return 0;
3434}
3435
3436static void msm_otg_debugfs_cleanup(void)
3437{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303438 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303439}
3440
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303441static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3442static struct platform_device *msm_otg_add_pdev(
3443 struct platform_device *ofdev, const char *name)
3444{
3445 struct platform_device *pdev;
3446 const struct resource *res = ofdev->resource;
3447 unsigned int num = ofdev->num_resources;
3448 int retval;
3449
3450 pdev = platform_device_alloc(name, -1);
3451 if (!pdev) {
3452 retval = -ENOMEM;
3453 goto error;
3454 }
3455
3456 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3457 pdev->dev.dma_mask = &msm_otg_dma_mask;
3458
3459 if (num) {
3460 retval = platform_device_add_resources(pdev, res, num);
3461 if (retval)
3462 goto error;
3463 }
3464
3465 retval = platform_device_add(pdev);
3466 if (retval)
3467 goto error;
3468
3469 return pdev;
3470
3471error:
3472 platform_device_put(pdev);
3473 return ERR_PTR(retval);
3474}
3475
3476static int msm_otg_setup_devices(struct platform_device *ofdev,
3477 enum usb_mode_type mode, bool init)
3478{
3479 const char *gadget_name = "msm_hsusb";
3480 const char *host_name = "msm_hsusb_host";
3481 static struct platform_device *gadget_pdev;
3482 static struct platform_device *host_pdev;
3483 int retval = 0;
3484
3485 if (!init) {
3486 if (gadget_pdev)
3487 platform_device_unregister(gadget_pdev);
3488 if (host_pdev)
3489 platform_device_unregister(host_pdev);
3490 return 0;
3491 }
3492
3493 switch (mode) {
3494 case USB_OTG:
3495 /* fall through */
3496 case USB_PERIPHERAL:
3497 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3498 if (IS_ERR(gadget_pdev)) {
3499 retval = PTR_ERR(gadget_pdev);
3500 break;
3501 }
3502 if (mode == USB_PERIPHERAL)
3503 break;
3504 /* fall through */
3505 case USB_HOST:
3506 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3507 if (IS_ERR(host_pdev)) {
3508 retval = PTR_ERR(host_pdev);
3509 if (mode == USB_OTG)
3510 platform_device_unregister(gadget_pdev);
3511 }
3512 break;
3513 default:
3514 break;
3515 }
3516
3517 return retval;
3518}
3519
3520struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3521{
3522 struct device_node *node = pdev->dev.of_node;
3523 struct msm_otg_platform_data *pdata;
3524 int len = 0;
3525
3526 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3527 if (!pdata) {
3528 pr_err("unable to allocate platform data\n");
3529 return NULL;
3530 }
3531 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3532 if (len) {
3533 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3534 if (!pdata->phy_init_seq)
3535 return NULL;
3536 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3537 pdata->phy_init_seq,
3538 len/sizeof(*pdata->phy_init_seq));
3539 }
3540 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3541 &pdata->power_budget);
3542 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3543 &pdata->mode);
3544 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3545 &pdata->otg_control);
3546 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3547 &pdata->default_mode);
3548 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3549 &pdata->phy_type);
3550 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3551 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303552 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3553 "qcom,hsusb-otg-disable-reset");
3554
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303555 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303556}
3557
3558static int __init msm_otg_probe(struct platform_device *pdev)
3559{
Manu Gautamf8c45642012-08-10 10:20:56 -07003560 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303561 struct resource *res;
3562 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003563 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303564 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303565
3566 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303567
3568 if (pdev->dev.of_node) {
3569 dev_dbg(&pdev->dev, "device tree enabled\n");
3570 pdata = msm_otg_dt_to_pdata(pdev);
3571 if (!pdata)
3572 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003573
3574 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3575 if (!pdata->bus_scale_table)
3576 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3577
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303578 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3579 if (ret) {
3580 dev_err(&pdev->dev, "devices setup failed\n");
3581 return ret;
3582 }
3583 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303584 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3585 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303586 } else {
3587 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303588 }
3589
3590 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3591 if (!motg) {
3592 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3593 return -ENOMEM;
3594 }
3595
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003596 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3597 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003598 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3599 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303600 goto free_motg;
3601 }
3602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003603 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303604 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003605 phy = &motg->phy;
3606 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303607
3608 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303609 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3610 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3611 * PMIC can detect ACA ID_GND and generate an interrupt.
3612 */
3613 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3614 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3615 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003616 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303617 }
3618
Ofir Cohen4da266f2012-01-03 10:19:29 +02003619 /* initialize reset counter */
3620 motg->reset_counter = 0;
3621
Amit Blay02eff132011-09-21 16:46:24 +03003622 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003623 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003624 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003625 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303626
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303627 /*
3628 * Targets on which link uses asynchronous reset methodology,
3629 * free running clock is not required during the reset.
3630 */
Manu Gautam5143b252012-01-05 19:25:23 -08003631 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303632 if (IS_ERR(motg->clk))
3633 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3634 else
3635 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303636
3637 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003638 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303639 * CORE CLK must be running at >55Mhz for correct HSUSB
3640 * operation and USB core cannot tolerate frequency changes on
3641 * CORE CLK. For such USB cores, vote for maximum clk frequency
3642 * on pclk source
3643 */
Manu Gautam5143b252012-01-05 19:25:23 -08003644 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3645 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303646 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003647 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303648 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003649 goto put_clk;
3650 }
3651 clk_set_rate(motg->core_clk, INT_MAX);
3652
3653 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3654 if (IS_ERR(motg->pclk)) {
3655 dev_err(&pdev->dev, "failed to get iface_clk\n");
3656 ret = PTR_ERR(motg->pclk);
3657 goto put_core_clk;
3658 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303659
3660 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3661 if (!res) {
3662 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3663 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003664 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303665 }
3666
3667 motg->regs = ioremap(res->start, resource_size(res));
3668 if (!motg->regs) {
3669 dev_err(&pdev->dev, "ioremap failed\n");
3670 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003671 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303672 }
3673 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3674
3675 motg->irq = platform_get_irq(pdev, 0);
3676 if (!motg->irq) {
3677 dev_err(&pdev->dev, "platform_get_irq failed\n");
3678 ret = -ENODEV;
3679 goto free_regs;
3680 }
3681
Manu Gautamf8c45642012-08-10 10:20:56 -07003682 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3683 if (motg->async_irq < 0) {
3684 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3685 motg->async_irq = 0;
3686 }
3687
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003688 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003689 if (IS_ERR(motg->xo_handle)) {
3690 dev_err(&pdev->dev, "%s not able to get the handle "
3691 "to vote for TCXO D0 buffer\n", __func__);
3692 ret = PTR_ERR(motg->xo_handle);
3693 goto free_regs;
3694 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303695
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003696 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003697 if (ret) {
3698 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3699 "D0 buffer%d\n", __func__, ret);
3700 goto free_xo_handle;
3701 }
3702
Manu Gautam28b1bac2012-01-30 16:43:06 +05303703 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303704
Mayank Rana248698c2012-04-19 00:03:16 +05303705 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003706 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303707 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003708 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303709 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003710 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003711 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303712 goto devote_xo_handle;
3713 }
3714 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303715 }
3716
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003717 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303718 if (ret) {
3719 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303720 goto devote_xo_handle;
3721 }
3722
3723 ret = regulator_enable(hsusb_vddcx);
3724 if (ret) {
3725 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3726 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303727 }
3728
3729 ret = msm_hsusb_ldo_init(motg, 1);
3730 if (ret) {
3731 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303732 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303733 }
3734
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303735 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303736 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3737 "mhl_usb_hs_switch");
3738 if (IS_ERR(mhl_usb_hs_switch)) {
3739 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003740 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303741 goto free_ldo_init;
3742 }
3743 }
3744
Amit Blay81801aa2012-09-19 12:08:12 +02003745 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303746 if (ret) {
3747 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003748 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303749 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303750 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303751
3752 writel(0, USB_USBINTR);
3753 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003754 /* Ensure that above STOREs are completed before enabling interrupts */
3755 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303756
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303757 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3758 if (ret)
3759 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003760 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303761 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303762 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303763 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303764 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003765 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303766 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3767 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05303768 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
3769 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303770 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3771 "msm_otg", motg);
3772 if (ret) {
3773 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003774 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303775 }
3776
Manu Gautamf8c45642012-08-10 10:20:56 -07003777 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05303778 ret = request_irq(motg->async_irq, msm_otg_irq,
3779 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07003780 if (ret) {
3781 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3782 goto free_irq;
3783 }
3784 disable_irq(motg->async_irq);
3785 }
3786
Jack Pham87f202f2012-08-06 00:24:22 -07003787 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3788 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3789
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003790 phy->init = msm_otg_reset;
3791 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003792 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303793
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003794 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303795
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003796 phy->otg->phy = &motg->phy;
3797 phy->otg->set_host = msm_otg_set_host;
3798 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003799 phy->otg->start_hnp = msm_otg_start_hnp;
3800 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003801
3802 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303803 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003804 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003805 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303806 }
3807
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303808 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303809 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003810 if (motg->pdata->pmic_id_irq) {
3811 ret = request_irq(motg->pdata->pmic_id_irq,
3812 msm_pmic_id_irq,
3813 IRQF_TRIGGER_RISING |
3814 IRQF_TRIGGER_FALLING,
3815 "msm_otg", motg);
3816 if (ret) {
3817 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003818 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003819 }
3820 } else {
3821 ret = -ENODEV;
3822 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003823 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003824 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303825 }
3826
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303827 msm_hsusb_mhl_switch_enable(motg, 1);
3828
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303829 platform_set_drvdata(pdev, motg);
3830 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003831 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303832
Anji jonnalad270e2d2011-08-09 11:28:32 +05303833 ret = msm_otg_debugfs_init(motg);
3834 if (ret)
3835 dev_dbg(&pdev->dev, "mode debugfs file is"
3836 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303837
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07003838 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3839 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3840
Amit Blay58b31472011-11-18 09:39:39 +02003841 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3842 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303843 (!(motg->pdata->mode == USB_OTG) ||
3844 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003845 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303846 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003847
Amit Blay58b31472011-11-18 09:39:39 +02003848 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02003849 motg->caps = ALLOW_PHY_RETENTION |
3850 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02003851 }
3852
Amit Blay6fa647a2012-05-24 14:12:08 +03003853 if (motg->pdata->enable_lpm_on_dev_suspend)
3854 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003856 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303857 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07003858 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303859
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303860 if (motg->pdata->bus_scale_table) {
3861 motg->bus_perf_client =
3862 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3863 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003864 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303865 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303866 else
3867 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303868 }
3869
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303870 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003871
Steve Mucklef132c6c2012-06-06 18:30:57 -07003872remove_phy:
3873 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003874free_async_irq:
3875 if (motg->async_irq)
3876 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303877free_irq:
3878 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003879destroy_wlock:
3880 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303881 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02003882 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003883free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303884 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303885free_hsusb_vddcx:
3886 regulator_disable(hsusb_vddcx);
3887free_config_vddcx:
3888 regulator_set_voltage(hsusb_vddcx,
3889 vdd_val[motg->vdd_type][VDD_NONE],
3890 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003891devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303892 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003893 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003894free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003895 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303896free_regs:
3897 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003898put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303899 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303900put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003901 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303902put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303903 if (!IS_ERR(motg->clk))
3904 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003905 if (!IS_ERR(motg->phy_reset_clk))
3906 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003907free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003908 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303909free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303910 kfree(motg);
3911 return ret;
3912}
3913
3914static int __devexit msm_otg_remove(struct platform_device *pdev)
3915{
3916 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003917 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303918 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303919
3920 if (otg->host || otg->gadget)
3921 return -EBUSY;
3922
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303923 if (pdev->dev.of_node)
3924 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003925 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3926 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303927 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303928 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303929 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303930 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003931 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303932 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303933
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303934 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303935
3936 device_init_wakeup(&pdev->dev, 0);
3937 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003938 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303939
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303940 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003941 if (motg->pdata->pmic_id_irq)
3942 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003943 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303944 free_irq(motg->irq, motg);
3945
Jack Pham87f202f2012-08-06 00:24:22 -07003946 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3947 motg->pdata->mpm_otgsessvld_int)
3948 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3949
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303950 /*
3951 * Put PHY in low power mode.
3952 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003953 ulpi_read(otg->phy, 0x14);
3954 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303955
3956 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3957 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3958 if (readl(USB_PORTSC) & PORTSC_PHCD)
3959 break;
3960 udelay(1);
3961 cnt++;
3962 }
3963 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003964 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303965
Manu Gautam28b1bac2012-01-30 16:43:06 +05303966 clk_disable_unprepare(motg->pclk);
3967 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003968 msm_xo_put(motg->xo_handle);
Amit Blay81801aa2012-09-19 12:08:12 +02003969 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303970 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303971 regulator_disable(hsusb_vddcx);
3972 regulator_set_voltage(hsusb_vddcx,
3973 vdd_val[motg->vdd_type][VDD_NONE],
3974 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303975
3976 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303977 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303978
Amit Blay02eff132011-09-21 16:46:24 +03003979 if (!IS_ERR(motg->phy_reset_clk))
3980 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303981 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303982 if (!IS_ERR(motg->clk))
3983 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003984 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303985
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303986 if (motg->bus_perf_client)
3987 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303988
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003989 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303990 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303991 return 0;
3992}
3993
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303994#ifdef CONFIG_PM_RUNTIME
3995static int msm_otg_runtime_idle(struct device *dev)
3996{
3997 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003998 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303999
4000 dev_dbg(dev, "OTG runtime idle\n");
4001
Steve Mucklef132c6c2012-06-06 18:30:57 -07004002 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304003 return -EAGAIN;
4004 else
4005 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304006}
4007
4008static int msm_otg_runtime_suspend(struct device *dev)
4009{
4010 struct msm_otg *motg = dev_get_drvdata(dev);
4011
4012 dev_dbg(dev, "OTG runtime suspend\n");
4013 return msm_otg_suspend(motg);
4014}
4015
4016static int msm_otg_runtime_resume(struct device *dev)
4017{
4018 struct msm_otg *motg = dev_get_drvdata(dev);
4019
4020 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304021 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304022 return msm_otg_resume(motg);
4023}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304024#endif
4025
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304026#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304027static int msm_otg_pm_suspend(struct device *dev)
4028{
Jack Pham5ca279b2012-05-14 18:42:54 -07004029 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304030 struct msm_otg *motg = dev_get_drvdata(dev);
4031
4032 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004033
4034 atomic_set(&motg->pm_suspended, 1);
4035 ret = msm_otg_suspend(motg);
4036 if (ret)
4037 atomic_set(&motg->pm_suspended, 0);
4038
4039 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304040}
4041
4042static int msm_otg_pm_resume(struct device *dev)
4043{
Jack Pham5ca279b2012-05-14 18:42:54 -07004044 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304045 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304046
4047 dev_dbg(dev, "OTG PM resume\n");
4048
Jack Pham5ca279b2012-05-14 18:42:54 -07004049 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004050 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004051 pm_runtime_get_noresume(dev);
4052 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304053
Jack Pham5ca279b2012-05-14 18:42:54 -07004054 /* Update runtime PM status */
4055 pm_runtime_disable(dev);
4056 pm_runtime_set_active(dev);
4057 pm_runtime_enable(dev);
4058
Jack Phamc7edb172012-08-13 15:32:39 -07004059 if (motg->sm_work_pending) {
4060 motg->sm_work_pending = false;
4061 queue_work(system_nrt_wq, &motg->sm_work);
4062 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004063 }
4064
4065 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304066}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304067#endif
4068
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304069#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304070static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304071 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4072 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4073 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304074};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304075#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304076
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304077static struct of_device_id msm_otg_dt_match[] = {
4078 { .compatible = "qcom,hsusb-otg",
4079 },
4080 {}
4081};
4082
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304083static struct platform_driver msm_otg_driver = {
4084 .remove = __devexit_p(msm_otg_remove),
4085 .driver = {
4086 .name = DRIVER_NAME,
4087 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304088#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304089 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304090#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304091 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304092 },
4093};
4094
4095static int __init msm_otg_init(void)
4096{
4097 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4098}
4099
4100static void __exit msm_otg_exit(void)
4101{
4102 platform_driver_unregister(&msm_otg_driver);
4103}
4104
4105module_init(msm_otg_init);
4106module_exit(msm_otg_exit);
4107
4108MODULE_LICENSE("GPL v2");
4109MODULE_DESCRIPTION("MSM USB transceiver driver");