blob: c69bc047e6e714f41347f02693e0be5114efe9c2 [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020042#include <linux/power_supply.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053043#include <linux/mhl_8334.h>
jh.koo78df86f2012-09-04 20:13:19 +090044#include <linux/slimport.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045
Devin Kimd10f03c2012-06-25 17:05:13 -070046#include <asm/mach-types.h>
47
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053048#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070049#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080050#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053051#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053052#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053053
54#define MSM_USB_BASE (motg->regs)
55#define DRIVER_NAME "msm_otg"
56
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053057#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053058#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053059#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
60#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
61#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
62#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
63
64#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
65#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
66#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
67#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
68
Mayank Rana248698c2012-04-19 00:03:16 +053069#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080070#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053071#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
72
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053073static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053075static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053076static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053077static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078
Anji jonnala11aa5c42011-05-04 10:19:48 +053079static struct regulator *hsusb_3p3;
80static struct regulator *hsusb_1p8;
81static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053082static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +053083static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053084static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053085
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053086static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053087static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +053088{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053089#ifdef CONFIG_USB_MSM_ACA
90 return true;
91#else
92 return debug_aca_enabled;
93#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +053094}
95
Mayank Rana248698c2012-04-19 00:03:16 +053096static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
97 { /* VDD_CX CORNER Voting */
98 [VDD_NONE] = RPM_VREG_CORNER_NONE,
99 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
100 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
101 },
102 { /* VDD_CX Voltage Voting */
103 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
104 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
105 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
106 },
107};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530108
109static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
110{
111 int rc = 0;
112
113 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700114 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530115 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200116 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530117 return PTR_ERR(hsusb_3p3);
118 }
119
120 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
121 USB_PHY_3P3_VOL_MAX);
122 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700123 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530125 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700127 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200129 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530130 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 }
133 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
134 USB_PHY_1P8_VOL_MAX);
135 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700136 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138 goto put_1p8;
139 }
140
141 return 0;
142 }
143
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146put_3p3_lpm:
147 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148 return rc;
149}
150
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530151static int msm_hsusb_config_vddcx(int high)
152{
Mayank Rana248698c2012-04-19 00:03:16 +0530153 struct msm_otg *motg = the_msm_otg;
154 enum usb_vdd_type vdd_type = motg->vdd_type;
155 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530156 int min_vol;
157 int ret;
158
Mayank Rana248698c2012-04-19 00:03:16 +0530159 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530160 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
161 if (ret) {
162 pr_err("%s: unable to set the voltage for regulator "
163 "HSUSB_VDDCX\n", __func__);
164 return ret;
165 }
166
167 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
168
169 return ret;
170}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530173{
174 int ret = 0;
175
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530176 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530177 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
178 return -ENODEV;
179 }
180
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530181 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530182 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
183 return -ENODEV;
184 }
185
186 if (on) {
187 ret = regulator_set_optimum_mode(hsusb_1p8,
188 USB_PHY_1P8_HPM_LOAD);
189 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530191 "HSUSB_1p8\n", __func__);
192 return ret;
193 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195 ret = regulator_enable(hsusb_1p8);
196 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700197 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 __func__);
199 regulator_set_optimum_mode(hsusb_1p8, 0);
200 return ret;
201 }
202
Anji jonnala11aa5c42011-05-04 10:19:48 +0530203 ret = regulator_set_optimum_mode(hsusb_3p3,
204 USB_PHY_3P3_HPM_LOAD);
205 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 regulator_set_optimum_mode(hsusb_1p8, 0);
209 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530210 return ret;
211 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212
213 ret = regulator_enable(hsusb_3p3);
214 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700215 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 __func__);
217 regulator_set_optimum_mode(hsusb_3p3, 0);
218 regulator_set_optimum_mode(hsusb_1p8, 0);
219 regulator_disable(hsusb_1p8);
220 return ret;
221 }
222
Anji jonnala11aa5c42011-05-04 10:19:48 +0530223 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 ret = regulator_disable(hsusb_1p8);
225 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700226 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 __func__);
228 return ret;
229 }
230
231 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530234 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235
236 ret = regulator_disable(hsusb_3p3);
237 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700238 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 __func__);
240 return ret;
241 }
242 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530243 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530245 "HSUSB_3p3\n", __func__);
246 }
247
248 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
249 return ret < 0 ? ret : 0;
250}
251
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530252static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
253{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530254 struct msm_otg_platform_data *pdata = motg->pdata;
255
256 if (!pdata->mhl_enable)
257 return;
258
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530259 if (!mhl_usb_hs_switch) {
260 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530261 return;
262 }
263
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530264 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530265 if (regulator_enable(mhl_usb_hs_switch))
266 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530267 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530268 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530269 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530270}
271
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200272static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530273{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200274 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530275 int cnt = 0;
276
277 /* initiate read operation */
278 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
279 USB_ULPI_VIEWPORT);
280
281 /* wait for completion */
282 while (cnt < ULPI_IO_TIMEOUT_USEC) {
283 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
284 break;
285 udelay(1);
286 cnt++;
287 }
288
289 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200290 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530291 readl(USB_ULPI_VIEWPORT));
292 return -ETIMEDOUT;
293 }
294 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
295}
296
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200297static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530298{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200299 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530300 int cnt = 0;
301
302 /* initiate write operation */
303 writel(ULPI_RUN | ULPI_WRITE |
304 ULPI_ADDR(reg) | ULPI_DATA(val),
305 USB_ULPI_VIEWPORT);
306
307 /* wait for completion */
308 while (cnt < ULPI_IO_TIMEOUT_USEC) {
309 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
310 break;
311 udelay(1);
312 cnt++;
313 }
314
315 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200316 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530317 return -ETIMEDOUT;
318 }
319 return 0;
320}
321
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200322static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530323 .read = ulpi_read,
324 .write = ulpi_write,
325};
326
327static void ulpi_init(struct msm_otg *motg)
328{
329 struct msm_otg_platform_data *pdata = motg->pdata;
330 int *seq = pdata->phy_init_seq;
331
332 if (!seq)
333 return;
334
335 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200336 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530337 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200338 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530339 seq += 2;
340 }
341}
342
343static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
344{
345 int ret;
346
347 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530348 if (!IS_ERR(motg->clk)) {
349 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
350 } else {
351 /* Using asynchronous block reset to the hardware */
352 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
353 clk_disable_unprepare(motg->pclk);
354 clk_disable_unprepare(motg->core_clk);
355 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
356 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530357 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200358 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530359 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530360 if (!IS_ERR(motg->clk)) {
361 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
362 } else {
363 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
364 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
365 ndelay(200);
366 clk_prepare_enable(motg->core_clk);
367 clk_prepare_enable(motg->pclk);
368 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530369 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200370 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530371 }
372 return ret;
373}
374
375static int msm_otg_phy_clk_reset(struct msm_otg *motg)
376{
377 int ret;
378
Amit Blay02eff132011-09-21 16:46:24 +0300379 if (IS_ERR(motg->phy_reset_clk))
380 return 0;
381
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530382 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
383 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200384 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530385 return ret;
386 }
387 usleep_range(10000, 12000);
388 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
389 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200390 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530391 return ret;
392}
393
394static int msm_otg_phy_reset(struct msm_otg *motg)
395{
396 u32 val;
397 int ret;
398 int retries;
399
400 ret = msm_otg_link_clk_reset(motg, 1);
401 if (ret)
402 return ret;
403 ret = msm_otg_phy_clk_reset(motg);
404 if (ret)
405 return ret;
406 ret = msm_otg_link_clk_reset(motg, 0);
407 if (ret)
408 return ret;
409
410 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
411 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
412
413 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200414 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530415 ULPI_CLR(ULPI_FUNC_CTRL));
416 if (!ret)
417 break;
418 ret = msm_otg_phy_clk_reset(motg);
419 if (ret)
420 return ret;
421 }
422 if (!retries)
423 return -ETIMEDOUT;
424
425 /* This reset calibrates the phy, if the above write succeeded */
426 ret = msm_otg_phy_clk_reset(motg);
427 if (ret)
428 return ret;
429
430 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200431 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 if (ret != -ETIMEDOUT)
433 break;
434 ret = msm_otg_phy_clk_reset(motg);
435 if (ret)
436 return ret;
437 }
438 if (!retries)
439 return -ETIMEDOUT;
440
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200441 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530442 return 0;
443}
444
445#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530446static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530447{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530448 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530449
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530450 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530451 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530452 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530453 break;
454 udelay(1);
455 cnt++;
456 }
457 if (cnt >= LINK_RESET_TIMEOUT_USEC)
458 return -ETIMEDOUT;
459
460 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530461 writel_relaxed(0x80000000, USB_PORTSC);
462 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530463 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530464
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530465 return 0;
466}
467
Steve Mucklef132c6c2012-06-06 18:30:57 -0700468static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530469{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700470 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530471 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530472 int ret;
473 u32 val = 0;
474 u32 ulpi_val = 0;
475
Ofir Cohen4da266f2012-01-03 10:19:29 +0200476 /*
477 * USB PHY and Link reset also reset the USB BAM.
478 * Thus perform reset operation only once to avoid
479 * USB BAM reset on other cases e.g. USB cable disconnections.
480 */
481 if (pdata->disable_reset_on_disconnect) {
482 if (motg->reset_counter)
483 return 0;
484 else
485 motg->reset_counter++;
486 }
487
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530488 if (!IS_ERR(motg->clk))
489 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530490 ret = msm_otg_phy_reset(motg);
491 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700492 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530493 return ret;
494 }
495
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530496 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530497 ret = msm_otg_link_reset(motg);
498 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700499 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530500 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530501 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530502 msleep(100);
503
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530504 ulpi_init(motg);
505
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 /* Ensure that RESET operation is completed before turning off clock */
507 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530508
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530509 if (!IS_ERR(motg->clk))
510 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530511
512 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530513 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530514 if (pdata->mode == USB_OTG) {
515 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
516 val |= OTGSC_IDIE | OTGSC_BSVIE;
517 } else if (pdata->mode == USB_PERIPHERAL) {
518 ulpi_val = ULPI_INT_SESS_VALID;
519 val |= OTGSC_BSVIE;
520 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530521 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200522 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
523 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530524 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700525 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530526 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530527 /* Enable PMIC pull-up */
528 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530529 }
530
531 return 0;
532}
533
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530534static const char *timer_string(int bit)
535{
536 switch (bit) {
537 case A_WAIT_VRISE: return "a_wait_vrise";
538 case A_WAIT_VFALL: return "a_wait_vfall";
539 case B_SRP_FAIL: return "b_srp_fail";
540 case A_WAIT_BCON: return "a_wait_bcon";
541 case A_AIDL_BDIS: return "a_aidl_bdis";
542 case A_BIDL_ADIS: return "a_bidl_adis";
543 case B_ASE0_BRST: return "b_ase0_brst";
544 case A_TST_MAINT: return "a_tst_maint";
545 case B_TST_SRP: return "b_tst_srp";
546 case B_TST_CONFIG: return "b_tst_config";
547 default: return "UNDEFINED";
548 }
549}
550
551static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
552{
553 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
554
555 switch (motg->active_tmout) {
556 case A_WAIT_VRISE:
557 /* TODO: use vbus_vld interrupt */
558 set_bit(A_VBUS_VLD, &motg->inputs);
559 break;
560 case A_TST_MAINT:
561 /* OTG PET: End session after TA_TST_MAINT */
562 set_bit(A_BUS_DROP, &motg->inputs);
563 break;
564 case B_TST_SRP:
565 /*
566 * OTG PET: Initiate SRP after TB_TST_SRP of
567 * previous session end.
568 */
569 set_bit(B_BUS_REQ, &motg->inputs);
570 break;
571 case B_TST_CONFIG:
572 clear_bit(A_CONN, &motg->inputs);
573 break;
574 default:
575 set_bit(motg->active_tmout, &motg->tmouts);
576 }
577
578 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
579 queue_work(system_nrt_wq, &motg->sm_work);
580 return HRTIMER_NORESTART;
581}
582
583static void msm_otg_del_timer(struct msm_otg *motg)
584{
585 int bit = motg->active_tmout;
586
587 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
588 div_s64(ktime_to_us(hrtimer_get_remaining(
589 &motg->timer)), 1000));
590 hrtimer_cancel(&motg->timer);
591 clear_bit(bit, &motg->tmouts);
592}
593
594static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
595{
596 clear_bit(bit, &motg->tmouts);
597 motg->active_tmout = bit;
598 pr_debug("starting %s timer\n", timer_string(bit));
599 hrtimer_start(&motg->timer,
600 ktime_set(time / 1000, (time % 1000) * 1000000),
601 HRTIMER_MODE_REL);
602}
603
604static void msm_otg_init_timer(struct msm_otg *motg)
605{
606 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
607 motg->timer.function = msm_otg_timer_func;
608}
609
Steve Mucklef132c6c2012-06-06 18:30:57 -0700610static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530611{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700612 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530613
Steve Mucklef132c6c2012-06-06 18:30:57 -0700614 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530615 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700616 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530617 return -EINVAL;
618 }
619
620 pr_debug("A-Host: HNP initiated\n");
621 clear_bit(A_BUS_REQ, &motg->inputs);
622 queue_work(system_nrt_wq, &motg->sm_work);
623 return 0;
624}
625
Steve Mucklef132c6c2012-06-06 18:30:57 -0700626static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530627{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700628 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530629 u32 val;
630 int ret = 0;
631
Steve Mucklef132c6c2012-06-06 18:30:57 -0700632 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530633 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700634 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530635 ret = -EINVAL;
636 goto out;
637 }
638
639 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
640 pr_debug("initial conditions of SRP are not met. Try again"
641 "after some time\n");
642 ret = -EAGAIN;
643 goto out;
644 }
645
646 pr_debug("B-Device SRP started\n");
647
648 /*
649 * PHY won't pull D+ high unless it detects Vbus valid.
650 * Since by definition, SRP is only done when Vbus is not valid,
651 * software work-around needs to be used to spoof the PHY into
652 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
653 * VBUSVLDEXT register bits.
654 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700655 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530656 /*
657 * Harware auto assist data pulsing: Data pulse is given
658 * for 7msec; wait for vbus
659 */
660 val = readl_relaxed(USB_OTGSC);
661 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
662
663 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
664out:
665 return ret;
666}
667
Steve Mucklef132c6c2012-06-06 18:30:57 -0700668static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530669{
670 struct usb_hcd *hcd = bus_to_hcd(otg->host);
671 struct usb_device *rhub = otg->host->root_hub;
672
673 if (enable) {
674 pm_runtime_disable(&rhub->dev);
675 rhub->state = USB_STATE_NOTATTACHED;
676 hcd->driver->bus_suspend(hcd);
677 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
678 } else {
679 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700680 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530681 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
682 }
683}
684
Steve Mucklef132c6c2012-06-06 18:30:57 -0700685static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530686{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700687 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530688
Amit Blay6fa647a2012-05-24 14:12:08 +0300689 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530690 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530691
Jack Pham69e621d2012-06-25 18:48:07 -0700692 if (atomic_read(&motg->in_lpm) == suspend)
693 return 0;
694
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530695 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700696 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530697 case OTG_STATE_A_WAIT_BCON:
698 if (TA_WAIT_BCON > 0)
699 break;
700 /* fall through */
701 case OTG_STATE_A_HOST:
702 pr_debug("host bus suspend\n");
703 clear_bit(A_BUS_REQ, &motg->inputs);
704 queue_work(system_nrt_wq, &motg->sm_work);
705 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300706 case OTG_STATE_B_PERIPHERAL:
707 pr_debug("peripheral bus suspend\n");
708 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
709 break;
710 set_bit(A_BUS_SUSPEND, &motg->inputs);
711 queue_work(system_nrt_wq, &motg->sm_work);
712 break;
713
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530714 default:
715 break;
716 }
717 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700718 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530719 case OTG_STATE_A_SUSPEND:
720 /* Remote wakeup or resume */
721 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700722 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700723
724 /* ensure hardware is not in low power mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700725 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530726 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300727 case OTG_STATE_B_PERIPHERAL:
728 pr_debug("peripheral bus resume\n");
729 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
730 break;
731 clear_bit(A_BUS_SUSPEND, &motg->inputs);
732 queue_work(system_nrt_wq, &motg->sm_work);
733 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530734 default:
735 break;
736 }
737 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530738 return 0;
739}
740
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530741#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530742#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
743
744#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530745static int msm_otg_suspend(struct msm_otg *motg)
746{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200747 struct usb_phy *phy = &motg->phy;
748 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530749 struct msm_otg_platform_data *pdata = motg->pdata;
750 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300751 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530752 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800753 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530754 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530755
756 if (atomic_read(&motg->in_lpm))
757 return 0;
758
759 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530760 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
761 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700762 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300763 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
764 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530765 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham6ff217b2012-08-13 15:34:20 -0700766
Pavankumar Kondetib91ab712012-10-22 13:21:19 +0530767 /*
768 * Abort suspend when,
769 * 1. charging detection in progress due to cable plug-in
770 * 2. host mode activation in progress due to Micro-A cable insertion
771 */
772
773 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
774 !dcp) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham6ff217b2012-08-13 15:34:20 -0700775 enable_irq(motg->irq);
776 return -EBUSY;
777 }
778
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530779 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530780 * Chipidea 45-nm PHY suspend sequence:
781 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530782 * Interrupt Latch Register auto-clear feature is not present
783 * in all PHY versions. Latch register is clear on read type.
784 * Clear latch register to avoid spurious wakeup from
785 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530786 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530787 * PHY comparators are disabled when PHY enters into low power
788 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
789 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
790 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530791 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530792 * PLL is not turned off when PHY enters into low power mode (LPM).
793 * Disable PLL for maximum power savings.
794 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530795
796 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200797 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530798 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200799 ulpi_write(phy, 0x01, 0x30);
800 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530801 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530802
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700803
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530804 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530805 * PHY may take some time or even fail to enter into low power
806 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
807 * in failure case.
808 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530809 portsc = readl_relaxed(USB_PORTSC);
810 if (!(portsc & PORTSC_PHCD)) {
811 writel_relaxed(portsc | PORTSC_PHCD,
812 USB_PORTSC);
813 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
814 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
815 break;
816 udelay(1);
817 cnt++;
818 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530819 }
820
821 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200822 dev_err(phy->dev, "Unable to suspend PHY\n");
823 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530824 enable_irq(motg->irq);
825 return -ETIMEDOUT;
826 }
827
828 /*
829 * PHY has capability to generate interrupt asynchronously in low
830 * power mode (LPM). This interrupt is level triggered. So USB IRQ
831 * line must be disabled till async interrupt enable bit is cleared
832 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
833 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530834 *
835 * PHY retention mode is disallowed while entering to LPM with wall
836 * charger connected. But PHY is put into suspend mode. Hence
837 * enable asynchronous interrupt to detect charger disconnection when
838 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530839 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530840 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300841 if (host_bus_suspend || device_bus_suspend ||
842 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530843 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
844 else
845 cmd_val |= ULPI_STP_CTRL;
846 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530847
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530848 /*
849 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
850 * PHY retention and collapse can not happen with VDP_SRC enabled.
851 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300852 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
853 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200854 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
855 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
856 /* Enable PHY HV interrupts to wake MPM/Link */
857 phy_ctrl_val |=
858 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530859
Amit Blay58b31472011-11-18 09:39:39 +0200860 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530862 }
863
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700864 /* Ensure that above operation is completed before turning off clocks */
865 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300866 /* Consider clocks on workaround flag only in case of bus suspend */
867 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
868 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
869 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300870 clk_disable_unprepare(motg->pclk);
871 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300872 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300873 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530874
Anji jonnala7da3f262011-12-02 17:22:14 -0800875 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530876 if (!host_bus_suspend) {
877 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
878 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700879 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530880 "TCXO D0 buffer%d\n", __func__, ret);
881 else
882 motg->lpm_flags |= XO_SHUTDOWN;
883 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800884
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530885 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
886 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 msm_hsusb_ldo_enable(motg, 0);
888 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530889 }
890
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530891 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530893 msm_hsusb_mhl_switch_enable(motg, 0);
894 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700895
Steve Mucklef132c6c2012-06-06 18:30:57 -0700896 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530897 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700898 if (motg->async_irq)
899 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 if (motg->pdata->pmic_id_irq)
901 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700902 if (pdata->otg_control == OTG_PHY_CONTROL &&
903 pdata->mpm_otgsessvld_int)
904 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530906 if (bus)
907 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
908
909 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -0700910 /* Enable ASYNC IRQ (if present) during LPM */
911 if (motg->async_irq)
912 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530913 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530915
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200916 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530917
918 return 0;
919}
920
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530921static int msm_otg_resume(struct msm_otg *motg)
922{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200923 struct usb_phy *phy = &motg->phy;
924 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700925 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530926 int cnt = 0;
927 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200928 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800929 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530930
931 if (!atomic_read(&motg->in_lpm))
932 return 0;
933
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700934 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530935
Anji jonnala7da3f262011-12-02 17:22:14 -0800936 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530937 if (motg->lpm_flags & XO_SHUTDOWN) {
938 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
939 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700940 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530941 "TCXO D0 buffer%d\n", __func__, ret);
942 motg->lpm_flags &= ~XO_SHUTDOWN;
943 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530944
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300945 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300946 clk_prepare_enable(motg->core_clk);
947 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300948 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300949 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530950
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700951 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
952 msm_hsusb_ldo_enable(motg, 1);
953 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
954 }
955
956 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530957 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530958 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200959 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
960 phy_ctrl_val |= PHY_RETEN;
961 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
962 /* Disable PHY HV interrupts */
963 phy_ctrl_val &=
964 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
965 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530967 }
968
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530969 temp = readl(USB_USBCMD);
970 temp &= ~ASYNC_INTR_CTRL;
971 temp &= ~ULPI_STP_CTRL;
972 writel(temp, USB_USBCMD);
973
974 /*
975 * PHY comes out of low power mode (LPM) in case of wakeup
976 * from asynchronous interrupt.
977 */
978 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
979 goto skip_phy_resume;
980
981 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
982 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
983 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
984 break;
985 udelay(1);
986 cnt++;
987 }
988
989 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
990 /*
991 * This is a fatal error. Reset the link and
992 * PHY. USB state can not be restored. Re-insertion
993 * of USB cable is the only way to get USB working.
994 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200995 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530996 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200997 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530998 }
999
1000skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001001 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301002 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001003 if (motg->async_irq)
1004 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005 if (motg->pdata->pmic_id_irq)
1006 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001007 if (pdata->otg_control == OTG_PHY_CONTROL &&
1008 pdata->mpm_otgsessvld_int)
1009 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001010 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301011 if (bus)
1012 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1013
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301014 atomic_set(&motg->in_lpm, 0);
1015
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301016 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001017 /* Match the disable_irq call from ISR */
1018 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301019 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301020 }
1021
Manu Gautamf8c45642012-08-10 10:20:56 -07001022 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1023 if (motg->async_irq)
1024 disable_irq(motg->async_irq);
1025
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001026 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301027
1028 return 0;
1029}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301030#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301031
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001032static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1033{
1034 if (!psy)
1035 goto psy_not_supported;
1036
1037 if (host_mode)
1038 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1039 else
1040 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1041
1042psy_not_supported:
1043 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1044 return -ENXIO;
1045}
1046
David Keitel081a3e22012-04-18 12:37:07 -07001047static int msm_otg_notify_chg_type(struct msm_otg *motg)
1048{
roy.parkd063ef32012-08-30 09:37:38 -07001049 int charger_type;
David Keitel081a3e22012-04-18 12:37:07 -07001050 /*
1051 * TODO
1052 * Unify OTG driver charger types and power supply charger types
1053 */
David Keitel081a3e22012-04-18 12:37:07 -07001054
1055 if (motg->chg_type == USB_SDP_CHARGER)
1056 charger_type = POWER_SUPPLY_TYPE_USB;
1057 else if (motg->chg_type == USB_CDP_CHARGER)
1058 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301059 else if (motg->chg_type == USB_DCP_CHARGER ||
1060 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001061 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1062 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1063 motg->chg_type == USB_ACA_A_CHARGER ||
1064 motg->chg_type == USB_ACA_B_CHARGER ||
1065 motg->chg_type == USB_ACA_C_CHARGER))
1066 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1067 else
1068 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1069
1070 return pm8921_set_usb_power_supply_type(charger_type);
1071}
1072
Amit Blay0f7edf72012-01-15 10:11:27 +02001073static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1074{
Amit Blay0f7edf72012-01-15 10:11:27 +02001075
Amit Blay0f7edf72012-01-15 10:11:27 +02001076 if (!psy)
1077 goto psy_not_supported;
1078
1079 if (motg->cur_power == 0 && mA > 0) {
1080 /* Enable charging */
1081 if (power_supply_set_online(psy, true))
1082 goto psy_not_supported;
1083 } else if (motg->cur_power > 0 && mA == 0) {
1084 /* Disable charging */
1085 if (power_supply_set_online(psy, false))
1086 goto psy_not_supported;
1087 return 0;
1088 }
1089 /* Set max current limit */
Devin Kim81265542012-09-01 05:06:22 -07001090 dev_info(motg->phy.dev, "current: %d -> %d (mA)\n",
1091 motg->cur_power, mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001092 if (power_supply_set_current_limit(psy, 1000*mA))
1093 goto psy_not_supported;
1094
1095 return 0;
1096
1097psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001098 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001099 return -ENXIO;
1100}
1101
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301102static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1103{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001104 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301105
1106 if (g && g->is_a_peripheral)
1107 return;
1108
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301109 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1110 motg->chg_type == USB_ACA_A_CHARGER ||
1111 motg->chg_type == USB_ACA_B_CHARGER ||
1112 motg->chg_type == USB_ACA_C_CHARGER) &&
1113 mA > IDEV_ACA_CHG_LIMIT)
1114 mA = IDEV_ACA_CHG_LIMIT;
1115
David Keitel081a3e22012-04-18 12:37:07 -07001116 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001117 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001118 "Failed notifying %d charger type to PMIC\n",
1119 motg->chg_type);
1120
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301121 if (motg->cur_power == mA)
1122 return;
1123
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001124 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001125
Devin Kim2073afb2012-09-05 13:01:51 -07001126 pm8921_charger_vbus_draw(mA);
1127 msm_otg_notify_power_supply(motg, mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001128
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301129 motg->cur_power = mA;
1130}
1131
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001132static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301133{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001134 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301135
1136 /*
1137 * Gadget driver uses set_power method to notify about the
1138 * available current based on suspend/configured states.
1139 *
1140 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1141 * states when CDP/ACA is connected.
1142 */
1143 if (motg->chg_type == USB_SDP_CHARGER)
1144 msm_otg_notify_charger(motg, mA);
1145
1146 return 0;
1147}
1148
Steve Mucklef132c6c2012-06-06 18:30:57 -07001149static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301150{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001151 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301152 struct msm_otg_platform_data *pdata = motg->pdata;
1153 struct usb_hcd *hcd;
1154
1155 if (!otg->host)
1156 return;
1157
1158 hcd = bus_to_hcd(otg->host);
1159
1160 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001161 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301162
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301163 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001164 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301165 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1166
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301167 /*
1168 * Some boards have a switch cotrolled by gpio
1169 * to enable/disable internal HUB. Enable internal
1170 * HUB before kicking the host.
1171 */
1172 if (pdata->setup_gpio)
1173 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301174 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301175 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001176 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301177
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301178 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301179 /* HCD core reset all bits of PORTSC. select ULPI phy */
1180 writel_relaxed(0x80000000, USB_PORTSC);
1181
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301182 if (pdata->setup_gpio)
1183 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301184
1185 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001186 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301187 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301188 }
1189}
1190
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001191static int msm_otg_usbdev_notify(struct notifier_block *self,
1192 unsigned long action, void *priv)
1193{
1194 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001195 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301196 struct usb_device *udev = priv;
1197
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301198 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1199 goto out;
1200
Steve Mucklef132c6c2012-06-06 18:30:57 -07001201 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301202 goto out;
1203 /*
1204 * Interested in devices connected directly to the root hub.
1205 * ACA dock can supply IDEV_CHG irrespective devices connected
1206 * on the accessory port.
1207 */
1208 if (!udev->parent || udev->parent->parent ||
1209 motg->chg_type == USB_ACA_DOCK_CHARGER)
1210 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211
1212 switch (action) {
1213 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301214 if (aca_enabled())
1215 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001216 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301217 pr_debug("B_CONN set\n");
1218 set_bit(B_CONN, &motg->inputs);
1219 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001220 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301221 /*
1222 * OTG PET: A-device must end session within
1223 * 10 sec after PET enumeration.
1224 */
1225 if (udev->quirks & USB_QUIRK_OTG_PET)
1226 msm_otg_start_timer(motg, TA_TST_MAINT,
1227 A_TST_MAINT);
1228 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301229 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 if (udev->actconfig)
1232 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1233 else
1234 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001235 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301236 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301237 break;
1238 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001239 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1240 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301241 pr_debug("B_CONN clear\n");
1242 clear_bit(B_CONN, &motg->inputs);
1243 /*
1244 * OTG PET: A-device must end session after
1245 * PET disconnection if it is enumerated
1246 * with bcdDevice[0] = 1. USB core sets
1247 * bus->otg_vbus_off for us. clear it here.
1248 */
1249 if (udev->bus->otg_vbus_off) {
1250 udev->bus->otg_vbus_off = 0;
1251 set_bit(A_BUS_DROP, &motg->inputs);
1252 }
1253 queue_work(system_nrt_wq, &motg->sm_work);
1254 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255 default:
1256 break;
1257 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301258 if (test_bit(ID_A, &motg->inputs))
1259 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1260 motg->mA_port);
1261out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001262 return NOTIFY_OK;
1263}
1264
Mayank Ranae3926882011-12-26 09:47:54 +05301265static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1266{
1267 int ret;
1268 static bool vbus_is_on;
1269
1270 if (vbus_is_on == on)
1271 return;
1272
1273 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301274 ret = motg->pdata->vbus_power(on);
1275 if (!ret)
1276 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301277 return;
1278 }
1279
1280 if (!vbus_otg) {
1281 pr_err("vbus_otg is NULL.");
1282 return;
1283 }
1284
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001285 /*
1286 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001287 * from usb before turning on the boost.
1288 * if exiting host mode disable the boost before enabling to draw
1289 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001290 */
Mayank Ranae3926882011-12-26 09:47:54 +05301291 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001292 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301293 ret = regulator_enable(vbus_otg);
1294 if (ret) {
1295 pr_err("unable to enable vbus_otg\n");
1296 return;
1297 }
1298 vbus_is_on = true;
1299 } else {
1300 ret = regulator_disable(vbus_otg);
1301 if (ret) {
1302 pr_err("unable to disable vbus_otg\n");
1303 return;
1304 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001305 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301306 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301307 }
1308}
1309
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001310static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301311{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001312 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301313 struct usb_hcd *hcd;
1314
1315 /*
1316 * Fail host registration if this board can support
1317 * only peripheral configuration.
1318 */
1319 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001320 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301321 return -ENODEV;
1322 }
1323
Devin Kimd10f03c2012-06-25 17:05:13 -07001324 if (!machine_is_apq8064_mako()) {
1325 if (!motg->pdata->vbus_power && host) {
1326 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1327 if (IS_ERR(vbus_otg)) {
1328 pr_err("Unable to get vbus_otg\n");
1329 return -ENODEV;
1330 }
Mayank Ranae3926882011-12-26 09:47:54 +05301331 }
1332 }
1333
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301334 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001335 if (otg->phy->state == OTG_STATE_A_HOST) {
1336 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001337 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301338 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301339 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301340 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001341 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301342 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301343 } else {
1344 otg->host = NULL;
1345 }
1346
1347 return 0;
1348 }
1349
1350 hcd = bus_to_hcd(host);
1351 hcd->power_budget = motg->pdata->power_budget;
1352
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301353#ifdef CONFIG_USB_OTG
1354 host->otg_port = 1;
1355#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001356 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1357 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301358 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001359 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301360
1361 /*
1362 * Kick the state machine work, if peripheral is not supported
1363 * or peripheral is already registered with us.
1364 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301365 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001366 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301367 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301368 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301369
1370 return 0;
1371}
1372
Steve Mucklef132c6c2012-06-06 18:30:57 -07001373static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301374{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301375 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001376 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301377 struct msm_otg_platform_data *pdata = motg->pdata;
1378
1379 if (!otg->gadget)
1380 return;
1381
1382 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001383 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301384 /*
1385 * Some boards have a switch cotrolled by gpio
1386 * to enable/disable internal HUB. Disable internal
1387 * HUB before kicking the gadget.
1388 */
1389 if (pdata->setup_gpio)
1390 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001391
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301392 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301393 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301394 ret = msm_bus_scale_client_update_request(
1395 motg->bus_perf_client, 1);
1396 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001397 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301398 "bus bandwidth %d\n", __func__, ret);
1399 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301400 usb_gadget_vbus_connect(otg->gadget);
1401 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001402 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301403 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301404 /* Configure BUS performance parameters to default */
1405 if (motg->bus_perf_client) {
1406 ret = msm_bus_scale_client_update_request(
1407 motg->bus_perf_client, 0);
1408 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001409 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301410 "for bus bw %d\n", __func__, ret);
1411 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301412 if (pdata->setup_gpio)
1413 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1414 }
1415
1416}
1417
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001418static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301419 struct usb_gadget *gadget)
1420{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001421 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301422
1423 /*
1424 * Fail peripheral registration if this board can support
1425 * only host configuration.
1426 */
1427 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001428 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301429 return -ENODEV;
1430 }
1431
1432 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001433 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1434 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301435 msm_otg_start_peripheral(otg, 0);
1436 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001437 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301438 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301439 } else {
1440 otg->gadget = NULL;
1441 }
1442
1443 return 0;
1444 }
1445 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001446 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301447
1448 /*
1449 * Kick the state machine work, if host is not supported
1450 * or host is already registered with us.
1451 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301452 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001453 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301454 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301455 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301456
1457 return 0;
1458}
1459
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301460static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1461{
1462 unsigned long flags;
1463 int id;
1464
1465 if (!motg->pdata->pmic_id_irq)
1466 return -ENODEV;
1467
1468 local_irq_save(flags);
1469 id = irq_read_line(motg->pdata->pmic_id_irq);
1470 local_irq_restore(flags);
1471
1472 /*
1473 * If we can not read ID line state for some reason, treat
1474 * it as float. This would prevent MHL discovery and kicking
1475 * host mode unnecessarily.
1476 */
1477 return !!id;
1478}
1479
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301480static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1481 void (*callback)(int on))
1482{
1483 struct usb_phy *phy = &motg->phy;
1484 int ret;
1485
1486 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1487 !motg->pdata->pmic_id_irq) {
1488 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1489 return -ENODEV;
1490 }
1491
1492 if (!motg->pdata->mhl_dev_name) {
1493 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1494 return -ENODEV;
1495 }
1496
1497 if (callback)
1498 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1499 callback);
1500 else
1501 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1502
1503 if (ret)
1504 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1505 motg->pdata->mhl_dev_name, ret);
1506 else
1507 motg->mhl_enabled = true;
1508
1509 return ret;
1510}
1511
1512static void msm_otg_mhl_notify_online(int on)
1513{
1514 struct msm_otg *motg = the_msm_otg;
1515 struct usb_phy *phy = &motg->phy;
1516 bool queue = false;
1517
1518 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1519
1520 if (on) {
1521 set_bit(MHL, &motg->inputs);
1522 } else {
1523 clear_bit(MHL, &motg->inputs);
1524 queue = true;
1525 }
1526
1527 if (queue && phy->state != OTG_STATE_UNDEFINED)
1528 schedule_work(&motg->sm_work);
1529}
1530
1531static bool msm_otg_is_mhl(struct msm_otg *motg)
1532{
1533 struct usb_phy *phy = &motg->phy;
1534 int is_mhl, ret;
1535
1536 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1537 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1538 /*
1539 * MHL driver calls our callback saying that MHL connected
1540 * if RID_GND is detected. But at later part of discovery
1541 * it may figure out MHL is not connected and returns
1542 * false. Hence clear MHL input here.
1543 */
1544 clear_bit(MHL, &motg->inputs);
1545 dev_dbg(phy->dev, "MHL device not found\n");
1546 return false;
1547 }
1548
1549 set_bit(MHL, &motg->inputs);
1550 dev_dbg(phy->dev, "MHL device found\n");
1551 return true;
1552}
1553
1554static bool msm_chg_mhl_detect(struct msm_otg *motg)
1555{
1556 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301557
1558 if (!motg->mhl_enabled)
1559 return false;
1560
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301561 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301562
1563 if (id)
1564 return false;
1565
1566 mhl_det_in_progress = true;
1567 ret = msm_otg_is_mhl(motg);
1568 mhl_det_in_progress = false;
1569
1570 return ret;
1571}
1572
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001573static bool msm_chg_aca_detect(struct msm_otg *motg)
1574{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001575 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001576 u32 int_sts;
1577 bool ret = false;
1578
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301579 if (!aca_enabled())
1580 goto out;
1581
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001582 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1583 goto out;
1584
Steve Mucklef132c6c2012-06-06 18:30:57 -07001585 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001586 switch (int_sts & 0x1C) {
1587 case 0x08:
1588 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001589 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001590 motg->chg_type = USB_ACA_A_CHARGER;
1591 motg->chg_state = USB_CHG_STATE_DETECTED;
1592 clear_bit(ID_B, &motg->inputs);
1593 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301594 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001595 ret = true;
1596 }
1597 break;
1598 case 0x0C:
1599 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001600 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001601 motg->chg_type = USB_ACA_B_CHARGER;
1602 motg->chg_state = USB_CHG_STATE_DETECTED;
1603 clear_bit(ID_A, &motg->inputs);
1604 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301605 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001606 ret = true;
1607 }
1608 break;
1609 case 0x10:
1610 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001611 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001612 motg->chg_type = USB_ACA_C_CHARGER;
1613 motg->chg_state = USB_CHG_STATE_DETECTED;
1614 clear_bit(ID_A, &motg->inputs);
1615 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301616 set_bit(ID, &motg->inputs);
1617 ret = true;
1618 }
1619 break;
1620 case 0x04:
1621 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001622 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301623 motg->chg_type = USB_INVALID_CHARGER;
1624 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1625 clear_bit(ID_A, &motg->inputs);
1626 clear_bit(ID_B, &motg->inputs);
1627 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001628 ret = true;
1629 }
1630 break;
1631 default:
1632 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1633 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301634 test_and_clear_bit(ID_C, &motg->inputs) |
1635 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001636 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001637 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001638 motg->chg_type = USB_INVALID_CHARGER;
1639 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1640 }
1641 }
1642out:
1643 return ret;
1644}
1645
1646static void msm_chg_enable_aca_det(struct msm_otg *motg)
1647{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001648 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001649
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301650 if (!aca_enabled())
1651 return;
1652
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001653 switch (motg->pdata->phy_type) {
1654 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301655 /* Disable ID_GND in link and PHY */
1656 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1657 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001658 ulpi_write(phy, 0x01, 0x0C);
1659 ulpi_write(phy, 0x10, 0x0F);
1660 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301661 /* Disable PMIC ID pull-up */
1662 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301663 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001664 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301665 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666 break;
1667 default:
1668 break;
1669 }
1670}
1671
1672static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1673{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001674 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301676 if (!aca_enabled())
1677 return;
1678
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001679 switch (motg->pdata->phy_type) {
1680 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301681 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001682 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301683 break;
1684 default:
1685 break;
1686 }
1687}
1688
1689static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1690{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001691 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301692
1693 if (!aca_enabled())
1694 return;
1695
1696 switch (motg->pdata->phy_type) {
1697 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001698 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001699 break;
1700 default:
1701 break;
1702 }
1703}
1704
1705static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1706{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001707 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001708 bool ret = false;
1709
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301710 if (!aca_enabled())
1711 return ret;
1712
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713 switch (motg->pdata->phy_type) {
1714 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001715 if (ulpi_read(phy, 0x91) & 1) {
1716 dev_dbg(phy->dev, "RID change\n");
1717 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001718 ret = msm_chg_aca_detect(motg);
1719 }
1720 default:
1721 break;
1722 }
1723 return ret;
1724}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301725
1726static void msm_otg_id_timer_func(unsigned long data)
1727{
1728 struct msm_otg *motg = (struct msm_otg *) data;
1729
1730 if (!aca_enabled())
1731 return;
1732
1733 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001734 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301735 return;
1736 }
1737
Steve Mucklef132c6c2012-06-06 18:30:57 -07001738 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301739 goto out;
1740
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301741 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001742 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301743 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301744 }
1745
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301746out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301747 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1748 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1749}
1750
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301751static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1752{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001753 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301754 u32 chg_det;
1755 bool ret = false;
1756
1757 switch (motg->pdata->phy_type) {
1758 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001759 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301760 ret = chg_det & (1 << 4);
1761 break;
1762 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001763 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301764 ret = chg_det & 1;
1765 break;
1766 default:
1767 break;
1768 }
1769 return ret;
1770}
1771
1772static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1773{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001774 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301775 u32 chg_det;
1776
1777 switch (motg->pdata->phy_type) {
1778 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001779 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301780 /* Turn off charger block */
1781 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001782 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301783 udelay(20);
1784 /* control chg block via ULPI */
1785 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001786 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301787 /* put it in host mode for enabling D- source */
1788 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001789 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301790 /* Turn on chg detect block */
1791 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001792 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301793 udelay(20);
1794 /* enable chg detection */
1795 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001796 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301797 break;
1798 case SNPS_28NM_INTEGRATED_PHY:
1799 /*
1800 * Configure DM as current source, DP as current sink
1801 * and enable battery charging comparators.
1802 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001803 ulpi_write(phy, 0x8, 0x85);
1804 ulpi_write(phy, 0x2, 0x85);
1805 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301806 break;
1807 default:
1808 break;
1809 }
1810}
1811
1812static bool msm_chg_check_primary_det(struct msm_otg *motg)
1813{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001814 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301815 u32 chg_det;
1816 bool ret = false;
1817
1818 switch (motg->pdata->phy_type) {
1819 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001820 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301821 ret = chg_det & (1 << 4);
1822 break;
1823 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001824 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301825 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301826 /* Turn off VDP_SRC */
1827 ulpi_write(phy, 0x3, 0x86);
1828 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301829 break;
1830 default:
1831 break;
1832 }
1833 return ret;
1834}
1835
1836static void msm_chg_enable_primary_det(struct msm_otg *motg)
1837{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001838 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301839 u32 chg_det;
1840
1841 switch (motg->pdata->phy_type) {
1842 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001843 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301844 /* enable chg detection */
1845 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001846 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301847 break;
1848 case SNPS_28NM_INTEGRATED_PHY:
1849 /*
1850 * Configure DP as current source, DM as current sink
1851 * and enable battery charging comparators.
1852 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001853 ulpi_write(phy, 0x2, 0x85);
1854 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301855 break;
1856 default:
1857 break;
1858 }
1859}
1860
1861static bool msm_chg_check_dcd(struct msm_otg *motg)
1862{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001863 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301864 u32 line_state;
1865 bool ret = false;
1866
1867 switch (motg->pdata->phy_type) {
1868 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001869 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301870 ret = !(line_state & 1);
1871 break;
1872 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001873 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301874 ret = line_state & 2;
1875 break;
1876 default:
1877 break;
1878 }
1879 return ret;
1880}
1881
1882static void msm_chg_disable_dcd(struct msm_otg *motg)
1883{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001884 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301885 u32 chg_det;
1886
1887 switch (motg->pdata->phy_type) {
1888 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001889 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301890 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001891 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301892 break;
1893 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001894 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301895 break;
1896 default:
1897 break;
1898 }
1899}
1900
1901static void msm_chg_enable_dcd(struct msm_otg *motg)
1902{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001903 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301904 u32 chg_det;
1905
1906 switch (motg->pdata->phy_type) {
1907 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001908 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301909 /* Turn on D+ current source */
1910 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001911 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301912 break;
1913 case SNPS_28NM_INTEGRATED_PHY:
1914 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001915 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301916 break;
1917 default:
1918 break;
1919 }
1920}
1921
1922static void msm_chg_block_on(struct msm_otg *motg)
1923{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001924 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301925 u32 func_ctrl, chg_det;
1926
1927 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001928 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301929 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1930 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001931 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301932
1933 switch (motg->pdata->phy_type) {
1934 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001935 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301936 /* control chg block via ULPI */
1937 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001938 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301939 /* Turn on chg detect block */
1940 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001941 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301942 udelay(20);
1943 break;
1944 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05301945 /* disable DP and DM pull down resistors */
1946 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301947 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001948 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301949 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001950 ulpi_write(phy, 0x1F, 0x92);
1951 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301952 udelay(100);
1953 break;
1954 default:
1955 break;
1956 }
1957}
1958
1959static void msm_chg_block_off(struct msm_otg *motg)
1960{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001961 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301962 u32 func_ctrl, chg_det;
1963
1964 switch (motg->pdata->phy_type) {
1965 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001966 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301967 /* Turn off charger block */
1968 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001969 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301970 break;
1971 case SNPS_28NM_INTEGRATED_PHY:
1972 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001973 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301974 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001975 ulpi_write(phy, 0x1F, 0x92);
1976 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301977 break;
1978 default:
1979 break;
1980 }
1981
1982 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001983 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301984 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1985 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001986 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301987}
1988
Anji jonnalad270e2d2011-08-09 11:28:32 +05301989static const char *chg_to_string(enum usb_chg_type chg_type)
1990{
1991 switch (chg_type) {
1992 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1993 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1994 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1995 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1996 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1997 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1998 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301999 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302000 default: return "INVALID_CHARGER";
2001 }
2002}
kibum.leeec040442012-08-22 22:29:46 +09002003
Devin Kimd93ff032012-09-14 15:01:31 -07002004#define MSM_CHECK_TA_DELAY (5 * HZ)
2005#define PORTSC_LS (3 << 10) /* Read - Port's Line status */
kibum.leeec040442012-08-22 22:29:46 +09002006static void msm_ta_detect_work(struct work_struct *w)
2007{
Devin Kimd93ff032012-09-14 15:01:31 -07002008 struct msm_otg *motg = container_of(w, struct msm_otg, check_ta_work.work);
kibum.leeec040442012-08-22 22:29:46 +09002009 struct usb_otg *otg = motg->phy.otg;
kibum.leeec040442012-08-22 22:29:46 +09002010
Devin Kim6b8a5d12012-10-16 23:12:18 -07002011 pr_debug("msm_ta_detect_work: ta detection work\n");
Devin Kimd93ff032012-09-14 15:01:31 -07002012
2013 /* Presence of FRame Index or FRINDEX rollover implies USB communication */
2014 if( (readl(USB_FRINDEX) != 0) || ( readl(USB_USBSTS) & (1<<3) ) ) {
2015 pr_info("msm_ta_detect_work: USB exit ta detection - frindex\n");
2016 return;
2017 }
2018
2019 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
2020 pr_info("msm_ta_detect_work: ta dectection success\n");
kibum.leeec040442012-08-22 22:29:46 +09002021 /* inform to user space that SDP is no longer detected */
2022 msm_otg_notify_charger(motg, 0);
2023 motg->chg_state = USB_CHG_STATE_DETECTED;
2024 motg->chg_type = USB_DCP_CHARGER;
2025 motg->cur_power = 0;
2026 msm_otg_start_peripheral(otg, 0);
2027 otg->phy->state = OTG_STATE_B_IDLE;
2028 schedule_work(&motg->sm_work);
2029 return;
kibum.leeec040442012-08-22 22:29:46 +09002030 }
Devin Kimd93ff032012-09-14 15:01:31 -07002031 schedule_delayed_work(&motg->check_ta_work, MSM_CHECK_TA_DELAY);
kibum.leeec040442012-08-22 22:29:46 +09002032}
Anji jonnalad270e2d2011-08-09 11:28:32 +05302033
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302034#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
2035#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302036#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2037#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302038static void msm_chg_detect_work(struct work_struct *w)
2039{
2040 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002041 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302042 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302043 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302044 unsigned long delay;
2045
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002046 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302047
2048 if (test_bit(MHL, &motg->inputs)) {
2049 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2050 return;
2051 }
2052
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302053 switch (motg->chg_state) {
2054 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302055 msm_chg_block_on(motg);
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302056 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002057 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302058 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2059 motg->dcd_retries = 0;
2060 delay = MSM_CHG_DCD_POLL_TIME;
2061 break;
2062 case USB_CHG_STATE_WAIT_FOR_DCD:
jh.koo78df86f2012-09-04 20:13:19 +09002063 if (slimport_is_connected()) {
2064 msm_chg_block_off(motg);
2065 motg->chg_state = USB_CHG_STATE_DETECTED;
2066 motg->chg_type = USB_SDP_CHARGER;
2067 queue_work(system_nrt_wq, &motg->sm_work);
2068 return;
2069 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302070 if (msm_chg_mhl_detect(motg)) {
2071 msm_chg_block_off(motg);
2072 motg->chg_state = USB_CHG_STATE_DETECTED;
2073 motg->chg_type = USB_INVALID_CHARGER;
2074 queue_work(system_nrt_wq, &motg->sm_work);
2075 return;
2076 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002077 is_aca = msm_chg_aca_detect(motg);
2078 if (is_aca) {
2079 /*
2080 * ID_A can be ACA dock too. continue
2081 * primary detection after DCD.
2082 */
2083 if (test_bit(ID_A, &motg->inputs)) {
2084 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2085 } else {
2086 delay = 0;
2087 break;
2088 }
2089 }
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302090 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302091 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2092 if (is_dcd || tmout) {
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302093 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302094 msm_chg_enable_primary_det(motg);
2095 delay = MSM_CHG_PRIMARY_DET_TIME;
2096 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2097 } else {
2098 delay = MSM_CHG_DCD_POLL_TIME;
2099 }
2100 break;
2101 case USB_CHG_STATE_DCD_DONE:
2102 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302103 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2104 dm_vlgc = line_state & PORTSC_LS_DM;
2105 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302106 if (test_bit(ID_A, &motg->inputs)) {
2107 motg->chg_type = USB_ACA_DOCK_CHARGER;
2108 motg->chg_state = USB_CHG_STATE_DETECTED;
2109 delay = 0;
2110 break;
2111 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302112 if (line_state) { /* DP > VLGC */
2113 motg->chg_type = USB_PROPRIETARY_CHARGER;
2114 motg->chg_state = USB_CHG_STATE_DETECTED;
2115 delay = 0;
2116 } else {
2117 msm_chg_enable_secondary_det(motg);
2118 delay = MSM_CHG_SECONDARY_DET_TIME;
2119 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2120 }
2121 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302122 if (test_bit(ID_A, &motg->inputs)) {
2123 motg->chg_type = USB_ACA_A_CHARGER;
2124 motg->chg_state = USB_CHG_STATE_DETECTED;
2125 delay = 0;
2126 break;
2127 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302128
2129 if (line_state) /* DP > VLGC or/and DM > VLGC */
2130 motg->chg_type = USB_PROPRIETARY_CHARGER;
2131 else
2132 motg->chg_type = USB_SDP_CHARGER;
2133
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302134 motg->chg_state = USB_CHG_STATE_DETECTED;
2135 delay = 0;
2136 }
2137 break;
2138 case USB_CHG_STATE_PRIMARY_DONE:
2139 vout = msm_chg_check_secondary_det(motg);
2140 if (vout)
2141 motg->chg_type = USB_DCP_CHARGER;
2142 else
2143 motg->chg_type = USB_CDP_CHARGER;
2144 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2145 /* fall through */
2146 case USB_CHG_STATE_SECONDARY_DONE:
2147 motg->chg_state = USB_CHG_STATE_DETECTED;
2148 case USB_CHG_STATE_DETECTED:
2149 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002150 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302151 /*
2152 * Spurious interrupt is seen after enabling ACA detection
2153 * due to which charger detection fails in case of PET.
2154 * Add delay of 100 microsec to avoid that.
2155 */
2156 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002158 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302159 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302160 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302161 return;
2162 default:
2163 return;
2164 }
2165
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302166 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302167}
2168
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302169/*
2170 * We support OTG, Peripheral only and Host only configurations. In case
2171 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2172 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2173 * enabled when switch is controlled by user and default mode is supplied
2174 * by board file, which can be changed by userspace later.
2175 */
2176static void msm_otg_init_sm(struct msm_otg *motg)
2177{
2178 struct msm_otg_platform_data *pdata = motg->pdata;
2179 u32 otgsc = readl(USB_OTGSC);
2180
2181 switch (pdata->mode) {
2182 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002183 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302184 if (pdata->default_mode == USB_HOST) {
2185 clear_bit(ID, &motg->inputs);
2186 } else if (pdata->default_mode == USB_PERIPHERAL) {
2187 set_bit(ID, &motg->inputs);
2188 set_bit(B_SESS_VLD, &motg->inputs);
2189 } else {
2190 set_bit(ID, &motg->inputs);
2191 clear_bit(B_SESS_VLD, &motg->inputs);
2192 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302193 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302194 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302195 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302196 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302197 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302198 set_bit(A_BUS_REQ, &motg->inputs);
2199 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002200 if (otgsc & OTGSC_BSV)
2201 set_bit(B_SESS_VLD, &motg->inputs);
2202 else
2203 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302204 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302205 if (pdata->pmic_id_irq) {
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302206 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302207 set_bit(ID, &motg->inputs);
2208 else
2209 clear_bit(ID, &motg->inputs);
2210 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302211 /*
2212 * VBUS initial state is reported after PMIC
2213 * driver initialization. Wait for it.
2214 */
2215 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302216 }
2217 break;
2218 case USB_HOST:
2219 clear_bit(ID, &motg->inputs);
2220 break;
2221 case USB_PERIPHERAL:
2222 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302223 if (pdata->otg_control == OTG_PHY_CONTROL) {
2224 if (otgsc & OTGSC_BSV)
2225 set_bit(B_SESS_VLD, &motg->inputs);
2226 else
2227 clear_bit(B_SESS_VLD, &motg->inputs);
2228 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2229 /*
2230 * VBUS initial state is reported after PMIC
2231 * driver initialization. Wait for it.
2232 */
2233 wait_for_completion(&pmic_vbus_init);
2234 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302235 break;
2236 default:
2237 break;
2238 }
2239}
2240
2241static void msm_otg_sm_work(struct work_struct *w)
2242{
2243 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002244 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302245 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302246
Steve Mucklef132c6c2012-06-06 18:30:57 -07002247 pm_runtime_resume(otg->phy->dev);
2248 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002249 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302250 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002251 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302252 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302253 psy = power_supply_get_by_name("usb");
2254 if (!psy)
2255 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002256 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302257 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2258 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002259 pm_runtime_put_noidle(otg->phy->dev);
2260 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302261 break;
2262 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302263 /* FALL THROUGH */
2264 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302265 if (test_bit(MHL, &motg->inputs)) {
2266 /* allow LPM */
2267 pm_runtime_put_noidle(otg->phy->dev);
2268 pm_runtime_suspend(otg->phy->dev);
2269 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002270 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302271 pr_debug("!id || id_A\n");
jh.koo78df86f2012-09-04 20:13:19 +09002272 if (slimport_is_connected()) {
2273 work = 1;
2274 break;
2275 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302276 if (msm_chg_mhl_detect(motg)) {
2277 work = 1;
2278 break;
2279 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302280 clear_bit(B_BUS_REQ, &motg->inputs);
2281 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002282 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302283 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302284 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302285 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302286 switch (motg->chg_state) {
2287 case USB_CHG_STATE_UNDEFINED:
2288 msm_chg_detect_work(&motg->chg_work.work);
2289 break;
2290 case USB_CHG_STATE_DETECTED:
2291 switch (motg->chg_type) {
2292 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302293 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002294 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302295 /* fall through */
2296 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302297 msm_otg_notify_charger(motg,
2298 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002299 pm_runtime_put_noidle(otg->phy->dev);
2300 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302301 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302302 case USB_ACA_B_CHARGER:
2303 msm_otg_notify_charger(motg,
2304 IDEV_ACA_CHG_MAX);
2305 /*
2306 * (ID_B --> ID_C) PHY_ALT interrupt can
2307 * not be detected in LPM.
2308 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302309 break;
2310 case USB_CDP_CHARGER:
2311 msm_otg_notify_charger(motg,
2312 IDEV_CHG_MAX);
2313 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002314 otg->phy->state =
2315 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302316 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302317 case USB_ACA_C_CHARGER:
2318 msm_otg_notify_charger(motg,
2319 IDEV_ACA_CHG_MAX);
2320 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002321 otg->phy->state =
2322 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302323 break;
2324 case USB_SDP_CHARGER:
roy.park99cef212012-07-26 17:00:40 -07002325 msm_otg_notify_charger(motg,
2326 IDEV_CHG_MIN);
jh.koo78df86f2012-09-04 20:13:19 +09002327 if(!slimport_is_connected()) {
2328 msm_otg_start_peripheral(otg, 1);
2329 otg->phy->state =
2330 OTG_STATE_B_PERIPHERAL;
2331 }
kibum.leeec040442012-08-22 22:29:46 +09002332 schedule_delayed_work(&motg->check_ta_work,
2333 MSM_CHECK_TA_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302334 break;
2335 default:
2336 break;
2337 }
2338 break;
2339 default:
2340 break;
2341 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302342 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2343 pr_debug("b_sess_end && b_bus_req\n");
2344 if (msm_otg_start_srp(otg) < 0) {
2345 clear_bit(B_BUS_REQ, &motg->inputs);
2346 work = 1;
2347 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302348 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002349 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302350 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2351 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302352 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302353 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302354 cancel_delayed_work_sync(&motg->chg_work);
kibum.leeec040442012-08-22 22:29:46 +09002355 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302356 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2357 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302358 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002359 msm_otg_reset(otg->phy);
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302360 /*
2361 * There is a small window where ID interrupt
2362 * is not monitored during ID detection circuit
2363 * switch from ACA to PMIC. Check ID state
2364 * before entering into low power mode.
2365 */
2366 if (!msm_otg_read_pmic_id_state(motg)) {
2367 pr_debug("process missed ID intr\n");
2368 clear_bit(ID, &motg->inputs);
2369 work = 1;
2370 break;
2371 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002372 pm_runtime_put_noidle(otg->phy->dev);
2373 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302374 }
2375 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302376 case OTG_STATE_B_SRP_INIT:
2377 if (!test_bit(ID, &motg->inputs) ||
2378 test_bit(ID_A, &motg->inputs) ||
2379 test_bit(ID_C, &motg->inputs) ||
2380 (test_bit(B_SESS_VLD, &motg->inputs) &&
2381 !test_bit(ID_B, &motg->inputs))) {
2382 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2383 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002384 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302385 /*
2386 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2387 * bits after SRP initiation.
2388 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002389 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302390 work = 1;
2391 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2392 pr_debug("b_srp_fail\n");
2393 pr_info("A-device did not respond to SRP\n");
2394 clear_bit(B_BUS_REQ, &motg->inputs);
2395 clear_bit(B_SRP_FAIL, &motg->tmouts);
2396 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002397 ulpi_write(otg->phy, 0x0, 0x98);
2398 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302399 motg->b_last_se0_sess = jiffies;
2400 work = 1;
2401 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302402 break;
2403 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302404 if (!test_bit(ID, &motg->inputs) ||
2405 test_bit(ID_A, &motg->inputs) ||
2406 test_bit(ID_B, &motg->inputs) ||
2407 !test_bit(B_SESS_VLD, &motg->inputs)) {
2408 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302409 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2410 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302411 msm_otg_notify_charger(motg, 0);
2412 srp_reqd = otg->gadget->otg_srp_reqd;
2413 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302414 if (test_bit(ID_B, &motg->inputs))
2415 clear_bit(ID_B, &motg->inputs);
2416 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002417 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302418 motg->b_last_se0_sess = jiffies;
2419 if (srp_reqd)
2420 msm_otg_start_timer(motg,
2421 TB_TST_SRP, B_TST_SRP);
2422 else
2423 work = 1;
2424 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2425 otg->gadget->b_hnp_enable &&
2426 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2427 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2428 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2429 /* D+ pullup should not be disconnected within 4msec
2430 * after A device suspends the bus. Otherwise PET will
2431 * fail the compliance test.
2432 */
2433 udelay(1000);
2434 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002435 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302436 /*
2437 * start HCD even before A-device enable
2438 * pull-up to meet HNP timings.
2439 */
2440 otg->host->is_b_host = 1;
2441 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002442 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2443 test_bit(B_SESS_VLD, &motg->inputs)) {
2444 pr_debug("a_bus_suspend && b_sess_vld\n");
2445 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002446 pm_runtime_put_noidle(otg->phy->dev);
2447 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002448 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002449 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302450 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002451 }
2452 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302453 case OTG_STATE_B_WAIT_ACON:
2454 if (!test_bit(ID, &motg->inputs) ||
2455 test_bit(ID_A, &motg->inputs) ||
2456 test_bit(ID_B, &motg->inputs) ||
2457 !test_bit(B_SESS_VLD, &motg->inputs)) {
2458 pr_debug("!id || id_a/b || !b_sess_vld\n");
2459 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302460 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302461 * A-device is physically disconnected during
2462 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302463 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302464 msm_otg_start_host(otg, 0);
2465 otg->host->is_b_host = 0;
2466
2467 clear_bit(B_BUS_REQ, &motg->inputs);
2468 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2469 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002470 otg->phy->state = OTG_STATE_B_IDLE;
2471 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302472 work = 1;
2473 } else if (test_bit(A_CONN, &motg->inputs)) {
2474 pr_debug("a_conn\n");
2475 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002476 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302477 /*
2478 * PET disconnects D+ pullup after reset is generated
2479 * by B device in B_HOST role which is not detected by
2480 * B device. As workaorund , start timer of 300msec
2481 * and stop timer if A device is enumerated else clear
2482 * A_CONN.
2483 */
2484 msm_otg_start_timer(motg, TB_TST_CONFIG,
2485 B_TST_CONFIG);
2486 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2487 pr_debug("b_ase0_brst_tmout\n");
2488 pr_info("B HNP fail:No response from A device\n");
2489 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002490 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302491 otg->host->is_b_host = 0;
2492 clear_bit(B_ASE0_BRST, &motg->tmouts);
2493 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2494 clear_bit(B_BUS_REQ, &motg->inputs);
2495 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002496 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302497 work = 1;
2498 } else if (test_bit(ID_C, &motg->inputs)) {
2499 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2500 }
2501 break;
2502 case OTG_STATE_B_HOST:
2503 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2504 !test_bit(A_CONN, &motg->inputs) ||
2505 !test_bit(B_SESS_VLD, &motg->inputs)) {
2506 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2507 clear_bit(A_CONN, &motg->inputs);
2508 clear_bit(B_BUS_REQ, &motg->inputs);
2509 msm_otg_start_host(otg, 0);
2510 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002511 otg->phy->state = OTG_STATE_B_IDLE;
2512 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302513 work = 1;
2514 } else if (test_bit(ID_C, &motg->inputs)) {
2515 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2516 }
2517 break;
2518 case OTG_STATE_A_IDLE:
2519 otg->default_a = 1;
2520 if (test_bit(ID, &motg->inputs) &&
2521 !test_bit(ID_A, &motg->inputs)) {
2522 pr_debug("id && !id_a\n");
2523 otg->default_a = 0;
2524 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002525 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302526 del_timer_sync(&motg->id_timer);
2527 msm_otg_link_reset(motg);
2528 msm_chg_enable_aca_intr(motg);
2529 msm_otg_notify_charger(motg, 0);
2530 work = 1;
2531 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2532 (test_bit(A_SRP_DET, &motg->inputs) ||
2533 test_bit(A_BUS_REQ, &motg->inputs))) {
2534 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2535
2536 clear_bit(A_SRP_DET, &motg->inputs);
2537 /* Disable SRP detection */
2538 writel_relaxed((readl_relaxed(USB_OTGSC) &
2539 ~OTGSC_INTSTS_MASK) &
2540 ~OTGSC_DPIE, USB_OTGSC);
2541
Steve Mucklef132c6c2012-06-06 18:30:57 -07002542 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302543 /* VBUS should not be supplied before end of SRP pulse
2544 * generated by PET, if not complaince test fail.
2545 */
2546 usleep_range(10000, 12000);
2547 /* ACA: ID_A: Stop charging untill enumeration */
2548 if (test_bit(ID_A, &motg->inputs))
2549 msm_otg_notify_charger(motg, 0);
2550 else
2551 msm_hsusb_vbus_power(motg, 1);
2552 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2553 } else {
2554 pr_debug("No session requested\n");
2555 clear_bit(A_BUS_DROP, &motg->inputs);
2556 if (test_bit(ID_A, &motg->inputs)) {
2557 msm_otg_notify_charger(motg,
2558 IDEV_ACA_CHG_MAX);
2559 } else if (!test_bit(ID, &motg->inputs)) {
2560 msm_otg_notify_charger(motg, 0);
2561 /*
2562 * A-device is not providing power on VBUS.
2563 * Enable SRP detection.
2564 */
2565 writel_relaxed(0x13, USB_USBMODE);
2566 writel_relaxed((readl_relaxed(USB_OTGSC) &
2567 ~OTGSC_INTSTS_MASK) |
2568 OTGSC_DPIE, USB_OTGSC);
2569 mb();
2570 }
2571 }
2572 break;
2573 case OTG_STATE_A_WAIT_VRISE:
2574 if ((test_bit(ID, &motg->inputs) &&
2575 !test_bit(ID_A, &motg->inputs)) ||
2576 test_bit(A_BUS_DROP, &motg->inputs) ||
2577 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2578 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2579 clear_bit(A_BUS_REQ, &motg->inputs);
2580 msm_otg_del_timer(motg);
2581 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002582 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302583 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2584 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2585 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002586 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302587 if (TA_WAIT_BCON > 0)
2588 msm_otg_start_timer(motg, TA_WAIT_BCON,
2589 A_WAIT_BCON);
2590 msm_otg_start_host(otg, 1);
2591 msm_chg_enable_aca_det(motg);
2592 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302593 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302594 if (msm_chg_check_aca_intr(motg))
2595 work = 1;
2596 }
2597 break;
2598 case OTG_STATE_A_WAIT_BCON:
2599 if ((test_bit(ID, &motg->inputs) &&
2600 !test_bit(ID_A, &motg->inputs)) ||
2601 test_bit(A_BUS_DROP, &motg->inputs) ||
2602 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2603 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2604 "a_wait_bcon_tmout\n");
2605 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2606 pr_info("Device No Response\n");
2607 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2608 }
2609 msm_otg_del_timer(motg);
2610 clear_bit(A_BUS_REQ, &motg->inputs);
2611 clear_bit(B_CONN, &motg->inputs);
2612 msm_otg_start_host(otg, 0);
2613 /*
2614 * ACA: ID_A with NO accessory, just the A plug is
2615 * attached to ACA: Use IDCHG_MAX for charging
2616 */
2617 if (test_bit(ID_A, &motg->inputs))
2618 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2619 else
2620 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002621 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302622 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2623 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2624 pr_debug("!a_vbus_vld\n");
2625 clear_bit(B_CONN, &motg->inputs);
2626 msm_otg_del_timer(motg);
2627 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002628 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2629 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302630 } else if (test_bit(ID_A, &motg->inputs)) {
2631 msm_hsusb_vbus_power(motg, 0);
2632 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2633 /*
2634 * If TA_WAIT_BCON is infinite, we don;t
2635 * turn off VBUS. Enter low power mode.
2636 */
2637 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002638 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302639 } else if (!test_bit(ID, &motg->inputs)) {
2640 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302641 }
2642 break;
2643 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302644 if ((test_bit(ID, &motg->inputs) &&
2645 !test_bit(ID_A, &motg->inputs)) ||
2646 test_bit(A_BUS_DROP, &motg->inputs)) {
2647 pr_debug("id_a/b/c || a_bus_drop\n");
2648 clear_bit(B_CONN, &motg->inputs);
2649 clear_bit(A_BUS_REQ, &motg->inputs);
2650 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002651 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302652 msm_otg_start_host(otg, 0);
2653 if (!test_bit(ID_A, &motg->inputs))
2654 msm_hsusb_vbus_power(motg, 0);
2655 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2656 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2657 pr_debug("!a_vbus_vld\n");
2658 clear_bit(B_CONN, &motg->inputs);
2659 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002660 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302661 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002662 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302663 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2664 /*
2665 * a_bus_req is de-asserted when root hub is
2666 * suspended or HNP is in progress.
2667 */
2668 pr_debug("!a_bus_req\n");
2669 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002670 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302671 if (otg->host->b_hnp_enable)
2672 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2673 A_AIDL_BDIS);
2674 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002675 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302676 } else if (!test_bit(B_CONN, &motg->inputs)) {
2677 pr_debug("!b_conn\n");
2678 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002679 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302680 if (TA_WAIT_BCON > 0)
2681 msm_otg_start_timer(motg, TA_WAIT_BCON,
2682 A_WAIT_BCON);
2683 if (msm_chg_check_aca_intr(motg))
2684 work = 1;
2685 } else if (test_bit(ID_A, &motg->inputs)) {
2686 msm_otg_del_timer(motg);
2687 msm_hsusb_vbus_power(motg, 0);
2688 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2689 msm_otg_notify_charger(motg,
2690 IDEV_ACA_CHG_MAX);
2691 else
2692 msm_otg_notify_charger(motg,
2693 IDEV_CHG_MIN - motg->mA_port);
2694 } else if (!test_bit(ID, &motg->inputs)) {
2695 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2696 motg->chg_type = USB_INVALID_CHARGER;
2697 msm_otg_notify_charger(motg, 0);
2698 msm_hsusb_vbus_power(motg, 1);
2699 }
2700 break;
2701 case OTG_STATE_A_SUSPEND:
2702 if ((test_bit(ID, &motg->inputs) &&
2703 !test_bit(ID_A, &motg->inputs)) ||
2704 test_bit(A_BUS_DROP, &motg->inputs) ||
2705 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2706 pr_debug("id_a/b/c || a_bus_drop ||"
2707 "a_aidl_bdis_tmout\n");
2708 msm_otg_del_timer(motg);
2709 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002710 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302711 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002712 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302713 if (!test_bit(ID_A, &motg->inputs))
2714 msm_hsusb_vbus_power(motg, 0);
2715 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2716 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2717 pr_debug("!a_vbus_vld\n");
2718 msm_otg_del_timer(motg);
2719 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002720 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302721 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002722 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302723 } else if (!test_bit(B_CONN, &motg->inputs) &&
2724 otg->host->b_hnp_enable) {
2725 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002726 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302727 msm_otg_host_hnp_enable(otg, 1);
2728 otg->gadget->is_a_peripheral = 1;
2729 msm_otg_start_peripheral(otg, 1);
2730 } else if (!test_bit(B_CONN, &motg->inputs) &&
2731 !otg->host->b_hnp_enable) {
2732 pr_debug("!b_conn && !b_hnp_enable");
2733 /*
2734 * bus request is dropped during suspend.
2735 * acquire again for next device.
2736 */
2737 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002738 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302739 if (TA_WAIT_BCON > 0)
2740 msm_otg_start_timer(motg, TA_WAIT_BCON,
2741 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002742 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302743 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002744 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302745 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002746 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002747 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302748 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302749 }
2750 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302751 case OTG_STATE_A_PERIPHERAL:
2752 if ((test_bit(ID, &motg->inputs) &&
2753 !test_bit(ID_A, &motg->inputs)) ||
2754 test_bit(A_BUS_DROP, &motg->inputs)) {
2755 pr_debug("id _f/b/c || a_bus_drop\n");
2756 /* Clear BIDL_ADIS timer */
2757 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002758 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302759 msm_otg_start_peripheral(otg, 0);
2760 otg->gadget->is_a_peripheral = 0;
2761 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002762 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302763 if (!test_bit(ID_A, &motg->inputs))
2764 msm_hsusb_vbus_power(motg, 0);
2765 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2766 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2767 pr_debug("!a_vbus_vld\n");
2768 /* Clear BIDL_ADIS timer */
2769 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002770 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302771 msm_otg_start_peripheral(otg, 0);
2772 otg->gadget->is_a_peripheral = 0;
2773 msm_otg_start_host(otg, 0);
2774 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2775 pr_debug("a_bidl_adis_tmout\n");
2776 msm_otg_start_peripheral(otg, 0);
2777 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002778 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302779 set_bit(A_BUS_REQ, &motg->inputs);
2780 msm_otg_host_hnp_enable(otg, 0);
2781 if (TA_WAIT_BCON > 0)
2782 msm_otg_start_timer(motg, TA_WAIT_BCON,
2783 A_WAIT_BCON);
2784 } else if (test_bit(ID_A, &motg->inputs)) {
2785 msm_hsusb_vbus_power(motg, 0);
2786 msm_otg_notify_charger(motg,
2787 IDEV_CHG_MIN - motg->mA_port);
2788 } else if (!test_bit(ID, &motg->inputs)) {
2789 msm_otg_notify_charger(motg, 0);
2790 msm_hsusb_vbus_power(motg, 1);
2791 }
2792 break;
2793 case OTG_STATE_A_WAIT_VFALL:
2794 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2795 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002796 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302797 work = 1;
2798 }
2799 break;
2800 case OTG_STATE_A_VBUS_ERR:
2801 if ((test_bit(ID, &motg->inputs) &&
2802 !test_bit(ID_A, &motg->inputs)) ||
2803 test_bit(A_BUS_DROP, &motg->inputs) ||
2804 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002805 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302806 if (!test_bit(ID_A, &motg->inputs))
2807 msm_hsusb_vbus_power(motg, 0);
2808 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2809 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2810 motg->chg_type = USB_INVALID_CHARGER;
2811 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302812 }
2813 break;
2814 default:
2815 break;
2816 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302817 if (work)
2818 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302819}
2820
2821static irqreturn_t msm_otg_irq(int irq, void *data)
2822{
2823 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002824 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302825 u32 otgsc = 0, usbsts, pc;
2826 bool work = 0;
2827 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302828
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302829 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002830 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302831 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002832 motg->async_int = irq;
Jack Phamb1aa4d82012-08-13 15:32:39 -07002833 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002834 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302835 return IRQ_HANDLED;
2836 }
2837
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002838 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302839 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302840
2841 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302842 return IRQ_NONE;
2843
2844 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302845 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302846 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302847 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302848 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302849 pr_debug("Id clear\n");
2850 /*
2851 * Assert a_bus_req to supply power on
2852 * VBUS when Micro/Mini-A cable is connected
2853 * with out user intervention.
2854 */
2855 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302856 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302857 msm_chg_enable_aca_det(motg);
2858 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302859 writel_relaxed(otgsc, USB_OTGSC);
2860 work = 1;
2861 } else if (otgsc & OTGSC_DPIS) {
2862 pr_debug("DPIS detected\n");
2863 writel_relaxed(otgsc, USB_OTGSC);
2864 set_bit(A_SRP_DET, &motg->inputs);
2865 set_bit(A_BUS_REQ, &motg->inputs);
2866 work = 1;
2867 } else if (otgsc & OTGSC_BSVIS) {
2868 writel_relaxed(otgsc, USB_OTGSC);
2869 /*
2870 * BSV interrupt comes when operating as an A-device
2871 * (VBUS on/off).
2872 * But, handle BSV when charger is removed from ACA in ID_A
2873 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002874 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302875 !test_bit(ID_A, &motg->inputs))
2876 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302877 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302878 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302879 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302880 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302881 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302882 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002883 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2884
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302885 msm_chg_check_aca_intr(motg);
2886 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302887 work = 1;
2888 } else if (usbsts & STS_PCI) {
2889 pc = readl_relaxed(USB_PORTSC);
2890 pr_debug("portsc = %x\n", pc);
2891 ret = IRQ_NONE;
2892 /*
2893 * HCD Acks PCI interrupt. We use this to switch
2894 * between different OTG states.
2895 */
2896 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002897 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302898 case OTG_STATE_A_SUSPEND:
2899 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2900 !(pc & PORTSC_CCS)) {
2901 pr_debug("B_CONN clear\n");
2902 clear_bit(B_CONN, &motg->inputs);
2903 msm_otg_del_timer(motg);
2904 }
2905 break;
2906 case OTG_STATE_A_PERIPHERAL:
2907 /*
2908 * A-peripheral observed activity on bus.
2909 * clear A_BIDL_ADIS timer.
2910 */
2911 msm_otg_del_timer(motg);
2912 work = 0;
2913 break;
2914 case OTG_STATE_B_WAIT_ACON:
2915 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2916 pr_debug("A_CONN set\n");
2917 set_bit(A_CONN, &motg->inputs);
2918 /* Clear ASE0_BRST timer */
2919 msm_otg_del_timer(motg);
2920 }
2921 break;
2922 case OTG_STATE_B_HOST:
2923 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2924 pr_debug("A_CONN clear\n");
2925 clear_bit(A_CONN, &motg->inputs);
2926 msm_otg_del_timer(motg);
2927 }
2928 break;
2929 case OTG_STATE_A_WAIT_BCON:
2930 if (TA_WAIT_BCON < 0)
2931 set_bit(A_BUS_REQ, &motg->inputs);
2932 default:
2933 work = 0;
2934 break;
2935 }
2936 } else if (usbsts & STS_URI) {
2937 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002938 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302939 case OTG_STATE_A_PERIPHERAL:
2940 /*
2941 * A-peripheral observed activity on bus.
2942 * clear A_BIDL_ADIS timer.
2943 */
2944 msm_otg_del_timer(motg);
2945 work = 0;
2946 break;
2947 default:
2948 work = 0;
2949 break;
2950 }
2951 } else if (usbsts & STS_SLI) {
2952 ret = IRQ_NONE;
2953 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002954 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302955 case OTG_STATE_B_PERIPHERAL:
2956 if (otg->gadget->b_hnp_enable) {
2957 set_bit(A_BUS_SUSPEND, &motg->inputs);
2958 set_bit(B_BUS_REQ, &motg->inputs);
2959 work = 1;
2960 }
2961 break;
2962 case OTG_STATE_A_PERIPHERAL:
2963 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2964 A_BIDL_ADIS);
2965 break;
2966 default:
2967 break;
2968 }
2969 } else if ((usbsts & PHY_ALT_INT)) {
2970 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2971 if (msm_chg_check_aca_intr(motg))
2972 work = 1;
2973 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302974 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302975 if (work)
2976 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302977
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302978 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002979}
2980
2981static void msm_otg_set_vbus_state(int online)
2982{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302983 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002984 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05302985 struct usb_otg *otg = motg->phy.otg;
2986
2987 /* In A Host Mode, ignore received BSV interrupts */
2988 if (otg->phy->state >= OTG_STATE_A_IDLE)
2989 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002990
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302991 if (online) {
2992 pr_debug("PMIC: BSV set\n");
2993 set_bit(B_SESS_VLD, &motg->inputs);
2994 } else {
2995 pr_debug("PMIC: BSV clear\n");
2996 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302997 }
2998
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302999 if (!init) {
3000 init = true;
3001 complete(&pmic_vbus_init);
3002 pr_debug("PMIC: BSV init complete\n");
3003 return;
3004 }
3005
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303006 if (test_bit(MHL, &motg->inputs) ||
3007 mhl_det_in_progress) {
3008 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3009 return;
3010 }
3011
Jack Pham5ca279b2012-05-14 18:42:54 -07003012 if (atomic_read(&motg->pm_suspended))
3013 motg->sm_work_pending = true;
3014 else
3015 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003016}
3017
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303018static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003019{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303020 struct msm_otg *motg = container_of(w, struct msm_otg,
3021 pmic_id_status_work.work);
3022 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003023
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05303024 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303025 if (!test_and_set_bit(ID, &motg->inputs)) {
3026 pr_debug("PMIC: ID set\n");
3027 work = 1;
3028 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303029 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303030 if (test_and_clear_bit(ID, &motg->inputs)) {
3031 pr_debug("PMIC: ID clear\n");
3032 set_bit(A_BUS_REQ, &motg->inputs);
3033 work = 1;
3034 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303035 }
3036
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303037 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003038 if (atomic_read(&motg->pm_suspended))
3039 motg->sm_work_pending = true;
3040 else
3041 queue_work(system_nrt_wq, &motg->sm_work);
3042 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303043
3044}
3045
3046#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3047static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3048{
3049 struct msm_otg *motg = data;
3050
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303051 if (test_bit(MHL, &motg->inputs) ||
3052 mhl_det_in_progress) {
3053 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3054 return IRQ_HANDLED;
3055 }
3056
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303057 if (!aca_id_turned_on)
3058 /*schedule delayed work for 5msec for ID line state to settle*/
3059 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3060 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003061
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303062 return IRQ_HANDLED;
3063}
3064
3065static int msm_otg_mode_show(struct seq_file *s, void *unused)
3066{
3067 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003068 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303069
Steve Mucklef132c6c2012-06-06 18:30:57 -07003070 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303071 case OTG_STATE_A_HOST:
3072 seq_printf(s, "host\n");
3073 break;
3074 case OTG_STATE_B_PERIPHERAL:
3075 seq_printf(s, "peripheral\n");
3076 break;
3077 default:
3078 seq_printf(s, "none\n");
3079 break;
3080 }
3081
3082 return 0;
3083}
3084
3085static int msm_otg_mode_open(struct inode *inode, struct file *file)
3086{
3087 return single_open(file, msm_otg_mode_show, inode->i_private);
3088}
3089
3090static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3091 size_t count, loff_t *ppos)
3092{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303093 struct seq_file *s = file->private_data;
3094 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303095 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003096 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303097 int status = count;
3098 enum usb_mode_type req_mode;
3099
3100 memset(buf, 0x00, sizeof(buf));
3101
3102 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3103 status = -EFAULT;
3104 goto out;
3105 }
3106
3107 if (!strncmp(buf, "host", 4)) {
3108 req_mode = USB_HOST;
3109 } else if (!strncmp(buf, "peripheral", 10)) {
3110 req_mode = USB_PERIPHERAL;
3111 } else if (!strncmp(buf, "none", 4)) {
3112 req_mode = USB_NONE;
3113 } else {
3114 status = -EINVAL;
3115 goto out;
3116 }
3117
3118 switch (req_mode) {
3119 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003120 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303121 case OTG_STATE_A_HOST:
3122 case OTG_STATE_B_PERIPHERAL:
3123 set_bit(ID, &motg->inputs);
3124 clear_bit(B_SESS_VLD, &motg->inputs);
3125 break;
3126 default:
3127 goto out;
3128 }
3129 break;
3130 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003131 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303132 case OTG_STATE_B_IDLE:
3133 case OTG_STATE_A_HOST:
3134 set_bit(ID, &motg->inputs);
3135 set_bit(B_SESS_VLD, &motg->inputs);
3136 break;
3137 default:
3138 goto out;
3139 }
3140 break;
3141 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003142 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303143 case OTG_STATE_B_IDLE:
3144 case OTG_STATE_B_PERIPHERAL:
3145 clear_bit(ID, &motg->inputs);
3146 break;
3147 default:
3148 goto out;
3149 }
3150 break;
3151 default:
3152 goto out;
3153 }
3154
Steve Mucklef132c6c2012-06-06 18:30:57 -07003155 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303156 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303157out:
3158 return status;
3159}
3160
3161const struct file_operations msm_otg_mode_fops = {
3162 .open = msm_otg_mode_open,
3163 .read = seq_read,
3164 .write = msm_otg_mode_write,
3165 .llseek = seq_lseek,
3166 .release = single_release,
3167};
3168
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303169static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3170{
3171 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003172 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303173
Steve Mucklef132c6c2012-06-06 18:30:57 -07003174 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303175 return 0;
3176}
3177
3178static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3179{
3180 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3181}
3182
3183const struct file_operations msm_otg_state_fops = {
3184 .open = msm_otg_otg_state_open,
3185 .read = seq_read,
3186 .llseek = seq_lseek,
3187 .release = single_release,
3188};
3189
Anji jonnalad270e2d2011-08-09 11:28:32 +05303190static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3191{
3192 struct msm_otg *motg = s->private;
3193
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303194 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303195 return 0;
3196}
3197
3198static int msm_otg_chg_open(struct inode *inode, struct file *file)
3199{
3200 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3201}
3202
3203const struct file_operations msm_otg_chg_fops = {
3204 .open = msm_otg_chg_open,
3205 .read = seq_read,
3206 .llseek = seq_lseek,
3207 .release = single_release,
3208};
3209
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303210static int msm_otg_aca_show(struct seq_file *s, void *unused)
3211{
3212 if (debug_aca_enabled)
3213 seq_printf(s, "enabled\n");
3214 else
3215 seq_printf(s, "disabled\n");
3216
3217 return 0;
3218}
3219
3220static int msm_otg_aca_open(struct inode *inode, struct file *file)
3221{
3222 return single_open(file, msm_otg_aca_show, inode->i_private);
3223}
3224
3225static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3226 size_t count, loff_t *ppos)
3227{
3228 char buf[8];
3229
3230 memset(buf, 0x00, sizeof(buf));
3231
3232 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3233 return -EFAULT;
3234
3235 if (!strncmp(buf, "enable", 6))
3236 debug_aca_enabled = true;
3237 else
3238 debug_aca_enabled = false;
3239
3240 return count;
3241}
3242
3243const struct file_operations msm_otg_aca_fops = {
3244 .open = msm_otg_aca_open,
3245 .read = seq_read,
3246 .write = msm_otg_aca_write,
3247 .llseek = seq_lseek,
3248 .release = single_release,
3249};
3250
Manu Gautam8bdcc592012-03-06 11:26:06 +05303251static int msm_otg_bus_show(struct seq_file *s, void *unused)
3252{
3253 if (debug_bus_voting_enabled)
3254 seq_printf(s, "enabled\n");
3255 else
3256 seq_printf(s, "disabled\n");
3257
3258 return 0;
3259}
3260
3261static int msm_otg_bus_open(struct inode *inode, struct file *file)
3262{
3263 return single_open(file, msm_otg_bus_show, inode->i_private);
3264}
3265
3266static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3267 size_t count, loff_t *ppos)
3268{
3269 char buf[8];
3270 int ret;
3271 struct seq_file *s = file->private_data;
3272 struct msm_otg *motg = s->private;
3273
3274 memset(buf, 0x00, sizeof(buf));
3275
3276 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3277 return -EFAULT;
3278
3279 if (!strncmp(buf, "enable", 6)) {
3280 /* Do not vote here. Let OTG statemachine decide when to vote */
3281 debug_bus_voting_enabled = true;
3282 } else {
3283 debug_bus_voting_enabled = false;
3284 if (motg->bus_perf_client) {
3285 ret = msm_bus_scale_client_update_request(
3286 motg->bus_perf_client, 0);
3287 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003288 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303289 "for bus bw %d\n", __func__, ret);
3290 }
3291 }
3292
3293 return count;
3294}
3295
3296const struct file_operations msm_otg_bus_fops = {
3297 .open = msm_otg_bus_open,
3298 .read = seq_read,
3299 .write = msm_otg_bus_write,
3300 .llseek = seq_lseek,
3301 .release = single_release,
3302};
3303
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303304static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303305
3306static int msm_otg_debugfs_init(struct msm_otg *motg)
3307{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303308 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303309
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303310 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3311
3312 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3313 return -ENODEV;
3314
Anji jonnalad270e2d2011-08-09 11:28:32 +05303315 if (motg->pdata->mode == USB_OTG &&
3316 motg->pdata->otg_control == OTG_USER_CONTROL) {
3317
Manu Gautam8bdcc592012-03-06 11:26:06 +05303318 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303319 S_IWUSR, msm_otg_dbg_root, motg,
3320 &msm_otg_mode_fops);
3321
Manu Gautam8bdcc592012-03-06 11:26:06 +05303322 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303323 debugfs_remove(msm_otg_dbg_root);
3324 msm_otg_dbg_root = NULL;
3325 return -ENODEV;
3326 }
3327 }
3328
Manu Gautam8bdcc592012-03-06 11:26:06 +05303329 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303330 msm_otg_dbg_root, motg,
3331 &msm_otg_chg_fops);
3332
Manu Gautam8bdcc592012-03-06 11:26:06 +05303333 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303334 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303335 return -ENODEV;
3336 }
3337
Manu Gautam8bdcc592012-03-06 11:26:06 +05303338 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303339 msm_otg_dbg_root, motg,
3340 &msm_otg_aca_fops);
3341
Manu Gautam8bdcc592012-03-06 11:26:06 +05303342 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303343 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303344 return -ENODEV;
3345 }
3346
Manu Gautam8bdcc592012-03-06 11:26:06 +05303347 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3348 msm_otg_dbg_root, motg,
3349 &msm_otg_bus_fops);
3350
3351 if (!msm_otg_dentry) {
3352 debugfs_remove_recursive(msm_otg_dbg_root);
3353 return -ENODEV;
3354 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303355
3356 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3357 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3358
3359 if (!msm_otg_dentry) {
3360 debugfs_remove_recursive(msm_otg_dbg_root);
3361 return -ENODEV;
3362 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303363 return 0;
3364}
3365
3366static void msm_otg_debugfs_cleanup(void)
3367{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303368 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303369}
3370
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303371static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3372static struct platform_device *msm_otg_add_pdev(
3373 struct platform_device *ofdev, const char *name)
3374{
3375 struct platform_device *pdev;
3376 const struct resource *res = ofdev->resource;
3377 unsigned int num = ofdev->num_resources;
3378 int retval;
3379
3380 pdev = platform_device_alloc(name, -1);
3381 if (!pdev) {
3382 retval = -ENOMEM;
3383 goto error;
3384 }
3385
3386 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3387 pdev->dev.dma_mask = &msm_otg_dma_mask;
3388
3389 if (num) {
3390 retval = platform_device_add_resources(pdev, res, num);
3391 if (retval)
3392 goto error;
3393 }
3394
3395 retval = platform_device_add(pdev);
3396 if (retval)
3397 goto error;
3398
3399 return pdev;
3400
3401error:
3402 platform_device_put(pdev);
3403 return ERR_PTR(retval);
3404}
3405
3406static int msm_otg_setup_devices(struct platform_device *ofdev,
3407 enum usb_mode_type mode, bool init)
3408{
3409 const char *gadget_name = "msm_hsusb";
3410 const char *host_name = "msm_hsusb_host";
3411 static struct platform_device *gadget_pdev;
3412 static struct platform_device *host_pdev;
3413 int retval = 0;
3414
3415 if (!init) {
3416 if (gadget_pdev)
3417 platform_device_unregister(gadget_pdev);
3418 if (host_pdev)
3419 platform_device_unregister(host_pdev);
3420 return 0;
3421 }
3422
3423 switch (mode) {
3424 case USB_OTG:
3425 /* fall through */
3426 case USB_PERIPHERAL:
3427 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3428 if (IS_ERR(gadget_pdev)) {
3429 retval = PTR_ERR(gadget_pdev);
3430 break;
3431 }
3432 if (mode == USB_PERIPHERAL)
3433 break;
3434 /* fall through */
3435 case USB_HOST:
3436 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3437 if (IS_ERR(host_pdev)) {
3438 retval = PTR_ERR(host_pdev);
3439 if (mode == USB_OTG)
3440 platform_device_unregister(gadget_pdev);
3441 }
3442 break;
3443 default:
3444 break;
3445 }
3446
3447 return retval;
3448}
3449
3450struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3451{
3452 struct device_node *node = pdev->dev.of_node;
3453 struct msm_otg_platform_data *pdata;
3454 int len = 0;
3455
3456 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3457 if (!pdata) {
3458 pr_err("unable to allocate platform data\n");
3459 return NULL;
3460 }
3461 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3462 if (len) {
3463 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3464 if (!pdata->phy_init_seq)
3465 return NULL;
3466 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3467 pdata->phy_init_seq,
3468 len/sizeof(*pdata->phy_init_seq));
3469 }
3470 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3471 &pdata->power_budget);
3472 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3473 &pdata->mode);
3474 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3475 &pdata->otg_control);
3476 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3477 &pdata->default_mode);
3478 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3479 &pdata->phy_type);
3480 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3481 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303482 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3483 "qcom,hsusb-otg-disable-reset");
3484
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303485 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303486}
3487
3488static int __init msm_otg_probe(struct platform_device *pdev)
3489{
3490 int ret = 0;
3491 struct resource *res;
3492 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003493 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303494 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303495
3496 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303497
3498 if (pdev->dev.of_node) {
3499 dev_dbg(&pdev->dev, "device tree enabled\n");
3500 pdata = msm_otg_dt_to_pdata(pdev);
3501 if (!pdata)
3502 return -ENOMEM;
3503 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3504 if (ret) {
3505 dev_err(&pdev->dev, "devices setup failed\n");
3506 return ret;
3507 }
3508 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303509 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3510 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303511 } else {
3512 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303513 }
3514
3515 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3516 if (!motg) {
3517 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3518 return -ENOMEM;
3519 }
3520
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003521 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3522 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003523 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3524 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303525 goto free_motg;
3526 }
3527
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003528 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303529 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003530 phy = &motg->phy;
3531 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303532
3533 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303534 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3535 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3536 * PMIC can detect ACA ID_GND and generate an interrupt.
3537 */
3538 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3539 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3540 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003541 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303542 }
3543
Ofir Cohen4da266f2012-01-03 10:19:29 +02003544 /* initialize reset counter */
3545 motg->reset_counter = 0;
3546
Amit Blay02eff132011-09-21 16:46:24 +03003547 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003548 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003549 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003550 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303551
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303552 /*
3553 * Targets on which link uses asynchronous reset methodology,
3554 * free running clock is not required during the reset.
3555 */
Manu Gautam5143b252012-01-05 19:25:23 -08003556 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303557 if (IS_ERR(motg->clk))
3558 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3559 else
3560 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303561
3562 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003563 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303564 * CORE CLK must be running at >55Mhz for correct HSUSB
3565 * operation and USB core cannot tolerate frequency changes on
3566 * CORE CLK. For such USB cores, vote for maximum clk frequency
3567 * on pclk source
3568 */
Manu Gautam5143b252012-01-05 19:25:23 -08003569 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3570 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303571 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003572 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303573 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003574 goto put_clk;
3575 }
3576 clk_set_rate(motg->core_clk, INT_MAX);
3577
3578 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3579 if (IS_ERR(motg->pclk)) {
3580 dev_err(&pdev->dev, "failed to get iface_clk\n");
3581 ret = PTR_ERR(motg->pclk);
3582 goto put_core_clk;
3583 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303584
3585 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3586 if (!res) {
3587 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3588 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003589 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303590 }
3591
3592 motg->regs = ioremap(res->start, resource_size(res));
3593 if (!motg->regs) {
3594 dev_err(&pdev->dev, "ioremap failed\n");
3595 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003596 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303597 }
3598 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3599
3600 motg->irq = platform_get_irq(pdev, 0);
3601 if (!motg->irq) {
3602 dev_err(&pdev->dev, "platform_get_irq failed\n");
3603 ret = -ENODEV;
3604 goto free_regs;
3605 }
3606
Manu Gautamf8c45642012-08-10 10:20:56 -07003607 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3608 if (motg->async_irq < 0) {
3609 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3610 motg->async_irq = 0;
3611 }
3612
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003613 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003614 if (IS_ERR(motg->xo_handle)) {
3615 dev_err(&pdev->dev, "%s not able to get the handle "
3616 "to vote for TCXO D0 buffer\n", __func__);
3617 ret = PTR_ERR(motg->xo_handle);
3618 goto free_regs;
3619 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303620
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003621 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003622 if (ret) {
3623 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3624 "D0 buffer%d\n", __func__, ret);
3625 goto free_xo_handle;
3626 }
3627
Manu Gautam28b1bac2012-01-30 16:43:06 +05303628 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303629
Mayank Rana248698c2012-04-19 00:03:16 +05303630 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003631 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303632 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003633 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303634 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003635 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003636 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303637 goto devote_xo_handle;
3638 }
3639 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303640 }
3641
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003642 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303643 if (ret) {
3644 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303645 goto devote_xo_handle;
3646 }
3647
3648 ret = regulator_enable(hsusb_vddcx);
3649 if (ret) {
3650 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3651 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303652 }
3653
3654 ret = msm_hsusb_ldo_init(motg, 1);
3655 if (ret) {
3656 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303657 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303658 }
3659
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303660 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303661 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3662 "mhl_usb_hs_switch");
3663 if (IS_ERR(mhl_usb_hs_switch)) {
3664 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003665 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303666 goto free_ldo_init;
3667 }
3668 }
3669
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003670 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303671 if (ret) {
3672 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003673 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303674 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303675 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303676
3677 writel(0, USB_USBINTR);
3678 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003679 /* Ensure that above STOREs are completed before enabling interrupts */
3680 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303681
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303682 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3683 if (ret)
3684 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003685 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303686 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303687 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303688 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303689 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
kibum.leeec040442012-08-22 22:29:46 +09003690 INIT_DELAYED_WORK(&motg->check_ta_work, msm_ta_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303691 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3692 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303693 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3694 "msm_otg", motg);
3695 if (ret) {
3696 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003697 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303698 }
3699
Manu Gautamf8c45642012-08-10 10:20:56 -07003700 if (motg->async_irq) {
3701 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3702 "msm_otg", motg);
3703 if (ret) {
3704 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3705 goto free_irq;
3706 }
3707 disable_irq(motg->async_irq);
3708 }
3709
Jack Pham87f202f2012-08-06 00:24:22 -07003710 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3711 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3712
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003713 phy->init = msm_otg_reset;
3714 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003715 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303716
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003717 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303718
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003719 phy->otg->phy = &motg->phy;
3720 phy->otg->set_host = msm_otg_set_host;
3721 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003722 phy->otg->start_hnp = msm_otg_start_hnp;
3723 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003724
3725 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303726 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003727 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003728 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303729 }
3730
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303731 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303732 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003733 if (motg->pdata->pmic_id_irq) {
3734 ret = request_irq(motg->pdata->pmic_id_irq,
3735 msm_pmic_id_irq,
3736 IRQF_TRIGGER_RISING |
3737 IRQF_TRIGGER_FALLING,
3738 "msm_otg", motg);
3739 if (ret) {
3740 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003741 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003742 }
3743 } else {
3744 ret = -ENODEV;
3745 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003746 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003747 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303748 }
3749
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303750 msm_hsusb_mhl_switch_enable(motg, 1);
3751
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303752 platform_set_drvdata(pdev, motg);
3753 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003754 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303755
Anji jonnalad270e2d2011-08-09 11:28:32 +05303756 ret = msm_otg_debugfs_init(motg);
3757 if (ret)
3758 dev_dbg(&pdev->dev, "mode debugfs file is"
3759 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303760
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003761 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3762 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3763
Amit Blay58b31472011-11-18 09:39:39 +02003764 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3765 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303766 (!(motg->pdata->mode == USB_OTG) ||
3767 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003768 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303769 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003770
Amit Blay58b31472011-11-18 09:39:39 +02003771 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3772 motg->caps = ALLOW_PHY_RETENTION;
3773 }
3774
Amit Blay6fa647a2012-05-24 14:12:08 +03003775 if (motg->pdata->enable_lpm_on_dev_suspend)
3776 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3777
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003778 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303779 pm_runtime_set_active(&pdev->dev);
3780 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303781
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303782 if (motg->pdata->bus_scale_table) {
3783 motg->bus_perf_client =
3784 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3785 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003786 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303787 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303788 else
3789 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303790 }
3791
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303792 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003793
Steve Mucklef132c6c2012-06-06 18:30:57 -07003794remove_phy:
3795 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003796free_async_irq:
3797 if (motg->async_irq)
3798 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303799free_irq:
3800 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003801destroy_wlock:
3802 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303803 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003804 msm_hsusb_ldo_enable(motg, 0);
3805free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303806 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303807free_hsusb_vddcx:
3808 regulator_disable(hsusb_vddcx);
3809free_config_vddcx:
3810 regulator_set_voltage(hsusb_vddcx,
3811 vdd_val[motg->vdd_type][VDD_NONE],
3812 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003813devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303814 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003815 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003816free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003817 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303818free_regs:
3819 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003820put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303821 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303822put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003823 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303824put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303825 if (!IS_ERR(motg->clk))
3826 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003827 if (!IS_ERR(motg->phy_reset_clk))
3828 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003829free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003830 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303831free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303832 kfree(motg);
3833 return ret;
3834}
3835
3836static int __devexit msm_otg_remove(struct platform_device *pdev)
3837{
3838 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003839 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303840 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303841
3842 if (otg->host || otg->gadget)
3843 return -EBUSY;
3844
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303845 if (pdev->dev.of_node)
3846 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003847 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3848 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303849 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303850 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303851 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303852 cancel_delayed_work_sync(&motg->pmic_id_status_work);
kibum.leeec040442012-08-22 22:29:46 +09003853 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303854 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303855
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303856 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303857
3858 device_init_wakeup(&pdev->dev, 0);
3859 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003860 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303861
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303862 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003863 if (motg->pdata->pmic_id_irq)
3864 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003865 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303866 free_irq(motg->irq, motg);
3867
Jack Pham87f202f2012-08-06 00:24:22 -07003868 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3869 motg->pdata->mpm_otgsessvld_int)
3870 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3871
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303872 /*
3873 * Put PHY in low power mode.
3874 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003875 ulpi_read(otg->phy, 0x14);
3876 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303877
3878 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3879 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3880 if (readl(USB_PORTSC) & PORTSC_PHCD)
3881 break;
3882 udelay(1);
3883 cnt++;
3884 }
3885 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003886 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303887
Manu Gautam28b1bac2012-01-30 16:43:06 +05303888 clk_disable_unprepare(motg->pclk);
3889 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003890 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003891 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303892 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303893 regulator_disable(hsusb_vddcx);
3894 regulator_set_voltage(hsusb_vddcx,
3895 vdd_val[motg->vdd_type][VDD_NONE],
3896 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303897
3898 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303899 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303900
Amit Blay02eff132011-09-21 16:46:24 +03003901 if (!IS_ERR(motg->phy_reset_clk))
3902 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303903 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303904 if (!IS_ERR(motg->clk))
3905 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003906 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303907
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303908 if (motg->bus_perf_client)
3909 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303910
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003911 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303912 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303913 return 0;
3914}
3915
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303916#ifdef CONFIG_PM_RUNTIME
3917static int msm_otg_runtime_idle(struct device *dev)
3918{
3919 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003920 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303921
3922 dev_dbg(dev, "OTG runtime idle\n");
3923
Steve Mucklef132c6c2012-06-06 18:30:57 -07003924 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303925 return -EAGAIN;
3926 else
3927 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303928}
3929
3930static int msm_otg_runtime_suspend(struct device *dev)
3931{
3932 struct msm_otg *motg = dev_get_drvdata(dev);
3933
3934 dev_dbg(dev, "OTG runtime suspend\n");
3935 return msm_otg_suspend(motg);
3936}
3937
3938static int msm_otg_runtime_resume(struct device *dev)
3939{
3940 struct msm_otg *motg = dev_get_drvdata(dev);
3941
3942 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303943 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303944 return msm_otg_resume(motg);
3945}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303946#endif
3947
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303948#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303949static int msm_otg_pm_suspend(struct device *dev)
3950{
Jack Pham5ca279b2012-05-14 18:42:54 -07003951 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303952 struct msm_otg *motg = dev_get_drvdata(dev);
3953
3954 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003955
3956 atomic_set(&motg->pm_suspended, 1);
3957 ret = msm_otg_suspend(motg);
3958 if (ret)
3959 atomic_set(&motg->pm_suspended, 0);
3960
3961 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303962}
3963
3964static int msm_otg_pm_resume(struct device *dev)
3965{
Jack Pham5ca279b2012-05-14 18:42:54 -07003966 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303967 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303968
3969 dev_dbg(dev, "OTG PM resume\n");
3970
Jack Pham5ca279b2012-05-14 18:42:54 -07003971 atomic_set(&motg->pm_suspended, 0);
Jack Phamb1aa4d82012-08-13 15:32:39 -07003972 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003973 pm_runtime_get_noresume(dev);
3974 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303975
Jack Pham5ca279b2012-05-14 18:42:54 -07003976 /* Update runtime PM status */
3977 pm_runtime_disable(dev);
3978 pm_runtime_set_active(dev);
3979 pm_runtime_enable(dev);
3980
Jack Phamb1aa4d82012-08-13 15:32:39 -07003981 if (motg->sm_work_pending) {
3982 motg->sm_work_pending = false;
3983 queue_work(system_nrt_wq, &motg->sm_work);
3984 }
Jack Pham5ca279b2012-05-14 18:42:54 -07003985 }
3986
3987 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303988}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303989#endif
3990
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303991#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303992static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303993 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3994 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3995 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303996};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303997#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303998
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303999static struct of_device_id msm_otg_dt_match[] = {
4000 { .compatible = "qcom,hsusb-otg",
4001 },
4002 {}
4003};
4004
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304005static struct platform_driver msm_otg_driver = {
4006 .remove = __devexit_p(msm_otg_remove),
4007 .driver = {
4008 .name = DRIVER_NAME,
4009 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304010#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304011 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304012#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304013 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304014 },
4015};
4016
4017static int __init msm_otg_init(void)
4018{
4019 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4020}
4021
4022static void __exit msm_otg_exit(void)
4023{
4024 platform_driver_unregister(&msm_otg_driver);
4025}
4026
4027module_init(msm_otg_init);
4028module_exit(msm_otg_exit);
4029
4030MODULE_LICENSE("GPL v2");
4031MODULE_DESCRIPTION("MSM USB transceiver driver");