blob: 78f17f4c958a6755bf8b68cde0a05202fa7a692c [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 */
Devin Kim81265542012-09-01 05:06:22 -07001104 dev_info(motg->phy.dev, "current: %d -> %d (mA)\n",
1105 motg->cur_power, mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001106 if (power_supply_set_current_limit(psy, 1000*mA))
1107 goto psy_not_supported;
1108
1109 return 0;
1110
1111psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001112 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001113 return -ENXIO;
1114}
1115
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301116static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1117{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001118 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301119
1120 if (g && g->is_a_peripheral)
1121 return;
1122
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301123 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1124 motg->chg_type == USB_ACA_A_CHARGER ||
1125 motg->chg_type == USB_ACA_B_CHARGER ||
1126 motg->chg_type == USB_ACA_C_CHARGER) &&
1127 mA > IDEV_ACA_CHG_LIMIT)
1128 mA = IDEV_ACA_CHG_LIMIT;
1129
David Keitel081a3e22012-04-18 12:37:07 -07001130 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001131 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001132 "Failed notifying %d charger type to PMIC\n",
1133 motg->chg_type);
1134
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301135 if (motg->cur_power == mA)
1136 return;
1137
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001138 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001139
Devin Kim2073afb2012-09-05 13:01:51 -07001140 pm8921_charger_vbus_draw(mA);
1141 msm_otg_notify_power_supply(motg, mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001142
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301143 motg->cur_power = mA;
1144}
1145
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001146static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301147{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001148 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301149
1150 /*
1151 * Gadget driver uses set_power method to notify about the
1152 * available current based on suspend/configured states.
1153 *
1154 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1155 * states when CDP/ACA is connected.
1156 */
1157 if (motg->chg_type == USB_SDP_CHARGER)
1158 msm_otg_notify_charger(motg, mA);
1159
1160 return 0;
1161}
1162
Steve Mucklef132c6c2012-06-06 18:30:57 -07001163static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301164{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001165 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301166 struct msm_otg_platform_data *pdata = motg->pdata;
1167 struct usb_hcd *hcd;
1168
1169 if (!otg->host)
1170 return;
1171
1172 hcd = bus_to_hcd(otg->host);
1173
1174 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001175 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301176
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301177 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001178 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301179 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1180
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301181 /*
1182 * Some boards have a switch cotrolled by gpio
1183 * to enable/disable internal HUB. Enable internal
1184 * HUB before kicking the host.
1185 */
1186 if (pdata->setup_gpio)
1187 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301188 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301189 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001190 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301191
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301192 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301193 /* HCD core reset all bits of PORTSC. select ULPI phy */
1194 writel_relaxed(0x80000000, USB_PORTSC);
1195
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301196 if (pdata->setup_gpio)
1197 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301198
1199 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001200 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301201 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301202 }
1203}
1204
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001205static int msm_otg_usbdev_notify(struct notifier_block *self,
1206 unsigned long action, void *priv)
1207{
1208 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001209 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301210 struct usb_device *udev = priv;
1211
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301212 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1213 goto out;
1214
Steve Mucklef132c6c2012-06-06 18:30:57 -07001215 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301216 goto out;
1217 /*
1218 * Interested in devices connected directly to the root hub.
1219 * ACA dock can supply IDEV_CHG irrespective devices connected
1220 * on the accessory port.
1221 */
1222 if (!udev->parent || udev->parent->parent ||
1223 motg->chg_type == USB_ACA_DOCK_CHARGER)
1224 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225
1226 switch (action) {
1227 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301228 if (aca_enabled())
1229 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001230 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301231 pr_debug("B_CONN set\n");
1232 set_bit(B_CONN, &motg->inputs);
1233 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001234 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301235 /*
1236 * OTG PET: A-device must end session within
1237 * 10 sec after PET enumeration.
1238 */
1239 if (udev->quirks & USB_QUIRK_OTG_PET)
1240 msm_otg_start_timer(motg, TA_TST_MAINT,
1241 A_TST_MAINT);
1242 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301243 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001244 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001245 if (udev->actconfig)
1246 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1247 else
1248 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001249 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301250 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301251 break;
1252 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001253 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1254 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301255 pr_debug("B_CONN clear\n");
1256 clear_bit(B_CONN, &motg->inputs);
1257 /*
1258 * OTG PET: A-device must end session after
1259 * PET disconnection if it is enumerated
1260 * with bcdDevice[0] = 1. USB core sets
1261 * bus->otg_vbus_off for us. clear it here.
1262 */
1263 if (udev->bus->otg_vbus_off) {
1264 udev->bus->otg_vbus_off = 0;
1265 set_bit(A_BUS_DROP, &motg->inputs);
1266 }
1267 queue_work(system_nrt_wq, &motg->sm_work);
1268 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269 default:
1270 break;
1271 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301272 if (test_bit(ID_A, &motg->inputs))
1273 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1274 motg->mA_port);
1275out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001276 return NOTIFY_OK;
1277}
1278
Mayank Ranae3926882011-12-26 09:47:54 +05301279static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1280{
1281 int ret;
1282 static bool vbus_is_on;
1283
1284 if (vbus_is_on == on)
1285 return;
1286
1287 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301288 ret = motg->pdata->vbus_power(on);
1289 if (!ret)
1290 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301291 return;
1292 }
1293
1294 if (!vbus_otg) {
1295 pr_err("vbus_otg is NULL.");
1296 return;
1297 }
1298
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001299 /*
1300 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001301 * from usb before turning on the boost.
1302 * if exiting host mode disable the boost before enabling to draw
1303 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001304 */
Mayank Ranae3926882011-12-26 09:47:54 +05301305 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001306 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301307 ret = regulator_enable(vbus_otg);
1308 if (ret) {
1309 pr_err("unable to enable vbus_otg\n");
1310 return;
1311 }
1312 vbus_is_on = true;
1313 } else {
1314 ret = regulator_disable(vbus_otg);
1315 if (ret) {
1316 pr_err("unable to disable vbus_otg\n");
1317 return;
1318 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001319 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301320 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301321 }
1322}
1323
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001324static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301325{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001326 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301327 struct usb_hcd *hcd;
1328
1329 /*
1330 * Fail host registration if this board can support
1331 * only peripheral configuration.
1332 */
1333 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001334 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301335 return -ENODEV;
1336 }
1337
Devin Kimd10f03c2012-06-25 17:05:13 -07001338 if (!machine_is_apq8064_mako()) {
1339 if (!motg->pdata->vbus_power && host) {
1340 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1341 if (IS_ERR(vbus_otg)) {
1342 pr_err("Unable to get vbus_otg\n");
1343 return -ENODEV;
1344 }
Mayank Ranae3926882011-12-26 09:47:54 +05301345 }
1346 }
1347
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301348 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001349 if (otg->phy->state == OTG_STATE_A_HOST) {
1350 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001351 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301352 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301353 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301354 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001355 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301356 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301357 } else {
1358 otg->host = NULL;
1359 }
1360
1361 return 0;
1362 }
1363
1364 hcd = bus_to_hcd(host);
1365 hcd->power_budget = motg->pdata->power_budget;
1366
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301367#ifdef CONFIG_USB_OTG
1368 host->otg_port = 1;
1369#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001370 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1371 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301372 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001373 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301374
1375 /*
1376 * Kick the state machine work, if peripheral is not supported
1377 * or peripheral is already registered with us.
1378 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301379 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001380 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301381 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301382 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301383
1384 return 0;
1385}
1386
Steve Mucklef132c6c2012-06-06 18:30:57 -07001387static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301388{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301389 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001390 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301391 struct msm_otg_platform_data *pdata = motg->pdata;
1392
1393 if (!otg->gadget)
1394 return;
1395
1396 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001397 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301398 /*
1399 * Some boards have a switch cotrolled by gpio
1400 * to enable/disable internal HUB. Disable internal
1401 * HUB before kicking the gadget.
1402 */
1403 if (pdata->setup_gpio)
1404 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001405
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301406 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301407 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301408 ret = msm_bus_scale_client_update_request(
1409 motg->bus_perf_client, 1);
1410 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001411 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301412 "bus bandwidth %d\n", __func__, ret);
1413 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301414 usb_gadget_vbus_connect(otg->gadget);
1415 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001416 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301417 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301418 /* Configure BUS performance parameters to default */
1419 if (motg->bus_perf_client) {
1420 ret = msm_bus_scale_client_update_request(
1421 motg->bus_perf_client, 0);
1422 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001423 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301424 "for bus bw %d\n", __func__, ret);
1425 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301426 if (pdata->setup_gpio)
1427 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1428 }
1429
1430}
1431
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001432static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301433 struct usb_gadget *gadget)
1434{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001435 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301436
1437 /*
1438 * Fail peripheral registration if this board can support
1439 * only host configuration.
1440 */
1441 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001442 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301443 return -ENODEV;
1444 }
1445
1446 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001447 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1448 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301449 msm_otg_start_peripheral(otg, 0);
1450 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001451 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301452 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301453 } else {
1454 otg->gadget = NULL;
1455 }
1456
1457 return 0;
1458 }
1459 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001460 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301461
1462 /*
1463 * Kick the state machine work, if host is not supported
1464 * or host is already registered with us.
1465 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301466 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001467 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301468 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301469 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301470
1471 return 0;
1472}
1473
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301474static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1475{
1476 unsigned long flags;
1477 int id;
1478
1479 if (!motg->pdata->pmic_id_irq)
1480 return -ENODEV;
1481
1482 local_irq_save(flags);
1483 id = irq_read_line(motg->pdata->pmic_id_irq);
1484 local_irq_restore(flags);
1485
1486 /*
1487 * If we can not read ID line state for some reason, treat
1488 * it as float. This would prevent MHL discovery and kicking
1489 * host mode unnecessarily.
1490 */
1491 return !!id;
1492}
1493
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301494static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1495 void (*callback)(int on))
1496{
1497 struct usb_phy *phy = &motg->phy;
1498 int ret;
1499
1500 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1501 !motg->pdata->pmic_id_irq) {
1502 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1503 return -ENODEV;
1504 }
1505
1506 if (!motg->pdata->mhl_dev_name) {
1507 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1508 return -ENODEV;
1509 }
1510
1511 if (callback)
1512 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1513 callback);
1514 else
1515 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1516
1517 if (ret)
1518 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1519 motg->pdata->mhl_dev_name, ret);
1520 else
1521 motg->mhl_enabled = true;
1522
1523 return ret;
1524}
1525
1526static void msm_otg_mhl_notify_online(int on)
1527{
1528 struct msm_otg *motg = the_msm_otg;
1529 struct usb_phy *phy = &motg->phy;
1530 bool queue = false;
1531
1532 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1533
1534 if (on) {
1535 set_bit(MHL, &motg->inputs);
1536 } else {
1537 clear_bit(MHL, &motg->inputs);
1538 queue = true;
1539 }
1540
1541 if (queue && phy->state != OTG_STATE_UNDEFINED)
1542 schedule_work(&motg->sm_work);
1543}
1544
1545static bool msm_otg_is_mhl(struct msm_otg *motg)
1546{
1547 struct usb_phy *phy = &motg->phy;
1548 int is_mhl, ret;
1549
1550 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1551 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1552 /*
1553 * MHL driver calls our callback saying that MHL connected
1554 * if RID_GND is detected. But at later part of discovery
1555 * it may figure out MHL is not connected and returns
1556 * false. Hence clear MHL input here.
1557 */
1558 clear_bit(MHL, &motg->inputs);
1559 dev_dbg(phy->dev, "MHL device not found\n");
1560 return false;
1561 }
1562
1563 set_bit(MHL, &motg->inputs);
1564 dev_dbg(phy->dev, "MHL device found\n");
1565 return true;
1566}
1567
1568static bool msm_chg_mhl_detect(struct msm_otg *motg)
1569{
1570 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301571
1572 if (!motg->mhl_enabled)
1573 return false;
1574
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301575 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301576
1577 if (id)
1578 return false;
1579
1580 mhl_det_in_progress = true;
1581 ret = msm_otg_is_mhl(motg);
1582 mhl_det_in_progress = false;
1583
1584 return ret;
1585}
1586
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001587static bool msm_chg_aca_detect(struct msm_otg *motg)
1588{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001589 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001590 u32 int_sts;
1591 bool ret = false;
1592
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301593 if (!aca_enabled())
1594 goto out;
1595
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001596 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1597 goto out;
1598
Steve Mucklef132c6c2012-06-06 18:30:57 -07001599 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001600 switch (int_sts & 0x1C) {
1601 case 0x08:
1602 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001603 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001604 motg->chg_type = USB_ACA_A_CHARGER;
1605 motg->chg_state = USB_CHG_STATE_DETECTED;
1606 clear_bit(ID_B, &motg->inputs);
1607 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301608 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001609 ret = true;
1610 }
1611 break;
1612 case 0x0C:
1613 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001614 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001615 motg->chg_type = USB_ACA_B_CHARGER;
1616 motg->chg_state = USB_CHG_STATE_DETECTED;
1617 clear_bit(ID_A, &motg->inputs);
1618 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301619 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620 ret = true;
1621 }
1622 break;
1623 case 0x10:
1624 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001625 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001626 motg->chg_type = USB_ACA_C_CHARGER;
1627 motg->chg_state = USB_CHG_STATE_DETECTED;
1628 clear_bit(ID_A, &motg->inputs);
1629 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301630 set_bit(ID, &motg->inputs);
1631 ret = true;
1632 }
1633 break;
1634 case 0x04:
1635 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001636 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301637 motg->chg_type = USB_INVALID_CHARGER;
1638 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1639 clear_bit(ID_A, &motg->inputs);
1640 clear_bit(ID_B, &motg->inputs);
1641 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001642 ret = true;
1643 }
1644 break;
1645 default:
1646 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1647 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301648 test_and_clear_bit(ID_C, &motg->inputs) |
1649 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001650 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001651 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652 motg->chg_type = USB_INVALID_CHARGER;
1653 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1654 }
1655 }
1656out:
1657 return ret;
1658}
1659
1660static void msm_chg_enable_aca_det(struct msm_otg *motg)
1661{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001662 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001663
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301664 if (!aca_enabled())
1665 return;
1666
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001667 switch (motg->pdata->phy_type) {
1668 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301669 /* Disable ID_GND in link and PHY */
1670 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1671 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001672 ulpi_write(phy, 0x01, 0x0C);
1673 ulpi_write(phy, 0x10, 0x0F);
1674 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301675 /* Disable PMIC ID pull-up */
1676 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301677 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001678 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301679 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001680 break;
1681 default:
1682 break;
1683 }
1684}
1685
1686static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1687{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001688 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001689
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301690 if (!aca_enabled())
1691 return;
1692
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001693 switch (motg->pdata->phy_type) {
1694 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301695 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001696 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301697 break;
1698 default:
1699 break;
1700 }
1701}
1702
1703static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1704{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001705 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301706
1707 if (!aca_enabled())
1708 return;
1709
1710 switch (motg->pdata->phy_type) {
1711 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001712 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713 break;
1714 default:
1715 break;
1716 }
1717}
1718
1719static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1720{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001721 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722 bool ret = false;
1723
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301724 if (!aca_enabled())
1725 return ret;
1726
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001727 switch (motg->pdata->phy_type) {
1728 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001729 if (ulpi_read(phy, 0x91) & 1) {
1730 dev_dbg(phy->dev, "RID change\n");
1731 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001732 ret = msm_chg_aca_detect(motg);
1733 }
1734 default:
1735 break;
1736 }
1737 return ret;
1738}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301739
1740static void msm_otg_id_timer_func(unsigned long data)
1741{
1742 struct msm_otg *motg = (struct msm_otg *) data;
1743
1744 if (!aca_enabled())
1745 return;
1746
1747 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001748 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301749 return;
1750 }
1751
Steve Mucklef132c6c2012-06-06 18:30:57 -07001752 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301753 goto out;
1754
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301755 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001756 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301757 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301758 }
1759
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301760out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301761 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1762 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1763}
1764
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301765static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1766{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001767 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301768 u32 chg_det;
1769 bool ret = false;
1770
1771 switch (motg->pdata->phy_type) {
1772 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001773 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301774 ret = chg_det & (1 << 4);
1775 break;
1776 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001777 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301778 ret = chg_det & 1;
1779 break;
1780 default:
1781 break;
1782 }
1783 return ret;
1784}
1785
1786static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1787{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001788 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301789 u32 chg_det;
1790
1791 switch (motg->pdata->phy_type) {
1792 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001793 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301794 /* Turn off charger block */
1795 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001796 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301797 udelay(20);
1798 /* control chg block via ULPI */
1799 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001800 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301801 /* put it in host mode for enabling D- source */
1802 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001803 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301804 /* Turn on chg detect block */
1805 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001806 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301807 udelay(20);
1808 /* enable chg detection */
1809 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001810 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301811 break;
1812 case SNPS_28NM_INTEGRATED_PHY:
1813 /*
1814 * Configure DM as current source, DP as current sink
1815 * and enable battery charging comparators.
1816 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001817 ulpi_write(phy, 0x8, 0x85);
1818 ulpi_write(phy, 0x2, 0x85);
1819 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301820 break;
1821 default:
1822 break;
1823 }
1824}
1825
1826static bool msm_chg_check_primary_det(struct msm_otg *motg)
1827{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001828 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301829 u32 chg_det;
1830 bool ret = false;
1831
1832 switch (motg->pdata->phy_type) {
1833 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001834 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301835 ret = chg_det & (1 << 4);
1836 break;
1837 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001838 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301839 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301840 /* Turn off VDP_SRC */
1841 ulpi_write(phy, 0x3, 0x86);
1842 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301843 break;
1844 default:
1845 break;
1846 }
1847 return ret;
1848}
1849
1850static void msm_chg_enable_primary_det(struct msm_otg *motg)
1851{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001852 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301853 u32 chg_det;
1854
1855 switch (motg->pdata->phy_type) {
1856 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001857 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301858 /* enable chg detection */
1859 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001860 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301861 break;
1862 case SNPS_28NM_INTEGRATED_PHY:
1863 /*
1864 * Configure DP as current source, DM as current sink
1865 * and enable battery charging comparators.
1866 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001867 ulpi_write(phy, 0x2, 0x85);
1868 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301869 break;
1870 default:
1871 break;
1872 }
1873}
1874
1875static bool msm_chg_check_dcd(struct msm_otg *motg)
1876{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001877 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301878 u32 line_state;
1879 bool ret = false;
1880
1881 switch (motg->pdata->phy_type) {
1882 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001883 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301884 ret = !(line_state & 1);
1885 break;
1886 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001887 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301888 ret = line_state & 2;
1889 break;
1890 default:
1891 break;
1892 }
1893 return ret;
1894}
1895
1896static void msm_chg_disable_dcd(struct msm_otg *motg)
1897{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001898 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301899 u32 chg_det;
1900
1901 switch (motg->pdata->phy_type) {
1902 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001903 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301904 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001905 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301906 break;
1907 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001908 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301909 break;
1910 default:
1911 break;
1912 }
1913}
1914
1915static void msm_chg_enable_dcd(struct msm_otg *motg)
1916{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001917 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301918 u32 chg_det;
1919
1920 switch (motg->pdata->phy_type) {
1921 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001922 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301923 /* Turn on D+ current source */
1924 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001925 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301926 break;
1927 case SNPS_28NM_INTEGRATED_PHY:
1928 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001929 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301930 break;
1931 default:
1932 break;
1933 }
1934}
1935
1936static void msm_chg_block_on(struct msm_otg *motg)
1937{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001938 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301939 u32 func_ctrl, chg_det;
1940
1941 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001942 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301943 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1944 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001945 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301946
1947 switch (motg->pdata->phy_type) {
1948 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001949 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301950 /* control chg block via ULPI */
1951 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001952 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301953 /* Turn on chg detect block */
1954 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001955 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301956 udelay(20);
1957 break;
1958 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05301959 /* disable DP and DM pull down resistors */
1960 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301961 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001962 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301963 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001964 ulpi_write(phy, 0x1F, 0x92);
1965 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301966 udelay(100);
1967 break;
1968 default:
1969 break;
1970 }
1971}
1972
1973static void msm_chg_block_off(struct msm_otg *motg)
1974{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001975 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301976 u32 func_ctrl, chg_det;
1977
1978 switch (motg->pdata->phy_type) {
1979 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001980 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301981 /* Turn off charger block */
1982 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001983 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301984 break;
1985 case SNPS_28NM_INTEGRATED_PHY:
1986 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001987 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301988 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001989 ulpi_write(phy, 0x1F, 0x92);
1990 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301991 break;
1992 default:
1993 break;
1994 }
1995
1996 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001997 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301998 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1999 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002000 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302001}
2002
Anji jonnalad270e2d2011-08-09 11:28:32 +05302003static const char *chg_to_string(enum usb_chg_type chg_type)
2004{
2005 switch (chg_type) {
2006 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2007 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2008 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2009 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2010 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2011 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2012 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302013 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302014 default: return "INVALID_CHARGER";
2015 }
2016}
kibum.leeec040442012-08-22 22:29:46 +09002017
Devin Kimd93ff032012-09-14 15:01:31 -07002018#define MSM_CHECK_TA_DELAY (5 * HZ)
2019#define PORTSC_LS (3 << 10) /* Read - Port's Line status */
kibum.leeec040442012-08-22 22:29:46 +09002020static void msm_ta_detect_work(struct work_struct *w)
2021{
Devin Kimd93ff032012-09-14 15:01:31 -07002022 struct msm_otg *motg = container_of(w, struct msm_otg, check_ta_work.work);
kibum.leeec040442012-08-22 22:29:46 +09002023 struct usb_otg *otg = motg->phy.otg;
kibum.leeec040442012-08-22 22:29:46 +09002024
Devin Kim6b8a5d12012-10-16 23:12:18 -07002025 pr_debug("msm_ta_detect_work: ta detection work\n");
Devin Kimd93ff032012-09-14 15:01:31 -07002026
2027 /* Presence of FRame Index or FRINDEX rollover implies USB communication */
2028 if( (readl(USB_FRINDEX) != 0) || ( readl(USB_USBSTS) & (1<<3) ) ) {
2029 pr_info("msm_ta_detect_work: USB exit ta detection - frindex\n");
2030 return;
2031 }
2032
2033 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
2034 pr_info("msm_ta_detect_work: ta dectection success\n");
kibum.leeec040442012-08-22 22:29:46 +09002035 /* inform to user space that SDP is no longer detected */
2036 msm_otg_notify_charger(motg, 0);
2037 motg->chg_state = USB_CHG_STATE_DETECTED;
2038 motg->chg_type = USB_DCP_CHARGER;
2039 motg->cur_power = 0;
2040 msm_otg_start_peripheral(otg, 0);
2041 otg->phy->state = OTG_STATE_B_IDLE;
2042 schedule_work(&motg->sm_work);
2043 return;
kibum.leeec040442012-08-22 22:29:46 +09002044 }
Devin Kimd93ff032012-09-14 15:01:31 -07002045 schedule_delayed_work(&motg->check_ta_work, MSM_CHECK_TA_DELAY);
kibum.leeec040442012-08-22 22:29:46 +09002046}
Anji jonnalad270e2d2011-08-09 11:28:32 +05302047
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302048#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
2049#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302050#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2051#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302052static void msm_chg_detect_work(struct work_struct *w)
2053{
2054 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002055 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302056 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302057 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302058 unsigned long delay;
2059
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002060 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302061
2062 if (test_bit(MHL, &motg->inputs)) {
2063 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2064 return;
2065 }
2066
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302067 switch (motg->chg_state) {
2068 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302069 msm_chg_block_on(motg);
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302070 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002071 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302072 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2073 motg->dcd_retries = 0;
2074 delay = MSM_CHG_DCD_POLL_TIME;
2075 break;
2076 case USB_CHG_STATE_WAIT_FOR_DCD:
jh.koo78df86f2012-09-04 20:13:19 +09002077 if (slimport_is_connected()) {
2078 msm_chg_block_off(motg);
2079 motg->chg_state = USB_CHG_STATE_DETECTED;
2080 motg->chg_type = USB_SDP_CHARGER;
2081 queue_work(system_nrt_wq, &motg->sm_work);
2082 return;
2083 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302084 if (msm_chg_mhl_detect(motg)) {
2085 msm_chg_block_off(motg);
2086 motg->chg_state = USB_CHG_STATE_DETECTED;
2087 motg->chg_type = USB_INVALID_CHARGER;
2088 queue_work(system_nrt_wq, &motg->sm_work);
2089 return;
2090 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002091 is_aca = msm_chg_aca_detect(motg);
2092 if (is_aca) {
2093 /*
2094 * ID_A can be ACA dock too. continue
2095 * primary detection after DCD.
2096 */
2097 if (test_bit(ID_A, &motg->inputs)) {
2098 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2099 } else {
2100 delay = 0;
2101 break;
2102 }
2103 }
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302104 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302105 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2106 if (is_dcd || tmout) {
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302107 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302108 msm_chg_enable_primary_det(motg);
2109 delay = MSM_CHG_PRIMARY_DET_TIME;
2110 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2111 } else {
2112 delay = MSM_CHG_DCD_POLL_TIME;
2113 }
2114 break;
2115 case USB_CHG_STATE_DCD_DONE:
2116 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302117 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2118 dm_vlgc = line_state & PORTSC_LS_DM;
2119 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302120 if (test_bit(ID_A, &motg->inputs)) {
2121 motg->chg_type = USB_ACA_DOCK_CHARGER;
2122 motg->chg_state = USB_CHG_STATE_DETECTED;
2123 delay = 0;
2124 break;
2125 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302126 if (line_state) { /* DP > VLGC */
2127 motg->chg_type = USB_PROPRIETARY_CHARGER;
2128 motg->chg_state = USB_CHG_STATE_DETECTED;
2129 delay = 0;
2130 } else {
2131 msm_chg_enable_secondary_det(motg);
2132 delay = MSM_CHG_SECONDARY_DET_TIME;
2133 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2134 }
2135 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302136 if (test_bit(ID_A, &motg->inputs)) {
2137 motg->chg_type = USB_ACA_A_CHARGER;
2138 motg->chg_state = USB_CHG_STATE_DETECTED;
2139 delay = 0;
2140 break;
2141 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302142
2143 if (line_state) /* DP > VLGC or/and DM > VLGC */
2144 motg->chg_type = USB_PROPRIETARY_CHARGER;
2145 else
2146 motg->chg_type = USB_SDP_CHARGER;
2147
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302148 motg->chg_state = USB_CHG_STATE_DETECTED;
2149 delay = 0;
2150 }
2151 break;
2152 case USB_CHG_STATE_PRIMARY_DONE:
2153 vout = msm_chg_check_secondary_det(motg);
2154 if (vout)
2155 motg->chg_type = USB_DCP_CHARGER;
2156 else
2157 motg->chg_type = USB_CDP_CHARGER;
2158 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2159 /* fall through */
2160 case USB_CHG_STATE_SECONDARY_DONE:
2161 motg->chg_state = USB_CHG_STATE_DETECTED;
2162 case USB_CHG_STATE_DETECTED:
2163 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002164 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302165 /*
2166 * Spurious interrupt is seen after enabling ACA detection
2167 * due to which charger detection fails in case of PET.
2168 * Add delay of 100 microsec to avoid that.
2169 */
2170 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002171 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002172 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302173 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302174 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302175 return;
2176 default:
2177 return;
2178 }
2179
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302180 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302181}
2182
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302183/*
2184 * We support OTG, Peripheral only and Host only configurations. In case
2185 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2186 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2187 * enabled when switch is controlled by user and default mode is supplied
2188 * by board file, which can be changed by userspace later.
2189 */
2190static void msm_otg_init_sm(struct msm_otg *motg)
2191{
2192 struct msm_otg_platform_data *pdata = motg->pdata;
2193 u32 otgsc = readl(USB_OTGSC);
2194
2195 switch (pdata->mode) {
2196 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302198 if (pdata->default_mode == USB_HOST) {
2199 clear_bit(ID, &motg->inputs);
2200 } else if (pdata->default_mode == USB_PERIPHERAL) {
2201 set_bit(ID, &motg->inputs);
2202 set_bit(B_SESS_VLD, &motg->inputs);
2203 } else {
2204 set_bit(ID, &motg->inputs);
2205 clear_bit(B_SESS_VLD, &motg->inputs);
2206 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302207 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302208 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302209 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302210 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302211 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302212 set_bit(A_BUS_REQ, &motg->inputs);
2213 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002214 if (otgsc & OTGSC_BSV)
2215 set_bit(B_SESS_VLD, &motg->inputs);
2216 else
2217 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302218 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302219 if (pdata->pmic_id_irq) {
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302220 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302221 set_bit(ID, &motg->inputs);
2222 else
2223 clear_bit(ID, &motg->inputs);
2224 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302225 /*
2226 * VBUS initial state is reported after PMIC
2227 * driver initialization. Wait for it.
2228 */
2229 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302230 }
2231 break;
2232 case USB_HOST:
2233 clear_bit(ID, &motg->inputs);
2234 break;
2235 case USB_PERIPHERAL:
2236 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302237 if (pdata->otg_control == OTG_PHY_CONTROL) {
2238 if (otgsc & OTGSC_BSV)
2239 set_bit(B_SESS_VLD, &motg->inputs);
2240 else
2241 clear_bit(B_SESS_VLD, &motg->inputs);
2242 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2243 /*
2244 * VBUS initial state is reported after PMIC
2245 * driver initialization. Wait for it.
2246 */
2247 wait_for_completion(&pmic_vbus_init);
2248 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302249 break;
2250 default:
2251 break;
2252 }
2253}
2254
2255static void msm_otg_sm_work(struct work_struct *w)
2256{
2257 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002258 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302259 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302260
Steve Mucklef132c6c2012-06-06 18:30:57 -07002261 pm_runtime_resume(otg->phy->dev);
2262 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002263 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302264 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002265 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302266 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302267 psy = power_supply_get_by_name("usb");
2268 if (!psy)
2269 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002270 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302271 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2272 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002273 pm_runtime_put_noidle(otg->phy->dev);
2274 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302275 break;
2276 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302277 /* FALL THROUGH */
2278 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302279 if (test_bit(MHL, &motg->inputs)) {
2280 /* allow LPM */
2281 pm_runtime_put_noidle(otg->phy->dev);
2282 pm_runtime_suspend(otg->phy->dev);
2283 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002284 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302285 pr_debug("!id || id_A\n");
jh.koo78df86f2012-09-04 20:13:19 +09002286 if (slimport_is_connected()) {
2287 work = 1;
2288 break;
2289 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302290 if (msm_chg_mhl_detect(motg)) {
2291 work = 1;
2292 break;
2293 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302294 clear_bit(B_BUS_REQ, &motg->inputs);
2295 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002296 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302297 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302298 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302299 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302300 switch (motg->chg_state) {
2301 case USB_CHG_STATE_UNDEFINED:
2302 msm_chg_detect_work(&motg->chg_work.work);
2303 break;
2304 case USB_CHG_STATE_DETECTED:
2305 switch (motg->chg_type) {
2306 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302307 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002308 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302309 /* fall through */
2310 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302311 msm_otg_notify_charger(motg,
2312 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002313 pm_runtime_put_noidle(otg->phy->dev);
2314 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302315 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302316 case USB_ACA_B_CHARGER:
2317 msm_otg_notify_charger(motg,
2318 IDEV_ACA_CHG_MAX);
2319 /*
2320 * (ID_B --> ID_C) PHY_ALT interrupt can
2321 * not be detected in LPM.
2322 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302323 break;
2324 case USB_CDP_CHARGER:
2325 msm_otg_notify_charger(motg,
2326 IDEV_CHG_MAX);
2327 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002328 otg->phy->state =
2329 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302330 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302331 case USB_ACA_C_CHARGER:
2332 msm_otg_notify_charger(motg,
2333 IDEV_ACA_CHG_MAX);
2334 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002335 otg->phy->state =
2336 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302337 break;
2338 case USB_SDP_CHARGER:
roy.park99cef212012-07-26 17:00:40 -07002339 msm_otg_notify_charger(motg,
2340 IDEV_CHG_MIN);
jh.koo78df86f2012-09-04 20:13:19 +09002341 if(!slimport_is_connected()) {
2342 msm_otg_start_peripheral(otg, 1);
2343 otg->phy->state =
2344 OTG_STATE_B_PERIPHERAL;
2345 }
kibum.leeec040442012-08-22 22:29:46 +09002346 schedule_delayed_work(&motg->check_ta_work,
2347 MSM_CHECK_TA_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302348 break;
2349 default:
2350 break;
2351 }
2352 break;
2353 default:
2354 break;
2355 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302356 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2357 pr_debug("b_sess_end && b_bus_req\n");
2358 if (msm_otg_start_srp(otg) < 0) {
2359 clear_bit(B_BUS_REQ, &motg->inputs);
2360 work = 1;
2361 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302362 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002363 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302364 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2365 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302366 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302367 pr_debug("chg_work cancel");
Mayank Rana9062a672013-01-23 19:26:21 +05302368 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302369 cancel_delayed_work_sync(&motg->chg_work);
kibum.leeec040442012-08-22 22:29:46 +09002370 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302371 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2372 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302373 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002374 msm_otg_reset(otg->phy);
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302375 /*
2376 * There is a small window where ID interrupt
2377 * is not monitored during ID detection circuit
2378 * switch from ACA to PMIC. Check ID state
2379 * before entering into low power mode.
2380 */
2381 if (!msm_otg_read_pmic_id_state(motg)) {
2382 pr_debug("process missed ID intr\n");
2383 clear_bit(ID, &motg->inputs);
2384 work = 1;
2385 break;
2386 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002387 pm_runtime_put_noidle(otg->phy->dev);
2388 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302389 }
2390 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302391 case OTG_STATE_B_SRP_INIT:
2392 if (!test_bit(ID, &motg->inputs) ||
2393 test_bit(ID_A, &motg->inputs) ||
2394 test_bit(ID_C, &motg->inputs) ||
2395 (test_bit(B_SESS_VLD, &motg->inputs) &&
2396 !test_bit(ID_B, &motg->inputs))) {
2397 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2398 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002399 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302400 /*
2401 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2402 * bits after SRP initiation.
2403 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002404 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302405 work = 1;
2406 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2407 pr_debug("b_srp_fail\n");
2408 pr_info("A-device did not respond to SRP\n");
2409 clear_bit(B_BUS_REQ, &motg->inputs);
2410 clear_bit(B_SRP_FAIL, &motg->tmouts);
2411 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002412 ulpi_write(otg->phy, 0x0, 0x98);
2413 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302414 motg->b_last_se0_sess = jiffies;
2415 work = 1;
2416 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302417 break;
2418 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302419 if (!test_bit(ID, &motg->inputs) ||
2420 test_bit(ID_A, &motg->inputs) ||
2421 test_bit(ID_B, &motg->inputs) ||
2422 !test_bit(B_SESS_VLD, &motg->inputs)) {
2423 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302424 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2425 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302426 msm_otg_notify_charger(motg, 0);
2427 srp_reqd = otg->gadget->otg_srp_reqd;
2428 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302429 if (test_bit(ID_B, &motg->inputs))
2430 clear_bit(ID_B, &motg->inputs);
2431 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002432 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302433 motg->b_last_se0_sess = jiffies;
2434 if (srp_reqd)
2435 msm_otg_start_timer(motg,
2436 TB_TST_SRP, B_TST_SRP);
2437 else
2438 work = 1;
2439 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2440 otg->gadget->b_hnp_enable &&
2441 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2442 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2443 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2444 /* D+ pullup should not be disconnected within 4msec
2445 * after A device suspends the bus. Otherwise PET will
2446 * fail the compliance test.
2447 */
2448 udelay(1000);
2449 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002450 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302451 /*
2452 * start HCD even before A-device enable
2453 * pull-up to meet HNP timings.
2454 */
2455 otg->host->is_b_host = 1;
2456 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002457 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2458 test_bit(B_SESS_VLD, &motg->inputs)) {
2459 pr_debug("a_bus_suspend && b_sess_vld\n");
2460 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002461 pm_runtime_put_noidle(otg->phy->dev);
2462 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002463 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002464 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302465 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002466 }
2467 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302468 case OTG_STATE_B_WAIT_ACON:
2469 if (!test_bit(ID, &motg->inputs) ||
2470 test_bit(ID_A, &motg->inputs) ||
2471 test_bit(ID_B, &motg->inputs) ||
2472 !test_bit(B_SESS_VLD, &motg->inputs)) {
2473 pr_debug("!id || id_a/b || !b_sess_vld\n");
2474 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302475 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302476 * A-device is physically disconnected during
2477 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302478 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302479 msm_otg_start_host(otg, 0);
2480 otg->host->is_b_host = 0;
2481
2482 clear_bit(B_BUS_REQ, &motg->inputs);
2483 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2484 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002485 otg->phy->state = OTG_STATE_B_IDLE;
2486 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302487 work = 1;
2488 } else if (test_bit(A_CONN, &motg->inputs)) {
2489 pr_debug("a_conn\n");
2490 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002491 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302492 /*
2493 * PET disconnects D+ pullup after reset is generated
2494 * by B device in B_HOST role which is not detected by
2495 * B device. As workaorund , start timer of 300msec
2496 * and stop timer if A device is enumerated else clear
2497 * A_CONN.
2498 */
2499 msm_otg_start_timer(motg, TB_TST_CONFIG,
2500 B_TST_CONFIG);
2501 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2502 pr_debug("b_ase0_brst_tmout\n");
2503 pr_info("B HNP fail:No response from A device\n");
2504 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002505 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302506 otg->host->is_b_host = 0;
2507 clear_bit(B_ASE0_BRST, &motg->tmouts);
2508 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2509 clear_bit(B_BUS_REQ, &motg->inputs);
2510 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002511 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302512 work = 1;
2513 } else if (test_bit(ID_C, &motg->inputs)) {
2514 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2515 }
2516 break;
2517 case OTG_STATE_B_HOST:
2518 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2519 !test_bit(A_CONN, &motg->inputs) ||
2520 !test_bit(B_SESS_VLD, &motg->inputs)) {
2521 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2522 clear_bit(A_CONN, &motg->inputs);
2523 clear_bit(B_BUS_REQ, &motg->inputs);
2524 msm_otg_start_host(otg, 0);
2525 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002526 otg->phy->state = OTG_STATE_B_IDLE;
2527 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302528 work = 1;
2529 } else if (test_bit(ID_C, &motg->inputs)) {
2530 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2531 }
2532 break;
2533 case OTG_STATE_A_IDLE:
2534 otg->default_a = 1;
2535 if (test_bit(ID, &motg->inputs) &&
2536 !test_bit(ID_A, &motg->inputs)) {
2537 pr_debug("id && !id_a\n");
2538 otg->default_a = 0;
2539 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002540 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302541 del_timer_sync(&motg->id_timer);
2542 msm_otg_link_reset(motg);
2543 msm_chg_enable_aca_intr(motg);
2544 msm_otg_notify_charger(motg, 0);
2545 work = 1;
2546 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2547 (test_bit(A_SRP_DET, &motg->inputs) ||
2548 test_bit(A_BUS_REQ, &motg->inputs))) {
2549 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2550
2551 clear_bit(A_SRP_DET, &motg->inputs);
2552 /* Disable SRP detection */
2553 writel_relaxed((readl_relaxed(USB_OTGSC) &
2554 ~OTGSC_INTSTS_MASK) &
2555 ~OTGSC_DPIE, USB_OTGSC);
2556
Steve Mucklef132c6c2012-06-06 18:30:57 -07002557 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302558 /* VBUS should not be supplied before end of SRP pulse
2559 * generated by PET, if not complaince test fail.
2560 */
2561 usleep_range(10000, 12000);
2562 /* ACA: ID_A: Stop charging untill enumeration */
2563 if (test_bit(ID_A, &motg->inputs))
2564 msm_otg_notify_charger(motg, 0);
2565 else
2566 msm_hsusb_vbus_power(motg, 1);
2567 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2568 } else {
2569 pr_debug("No session requested\n");
2570 clear_bit(A_BUS_DROP, &motg->inputs);
2571 if (test_bit(ID_A, &motg->inputs)) {
2572 msm_otg_notify_charger(motg,
2573 IDEV_ACA_CHG_MAX);
2574 } else if (!test_bit(ID, &motg->inputs)) {
2575 msm_otg_notify_charger(motg, 0);
2576 /*
2577 * A-device is not providing power on VBUS.
2578 * Enable SRP detection.
2579 */
2580 writel_relaxed(0x13, USB_USBMODE);
2581 writel_relaxed((readl_relaxed(USB_OTGSC) &
2582 ~OTGSC_INTSTS_MASK) |
2583 OTGSC_DPIE, USB_OTGSC);
2584 mb();
2585 }
2586 }
2587 break;
2588 case OTG_STATE_A_WAIT_VRISE:
2589 if ((test_bit(ID, &motg->inputs) &&
2590 !test_bit(ID_A, &motg->inputs)) ||
2591 test_bit(A_BUS_DROP, &motg->inputs) ||
2592 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2593 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2594 clear_bit(A_BUS_REQ, &motg->inputs);
2595 msm_otg_del_timer(motg);
2596 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002597 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302598 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2599 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2600 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002601 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302602 if (TA_WAIT_BCON > 0)
2603 msm_otg_start_timer(motg, TA_WAIT_BCON,
2604 A_WAIT_BCON);
2605 msm_otg_start_host(otg, 1);
2606 msm_chg_enable_aca_det(motg);
2607 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302608 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302609 if (msm_chg_check_aca_intr(motg))
2610 work = 1;
2611 }
2612 break;
2613 case OTG_STATE_A_WAIT_BCON:
2614 if ((test_bit(ID, &motg->inputs) &&
2615 !test_bit(ID_A, &motg->inputs)) ||
2616 test_bit(A_BUS_DROP, &motg->inputs) ||
2617 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2618 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2619 "a_wait_bcon_tmout\n");
2620 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2621 pr_info("Device No Response\n");
2622 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2623 }
2624 msm_otg_del_timer(motg);
2625 clear_bit(A_BUS_REQ, &motg->inputs);
2626 clear_bit(B_CONN, &motg->inputs);
2627 msm_otg_start_host(otg, 0);
2628 /*
2629 * ACA: ID_A with NO accessory, just the A plug is
2630 * attached to ACA: Use IDCHG_MAX for charging
2631 */
2632 if (test_bit(ID_A, &motg->inputs))
2633 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2634 else
2635 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002636 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302637 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2638 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2639 pr_debug("!a_vbus_vld\n");
2640 clear_bit(B_CONN, &motg->inputs);
2641 msm_otg_del_timer(motg);
2642 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002643 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2644 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302645 } else if (test_bit(ID_A, &motg->inputs)) {
2646 msm_hsusb_vbus_power(motg, 0);
2647 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2648 /*
2649 * If TA_WAIT_BCON is infinite, we don;t
2650 * turn off VBUS. Enter low power mode.
2651 */
2652 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002653 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302654 } else if (!test_bit(ID, &motg->inputs)) {
2655 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302656 }
2657 break;
2658 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302659 if ((test_bit(ID, &motg->inputs) &&
2660 !test_bit(ID_A, &motg->inputs)) ||
2661 test_bit(A_BUS_DROP, &motg->inputs)) {
2662 pr_debug("id_a/b/c || a_bus_drop\n");
2663 clear_bit(B_CONN, &motg->inputs);
2664 clear_bit(A_BUS_REQ, &motg->inputs);
2665 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002666 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302667 msm_otg_start_host(otg, 0);
2668 if (!test_bit(ID_A, &motg->inputs))
2669 msm_hsusb_vbus_power(motg, 0);
2670 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2671 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2672 pr_debug("!a_vbus_vld\n");
2673 clear_bit(B_CONN, &motg->inputs);
2674 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002675 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302676 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002677 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302678 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2679 /*
2680 * a_bus_req is de-asserted when root hub is
2681 * suspended or HNP is in progress.
2682 */
2683 pr_debug("!a_bus_req\n");
2684 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002685 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302686 if (otg->host->b_hnp_enable)
2687 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2688 A_AIDL_BDIS);
2689 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002690 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302691 } else if (!test_bit(B_CONN, &motg->inputs)) {
2692 pr_debug("!b_conn\n");
2693 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002694 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302695 if (TA_WAIT_BCON > 0)
2696 msm_otg_start_timer(motg, TA_WAIT_BCON,
2697 A_WAIT_BCON);
2698 if (msm_chg_check_aca_intr(motg))
2699 work = 1;
2700 } else if (test_bit(ID_A, &motg->inputs)) {
2701 msm_otg_del_timer(motg);
2702 msm_hsusb_vbus_power(motg, 0);
2703 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2704 msm_otg_notify_charger(motg,
2705 IDEV_ACA_CHG_MAX);
2706 else
2707 msm_otg_notify_charger(motg,
2708 IDEV_CHG_MIN - motg->mA_port);
2709 } else if (!test_bit(ID, &motg->inputs)) {
2710 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2711 motg->chg_type = USB_INVALID_CHARGER;
2712 msm_otg_notify_charger(motg, 0);
2713 msm_hsusb_vbus_power(motg, 1);
2714 }
2715 break;
2716 case OTG_STATE_A_SUSPEND:
2717 if ((test_bit(ID, &motg->inputs) &&
2718 !test_bit(ID_A, &motg->inputs)) ||
2719 test_bit(A_BUS_DROP, &motg->inputs) ||
2720 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2721 pr_debug("id_a/b/c || a_bus_drop ||"
2722 "a_aidl_bdis_tmout\n");
2723 msm_otg_del_timer(motg);
2724 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002725 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302726 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002727 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302728 if (!test_bit(ID_A, &motg->inputs))
2729 msm_hsusb_vbus_power(motg, 0);
2730 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2731 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2732 pr_debug("!a_vbus_vld\n");
2733 msm_otg_del_timer(motg);
2734 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002735 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302736 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002737 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302738 } else if (!test_bit(B_CONN, &motg->inputs) &&
2739 otg->host->b_hnp_enable) {
2740 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002741 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302742 msm_otg_host_hnp_enable(otg, 1);
2743 otg->gadget->is_a_peripheral = 1;
2744 msm_otg_start_peripheral(otg, 1);
2745 } else if (!test_bit(B_CONN, &motg->inputs) &&
2746 !otg->host->b_hnp_enable) {
2747 pr_debug("!b_conn && !b_hnp_enable");
2748 /*
2749 * bus request is dropped during suspend.
2750 * acquire again for next device.
2751 */
2752 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002753 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302754 if (TA_WAIT_BCON > 0)
2755 msm_otg_start_timer(motg, TA_WAIT_BCON,
2756 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002757 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302758 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002759 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302760 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002761 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002762 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302763 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302764 }
2765 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302766 case OTG_STATE_A_PERIPHERAL:
2767 if ((test_bit(ID, &motg->inputs) &&
2768 !test_bit(ID_A, &motg->inputs)) ||
2769 test_bit(A_BUS_DROP, &motg->inputs)) {
2770 pr_debug("id _f/b/c || a_bus_drop\n");
2771 /* Clear BIDL_ADIS timer */
2772 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002773 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302774 msm_otg_start_peripheral(otg, 0);
2775 otg->gadget->is_a_peripheral = 0;
2776 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002777 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302778 if (!test_bit(ID_A, &motg->inputs))
2779 msm_hsusb_vbus_power(motg, 0);
2780 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2781 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2782 pr_debug("!a_vbus_vld\n");
2783 /* Clear BIDL_ADIS timer */
2784 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002785 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302786 msm_otg_start_peripheral(otg, 0);
2787 otg->gadget->is_a_peripheral = 0;
2788 msm_otg_start_host(otg, 0);
2789 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2790 pr_debug("a_bidl_adis_tmout\n");
2791 msm_otg_start_peripheral(otg, 0);
2792 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002793 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302794 set_bit(A_BUS_REQ, &motg->inputs);
2795 msm_otg_host_hnp_enable(otg, 0);
2796 if (TA_WAIT_BCON > 0)
2797 msm_otg_start_timer(motg, TA_WAIT_BCON,
2798 A_WAIT_BCON);
2799 } else if (test_bit(ID_A, &motg->inputs)) {
2800 msm_hsusb_vbus_power(motg, 0);
2801 msm_otg_notify_charger(motg,
2802 IDEV_CHG_MIN - motg->mA_port);
2803 } else if (!test_bit(ID, &motg->inputs)) {
2804 msm_otg_notify_charger(motg, 0);
2805 msm_hsusb_vbus_power(motg, 1);
2806 }
2807 break;
2808 case OTG_STATE_A_WAIT_VFALL:
2809 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2810 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002811 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302812 work = 1;
2813 }
2814 break;
2815 case OTG_STATE_A_VBUS_ERR:
2816 if ((test_bit(ID, &motg->inputs) &&
2817 !test_bit(ID_A, &motg->inputs)) ||
2818 test_bit(A_BUS_DROP, &motg->inputs) ||
2819 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002820 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302821 if (!test_bit(ID_A, &motg->inputs))
2822 msm_hsusb_vbus_power(motg, 0);
2823 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2824 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2825 motg->chg_type = USB_INVALID_CHARGER;
2826 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302827 }
2828 break;
2829 default:
2830 break;
2831 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302832 if (work)
2833 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302834}
2835
2836static irqreturn_t msm_otg_irq(int irq, void *data)
2837{
2838 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002839 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302840 u32 otgsc = 0, usbsts, pc;
2841 bool work = 0;
2842 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302843
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302844 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002845 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302846 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002847 motg->async_int = irq;
Jack Phamb1aa4d82012-08-13 15:32:39 -07002848 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002849 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302850 return IRQ_HANDLED;
2851 }
2852
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002853 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302854 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302855
2856 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302857 return IRQ_NONE;
2858
2859 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302860 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302861 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302862 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302863 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302864 pr_debug("Id clear\n");
2865 /*
2866 * Assert a_bus_req to supply power on
2867 * VBUS when Micro/Mini-A cable is connected
2868 * with out user intervention.
2869 */
2870 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302871 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302872 msm_chg_enable_aca_det(motg);
2873 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302874 writel_relaxed(otgsc, USB_OTGSC);
2875 work = 1;
2876 } else if (otgsc & OTGSC_DPIS) {
2877 pr_debug("DPIS detected\n");
2878 writel_relaxed(otgsc, USB_OTGSC);
2879 set_bit(A_SRP_DET, &motg->inputs);
2880 set_bit(A_BUS_REQ, &motg->inputs);
2881 work = 1;
2882 } else if (otgsc & OTGSC_BSVIS) {
2883 writel_relaxed(otgsc, USB_OTGSC);
2884 /*
2885 * BSV interrupt comes when operating as an A-device
2886 * (VBUS on/off).
2887 * But, handle BSV when charger is removed from ACA in ID_A
2888 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002889 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302890 !test_bit(ID_A, &motg->inputs))
2891 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302892 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302893 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302894 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302895 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302896 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302897 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002898 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2899
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302900 msm_chg_check_aca_intr(motg);
2901 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302902 work = 1;
2903 } else if (usbsts & STS_PCI) {
2904 pc = readl_relaxed(USB_PORTSC);
2905 pr_debug("portsc = %x\n", pc);
2906 ret = IRQ_NONE;
2907 /*
2908 * HCD Acks PCI interrupt. We use this to switch
2909 * between different OTG states.
2910 */
2911 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002912 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302913 case OTG_STATE_A_SUSPEND:
2914 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2915 !(pc & PORTSC_CCS)) {
2916 pr_debug("B_CONN clear\n");
2917 clear_bit(B_CONN, &motg->inputs);
2918 msm_otg_del_timer(motg);
2919 }
2920 break;
2921 case OTG_STATE_A_PERIPHERAL:
2922 /*
2923 * A-peripheral observed activity on bus.
2924 * clear A_BIDL_ADIS timer.
2925 */
2926 msm_otg_del_timer(motg);
2927 work = 0;
2928 break;
2929 case OTG_STATE_B_WAIT_ACON:
2930 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2931 pr_debug("A_CONN set\n");
2932 set_bit(A_CONN, &motg->inputs);
2933 /* Clear ASE0_BRST timer */
2934 msm_otg_del_timer(motg);
2935 }
2936 break;
2937 case OTG_STATE_B_HOST:
2938 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2939 pr_debug("A_CONN clear\n");
2940 clear_bit(A_CONN, &motg->inputs);
2941 msm_otg_del_timer(motg);
2942 }
2943 break;
2944 case OTG_STATE_A_WAIT_BCON:
2945 if (TA_WAIT_BCON < 0)
2946 set_bit(A_BUS_REQ, &motg->inputs);
2947 default:
2948 work = 0;
2949 break;
2950 }
2951 } else if (usbsts & STS_URI) {
2952 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002953 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302954 case OTG_STATE_A_PERIPHERAL:
2955 /*
2956 * A-peripheral observed activity on bus.
2957 * clear A_BIDL_ADIS timer.
2958 */
2959 msm_otg_del_timer(motg);
2960 work = 0;
2961 break;
2962 default:
2963 work = 0;
2964 break;
2965 }
2966 } else if (usbsts & STS_SLI) {
2967 ret = IRQ_NONE;
2968 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002969 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302970 case OTG_STATE_B_PERIPHERAL:
2971 if (otg->gadget->b_hnp_enable) {
2972 set_bit(A_BUS_SUSPEND, &motg->inputs);
2973 set_bit(B_BUS_REQ, &motg->inputs);
2974 work = 1;
2975 }
2976 break;
2977 case OTG_STATE_A_PERIPHERAL:
2978 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2979 A_BIDL_ADIS);
2980 break;
2981 default:
2982 break;
2983 }
2984 } else if ((usbsts & PHY_ALT_INT)) {
2985 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2986 if (msm_chg_check_aca_intr(motg))
2987 work = 1;
2988 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302989 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302990 if (work)
2991 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302992
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302993 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002994}
2995
2996static void msm_otg_set_vbus_state(int online)
2997{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302998 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002999 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303000 struct usb_otg *otg = motg->phy.otg;
3001
3002 /* In A Host Mode, ignore received BSV interrupts */
3003 if (otg->phy->state >= OTG_STATE_A_IDLE)
3004 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003005
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303006 if (online) {
3007 pr_debug("PMIC: BSV set\n");
3008 set_bit(B_SESS_VLD, &motg->inputs);
3009 } else {
3010 pr_debug("PMIC: BSV clear\n");
3011 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303012 }
3013
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303014 if (!init) {
3015 init = true;
3016 complete(&pmic_vbus_init);
3017 pr_debug("PMIC: BSV init complete\n");
3018 return;
3019 }
3020
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303021 if (test_bit(MHL, &motg->inputs) ||
3022 mhl_det_in_progress) {
3023 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3024 return;
3025 }
3026
Jack Pham5ca279b2012-05-14 18:42:54 -07003027 if (atomic_read(&motg->pm_suspended))
3028 motg->sm_work_pending = true;
3029 else
3030 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003031}
3032
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303033static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003034{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303035 struct msm_otg *motg = container_of(w, struct msm_otg,
3036 pmic_id_status_work.work);
3037 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003038
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05303039 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303040 if (!test_and_set_bit(ID, &motg->inputs)) {
3041 pr_debug("PMIC: ID set\n");
3042 work = 1;
3043 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303044 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303045 if (test_and_clear_bit(ID, &motg->inputs)) {
3046 pr_debug("PMIC: ID clear\n");
3047 set_bit(A_BUS_REQ, &motg->inputs);
3048 work = 1;
3049 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303050 }
3051
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303052 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003053 if (atomic_read(&motg->pm_suspended))
3054 motg->sm_work_pending = true;
3055 else
3056 queue_work(system_nrt_wq, &motg->sm_work);
3057 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303058
3059}
3060
3061#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3062static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3063{
3064 struct msm_otg *motg = data;
3065
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303066 if (test_bit(MHL, &motg->inputs) ||
3067 mhl_det_in_progress) {
3068 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3069 return IRQ_HANDLED;
3070 }
3071
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303072 if (!aca_id_turned_on)
3073 /*schedule delayed work for 5msec for ID line state to settle*/
3074 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3075 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003076
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303077 return IRQ_HANDLED;
3078}
3079
3080static int msm_otg_mode_show(struct seq_file *s, void *unused)
3081{
3082 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003083 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303084
Steve Mucklef132c6c2012-06-06 18:30:57 -07003085 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303086 case OTG_STATE_A_HOST:
3087 seq_printf(s, "host\n");
3088 break;
3089 case OTG_STATE_B_PERIPHERAL:
3090 seq_printf(s, "peripheral\n");
3091 break;
3092 default:
3093 seq_printf(s, "none\n");
3094 break;
3095 }
3096
3097 return 0;
3098}
3099
3100static int msm_otg_mode_open(struct inode *inode, struct file *file)
3101{
3102 return single_open(file, msm_otg_mode_show, inode->i_private);
3103}
3104
3105static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3106 size_t count, loff_t *ppos)
3107{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303108 struct seq_file *s = file->private_data;
3109 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303110 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003111 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303112 int status = count;
3113 enum usb_mode_type req_mode;
3114
3115 memset(buf, 0x00, sizeof(buf));
3116
3117 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3118 status = -EFAULT;
3119 goto out;
3120 }
3121
3122 if (!strncmp(buf, "host", 4)) {
3123 req_mode = USB_HOST;
3124 } else if (!strncmp(buf, "peripheral", 10)) {
3125 req_mode = USB_PERIPHERAL;
3126 } else if (!strncmp(buf, "none", 4)) {
3127 req_mode = USB_NONE;
3128 } else {
3129 status = -EINVAL;
3130 goto out;
3131 }
3132
3133 switch (req_mode) {
3134 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003135 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303136 case OTG_STATE_A_HOST:
3137 case OTG_STATE_B_PERIPHERAL:
3138 set_bit(ID, &motg->inputs);
3139 clear_bit(B_SESS_VLD, &motg->inputs);
3140 break;
3141 default:
3142 goto out;
3143 }
3144 break;
3145 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003146 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303147 case OTG_STATE_B_IDLE:
3148 case OTG_STATE_A_HOST:
3149 set_bit(ID, &motg->inputs);
3150 set_bit(B_SESS_VLD, &motg->inputs);
3151 break;
3152 default:
3153 goto out;
3154 }
3155 break;
3156 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003157 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303158 case OTG_STATE_B_IDLE:
3159 case OTG_STATE_B_PERIPHERAL:
3160 clear_bit(ID, &motg->inputs);
3161 break;
3162 default:
3163 goto out;
3164 }
3165 break;
3166 default:
3167 goto out;
3168 }
3169
Steve Mucklef132c6c2012-06-06 18:30:57 -07003170 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303171 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303172out:
3173 return status;
3174}
3175
3176const struct file_operations msm_otg_mode_fops = {
3177 .open = msm_otg_mode_open,
3178 .read = seq_read,
3179 .write = msm_otg_mode_write,
3180 .llseek = seq_lseek,
3181 .release = single_release,
3182};
3183
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303184static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3185{
3186 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003187 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303188
Steve Mucklef132c6c2012-06-06 18:30:57 -07003189 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303190 return 0;
3191}
3192
3193static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3194{
3195 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3196}
3197
3198const struct file_operations msm_otg_state_fops = {
3199 .open = msm_otg_otg_state_open,
3200 .read = seq_read,
3201 .llseek = seq_lseek,
3202 .release = single_release,
3203};
3204
Anji jonnalad270e2d2011-08-09 11:28:32 +05303205static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3206{
3207 struct msm_otg *motg = s->private;
3208
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303209 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303210 return 0;
3211}
3212
3213static int msm_otg_chg_open(struct inode *inode, struct file *file)
3214{
3215 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3216}
3217
3218const struct file_operations msm_otg_chg_fops = {
3219 .open = msm_otg_chg_open,
3220 .read = seq_read,
3221 .llseek = seq_lseek,
3222 .release = single_release,
3223};
3224
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303225static int msm_otg_aca_show(struct seq_file *s, void *unused)
3226{
3227 if (debug_aca_enabled)
3228 seq_printf(s, "enabled\n");
3229 else
3230 seq_printf(s, "disabled\n");
3231
3232 return 0;
3233}
3234
3235static int msm_otg_aca_open(struct inode *inode, struct file *file)
3236{
3237 return single_open(file, msm_otg_aca_show, inode->i_private);
3238}
3239
3240static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3241 size_t count, loff_t *ppos)
3242{
3243 char buf[8];
3244
3245 memset(buf, 0x00, sizeof(buf));
3246
3247 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3248 return -EFAULT;
3249
3250 if (!strncmp(buf, "enable", 6))
3251 debug_aca_enabled = true;
3252 else
3253 debug_aca_enabled = false;
3254
3255 return count;
3256}
3257
3258const struct file_operations msm_otg_aca_fops = {
3259 .open = msm_otg_aca_open,
3260 .read = seq_read,
3261 .write = msm_otg_aca_write,
3262 .llseek = seq_lseek,
3263 .release = single_release,
3264};
3265
Manu Gautam8bdcc592012-03-06 11:26:06 +05303266static int msm_otg_bus_show(struct seq_file *s, void *unused)
3267{
3268 if (debug_bus_voting_enabled)
3269 seq_printf(s, "enabled\n");
3270 else
3271 seq_printf(s, "disabled\n");
3272
3273 return 0;
3274}
3275
3276static int msm_otg_bus_open(struct inode *inode, struct file *file)
3277{
3278 return single_open(file, msm_otg_bus_show, inode->i_private);
3279}
3280
3281static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3282 size_t count, loff_t *ppos)
3283{
3284 char buf[8];
3285 int ret;
3286 struct seq_file *s = file->private_data;
3287 struct msm_otg *motg = s->private;
3288
3289 memset(buf, 0x00, sizeof(buf));
3290
3291 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3292 return -EFAULT;
3293
3294 if (!strncmp(buf, "enable", 6)) {
3295 /* Do not vote here. Let OTG statemachine decide when to vote */
3296 debug_bus_voting_enabled = true;
3297 } else {
3298 debug_bus_voting_enabled = false;
3299 if (motg->bus_perf_client) {
3300 ret = msm_bus_scale_client_update_request(
3301 motg->bus_perf_client, 0);
3302 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003303 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303304 "for bus bw %d\n", __func__, ret);
3305 }
3306 }
3307
3308 return count;
3309}
3310
3311const struct file_operations msm_otg_bus_fops = {
3312 .open = msm_otg_bus_open,
3313 .read = seq_read,
3314 .write = msm_otg_bus_write,
3315 .llseek = seq_lseek,
3316 .release = single_release,
3317};
3318
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303319static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303320
3321static int msm_otg_debugfs_init(struct msm_otg *motg)
3322{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303323 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303324
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303325 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3326
3327 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3328 return -ENODEV;
3329
Anji jonnalad270e2d2011-08-09 11:28:32 +05303330 if (motg->pdata->mode == USB_OTG &&
3331 motg->pdata->otg_control == OTG_USER_CONTROL) {
3332
Manu Gautam8bdcc592012-03-06 11:26:06 +05303333 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303334 S_IWUSR, msm_otg_dbg_root, motg,
3335 &msm_otg_mode_fops);
3336
Manu Gautam8bdcc592012-03-06 11:26:06 +05303337 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303338 debugfs_remove(msm_otg_dbg_root);
3339 msm_otg_dbg_root = NULL;
3340 return -ENODEV;
3341 }
3342 }
3343
Manu Gautam8bdcc592012-03-06 11:26:06 +05303344 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303345 msm_otg_dbg_root, motg,
3346 &msm_otg_chg_fops);
3347
Manu Gautam8bdcc592012-03-06 11:26:06 +05303348 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303349 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303350 return -ENODEV;
3351 }
3352
Manu Gautam8bdcc592012-03-06 11:26:06 +05303353 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303354 msm_otg_dbg_root, motg,
3355 &msm_otg_aca_fops);
3356
Manu Gautam8bdcc592012-03-06 11:26:06 +05303357 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303358 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303359 return -ENODEV;
3360 }
3361
Manu Gautam8bdcc592012-03-06 11:26:06 +05303362 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3363 msm_otg_dbg_root, motg,
3364 &msm_otg_bus_fops);
3365
3366 if (!msm_otg_dentry) {
3367 debugfs_remove_recursive(msm_otg_dbg_root);
3368 return -ENODEV;
3369 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303370
3371 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3372 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3373
3374 if (!msm_otg_dentry) {
3375 debugfs_remove_recursive(msm_otg_dbg_root);
3376 return -ENODEV;
3377 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303378 return 0;
3379}
3380
3381static void msm_otg_debugfs_cleanup(void)
3382{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303383 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303384}
3385
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303386static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3387static struct platform_device *msm_otg_add_pdev(
3388 struct platform_device *ofdev, const char *name)
3389{
3390 struct platform_device *pdev;
3391 const struct resource *res = ofdev->resource;
3392 unsigned int num = ofdev->num_resources;
3393 int retval;
3394
3395 pdev = platform_device_alloc(name, -1);
3396 if (!pdev) {
3397 retval = -ENOMEM;
3398 goto error;
3399 }
3400
3401 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3402 pdev->dev.dma_mask = &msm_otg_dma_mask;
3403
3404 if (num) {
3405 retval = platform_device_add_resources(pdev, res, num);
3406 if (retval)
3407 goto error;
3408 }
3409
3410 retval = platform_device_add(pdev);
3411 if (retval)
3412 goto error;
3413
3414 return pdev;
3415
3416error:
3417 platform_device_put(pdev);
3418 return ERR_PTR(retval);
3419}
3420
3421static int msm_otg_setup_devices(struct platform_device *ofdev,
3422 enum usb_mode_type mode, bool init)
3423{
3424 const char *gadget_name = "msm_hsusb";
3425 const char *host_name = "msm_hsusb_host";
3426 static struct platform_device *gadget_pdev;
3427 static struct platform_device *host_pdev;
3428 int retval = 0;
3429
3430 if (!init) {
3431 if (gadget_pdev)
3432 platform_device_unregister(gadget_pdev);
3433 if (host_pdev)
3434 platform_device_unregister(host_pdev);
3435 return 0;
3436 }
3437
3438 switch (mode) {
3439 case USB_OTG:
3440 /* fall through */
3441 case USB_PERIPHERAL:
3442 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3443 if (IS_ERR(gadget_pdev)) {
3444 retval = PTR_ERR(gadget_pdev);
3445 break;
3446 }
3447 if (mode == USB_PERIPHERAL)
3448 break;
3449 /* fall through */
3450 case USB_HOST:
3451 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3452 if (IS_ERR(host_pdev)) {
3453 retval = PTR_ERR(host_pdev);
3454 if (mode == USB_OTG)
3455 platform_device_unregister(gadget_pdev);
3456 }
3457 break;
3458 default:
3459 break;
3460 }
3461
3462 return retval;
3463}
3464
3465struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3466{
3467 struct device_node *node = pdev->dev.of_node;
3468 struct msm_otg_platform_data *pdata;
3469 int len = 0;
3470
3471 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3472 if (!pdata) {
3473 pr_err("unable to allocate platform data\n");
3474 return NULL;
3475 }
3476 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3477 if (len) {
3478 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3479 if (!pdata->phy_init_seq)
3480 return NULL;
3481 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3482 pdata->phy_init_seq,
3483 len/sizeof(*pdata->phy_init_seq));
3484 }
3485 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3486 &pdata->power_budget);
3487 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3488 &pdata->mode);
3489 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3490 &pdata->otg_control);
3491 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3492 &pdata->default_mode);
3493 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3494 &pdata->phy_type);
3495 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3496 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303497 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3498 "qcom,hsusb-otg-disable-reset");
3499
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303500 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303501}
3502
3503static int __init msm_otg_probe(struct platform_device *pdev)
3504{
3505 int ret = 0;
3506 struct resource *res;
3507 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003508 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303509 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303510
3511 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303512
3513 if (pdev->dev.of_node) {
3514 dev_dbg(&pdev->dev, "device tree enabled\n");
3515 pdata = msm_otg_dt_to_pdata(pdev);
3516 if (!pdata)
3517 return -ENOMEM;
3518 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3519 if (ret) {
3520 dev_err(&pdev->dev, "devices setup failed\n");
3521 return ret;
3522 }
3523 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303524 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3525 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303526 } else {
3527 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303528 }
3529
3530 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3531 if (!motg) {
3532 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3533 return -ENOMEM;
3534 }
3535
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003536 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3537 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003538 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3539 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303540 goto free_motg;
3541 }
3542
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003543 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303544 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003545 phy = &motg->phy;
3546 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303547
3548 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303549 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3550 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3551 * PMIC can detect ACA ID_GND and generate an interrupt.
3552 */
3553 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3554 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3555 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003556 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303557 }
3558
Ofir Cohen4da266f2012-01-03 10:19:29 +02003559 /* initialize reset counter */
3560 motg->reset_counter = 0;
3561
Amit Blay02eff132011-09-21 16:46:24 +03003562 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003563 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003564 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003565 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303566
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303567 /*
3568 * Targets on which link uses asynchronous reset methodology,
3569 * free running clock is not required during the reset.
3570 */
Manu Gautam5143b252012-01-05 19:25:23 -08003571 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303572 if (IS_ERR(motg->clk))
3573 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3574 else
3575 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303576
3577 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003578 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303579 * CORE CLK must be running at >55Mhz for correct HSUSB
3580 * operation and USB core cannot tolerate frequency changes on
3581 * CORE CLK. For such USB cores, vote for maximum clk frequency
3582 * on pclk source
3583 */
Manu Gautam5143b252012-01-05 19:25:23 -08003584 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3585 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303586 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003587 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303588 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003589 goto put_clk;
3590 }
3591 clk_set_rate(motg->core_clk, INT_MAX);
3592
3593 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3594 if (IS_ERR(motg->pclk)) {
3595 dev_err(&pdev->dev, "failed to get iface_clk\n");
3596 ret = PTR_ERR(motg->pclk);
3597 goto put_core_clk;
3598 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303599
3600 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3601 if (!res) {
3602 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3603 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003604 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303605 }
3606
3607 motg->regs = ioremap(res->start, resource_size(res));
3608 if (!motg->regs) {
3609 dev_err(&pdev->dev, "ioremap failed\n");
3610 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003611 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303612 }
3613 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3614
3615 motg->irq = platform_get_irq(pdev, 0);
3616 if (!motg->irq) {
3617 dev_err(&pdev->dev, "platform_get_irq failed\n");
3618 ret = -ENODEV;
3619 goto free_regs;
3620 }
3621
Manu Gautamf8c45642012-08-10 10:20:56 -07003622 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3623 if (motg->async_irq < 0) {
3624 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3625 motg->async_irq = 0;
3626 }
3627
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003628 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003629 if (IS_ERR(motg->xo_handle)) {
3630 dev_err(&pdev->dev, "%s not able to get the handle "
3631 "to vote for TCXO D0 buffer\n", __func__);
3632 ret = PTR_ERR(motg->xo_handle);
3633 goto free_regs;
3634 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303635
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003636 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003637 if (ret) {
3638 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3639 "D0 buffer%d\n", __func__, ret);
3640 goto free_xo_handle;
3641 }
3642
Manu Gautam28b1bac2012-01-30 16:43:06 +05303643 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303644
Mayank Rana248698c2012-04-19 00:03:16 +05303645 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003646 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303647 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003648 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303649 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003650 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003651 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303652 goto devote_xo_handle;
3653 }
3654 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303655 }
3656
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003657 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303658 if (ret) {
3659 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303660 goto devote_xo_handle;
3661 }
3662
3663 ret = regulator_enable(hsusb_vddcx);
3664 if (ret) {
3665 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3666 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303667 }
3668
3669 ret = msm_hsusb_ldo_init(motg, 1);
3670 if (ret) {
3671 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303672 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303673 }
3674
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303675 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303676 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3677 "mhl_usb_hs_switch");
3678 if (IS_ERR(mhl_usb_hs_switch)) {
3679 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003680 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303681 goto free_ldo_init;
3682 }
3683 }
3684
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003685 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303686 if (ret) {
3687 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003688 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303689 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303690 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303691
3692 writel(0, USB_USBINTR);
3693 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003694 /* Ensure that above STOREs are completed before enabling interrupts */
3695 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303696
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303697 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3698 if (ret)
3699 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003700 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303701 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303702 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303703 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303704 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
kibum.leeec040442012-08-22 22:29:46 +09003705 INIT_DELAYED_WORK(&motg->check_ta_work, msm_ta_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303706 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3707 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303708 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3709 "msm_otg", motg);
3710 if (ret) {
3711 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003712 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303713 }
3714
Manu Gautamf8c45642012-08-10 10:20:56 -07003715 if (motg->async_irq) {
3716 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3717 "msm_otg", motg);
3718 if (ret) {
3719 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3720 goto free_irq;
3721 }
3722 disable_irq(motg->async_irq);
3723 }
3724
Jack Pham87f202f2012-08-06 00:24:22 -07003725 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3726 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3727
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003728 phy->init = msm_otg_reset;
3729 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003730 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303731
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003732 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303733
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003734 phy->otg->phy = &motg->phy;
3735 phy->otg->set_host = msm_otg_set_host;
3736 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003737 phy->otg->start_hnp = msm_otg_start_hnp;
3738 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003739
3740 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303741 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003742 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003743 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303744 }
3745
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303746 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303747 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003748 if (motg->pdata->pmic_id_irq) {
3749 ret = request_irq(motg->pdata->pmic_id_irq,
3750 msm_pmic_id_irq,
3751 IRQF_TRIGGER_RISING |
3752 IRQF_TRIGGER_FALLING,
3753 "msm_otg", motg);
3754 if (ret) {
3755 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003756 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003757 }
3758 } else {
3759 ret = -ENODEV;
3760 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003761 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003762 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303763 }
3764
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303765 msm_hsusb_mhl_switch_enable(motg, 1);
3766
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303767 platform_set_drvdata(pdev, motg);
3768 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003769 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303770
Anji jonnalad270e2d2011-08-09 11:28:32 +05303771 ret = msm_otg_debugfs_init(motg);
3772 if (ret)
3773 dev_dbg(&pdev->dev, "mode debugfs file is"
3774 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303775
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003776 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3777 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3778
Amit Blay58b31472011-11-18 09:39:39 +02003779 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3780 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303781 (!(motg->pdata->mode == USB_OTG) ||
3782 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003783 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303784 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003785
Amit Blay58b31472011-11-18 09:39:39 +02003786 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3787 motg->caps = ALLOW_PHY_RETENTION;
3788 }
3789
Amit Blay6fa647a2012-05-24 14:12:08 +03003790 if (motg->pdata->enable_lpm_on_dev_suspend)
3791 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3792
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003793 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303794 pm_runtime_set_active(&pdev->dev);
3795 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303796
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303797 if (motg->pdata->bus_scale_table) {
3798 motg->bus_perf_client =
3799 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3800 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003801 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303802 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303803 else
3804 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303805 }
3806
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303807 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003808
Steve Mucklef132c6c2012-06-06 18:30:57 -07003809remove_phy:
3810 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003811free_async_irq:
3812 if (motg->async_irq)
3813 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303814free_irq:
3815 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003816destroy_wlock:
3817 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303818 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003819 msm_hsusb_ldo_enable(motg, 0);
3820free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303821 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303822free_hsusb_vddcx:
3823 regulator_disable(hsusb_vddcx);
3824free_config_vddcx:
3825 regulator_set_voltage(hsusb_vddcx,
3826 vdd_val[motg->vdd_type][VDD_NONE],
3827 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003828devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303829 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003830 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003831free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003832 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303833free_regs:
3834 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003835put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303836 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303837put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003838 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303839put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303840 if (!IS_ERR(motg->clk))
3841 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003842 if (!IS_ERR(motg->phy_reset_clk))
3843 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003844free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003845 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303846free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303847 kfree(motg);
3848 return ret;
3849}
3850
3851static int __devexit msm_otg_remove(struct platform_device *pdev)
3852{
3853 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003854 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303855 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303856
3857 if (otg->host || otg->gadget)
3858 return -EBUSY;
3859
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303860 if (pdev->dev.of_node)
3861 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003862 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3863 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303864 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303865 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303866 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303867 cancel_delayed_work_sync(&motg->pmic_id_status_work);
kibum.leeec040442012-08-22 22:29:46 +09003868 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303869 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303870
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303871 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303872
3873 device_init_wakeup(&pdev->dev, 0);
3874 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003875 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303876
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303877 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003878 if (motg->pdata->pmic_id_irq)
3879 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003880 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303881 free_irq(motg->irq, motg);
3882
Jack Pham87f202f2012-08-06 00:24:22 -07003883 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3884 motg->pdata->mpm_otgsessvld_int)
3885 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3886
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303887 /*
3888 * Put PHY in low power mode.
3889 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003890 ulpi_read(otg->phy, 0x14);
3891 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303892
3893 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3894 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3895 if (readl(USB_PORTSC) & PORTSC_PHCD)
3896 break;
3897 udelay(1);
3898 cnt++;
3899 }
3900 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003901 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303902
Manu Gautam28b1bac2012-01-30 16:43:06 +05303903 clk_disable_unprepare(motg->pclk);
3904 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003905 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003906 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303907 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303908 regulator_disable(hsusb_vddcx);
3909 regulator_set_voltage(hsusb_vddcx,
3910 vdd_val[motg->vdd_type][VDD_NONE],
3911 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303912
3913 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303914 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303915
Amit Blay02eff132011-09-21 16:46:24 +03003916 if (!IS_ERR(motg->phy_reset_clk))
3917 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303918 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303919 if (!IS_ERR(motg->clk))
3920 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003921 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303922
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303923 if (motg->bus_perf_client)
3924 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303925
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003926 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303927 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303928 return 0;
3929}
3930
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303931#ifdef CONFIG_PM_RUNTIME
3932static int msm_otg_runtime_idle(struct device *dev)
3933{
3934 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003935 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303936
3937 dev_dbg(dev, "OTG runtime idle\n");
3938
Steve Mucklef132c6c2012-06-06 18:30:57 -07003939 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303940 return -EAGAIN;
3941 else
3942 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303943}
3944
3945static int msm_otg_runtime_suspend(struct device *dev)
3946{
3947 struct msm_otg *motg = dev_get_drvdata(dev);
3948
3949 dev_dbg(dev, "OTG runtime suspend\n");
3950 return msm_otg_suspend(motg);
3951}
3952
3953static int msm_otg_runtime_resume(struct device *dev)
3954{
3955 struct msm_otg *motg = dev_get_drvdata(dev);
3956
3957 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303958 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303959 return msm_otg_resume(motg);
3960}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303961#endif
3962
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303963#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303964static int msm_otg_pm_suspend(struct device *dev)
3965{
Jack Pham5ca279b2012-05-14 18:42:54 -07003966 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303967 struct msm_otg *motg = dev_get_drvdata(dev);
3968
3969 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003970
3971 atomic_set(&motg->pm_suspended, 1);
3972 ret = msm_otg_suspend(motg);
3973 if (ret)
3974 atomic_set(&motg->pm_suspended, 0);
3975
3976 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303977}
3978
3979static int msm_otg_pm_resume(struct device *dev)
3980{
Jack Pham5ca279b2012-05-14 18:42:54 -07003981 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303982 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303983
3984 dev_dbg(dev, "OTG PM resume\n");
3985
Jack Pham5ca279b2012-05-14 18:42:54 -07003986 atomic_set(&motg->pm_suspended, 0);
Jack Phamb1aa4d82012-08-13 15:32:39 -07003987 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003988 pm_runtime_get_noresume(dev);
3989 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303990
Jack Pham5ca279b2012-05-14 18:42:54 -07003991 /* Update runtime PM status */
3992 pm_runtime_disable(dev);
3993 pm_runtime_set_active(dev);
3994 pm_runtime_enable(dev);
3995
Jack Phamb1aa4d82012-08-13 15:32:39 -07003996 if (motg->sm_work_pending) {
3997 motg->sm_work_pending = false;
3998 queue_work(system_nrt_wq, &motg->sm_work);
3999 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004000 }
4001
4002 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304003}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304004#endif
4005
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304006#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304007static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304008 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4009 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4010 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304011};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304012#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304013
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304014static struct of_device_id msm_otg_dt_match[] = {
4015 { .compatible = "qcom,hsusb-otg",
4016 },
4017 {}
4018};
4019
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304020static struct platform_driver msm_otg_driver = {
4021 .remove = __devexit_p(msm_otg_remove),
4022 .driver = {
4023 .name = DRIVER_NAME,
4024 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304025#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304026 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304027#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304028 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304029 },
4030};
4031
4032static int __init msm_otg_init(void)
4033{
4034 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4035}
4036
4037static void __exit msm_otg_exit(void)
4038{
4039 platform_driver_unregister(&msm_otg_driver);
4040}
4041
4042module_init(msm_otg_init);
4043module_exit(msm_otg_exit);
4044
4045MODULE_LICENSE("GPL v2");
4046MODULE_DESCRIPTION("MSM USB transceiver driver");