blob: 6eca25d6c02acd24c1da5546e28f1a8f1ac82e4d [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
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530692 /*
693 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
694 * during bus suspend/resume. Update the relevant state
695 * machine inputs and trigger LPM entry/exit. Checking
696 * in_lpm flag would avoid unnecessary work scheduling.
697 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530698 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700699 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530700 case OTG_STATE_A_WAIT_BCON:
701 if (TA_WAIT_BCON > 0)
702 break;
703 /* fall through */
704 case OTG_STATE_A_HOST:
705 pr_debug("host bus suspend\n");
706 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530707 if (!atomic_read(&motg->in_lpm))
708 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530709 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300710 case OTG_STATE_B_PERIPHERAL:
711 pr_debug("peripheral bus suspend\n");
712 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
713 break;
714 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530715 if (!atomic_read(&motg->in_lpm))
716 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300717 break;
718
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530719 default:
720 break;
721 }
722 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700723 switch (phy->state) {
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530724 case OTG_STATE_A_WAIT_BCON:
725 /* Remote wakeup or resume */
726 set_bit(A_BUS_REQ, &motg->inputs);
727 /* ensure hardware is not in low power mode */
728 if (atomic_read(&motg->in_lpm))
729 pm_runtime_resume(phy->dev);
730 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530731 case OTG_STATE_A_SUSPEND:
732 /* Remote wakeup or resume */
733 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700734 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700735
736 /* ensure hardware is not in low power mode */
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530737 if (atomic_read(&motg->in_lpm))
738 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530739 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300740 case OTG_STATE_B_PERIPHERAL:
741 pr_debug("peripheral bus resume\n");
742 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
743 break;
744 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530745 if (atomic_read(&motg->in_lpm))
746 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300747 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530748 default:
749 break;
750 }
751 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530752 return 0;
753}
754
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530755#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530756#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
757
758#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530759static int msm_otg_suspend(struct msm_otg *motg)
760{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200761 struct usb_phy *phy = &motg->phy;
762 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763 struct msm_otg_platform_data *pdata = motg->pdata;
764 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300765 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530766 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800767 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530768 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530769
770 if (atomic_read(&motg->in_lpm))
771 return 0;
772
773 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530774 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
775 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700776 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300777 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
778 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530779 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham6ff217b2012-08-13 15:34:20 -0700780
Pavankumar Kondetib91ab712012-10-22 13:21:19 +0530781 /*
782 * Abort suspend when,
783 * 1. charging detection in progress due to cable plug-in
784 * 2. host mode activation in progress due to Micro-A cable insertion
785 */
786
787 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
788 !dcp) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham6ff217b2012-08-13 15:34:20 -0700789 enable_irq(motg->irq);
790 return -EBUSY;
791 }
792
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530793 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530794 * Chipidea 45-nm PHY suspend sequence:
795 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530796 * Interrupt Latch Register auto-clear feature is not present
797 * in all PHY versions. Latch register is clear on read type.
798 * Clear latch register to avoid spurious wakeup from
799 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530800 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530801 * PHY comparators are disabled when PHY enters into low power
802 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
803 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
804 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530805 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530806 * PLL is not turned off when PHY enters into low power mode (LPM).
807 * Disable PLL for maximum power savings.
808 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530809
810 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200811 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530812 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200813 ulpi_write(phy, 0x01, 0x30);
814 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530815 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530816
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530818 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530819 * PHY may take some time or even fail to enter into low power
820 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
821 * in failure case.
822 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530823 portsc = readl_relaxed(USB_PORTSC);
824 if (!(portsc & PORTSC_PHCD)) {
825 writel_relaxed(portsc | PORTSC_PHCD,
826 USB_PORTSC);
827 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
828 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
829 break;
830 udelay(1);
831 cnt++;
832 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530833 }
834
835 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200836 dev_err(phy->dev, "Unable to suspend PHY\n");
837 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530838 enable_irq(motg->irq);
839 return -ETIMEDOUT;
840 }
841
842 /*
843 * PHY has capability to generate interrupt asynchronously in low
844 * power mode (LPM). This interrupt is level triggered. So USB IRQ
845 * line must be disabled till async interrupt enable bit is cleared
846 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
847 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530848 *
849 * PHY retention mode is disallowed while entering to LPM with wall
850 * charger connected. But PHY is put into suspend mode. Hence
851 * enable asynchronous interrupt to detect charger disconnection when
852 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530853 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530854 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300855 if (host_bus_suspend || device_bus_suspend ||
856 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530857 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
858 else
859 cmd_val |= ULPI_STP_CTRL;
860 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530861
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530862 /*
863 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
864 * PHY retention and collapse can not happen with VDP_SRC enabled.
865 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300866 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
867 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200868 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
869 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
870 /* Enable PHY HV interrupts to wake MPM/Link */
871 phy_ctrl_val |=
872 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530873
Amit Blay58b31472011-11-18 09:39:39 +0200874 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530876 }
877
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 /* Ensure that above operation is completed before turning off clocks */
879 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300880 /* Consider clocks on workaround flag only in case of bus suspend */
881 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
882 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
883 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300884 clk_disable_unprepare(motg->pclk);
885 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300886 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300887 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530888
Anji jonnala7da3f262011-12-02 17:22:14 -0800889 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530890 if (!host_bus_suspend) {
891 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
892 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700893 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530894 "TCXO D0 buffer%d\n", __func__, ret);
895 else
896 motg->lpm_flags |= XO_SHUTDOWN;
897 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800898
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530899 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
900 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 msm_hsusb_ldo_enable(motg, 0);
902 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530903 }
904
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530905 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530907 msm_hsusb_mhl_switch_enable(motg, 0);
908 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909
Steve Mucklef132c6c2012-06-06 18:30:57 -0700910 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530911 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700912 if (motg->async_irq)
913 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 if (motg->pdata->pmic_id_irq)
915 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700916 if (pdata->otg_control == OTG_PHY_CONTROL &&
917 pdata->mpm_otgsessvld_int)
918 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530920 if (bus)
921 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
922
923 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -0700924 /* Enable ASYNC IRQ (if present) during LPM */
925 if (motg->async_irq)
926 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530927 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530929
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200930 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530931
932 return 0;
933}
934
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530935static int msm_otg_resume(struct msm_otg *motg)
936{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200937 struct usb_phy *phy = &motg->phy;
938 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700939 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530940 int cnt = 0;
941 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200942 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800943 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530944
945 if (!atomic_read(&motg->in_lpm))
946 return 0;
947
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530949
Anji jonnala7da3f262011-12-02 17:22:14 -0800950 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530951 if (motg->lpm_flags & XO_SHUTDOWN) {
952 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
953 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700954 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530955 "TCXO D0 buffer%d\n", __func__, ret);
956 motg->lpm_flags &= ~XO_SHUTDOWN;
957 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530958
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300959 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300960 clk_prepare_enable(motg->core_clk);
961 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300962 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300963 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
966 msm_hsusb_ldo_enable(motg, 1);
967 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
968 }
969
970 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530971 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530972 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200973 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
974 phy_ctrl_val |= PHY_RETEN;
975 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
976 /* Disable PHY HV interrupts */
977 phy_ctrl_val &=
978 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
979 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530981 }
982
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530983 temp = readl(USB_USBCMD);
984 temp &= ~ASYNC_INTR_CTRL;
985 temp &= ~ULPI_STP_CTRL;
986 writel(temp, USB_USBCMD);
987
988 /*
989 * PHY comes out of low power mode (LPM) in case of wakeup
990 * from asynchronous interrupt.
991 */
992 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
993 goto skip_phy_resume;
994
995 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
996 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
997 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
998 break;
999 udelay(1);
1000 cnt++;
1001 }
1002
1003 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1004 /*
1005 * This is a fatal error. Reset the link and
1006 * PHY. USB state can not be restored. Re-insertion
1007 * of USB cable is the only way to get USB working.
1008 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001009 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301010 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001011 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301012 }
1013
1014skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001015 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301016 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001017 if (motg->async_irq)
1018 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 if (motg->pdata->pmic_id_irq)
1020 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001021 if (pdata->otg_control == OTG_PHY_CONTROL &&
1022 pdata->mpm_otgsessvld_int)
1023 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301025 if (bus)
1026 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1027
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301028 atomic_set(&motg->in_lpm, 0);
1029
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301030 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001031 /* Match the disable_irq call from ISR */
1032 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301033 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301034 }
1035
Manu Gautamf8c45642012-08-10 10:20:56 -07001036 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1037 if (motg->async_irq)
1038 disable_irq(motg->async_irq);
1039
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001040 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301041
1042 return 0;
1043}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301044#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301045
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001046static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1047{
1048 if (!psy)
1049 goto psy_not_supported;
1050
1051 if (host_mode)
1052 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1053 else
1054 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1055
1056psy_not_supported:
1057 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1058 return -ENXIO;
1059}
1060
David Keitel081a3e22012-04-18 12:37:07 -07001061static int msm_otg_notify_chg_type(struct msm_otg *motg)
1062{
roy.parkd063ef32012-08-30 09:37:38 -07001063 int charger_type;
David Keitel081a3e22012-04-18 12:37:07 -07001064 /*
1065 * TODO
1066 * Unify OTG driver charger types and power supply charger types
1067 */
David Keitel081a3e22012-04-18 12:37:07 -07001068
1069 if (motg->chg_type == USB_SDP_CHARGER)
1070 charger_type = POWER_SUPPLY_TYPE_USB;
1071 else if (motg->chg_type == USB_CDP_CHARGER)
1072 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301073 else if (motg->chg_type == USB_DCP_CHARGER ||
1074 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001075 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1076 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1077 motg->chg_type == USB_ACA_A_CHARGER ||
1078 motg->chg_type == USB_ACA_B_CHARGER ||
1079 motg->chg_type == USB_ACA_C_CHARGER))
1080 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1081 else
1082 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1083
1084 return pm8921_set_usb_power_supply_type(charger_type);
1085}
1086
Amit Blay0f7edf72012-01-15 10:11:27 +02001087static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1088{
Amit Blay0f7edf72012-01-15 10:11:27 +02001089
Amit Blay0f7edf72012-01-15 10:11:27 +02001090 if (!psy)
1091 goto psy_not_supported;
1092
1093 if (motg->cur_power == 0 && mA > 0) {
1094 /* Enable charging */
1095 if (power_supply_set_online(psy, true))
1096 goto psy_not_supported;
1097 } else if (motg->cur_power > 0 && mA == 0) {
1098 /* Disable charging */
1099 if (power_supply_set_online(psy, false))
1100 goto psy_not_supported;
1101 return 0;
1102 }
1103 /* Set max current limit */
1104 if (power_supply_set_current_limit(psy, 1000*mA))
1105 goto psy_not_supported;
1106
1107 return 0;
1108
1109psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001110 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001111 return -ENXIO;
1112}
1113
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301114static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1115{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001116 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301117
1118 if (g && g->is_a_peripheral)
1119 return;
1120
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301121 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1122 motg->chg_type == USB_ACA_A_CHARGER ||
1123 motg->chg_type == USB_ACA_B_CHARGER ||
1124 motg->chg_type == USB_ACA_C_CHARGER) &&
1125 mA > IDEV_ACA_CHG_LIMIT)
1126 mA = IDEV_ACA_CHG_LIMIT;
1127
David Keitel081a3e22012-04-18 12:37:07 -07001128 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001129 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001130 "Failed notifying %d charger type to PMIC\n",
1131 motg->chg_type);
1132
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301133 if (motg->cur_power == mA)
1134 return;
1135
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001136 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001137
Xiaozhe Shi7c229e52013-01-22 09:46:45 -08001138 /*
1139 * Use Power Supply API if supported, otherwise fallback
1140 * to legacy pm8921 API.
1141 */
1142 if (msm_otg_notify_power_supply(motg, mA))
1143 pm8921_charger_vbus_draw(mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001144
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301145 motg->cur_power = mA;
1146}
1147
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001148static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301149{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001150 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301151
1152 /*
1153 * Gadget driver uses set_power method to notify about the
1154 * available current based on suspend/configured states.
1155 *
1156 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1157 * states when CDP/ACA is connected.
1158 */
1159 if (motg->chg_type == USB_SDP_CHARGER)
1160 msm_otg_notify_charger(motg, mA);
1161
1162 return 0;
1163}
1164
Steve Mucklef132c6c2012-06-06 18:30:57 -07001165static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301166{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001167 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301168 struct msm_otg_platform_data *pdata = motg->pdata;
1169 struct usb_hcd *hcd;
1170
1171 if (!otg->host)
1172 return;
1173
1174 hcd = bus_to_hcd(otg->host);
1175
1176 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001177 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301178
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301179 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001180 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301181 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1182
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301183 /*
1184 * Some boards have a switch cotrolled by gpio
1185 * to enable/disable internal HUB. Enable internal
1186 * HUB before kicking the host.
1187 */
1188 if (pdata->setup_gpio)
1189 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301190 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301191 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001192 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301193
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301194 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301195 /* HCD core reset all bits of PORTSC. select ULPI phy */
1196 writel_relaxed(0x80000000, USB_PORTSC);
1197
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301198 if (pdata->setup_gpio)
1199 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301200
1201 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001202 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301203 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301204 }
1205}
1206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207static int msm_otg_usbdev_notify(struct notifier_block *self,
1208 unsigned long action, void *priv)
1209{
1210 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001211 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301212 struct usb_device *udev = priv;
1213
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301214 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1215 goto out;
1216
Steve Mucklef132c6c2012-06-06 18:30:57 -07001217 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301218 goto out;
1219 /*
1220 * Interested in devices connected directly to the root hub.
1221 * ACA dock can supply IDEV_CHG irrespective devices connected
1222 * on the accessory port.
1223 */
1224 if (!udev->parent || udev->parent->parent ||
1225 motg->chg_type == USB_ACA_DOCK_CHARGER)
1226 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227
1228 switch (action) {
1229 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301230 if (aca_enabled())
1231 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001232 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301233 pr_debug("B_CONN set\n");
1234 set_bit(B_CONN, &motg->inputs);
1235 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001236 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301237 /*
1238 * OTG PET: A-device must end session within
1239 * 10 sec after PET enumeration.
1240 */
1241 if (udev->quirks & USB_QUIRK_OTG_PET)
1242 msm_otg_start_timer(motg, TA_TST_MAINT,
1243 A_TST_MAINT);
1244 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301245 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001246 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001247 if (udev->actconfig)
1248 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1249 else
1250 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001251 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301252 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301253 break;
1254 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001255 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1256 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301257 pr_debug("B_CONN clear\n");
1258 clear_bit(B_CONN, &motg->inputs);
1259 /*
1260 * OTG PET: A-device must end session after
1261 * PET disconnection if it is enumerated
1262 * with bcdDevice[0] = 1. USB core sets
1263 * bus->otg_vbus_off for us. clear it here.
1264 */
1265 if (udev->bus->otg_vbus_off) {
1266 udev->bus->otg_vbus_off = 0;
1267 set_bit(A_BUS_DROP, &motg->inputs);
1268 }
1269 queue_work(system_nrt_wq, &motg->sm_work);
1270 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001271 default:
1272 break;
1273 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301274 if (test_bit(ID_A, &motg->inputs))
1275 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1276 motg->mA_port);
1277out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278 return NOTIFY_OK;
1279}
1280
Mayank Ranae3926882011-12-26 09:47:54 +05301281static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1282{
1283 int ret;
1284 static bool vbus_is_on;
1285
1286 if (vbus_is_on == on)
1287 return;
1288
1289 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301290 ret = motg->pdata->vbus_power(on);
1291 if (!ret)
1292 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301293 return;
1294 }
1295
1296 if (!vbus_otg) {
1297 pr_err("vbus_otg is NULL.");
1298 return;
1299 }
1300
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001301 /*
1302 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001303 * from usb before turning on the boost.
1304 * if exiting host mode disable the boost before enabling to draw
1305 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001306 */
Mayank Ranae3926882011-12-26 09:47:54 +05301307 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001308 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301309 ret = regulator_enable(vbus_otg);
1310 if (ret) {
1311 pr_err("unable to enable vbus_otg\n");
1312 return;
1313 }
1314 vbus_is_on = true;
1315 } else {
1316 ret = regulator_disable(vbus_otg);
1317 if (ret) {
1318 pr_err("unable to disable vbus_otg\n");
1319 return;
1320 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001321 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301322 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301323 }
1324}
1325
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001326static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301327{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001328 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301329 struct usb_hcd *hcd;
1330
1331 /*
1332 * Fail host registration if this board can support
1333 * only peripheral configuration.
1334 */
1335 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001336 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301337 return -ENODEV;
1338 }
1339
Devin Kimd10f03c2012-06-25 17:05:13 -07001340 if (!machine_is_apq8064_mako()) {
1341 if (!motg->pdata->vbus_power && host) {
1342 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1343 if (IS_ERR(vbus_otg)) {
1344 pr_err("Unable to get vbus_otg\n");
1345 return -ENODEV;
1346 }
Mayank Ranae3926882011-12-26 09:47:54 +05301347 }
1348 }
1349
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301350 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001351 if (otg->phy->state == OTG_STATE_A_HOST) {
1352 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001353 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301354 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301355 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301356 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001357 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301358 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301359 } else {
1360 otg->host = NULL;
1361 }
1362
1363 return 0;
1364 }
1365
1366 hcd = bus_to_hcd(host);
1367 hcd->power_budget = motg->pdata->power_budget;
1368
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301369#ifdef CONFIG_USB_OTG
1370 host->otg_port = 1;
1371#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001372 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1373 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301374 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001375 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301376
1377 /*
1378 * Kick the state machine work, if peripheral is not supported
1379 * or peripheral is already registered with us.
1380 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301381 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001382 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301383 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301384 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301385
1386 return 0;
1387}
1388
Steve Mucklef132c6c2012-06-06 18:30:57 -07001389static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301390{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301391 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001392 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301393 struct msm_otg_platform_data *pdata = motg->pdata;
1394
1395 if (!otg->gadget)
1396 return;
1397
1398 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001399 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301400 /*
1401 * Some boards have a switch cotrolled by gpio
1402 * to enable/disable internal HUB. Disable internal
1403 * HUB before kicking the gadget.
1404 */
1405 if (pdata->setup_gpio)
1406 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001407
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301408 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301409 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301410 ret = msm_bus_scale_client_update_request(
1411 motg->bus_perf_client, 1);
1412 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001413 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301414 "bus bandwidth %d\n", __func__, ret);
1415 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301416 usb_gadget_vbus_connect(otg->gadget);
1417 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001418 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301419 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301420 /* Configure BUS performance parameters to default */
1421 if (motg->bus_perf_client) {
1422 ret = msm_bus_scale_client_update_request(
1423 motg->bus_perf_client, 0);
1424 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001425 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301426 "for bus bw %d\n", __func__, ret);
1427 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301428 if (pdata->setup_gpio)
1429 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1430 }
1431
1432}
1433
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001434static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301435 struct usb_gadget *gadget)
1436{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001437 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301438
1439 /*
1440 * Fail peripheral registration if this board can support
1441 * only host configuration.
1442 */
1443 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001444 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301445 return -ENODEV;
1446 }
1447
1448 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001449 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1450 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301451 msm_otg_start_peripheral(otg, 0);
1452 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001453 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301454 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301455 } else {
1456 otg->gadget = NULL;
1457 }
1458
1459 return 0;
1460 }
1461 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001462 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301463
1464 /*
1465 * Kick the state machine work, if host is not supported
1466 * or host is already registered with us.
1467 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301468 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001469 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301470 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301471 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301472
1473 return 0;
1474}
1475
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301476static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1477{
1478 unsigned long flags;
1479 int id;
1480
1481 if (!motg->pdata->pmic_id_irq)
1482 return -ENODEV;
1483
1484 local_irq_save(flags);
1485 id = irq_read_line(motg->pdata->pmic_id_irq);
1486 local_irq_restore(flags);
1487
1488 /*
1489 * If we can not read ID line state for some reason, treat
1490 * it as float. This would prevent MHL discovery and kicking
1491 * host mode unnecessarily.
1492 */
1493 return !!id;
1494}
1495
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301496static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1497 void (*callback)(int on))
1498{
1499 struct usb_phy *phy = &motg->phy;
1500 int ret;
1501
1502 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1503 !motg->pdata->pmic_id_irq) {
1504 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1505 return -ENODEV;
1506 }
1507
1508 if (!motg->pdata->mhl_dev_name) {
1509 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1510 return -ENODEV;
1511 }
1512
1513 if (callback)
1514 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1515 callback);
1516 else
1517 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1518
1519 if (ret)
1520 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1521 motg->pdata->mhl_dev_name, ret);
1522 else
1523 motg->mhl_enabled = true;
1524
1525 return ret;
1526}
1527
1528static void msm_otg_mhl_notify_online(int on)
1529{
1530 struct msm_otg *motg = the_msm_otg;
1531 struct usb_phy *phy = &motg->phy;
1532 bool queue = false;
1533
1534 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1535
1536 if (on) {
1537 set_bit(MHL, &motg->inputs);
1538 } else {
1539 clear_bit(MHL, &motg->inputs);
1540 queue = true;
1541 }
1542
1543 if (queue && phy->state != OTG_STATE_UNDEFINED)
1544 schedule_work(&motg->sm_work);
1545}
1546
1547static bool msm_otg_is_mhl(struct msm_otg *motg)
1548{
1549 struct usb_phy *phy = &motg->phy;
1550 int is_mhl, ret;
1551
1552 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1553 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1554 /*
1555 * MHL driver calls our callback saying that MHL connected
1556 * if RID_GND is detected. But at later part of discovery
1557 * it may figure out MHL is not connected and returns
1558 * false. Hence clear MHL input here.
1559 */
1560 clear_bit(MHL, &motg->inputs);
1561 dev_dbg(phy->dev, "MHL device not found\n");
1562 return false;
1563 }
1564
1565 set_bit(MHL, &motg->inputs);
1566 dev_dbg(phy->dev, "MHL device found\n");
1567 return true;
1568}
1569
1570static bool msm_chg_mhl_detect(struct msm_otg *motg)
1571{
1572 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301573
1574 if (!motg->mhl_enabled)
1575 return false;
1576
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301577 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301578
1579 if (id)
1580 return false;
1581
1582 mhl_det_in_progress = true;
1583 ret = msm_otg_is_mhl(motg);
1584 mhl_det_in_progress = false;
1585
1586 return ret;
1587}
1588
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001589static bool msm_chg_aca_detect(struct msm_otg *motg)
1590{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001591 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001592 u32 int_sts;
1593 bool ret = false;
1594
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301595 if (!aca_enabled())
1596 goto out;
1597
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001598 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1599 goto out;
1600
Steve Mucklef132c6c2012-06-06 18:30:57 -07001601 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001602 switch (int_sts & 0x1C) {
1603 case 0x08:
1604 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001605 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001606 motg->chg_type = USB_ACA_A_CHARGER;
1607 motg->chg_state = USB_CHG_STATE_DETECTED;
1608 clear_bit(ID_B, &motg->inputs);
1609 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301610 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001611 ret = true;
1612 }
1613 break;
1614 case 0x0C:
1615 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001616 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001617 motg->chg_type = USB_ACA_B_CHARGER;
1618 motg->chg_state = USB_CHG_STATE_DETECTED;
1619 clear_bit(ID_A, &motg->inputs);
1620 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301621 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001622 ret = true;
1623 }
1624 break;
1625 case 0x10:
1626 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001627 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001628 motg->chg_type = USB_ACA_C_CHARGER;
1629 motg->chg_state = USB_CHG_STATE_DETECTED;
1630 clear_bit(ID_A, &motg->inputs);
1631 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301632 set_bit(ID, &motg->inputs);
1633 ret = true;
1634 }
1635 break;
1636 case 0x04:
1637 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001638 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301639 motg->chg_type = USB_INVALID_CHARGER;
1640 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1641 clear_bit(ID_A, &motg->inputs);
1642 clear_bit(ID_B, &motg->inputs);
1643 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001644 ret = true;
1645 }
1646 break;
1647 default:
1648 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1649 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301650 test_and_clear_bit(ID_C, &motg->inputs) |
1651 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001653 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001654 motg->chg_type = USB_INVALID_CHARGER;
1655 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1656 }
1657 }
1658out:
1659 return ret;
1660}
1661
1662static void msm_chg_enable_aca_det(struct msm_otg *motg)
1663{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001664 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301666 if (!aca_enabled())
1667 return;
1668
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001669 switch (motg->pdata->phy_type) {
1670 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301671 /* Disable ID_GND in link and PHY */
1672 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1673 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001674 ulpi_write(phy, 0x01, 0x0C);
1675 ulpi_write(phy, 0x10, 0x0F);
1676 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301677 /* Disable PMIC ID pull-up */
1678 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301679 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001680 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301681 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001682 break;
1683 default:
1684 break;
1685 }
1686}
1687
1688static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1689{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001690 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001691
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301692 if (!aca_enabled())
1693 return;
1694
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001695 switch (motg->pdata->phy_type) {
1696 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301697 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001698 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301699 break;
1700 default:
1701 break;
1702 }
1703}
1704
1705static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1706{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001707 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301708
1709 if (!aca_enabled())
1710 return;
1711
1712 switch (motg->pdata->phy_type) {
1713 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001714 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001715 break;
1716 default:
1717 break;
1718 }
1719}
1720
1721static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1722{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001723 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001724 bool ret = false;
1725
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301726 if (!aca_enabled())
1727 return ret;
1728
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001729 switch (motg->pdata->phy_type) {
1730 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001731 if (ulpi_read(phy, 0x91) & 1) {
1732 dev_dbg(phy->dev, "RID change\n");
1733 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734 ret = msm_chg_aca_detect(motg);
1735 }
1736 default:
1737 break;
1738 }
1739 return ret;
1740}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301741
1742static void msm_otg_id_timer_func(unsigned long data)
1743{
1744 struct msm_otg *motg = (struct msm_otg *) data;
1745
1746 if (!aca_enabled())
1747 return;
1748
1749 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001750 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301751 return;
1752 }
1753
Steve Mucklef132c6c2012-06-06 18:30:57 -07001754 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301755 goto out;
1756
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301757 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001758 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301759 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301760 }
1761
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301762out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301763 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1764 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1765}
1766
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301767static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1768{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001769 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301770 u32 chg_det;
1771 bool ret = false;
1772
1773 switch (motg->pdata->phy_type) {
1774 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001775 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301776 ret = chg_det & (1 << 4);
1777 break;
1778 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001779 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301780 ret = chg_det & 1;
1781 break;
1782 default:
1783 break;
1784 }
1785 return ret;
1786}
1787
1788static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1789{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001790 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301791 u32 chg_det;
1792
1793 switch (motg->pdata->phy_type) {
1794 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001795 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301796 /* Turn off charger block */
1797 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001798 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301799 udelay(20);
1800 /* control chg block via ULPI */
1801 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001802 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301803 /* put it in host mode for enabling D- source */
1804 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001805 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301806 /* Turn on chg detect block */
1807 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001808 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301809 udelay(20);
1810 /* enable chg detection */
1811 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001812 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301813 break;
1814 case SNPS_28NM_INTEGRATED_PHY:
1815 /*
1816 * Configure DM as current source, DP as current sink
1817 * and enable battery charging comparators.
1818 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001819 ulpi_write(phy, 0x8, 0x85);
1820 ulpi_write(phy, 0x2, 0x85);
1821 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301822 break;
1823 default:
1824 break;
1825 }
1826}
1827
1828static bool msm_chg_check_primary_det(struct msm_otg *motg)
1829{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001830 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301831 u32 chg_det;
1832 bool ret = false;
1833
1834 switch (motg->pdata->phy_type) {
1835 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001836 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301837 ret = chg_det & (1 << 4);
1838 break;
1839 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001840 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301841 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301842 /* Turn off VDP_SRC */
1843 ulpi_write(phy, 0x3, 0x86);
1844 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301845 break;
1846 default:
1847 break;
1848 }
1849 return ret;
1850}
1851
1852static void msm_chg_enable_primary_det(struct msm_otg *motg)
1853{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001854 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301855 u32 chg_det;
1856
1857 switch (motg->pdata->phy_type) {
1858 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001859 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301860 /* enable chg detection */
1861 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001862 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301863 break;
1864 case SNPS_28NM_INTEGRATED_PHY:
1865 /*
1866 * Configure DP as current source, DM as current sink
1867 * and enable battery charging comparators.
1868 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001869 ulpi_write(phy, 0x2, 0x85);
1870 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301871 break;
1872 default:
1873 break;
1874 }
1875}
1876
1877static bool msm_chg_check_dcd(struct msm_otg *motg)
1878{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001879 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301880 u32 line_state;
1881 bool ret = false;
1882
1883 switch (motg->pdata->phy_type) {
1884 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001885 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301886 ret = !(line_state & 1);
1887 break;
1888 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001889 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301890 ret = line_state & 2;
1891 break;
1892 default:
1893 break;
1894 }
1895 return ret;
1896}
1897
1898static void msm_chg_disable_dcd(struct msm_otg *motg)
1899{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001900 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301901 u32 chg_det;
1902
1903 switch (motg->pdata->phy_type) {
1904 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001905 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301906 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001907 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301908 break;
1909 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001910 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301911 break;
1912 default:
1913 break;
1914 }
1915}
1916
1917static void msm_chg_enable_dcd(struct msm_otg *motg)
1918{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001919 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301920 u32 chg_det;
1921
1922 switch (motg->pdata->phy_type) {
1923 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001924 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301925 /* Turn on D+ current source */
1926 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001927 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301928 break;
1929 case SNPS_28NM_INTEGRATED_PHY:
1930 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001931 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301932 break;
1933 default:
1934 break;
1935 }
1936}
1937
1938static void msm_chg_block_on(struct msm_otg *motg)
1939{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001940 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301941 u32 func_ctrl, chg_det;
1942
1943 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001944 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301945 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1946 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001947 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301948
1949 switch (motg->pdata->phy_type) {
1950 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001951 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301952 /* control chg block via ULPI */
1953 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001954 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301955 /* Turn on chg detect block */
1956 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001957 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301958 udelay(20);
1959 break;
1960 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05301961 /* disable DP and DM pull down resistors */
1962 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301963 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001964 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301965 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001966 ulpi_write(phy, 0x1F, 0x92);
1967 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301968 udelay(100);
1969 break;
1970 default:
1971 break;
1972 }
1973}
1974
1975static void msm_chg_block_off(struct msm_otg *motg)
1976{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001977 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301978 u32 func_ctrl, chg_det;
1979
1980 switch (motg->pdata->phy_type) {
1981 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001982 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301983 /* Turn off charger block */
1984 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001985 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301986 break;
1987 case SNPS_28NM_INTEGRATED_PHY:
1988 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001989 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301990 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001991 ulpi_write(phy, 0x1F, 0x92);
1992 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301993 break;
1994 default:
1995 break;
1996 }
1997
1998 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001999 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302000 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2001 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002002 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302003}
2004
Anji jonnalad270e2d2011-08-09 11:28:32 +05302005static const char *chg_to_string(enum usb_chg_type chg_type)
2006{
2007 switch (chg_type) {
2008 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2009 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2010 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2011 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2012 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2013 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2014 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302015 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302016 default: return "INVALID_CHARGER";
2017 }
2018}
kibum.leeec040442012-08-22 22:29:46 +09002019
Devin Kimd93ff032012-09-14 15:01:31 -07002020#define MSM_CHECK_TA_DELAY (5 * HZ)
2021#define PORTSC_LS (3 << 10) /* Read - Port's Line status */
kibum.leeec040442012-08-22 22:29:46 +09002022static void msm_ta_detect_work(struct work_struct *w)
2023{
Devin Kimd93ff032012-09-14 15:01:31 -07002024 struct msm_otg *motg = container_of(w, struct msm_otg, check_ta_work.work);
kibum.leeec040442012-08-22 22:29:46 +09002025 struct usb_otg *otg = motg->phy.otg;
kibum.leeec040442012-08-22 22:29:46 +09002026
Devin Kim6b8a5d12012-10-16 23:12:18 -07002027 pr_debug("msm_ta_detect_work: ta detection work\n");
Devin Kimd93ff032012-09-14 15:01:31 -07002028
2029 /* Presence of FRame Index or FRINDEX rollover implies USB communication */
2030 if( (readl(USB_FRINDEX) != 0) || ( readl(USB_USBSTS) & (1<<3) ) ) {
2031 pr_info("msm_ta_detect_work: USB exit ta detection - frindex\n");
2032 return;
2033 }
2034
2035 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
2036 pr_info("msm_ta_detect_work: ta dectection success\n");
kibum.leeec040442012-08-22 22:29:46 +09002037 /* inform to user space that SDP is no longer detected */
2038 msm_otg_notify_charger(motg, 0);
2039 motg->chg_state = USB_CHG_STATE_DETECTED;
2040 motg->chg_type = USB_DCP_CHARGER;
2041 motg->cur_power = 0;
2042 msm_otg_start_peripheral(otg, 0);
2043 otg->phy->state = OTG_STATE_B_IDLE;
2044 schedule_work(&motg->sm_work);
2045 return;
kibum.leeec040442012-08-22 22:29:46 +09002046 }
Devin Kimd93ff032012-09-14 15:01:31 -07002047 schedule_delayed_work(&motg->check_ta_work, MSM_CHECK_TA_DELAY);
kibum.leeec040442012-08-22 22:29:46 +09002048}
Anji jonnalad270e2d2011-08-09 11:28:32 +05302049
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302050#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
2051#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302052#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2053#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302054static void msm_chg_detect_work(struct work_struct *w)
2055{
2056 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002057 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302058 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302059 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302060 unsigned long delay;
2061
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002062 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302063
2064 if (test_bit(MHL, &motg->inputs)) {
2065 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2066 return;
2067 }
2068
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302069 switch (motg->chg_state) {
2070 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302071 msm_chg_block_on(motg);
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302072 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002073 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302074 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2075 motg->dcd_retries = 0;
2076 delay = MSM_CHG_DCD_POLL_TIME;
2077 break;
2078 case USB_CHG_STATE_WAIT_FOR_DCD:
jh.koo78df86f2012-09-04 20:13:19 +09002079 if (slimport_is_connected()) {
2080 msm_chg_block_off(motg);
2081 motg->chg_state = USB_CHG_STATE_DETECTED;
2082 motg->chg_type = USB_SDP_CHARGER;
2083 queue_work(system_nrt_wq, &motg->sm_work);
2084 return;
2085 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302086 if (msm_chg_mhl_detect(motg)) {
2087 msm_chg_block_off(motg);
2088 motg->chg_state = USB_CHG_STATE_DETECTED;
2089 motg->chg_type = USB_INVALID_CHARGER;
2090 queue_work(system_nrt_wq, &motg->sm_work);
2091 return;
2092 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002093 is_aca = msm_chg_aca_detect(motg);
2094 if (is_aca) {
2095 /*
2096 * ID_A can be ACA dock too. continue
2097 * primary detection after DCD.
2098 */
2099 if (test_bit(ID_A, &motg->inputs)) {
2100 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2101 } else {
2102 delay = 0;
2103 break;
2104 }
2105 }
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302106 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302107 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2108 if (is_dcd || tmout) {
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302109 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302110 msm_chg_enable_primary_det(motg);
2111 delay = MSM_CHG_PRIMARY_DET_TIME;
2112 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2113 } else {
2114 delay = MSM_CHG_DCD_POLL_TIME;
2115 }
2116 break;
2117 case USB_CHG_STATE_DCD_DONE:
2118 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302119 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2120 dm_vlgc = line_state & PORTSC_LS_DM;
2121 if (vout && !dm_vlgc) { /* 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_DOCK_CHARGER;
2124 motg->chg_state = USB_CHG_STATE_DETECTED;
2125 delay = 0;
2126 break;
2127 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302128 if (line_state) { /* DP > VLGC */
2129 motg->chg_type = USB_PROPRIETARY_CHARGER;
2130 motg->chg_state = USB_CHG_STATE_DETECTED;
2131 delay = 0;
2132 } else {
2133 msm_chg_enable_secondary_det(motg);
2134 delay = MSM_CHG_SECONDARY_DET_TIME;
2135 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2136 }
2137 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302138 if (test_bit(ID_A, &motg->inputs)) {
2139 motg->chg_type = USB_ACA_A_CHARGER;
2140 motg->chg_state = USB_CHG_STATE_DETECTED;
2141 delay = 0;
2142 break;
2143 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302144
2145 if (line_state) /* DP > VLGC or/and DM > VLGC */
2146 motg->chg_type = USB_PROPRIETARY_CHARGER;
2147 else
2148 motg->chg_type = USB_SDP_CHARGER;
2149
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302150 motg->chg_state = USB_CHG_STATE_DETECTED;
2151 delay = 0;
2152 }
2153 break;
2154 case USB_CHG_STATE_PRIMARY_DONE:
2155 vout = msm_chg_check_secondary_det(motg);
2156 if (vout)
2157 motg->chg_type = USB_DCP_CHARGER;
2158 else
2159 motg->chg_type = USB_CDP_CHARGER;
2160 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2161 /* fall through */
2162 case USB_CHG_STATE_SECONDARY_DONE:
2163 motg->chg_state = USB_CHG_STATE_DETECTED;
2164 case USB_CHG_STATE_DETECTED:
2165 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002166 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302167 /*
2168 * Spurious interrupt is seen after enabling ACA detection
2169 * due to which charger detection fails in case of PET.
2170 * Add delay of 100 microsec to avoid that.
2171 */
2172 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002173 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002174 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302175 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302176 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302177 return;
2178 default:
2179 return;
2180 }
2181
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302182 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302183}
2184
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302185/*
2186 * We support OTG, Peripheral only and Host only configurations. In case
2187 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2188 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2189 * enabled when switch is controlled by user and default mode is supplied
2190 * by board file, which can be changed by userspace later.
2191 */
2192static void msm_otg_init_sm(struct msm_otg *motg)
2193{
2194 struct msm_otg_platform_data *pdata = motg->pdata;
2195 u32 otgsc = readl(USB_OTGSC);
2196
2197 switch (pdata->mode) {
2198 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002199 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302200 if (pdata->default_mode == USB_HOST) {
2201 clear_bit(ID, &motg->inputs);
2202 } else if (pdata->default_mode == USB_PERIPHERAL) {
2203 set_bit(ID, &motg->inputs);
2204 set_bit(B_SESS_VLD, &motg->inputs);
2205 } else {
2206 set_bit(ID, &motg->inputs);
2207 clear_bit(B_SESS_VLD, &motg->inputs);
2208 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302209 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302210 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302211 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302212 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302213 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302214 set_bit(A_BUS_REQ, &motg->inputs);
2215 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002216 if (otgsc & OTGSC_BSV)
2217 set_bit(B_SESS_VLD, &motg->inputs);
2218 else
2219 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302220 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302221 if (pdata->pmic_id_irq) {
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302222 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302223 set_bit(ID, &motg->inputs);
2224 else
2225 clear_bit(ID, &motg->inputs);
2226 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302227 /*
2228 * VBUS initial state is reported after PMIC
2229 * driver initialization. Wait for it.
2230 */
2231 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302232 }
2233 break;
2234 case USB_HOST:
2235 clear_bit(ID, &motg->inputs);
2236 break;
2237 case USB_PERIPHERAL:
2238 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302239 if (pdata->otg_control == OTG_PHY_CONTROL) {
2240 if (otgsc & OTGSC_BSV)
2241 set_bit(B_SESS_VLD, &motg->inputs);
2242 else
2243 clear_bit(B_SESS_VLD, &motg->inputs);
2244 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2245 /*
2246 * VBUS initial state is reported after PMIC
2247 * driver initialization. Wait for it.
2248 */
2249 wait_for_completion(&pmic_vbus_init);
2250 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302251 break;
2252 default:
2253 break;
2254 }
2255}
2256
2257static void msm_otg_sm_work(struct work_struct *w)
2258{
2259 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002260 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302261 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302262
Steve Mucklef132c6c2012-06-06 18:30:57 -07002263 pm_runtime_resume(otg->phy->dev);
2264 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002265 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302266 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002267 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302268 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302269 psy = power_supply_get_by_name("usb");
2270 if (!psy)
2271 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002272 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302273 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2274 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002275 pm_runtime_put_noidle(otg->phy->dev);
2276 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302277 break;
2278 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302279 /* FALL THROUGH */
2280 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302281 if (test_bit(MHL, &motg->inputs)) {
2282 /* allow LPM */
2283 pm_runtime_put_noidle(otg->phy->dev);
2284 pm_runtime_suspend(otg->phy->dev);
2285 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002286 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302287 pr_debug("!id || id_A\n");
jh.koo78df86f2012-09-04 20:13:19 +09002288 if (slimport_is_connected()) {
2289 work = 1;
2290 break;
2291 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302292 if (msm_chg_mhl_detect(motg)) {
2293 work = 1;
2294 break;
2295 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302296 clear_bit(B_BUS_REQ, &motg->inputs);
2297 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002298 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302299 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302300 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302301 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302302 switch (motg->chg_state) {
2303 case USB_CHG_STATE_UNDEFINED:
2304 msm_chg_detect_work(&motg->chg_work.work);
2305 break;
2306 case USB_CHG_STATE_DETECTED:
2307 switch (motg->chg_type) {
2308 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302309 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002310 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302311 /* fall through */
2312 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302313 msm_otg_notify_charger(motg,
2314 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002315 pm_runtime_put_noidle(otg->phy->dev);
2316 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302317 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302318 case USB_ACA_B_CHARGER:
2319 msm_otg_notify_charger(motg,
2320 IDEV_ACA_CHG_MAX);
2321 /*
2322 * (ID_B --> ID_C) PHY_ALT interrupt can
2323 * not be detected in LPM.
2324 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302325 break;
2326 case USB_CDP_CHARGER:
2327 msm_otg_notify_charger(motg,
2328 IDEV_CHG_MAX);
2329 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002330 otg->phy->state =
2331 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302332 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302333 case USB_ACA_C_CHARGER:
2334 msm_otg_notify_charger(motg,
2335 IDEV_ACA_CHG_MAX);
2336 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002337 otg->phy->state =
2338 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302339 break;
2340 case USB_SDP_CHARGER:
roy.park99cef212012-07-26 17:00:40 -07002341 msm_otg_notify_charger(motg,
2342 IDEV_CHG_MIN);
jh.koo78df86f2012-09-04 20:13:19 +09002343 if(!slimport_is_connected()) {
2344 msm_otg_start_peripheral(otg, 1);
2345 otg->phy->state =
2346 OTG_STATE_B_PERIPHERAL;
2347 }
kibum.leeec040442012-08-22 22:29:46 +09002348 schedule_delayed_work(&motg->check_ta_work,
2349 MSM_CHECK_TA_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302350 break;
2351 default:
2352 break;
2353 }
2354 break;
2355 default:
2356 break;
2357 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302358 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2359 pr_debug("b_sess_end && b_bus_req\n");
2360 if (msm_otg_start_srp(otg) < 0) {
2361 clear_bit(B_BUS_REQ, &motg->inputs);
2362 work = 1;
2363 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302364 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002365 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302366 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2367 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302368 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302369 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302370 cancel_delayed_work_sync(&motg->chg_work);
kibum.leeec040442012-08-22 22:29:46 +09002371 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302372 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2373 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302374 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002375 msm_otg_reset(otg->phy);
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302376 /*
2377 * There is a small window where ID interrupt
2378 * is not monitored during ID detection circuit
2379 * switch from ACA to PMIC. Check ID state
2380 * before entering into low power mode.
2381 */
2382 if (!msm_otg_read_pmic_id_state(motg)) {
2383 pr_debug("process missed ID intr\n");
2384 clear_bit(ID, &motg->inputs);
2385 work = 1;
2386 break;
2387 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002388 pm_runtime_put_noidle(otg->phy->dev);
2389 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302390 }
2391 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302392 case OTG_STATE_B_SRP_INIT:
2393 if (!test_bit(ID, &motg->inputs) ||
2394 test_bit(ID_A, &motg->inputs) ||
2395 test_bit(ID_C, &motg->inputs) ||
2396 (test_bit(B_SESS_VLD, &motg->inputs) &&
2397 !test_bit(ID_B, &motg->inputs))) {
2398 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2399 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002400 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302401 /*
2402 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2403 * bits after SRP initiation.
2404 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002405 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302406 work = 1;
2407 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2408 pr_debug("b_srp_fail\n");
2409 pr_info("A-device did not respond to SRP\n");
2410 clear_bit(B_BUS_REQ, &motg->inputs);
2411 clear_bit(B_SRP_FAIL, &motg->tmouts);
2412 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002413 ulpi_write(otg->phy, 0x0, 0x98);
2414 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302415 motg->b_last_se0_sess = jiffies;
2416 work = 1;
2417 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302418 break;
2419 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302420 if (!test_bit(ID, &motg->inputs) ||
2421 test_bit(ID_A, &motg->inputs) ||
2422 test_bit(ID_B, &motg->inputs) ||
2423 !test_bit(B_SESS_VLD, &motg->inputs)) {
2424 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302425 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2426 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302427 msm_otg_notify_charger(motg, 0);
2428 srp_reqd = otg->gadget->otg_srp_reqd;
2429 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302430 if (test_bit(ID_B, &motg->inputs))
2431 clear_bit(ID_B, &motg->inputs);
2432 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002433 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302434 motg->b_last_se0_sess = jiffies;
2435 if (srp_reqd)
2436 msm_otg_start_timer(motg,
2437 TB_TST_SRP, B_TST_SRP);
2438 else
2439 work = 1;
2440 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2441 otg->gadget->b_hnp_enable &&
2442 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2443 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2444 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2445 /* D+ pullup should not be disconnected within 4msec
2446 * after A device suspends the bus. Otherwise PET will
2447 * fail the compliance test.
2448 */
2449 udelay(1000);
2450 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002451 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302452 /*
2453 * start HCD even before A-device enable
2454 * pull-up to meet HNP timings.
2455 */
2456 otg->host->is_b_host = 1;
2457 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002458 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2459 test_bit(B_SESS_VLD, &motg->inputs)) {
2460 pr_debug("a_bus_suspend && b_sess_vld\n");
2461 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002462 pm_runtime_put_noidle(otg->phy->dev);
2463 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002464 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002465 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302466 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002467 }
2468 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302469 case OTG_STATE_B_WAIT_ACON:
2470 if (!test_bit(ID, &motg->inputs) ||
2471 test_bit(ID_A, &motg->inputs) ||
2472 test_bit(ID_B, &motg->inputs) ||
2473 !test_bit(B_SESS_VLD, &motg->inputs)) {
2474 pr_debug("!id || id_a/b || !b_sess_vld\n");
2475 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302476 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302477 * A-device is physically disconnected during
2478 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302479 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302480 msm_otg_start_host(otg, 0);
2481 otg->host->is_b_host = 0;
2482
2483 clear_bit(B_BUS_REQ, &motg->inputs);
2484 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2485 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002486 otg->phy->state = OTG_STATE_B_IDLE;
2487 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302488 work = 1;
2489 } else if (test_bit(A_CONN, &motg->inputs)) {
2490 pr_debug("a_conn\n");
2491 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002492 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302493 /*
2494 * PET disconnects D+ pullup after reset is generated
2495 * by B device in B_HOST role which is not detected by
2496 * B device. As workaorund , start timer of 300msec
2497 * and stop timer if A device is enumerated else clear
2498 * A_CONN.
2499 */
2500 msm_otg_start_timer(motg, TB_TST_CONFIG,
2501 B_TST_CONFIG);
2502 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2503 pr_debug("b_ase0_brst_tmout\n");
2504 pr_info("B HNP fail:No response from A device\n");
2505 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002506 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302507 otg->host->is_b_host = 0;
2508 clear_bit(B_ASE0_BRST, &motg->tmouts);
2509 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2510 clear_bit(B_BUS_REQ, &motg->inputs);
2511 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002512 otg->phy->state = OTG_STATE_B_IDLE;
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_B_HOST:
2519 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2520 !test_bit(A_CONN, &motg->inputs) ||
2521 !test_bit(B_SESS_VLD, &motg->inputs)) {
2522 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2523 clear_bit(A_CONN, &motg->inputs);
2524 clear_bit(B_BUS_REQ, &motg->inputs);
2525 msm_otg_start_host(otg, 0);
2526 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002527 otg->phy->state = OTG_STATE_B_IDLE;
2528 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302529 work = 1;
2530 } else if (test_bit(ID_C, &motg->inputs)) {
2531 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2532 }
2533 break;
2534 case OTG_STATE_A_IDLE:
2535 otg->default_a = 1;
2536 if (test_bit(ID, &motg->inputs) &&
2537 !test_bit(ID_A, &motg->inputs)) {
2538 pr_debug("id && !id_a\n");
2539 otg->default_a = 0;
2540 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002541 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302542 del_timer_sync(&motg->id_timer);
2543 msm_otg_link_reset(motg);
2544 msm_chg_enable_aca_intr(motg);
2545 msm_otg_notify_charger(motg, 0);
2546 work = 1;
2547 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2548 (test_bit(A_SRP_DET, &motg->inputs) ||
2549 test_bit(A_BUS_REQ, &motg->inputs))) {
2550 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2551
2552 clear_bit(A_SRP_DET, &motg->inputs);
2553 /* Disable SRP detection */
2554 writel_relaxed((readl_relaxed(USB_OTGSC) &
2555 ~OTGSC_INTSTS_MASK) &
2556 ~OTGSC_DPIE, USB_OTGSC);
2557
Steve Mucklef132c6c2012-06-06 18:30:57 -07002558 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302559 /* VBUS should not be supplied before end of SRP pulse
2560 * generated by PET, if not complaince test fail.
2561 */
2562 usleep_range(10000, 12000);
2563 /* ACA: ID_A: Stop charging untill enumeration */
2564 if (test_bit(ID_A, &motg->inputs))
2565 msm_otg_notify_charger(motg, 0);
2566 else
2567 msm_hsusb_vbus_power(motg, 1);
2568 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2569 } else {
2570 pr_debug("No session requested\n");
2571 clear_bit(A_BUS_DROP, &motg->inputs);
2572 if (test_bit(ID_A, &motg->inputs)) {
2573 msm_otg_notify_charger(motg,
2574 IDEV_ACA_CHG_MAX);
2575 } else if (!test_bit(ID, &motg->inputs)) {
2576 msm_otg_notify_charger(motg, 0);
2577 /*
2578 * A-device is not providing power on VBUS.
2579 * Enable SRP detection.
2580 */
2581 writel_relaxed(0x13, USB_USBMODE);
2582 writel_relaxed((readl_relaxed(USB_OTGSC) &
2583 ~OTGSC_INTSTS_MASK) |
2584 OTGSC_DPIE, USB_OTGSC);
2585 mb();
2586 }
2587 }
2588 break;
2589 case OTG_STATE_A_WAIT_VRISE:
2590 if ((test_bit(ID, &motg->inputs) &&
2591 !test_bit(ID_A, &motg->inputs)) ||
2592 test_bit(A_BUS_DROP, &motg->inputs) ||
2593 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2594 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2595 clear_bit(A_BUS_REQ, &motg->inputs);
2596 msm_otg_del_timer(motg);
2597 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002598 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302599 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2600 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2601 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002602 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302603 if (TA_WAIT_BCON > 0)
2604 msm_otg_start_timer(motg, TA_WAIT_BCON,
2605 A_WAIT_BCON);
2606 msm_otg_start_host(otg, 1);
2607 msm_chg_enable_aca_det(motg);
2608 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302609 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302610 if (msm_chg_check_aca_intr(motg))
2611 work = 1;
2612 }
2613 break;
2614 case OTG_STATE_A_WAIT_BCON:
2615 if ((test_bit(ID, &motg->inputs) &&
2616 !test_bit(ID_A, &motg->inputs)) ||
2617 test_bit(A_BUS_DROP, &motg->inputs) ||
2618 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2619 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2620 "a_wait_bcon_tmout\n");
2621 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2622 pr_info("Device No Response\n");
2623 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2624 }
2625 msm_otg_del_timer(motg);
2626 clear_bit(A_BUS_REQ, &motg->inputs);
2627 clear_bit(B_CONN, &motg->inputs);
2628 msm_otg_start_host(otg, 0);
2629 /*
2630 * ACA: ID_A with NO accessory, just the A plug is
2631 * attached to ACA: Use IDCHG_MAX for charging
2632 */
2633 if (test_bit(ID_A, &motg->inputs))
2634 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2635 else
2636 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002637 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302638 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2639 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2640 pr_debug("!a_vbus_vld\n");
2641 clear_bit(B_CONN, &motg->inputs);
2642 msm_otg_del_timer(motg);
2643 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002644 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2645 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302646 } else if (test_bit(ID_A, &motg->inputs)) {
2647 msm_hsusb_vbus_power(motg, 0);
2648 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2649 /*
2650 * If TA_WAIT_BCON is infinite, we don;t
2651 * turn off VBUS. Enter low power mode.
2652 */
2653 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002654 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302655 } else if (!test_bit(ID, &motg->inputs)) {
2656 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302657 }
2658 break;
2659 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302660 if ((test_bit(ID, &motg->inputs) &&
2661 !test_bit(ID_A, &motg->inputs)) ||
2662 test_bit(A_BUS_DROP, &motg->inputs)) {
2663 pr_debug("id_a/b/c || a_bus_drop\n");
2664 clear_bit(B_CONN, &motg->inputs);
2665 clear_bit(A_BUS_REQ, &motg->inputs);
2666 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002667 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302668 msm_otg_start_host(otg, 0);
2669 if (!test_bit(ID_A, &motg->inputs))
2670 msm_hsusb_vbus_power(motg, 0);
2671 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2672 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2673 pr_debug("!a_vbus_vld\n");
2674 clear_bit(B_CONN, &motg->inputs);
2675 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002676 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302677 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002678 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302679 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2680 /*
2681 * a_bus_req is de-asserted when root hub is
2682 * suspended or HNP is in progress.
2683 */
2684 pr_debug("!a_bus_req\n");
2685 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002686 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302687 if (otg->host->b_hnp_enable)
2688 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2689 A_AIDL_BDIS);
2690 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002691 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302692 } else if (!test_bit(B_CONN, &motg->inputs)) {
2693 pr_debug("!b_conn\n");
2694 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002695 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302696 if (TA_WAIT_BCON > 0)
2697 msm_otg_start_timer(motg, TA_WAIT_BCON,
2698 A_WAIT_BCON);
2699 if (msm_chg_check_aca_intr(motg))
2700 work = 1;
2701 } else if (test_bit(ID_A, &motg->inputs)) {
2702 msm_otg_del_timer(motg);
2703 msm_hsusb_vbus_power(motg, 0);
2704 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2705 msm_otg_notify_charger(motg,
2706 IDEV_ACA_CHG_MAX);
2707 else
2708 msm_otg_notify_charger(motg,
2709 IDEV_CHG_MIN - motg->mA_port);
2710 } else if (!test_bit(ID, &motg->inputs)) {
2711 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2712 motg->chg_type = USB_INVALID_CHARGER;
2713 msm_otg_notify_charger(motg, 0);
2714 msm_hsusb_vbus_power(motg, 1);
2715 }
2716 break;
2717 case OTG_STATE_A_SUSPEND:
2718 if ((test_bit(ID, &motg->inputs) &&
2719 !test_bit(ID_A, &motg->inputs)) ||
2720 test_bit(A_BUS_DROP, &motg->inputs) ||
2721 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2722 pr_debug("id_a/b/c || a_bus_drop ||"
2723 "a_aidl_bdis_tmout\n");
2724 msm_otg_del_timer(motg);
2725 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002726 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302727 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002728 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302729 if (!test_bit(ID_A, &motg->inputs))
2730 msm_hsusb_vbus_power(motg, 0);
2731 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2732 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2733 pr_debug("!a_vbus_vld\n");
2734 msm_otg_del_timer(motg);
2735 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002736 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302737 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002738 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302739 } else if (!test_bit(B_CONN, &motg->inputs) &&
2740 otg->host->b_hnp_enable) {
2741 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002742 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302743 msm_otg_host_hnp_enable(otg, 1);
2744 otg->gadget->is_a_peripheral = 1;
2745 msm_otg_start_peripheral(otg, 1);
2746 } else if (!test_bit(B_CONN, &motg->inputs) &&
2747 !otg->host->b_hnp_enable) {
2748 pr_debug("!b_conn && !b_hnp_enable");
2749 /*
2750 * bus request is dropped during suspend.
2751 * acquire again for next device.
2752 */
2753 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002754 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302755 if (TA_WAIT_BCON > 0)
2756 msm_otg_start_timer(motg, TA_WAIT_BCON,
2757 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002758 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302759 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002760 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302761 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002762 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002763 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302764 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302765 }
2766 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302767 case OTG_STATE_A_PERIPHERAL:
2768 if ((test_bit(ID, &motg->inputs) &&
2769 !test_bit(ID_A, &motg->inputs)) ||
2770 test_bit(A_BUS_DROP, &motg->inputs)) {
2771 pr_debug("id _f/b/c || a_bus_drop\n");
2772 /* Clear BIDL_ADIS timer */
2773 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002774 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302775 msm_otg_start_peripheral(otg, 0);
2776 otg->gadget->is_a_peripheral = 0;
2777 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002778 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302779 if (!test_bit(ID_A, &motg->inputs))
2780 msm_hsusb_vbus_power(motg, 0);
2781 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2782 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2783 pr_debug("!a_vbus_vld\n");
2784 /* Clear BIDL_ADIS timer */
2785 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002786 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302787 msm_otg_start_peripheral(otg, 0);
2788 otg->gadget->is_a_peripheral = 0;
2789 msm_otg_start_host(otg, 0);
2790 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2791 pr_debug("a_bidl_adis_tmout\n");
2792 msm_otg_start_peripheral(otg, 0);
2793 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002794 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302795 set_bit(A_BUS_REQ, &motg->inputs);
2796 msm_otg_host_hnp_enable(otg, 0);
2797 if (TA_WAIT_BCON > 0)
2798 msm_otg_start_timer(motg, TA_WAIT_BCON,
2799 A_WAIT_BCON);
2800 } else if (test_bit(ID_A, &motg->inputs)) {
2801 msm_hsusb_vbus_power(motg, 0);
2802 msm_otg_notify_charger(motg,
2803 IDEV_CHG_MIN - motg->mA_port);
2804 } else if (!test_bit(ID, &motg->inputs)) {
2805 msm_otg_notify_charger(motg, 0);
2806 msm_hsusb_vbus_power(motg, 1);
2807 }
2808 break;
2809 case OTG_STATE_A_WAIT_VFALL:
2810 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2811 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002812 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302813 work = 1;
2814 }
2815 break;
2816 case OTG_STATE_A_VBUS_ERR:
2817 if ((test_bit(ID, &motg->inputs) &&
2818 !test_bit(ID_A, &motg->inputs)) ||
2819 test_bit(A_BUS_DROP, &motg->inputs) ||
2820 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002821 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302822 if (!test_bit(ID_A, &motg->inputs))
2823 msm_hsusb_vbus_power(motg, 0);
2824 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2825 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2826 motg->chg_type = USB_INVALID_CHARGER;
2827 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302828 }
2829 break;
2830 default:
2831 break;
2832 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302833 if (work)
2834 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302835}
2836
2837static irqreturn_t msm_otg_irq(int irq, void *data)
2838{
2839 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002840 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302841 u32 otgsc = 0, usbsts, pc;
2842 bool work = 0;
2843 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302844
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302845 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002846 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302847 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002848 motg->async_int = irq;
Jack Phamb1aa4d82012-08-13 15:32:39 -07002849 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002850 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302851 return IRQ_HANDLED;
2852 }
2853
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002854 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302855 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302856
2857 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302858 return IRQ_NONE;
2859
2860 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302861 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302862 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302863 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302864 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302865 pr_debug("Id clear\n");
2866 /*
2867 * Assert a_bus_req to supply power on
2868 * VBUS when Micro/Mini-A cable is connected
2869 * with out user intervention.
2870 */
2871 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302872 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302873 msm_chg_enable_aca_det(motg);
2874 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302875 writel_relaxed(otgsc, USB_OTGSC);
2876 work = 1;
2877 } else if (otgsc & OTGSC_DPIS) {
2878 pr_debug("DPIS detected\n");
2879 writel_relaxed(otgsc, USB_OTGSC);
2880 set_bit(A_SRP_DET, &motg->inputs);
2881 set_bit(A_BUS_REQ, &motg->inputs);
2882 work = 1;
2883 } else if (otgsc & OTGSC_BSVIS) {
2884 writel_relaxed(otgsc, USB_OTGSC);
2885 /*
2886 * BSV interrupt comes when operating as an A-device
2887 * (VBUS on/off).
2888 * But, handle BSV when charger is removed from ACA in ID_A
2889 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002890 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302891 !test_bit(ID_A, &motg->inputs))
2892 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302893 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302894 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302895 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302896 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302897 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302898 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002899 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2900
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302901 msm_chg_check_aca_intr(motg);
2902 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302903 work = 1;
2904 } else if (usbsts & STS_PCI) {
2905 pc = readl_relaxed(USB_PORTSC);
2906 pr_debug("portsc = %x\n", pc);
2907 ret = IRQ_NONE;
2908 /*
2909 * HCD Acks PCI interrupt. We use this to switch
2910 * between different OTG states.
2911 */
2912 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002913 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302914 case OTG_STATE_A_SUSPEND:
2915 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2916 !(pc & PORTSC_CCS)) {
2917 pr_debug("B_CONN clear\n");
2918 clear_bit(B_CONN, &motg->inputs);
2919 msm_otg_del_timer(motg);
2920 }
2921 break;
2922 case OTG_STATE_A_PERIPHERAL:
2923 /*
2924 * A-peripheral observed activity on bus.
2925 * clear A_BIDL_ADIS timer.
2926 */
2927 msm_otg_del_timer(motg);
2928 work = 0;
2929 break;
2930 case OTG_STATE_B_WAIT_ACON:
2931 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2932 pr_debug("A_CONN set\n");
2933 set_bit(A_CONN, &motg->inputs);
2934 /* Clear ASE0_BRST timer */
2935 msm_otg_del_timer(motg);
2936 }
2937 break;
2938 case OTG_STATE_B_HOST:
2939 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2940 pr_debug("A_CONN clear\n");
2941 clear_bit(A_CONN, &motg->inputs);
2942 msm_otg_del_timer(motg);
2943 }
2944 break;
2945 case OTG_STATE_A_WAIT_BCON:
2946 if (TA_WAIT_BCON < 0)
2947 set_bit(A_BUS_REQ, &motg->inputs);
2948 default:
2949 work = 0;
2950 break;
2951 }
2952 } else if (usbsts & STS_URI) {
2953 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002954 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302955 case OTG_STATE_A_PERIPHERAL:
2956 /*
2957 * A-peripheral observed activity on bus.
2958 * clear A_BIDL_ADIS timer.
2959 */
2960 msm_otg_del_timer(motg);
2961 work = 0;
2962 break;
2963 default:
2964 work = 0;
2965 break;
2966 }
2967 } else if (usbsts & STS_SLI) {
2968 ret = IRQ_NONE;
2969 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002970 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302971 case OTG_STATE_B_PERIPHERAL:
2972 if (otg->gadget->b_hnp_enable) {
2973 set_bit(A_BUS_SUSPEND, &motg->inputs);
2974 set_bit(B_BUS_REQ, &motg->inputs);
2975 work = 1;
2976 }
2977 break;
2978 case OTG_STATE_A_PERIPHERAL:
2979 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2980 A_BIDL_ADIS);
2981 break;
2982 default:
2983 break;
2984 }
2985 } else if ((usbsts & PHY_ALT_INT)) {
2986 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2987 if (msm_chg_check_aca_intr(motg))
2988 work = 1;
2989 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302990 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302991 if (work)
2992 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302993
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302994 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002995}
2996
2997static void msm_otg_set_vbus_state(int online)
2998{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302999 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003000 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303001 struct usb_otg *otg = motg->phy.otg;
3002
3003 /* In A Host Mode, ignore received BSV interrupts */
3004 if (otg->phy->state >= OTG_STATE_A_IDLE)
3005 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003006
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303007 if (online) {
3008 pr_debug("PMIC: BSV set\n");
3009 set_bit(B_SESS_VLD, &motg->inputs);
3010 } else {
3011 pr_debug("PMIC: BSV clear\n");
3012 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303013 }
3014
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303015 if (!init) {
3016 init = true;
3017 complete(&pmic_vbus_init);
3018 pr_debug("PMIC: BSV init complete\n");
3019 return;
3020 }
3021
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303022 if (test_bit(MHL, &motg->inputs) ||
3023 mhl_det_in_progress) {
3024 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3025 return;
3026 }
3027
Jack Pham5ca279b2012-05-14 18:42:54 -07003028 if (atomic_read(&motg->pm_suspended))
3029 motg->sm_work_pending = true;
3030 else
3031 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003032}
3033
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303034static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003035{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303036 struct msm_otg *motg = container_of(w, struct msm_otg,
3037 pmic_id_status_work.work);
3038 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003039
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05303040 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303041 if (!test_and_set_bit(ID, &motg->inputs)) {
3042 pr_debug("PMIC: ID set\n");
3043 work = 1;
3044 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303045 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303046 if (test_and_clear_bit(ID, &motg->inputs)) {
3047 pr_debug("PMIC: ID clear\n");
3048 set_bit(A_BUS_REQ, &motg->inputs);
3049 work = 1;
3050 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303051 }
3052
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303053 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003054 if (atomic_read(&motg->pm_suspended))
3055 motg->sm_work_pending = true;
3056 else
3057 queue_work(system_nrt_wq, &motg->sm_work);
3058 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303059
3060}
3061
3062#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3063static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3064{
3065 struct msm_otg *motg = data;
3066
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303067 if (test_bit(MHL, &motg->inputs) ||
3068 mhl_det_in_progress) {
3069 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3070 return IRQ_HANDLED;
3071 }
3072
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303073 if (!aca_id_turned_on)
3074 /*schedule delayed work for 5msec for ID line state to settle*/
3075 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3076 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003077
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303078 return IRQ_HANDLED;
3079}
3080
3081static int msm_otg_mode_show(struct seq_file *s, void *unused)
3082{
3083 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003084 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303085
Steve Mucklef132c6c2012-06-06 18:30:57 -07003086 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303087 case OTG_STATE_A_HOST:
3088 seq_printf(s, "host\n");
3089 break;
3090 case OTG_STATE_B_PERIPHERAL:
3091 seq_printf(s, "peripheral\n");
3092 break;
3093 default:
3094 seq_printf(s, "none\n");
3095 break;
3096 }
3097
3098 return 0;
3099}
3100
3101static int msm_otg_mode_open(struct inode *inode, struct file *file)
3102{
3103 return single_open(file, msm_otg_mode_show, inode->i_private);
3104}
3105
3106static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3107 size_t count, loff_t *ppos)
3108{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303109 struct seq_file *s = file->private_data;
3110 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303111 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003112 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303113 int status = count;
3114 enum usb_mode_type req_mode;
3115
3116 memset(buf, 0x00, sizeof(buf));
3117
3118 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3119 status = -EFAULT;
3120 goto out;
3121 }
3122
3123 if (!strncmp(buf, "host", 4)) {
3124 req_mode = USB_HOST;
3125 } else if (!strncmp(buf, "peripheral", 10)) {
3126 req_mode = USB_PERIPHERAL;
3127 } else if (!strncmp(buf, "none", 4)) {
3128 req_mode = USB_NONE;
3129 } else {
3130 status = -EINVAL;
3131 goto out;
3132 }
3133
3134 switch (req_mode) {
3135 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003136 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303137 case OTG_STATE_A_HOST:
3138 case OTG_STATE_B_PERIPHERAL:
3139 set_bit(ID, &motg->inputs);
3140 clear_bit(B_SESS_VLD, &motg->inputs);
3141 break;
3142 default:
3143 goto out;
3144 }
3145 break;
3146 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003147 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303148 case OTG_STATE_B_IDLE:
3149 case OTG_STATE_A_HOST:
3150 set_bit(ID, &motg->inputs);
3151 set_bit(B_SESS_VLD, &motg->inputs);
3152 break;
3153 default:
3154 goto out;
3155 }
3156 break;
3157 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003158 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303159 case OTG_STATE_B_IDLE:
3160 case OTG_STATE_B_PERIPHERAL:
3161 clear_bit(ID, &motg->inputs);
3162 break;
3163 default:
3164 goto out;
3165 }
3166 break;
3167 default:
3168 goto out;
3169 }
3170
Steve Mucklef132c6c2012-06-06 18:30:57 -07003171 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303172 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303173out:
3174 return status;
3175}
3176
3177const struct file_operations msm_otg_mode_fops = {
3178 .open = msm_otg_mode_open,
3179 .read = seq_read,
3180 .write = msm_otg_mode_write,
3181 .llseek = seq_lseek,
3182 .release = single_release,
3183};
3184
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303185static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3186{
3187 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003188 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303189
Steve Mucklef132c6c2012-06-06 18:30:57 -07003190 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303191 return 0;
3192}
3193
3194static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3195{
3196 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3197}
3198
3199const struct file_operations msm_otg_state_fops = {
3200 .open = msm_otg_otg_state_open,
3201 .read = seq_read,
3202 .llseek = seq_lseek,
3203 .release = single_release,
3204};
3205
Anji jonnalad270e2d2011-08-09 11:28:32 +05303206static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3207{
3208 struct msm_otg *motg = s->private;
3209
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303210 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303211 return 0;
3212}
3213
3214static int msm_otg_chg_open(struct inode *inode, struct file *file)
3215{
3216 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3217}
3218
3219const struct file_operations msm_otg_chg_fops = {
3220 .open = msm_otg_chg_open,
3221 .read = seq_read,
3222 .llseek = seq_lseek,
3223 .release = single_release,
3224};
3225
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303226static int msm_otg_aca_show(struct seq_file *s, void *unused)
3227{
3228 if (debug_aca_enabled)
3229 seq_printf(s, "enabled\n");
3230 else
3231 seq_printf(s, "disabled\n");
3232
3233 return 0;
3234}
3235
3236static int msm_otg_aca_open(struct inode *inode, struct file *file)
3237{
3238 return single_open(file, msm_otg_aca_show, inode->i_private);
3239}
3240
3241static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3242 size_t count, loff_t *ppos)
3243{
3244 char buf[8];
3245
3246 memset(buf, 0x00, sizeof(buf));
3247
3248 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3249 return -EFAULT;
3250
3251 if (!strncmp(buf, "enable", 6))
3252 debug_aca_enabled = true;
3253 else
3254 debug_aca_enabled = false;
3255
3256 return count;
3257}
3258
3259const struct file_operations msm_otg_aca_fops = {
3260 .open = msm_otg_aca_open,
3261 .read = seq_read,
3262 .write = msm_otg_aca_write,
3263 .llseek = seq_lseek,
3264 .release = single_release,
3265};
3266
Manu Gautam8bdcc592012-03-06 11:26:06 +05303267static int msm_otg_bus_show(struct seq_file *s, void *unused)
3268{
3269 if (debug_bus_voting_enabled)
3270 seq_printf(s, "enabled\n");
3271 else
3272 seq_printf(s, "disabled\n");
3273
3274 return 0;
3275}
3276
3277static int msm_otg_bus_open(struct inode *inode, struct file *file)
3278{
3279 return single_open(file, msm_otg_bus_show, inode->i_private);
3280}
3281
3282static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3283 size_t count, loff_t *ppos)
3284{
3285 char buf[8];
3286 int ret;
3287 struct seq_file *s = file->private_data;
3288 struct msm_otg *motg = s->private;
3289
3290 memset(buf, 0x00, sizeof(buf));
3291
3292 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3293 return -EFAULT;
3294
3295 if (!strncmp(buf, "enable", 6)) {
3296 /* Do not vote here. Let OTG statemachine decide when to vote */
3297 debug_bus_voting_enabled = true;
3298 } else {
3299 debug_bus_voting_enabled = false;
3300 if (motg->bus_perf_client) {
3301 ret = msm_bus_scale_client_update_request(
3302 motg->bus_perf_client, 0);
3303 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003304 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303305 "for bus bw %d\n", __func__, ret);
3306 }
3307 }
3308
3309 return count;
3310}
3311
3312const struct file_operations msm_otg_bus_fops = {
3313 .open = msm_otg_bus_open,
3314 .read = seq_read,
3315 .write = msm_otg_bus_write,
3316 .llseek = seq_lseek,
3317 .release = single_release,
3318};
3319
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303320static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303321
3322static int msm_otg_debugfs_init(struct msm_otg *motg)
3323{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303324 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303325
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303326 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3327
3328 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3329 return -ENODEV;
3330
Anji jonnalad270e2d2011-08-09 11:28:32 +05303331 if (motg->pdata->mode == USB_OTG &&
3332 motg->pdata->otg_control == OTG_USER_CONTROL) {
3333
Manu Gautam8bdcc592012-03-06 11:26:06 +05303334 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303335 S_IWUSR, msm_otg_dbg_root, motg,
3336 &msm_otg_mode_fops);
3337
Manu Gautam8bdcc592012-03-06 11:26:06 +05303338 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303339 debugfs_remove(msm_otg_dbg_root);
3340 msm_otg_dbg_root = NULL;
3341 return -ENODEV;
3342 }
3343 }
3344
Manu Gautam8bdcc592012-03-06 11:26:06 +05303345 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303346 msm_otg_dbg_root, motg,
3347 &msm_otg_chg_fops);
3348
Manu Gautam8bdcc592012-03-06 11:26:06 +05303349 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303350 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303351 return -ENODEV;
3352 }
3353
Manu Gautam8bdcc592012-03-06 11:26:06 +05303354 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303355 msm_otg_dbg_root, motg,
3356 &msm_otg_aca_fops);
3357
Manu Gautam8bdcc592012-03-06 11:26:06 +05303358 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303359 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303360 return -ENODEV;
3361 }
3362
Manu Gautam8bdcc592012-03-06 11:26:06 +05303363 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3364 msm_otg_dbg_root, motg,
3365 &msm_otg_bus_fops);
3366
3367 if (!msm_otg_dentry) {
3368 debugfs_remove_recursive(msm_otg_dbg_root);
3369 return -ENODEV;
3370 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303371
3372 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3373 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3374
3375 if (!msm_otg_dentry) {
3376 debugfs_remove_recursive(msm_otg_dbg_root);
3377 return -ENODEV;
3378 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303379 return 0;
3380}
3381
3382static void msm_otg_debugfs_cleanup(void)
3383{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303384 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303385}
3386
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303387static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3388static struct platform_device *msm_otg_add_pdev(
3389 struct platform_device *ofdev, const char *name)
3390{
3391 struct platform_device *pdev;
3392 const struct resource *res = ofdev->resource;
3393 unsigned int num = ofdev->num_resources;
3394 int retval;
3395
3396 pdev = platform_device_alloc(name, -1);
3397 if (!pdev) {
3398 retval = -ENOMEM;
3399 goto error;
3400 }
3401
3402 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3403 pdev->dev.dma_mask = &msm_otg_dma_mask;
3404
3405 if (num) {
3406 retval = platform_device_add_resources(pdev, res, num);
3407 if (retval)
3408 goto error;
3409 }
3410
3411 retval = platform_device_add(pdev);
3412 if (retval)
3413 goto error;
3414
3415 return pdev;
3416
3417error:
3418 platform_device_put(pdev);
3419 return ERR_PTR(retval);
3420}
3421
3422static int msm_otg_setup_devices(struct platform_device *ofdev,
3423 enum usb_mode_type mode, bool init)
3424{
3425 const char *gadget_name = "msm_hsusb";
3426 const char *host_name = "msm_hsusb_host";
3427 static struct platform_device *gadget_pdev;
3428 static struct platform_device *host_pdev;
3429 int retval = 0;
3430
3431 if (!init) {
3432 if (gadget_pdev)
3433 platform_device_unregister(gadget_pdev);
3434 if (host_pdev)
3435 platform_device_unregister(host_pdev);
3436 return 0;
3437 }
3438
3439 switch (mode) {
3440 case USB_OTG:
3441 /* fall through */
3442 case USB_PERIPHERAL:
3443 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3444 if (IS_ERR(gadget_pdev)) {
3445 retval = PTR_ERR(gadget_pdev);
3446 break;
3447 }
3448 if (mode == USB_PERIPHERAL)
3449 break;
3450 /* fall through */
3451 case USB_HOST:
3452 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3453 if (IS_ERR(host_pdev)) {
3454 retval = PTR_ERR(host_pdev);
3455 if (mode == USB_OTG)
3456 platform_device_unregister(gadget_pdev);
3457 }
3458 break;
3459 default:
3460 break;
3461 }
3462
3463 return retval;
3464}
3465
3466struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3467{
3468 struct device_node *node = pdev->dev.of_node;
3469 struct msm_otg_platform_data *pdata;
3470 int len = 0;
3471
3472 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3473 if (!pdata) {
3474 pr_err("unable to allocate platform data\n");
3475 return NULL;
3476 }
3477 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3478 if (len) {
3479 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3480 if (!pdata->phy_init_seq)
3481 return NULL;
3482 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3483 pdata->phy_init_seq,
3484 len/sizeof(*pdata->phy_init_seq));
3485 }
3486 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3487 &pdata->power_budget);
3488 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3489 &pdata->mode);
3490 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3491 &pdata->otg_control);
3492 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3493 &pdata->default_mode);
3494 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3495 &pdata->phy_type);
3496 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3497 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303498 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3499 "qcom,hsusb-otg-disable-reset");
3500
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303501 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303502}
3503
3504static int __init msm_otg_probe(struct platform_device *pdev)
3505{
3506 int ret = 0;
3507 struct resource *res;
3508 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003509 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303510 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303511
3512 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303513
3514 if (pdev->dev.of_node) {
3515 dev_dbg(&pdev->dev, "device tree enabled\n");
3516 pdata = msm_otg_dt_to_pdata(pdev);
3517 if (!pdata)
3518 return -ENOMEM;
3519 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3520 if (ret) {
3521 dev_err(&pdev->dev, "devices setup failed\n");
3522 return ret;
3523 }
3524 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303525 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3526 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303527 } else {
3528 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303529 }
3530
3531 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3532 if (!motg) {
3533 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3534 return -ENOMEM;
3535 }
3536
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003537 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3538 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003539 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3540 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303541 goto free_motg;
3542 }
3543
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003544 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303545 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003546 phy = &motg->phy;
3547 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303548
3549 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303550 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3551 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3552 * PMIC can detect ACA ID_GND and generate an interrupt.
3553 */
3554 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3555 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3556 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003557 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303558 }
3559
Ofir Cohen4da266f2012-01-03 10:19:29 +02003560 /* initialize reset counter */
3561 motg->reset_counter = 0;
3562
Amit Blay02eff132011-09-21 16:46:24 +03003563 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003564 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003565 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003566 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303567
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303568 /*
3569 * Targets on which link uses asynchronous reset methodology,
3570 * free running clock is not required during the reset.
3571 */
Manu Gautam5143b252012-01-05 19:25:23 -08003572 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303573 if (IS_ERR(motg->clk))
3574 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3575 else
3576 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303577
3578 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003579 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303580 * CORE CLK must be running at >55Mhz for correct HSUSB
3581 * operation and USB core cannot tolerate frequency changes on
3582 * CORE CLK. For such USB cores, vote for maximum clk frequency
3583 * on pclk source
3584 */
Manu Gautam5143b252012-01-05 19:25:23 -08003585 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3586 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303587 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003588 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303589 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003590 goto put_clk;
3591 }
3592 clk_set_rate(motg->core_clk, INT_MAX);
3593
3594 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3595 if (IS_ERR(motg->pclk)) {
3596 dev_err(&pdev->dev, "failed to get iface_clk\n");
3597 ret = PTR_ERR(motg->pclk);
3598 goto put_core_clk;
3599 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303600
3601 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3602 if (!res) {
3603 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3604 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003605 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303606 }
3607
3608 motg->regs = ioremap(res->start, resource_size(res));
3609 if (!motg->regs) {
3610 dev_err(&pdev->dev, "ioremap failed\n");
3611 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003612 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303613 }
3614 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3615
3616 motg->irq = platform_get_irq(pdev, 0);
3617 if (!motg->irq) {
3618 dev_err(&pdev->dev, "platform_get_irq failed\n");
3619 ret = -ENODEV;
3620 goto free_regs;
3621 }
3622
Manu Gautamf8c45642012-08-10 10:20:56 -07003623 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3624 if (motg->async_irq < 0) {
3625 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3626 motg->async_irq = 0;
3627 }
3628
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003629 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003630 if (IS_ERR(motg->xo_handle)) {
3631 dev_err(&pdev->dev, "%s not able to get the handle "
3632 "to vote for TCXO D0 buffer\n", __func__);
3633 ret = PTR_ERR(motg->xo_handle);
3634 goto free_regs;
3635 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303636
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003637 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003638 if (ret) {
3639 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3640 "D0 buffer%d\n", __func__, ret);
3641 goto free_xo_handle;
3642 }
3643
Manu Gautam28b1bac2012-01-30 16:43:06 +05303644 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303645
Mayank Rana248698c2012-04-19 00:03:16 +05303646 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003647 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303648 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003649 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303650 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003651 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003652 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303653 goto devote_xo_handle;
3654 }
3655 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303656 }
3657
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003658 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303659 if (ret) {
3660 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303661 goto devote_xo_handle;
3662 }
3663
3664 ret = regulator_enable(hsusb_vddcx);
3665 if (ret) {
3666 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3667 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303668 }
3669
3670 ret = msm_hsusb_ldo_init(motg, 1);
3671 if (ret) {
3672 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303673 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303674 }
3675
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303676 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303677 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3678 "mhl_usb_hs_switch");
3679 if (IS_ERR(mhl_usb_hs_switch)) {
3680 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003681 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303682 goto free_ldo_init;
3683 }
3684 }
3685
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003686 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303687 if (ret) {
3688 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003689 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303690 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303691 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303692
3693 writel(0, USB_USBINTR);
3694 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003695 /* Ensure that above STOREs are completed before enabling interrupts */
3696 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303697
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303698 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3699 if (ret)
3700 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003701 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303702 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303703 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303704 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303705 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
kibum.leeec040442012-08-22 22:29:46 +09003706 INIT_DELAYED_WORK(&motg->check_ta_work, msm_ta_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303707 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3708 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303709 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3710 "msm_otg", motg);
3711 if (ret) {
3712 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003713 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303714 }
3715
Manu Gautamf8c45642012-08-10 10:20:56 -07003716 if (motg->async_irq) {
3717 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3718 "msm_otg", motg);
3719 if (ret) {
3720 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3721 goto free_irq;
3722 }
3723 disable_irq(motg->async_irq);
3724 }
3725
Jack Pham87f202f2012-08-06 00:24:22 -07003726 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3727 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3728
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003729 phy->init = msm_otg_reset;
3730 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003731 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303732
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003733 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303734
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003735 phy->otg->phy = &motg->phy;
3736 phy->otg->set_host = msm_otg_set_host;
3737 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003738 phy->otg->start_hnp = msm_otg_start_hnp;
3739 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003740
3741 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303742 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003743 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003744 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303745 }
3746
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303747 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303748 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003749 if (motg->pdata->pmic_id_irq) {
3750 ret = request_irq(motg->pdata->pmic_id_irq,
3751 msm_pmic_id_irq,
3752 IRQF_TRIGGER_RISING |
3753 IRQF_TRIGGER_FALLING,
3754 "msm_otg", motg);
3755 if (ret) {
3756 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003757 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003758 }
3759 } else {
3760 ret = -ENODEV;
3761 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003762 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003763 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303764 }
3765
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303766 msm_hsusb_mhl_switch_enable(motg, 1);
3767
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303768 platform_set_drvdata(pdev, motg);
3769 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003770 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303771
Anji jonnalad270e2d2011-08-09 11:28:32 +05303772 ret = msm_otg_debugfs_init(motg);
3773 if (ret)
3774 dev_dbg(&pdev->dev, "mode debugfs file is"
3775 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003777 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3778 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3779
Amit Blay58b31472011-11-18 09:39:39 +02003780 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3781 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303782 (!(motg->pdata->mode == USB_OTG) ||
3783 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003784 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303785 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003786
Amit Blay58b31472011-11-18 09:39:39 +02003787 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3788 motg->caps = ALLOW_PHY_RETENTION;
3789 }
3790
Amit Blay6fa647a2012-05-24 14:12:08 +03003791 if (motg->pdata->enable_lpm_on_dev_suspend)
3792 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3793
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003794 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303795 pm_runtime_set_active(&pdev->dev);
3796 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303797
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303798 if (motg->pdata->bus_scale_table) {
3799 motg->bus_perf_client =
3800 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3801 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003802 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303803 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303804 else
3805 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303806 }
3807
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303808 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003809
Steve Mucklef132c6c2012-06-06 18:30:57 -07003810remove_phy:
3811 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003812free_async_irq:
3813 if (motg->async_irq)
3814 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303815free_irq:
3816 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003817destroy_wlock:
3818 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303819 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003820 msm_hsusb_ldo_enable(motg, 0);
3821free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303822 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303823free_hsusb_vddcx:
3824 regulator_disable(hsusb_vddcx);
3825free_config_vddcx:
3826 regulator_set_voltage(hsusb_vddcx,
3827 vdd_val[motg->vdd_type][VDD_NONE],
3828 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003829devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303830 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003831 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003832free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003833 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303834free_regs:
3835 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003836put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303837 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303838put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003839 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303840put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303841 if (!IS_ERR(motg->clk))
3842 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003843 if (!IS_ERR(motg->phy_reset_clk))
3844 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003845free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003846 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303847free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303848 kfree(motg);
3849 return ret;
3850}
3851
3852static int __devexit msm_otg_remove(struct platform_device *pdev)
3853{
3854 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003855 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303856 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303857
3858 if (otg->host || otg->gadget)
3859 return -EBUSY;
3860
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303861 if (pdev->dev.of_node)
3862 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003863 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3864 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303865 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303866 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303867 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303868 cancel_delayed_work_sync(&motg->pmic_id_status_work);
kibum.leeec040442012-08-22 22:29:46 +09003869 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303870 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303871
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303872 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303873
3874 device_init_wakeup(&pdev->dev, 0);
3875 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003876 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303877
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303878 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003879 if (motg->pdata->pmic_id_irq)
3880 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003881 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303882 free_irq(motg->irq, motg);
3883
Jack Pham87f202f2012-08-06 00:24:22 -07003884 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3885 motg->pdata->mpm_otgsessvld_int)
3886 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3887
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303888 /*
3889 * Put PHY in low power mode.
3890 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003891 ulpi_read(otg->phy, 0x14);
3892 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303893
3894 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3895 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3896 if (readl(USB_PORTSC) & PORTSC_PHCD)
3897 break;
3898 udelay(1);
3899 cnt++;
3900 }
3901 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003902 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303903
Manu Gautam28b1bac2012-01-30 16:43:06 +05303904 clk_disable_unprepare(motg->pclk);
3905 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003906 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003907 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303908 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303909 regulator_disable(hsusb_vddcx);
3910 regulator_set_voltage(hsusb_vddcx,
3911 vdd_val[motg->vdd_type][VDD_NONE],
3912 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303913
3914 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303915 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303916
Amit Blay02eff132011-09-21 16:46:24 +03003917 if (!IS_ERR(motg->phy_reset_clk))
3918 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303919 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303920 if (!IS_ERR(motg->clk))
3921 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003922 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303923
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303924 if (motg->bus_perf_client)
3925 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303926
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003927 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303928 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303929 return 0;
3930}
3931
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303932#ifdef CONFIG_PM_RUNTIME
3933static int msm_otg_runtime_idle(struct device *dev)
3934{
3935 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003936 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303937
3938 dev_dbg(dev, "OTG runtime idle\n");
3939
Steve Mucklef132c6c2012-06-06 18:30:57 -07003940 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303941 return -EAGAIN;
3942 else
3943 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303944}
3945
3946static int msm_otg_runtime_suspend(struct device *dev)
3947{
3948 struct msm_otg *motg = dev_get_drvdata(dev);
3949
3950 dev_dbg(dev, "OTG runtime suspend\n");
3951 return msm_otg_suspend(motg);
3952}
3953
3954static int msm_otg_runtime_resume(struct device *dev)
3955{
3956 struct msm_otg *motg = dev_get_drvdata(dev);
3957
3958 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303959 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303960 return msm_otg_resume(motg);
3961}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303962#endif
3963
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303964#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303965static int msm_otg_pm_suspend(struct device *dev)
3966{
Jack Pham5ca279b2012-05-14 18:42:54 -07003967 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303968 struct msm_otg *motg = dev_get_drvdata(dev);
3969
3970 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003971
3972 atomic_set(&motg->pm_suspended, 1);
3973 ret = msm_otg_suspend(motg);
3974 if (ret)
3975 atomic_set(&motg->pm_suspended, 0);
3976
3977 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303978}
3979
3980static int msm_otg_pm_resume(struct device *dev)
3981{
Jack Pham5ca279b2012-05-14 18:42:54 -07003982 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303983 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303984
3985 dev_dbg(dev, "OTG PM resume\n");
3986
Jack Pham5ca279b2012-05-14 18:42:54 -07003987 atomic_set(&motg->pm_suspended, 0);
Jack Phamb1aa4d82012-08-13 15:32:39 -07003988 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003989 pm_runtime_get_noresume(dev);
3990 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303991
Jack Pham5ca279b2012-05-14 18:42:54 -07003992 /* Update runtime PM status */
3993 pm_runtime_disable(dev);
3994 pm_runtime_set_active(dev);
3995 pm_runtime_enable(dev);
3996
Jack Phamb1aa4d82012-08-13 15:32:39 -07003997 if (motg->sm_work_pending) {
3998 motg->sm_work_pending = false;
3999 queue_work(system_nrt_wq, &motg->sm_work);
4000 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004001 }
4002
4003 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304004}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304005#endif
4006
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304007#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304008static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304009 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4010 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4011 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304012};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304013#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304014
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304015static struct of_device_id msm_otg_dt_match[] = {
4016 { .compatible = "qcom,hsusb-otg",
4017 },
4018 {}
4019};
4020
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304021static struct platform_driver msm_otg_driver = {
4022 .remove = __devexit_p(msm_otg_remove),
4023 .driver = {
4024 .name = DRIVER_NAME,
4025 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304026#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304027 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304028#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304029 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304030 },
4031};
4032
4033static int __init msm_otg_init(void)
4034{
4035 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4036}
4037
4038static void __exit msm_otg_exit(void)
4039{
4040 platform_driver_unregister(&msm_otg_driver);
4041}
4042
4043module_init(msm_otg_init);
4044module_exit(msm_otg_exit);
4045
4046MODULE_LICENSE("GPL v2");
4047MODULE_DESCRIPTION("MSM USB transceiver driver");