blob: d6704c8c7c3fd8442da1a6eed0871618c36d71ca [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
Amit Blayd0fe07b2012-09-05 16:42:09 +0300774 if (atomic_read(&motg->in_lpm) == suspend &&
775 !atomic_read(&motg->suspend_work_pending))
Jack Pham69e621d2012-06-25 18:48:07 -0700776 return 0;
777
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530778 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700779 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530780 case OTG_STATE_A_WAIT_BCON:
781 if (TA_WAIT_BCON > 0)
782 break;
783 /* fall through */
784 case OTG_STATE_A_HOST:
785 pr_debug("host bus suspend\n");
786 clear_bit(A_BUS_REQ, &motg->inputs);
787 queue_work(system_nrt_wq, &motg->sm_work);
788 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300789 case OTG_STATE_B_PERIPHERAL:
790 pr_debug("peripheral bus suspend\n");
791 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
792 break;
793 set_bit(A_BUS_SUSPEND, &motg->inputs);
Amit Blayd0fe07b2012-09-05 16:42:09 +0300794 atomic_set(&motg->suspend_work_pending, 1);
795 queue_delayed_work(system_nrt_wq, &motg->suspend_work,
796 USB_SUSPEND_DELAY_TIME);
Amit Blay6fa647a2012-05-24 14:12:08 +0300797 break;
798
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530799 default:
800 break;
801 }
802 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700803 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530804 case OTG_STATE_A_SUSPEND:
805 /* Remote wakeup or resume */
806 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700807 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700808
809 /* ensure hardware is not in low power mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700810 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530811 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300812 case OTG_STATE_B_PERIPHERAL:
813 pr_debug("peripheral bus resume\n");
814 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
815 break;
816 clear_bit(A_BUS_SUSPEND, &motg->inputs);
817 queue_work(system_nrt_wq, &motg->sm_work);
818 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530819 default:
820 break;
821 }
822 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530823 return 0;
824}
825
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530826#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530827#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
828
829#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530830static int msm_otg_suspend(struct msm_otg *motg)
831{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200832 struct usb_phy *phy = &motg->phy;
833 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530834 struct msm_otg_platform_data *pdata = motg->pdata;
835 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300836 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530837 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800838 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530839 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530840
841 if (atomic_read(&motg->in_lpm))
842 return 0;
843
844 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530845 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
846 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700847 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300848 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
849 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530850 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700851
Pavankumar Kondeticfe05392012-10-22 13:21:19 +0530852 /*
853 * Abort suspend when,
854 * 1. charging detection in progress due to cable plug-in
855 * 2. host mode activation in progress due to Micro-A cable insertion
856 */
857
858 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
859 !dcp) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham502bea32012-08-13 15:34:20 -0700860 enable_irq(motg->irq);
861 return -EBUSY;
862 }
863
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530864 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530865 * Chipidea 45-nm PHY suspend sequence:
866 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530867 * Interrupt Latch Register auto-clear feature is not present
868 * in all PHY versions. Latch register is clear on read type.
869 * Clear latch register to avoid spurious wakeup from
870 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530871 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530872 * PHY comparators are disabled when PHY enters into low power
873 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
874 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
875 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530876 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530877 * PLL is not turned off when PHY enters into low power mode (LPM).
878 * Disable PLL for maximum power savings.
879 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530880
881 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200882 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530883 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200884 ulpi_write(phy, 0x01, 0x30);
885 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530886 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530887
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530889 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530890 * PHY may take some time or even fail to enter into low power
891 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
892 * in failure case.
893 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530894 portsc = readl_relaxed(USB_PORTSC);
895 if (!(portsc & PORTSC_PHCD)) {
896 writel_relaxed(portsc | PORTSC_PHCD,
897 USB_PORTSC);
898 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
899 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
900 break;
901 udelay(1);
902 cnt++;
903 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530904 }
905
906 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200907 dev_err(phy->dev, "Unable to suspend PHY\n");
908 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530909 enable_irq(motg->irq);
910 return -ETIMEDOUT;
911 }
912
913 /*
914 * PHY has capability to generate interrupt asynchronously in low
915 * power mode (LPM). This interrupt is level triggered. So USB IRQ
916 * line must be disabled till async interrupt enable bit is cleared
917 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
918 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530919 *
920 * PHY retention mode is disallowed while entering to LPM with wall
921 * charger connected. But PHY is put into suspend mode. Hence
922 * enable asynchronous interrupt to detect charger disconnection when
923 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530924 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530925 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300926 if (host_bus_suspend || device_bus_suspend ||
927 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530928 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
929 else
930 cmd_val |= ULPI_STP_CTRL;
931 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530932
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530933 /*
934 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
935 * PHY retention and collapse can not happen with VDP_SRC enabled.
936 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300937 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
938 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200939 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530940 if (motg->pdata->otg_control == OTG_PHY_CONTROL) {
Amit Blay58b31472011-11-18 09:39:39 +0200941 /* Enable PHY HV interrupts to wake MPM/Link */
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +0530942 if ((motg->pdata->mode == USB_OTG) ||
943 (motg->pdata->mode == USB_HOST))
944 phy_ctrl_val |= (PHY_IDHV_INTEN |
945 PHY_OTGSESSVLDHV_INTEN);
946 else
947 phy_ctrl_val |= PHY_OTGSESSVLDHV_INTEN;
948 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530949
Amit Blay58b31472011-11-18 09:39:39 +0200950 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530952 }
953
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 /* Ensure that above operation is completed before turning off clocks */
955 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300956 /* Consider clocks on workaround flag only in case of bus suspend */
957 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
958 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
959 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300960 clk_disable_unprepare(motg->pclk);
961 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300962 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300963 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964
Anji jonnala7da3f262011-12-02 17:22:14 -0800965 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530966 if (!host_bus_suspend) {
967 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
968 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700969 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530970 "TCXO D0 buffer%d\n", __func__, ret);
971 else
972 motg->lpm_flags |= XO_SHUTDOWN;
973 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800974
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530975 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
976 !host_bus_suspend && !dcp) {
Amit Blay81801aa2012-09-19 12:08:12 +0200977 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +0200979 } else if (motg->caps & ALLOW_PHY_REGULATORS_LPM &&
980 !host_bus_suspend && !device_bus_suspend && !dcp) {
981 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_ON);
982 motg->lpm_flags |= PHY_REGULATORS_LPM;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530983 }
984
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530985 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530987 msm_hsusb_mhl_switch_enable(motg, 0);
988 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989
Steve Mucklef132c6c2012-06-06 18:30:57 -0700990 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530991 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700992 if (motg->async_irq)
993 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700994 if (motg->pdata->pmic_id_irq)
995 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700996 if (pdata->otg_control == OTG_PHY_CONTROL &&
997 pdata->mpm_otgsessvld_int)
998 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700999 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301000 if (bus)
1001 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1002
1003 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -07001004 /* Enable ASYNC IRQ (if present) during LPM */
1005 if (motg->async_irq)
1006 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301007 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301009
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001010 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301011
1012 return 0;
1013}
1014
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301015static int msm_otg_resume(struct msm_otg *motg)
1016{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001017 struct usb_phy *phy = &motg->phy;
1018 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -07001019 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301020 int cnt = 0;
1021 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +02001022 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -08001023 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301024
1025 if (!atomic_read(&motg->in_lpm))
1026 return 0;
1027
Jack Pham8978b892012-10-17 16:31:39 -07001028 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301030
Anji jonnala7da3f262011-12-02 17:22:14 -08001031 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301032 if (motg->lpm_flags & XO_SHUTDOWN) {
1033 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
1034 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -07001035 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +05301036 "TCXO D0 buffer%d\n", __func__, ret);
1037 motg->lpm_flags &= ~XO_SHUTDOWN;
1038 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301039
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001040 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +03001041 clk_prepare_enable(motg->core_clk);
1042 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +03001043 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +03001044 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301045
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
Amit Blay81801aa2012-09-19 12:08:12 +02001047 msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001048 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
Amit Blay81801aa2012-09-19 12:08:12 +02001049 } else if (motg->lpm_flags & PHY_REGULATORS_LPM) {
1050 msm_hsusb_ldo_enable(motg, USB_PHY_REG_LPM_OFF);
1051 motg->lpm_flags &= ~PHY_REGULATORS_LPM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001052 }
1053
1054 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05301055 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301056 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +02001057 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
1058 phy_ctrl_val |= PHY_RETEN;
1059 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
1060 /* Disable PHY HV interrupts */
1061 phy_ctrl_val &=
1062 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
1063 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +05301065 }
1066
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301067 temp = readl(USB_USBCMD);
1068 temp &= ~ASYNC_INTR_CTRL;
1069 temp &= ~ULPI_STP_CTRL;
1070 writel(temp, USB_USBCMD);
1071
1072 /*
1073 * PHY comes out of low power mode (LPM) in case of wakeup
1074 * from asynchronous interrupt.
1075 */
1076 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1077 goto skip_phy_resume;
1078
1079 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
1080 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
1081 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
1082 break;
1083 udelay(1);
1084 cnt++;
1085 }
1086
1087 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1088 /*
1089 * This is a fatal error. Reset the link and
1090 * PHY. USB state can not be restored. Re-insertion
1091 * of USB cable is the only way to get USB working.
1092 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001093 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301094 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001095 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301096 }
1097
1098skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001099 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301100 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001101 if (motg->async_irq)
1102 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001103 if (motg->pdata->pmic_id_irq)
1104 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001105 if (pdata->otg_control == OTG_PHY_CONTROL &&
1106 pdata->mpm_otgsessvld_int)
1107 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301109 if (bus)
1110 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1111
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301112 atomic_set(&motg->in_lpm, 0);
1113
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301114 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001115 /* Match the disable_irq call from ISR */
1116 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301117 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301118 }
Jack Pham8978b892012-10-17 16:31:39 -07001119 enable_irq(motg->irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301120
Manu Gautamf8c45642012-08-10 10:20:56 -07001121 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1122 if (motg->async_irq)
1123 disable_irq(motg->async_irq);
1124
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001125 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301126
1127 return 0;
1128}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301129#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301130
David Keitel272ce522012-08-17 16:25:24 -07001131static void msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001132{
Jack Pham0c695282012-10-19 18:13:03 -07001133 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001134 pr_err("No USB power supply registered!\n");
1135 return;
1136 }
Sridhar Parasuram3c67a412012-09-26 09:36:22 -07001137
Jack Pham0c695282012-10-19 18:13:03 -07001138 if (legacy_power_supply) {
David Keitel272ce522012-08-17 16:25:24 -07001139 /* legacy support */
1140 if (host_mode)
1141 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1142 else
1143 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1144 return;
1145 } else {
1146 motg->host_mode = host_mode;
Jack Pham0c695282012-10-19 18:13:03 -07001147 power_supply_changed(psy);
David Keitel272ce522012-08-17 16:25:24 -07001148 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001149}
1150
David Keitel081a3e22012-04-18 12:37:07 -07001151static int msm_otg_notify_chg_type(struct msm_otg *motg)
1152{
1153 static int charger_type;
David Keitelba8f8322012-06-01 17:14:10 -07001154
David Keitel081a3e22012-04-18 12:37:07 -07001155 /*
1156 * TODO
1157 * Unify OTG driver charger types and power supply charger types
1158 */
1159 if (charger_type == motg->chg_type)
1160 return 0;
1161
1162 if (motg->chg_type == USB_SDP_CHARGER)
1163 charger_type = POWER_SUPPLY_TYPE_USB;
1164 else if (motg->chg_type == USB_CDP_CHARGER)
1165 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301166 else if (motg->chg_type == USB_DCP_CHARGER ||
1167 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001168 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1169 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1170 motg->chg_type == USB_ACA_A_CHARGER ||
1171 motg->chg_type == USB_ACA_B_CHARGER ||
1172 motg->chg_type == USB_ACA_C_CHARGER))
1173 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1174 else
1175 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1176
Jack Pham0c695282012-10-19 18:13:03 -07001177 if (!psy) {
David Keitelba8f8322012-06-01 17:14:10 -07001178 pr_err("No USB power supply registered!\n");
1179 return -EINVAL;
1180 }
1181
1182 pr_debug("setting usb power supply type %d\n", charger_type);
Jack Pham0c695282012-10-19 18:13:03 -07001183 power_supply_set_supply_type(psy, charger_type);
David Keitelba8f8322012-06-01 17:14:10 -07001184 return 0;
David Keitel081a3e22012-04-18 12:37:07 -07001185}
1186
Amit Blay0f7edf72012-01-15 10:11:27 +02001187static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1188{
Jack Pham0c695282012-10-19 18:13:03 -07001189 if (!psy) {
David Keitel272ce522012-08-17 16:25:24 -07001190 dev_dbg(motg->phy.dev, "no usb power supply registered\n");
1191 goto psy_error;
David Keitelf5c5d602012-08-17 16:25:24 -07001192 }
1193
David Keitel272ce522012-08-17 16:25:24 -07001194 if (motg->cur_power == 0 && mA > 2) {
1195 /* Enable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001196 if (power_supply_set_online(psy, true))
David Keitel272ce522012-08-17 16:25:24 -07001197 goto psy_error;
Jack Pham0c695282012-10-19 18:13:03 -07001198 if (power_supply_set_current_limit(psy, 1000*mA))
David Keitel272ce522012-08-17 16:25:24 -07001199 goto psy_error;
1200 } else if (motg->cur_power > 0 && (mA == 0 || mA == 2)) {
1201 /* Disable charging */
Jack Pham0c695282012-10-19 18:13:03 -07001202 if (power_supply_set_online(psy, false))
David Keitel272ce522012-08-17 16:25:24 -07001203 goto psy_error;
1204 /* Set max current limit */
Jack Pham0c695282012-10-19 18:13:03 -07001205 if (power_supply_set_current_limit(psy, 0))
David Keitel272ce522012-08-17 16:25:24 -07001206 goto psy_error;
1207 }
Jack Pham0c695282012-10-19 18:13:03 -07001208 power_supply_changed(psy);
Amit Blay0f7edf72012-01-15 10:11:27 +02001209 return 0;
1210
David Keitel272ce522012-08-17 16:25:24 -07001211psy_error:
1212 dev_dbg(motg->phy.dev, "power supply error when setting property\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001213 return -ENXIO;
1214}
1215
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301216static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1217{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001218 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301219
1220 if (g && g->is_a_peripheral)
1221 return;
1222
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301223 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1224 motg->chg_type == USB_ACA_A_CHARGER ||
1225 motg->chg_type == USB_ACA_B_CHARGER ||
1226 motg->chg_type == USB_ACA_C_CHARGER) &&
1227 mA > IDEV_ACA_CHG_LIMIT)
1228 mA = IDEV_ACA_CHG_LIMIT;
1229
David Keitel081a3e22012-04-18 12:37:07 -07001230 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001231 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001232 "Failed notifying %d charger type to PMIC\n",
1233 motg->chg_type);
1234
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301235 if (motg->cur_power == mA)
1236 return;
1237
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001238 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001239
1240 /*
1241 * Use Power Supply API if supported, otherwise fallback
1242 * to legacy pm8921 API.
1243 */
1244 if (msm_otg_notify_power_supply(motg, mA))
1245 pm8921_charger_vbus_draw(mA);
1246
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301247 motg->cur_power = mA;
1248}
1249
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001250static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301251{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001252 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301253
1254 /*
1255 * Gadget driver uses set_power method to notify about the
1256 * available current based on suspend/configured states.
1257 *
1258 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1259 * states when CDP/ACA is connected.
1260 */
1261 if (motg->chg_type == USB_SDP_CHARGER)
1262 msm_otg_notify_charger(motg, mA);
1263
1264 return 0;
1265}
1266
Steve Mucklef132c6c2012-06-06 18:30:57 -07001267static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301268{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001269 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301270 struct msm_otg_platform_data *pdata = motg->pdata;
1271 struct usb_hcd *hcd;
1272
1273 if (!otg->host)
1274 return;
1275
1276 hcd = bus_to_hcd(otg->host);
1277
1278 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001279 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301280
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301281 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001282 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301283 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1284
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301285 /*
1286 * Some boards have a switch cotrolled by gpio
1287 * to enable/disable internal HUB. Enable internal
1288 * HUB before kicking the host.
1289 */
1290 if (pdata->setup_gpio)
1291 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301292 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301293 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001294 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301295
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301296 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301297 /* HCD core reset all bits of PORTSC. select ULPI phy */
1298 writel_relaxed(0x80000000, USB_PORTSC);
1299
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301300 if (pdata->setup_gpio)
1301 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301302
1303 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_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301306 }
1307}
1308
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309static int msm_otg_usbdev_notify(struct notifier_block *self,
1310 unsigned long action, void *priv)
1311{
1312 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001313 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301314 struct usb_device *udev = priv;
1315
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301316 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1317 goto out;
1318
Steve Mucklef132c6c2012-06-06 18:30:57 -07001319 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301320 goto out;
1321 /*
1322 * Interested in devices connected directly to the root hub.
1323 * ACA dock can supply IDEV_CHG irrespective devices connected
1324 * on the accessory port.
1325 */
1326 if (!udev->parent || udev->parent->parent ||
1327 motg->chg_type == USB_ACA_DOCK_CHARGER)
1328 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329
1330 switch (action) {
1331 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301332 if (aca_enabled())
1333 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001334 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301335 pr_debug("B_CONN set\n");
1336 set_bit(B_CONN, &motg->inputs);
1337 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001338 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301339 /*
1340 * OTG PET: A-device must end session within
1341 * 10 sec after PET enumeration.
1342 */
1343 if (udev->quirks & USB_QUIRK_OTG_PET)
1344 msm_otg_start_timer(motg, TA_TST_MAINT,
1345 A_TST_MAINT);
1346 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301347 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001348 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001349 if (udev->actconfig)
1350 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1351 else
1352 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001353 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301354 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301355 break;
1356 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001357 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1358 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301359 pr_debug("B_CONN clear\n");
1360 clear_bit(B_CONN, &motg->inputs);
1361 /*
1362 * OTG PET: A-device must end session after
1363 * PET disconnection if it is enumerated
1364 * with bcdDevice[0] = 1. USB core sets
1365 * bus->otg_vbus_off for us. clear it here.
1366 */
1367 if (udev->bus->otg_vbus_off) {
1368 udev->bus->otg_vbus_off = 0;
1369 set_bit(A_BUS_DROP, &motg->inputs);
1370 }
1371 queue_work(system_nrt_wq, &motg->sm_work);
1372 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001373 default:
1374 break;
1375 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301376 if (test_bit(ID_A, &motg->inputs))
1377 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1378 motg->mA_port);
1379out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001380 return NOTIFY_OK;
1381}
1382
Mayank Ranae3926882011-12-26 09:47:54 +05301383static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1384{
1385 int ret;
1386 static bool vbus_is_on;
1387
1388 if (vbus_is_on == on)
1389 return;
1390
1391 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301392 ret = motg->pdata->vbus_power(on);
1393 if (!ret)
1394 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301395 return;
1396 }
1397
1398 if (!vbus_otg) {
1399 pr_err("vbus_otg is NULL.");
1400 return;
1401 }
1402
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001403 /*
1404 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001405 * from usb before turning on the boost.
1406 * if exiting host mode disable the boost before enabling to draw
1407 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001408 */
Mayank Ranae3926882011-12-26 09:47:54 +05301409 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001410 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301411 ret = regulator_enable(vbus_otg);
1412 if (ret) {
1413 pr_err("unable to enable vbus_otg\n");
1414 return;
1415 }
1416 vbus_is_on = true;
1417 } else {
1418 ret = regulator_disable(vbus_otg);
1419 if (ret) {
1420 pr_err("unable to disable vbus_otg\n");
1421 return;
1422 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001423 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301424 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301425 }
1426}
1427
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001428static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301429{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001430 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301431 struct usb_hcd *hcd;
1432
1433 /*
1434 * Fail host registration if this board can support
1435 * only peripheral configuration.
1436 */
1437 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001438 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301439 return -ENODEV;
1440 }
1441
Mayank Ranae3926882011-12-26 09:47:54 +05301442 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001443 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301444 if (IS_ERR(vbus_otg)) {
1445 pr_err("Unable to get vbus_otg\n");
1446 return -ENODEV;
1447 }
1448 }
1449
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301450 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001451 if (otg->phy->state == OTG_STATE_A_HOST) {
1452 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001453 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301454 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301455 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301456 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001457 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301458 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301459 } else {
1460 otg->host = NULL;
1461 }
1462
1463 return 0;
1464 }
1465
1466 hcd = bus_to_hcd(host);
1467 hcd->power_budget = motg->pdata->power_budget;
1468
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301469#ifdef CONFIG_USB_OTG
1470 host->otg_port = 1;
1471#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001472 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1473 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301474 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001475 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301476
1477 /*
1478 * Kick the state machine work, if peripheral is not supported
1479 * or peripheral is already registered with us.
1480 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301481 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001482 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301483 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301484 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301485
1486 return 0;
1487}
1488
Steve Mucklef132c6c2012-06-06 18:30:57 -07001489static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301490{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301491 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001492 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301493 struct msm_otg_platform_data *pdata = motg->pdata;
1494
1495 if (!otg->gadget)
1496 return;
1497
1498 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001499 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301500 /*
1501 * Some boards have a switch cotrolled by gpio
1502 * to enable/disable internal HUB. Disable internal
1503 * HUB before kicking the gadget.
1504 */
1505 if (pdata->setup_gpio)
1506 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001507
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301508 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301509 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301510 ret = msm_bus_scale_client_update_request(
1511 motg->bus_perf_client, 1);
1512 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001513 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301514 "bus bandwidth %d\n", __func__, ret);
1515 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301516 usb_gadget_vbus_connect(otg->gadget);
1517 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001518 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301519 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301520 /* Configure BUS performance parameters to default */
1521 if (motg->bus_perf_client) {
1522 ret = msm_bus_scale_client_update_request(
1523 motg->bus_perf_client, 0);
1524 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001525 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301526 "for bus bw %d\n", __func__, ret);
1527 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301528 if (pdata->setup_gpio)
1529 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1530 }
1531
1532}
1533
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001534static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301535 struct usb_gadget *gadget)
1536{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001537 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301538
1539 /*
1540 * Fail peripheral registration if this board can support
1541 * only host configuration.
1542 */
1543 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001544 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301545 return -ENODEV;
1546 }
1547
1548 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001549 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1550 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301551 msm_otg_start_peripheral(otg, 0);
1552 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001553 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301554 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301555 } else {
1556 otg->gadget = NULL;
1557 }
1558
1559 return 0;
1560 }
1561 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001562 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301563
1564 /*
1565 * Kick the state machine work, if host is not supported
1566 * or host is already registered with us.
1567 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301568 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001569 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301570 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301571 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301572
1573 return 0;
1574}
1575
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301576static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1577{
1578 unsigned long flags;
1579 int id;
1580
1581 if (!motg->pdata->pmic_id_irq)
1582 return -ENODEV;
1583
1584 local_irq_save(flags);
1585 id = irq_read_line(motg->pdata->pmic_id_irq);
1586 local_irq_restore(flags);
1587
1588 /*
1589 * If we can not read ID line state for some reason, treat
1590 * it as float. This would prevent MHL discovery and kicking
1591 * host mode unnecessarily.
1592 */
1593 return !!id;
1594}
1595
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301596static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1597 void (*callback)(int on))
1598{
1599 struct usb_phy *phy = &motg->phy;
1600 int ret;
1601
Manoj Raoa7bddd12012-08-27 20:36:45 -07001602 if (!motg->pdata->mhl_enable) {
1603 dev_dbg(phy->dev, "MHL feature not enabled\n");
1604 return -ENODEV;
1605 }
1606
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301607 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1608 !motg->pdata->pmic_id_irq) {
1609 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1610 return -ENODEV;
1611 }
1612
1613 if (!motg->pdata->mhl_dev_name) {
1614 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1615 return -ENODEV;
1616 }
1617
1618 if (callback)
1619 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1620 callback);
1621 else
1622 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1623
1624 if (ret)
1625 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1626 motg->pdata->mhl_dev_name, ret);
1627 else
1628 motg->mhl_enabled = true;
1629
1630 return ret;
1631}
1632
1633static void msm_otg_mhl_notify_online(int on)
1634{
1635 struct msm_otg *motg = the_msm_otg;
1636 struct usb_phy *phy = &motg->phy;
1637 bool queue = false;
1638
1639 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1640
1641 if (on) {
1642 set_bit(MHL, &motg->inputs);
1643 } else {
1644 clear_bit(MHL, &motg->inputs);
1645 queue = true;
1646 }
1647
1648 if (queue && phy->state != OTG_STATE_UNDEFINED)
1649 schedule_work(&motg->sm_work);
1650}
1651
1652static bool msm_otg_is_mhl(struct msm_otg *motg)
1653{
1654 struct usb_phy *phy = &motg->phy;
1655 int is_mhl, ret;
1656
1657 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1658 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1659 /*
1660 * MHL driver calls our callback saying that MHL connected
1661 * if RID_GND is detected. But at later part of discovery
1662 * it may figure out MHL is not connected and returns
1663 * false. Hence clear MHL input here.
1664 */
1665 clear_bit(MHL, &motg->inputs);
1666 dev_dbg(phy->dev, "MHL device not found\n");
1667 return false;
1668 }
1669
1670 set_bit(MHL, &motg->inputs);
1671 dev_dbg(phy->dev, "MHL device found\n");
1672 return true;
1673}
1674
1675static bool msm_chg_mhl_detect(struct msm_otg *motg)
1676{
1677 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301678
1679 if (!motg->mhl_enabled)
1680 return false;
1681
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05301682 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301683
1684 if (id)
1685 return false;
1686
1687 mhl_det_in_progress = true;
1688 ret = msm_otg_is_mhl(motg);
1689 mhl_det_in_progress = false;
1690
1691 return ret;
1692}
1693
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05301694static void msm_otg_chg_check_timer_func(unsigned long data)
1695{
1696 struct msm_otg *motg = (struct msm_otg *) data;
1697 struct usb_otg *otg = motg->phy.otg;
1698
1699 if (atomic_read(&motg->in_lpm) ||
1700 !test_bit(B_SESS_VLD, &motg->inputs) ||
1701 otg->phy->state != OTG_STATE_B_PERIPHERAL ||
1702 otg->gadget->speed != USB_SPEED_UNKNOWN) {
1703 dev_dbg(otg->phy->dev, "Nothing to do in chg_check_timer\n");
1704 return;
1705 }
1706
1707 if ((readl_relaxed(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1708 dev_dbg(otg->phy->dev, "DCP is detected as SDP\n");
1709 set_bit(B_FALSE_SDP, &motg->inputs);
1710 queue_work(system_nrt_wq, &motg->sm_work);
1711 }
1712}
1713
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001714static bool msm_chg_aca_detect(struct msm_otg *motg)
1715{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001716 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001717 u32 int_sts;
1718 bool ret = false;
1719
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301720 if (!aca_enabled())
1721 goto out;
1722
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001723 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1724 goto out;
1725
Steve Mucklef132c6c2012-06-06 18:30:57 -07001726 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001727 switch (int_sts & 0x1C) {
1728 case 0x08:
1729 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001730 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001731 motg->chg_type = USB_ACA_A_CHARGER;
1732 motg->chg_state = USB_CHG_STATE_DETECTED;
1733 clear_bit(ID_B, &motg->inputs);
1734 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301735 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001736 ret = true;
1737 }
1738 break;
1739 case 0x0C:
1740 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001741 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742 motg->chg_type = USB_ACA_B_CHARGER;
1743 motg->chg_state = USB_CHG_STATE_DETECTED;
1744 clear_bit(ID_A, &motg->inputs);
1745 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301746 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001747 ret = true;
1748 }
1749 break;
1750 case 0x10:
1751 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001752 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001753 motg->chg_type = USB_ACA_C_CHARGER;
1754 motg->chg_state = USB_CHG_STATE_DETECTED;
1755 clear_bit(ID_A, &motg->inputs);
1756 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301757 set_bit(ID, &motg->inputs);
1758 ret = true;
1759 }
1760 break;
1761 case 0x04:
1762 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001763 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301764 motg->chg_type = USB_INVALID_CHARGER;
1765 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1766 clear_bit(ID_A, &motg->inputs);
1767 clear_bit(ID_B, &motg->inputs);
1768 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001769 ret = true;
1770 }
1771 break;
1772 default:
1773 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1774 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301775 test_and_clear_bit(ID_C, &motg->inputs) |
1776 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001777 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001778 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001779 motg->chg_type = USB_INVALID_CHARGER;
1780 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1781 }
1782 }
1783out:
1784 return ret;
1785}
1786
1787static void msm_chg_enable_aca_det(struct msm_otg *motg)
1788{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001789 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301791 if (!aca_enabled())
1792 return;
1793
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001794 switch (motg->pdata->phy_type) {
1795 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301796 /* Disable ID_GND in link and PHY */
1797 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1798 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001799 ulpi_write(phy, 0x01, 0x0C);
1800 ulpi_write(phy, 0x10, 0x0F);
1801 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301802 /* Disable PMIC ID pull-up */
1803 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301804 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001805 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301806 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807 break;
1808 default:
1809 break;
1810 }
1811}
1812
1813static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1814{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001815 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001816
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301817 if (!aca_enabled())
1818 return;
1819
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001820 switch (motg->pdata->phy_type) {
1821 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301822 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001823 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301824 break;
1825 default:
1826 break;
1827 }
1828}
1829
1830static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1831{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001832 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301833
1834 if (!aca_enabled())
1835 return;
1836
1837 switch (motg->pdata->phy_type) {
1838 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001839 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001840 break;
1841 default:
1842 break;
1843 }
1844}
1845
1846static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1847{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001848 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 bool ret = false;
1850
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301851 if (!aca_enabled())
1852 return ret;
1853
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001854 switch (motg->pdata->phy_type) {
1855 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001856 if (ulpi_read(phy, 0x91) & 1) {
1857 dev_dbg(phy->dev, "RID change\n");
1858 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001859 ret = msm_chg_aca_detect(motg);
1860 }
1861 default:
1862 break;
1863 }
1864 return ret;
1865}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301866
1867static void msm_otg_id_timer_func(unsigned long data)
1868{
1869 struct msm_otg *motg = (struct msm_otg *) data;
1870
1871 if (!aca_enabled())
1872 return;
1873
1874 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001875 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301876 return;
1877 }
1878
Steve Mucklef132c6c2012-06-06 18:30:57 -07001879 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301880 goto out;
1881
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301882 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001883 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301884 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301885 }
1886
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301887out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301888 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1889 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1890}
1891
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301892static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1893{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001894 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301895 u32 chg_det;
1896 bool ret = false;
1897
1898 switch (motg->pdata->phy_type) {
1899 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001900 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301901 ret = chg_det & (1 << 4);
1902 break;
1903 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001904 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301905 ret = chg_det & 1;
1906 break;
1907 default:
1908 break;
1909 }
1910 return ret;
1911}
1912
1913static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1914{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001915 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301916 u32 chg_det;
1917
1918 switch (motg->pdata->phy_type) {
1919 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001920 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301921 /* Turn off charger block */
1922 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001923 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301924 udelay(20);
1925 /* control chg block via ULPI */
1926 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001927 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301928 /* put it in host mode for enabling D- source */
1929 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001930 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301931 /* Turn on chg detect block */
1932 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001933 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301934 udelay(20);
1935 /* enable chg detection */
1936 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001937 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301938 break;
1939 case SNPS_28NM_INTEGRATED_PHY:
1940 /*
1941 * Configure DM as current source, DP as current sink
1942 * and enable battery charging comparators.
1943 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001944 ulpi_write(phy, 0x8, 0x85);
1945 ulpi_write(phy, 0x2, 0x85);
1946 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301947 break;
1948 default:
1949 break;
1950 }
1951}
1952
1953static bool msm_chg_check_primary_det(struct msm_otg *motg)
1954{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001955 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301956 u32 chg_det;
1957 bool ret = false;
1958
1959 switch (motg->pdata->phy_type) {
1960 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001961 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301962 ret = chg_det & (1 << 4);
1963 break;
1964 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001965 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301966 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301967 /* Turn off VDP_SRC */
1968 ulpi_write(phy, 0x3, 0x86);
1969 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301970 break;
1971 default:
1972 break;
1973 }
1974 return ret;
1975}
1976
1977static void msm_chg_enable_primary_det(struct msm_otg *motg)
1978{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001979 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301980 u32 chg_det;
1981
1982 switch (motg->pdata->phy_type) {
1983 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001984 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301985 /* enable chg detection */
1986 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001987 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301988 break;
1989 case SNPS_28NM_INTEGRATED_PHY:
1990 /*
1991 * Configure DP as current source, DM as current sink
1992 * and enable battery charging comparators.
1993 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001994 ulpi_write(phy, 0x2, 0x85);
1995 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301996 break;
1997 default:
1998 break;
1999 }
2000}
2001
2002static bool msm_chg_check_dcd(struct msm_otg *motg)
2003{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002004 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302005 u32 line_state;
2006 bool ret = false;
2007
2008 switch (motg->pdata->phy_type) {
2009 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002010 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302011 ret = !(line_state & 1);
2012 break;
2013 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002014 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302015 ret = line_state & 2;
2016 break;
2017 default:
2018 break;
2019 }
2020 return ret;
2021}
2022
2023static void msm_chg_disable_dcd(struct msm_otg *motg)
2024{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002025 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302026 u32 chg_det;
2027
2028 switch (motg->pdata->phy_type) {
2029 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002030 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302031 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002032 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302033 break;
2034 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002035 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302036 break;
2037 default:
2038 break;
2039 }
2040}
2041
2042static void msm_chg_enable_dcd(struct msm_otg *motg)
2043{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002044 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302045 u32 chg_det;
2046
2047 switch (motg->pdata->phy_type) {
2048 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002049 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302050 /* Turn on D+ current source */
2051 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002052 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302053 break;
2054 case SNPS_28NM_INTEGRATED_PHY:
2055 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002056 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302057 break;
2058 default:
2059 break;
2060 }
2061}
2062
2063static void msm_chg_block_on(struct msm_otg *motg)
2064{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002065 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066 u32 func_ctrl, chg_det;
2067
2068 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002069 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302070 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2071 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002072 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302073
2074 switch (motg->pdata->phy_type) {
2075 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002076 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302077 /* control chg block via ULPI */
2078 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002079 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302080 /* Turn on chg detect block */
2081 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002082 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302083 udelay(20);
2084 break;
2085 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302086 /* disable DP and DM pull down resistors */
2087 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302088 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002089 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302090 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002091 ulpi_write(phy, 0x1F, 0x92);
2092 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302093 udelay(100);
2094 break;
2095 default:
2096 break;
2097 }
2098}
2099
2100static void msm_chg_block_off(struct msm_otg *motg)
2101{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002102 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302103 u32 func_ctrl, chg_det;
2104
2105 switch (motg->pdata->phy_type) {
2106 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002107 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302108 /* Turn off charger block */
2109 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002110 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302111 break;
2112 case SNPS_28NM_INTEGRATED_PHY:
2113 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002114 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302115 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002116 ulpi_write(phy, 0x1F, 0x92);
2117 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302118 break;
2119 default:
2120 break;
2121 }
2122
2123 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002124 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302125 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2126 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002127 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302128}
2129
Anji jonnalad270e2d2011-08-09 11:28:32 +05302130static const char *chg_to_string(enum usb_chg_type chg_type)
2131{
2132 switch (chg_type) {
2133 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2134 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2135 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2136 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2137 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2138 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2139 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302140 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302141 default: return "INVALID_CHARGER";
2142 }
2143}
2144
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302145#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
2146#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302147#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2148#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302149static void msm_chg_detect_work(struct work_struct *w)
2150{
2151 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002152 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302153 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302154 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302155 unsigned long delay;
2156
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002157 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302158
2159 if (test_bit(MHL, &motg->inputs)) {
2160 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2161 return;
2162 }
2163
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302164 switch (motg->chg_state) {
2165 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302166 msm_chg_block_on(motg);
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302167 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002168 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302169 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2170 motg->dcd_retries = 0;
2171 delay = MSM_CHG_DCD_POLL_TIME;
2172 break;
2173 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302174 if (msm_chg_mhl_detect(motg)) {
2175 msm_chg_block_off(motg);
2176 motg->chg_state = USB_CHG_STATE_DETECTED;
2177 motg->chg_type = USB_INVALID_CHARGER;
2178 queue_work(system_nrt_wq, &motg->sm_work);
2179 return;
2180 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181 is_aca = msm_chg_aca_detect(motg);
2182 if (is_aca) {
2183 /*
2184 * ID_A can be ACA dock too. continue
2185 * primary detection after DCD.
2186 */
2187 if (test_bit(ID_A, &motg->inputs)) {
2188 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2189 } else {
2190 delay = 0;
2191 break;
2192 }
2193 }
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302194 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302195 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2196 if (is_dcd || tmout) {
Pavankumar Kondeti768dcb82012-10-05 13:21:45 +05302197 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302198 msm_chg_enable_primary_det(motg);
2199 delay = MSM_CHG_PRIMARY_DET_TIME;
2200 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2201 } else {
2202 delay = MSM_CHG_DCD_POLL_TIME;
2203 }
2204 break;
2205 case USB_CHG_STATE_DCD_DONE:
2206 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302207 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2208 dm_vlgc = line_state & PORTSC_LS_DM;
2209 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302210 if (test_bit(ID_A, &motg->inputs)) {
2211 motg->chg_type = USB_ACA_DOCK_CHARGER;
2212 motg->chg_state = USB_CHG_STATE_DETECTED;
2213 delay = 0;
2214 break;
2215 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302216 if (line_state) { /* DP > VLGC */
2217 motg->chg_type = USB_PROPRIETARY_CHARGER;
2218 motg->chg_state = USB_CHG_STATE_DETECTED;
2219 delay = 0;
2220 } else {
2221 msm_chg_enable_secondary_det(motg);
2222 delay = MSM_CHG_SECONDARY_DET_TIME;
2223 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2224 }
2225 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302226 if (test_bit(ID_A, &motg->inputs)) {
2227 motg->chg_type = USB_ACA_A_CHARGER;
2228 motg->chg_state = USB_CHG_STATE_DETECTED;
2229 delay = 0;
2230 break;
2231 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302232
2233 if (line_state) /* DP > VLGC or/and DM > VLGC */
2234 motg->chg_type = USB_PROPRIETARY_CHARGER;
2235 else
2236 motg->chg_type = USB_SDP_CHARGER;
2237
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302238 motg->chg_state = USB_CHG_STATE_DETECTED;
2239 delay = 0;
2240 }
2241 break;
2242 case USB_CHG_STATE_PRIMARY_DONE:
2243 vout = msm_chg_check_secondary_det(motg);
2244 if (vout)
2245 motg->chg_type = USB_DCP_CHARGER;
2246 else
2247 motg->chg_type = USB_CDP_CHARGER;
2248 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2249 /* fall through */
2250 case USB_CHG_STATE_SECONDARY_DONE:
2251 motg->chg_state = USB_CHG_STATE_DETECTED;
2252 case USB_CHG_STATE_DETECTED:
2253 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002254 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302255 /*
2256 * Spurious interrupt is seen after enabling ACA detection
2257 * due to which charger detection fails in case of PET.
2258 * Add delay of 100 microsec to avoid that.
2259 */
2260 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002261 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002262 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302263 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302264 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302265 return;
2266 default:
2267 return;
2268 }
2269
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302270 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302271}
2272
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302273/*
2274 * We support OTG, Peripheral only and Host only configurations. In case
2275 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2276 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2277 * enabled when switch is controlled by user and default mode is supplied
2278 * by board file, which can be changed by userspace later.
2279 */
2280static void msm_otg_init_sm(struct msm_otg *motg)
2281{
2282 struct msm_otg_platform_data *pdata = motg->pdata;
2283 u32 otgsc = readl(USB_OTGSC);
2284
2285 switch (pdata->mode) {
2286 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002287 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302288 if (pdata->default_mode == USB_HOST) {
2289 clear_bit(ID, &motg->inputs);
2290 } else if (pdata->default_mode == USB_PERIPHERAL) {
2291 set_bit(ID, &motg->inputs);
2292 set_bit(B_SESS_VLD, &motg->inputs);
2293 } else {
2294 set_bit(ID, &motg->inputs);
2295 clear_bit(B_SESS_VLD, &motg->inputs);
2296 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302297 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302298 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302299 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302300 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302301 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302302 set_bit(A_BUS_REQ, &motg->inputs);
2303 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002304 if (otgsc & OTGSC_BSV)
2305 set_bit(B_SESS_VLD, &motg->inputs);
2306 else
2307 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302308 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302309 if (pdata->pmic_id_irq) {
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302310 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302311 set_bit(ID, &motg->inputs);
2312 else
2313 clear_bit(ID, &motg->inputs);
2314 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302315 /*
2316 * VBUS initial state is reported after PMIC
2317 * driver initialization. Wait for it.
2318 */
2319 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302320 }
2321 break;
2322 case USB_HOST:
2323 clear_bit(ID, &motg->inputs);
2324 break;
2325 case USB_PERIPHERAL:
2326 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302327 if (pdata->otg_control == OTG_PHY_CONTROL) {
2328 if (otgsc & OTGSC_BSV)
2329 set_bit(B_SESS_VLD, &motg->inputs);
2330 else
2331 clear_bit(B_SESS_VLD, &motg->inputs);
2332 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2333 /*
2334 * VBUS initial state is reported after PMIC
2335 * driver initialization. Wait for it.
2336 */
2337 wait_for_completion(&pmic_vbus_init);
2338 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302339 break;
2340 default:
2341 break;
2342 }
2343}
2344
2345static void msm_otg_sm_work(struct work_struct *w)
2346{
2347 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002348 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302349 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302350
Steve Mucklef132c6c2012-06-06 18:30:57 -07002351 pm_runtime_resume(otg->phy->dev);
2352 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002353 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302354 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002355 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302356 msm_otg_init_sm(motg);
David Keitel272ce522012-08-17 16:25:24 -07002357 if (!psy && legacy_power_supply) {
2358 psy = power_supply_get_by_name("usb");
2359
2360 if (!psy)
2361 pr_err("couldn't get usb power supply\n");
2362 }
2363
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002364 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302365 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2366 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002367 pm_runtime_put_noidle(otg->phy->dev);
2368 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302369 break;
2370 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302371 /* FALL THROUGH */
2372 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302373 if (test_bit(MHL, &motg->inputs)) {
2374 /* allow LPM */
2375 pm_runtime_put_noidle(otg->phy->dev);
2376 pm_runtime_suspend(otg->phy->dev);
2377 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002378 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302379 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302380 if (msm_chg_mhl_detect(motg)) {
2381 work = 1;
2382 break;
2383 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302384 clear_bit(B_BUS_REQ, &motg->inputs);
2385 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002386 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302387 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302388 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302389 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302390 switch (motg->chg_state) {
2391 case USB_CHG_STATE_UNDEFINED:
2392 msm_chg_detect_work(&motg->chg_work.work);
2393 break;
2394 case USB_CHG_STATE_DETECTED:
2395 switch (motg->chg_type) {
2396 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302397 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002398 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302399 /* fall through */
2400 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302401 msm_otg_notify_charger(motg,
2402 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002403 pm_runtime_put_noidle(otg->phy->dev);
2404 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302405 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302406 case USB_ACA_B_CHARGER:
2407 msm_otg_notify_charger(motg,
2408 IDEV_ACA_CHG_MAX);
2409 /*
2410 * (ID_B --> ID_C) PHY_ALT interrupt can
2411 * not be detected in LPM.
2412 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302413 break;
2414 case USB_CDP_CHARGER:
2415 msm_otg_notify_charger(motg,
2416 IDEV_CHG_MAX);
2417 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002418 otg->phy->state =
2419 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302420 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302421 case USB_ACA_C_CHARGER:
2422 msm_otg_notify_charger(motg,
2423 IDEV_ACA_CHG_MAX);
2424 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002425 otg->phy->state =
2426 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302427 break;
2428 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302429 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002430 otg->phy->state =
2431 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302432 mod_timer(&motg->chg_check_timer,
2433 CHG_RECHECK_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302434 break;
2435 default:
2436 break;
2437 }
2438 break;
2439 default:
2440 break;
2441 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302442 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2443 pr_debug("b_sess_end && b_bus_req\n");
2444 if (msm_otg_start_srp(otg) < 0) {
2445 clear_bit(B_BUS_REQ, &motg->inputs);
2446 work = 1;
2447 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302448 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002449 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302450 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2451 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302452 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302453 pr_debug("chg_work cancel");
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302454 del_timer_sync(&motg->chg_check_timer);
2455 clear_bit(B_FALSE_SDP, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302456 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302457 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2458 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302459 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002460 msm_otg_reset(otg->phy);
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05302461 /*
2462 * There is a small window where ID interrupt
2463 * is not monitored during ID detection circuit
2464 * switch from ACA to PMIC. Check ID state
2465 * before entering into low power mode.
2466 */
2467 if (!msm_otg_read_pmic_id_state(motg)) {
2468 pr_debug("process missed ID intr\n");
2469 clear_bit(ID, &motg->inputs);
2470 work = 1;
2471 break;
2472 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002473 pm_runtime_put_noidle(otg->phy->dev);
Amit Blayd6f38282012-10-29 13:13:46 +02002474 /*
2475 * Only if autosuspend was enabled in probe, it will be
2476 * used here. Otherwise, no delay will be used.
2477 */
2478 pm_runtime_mark_last_busy(otg->phy->dev);
2479 pm_runtime_autosuspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302480 }
2481 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302482 case OTG_STATE_B_SRP_INIT:
2483 if (!test_bit(ID, &motg->inputs) ||
2484 test_bit(ID_A, &motg->inputs) ||
2485 test_bit(ID_C, &motg->inputs) ||
2486 (test_bit(B_SESS_VLD, &motg->inputs) &&
2487 !test_bit(ID_B, &motg->inputs))) {
2488 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2489 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002490 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302491 /*
2492 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2493 * bits after SRP initiation.
2494 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002495 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302496 work = 1;
2497 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2498 pr_debug("b_srp_fail\n");
2499 pr_info("A-device did not respond to SRP\n");
2500 clear_bit(B_BUS_REQ, &motg->inputs);
2501 clear_bit(B_SRP_FAIL, &motg->tmouts);
2502 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002503 ulpi_write(otg->phy, 0x0, 0x98);
2504 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302505 motg->b_last_se0_sess = jiffies;
2506 work = 1;
2507 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302508 break;
2509 case OTG_STATE_B_PERIPHERAL:
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05302510 if (test_bit(B_SESS_VLD, &motg->inputs) &&
2511 test_bit(B_FALSE_SDP, &motg->inputs)) {
2512 pr_debug("B_FALSE_SDP\n");
2513 msm_otg_start_peripheral(otg, 0);
2514 motg->chg_type = USB_DCP_CHARGER;
2515 clear_bit(B_FALSE_SDP, &motg->inputs);
2516 otg->phy->state = OTG_STATE_B_IDLE;
2517 work = 1;
2518 } else if (!test_bit(ID, &motg->inputs) ||
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302519 test_bit(ID_A, &motg->inputs) ||
2520 test_bit(ID_B, &motg->inputs) ||
2521 !test_bit(B_SESS_VLD, &motg->inputs)) {
2522 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302523 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2524 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302525 msm_otg_notify_charger(motg, 0);
2526 srp_reqd = otg->gadget->otg_srp_reqd;
2527 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302528 if (test_bit(ID_B, &motg->inputs))
2529 clear_bit(ID_B, &motg->inputs);
2530 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002531 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302532 motg->b_last_se0_sess = jiffies;
2533 if (srp_reqd)
2534 msm_otg_start_timer(motg,
2535 TB_TST_SRP, B_TST_SRP);
2536 else
2537 work = 1;
2538 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2539 otg->gadget->b_hnp_enable &&
2540 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2541 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2542 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2543 /* D+ pullup should not be disconnected within 4msec
2544 * after A device suspends the bus. Otherwise PET will
2545 * fail the compliance test.
2546 */
2547 udelay(1000);
2548 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002549 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302550 /*
2551 * start HCD even before A-device enable
2552 * pull-up to meet HNP timings.
2553 */
2554 otg->host->is_b_host = 1;
2555 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002556 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2557 test_bit(B_SESS_VLD, &motg->inputs)) {
2558 pr_debug("a_bus_suspend && b_sess_vld\n");
2559 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002560 pm_runtime_put_noidle(otg->phy->dev);
2561 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002562 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002563 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302564 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002565 }
2566 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302567 case OTG_STATE_B_WAIT_ACON:
2568 if (!test_bit(ID, &motg->inputs) ||
2569 test_bit(ID_A, &motg->inputs) ||
2570 test_bit(ID_B, &motg->inputs) ||
2571 !test_bit(B_SESS_VLD, &motg->inputs)) {
2572 pr_debug("!id || id_a/b || !b_sess_vld\n");
2573 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302574 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302575 * A-device is physically disconnected during
2576 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302577 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302578 msm_otg_start_host(otg, 0);
2579 otg->host->is_b_host = 0;
2580
2581 clear_bit(B_BUS_REQ, &motg->inputs);
2582 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2583 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002584 otg->phy->state = OTG_STATE_B_IDLE;
2585 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302586 work = 1;
2587 } else if (test_bit(A_CONN, &motg->inputs)) {
2588 pr_debug("a_conn\n");
2589 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002590 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302591 /*
2592 * PET disconnects D+ pullup after reset is generated
2593 * by B device in B_HOST role which is not detected by
2594 * B device. As workaorund , start timer of 300msec
2595 * and stop timer if A device is enumerated else clear
2596 * A_CONN.
2597 */
2598 msm_otg_start_timer(motg, TB_TST_CONFIG,
2599 B_TST_CONFIG);
2600 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2601 pr_debug("b_ase0_brst_tmout\n");
2602 pr_info("B HNP fail:No response from A device\n");
2603 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002604 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302605 otg->host->is_b_host = 0;
2606 clear_bit(B_ASE0_BRST, &motg->tmouts);
2607 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2608 clear_bit(B_BUS_REQ, &motg->inputs);
2609 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002610 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302611 work = 1;
2612 } else if (test_bit(ID_C, &motg->inputs)) {
2613 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2614 }
2615 break;
2616 case OTG_STATE_B_HOST:
2617 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2618 !test_bit(A_CONN, &motg->inputs) ||
2619 !test_bit(B_SESS_VLD, &motg->inputs)) {
2620 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2621 clear_bit(A_CONN, &motg->inputs);
2622 clear_bit(B_BUS_REQ, &motg->inputs);
2623 msm_otg_start_host(otg, 0);
2624 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002625 otg->phy->state = OTG_STATE_B_IDLE;
2626 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302627 work = 1;
2628 } else if (test_bit(ID_C, &motg->inputs)) {
2629 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2630 }
2631 break;
2632 case OTG_STATE_A_IDLE:
2633 otg->default_a = 1;
2634 if (test_bit(ID, &motg->inputs) &&
2635 !test_bit(ID_A, &motg->inputs)) {
2636 pr_debug("id && !id_a\n");
2637 otg->default_a = 0;
2638 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002639 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302640 del_timer_sync(&motg->id_timer);
2641 msm_otg_link_reset(motg);
2642 msm_chg_enable_aca_intr(motg);
2643 msm_otg_notify_charger(motg, 0);
2644 work = 1;
2645 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2646 (test_bit(A_SRP_DET, &motg->inputs) ||
2647 test_bit(A_BUS_REQ, &motg->inputs))) {
2648 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2649
2650 clear_bit(A_SRP_DET, &motg->inputs);
2651 /* Disable SRP detection */
2652 writel_relaxed((readl_relaxed(USB_OTGSC) &
2653 ~OTGSC_INTSTS_MASK) &
2654 ~OTGSC_DPIE, USB_OTGSC);
2655
Steve Mucklef132c6c2012-06-06 18:30:57 -07002656 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302657 /* VBUS should not be supplied before end of SRP pulse
2658 * generated by PET, if not complaince test fail.
2659 */
2660 usleep_range(10000, 12000);
2661 /* ACA: ID_A: Stop charging untill enumeration */
2662 if (test_bit(ID_A, &motg->inputs))
2663 msm_otg_notify_charger(motg, 0);
2664 else
2665 msm_hsusb_vbus_power(motg, 1);
2666 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2667 } else {
2668 pr_debug("No session requested\n");
2669 clear_bit(A_BUS_DROP, &motg->inputs);
2670 if (test_bit(ID_A, &motg->inputs)) {
2671 msm_otg_notify_charger(motg,
2672 IDEV_ACA_CHG_MAX);
2673 } else if (!test_bit(ID, &motg->inputs)) {
2674 msm_otg_notify_charger(motg, 0);
2675 /*
2676 * A-device is not providing power on VBUS.
2677 * Enable SRP detection.
2678 */
2679 writel_relaxed(0x13, USB_USBMODE);
2680 writel_relaxed((readl_relaxed(USB_OTGSC) &
2681 ~OTGSC_INTSTS_MASK) |
2682 OTGSC_DPIE, USB_OTGSC);
2683 mb();
2684 }
2685 }
2686 break;
2687 case OTG_STATE_A_WAIT_VRISE:
2688 if ((test_bit(ID, &motg->inputs) &&
2689 !test_bit(ID_A, &motg->inputs)) ||
2690 test_bit(A_BUS_DROP, &motg->inputs) ||
2691 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2692 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2693 clear_bit(A_BUS_REQ, &motg->inputs);
2694 msm_otg_del_timer(motg);
2695 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002696 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302697 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2698 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2699 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002700 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302701 if (TA_WAIT_BCON > 0)
2702 msm_otg_start_timer(motg, TA_WAIT_BCON,
2703 A_WAIT_BCON);
2704 msm_otg_start_host(otg, 1);
2705 msm_chg_enable_aca_det(motg);
2706 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302707 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302708 if (msm_chg_check_aca_intr(motg))
2709 work = 1;
2710 }
2711 break;
2712 case OTG_STATE_A_WAIT_BCON:
2713 if ((test_bit(ID, &motg->inputs) &&
2714 !test_bit(ID_A, &motg->inputs)) ||
2715 test_bit(A_BUS_DROP, &motg->inputs) ||
2716 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2717 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2718 "a_wait_bcon_tmout\n");
2719 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2720 pr_info("Device No Response\n");
2721 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2722 }
2723 msm_otg_del_timer(motg);
2724 clear_bit(A_BUS_REQ, &motg->inputs);
2725 clear_bit(B_CONN, &motg->inputs);
2726 msm_otg_start_host(otg, 0);
2727 /*
2728 * ACA: ID_A with NO accessory, just the A plug is
2729 * attached to ACA: Use IDCHG_MAX for charging
2730 */
2731 if (test_bit(ID_A, &motg->inputs))
2732 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2733 else
2734 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002735 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302736 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2737 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2738 pr_debug("!a_vbus_vld\n");
2739 clear_bit(B_CONN, &motg->inputs);
2740 msm_otg_del_timer(motg);
2741 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002742 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2743 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302744 } else if (test_bit(ID_A, &motg->inputs)) {
2745 msm_hsusb_vbus_power(motg, 0);
2746 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2747 /*
2748 * If TA_WAIT_BCON is infinite, we don;t
2749 * turn off VBUS. Enter low power mode.
2750 */
2751 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002752 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302753 } else if (!test_bit(ID, &motg->inputs)) {
2754 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302755 }
2756 break;
2757 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302758 if ((test_bit(ID, &motg->inputs) &&
2759 !test_bit(ID_A, &motg->inputs)) ||
2760 test_bit(A_BUS_DROP, &motg->inputs)) {
2761 pr_debug("id_a/b/c || a_bus_drop\n");
2762 clear_bit(B_CONN, &motg->inputs);
2763 clear_bit(A_BUS_REQ, &motg->inputs);
2764 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002765 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302766 msm_otg_start_host(otg, 0);
2767 if (!test_bit(ID_A, &motg->inputs))
2768 msm_hsusb_vbus_power(motg, 0);
2769 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2770 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2771 pr_debug("!a_vbus_vld\n");
2772 clear_bit(B_CONN, &motg->inputs);
2773 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002774 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302775 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002776 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302777 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2778 /*
2779 * a_bus_req is de-asserted when root hub is
2780 * suspended or HNP is in progress.
2781 */
2782 pr_debug("!a_bus_req\n");
2783 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002784 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302785 if (otg->host->b_hnp_enable)
2786 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2787 A_AIDL_BDIS);
2788 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002789 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302790 } else if (!test_bit(B_CONN, &motg->inputs)) {
2791 pr_debug("!b_conn\n");
2792 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002793 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302794 if (TA_WAIT_BCON > 0)
2795 msm_otg_start_timer(motg, TA_WAIT_BCON,
2796 A_WAIT_BCON);
2797 if (msm_chg_check_aca_intr(motg))
2798 work = 1;
2799 } else if (test_bit(ID_A, &motg->inputs)) {
2800 msm_otg_del_timer(motg);
2801 msm_hsusb_vbus_power(motg, 0);
2802 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2803 msm_otg_notify_charger(motg,
2804 IDEV_ACA_CHG_MAX);
2805 else
2806 msm_otg_notify_charger(motg,
2807 IDEV_CHG_MIN - motg->mA_port);
2808 } else if (!test_bit(ID, &motg->inputs)) {
2809 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2810 motg->chg_type = USB_INVALID_CHARGER;
2811 msm_otg_notify_charger(motg, 0);
2812 msm_hsusb_vbus_power(motg, 1);
2813 }
2814 break;
2815 case OTG_STATE_A_SUSPEND:
2816 if ((test_bit(ID, &motg->inputs) &&
2817 !test_bit(ID_A, &motg->inputs)) ||
2818 test_bit(A_BUS_DROP, &motg->inputs) ||
2819 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2820 pr_debug("id_a/b/c || a_bus_drop ||"
2821 "a_aidl_bdis_tmout\n");
2822 msm_otg_del_timer(motg);
2823 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002824 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302825 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002826 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302827 if (!test_bit(ID_A, &motg->inputs))
2828 msm_hsusb_vbus_power(motg, 0);
2829 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2830 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2831 pr_debug("!a_vbus_vld\n");
2832 msm_otg_del_timer(motg);
2833 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002834 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302835 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002836 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302837 } else if (!test_bit(B_CONN, &motg->inputs) &&
2838 otg->host->b_hnp_enable) {
2839 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002840 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302841 msm_otg_host_hnp_enable(otg, 1);
2842 otg->gadget->is_a_peripheral = 1;
2843 msm_otg_start_peripheral(otg, 1);
2844 } else if (!test_bit(B_CONN, &motg->inputs) &&
2845 !otg->host->b_hnp_enable) {
2846 pr_debug("!b_conn && !b_hnp_enable");
2847 /*
2848 * bus request is dropped during suspend.
2849 * acquire again for next device.
2850 */
2851 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002852 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302853 if (TA_WAIT_BCON > 0)
2854 msm_otg_start_timer(motg, TA_WAIT_BCON,
2855 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002856 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302857 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002858 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302859 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002860 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002861 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302862 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302863 }
2864 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302865 case OTG_STATE_A_PERIPHERAL:
2866 if ((test_bit(ID, &motg->inputs) &&
2867 !test_bit(ID_A, &motg->inputs)) ||
2868 test_bit(A_BUS_DROP, &motg->inputs)) {
2869 pr_debug("id _f/b/c || a_bus_drop\n");
2870 /* Clear BIDL_ADIS timer */
2871 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002872 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302873 msm_otg_start_peripheral(otg, 0);
2874 otg->gadget->is_a_peripheral = 0;
2875 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002876 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302877 if (!test_bit(ID_A, &motg->inputs))
2878 msm_hsusb_vbus_power(motg, 0);
2879 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2880 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2881 pr_debug("!a_vbus_vld\n");
2882 /* Clear BIDL_ADIS timer */
2883 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002884 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302885 msm_otg_start_peripheral(otg, 0);
2886 otg->gadget->is_a_peripheral = 0;
2887 msm_otg_start_host(otg, 0);
2888 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2889 pr_debug("a_bidl_adis_tmout\n");
2890 msm_otg_start_peripheral(otg, 0);
2891 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002892 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302893 set_bit(A_BUS_REQ, &motg->inputs);
2894 msm_otg_host_hnp_enable(otg, 0);
2895 if (TA_WAIT_BCON > 0)
2896 msm_otg_start_timer(motg, TA_WAIT_BCON,
2897 A_WAIT_BCON);
2898 } else if (test_bit(ID_A, &motg->inputs)) {
2899 msm_hsusb_vbus_power(motg, 0);
2900 msm_otg_notify_charger(motg,
2901 IDEV_CHG_MIN - motg->mA_port);
2902 } else if (!test_bit(ID, &motg->inputs)) {
2903 msm_otg_notify_charger(motg, 0);
2904 msm_hsusb_vbus_power(motg, 1);
2905 }
2906 break;
2907 case OTG_STATE_A_WAIT_VFALL:
2908 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2909 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002910 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302911 work = 1;
2912 }
2913 break;
2914 case OTG_STATE_A_VBUS_ERR:
2915 if ((test_bit(ID, &motg->inputs) &&
2916 !test_bit(ID_A, &motg->inputs)) ||
2917 test_bit(A_BUS_DROP, &motg->inputs) ||
2918 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002919 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302920 if (!test_bit(ID_A, &motg->inputs))
2921 msm_hsusb_vbus_power(motg, 0);
2922 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2923 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2924 motg->chg_type = USB_INVALID_CHARGER;
2925 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302926 }
2927 break;
2928 default:
2929 break;
2930 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302931 if (work)
2932 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302933}
2934
Amit Blayd0fe07b2012-09-05 16:42:09 +03002935static void msm_otg_suspend_work(struct work_struct *w)
2936{
2937 struct msm_otg *motg =
2938 container_of(w, struct msm_otg, suspend_work.work);
2939 atomic_set(&motg->suspend_work_pending, 0);
2940 msm_otg_sm_work(&motg->sm_work);
2941}
2942
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302943static irqreturn_t msm_otg_irq(int irq, void *data)
2944{
2945 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002946 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302947 u32 otgsc = 0, usbsts, pc;
2948 bool work = 0;
2949 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302950
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302951 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002952 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302953 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002954 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07002955 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002956 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302957 return IRQ_HANDLED;
2958 }
2959
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002960 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302961 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302962
2963 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302964 return IRQ_NONE;
2965
2966 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302967 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302968 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302969 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302970 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302971 pr_debug("Id clear\n");
2972 /*
2973 * Assert a_bus_req to supply power on
2974 * VBUS when Micro/Mini-A cable is connected
2975 * with out user intervention.
2976 */
2977 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302978 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302979 msm_chg_enable_aca_det(motg);
2980 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302981 writel_relaxed(otgsc, USB_OTGSC);
2982 work = 1;
2983 } else if (otgsc & OTGSC_DPIS) {
2984 pr_debug("DPIS detected\n");
2985 writel_relaxed(otgsc, USB_OTGSC);
2986 set_bit(A_SRP_DET, &motg->inputs);
2987 set_bit(A_BUS_REQ, &motg->inputs);
2988 work = 1;
2989 } else if (otgsc & OTGSC_BSVIS) {
2990 writel_relaxed(otgsc, USB_OTGSC);
2991 /*
2992 * BSV interrupt comes when operating as an A-device
2993 * (VBUS on/off).
2994 * But, handle BSV when charger is removed from ACA in ID_A
2995 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002996 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302997 !test_bit(ID_A, &motg->inputs))
2998 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302999 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303000 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303001 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303002 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303003 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303004 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03003005 clear_bit(A_BUS_SUSPEND, &motg->inputs);
3006
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303007 msm_chg_check_aca_intr(motg);
3008 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303009 work = 1;
3010 } else if (usbsts & STS_PCI) {
3011 pc = readl_relaxed(USB_PORTSC);
3012 pr_debug("portsc = %x\n", pc);
3013 ret = IRQ_NONE;
3014 /*
3015 * HCD Acks PCI interrupt. We use this to switch
3016 * between different OTG states.
3017 */
3018 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003019 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303020 case OTG_STATE_A_SUSPEND:
3021 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
3022 !(pc & PORTSC_CCS)) {
3023 pr_debug("B_CONN clear\n");
3024 clear_bit(B_CONN, &motg->inputs);
3025 msm_otg_del_timer(motg);
3026 }
3027 break;
3028 case OTG_STATE_A_PERIPHERAL:
3029 /*
3030 * A-peripheral observed activity on bus.
3031 * clear A_BIDL_ADIS timer.
3032 */
3033 msm_otg_del_timer(motg);
3034 work = 0;
3035 break;
3036 case OTG_STATE_B_WAIT_ACON:
3037 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
3038 pr_debug("A_CONN set\n");
3039 set_bit(A_CONN, &motg->inputs);
3040 /* Clear ASE0_BRST timer */
3041 msm_otg_del_timer(motg);
3042 }
3043 break;
3044 case OTG_STATE_B_HOST:
3045 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
3046 pr_debug("A_CONN clear\n");
3047 clear_bit(A_CONN, &motg->inputs);
3048 msm_otg_del_timer(motg);
3049 }
3050 break;
3051 case OTG_STATE_A_WAIT_BCON:
3052 if (TA_WAIT_BCON < 0)
3053 set_bit(A_BUS_REQ, &motg->inputs);
3054 default:
3055 work = 0;
3056 break;
3057 }
3058 } else if (usbsts & STS_URI) {
3059 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003060 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303061 case OTG_STATE_A_PERIPHERAL:
3062 /*
3063 * A-peripheral observed activity on bus.
3064 * clear A_BIDL_ADIS timer.
3065 */
3066 msm_otg_del_timer(motg);
3067 work = 0;
3068 break;
3069 default:
3070 work = 0;
3071 break;
3072 }
3073 } else if (usbsts & STS_SLI) {
3074 ret = IRQ_NONE;
3075 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003076 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303077 case OTG_STATE_B_PERIPHERAL:
3078 if (otg->gadget->b_hnp_enable) {
3079 set_bit(A_BUS_SUSPEND, &motg->inputs);
3080 set_bit(B_BUS_REQ, &motg->inputs);
3081 work = 1;
3082 }
3083 break;
3084 case OTG_STATE_A_PERIPHERAL:
3085 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3086 A_BIDL_ADIS);
3087 break;
3088 default:
3089 break;
3090 }
3091 } else if ((usbsts & PHY_ALT_INT)) {
3092 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3093 if (msm_chg_check_aca_intr(motg))
3094 work = 1;
3095 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303096 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303097 if (work)
3098 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303099
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303100 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003101}
3102
3103static void msm_otg_set_vbus_state(int online)
3104{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303105 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003106 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303107
Chiranjeevi Velempati2e1048b2012-11-08 09:54:33 +05303108 /* Ignore received BSV interrupts, if ID pin is GND */
3109 if (!test_bit(ID, &motg->inputs))
Mayank Ranabaf31f42012-07-05 09:43:54 +05303110 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003111
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303112 if (online) {
3113 pr_debug("PMIC: BSV set\n");
3114 set_bit(B_SESS_VLD, &motg->inputs);
3115 } else {
3116 pr_debug("PMIC: BSV clear\n");
3117 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303118 }
3119
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303120 if (!init) {
3121 init = true;
3122 complete(&pmic_vbus_init);
3123 pr_debug("PMIC: BSV init complete\n");
3124 return;
3125 }
3126
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303127 if (test_bit(MHL, &motg->inputs) ||
3128 mhl_det_in_progress) {
3129 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3130 return;
3131 }
3132
Jack Pham5ca279b2012-05-14 18:42:54 -07003133 if (atomic_read(&motg->pm_suspended))
3134 motg->sm_work_pending = true;
3135 else
3136 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003137}
3138
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303139static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003140{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303141 struct msm_otg *motg = container_of(w, struct msm_otg,
3142 pmic_id_status_work.work);
3143 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003144
Pavankumar Kondetibecab5b2012-11-06 16:45:10 +05303145 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303146 if (!test_and_set_bit(ID, &motg->inputs)) {
3147 pr_debug("PMIC: ID set\n");
3148 work = 1;
3149 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303150 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303151 if (test_and_clear_bit(ID, &motg->inputs)) {
3152 pr_debug("PMIC: ID clear\n");
3153 set_bit(A_BUS_REQ, &motg->inputs);
3154 work = 1;
3155 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303156 }
3157
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303158 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003159 if (atomic_read(&motg->pm_suspended))
3160 motg->sm_work_pending = true;
3161 else
3162 queue_work(system_nrt_wq, &motg->sm_work);
3163 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303164
3165}
3166
3167#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3168static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3169{
3170 struct msm_otg *motg = data;
3171
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303172 if (test_bit(MHL, &motg->inputs) ||
3173 mhl_det_in_progress) {
3174 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3175 return IRQ_HANDLED;
3176 }
3177
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303178 if (!aca_id_turned_on)
3179 /*schedule delayed work for 5msec for ID line state to settle*/
3180 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3181 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003182
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303183 return IRQ_HANDLED;
3184}
3185
3186static int msm_otg_mode_show(struct seq_file *s, void *unused)
3187{
3188 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003189 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303190
Steve Mucklef132c6c2012-06-06 18:30:57 -07003191 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303192 case OTG_STATE_A_HOST:
3193 seq_printf(s, "host\n");
3194 break;
3195 case OTG_STATE_B_PERIPHERAL:
3196 seq_printf(s, "peripheral\n");
3197 break;
3198 default:
3199 seq_printf(s, "none\n");
3200 break;
3201 }
3202
3203 return 0;
3204}
3205
3206static int msm_otg_mode_open(struct inode *inode, struct file *file)
3207{
3208 return single_open(file, msm_otg_mode_show, inode->i_private);
3209}
3210
3211static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3212 size_t count, loff_t *ppos)
3213{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303214 struct seq_file *s = file->private_data;
3215 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303216 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003217 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303218 int status = count;
3219 enum usb_mode_type req_mode;
3220
3221 memset(buf, 0x00, sizeof(buf));
3222
3223 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3224 status = -EFAULT;
3225 goto out;
3226 }
3227
3228 if (!strncmp(buf, "host", 4)) {
3229 req_mode = USB_HOST;
3230 } else if (!strncmp(buf, "peripheral", 10)) {
3231 req_mode = USB_PERIPHERAL;
3232 } else if (!strncmp(buf, "none", 4)) {
3233 req_mode = USB_NONE;
3234 } else {
3235 status = -EINVAL;
3236 goto out;
3237 }
3238
3239 switch (req_mode) {
3240 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003241 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303242 case OTG_STATE_A_HOST:
3243 case OTG_STATE_B_PERIPHERAL:
3244 set_bit(ID, &motg->inputs);
3245 clear_bit(B_SESS_VLD, &motg->inputs);
3246 break;
3247 default:
3248 goto out;
3249 }
3250 break;
3251 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003252 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303253 case OTG_STATE_B_IDLE:
3254 case OTG_STATE_A_HOST:
3255 set_bit(ID, &motg->inputs);
3256 set_bit(B_SESS_VLD, &motg->inputs);
3257 break;
3258 default:
3259 goto out;
3260 }
3261 break;
3262 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003263 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303264 case OTG_STATE_B_IDLE:
3265 case OTG_STATE_B_PERIPHERAL:
3266 clear_bit(ID, &motg->inputs);
3267 break;
3268 default:
3269 goto out;
3270 }
3271 break;
3272 default:
3273 goto out;
3274 }
3275
Steve Mucklef132c6c2012-06-06 18:30:57 -07003276 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303277 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303278out:
3279 return status;
3280}
3281
3282const struct file_operations msm_otg_mode_fops = {
3283 .open = msm_otg_mode_open,
3284 .read = seq_read,
3285 .write = msm_otg_mode_write,
3286 .llseek = seq_lseek,
3287 .release = single_release,
3288};
3289
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303290static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3291{
3292 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003293 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303294
Steve Mucklef132c6c2012-06-06 18:30:57 -07003295 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303296 return 0;
3297}
3298
3299static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3300{
3301 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3302}
3303
3304const struct file_operations msm_otg_state_fops = {
3305 .open = msm_otg_otg_state_open,
3306 .read = seq_read,
3307 .llseek = seq_lseek,
3308 .release = single_release,
3309};
3310
Anji jonnalad270e2d2011-08-09 11:28:32 +05303311static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3312{
3313 struct msm_otg *motg = s->private;
3314
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303315 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303316 return 0;
3317}
3318
3319static int msm_otg_chg_open(struct inode *inode, struct file *file)
3320{
3321 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3322}
3323
3324const struct file_operations msm_otg_chg_fops = {
3325 .open = msm_otg_chg_open,
3326 .read = seq_read,
3327 .llseek = seq_lseek,
3328 .release = single_release,
3329};
3330
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303331static int msm_otg_aca_show(struct seq_file *s, void *unused)
3332{
3333 if (debug_aca_enabled)
3334 seq_printf(s, "enabled\n");
3335 else
3336 seq_printf(s, "disabled\n");
3337
3338 return 0;
3339}
3340
3341static int msm_otg_aca_open(struct inode *inode, struct file *file)
3342{
3343 return single_open(file, msm_otg_aca_show, inode->i_private);
3344}
3345
3346static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3347 size_t count, loff_t *ppos)
3348{
3349 char buf[8];
3350
3351 memset(buf, 0x00, sizeof(buf));
3352
3353 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3354 return -EFAULT;
3355
3356 if (!strncmp(buf, "enable", 6))
3357 debug_aca_enabled = true;
3358 else
3359 debug_aca_enabled = false;
3360
3361 return count;
3362}
3363
3364const struct file_operations msm_otg_aca_fops = {
3365 .open = msm_otg_aca_open,
3366 .read = seq_read,
3367 .write = msm_otg_aca_write,
3368 .llseek = seq_lseek,
3369 .release = single_release,
3370};
3371
Manu Gautam8bdcc592012-03-06 11:26:06 +05303372static int msm_otg_bus_show(struct seq_file *s, void *unused)
3373{
3374 if (debug_bus_voting_enabled)
3375 seq_printf(s, "enabled\n");
3376 else
3377 seq_printf(s, "disabled\n");
3378
3379 return 0;
3380}
3381
3382static int msm_otg_bus_open(struct inode *inode, struct file *file)
3383{
3384 return single_open(file, msm_otg_bus_show, inode->i_private);
3385}
3386
3387static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3388 size_t count, loff_t *ppos)
3389{
3390 char buf[8];
3391 int ret;
3392 struct seq_file *s = file->private_data;
3393 struct msm_otg *motg = s->private;
3394
3395 memset(buf, 0x00, sizeof(buf));
3396
3397 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3398 return -EFAULT;
3399
3400 if (!strncmp(buf, "enable", 6)) {
3401 /* Do not vote here. Let OTG statemachine decide when to vote */
3402 debug_bus_voting_enabled = true;
3403 } else {
3404 debug_bus_voting_enabled = false;
3405 if (motg->bus_perf_client) {
3406 ret = msm_bus_scale_client_update_request(
3407 motg->bus_perf_client, 0);
3408 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003409 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303410 "for bus bw %d\n", __func__, ret);
3411 }
3412 }
3413
3414 return count;
3415}
3416
David Keitel272ce522012-08-17 16:25:24 -07003417static int otg_power_get_property_usb(struct power_supply *psy,
3418 enum power_supply_property psp,
3419 union power_supply_propval *val)
3420{
Jack Pham0c695282012-10-19 18:13:03 -07003421 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003422 switch (psp) {
3423 case POWER_SUPPLY_PROP_SCOPE:
3424 if (motg->host_mode)
3425 val->intval = POWER_SUPPLY_SCOPE_SYSTEM;
3426 else
3427 val->intval = POWER_SUPPLY_SCOPE_DEVICE;
3428 break;
3429 case POWER_SUPPLY_PROP_CURRENT_MAX:
3430 val->intval = motg->current_max;
3431 break;
3432 /* Reflect USB enumeration */
3433 case POWER_SUPPLY_PROP_PRESENT:
3434 case POWER_SUPPLY_PROP_ONLINE:
3435 val->intval = motg->online;
3436 break;
3437 default:
3438 return -EINVAL;
3439 }
3440 return 0;
3441}
3442
3443static int otg_power_set_property_usb(struct power_supply *psy,
3444 enum power_supply_property psp,
3445 const union power_supply_propval *val)
3446{
Jack Pham0c695282012-10-19 18:13:03 -07003447 struct msm_otg *motg = container_of(psy, struct msm_otg, usb_psy);
David Keitel272ce522012-08-17 16:25:24 -07003448
3449 switch (psp) {
3450 /* Process PMIC notification in PRESENT prop */
3451 case POWER_SUPPLY_PROP_PRESENT:
3452 msm_otg_set_vbus_state(val->intval);
3453 break;
3454 /* The ONLINE property reflects if usb has enumerated */
3455 case POWER_SUPPLY_PROP_ONLINE:
3456 motg->online = val->intval;
3457 break;
3458 case POWER_SUPPLY_PROP_CURRENT_MAX:
3459 motg->current_max = val->intval;
3460 break;
3461 default:
3462 return -EINVAL;
3463 }
3464
3465 power_supply_changed(&motg->usb_psy);
3466 return 0;
3467}
3468
3469static char *otg_pm_power_supplied_to[] = {
3470 "battery",
3471};
3472
3473static enum power_supply_property otg_pm_power_props_usb[] = {
3474 POWER_SUPPLY_PROP_PRESENT,
3475 POWER_SUPPLY_PROP_ONLINE,
3476 POWER_SUPPLY_PROP_CURRENT_MAX,
3477 POWER_SUPPLY_PROP_SCOPE,
3478};
3479
Manu Gautam8bdcc592012-03-06 11:26:06 +05303480const struct file_operations msm_otg_bus_fops = {
3481 .open = msm_otg_bus_open,
3482 .read = seq_read,
3483 .write = msm_otg_bus_write,
3484 .llseek = seq_lseek,
3485 .release = single_release,
3486};
3487
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303488static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303489
3490static int msm_otg_debugfs_init(struct msm_otg *motg)
3491{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303492 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303493
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303494 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3495
3496 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3497 return -ENODEV;
3498
Anji jonnalad270e2d2011-08-09 11:28:32 +05303499 if (motg->pdata->mode == USB_OTG &&
3500 motg->pdata->otg_control == OTG_USER_CONTROL) {
3501
Manu Gautam8bdcc592012-03-06 11:26:06 +05303502 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303503 S_IWUSR, msm_otg_dbg_root, motg,
3504 &msm_otg_mode_fops);
3505
Manu Gautam8bdcc592012-03-06 11:26:06 +05303506 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303507 debugfs_remove(msm_otg_dbg_root);
3508 msm_otg_dbg_root = NULL;
3509 return -ENODEV;
3510 }
3511 }
3512
Manu Gautam8bdcc592012-03-06 11:26:06 +05303513 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303514 msm_otg_dbg_root, motg,
3515 &msm_otg_chg_fops);
3516
Manu Gautam8bdcc592012-03-06 11:26:06 +05303517 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303518 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303519 return -ENODEV;
3520 }
3521
Manu Gautam8bdcc592012-03-06 11:26:06 +05303522 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303523 msm_otg_dbg_root, motg,
3524 &msm_otg_aca_fops);
3525
Manu Gautam8bdcc592012-03-06 11:26:06 +05303526 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303527 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303528 return -ENODEV;
3529 }
3530
Manu Gautam8bdcc592012-03-06 11:26:06 +05303531 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3532 msm_otg_dbg_root, motg,
3533 &msm_otg_bus_fops);
3534
3535 if (!msm_otg_dentry) {
3536 debugfs_remove_recursive(msm_otg_dbg_root);
3537 return -ENODEV;
3538 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303539
3540 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3541 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3542
3543 if (!msm_otg_dentry) {
3544 debugfs_remove_recursive(msm_otg_dbg_root);
3545 return -ENODEV;
3546 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303547 return 0;
3548}
3549
3550static void msm_otg_debugfs_cleanup(void)
3551{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303552 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303553}
3554
Manu Gautam0ddbd922012-09-21 17:17:38 +05303555#define MSM_OTG_CMD_ID 0x09
3556#define MSM_OTG_DEVICE_ID 0x04
3557#define MSM_OTG_VMID_IDX 0xFF
3558#define MSM_OTG_MEM_TYPE 0x02
3559struct msm_otg_scm_cmd_buf {
3560 unsigned int device_id;
3561 unsigned int vmid_idx;
3562 unsigned int mem_type;
3563} __attribute__ ((__packed__));
3564
3565static void msm_otg_pnoc_errata_fix(struct msm_otg *motg)
3566{
3567 int ret;
3568 struct msm_otg_platform_data *pdata = motg->pdata;
3569 struct msm_otg_scm_cmd_buf cmd_buf;
3570
3571 if (!pdata->pnoc_errata_fix)
3572 return;
3573
3574 dev_dbg(motg->phy.dev, "applying fix for pnoc h/w issue\n");
3575
3576 cmd_buf.device_id = MSM_OTG_DEVICE_ID;
3577 cmd_buf.vmid_idx = MSM_OTG_VMID_IDX;
3578 cmd_buf.mem_type = MSM_OTG_MEM_TYPE;
3579
3580 ret = scm_call(SCM_SVC_CP, MSM_OTG_CMD_ID, &cmd_buf,
3581 sizeof(cmd_buf), NULL, 0);
3582
3583 if (ret)
3584 dev_err(motg->phy.dev, "scm command failed to update VMIDMT\n");
3585}
3586
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303587static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3588static struct platform_device *msm_otg_add_pdev(
3589 struct platform_device *ofdev, const char *name)
3590{
3591 struct platform_device *pdev;
3592 const struct resource *res = ofdev->resource;
3593 unsigned int num = ofdev->num_resources;
3594 int retval;
3595
3596 pdev = platform_device_alloc(name, -1);
3597 if (!pdev) {
3598 retval = -ENOMEM;
3599 goto error;
3600 }
3601
3602 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3603 pdev->dev.dma_mask = &msm_otg_dma_mask;
3604
3605 if (num) {
3606 retval = platform_device_add_resources(pdev, res, num);
3607 if (retval)
3608 goto error;
3609 }
3610
3611 retval = platform_device_add(pdev);
3612 if (retval)
3613 goto error;
3614
3615 return pdev;
3616
3617error:
3618 platform_device_put(pdev);
3619 return ERR_PTR(retval);
3620}
3621
3622static int msm_otg_setup_devices(struct platform_device *ofdev,
3623 enum usb_mode_type mode, bool init)
3624{
3625 const char *gadget_name = "msm_hsusb";
3626 const char *host_name = "msm_hsusb_host";
3627 static struct platform_device *gadget_pdev;
3628 static struct platform_device *host_pdev;
3629 int retval = 0;
3630
3631 if (!init) {
3632 if (gadget_pdev)
3633 platform_device_unregister(gadget_pdev);
3634 if (host_pdev)
3635 platform_device_unregister(host_pdev);
3636 return 0;
3637 }
3638
3639 switch (mode) {
3640 case USB_OTG:
3641 /* fall through */
3642 case USB_PERIPHERAL:
3643 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3644 if (IS_ERR(gadget_pdev)) {
3645 retval = PTR_ERR(gadget_pdev);
3646 break;
3647 }
3648 if (mode == USB_PERIPHERAL)
3649 break;
3650 /* fall through */
3651 case USB_HOST:
3652 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3653 if (IS_ERR(host_pdev)) {
3654 retval = PTR_ERR(host_pdev);
3655 if (mode == USB_OTG)
3656 platform_device_unregister(gadget_pdev);
3657 }
3658 break;
3659 default:
3660 break;
3661 }
3662
3663 return retval;
3664}
3665
David Keitel272ce522012-08-17 16:25:24 -07003666static int msm_otg_register_power_supply(struct platform_device *pdev,
3667 struct msm_otg *motg)
3668{
3669 int ret;
3670
3671 ret = power_supply_register(&pdev->dev, &motg->usb_psy);
3672 if (ret < 0) {
3673 dev_err(motg->phy.dev,
3674 "%s:power_supply_register usb failed\n",
3675 __func__);
3676 return ret;
3677 }
3678
3679 legacy_power_supply = false;
3680 return 0;
3681}
3682
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303683struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3684{
3685 struct device_node *node = pdev->dev.of_node;
3686 struct msm_otg_platform_data *pdata;
3687 int len = 0;
3688
3689 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3690 if (!pdata) {
3691 pr_err("unable to allocate platform data\n");
3692 return NULL;
3693 }
3694 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3695 if (len) {
3696 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3697 if (!pdata->phy_init_seq)
3698 return NULL;
3699 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3700 pdata->phy_init_seq,
3701 len/sizeof(*pdata->phy_init_seq));
3702 }
3703 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3704 &pdata->power_budget);
3705 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3706 &pdata->mode);
3707 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3708 &pdata->otg_control);
3709 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3710 &pdata->default_mode);
3711 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3712 &pdata->phy_type);
3713 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3714 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303715 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3716 "qcom,hsusb-otg-disable-reset");
Manu Gautam0ddbd922012-09-21 17:17:38 +05303717 pdata->pnoc_errata_fix = of_property_read_bool(node,
3718 "qcom,hsusb-otg-pnoc-errata-fix");
Manu Gautambd53fba2012-07-31 16:13:06 +05303719
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303720 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303721}
3722
3723static int __init msm_otg_probe(struct platform_device *pdev)
3724{
Manu Gautamf8c45642012-08-10 10:20:56 -07003725 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303726 struct resource *res;
3727 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003728 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303729 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303730
3731 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303732
3733 if (pdev->dev.of_node) {
3734 dev_dbg(&pdev->dev, "device tree enabled\n");
3735 pdata = msm_otg_dt_to_pdata(pdev);
3736 if (!pdata)
3737 return -ENOMEM;
Manu Gautam2e8ac102012-08-31 11:41:16 -07003738
3739 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
3740 if (!pdata->bus_scale_table)
3741 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
3742
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303743 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3744 if (ret) {
3745 dev_err(&pdev->dev, "devices setup failed\n");
3746 return ret;
3747 }
3748 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303749 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3750 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303751 } else {
3752 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303753 }
3754
3755 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3756 if (!motg) {
3757 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3758 return -ENOMEM;
3759 }
3760
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003761 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3762 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003763 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3764 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303765 goto free_motg;
3766 }
3767
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003768 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303769 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003770 phy = &motg->phy;
3771 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303772
3773 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303774 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3775 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3776 * PMIC can detect ACA ID_GND and generate an interrupt.
3777 */
3778 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3779 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3780 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003781 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303782 }
3783
Ofir Cohen4da266f2012-01-03 10:19:29 +02003784 /* initialize reset counter */
3785 motg->reset_counter = 0;
3786
Amit Blay02eff132011-09-21 16:46:24 +03003787 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003788 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003789 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003790 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303791
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303792 /*
3793 * Targets on which link uses asynchronous reset methodology,
3794 * free running clock is not required during the reset.
3795 */
Manu Gautam5143b252012-01-05 19:25:23 -08003796 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303797 if (IS_ERR(motg->clk))
3798 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3799 else
3800 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303801
3802 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003803 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303804 * CORE CLK must be running at >55Mhz for correct HSUSB
3805 * operation and USB core cannot tolerate frequency changes on
3806 * CORE CLK. For such USB cores, vote for maximum clk frequency
3807 * on pclk source
3808 */
Manu Gautam5143b252012-01-05 19:25:23 -08003809 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3810 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303811 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003812 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303813 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003814 goto put_clk;
3815 }
3816 clk_set_rate(motg->core_clk, INT_MAX);
3817
3818 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3819 if (IS_ERR(motg->pclk)) {
3820 dev_err(&pdev->dev, "failed to get iface_clk\n");
3821 ret = PTR_ERR(motg->pclk);
3822 goto put_core_clk;
3823 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303824
3825 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3826 if (!res) {
3827 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3828 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003829 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303830 }
3831
3832 motg->regs = ioremap(res->start, resource_size(res));
3833 if (!motg->regs) {
3834 dev_err(&pdev->dev, "ioremap failed\n");
3835 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003836 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303837 }
3838 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3839
3840 motg->irq = platform_get_irq(pdev, 0);
3841 if (!motg->irq) {
3842 dev_err(&pdev->dev, "platform_get_irq failed\n");
3843 ret = -ENODEV;
3844 goto free_regs;
3845 }
3846
Manu Gautamf8c45642012-08-10 10:20:56 -07003847 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3848 if (motg->async_irq < 0) {
3849 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3850 motg->async_irq = 0;
3851 }
3852
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003853 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003854 if (IS_ERR(motg->xo_handle)) {
3855 dev_err(&pdev->dev, "%s not able to get the handle "
3856 "to vote for TCXO D0 buffer\n", __func__);
3857 ret = PTR_ERR(motg->xo_handle);
3858 goto free_regs;
3859 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303860
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003861 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003862 if (ret) {
3863 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3864 "D0 buffer%d\n", __func__, ret);
3865 goto free_xo_handle;
3866 }
3867
Manu Gautam28b1bac2012-01-30 16:43:06 +05303868 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303869
Mayank Rana248698c2012-04-19 00:03:16 +05303870 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003871 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303872 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003873 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303874 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003875 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003876 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303877 goto devote_xo_handle;
3878 }
3879 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303880 }
3881
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003882 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303883 if (ret) {
3884 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303885 goto devote_xo_handle;
3886 }
3887
3888 ret = regulator_enable(hsusb_vddcx);
3889 if (ret) {
3890 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3891 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303892 }
3893
3894 ret = msm_hsusb_ldo_init(motg, 1);
3895 if (ret) {
3896 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303897 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303898 }
3899
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303900 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303901 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3902 "mhl_usb_hs_switch");
3903 if (IS_ERR(mhl_usb_hs_switch)) {
3904 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003905 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303906 goto free_ldo_init;
3907 }
3908 }
3909
Amit Blay81801aa2012-09-19 12:08:12 +02003910 ret = msm_hsusb_ldo_enable(motg, USB_PHY_REG_ON);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303911 if (ret) {
3912 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003913 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303914 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303915 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303916
Manu Gautam0ddbd922012-09-21 17:17:38 +05303917 /* Check if USB mem_type change is needed to workaround PNOC hw issue */
3918 msm_otg_pnoc_errata_fix(motg);
3919
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303920 writel(0, USB_USBINTR);
3921 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003922 /* Ensure that above STOREs are completed before enabling interrupts */
3923 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303924
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303925 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3926 if (ret)
3927 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003928 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303929 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303930 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303931 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303932 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Amit Blayd0fe07b2012-09-05 16:42:09 +03003933 INIT_DELAYED_WORK(&motg->suspend_work, msm_otg_suspend_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303934 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3935 (unsigned long) motg);
Pavankumar Kondeti458d8792012-09-28 14:45:18 +05303936 setup_timer(&motg->chg_check_timer, msm_otg_chg_check_timer_func,
3937 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303938 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3939 "msm_otg", motg);
3940 if (ret) {
3941 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003942 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303943 }
3944
Manu Gautamf8c45642012-08-10 10:20:56 -07003945 if (motg->async_irq) {
Vijayavardhan Vennapusa9ab6f972012-10-11 19:18:55 +05303946 ret = request_irq(motg->async_irq, msm_otg_irq,
3947 IRQF_TRIGGER_RISING, "msm_otg", motg);
Manu Gautamf8c45642012-08-10 10:20:56 -07003948 if (ret) {
3949 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3950 goto free_irq;
3951 }
3952 disable_irq(motg->async_irq);
3953 }
3954
Jack Pham87f202f2012-08-06 00:24:22 -07003955 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3956 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3957
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003958 phy->init = msm_otg_reset;
3959 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003960 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303961
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003962 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303963
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003964 phy->otg->phy = &motg->phy;
3965 phy->otg->set_host = msm_otg_set_host;
3966 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003967 phy->otg->start_hnp = msm_otg_start_hnp;
3968 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003969
3970 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303971 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003972 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003973 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303974 }
3975
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303976 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303977 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003978 if (motg->pdata->pmic_id_irq) {
3979 ret = request_irq(motg->pdata->pmic_id_irq,
3980 msm_pmic_id_irq,
3981 IRQF_TRIGGER_RISING |
3982 IRQF_TRIGGER_FALLING,
3983 "msm_otg", motg);
3984 if (ret) {
3985 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003986 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003987 }
3988 } else {
3989 ret = -ENODEV;
3990 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003991 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003992 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303993 }
3994
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303995 msm_hsusb_mhl_switch_enable(motg, 1);
3996
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303997 platform_set_drvdata(pdev, motg);
3998 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003999 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304000
Anji jonnalad270e2d2011-08-09 11:28:32 +05304001 ret = msm_otg_debugfs_init(motg);
4002 if (ret)
4003 dev_dbg(&pdev->dev, "mode debugfs file is"
4004 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304005
Amit Blay58b31472011-11-18 09:39:39 +02004006 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
4007 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05304008 (!(motg->pdata->mode == USB_OTG) ||
4009 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02004010 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05304011 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004012
Amit Blay58b31472011-11-18 09:39:39 +02004013 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
Amit Blay81801aa2012-09-19 12:08:12 +02004014 motg->caps = ALLOW_PHY_RETENTION |
4015 ALLOW_PHY_REGULATORS_LPM;
Amit Blay58b31472011-11-18 09:39:39 +02004016 }
4017
Amit Blay6fa647a2012-05-24 14:12:08 +03004018 if (motg->pdata->enable_lpm_on_dev_suspend)
4019 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
4020
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004021 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304022 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07004023 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304024
Amit Blayd6f38282012-10-29 13:13:46 +02004025 if (motg->pdata->delay_lpm_on_disconnect) {
4026 pm_runtime_set_autosuspend_delay(&pdev->dev,
4027 lpm_disconnect_thresh);
4028 pm_runtime_use_autosuspend(&pdev->dev);
4029 }
4030
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304031 if (motg->pdata->bus_scale_table) {
4032 motg->bus_perf_client =
4033 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
4034 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004035 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304036 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05304037 else
4038 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304039 }
4040
David Keitel272ce522012-08-17 16:25:24 -07004041 motg->usb_psy.name = "usb";
4042 motg->usb_psy.type = POWER_SUPPLY_TYPE_USB;
4043 motg->usb_psy.supplied_to = otg_pm_power_supplied_to;
4044 motg->usb_psy.num_supplicants = ARRAY_SIZE(otg_pm_power_supplied_to);
4045 motg->usb_psy.properties = otg_pm_power_props_usb;
4046 motg->usb_psy.num_properties = ARRAY_SIZE(otg_pm_power_props_usb);
4047 motg->usb_psy.get_property = otg_power_get_property_usb;
4048 motg->usb_psy.set_property = otg_power_set_property_usb;
4049
Jack Pham0c695282012-10-19 18:13:03 -07004050 if (!pm8921_charger_register_vbus_sn(NULL)) {
David Keitel272ce522012-08-17 16:25:24 -07004051 /* if pm8921 use legacy implementation */
Jack Pham0c695282012-10-19 18:13:03 -07004052 dev_dbg(motg->phy.dev, "%s: legacy support\n", __func__);
4053 legacy_power_supply = true;
4054 } else {
4055 /* otherwise register our own power supply */
4056 if (!msm_otg_register_power_supply(pdev, motg))
4057 psy = &motg->usb_psy;
David Keitel272ce522012-08-17 16:25:24 -07004058 }
4059
Jack Pham0c695282012-10-19 18:13:03 -07004060 if (legacy_power_supply && pdata->otg_control == OTG_PMIC_CONTROL)
4061 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
4062
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304063 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004064
Steve Mucklef132c6c2012-06-06 18:30:57 -07004065remove_phy:
4066 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07004067free_async_irq:
4068 if (motg->async_irq)
4069 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304070free_irq:
4071 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004072destroy_wlock:
4073 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05304074 clk_disable_unprepare(motg->core_clk);
Amit Blay81801aa2012-09-19 12:08:12 +02004075 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004076free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05304077 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304078free_hsusb_vddcx:
4079 regulator_disable(hsusb_vddcx);
4080free_config_vddcx:
4081 regulator_set_voltage(hsusb_vddcx,
4082 vdd_val[motg->vdd_type][VDD_NONE],
4083 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08004084devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05304085 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004086 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08004087free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004088 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304089free_regs:
4090 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08004091put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304092 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304093put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08004094 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304095put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304096 if (!IS_ERR(motg->clk))
4097 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03004098 if (!IS_ERR(motg->phy_reset_clk))
4099 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004100free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004101 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05304102free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304103 kfree(motg);
4104 return ret;
4105}
4106
4107static int __devexit msm_otg_remove(struct platform_device *pdev)
4108{
4109 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004110 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304111 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304112
4113 if (otg->host || otg->gadget)
4114 return -EBUSY;
4115
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304116 if (pdev->dev.of_node)
4117 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004118 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
4119 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05304120 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304121 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05304122 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05304123 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Amit Blayd0fe07b2012-09-05 16:42:09 +03004124 cancel_delayed_work_sync(&motg->suspend_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304125 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304126
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304127 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304128
4129 device_init_wakeup(&pdev->dev, 0);
4130 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004131 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304132
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05304133 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004134 if (motg->pdata->pmic_id_irq)
4135 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004136 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304137 free_irq(motg->irq, motg);
4138
Jack Pham87f202f2012-08-06 00:24:22 -07004139 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
4140 motg->pdata->mpm_otgsessvld_int)
4141 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
4142
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304143 /*
4144 * Put PHY in low power mode.
4145 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07004146 ulpi_read(otg->phy, 0x14);
4147 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304148
4149 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
4150 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
4151 if (readl(USB_PORTSC) & PORTSC_PHCD)
4152 break;
4153 udelay(1);
4154 cnt++;
4155 }
4156 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07004157 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304158
Manu Gautam28b1bac2012-01-30 16:43:06 +05304159 clk_disable_unprepare(motg->pclk);
4160 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08004161 msm_xo_put(motg->xo_handle);
Amit Blay81801aa2012-09-19 12:08:12 +02004162 msm_hsusb_ldo_enable(motg, USB_PHY_REG_OFF);
Anji jonnala11aa5c42011-05-04 10:19:48 +05304163 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05304164 regulator_disable(hsusb_vddcx);
4165 regulator_set_voltage(hsusb_vddcx,
4166 vdd_val[motg->vdd_type][VDD_NONE],
4167 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304168
4169 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304170 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304171
Amit Blay02eff132011-09-21 16:46:24 +03004172 if (!IS_ERR(motg->phy_reset_clk))
4173 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304174 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05304175 if (!IS_ERR(motg->clk))
4176 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08004177 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304178
Manu Gautamcd82e9d2011-12-20 14:17:28 +05304179 if (motg->bus_perf_client)
4180 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304181
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02004182 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304183 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304184 return 0;
4185}
4186
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304187#ifdef CONFIG_PM_RUNTIME
4188static int msm_otg_runtime_idle(struct device *dev)
4189{
4190 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07004191 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304192
4193 dev_dbg(dev, "OTG runtime idle\n");
4194
Steve Mucklef132c6c2012-06-06 18:30:57 -07004195 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304196 return -EAGAIN;
4197 else
4198 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304199}
4200
4201static int msm_otg_runtime_suspend(struct device *dev)
4202{
4203 struct msm_otg *motg = dev_get_drvdata(dev);
4204
4205 dev_dbg(dev, "OTG runtime suspend\n");
4206 return msm_otg_suspend(motg);
4207}
4208
4209static int msm_otg_runtime_resume(struct device *dev)
4210{
4211 struct msm_otg *motg = dev_get_drvdata(dev);
4212
4213 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05304214 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304215 return msm_otg_resume(motg);
4216}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304217#endif
4218
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304219#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304220static int msm_otg_pm_suspend(struct device *dev)
4221{
Jack Pham5ca279b2012-05-14 18:42:54 -07004222 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304223 struct msm_otg *motg = dev_get_drvdata(dev);
4224
4225 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004226
4227 atomic_set(&motg->pm_suspended, 1);
4228 ret = msm_otg_suspend(motg);
4229 if (ret)
4230 atomic_set(&motg->pm_suspended, 0);
4231
4232 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304233}
4234
4235static int msm_otg_pm_resume(struct device *dev)
4236{
Jack Pham5ca279b2012-05-14 18:42:54 -07004237 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304238 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304239
4240 dev_dbg(dev, "OTG PM resume\n");
4241
Jack Pham5ca279b2012-05-14 18:42:54 -07004242 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07004243 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004244 pm_runtime_get_noresume(dev);
4245 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304246
Jack Pham5ca279b2012-05-14 18:42:54 -07004247 /* Update runtime PM status */
4248 pm_runtime_disable(dev);
4249 pm_runtime_set_active(dev);
4250 pm_runtime_enable(dev);
4251
Jack Phamc7edb172012-08-13 15:32:39 -07004252 if (motg->sm_work_pending) {
4253 motg->sm_work_pending = false;
4254 queue_work(system_nrt_wq, &motg->sm_work);
4255 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004256 }
4257
4258 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304259}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304260#endif
4261
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304262#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304263static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304264 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4265 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4266 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304267};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304268#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304269
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304270static struct of_device_id msm_otg_dt_match[] = {
4271 { .compatible = "qcom,hsusb-otg",
4272 },
4273 {}
4274};
4275
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304276static struct platform_driver msm_otg_driver = {
4277 .remove = __devexit_p(msm_otg_remove),
4278 .driver = {
4279 .name = DRIVER_NAME,
4280 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304281#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304282 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304283#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304284 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304285 },
4286};
4287
4288static int __init msm_otg_init(void)
4289{
4290 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4291}
4292
4293static void __exit msm_otg_exit(void)
4294{
4295 platform_driver_unregister(&msm_otg_driver);
4296}
4297
4298module_init(msm_otg_init);
4299module_exit(msm_otg_exit);
4300
4301MODULE_LICENSE("GPL v2");
4302MODULE_DESCRIPTION("MSM USB transceiver driver");