blob: f1fef85c8170e19351870c7528b0a81c8b436275 [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>
Amit Blay0f7edf72012-01-15 10:11:27 +020042#include <linux/power_supply.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053043#include <linux/mhl_8334.h>
jh.koo78df86f2012-09-04 20:13:19 +090044#include <linux/slimport.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045
Devin Kimd10f03c2012-06-25 17:05:13 -070046#include <asm/mach-types.h>
47
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053048#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070049#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080050#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053051#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053052#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053053
54#define MSM_USB_BASE (motg->regs)
55#define DRIVER_NAME "msm_otg"
56
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053057#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053058#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053059#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
60#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
61#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
62#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
63
64#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
65#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
66#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
67#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
68
Mayank Rana248698c2012-04-19 00:03:16 +053069#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080070#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053071#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
72
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053073static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053075static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053076static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053077static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078
Anji jonnala11aa5c42011-05-04 10:19:48 +053079static struct regulator *hsusb_3p3;
80static struct regulator *hsusb_1p8;
81static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053082static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +053083static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053084static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053085
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053086static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053087static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +053088{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053089#ifdef CONFIG_USB_MSM_ACA
90 return true;
91#else
92 return debug_aca_enabled;
93#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +053094}
95
Mayank Rana248698c2012-04-19 00:03:16 +053096static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
97 { /* VDD_CX CORNER Voting */
98 [VDD_NONE] = RPM_VREG_CORNER_NONE,
99 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
100 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
101 },
102 { /* VDD_CX Voltage Voting */
103 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
104 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
105 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
106 },
107};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530108
109static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
110{
111 int rc = 0;
112
113 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700114 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530115 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200116 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530117 return PTR_ERR(hsusb_3p3);
118 }
119
120 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
121 USB_PHY_3P3_VOL_MAX);
122 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700123 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530125 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700127 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200129 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530130 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 }
133 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
134 USB_PHY_1P8_VOL_MAX);
135 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700136 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138 goto put_1p8;
139 }
140
141 return 0;
142 }
143
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146put_3p3_lpm:
147 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148 return rc;
149}
150
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530151static int msm_hsusb_config_vddcx(int high)
152{
Mayank Rana248698c2012-04-19 00:03:16 +0530153 struct msm_otg *motg = the_msm_otg;
154 enum usb_vdd_type vdd_type = motg->vdd_type;
155 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530156 int min_vol;
157 int ret;
158
Mayank Rana248698c2012-04-19 00:03:16 +0530159 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530160 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
161 if (ret) {
162 pr_err("%s: unable to set the voltage for regulator "
163 "HSUSB_VDDCX\n", __func__);
164 return ret;
165 }
166
167 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
168
169 return ret;
170}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530173{
174 int ret = 0;
175
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530176 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530177 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
178 return -ENODEV;
179 }
180
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530181 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530182 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
183 return -ENODEV;
184 }
185
186 if (on) {
187 ret = regulator_set_optimum_mode(hsusb_1p8,
188 USB_PHY_1P8_HPM_LOAD);
189 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530191 "HSUSB_1p8\n", __func__);
192 return ret;
193 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195 ret = regulator_enable(hsusb_1p8);
196 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700197 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 __func__);
199 regulator_set_optimum_mode(hsusb_1p8, 0);
200 return ret;
201 }
202
Anji jonnala11aa5c42011-05-04 10:19:48 +0530203 ret = regulator_set_optimum_mode(hsusb_3p3,
204 USB_PHY_3P3_HPM_LOAD);
205 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 regulator_set_optimum_mode(hsusb_1p8, 0);
209 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530210 return ret;
211 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212
213 ret = regulator_enable(hsusb_3p3);
214 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700215 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 __func__);
217 regulator_set_optimum_mode(hsusb_3p3, 0);
218 regulator_set_optimum_mode(hsusb_1p8, 0);
219 regulator_disable(hsusb_1p8);
220 return ret;
221 }
222
Anji jonnala11aa5c42011-05-04 10:19:48 +0530223 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 ret = regulator_disable(hsusb_1p8);
225 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700226 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 __func__);
228 return ret;
229 }
230
231 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530234 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235
236 ret = regulator_disable(hsusb_3p3);
237 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700238 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 __func__);
240 return ret;
241 }
242 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530243 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530245 "HSUSB_3p3\n", __func__);
246 }
247
248 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
249 return ret < 0 ? ret : 0;
250}
251
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530252static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
253{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530254 struct msm_otg_platform_data *pdata = motg->pdata;
255
256 if (!pdata->mhl_enable)
257 return;
258
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530259 if (!mhl_usb_hs_switch) {
260 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530261 return;
262 }
263
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530264 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530265 if (regulator_enable(mhl_usb_hs_switch))
266 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530267 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530268 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530269 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530270}
271
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200272static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530273{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200274 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530275 int cnt = 0;
276
277 /* initiate read operation */
278 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
279 USB_ULPI_VIEWPORT);
280
281 /* wait for completion */
282 while (cnt < ULPI_IO_TIMEOUT_USEC) {
283 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
284 break;
285 udelay(1);
286 cnt++;
287 }
288
289 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200290 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530291 readl(USB_ULPI_VIEWPORT));
292 return -ETIMEDOUT;
293 }
294 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
295}
296
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200297static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530298{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200299 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530300 int cnt = 0;
301
302 /* initiate write operation */
303 writel(ULPI_RUN | ULPI_WRITE |
304 ULPI_ADDR(reg) | ULPI_DATA(val),
305 USB_ULPI_VIEWPORT);
306
307 /* wait for completion */
308 while (cnt < ULPI_IO_TIMEOUT_USEC) {
309 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
310 break;
311 udelay(1);
312 cnt++;
313 }
314
315 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200316 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530317 return -ETIMEDOUT;
318 }
319 return 0;
320}
321
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200322static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530323 .read = ulpi_read,
324 .write = ulpi_write,
325};
326
327static void ulpi_init(struct msm_otg *motg)
328{
329 struct msm_otg_platform_data *pdata = motg->pdata;
330 int *seq = pdata->phy_init_seq;
331
332 if (!seq)
333 return;
334
335 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200336 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530337 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200338 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530339 seq += 2;
340 }
341}
342
343static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
344{
345 int ret;
346
347 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530348 if (!IS_ERR(motg->clk)) {
349 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
350 } else {
351 /* Using asynchronous block reset to the hardware */
352 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
353 clk_disable_unprepare(motg->pclk);
354 clk_disable_unprepare(motg->core_clk);
355 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
356 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530357 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200358 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530359 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530360 if (!IS_ERR(motg->clk)) {
361 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
362 } else {
363 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
364 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
365 ndelay(200);
366 clk_prepare_enable(motg->core_clk);
367 clk_prepare_enable(motg->pclk);
368 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530369 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200370 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530371 }
372 return ret;
373}
374
375static int msm_otg_phy_clk_reset(struct msm_otg *motg)
376{
377 int ret;
378
Amit Blay02eff132011-09-21 16:46:24 +0300379 if (IS_ERR(motg->phy_reset_clk))
380 return 0;
381
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530382 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
383 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200384 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530385 return ret;
386 }
387 usleep_range(10000, 12000);
388 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
389 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200390 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530391 return ret;
392}
393
394static int msm_otg_phy_reset(struct msm_otg *motg)
395{
396 u32 val;
397 int ret;
398 int retries;
399
400 ret = msm_otg_link_clk_reset(motg, 1);
401 if (ret)
402 return ret;
403 ret = msm_otg_phy_clk_reset(motg);
404 if (ret)
405 return ret;
406 ret = msm_otg_link_clk_reset(motg, 0);
407 if (ret)
408 return ret;
409
410 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
411 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
412
413 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200414 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530415 ULPI_CLR(ULPI_FUNC_CTRL));
416 if (!ret)
417 break;
418 ret = msm_otg_phy_clk_reset(motg);
419 if (ret)
420 return ret;
421 }
422 if (!retries)
423 return -ETIMEDOUT;
424
425 /* This reset calibrates the phy, if the above write succeeded */
426 ret = msm_otg_phy_clk_reset(motg);
427 if (ret)
428 return ret;
429
430 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200431 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 if (ret != -ETIMEDOUT)
433 break;
434 ret = msm_otg_phy_clk_reset(motg);
435 if (ret)
436 return ret;
437 }
438 if (!retries)
439 return -ETIMEDOUT;
440
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200441 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530442 return 0;
443}
444
445#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530446static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530447{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530448 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530449
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530450 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530451 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530452 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530453 break;
454 udelay(1);
455 cnt++;
456 }
457 if (cnt >= LINK_RESET_TIMEOUT_USEC)
458 return -ETIMEDOUT;
459
460 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530461 writel_relaxed(0x80000000, USB_PORTSC);
462 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530463 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530464
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530465 return 0;
466}
467
Steve Mucklef132c6c2012-06-06 18:30:57 -0700468static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530469{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700470 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530471 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530472 int ret;
473 u32 val = 0;
474 u32 ulpi_val = 0;
475
Ofir Cohen4da266f2012-01-03 10:19:29 +0200476 /*
477 * USB PHY and Link reset also reset the USB BAM.
478 * Thus perform reset operation only once to avoid
479 * USB BAM reset on other cases e.g. USB cable disconnections.
480 */
481 if (pdata->disable_reset_on_disconnect) {
482 if (motg->reset_counter)
483 return 0;
484 else
485 motg->reset_counter++;
486 }
487
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530488 if (!IS_ERR(motg->clk))
489 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530490 ret = msm_otg_phy_reset(motg);
491 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700492 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530493 return ret;
494 }
495
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530496 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530497 ret = msm_otg_link_reset(motg);
498 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700499 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530500 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530501 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530502 msleep(100);
503
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530504 ulpi_init(motg);
505
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 /* Ensure that RESET operation is completed before turning off clock */
507 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530508
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530509 if (!IS_ERR(motg->clk))
510 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530511
512 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530513 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530514 if (pdata->mode == USB_OTG) {
515 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
516 val |= OTGSC_IDIE | OTGSC_BSVIE;
517 } else if (pdata->mode == USB_PERIPHERAL) {
518 ulpi_val = ULPI_INT_SESS_VALID;
519 val |= OTGSC_BSVIE;
520 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530521 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200522 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
523 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530524 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700525 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530526 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530527 /* Enable PMIC pull-up */
528 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530529 }
530
531 return 0;
532}
533
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530534static const char *timer_string(int bit)
535{
536 switch (bit) {
537 case A_WAIT_VRISE: return "a_wait_vrise";
538 case A_WAIT_VFALL: return "a_wait_vfall";
539 case B_SRP_FAIL: return "b_srp_fail";
540 case A_WAIT_BCON: return "a_wait_bcon";
541 case A_AIDL_BDIS: return "a_aidl_bdis";
542 case A_BIDL_ADIS: return "a_bidl_adis";
543 case B_ASE0_BRST: return "b_ase0_brst";
544 case A_TST_MAINT: return "a_tst_maint";
545 case B_TST_SRP: return "b_tst_srp";
546 case B_TST_CONFIG: return "b_tst_config";
547 default: return "UNDEFINED";
548 }
549}
550
551static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
552{
553 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
554
555 switch (motg->active_tmout) {
556 case A_WAIT_VRISE:
557 /* TODO: use vbus_vld interrupt */
558 set_bit(A_VBUS_VLD, &motg->inputs);
559 break;
560 case A_TST_MAINT:
561 /* OTG PET: End session after TA_TST_MAINT */
562 set_bit(A_BUS_DROP, &motg->inputs);
563 break;
564 case B_TST_SRP:
565 /*
566 * OTG PET: Initiate SRP after TB_TST_SRP of
567 * previous session end.
568 */
569 set_bit(B_BUS_REQ, &motg->inputs);
570 break;
571 case B_TST_CONFIG:
572 clear_bit(A_CONN, &motg->inputs);
573 break;
574 default:
575 set_bit(motg->active_tmout, &motg->tmouts);
576 }
577
578 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
579 queue_work(system_nrt_wq, &motg->sm_work);
580 return HRTIMER_NORESTART;
581}
582
583static void msm_otg_del_timer(struct msm_otg *motg)
584{
585 int bit = motg->active_tmout;
586
587 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
588 div_s64(ktime_to_us(hrtimer_get_remaining(
589 &motg->timer)), 1000));
590 hrtimer_cancel(&motg->timer);
591 clear_bit(bit, &motg->tmouts);
592}
593
594static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
595{
596 clear_bit(bit, &motg->tmouts);
597 motg->active_tmout = bit;
598 pr_debug("starting %s timer\n", timer_string(bit));
599 hrtimer_start(&motg->timer,
600 ktime_set(time / 1000, (time % 1000) * 1000000),
601 HRTIMER_MODE_REL);
602}
603
604static void msm_otg_init_timer(struct msm_otg *motg)
605{
606 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
607 motg->timer.function = msm_otg_timer_func;
608}
609
Steve Mucklef132c6c2012-06-06 18:30:57 -0700610static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530611{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700612 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530613
Steve Mucklef132c6c2012-06-06 18:30:57 -0700614 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530615 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700616 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530617 return -EINVAL;
618 }
619
620 pr_debug("A-Host: HNP initiated\n");
621 clear_bit(A_BUS_REQ, &motg->inputs);
622 queue_work(system_nrt_wq, &motg->sm_work);
623 return 0;
624}
625
Steve Mucklef132c6c2012-06-06 18:30:57 -0700626static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530627{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700628 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530629 u32 val;
630 int ret = 0;
631
Steve Mucklef132c6c2012-06-06 18:30:57 -0700632 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530633 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700634 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530635 ret = -EINVAL;
636 goto out;
637 }
638
639 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
640 pr_debug("initial conditions of SRP are not met. Try again"
641 "after some time\n");
642 ret = -EAGAIN;
643 goto out;
644 }
645
646 pr_debug("B-Device SRP started\n");
647
648 /*
649 * PHY won't pull D+ high unless it detects Vbus valid.
650 * Since by definition, SRP is only done when Vbus is not valid,
651 * software work-around needs to be used to spoof the PHY into
652 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
653 * VBUSVLDEXT register bits.
654 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700655 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530656 /*
657 * Harware auto assist data pulsing: Data pulse is given
658 * for 7msec; wait for vbus
659 */
660 val = readl_relaxed(USB_OTGSC);
661 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
662
663 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
664out:
665 return ret;
666}
667
Steve Mucklef132c6c2012-06-06 18:30:57 -0700668static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530669{
670 struct usb_hcd *hcd = bus_to_hcd(otg->host);
671 struct usb_device *rhub = otg->host->root_hub;
672
673 if (enable) {
674 pm_runtime_disable(&rhub->dev);
675 rhub->state = USB_STATE_NOTATTACHED;
676 hcd->driver->bus_suspend(hcd);
677 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
678 } else {
679 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700680 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530681 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
682 }
683}
684
Steve Mucklef132c6c2012-06-06 18:30:57 -0700685static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530686{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700687 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530688
Amit Blay6fa647a2012-05-24 14:12:08 +0300689 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530690 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530691
Jack Pham69e621d2012-06-25 18:48:07 -0700692 if (atomic_read(&motg->in_lpm) == suspend)
693 return 0;
694
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530695 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700696 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530697 case OTG_STATE_A_WAIT_BCON:
698 if (TA_WAIT_BCON > 0)
699 break;
700 /* fall through */
701 case OTG_STATE_A_HOST:
702 pr_debug("host bus suspend\n");
703 clear_bit(A_BUS_REQ, &motg->inputs);
704 queue_work(system_nrt_wq, &motg->sm_work);
705 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300706 case OTG_STATE_B_PERIPHERAL:
707 pr_debug("peripheral bus suspend\n");
708 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
709 break;
710 set_bit(A_BUS_SUSPEND, &motg->inputs);
711 queue_work(system_nrt_wq, &motg->sm_work);
712 break;
713
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530714 default:
715 break;
716 }
717 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700718 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530719 case OTG_STATE_A_SUSPEND:
720 /* Remote wakeup or resume */
721 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700722 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700723
724 /* ensure hardware is not in low power mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700725 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530726 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300727 case OTG_STATE_B_PERIPHERAL:
728 pr_debug("peripheral bus resume\n");
729 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
730 break;
731 clear_bit(A_BUS_SUSPEND, &motg->inputs);
732 queue_work(system_nrt_wq, &motg->sm_work);
733 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530734 default:
735 break;
736 }
737 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530738 return 0;
739}
740
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530741#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530742#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
743
744#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530745static int msm_otg_suspend(struct msm_otg *motg)
746{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200747 struct usb_phy *phy = &motg->phy;
748 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530749 struct msm_otg_platform_data *pdata = motg->pdata;
750 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300751 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530752 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800753 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530754 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530755
756 if (atomic_read(&motg->in_lpm))
757 return 0;
758
759 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530760 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
761 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700762 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300763 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
764 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530765 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham6ff217b2012-08-13 15:34:20 -0700766
Pavankumar Kondetib91ab712012-10-22 13:21:19 +0530767 /*
768 * Abort suspend when,
769 * 1. charging detection in progress due to cable plug-in
770 * 2. host mode activation in progress due to Micro-A cable insertion
771 */
772
773 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
774 !dcp) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham6ff217b2012-08-13 15:34:20 -0700775 enable_irq(motg->irq);
776 return -EBUSY;
777 }
778
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530779 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530780 * Chipidea 45-nm PHY suspend sequence:
781 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530782 * Interrupt Latch Register auto-clear feature is not present
783 * in all PHY versions. Latch register is clear on read type.
784 * Clear latch register to avoid spurious wakeup from
785 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530786 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530787 * PHY comparators are disabled when PHY enters into low power
788 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
789 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
790 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530791 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530792 * PLL is not turned off when PHY enters into low power mode (LPM).
793 * Disable PLL for maximum power savings.
794 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530795
796 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200797 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530798 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200799 ulpi_write(phy, 0x01, 0x30);
800 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530801 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530802
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530804 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530805 * PHY may take some time or even fail to enter into low power
806 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
807 * in failure case.
808 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530809 portsc = readl_relaxed(USB_PORTSC);
810 if (!(portsc & PORTSC_PHCD)) {
811 writel_relaxed(portsc | PORTSC_PHCD,
812 USB_PORTSC);
813 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
814 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
815 break;
816 udelay(1);
817 cnt++;
818 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530819 }
820
821 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200822 dev_err(phy->dev, "Unable to suspend PHY\n");
823 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530824 enable_irq(motg->irq);
825 return -ETIMEDOUT;
826 }
827
828 /*
829 * PHY has capability to generate interrupt asynchronously in low
830 * power mode (LPM). This interrupt is level triggered. So USB IRQ
831 * line must be disabled till async interrupt enable bit is cleared
832 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
833 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530834 *
835 * PHY retention mode is disallowed while entering to LPM with wall
836 * charger connected. But PHY is put into suspend mode. Hence
837 * enable asynchronous interrupt to detect charger disconnection when
838 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530839 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530840 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300841 if (host_bus_suspend || device_bus_suspend ||
842 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530843 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
844 else
845 cmd_val |= ULPI_STP_CTRL;
846 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530847
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530848 /*
849 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
850 * PHY retention and collapse can not happen with VDP_SRC enabled.
851 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300852 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
853 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200854 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
855 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
856 /* Enable PHY HV interrupts to wake MPM/Link */
857 phy_ctrl_val |=
858 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530859
Amit Blay58b31472011-11-18 09:39:39 +0200860 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530862 }
863
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 /* Ensure that above operation is completed before turning off clocks */
865 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300866 /* Consider clocks on workaround flag only in case of bus suspend */
867 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
868 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
869 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300870 clk_disable_unprepare(motg->pclk);
871 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300872 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300873 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530874
Anji jonnala7da3f262011-12-02 17:22:14 -0800875 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530876 if (!host_bus_suspend) {
877 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
878 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700879 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530880 "TCXO D0 buffer%d\n", __func__, ret);
881 else
882 motg->lpm_flags |= XO_SHUTDOWN;
883 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800884
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530885 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
886 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 msm_hsusb_ldo_enable(motg, 0);
888 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530889 }
890
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530891 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530893 msm_hsusb_mhl_switch_enable(motg, 0);
894 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895
Steve Mucklef132c6c2012-06-06 18:30:57 -0700896 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530897 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700898 if (motg->async_irq)
899 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 if (motg->pdata->pmic_id_irq)
901 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700902 if (pdata->otg_control == OTG_PHY_CONTROL &&
903 pdata->mpm_otgsessvld_int)
904 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530906 if (bus)
907 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
908
909 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -0700910 /* Enable ASYNC IRQ (if present) during LPM */
911 if (motg->async_irq)
912 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530913 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530915
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200916 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530917
918 return 0;
919}
920
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530921static int msm_otg_resume(struct msm_otg *motg)
922{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200923 struct usb_phy *phy = &motg->phy;
924 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700925 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530926 int cnt = 0;
927 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200928 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800929 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530930
931 if (!atomic_read(&motg->in_lpm))
932 return 0;
933
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530935
Anji jonnala7da3f262011-12-02 17:22:14 -0800936 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530937 if (motg->lpm_flags & XO_SHUTDOWN) {
938 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
939 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700940 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530941 "TCXO D0 buffer%d\n", __func__, ret);
942 motg->lpm_flags &= ~XO_SHUTDOWN;
943 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530944
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300945 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300946 clk_prepare_enable(motg->core_clk);
947 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300948 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300949 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530950
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
952 msm_hsusb_ldo_enable(motg, 1);
953 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
954 }
955
956 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530957 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530958 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200959 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
960 phy_ctrl_val |= PHY_RETEN;
961 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
962 /* Disable PHY HV interrupts */
963 phy_ctrl_val &=
964 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
965 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530967 }
968
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530969 temp = readl(USB_USBCMD);
970 temp &= ~ASYNC_INTR_CTRL;
971 temp &= ~ULPI_STP_CTRL;
972 writel(temp, USB_USBCMD);
973
974 /*
975 * PHY comes out of low power mode (LPM) in case of wakeup
976 * from asynchronous interrupt.
977 */
978 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
979 goto skip_phy_resume;
980
981 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
982 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
983 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
984 break;
985 udelay(1);
986 cnt++;
987 }
988
989 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
990 /*
991 * This is a fatal error. Reset the link and
992 * PHY. USB state can not be restored. Re-insertion
993 * of USB cable is the only way to get USB working.
994 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200995 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530996 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200997 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530998 }
999
1000skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001001 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301002 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001003 if (motg->async_irq)
1004 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005 if (motg->pdata->pmic_id_irq)
1006 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001007 if (pdata->otg_control == OTG_PHY_CONTROL &&
1008 pdata->mpm_otgsessvld_int)
1009 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301011 if (bus)
1012 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1013
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301014 atomic_set(&motg->in_lpm, 0);
1015
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301016 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001017 /* Match the disable_irq call from ISR */
1018 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301019 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301020 }
1021
Manu Gautamf8c45642012-08-10 10:20:56 -07001022 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1023 if (motg->async_irq)
1024 disable_irq(motg->async_irq);
1025
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001026 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301027
1028 return 0;
1029}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301030#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301031
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001032static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1033{
1034 if (!psy)
1035 goto psy_not_supported;
1036
1037 if (host_mode)
1038 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1039 else
1040 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1041
1042psy_not_supported:
1043 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1044 return -ENXIO;
1045}
1046
David Keitel081a3e22012-04-18 12:37:07 -07001047static int msm_otg_notify_chg_type(struct msm_otg *motg)
1048{
roy.parkd063ef32012-08-30 09:37:38 -07001049 int charger_type;
David Keitel081a3e22012-04-18 12:37:07 -07001050 /*
1051 * TODO
1052 * Unify OTG driver charger types and power supply charger types
1053 */
David Keitel081a3e22012-04-18 12:37:07 -07001054
1055 if (motg->chg_type == USB_SDP_CHARGER)
1056 charger_type = POWER_SUPPLY_TYPE_USB;
1057 else if (motg->chg_type == USB_CDP_CHARGER)
1058 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301059 else if (motg->chg_type == USB_DCP_CHARGER ||
1060 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001061 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1062 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1063 motg->chg_type == USB_ACA_A_CHARGER ||
1064 motg->chg_type == USB_ACA_B_CHARGER ||
1065 motg->chg_type == USB_ACA_C_CHARGER))
1066 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1067 else
1068 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1069
1070 return pm8921_set_usb_power_supply_type(charger_type);
1071}
1072
Amit Blay0f7edf72012-01-15 10:11:27 +02001073static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1074{
Amit Blay0f7edf72012-01-15 10:11:27 +02001075
Amit Blay0f7edf72012-01-15 10:11:27 +02001076 if (!psy)
1077 goto psy_not_supported;
1078
1079 if (motg->cur_power == 0 && mA > 0) {
1080 /* Enable charging */
1081 if (power_supply_set_online(psy, true))
1082 goto psy_not_supported;
1083 } else if (motg->cur_power > 0 && mA == 0) {
1084 /* Disable charging */
1085 if (power_supply_set_online(psy, false))
1086 goto psy_not_supported;
1087 return 0;
1088 }
1089 /* Set max current limit */
Devin Kim81265542012-09-01 05:06:22 -07001090 dev_info(motg->phy.dev, "current: %d -> %d (mA)\n",
1091 motg->cur_power, mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001092 if (power_supply_set_current_limit(psy, 1000*mA))
1093 goto psy_not_supported;
1094
1095 return 0;
1096
1097psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001098 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001099 return -ENXIO;
1100}
1101
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301102static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1103{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001104 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301105
1106 if (g && g->is_a_peripheral)
1107 return;
1108
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301109 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1110 motg->chg_type == USB_ACA_A_CHARGER ||
1111 motg->chg_type == USB_ACA_B_CHARGER ||
1112 motg->chg_type == USB_ACA_C_CHARGER) &&
1113 mA > IDEV_ACA_CHG_LIMIT)
1114 mA = IDEV_ACA_CHG_LIMIT;
1115
David Keitel081a3e22012-04-18 12:37:07 -07001116 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001117 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001118 "Failed notifying %d charger type to PMIC\n",
1119 motg->chg_type);
1120
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301121 if (motg->cur_power == mA)
1122 return;
1123
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001124 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001125
Devin Kim2073afb2012-09-05 13:01:51 -07001126 pm8921_charger_vbus_draw(mA);
1127 msm_otg_notify_power_supply(motg, mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001128
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301129 motg->cur_power = mA;
1130}
1131
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001132static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301133{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001134 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301135
1136 /*
1137 * Gadget driver uses set_power method to notify about the
1138 * available current based on suspend/configured states.
1139 *
1140 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1141 * states when CDP/ACA is connected.
1142 */
1143 if (motg->chg_type == USB_SDP_CHARGER)
1144 msm_otg_notify_charger(motg, mA);
1145
1146 return 0;
1147}
1148
Steve Mucklef132c6c2012-06-06 18:30:57 -07001149static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301150{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001151 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301152 struct msm_otg_platform_data *pdata = motg->pdata;
1153 struct usb_hcd *hcd;
1154
1155 if (!otg->host)
1156 return;
1157
1158 hcd = bus_to_hcd(otg->host);
1159
1160 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001161 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301162
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301163 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001164 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301165 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1166
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301167 /*
1168 * Some boards have a switch cotrolled by gpio
1169 * to enable/disable internal HUB. Enable internal
1170 * HUB before kicking the host.
1171 */
1172 if (pdata->setup_gpio)
1173 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301174 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301175 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001176 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301177
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301178 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301179 /* HCD core reset all bits of PORTSC. select ULPI phy */
1180 writel_relaxed(0x80000000, USB_PORTSC);
1181
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301182 if (pdata->setup_gpio)
1183 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301184
1185 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001186 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301187 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301188 }
1189}
1190
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001191static int msm_otg_usbdev_notify(struct notifier_block *self,
1192 unsigned long action, void *priv)
1193{
1194 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001195 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301196 struct usb_device *udev = priv;
1197
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301198 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1199 goto out;
1200
Steve Mucklef132c6c2012-06-06 18:30:57 -07001201 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301202 goto out;
1203 /*
1204 * Interested in devices connected directly to the root hub.
1205 * ACA dock can supply IDEV_CHG irrespective devices connected
1206 * on the accessory port.
1207 */
1208 if (!udev->parent || udev->parent->parent ||
1209 motg->chg_type == USB_ACA_DOCK_CHARGER)
1210 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211
1212 switch (action) {
1213 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301214 if (aca_enabled())
1215 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001216 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301217 pr_debug("B_CONN set\n");
1218 set_bit(B_CONN, &motg->inputs);
1219 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001220 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301221 /*
1222 * OTG PET: A-device must end session within
1223 * 10 sec after PET enumeration.
1224 */
1225 if (udev->quirks & USB_QUIRK_OTG_PET)
1226 msm_otg_start_timer(motg, TA_TST_MAINT,
1227 A_TST_MAINT);
1228 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301229 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 if (udev->actconfig)
1232 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1233 else
1234 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001235 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301236 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301237 break;
1238 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001239 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1240 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301241 pr_debug("B_CONN clear\n");
1242 clear_bit(B_CONN, &motg->inputs);
1243 /*
1244 * OTG PET: A-device must end session after
1245 * PET disconnection if it is enumerated
1246 * with bcdDevice[0] = 1. USB core sets
1247 * bus->otg_vbus_off for us. clear it here.
1248 */
1249 if (udev->bus->otg_vbus_off) {
1250 udev->bus->otg_vbus_off = 0;
1251 set_bit(A_BUS_DROP, &motg->inputs);
1252 }
1253 queue_work(system_nrt_wq, &motg->sm_work);
1254 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255 default:
1256 break;
1257 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301258 if (test_bit(ID_A, &motg->inputs))
1259 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1260 motg->mA_port);
1261out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001262 return NOTIFY_OK;
1263}
1264
Mayank Ranae3926882011-12-26 09:47:54 +05301265static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1266{
1267 int ret;
1268 static bool vbus_is_on;
1269
1270 if (vbus_is_on == on)
1271 return;
1272
1273 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301274 ret = motg->pdata->vbus_power(on);
1275 if (!ret)
1276 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301277 return;
1278 }
1279
1280 if (!vbus_otg) {
1281 pr_err("vbus_otg is NULL.");
1282 return;
1283 }
1284
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001285 /*
1286 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001287 * from usb before turning on the boost.
1288 * if exiting host mode disable the boost before enabling to draw
1289 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001290 */
Mayank Ranae3926882011-12-26 09:47:54 +05301291 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001292 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301293 ret = regulator_enable(vbus_otg);
1294 if (ret) {
1295 pr_err("unable to enable vbus_otg\n");
1296 return;
1297 }
1298 vbus_is_on = true;
1299 } else {
1300 ret = regulator_disable(vbus_otg);
1301 if (ret) {
1302 pr_err("unable to disable vbus_otg\n");
1303 return;
1304 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001305 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301306 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301307 }
1308}
1309
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001310static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301311{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001312 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301313 struct usb_hcd *hcd;
1314
1315 /*
1316 * Fail host registration if this board can support
1317 * only peripheral configuration.
1318 */
1319 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001320 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301321 return -ENODEV;
1322 }
1323
Devin Kimd10f03c2012-06-25 17:05:13 -07001324 if (!machine_is_apq8064_mako()) {
1325 if (!motg->pdata->vbus_power && host) {
1326 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1327 if (IS_ERR(vbus_otg)) {
1328 pr_err("Unable to get vbus_otg\n");
1329 return -ENODEV;
1330 }
Mayank Ranae3926882011-12-26 09:47:54 +05301331 }
1332 }
1333
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301334 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001335 if (otg->phy->state == OTG_STATE_A_HOST) {
1336 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301338 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301339 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301340 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001341 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301342 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301343 } else {
1344 otg->host = NULL;
1345 }
1346
1347 return 0;
1348 }
1349
1350 hcd = bus_to_hcd(host);
1351 hcd->power_budget = motg->pdata->power_budget;
1352
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301353#ifdef CONFIG_USB_OTG
1354 host->otg_port = 1;
1355#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001356 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1357 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301358 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001359 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301360
1361 /*
1362 * Kick the state machine work, if peripheral is not supported
1363 * or peripheral is already registered with us.
1364 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301365 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001366 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301367 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301368 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301369
1370 return 0;
1371}
1372
Steve Mucklef132c6c2012-06-06 18:30:57 -07001373static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301374{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301375 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001376 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301377 struct msm_otg_platform_data *pdata = motg->pdata;
1378
1379 if (!otg->gadget)
1380 return;
1381
1382 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001383 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301384 /*
1385 * Some boards have a switch cotrolled by gpio
1386 * to enable/disable internal HUB. Disable internal
1387 * HUB before kicking the gadget.
1388 */
1389 if (pdata->setup_gpio)
1390 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001391
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301392 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301393 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301394 ret = msm_bus_scale_client_update_request(
1395 motg->bus_perf_client, 1);
1396 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001397 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301398 "bus bandwidth %d\n", __func__, ret);
1399 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301400 usb_gadget_vbus_connect(otg->gadget);
1401 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001402 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301403 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301404 /* Configure BUS performance parameters to default */
1405 if (motg->bus_perf_client) {
1406 ret = msm_bus_scale_client_update_request(
1407 motg->bus_perf_client, 0);
1408 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001409 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301410 "for bus bw %d\n", __func__, ret);
1411 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301412 if (pdata->setup_gpio)
1413 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1414 }
1415
1416}
1417
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001418static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301419 struct usb_gadget *gadget)
1420{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001421 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301422
1423 /*
1424 * Fail peripheral registration if this board can support
1425 * only host configuration.
1426 */
1427 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001428 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301429 return -ENODEV;
1430 }
1431
1432 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001433 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1434 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301435 msm_otg_start_peripheral(otg, 0);
1436 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001437 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301438 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301439 } else {
1440 otg->gadget = NULL;
1441 }
1442
1443 return 0;
1444 }
1445 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001446 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301447
1448 /*
1449 * Kick the state machine work, if host is not supported
1450 * or host is already registered with us.
1451 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301452 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001453 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301454 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301455 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301456
1457 return 0;
1458}
1459
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301460static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1461 void (*callback)(int on))
1462{
1463 struct usb_phy *phy = &motg->phy;
1464 int ret;
1465
1466 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1467 !motg->pdata->pmic_id_irq) {
1468 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1469 return -ENODEV;
1470 }
1471
1472 if (!motg->pdata->mhl_dev_name) {
1473 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1474 return -ENODEV;
1475 }
1476
1477 if (callback)
1478 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1479 callback);
1480 else
1481 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1482
1483 if (ret)
1484 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1485 motg->pdata->mhl_dev_name, ret);
1486 else
1487 motg->mhl_enabled = true;
1488
1489 return ret;
1490}
1491
1492static void msm_otg_mhl_notify_online(int on)
1493{
1494 struct msm_otg *motg = the_msm_otg;
1495 struct usb_phy *phy = &motg->phy;
1496 bool queue = false;
1497
1498 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1499
1500 if (on) {
1501 set_bit(MHL, &motg->inputs);
1502 } else {
1503 clear_bit(MHL, &motg->inputs);
1504 queue = true;
1505 }
1506
1507 if (queue && phy->state != OTG_STATE_UNDEFINED)
1508 schedule_work(&motg->sm_work);
1509}
1510
1511static bool msm_otg_is_mhl(struct msm_otg *motg)
1512{
1513 struct usb_phy *phy = &motg->phy;
1514 int is_mhl, ret;
1515
1516 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1517 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1518 /*
1519 * MHL driver calls our callback saying that MHL connected
1520 * if RID_GND is detected. But at later part of discovery
1521 * it may figure out MHL is not connected and returns
1522 * false. Hence clear MHL input here.
1523 */
1524 clear_bit(MHL, &motg->inputs);
1525 dev_dbg(phy->dev, "MHL device not found\n");
1526 return false;
1527 }
1528
1529 set_bit(MHL, &motg->inputs);
1530 dev_dbg(phy->dev, "MHL device found\n");
1531 return true;
1532}
1533
1534static bool msm_chg_mhl_detect(struct msm_otg *motg)
1535{
1536 bool ret, id;
1537 unsigned long flags;
1538
1539 if (!motg->mhl_enabled)
1540 return false;
1541
1542 local_irq_save(flags);
1543 id = irq_read_line(motg->pdata->pmic_id_irq);
1544 local_irq_restore(flags);
1545
1546 if (id)
1547 return false;
1548
1549 mhl_det_in_progress = true;
1550 ret = msm_otg_is_mhl(motg);
1551 mhl_det_in_progress = false;
1552
1553 return ret;
1554}
1555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001556static bool msm_chg_aca_detect(struct msm_otg *motg)
1557{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001558 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001559 u32 int_sts;
1560 bool ret = false;
1561
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301562 if (!aca_enabled())
1563 goto out;
1564
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001565 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1566 goto out;
1567
Steve Mucklef132c6c2012-06-06 18:30:57 -07001568 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001569 switch (int_sts & 0x1C) {
1570 case 0x08:
1571 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001572 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001573 motg->chg_type = USB_ACA_A_CHARGER;
1574 motg->chg_state = USB_CHG_STATE_DETECTED;
1575 clear_bit(ID_B, &motg->inputs);
1576 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301577 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001578 ret = true;
1579 }
1580 break;
1581 case 0x0C:
1582 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001583 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001584 motg->chg_type = USB_ACA_B_CHARGER;
1585 motg->chg_state = USB_CHG_STATE_DETECTED;
1586 clear_bit(ID_A, &motg->inputs);
1587 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301588 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001589 ret = true;
1590 }
1591 break;
1592 case 0x10:
1593 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001594 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001595 motg->chg_type = USB_ACA_C_CHARGER;
1596 motg->chg_state = USB_CHG_STATE_DETECTED;
1597 clear_bit(ID_A, &motg->inputs);
1598 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301599 set_bit(ID, &motg->inputs);
1600 ret = true;
1601 }
1602 break;
1603 case 0x04:
1604 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001605 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301606 motg->chg_type = USB_INVALID_CHARGER;
1607 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1608 clear_bit(ID_A, &motg->inputs);
1609 clear_bit(ID_B, &motg->inputs);
1610 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001611 ret = true;
1612 }
1613 break;
1614 default:
1615 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1616 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301617 test_and_clear_bit(ID_C, &motg->inputs) |
1618 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001619 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001620 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001621 motg->chg_type = USB_INVALID_CHARGER;
1622 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1623 }
1624 }
1625out:
1626 return ret;
1627}
1628
1629static void msm_chg_enable_aca_det(struct msm_otg *motg)
1630{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001631 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001632
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301633 if (!aca_enabled())
1634 return;
1635
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001636 switch (motg->pdata->phy_type) {
1637 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301638 /* Disable ID_GND in link and PHY */
1639 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1640 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001641 ulpi_write(phy, 0x01, 0x0C);
1642 ulpi_write(phy, 0x10, 0x0F);
1643 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301644 /* Disable PMIC ID pull-up */
1645 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301646 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001647 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301648 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001649 break;
1650 default:
1651 break;
1652 }
1653}
1654
1655static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1656{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001657 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001658
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301659 if (!aca_enabled())
1660 return;
1661
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001662 switch (motg->pdata->phy_type) {
1663 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301664 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001665 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301666 break;
1667 default:
1668 break;
1669 }
1670}
1671
1672static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1673{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001674 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301675
1676 if (!aca_enabled())
1677 return;
1678
1679 switch (motg->pdata->phy_type) {
1680 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001681 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001682 break;
1683 default:
1684 break;
1685 }
1686}
1687
1688static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1689{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001690 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001691 bool ret = false;
1692
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301693 if (!aca_enabled())
1694 return ret;
1695
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696 switch (motg->pdata->phy_type) {
1697 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001698 if (ulpi_read(phy, 0x91) & 1) {
1699 dev_dbg(phy->dev, "RID change\n");
1700 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001701 ret = msm_chg_aca_detect(motg);
1702 }
1703 default:
1704 break;
1705 }
1706 return ret;
1707}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301708
1709static void msm_otg_id_timer_func(unsigned long data)
1710{
1711 struct msm_otg *motg = (struct msm_otg *) data;
1712
1713 if (!aca_enabled())
1714 return;
1715
1716 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001717 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301718 return;
1719 }
1720
Steve Mucklef132c6c2012-06-06 18:30:57 -07001721 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301722 goto out;
1723
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301724 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001725 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301726 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301727 }
1728
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301729out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301730 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1731 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1732}
1733
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301734static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1735{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001736 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301737 u32 chg_det;
1738 bool ret = false;
1739
1740 switch (motg->pdata->phy_type) {
1741 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001742 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301743 ret = chg_det & (1 << 4);
1744 break;
1745 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001746 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301747 ret = chg_det & 1;
1748 break;
1749 default:
1750 break;
1751 }
1752 return ret;
1753}
1754
1755static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1756{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001757 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301758 u32 chg_det;
1759
1760 switch (motg->pdata->phy_type) {
1761 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001762 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301763 /* Turn off charger block */
1764 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001765 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301766 udelay(20);
1767 /* control chg block via ULPI */
1768 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001769 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301770 /* put it in host mode for enabling D- source */
1771 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001772 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301773 /* Turn on chg detect block */
1774 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001775 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301776 udelay(20);
1777 /* enable chg detection */
1778 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001779 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301780 break;
1781 case SNPS_28NM_INTEGRATED_PHY:
1782 /*
1783 * Configure DM as current source, DP as current sink
1784 * and enable battery charging comparators.
1785 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001786 ulpi_write(phy, 0x8, 0x85);
1787 ulpi_write(phy, 0x2, 0x85);
1788 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301789 break;
1790 default:
1791 break;
1792 }
1793}
1794
1795static bool msm_chg_check_primary_det(struct msm_otg *motg)
1796{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001797 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301798 u32 chg_det;
1799 bool ret = false;
1800
1801 switch (motg->pdata->phy_type) {
1802 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001803 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301804 ret = chg_det & (1 << 4);
1805 break;
1806 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001807 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301808 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301809 /* Turn off VDP_SRC */
1810 ulpi_write(phy, 0x3, 0x86);
1811 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301812 break;
1813 default:
1814 break;
1815 }
1816 return ret;
1817}
1818
1819static void msm_chg_enable_primary_det(struct msm_otg *motg)
1820{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001821 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301822 u32 chg_det;
1823
1824 switch (motg->pdata->phy_type) {
1825 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001826 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301827 /* enable chg detection */
1828 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001829 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301830 break;
1831 case SNPS_28NM_INTEGRATED_PHY:
1832 /*
1833 * Configure DP as current source, DM as current sink
1834 * and enable battery charging comparators.
1835 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001836 ulpi_write(phy, 0x2, 0x85);
1837 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301838 break;
1839 default:
1840 break;
1841 }
1842}
1843
1844static bool msm_chg_check_dcd(struct msm_otg *motg)
1845{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001846 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301847 u32 line_state;
1848 bool ret = false;
1849
1850 switch (motg->pdata->phy_type) {
1851 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001852 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301853 ret = !(line_state & 1);
1854 break;
1855 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001856 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301857 ret = line_state & 2;
1858 break;
1859 default:
1860 break;
1861 }
1862 return ret;
1863}
1864
1865static void msm_chg_disable_dcd(struct msm_otg *motg)
1866{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001867 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301868 u32 chg_det;
1869
1870 switch (motg->pdata->phy_type) {
1871 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001872 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301873 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001874 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301875 break;
1876 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001877 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301878 break;
1879 default:
1880 break;
1881 }
1882}
1883
1884static void msm_chg_enable_dcd(struct msm_otg *motg)
1885{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001886 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301887 u32 chg_det;
1888
1889 switch (motg->pdata->phy_type) {
1890 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001891 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301892 /* Turn on D+ current source */
1893 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001894 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301895 break;
1896 case SNPS_28NM_INTEGRATED_PHY:
1897 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001898 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301899 break;
1900 default:
1901 break;
1902 }
1903}
1904
1905static void msm_chg_block_on(struct msm_otg *motg)
1906{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001907 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301908 u32 func_ctrl, chg_det;
1909
1910 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001911 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301912 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1913 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001914 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301915
1916 switch (motg->pdata->phy_type) {
1917 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001918 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301919 /* control chg block via ULPI */
1920 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001921 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301922 /* Turn on chg detect block */
1923 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001924 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301925 udelay(20);
1926 break;
1927 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05301928 /* disable DP and DM pull down resistors */
1929 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301930 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001931 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301932 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001933 ulpi_write(phy, 0x1F, 0x92);
1934 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301935 udelay(100);
1936 break;
1937 default:
1938 break;
1939 }
1940}
1941
1942static void msm_chg_block_off(struct msm_otg *motg)
1943{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001944 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301945 u32 func_ctrl, chg_det;
1946
1947 switch (motg->pdata->phy_type) {
1948 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001949 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301950 /* Turn off charger block */
1951 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001952 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301953 break;
1954 case SNPS_28NM_INTEGRATED_PHY:
1955 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001956 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301957 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001958 ulpi_write(phy, 0x1F, 0x92);
1959 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301960 break;
1961 default:
1962 break;
1963 }
1964
1965 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001966 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301967 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1968 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001969 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301970}
1971
Anji jonnalad270e2d2011-08-09 11:28:32 +05301972static const char *chg_to_string(enum usb_chg_type chg_type)
1973{
1974 switch (chg_type) {
1975 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1976 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1977 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1978 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1979 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1980 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1981 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301982 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05301983 default: return "INVALID_CHARGER";
1984 }
1985}
kibum.leeec040442012-08-22 22:29:46 +09001986
Devin Kimd93ff032012-09-14 15:01:31 -07001987#define MSM_CHECK_TA_DELAY (5 * HZ)
1988#define PORTSC_LS (3 << 10) /* Read - Port's Line status */
kibum.leeec040442012-08-22 22:29:46 +09001989static void msm_ta_detect_work(struct work_struct *w)
1990{
Devin Kimd93ff032012-09-14 15:01:31 -07001991 struct msm_otg *motg = container_of(w, struct msm_otg, check_ta_work.work);
kibum.leeec040442012-08-22 22:29:46 +09001992 struct usb_otg *otg = motg->phy.otg;
kibum.leeec040442012-08-22 22:29:46 +09001993
Devin Kim6b8a5d12012-10-16 23:12:18 -07001994 pr_debug("msm_ta_detect_work: ta detection work\n");
Devin Kimd93ff032012-09-14 15:01:31 -07001995
1996 /* Presence of FRame Index or FRINDEX rollover implies USB communication */
1997 if( (readl(USB_FRINDEX) != 0) || ( readl(USB_USBSTS) & (1<<3) ) ) {
1998 pr_info("msm_ta_detect_work: USB exit ta detection - frindex\n");
1999 return;
2000 }
2001
2002 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
2003 pr_info("msm_ta_detect_work: ta dectection success\n");
kibum.leeec040442012-08-22 22:29:46 +09002004 /* inform to user space that SDP is no longer detected */
2005 msm_otg_notify_charger(motg, 0);
2006 motg->chg_state = USB_CHG_STATE_DETECTED;
2007 motg->chg_type = USB_DCP_CHARGER;
2008 motg->cur_power = 0;
2009 msm_otg_start_peripheral(otg, 0);
2010 otg->phy->state = OTG_STATE_B_IDLE;
2011 schedule_work(&motg->sm_work);
2012 return;
kibum.leeec040442012-08-22 22:29:46 +09002013 }
Devin Kimd93ff032012-09-14 15:01:31 -07002014 schedule_delayed_work(&motg->check_ta_work, MSM_CHECK_TA_DELAY);
kibum.leeec040442012-08-22 22:29:46 +09002015}
Anji jonnalad270e2d2011-08-09 11:28:32 +05302016
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302017#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
2018#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302019#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2020#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302021static void msm_chg_detect_work(struct work_struct *w)
2022{
2023 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002024 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302025 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302026 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302027 unsigned long delay;
2028
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002029 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302030
2031 if (test_bit(MHL, &motg->inputs)) {
2032 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2033 return;
2034 }
2035
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302036 switch (motg->chg_state) {
2037 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302038 msm_chg_block_on(motg);
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302039 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002040 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302041 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2042 motg->dcd_retries = 0;
2043 delay = MSM_CHG_DCD_POLL_TIME;
2044 break;
2045 case USB_CHG_STATE_WAIT_FOR_DCD:
jh.koo78df86f2012-09-04 20:13:19 +09002046 if (slimport_is_connected()) {
2047 msm_chg_block_off(motg);
2048 motg->chg_state = USB_CHG_STATE_DETECTED;
2049 motg->chg_type = USB_SDP_CHARGER;
2050 queue_work(system_nrt_wq, &motg->sm_work);
2051 return;
2052 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302053 if (msm_chg_mhl_detect(motg)) {
2054 msm_chg_block_off(motg);
2055 motg->chg_state = USB_CHG_STATE_DETECTED;
2056 motg->chg_type = USB_INVALID_CHARGER;
2057 queue_work(system_nrt_wq, &motg->sm_work);
2058 return;
2059 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002060 is_aca = msm_chg_aca_detect(motg);
2061 if (is_aca) {
2062 /*
2063 * ID_A can be ACA dock too. continue
2064 * primary detection after DCD.
2065 */
2066 if (test_bit(ID_A, &motg->inputs)) {
2067 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2068 } else {
2069 delay = 0;
2070 break;
2071 }
2072 }
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302073 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302074 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2075 if (is_dcd || tmout) {
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302076 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302077 msm_chg_enable_primary_det(motg);
2078 delay = MSM_CHG_PRIMARY_DET_TIME;
2079 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2080 } else {
2081 delay = MSM_CHG_DCD_POLL_TIME;
2082 }
2083 break;
2084 case USB_CHG_STATE_DCD_DONE:
2085 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302086 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2087 dm_vlgc = line_state & PORTSC_LS_DM;
2088 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302089 if (test_bit(ID_A, &motg->inputs)) {
2090 motg->chg_type = USB_ACA_DOCK_CHARGER;
2091 motg->chg_state = USB_CHG_STATE_DETECTED;
2092 delay = 0;
2093 break;
2094 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302095 if (line_state) { /* DP > VLGC */
2096 motg->chg_type = USB_PROPRIETARY_CHARGER;
2097 motg->chg_state = USB_CHG_STATE_DETECTED;
2098 delay = 0;
2099 } else {
2100 msm_chg_enable_secondary_det(motg);
2101 delay = MSM_CHG_SECONDARY_DET_TIME;
2102 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2103 }
2104 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302105 if (test_bit(ID_A, &motg->inputs)) {
2106 motg->chg_type = USB_ACA_A_CHARGER;
2107 motg->chg_state = USB_CHG_STATE_DETECTED;
2108 delay = 0;
2109 break;
2110 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302111
2112 if (line_state) /* DP > VLGC or/and DM > VLGC */
2113 motg->chg_type = USB_PROPRIETARY_CHARGER;
2114 else
2115 motg->chg_type = USB_SDP_CHARGER;
2116
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302117 motg->chg_state = USB_CHG_STATE_DETECTED;
2118 delay = 0;
2119 }
2120 break;
2121 case USB_CHG_STATE_PRIMARY_DONE:
2122 vout = msm_chg_check_secondary_det(motg);
2123 if (vout)
2124 motg->chg_type = USB_DCP_CHARGER;
2125 else
2126 motg->chg_type = USB_CDP_CHARGER;
2127 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2128 /* fall through */
2129 case USB_CHG_STATE_SECONDARY_DONE:
2130 motg->chg_state = USB_CHG_STATE_DETECTED;
2131 case USB_CHG_STATE_DETECTED:
2132 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002133 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302134 /*
2135 * Spurious interrupt is seen after enabling ACA detection
2136 * due to which charger detection fails in case of PET.
2137 * Add delay of 100 microsec to avoid that.
2138 */
2139 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002140 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002141 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302142 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302143 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302144 return;
2145 default:
2146 return;
2147 }
2148
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302149 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302150}
2151
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302152/*
2153 * We support OTG, Peripheral only and Host only configurations. In case
2154 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2155 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2156 * enabled when switch is controlled by user and default mode is supplied
2157 * by board file, which can be changed by userspace later.
2158 */
2159static void msm_otg_init_sm(struct msm_otg *motg)
2160{
2161 struct msm_otg_platform_data *pdata = motg->pdata;
2162 u32 otgsc = readl(USB_OTGSC);
2163
2164 switch (pdata->mode) {
2165 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002166 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302167 if (pdata->default_mode == USB_HOST) {
2168 clear_bit(ID, &motg->inputs);
2169 } else if (pdata->default_mode == USB_PERIPHERAL) {
2170 set_bit(ID, &motg->inputs);
2171 set_bit(B_SESS_VLD, &motg->inputs);
2172 } else {
2173 set_bit(ID, &motg->inputs);
2174 clear_bit(B_SESS_VLD, &motg->inputs);
2175 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302176 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302177 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302178 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302179 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302180 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302181 set_bit(A_BUS_REQ, &motg->inputs);
2182 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002183 if (otgsc & OTGSC_BSV)
2184 set_bit(B_SESS_VLD, &motg->inputs);
2185 else
2186 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302187 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302188 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07002189 unsigned long flags;
2190 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302191 if (irq_read_line(pdata->pmic_id_irq))
2192 set_bit(ID, &motg->inputs);
2193 else
2194 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07002195 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302196 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302197 /*
2198 * VBUS initial state is reported after PMIC
2199 * driver initialization. Wait for it.
2200 */
2201 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302202 }
2203 break;
2204 case USB_HOST:
2205 clear_bit(ID, &motg->inputs);
2206 break;
2207 case USB_PERIPHERAL:
2208 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302209 if (pdata->otg_control == OTG_PHY_CONTROL) {
2210 if (otgsc & OTGSC_BSV)
2211 set_bit(B_SESS_VLD, &motg->inputs);
2212 else
2213 clear_bit(B_SESS_VLD, &motg->inputs);
2214 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2215 /*
2216 * VBUS initial state is reported after PMIC
2217 * driver initialization. Wait for it.
2218 */
2219 wait_for_completion(&pmic_vbus_init);
2220 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302221 break;
2222 default:
2223 break;
2224 }
2225}
2226
2227static void msm_otg_sm_work(struct work_struct *w)
2228{
2229 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002230 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302231 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302232
Steve Mucklef132c6c2012-06-06 18:30:57 -07002233 pm_runtime_resume(otg->phy->dev);
2234 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002235 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302236 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002237 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302238 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302239 psy = power_supply_get_by_name("usb");
2240 if (!psy)
2241 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002242 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302243 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2244 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002245 pm_runtime_put_noidle(otg->phy->dev);
2246 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302247 break;
2248 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302249 /* FALL THROUGH */
2250 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302251 if (test_bit(MHL, &motg->inputs)) {
2252 /* allow LPM */
2253 pm_runtime_put_noidle(otg->phy->dev);
2254 pm_runtime_suspend(otg->phy->dev);
2255 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002256 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302257 pr_debug("!id || id_A\n");
jh.koo78df86f2012-09-04 20:13:19 +09002258 if (slimport_is_connected()) {
2259 work = 1;
2260 break;
2261 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302262 if (msm_chg_mhl_detect(motg)) {
2263 work = 1;
2264 break;
2265 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302266 clear_bit(B_BUS_REQ, &motg->inputs);
2267 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002268 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302269 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302270 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302271 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302272 switch (motg->chg_state) {
2273 case USB_CHG_STATE_UNDEFINED:
2274 msm_chg_detect_work(&motg->chg_work.work);
2275 break;
2276 case USB_CHG_STATE_DETECTED:
2277 switch (motg->chg_type) {
2278 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302279 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002280 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302281 /* fall through */
2282 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302283 msm_otg_notify_charger(motg,
2284 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002285 pm_runtime_put_noidle(otg->phy->dev);
2286 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302287 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302288 case USB_ACA_B_CHARGER:
2289 msm_otg_notify_charger(motg,
2290 IDEV_ACA_CHG_MAX);
2291 /*
2292 * (ID_B --> ID_C) PHY_ALT interrupt can
2293 * not be detected in LPM.
2294 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302295 break;
2296 case USB_CDP_CHARGER:
2297 msm_otg_notify_charger(motg,
2298 IDEV_CHG_MAX);
2299 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002300 otg->phy->state =
2301 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302302 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302303 case USB_ACA_C_CHARGER:
2304 msm_otg_notify_charger(motg,
2305 IDEV_ACA_CHG_MAX);
2306 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002307 otg->phy->state =
2308 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302309 break;
2310 case USB_SDP_CHARGER:
roy.park99cef212012-07-26 17:00:40 -07002311 msm_otg_notify_charger(motg,
2312 IDEV_CHG_MIN);
jh.koo78df86f2012-09-04 20:13:19 +09002313 if(!slimport_is_connected()) {
2314 msm_otg_start_peripheral(otg, 1);
2315 otg->phy->state =
2316 OTG_STATE_B_PERIPHERAL;
2317 }
kibum.leeec040442012-08-22 22:29:46 +09002318 schedule_delayed_work(&motg->check_ta_work,
2319 MSM_CHECK_TA_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302320 break;
2321 default:
2322 break;
2323 }
2324 break;
2325 default:
2326 break;
2327 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302328 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2329 pr_debug("b_sess_end && b_bus_req\n");
2330 if (msm_otg_start_srp(otg) < 0) {
2331 clear_bit(B_BUS_REQ, &motg->inputs);
2332 work = 1;
2333 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302334 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002335 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302336 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2337 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302338 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302339 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302340 cancel_delayed_work_sync(&motg->chg_work);
kibum.leeec040442012-08-22 22:29:46 +09002341 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302342 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2343 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302344 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002345 msm_otg_reset(otg->phy);
2346 pm_runtime_put_noidle(otg->phy->dev);
2347 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302348 }
2349 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302350 case OTG_STATE_B_SRP_INIT:
2351 if (!test_bit(ID, &motg->inputs) ||
2352 test_bit(ID_A, &motg->inputs) ||
2353 test_bit(ID_C, &motg->inputs) ||
2354 (test_bit(B_SESS_VLD, &motg->inputs) &&
2355 !test_bit(ID_B, &motg->inputs))) {
2356 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2357 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002358 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302359 /*
2360 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2361 * bits after SRP initiation.
2362 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002363 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302364 work = 1;
2365 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2366 pr_debug("b_srp_fail\n");
2367 pr_info("A-device did not respond to SRP\n");
2368 clear_bit(B_BUS_REQ, &motg->inputs);
2369 clear_bit(B_SRP_FAIL, &motg->tmouts);
2370 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002371 ulpi_write(otg->phy, 0x0, 0x98);
2372 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302373 motg->b_last_se0_sess = jiffies;
2374 work = 1;
2375 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302376 break;
2377 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302378 if (!test_bit(ID, &motg->inputs) ||
2379 test_bit(ID_A, &motg->inputs) ||
2380 test_bit(ID_B, &motg->inputs) ||
2381 !test_bit(B_SESS_VLD, &motg->inputs)) {
2382 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302383 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2384 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302385 msm_otg_notify_charger(motg, 0);
2386 srp_reqd = otg->gadget->otg_srp_reqd;
2387 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302388 if (test_bit(ID_B, &motg->inputs))
2389 clear_bit(ID_B, &motg->inputs);
2390 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002391 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302392 motg->b_last_se0_sess = jiffies;
2393 if (srp_reqd)
2394 msm_otg_start_timer(motg,
2395 TB_TST_SRP, B_TST_SRP);
2396 else
2397 work = 1;
2398 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2399 otg->gadget->b_hnp_enable &&
2400 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2401 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2402 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2403 /* D+ pullup should not be disconnected within 4msec
2404 * after A device suspends the bus. Otherwise PET will
2405 * fail the compliance test.
2406 */
2407 udelay(1000);
2408 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002409 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302410 /*
2411 * start HCD even before A-device enable
2412 * pull-up to meet HNP timings.
2413 */
2414 otg->host->is_b_host = 1;
2415 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002416 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2417 test_bit(B_SESS_VLD, &motg->inputs)) {
2418 pr_debug("a_bus_suspend && b_sess_vld\n");
2419 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002420 pm_runtime_put_noidle(otg->phy->dev);
2421 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002422 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002423 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302424 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002425 }
2426 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302427 case OTG_STATE_B_WAIT_ACON:
2428 if (!test_bit(ID, &motg->inputs) ||
2429 test_bit(ID_A, &motg->inputs) ||
2430 test_bit(ID_B, &motg->inputs) ||
2431 !test_bit(B_SESS_VLD, &motg->inputs)) {
2432 pr_debug("!id || id_a/b || !b_sess_vld\n");
2433 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302434 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302435 * A-device is physically disconnected during
2436 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302437 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302438 msm_otg_start_host(otg, 0);
2439 otg->host->is_b_host = 0;
2440
2441 clear_bit(B_BUS_REQ, &motg->inputs);
2442 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2443 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002444 otg->phy->state = OTG_STATE_B_IDLE;
2445 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302446 work = 1;
2447 } else if (test_bit(A_CONN, &motg->inputs)) {
2448 pr_debug("a_conn\n");
2449 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002450 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302451 /*
2452 * PET disconnects D+ pullup after reset is generated
2453 * by B device in B_HOST role which is not detected by
2454 * B device. As workaorund , start timer of 300msec
2455 * and stop timer if A device is enumerated else clear
2456 * A_CONN.
2457 */
2458 msm_otg_start_timer(motg, TB_TST_CONFIG,
2459 B_TST_CONFIG);
2460 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2461 pr_debug("b_ase0_brst_tmout\n");
2462 pr_info("B HNP fail:No response from A device\n");
2463 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002464 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302465 otg->host->is_b_host = 0;
2466 clear_bit(B_ASE0_BRST, &motg->tmouts);
2467 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2468 clear_bit(B_BUS_REQ, &motg->inputs);
2469 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002470 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302471 work = 1;
2472 } else if (test_bit(ID_C, &motg->inputs)) {
2473 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2474 }
2475 break;
2476 case OTG_STATE_B_HOST:
2477 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2478 !test_bit(A_CONN, &motg->inputs) ||
2479 !test_bit(B_SESS_VLD, &motg->inputs)) {
2480 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2481 clear_bit(A_CONN, &motg->inputs);
2482 clear_bit(B_BUS_REQ, &motg->inputs);
2483 msm_otg_start_host(otg, 0);
2484 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002485 otg->phy->state = OTG_STATE_B_IDLE;
2486 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302487 work = 1;
2488 } else if (test_bit(ID_C, &motg->inputs)) {
2489 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2490 }
2491 break;
2492 case OTG_STATE_A_IDLE:
2493 otg->default_a = 1;
2494 if (test_bit(ID, &motg->inputs) &&
2495 !test_bit(ID_A, &motg->inputs)) {
2496 pr_debug("id && !id_a\n");
2497 otg->default_a = 0;
2498 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002499 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302500 del_timer_sync(&motg->id_timer);
2501 msm_otg_link_reset(motg);
2502 msm_chg_enable_aca_intr(motg);
2503 msm_otg_notify_charger(motg, 0);
2504 work = 1;
2505 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2506 (test_bit(A_SRP_DET, &motg->inputs) ||
2507 test_bit(A_BUS_REQ, &motg->inputs))) {
2508 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2509
2510 clear_bit(A_SRP_DET, &motg->inputs);
2511 /* Disable SRP detection */
2512 writel_relaxed((readl_relaxed(USB_OTGSC) &
2513 ~OTGSC_INTSTS_MASK) &
2514 ~OTGSC_DPIE, USB_OTGSC);
2515
Steve Mucklef132c6c2012-06-06 18:30:57 -07002516 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302517 /* VBUS should not be supplied before end of SRP pulse
2518 * generated by PET, if not complaince test fail.
2519 */
2520 usleep_range(10000, 12000);
2521 /* ACA: ID_A: Stop charging untill enumeration */
2522 if (test_bit(ID_A, &motg->inputs))
2523 msm_otg_notify_charger(motg, 0);
2524 else
2525 msm_hsusb_vbus_power(motg, 1);
2526 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2527 } else {
2528 pr_debug("No session requested\n");
2529 clear_bit(A_BUS_DROP, &motg->inputs);
2530 if (test_bit(ID_A, &motg->inputs)) {
2531 msm_otg_notify_charger(motg,
2532 IDEV_ACA_CHG_MAX);
2533 } else if (!test_bit(ID, &motg->inputs)) {
2534 msm_otg_notify_charger(motg, 0);
2535 /*
2536 * A-device is not providing power on VBUS.
2537 * Enable SRP detection.
2538 */
2539 writel_relaxed(0x13, USB_USBMODE);
2540 writel_relaxed((readl_relaxed(USB_OTGSC) &
2541 ~OTGSC_INTSTS_MASK) |
2542 OTGSC_DPIE, USB_OTGSC);
2543 mb();
2544 }
2545 }
2546 break;
2547 case OTG_STATE_A_WAIT_VRISE:
2548 if ((test_bit(ID, &motg->inputs) &&
2549 !test_bit(ID_A, &motg->inputs)) ||
2550 test_bit(A_BUS_DROP, &motg->inputs) ||
2551 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2552 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2553 clear_bit(A_BUS_REQ, &motg->inputs);
2554 msm_otg_del_timer(motg);
2555 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002556 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302557 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2558 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2559 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002560 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302561 if (TA_WAIT_BCON > 0)
2562 msm_otg_start_timer(motg, TA_WAIT_BCON,
2563 A_WAIT_BCON);
2564 msm_otg_start_host(otg, 1);
2565 msm_chg_enable_aca_det(motg);
2566 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302567 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302568 if (msm_chg_check_aca_intr(motg))
2569 work = 1;
2570 }
2571 break;
2572 case OTG_STATE_A_WAIT_BCON:
2573 if ((test_bit(ID, &motg->inputs) &&
2574 !test_bit(ID_A, &motg->inputs)) ||
2575 test_bit(A_BUS_DROP, &motg->inputs) ||
2576 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2577 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2578 "a_wait_bcon_tmout\n");
2579 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2580 pr_info("Device No Response\n");
2581 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2582 }
2583 msm_otg_del_timer(motg);
2584 clear_bit(A_BUS_REQ, &motg->inputs);
2585 clear_bit(B_CONN, &motg->inputs);
2586 msm_otg_start_host(otg, 0);
2587 /*
2588 * ACA: ID_A with NO accessory, just the A plug is
2589 * attached to ACA: Use IDCHG_MAX for charging
2590 */
2591 if (test_bit(ID_A, &motg->inputs))
2592 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2593 else
2594 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002595 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302596 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2597 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2598 pr_debug("!a_vbus_vld\n");
2599 clear_bit(B_CONN, &motg->inputs);
2600 msm_otg_del_timer(motg);
2601 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002602 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2603 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302604 } else if (test_bit(ID_A, &motg->inputs)) {
2605 msm_hsusb_vbus_power(motg, 0);
2606 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2607 /*
2608 * If TA_WAIT_BCON is infinite, we don;t
2609 * turn off VBUS. Enter low power mode.
2610 */
2611 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002612 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302613 } else if (!test_bit(ID, &motg->inputs)) {
2614 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302615 }
2616 break;
2617 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302618 if ((test_bit(ID, &motg->inputs) &&
2619 !test_bit(ID_A, &motg->inputs)) ||
2620 test_bit(A_BUS_DROP, &motg->inputs)) {
2621 pr_debug("id_a/b/c || a_bus_drop\n");
2622 clear_bit(B_CONN, &motg->inputs);
2623 clear_bit(A_BUS_REQ, &motg->inputs);
2624 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002625 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302626 msm_otg_start_host(otg, 0);
2627 if (!test_bit(ID_A, &motg->inputs))
2628 msm_hsusb_vbus_power(motg, 0);
2629 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2630 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2631 pr_debug("!a_vbus_vld\n");
2632 clear_bit(B_CONN, &motg->inputs);
2633 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002634 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302635 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002636 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302637 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2638 /*
2639 * a_bus_req is de-asserted when root hub is
2640 * suspended or HNP is in progress.
2641 */
2642 pr_debug("!a_bus_req\n");
2643 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002644 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302645 if (otg->host->b_hnp_enable)
2646 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2647 A_AIDL_BDIS);
2648 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002649 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302650 } else if (!test_bit(B_CONN, &motg->inputs)) {
2651 pr_debug("!b_conn\n");
2652 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002653 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302654 if (TA_WAIT_BCON > 0)
2655 msm_otg_start_timer(motg, TA_WAIT_BCON,
2656 A_WAIT_BCON);
2657 if (msm_chg_check_aca_intr(motg))
2658 work = 1;
2659 } else if (test_bit(ID_A, &motg->inputs)) {
2660 msm_otg_del_timer(motg);
2661 msm_hsusb_vbus_power(motg, 0);
2662 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2663 msm_otg_notify_charger(motg,
2664 IDEV_ACA_CHG_MAX);
2665 else
2666 msm_otg_notify_charger(motg,
2667 IDEV_CHG_MIN - motg->mA_port);
2668 } else if (!test_bit(ID, &motg->inputs)) {
2669 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2670 motg->chg_type = USB_INVALID_CHARGER;
2671 msm_otg_notify_charger(motg, 0);
2672 msm_hsusb_vbus_power(motg, 1);
2673 }
2674 break;
2675 case OTG_STATE_A_SUSPEND:
2676 if ((test_bit(ID, &motg->inputs) &&
2677 !test_bit(ID_A, &motg->inputs)) ||
2678 test_bit(A_BUS_DROP, &motg->inputs) ||
2679 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2680 pr_debug("id_a/b/c || a_bus_drop ||"
2681 "a_aidl_bdis_tmout\n");
2682 msm_otg_del_timer(motg);
2683 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002684 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302685 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002686 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302687 if (!test_bit(ID_A, &motg->inputs))
2688 msm_hsusb_vbus_power(motg, 0);
2689 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2690 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2691 pr_debug("!a_vbus_vld\n");
2692 msm_otg_del_timer(motg);
2693 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002694 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302695 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002696 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302697 } else if (!test_bit(B_CONN, &motg->inputs) &&
2698 otg->host->b_hnp_enable) {
2699 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002700 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302701 msm_otg_host_hnp_enable(otg, 1);
2702 otg->gadget->is_a_peripheral = 1;
2703 msm_otg_start_peripheral(otg, 1);
2704 } else if (!test_bit(B_CONN, &motg->inputs) &&
2705 !otg->host->b_hnp_enable) {
2706 pr_debug("!b_conn && !b_hnp_enable");
2707 /*
2708 * bus request is dropped during suspend.
2709 * acquire again for next device.
2710 */
2711 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002712 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302713 if (TA_WAIT_BCON > 0)
2714 msm_otg_start_timer(motg, TA_WAIT_BCON,
2715 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002716 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302717 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002718 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302719 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002720 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002721 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302722 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302723 }
2724 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302725 case OTG_STATE_A_PERIPHERAL:
2726 if ((test_bit(ID, &motg->inputs) &&
2727 !test_bit(ID_A, &motg->inputs)) ||
2728 test_bit(A_BUS_DROP, &motg->inputs)) {
2729 pr_debug("id _f/b/c || a_bus_drop\n");
2730 /* Clear BIDL_ADIS timer */
2731 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002732 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302733 msm_otg_start_peripheral(otg, 0);
2734 otg->gadget->is_a_peripheral = 0;
2735 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002736 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302737 if (!test_bit(ID_A, &motg->inputs))
2738 msm_hsusb_vbus_power(motg, 0);
2739 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2740 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2741 pr_debug("!a_vbus_vld\n");
2742 /* Clear BIDL_ADIS timer */
2743 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002744 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302745 msm_otg_start_peripheral(otg, 0);
2746 otg->gadget->is_a_peripheral = 0;
2747 msm_otg_start_host(otg, 0);
2748 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2749 pr_debug("a_bidl_adis_tmout\n");
2750 msm_otg_start_peripheral(otg, 0);
2751 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002752 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302753 set_bit(A_BUS_REQ, &motg->inputs);
2754 msm_otg_host_hnp_enable(otg, 0);
2755 if (TA_WAIT_BCON > 0)
2756 msm_otg_start_timer(motg, TA_WAIT_BCON,
2757 A_WAIT_BCON);
2758 } else if (test_bit(ID_A, &motg->inputs)) {
2759 msm_hsusb_vbus_power(motg, 0);
2760 msm_otg_notify_charger(motg,
2761 IDEV_CHG_MIN - motg->mA_port);
2762 } else if (!test_bit(ID, &motg->inputs)) {
2763 msm_otg_notify_charger(motg, 0);
2764 msm_hsusb_vbus_power(motg, 1);
2765 }
2766 break;
2767 case OTG_STATE_A_WAIT_VFALL:
2768 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2769 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002770 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302771 work = 1;
2772 }
2773 break;
2774 case OTG_STATE_A_VBUS_ERR:
2775 if ((test_bit(ID, &motg->inputs) &&
2776 !test_bit(ID_A, &motg->inputs)) ||
2777 test_bit(A_BUS_DROP, &motg->inputs) ||
2778 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002779 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302780 if (!test_bit(ID_A, &motg->inputs))
2781 msm_hsusb_vbus_power(motg, 0);
2782 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2783 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2784 motg->chg_type = USB_INVALID_CHARGER;
2785 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302786 }
2787 break;
2788 default:
2789 break;
2790 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302791 if (work)
2792 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302793}
2794
2795static irqreturn_t msm_otg_irq(int irq, void *data)
2796{
2797 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002798 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302799 u32 otgsc = 0, usbsts, pc;
2800 bool work = 0;
2801 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302802
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302803 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002804 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302805 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002806 motg->async_int = irq;
Jack Phamb1aa4d82012-08-13 15:32:39 -07002807 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002808 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302809 return IRQ_HANDLED;
2810 }
2811
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002812 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302813 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302814
2815 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302816 return IRQ_NONE;
2817
2818 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302819 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302820 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302821 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302822 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302823 pr_debug("Id clear\n");
2824 /*
2825 * Assert a_bus_req to supply power on
2826 * VBUS when Micro/Mini-A cable is connected
2827 * with out user intervention.
2828 */
2829 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302830 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302831 msm_chg_enable_aca_det(motg);
2832 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302833 writel_relaxed(otgsc, USB_OTGSC);
2834 work = 1;
2835 } else if (otgsc & OTGSC_DPIS) {
2836 pr_debug("DPIS detected\n");
2837 writel_relaxed(otgsc, USB_OTGSC);
2838 set_bit(A_SRP_DET, &motg->inputs);
2839 set_bit(A_BUS_REQ, &motg->inputs);
2840 work = 1;
2841 } else if (otgsc & OTGSC_BSVIS) {
2842 writel_relaxed(otgsc, USB_OTGSC);
2843 /*
2844 * BSV interrupt comes when operating as an A-device
2845 * (VBUS on/off).
2846 * But, handle BSV when charger is removed from ACA in ID_A
2847 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002848 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302849 !test_bit(ID_A, &motg->inputs))
2850 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302851 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302852 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302853 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302854 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302855 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302856 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002857 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2858
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302859 msm_chg_check_aca_intr(motg);
2860 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302861 work = 1;
2862 } else if (usbsts & STS_PCI) {
2863 pc = readl_relaxed(USB_PORTSC);
2864 pr_debug("portsc = %x\n", pc);
2865 ret = IRQ_NONE;
2866 /*
2867 * HCD Acks PCI interrupt. We use this to switch
2868 * between different OTG states.
2869 */
2870 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002871 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302872 case OTG_STATE_A_SUSPEND:
2873 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2874 !(pc & PORTSC_CCS)) {
2875 pr_debug("B_CONN clear\n");
2876 clear_bit(B_CONN, &motg->inputs);
2877 msm_otg_del_timer(motg);
2878 }
2879 break;
2880 case OTG_STATE_A_PERIPHERAL:
2881 /*
2882 * A-peripheral observed activity on bus.
2883 * clear A_BIDL_ADIS timer.
2884 */
2885 msm_otg_del_timer(motg);
2886 work = 0;
2887 break;
2888 case OTG_STATE_B_WAIT_ACON:
2889 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2890 pr_debug("A_CONN set\n");
2891 set_bit(A_CONN, &motg->inputs);
2892 /* Clear ASE0_BRST timer */
2893 msm_otg_del_timer(motg);
2894 }
2895 break;
2896 case OTG_STATE_B_HOST:
2897 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2898 pr_debug("A_CONN clear\n");
2899 clear_bit(A_CONN, &motg->inputs);
2900 msm_otg_del_timer(motg);
2901 }
2902 break;
2903 case OTG_STATE_A_WAIT_BCON:
2904 if (TA_WAIT_BCON < 0)
2905 set_bit(A_BUS_REQ, &motg->inputs);
2906 default:
2907 work = 0;
2908 break;
2909 }
2910 } else if (usbsts & STS_URI) {
2911 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002912 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302913 case OTG_STATE_A_PERIPHERAL:
2914 /*
2915 * A-peripheral observed activity on bus.
2916 * clear A_BIDL_ADIS timer.
2917 */
2918 msm_otg_del_timer(motg);
2919 work = 0;
2920 break;
2921 default:
2922 work = 0;
2923 break;
2924 }
2925 } else if (usbsts & STS_SLI) {
2926 ret = IRQ_NONE;
2927 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002928 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302929 case OTG_STATE_B_PERIPHERAL:
2930 if (otg->gadget->b_hnp_enable) {
2931 set_bit(A_BUS_SUSPEND, &motg->inputs);
2932 set_bit(B_BUS_REQ, &motg->inputs);
2933 work = 1;
2934 }
2935 break;
2936 case OTG_STATE_A_PERIPHERAL:
2937 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2938 A_BIDL_ADIS);
2939 break;
2940 default:
2941 break;
2942 }
2943 } else if ((usbsts & PHY_ALT_INT)) {
2944 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2945 if (msm_chg_check_aca_intr(motg))
2946 work = 1;
2947 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302948 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302949 if (work)
2950 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302951
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302952 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002953}
2954
2955static void msm_otg_set_vbus_state(int online)
2956{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302957 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002958 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05302959 struct usb_otg *otg = motg->phy.otg;
2960
2961 /* In A Host Mode, ignore received BSV interrupts */
2962 if (otg->phy->state >= OTG_STATE_A_IDLE)
2963 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002964
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302965 if (online) {
2966 pr_debug("PMIC: BSV set\n");
2967 set_bit(B_SESS_VLD, &motg->inputs);
2968 } else {
2969 pr_debug("PMIC: BSV clear\n");
2970 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302971 }
2972
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302973 if (!init) {
2974 init = true;
2975 complete(&pmic_vbus_init);
2976 pr_debug("PMIC: BSV init complete\n");
2977 return;
2978 }
2979
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302980 if (test_bit(MHL, &motg->inputs) ||
2981 mhl_det_in_progress) {
2982 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
2983 return;
2984 }
2985
Jack Pham5ca279b2012-05-14 18:42:54 -07002986 if (atomic_read(&motg->pm_suspended))
2987 motg->sm_work_pending = true;
2988 else
2989 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002990}
2991
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302992static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002993{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302994 struct msm_otg *motg = container_of(w, struct msm_otg,
2995 pmic_id_status_work.work);
2996 int work = 0;
2997 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002998
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302999 local_irq_save(flags);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303000 if (irq_read_line(motg->pdata->pmic_id_irq)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303001 if (!test_and_set_bit(ID, &motg->inputs)) {
3002 pr_debug("PMIC: ID set\n");
3003 work = 1;
3004 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303005 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303006 if (test_and_clear_bit(ID, &motg->inputs)) {
3007 pr_debug("PMIC: ID clear\n");
3008 set_bit(A_BUS_REQ, &motg->inputs);
3009 work = 1;
3010 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303011 }
3012
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303013 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003014 if (atomic_read(&motg->pm_suspended))
3015 motg->sm_work_pending = true;
3016 else
3017 queue_work(system_nrt_wq, &motg->sm_work);
3018 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303019 local_irq_restore(flags);
3020
3021}
3022
3023#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3024static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3025{
3026 struct msm_otg *motg = data;
3027
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303028 if (test_bit(MHL, &motg->inputs) ||
3029 mhl_det_in_progress) {
3030 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3031 return IRQ_HANDLED;
3032 }
3033
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303034 if (!aca_id_turned_on)
3035 /*schedule delayed work for 5msec for ID line state to settle*/
3036 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3037 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003038
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303039 return IRQ_HANDLED;
3040}
3041
3042static int msm_otg_mode_show(struct seq_file *s, void *unused)
3043{
3044 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003045 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303046
Steve Mucklef132c6c2012-06-06 18:30:57 -07003047 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303048 case OTG_STATE_A_HOST:
3049 seq_printf(s, "host\n");
3050 break;
3051 case OTG_STATE_B_PERIPHERAL:
3052 seq_printf(s, "peripheral\n");
3053 break;
3054 default:
3055 seq_printf(s, "none\n");
3056 break;
3057 }
3058
3059 return 0;
3060}
3061
3062static int msm_otg_mode_open(struct inode *inode, struct file *file)
3063{
3064 return single_open(file, msm_otg_mode_show, inode->i_private);
3065}
3066
3067static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3068 size_t count, loff_t *ppos)
3069{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303070 struct seq_file *s = file->private_data;
3071 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303072 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003073 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303074 int status = count;
3075 enum usb_mode_type req_mode;
3076
3077 memset(buf, 0x00, sizeof(buf));
3078
3079 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3080 status = -EFAULT;
3081 goto out;
3082 }
3083
3084 if (!strncmp(buf, "host", 4)) {
3085 req_mode = USB_HOST;
3086 } else if (!strncmp(buf, "peripheral", 10)) {
3087 req_mode = USB_PERIPHERAL;
3088 } else if (!strncmp(buf, "none", 4)) {
3089 req_mode = USB_NONE;
3090 } else {
3091 status = -EINVAL;
3092 goto out;
3093 }
3094
3095 switch (req_mode) {
3096 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003097 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303098 case OTG_STATE_A_HOST:
3099 case OTG_STATE_B_PERIPHERAL:
3100 set_bit(ID, &motg->inputs);
3101 clear_bit(B_SESS_VLD, &motg->inputs);
3102 break;
3103 default:
3104 goto out;
3105 }
3106 break;
3107 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003108 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303109 case OTG_STATE_B_IDLE:
3110 case OTG_STATE_A_HOST:
3111 set_bit(ID, &motg->inputs);
3112 set_bit(B_SESS_VLD, &motg->inputs);
3113 break;
3114 default:
3115 goto out;
3116 }
3117 break;
3118 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003119 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303120 case OTG_STATE_B_IDLE:
3121 case OTG_STATE_B_PERIPHERAL:
3122 clear_bit(ID, &motg->inputs);
3123 break;
3124 default:
3125 goto out;
3126 }
3127 break;
3128 default:
3129 goto out;
3130 }
3131
Steve Mucklef132c6c2012-06-06 18:30:57 -07003132 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303133 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303134out:
3135 return status;
3136}
3137
3138const struct file_operations msm_otg_mode_fops = {
3139 .open = msm_otg_mode_open,
3140 .read = seq_read,
3141 .write = msm_otg_mode_write,
3142 .llseek = seq_lseek,
3143 .release = single_release,
3144};
3145
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303146static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3147{
3148 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003149 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303150
Steve Mucklef132c6c2012-06-06 18:30:57 -07003151 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303152 return 0;
3153}
3154
3155static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3156{
3157 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3158}
3159
3160const struct file_operations msm_otg_state_fops = {
3161 .open = msm_otg_otg_state_open,
3162 .read = seq_read,
3163 .llseek = seq_lseek,
3164 .release = single_release,
3165};
3166
Anji jonnalad270e2d2011-08-09 11:28:32 +05303167static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3168{
3169 struct msm_otg *motg = s->private;
3170
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303171 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303172 return 0;
3173}
3174
3175static int msm_otg_chg_open(struct inode *inode, struct file *file)
3176{
3177 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3178}
3179
3180const struct file_operations msm_otg_chg_fops = {
3181 .open = msm_otg_chg_open,
3182 .read = seq_read,
3183 .llseek = seq_lseek,
3184 .release = single_release,
3185};
3186
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303187static int msm_otg_aca_show(struct seq_file *s, void *unused)
3188{
3189 if (debug_aca_enabled)
3190 seq_printf(s, "enabled\n");
3191 else
3192 seq_printf(s, "disabled\n");
3193
3194 return 0;
3195}
3196
3197static int msm_otg_aca_open(struct inode *inode, struct file *file)
3198{
3199 return single_open(file, msm_otg_aca_show, inode->i_private);
3200}
3201
3202static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3203 size_t count, loff_t *ppos)
3204{
3205 char buf[8];
3206
3207 memset(buf, 0x00, sizeof(buf));
3208
3209 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3210 return -EFAULT;
3211
3212 if (!strncmp(buf, "enable", 6))
3213 debug_aca_enabled = true;
3214 else
3215 debug_aca_enabled = false;
3216
3217 return count;
3218}
3219
3220const struct file_operations msm_otg_aca_fops = {
3221 .open = msm_otg_aca_open,
3222 .read = seq_read,
3223 .write = msm_otg_aca_write,
3224 .llseek = seq_lseek,
3225 .release = single_release,
3226};
3227
Manu Gautam8bdcc592012-03-06 11:26:06 +05303228static int msm_otg_bus_show(struct seq_file *s, void *unused)
3229{
3230 if (debug_bus_voting_enabled)
3231 seq_printf(s, "enabled\n");
3232 else
3233 seq_printf(s, "disabled\n");
3234
3235 return 0;
3236}
3237
3238static int msm_otg_bus_open(struct inode *inode, struct file *file)
3239{
3240 return single_open(file, msm_otg_bus_show, inode->i_private);
3241}
3242
3243static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3244 size_t count, loff_t *ppos)
3245{
3246 char buf[8];
3247 int ret;
3248 struct seq_file *s = file->private_data;
3249 struct msm_otg *motg = s->private;
3250
3251 memset(buf, 0x00, sizeof(buf));
3252
3253 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3254 return -EFAULT;
3255
3256 if (!strncmp(buf, "enable", 6)) {
3257 /* Do not vote here. Let OTG statemachine decide when to vote */
3258 debug_bus_voting_enabled = true;
3259 } else {
3260 debug_bus_voting_enabled = false;
3261 if (motg->bus_perf_client) {
3262 ret = msm_bus_scale_client_update_request(
3263 motg->bus_perf_client, 0);
3264 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003265 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303266 "for bus bw %d\n", __func__, ret);
3267 }
3268 }
3269
3270 return count;
3271}
3272
3273const struct file_operations msm_otg_bus_fops = {
3274 .open = msm_otg_bus_open,
3275 .read = seq_read,
3276 .write = msm_otg_bus_write,
3277 .llseek = seq_lseek,
3278 .release = single_release,
3279};
3280
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303281static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303282
3283static int msm_otg_debugfs_init(struct msm_otg *motg)
3284{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303285 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303286
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303287 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3288
3289 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3290 return -ENODEV;
3291
Anji jonnalad270e2d2011-08-09 11:28:32 +05303292 if (motg->pdata->mode == USB_OTG &&
3293 motg->pdata->otg_control == OTG_USER_CONTROL) {
3294
Manu Gautam8bdcc592012-03-06 11:26:06 +05303295 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303296 S_IWUSR, msm_otg_dbg_root, motg,
3297 &msm_otg_mode_fops);
3298
Manu Gautam8bdcc592012-03-06 11:26:06 +05303299 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303300 debugfs_remove(msm_otg_dbg_root);
3301 msm_otg_dbg_root = NULL;
3302 return -ENODEV;
3303 }
3304 }
3305
Manu Gautam8bdcc592012-03-06 11:26:06 +05303306 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303307 msm_otg_dbg_root, motg,
3308 &msm_otg_chg_fops);
3309
Manu Gautam8bdcc592012-03-06 11:26:06 +05303310 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303311 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303312 return -ENODEV;
3313 }
3314
Manu Gautam8bdcc592012-03-06 11:26:06 +05303315 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303316 msm_otg_dbg_root, motg,
3317 &msm_otg_aca_fops);
3318
Manu Gautam8bdcc592012-03-06 11:26:06 +05303319 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303320 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303321 return -ENODEV;
3322 }
3323
Manu Gautam8bdcc592012-03-06 11:26:06 +05303324 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3325 msm_otg_dbg_root, motg,
3326 &msm_otg_bus_fops);
3327
3328 if (!msm_otg_dentry) {
3329 debugfs_remove_recursive(msm_otg_dbg_root);
3330 return -ENODEV;
3331 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303332
3333 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3334 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3335
3336 if (!msm_otg_dentry) {
3337 debugfs_remove_recursive(msm_otg_dbg_root);
3338 return -ENODEV;
3339 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303340 return 0;
3341}
3342
3343static void msm_otg_debugfs_cleanup(void)
3344{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303345 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303346}
3347
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303348static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3349static struct platform_device *msm_otg_add_pdev(
3350 struct platform_device *ofdev, const char *name)
3351{
3352 struct platform_device *pdev;
3353 const struct resource *res = ofdev->resource;
3354 unsigned int num = ofdev->num_resources;
3355 int retval;
3356
3357 pdev = platform_device_alloc(name, -1);
3358 if (!pdev) {
3359 retval = -ENOMEM;
3360 goto error;
3361 }
3362
3363 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3364 pdev->dev.dma_mask = &msm_otg_dma_mask;
3365
3366 if (num) {
3367 retval = platform_device_add_resources(pdev, res, num);
3368 if (retval)
3369 goto error;
3370 }
3371
3372 retval = platform_device_add(pdev);
3373 if (retval)
3374 goto error;
3375
3376 return pdev;
3377
3378error:
3379 platform_device_put(pdev);
3380 return ERR_PTR(retval);
3381}
3382
3383static int msm_otg_setup_devices(struct platform_device *ofdev,
3384 enum usb_mode_type mode, bool init)
3385{
3386 const char *gadget_name = "msm_hsusb";
3387 const char *host_name = "msm_hsusb_host";
3388 static struct platform_device *gadget_pdev;
3389 static struct platform_device *host_pdev;
3390 int retval = 0;
3391
3392 if (!init) {
3393 if (gadget_pdev)
3394 platform_device_unregister(gadget_pdev);
3395 if (host_pdev)
3396 platform_device_unregister(host_pdev);
3397 return 0;
3398 }
3399
3400 switch (mode) {
3401 case USB_OTG:
3402 /* fall through */
3403 case USB_PERIPHERAL:
3404 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3405 if (IS_ERR(gadget_pdev)) {
3406 retval = PTR_ERR(gadget_pdev);
3407 break;
3408 }
3409 if (mode == USB_PERIPHERAL)
3410 break;
3411 /* fall through */
3412 case USB_HOST:
3413 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3414 if (IS_ERR(host_pdev)) {
3415 retval = PTR_ERR(host_pdev);
3416 if (mode == USB_OTG)
3417 platform_device_unregister(gadget_pdev);
3418 }
3419 break;
3420 default:
3421 break;
3422 }
3423
3424 return retval;
3425}
3426
3427struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3428{
3429 struct device_node *node = pdev->dev.of_node;
3430 struct msm_otg_platform_data *pdata;
3431 int len = 0;
3432
3433 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3434 if (!pdata) {
3435 pr_err("unable to allocate platform data\n");
3436 return NULL;
3437 }
3438 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3439 if (len) {
3440 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3441 if (!pdata->phy_init_seq)
3442 return NULL;
3443 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3444 pdata->phy_init_seq,
3445 len/sizeof(*pdata->phy_init_seq));
3446 }
3447 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3448 &pdata->power_budget);
3449 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3450 &pdata->mode);
3451 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3452 &pdata->otg_control);
3453 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3454 &pdata->default_mode);
3455 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3456 &pdata->phy_type);
3457 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3458 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303459 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3460 "qcom,hsusb-otg-disable-reset");
3461
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303462 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303463}
3464
3465static int __init msm_otg_probe(struct platform_device *pdev)
3466{
3467 int ret = 0;
3468 struct resource *res;
3469 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003470 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303471 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303472
3473 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303474
3475 if (pdev->dev.of_node) {
3476 dev_dbg(&pdev->dev, "device tree enabled\n");
3477 pdata = msm_otg_dt_to_pdata(pdev);
3478 if (!pdata)
3479 return -ENOMEM;
3480 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3481 if (ret) {
3482 dev_err(&pdev->dev, "devices setup failed\n");
3483 return ret;
3484 }
3485 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303486 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3487 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303488 } else {
3489 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303490 }
3491
3492 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3493 if (!motg) {
3494 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3495 return -ENOMEM;
3496 }
3497
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003498 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3499 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003500 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3501 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303502 goto free_motg;
3503 }
3504
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003505 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303506 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003507 phy = &motg->phy;
3508 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303509
3510 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303511 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3512 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3513 * PMIC can detect ACA ID_GND and generate an interrupt.
3514 */
3515 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3516 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3517 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003518 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303519 }
3520
Ofir Cohen4da266f2012-01-03 10:19:29 +02003521 /* initialize reset counter */
3522 motg->reset_counter = 0;
3523
Amit Blay02eff132011-09-21 16:46:24 +03003524 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003525 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003526 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003527 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303528
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303529 /*
3530 * Targets on which link uses asynchronous reset methodology,
3531 * free running clock is not required during the reset.
3532 */
Manu Gautam5143b252012-01-05 19:25:23 -08003533 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303534 if (IS_ERR(motg->clk))
3535 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3536 else
3537 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303538
3539 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003540 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303541 * CORE CLK must be running at >55Mhz for correct HSUSB
3542 * operation and USB core cannot tolerate frequency changes on
3543 * CORE CLK. For such USB cores, vote for maximum clk frequency
3544 * on pclk source
3545 */
Manu Gautam5143b252012-01-05 19:25:23 -08003546 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3547 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303548 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003549 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303550 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003551 goto put_clk;
3552 }
3553 clk_set_rate(motg->core_clk, INT_MAX);
3554
3555 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3556 if (IS_ERR(motg->pclk)) {
3557 dev_err(&pdev->dev, "failed to get iface_clk\n");
3558 ret = PTR_ERR(motg->pclk);
3559 goto put_core_clk;
3560 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303561
3562 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3563 if (!res) {
3564 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3565 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003566 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303567 }
3568
3569 motg->regs = ioremap(res->start, resource_size(res));
3570 if (!motg->regs) {
3571 dev_err(&pdev->dev, "ioremap failed\n");
3572 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003573 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303574 }
3575 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3576
3577 motg->irq = platform_get_irq(pdev, 0);
3578 if (!motg->irq) {
3579 dev_err(&pdev->dev, "platform_get_irq failed\n");
3580 ret = -ENODEV;
3581 goto free_regs;
3582 }
3583
Manu Gautamf8c45642012-08-10 10:20:56 -07003584 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3585 if (motg->async_irq < 0) {
3586 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3587 motg->async_irq = 0;
3588 }
3589
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003590 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003591 if (IS_ERR(motg->xo_handle)) {
3592 dev_err(&pdev->dev, "%s not able to get the handle "
3593 "to vote for TCXO D0 buffer\n", __func__);
3594 ret = PTR_ERR(motg->xo_handle);
3595 goto free_regs;
3596 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303597
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003598 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003599 if (ret) {
3600 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3601 "D0 buffer%d\n", __func__, ret);
3602 goto free_xo_handle;
3603 }
3604
Manu Gautam28b1bac2012-01-30 16:43:06 +05303605 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303606
Mayank Rana248698c2012-04-19 00:03:16 +05303607 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003608 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303609 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003610 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303611 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003612 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003613 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303614 goto devote_xo_handle;
3615 }
3616 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303617 }
3618
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003619 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303620 if (ret) {
3621 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303622 goto devote_xo_handle;
3623 }
3624
3625 ret = regulator_enable(hsusb_vddcx);
3626 if (ret) {
3627 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3628 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303629 }
3630
3631 ret = msm_hsusb_ldo_init(motg, 1);
3632 if (ret) {
3633 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303634 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303635 }
3636
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303637 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303638 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3639 "mhl_usb_hs_switch");
3640 if (IS_ERR(mhl_usb_hs_switch)) {
3641 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003642 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303643 goto free_ldo_init;
3644 }
3645 }
3646
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003647 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303648 if (ret) {
3649 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003650 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303651 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303652 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303653
3654 writel(0, USB_USBINTR);
3655 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003656 /* Ensure that above STOREs are completed before enabling interrupts */
3657 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303658
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303659 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3660 if (ret)
3661 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003662 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303663 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303664 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303665 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303666 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
kibum.leeec040442012-08-22 22:29:46 +09003667 INIT_DELAYED_WORK(&motg->check_ta_work, msm_ta_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303668 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3669 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303670 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3671 "msm_otg", motg);
3672 if (ret) {
3673 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003674 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303675 }
3676
Manu Gautamf8c45642012-08-10 10:20:56 -07003677 if (motg->async_irq) {
3678 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3679 "msm_otg", motg);
3680 if (ret) {
3681 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3682 goto free_irq;
3683 }
3684 disable_irq(motg->async_irq);
3685 }
3686
Jack Pham87f202f2012-08-06 00:24:22 -07003687 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3688 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3689
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003690 phy->init = msm_otg_reset;
3691 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003692 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303693
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003694 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303695
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003696 phy->otg->phy = &motg->phy;
3697 phy->otg->set_host = msm_otg_set_host;
3698 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003699 phy->otg->start_hnp = msm_otg_start_hnp;
3700 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003701
3702 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303703 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003704 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003705 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303706 }
3707
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303708 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303709 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003710 if (motg->pdata->pmic_id_irq) {
3711 ret = request_irq(motg->pdata->pmic_id_irq,
3712 msm_pmic_id_irq,
3713 IRQF_TRIGGER_RISING |
3714 IRQF_TRIGGER_FALLING,
3715 "msm_otg", motg);
3716 if (ret) {
3717 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003718 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003719 }
3720 } else {
3721 ret = -ENODEV;
3722 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003723 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003724 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303725 }
3726
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303727 msm_hsusb_mhl_switch_enable(motg, 1);
3728
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303729 platform_set_drvdata(pdev, motg);
3730 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003731 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303732
Anji jonnalad270e2d2011-08-09 11:28:32 +05303733 ret = msm_otg_debugfs_init(motg);
3734 if (ret)
3735 dev_dbg(&pdev->dev, "mode debugfs file is"
3736 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303737
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003738 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3739 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3740
Amit Blay58b31472011-11-18 09:39:39 +02003741 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3742 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303743 (!(motg->pdata->mode == USB_OTG) ||
3744 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003745 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303746 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003747
Amit Blay58b31472011-11-18 09:39:39 +02003748 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3749 motg->caps = ALLOW_PHY_RETENTION;
3750 }
3751
Amit Blay6fa647a2012-05-24 14:12:08 +03003752 if (motg->pdata->enable_lpm_on_dev_suspend)
3753 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3754
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003755 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303756 pm_runtime_set_active(&pdev->dev);
3757 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303758
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303759 if (motg->pdata->bus_scale_table) {
3760 motg->bus_perf_client =
3761 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3762 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003763 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303764 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303765 else
3766 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303767 }
3768
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303769 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003770
Steve Mucklef132c6c2012-06-06 18:30:57 -07003771remove_phy:
3772 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003773free_async_irq:
3774 if (motg->async_irq)
3775 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303776free_irq:
3777 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003778destroy_wlock:
3779 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303780 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003781 msm_hsusb_ldo_enable(motg, 0);
3782free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303783 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303784free_hsusb_vddcx:
3785 regulator_disable(hsusb_vddcx);
3786free_config_vddcx:
3787 regulator_set_voltage(hsusb_vddcx,
3788 vdd_val[motg->vdd_type][VDD_NONE],
3789 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003790devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303791 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003792 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003793free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003794 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303795free_regs:
3796 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003797put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303798 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303799put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003800 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303801put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303802 if (!IS_ERR(motg->clk))
3803 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003804 if (!IS_ERR(motg->phy_reset_clk))
3805 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003806free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003807 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303808free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303809 kfree(motg);
3810 return ret;
3811}
3812
3813static int __devexit msm_otg_remove(struct platform_device *pdev)
3814{
3815 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003816 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303817 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303818
3819 if (otg->host || otg->gadget)
3820 return -EBUSY;
3821
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303822 if (pdev->dev.of_node)
3823 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003824 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3825 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303826 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303827 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303828 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303829 cancel_delayed_work_sync(&motg->pmic_id_status_work);
kibum.leeec040442012-08-22 22:29:46 +09003830 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303831 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303832
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303833 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303834
3835 device_init_wakeup(&pdev->dev, 0);
3836 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003837 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303838
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303839 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003840 if (motg->pdata->pmic_id_irq)
3841 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003842 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303843 free_irq(motg->irq, motg);
3844
Jack Pham87f202f2012-08-06 00:24:22 -07003845 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3846 motg->pdata->mpm_otgsessvld_int)
3847 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3848
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303849 /*
3850 * Put PHY in low power mode.
3851 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003852 ulpi_read(otg->phy, 0x14);
3853 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303854
3855 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3856 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3857 if (readl(USB_PORTSC) & PORTSC_PHCD)
3858 break;
3859 udelay(1);
3860 cnt++;
3861 }
3862 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003863 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303864
Manu Gautam28b1bac2012-01-30 16:43:06 +05303865 clk_disable_unprepare(motg->pclk);
3866 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003867 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003868 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303869 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303870 regulator_disable(hsusb_vddcx);
3871 regulator_set_voltage(hsusb_vddcx,
3872 vdd_val[motg->vdd_type][VDD_NONE],
3873 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303874
3875 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303876 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303877
Amit Blay02eff132011-09-21 16:46:24 +03003878 if (!IS_ERR(motg->phy_reset_clk))
3879 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303880 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303881 if (!IS_ERR(motg->clk))
3882 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003883 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303884
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303885 if (motg->bus_perf_client)
3886 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303887
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003888 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303889 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303890 return 0;
3891}
3892
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303893#ifdef CONFIG_PM_RUNTIME
3894static int msm_otg_runtime_idle(struct device *dev)
3895{
3896 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003897 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303898
3899 dev_dbg(dev, "OTG runtime idle\n");
3900
Steve Mucklef132c6c2012-06-06 18:30:57 -07003901 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303902 return -EAGAIN;
3903 else
3904 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303905}
3906
3907static int msm_otg_runtime_suspend(struct device *dev)
3908{
3909 struct msm_otg *motg = dev_get_drvdata(dev);
3910
3911 dev_dbg(dev, "OTG runtime suspend\n");
3912 return msm_otg_suspend(motg);
3913}
3914
3915static int msm_otg_runtime_resume(struct device *dev)
3916{
3917 struct msm_otg *motg = dev_get_drvdata(dev);
3918
3919 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303920 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303921 return msm_otg_resume(motg);
3922}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303923#endif
3924
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303925#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303926static int msm_otg_pm_suspend(struct device *dev)
3927{
Jack Pham5ca279b2012-05-14 18:42:54 -07003928 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303929 struct msm_otg *motg = dev_get_drvdata(dev);
3930
3931 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003932
3933 atomic_set(&motg->pm_suspended, 1);
3934 ret = msm_otg_suspend(motg);
3935 if (ret)
3936 atomic_set(&motg->pm_suspended, 0);
3937
3938 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303939}
3940
3941static int msm_otg_pm_resume(struct device *dev)
3942{
Jack Pham5ca279b2012-05-14 18:42:54 -07003943 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303944 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303945
3946 dev_dbg(dev, "OTG PM resume\n");
3947
Jack Pham5ca279b2012-05-14 18:42:54 -07003948 atomic_set(&motg->pm_suspended, 0);
Jack Phamb1aa4d82012-08-13 15:32:39 -07003949 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003950 pm_runtime_get_noresume(dev);
3951 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303952
Jack Pham5ca279b2012-05-14 18:42:54 -07003953 /* Update runtime PM status */
3954 pm_runtime_disable(dev);
3955 pm_runtime_set_active(dev);
3956 pm_runtime_enable(dev);
3957
Jack Phamb1aa4d82012-08-13 15:32:39 -07003958 if (motg->sm_work_pending) {
3959 motg->sm_work_pending = false;
3960 queue_work(system_nrt_wq, &motg->sm_work);
3961 }
Jack Pham5ca279b2012-05-14 18:42:54 -07003962 }
3963
3964 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303965}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303966#endif
3967
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303968#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303969static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303970 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3971 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3972 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303973};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303974#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303975
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303976static struct of_device_id msm_otg_dt_match[] = {
3977 { .compatible = "qcom,hsusb-otg",
3978 },
3979 {}
3980};
3981
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303982static struct platform_driver msm_otg_driver = {
3983 .remove = __devexit_p(msm_otg_remove),
3984 .driver = {
3985 .name = DRIVER_NAME,
3986 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303987#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303988 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303989#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303990 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303991 },
3992};
3993
3994static int __init msm_otg_init(void)
3995{
3996 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3997}
3998
3999static void __exit msm_otg_exit(void)
4000{
4001 platform_driver_unregister(&msm_otg_driver);
4002}
4003
4004module_init(msm_otg_init);
4005module_exit(msm_otg_exit);
4006
4007MODULE_LICENSE("GPL v2");
4008MODULE_DESCRIPTION("MSM USB transceiver driver");