blob: 269542a111ae04a653511eb33b004b1ef45b9d2f [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053042#include <linux/mhl_8334.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053043
Manu Gautam0ddbd922012-09-21 17:17:38 +053044#include <mach/scm.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070046#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080047#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053048#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053049#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053050
51#define MSM_USB_BASE (motg->regs)
52#define DRIVER_NAME "msm_otg"
53
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053054#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondeti458d8792012-09-28 14:45:18 +053055#define CHG_RECHECK_DELAY (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053056#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053057#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
58#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
59#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
60#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
61
62#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
63#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
64#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
65#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
66
Mayank Rana248698c2012-04-19 00:03:16 +053067#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080068#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053069#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
70
Amit Blayd0fe07b2012-09-05 16:42:09 +030071#define USB_SUSPEND_DELAY_TIME (500 * HZ/1000) /* 500 msec */
72
Amit Blay81801aa2012-09-19 12:08:12 +020073enum msm_otg_phy_reg_mode {
74 USB_PHY_REG_OFF,
75 USB_PHY_REG_ON,
76 USB_PHY_REG_LPM_ON,
77 USB_PHY_REG_LPM_OFF,
78};
79
Mayank Rana443f9e42012-09-21 18:32:39 +053080static char *override_phy_init;
81module_param(override_phy_init, charp, S_IRUGO|S_IWUSR);
82MODULE_PARM_DESC(override_phy_init,
83 "Override HSUSB PHY Init Settings");
84
Amit Blayd6f38282012-10-29 13:13:46 +020085unsigned int lpm_disconnect_thresh = 1000;
86module_param(lpm_disconnect_thresh , uint, S_IRUGO | S_IWUSR);
87MODULE_PARM_DESC(lpm_disconnect_thresh,
88 "Delay before entering LPM on USB disconnect");
89
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053090static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070091static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053092static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053093static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053094static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070095
Anji jonnala11aa5c42011-05-04 10:19:48 +053096static struct regulator *hsusb_3p3;
97static struct regulator *hsusb_1p8;
98static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053099static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530100static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +0530101static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530102
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530103static bool aca_id_turned_on;
David Keitel272ce522012-08-17 16:25:24 -0700104static bool legacy_power_supply;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530105static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530106{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530107#ifdef CONFIG_USB_MSM_ACA
108 return true;
109#else
110 return debug_aca_enabled;
111#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +0530112}
113
Mayank Rana248698c2012-04-19 00:03:16 +0530114static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
115 { /* VDD_CX CORNER Voting */
116 [VDD_NONE] = RPM_VREG_CORNER_NONE,
117 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
118 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
119 },
120 { /* VDD_CX Voltage Voting */
121 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
122 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
123 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
124 },
125};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126
127static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
128{
129 int rc = 0;
130
131 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700132 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200134 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530135 return PTR_ERR(hsusb_3p3);
136 }
137
138 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
139 USB_PHY_3P3_VOL_MAX);
140 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700141 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530143 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700145 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530146 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200147 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530150 }
151 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
152 USB_PHY_1P8_VOL_MAX);
153 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700154 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700155 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530156 goto put_1p8;
157 }
158
159 return 0;
160 }
161
Anji jonnala11aa5c42011-05-04 10:19:48 +0530162put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164put_3p3_lpm:
165 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530166 return rc;
167}
168
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530169static int msm_hsusb_config_vddcx(int high)
170{
Mayank Rana248698c2012-04-19 00:03:16 +0530171 struct msm_otg *motg = the_msm_otg;
172 enum usb_vdd_type vdd_type = motg->vdd_type;
173 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530174 int min_vol;
175 int ret;
176
Mayank Rana248698c2012-04-19 00:03:16 +0530177 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530178 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
179 if (ret) {
180 pr_err("%s: unable to set the voltage for regulator "
181 "HSUSB_VDDCX\n", __func__);
182 return ret;
183 }
184
185 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
186
187 return ret;
188}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530189
Amit Blay81801aa2012-09-19 12:08:12 +0200190static int msm_hsusb_ldo_enable(struct msm_otg *motg,
191 enum msm_otg_phy_reg_mode mode)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530192{
193 int ret = 0;
194
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530195 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530196 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
197 return -ENODEV;
198 }
199
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530200 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530201 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
202 return -ENODEV;
203 }
204
Amit Blay81801aa2012-09-19 12:08:12 +0200205 switch (mode) {
206 case USB_PHY_REG_ON:
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207 ret = regulator_set_optimum_mode(hsusb_1p8,
208 USB_PHY_1P8_HPM_LOAD);
209 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530211 "HSUSB_1p8\n", __func__);
212 return ret;
213 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700214
215 ret = regulator_enable(hsusb_1p8);
216 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700217 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 __func__);
219 regulator_set_optimum_mode(hsusb_1p8, 0);
220 return ret;
221 }
222
Anji jonnala11aa5c42011-05-04 10:19:48 +0530223 ret = regulator_set_optimum_mode(hsusb_3p3,
224 USB_PHY_3P3_HPM_LOAD);
225 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530227 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228 regulator_set_optimum_mode(hsusb_1p8, 0);
229 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530230 return ret;
231 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232
233 ret = regulator_enable(hsusb_3p3);
234 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700235 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 __func__);
237 regulator_set_optimum_mode(hsusb_3p3, 0);
238 regulator_set_optimum_mode(hsusb_1p8, 0);
239 regulator_disable(hsusb_1p8);
240 return ret;
241 }
242
Amit Blay81801aa2012-09-19 12:08:12 +0200243 break;
244
245 case USB_PHY_REG_OFF:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 ret = regulator_disable(hsusb_1p8);
247 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700248 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 __func__);
250 return ret;
251 }
252
253 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530254 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530256 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257
258 ret = regulator_disable(hsusb_3p3);
259 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700260 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 __func__);
262 return ret;
263 }
264 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530265 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530267 "HSUSB_3p3\n", __func__);
Amit Blay81801aa2012-09-19 12:08:12 +0200268
269 break;
270
271 case USB_PHY_REG_LPM_ON:
272 ret = regulator_set_optimum_mode(hsusb_1p8,
273 USB_PHY_1P8_LPM_LOAD);
274 if (ret < 0) {
275 pr_err("%s: Unable to set LPM of the regulator: HSUSB_1p8\n",
276 __func__);
277 return ret;
278 }
279
280 ret = regulator_set_optimum_mode(hsusb_3p3,
281 USB_PHY_3P3_LPM_LOAD);
282 if (ret < 0) {
283 pr_err("%s: Unable to set LPM of the regulator: HSUSB_3p3\n",
284 __func__);
285 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
286 return ret;
287 }
288
289 break;
290
291 case USB_PHY_REG_LPM_OFF:
292 ret = regulator_set_optimum_mode(hsusb_1p8,
293 USB_PHY_1P8_HPM_LOAD);
294 if (ret < 0) {
295 pr_err("%s: Unable to set HPM of the regulator: HSUSB_1p8\n",
296 __func__);
297 return ret;
298 }
299
300 ret = regulator_set_optimum_mode(hsusb_3p3,
301 USB_PHY_3P3_HPM_LOAD);
302 if (ret < 0) {
303 pr_err("%s: Unable to set HPM of the regulator: HSUSB_3p3\n",
304 __func__);
305 regulator_set_optimum_mode(hsusb_1p8, USB_PHY_REG_ON);
306 return ret;
307 }
308
309 break;
310
311 default:
312 pr_err("%s: Unsupported mode (%d).", __func__, mode);
313 return -ENOTSUPP;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530314 }
315
Amit Blay81801aa2012-09-19 12:08:12 +0200316 pr_debug("%s: USB reg mode (%d) (OFF/HPM/LPM)\n", __func__, mode);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530317 return ret < 0 ? ret : 0;
318}
319
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530320static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
321{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530322 struct msm_otg_platform_data *pdata = motg->pdata;
323
324 if (!pdata->mhl_enable)
325 return;
326
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530327 if (!mhl_usb_hs_switch) {
328 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530329 return;
330 }
331
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530332 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530333 if (regulator_enable(mhl_usb_hs_switch))
334 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530335 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530336 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530337 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530338}
339
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200340static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530341{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200342 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530343 int cnt = 0;
344
345 /* initiate read operation */
346 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
347 USB_ULPI_VIEWPORT);
348
349 /* wait for completion */
350 while (cnt < ULPI_IO_TIMEOUT_USEC) {
351 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
352 break;
353 udelay(1);
354 cnt++;
355 }
356
357 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200358 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530359 readl(USB_ULPI_VIEWPORT));
360 return -ETIMEDOUT;
361 }
362 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
363}
364
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200365static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530366{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200367 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530368 int cnt = 0;
369
370 /* initiate write operation */
371 writel(ULPI_RUN | ULPI_WRITE |
372 ULPI_ADDR(reg) | ULPI_DATA(val),
373 USB_ULPI_VIEWPORT);
374
375 /* wait for completion */
376 while (cnt < ULPI_IO_TIMEOUT_USEC) {
377 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
378 break;
379 udelay(1);
380 cnt++;
381 }
382
383 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200384 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530385 return -ETIMEDOUT;
386 }
387 return 0;
388}
389
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200390static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530391 .read = ulpi_read,
392 .write = ulpi_write,
393};
394
395static void ulpi_init(struct msm_otg *motg)
396{
397 struct msm_otg_platform_data *pdata = motg->pdata;
Mayank Rana443f9e42012-09-21 18:32:39 +0530398 int aseq[10];
399 int *seq = NULL;
400
401 if (override_phy_init) {
402 pr_debug("%s(): HUSB PHY Init:%s\n", __func__,
403 override_phy_init);
404 get_options(override_phy_init, ARRAY_SIZE(aseq), aseq);
405 seq = &aseq[1];
406 } else {
407 seq = pdata->phy_init_seq;
408 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530409
410 if (!seq)
411 return;
412
413 while (seq[0] >= 0) {
Mayank Rana443f9e42012-09-21 18:32:39 +0530414 if (override_phy_init)
415 pr_debug("ulpi: write 0x%02x to 0x%02x\n",
416 seq[0], seq[1]);
417
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200418 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530419 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200420 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530421 seq += 2;
422 }
423}
424
425static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
426{
427 int ret;
428
429 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530430 if (!IS_ERR(motg->clk)) {
431 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
432 } else {
433 /* Using asynchronous block reset to the hardware */
434 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
435 clk_disable_unprepare(motg->pclk);
436 clk_disable_unprepare(motg->core_clk);
437 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
438 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530439 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200440 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530441 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530442 if (!IS_ERR(motg->clk)) {
443 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
444 } else {
445 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
446 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
447 ndelay(200);
448 clk_prepare_enable(motg->core_clk);
449 clk_prepare_enable(motg->pclk);
450 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530451 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200452 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530453 }
454 return ret;
455}
456
457static int msm_otg_phy_clk_reset(struct msm_otg *motg)
458{
459 int ret;
460
Amit Blay02eff132011-09-21 16:46:24 +0300461 if (IS_ERR(motg->phy_reset_clk))
462 return 0;
463
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530464 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
465 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200466 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530467 return ret;
468 }
469 usleep_range(10000, 12000);
470 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
471 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200472 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530473 return ret;
474}
475
476static int msm_otg_phy_reset(struct msm_otg *motg)
477{
478 u32 val;
479 int ret;
480 int retries;
481
482 ret = msm_otg_link_clk_reset(motg, 1);
483 if (ret)
484 return ret;
485 ret = msm_otg_phy_clk_reset(motg);
486 if (ret)
487 return ret;
488 ret = msm_otg_link_clk_reset(motg, 0);
489 if (ret)
490 return ret;
491
492 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
493 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
494
495 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200496 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530497 ULPI_CLR(ULPI_FUNC_CTRL));
498 if (!ret)
499 break;
500 ret = msm_otg_phy_clk_reset(motg);
501 if (ret)
502 return ret;
503 }
504 if (!retries)
505 return -ETIMEDOUT;
506
507 /* This reset calibrates the phy, if the above write succeeded */
508 ret = msm_otg_phy_clk_reset(motg);
509 if (ret)
510 return ret;
511
512 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200513 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530514 if (ret != -ETIMEDOUT)
515 break;
516 ret = msm_otg_phy_clk_reset(motg);
517 if (ret)
518 return ret;
519 }
520 if (!retries)
521 return -ETIMEDOUT;
522
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200523 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530524 return 0;
525}
526
527#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530528static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530529{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530530 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530531
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530532 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530533 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530534 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530535 break;
536 udelay(1);
537 cnt++;
538 }
539 if (cnt >= LINK_RESET_TIMEOUT_USEC)
540 return -ETIMEDOUT;
541
542 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530543 writel_relaxed(0x80000000, USB_PORTSC);
544 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530545 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530546
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530547 return 0;
548}
549
Steve Mucklef132c6c2012-06-06 18:30:57 -0700550static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700552 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530553 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530554 int ret;
555 u32 val = 0;
556 u32 ulpi_val = 0;
557
Ofir Cohen4da266f2012-01-03 10:19:29 +0200558 /*
559 * USB PHY and Link reset also reset the USB BAM.
560 * Thus perform reset operation only once to avoid
561 * USB BAM reset on other cases e.g. USB cable disconnections.
562 */
563 if (pdata->disable_reset_on_disconnect) {
564 if (motg->reset_counter)
565 return 0;
566 else
567 motg->reset_counter++;
568 }
569
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530570 if (!IS_ERR(motg->clk))
571 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530572 ret = msm_otg_phy_reset(motg);
573 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700574 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530575 return ret;
576 }
577
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530578 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530579 ret = msm_otg_link_reset(motg);
580 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700581 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530582 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530583 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530584 msleep(100);
585
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530586 ulpi_init(motg);
587
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700588 /* Ensure that RESET operation is completed before turning off clock */
589 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530590
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530591 if (!IS_ERR(motg->clk))
592 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530593
594 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530595 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530596 if (pdata->mode == USB_OTG) {
597 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
598 val |= OTGSC_IDIE | OTGSC_BSVIE;
599 } else if (pdata->mode == USB_PERIPHERAL) {
600 ulpi_val = ULPI_INT_SESS_VALID;
601 val |= OTGSC_BSVIE;
602 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530603 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200604 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
605 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530606 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700607 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530608 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530609 /* Enable PMIC pull-up */
610 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530611 }
612
613 return 0;
614}
615
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530616static const char *timer_string(int bit)
617{
618 switch (bit) {
619 case A_WAIT_VRISE: return "a_wait_vrise";
620 case A_WAIT_VFALL: return "a_wait_vfall";
621 case B_SRP_FAIL: return "b_srp_fail";
622 case A_WAIT_BCON: return "a_wait_bcon";
623 case A_AIDL_BDIS: return "a_aidl_bdis";
624 case A_BIDL_ADIS: return "a_bidl_adis";
625 case B_ASE0_BRST: return "b_ase0_brst";
626 case A_TST_MAINT: return "a_tst_maint";
627 case B_TST_SRP: return "b_tst_srp";
628 case B_TST_CONFIG: return "b_tst_config";
629 default: return "UNDEFINED";
630 }
631}
632
633static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
634{
635 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
636
637 switch (motg->active_tmout) {
638 case A_WAIT_VRISE:
639 /* TODO: use vbus_vld interrupt */
640 set_bit(A_VBUS_VLD, &motg->inputs);
641 break;
642 case A_TST_MAINT:
643 /* OTG PET: End session after TA_TST_MAINT */
644 set_bit(A_BUS_DROP, &motg->inputs);
645 break;
646 case B_TST_SRP:
647 /*
648 * OTG PET: Initiate SRP after TB_TST_SRP of
649 * previous session end.
650 */
651 set_bit(B_BUS_REQ, &motg->inputs);
652 break;
653 case B_TST_CONFIG:
654 clear_bit(A_CONN, &motg->inputs);
655 break;
656 default:
657 set_bit(motg->active_tmout, &motg->tmouts);
658 }
659
660 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
661 queue_work(system_nrt_wq, &motg->sm_work);
662 return HRTIMER_NORESTART;
663}
664
665static void msm_otg_del_timer(struct msm_otg *motg)
666{
667 int bit = motg->active_tmout;
668
669 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
670 div_s64(ktime_to_us(hrtimer_get_remaining(
671 &motg->timer)), 1000));
672 hrtimer_cancel(&motg->timer);
673 clear_bit(bit, &motg->tmouts);
674}
675
676static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
677{
678 clear_bit(bit, &motg->tmouts);
679 motg->active_tmout = bit;
680 pr_debug("starting %s timer\n", timer_string(bit));
681 hrtimer_start(&motg->timer,
682 ktime_set(time / 1000, (time % 1000) * 1000000),
683 HRTIMER_MODE_REL);
684}
685
686static void msm_otg_init_timer(struct msm_otg *motg)
687{
688 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
689 motg->timer.function = msm_otg_timer_func;
690}
691
Steve Mucklef132c6c2012-06-06 18:30:57 -0700692static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530693{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700694 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530695
Steve Mucklef132c6c2012-06-06 18:30:57 -0700696 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530697 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700698 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530699 return -EINVAL;
700 }
701
702 pr_debug("A-Host: HNP initiated\n");
703 clear_bit(A_BUS_REQ, &motg->inputs);
704 queue_work(system_nrt_wq, &motg->sm_work);
705 return 0;
706}
707
Steve Mucklef132c6c2012-06-06 18:30:57 -0700708static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530709{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700710 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530711 u32 val;
712 int ret = 0;
713
Steve Mucklef132c6c2012-06-06 18:30:57 -0700714 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530715 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700716 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530717 ret = -EINVAL;
718 goto out;
719 }
720
721 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
722 pr_debug("initial conditions of SRP are not met. Try again"
723 "after some time\n");
724 ret = -EAGAIN;
725 goto out;
726 }
727
728 pr_debug("B-Device SRP started\n");
729
730 /*
731 * PHY won't pull D+ high unless it detects Vbus valid.
732 * Since by definition, SRP is only done when Vbus is not valid,
733 * software work-around needs to be used to spoof the PHY into
734 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
735 * VBUSVLDEXT register bits.
736 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700737 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530738 /*
739 * Harware auto assist data pulsing: Data pulse is given
740 * for 7msec; wait for vbus
741 */
742 val = readl_relaxed(USB_OTGSC);
743 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
744
745 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
746out:
747 return ret;
748}
749
Steve Mucklef132c6c2012-06-06 18:30:57 -0700750static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530751{
752 struct usb_hcd *hcd = bus_to_hcd(otg->host);
753 struct usb_device *rhub = otg->host->root_hub;
754
755 if (enable) {
756 pm_runtime_disable(&rhub->dev);
757 rhub->state = USB_STATE_NOTATTACHED;
758 hcd->driver->bus_suspend(hcd);
759 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
760 } else {
761 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700762 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530763 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
764 }
765}
766
Steve Mucklef132c6c2012-06-06 18:30:57 -0700767static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530768{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700769 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530770
Amit Blay6fa647a2012-05-24 14:12:08 +0300771 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530772 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530773
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530774 /*
775 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
776 * during bus suspend/resume. Update the relevant state
777 * machine inputs and trigger LPM entry/exit. Checking
778 * in_lpm flag would avoid unnecessary work scheduling.
779 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530780 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700781 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530782 case OTG_STATE_A_WAIT_BCON:
783 if (TA_WAIT_BCON > 0)
784 break;
785 /* fall through */
786 case OTG_STATE_A_HOST:
787 pr_debug("host bus suspend\n");
788 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530789 if (!atomic_read(&motg->in_lpm))
790 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530791 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300792 case OTG_STATE_B_PERIPHERAL:
793 pr_debug("peripheral bus suspend\n");
794 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
795 break;
796 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530797 if (!atomic_read(&motg->in_lpm))
798 queue_delayed_work(system_nrt_wq,
799 &motg->suspend_work,
800 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300801 break;
802
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530803 default:
804 break;
805 }
806 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700807 switch (phy->state) {
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530808 case OTG_STATE_A_WAIT_BCON:
809 /* Remote wakeup or resume */
810 set_bit(A_BUS_REQ, &motg->inputs);
811 /* ensure hardware is not in low power mode */
812 if (atomic_read(&motg->in_lpm))
813 pm_runtime_resume(phy->dev);
814 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530815 case OTG_STATE_A_SUSPEND:
816 /* Remote wakeup or resume */
817 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700818 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700819
820 /* ensure hardware is not in low power mode */
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530821 if (atomic_read(&motg->in_lpm))
822 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530823 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300824 case OTG_STATE_B_PERIPHERAL:
825 pr_debug("peripheral bus resume\n");
826 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
827 break;
828 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +0530829 if (atomic_read(&motg->in_lpm))
830 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300831 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530832 default:
833 break;
834 }
835 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530836 return 0;
837}
838
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530839#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530840#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
841
842#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530843static int msm_otg_suspend(struct msm_otg *motg)
844{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200845 struct usb_phy *phy = &motg->phy;
846 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530847 struct msm_otg_platform_data *pdata = motg->pdata;
848 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300849 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530850 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800851 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530852 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530853
854 if (atomic_read(&motg->in_lpm))
855 return 0;
856
857 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530858 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
859 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700860 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300861 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
862 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530863 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700864
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530865 /*
866 * Abort suspend when,
867 * 1. charging detection in progress due to cable plug-in
868 * 2. host mode activation in progress due to Micro-A cable insertion
869 */
870
871 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
872 !dcp) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham502bea32012-08-13 15:34:20 -0700873 enable_irq(motg->irq);
874 return -EBUSY;
875 }
876
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530877 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530878 * Chipidea 45-nm PHY suspend sequence:
879 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530880 * Interrupt Latch Register auto-clear feature is not present
881 * in all PHY versions. Latch register is clear on read type.
882 * Clear latch register to avoid spurious wakeup from
883 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530884 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530885 * PHY comparators are disabled when PHY enters into low power
886 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
887 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
888 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530889 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530890 * PLL is not turned off when PHY enters into low power mode (LPM).
891 * Disable PLL for maximum power savings.
892 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530893
894 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200895 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530896 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200897 ulpi_write(phy, 0x01, 0x30);
898 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530899 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530900
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530902 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530903 * PHY may take some time or even fail to enter into low power
904 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
905 * in failure case.
906 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530907 portsc = readl_relaxed(USB_PORTSC);
908 if (!(portsc & PORTSC_PHCD)) {
909 writel_relaxed(portsc | PORTSC_PHCD,
910 USB_PORTSC);
911 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
912 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
913 break;
914 udelay(1);
915 cnt++;
916 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530917 }
918
919 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200920 dev_err(phy->dev, "Unable to suspend PHY\n");
921 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530922 enable_irq(motg->irq);
923 return -ETIMEDOUT;
924 }
925
926 /*
927 * PHY has capability to generate interrupt asynchronously in low
928 * power mode (LPM). This interrupt is level triggered. So USB IRQ
929 * line must be disabled till async interrupt enable bit is cleared
930 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
931 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530932 *
933 * PHY retention mode is disallowed while entering to LPM with wall
934 * charger connected. But PHY is put into suspend mode. Hence
935 * enable asynchronous interrupt to detect charger disconnection when
936 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530937 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530938 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300939 if (host_bus_suspend || device_bus_suspend ||
940 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530941 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
942 else
943 cmd_val |= ULPI_STP_CTRL;
944 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530945
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530946 /*
947 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
948 * PHY retention and collapse can not happen with VDP_SRC enabled.
949 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300950 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
951 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200952 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530953 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +0200954 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530955 if ((motg->pdata->mode == USB_OTG) ||
956 (motg->pdata->mode == USB_HOST))
957 phy_ctrl_val |= (PHY_IDHV_INTEN |
958 PHY_OTGSESSVLDHV_INTEN);
959 else
960 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
961 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530962
Amit Blay58b31472011-11-18 09:39:39 +0200963 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700964 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530965 }
966
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967 /* Ensure that above operation is completed before turning off clocks */
968 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300969 /* Consider clocks on workaround flag only in case of bus suspend */
970 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
971 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
972 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300973 clk_disable_unprepare(motg->pclk);
974 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300975 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300976 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530977
Anji jonnala7da3f262011-12-02 17:22:14 -0800978 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530979 if (!host_bus_suspend) {
980 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
981 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700982 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530983 "TCXO D0 buffer%d\n", __func__, ret);
984 else
985 motg->lpm_flags |= XO_SHUTDOWN;
986 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800987
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530988 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
989 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +0200990 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +0200992 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
993 !host_bus_suspend && !device_bus_suspend && !dcp) {
994 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
995 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530996 }
997
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530998 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301000 msm_hsusb_mhl_switch_enable(motg, 0);
1001 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002
Steve Mucklef132c6c2012-06-06 18:30:57 -07001003 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301004 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001005 if (motg->async_irq)
1006 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001007 if (motg->pdata->pmic_id_irq)
1008 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001009 if (pdata->otg_control == OTG_PHY_CONTROL &&
1010 pdata->mpm_otgsessvld_int)
1011 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001012 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301013 if (bus)
1014 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1015
1016 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001017 /* Enable ASYNC IRQ (if present) during LPM */
1018 if (motg->async_irq)
1019 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301020 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301022
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001023 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301024
1025 return 0;
1026}
1027
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301028static int msm_otg_resume(struct msm_otg *motg)
1029{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001030 struct usb_phy *phy = &motg->phy;
1031 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001032 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301033 int cnt = 0;
1034 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001035 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001036 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301037
1038 if (!atomic_read(&motg->in_lpm))
1039 return 0;
1040
Jack Pham8978b892012-10-17 16:31:39 -07001041 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001042 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301043
Anji jonnala7da3f262011-12-02 17:22:14 -08001044 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301045 if (motg->lpm_flags & XO_SHUTDOWN) {
1046 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1047 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -07001048 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301049 "TCXO D0 buffer%d\n", __func__, ret);
1050 motg->lpm_flags &= ~XO_SHUTDOWN;
1051 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301052
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001053 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001054 clk_prepare_enable(motg->core_clk);
1055 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001056 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001057 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301058
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001059 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001060 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001061 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001062 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1063 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1064 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001065 }
1066
1067 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301068 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301069 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001070 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1071 phy_ctrl_val |= PHY_RETEN;
1072 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1073 /* Disable PHY HV interrupts */
1074 phy_ctrl_val &=
1075 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
1076 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301078 }
1079
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301080 temp = readl(USB_USBCMD);
1081 temp &= ~ASYNC_INTR_CTRL;
1082 temp &= ~ULPI_STP_CTRL;
1083 writel(temp, USB_USBCMD);
1084
1085 /*
1086 * PHY comes out of low power mode (LPM) in case of wakeup
1087 * from asynchronous interrupt.
1088 */
1089 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1090 goto skip_phy_resume;
1091
1092 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1093 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1094 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1095 break;
1096 udelay(1);
1097 cnt++;
1098 }
1099
1100 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1101 /*
1102 * This is a fatal error. Reset the link and
1103 * PHY. USB state can not be restored. Re-insertion
1104 * of USB cable is the only way to get USB working.
1105 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001106 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301107 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001108 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301109 }
1110
1111skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001112 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301113 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001114 if (motg->async_irq)
1115 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001116 if (motg->pdata->pmic_id_irq)
1117 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001118 if (pdata->otg_control == OTG_PHY_CONTROL &&
1119 pdata->mpm_otgsessvld_int)
1120 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001121 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301122 if (bus)
1123 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1124
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301125 atomic_set(&motg->in_lpm, 0);
1126
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301127 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001128 /* Match the disable_irq call from ISR */
1129 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301130 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301131 }
Jack Pham8978b892012-10-17 16:31:39 -07001132 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301133
Manu Gautamf8c45642012-08-10 10:20:56 -07001134 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1135 if (motg->async_irq)
1136 disable_irq(motg->async_irq);
1137
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001138 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301139
1140 return 0;
1141}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301142#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301143
David Keitel272ce522012-08-17 16:25:24 -07001144static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001145{
Jack Pham0c695282012-10-19 18:13:03 -07001146 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001147 pr_err("No USB power supply registered!\n");
1148 return;
1149 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001150
Jack Pham0c695282012-10-19 18:13:03 -07001151 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001152 /* legacy support */
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301153 if (host_mode) {
David Keitel272ce522012-08-17 16:25:24 -07001154 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301155 } else {
David Keitel272ce522012-08-17 16:25:24 -07001156 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
Pavankumar Kondeti811cab32012-11-09 20:51:36 +05301157 /*
1158 * VBUS comparator is disabled by PMIC charging driver
1159 * when SYSTEM scope is selected. For ID_GND->ID_A
1160 * transition, give 50 msec delay so that PMIC charger
1161 * driver detect the VBUS and ready for accepting
1162 * charging current value from USB.
1163 */
1164 if (test_bit(ID_A, &motg->inputs))
1165 msleep(50);
1166 }
David Keitel272ce522012-08-17 16:25:24 -07001167 } else {
1168 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001169 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001170 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001171}
1172
David Keitel081a3e22012-04-18 12:37:07 -07001173static int msm_otg_notify_chg_type(struct msm_otg *motg)
1174{
1175 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001176
David Keitel081a3e22012-04-18 12:37:07 -07001177 /*
1178 * TODO
1179 * Unify OTG driver charger types and power supply charger types
1180 */
1181 if (charger_type == motg->chg_type)
1182 return 0;
1183
1184 if (motg->chg_type == USB_SDP_CHARGER)
1185 charger_type = POWER_SUPPLY_TYPE_USB;
1186 else if (motg->chg_type == USB_CDP_CHARGER)
1187 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301188 else if (motg->chg_type == USB_DCP_CHARGER ||
1189 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001190 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1191 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1192 motg->chg_type == USB_ACA_A_CHARGER ||
1193 motg->chg_type == USB_ACA_B_CHARGER ||
1194 motg->chg_type == USB_ACA_C_CHARGER))
1195 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1196 else
1197 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1198
Jack Pham0c695282012-10-19 18:13:03 -07001199 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001200 pr_err("No USB power supply registered!\n");
1201 return -EINVAL;
1202 }
1203
1204 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001205 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001206 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001207}
1208
Amit Blay0f7edf72012-01-15 10:11:27 +02001209static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1210{
Jack Pham0c695282012-10-19 18:13:03 -07001211 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001212 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1213 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001214 }
1215
David Keitel272ce522012-08-17 16:25:24 -07001216 if (motg->cur_power == 0 && mA > 2) {
1217 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001218 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001219 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001220 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001221 goto psy_error;
1222 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1223 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001224 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001225 goto psy_error;
1226 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001227 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001228 goto psy_error;
1229 }
Jack Pham0c695282012-10-19 18:13:03 -07001230 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001231 return 0;
1232
David Keitel272ce522012-08-17 16:25:24 -07001233psy_error:
1234 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001235 return -ENXIO;
1236}
1237
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301238static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1239{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001240 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301241
1242 if (g && g->is_a_peripheral)
1243 return;
1244
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301245 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1246 motg->chg_type == USB_ACA_A_CHARGER ||
1247 motg->chg_type == USB_ACA_B_CHARGER ||
1248 motg->chg_type == USB_ACA_C_CHARGER) &&
1249 mA > IDEV_ACA_CHG_LIMIT)
1250 mA = IDEV_ACA_CHG_LIMIT;
1251
David Keitel081a3e22012-04-18 12:37:07 -07001252 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001253 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001254 "Failed notifying %d charger type to PMIC\n",
1255 motg->chg_type);
1256
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301257 if (motg->cur_power == mA)
1258 return;
1259
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001260 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001261
1262 /*
1263 * Use Power Supply API if supported, otherwise fallback
1264 * to legacy pm8921 API.
1265 */
1266 if (msm_otg_notify_power_supply(motg, mA))
1267 pm8921_charger_vbus_draw(mA);
1268
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301269 motg->cur_power = mA;
1270}
1271
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001272static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301273{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001274 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301275
1276 /*
1277 * Gadget driver uses set_power method to notify about the
1278 * available current based on suspend/configured states.
1279 *
1280 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1281 * states when CDP/ACA is connected.
1282 */
1283 if (motg->chg_type == USB_SDP_CHARGER)
1284 msm_otg_notify_charger(motg, mA);
1285
1286 return 0;
1287}
1288
Steve Mucklef132c6c2012-06-06 18:30:57 -07001289static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301290{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001291 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301292 struct msm_otg_platform_data *pdata = motg->pdata;
1293 struct usb_hcd *hcd;
1294
1295 if (!otg->host)
1296 return;
1297
1298 hcd = bus_to_hcd(otg->host);
1299
1300 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001301 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301302
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301303 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001304 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301305 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1306
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301307 /*
1308 * Some boards have a switch cotrolled by gpio
1309 * to enable/disable internal HUB. Enable internal
1310 * HUB before kicking the host.
1311 */
1312 if (pdata->setup_gpio)
1313 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301314 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301315 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001316 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301317
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301318 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301319 /* HCD core reset all bits of PORTSC. select ULPI phy */
1320 writel_relaxed(0x80000000, USB_PORTSC);
1321
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301322 if (pdata->setup_gpio)
1323 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301324
1325 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001326 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301327 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301328 }
1329}
1330
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331static int msm_otg_usbdev_notify(struct notifier_block *self,
1332 unsigned long action, void *priv)
1333{
1334 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001335 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301336 struct usb_device *udev = priv;
1337
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301338 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1339 goto out;
1340
Steve Mucklef132c6c2012-06-06 18:30:57 -07001341 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301342 goto out;
1343 /*
1344 * Interested in devices connected directly to the root hub.
1345 * ACA dock can supply IDEV_CHG irrespective devices connected
1346 * on the accessory port.
1347 */
1348 if (!udev->parent || udev->parent->parent ||
1349 motg->chg_type == USB_ACA_DOCK_CHARGER)
1350 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001351
1352 switch (action) {
1353 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301354 if (aca_enabled())
1355 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001356 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301357 pr_debug("B_CONN set\n");
1358 set_bit(B_CONN, &motg->inputs);
1359 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001360 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301361 /*
1362 * OTG PET: A-device must end session within
1363 * 10 sec after PET enumeration.
1364 */
1365 if (udev->quirks & USB_QUIRK_OTG_PET)
1366 msm_otg_start_timer(motg, TA_TST_MAINT,
1367 A_TST_MAINT);
1368 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301369 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001370 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001371 if (udev->actconfig)
1372 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1373 else
1374 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001375 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301376 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301377 break;
1378 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001379 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1380 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301381 pr_debug("B_CONN clear\n");
1382 clear_bit(B_CONN, &motg->inputs);
1383 /*
1384 * OTG PET: A-device must end session after
1385 * PET disconnection if it is enumerated
1386 * with bcdDevice[0] = 1. USB core sets
1387 * bus->otg_vbus_off for us. clear it here.
1388 */
1389 if (udev->bus->otg_vbus_off) {
1390 udev->bus->otg_vbus_off = 0;
1391 set_bit(A_BUS_DROP, &motg->inputs);
1392 }
1393 queue_work(system_nrt_wq, &motg->sm_work);
1394 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001395 default:
1396 break;
1397 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301398 if (test_bit(ID_A, &motg->inputs))
1399 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1400 motg->mA_port);
1401out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402 return NOTIFY_OK;
1403}
1404
Mayank Ranae3926882011-12-26 09:47:54 +05301405static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1406{
1407 int ret;
1408 static bool vbus_is_on;
1409
1410 if (vbus_is_on == on)
1411 return;
1412
1413 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301414 ret = motg->pdata->vbus_power(on);
1415 if (!ret)
1416 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301417 return;
1418 }
1419
1420 if (!vbus_otg) {
1421 pr_err("vbus_otg is NULL.");
1422 return;
1423 }
1424
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001425 /*
1426 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001427 * from usb before turning on the boost.
1428 * if exiting host mode disable the boost before enabling to draw
1429 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001430 */
Mayank Ranae3926882011-12-26 09:47:54 +05301431 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001432 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301433 ret = regulator_enable(vbus_otg);
1434 if (ret) {
1435 pr_err("unable to enable vbus_otg\n");
1436 return;
1437 }
1438 vbus_is_on = true;
1439 } else {
1440 ret = regulator_disable(vbus_otg);
1441 if (ret) {
1442 pr_err("unable to disable vbus_otg\n");
1443 return;
1444 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001445 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301446 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301447 }
1448}
1449
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001450static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301451{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001452 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301453 struct usb_hcd *hcd;
1454
1455 /*
1456 * Fail host registration if this board can support
1457 * only peripheral configuration.
1458 */
1459 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001460 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301461 return -ENODEV;
1462 }
1463
Mayank Ranae3926882011-12-26 09:47:54 +05301464 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001465 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301466 if (IS_ERR(vbus_otg)) {
1467 pr_err("Unable to get vbus_otg\n");
1468 return -ENODEV;
1469 }
1470 }
1471
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301472 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001473 if (otg->phy->state == OTG_STATE_A_HOST) {
1474 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001475 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301476 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301477 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301478 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001479 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301480 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301481 } else {
1482 otg->host = NULL;
1483 }
1484
1485 return 0;
1486 }
1487
1488 hcd = bus_to_hcd(host);
1489 hcd->power_budget = motg->pdata->power_budget;
1490
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301491#ifdef CONFIG_USB_OTG
1492 host->otg_port = 1;
1493#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001494 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1495 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301496 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001497 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301498
1499 /*
1500 * Kick the state machine work, if peripheral is not supported
1501 * or peripheral is already registered with us.
1502 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301503 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001504 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301505 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301506 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301507
1508 return 0;
1509}
1510
Steve Mucklef132c6c2012-06-06 18:30:57 -07001511static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301512{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301513 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001514 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301515 struct msm_otg_platform_data *pdata = motg->pdata;
1516
1517 if (!otg->gadget)
1518 return;
1519
1520 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001521 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301522 /*
1523 * Some boards have a switch cotrolled by gpio
1524 * to enable/disable internal HUB. Disable internal
1525 * HUB before kicking the gadget.
1526 */
1527 if (pdata->setup_gpio)
1528 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001529
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301530 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301531 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301532 ret = msm_bus_scale_client_update_request(
1533 motg->bus_perf_client, 1);
1534 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001535 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301536 "bus bandwidth %d\n", __func__, ret);
1537 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301538 usb_gadget_vbus_connect(otg->gadget);
1539 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001540 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301541 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301542 /* Configure BUS performance parameters to default */
1543 if (motg->bus_perf_client) {
1544 ret = msm_bus_scale_client_update_request(
1545 motg->bus_perf_client, 0);
1546 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001547 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301548 "for bus bw %d\n", __func__, ret);
1549 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301550 if (pdata->setup_gpio)
1551 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1552 }
1553
1554}
1555
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001556static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301557 struct usb_gadget *gadget)
1558{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001559 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301560
1561 /*
1562 * Fail peripheral registration if this board can support
1563 * only host configuration.
1564 */
1565 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001566 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301567 return -ENODEV;
1568 }
1569
1570 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001571 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1572 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301573 msm_otg_start_peripheral(otg, 0);
1574 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001575 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301576 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301577 } else {
1578 otg->gadget = NULL;
1579 }
1580
1581 return 0;
1582 }
1583 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001584 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301585
1586 /*
1587 * Kick the state machine work, if host is not supported
1588 * or host is already registered with us.
1589 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301590 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001591 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301592 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301593 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301594
1595 return 0;
1596}
1597
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301598static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1599{
1600 unsigned long flags;
1601 int id;
1602
1603 if (!motg->pdata->pmic_id_irq)
1604 return -ENODEV;
1605
1606 local_irq_save(flags);
1607 id = irq_read_line(motg->pdata->pmic_id_irq);
1608 local_irq_restore(flags);
1609
1610 /*
1611 * If we can not read ID line state for some reason, treat
1612 * it as float. This would prevent MHL discovery and kicking
1613 * host mode unnecessarily.
1614 */
1615 return !!id;
1616}
1617
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301618static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1619 void (*callback)(int on))
1620{
1621 struct usb_phy *phy = &motg->phy;
1622 int ret;
1623
Manoj Raoa7bddd12012-08-27 20:36:45 -07001624 if (!motg->pdata->mhl_enable) {
1625 dev_dbg(phy->dev, "MHL feature not enabled\n");
1626 return -ENODEV;
1627 }
1628
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301629 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1630 !motg->pdata->pmic_id_irq) {
1631 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1632 return -ENODEV;
1633 }
1634
1635 if (!motg->pdata->mhl_dev_name) {
1636 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1637 return -ENODEV;
1638 }
1639
1640 if (callback)
1641 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1642 callback);
1643 else
1644 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1645
1646 if (ret)
1647 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1648 motg->pdata->mhl_dev_name, ret);
1649 else
1650 motg->mhl_enabled = true;
1651
1652 return ret;
1653}
1654
1655static void msm_otg_mhl_notify_online(int on)
1656{
1657 struct msm_otg *motg = the_msm_otg;
1658 struct usb_phy *phy = &motg->phy;
1659 bool queue = false;
1660
1661 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1662
1663 if (on) {
1664 set_bit(MHL, &motg->inputs);
1665 } else {
1666 clear_bit(MHL, &motg->inputs);
1667 queue = true;
1668 }
1669
1670 if (queue && phy->state != OTG_STATE_UNDEFINED)
1671 schedule_work(&motg->sm_work);
1672}
1673
1674static bool msm_otg_is_mhl(struct msm_otg *motg)
1675{
1676 struct usb_phy *phy = &motg->phy;
1677 int is_mhl, ret;
1678
1679 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1680 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1681 /*
1682 * MHL driver calls our callback saying that MHL connected
1683 * if RID_GND is detected. But at later part of discovery
1684 * it may figure out MHL is not connected and returns
1685 * false. Hence clear MHL input here.
1686 */
1687 clear_bit(MHL, &motg->inputs);
1688 dev_dbg(phy->dev, "MHL device not found\n");
1689 return false;
1690 }
1691
1692 set_bit(MHL, &motg->inputs);
1693 dev_dbg(phy->dev, "MHL device found\n");
1694 return true;
1695}
1696
1697static bool msm_chg_mhl_detect(struct msm_otg *motg)
1698{
1699 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301700
1701 if (!motg->mhl_enabled)
1702 return false;
1703
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301704 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301705
1706 if (id)
1707 return false;
1708
1709 mhl_det_in_progress = true;
1710 ret = msm_otg_is_mhl(motg);
1711 mhl_det_in_progress = false;
1712
1713 return ret;
1714}
1715
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301716static void msm_otg_chg_check_timer_func(unsigned long data)
1717{
1718 struct msm_otg *motg = (struct msm_otg *) data;
1719 struct usb_otg *otg = motg->phy.otg;
1720
1721 if (atomic_read(&motg->in_lpm) ||
1722 !test_bit(B_SESS_VLD, &motg->inputs) ||
1723 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1724 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1725 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1726 return;
1727 }
1728
1729 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1730 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1731 set_bit(B_FALSE_SDP, &motg->inputs);
1732 queue_work(system_nrt_wq, &motg->sm_work);
1733 }
1734}
1735
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001736static bool msm_chg_aca_detect(struct msm_otg *motg)
1737{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001738 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001739 u32 int_sts;
1740 bool ret = false;
1741
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301742 if (!aca_enabled())
1743 goto out;
1744
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001745 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1746 goto out;
1747
Steve Mucklef132c6c2012-06-06 18:30:57 -07001748 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001749 switch (int_sts & 0x1C) {
1750 case 0x08:
1751 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001752 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001753 motg->chg_type = USB_ACA_A_CHARGER;
1754 motg->chg_state = USB_CHG_STATE_DETECTED;
1755 clear_bit(ID_B, &motg->inputs);
1756 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301757 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001758 ret = true;
1759 }
1760 break;
1761 case 0x0C:
1762 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001763 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001764 motg->chg_type = USB_ACA_B_CHARGER;
1765 motg->chg_state = USB_CHG_STATE_DETECTED;
1766 clear_bit(ID_A, &motg->inputs);
1767 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301768 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769 ret = true;
1770 }
1771 break;
1772 case 0x10:
1773 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001774 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001775 motg->chg_type = USB_ACA_C_CHARGER;
1776 motg->chg_state = USB_CHG_STATE_DETECTED;
1777 clear_bit(ID_A, &motg->inputs);
1778 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301779 set_bit(ID, &motg->inputs);
1780 ret = true;
1781 }
1782 break;
1783 case 0x04:
1784 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001785 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301786 motg->chg_type = USB_INVALID_CHARGER;
1787 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1788 clear_bit(ID_A, &motg->inputs);
1789 clear_bit(ID_B, &motg->inputs);
1790 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001791 ret = true;
1792 }
1793 break;
1794 default:
1795 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1796 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301797 test_and_clear_bit(ID_C, &motg->inputs) |
1798 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001799 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001800 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001801 motg->chg_type = USB_INVALID_CHARGER;
1802 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1803 }
1804 }
1805out:
1806 return ret;
1807}
1808
1809static void msm_chg_enable_aca_det(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
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301813 if (!aca_enabled())
1814 return;
1815
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001816 switch (motg->pdata->phy_type) {
1817 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301818 /* Disable ID_GND in link and PHY */
1819 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1820 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001821 ulpi_write(phy, 0x01, 0x0C);
1822 ulpi_write(phy, 0x10, 0x0F);
1823 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301824 /* Disable PMIC ID pull-up */
1825 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301826 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001827 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301828 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001829 break;
1830 default:
1831 break;
1832 }
1833}
1834
1835static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1836{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001837 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001838
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301839 if (!aca_enabled())
1840 return;
1841
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 switch (motg->pdata->phy_type) {
1843 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301844 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001845 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301846 break;
1847 default:
1848 break;
1849 }
1850}
1851
1852static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1853{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001854 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301855
1856 if (!aca_enabled())
1857 return;
1858
1859 switch (motg->pdata->phy_type) {
1860 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001861 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001862 break;
1863 default:
1864 break;
1865 }
1866}
1867
1868static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1869{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001870 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871 bool ret = false;
1872
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301873 if (!aca_enabled())
1874 return ret;
1875
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001876 switch (motg->pdata->phy_type) {
1877 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001878 if (ulpi_read(phy, 0x91) & 1) {
1879 dev_dbg(phy->dev, "RID change\n");
1880 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001881 ret = msm_chg_aca_detect(motg);
1882 }
1883 default:
1884 break;
1885 }
1886 return ret;
1887}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301888
1889static void msm_otg_id_timer_func(unsigned long data)
1890{
1891 struct msm_otg *motg = (struct msm_otg *) data;
1892
1893 if (!aca_enabled())
1894 return;
1895
1896 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001897 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301898 return;
1899 }
1900
Steve Mucklef132c6c2012-06-06 18:30:57 -07001901 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301902 goto out;
1903
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301904 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001905 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301906 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301907 }
1908
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301909out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301910 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1911 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1912}
1913
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301914static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1915{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001916 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301917 u32 chg_det;
1918 bool ret = false;
1919
1920 switch (motg->pdata->phy_type) {
1921 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001922 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301923 ret = chg_det & (1 << 4);
1924 break;
1925 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001926 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301927 ret = chg_det & 1;
1928 break;
1929 default:
1930 break;
1931 }
1932 return ret;
1933}
1934
1935static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1936{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001937 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301938 u32 chg_det;
1939
1940 switch (motg->pdata->phy_type) {
1941 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001942 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301943 /* Turn off charger block */
1944 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001945 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301946 udelay(20);
1947 /* control chg block via ULPI */
1948 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001949 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301950 /* put it in host mode for enabling D- source */
1951 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001952 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301953 /* Turn on chg detect block */
1954 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001955 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301956 udelay(20);
1957 /* enable chg detection */
1958 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001959 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301960 break;
1961 case SNPS_28NM_INTEGRATED_PHY:
1962 /*
1963 * Configure DM as current source, DP as current sink
1964 * and enable battery charging comparators.
1965 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001966 ulpi_write(phy, 0x8, 0x85);
1967 ulpi_write(phy, 0x2, 0x85);
1968 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301969 break;
1970 default:
1971 break;
1972 }
1973}
1974
1975static bool msm_chg_check_primary_det(struct msm_otg *motg)
1976{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001977 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301978 u32 chg_det;
1979 bool ret = false;
1980
1981 switch (motg->pdata->phy_type) {
1982 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001983 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301984 ret = chg_det & (1 << 4);
1985 break;
1986 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001987 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301988 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301989 /* Turn off VDP_SRC */
1990 ulpi_write(phy, 0x3, 0x86);
1991 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301992 break;
1993 default:
1994 break;
1995 }
1996 return ret;
1997}
1998
1999static void msm_chg_enable_primary_det(struct msm_otg *motg)
2000{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002001 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302002 u32 chg_det;
2003
2004 switch (motg->pdata->phy_type) {
2005 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002006 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302007 /* enable chg detection */
2008 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002009 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302010 break;
2011 case SNPS_28NM_INTEGRATED_PHY:
2012 /*
2013 * Configure DP as current source, DM as current sink
2014 * and enable battery charging comparators.
2015 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002016 ulpi_write(phy, 0x2, 0x85);
2017 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302018 break;
2019 default:
2020 break;
2021 }
2022}
2023
2024static bool msm_chg_check_dcd(struct msm_otg *motg)
2025{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002026 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302027 u32 line_state;
2028 bool ret = false;
2029
2030 switch (motg->pdata->phy_type) {
2031 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002032 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302033 ret = !(line_state & 1);
2034 break;
2035 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002036 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302037 ret = line_state & 2;
2038 break;
2039 default:
2040 break;
2041 }
2042 return ret;
2043}
2044
2045static void msm_chg_disable_dcd(struct msm_otg *motg)
2046{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002047 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302048 u32 chg_det;
2049
2050 switch (motg->pdata->phy_type) {
2051 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002052 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302053 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002054 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302055 break;
2056 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002057 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302058 break;
2059 default:
2060 break;
2061 }
2062}
2063
2064static void msm_chg_enable_dcd(struct msm_otg *motg)
2065{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002066 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302067 u32 chg_det;
2068
2069 switch (motg->pdata->phy_type) {
2070 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002071 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302072 /* Turn on D+ current source */
2073 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002074 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302075 break;
2076 case SNPS_28NM_INTEGRATED_PHY:
2077 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002078 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302079 break;
2080 default:
2081 break;
2082 }
2083}
2084
2085static void msm_chg_block_on(struct msm_otg *motg)
2086{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002087 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302088 u32 func_ctrl, chg_det;
2089
2090 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002091 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302092 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2093 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002094 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302095
2096 switch (motg->pdata->phy_type) {
2097 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002098 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302099 /* control chg block via ULPI */
2100 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002101 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302102 /* Turn on chg detect block */
2103 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002104 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302105 udelay(20);
2106 break;
2107 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302108 /* disable DP and DM pull down resistors */
2109 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302110 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002111 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302112 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002113 ulpi_write(phy, 0x1F, 0x92);
2114 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302115 udelay(100);
2116 break;
2117 default:
2118 break;
2119 }
2120}
2121
2122static void msm_chg_block_off(struct msm_otg *motg)
2123{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002124 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302125 u32 func_ctrl, chg_det;
2126
2127 switch (motg->pdata->phy_type) {
2128 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002129 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302130 /* Turn off charger block */
2131 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002132 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302133 break;
2134 case SNPS_28NM_INTEGRATED_PHY:
2135 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002136 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302137 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002138 ulpi_write(phy, 0x1F, 0x92);
2139 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302140 break;
2141 default:
2142 break;
2143 }
2144
2145 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002146 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302147 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2148 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002149 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302150}
2151
Anji jonnalad270e2d2011-08-09 11:28:32 +05302152static const char *chg_to_string(enum usb_chg_type chg_type)
2153{
2154 switch (chg_type) {
2155 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2156 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2157 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2158 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2159 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2160 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2161 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302162 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302163 default: return "INVALID_CHARGER";
2164 }
2165}
2166
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302167#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
2168#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302169#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2170#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302171static void msm_chg_detect_work(struct work_struct *w)
2172{
2173 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002174 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302175 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302176 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302177 unsigned long delay;
2178
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002179 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302180
2181 if (test_bit(MHL, &motg->inputs)) {
2182 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2183 return;
2184 }
2185
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302186 switch (motg->chg_state) {
2187 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302188 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302189 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002190 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302191 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2192 motg->dcd_retries = 0;
2193 delay = MSM_CHG_DCD_POLL_TIME;
2194 break;
2195 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302196 if (msm_chg_mhl_detect(motg)) {
2197 msm_chg_block_off(motg);
2198 motg->chg_state = USB_CHG_STATE_DETECTED;
2199 motg->chg_type = USB_INVALID_CHARGER;
2200 queue_work(system_nrt_wq, &motg->sm_work);
2201 return;
2202 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203 is_aca = msm_chg_aca_detect(motg);
2204 if (is_aca) {
2205 /*
2206 * ID_A can be ACA dock too. continue
2207 * primary detection after DCD.
2208 */
2209 if (test_bit(ID_A, &motg->inputs)) {
2210 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2211 } else {
2212 delay = 0;
2213 break;
2214 }
2215 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302216 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302217 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2218 if (is_dcd || tmout) {
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302219 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302220 msm_chg_enable_primary_det(motg);
2221 delay = MSM_CHG_PRIMARY_DET_TIME;
2222 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2223 } else {
2224 delay = MSM_CHG_DCD_POLL_TIME;
2225 }
2226 break;
2227 case USB_CHG_STATE_DCD_DONE:
2228 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302229 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2230 dm_vlgc = line_state & PORTSC_LS_DM;
2231 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302232 if (test_bit(ID_A, &motg->inputs)) {
2233 motg->chg_type = USB_ACA_DOCK_CHARGER;
2234 motg->chg_state = USB_CHG_STATE_DETECTED;
2235 delay = 0;
2236 break;
2237 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302238 if (line_state) { /* DP > VLGC */
2239 motg->chg_type = USB_PROPRIETARY_CHARGER;
2240 motg->chg_state = USB_CHG_STATE_DETECTED;
2241 delay = 0;
2242 } else {
2243 msm_chg_enable_secondary_det(motg);
2244 delay = MSM_CHG_SECONDARY_DET_TIME;
2245 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2246 }
2247 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302248 if (test_bit(ID_A, &motg->inputs)) {
2249 motg->chg_type = USB_ACA_A_CHARGER;
2250 motg->chg_state = USB_CHG_STATE_DETECTED;
2251 delay = 0;
2252 break;
2253 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302254
2255 if (line_state) /* DP > VLGC or/and DM > VLGC */
2256 motg->chg_type = USB_PROPRIETARY_CHARGER;
2257 else
2258 motg->chg_type = USB_SDP_CHARGER;
2259
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302260 motg->chg_state = USB_CHG_STATE_DETECTED;
2261 delay = 0;
2262 }
2263 break;
2264 case USB_CHG_STATE_PRIMARY_DONE:
2265 vout = msm_chg_check_secondary_det(motg);
2266 if (vout)
2267 motg->chg_type = USB_DCP_CHARGER;
2268 else
2269 motg->chg_type = USB_CDP_CHARGER;
2270 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2271 /* fall through */
2272 case USB_CHG_STATE_SECONDARY_DONE:
2273 motg->chg_state = USB_CHG_STATE_DETECTED;
2274 case USB_CHG_STATE_DETECTED:
2275 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002276 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302277 /*
2278 * Spurious interrupt is seen after enabling ACA detection
2279 * due to which charger detection fails in case of PET.
2280 * Add delay of 100 microsec to avoid that.
2281 */
2282 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002283 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002284 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302285 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302286 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302287 return;
2288 default:
2289 return;
2290 }
2291
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302292 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302293}
2294
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302295/*
2296 * We support OTG, Peripheral only and Host only configurations. In case
2297 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2298 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2299 * enabled when switch is controlled by user and default mode is supplied
2300 * by board file, which can be changed by userspace later.
2301 */
2302static void msm_otg_init_sm(struct msm_otg *motg)
2303{
2304 struct msm_otg_platform_data *pdata = motg->pdata;
2305 u32 otgsc = readl(USB_OTGSC);
2306
2307 switch (pdata->mode) {
2308 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002309 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302310 if (pdata->default_mode == USB_HOST) {
2311 clear_bit(ID, &motg->inputs);
2312 } else if (pdata->default_mode == USB_PERIPHERAL) {
2313 set_bit(ID, &motg->inputs);
2314 set_bit(B_SESS_VLD, &motg->inputs);
2315 } else {
2316 set_bit(ID, &motg->inputs);
2317 clear_bit(B_SESS_VLD, &motg->inputs);
2318 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302319 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302320 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302321 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302322 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302323 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302324 set_bit(A_BUS_REQ, &motg->inputs);
2325 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002326 if (otgsc & OTGSC_BSV)
2327 set_bit(B_SESS_VLD, &motg->inputs);
2328 else
2329 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302330 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302331 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302332 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302333 set_bit(ID, &motg->inputs);
2334 else
2335 clear_bit(ID, &motg->inputs);
2336 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302337 /*
2338 * VBUS initial state is reported after PMIC
2339 * driver initialization. Wait for it.
2340 */
2341 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302342 }
2343 break;
2344 case USB_HOST:
2345 clear_bit(ID, &motg->inputs);
2346 break;
2347 case USB_PERIPHERAL:
2348 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302349 if (pdata->otg_control == OTG_PHY_CONTROL) {
2350 if (otgsc & OTGSC_BSV)
2351 set_bit(B_SESS_VLD, &motg->inputs);
2352 else
2353 clear_bit(B_SESS_VLD, &motg->inputs);
2354 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2355 /*
2356 * VBUS initial state is reported after PMIC
2357 * driver initialization. Wait for it.
2358 */
2359 wait_for_completion(&pmic_vbus_init);
2360 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302361 break;
2362 default:
2363 break;
2364 }
2365}
2366
2367static void msm_otg_sm_work(struct work_struct *w)
2368{
2369 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002370 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302371 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302372
Steve Mucklef132c6c2012-06-06 18:30:57 -07002373 pm_runtime_resume(otg->phy->dev);
2374 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002375 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302376 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002377 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302378 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002379 if (!psy && legacy_power_supply) {
2380 psy = power_supply_get_by_name("usb");
2381
2382 if (!psy)
2383 pr_err("couldn't get usb power supply\n");
2384 }
2385
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002386 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302387 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2388 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002389 pm_runtime_put_noidle(otg->phy->dev);
2390 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302391 break;
2392 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302393 /* FALL THROUGH */
2394 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302395 if (test_bit(MHL, &motg->inputs)) {
2396 /* allow LPM */
2397 pm_runtime_put_noidle(otg->phy->dev);
2398 pm_runtime_suspend(otg->phy->dev);
2399 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002400 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302401 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302402 if (msm_chg_mhl_detect(motg)) {
2403 work = 1;
2404 break;
2405 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302406 clear_bit(B_BUS_REQ, &motg->inputs);
2407 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002408 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302409 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302410 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302411 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302412 switch (motg->chg_state) {
2413 case USB_CHG_STATE_UNDEFINED:
2414 msm_chg_detect_work(&motg->chg_work.work);
2415 break;
2416 case USB_CHG_STATE_DETECTED:
2417 switch (motg->chg_type) {
2418 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302419 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002420 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302421 /* fall through */
2422 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302423 msm_otg_notify_charger(motg,
2424 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002425 pm_runtime_put_noidle(otg->phy->dev);
2426 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302427 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302428 case USB_ACA_B_CHARGER:
2429 msm_otg_notify_charger(motg,
2430 IDEV_ACA_CHG_MAX);
2431 /*
2432 * (ID_B --> ID_C) PHY_ALT interrupt can
2433 * not be detected in LPM.
2434 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302435 break;
2436 case USB_CDP_CHARGER:
2437 msm_otg_notify_charger(motg,
2438 IDEV_CHG_MAX);
2439 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002440 otg->phy->state =
2441 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302442 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302443 case USB_ACA_C_CHARGER:
2444 msm_otg_notify_charger(motg,
2445 IDEV_ACA_CHG_MAX);
2446 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002447 otg->phy->state =
2448 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302449 break;
2450 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302451 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002452 otg->phy->state =
2453 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302454 mod_timer(&motg->chg_check_timer,
2455 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302456 break;
2457 default:
2458 break;
2459 }
2460 break;
2461 default:
2462 break;
2463 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302464 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2465 pr_debug("b_sess_end && b_bus_req\n");
2466 if (msm_otg_start_srp(otg) < 0) {
2467 clear_bit(B_BUS_REQ, &motg->inputs);
2468 work = 1;
2469 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302470 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002471 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302472 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2473 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302474 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302475 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302476 del_timer_sync(&motg->chg_check_timer);
2477 clear_bit(B_FALSE_SDP, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302478 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302479 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2480 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302481 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002482 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302483 /*
2484 * There is a small window where ID interrupt
2485 * is not monitored during ID detection circuit
2486 * switch from ACA to PMIC. Check ID state
2487 * before entering into low power mode.
2488 */
2489 if (!msm_otg_read_pmic_id_state(motg)) {
2490 pr_debug("process missed ID intr\n");
2491 clear_bit(ID, &motg->inputs);
2492 work = 1;
2493 break;
2494 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002495 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002496 /*
2497 * Only if autosuspend was enabled in probe, it will be
2498 * used here. Otherwise, no delay will be used.
2499 */
2500 pm_runtime_mark_last_busy(otg->phy->dev);
2501 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302502 }
2503 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302504 case OTG_STATE_B_SRP_INIT:
2505 if (!test_bit(ID, &motg->inputs) ||
2506 test_bit(ID_A, &motg->inputs) ||
2507 test_bit(ID_C, &motg->inputs) ||
2508 (test_bit(B_SESS_VLD, &motg->inputs) &&
2509 !test_bit(ID_B, &motg->inputs))) {
2510 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2511 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002512 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302513 /*
2514 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2515 * bits after SRP initiation.
2516 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002517 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302518 work = 1;
2519 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2520 pr_debug("b_srp_fail\n");
2521 pr_info("A-device did not respond to SRP\n");
2522 clear_bit(B_BUS_REQ, &motg->inputs);
2523 clear_bit(B_SRP_FAIL, &motg->tmouts);
2524 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002525 ulpi_write(otg->phy, 0x0, 0x98);
2526 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302527 motg->b_last_se0_sess = jiffies;
2528 work = 1;
2529 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302530 break;
2531 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302532 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2533 test_bit(B_FALSE_SDP, &motg->inputs)) {
2534 pr_debug("B_FALSE_SDP\n");
2535 msm_otg_start_peripheral(otg, 0);
2536 motg->chg_type = USB_DCP_CHARGER;
2537 clear_bit(B_FALSE_SDP, &motg->inputs);
2538 otg->phy->state = OTG_STATE_B_IDLE;
2539 work = 1;
2540 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302541 test_bit(ID_A, &motg->inputs) ||
2542 test_bit(ID_B, &motg->inputs) ||
2543 !test_bit(B_SESS_VLD, &motg->inputs)) {
2544 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302545 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2546 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302547 msm_otg_notify_charger(motg, 0);
2548 srp_reqd = otg->gadget->otg_srp_reqd;
2549 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302550 if (test_bit(ID_B, &motg->inputs))
2551 clear_bit(ID_B, &motg->inputs);
2552 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002553 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302554 motg->b_last_se0_sess = jiffies;
2555 if (srp_reqd)
2556 msm_otg_start_timer(motg,
2557 TB_TST_SRP, B_TST_SRP);
2558 else
2559 work = 1;
2560 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2561 otg->gadget->b_hnp_enable &&
2562 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2563 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2564 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2565 /* D+ pullup should not be disconnected within 4msec
2566 * after A device suspends the bus. Otherwise PET will
2567 * fail the compliance test.
2568 */
2569 udelay(1000);
2570 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002571 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302572 /*
2573 * start HCD even before A-device enable
2574 * pull-up to meet HNP timings.
2575 */
2576 otg->host->is_b_host = 1;
2577 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002578 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2579 test_bit(B_SESS_VLD, &motg->inputs)) {
2580 pr_debug("a_bus_suspend && b_sess_vld\n");
2581 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002582 pm_runtime_put_noidle(otg->phy->dev);
2583 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002584 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002585 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302586 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002587 }
2588 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302589 case OTG_STATE_B_WAIT_ACON:
2590 if (!test_bit(ID, &motg->inputs) ||
2591 test_bit(ID_A, &motg->inputs) ||
2592 test_bit(ID_B, &motg->inputs) ||
2593 !test_bit(B_SESS_VLD, &motg->inputs)) {
2594 pr_debug("!id || id_a/b || !b_sess_vld\n");
2595 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302596 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302597 * A-device is physically disconnected during
2598 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302599 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302600 msm_otg_start_host(otg, 0);
2601 otg->host->is_b_host = 0;
2602
2603 clear_bit(B_BUS_REQ, &motg->inputs);
2604 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2605 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002606 otg->phy->state = OTG_STATE_B_IDLE;
2607 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302608 work = 1;
2609 } else if (test_bit(A_CONN, &motg->inputs)) {
2610 pr_debug("a_conn\n");
2611 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002612 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302613 /*
2614 * PET disconnects D+ pullup after reset is generated
2615 * by B device in B_HOST role which is not detected by
2616 * B device. As workaorund , start timer of 300msec
2617 * and stop timer if A device is enumerated else clear
2618 * A_CONN.
2619 */
2620 msm_otg_start_timer(motg, TB_TST_CONFIG,
2621 B_TST_CONFIG);
2622 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2623 pr_debug("b_ase0_brst_tmout\n");
2624 pr_info("B HNP fail:No response from A device\n");
2625 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002626 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302627 otg->host->is_b_host = 0;
2628 clear_bit(B_ASE0_BRST, &motg->tmouts);
2629 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2630 clear_bit(B_BUS_REQ, &motg->inputs);
2631 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002632 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302633 work = 1;
2634 } else if (test_bit(ID_C, &motg->inputs)) {
2635 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2636 }
2637 break;
2638 case OTG_STATE_B_HOST:
2639 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2640 !test_bit(A_CONN, &motg->inputs) ||
2641 !test_bit(B_SESS_VLD, &motg->inputs)) {
2642 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2643 clear_bit(A_CONN, &motg->inputs);
2644 clear_bit(B_BUS_REQ, &motg->inputs);
2645 msm_otg_start_host(otg, 0);
2646 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002647 otg->phy->state = OTG_STATE_B_IDLE;
2648 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302649 work = 1;
2650 } else if (test_bit(ID_C, &motg->inputs)) {
2651 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2652 }
2653 break;
2654 case OTG_STATE_A_IDLE:
2655 otg->default_a = 1;
2656 if (test_bit(ID, &motg->inputs) &&
2657 !test_bit(ID_A, &motg->inputs)) {
2658 pr_debug("id && !id_a\n");
2659 otg->default_a = 0;
2660 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002661 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302662 del_timer_sync(&motg->id_timer);
2663 msm_otg_link_reset(motg);
2664 msm_chg_enable_aca_intr(motg);
2665 msm_otg_notify_charger(motg, 0);
2666 work = 1;
2667 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2668 (test_bit(A_SRP_DET, &motg->inputs) ||
2669 test_bit(A_BUS_REQ, &motg->inputs))) {
2670 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2671
2672 clear_bit(A_SRP_DET, &motg->inputs);
2673 /* Disable SRP detection */
2674 writel_relaxed((readl_relaxed(USB_OTGSC) &
2675 ~OTGSC_INTSTS_MASK) &
2676 ~OTGSC_DPIE, USB_OTGSC);
2677
Steve Mucklef132c6c2012-06-06 18:30:57 -07002678 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302679 /* VBUS should not be supplied before end of SRP pulse
2680 * generated by PET, if not complaince test fail.
2681 */
2682 usleep_range(10000, 12000);
2683 /* ACA: ID_A: Stop charging untill enumeration */
2684 if (test_bit(ID_A, &motg->inputs))
2685 msm_otg_notify_charger(motg, 0);
2686 else
2687 msm_hsusb_vbus_power(motg, 1);
2688 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2689 } else {
2690 pr_debug("No session requested\n");
2691 clear_bit(A_BUS_DROP, &motg->inputs);
2692 if (test_bit(ID_A, &motg->inputs)) {
2693 msm_otg_notify_charger(motg,
2694 IDEV_ACA_CHG_MAX);
2695 } else if (!test_bit(ID, &motg->inputs)) {
2696 msm_otg_notify_charger(motg, 0);
2697 /*
2698 * A-device is not providing power on VBUS.
2699 * Enable SRP detection.
2700 */
2701 writel_relaxed(0x13, USB_USBMODE);
2702 writel_relaxed((readl_relaxed(USB_OTGSC) &
2703 ~OTGSC_INTSTS_MASK) |
2704 OTGSC_DPIE, USB_OTGSC);
2705 mb();
2706 }
2707 }
2708 break;
2709 case OTG_STATE_A_WAIT_VRISE:
2710 if ((test_bit(ID, &motg->inputs) &&
2711 !test_bit(ID_A, &motg->inputs)) ||
2712 test_bit(A_BUS_DROP, &motg->inputs) ||
2713 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2714 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2715 clear_bit(A_BUS_REQ, &motg->inputs);
2716 msm_otg_del_timer(motg);
2717 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002718 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302719 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2720 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2721 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002722 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302723 if (TA_WAIT_BCON > 0)
2724 msm_otg_start_timer(motg, TA_WAIT_BCON,
2725 A_WAIT_BCON);
2726 msm_otg_start_host(otg, 1);
2727 msm_chg_enable_aca_det(motg);
2728 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302729 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302730 if (msm_chg_check_aca_intr(motg))
2731 work = 1;
2732 }
2733 break;
2734 case OTG_STATE_A_WAIT_BCON:
2735 if ((test_bit(ID, &motg->inputs) &&
2736 !test_bit(ID_A, &motg->inputs)) ||
2737 test_bit(A_BUS_DROP, &motg->inputs) ||
2738 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2739 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2740 "a_wait_bcon_tmout\n");
2741 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2742 pr_info("Device No Response\n");
2743 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2744 }
2745 msm_otg_del_timer(motg);
2746 clear_bit(A_BUS_REQ, &motg->inputs);
2747 clear_bit(B_CONN, &motg->inputs);
2748 msm_otg_start_host(otg, 0);
2749 /*
2750 * ACA: ID_A with NO accessory, just the A plug is
2751 * attached to ACA: Use IDCHG_MAX for charging
2752 */
2753 if (test_bit(ID_A, &motg->inputs))
2754 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2755 else
2756 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002757 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302758 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2759 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2760 pr_debug("!a_vbus_vld\n");
2761 clear_bit(B_CONN, &motg->inputs);
2762 msm_otg_del_timer(motg);
2763 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002764 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2765 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302766 } else if (test_bit(ID_A, &motg->inputs)) {
2767 msm_hsusb_vbus_power(motg, 0);
2768 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2769 /*
2770 * If TA_WAIT_BCON is infinite, we don;t
2771 * turn off VBUS. Enter low power mode.
2772 */
2773 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002774 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302775 } else if (!test_bit(ID, &motg->inputs)) {
2776 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302777 }
2778 break;
2779 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302780 if ((test_bit(ID, &motg->inputs) &&
2781 !test_bit(ID_A, &motg->inputs)) ||
2782 test_bit(A_BUS_DROP, &motg->inputs)) {
2783 pr_debug("id_a/b/c || a_bus_drop\n");
2784 clear_bit(B_CONN, &motg->inputs);
2785 clear_bit(A_BUS_REQ, &motg->inputs);
2786 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002787 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302788 msm_otg_start_host(otg, 0);
2789 if (!test_bit(ID_A, &motg->inputs))
2790 msm_hsusb_vbus_power(motg, 0);
2791 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2792 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2793 pr_debug("!a_vbus_vld\n");
2794 clear_bit(B_CONN, &motg->inputs);
2795 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002796 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302797 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002798 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302799 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2800 /*
2801 * a_bus_req is de-asserted when root hub is
2802 * suspended or HNP is in progress.
2803 */
2804 pr_debug("!a_bus_req\n");
2805 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002806 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302807 if (otg->host->b_hnp_enable)
2808 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2809 A_AIDL_BDIS);
2810 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002811 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302812 } else if (!test_bit(B_CONN, &motg->inputs)) {
2813 pr_debug("!b_conn\n");
2814 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002815 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302816 if (TA_WAIT_BCON > 0)
2817 msm_otg_start_timer(motg, TA_WAIT_BCON,
2818 A_WAIT_BCON);
2819 if (msm_chg_check_aca_intr(motg))
2820 work = 1;
2821 } else if (test_bit(ID_A, &motg->inputs)) {
2822 msm_otg_del_timer(motg);
2823 msm_hsusb_vbus_power(motg, 0);
2824 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2825 msm_otg_notify_charger(motg,
2826 IDEV_ACA_CHG_MAX);
2827 else
2828 msm_otg_notify_charger(motg,
2829 IDEV_CHG_MIN - motg->mA_port);
2830 } else if (!test_bit(ID, &motg->inputs)) {
2831 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2832 motg->chg_type = USB_INVALID_CHARGER;
2833 msm_otg_notify_charger(motg, 0);
2834 msm_hsusb_vbus_power(motg, 1);
2835 }
2836 break;
2837 case OTG_STATE_A_SUSPEND:
2838 if ((test_bit(ID, &motg->inputs) &&
2839 !test_bit(ID_A, &motg->inputs)) ||
2840 test_bit(A_BUS_DROP, &motg->inputs) ||
2841 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2842 pr_debug("id_a/b/c || a_bus_drop ||"
2843 "a_aidl_bdis_tmout\n");
2844 msm_otg_del_timer(motg);
2845 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002846 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302847 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002848 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302849 if (!test_bit(ID_A, &motg->inputs))
2850 msm_hsusb_vbus_power(motg, 0);
2851 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2852 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2853 pr_debug("!a_vbus_vld\n");
2854 msm_otg_del_timer(motg);
2855 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002856 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302857 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002858 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302859 } else if (!test_bit(B_CONN, &motg->inputs) &&
2860 otg->host->b_hnp_enable) {
2861 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002862 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302863 msm_otg_host_hnp_enable(otg, 1);
2864 otg->gadget->is_a_peripheral = 1;
2865 msm_otg_start_peripheral(otg, 1);
2866 } else if (!test_bit(B_CONN, &motg->inputs) &&
2867 !otg->host->b_hnp_enable) {
2868 pr_debug("!b_conn && !b_hnp_enable");
2869 /*
2870 * bus request is dropped during suspend.
2871 * acquire again for next device.
2872 */
2873 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002874 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302875 if (TA_WAIT_BCON > 0)
2876 msm_otg_start_timer(motg, TA_WAIT_BCON,
2877 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002878 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302879 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002880 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302881 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002882 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002883 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302884 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302885 }
2886 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302887 case OTG_STATE_A_PERIPHERAL:
2888 if ((test_bit(ID, &motg->inputs) &&
2889 !test_bit(ID_A, &motg->inputs)) ||
2890 test_bit(A_BUS_DROP, &motg->inputs)) {
2891 pr_debug("id _f/b/c || a_bus_drop\n");
2892 /* Clear BIDL_ADIS timer */
2893 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002894 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302895 msm_otg_start_peripheral(otg, 0);
2896 otg->gadget->is_a_peripheral = 0;
2897 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002898 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302899 if (!test_bit(ID_A, &motg->inputs))
2900 msm_hsusb_vbus_power(motg, 0);
2901 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2902 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2903 pr_debug("!a_vbus_vld\n");
2904 /* Clear BIDL_ADIS timer */
2905 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002906 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302907 msm_otg_start_peripheral(otg, 0);
2908 otg->gadget->is_a_peripheral = 0;
2909 msm_otg_start_host(otg, 0);
2910 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2911 pr_debug("a_bidl_adis_tmout\n");
2912 msm_otg_start_peripheral(otg, 0);
2913 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002914 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302915 set_bit(A_BUS_REQ, &motg->inputs);
2916 msm_otg_host_hnp_enable(otg, 0);
2917 if (TA_WAIT_BCON > 0)
2918 msm_otg_start_timer(motg, TA_WAIT_BCON,
2919 A_WAIT_BCON);
2920 } else if (test_bit(ID_A, &motg->inputs)) {
2921 msm_hsusb_vbus_power(motg, 0);
2922 msm_otg_notify_charger(motg,
2923 IDEV_CHG_MIN - motg->mA_port);
2924 } else if (!test_bit(ID, &motg->inputs)) {
2925 msm_otg_notify_charger(motg, 0);
2926 msm_hsusb_vbus_power(motg, 1);
2927 }
2928 break;
2929 case OTG_STATE_A_WAIT_VFALL:
2930 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2931 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002932 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302933 work = 1;
2934 }
2935 break;
2936 case OTG_STATE_A_VBUS_ERR:
2937 if ((test_bit(ID, &motg->inputs) &&
2938 !test_bit(ID_A, &motg->inputs)) ||
2939 test_bit(A_BUS_DROP, &motg->inputs) ||
2940 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002941 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302942 if (!test_bit(ID_A, &motg->inputs))
2943 msm_hsusb_vbus_power(motg, 0);
2944 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2945 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2946 motg->chg_type = USB_INVALID_CHARGER;
2947 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302948 }
2949 break;
2950 default:
2951 break;
2952 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302953 if (work)
2954 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302955}
2956
Amit Blayd0fe07b2012-09-05 16:42:09 +03002957static void msm_otg_suspend_work(struct work_struct *w)
2958{
2959 struct msm_otg *motg =
2960 container_of(w, struct msm_otg, suspend_work.work);
Pavankumar Kondetie618bf82012-11-28 21:12:44 +05302961
2962 /* This work is only for device bus suspend */
2963 if (test_bit(A_BUS_SUSPEND, &motg->inputs))
2964 msm_otg_sm_work(&motg->sm_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03002965}
2966
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302967static irqreturn_t msm_otg_irq(int irq, void *data)
2968{
2969 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002970 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302971 u32 otgsc = 0, usbsts, pc;
2972 bool work = 0;
2973 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302974
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302975 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002976 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302977 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002978 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07002979 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002980 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302981 return IRQ_HANDLED;
2982 }
2983
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002984 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302985 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302986
2987 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302988 return IRQ_NONE;
2989
2990 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302991 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302992 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302993 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302994 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302995 pr_debug("Id clear\n");
2996 /*
2997 * Assert a_bus_req to supply power on
2998 * VBUS when Micro/Mini-A cable is connected
2999 * with out user intervention.
3000 */
3001 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303002 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303003 msm_chg_enable_aca_det(motg);
3004 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303005 writel_relaxed(otgsc, USB_OTGSC);
3006 work = 1;
3007 } else if (otgsc & OTGSC_DPIS) {
3008 pr_debug("DPIS detected\n");
3009 writel_relaxed(otgsc, USB_OTGSC);
3010 set_bit(A_SRP_DET, &motg->inputs);
3011 set_bit(A_BUS_REQ, &motg->inputs);
3012 work = 1;
3013 } else if (otgsc & OTGSC_BSVIS) {
3014 writel_relaxed(otgsc, USB_OTGSC);
3015 /*
3016 * BSV interrupt comes when operating as an A-device
3017 * (VBUS on/off).
3018 * But, handle BSV when charger is removed from ACA in ID_A
3019 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003020 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303021 !test_bit(ID_A, &motg->inputs))
3022 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303023 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303024 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303025 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303026 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303027 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303028 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003029 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3030
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303031 msm_chg_check_aca_intr(motg);
3032 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303033 work = 1;
3034 } else if (usbsts & STS_PCI) {
3035 pc = readl_relaxed(USB_PORTSC);
3036 pr_debug("portsc = %x\n", pc);
3037 ret = IRQ_NONE;
3038 /*
3039 * HCD Acks PCI interrupt. We use this to switch
3040 * between different OTG states.
3041 */
3042 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003043 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303044 case OTG_STATE_A_SUSPEND:
3045 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3046 !(pc & PORTSC_CCS)) {
3047 pr_debug("B_CONN clear\n");
3048 clear_bit(B_CONN, &motg->inputs);
3049 msm_otg_del_timer(motg);
3050 }
3051 break;
3052 case OTG_STATE_A_PERIPHERAL:
3053 /*
3054 * A-peripheral observed activity on bus.
3055 * clear A_BIDL_ADIS timer.
3056 */
3057 msm_otg_del_timer(motg);
3058 work = 0;
3059 break;
3060 case OTG_STATE_B_WAIT_ACON:
3061 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3062 pr_debug("A_CONN set\n");
3063 set_bit(A_CONN, &motg->inputs);
3064 /* Clear ASE0_BRST timer */
3065 msm_otg_del_timer(motg);
3066 }
3067 break;
3068 case OTG_STATE_B_HOST:
3069 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3070 pr_debug("A_CONN clear\n");
3071 clear_bit(A_CONN, &motg->inputs);
3072 msm_otg_del_timer(motg);
3073 }
3074 break;
3075 case OTG_STATE_A_WAIT_BCON:
3076 if (TA_WAIT_BCON < 0)
3077 set_bit(A_BUS_REQ, &motg->inputs);
3078 default:
3079 work = 0;
3080 break;
3081 }
3082 } else if (usbsts & STS_URI) {
3083 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003084 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303085 case OTG_STATE_A_PERIPHERAL:
3086 /*
3087 * A-peripheral observed activity on bus.
3088 * clear A_BIDL_ADIS timer.
3089 */
3090 msm_otg_del_timer(motg);
3091 work = 0;
3092 break;
3093 default:
3094 work = 0;
3095 break;
3096 }
3097 } else if (usbsts & STS_SLI) {
3098 ret = IRQ_NONE;
3099 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003100 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303101 case OTG_STATE_B_PERIPHERAL:
3102 if (otg->gadget->b_hnp_enable) {
3103 set_bit(A_BUS_SUSPEND, &motg->inputs);
3104 set_bit(B_BUS_REQ, &motg->inputs);
3105 work = 1;
3106 }
3107 break;
3108 case OTG_STATE_A_PERIPHERAL:
3109 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3110 A_BIDL_ADIS);
3111 break;
3112 default:
3113 break;
3114 }
3115 } else if ((usbsts & PHY_ALT_INT)) {
3116 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3117 if (msm_chg_check_aca_intr(motg))
3118 work = 1;
3119 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303120 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303121 if (work)
3122 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303123
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303124 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003125}
3126
3127static void msm_otg_set_vbus_state(int online)
3128{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303129 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003130 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303131
Chiranjeevi Velempati2e1048b2012-11-08 09:54:33 +05303132 /* Ignore received BSV interrupts, if ID pin is GND */
3133 if (!test_bit(ID, &motg->inputs))
Mayank Ranabaf31f42012-07-05 09:43:54 +05303134 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003135
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303136 if (online) {
3137 pr_debug("PMIC: BSV set\n");
3138 set_bit(B_SESS_VLD, &motg->inputs);
3139 } else {
3140 pr_debug("PMIC: BSV clear\n");
3141 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303142 }
3143
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303144 if (!init) {
3145 init = true;
3146 complete(&pmic_vbus_init);
3147 pr_debug("PMIC: BSV init complete\n");
3148 return;
3149 }
3150
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303151 if (test_bit(MHL, &motg->inputs) ||
3152 mhl_det_in_progress) {
3153 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3154 return;
3155 }
3156
Jack Pham5ca279b2012-05-14 18:42:54 -07003157 if (atomic_read(&motg->pm_suspended))
3158 motg->sm_work_pending = true;
3159 else
3160 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003161}
3162
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303163static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003164{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303165 struct msm_otg *motg = container_of(w, struct msm_otg,
3166 pmic_id_status_work.work);
3167 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003168
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303169 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303170 if (!test_and_set_bit(ID, &motg->inputs)) {
3171 pr_debug("PMIC: ID set\n");
3172 work = 1;
3173 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303174 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303175 if (test_and_clear_bit(ID, &motg->inputs)) {
3176 pr_debug("PMIC: ID clear\n");
3177 set_bit(A_BUS_REQ, &motg->inputs);
3178 work = 1;
3179 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303180 }
3181
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303182 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003183 if (atomic_read(&motg->pm_suspended))
3184 motg->sm_work_pending = true;
3185 else
3186 queue_work(system_nrt_wq, &motg->sm_work);
3187 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303188
3189}
3190
3191#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3192static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3193{
3194 struct msm_otg *motg = data;
3195
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303196 if (test_bit(MHL, &motg->inputs) ||
3197 mhl_det_in_progress) {
3198 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3199 return IRQ_HANDLED;
3200 }
3201
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303202 if (!aca_id_turned_on)
3203 /*schedule delayed work for 5msec for ID line state to settle*/
3204 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3205 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003206
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303207 return IRQ_HANDLED;
3208}
3209
3210static int msm_otg_mode_show(struct seq_file *s, void *unused)
3211{
3212 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003213 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303214
Steve Mucklef132c6c2012-06-06 18:30:57 -07003215 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303216 case OTG_STATE_A_HOST:
3217 seq_printf(s, "host\n");
3218 break;
3219 case OTG_STATE_B_PERIPHERAL:
3220 seq_printf(s, "peripheral\n");
3221 break;
3222 default:
3223 seq_printf(s, "none\n");
3224 break;
3225 }
3226
3227 return 0;
3228}
3229
3230static int msm_otg_mode_open(struct inode *inode, struct file *file)
3231{
3232 return single_open(file, msm_otg_mode_show, inode->i_private);
3233}
3234
3235static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3236 size_t count, loff_t *ppos)
3237{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303238 struct seq_file *s = file->private_data;
3239 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303240 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003241 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303242 int status = count;
3243 enum usb_mode_type req_mode;
3244
3245 memset(buf, 0x00, sizeof(buf));
3246
3247 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3248 status = -EFAULT;
3249 goto out;
3250 }
3251
3252 if (!strncmp(buf, "host", 4)) {
3253 req_mode = USB_HOST;
3254 } else if (!strncmp(buf, "peripheral", 10)) {
3255 req_mode = USB_PERIPHERAL;
3256 } else if (!strncmp(buf, "none", 4)) {
3257 req_mode = USB_NONE;
3258 } else {
3259 status = -EINVAL;
3260 goto out;
3261 }
3262
3263 switch (req_mode) {
3264 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003265 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303266 case OTG_STATE_A_HOST:
3267 case OTG_STATE_B_PERIPHERAL:
3268 set_bit(ID, &motg->inputs);
3269 clear_bit(B_SESS_VLD, &motg->inputs);
3270 break;
3271 default:
3272 goto out;
3273 }
3274 break;
3275 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003276 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303277 case OTG_STATE_B_IDLE:
3278 case OTG_STATE_A_HOST:
3279 set_bit(ID, &motg->inputs);
3280 set_bit(B_SESS_VLD, &motg->inputs);
3281 break;
3282 default:
3283 goto out;
3284 }
3285 break;
3286 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003287 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303288 case OTG_STATE_B_IDLE:
3289 case OTG_STATE_B_PERIPHERAL:
3290 clear_bit(ID, &motg->inputs);
3291 break;
3292 default:
3293 goto out;
3294 }
3295 break;
3296 default:
3297 goto out;
3298 }
3299
Steve Mucklef132c6c2012-06-06 18:30:57 -07003300 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303301 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303302out:
3303 return status;
3304}
3305
3306const struct file_operations msm_otg_mode_fops = {
3307 .open = msm_otg_mode_open,
3308 .read = seq_read,
3309 .write = msm_otg_mode_write,
3310 .llseek = seq_lseek,
3311 .release = single_release,
3312};
3313
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303314static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3315{
3316 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003317 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303318
Steve Mucklef132c6c2012-06-06 18:30:57 -07003319 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303320 return 0;
3321}
3322
3323static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3324{
3325 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3326}
3327
3328const struct file_operations msm_otg_state_fops = {
3329 .open = msm_otg_otg_state_open,
3330 .read = seq_read,
3331 .llseek = seq_lseek,
3332 .release = single_release,
3333};
3334
Anji jonnalad270e2d2011-08-09 11:28:32 +05303335static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3336{
3337 struct msm_otg *motg = s->private;
3338
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303339 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303340 return 0;
3341}
3342
3343static int msm_otg_chg_open(struct inode *inode, struct file *file)
3344{
3345 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3346}
3347
3348const struct file_operations msm_otg_chg_fops = {
3349 .open = msm_otg_chg_open,
3350 .read = seq_read,
3351 .llseek = seq_lseek,
3352 .release = single_release,
3353};
3354
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303355static int msm_otg_aca_show(struct seq_file *s, void *unused)
3356{
3357 if (debug_aca_enabled)
3358 seq_printf(s, "enabled\n");
3359 else
3360 seq_printf(s, "disabled\n");
3361
3362 return 0;
3363}
3364
3365static int msm_otg_aca_open(struct inode *inode, struct file *file)
3366{
3367 return single_open(file, msm_otg_aca_show, inode->i_private);
3368}
3369
3370static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3371 size_t count, loff_t *ppos)
3372{
3373 char buf[8];
3374
3375 memset(buf, 0x00, sizeof(buf));
3376
3377 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3378 return -EFAULT;
3379
3380 if (!strncmp(buf, "enable", 6))
3381 debug_aca_enabled = true;
3382 else
3383 debug_aca_enabled = false;
3384
3385 return count;
3386}
3387
3388const struct file_operations msm_otg_aca_fops = {
3389 .open = msm_otg_aca_open,
3390 .read = seq_read,
3391 .write = msm_otg_aca_write,
3392 .llseek = seq_lseek,
3393 .release = single_release,
3394};
3395
Manu Gautam8bdcc592012-03-06 11:26:06 +05303396static int msm_otg_bus_show(struct seq_file *s, void *unused)
3397{
3398 if (debug_bus_voting_enabled)
3399 seq_printf(s, "enabled\n");
3400 else
3401 seq_printf(s, "disabled\n");
3402
3403 return 0;
3404}
3405
3406static int msm_otg_bus_open(struct inode *inode, struct file *file)
3407{
3408 return single_open(file, msm_otg_bus_show, inode->i_private);
3409}
3410
3411static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3412 size_t count, loff_t *ppos)
3413{
3414 char buf[8];
3415 int ret;
3416 struct seq_file *s = file->private_data;
3417 struct msm_otg *motg = s->private;
3418
3419 memset(buf, 0x00, sizeof(buf));
3420
3421 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3422 return -EFAULT;
3423
3424 if (!strncmp(buf, "enable", 6)) {
3425 /* Do not vote here. Let OTG statemachine decide when to vote */
3426 debug_bus_voting_enabled = true;
3427 } else {
3428 debug_bus_voting_enabled = false;
3429 if (motg->bus_perf_client) {
3430 ret = msm_bus_scale_client_update_request(
3431 motg->bus_perf_client, 0);
3432 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003433 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303434 "for bus bw %d\n", __func__, ret);
3435 }
3436 }
3437
3438 return count;
3439}
3440
David Keitel272ce522012-08-17 16:25:24 -07003441static int otg_power_get_property_usb(struct power_supply *psy,
3442 enum power_supply_property psp,
3443 union power_supply_propval *val)
3444{
Jack Pham0c695282012-10-19 18:13:03 -07003445 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003446 switch (psp) {
3447 case POWER_SUPPLY_PROP_SCOPE:
3448 if (motg->host_mode)
3449 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3450 else
3451 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3452 break;
3453 case POWER_SUPPLY_PROP_CURRENT_MAX:
3454 val->intval = motg->current_max;
3455 break;
3456 /* Reflect USB enumeration */
3457 case POWER_SUPPLY_PROP_PRESENT:
3458 case POWER_SUPPLY_PROP_ONLINE:
3459 val->intval = motg->online;
3460 break;
3461 default:
3462 return -EINVAL;
3463 }
3464 return 0;
3465}
3466
3467static int otg_power_set_property_usb(struct power_supply *psy,
3468 enum power_supply_property psp,
3469 const union power_supply_propval *val)
3470{
Jack Pham0c695282012-10-19 18:13:03 -07003471 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003472
3473 switch (psp) {
3474 /* Process PMIC notification in PRESENT prop */
3475 case POWER_SUPPLY_PROP_PRESENT:
3476 msm_otg_set_vbus_state(val->intval);
3477 break;
3478 /* The ONLINE property reflects if usb has enumerated */
3479 case POWER_SUPPLY_PROP_ONLINE:
3480 motg->online = val->intval;
3481 break;
3482 case POWER_SUPPLY_PROP_CURRENT_MAX:
3483 motg->current_max = val->intval;
3484 break;
3485 default:
3486 return -EINVAL;
3487 }
3488
3489 power_supply_changed(&motg->usb_psy);
3490 return 0;
3491}
3492
3493static char *otg_pm_power_supplied_to[] = {
3494 "battery",
3495};
3496
3497static enum power_supply_property otg_pm_power_props_usb[] = {
3498 POWER_SUPPLY_PROP_PRESENT,
3499 POWER_SUPPLY_PROP_ONLINE,
3500 POWER_SUPPLY_PROP_CURRENT_MAX,
3501 POWER_SUPPLY_PROP_SCOPE,
3502};
3503
Manu Gautam8bdcc592012-03-06 11:26:06 +05303504const struct file_operations msm_otg_bus_fops = {
3505 .open = msm_otg_bus_open,
3506 .read = seq_read,
3507 .write = msm_otg_bus_write,
3508 .llseek = seq_lseek,
3509 .release = single_release,
3510};
3511
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303512static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303513
3514static int msm_otg_debugfs_init(struct msm_otg *motg)
3515{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303516 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303517
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303518 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3519
3520 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3521 return -ENODEV;
3522
Anji jonnalad270e2d2011-08-09 11:28:32 +05303523 if (motg->pdata->mode == USB_OTG &&
3524 motg->pdata->otg_control == OTG_USER_CONTROL) {
3525
Manu Gautam8bdcc592012-03-06 11:26:06 +05303526 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303527 S_IWUSR, msm_otg_dbg_root, motg,
3528 &msm_otg_mode_fops);
3529
Manu Gautam8bdcc592012-03-06 11:26:06 +05303530 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303531 debugfs_remove(msm_otg_dbg_root);
3532 msm_otg_dbg_root = NULL;
3533 return -ENODEV;
3534 }
3535 }
3536
Manu Gautam8bdcc592012-03-06 11:26:06 +05303537 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303538 msm_otg_dbg_root, motg,
3539 &msm_otg_chg_fops);
3540
Manu Gautam8bdcc592012-03-06 11:26:06 +05303541 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303542 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303543 return -ENODEV;
3544 }
3545
Manu Gautam8bdcc592012-03-06 11:26:06 +05303546 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303547 msm_otg_dbg_root, motg,
3548 &msm_otg_aca_fops);
3549
Manu Gautam8bdcc592012-03-06 11:26:06 +05303550 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303551 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303552 return -ENODEV;
3553 }
3554
Manu Gautam8bdcc592012-03-06 11:26:06 +05303555 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3556 msm_otg_dbg_root, motg,
3557 &msm_otg_bus_fops);
3558
3559 if (!msm_otg_dentry) {
3560 debugfs_remove_recursive(msm_otg_dbg_root);
3561 return -ENODEV;
3562 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303563
3564 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3565 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3566
3567 if (!msm_otg_dentry) {
3568 debugfs_remove_recursive(msm_otg_dbg_root);
3569 return -ENODEV;
3570 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303571 return 0;
3572}
3573
3574static void msm_otg_debugfs_cleanup(void)
3575{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303576 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303577}
3578
Manu Gautam0ddbd922012-09-21 17:17:38 +05303579#define MSM_OTG_CMD_ID 0x09
3580#define MSM_OTG_DEVICE_ID 0x04
3581#define MSM_OTG_VMID_IDX 0xFF
3582#define MSM_OTG_MEM_TYPE 0x02
3583struct msm_otg_scm_cmd_buf {
3584 unsigned int device_id;
3585 unsigned int vmid_idx;
3586 unsigned int mem_type;
3587} __attribute__ ((__packed__));
3588
3589static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3590{
3591 int ret;
3592 struct msm_otg_platform_data *pdata = motg->pdata;
3593 struct msm_otg_scm_cmd_buf cmd_buf;
3594
3595 if (!pdata->pnoc_errata_fix)
3596 return;
3597
3598 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3599
3600 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3601 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3602 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3603
3604 ret = scm_call(SCM_SVC_CP, MSM_OTG_CMD_ID, &cmd_buf,
3605 sizeof(cmd_buf), NULL, 0);
3606
3607 if (ret)
3608 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3609}
3610
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303611static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3612static struct platform_device *msm_otg_add_pdev(
3613 struct platform_device *ofdev, const char *name)
3614{
3615 struct platform_device *pdev;
3616 const struct resource *res = ofdev->resource;
3617 unsigned int num = ofdev->num_resources;
3618 int retval;
3619
3620 pdev = platform_device_alloc(name, -1);
3621 if (!pdev) {
3622 retval = -ENOMEM;
3623 goto error;
3624 }
3625
3626 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3627 pdev->dev.dma_mask = &msm_otg_dma_mask;
3628
3629 if (num) {
3630 retval = platform_device_add_resources(pdev, res, num);
3631 if (retval)
3632 goto error;
3633 }
3634
3635 retval = platform_device_add(pdev);
3636 if (retval)
3637 goto error;
3638
3639 return pdev;
3640
3641error:
3642 platform_device_put(pdev);
3643 return ERR_PTR(retval);
3644}
3645
3646static int msm_otg_setup_devices(struct platform_device *ofdev,
3647 enum usb_mode_type mode, bool init)
3648{
3649 const char *gadget_name = "msm_hsusb";
3650 const char *host_name = "msm_hsusb_host";
3651 static struct platform_device *gadget_pdev;
3652 static struct platform_device *host_pdev;
3653 int retval = 0;
3654
3655 if (!init) {
3656 if (gadget_pdev)
3657 platform_device_unregister(gadget_pdev);
3658 if (host_pdev)
3659 platform_device_unregister(host_pdev);
3660 return 0;
3661 }
3662
3663 switch (mode) {
3664 case USB_OTG:
3665 /* fall through */
3666 case USB_PERIPHERAL:
3667 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3668 if (IS_ERR(gadget_pdev)) {
3669 retval = PTR_ERR(gadget_pdev);
3670 break;
3671 }
3672 if (mode == USB_PERIPHERAL)
3673 break;
3674 /* fall through */
3675 case USB_HOST:
3676 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3677 if (IS_ERR(host_pdev)) {
3678 retval = PTR_ERR(host_pdev);
3679 if (mode == USB_OTG)
3680 platform_device_unregister(gadget_pdev);
3681 }
3682 break;
3683 default:
3684 break;
3685 }
3686
3687 return retval;
3688}
3689
David Keitel272ce522012-08-17 16:25:24 -07003690static int msm_otg_register_power_supply(struct platform_device *pdev,
3691 struct msm_otg *motg)
3692{
3693 int ret;
3694
3695 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3696 if (ret < 0) {
3697 dev_err(motg->phy.dev,
3698 "%s:power_supply_register usb failed\n",
3699 __func__);
3700 return ret;
3701 }
3702
3703 legacy_power_supply = false;
3704 return 0;
3705}
3706
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303707struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3708{
3709 struct device_node *node = pdev->dev.of_node;
3710 struct msm_otg_platform_data *pdata;
3711 int len = 0;
3712
3713 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3714 if (!pdata) {
3715 pr_err("unable to allocate platform data\n");
3716 return NULL;
3717 }
3718 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3719 if (len) {
3720 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3721 if (!pdata->phy_init_seq)
3722 return NULL;
3723 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3724 pdata->phy_init_seq,
3725 len/sizeof(*pdata->phy_init_seq));
3726 }
3727 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3728 &pdata->power_budget);
3729 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3730 &pdata->mode);
3731 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3732 &pdata->otg_control);
3733 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3734 &pdata->default_mode);
3735 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3736 &pdata->phy_type);
3737 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3738 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303739 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3740 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05303741 pdata->pnoc_errata_fix = of_property_read_bool(node,
3742 "qcom,hsusb-otg-pnoc-errata-fix");
Manu Gautambd53fba2012-07-31 16:13:06 +05303743
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303744 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303745}
3746
3747static int __init msm_otg_probe(struct platform_device *pdev)
3748{
Manu Gautamf8c45642012-08-10 10:20:56 -07003749 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303750 struct resource *res;
3751 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003752 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303753 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303754
3755 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303756
3757 if (pdev->dev.of_node) {
3758 dev_dbg(&pdev->dev, "device tree enabled\n");
3759 pdata = msm_otg_dt_to_pdata(pdev);
3760 if (!pdata)
3761 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003762
3763 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3764 if (!pdata->bus_scale_table)
3765 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3766
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303767 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3768 if (ret) {
3769 dev_err(&pdev->dev, "devices setup failed\n");
3770 return ret;
3771 }
3772 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303773 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3774 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303775 } else {
3776 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303777 }
3778
3779 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3780 if (!motg) {
3781 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3782 return -ENOMEM;
3783 }
3784
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003785 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3786 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003787 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3788 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303789 goto free_motg;
3790 }
3791
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003792 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303793 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003794 phy = &motg->phy;
3795 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303796
3797 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303798 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3799 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3800 * PMIC can detect ACA ID_GND and generate an interrupt.
3801 */
3802 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3803 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3804 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003805 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303806 }
3807
Ofir Cohen4da266f2012-01-03 10:19:29 +02003808 /* initialize reset counter */
3809 motg->reset_counter = 0;
3810
Amit Blay02eff132011-09-21 16:46:24 +03003811 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003812 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003813 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003814 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303815
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303816 /*
3817 * Targets on which link uses asynchronous reset methodology,
3818 * free running clock is not required during the reset.
3819 */
Manu Gautam5143b252012-01-05 19:25:23 -08003820 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303821 if (IS_ERR(motg->clk))
3822 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3823 else
3824 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303825
3826 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003827 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303828 * CORE CLK must be running at >55Mhz for correct HSUSB
3829 * operation and USB core cannot tolerate frequency changes on
3830 * CORE CLK. For such USB cores, vote for maximum clk frequency
3831 * on pclk source
3832 */
Manu Gautam5143b252012-01-05 19:25:23 -08003833 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3834 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303835 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003836 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303837 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003838 goto put_clk;
3839 }
3840 clk_set_rate(motg->core_clk, INT_MAX);
3841
3842 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3843 if (IS_ERR(motg->pclk)) {
3844 dev_err(&pdev->dev, "failed to get iface_clk\n");
3845 ret = PTR_ERR(motg->pclk);
3846 goto put_core_clk;
3847 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303848
3849 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3850 if (!res) {
3851 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3852 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003853 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303854 }
3855
3856 motg->regs = ioremap(res->start, resource_size(res));
3857 if (!motg->regs) {
3858 dev_err(&pdev->dev, "ioremap failed\n");
3859 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003860 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303861 }
3862 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3863
3864 motg->irq = platform_get_irq(pdev, 0);
3865 if (!motg->irq) {
3866 dev_err(&pdev->dev, "platform_get_irq failed\n");
3867 ret = -ENODEV;
3868 goto free_regs;
3869 }
3870
Manu Gautamf8c45642012-08-10 10:20:56 -07003871 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3872 if (motg->async_irq < 0) {
3873 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3874 motg->async_irq = 0;
3875 }
3876
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003877 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003878 if (IS_ERR(motg->xo_handle)) {
3879 dev_err(&pdev->dev, "%s not able to get the handle "
3880 "to vote for TCXO D0 buffer\n", __func__);
3881 ret = PTR_ERR(motg->xo_handle);
3882 goto free_regs;
3883 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303884
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003885 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003886 if (ret) {
3887 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3888 "D0 buffer%d\n", __func__, ret);
3889 goto free_xo_handle;
3890 }
3891
Manu Gautam28b1bac2012-01-30 16:43:06 +05303892 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303893
Mayank Rana248698c2012-04-19 00:03:16 +05303894 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003895 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303896 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003897 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303898 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003899 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003900 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303901 goto devote_xo_handle;
3902 }
3903 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303904 }
3905
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003906 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303907 if (ret) {
3908 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303909 goto devote_xo_handle;
3910 }
3911
3912 ret = regulator_enable(hsusb_vddcx);
3913 if (ret) {
3914 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3915 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303916 }
3917
3918 ret = msm_hsusb_ldo_init(motg, 1);
3919 if (ret) {
3920 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303921 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303922 }
3923
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303924 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303925 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3926 "mhl_usb_hs_switch");
3927 if (IS_ERR(mhl_usb_hs_switch)) {
3928 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003929 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303930 goto free_ldo_init;
3931 }
3932 }
3933
Amit Blay81801aa2012-09-19 12:08:12 +02003934 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303935 if (ret) {
3936 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003937 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303938 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303939 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303940
Manu Gautam0ddbd922012-09-21 17:17:38 +05303941 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
3942 msm_otg_pnoc_errata_fix(motg);
3943
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303944 writel(0, USB_USBINTR);
3945 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003946 /* Ensure that above STOREs are completed before enabling interrupts */
3947 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303948
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303949 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3950 if (ret)
3951 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003952 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303953 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303954 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303955 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303956 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003957 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303958 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3959 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05303960 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
3961 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303962 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3963 "msm_otg", motg);
3964 if (ret) {
3965 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003966 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303967 }
3968
Manu Gautamf8c45642012-08-10 10:20:56 -07003969 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05303970 ret = request_irq(motg->async_irq, msm_otg_irq,
3971 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07003972 if (ret) {
3973 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3974 goto free_irq;
3975 }
3976 disable_irq(motg->async_irq);
3977 }
3978
Jack Pham87f202f2012-08-06 00:24:22 -07003979 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3980 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3981
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003982 phy->init = msm_otg_reset;
3983 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003984 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303985
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003986 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303987
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003988 phy->otg->phy = &motg->phy;
3989 phy->otg->set_host = msm_otg_set_host;
3990 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003991 phy->otg->start_hnp = msm_otg_start_hnp;
3992 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003993
3994 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303995 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003996 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003997 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303998 }
3999
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304000 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304001 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004002 if (motg->pdata->pmic_id_irq) {
4003 ret = request_irq(motg->pdata->pmic_id_irq,
4004 msm_pmic_id_irq,
4005 IRQF_TRIGGER_RISING |
4006 IRQF_TRIGGER_FALLING,
4007 "msm_otg", motg);
4008 if (ret) {
4009 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004010 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004011 }
4012 } else {
4013 ret = -ENODEV;
4014 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07004015 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004016 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304017 }
4018
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304019 msm_hsusb_mhl_switch_enable(motg, 1);
4020
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304021 platform_set_drvdata(pdev, motg);
4022 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004023 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304024
Anji jonnalad270e2d2011-08-09 11:28:32 +05304025 ret = msm_otg_debugfs_init(motg);
4026 if (ret)
4027 dev_dbg(&pdev->dev, "mode debugfs file is"
4028 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304029
Amit Blay58b31472011-11-18 09:39:39 +02004030 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4031 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304032 (!(motg->pdata->mode == USB_OTG) ||
4033 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004034 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304035 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004036
Amit Blay58b31472011-11-18 09:39:39 +02004037 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004038 motg->caps = ALLOW_PHY_RETENTION |
4039 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02004040 }
4041
Amit Blay6fa647a2012-05-24 14:12:08 +03004042 if (motg->pdata->enable_lpm_on_dev_suspend)
4043 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4044
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004045 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304046 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004047 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304048
Amit Blayd6f38282012-10-29 13:13:46 +02004049 if (motg->pdata->delay_lpm_on_disconnect) {
4050 pm_runtime_set_autosuspend_delay(&pdev->dev,
4051 lpm_disconnect_thresh);
4052 pm_runtime_use_autosuspend(&pdev->dev);
4053 }
4054
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304055 if (motg->pdata->bus_scale_table) {
4056 motg->bus_perf_client =
4057 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4058 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004059 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304060 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05304061 else
4062 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304063 }
4064
David Keitel272ce522012-08-17 16:25:24 -07004065 motg->usb_psy.name = "usb";
4066 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4067 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4068 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4069 motg->usb_psy.properties = otg_pm_power_props_usb;
4070 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4071 motg->usb_psy.get_property = otg_power_get_property_usb;
4072 motg->usb_psy.set_property = otg_power_set_property_usb;
4073
Jack Pham0c695282012-10-19 18:13:03 -07004074 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004075 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004076 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4077 legacy_power_supply = true;
4078 } else {
4079 /* otherwise register our own power supply */
4080 if (!msm_otg_register_power_supply(pdev, motg))
4081 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004082 }
4083
Jack Pham0c695282012-10-19 18:13:03 -07004084 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4085 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4086
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304087 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004088
Steve Mucklef132c6c2012-06-06 18:30:57 -07004089remove_phy:
4090 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004091free_async_irq:
4092 if (motg->async_irq)
4093 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304094free_irq:
4095 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004096destroy_wlock:
4097 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304098 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004099 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004100free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304101 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304102free_hsusb_vddcx:
4103 regulator_disable(hsusb_vddcx);
4104free_config_vddcx:
4105 regulator_set_voltage(hsusb_vddcx,
4106 vdd_val[motg->vdd_type][VDD_NONE],
4107 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004108devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304109 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004110 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004111free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004112 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304113free_regs:
4114 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004115put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304116 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304117put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004118 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304119put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304120 if (!IS_ERR(motg->clk))
4121 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004122 if (!IS_ERR(motg->phy_reset_clk))
4123 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004124free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004125 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304126free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304127 kfree(motg);
4128 return ret;
4129}
4130
4131static int __devexit msm_otg_remove(struct platform_device *pdev)
4132{
4133 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004134 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304135 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304136
4137 if (otg->host || otg->gadget)
4138 return -EBUSY;
4139
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304140 if (pdev->dev.of_node)
4141 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004142 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4143 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304144 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304145 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304146 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304147 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004148 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304149 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304150
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304151 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304152
4153 device_init_wakeup(&pdev->dev, 0);
4154 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004155 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304156
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304157 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004158 if (motg->pdata->pmic_id_irq)
4159 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004160 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304161 free_irq(motg->irq, motg);
4162
Jack Pham87f202f2012-08-06 00:24:22 -07004163 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4164 motg->pdata->mpm_otgsessvld_int)
4165 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4166
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304167 /*
4168 * Put PHY in low power mode.
4169 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07004170 ulpi_read(otg->phy, 0x14);
4171 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304172
4173 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4174 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4175 if (readl(USB_PORTSC) & PORTSC_PHCD)
4176 break;
4177 udelay(1);
4178 cnt++;
4179 }
4180 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004181 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304182
Manu Gautam28b1bac2012-01-30 16:43:06 +05304183 clk_disable_unprepare(motg->pclk);
4184 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004185 msm_xo_put(motg->xo_handle);
Amit Blay81801aa2012-09-19 12:08:12 +02004186 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304187 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304188 regulator_disable(hsusb_vddcx);
4189 regulator_set_voltage(hsusb_vddcx,
4190 vdd_val[motg->vdd_type][VDD_NONE],
4191 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304192
4193 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304194 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304195
Amit Blay02eff132011-09-21 16:46:24 +03004196 if (!IS_ERR(motg->phy_reset_clk))
4197 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304198 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304199 if (!IS_ERR(motg->clk))
4200 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004201 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304202
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304203 if (motg->bus_perf_client)
4204 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304205
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004206 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304207 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304208 return 0;
4209}
4210
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304211#ifdef CONFIG_PM_RUNTIME
4212static int msm_otg_runtime_idle(struct device *dev)
4213{
4214 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004215 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304216
4217 dev_dbg(dev, "OTG runtime idle\n");
4218
Steve Mucklef132c6c2012-06-06 18:30:57 -07004219 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304220 return -EAGAIN;
4221 else
4222 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304223}
4224
4225static int msm_otg_runtime_suspend(struct device *dev)
4226{
4227 struct msm_otg *motg = dev_get_drvdata(dev);
4228
4229 dev_dbg(dev, "OTG runtime suspend\n");
4230 return msm_otg_suspend(motg);
4231}
4232
4233static int msm_otg_runtime_resume(struct device *dev)
4234{
4235 struct msm_otg *motg = dev_get_drvdata(dev);
4236
4237 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304238 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304239 return msm_otg_resume(motg);
4240}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304241#endif
4242
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304243#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304244static int msm_otg_pm_suspend(struct device *dev)
4245{
Jack Pham5ca279b2012-05-14 18:42:54 -07004246 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304247 struct msm_otg *motg = dev_get_drvdata(dev);
4248
4249 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004250
4251 atomic_set(&motg->pm_suspended, 1);
4252 ret = msm_otg_suspend(motg);
4253 if (ret)
4254 atomic_set(&motg->pm_suspended, 0);
4255
4256 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304257}
4258
4259static int msm_otg_pm_resume(struct device *dev)
4260{
Jack Pham5ca279b2012-05-14 18:42:54 -07004261 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304262 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304263
4264 dev_dbg(dev, "OTG PM resume\n");
4265
Jack Pham5ca279b2012-05-14 18:42:54 -07004266 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004267 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004268 pm_runtime_get_noresume(dev);
4269 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304270
Jack Pham5ca279b2012-05-14 18:42:54 -07004271 /* Update runtime PM status */
4272 pm_runtime_disable(dev);
4273 pm_runtime_set_active(dev);
4274 pm_runtime_enable(dev);
4275
Jack Phamc7edb172012-08-13 15:32:39 -07004276 if (motg->sm_work_pending) {
4277 motg->sm_work_pending = false;
4278 queue_work(system_nrt_wq, &motg->sm_work);
4279 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004280 }
4281
4282 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304283}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304284#endif
4285
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304286#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304287static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304288 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4289 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4290 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304291};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304292#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304293
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304294static struct of_device_id msm_otg_dt_match[] = {
4295 { .compatible = "qcom,hsusb-otg",
4296 },
4297 {}
4298};
4299
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304300static struct platform_driver msm_otg_driver = {
4301 .remove = __devexit_p(msm_otg_remove),
4302 .driver = {
4303 .name = DRIVER_NAME,
4304 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304305#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304306 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304307#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304308 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304309 },
4310};
4311
4312static int __init msm_otg_init(void)
4313{
4314 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4315}
4316
4317static void __exit msm_otg_exit(void)
4318{
4319 platform_driver_unregister(&msm_otg_driver);
4320}
4321
4322module_init(msm_otg_init);
4323module_exit(msm_otg_exit);
4324
4325MODULE_LICENSE("GPL v2");
4326MODULE_DESCRIPTION("MSM USB transceiver driver");