blob: a86e1ae09acb0d10dd78675272817e2a7618230a [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
767 /* charging detection in progress due to cable plug-in */
768 if (test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
769 !dcp) {
770 enable_irq(motg->irq);
771 return -EBUSY;
772 }
773
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530774 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530775 * Chipidea 45-nm PHY suspend sequence:
776 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530777 * Interrupt Latch Register auto-clear feature is not present
778 * in all PHY versions. Latch register is clear on read type.
779 * Clear latch register to avoid spurious wakeup from
780 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530781 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530782 * PHY comparators are disabled when PHY enters into low power
783 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
784 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
785 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530786 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530787 * PLL is not turned off when PHY enters into low power mode (LPM).
788 * Disable PLL for maximum power savings.
789 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530790
791 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200792 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530793 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200794 ulpi_write(phy, 0x01, 0x30);
795 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530796 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530797
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700798
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530799 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530800 * PHY may take some time or even fail to enter into low power
801 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
802 * in failure case.
803 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530804 portsc = readl_relaxed(USB_PORTSC);
805 if (!(portsc & PORTSC_PHCD)) {
806 writel_relaxed(portsc | PORTSC_PHCD,
807 USB_PORTSC);
808 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
809 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
810 break;
811 udelay(1);
812 cnt++;
813 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530814 }
815
816 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200817 dev_err(phy->dev, "Unable to suspend PHY\n");
818 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530819 enable_irq(motg->irq);
820 return -ETIMEDOUT;
821 }
822
823 /*
824 * PHY has capability to generate interrupt asynchronously in low
825 * power mode (LPM). This interrupt is level triggered. So USB IRQ
826 * line must be disabled till async interrupt enable bit is cleared
827 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
828 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530829 *
830 * PHY retention mode is disallowed while entering to LPM with wall
831 * charger connected. But PHY is put into suspend mode. Hence
832 * enable asynchronous interrupt to detect charger disconnection when
833 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530834 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530835 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300836 if (host_bus_suspend || device_bus_suspend ||
837 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530838 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
839 else
840 cmd_val |= ULPI_STP_CTRL;
841 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530842
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530843 /*
844 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
845 * PHY retention and collapse can not happen with VDP_SRC enabled.
846 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300847 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
848 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200849 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
850 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
851 /* Enable PHY HV interrupts to wake MPM/Link */
852 phy_ctrl_val |=
853 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530854
Amit Blay58b31472011-11-18 09:39:39 +0200855 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530857 }
858
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700859 /* Ensure that above operation is completed before turning off clocks */
860 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300861 /* Consider clocks on workaround flag only in case of bus suspend */
862 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
863 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
864 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300865 clk_disable_unprepare(motg->pclk);
866 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300867 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300868 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530869
Anji jonnala7da3f262011-12-02 17:22:14 -0800870 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530871 if (!host_bus_suspend) {
872 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
873 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700874 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530875 "TCXO D0 buffer%d\n", __func__, ret);
876 else
877 motg->lpm_flags |= XO_SHUTDOWN;
878 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800879
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530880 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
881 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 msm_hsusb_ldo_enable(motg, 0);
883 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530884 }
885
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530886 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530888 msm_hsusb_mhl_switch_enable(motg, 0);
889 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890
Steve Mucklef132c6c2012-06-06 18:30:57 -0700891 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530892 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700893 if (motg->async_irq)
894 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895 if (motg->pdata->pmic_id_irq)
896 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700897 if (pdata->otg_control == OTG_PHY_CONTROL &&
898 pdata->mpm_otgsessvld_int)
899 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530901 if (bus)
902 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
903
904 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -0700905 /* Enable ASYNC IRQ (if present) during LPM */
906 if (motg->async_irq)
907 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530908 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530910
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200911 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530912
913 return 0;
914}
915
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530916static int msm_otg_resume(struct msm_otg *motg)
917{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200918 struct usb_phy *phy = &motg->phy;
919 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700920 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530921 int cnt = 0;
922 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200923 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800924 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530925
926 if (!atomic_read(&motg->in_lpm))
927 return 0;
928
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530930
Anji jonnala7da3f262011-12-02 17:22:14 -0800931 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530932 if (motg->lpm_flags & XO_SHUTDOWN) {
933 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
934 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700935 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530936 "TCXO D0 buffer%d\n", __func__, ret);
937 motg->lpm_flags &= ~XO_SHUTDOWN;
938 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530939
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300940 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300941 clk_prepare_enable(motg->core_clk);
942 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300943 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300944 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530945
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
947 msm_hsusb_ldo_enable(motg, 1);
948 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
949 }
950
951 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530952 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530953 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200954 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
955 phy_ctrl_val |= PHY_RETEN;
956 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
957 /* Disable PHY HV interrupts */
958 phy_ctrl_val &=
959 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
960 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700961 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530962 }
963
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964 temp = readl(USB_USBCMD);
965 temp &= ~ASYNC_INTR_CTRL;
966 temp &= ~ULPI_STP_CTRL;
967 writel(temp, USB_USBCMD);
968
969 /*
970 * PHY comes out of low power mode (LPM) in case of wakeup
971 * from asynchronous interrupt.
972 */
973 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
974 goto skip_phy_resume;
975
976 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
977 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
978 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
979 break;
980 udelay(1);
981 cnt++;
982 }
983
984 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
985 /*
986 * This is a fatal error. Reset the link and
987 * PHY. USB state can not be restored. Re-insertion
988 * of USB cable is the only way to get USB working.
989 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200990 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530991 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200992 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530993 }
994
995skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700996 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530997 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700998 if (motg->async_irq)
999 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000 if (motg->pdata->pmic_id_irq)
1001 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001002 if (pdata->otg_control == OTG_PHY_CONTROL &&
1003 pdata->mpm_otgsessvld_int)
1004 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301006 if (bus)
1007 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1008
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301009 atomic_set(&motg->in_lpm, 0);
1010
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301011 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001012 /* Match the disable_irq call from ISR */
1013 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301014 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301015 }
1016
Manu Gautamf8c45642012-08-10 10:20:56 -07001017 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1018 if (motg->async_irq)
1019 disable_irq(motg->async_irq);
1020
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001021 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301022
1023 return 0;
1024}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301025#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301026
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001027static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1028{
1029 if (!psy)
1030 goto psy_not_supported;
1031
1032 if (host_mode)
1033 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1034 else
1035 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1036
1037psy_not_supported:
1038 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1039 return -ENXIO;
1040}
1041
David Keitel081a3e22012-04-18 12:37:07 -07001042static int msm_otg_notify_chg_type(struct msm_otg *motg)
1043{
roy.parkd063ef32012-08-30 09:37:38 -07001044 int charger_type;
David Keitel081a3e22012-04-18 12:37:07 -07001045 /*
1046 * TODO
1047 * Unify OTG driver charger types and power supply charger types
1048 */
David Keitel081a3e22012-04-18 12:37:07 -07001049
1050 if (motg->chg_type == USB_SDP_CHARGER)
1051 charger_type = POWER_SUPPLY_TYPE_USB;
1052 else if (motg->chg_type == USB_CDP_CHARGER)
1053 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301054 else if (motg->chg_type == USB_DCP_CHARGER ||
1055 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001056 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1057 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1058 motg->chg_type == USB_ACA_A_CHARGER ||
1059 motg->chg_type == USB_ACA_B_CHARGER ||
1060 motg->chg_type == USB_ACA_C_CHARGER))
1061 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1062 else
1063 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1064
1065 return pm8921_set_usb_power_supply_type(charger_type);
1066}
1067
Amit Blay0f7edf72012-01-15 10:11:27 +02001068static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1069{
Amit Blay0f7edf72012-01-15 10:11:27 +02001070
Amit Blay0f7edf72012-01-15 10:11:27 +02001071 if (!psy)
1072 goto psy_not_supported;
1073
1074 if (motg->cur_power == 0 && mA > 0) {
1075 /* Enable charging */
1076 if (power_supply_set_online(psy, true))
1077 goto psy_not_supported;
1078 } else if (motg->cur_power > 0 && mA == 0) {
1079 /* Disable charging */
1080 if (power_supply_set_online(psy, false))
1081 goto psy_not_supported;
1082 return 0;
1083 }
1084 /* Set max current limit */
Devin Kim81265542012-09-01 05:06:22 -07001085 dev_info(motg->phy.dev, "current: %d -> %d (mA)\n",
1086 motg->cur_power, mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001087 if (power_supply_set_current_limit(psy, 1000*mA))
1088 goto psy_not_supported;
1089
1090 return 0;
1091
1092psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001093 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001094 return -ENXIO;
1095}
1096
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301097static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1098{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001099 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301100
1101 if (g && g->is_a_peripheral)
1102 return;
1103
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301104 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1105 motg->chg_type == USB_ACA_A_CHARGER ||
1106 motg->chg_type == USB_ACA_B_CHARGER ||
1107 motg->chg_type == USB_ACA_C_CHARGER) &&
1108 mA > IDEV_ACA_CHG_LIMIT)
1109 mA = IDEV_ACA_CHG_LIMIT;
1110
David Keitel081a3e22012-04-18 12:37:07 -07001111 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001112 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001113 "Failed notifying %d charger type to PMIC\n",
1114 motg->chg_type);
1115
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301116 if (motg->cur_power == mA)
1117 return;
1118
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001119 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001120
Devin Kim2073afb2012-09-05 13:01:51 -07001121 pm8921_charger_vbus_draw(mA);
1122 msm_otg_notify_power_supply(motg, mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001123
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301124 motg->cur_power = mA;
1125}
1126
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001127static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301128{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001129 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301130
1131 /*
1132 * Gadget driver uses set_power method to notify about the
1133 * available current based on suspend/configured states.
1134 *
1135 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1136 * states when CDP/ACA is connected.
1137 */
1138 if (motg->chg_type == USB_SDP_CHARGER)
1139 msm_otg_notify_charger(motg, mA);
1140
1141 return 0;
1142}
1143
Steve Mucklef132c6c2012-06-06 18:30:57 -07001144static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301145{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001146 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301147 struct msm_otg_platform_data *pdata = motg->pdata;
1148 struct usb_hcd *hcd;
1149
1150 if (!otg->host)
1151 return;
1152
1153 hcd = bus_to_hcd(otg->host);
1154
1155 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001156 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301157
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301158 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001159 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301160 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1161
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301162 /*
1163 * Some boards have a switch cotrolled by gpio
1164 * to enable/disable internal HUB. Enable internal
1165 * HUB before kicking the host.
1166 */
1167 if (pdata->setup_gpio)
1168 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301169 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301170 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001171 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301172
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301173 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301174 /* HCD core reset all bits of PORTSC. select ULPI phy */
1175 writel_relaxed(0x80000000, USB_PORTSC);
1176
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301177 if (pdata->setup_gpio)
1178 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301179
1180 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001181 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301182 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301183 }
1184}
1185
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186static int msm_otg_usbdev_notify(struct notifier_block *self,
1187 unsigned long action, void *priv)
1188{
1189 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001190 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301191 struct usb_device *udev = priv;
1192
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301193 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1194 goto out;
1195
Steve Mucklef132c6c2012-06-06 18:30:57 -07001196 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301197 goto out;
1198 /*
1199 * Interested in devices connected directly to the root hub.
1200 * ACA dock can supply IDEV_CHG irrespective devices connected
1201 * on the accessory port.
1202 */
1203 if (!udev->parent || udev->parent->parent ||
1204 motg->chg_type == USB_ACA_DOCK_CHARGER)
1205 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001206
1207 switch (action) {
1208 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301209 if (aca_enabled())
1210 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001211 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301212 pr_debug("B_CONN set\n");
1213 set_bit(B_CONN, &motg->inputs);
1214 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001215 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301216 /*
1217 * OTG PET: A-device must end session within
1218 * 10 sec after PET enumeration.
1219 */
1220 if (udev->quirks & USB_QUIRK_OTG_PET)
1221 msm_otg_start_timer(motg, TA_TST_MAINT,
1222 A_TST_MAINT);
1223 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301224 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226 if (udev->actconfig)
1227 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1228 else
1229 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001230 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301231 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301232 break;
1233 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001234 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1235 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301236 pr_debug("B_CONN clear\n");
1237 clear_bit(B_CONN, &motg->inputs);
1238 /*
1239 * OTG PET: A-device must end session after
1240 * PET disconnection if it is enumerated
1241 * with bcdDevice[0] = 1. USB core sets
1242 * bus->otg_vbus_off for us. clear it here.
1243 */
1244 if (udev->bus->otg_vbus_off) {
1245 udev->bus->otg_vbus_off = 0;
1246 set_bit(A_BUS_DROP, &motg->inputs);
1247 }
1248 queue_work(system_nrt_wq, &motg->sm_work);
1249 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001250 default:
1251 break;
1252 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301253 if (test_bit(ID_A, &motg->inputs))
1254 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1255 motg->mA_port);
1256out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001257 return NOTIFY_OK;
1258}
1259
Mayank Ranae3926882011-12-26 09:47:54 +05301260static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1261{
1262 int ret;
1263 static bool vbus_is_on;
1264
1265 if (vbus_is_on == on)
1266 return;
1267
1268 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301269 ret = motg->pdata->vbus_power(on);
1270 if (!ret)
1271 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301272 return;
1273 }
1274
1275 if (!vbus_otg) {
1276 pr_err("vbus_otg is NULL.");
1277 return;
1278 }
1279
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001280 /*
1281 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001282 * from usb before turning on the boost.
1283 * if exiting host mode disable the boost before enabling to draw
1284 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001285 */
Mayank Ranae3926882011-12-26 09:47:54 +05301286 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001287 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301288 ret = regulator_enable(vbus_otg);
1289 if (ret) {
1290 pr_err("unable to enable vbus_otg\n");
1291 return;
1292 }
1293 vbus_is_on = true;
1294 } else {
1295 ret = regulator_disable(vbus_otg);
1296 if (ret) {
1297 pr_err("unable to disable vbus_otg\n");
1298 return;
1299 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001300 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301301 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301302 }
1303}
1304
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001305static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301306{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001307 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301308 struct usb_hcd *hcd;
1309
1310 /*
1311 * Fail host registration if this board can support
1312 * only peripheral configuration.
1313 */
1314 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001315 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301316 return -ENODEV;
1317 }
1318
Devin Kimd10f03c2012-06-25 17:05:13 -07001319 if (!machine_is_apq8064_mako()) {
1320 if (!motg->pdata->vbus_power && host) {
1321 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1322 if (IS_ERR(vbus_otg)) {
1323 pr_err("Unable to get vbus_otg\n");
1324 return -ENODEV;
1325 }
Mayank Ranae3926882011-12-26 09:47:54 +05301326 }
1327 }
1328
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301329 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001330 if (otg->phy->state == OTG_STATE_A_HOST) {
1331 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001332 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301333 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301334 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301335 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001336 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301337 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301338 } else {
1339 otg->host = NULL;
1340 }
1341
1342 return 0;
1343 }
1344
1345 hcd = bus_to_hcd(host);
1346 hcd->power_budget = motg->pdata->power_budget;
1347
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301348#ifdef CONFIG_USB_OTG
1349 host->otg_port = 1;
1350#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001351 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1352 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301353 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001354 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301355
1356 /*
1357 * Kick the state machine work, if peripheral is not supported
1358 * or peripheral is already registered with us.
1359 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301360 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001361 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301362 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301363 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301364
1365 return 0;
1366}
1367
Steve Mucklef132c6c2012-06-06 18:30:57 -07001368static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301369{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301370 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001371 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301372 struct msm_otg_platform_data *pdata = motg->pdata;
1373
1374 if (!otg->gadget)
1375 return;
1376
1377 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001378 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301379 /*
1380 * Some boards have a switch cotrolled by gpio
1381 * to enable/disable internal HUB. Disable internal
1382 * HUB before kicking the gadget.
1383 */
1384 if (pdata->setup_gpio)
1385 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001386
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301387 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301388 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301389 ret = msm_bus_scale_client_update_request(
1390 motg->bus_perf_client, 1);
1391 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001392 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301393 "bus bandwidth %d\n", __func__, ret);
1394 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301395 usb_gadget_vbus_connect(otg->gadget);
1396 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001397 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301398 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301399 /* Configure BUS performance parameters to default */
1400 if (motg->bus_perf_client) {
1401 ret = msm_bus_scale_client_update_request(
1402 motg->bus_perf_client, 0);
1403 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001404 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301405 "for bus bw %d\n", __func__, ret);
1406 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301407 if (pdata->setup_gpio)
1408 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1409 }
1410
1411}
1412
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001413static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301414 struct usb_gadget *gadget)
1415{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001416 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301417
1418 /*
1419 * Fail peripheral registration if this board can support
1420 * only host configuration.
1421 */
1422 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001423 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301424 return -ENODEV;
1425 }
1426
1427 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001428 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1429 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301430 msm_otg_start_peripheral(otg, 0);
1431 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001432 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301433 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301434 } else {
1435 otg->gadget = NULL;
1436 }
1437
1438 return 0;
1439 }
1440 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001441 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301442
1443 /*
1444 * Kick the state machine work, if host is not supported
1445 * or host is already registered with us.
1446 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301447 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001448 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301449 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301450 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301451
1452 return 0;
1453}
1454
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301455static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1456 void (*callback)(int on))
1457{
1458 struct usb_phy *phy = &motg->phy;
1459 int ret;
1460
1461 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1462 !motg->pdata->pmic_id_irq) {
1463 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1464 return -ENODEV;
1465 }
1466
1467 if (!motg->pdata->mhl_dev_name) {
1468 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1469 return -ENODEV;
1470 }
1471
1472 if (callback)
1473 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1474 callback);
1475 else
1476 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1477
1478 if (ret)
1479 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1480 motg->pdata->mhl_dev_name, ret);
1481 else
1482 motg->mhl_enabled = true;
1483
1484 return ret;
1485}
1486
1487static void msm_otg_mhl_notify_online(int on)
1488{
1489 struct msm_otg *motg = the_msm_otg;
1490 struct usb_phy *phy = &motg->phy;
1491 bool queue = false;
1492
1493 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1494
1495 if (on) {
1496 set_bit(MHL, &motg->inputs);
1497 } else {
1498 clear_bit(MHL, &motg->inputs);
1499 queue = true;
1500 }
1501
1502 if (queue && phy->state != OTG_STATE_UNDEFINED)
1503 schedule_work(&motg->sm_work);
1504}
1505
1506static bool msm_otg_is_mhl(struct msm_otg *motg)
1507{
1508 struct usb_phy *phy = &motg->phy;
1509 int is_mhl, ret;
1510
1511 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1512 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1513 /*
1514 * MHL driver calls our callback saying that MHL connected
1515 * if RID_GND is detected. But at later part of discovery
1516 * it may figure out MHL is not connected and returns
1517 * false. Hence clear MHL input here.
1518 */
1519 clear_bit(MHL, &motg->inputs);
1520 dev_dbg(phy->dev, "MHL device not found\n");
1521 return false;
1522 }
1523
1524 set_bit(MHL, &motg->inputs);
1525 dev_dbg(phy->dev, "MHL device found\n");
1526 return true;
1527}
1528
1529static bool msm_chg_mhl_detect(struct msm_otg *motg)
1530{
1531 bool ret, id;
1532 unsigned long flags;
1533
1534 if (!motg->mhl_enabled)
1535 return false;
1536
1537 local_irq_save(flags);
1538 id = irq_read_line(motg->pdata->pmic_id_irq);
1539 local_irq_restore(flags);
1540
1541 if (id)
1542 return false;
1543
1544 mhl_det_in_progress = true;
1545 ret = msm_otg_is_mhl(motg);
1546 mhl_det_in_progress = false;
1547
1548 return ret;
1549}
1550
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001551static bool msm_chg_aca_detect(struct msm_otg *motg)
1552{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001553 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554 u32 int_sts;
1555 bool ret = false;
1556
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301557 if (!aca_enabled())
1558 goto out;
1559
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001560 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1561 goto out;
1562
Steve Mucklef132c6c2012-06-06 18:30:57 -07001563 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001564 switch (int_sts & 0x1C) {
1565 case 0x08:
1566 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001567 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001568 motg->chg_type = USB_ACA_A_CHARGER;
1569 motg->chg_state = USB_CHG_STATE_DETECTED;
1570 clear_bit(ID_B, &motg->inputs);
1571 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301572 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001573 ret = true;
1574 }
1575 break;
1576 case 0x0C:
1577 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001578 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001579 motg->chg_type = USB_ACA_B_CHARGER;
1580 motg->chg_state = USB_CHG_STATE_DETECTED;
1581 clear_bit(ID_A, &motg->inputs);
1582 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301583 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001584 ret = true;
1585 }
1586 break;
1587 case 0x10:
1588 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001589 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001590 motg->chg_type = USB_ACA_C_CHARGER;
1591 motg->chg_state = USB_CHG_STATE_DETECTED;
1592 clear_bit(ID_A, &motg->inputs);
1593 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301594 set_bit(ID, &motg->inputs);
1595 ret = true;
1596 }
1597 break;
1598 case 0x04:
1599 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001600 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301601 motg->chg_type = USB_INVALID_CHARGER;
1602 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1603 clear_bit(ID_A, &motg->inputs);
1604 clear_bit(ID_B, &motg->inputs);
1605 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001606 ret = true;
1607 }
1608 break;
1609 default:
1610 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1611 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301612 test_and_clear_bit(ID_C, &motg->inputs) |
1613 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001614 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001615 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001616 motg->chg_type = USB_INVALID_CHARGER;
1617 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1618 }
1619 }
1620out:
1621 return ret;
1622}
1623
1624static void msm_chg_enable_aca_det(struct msm_otg *motg)
1625{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001626 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001627
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301628 if (!aca_enabled())
1629 return;
1630
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631 switch (motg->pdata->phy_type) {
1632 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301633 /* Disable ID_GND in link and PHY */
1634 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1635 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001636 ulpi_write(phy, 0x01, 0x0C);
1637 ulpi_write(phy, 0x10, 0x0F);
1638 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301639 /* Disable PMIC ID pull-up */
1640 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301641 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001642 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301643 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001644 break;
1645 default:
1646 break;
1647 }
1648}
1649
1650static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1651{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001652 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001653
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301654 if (!aca_enabled())
1655 return;
1656
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001657 switch (motg->pdata->phy_type) {
1658 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301659 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001660 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301661 break;
1662 default:
1663 break;
1664 }
1665}
1666
1667static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1668{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001669 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301670
1671 if (!aca_enabled())
1672 return;
1673
1674 switch (motg->pdata->phy_type) {
1675 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001676 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001677 break;
1678 default:
1679 break;
1680 }
1681}
1682
1683static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1684{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001685 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001686 bool ret = false;
1687
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301688 if (!aca_enabled())
1689 return ret;
1690
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001691 switch (motg->pdata->phy_type) {
1692 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001693 if (ulpi_read(phy, 0x91) & 1) {
1694 dev_dbg(phy->dev, "RID change\n");
1695 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696 ret = msm_chg_aca_detect(motg);
1697 }
1698 default:
1699 break;
1700 }
1701 return ret;
1702}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301703
1704static void msm_otg_id_timer_func(unsigned long data)
1705{
1706 struct msm_otg *motg = (struct msm_otg *) data;
1707
1708 if (!aca_enabled())
1709 return;
1710
1711 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001712 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301713 return;
1714 }
1715
Steve Mucklef132c6c2012-06-06 18:30:57 -07001716 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301717 goto out;
1718
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301719 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001720 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301721 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301722 }
1723
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301724out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301725 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1726 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1727}
1728
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301729static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1730{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001731 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301732 u32 chg_det;
1733 bool ret = false;
1734
1735 switch (motg->pdata->phy_type) {
1736 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001737 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301738 ret = chg_det & (1 << 4);
1739 break;
1740 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001741 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301742 ret = chg_det & 1;
1743 break;
1744 default:
1745 break;
1746 }
1747 return ret;
1748}
1749
1750static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1751{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001752 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301753 u32 chg_det;
1754
1755 switch (motg->pdata->phy_type) {
1756 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001757 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301758 /* Turn off charger block */
1759 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001760 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301761 udelay(20);
1762 /* control chg block via ULPI */
1763 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001764 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301765 /* put it in host mode for enabling D- source */
1766 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001767 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301768 /* Turn on chg detect block */
1769 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001770 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301771 udelay(20);
1772 /* enable chg detection */
1773 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001774 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301775 break;
1776 case SNPS_28NM_INTEGRATED_PHY:
1777 /*
1778 * Configure DM as current source, DP as current sink
1779 * and enable battery charging comparators.
1780 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001781 ulpi_write(phy, 0x8, 0x85);
1782 ulpi_write(phy, 0x2, 0x85);
1783 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301784 break;
1785 default:
1786 break;
1787 }
1788}
1789
1790static bool msm_chg_check_primary_det(struct msm_otg *motg)
1791{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001792 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301793 u32 chg_det;
1794 bool ret = false;
1795
1796 switch (motg->pdata->phy_type) {
1797 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001798 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301799 ret = chg_det & (1 << 4);
1800 break;
1801 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001802 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301803 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301804 /* Turn off VDP_SRC */
1805 ulpi_write(phy, 0x3, 0x86);
1806 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301807 break;
1808 default:
1809 break;
1810 }
1811 return ret;
1812}
1813
1814static void msm_chg_enable_primary_det(struct msm_otg *motg)
1815{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001816 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301817 u32 chg_det;
1818
1819 switch (motg->pdata->phy_type) {
1820 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001821 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301822 /* enable chg detection */
1823 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001824 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301825 break;
1826 case SNPS_28NM_INTEGRATED_PHY:
1827 /*
1828 * Configure DP as current source, DM as current sink
1829 * and enable battery charging comparators.
1830 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001831 ulpi_write(phy, 0x2, 0x85);
1832 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301833 break;
1834 default:
1835 break;
1836 }
1837}
1838
1839static bool msm_chg_check_dcd(struct msm_otg *motg)
1840{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001841 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301842 u32 line_state;
1843 bool ret = false;
1844
1845 switch (motg->pdata->phy_type) {
1846 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001847 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301848 ret = !(line_state & 1);
1849 break;
1850 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001851 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301852 ret = line_state & 2;
1853 break;
1854 default:
1855 break;
1856 }
1857 return ret;
1858}
1859
1860static void msm_chg_disable_dcd(struct msm_otg *motg)
1861{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001862 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301863 u32 chg_det;
1864
1865 switch (motg->pdata->phy_type) {
1866 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001867 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301868 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001869 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301870 break;
1871 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001872 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301873 break;
1874 default:
1875 break;
1876 }
1877}
1878
1879static void msm_chg_enable_dcd(struct msm_otg *motg)
1880{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001881 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301882 u32 chg_det;
1883
1884 switch (motg->pdata->phy_type) {
1885 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001886 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301887 /* Turn on D+ current source */
1888 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001889 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301890 break;
1891 case SNPS_28NM_INTEGRATED_PHY:
1892 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001893 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301894 break;
1895 default:
1896 break;
1897 }
1898}
1899
1900static void msm_chg_block_on(struct msm_otg *motg)
1901{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001902 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301903 u32 func_ctrl, chg_det;
1904
1905 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001906 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301907 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1908 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001909 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301910
1911 switch (motg->pdata->phy_type) {
1912 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001913 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301914 /* control chg block via ULPI */
1915 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001916 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301917 /* Turn on chg detect block */
1918 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001919 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301920 udelay(20);
1921 break;
1922 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05301923 /* disable DP and DM pull down resistors */
1924 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301925 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001926 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301927 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001928 ulpi_write(phy, 0x1F, 0x92);
1929 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301930 udelay(100);
1931 break;
1932 default:
1933 break;
1934 }
1935}
1936
1937static void msm_chg_block_off(struct msm_otg *motg)
1938{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001939 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301940 u32 func_ctrl, chg_det;
1941
1942 switch (motg->pdata->phy_type) {
1943 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001944 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301945 /* Turn off charger block */
1946 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001947 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301948 break;
1949 case SNPS_28NM_INTEGRATED_PHY:
1950 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001951 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301952 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001953 ulpi_write(phy, 0x1F, 0x92);
1954 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301955 break;
1956 default:
1957 break;
1958 }
1959
1960 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001961 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301962 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1963 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001964 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301965}
1966
Anji jonnalad270e2d2011-08-09 11:28:32 +05301967static const char *chg_to_string(enum usb_chg_type chg_type)
1968{
1969 switch (chg_type) {
1970 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1971 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1972 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1973 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1974 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1975 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1976 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301977 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05301978 default: return "INVALID_CHARGER";
1979 }
1980}
kibum.leeec040442012-08-22 22:29:46 +09001981
Devin Kimd93ff032012-09-14 15:01:31 -07001982#define MSM_CHECK_TA_DELAY (5 * HZ)
1983#define PORTSC_LS (3 << 10) /* Read - Port's Line status */
kibum.leeec040442012-08-22 22:29:46 +09001984static void msm_ta_detect_work(struct work_struct *w)
1985{
Devin Kimd93ff032012-09-14 15:01:31 -07001986 struct msm_otg *motg = container_of(w, struct msm_otg, check_ta_work.work);
kibum.leeec040442012-08-22 22:29:46 +09001987 struct usb_otg *otg = motg->phy.otg;
kibum.leeec040442012-08-22 22:29:46 +09001988
Devin Kimd93ff032012-09-14 15:01:31 -07001989 pr_info("msm_ta_detect_work: ta detection work\n");
1990
1991 /* Presence of FRame Index or FRINDEX rollover implies USB communication */
1992 if( (readl(USB_FRINDEX) != 0) || ( readl(USB_USBSTS) & (1<<3) ) ) {
1993 pr_info("msm_ta_detect_work: USB exit ta detection - frindex\n");
1994 return;
1995 }
1996
1997 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
1998 pr_info("msm_ta_detect_work: ta dectection success\n");
kibum.leeec040442012-08-22 22:29:46 +09001999 /* inform to user space that SDP is no longer detected */
2000 msm_otg_notify_charger(motg, 0);
2001 motg->chg_state = USB_CHG_STATE_DETECTED;
2002 motg->chg_type = USB_DCP_CHARGER;
2003 motg->cur_power = 0;
2004 msm_otg_start_peripheral(otg, 0);
2005 otg->phy->state = OTG_STATE_B_IDLE;
2006 schedule_work(&motg->sm_work);
2007 return;
kibum.leeec040442012-08-22 22:29:46 +09002008 }
Devin Kimd93ff032012-09-14 15:01:31 -07002009 schedule_delayed_work(&motg->check_ta_work, MSM_CHECK_TA_DELAY);
kibum.leeec040442012-08-22 22:29:46 +09002010}
Anji jonnalad270e2d2011-08-09 11:28:32 +05302011
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302012#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
2013#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302014#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2015#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302016static void msm_chg_detect_work(struct work_struct *w)
2017{
2018 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002019 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302020 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302021 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302022 unsigned long delay;
2023
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002024 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302025
2026 if (test_bit(MHL, &motg->inputs)) {
2027 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2028 return;
2029 }
2030
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302031 switch (motg->chg_state) {
2032 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302033 msm_chg_block_on(motg);
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302034 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302036 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2037 motg->dcd_retries = 0;
2038 delay = MSM_CHG_DCD_POLL_TIME;
2039 break;
2040 case USB_CHG_STATE_WAIT_FOR_DCD:
jh.koo78df86f2012-09-04 20:13:19 +09002041 if (slimport_is_connected()) {
2042 msm_chg_block_off(motg);
2043 motg->chg_state = USB_CHG_STATE_DETECTED;
2044 motg->chg_type = USB_SDP_CHARGER;
2045 queue_work(system_nrt_wq, &motg->sm_work);
2046 return;
2047 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302048 if (msm_chg_mhl_detect(motg)) {
2049 msm_chg_block_off(motg);
2050 motg->chg_state = USB_CHG_STATE_DETECTED;
2051 motg->chg_type = USB_INVALID_CHARGER;
2052 queue_work(system_nrt_wq, &motg->sm_work);
2053 return;
2054 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002055 is_aca = msm_chg_aca_detect(motg);
2056 if (is_aca) {
2057 /*
2058 * ID_A can be ACA dock too. continue
2059 * primary detection after DCD.
2060 */
2061 if (test_bit(ID_A, &motg->inputs)) {
2062 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2063 } else {
2064 delay = 0;
2065 break;
2066 }
2067 }
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302068 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302069 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2070 if (is_dcd || tmout) {
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302071 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302072 msm_chg_enable_primary_det(motg);
2073 delay = MSM_CHG_PRIMARY_DET_TIME;
2074 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2075 } else {
2076 delay = MSM_CHG_DCD_POLL_TIME;
2077 }
2078 break;
2079 case USB_CHG_STATE_DCD_DONE:
2080 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302081 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2082 dm_vlgc = line_state & PORTSC_LS_DM;
2083 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302084 if (test_bit(ID_A, &motg->inputs)) {
2085 motg->chg_type = USB_ACA_DOCK_CHARGER;
2086 motg->chg_state = USB_CHG_STATE_DETECTED;
2087 delay = 0;
2088 break;
2089 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302090 if (line_state) { /* DP > VLGC */
2091 motg->chg_type = USB_PROPRIETARY_CHARGER;
2092 motg->chg_state = USB_CHG_STATE_DETECTED;
2093 delay = 0;
2094 } else {
2095 msm_chg_enable_secondary_det(motg);
2096 delay = MSM_CHG_SECONDARY_DET_TIME;
2097 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2098 }
2099 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302100 if (test_bit(ID_A, &motg->inputs)) {
2101 motg->chg_type = USB_ACA_A_CHARGER;
2102 motg->chg_state = USB_CHG_STATE_DETECTED;
2103 delay = 0;
2104 break;
2105 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302106
2107 if (line_state) /* DP > VLGC or/and DM > VLGC */
2108 motg->chg_type = USB_PROPRIETARY_CHARGER;
2109 else
2110 motg->chg_type = USB_SDP_CHARGER;
2111
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302112 motg->chg_state = USB_CHG_STATE_DETECTED;
2113 delay = 0;
2114 }
2115 break;
2116 case USB_CHG_STATE_PRIMARY_DONE:
2117 vout = msm_chg_check_secondary_det(motg);
2118 if (vout)
2119 motg->chg_type = USB_DCP_CHARGER;
2120 else
2121 motg->chg_type = USB_CDP_CHARGER;
2122 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2123 /* fall through */
2124 case USB_CHG_STATE_SECONDARY_DONE:
2125 motg->chg_state = USB_CHG_STATE_DETECTED;
2126 case USB_CHG_STATE_DETECTED:
2127 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002128 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302129 /*
2130 * Spurious interrupt is seen after enabling ACA detection
2131 * due to which charger detection fails in case of PET.
2132 * Add delay of 100 microsec to avoid that.
2133 */
2134 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002135 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002136 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302137 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302138 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302139 return;
2140 default:
2141 return;
2142 }
2143
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302144 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302145}
2146
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302147/*
2148 * We support OTG, Peripheral only and Host only configurations. In case
2149 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2150 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2151 * enabled when switch is controlled by user and default mode is supplied
2152 * by board file, which can be changed by userspace later.
2153 */
2154static void msm_otg_init_sm(struct msm_otg *motg)
2155{
2156 struct msm_otg_platform_data *pdata = motg->pdata;
2157 u32 otgsc = readl(USB_OTGSC);
2158
2159 switch (pdata->mode) {
2160 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002161 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302162 if (pdata->default_mode == USB_HOST) {
2163 clear_bit(ID, &motg->inputs);
2164 } else if (pdata->default_mode == USB_PERIPHERAL) {
2165 set_bit(ID, &motg->inputs);
2166 set_bit(B_SESS_VLD, &motg->inputs);
2167 } else {
2168 set_bit(ID, &motg->inputs);
2169 clear_bit(B_SESS_VLD, &motg->inputs);
2170 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302171 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302172 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302173 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302174 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302175 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302176 set_bit(A_BUS_REQ, &motg->inputs);
2177 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002178 if (otgsc & OTGSC_BSV)
2179 set_bit(B_SESS_VLD, &motg->inputs);
2180 else
2181 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302182 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302183 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07002184 unsigned long flags;
2185 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302186 if (irq_read_line(pdata->pmic_id_irq))
2187 set_bit(ID, &motg->inputs);
2188 else
2189 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07002190 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302191 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302192 /*
2193 * VBUS initial state is reported after PMIC
2194 * driver initialization. Wait for it.
2195 */
2196 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302197 }
2198 break;
2199 case USB_HOST:
2200 clear_bit(ID, &motg->inputs);
2201 break;
2202 case USB_PERIPHERAL:
2203 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302204 if (pdata->otg_control == OTG_PHY_CONTROL) {
2205 if (otgsc & OTGSC_BSV)
2206 set_bit(B_SESS_VLD, &motg->inputs);
2207 else
2208 clear_bit(B_SESS_VLD, &motg->inputs);
2209 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2210 /*
2211 * VBUS initial state is reported after PMIC
2212 * driver initialization. Wait for it.
2213 */
2214 wait_for_completion(&pmic_vbus_init);
2215 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302216 break;
2217 default:
2218 break;
2219 }
2220}
2221
2222static void msm_otg_sm_work(struct work_struct *w)
2223{
2224 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002225 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302226 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302227
Steve Mucklef132c6c2012-06-06 18:30:57 -07002228 pm_runtime_resume(otg->phy->dev);
2229 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002230 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302231 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002232 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302233 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302234 psy = power_supply_get_by_name("usb");
2235 if (!psy)
2236 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002237 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302238 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2239 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002240 pm_runtime_put_noidle(otg->phy->dev);
2241 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302242 break;
2243 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302244 /* FALL THROUGH */
2245 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302246 if (test_bit(MHL, &motg->inputs)) {
2247 /* allow LPM */
2248 pm_runtime_put_noidle(otg->phy->dev);
2249 pm_runtime_suspend(otg->phy->dev);
2250 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002251 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302252 pr_debug("!id || id_A\n");
jh.koo78df86f2012-09-04 20:13:19 +09002253 if (slimport_is_connected()) {
2254 work = 1;
2255 break;
2256 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302257 if (msm_chg_mhl_detect(motg)) {
2258 work = 1;
2259 break;
2260 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302261 clear_bit(B_BUS_REQ, &motg->inputs);
2262 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002263 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302264 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302265 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302266 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302267 switch (motg->chg_state) {
2268 case USB_CHG_STATE_UNDEFINED:
2269 msm_chg_detect_work(&motg->chg_work.work);
2270 break;
2271 case USB_CHG_STATE_DETECTED:
2272 switch (motg->chg_type) {
2273 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302274 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002275 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302276 /* fall through */
2277 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302278 msm_otg_notify_charger(motg,
2279 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002280 pm_runtime_put_noidle(otg->phy->dev);
2281 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302282 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302283 case USB_ACA_B_CHARGER:
2284 msm_otg_notify_charger(motg,
2285 IDEV_ACA_CHG_MAX);
2286 /*
2287 * (ID_B --> ID_C) PHY_ALT interrupt can
2288 * not be detected in LPM.
2289 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302290 break;
2291 case USB_CDP_CHARGER:
2292 msm_otg_notify_charger(motg,
2293 IDEV_CHG_MAX);
2294 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002295 otg->phy->state =
2296 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302297 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302298 case USB_ACA_C_CHARGER:
2299 msm_otg_notify_charger(motg,
2300 IDEV_ACA_CHG_MAX);
2301 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002302 otg->phy->state =
2303 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302304 break;
2305 case USB_SDP_CHARGER:
roy.park99cef212012-07-26 17:00:40 -07002306 msm_otg_notify_charger(motg,
2307 IDEV_CHG_MIN);
jh.koo78df86f2012-09-04 20:13:19 +09002308 if(!slimport_is_connected()) {
2309 msm_otg_start_peripheral(otg, 1);
2310 otg->phy->state =
2311 OTG_STATE_B_PERIPHERAL;
2312 }
kibum.leeec040442012-08-22 22:29:46 +09002313 schedule_delayed_work(&motg->check_ta_work,
2314 MSM_CHECK_TA_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302315 break;
2316 default:
2317 break;
2318 }
2319 break;
2320 default:
2321 break;
2322 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302323 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2324 pr_debug("b_sess_end && b_bus_req\n");
2325 if (msm_otg_start_srp(otg) < 0) {
2326 clear_bit(B_BUS_REQ, &motg->inputs);
2327 work = 1;
2328 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302329 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002330 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302331 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2332 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302333 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302334 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302335 cancel_delayed_work_sync(&motg->chg_work);
kibum.leeec040442012-08-22 22:29:46 +09002336 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302337 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2338 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302339 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002340 msm_otg_reset(otg->phy);
2341 pm_runtime_put_noidle(otg->phy->dev);
2342 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302343 }
2344 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302345 case OTG_STATE_B_SRP_INIT:
2346 if (!test_bit(ID, &motg->inputs) ||
2347 test_bit(ID_A, &motg->inputs) ||
2348 test_bit(ID_C, &motg->inputs) ||
2349 (test_bit(B_SESS_VLD, &motg->inputs) &&
2350 !test_bit(ID_B, &motg->inputs))) {
2351 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2352 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002353 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302354 /*
2355 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2356 * bits after SRP initiation.
2357 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002358 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302359 work = 1;
2360 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2361 pr_debug("b_srp_fail\n");
2362 pr_info("A-device did not respond to SRP\n");
2363 clear_bit(B_BUS_REQ, &motg->inputs);
2364 clear_bit(B_SRP_FAIL, &motg->tmouts);
2365 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002366 ulpi_write(otg->phy, 0x0, 0x98);
2367 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302368 motg->b_last_se0_sess = jiffies;
2369 work = 1;
2370 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302371 break;
2372 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302373 if (!test_bit(ID, &motg->inputs) ||
2374 test_bit(ID_A, &motg->inputs) ||
2375 test_bit(ID_B, &motg->inputs) ||
2376 !test_bit(B_SESS_VLD, &motg->inputs)) {
2377 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302378 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2379 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302380 msm_otg_notify_charger(motg, 0);
2381 srp_reqd = otg->gadget->otg_srp_reqd;
2382 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302383 if (test_bit(ID_B, &motg->inputs))
2384 clear_bit(ID_B, &motg->inputs);
2385 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002386 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302387 motg->b_last_se0_sess = jiffies;
2388 if (srp_reqd)
2389 msm_otg_start_timer(motg,
2390 TB_TST_SRP, B_TST_SRP);
2391 else
2392 work = 1;
2393 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2394 otg->gadget->b_hnp_enable &&
2395 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2396 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2397 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2398 /* D+ pullup should not be disconnected within 4msec
2399 * after A device suspends the bus. Otherwise PET will
2400 * fail the compliance test.
2401 */
2402 udelay(1000);
2403 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002404 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302405 /*
2406 * start HCD even before A-device enable
2407 * pull-up to meet HNP timings.
2408 */
2409 otg->host->is_b_host = 1;
2410 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002411 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2412 test_bit(B_SESS_VLD, &motg->inputs)) {
2413 pr_debug("a_bus_suspend && b_sess_vld\n");
2414 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002415 pm_runtime_put_noidle(otg->phy->dev);
2416 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002417 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002418 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302419 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002420 }
2421 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302422 case OTG_STATE_B_WAIT_ACON:
2423 if (!test_bit(ID, &motg->inputs) ||
2424 test_bit(ID_A, &motg->inputs) ||
2425 test_bit(ID_B, &motg->inputs) ||
2426 !test_bit(B_SESS_VLD, &motg->inputs)) {
2427 pr_debug("!id || id_a/b || !b_sess_vld\n");
2428 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302429 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302430 * A-device is physically disconnected during
2431 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302432 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302433 msm_otg_start_host(otg, 0);
2434 otg->host->is_b_host = 0;
2435
2436 clear_bit(B_BUS_REQ, &motg->inputs);
2437 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2438 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002439 otg->phy->state = OTG_STATE_B_IDLE;
2440 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302441 work = 1;
2442 } else if (test_bit(A_CONN, &motg->inputs)) {
2443 pr_debug("a_conn\n");
2444 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002445 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302446 /*
2447 * PET disconnects D+ pullup after reset is generated
2448 * by B device in B_HOST role which is not detected by
2449 * B device. As workaorund , start timer of 300msec
2450 * and stop timer if A device is enumerated else clear
2451 * A_CONN.
2452 */
2453 msm_otg_start_timer(motg, TB_TST_CONFIG,
2454 B_TST_CONFIG);
2455 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2456 pr_debug("b_ase0_brst_tmout\n");
2457 pr_info("B HNP fail:No response from A device\n");
2458 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002459 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302460 otg->host->is_b_host = 0;
2461 clear_bit(B_ASE0_BRST, &motg->tmouts);
2462 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2463 clear_bit(B_BUS_REQ, &motg->inputs);
2464 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002465 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302466 work = 1;
2467 } else if (test_bit(ID_C, &motg->inputs)) {
2468 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2469 }
2470 break;
2471 case OTG_STATE_B_HOST:
2472 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2473 !test_bit(A_CONN, &motg->inputs) ||
2474 !test_bit(B_SESS_VLD, &motg->inputs)) {
2475 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2476 clear_bit(A_CONN, &motg->inputs);
2477 clear_bit(B_BUS_REQ, &motg->inputs);
2478 msm_otg_start_host(otg, 0);
2479 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002480 otg->phy->state = OTG_STATE_B_IDLE;
2481 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302482 work = 1;
2483 } else if (test_bit(ID_C, &motg->inputs)) {
2484 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2485 }
2486 break;
2487 case OTG_STATE_A_IDLE:
2488 otg->default_a = 1;
2489 if (test_bit(ID, &motg->inputs) &&
2490 !test_bit(ID_A, &motg->inputs)) {
2491 pr_debug("id && !id_a\n");
2492 otg->default_a = 0;
2493 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002494 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302495 del_timer_sync(&motg->id_timer);
2496 msm_otg_link_reset(motg);
2497 msm_chg_enable_aca_intr(motg);
2498 msm_otg_notify_charger(motg, 0);
2499 work = 1;
2500 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2501 (test_bit(A_SRP_DET, &motg->inputs) ||
2502 test_bit(A_BUS_REQ, &motg->inputs))) {
2503 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2504
2505 clear_bit(A_SRP_DET, &motg->inputs);
2506 /* Disable SRP detection */
2507 writel_relaxed((readl_relaxed(USB_OTGSC) &
2508 ~OTGSC_INTSTS_MASK) &
2509 ~OTGSC_DPIE, USB_OTGSC);
2510
Steve Mucklef132c6c2012-06-06 18:30:57 -07002511 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302512 /* VBUS should not be supplied before end of SRP pulse
2513 * generated by PET, if not complaince test fail.
2514 */
2515 usleep_range(10000, 12000);
2516 /* ACA: ID_A: Stop charging untill enumeration */
2517 if (test_bit(ID_A, &motg->inputs))
2518 msm_otg_notify_charger(motg, 0);
2519 else
2520 msm_hsusb_vbus_power(motg, 1);
2521 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2522 } else {
2523 pr_debug("No session requested\n");
2524 clear_bit(A_BUS_DROP, &motg->inputs);
2525 if (test_bit(ID_A, &motg->inputs)) {
2526 msm_otg_notify_charger(motg,
2527 IDEV_ACA_CHG_MAX);
2528 } else if (!test_bit(ID, &motg->inputs)) {
2529 msm_otg_notify_charger(motg, 0);
2530 /*
2531 * A-device is not providing power on VBUS.
2532 * Enable SRP detection.
2533 */
2534 writel_relaxed(0x13, USB_USBMODE);
2535 writel_relaxed((readl_relaxed(USB_OTGSC) &
2536 ~OTGSC_INTSTS_MASK) |
2537 OTGSC_DPIE, USB_OTGSC);
2538 mb();
2539 }
2540 }
2541 break;
2542 case OTG_STATE_A_WAIT_VRISE:
2543 if ((test_bit(ID, &motg->inputs) &&
2544 !test_bit(ID_A, &motg->inputs)) ||
2545 test_bit(A_BUS_DROP, &motg->inputs) ||
2546 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2547 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2548 clear_bit(A_BUS_REQ, &motg->inputs);
2549 msm_otg_del_timer(motg);
2550 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002551 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302552 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2553 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2554 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002555 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302556 if (TA_WAIT_BCON > 0)
2557 msm_otg_start_timer(motg, TA_WAIT_BCON,
2558 A_WAIT_BCON);
2559 msm_otg_start_host(otg, 1);
2560 msm_chg_enable_aca_det(motg);
2561 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302562 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302563 if (msm_chg_check_aca_intr(motg))
2564 work = 1;
2565 }
2566 break;
2567 case OTG_STATE_A_WAIT_BCON:
2568 if ((test_bit(ID, &motg->inputs) &&
2569 !test_bit(ID_A, &motg->inputs)) ||
2570 test_bit(A_BUS_DROP, &motg->inputs) ||
2571 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2572 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2573 "a_wait_bcon_tmout\n");
2574 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2575 pr_info("Device No Response\n");
2576 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2577 }
2578 msm_otg_del_timer(motg);
2579 clear_bit(A_BUS_REQ, &motg->inputs);
2580 clear_bit(B_CONN, &motg->inputs);
2581 msm_otg_start_host(otg, 0);
2582 /*
2583 * ACA: ID_A with NO accessory, just the A plug is
2584 * attached to ACA: Use IDCHG_MAX for charging
2585 */
2586 if (test_bit(ID_A, &motg->inputs))
2587 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2588 else
2589 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002590 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302591 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2592 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2593 pr_debug("!a_vbus_vld\n");
2594 clear_bit(B_CONN, &motg->inputs);
2595 msm_otg_del_timer(motg);
2596 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002597 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2598 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302599 } else if (test_bit(ID_A, &motg->inputs)) {
2600 msm_hsusb_vbus_power(motg, 0);
2601 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2602 /*
2603 * If TA_WAIT_BCON is infinite, we don;t
2604 * turn off VBUS. Enter low power mode.
2605 */
2606 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002607 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302608 } else if (!test_bit(ID, &motg->inputs)) {
2609 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302610 }
2611 break;
2612 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302613 if ((test_bit(ID, &motg->inputs) &&
2614 !test_bit(ID_A, &motg->inputs)) ||
2615 test_bit(A_BUS_DROP, &motg->inputs)) {
2616 pr_debug("id_a/b/c || a_bus_drop\n");
2617 clear_bit(B_CONN, &motg->inputs);
2618 clear_bit(A_BUS_REQ, &motg->inputs);
2619 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002620 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302621 msm_otg_start_host(otg, 0);
2622 if (!test_bit(ID_A, &motg->inputs))
2623 msm_hsusb_vbus_power(motg, 0);
2624 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2625 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2626 pr_debug("!a_vbus_vld\n");
2627 clear_bit(B_CONN, &motg->inputs);
2628 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002629 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302630 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002631 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302632 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2633 /*
2634 * a_bus_req is de-asserted when root hub is
2635 * suspended or HNP is in progress.
2636 */
2637 pr_debug("!a_bus_req\n");
2638 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002639 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302640 if (otg->host->b_hnp_enable)
2641 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2642 A_AIDL_BDIS);
2643 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002644 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302645 } else if (!test_bit(B_CONN, &motg->inputs)) {
2646 pr_debug("!b_conn\n");
2647 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002648 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302649 if (TA_WAIT_BCON > 0)
2650 msm_otg_start_timer(motg, TA_WAIT_BCON,
2651 A_WAIT_BCON);
2652 if (msm_chg_check_aca_intr(motg))
2653 work = 1;
2654 } else if (test_bit(ID_A, &motg->inputs)) {
2655 msm_otg_del_timer(motg);
2656 msm_hsusb_vbus_power(motg, 0);
2657 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2658 msm_otg_notify_charger(motg,
2659 IDEV_ACA_CHG_MAX);
2660 else
2661 msm_otg_notify_charger(motg,
2662 IDEV_CHG_MIN - motg->mA_port);
2663 } else if (!test_bit(ID, &motg->inputs)) {
2664 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2665 motg->chg_type = USB_INVALID_CHARGER;
2666 msm_otg_notify_charger(motg, 0);
2667 msm_hsusb_vbus_power(motg, 1);
2668 }
2669 break;
2670 case OTG_STATE_A_SUSPEND:
2671 if ((test_bit(ID, &motg->inputs) &&
2672 !test_bit(ID_A, &motg->inputs)) ||
2673 test_bit(A_BUS_DROP, &motg->inputs) ||
2674 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2675 pr_debug("id_a/b/c || a_bus_drop ||"
2676 "a_aidl_bdis_tmout\n");
2677 msm_otg_del_timer(motg);
2678 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002679 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302680 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002681 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302682 if (!test_bit(ID_A, &motg->inputs))
2683 msm_hsusb_vbus_power(motg, 0);
2684 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2685 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2686 pr_debug("!a_vbus_vld\n");
2687 msm_otg_del_timer(motg);
2688 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002689 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302690 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002691 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302692 } else if (!test_bit(B_CONN, &motg->inputs) &&
2693 otg->host->b_hnp_enable) {
2694 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002695 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302696 msm_otg_host_hnp_enable(otg, 1);
2697 otg->gadget->is_a_peripheral = 1;
2698 msm_otg_start_peripheral(otg, 1);
2699 } else if (!test_bit(B_CONN, &motg->inputs) &&
2700 !otg->host->b_hnp_enable) {
2701 pr_debug("!b_conn && !b_hnp_enable");
2702 /*
2703 * bus request is dropped during suspend.
2704 * acquire again for next device.
2705 */
2706 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002707 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302708 if (TA_WAIT_BCON > 0)
2709 msm_otg_start_timer(motg, TA_WAIT_BCON,
2710 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002711 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302712 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002713 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302714 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002715 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002716 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302717 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302718 }
2719 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302720 case OTG_STATE_A_PERIPHERAL:
2721 if ((test_bit(ID, &motg->inputs) &&
2722 !test_bit(ID_A, &motg->inputs)) ||
2723 test_bit(A_BUS_DROP, &motg->inputs)) {
2724 pr_debug("id _f/b/c || a_bus_drop\n");
2725 /* Clear BIDL_ADIS timer */
2726 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002727 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302728 msm_otg_start_peripheral(otg, 0);
2729 otg->gadget->is_a_peripheral = 0;
2730 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002731 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302732 if (!test_bit(ID_A, &motg->inputs))
2733 msm_hsusb_vbus_power(motg, 0);
2734 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2735 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2736 pr_debug("!a_vbus_vld\n");
2737 /* Clear BIDL_ADIS timer */
2738 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002739 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302740 msm_otg_start_peripheral(otg, 0);
2741 otg->gadget->is_a_peripheral = 0;
2742 msm_otg_start_host(otg, 0);
2743 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2744 pr_debug("a_bidl_adis_tmout\n");
2745 msm_otg_start_peripheral(otg, 0);
2746 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002747 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302748 set_bit(A_BUS_REQ, &motg->inputs);
2749 msm_otg_host_hnp_enable(otg, 0);
2750 if (TA_WAIT_BCON > 0)
2751 msm_otg_start_timer(motg, TA_WAIT_BCON,
2752 A_WAIT_BCON);
2753 } else if (test_bit(ID_A, &motg->inputs)) {
2754 msm_hsusb_vbus_power(motg, 0);
2755 msm_otg_notify_charger(motg,
2756 IDEV_CHG_MIN - motg->mA_port);
2757 } else if (!test_bit(ID, &motg->inputs)) {
2758 msm_otg_notify_charger(motg, 0);
2759 msm_hsusb_vbus_power(motg, 1);
2760 }
2761 break;
2762 case OTG_STATE_A_WAIT_VFALL:
2763 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2764 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002765 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302766 work = 1;
2767 }
2768 break;
2769 case OTG_STATE_A_VBUS_ERR:
2770 if ((test_bit(ID, &motg->inputs) &&
2771 !test_bit(ID_A, &motg->inputs)) ||
2772 test_bit(A_BUS_DROP, &motg->inputs) ||
2773 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002774 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302775 if (!test_bit(ID_A, &motg->inputs))
2776 msm_hsusb_vbus_power(motg, 0);
2777 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2778 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2779 motg->chg_type = USB_INVALID_CHARGER;
2780 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302781 }
2782 break;
2783 default:
2784 break;
2785 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302786 if (work)
2787 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302788}
2789
2790static irqreturn_t msm_otg_irq(int irq, void *data)
2791{
2792 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002793 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302794 u32 otgsc = 0, usbsts, pc;
2795 bool work = 0;
2796 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302797
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302798 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002799 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302800 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002801 motg->async_int = irq;
Jack Phamb1aa4d82012-08-13 15:32:39 -07002802 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002803 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302804 return IRQ_HANDLED;
2805 }
2806
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002807 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302808 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302809
2810 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302811 return IRQ_NONE;
2812
2813 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302814 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302815 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302816 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302817 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302818 pr_debug("Id clear\n");
2819 /*
2820 * Assert a_bus_req to supply power on
2821 * VBUS when Micro/Mini-A cable is connected
2822 * with out user intervention.
2823 */
2824 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302825 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302826 msm_chg_enable_aca_det(motg);
2827 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302828 writel_relaxed(otgsc, USB_OTGSC);
2829 work = 1;
2830 } else if (otgsc & OTGSC_DPIS) {
2831 pr_debug("DPIS detected\n");
2832 writel_relaxed(otgsc, USB_OTGSC);
2833 set_bit(A_SRP_DET, &motg->inputs);
2834 set_bit(A_BUS_REQ, &motg->inputs);
2835 work = 1;
2836 } else if (otgsc & OTGSC_BSVIS) {
2837 writel_relaxed(otgsc, USB_OTGSC);
2838 /*
2839 * BSV interrupt comes when operating as an A-device
2840 * (VBUS on/off).
2841 * But, handle BSV when charger is removed from ACA in ID_A
2842 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002843 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302844 !test_bit(ID_A, &motg->inputs))
2845 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302846 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302847 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302848 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302849 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302850 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302851 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002852 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2853
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302854 msm_chg_check_aca_intr(motg);
2855 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302856 work = 1;
2857 } else if (usbsts & STS_PCI) {
2858 pc = readl_relaxed(USB_PORTSC);
2859 pr_debug("portsc = %x\n", pc);
2860 ret = IRQ_NONE;
2861 /*
2862 * HCD Acks PCI interrupt. We use this to switch
2863 * between different OTG states.
2864 */
2865 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002866 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302867 case OTG_STATE_A_SUSPEND:
2868 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2869 !(pc & PORTSC_CCS)) {
2870 pr_debug("B_CONN clear\n");
2871 clear_bit(B_CONN, &motg->inputs);
2872 msm_otg_del_timer(motg);
2873 }
2874 break;
2875 case OTG_STATE_A_PERIPHERAL:
2876 /*
2877 * A-peripheral observed activity on bus.
2878 * clear A_BIDL_ADIS timer.
2879 */
2880 msm_otg_del_timer(motg);
2881 work = 0;
2882 break;
2883 case OTG_STATE_B_WAIT_ACON:
2884 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2885 pr_debug("A_CONN set\n");
2886 set_bit(A_CONN, &motg->inputs);
2887 /* Clear ASE0_BRST timer */
2888 msm_otg_del_timer(motg);
2889 }
2890 break;
2891 case OTG_STATE_B_HOST:
2892 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2893 pr_debug("A_CONN clear\n");
2894 clear_bit(A_CONN, &motg->inputs);
2895 msm_otg_del_timer(motg);
2896 }
2897 break;
2898 case OTG_STATE_A_WAIT_BCON:
2899 if (TA_WAIT_BCON < 0)
2900 set_bit(A_BUS_REQ, &motg->inputs);
2901 default:
2902 work = 0;
2903 break;
2904 }
2905 } else if (usbsts & STS_URI) {
2906 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002907 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302908 case OTG_STATE_A_PERIPHERAL:
2909 /*
2910 * A-peripheral observed activity on bus.
2911 * clear A_BIDL_ADIS timer.
2912 */
2913 msm_otg_del_timer(motg);
2914 work = 0;
2915 break;
2916 default:
2917 work = 0;
2918 break;
2919 }
2920 } else if (usbsts & STS_SLI) {
2921 ret = IRQ_NONE;
2922 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002923 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302924 case OTG_STATE_B_PERIPHERAL:
2925 if (otg->gadget->b_hnp_enable) {
2926 set_bit(A_BUS_SUSPEND, &motg->inputs);
2927 set_bit(B_BUS_REQ, &motg->inputs);
2928 work = 1;
2929 }
2930 break;
2931 case OTG_STATE_A_PERIPHERAL:
2932 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2933 A_BIDL_ADIS);
2934 break;
2935 default:
2936 break;
2937 }
2938 } else if ((usbsts & PHY_ALT_INT)) {
2939 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2940 if (msm_chg_check_aca_intr(motg))
2941 work = 1;
2942 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302943 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302944 if (work)
2945 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302946
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302947 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002948}
2949
2950static void msm_otg_set_vbus_state(int online)
2951{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302952 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002953 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05302954 struct usb_otg *otg = motg->phy.otg;
2955
2956 /* In A Host Mode, ignore received BSV interrupts */
2957 if (otg->phy->state >= OTG_STATE_A_IDLE)
2958 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002959
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302960 if (online) {
2961 pr_debug("PMIC: BSV set\n");
2962 set_bit(B_SESS_VLD, &motg->inputs);
2963 } else {
2964 pr_debug("PMIC: BSV clear\n");
2965 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302966 }
2967
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302968 if (!init) {
2969 init = true;
2970 complete(&pmic_vbus_init);
2971 pr_debug("PMIC: BSV init complete\n");
2972 return;
2973 }
2974
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302975 if (test_bit(MHL, &motg->inputs) ||
2976 mhl_det_in_progress) {
2977 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
2978 return;
2979 }
2980
Jack Pham5ca279b2012-05-14 18:42:54 -07002981 if (atomic_read(&motg->pm_suspended))
2982 motg->sm_work_pending = true;
2983 else
2984 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002985}
2986
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302987static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002988{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302989 struct msm_otg *motg = container_of(w, struct msm_otg,
2990 pmic_id_status_work.work);
2991 int work = 0;
2992 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002993
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302994 local_irq_save(flags);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302995 if (irq_read_line(motg->pdata->pmic_id_irq)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302996 if (!test_and_set_bit(ID, &motg->inputs)) {
2997 pr_debug("PMIC: ID set\n");
2998 work = 1;
2999 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303000 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303001 if (test_and_clear_bit(ID, &motg->inputs)) {
3002 pr_debug("PMIC: ID clear\n");
3003 set_bit(A_BUS_REQ, &motg->inputs);
3004 work = 1;
3005 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303006 }
3007
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303008 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003009 if (atomic_read(&motg->pm_suspended))
3010 motg->sm_work_pending = true;
3011 else
3012 queue_work(system_nrt_wq, &motg->sm_work);
3013 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303014 local_irq_restore(flags);
3015
3016}
3017
3018#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3019static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3020{
3021 struct msm_otg *motg = data;
3022
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303023 if (test_bit(MHL, &motg->inputs) ||
3024 mhl_det_in_progress) {
3025 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3026 return IRQ_HANDLED;
3027 }
3028
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303029 if (!aca_id_turned_on)
3030 /*schedule delayed work for 5msec for ID line state to settle*/
3031 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3032 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003033
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303034 return IRQ_HANDLED;
3035}
3036
3037static int msm_otg_mode_show(struct seq_file *s, void *unused)
3038{
3039 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003040 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303041
Steve Mucklef132c6c2012-06-06 18:30:57 -07003042 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303043 case OTG_STATE_A_HOST:
3044 seq_printf(s, "host\n");
3045 break;
3046 case OTG_STATE_B_PERIPHERAL:
3047 seq_printf(s, "peripheral\n");
3048 break;
3049 default:
3050 seq_printf(s, "none\n");
3051 break;
3052 }
3053
3054 return 0;
3055}
3056
3057static int msm_otg_mode_open(struct inode *inode, struct file *file)
3058{
3059 return single_open(file, msm_otg_mode_show, inode->i_private);
3060}
3061
3062static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3063 size_t count, loff_t *ppos)
3064{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303065 struct seq_file *s = file->private_data;
3066 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303067 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003068 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303069 int status = count;
3070 enum usb_mode_type req_mode;
3071
3072 memset(buf, 0x00, sizeof(buf));
3073
3074 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3075 status = -EFAULT;
3076 goto out;
3077 }
3078
3079 if (!strncmp(buf, "host", 4)) {
3080 req_mode = USB_HOST;
3081 } else if (!strncmp(buf, "peripheral", 10)) {
3082 req_mode = USB_PERIPHERAL;
3083 } else if (!strncmp(buf, "none", 4)) {
3084 req_mode = USB_NONE;
3085 } else {
3086 status = -EINVAL;
3087 goto out;
3088 }
3089
3090 switch (req_mode) {
3091 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003092 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303093 case OTG_STATE_A_HOST:
3094 case OTG_STATE_B_PERIPHERAL:
3095 set_bit(ID, &motg->inputs);
3096 clear_bit(B_SESS_VLD, &motg->inputs);
3097 break;
3098 default:
3099 goto out;
3100 }
3101 break;
3102 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003103 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303104 case OTG_STATE_B_IDLE:
3105 case OTG_STATE_A_HOST:
3106 set_bit(ID, &motg->inputs);
3107 set_bit(B_SESS_VLD, &motg->inputs);
3108 break;
3109 default:
3110 goto out;
3111 }
3112 break;
3113 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003114 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303115 case OTG_STATE_B_IDLE:
3116 case OTG_STATE_B_PERIPHERAL:
3117 clear_bit(ID, &motg->inputs);
3118 break;
3119 default:
3120 goto out;
3121 }
3122 break;
3123 default:
3124 goto out;
3125 }
3126
Steve Mucklef132c6c2012-06-06 18:30:57 -07003127 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303128 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303129out:
3130 return status;
3131}
3132
3133const struct file_operations msm_otg_mode_fops = {
3134 .open = msm_otg_mode_open,
3135 .read = seq_read,
3136 .write = msm_otg_mode_write,
3137 .llseek = seq_lseek,
3138 .release = single_release,
3139};
3140
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303141static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3142{
3143 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003144 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303145
Steve Mucklef132c6c2012-06-06 18:30:57 -07003146 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303147 return 0;
3148}
3149
3150static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3151{
3152 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3153}
3154
3155const struct file_operations msm_otg_state_fops = {
3156 .open = msm_otg_otg_state_open,
3157 .read = seq_read,
3158 .llseek = seq_lseek,
3159 .release = single_release,
3160};
3161
Anji jonnalad270e2d2011-08-09 11:28:32 +05303162static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3163{
3164 struct msm_otg *motg = s->private;
3165
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303166 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303167 return 0;
3168}
3169
3170static int msm_otg_chg_open(struct inode *inode, struct file *file)
3171{
3172 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3173}
3174
3175const struct file_operations msm_otg_chg_fops = {
3176 .open = msm_otg_chg_open,
3177 .read = seq_read,
3178 .llseek = seq_lseek,
3179 .release = single_release,
3180};
3181
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303182static int msm_otg_aca_show(struct seq_file *s, void *unused)
3183{
3184 if (debug_aca_enabled)
3185 seq_printf(s, "enabled\n");
3186 else
3187 seq_printf(s, "disabled\n");
3188
3189 return 0;
3190}
3191
3192static int msm_otg_aca_open(struct inode *inode, struct file *file)
3193{
3194 return single_open(file, msm_otg_aca_show, inode->i_private);
3195}
3196
3197static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3198 size_t count, loff_t *ppos)
3199{
3200 char buf[8];
3201
3202 memset(buf, 0x00, sizeof(buf));
3203
3204 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3205 return -EFAULT;
3206
3207 if (!strncmp(buf, "enable", 6))
3208 debug_aca_enabled = true;
3209 else
3210 debug_aca_enabled = false;
3211
3212 return count;
3213}
3214
3215const struct file_operations msm_otg_aca_fops = {
3216 .open = msm_otg_aca_open,
3217 .read = seq_read,
3218 .write = msm_otg_aca_write,
3219 .llseek = seq_lseek,
3220 .release = single_release,
3221};
3222
Manu Gautam8bdcc592012-03-06 11:26:06 +05303223static int msm_otg_bus_show(struct seq_file *s, void *unused)
3224{
3225 if (debug_bus_voting_enabled)
3226 seq_printf(s, "enabled\n");
3227 else
3228 seq_printf(s, "disabled\n");
3229
3230 return 0;
3231}
3232
3233static int msm_otg_bus_open(struct inode *inode, struct file *file)
3234{
3235 return single_open(file, msm_otg_bus_show, inode->i_private);
3236}
3237
3238static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3239 size_t count, loff_t *ppos)
3240{
3241 char buf[8];
3242 int ret;
3243 struct seq_file *s = file->private_data;
3244 struct msm_otg *motg = s->private;
3245
3246 memset(buf, 0x00, sizeof(buf));
3247
3248 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3249 return -EFAULT;
3250
3251 if (!strncmp(buf, "enable", 6)) {
3252 /* Do not vote here. Let OTG statemachine decide when to vote */
3253 debug_bus_voting_enabled = true;
3254 } else {
3255 debug_bus_voting_enabled = false;
3256 if (motg->bus_perf_client) {
3257 ret = msm_bus_scale_client_update_request(
3258 motg->bus_perf_client, 0);
3259 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003260 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303261 "for bus bw %d\n", __func__, ret);
3262 }
3263 }
3264
3265 return count;
3266}
3267
3268const struct file_operations msm_otg_bus_fops = {
3269 .open = msm_otg_bus_open,
3270 .read = seq_read,
3271 .write = msm_otg_bus_write,
3272 .llseek = seq_lseek,
3273 .release = single_release,
3274};
3275
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303276static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303277
3278static int msm_otg_debugfs_init(struct msm_otg *motg)
3279{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303280 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303281
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303282 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3283
3284 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3285 return -ENODEV;
3286
Anji jonnalad270e2d2011-08-09 11:28:32 +05303287 if (motg->pdata->mode == USB_OTG &&
3288 motg->pdata->otg_control == OTG_USER_CONTROL) {
3289
Manu Gautam8bdcc592012-03-06 11:26:06 +05303290 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303291 S_IWUSR, msm_otg_dbg_root, motg,
3292 &msm_otg_mode_fops);
3293
Manu Gautam8bdcc592012-03-06 11:26:06 +05303294 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303295 debugfs_remove(msm_otg_dbg_root);
3296 msm_otg_dbg_root = NULL;
3297 return -ENODEV;
3298 }
3299 }
3300
Manu Gautam8bdcc592012-03-06 11:26:06 +05303301 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303302 msm_otg_dbg_root, motg,
3303 &msm_otg_chg_fops);
3304
Manu Gautam8bdcc592012-03-06 11:26:06 +05303305 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303306 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303307 return -ENODEV;
3308 }
3309
Manu Gautam8bdcc592012-03-06 11:26:06 +05303310 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303311 msm_otg_dbg_root, motg,
3312 &msm_otg_aca_fops);
3313
Manu Gautam8bdcc592012-03-06 11:26:06 +05303314 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303315 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303316 return -ENODEV;
3317 }
3318
Manu Gautam8bdcc592012-03-06 11:26:06 +05303319 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3320 msm_otg_dbg_root, motg,
3321 &msm_otg_bus_fops);
3322
3323 if (!msm_otg_dentry) {
3324 debugfs_remove_recursive(msm_otg_dbg_root);
3325 return -ENODEV;
3326 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303327
3328 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3329 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3330
3331 if (!msm_otg_dentry) {
3332 debugfs_remove_recursive(msm_otg_dbg_root);
3333 return -ENODEV;
3334 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303335 return 0;
3336}
3337
3338static void msm_otg_debugfs_cleanup(void)
3339{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303340 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303341}
3342
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303343static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3344static struct platform_device *msm_otg_add_pdev(
3345 struct platform_device *ofdev, const char *name)
3346{
3347 struct platform_device *pdev;
3348 const struct resource *res = ofdev->resource;
3349 unsigned int num = ofdev->num_resources;
3350 int retval;
3351
3352 pdev = platform_device_alloc(name, -1);
3353 if (!pdev) {
3354 retval = -ENOMEM;
3355 goto error;
3356 }
3357
3358 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3359 pdev->dev.dma_mask = &msm_otg_dma_mask;
3360
3361 if (num) {
3362 retval = platform_device_add_resources(pdev, res, num);
3363 if (retval)
3364 goto error;
3365 }
3366
3367 retval = platform_device_add(pdev);
3368 if (retval)
3369 goto error;
3370
3371 return pdev;
3372
3373error:
3374 platform_device_put(pdev);
3375 return ERR_PTR(retval);
3376}
3377
3378static int msm_otg_setup_devices(struct platform_device *ofdev,
3379 enum usb_mode_type mode, bool init)
3380{
3381 const char *gadget_name = "msm_hsusb";
3382 const char *host_name = "msm_hsusb_host";
3383 static struct platform_device *gadget_pdev;
3384 static struct platform_device *host_pdev;
3385 int retval = 0;
3386
3387 if (!init) {
3388 if (gadget_pdev)
3389 platform_device_unregister(gadget_pdev);
3390 if (host_pdev)
3391 platform_device_unregister(host_pdev);
3392 return 0;
3393 }
3394
3395 switch (mode) {
3396 case USB_OTG:
3397 /* fall through */
3398 case USB_PERIPHERAL:
3399 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3400 if (IS_ERR(gadget_pdev)) {
3401 retval = PTR_ERR(gadget_pdev);
3402 break;
3403 }
3404 if (mode == USB_PERIPHERAL)
3405 break;
3406 /* fall through */
3407 case USB_HOST:
3408 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3409 if (IS_ERR(host_pdev)) {
3410 retval = PTR_ERR(host_pdev);
3411 if (mode == USB_OTG)
3412 platform_device_unregister(gadget_pdev);
3413 }
3414 break;
3415 default:
3416 break;
3417 }
3418
3419 return retval;
3420}
3421
3422struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3423{
3424 struct device_node *node = pdev->dev.of_node;
3425 struct msm_otg_platform_data *pdata;
3426 int len = 0;
3427
3428 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3429 if (!pdata) {
3430 pr_err("unable to allocate platform data\n");
3431 return NULL;
3432 }
3433 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3434 if (len) {
3435 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3436 if (!pdata->phy_init_seq)
3437 return NULL;
3438 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3439 pdata->phy_init_seq,
3440 len/sizeof(*pdata->phy_init_seq));
3441 }
3442 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3443 &pdata->power_budget);
3444 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3445 &pdata->mode);
3446 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3447 &pdata->otg_control);
3448 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3449 &pdata->default_mode);
3450 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3451 &pdata->phy_type);
3452 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3453 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303454 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3455 "qcom,hsusb-otg-disable-reset");
3456
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303457 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303458}
3459
3460static int __init msm_otg_probe(struct platform_device *pdev)
3461{
3462 int ret = 0;
3463 struct resource *res;
3464 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003465 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303466 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303467
3468 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303469
3470 if (pdev->dev.of_node) {
3471 dev_dbg(&pdev->dev, "device tree enabled\n");
3472 pdata = msm_otg_dt_to_pdata(pdev);
3473 if (!pdata)
3474 return -ENOMEM;
3475 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3476 if (ret) {
3477 dev_err(&pdev->dev, "devices setup failed\n");
3478 return ret;
3479 }
3480 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303481 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3482 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303483 } else {
3484 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303485 }
3486
3487 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3488 if (!motg) {
3489 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3490 return -ENOMEM;
3491 }
3492
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003493 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3494 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003495 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3496 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303497 goto free_motg;
3498 }
3499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003500 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303501 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003502 phy = &motg->phy;
3503 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303504
3505 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303506 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3507 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3508 * PMIC can detect ACA ID_GND and generate an interrupt.
3509 */
3510 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3511 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3512 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003513 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303514 }
3515
Ofir Cohen4da266f2012-01-03 10:19:29 +02003516 /* initialize reset counter */
3517 motg->reset_counter = 0;
3518
Amit Blay02eff132011-09-21 16:46:24 +03003519 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003520 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003521 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003522 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303523
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303524 /*
3525 * Targets on which link uses asynchronous reset methodology,
3526 * free running clock is not required during the reset.
3527 */
Manu Gautam5143b252012-01-05 19:25:23 -08003528 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303529 if (IS_ERR(motg->clk))
3530 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3531 else
3532 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303533
3534 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003535 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303536 * CORE CLK must be running at >55Mhz for correct HSUSB
3537 * operation and USB core cannot tolerate frequency changes on
3538 * CORE CLK. For such USB cores, vote for maximum clk frequency
3539 * on pclk source
3540 */
Manu Gautam5143b252012-01-05 19:25:23 -08003541 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3542 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303543 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003544 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303545 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003546 goto put_clk;
3547 }
3548 clk_set_rate(motg->core_clk, INT_MAX);
3549
3550 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3551 if (IS_ERR(motg->pclk)) {
3552 dev_err(&pdev->dev, "failed to get iface_clk\n");
3553 ret = PTR_ERR(motg->pclk);
3554 goto put_core_clk;
3555 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303556
3557 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3558 if (!res) {
3559 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3560 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003561 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303562 }
3563
3564 motg->regs = ioremap(res->start, resource_size(res));
3565 if (!motg->regs) {
3566 dev_err(&pdev->dev, "ioremap failed\n");
3567 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003568 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303569 }
3570 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3571
3572 motg->irq = platform_get_irq(pdev, 0);
3573 if (!motg->irq) {
3574 dev_err(&pdev->dev, "platform_get_irq failed\n");
3575 ret = -ENODEV;
3576 goto free_regs;
3577 }
3578
Manu Gautamf8c45642012-08-10 10:20:56 -07003579 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3580 if (motg->async_irq < 0) {
3581 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3582 motg->async_irq = 0;
3583 }
3584
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003585 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003586 if (IS_ERR(motg->xo_handle)) {
3587 dev_err(&pdev->dev, "%s not able to get the handle "
3588 "to vote for TCXO D0 buffer\n", __func__);
3589 ret = PTR_ERR(motg->xo_handle);
3590 goto free_regs;
3591 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303592
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003593 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003594 if (ret) {
3595 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3596 "D0 buffer%d\n", __func__, ret);
3597 goto free_xo_handle;
3598 }
3599
Manu Gautam28b1bac2012-01-30 16:43:06 +05303600 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303601
Mayank Rana248698c2012-04-19 00:03:16 +05303602 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003603 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303604 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003605 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303606 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003607 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003608 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303609 goto devote_xo_handle;
3610 }
3611 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303612 }
3613
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003614 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303615 if (ret) {
3616 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303617 goto devote_xo_handle;
3618 }
3619
3620 ret = regulator_enable(hsusb_vddcx);
3621 if (ret) {
3622 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3623 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303624 }
3625
3626 ret = msm_hsusb_ldo_init(motg, 1);
3627 if (ret) {
3628 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303629 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303630 }
3631
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303632 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303633 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3634 "mhl_usb_hs_switch");
3635 if (IS_ERR(mhl_usb_hs_switch)) {
3636 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003637 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303638 goto free_ldo_init;
3639 }
3640 }
3641
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003642 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303643 if (ret) {
3644 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003645 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303646 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303647 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303648
3649 writel(0, USB_USBINTR);
3650 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003651 /* Ensure that above STOREs are completed before enabling interrupts */
3652 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303653
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303654 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3655 if (ret)
3656 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003657 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303658 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303659 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303660 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303661 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
kibum.leeec040442012-08-22 22:29:46 +09003662 INIT_DELAYED_WORK(&motg->check_ta_work, msm_ta_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303663 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3664 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303665 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3666 "msm_otg", motg);
3667 if (ret) {
3668 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003669 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303670 }
3671
Manu Gautamf8c45642012-08-10 10:20:56 -07003672 if (motg->async_irq) {
3673 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3674 "msm_otg", motg);
3675 if (ret) {
3676 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3677 goto free_irq;
3678 }
3679 disable_irq(motg->async_irq);
3680 }
3681
Jack Pham87f202f2012-08-06 00:24:22 -07003682 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3683 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3684
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003685 phy->init = msm_otg_reset;
3686 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003687 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303688
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003689 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303690
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003691 phy->otg->phy = &motg->phy;
3692 phy->otg->set_host = msm_otg_set_host;
3693 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003694 phy->otg->start_hnp = msm_otg_start_hnp;
3695 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003696
3697 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303698 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003699 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003700 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303701 }
3702
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303703 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303704 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003705 if (motg->pdata->pmic_id_irq) {
3706 ret = request_irq(motg->pdata->pmic_id_irq,
3707 msm_pmic_id_irq,
3708 IRQF_TRIGGER_RISING |
3709 IRQF_TRIGGER_FALLING,
3710 "msm_otg", motg);
3711 if (ret) {
3712 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003713 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003714 }
3715 } else {
3716 ret = -ENODEV;
3717 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003718 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003719 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303720 }
3721
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303722 msm_hsusb_mhl_switch_enable(motg, 1);
3723
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303724 platform_set_drvdata(pdev, motg);
3725 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003726 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303727
Anji jonnalad270e2d2011-08-09 11:28:32 +05303728 ret = msm_otg_debugfs_init(motg);
3729 if (ret)
3730 dev_dbg(&pdev->dev, "mode debugfs file is"
3731 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303732
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003733 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3734 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3735
Amit Blay58b31472011-11-18 09:39:39 +02003736 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3737 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303738 (!(motg->pdata->mode == USB_OTG) ||
3739 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003740 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303741 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003742
Amit Blay58b31472011-11-18 09:39:39 +02003743 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3744 motg->caps = ALLOW_PHY_RETENTION;
3745 }
3746
Amit Blay6fa647a2012-05-24 14:12:08 +03003747 if (motg->pdata->enable_lpm_on_dev_suspend)
3748 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3749
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003750 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303751 pm_runtime_set_active(&pdev->dev);
3752 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303753
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303754 if (motg->pdata->bus_scale_table) {
3755 motg->bus_perf_client =
3756 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3757 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003758 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303759 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303760 else
3761 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303762 }
3763
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303764 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003765
Steve Mucklef132c6c2012-06-06 18:30:57 -07003766remove_phy:
3767 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003768free_async_irq:
3769 if (motg->async_irq)
3770 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303771free_irq:
3772 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003773destroy_wlock:
3774 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303775 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003776 msm_hsusb_ldo_enable(motg, 0);
3777free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303778 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303779free_hsusb_vddcx:
3780 regulator_disable(hsusb_vddcx);
3781free_config_vddcx:
3782 regulator_set_voltage(hsusb_vddcx,
3783 vdd_val[motg->vdd_type][VDD_NONE],
3784 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003785devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303786 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003787 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003788free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003789 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303790free_regs:
3791 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003792put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303793 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303794put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003795 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303796put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303797 if (!IS_ERR(motg->clk))
3798 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003799 if (!IS_ERR(motg->phy_reset_clk))
3800 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003801free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003802 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303803free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303804 kfree(motg);
3805 return ret;
3806}
3807
3808static int __devexit msm_otg_remove(struct platform_device *pdev)
3809{
3810 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003811 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303812 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303813
3814 if (otg->host || otg->gadget)
3815 return -EBUSY;
3816
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303817 if (pdev->dev.of_node)
3818 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003819 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3820 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303821 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303822 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303823 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303824 cancel_delayed_work_sync(&motg->pmic_id_status_work);
kibum.leeec040442012-08-22 22:29:46 +09003825 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303826 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303827
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303828 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303829
3830 device_init_wakeup(&pdev->dev, 0);
3831 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003832 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303833
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303834 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003835 if (motg->pdata->pmic_id_irq)
3836 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003837 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303838 free_irq(motg->irq, motg);
3839
Jack Pham87f202f2012-08-06 00:24:22 -07003840 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3841 motg->pdata->mpm_otgsessvld_int)
3842 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3843
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303844 /*
3845 * Put PHY in low power mode.
3846 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003847 ulpi_read(otg->phy, 0x14);
3848 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303849
3850 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3851 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3852 if (readl(USB_PORTSC) & PORTSC_PHCD)
3853 break;
3854 udelay(1);
3855 cnt++;
3856 }
3857 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003858 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303859
Manu Gautam28b1bac2012-01-30 16:43:06 +05303860 clk_disable_unprepare(motg->pclk);
3861 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003862 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003863 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303864 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303865 regulator_disable(hsusb_vddcx);
3866 regulator_set_voltage(hsusb_vddcx,
3867 vdd_val[motg->vdd_type][VDD_NONE],
3868 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303869
3870 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303871 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303872
Amit Blay02eff132011-09-21 16:46:24 +03003873 if (!IS_ERR(motg->phy_reset_clk))
3874 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303875 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303876 if (!IS_ERR(motg->clk))
3877 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003878 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303879
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303880 if (motg->bus_perf_client)
3881 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303882
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003883 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303884 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303885 return 0;
3886}
3887
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303888#ifdef CONFIG_PM_RUNTIME
3889static int msm_otg_runtime_idle(struct device *dev)
3890{
3891 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003892 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303893
3894 dev_dbg(dev, "OTG runtime idle\n");
3895
Steve Mucklef132c6c2012-06-06 18:30:57 -07003896 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303897 return -EAGAIN;
3898 else
3899 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303900}
3901
3902static int msm_otg_runtime_suspend(struct device *dev)
3903{
3904 struct msm_otg *motg = dev_get_drvdata(dev);
3905
3906 dev_dbg(dev, "OTG runtime suspend\n");
3907 return msm_otg_suspend(motg);
3908}
3909
3910static int msm_otg_runtime_resume(struct device *dev)
3911{
3912 struct msm_otg *motg = dev_get_drvdata(dev);
3913
3914 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303915 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303916 return msm_otg_resume(motg);
3917}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303918#endif
3919
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303920#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303921static int msm_otg_pm_suspend(struct device *dev)
3922{
Jack Pham5ca279b2012-05-14 18:42:54 -07003923 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303924 struct msm_otg *motg = dev_get_drvdata(dev);
3925
3926 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003927
3928 atomic_set(&motg->pm_suspended, 1);
3929 ret = msm_otg_suspend(motg);
3930 if (ret)
3931 atomic_set(&motg->pm_suspended, 0);
3932
3933 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303934}
3935
3936static int msm_otg_pm_resume(struct device *dev)
3937{
Jack Pham5ca279b2012-05-14 18:42:54 -07003938 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303939 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303940
3941 dev_dbg(dev, "OTG PM resume\n");
3942
Jack Pham5ca279b2012-05-14 18:42:54 -07003943 atomic_set(&motg->pm_suspended, 0);
Jack Phamb1aa4d82012-08-13 15:32:39 -07003944 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003945 pm_runtime_get_noresume(dev);
3946 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303947
Jack Pham5ca279b2012-05-14 18:42:54 -07003948 /* Update runtime PM status */
3949 pm_runtime_disable(dev);
3950 pm_runtime_set_active(dev);
3951 pm_runtime_enable(dev);
3952
Jack Phamb1aa4d82012-08-13 15:32:39 -07003953 if (motg->sm_work_pending) {
3954 motg->sm_work_pending = false;
3955 queue_work(system_nrt_wq, &motg->sm_work);
3956 }
Jack Pham5ca279b2012-05-14 18:42:54 -07003957 }
3958
3959 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303960}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303961#endif
3962
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303963#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303964static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303965 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3966 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3967 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303968};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303969#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303970
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303971static struct of_device_id msm_otg_dt_match[] = {
3972 { .compatible = "qcom,hsusb-otg",
3973 },
3974 {}
3975};
3976
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303977static struct platform_driver msm_otg_driver = {
3978 .remove = __devexit_p(msm_otg_remove),
3979 .driver = {
3980 .name = DRIVER_NAME,
3981 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303982#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303983 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303984#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303985 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303986 },
3987};
3988
3989static int __init msm_otg_init(void)
3990{
3991 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3992}
3993
3994static void __exit msm_otg_exit(void)
3995{
3996 platform_driver_unregister(&msm_otg_driver);
3997}
3998
3999module_init(msm_otg_init);
4000module_exit(msm_otg_exit);
4001
4002MODULE_LICENSE("GPL v2");
4003MODULE_DESCRIPTION("MSM USB transceiver driver");