blob: f2ba9f11c356b13f9e78e848b3fb1086914016d8 [file] [log] [blame]
Pavankumar Kondeti39504502013-01-04 12:28:10 +05301/* Copyright (c) 2009-2013, Linux Foundation. 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;
Pavankumar Kondeti853d1982013-05-16 13:37:24 +0530765 bool host_bus_suspend, device_bus_suspend, dcp, prop_charger;
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;
Pavankumar Kondeti853d1982013-05-16 13:37:24 +0530780 prop_charger = motg->chg_type == USB_PROPRIETARY_CHARGER;
Jack Pham6ff217b2012-08-13 15:34:20 -0700781
Pavankumar Kondetib91ab712012-10-22 13:21:19 +0530782 /*
783 * Abort suspend when,
784 * 1. charging detection in progress due to cable plug-in
785 * 2. host mode activation in progress due to Micro-A cable insertion
786 */
787
788 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
Pavankumar Kondeti853d1982013-05-16 13:37:24 +0530789 !dcp && !prop_charger) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham6ff217b2012-08-13 15:34:20 -0700790 enable_irq(motg->irq);
791 return -EBUSY;
792 }
793
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530794 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530795 * Chipidea 45-nm PHY suspend sequence:
796 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530797 * Interrupt Latch Register auto-clear feature is not present
798 * in all PHY versions. Latch register is clear on read type.
799 * Clear latch register to avoid spurious wakeup from
800 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530801 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530802 * PHY comparators are disabled when PHY enters into low power
803 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
804 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
805 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530806 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530807 * PLL is not turned off when PHY enters into low power mode (LPM).
808 * Disable PLL for maximum power savings.
809 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530810
811 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200812 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530813 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200814 ulpi_write(phy, 0x01, 0x30);
815 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530816 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530817
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530819 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530820 * PHY may take some time or even fail to enter into low power
821 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
822 * in failure case.
823 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530824 portsc = readl_relaxed(USB_PORTSC);
825 if (!(portsc & PORTSC_PHCD)) {
826 writel_relaxed(portsc | PORTSC_PHCD,
827 USB_PORTSC);
828 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
829 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
830 break;
831 udelay(1);
832 cnt++;
833 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530834 }
835
836 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200837 dev_err(phy->dev, "Unable to suspend PHY\n");
838 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530839 enable_irq(motg->irq);
840 return -ETIMEDOUT;
841 }
842
843 /*
844 * PHY has capability to generate interrupt asynchronously in low
845 * power mode (LPM). This interrupt is level triggered. So USB IRQ
846 * line must be disabled till async interrupt enable bit is cleared
847 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
848 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530849 *
850 * PHY retention mode is disallowed while entering to LPM with wall
851 * charger connected. But PHY is put into suspend mode. Hence
852 * enable asynchronous interrupt to detect charger disconnection when
853 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530854 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530855 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300856 if (host_bus_suspend || device_bus_suspend ||
Pavankumar Kondeti853d1982013-05-16 13:37:24 +0530857 (motg->pdata->otg_control == OTG_PHY_CONTROL))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530858 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
859 else
860 cmd_val |= ULPI_STP_CTRL;
861 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530862
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530863 /*
864 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
865 * PHY retention and collapse can not happen with VDP_SRC enabled.
866 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300867 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
868 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200869 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
870 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
871 /* Enable PHY HV interrupts to wake MPM/Link */
872 phy_ctrl_val |=
873 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530874
Amit Blay58b31472011-11-18 09:39:39 +0200875 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530877 }
878
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 /* Ensure that above operation is completed before turning off clocks */
880 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300881 /* Consider clocks on workaround flag only in case of bus suspend */
882 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
883 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
884 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300885 clk_disable_unprepare(motg->pclk);
886 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300887 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300888 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530889
Anji jonnala7da3f262011-12-02 17:22:14 -0800890 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530891 if (!host_bus_suspend) {
892 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
893 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700894 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530895 "TCXO D0 buffer%d\n", __func__, ret);
896 else
897 motg->lpm_flags |= XO_SHUTDOWN;
898 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800899
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530900 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
901 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 msm_hsusb_ldo_enable(motg, 0);
903 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530904 }
905
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530906 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700907 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530908 msm_hsusb_mhl_switch_enable(motg, 0);
909 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910
Steve Mucklef132c6c2012-06-06 18:30:57 -0700911 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530912 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700913 if (motg->async_irq)
914 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700915 if (motg->pdata->pmic_id_irq)
916 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700917 if (pdata->otg_control == OTG_PHY_CONTROL &&
918 pdata->mpm_otgsessvld_int)
919 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530921 if (bus)
922 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
923
924 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -0700925 /* Enable ASYNC IRQ (if present) during LPM */
926 if (motg->async_irq)
927 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530928 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700929 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530930
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200931 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530932
933 return 0;
934}
935
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530936static int msm_otg_resume(struct msm_otg *motg)
937{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200938 struct usb_phy *phy = &motg->phy;
939 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700940 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530941 int cnt = 0;
942 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200943 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800944 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530945
946 if (!atomic_read(&motg->in_lpm))
947 return 0;
948
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530950
Anji jonnala7da3f262011-12-02 17:22:14 -0800951 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530952 if (motg->lpm_flags & XO_SHUTDOWN) {
953 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
954 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700955 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530956 "TCXO D0 buffer%d\n", __func__, ret);
957 motg->lpm_flags &= ~XO_SHUTDOWN;
958 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530959
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300960 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300961 clk_prepare_enable(motg->core_clk);
962 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300963 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300964 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530965
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
967 msm_hsusb_ldo_enable(motg, 1);
968 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
969 }
970
971 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530972 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530973 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200974 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
975 phy_ctrl_val |= PHY_RETEN;
976 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
977 /* Disable PHY HV interrupts */
978 phy_ctrl_val &=
979 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
980 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530982 }
983
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530984 temp = readl(USB_USBCMD);
985 temp &= ~ASYNC_INTR_CTRL;
986 temp &= ~ULPI_STP_CTRL;
987 writel(temp, USB_USBCMD);
988
989 /*
990 * PHY comes out of low power mode (LPM) in case of wakeup
991 * from asynchronous interrupt.
992 */
993 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
994 goto skip_phy_resume;
995
996 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
997 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
998 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
999 break;
1000 udelay(1);
1001 cnt++;
1002 }
1003
1004 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1005 /*
1006 * This is a fatal error. Reset the link and
1007 * PHY. USB state can not be restored. Re-insertion
1008 * of USB cable is the only way to get USB working.
1009 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001010 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301011 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001012 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301013 }
1014
1015skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001016 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301017 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001018 if (motg->async_irq)
1019 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 if (motg->pdata->pmic_id_irq)
1021 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001022 if (pdata->otg_control == OTG_PHY_CONTROL &&
1023 pdata->mpm_otgsessvld_int)
1024 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001025 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301026 if (bus)
1027 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1028
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301029 atomic_set(&motg->in_lpm, 0);
1030
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301031 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001032 /* Match the disable_irq call from ISR */
1033 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301034 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301035 }
1036
Manu Gautamf8c45642012-08-10 10:20:56 -07001037 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1038 if (motg->async_irq)
1039 disable_irq(motg->async_irq);
1040
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001041 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301042
1043 return 0;
1044}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301045#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301046
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001047static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1048{
Pavankumar Kondeti84fec152012-11-09 20:51:36 +05301049 int ret;
1050
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001051 if (!psy)
1052 goto psy_not_supported;
1053
Pavankumar Kondeti84fec152012-11-09 20:51:36 +05301054 if (host_mode) {
1055 ret = power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1056 } else {
1057 ret = power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1058 /*
1059 * VBUS comparator is disabled by PMIC charging driver
1060 * when SYSTEM scope is selected. For ID_GND->ID_A
1061 * transition, give 50 msec delay so that PMIC charger
1062 * driver detect the VBUS and ready for accepting
1063 * charging current value from USB.
1064 */
1065 if (test_bit(ID_A, &motg->inputs))
1066 msleep(50);
1067 }
1068 return ret;
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001069
1070psy_not_supported:
1071 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1072 return -ENXIO;
1073}
1074
David Keitel081a3e22012-04-18 12:37:07 -07001075static int msm_otg_notify_chg_type(struct msm_otg *motg)
1076{
roy.parkd063ef32012-08-30 09:37:38 -07001077 int charger_type;
David Keitel081a3e22012-04-18 12:37:07 -07001078 /*
1079 * TODO
1080 * Unify OTG driver charger types and power supply charger types
1081 */
David Keitel081a3e22012-04-18 12:37:07 -07001082
1083 if (motg->chg_type == USB_SDP_CHARGER)
1084 charger_type = POWER_SUPPLY_TYPE_USB;
1085 else if (motg->chg_type == USB_CDP_CHARGER)
1086 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301087 else if (motg->chg_type == USB_DCP_CHARGER ||
1088 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001089 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1090 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1091 motg->chg_type == USB_ACA_A_CHARGER ||
1092 motg->chg_type == USB_ACA_B_CHARGER ||
1093 motg->chg_type == USB_ACA_C_CHARGER))
1094 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1095 else
Abhijeet Dharmapurikar999ee572012-08-28 19:33:49 -07001096 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001097
1098 return pm8921_set_usb_power_supply_type(charger_type);
1099}
1100
Amit Blay0f7edf72012-01-15 10:11:27 +02001101static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1102{
Amit Blay0f7edf72012-01-15 10:11:27 +02001103
Amit Blay0f7edf72012-01-15 10:11:27 +02001104 if (!psy)
1105 goto psy_not_supported;
1106
1107 if (motg->cur_power == 0 && mA > 0) {
1108 /* Enable charging */
1109 if (power_supply_set_online(psy, true))
1110 goto psy_not_supported;
1111 } else if (motg->cur_power > 0 && mA == 0) {
1112 /* Disable charging */
1113 if (power_supply_set_online(psy, false))
1114 goto psy_not_supported;
1115 return 0;
1116 }
1117 /* Set max current limit */
1118 if (power_supply_set_current_limit(psy, 1000*mA))
1119 goto psy_not_supported;
1120
1121 return 0;
1122
1123psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001124 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001125 return -ENXIO;
1126}
1127
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301128static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1129{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001130 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301131
1132 if (g && g->is_a_peripheral)
1133 return;
1134
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301135 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1136 motg->chg_type == USB_ACA_A_CHARGER ||
1137 motg->chg_type == USB_ACA_B_CHARGER ||
1138 motg->chg_type == USB_ACA_C_CHARGER) &&
1139 mA > IDEV_ACA_CHG_LIMIT)
1140 mA = IDEV_ACA_CHG_LIMIT;
1141
David Keitel081a3e22012-04-18 12:37:07 -07001142 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001143 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001144 "Failed notifying %d charger type to PMIC\n",
1145 motg->chg_type);
1146
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301147 if (motg->cur_power == mA)
1148 return;
1149
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001150 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001151
Xiaozhe Shi7c229e52013-01-22 09:46:45 -08001152 /*
1153 * Use Power Supply API if supported, otherwise fallback
1154 * to legacy pm8921 API.
1155 */
1156 if (msm_otg_notify_power_supply(motg, mA))
1157 pm8921_charger_vbus_draw(mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001158
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301159 motg->cur_power = mA;
1160}
1161
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001162static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301163{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001164 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301165
1166 /*
1167 * Gadget driver uses set_power method to notify about the
1168 * available current based on suspend/configured states.
1169 *
1170 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1171 * states when CDP/ACA is connected.
1172 */
1173 if (motg->chg_type == USB_SDP_CHARGER)
1174 msm_otg_notify_charger(motg, mA);
1175
1176 return 0;
1177}
1178
Steve Mucklef132c6c2012-06-06 18:30:57 -07001179static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301180{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001181 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301182 struct msm_otg_platform_data *pdata = motg->pdata;
1183 struct usb_hcd *hcd;
1184
1185 if (!otg->host)
1186 return;
1187
1188 hcd = bus_to_hcd(otg->host);
1189
1190 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001191 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301192
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301193 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001194 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301195 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1196
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301197 /*
1198 * Some boards have a switch cotrolled by gpio
1199 * to enable/disable internal HUB. Enable internal
1200 * HUB before kicking the host.
1201 */
1202 if (pdata->setup_gpio)
1203 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301204 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301205 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001206 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301207
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301208 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301209 /* HCD core reset all bits of PORTSC. select ULPI phy */
1210 writel_relaxed(0x80000000, USB_PORTSC);
1211
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301212 if (pdata->setup_gpio)
1213 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301214
1215 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001216 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301217 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301218 }
1219}
1220
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221static int msm_otg_usbdev_notify(struct notifier_block *self,
1222 unsigned long action, void *priv)
1223{
1224 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001225 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301226 struct usb_device *udev = priv;
1227
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301228 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1229 goto out;
1230
Steve Mucklef132c6c2012-06-06 18:30:57 -07001231 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301232 goto out;
1233 /*
1234 * Interested in devices connected directly to the root hub.
1235 * ACA dock can supply IDEV_CHG irrespective devices connected
1236 * on the accessory port.
1237 */
1238 if (!udev->parent || udev->parent->parent ||
1239 motg->chg_type == USB_ACA_DOCK_CHARGER)
1240 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001241
1242 switch (action) {
1243 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301244 if (aca_enabled())
1245 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001246 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301247 pr_debug("B_CONN set\n");
1248 set_bit(B_CONN, &motg->inputs);
1249 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001250 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301251 /*
1252 * OTG PET: A-device must end session within
1253 * 10 sec after PET enumeration.
1254 */
1255 if (udev->quirks & USB_QUIRK_OTG_PET)
1256 msm_otg_start_timer(motg, TA_TST_MAINT,
1257 A_TST_MAINT);
1258 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301259 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001261 if (udev->actconfig)
1262 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1263 else
1264 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001265 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301266 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301267 break;
1268 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001269 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1270 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301271 pr_debug("B_CONN clear\n");
1272 clear_bit(B_CONN, &motg->inputs);
1273 /*
1274 * OTG PET: A-device must end session after
1275 * PET disconnection if it is enumerated
1276 * with bcdDevice[0] = 1. USB core sets
1277 * bus->otg_vbus_off for us. clear it here.
1278 */
1279 if (udev->bus->otg_vbus_off) {
1280 udev->bus->otg_vbus_off = 0;
1281 set_bit(A_BUS_DROP, &motg->inputs);
1282 }
1283 queue_work(system_nrt_wq, &motg->sm_work);
1284 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285 default:
1286 break;
1287 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301288 if (test_bit(ID_A, &motg->inputs))
1289 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1290 motg->mA_port);
1291out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 return NOTIFY_OK;
1293}
1294
Mayank Ranae3926882011-12-26 09:47:54 +05301295static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1296{
1297 int ret;
1298 static bool vbus_is_on;
1299
1300 if (vbus_is_on == on)
1301 return;
1302
1303 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301304 ret = motg->pdata->vbus_power(on);
1305 if (!ret)
1306 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301307 return;
1308 }
1309
1310 if (!vbus_otg) {
1311 pr_err("vbus_otg is NULL.");
1312 return;
1313 }
1314
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001315 /*
1316 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001317 * from usb before turning on the boost.
1318 * if exiting host mode disable the boost before enabling to draw
1319 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001320 */
Mayank Ranae3926882011-12-26 09:47:54 +05301321 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001322 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301323 ret = regulator_enable(vbus_otg);
1324 if (ret) {
1325 pr_err("unable to enable vbus_otg\n");
1326 return;
1327 }
1328 vbus_is_on = true;
1329 } else {
1330 ret = regulator_disable(vbus_otg);
1331 if (ret) {
1332 pr_err("unable to disable vbus_otg\n");
1333 return;
1334 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001335 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301336 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301337 }
1338}
1339
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001340static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301341{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001342 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301343 struct usb_hcd *hcd;
1344
1345 /*
1346 * Fail host registration if this board can support
1347 * only peripheral configuration.
1348 */
1349 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001350 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301351 return -ENODEV;
1352 }
1353
Devin Kimd10f03c2012-06-25 17:05:13 -07001354 if (!machine_is_apq8064_mako()) {
1355 if (!motg->pdata->vbus_power && host) {
1356 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1357 if (IS_ERR(vbus_otg)) {
1358 pr_err("Unable to get vbus_otg\n");
1359 return -ENODEV;
1360 }
Mayank Ranae3926882011-12-26 09:47:54 +05301361 }
1362 }
1363
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301364 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001365 if (otg->phy->state == OTG_STATE_A_HOST) {
1366 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001367 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301368 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301369 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301370 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001371 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301372 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301373 } else {
1374 otg->host = NULL;
1375 }
1376
1377 return 0;
1378 }
1379
1380 hcd = bus_to_hcd(host);
1381 hcd->power_budget = motg->pdata->power_budget;
1382
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301383#ifdef CONFIG_USB_OTG
1384 host->otg_port = 1;
1385#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1387 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301388 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001389 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301390
1391 /*
1392 * Kick the state machine work, if peripheral is not supported
1393 * or peripheral is already registered with us.
1394 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301395 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001396 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301397 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301398 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301399
1400 return 0;
1401}
1402
Steve Mucklef132c6c2012-06-06 18:30:57 -07001403static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301404{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301405 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001406 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301407 struct msm_otg_platform_data *pdata = motg->pdata;
1408
1409 if (!otg->gadget)
1410 return;
1411
1412 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001413 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301414 /*
1415 * Some boards have a switch cotrolled by gpio
1416 * to enable/disable internal HUB. Disable internal
1417 * HUB before kicking the gadget.
1418 */
1419 if (pdata->setup_gpio)
1420 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001421
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301422 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301423 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301424 ret = msm_bus_scale_client_update_request(
1425 motg->bus_perf_client, 1);
1426 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001427 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301428 "bus bandwidth %d\n", __func__, ret);
1429 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301430 usb_gadget_vbus_connect(otg->gadget);
1431 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001432 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301433 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301434 /* Configure BUS performance parameters to default */
1435 if (motg->bus_perf_client) {
1436 ret = msm_bus_scale_client_update_request(
1437 motg->bus_perf_client, 0);
1438 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001439 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301440 "for bus bw %d\n", __func__, ret);
1441 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301442 if (pdata->setup_gpio)
1443 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1444 }
1445
1446}
1447
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001448static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301449 struct usb_gadget *gadget)
1450{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001451 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301452
1453 /*
1454 * Fail peripheral registration if this board can support
1455 * only host configuration.
1456 */
1457 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001458 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301459 return -ENODEV;
1460 }
1461
1462 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001463 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1464 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301465 msm_otg_start_peripheral(otg, 0);
1466 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001467 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301468 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301469 } else {
1470 otg->gadget = NULL;
1471 }
1472
1473 return 0;
1474 }
1475 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001476 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301477
1478 /*
1479 * Kick the state machine work, if host is not supported
1480 * or host is already registered with us.
1481 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301482 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001483 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301484 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301485 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301486
1487 return 0;
1488}
1489
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301490static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1491{
1492 unsigned long flags;
1493 int id;
1494
1495 if (!motg->pdata->pmic_id_irq)
1496 return -ENODEV;
1497
1498 local_irq_save(flags);
1499 id = irq_read_line(motg->pdata->pmic_id_irq);
1500 local_irq_restore(flags);
1501
1502 /*
1503 * If we can not read ID line state for some reason, treat
1504 * it as float. This would prevent MHL discovery and kicking
1505 * host mode unnecessarily.
1506 */
1507 return !!id;
1508}
1509
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301510static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1511 void (*callback)(int on))
1512{
1513 struct usb_phy *phy = &motg->phy;
1514 int ret;
1515
1516 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1517 !motg->pdata->pmic_id_irq) {
1518 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1519 return -ENODEV;
1520 }
1521
1522 if (!motg->pdata->mhl_dev_name) {
1523 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1524 return -ENODEV;
1525 }
1526
1527 if (callback)
1528 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1529 callback);
1530 else
1531 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1532
1533 if (ret)
1534 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1535 motg->pdata->mhl_dev_name, ret);
1536 else
1537 motg->mhl_enabled = true;
1538
1539 return ret;
1540}
1541
1542static void msm_otg_mhl_notify_online(int on)
1543{
1544 struct msm_otg *motg = the_msm_otg;
1545 struct usb_phy *phy = &motg->phy;
1546 bool queue = false;
1547
1548 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1549
1550 if (on) {
1551 set_bit(MHL, &motg->inputs);
1552 } else {
1553 clear_bit(MHL, &motg->inputs);
1554 queue = true;
1555 }
1556
1557 if (queue && phy->state != OTG_STATE_UNDEFINED)
1558 schedule_work(&motg->sm_work);
1559}
1560
1561static bool msm_otg_is_mhl(struct msm_otg *motg)
1562{
1563 struct usb_phy *phy = &motg->phy;
1564 int is_mhl, ret;
1565
1566 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1567 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1568 /*
1569 * MHL driver calls our callback saying that MHL connected
1570 * if RID_GND is detected. But at later part of discovery
1571 * it may figure out MHL is not connected and returns
1572 * false. Hence clear MHL input here.
1573 */
1574 clear_bit(MHL, &motg->inputs);
1575 dev_dbg(phy->dev, "MHL device not found\n");
1576 return false;
1577 }
1578
1579 set_bit(MHL, &motg->inputs);
1580 dev_dbg(phy->dev, "MHL device found\n");
1581 return true;
1582}
1583
1584static bool msm_chg_mhl_detect(struct msm_otg *motg)
1585{
1586 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301587
1588 if (!motg->mhl_enabled)
1589 return false;
1590
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301591 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301592
1593 if (id)
1594 return false;
1595
1596 mhl_det_in_progress = true;
1597 ret = msm_otg_is_mhl(motg);
1598 mhl_det_in_progress = false;
1599
1600 return ret;
1601}
1602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001603static bool msm_chg_aca_detect(struct msm_otg *motg)
1604{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001605 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001606 u32 int_sts;
1607 bool ret = false;
1608
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301609 if (!aca_enabled())
1610 goto out;
1611
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001612 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1613 goto out;
1614
Steve Mucklef132c6c2012-06-06 18:30:57 -07001615 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001616 switch (int_sts & 0x1C) {
1617 case 0x08:
1618 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001619 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620 motg->chg_type = USB_ACA_A_CHARGER;
1621 motg->chg_state = USB_CHG_STATE_DETECTED;
1622 clear_bit(ID_B, &motg->inputs);
1623 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301624 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001625 ret = true;
1626 }
1627 break;
1628 case 0x0C:
1629 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001630 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631 motg->chg_type = USB_ACA_B_CHARGER;
1632 motg->chg_state = USB_CHG_STATE_DETECTED;
1633 clear_bit(ID_A, &motg->inputs);
1634 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301635 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001636 ret = true;
1637 }
1638 break;
1639 case 0x10:
1640 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001641 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001642 motg->chg_type = USB_ACA_C_CHARGER;
1643 motg->chg_state = USB_CHG_STATE_DETECTED;
1644 clear_bit(ID_A, &motg->inputs);
1645 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301646 set_bit(ID, &motg->inputs);
1647 ret = true;
1648 }
1649 break;
1650 case 0x04:
1651 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001652 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301653 motg->chg_type = USB_INVALID_CHARGER;
1654 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1655 clear_bit(ID_A, &motg->inputs);
1656 clear_bit(ID_B, &motg->inputs);
1657 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001658 ret = true;
1659 }
1660 break;
1661 default:
1662 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1663 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301664 test_and_clear_bit(ID_C, &motg->inputs) |
1665 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001666 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001667 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668 motg->chg_type = USB_INVALID_CHARGER;
1669 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1670 }
1671 }
1672out:
1673 return ret;
1674}
1675
1676static void msm_chg_enable_aca_det(struct msm_otg *motg)
1677{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001678 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001679
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301680 if (!aca_enabled())
1681 return;
1682
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001683 switch (motg->pdata->phy_type) {
1684 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301685 /* Disable ID_GND in link and PHY */
1686 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1687 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001688 ulpi_write(phy, 0x01, 0x0C);
1689 ulpi_write(phy, 0x10, 0x0F);
1690 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301691 /* Disable PMIC ID pull-up */
1692 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301693 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001694 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301695 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696 break;
1697 default:
1698 break;
1699 }
1700}
1701
1702static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1703{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001704 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001705
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301706 if (!aca_enabled())
1707 return;
1708
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001709 switch (motg->pdata->phy_type) {
1710 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301711 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001712 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301713 break;
1714 default:
1715 break;
1716 }
1717}
1718
1719static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1720{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001721 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301722
1723 if (!aca_enabled())
1724 return;
1725
1726 switch (motg->pdata->phy_type) {
1727 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001728 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001729 break;
1730 default:
1731 break;
1732 }
1733}
1734
1735static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1736{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001737 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001738 bool ret = false;
1739
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301740 if (!aca_enabled())
1741 return ret;
1742
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001743 switch (motg->pdata->phy_type) {
1744 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001745 if (ulpi_read(phy, 0x91) & 1) {
1746 dev_dbg(phy->dev, "RID change\n");
1747 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001748 ret = msm_chg_aca_detect(motg);
1749 }
1750 default:
1751 break;
1752 }
1753 return ret;
1754}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301755
1756static void msm_otg_id_timer_func(unsigned long data)
1757{
1758 struct msm_otg *motg = (struct msm_otg *) data;
1759
1760 if (!aca_enabled())
1761 return;
1762
1763 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001764 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301765 return;
1766 }
1767
Steve Mucklef132c6c2012-06-06 18:30:57 -07001768 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301769 goto out;
1770
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301771 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001772 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301773 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301774 }
1775
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301776out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301777 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1778 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1779}
1780
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301781static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1782{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001783 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301784 u32 chg_det;
1785 bool ret = false;
1786
1787 switch (motg->pdata->phy_type) {
1788 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001789 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301790 ret = chg_det & (1 << 4);
1791 break;
1792 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001793 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301794 ret = chg_det & 1;
1795 break;
1796 default:
1797 break;
1798 }
1799 return ret;
1800}
1801
1802static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1803{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001804 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301805 u32 chg_det;
1806
1807 switch (motg->pdata->phy_type) {
1808 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001809 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301810 /* Turn off charger block */
1811 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001812 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301813 udelay(20);
1814 /* control chg block via ULPI */
1815 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001816 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301817 /* put it in host mode for enabling D- source */
1818 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001819 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301820 /* Turn on chg detect block */
1821 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001822 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301823 udelay(20);
1824 /* enable chg detection */
1825 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001826 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301827 break;
1828 case SNPS_28NM_INTEGRATED_PHY:
1829 /*
1830 * Configure DM as current source, DP as current sink
1831 * and enable battery charging comparators.
1832 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001833 ulpi_write(phy, 0x8, 0x85);
1834 ulpi_write(phy, 0x2, 0x85);
1835 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301836 break;
1837 default:
1838 break;
1839 }
1840}
1841
1842static bool msm_chg_check_primary_det(struct msm_otg *motg)
1843{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001844 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301845 u32 chg_det;
1846 bool ret = false;
1847
1848 switch (motg->pdata->phy_type) {
1849 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001850 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301851 ret = chg_det & (1 << 4);
1852 break;
1853 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001854 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301855 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301856 /* Turn off VDP_SRC */
1857 ulpi_write(phy, 0x3, 0x86);
1858 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301859 break;
1860 default:
1861 break;
1862 }
1863 return ret;
1864}
1865
1866static void msm_chg_enable_primary_det(struct msm_otg *motg)
1867{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001868 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301869 u32 chg_det;
1870
1871 switch (motg->pdata->phy_type) {
1872 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001873 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301874 /* enable chg detection */
1875 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001876 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301877 break;
1878 case SNPS_28NM_INTEGRATED_PHY:
1879 /*
1880 * Configure DP as current source, DM as current sink
1881 * and enable battery charging comparators.
1882 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001883 ulpi_write(phy, 0x2, 0x85);
1884 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301885 break;
1886 default:
1887 break;
1888 }
1889}
1890
1891static bool msm_chg_check_dcd(struct msm_otg *motg)
1892{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001893 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301894 u32 line_state;
1895 bool ret = false;
1896
1897 switch (motg->pdata->phy_type) {
1898 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001899 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301900 ret = !(line_state & 1);
1901 break;
1902 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001903 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301904 ret = line_state & 2;
1905 break;
1906 default:
1907 break;
1908 }
1909 return ret;
1910}
1911
1912static void msm_chg_disable_dcd(struct msm_otg *motg)
1913{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001914 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301915 u32 chg_det;
1916
1917 switch (motg->pdata->phy_type) {
1918 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001919 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301920 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001921 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301922 break;
1923 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001924 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301925 break;
1926 default:
1927 break;
1928 }
1929}
1930
1931static void msm_chg_enable_dcd(struct msm_otg *motg)
1932{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001933 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301934 u32 chg_det;
1935
1936 switch (motg->pdata->phy_type) {
1937 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001938 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301939 /* Turn on D+ current source */
1940 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001941 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301942 break;
1943 case SNPS_28NM_INTEGRATED_PHY:
1944 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001945 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301946 break;
1947 default:
1948 break;
1949 }
1950}
1951
1952static void msm_chg_block_on(struct msm_otg *motg)
1953{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001954 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301955 u32 func_ctrl, chg_det;
1956
1957 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001958 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301959 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1960 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001961 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301962
1963 switch (motg->pdata->phy_type) {
1964 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001965 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301966 /* control chg block via ULPI */
1967 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001968 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301969 /* Turn on chg detect block */
1970 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001971 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301972 udelay(20);
1973 break;
1974 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05301975 /* disable DP and DM pull down resistors */
1976 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301977 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001978 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301979 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001980 ulpi_write(phy, 0x1F, 0x92);
1981 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301982 udelay(100);
1983 break;
1984 default:
1985 break;
1986 }
1987}
1988
1989static void msm_chg_block_off(struct msm_otg *motg)
1990{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001991 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301992 u32 func_ctrl, chg_det;
1993
1994 switch (motg->pdata->phy_type) {
1995 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001996 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301997 /* Turn off charger block */
1998 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001999 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302000 break;
2001 case SNPS_28NM_INTEGRATED_PHY:
2002 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002003 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302004 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002005 ulpi_write(phy, 0x1F, 0x92);
2006 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302007 break;
2008 default:
2009 break;
2010 }
2011
2012 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002013 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302014 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2015 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002016 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302017}
2018
Anji jonnalad270e2d2011-08-09 11:28:32 +05302019static const char *chg_to_string(enum usb_chg_type chg_type)
2020{
2021 switch (chg_type) {
2022 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2023 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2024 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2025 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2026 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2027 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2028 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302029 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302030 default: return "INVALID_CHARGER";
2031 }
2032}
kibum.leeec040442012-08-22 22:29:46 +09002033
Devin Kimd93ff032012-09-14 15:01:31 -07002034#define MSM_CHECK_TA_DELAY (5 * HZ)
2035#define PORTSC_LS (3 << 10) /* Read - Port's Line status */
kibum.leeec040442012-08-22 22:29:46 +09002036static void msm_ta_detect_work(struct work_struct *w)
2037{
Devin Kimd93ff032012-09-14 15:01:31 -07002038 struct msm_otg *motg = container_of(w, struct msm_otg, check_ta_work.work);
kibum.leeec040442012-08-22 22:29:46 +09002039 struct usb_otg *otg = motg->phy.otg;
kibum.leeec040442012-08-22 22:29:46 +09002040
Devin Kim6b8a5d12012-10-16 23:12:18 -07002041 pr_debug("msm_ta_detect_work: ta detection work\n");
Devin Kimd93ff032012-09-14 15:01:31 -07002042
2043 /* Presence of FRame Index or FRINDEX rollover implies USB communication */
2044 if( (readl(USB_FRINDEX) != 0) || ( readl(USB_USBSTS) & (1<<3) ) ) {
2045 pr_info("msm_ta_detect_work: USB exit ta detection - frindex\n");
2046 return;
2047 }
2048
2049 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
2050 pr_info("msm_ta_detect_work: ta dectection success\n");
kibum.leeec040442012-08-22 22:29:46 +09002051 /* inform to user space that SDP is no longer detected */
2052 msm_otg_notify_charger(motg, 0);
2053 motg->chg_state = USB_CHG_STATE_DETECTED;
2054 motg->chg_type = USB_DCP_CHARGER;
2055 motg->cur_power = 0;
2056 msm_otg_start_peripheral(otg, 0);
2057 otg->phy->state = OTG_STATE_B_IDLE;
2058 schedule_work(&motg->sm_work);
2059 return;
kibum.leeec040442012-08-22 22:29:46 +09002060 }
Devin Kimd93ff032012-09-14 15:01:31 -07002061 schedule_delayed_work(&motg->check_ta_work, MSM_CHECK_TA_DELAY);
kibum.leeec040442012-08-22 22:29:46 +09002062}
Anji jonnalad270e2d2011-08-09 11:28:32 +05302063
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302064#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2065#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302066#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2067#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302068static void msm_chg_detect_work(struct work_struct *w)
2069{
2070 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002071 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302072 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302073 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302074 unsigned long delay;
2075
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002076 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302077
2078 if (test_bit(MHL, &motg->inputs)) {
2079 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2080 return;
2081 }
2082
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302083 switch (motg->chg_state) {
2084 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302085 msm_chg_block_on(motg);
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302086 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002087 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302088 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302089 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302090 delay = MSM_CHG_DCD_POLL_TIME;
2091 break;
2092 case USB_CHG_STATE_WAIT_FOR_DCD:
jh.koo78df86f2012-09-04 20:13:19 +09002093 if (slimport_is_connected()) {
2094 msm_chg_block_off(motg);
2095 motg->chg_state = USB_CHG_STATE_DETECTED;
2096 motg->chg_type = USB_SDP_CHARGER;
2097 queue_work(system_nrt_wq, &motg->sm_work);
2098 return;
2099 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302100 if (msm_chg_mhl_detect(motg)) {
2101 msm_chg_block_off(motg);
2102 motg->chg_state = USB_CHG_STATE_DETECTED;
2103 motg->chg_type = USB_INVALID_CHARGER;
2104 queue_work(system_nrt_wq, &motg->sm_work);
2105 return;
2106 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002107 is_aca = msm_chg_aca_detect(motg);
2108 if (is_aca) {
2109 /*
2110 * ID_A can be ACA dock too. continue
2111 * primary detection after DCD.
2112 */
2113 if (test_bit(ID_A, &motg->inputs)) {
2114 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2115 } else {
2116 delay = 0;
2117 break;
2118 }
2119 }
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302120 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302121 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2122 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302123 if (is_dcd || tmout) {
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302124 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302125 msm_chg_enable_primary_det(motg);
2126 delay = MSM_CHG_PRIMARY_DET_TIME;
2127 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2128 } else {
2129 delay = MSM_CHG_DCD_POLL_TIME;
2130 }
2131 break;
2132 case USB_CHG_STATE_DCD_DONE:
2133 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302134 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2135 dm_vlgc = line_state & PORTSC_LS_DM;
2136 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302137 if (test_bit(ID_A, &motg->inputs)) {
2138 motg->chg_type = USB_ACA_DOCK_CHARGER;
2139 motg->chg_state = USB_CHG_STATE_DETECTED;
2140 delay = 0;
2141 break;
2142 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302143 if (line_state) { /* DP > VLGC */
2144 motg->chg_type = USB_PROPRIETARY_CHARGER;
2145 motg->chg_state = USB_CHG_STATE_DETECTED;
2146 delay = 0;
2147 } else {
2148 msm_chg_enable_secondary_det(motg);
2149 delay = MSM_CHG_SECONDARY_DET_TIME;
2150 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2151 }
2152 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302153 if (test_bit(ID_A, &motg->inputs)) {
2154 motg->chg_type = USB_ACA_A_CHARGER;
2155 motg->chg_state = USB_CHG_STATE_DETECTED;
2156 delay = 0;
2157 break;
2158 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302159
2160 if (line_state) /* DP > VLGC or/and DM > VLGC */
2161 motg->chg_type = USB_PROPRIETARY_CHARGER;
2162 else
2163 motg->chg_type = USB_SDP_CHARGER;
2164
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302165 motg->chg_state = USB_CHG_STATE_DETECTED;
2166 delay = 0;
2167 }
2168 break;
2169 case USB_CHG_STATE_PRIMARY_DONE:
2170 vout = msm_chg_check_secondary_det(motg);
2171 if (vout)
2172 motg->chg_type = USB_DCP_CHARGER;
2173 else
2174 motg->chg_type = USB_CDP_CHARGER;
2175 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2176 /* fall through */
2177 case USB_CHG_STATE_SECONDARY_DONE:
2178 motg->chg_state = USB_CHG_STATE_DETECTED;
2179 case USB_CHG_STATE_DETECTED:
2180 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302182 /*
2183 * Spurious interrupt is seen after enabling ACA detection
2184 * due to which charger detection fails in case of PET.
2185 * Add delay of 100 microsec to avoid that.
2186 */
2187 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002188 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002189 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302190 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302191 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302192 return;
2193 default:
2194 return;
2195 }
2196
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302197 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302198}
2199
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302200/*
2201 * We support OTG, Peripheral only and Host only configurations. In case
2202 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2203 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2204 * enabled when switch is controlled by user and default mode is supplied
2205 * by board file, which can be changed by userspace later.
2206 */
2207static void msm_otg_init_sm(struct msm_otg *motg)
2208{
2209 struct msm_otg_platform_data *pdata = motg->pdata;
2210 u32 otgsc = readl(USB_OTGSC);
2211
2212 switch (pdata->mode) {
2213 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002214 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302215 if (pdata->default_mode == USB_HOST) {
2216 clear_bit(ID, &motg->inputs);
2217 } else if (pdata->default_mode == USB_PERIPHERAL) {
2218 set_bit(ID, &motg->inputs);
2219 set_bit(B_SESS_VLD, &motg->inputs);
2220 } else {
2221 set_bit(ID, &motg->inputs);
2222 clear_bit(B_SESS_VLD, &motg->inputs);
2223 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302224 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302225 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302226 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302227 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302228 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302229 set_bit(A_BUS_REQ, &motg->inputs);
2230 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002231 if (otgsc & OTGSC_BSV)
2232 set_bit(B_SESS_VLD, &motg->inputs);
2233 else
2234 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302235 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302236 if (pdata->pmic_id_irq) {
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302237 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302238 set_bit(ID, &motg->inputs);
2239 else
2240 clear_bit(ID, &motg->inputs);
2241 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302242 /*
2243 * VBUS initial state is reported after PMIC
2244 * driver initialization. Wait for it.
2245 */
2246 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302247 }
2248 break;
2249 case USB_HOST:
2250 clear_bit(ID, &motg->inputs);
2251 break;
2252 case USB_PERIPHERAL:
2253 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302254 if (pdata->otg_control == OTG_PHY_CONTROL) {
2255 if (otgsc & OTGSC_BSV)
2256 set_bit(B_SESS_VLD, &motg->inputs);
2257 else
2258 clear_bit(B_SESS_VLD, &motg->inputs);
2259 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2260 /*
2261 * VBUS initial state is reported after PMIC
2262 * driver initialization. Wait for it.
2263 */
2264 wait_for_completion(&pmic_vbus_init);
2265 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302266 break;
2267 default:
2268 break;
2269 }
2270}
2271
2272static void msm_otg_sm_work(struct work_struct *w)
2273{
2274 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002275 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302276 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302277
Steve Mucklef132c6c2012-06-06 18:30:57 -07002278 pm_runtime_resume(otg->phy->dev);
2279 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002280 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302281 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002282 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302283 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302284 psy = power_supply_get_by_name("usb");
2285 if (!psy)
2286 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002287 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302288 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2289 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002290 pm_runtime_put_noidle(otg->phy->dev);
2291 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302292 break;
2293 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302294 /* FALL THROUGH */
2295 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302296 if (test_bit(MHL, &motg->inputs)) {
2297 /* allow LPM */
2298 pm_runtime_put_noidle(otg->phy->dev);
2299 pm_runtime_suspend(otg->phy->dev);
2300 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002301 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302302 pr_debug("!id || id_A\n");
jh.koo78df86f2012-09-04 20:13:19 +09002303 if (slimport_is_connected()) {
2304 work = 1;
2305 break;
2306 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302307 if (msm_chg_mhl_detect(motg)) {
2308 work = 1;
2309 break;
2310 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302311 clear_bit(B_BUS_REQ, &motg->inputs);
2312 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002313 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302314 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302315 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302316 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302317 switch (motg->chg_state) {
2318 case USB_CHG_STATE_UNDEFINED:
2319 msm_chg_detect_work(&motg->chg_work.work);
2320 break;
2321 case USB_CHG_STATE_DETECTED:
2322 switch (motg->chg_type) {
2323 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302324 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002325 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302326 /* fall through */
2327 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302328 msm_otg_notify_charger(motg,
2329 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002330 pm_runtime_put_noidle(otg->phy->dev);
2331 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302332 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302333 case USB_ACA_B_CHARGER:
2334 msm_otg_notify_charger(motg,
2335 IDEV_ACA_CHG_MAX);
2336 /*
2337 * (ID_B --> ID_C) PHY_ALT interrupt can
2338 * not be detected in LPM.
2339 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302340 break;
2341 case USB_CDP_CHARGER:
2342 msm_otg_notify_charger(motg,
2343 IDEV_CHG_MAX);
2344 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002345 otg->phy->state =
2346 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302347 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302348 case USB_ACA_C_CHARGER:
2349 msm_otg_notify_charger(motg,
2350 IDEV_ACA_CHG_MAX);
2351 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002352 otg->phy->state =
2353 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302354 break;
2355 case USB_SDP_CHARGER:
jh.koo78df86f2012-09-04 20:13:19 +09002356 if(!slimport_is_connected()) {
2357 msm_otg_start_peripheral(otg, 1);
2358 otg->phy->state =
2359 OTG_STATE_B_PERIPHERAL;
2360 }
kibum.leeec040442012-08-22 22:29:46 +09002361 schedule_delayed_work(&motg->check_ta_work,
2362 MSM_CHECK_TA_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302363 break;
2364 default:
2365 break;
2366 }
2367 break;
2368 default:
2369 break;
2370 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302371 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2372 pr_debug("b_sess_end && b_bus_req\n");
2373 if (msm_otg_start_srp(otg) < 0) {
2374 clear_bit(B_BUS_REQ, &motg->inputs);
2375 work = 1;
2376 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302377 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002378 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302379 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2380 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302381 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302382 pr_debug("chg_work cancel");
Mayank Rana9062a672013-01-23 19:26:21 +05302383 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302384 cancel_delayed_work_sync(&motg->chg_work);
kibum.leeec040442012-08-22 22:29:46 +09002385 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302386 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2387 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302388 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002389 msm_otg_reset(otg->phy);
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302390 /*
2391 * There is a small window where ID interrupt
2392 * is not monitored during ID detection circuit
2393 * switch from ACA to PMIC. Check ID state
2394 * before entering into low power mode.
2395 */
2396 if (!msm_otg_read_pmic_id_state(motg)) {
2397 pr_debug("process missed ID intr\n");
2398 clear_bit(ID, &motg->inputs);
2399 work = 1;
2400 break;
2401 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002402 pm_runtime_put_noidle(otg->phy->dev);
2403 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302404 }
2405 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302406 case OTG_STATE_B_SRP_INIT:
2407 if (!test_bit(ID, &motg->inputs) ||
2408 test_bit(ID_A, &motg->inputs) ||
2409 test_bit(ID_C, &motg->inputs) ||
2410 (test_bit(B_SESS_VLD, &motg->inputs) &&
2411 !test_bit(ID_B, &motg->inputs))) {
2412 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2413 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002414 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302415 /*
2416 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2417 * bits after SRP initiation.
2418 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002419 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302420 work = 1;
2421 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2422 pr_debug("b_srp_fail\n");
2423 pr_info("A-device did not respond to SRP\n");
2424 clear_bit(B_BUS_REQ, &motg->inputs);
2425 clear_bit(B_SRP_FAIL, &motg->tmouts);
2426 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002427 ulpi_write(otg->phy, 0x0, 0x98);
2428 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302429 motg->b_last_se0_sess = jiffies;
2430 work = 1;
2431 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302432 break;
2433 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302434 if (!test_bit(ID, &motg->inputs) ||
2435 test_bit(ID_A, &motg->inputs) ||
2436 test_bit(ID_B, &motg->inputs) ||
2437 !test_bit(B_SESS_VLD, &motg->inputs)) {
2438 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302439 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2440 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302441 msm_otg_notify_charger(motg, 0);
2442 srp_reqd = otg->gadget->otg_srp_reqd;
2443 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302444 if (test_bit(ID_B, &motg->inputs))
2445 clear_bit(ID_B, &motg->inputs);
2446 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002447 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302448 motg->b_last_se0_sess = jiffies;
2449 if (srp_reqd)
2450 msm_otg_start_timer(motg,
2451 TB_TST_SRP, B_TST_SRP);
2452 else
2453 work = 1;
2454 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2455 otg->gadget->b_hnp_enable &&
2456 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2457 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2458 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2459 /* D+ pullup should not be disconnected within 4msec
2460 * after A device suspends the bus. Otherwise PET will
2461 * fail the compliance test.
2462 */
2463 udelay(1000);
2464 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002465 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302466 /*
2467 * start HCD even before A-device enable
2468 * pull-up to meet HNP timings.
2469 */
2470 otg->host->is_b_host = 1;
2471 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002472 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2473 test_bit(B_SESS_VLD, &motg->inputs)) {
2474 pr_debug("a_bus_suspend && b_sess_vld\n");
2475 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002476 pm_runtime_put_noidle(otg->phy->dev);
2477 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002478 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002479 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302480 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481 }
2482 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302483 case OTG_STATE_B_WAIT_ACON:
2484 if (!test_bit(ID, &motg->inputs) ||
2485 test_bit(ID_A, &motg->inputs) ||
2486 test_bit(ID_B, &motg->inputs) ||
2487 !test_bit(B_SESS_VLD, &motg->inputs)) {
2488 pr_debug("!id || id_a/b || !b_sess_vld\n");
2489 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302490 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302491 * A-device is physically disconnected during
2492 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302493 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302494 msm_otg_start_host(otg, 0);
2495 otg->host->is_b_host = 0;
2496
2497 clear_bit(B_BUS_REQ, &motg->inputs);
2498 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2499 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002500 otg->phy->state = OTG_STATE_B_IDLE;
2501 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302502 work = 1;
2503 } else if (test_bit(A_CONN, &motg->inputs)) {
2504 pr_debug("a_conn\n");
2505 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002506 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302507 /*
2508 * PET disconnects D+ pullup after reset is generated
2509 * by B device in B_HOST role which is not detected by
2510 * B device. As workaorund , start timer of 300msec
2511 * and stop timer if A device is enumerated else clear
2512 * A_CONN.
2513 */
2514 msm_otg_start_timer(motg, TB_TST_CONFIG,
2515 B_TST_CONFIG);
2516 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2517 pr_debug("b_ase0_brst_tmout\n");
2518 pr_info("B HNP fail:No response from A device\n");
2519 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002520 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302521 otg->host->is_b_host = 0;
2522 clear_bit(B_ASE0_BRST, &motg->tmouts);
2523 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2524 clear_bit(B_BUS_REQ, &motg->inputs);
2525 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002526 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302527 work = 1;
2528 } else if (test_bit(ID_C, &motg->inputs)) {
2529 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2530 }
2531 break;
2532 case OTG_STATE_B_HOST:
2533 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2534 !test_bit(A_CONN, &motg->inputs) ||
2535 !test_bit(B_SESS_VLD, &motg->inputs)) {
2536 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2537 clear_bit(A_CONN, &motg->inputs);
2538 clear_bit(B_BUS_REQ, &motg->inputs);
2539 msm_otg_start_host(otg, 0);
2540 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002541 otg->phy->state = OTG_STATE_B_IDLE;
2542 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302543 work = 1;
2544 } else if (test_bit(ID_C, &motg->inputs)) {
2545 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2546 }
2547 break;
2548 case OTG_STATE_A_IDLE:
2549 otg->default_a = 1;
2550 if (test_bit(ID, &motg->inputs) &&
2551 !test_bit(ID_A, &motg->inputs)) {
2552 pr_debug("id && !id_a\n");
2553 otg->default_a = 0;
2554 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002555 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302556 del_timer_sync(&motg->id_timer);
2557 msm_otg_link_reset(motg);
2558 msm_chg_enable_aca_intr(motg);
2559 msm_otg_notify_charger(motg, 0);
2560 work = 1;
2561 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2562 (test_bit(A_SRP_DET, &motg->inputs) ||
2563 test_bit(A_BUS_REQ, &motg->inputs))) {
2564 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2565
2566 clear_bit(A_SRP_DET, &motg->inputs);
2567 /* Disable SRP detection */
2568 writel_relaxed((readl_relaxed(USB_OTGSC) &
2569 ~OTGSC_INTSTS_MASK) &
2570 ~OTGSC_DPIE, USB_OTGSC);
2571
Steve Mucklef132c6c2012-06-06 18:30:57 -07002572 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302573 /* VBUS should not be supplied before end of SRP pulse
2574 * generated by PET, if not complaince test fail.
2575 */
2576 usleep_range(10000, 12000);
2577 /* ACA: ID_A: Stop charging untill enumeration */
2578 if (test_bit(ID_A, &motg->inputs))
2579 msm_otg_notify_charger(motg, 0);
2580 else
2581 msm_hsusb_vbus_power(motg, 1);
2582 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2583 } else {
2584 pr_debug("No session requested\n");
2585 clear_bit(A_BUS_DROP, &motg->inputs);
2586 if (test_bit(ID_A, &motg->inputs)) {
2587 msm_otg_notify_charger(motg,
2588 IDEV_ACA_CHG_MAX);
2589 } else if (!test_bit(ID, &motg->inputs)) {
2590 msm_otg_notify_charger(motg, 0);
2591 /*
2592 * A-device is not providing power on VBUS.
2593 * Enable SRP detection.
2594 */
2595 writel_relaxed(0x13, USB_USBMODE);
2596 writel_relaxed((readl_relaxed(USB_OTGSC) &
2597 ~OTGSC_INTSTS_MASK) |
2598 OTGSC_DPIE, USB_OTGSC);
2599 mb();
2600 }
2601 }
2602 break;
2603 case OTG_STATE_A_WAIT_VRISE:
2604 if ((test_bit(ID, &motg->inputs) &&
2605 !test_bit(ID_A, &motg->inputs)) ||
2606 test_bit(A_BUS_DROP, &motg->inputs) ||
2607 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2608 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2609 clear_bit(A_BUS_REQ, &motg->inputs);
2610 msm_otg_del_timer(motg);
2611 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002612 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302613 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2614 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2615 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002616 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302617 if (TA_WAIT_BCON > 0)
2618 msm_otg_start_timer(motg, TA_WAIT_BCON,
2619 A_WAIT_BCON);
2620 msm_otg_start_host(otg, 1);
2621 msm_chg_enable_aca_det(motg);
2622 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302623 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302624 if (msm_chg_check_aca_intr(motg))
2625 work = 1;
2626 }
2627 break;
2628 case OTG_STATE_A_WAIT_BCON:
2629 if ((test_bit(ID, &motg->inputs) &&
2630 !test_bit(ID_A, &motg->inputs)) ||
2631 test_bit(A_BUS_DROP, &motg->inputs) ||
2632 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2633 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2634 "a_wait_bcon_tmout\n");
2635 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2636 pr_info("Device No Response\n");
2637 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2638 }
2639 msm_otg_del_timer(motg);
2640 clear_bit(A_BUS_REQ, &motg->inputs);
2641 clear_bit(B_CONN, &motg->inputs);
2642 msm_otg_start_host(otg, 0);
2643 /*
2644 * ACA: ID_A with NO accessory, just the A plug is
2645 * attached to ACA: Use IDCHG_MAX for charging
2646 */
2647 if (test_bit(ID_A, &motg->inputs))
2648 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2649 else
2650 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002651 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302652 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2653 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2654 pr_debug("!a_vbus_vld\n");
2655 clear_bit(B_CONN, &motg->inputs);
2656 msm_otg_del_timer(motg);
2657 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002658 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2659 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302660 } else if (test_bit(ID_A, &motg->inputs)) {
2661 msm_hsusb_vbus_power(motg, 0);
2662 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2663 /*
2664 * If TA_WAIT_BCON is infinite, we don;t
2665 * turn off VBUS. Enter low power mode.
2666 */
2667 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002668 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302669 } else if (!test_bit(ID, &motg->inputs)) {
2670 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302671 }
2672 break;
2673 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302674 if ((test_bit(ID, &motg->inputs) &&
2675 !test_bit(ID_A, &motg->inputs)) ||
2676 test_bit(A_BUS_DROP, &motg->inputs)) {
2677 pr_debug("id_a/b/c || a_bus_drop\n");
2678 clear_bit(B_CONN, &motg->inputs);
2679 clear_bit(A_BUS_REQ, &motg->inputs);
2680 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002681 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302682 msm_otg_start_host(otg, 0);
2683 if (!test_bit(ID_A, &motg->inputs))
2684 msm_hsusb_vbus_power(motg, 0);
2685 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2686 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2687 pr_debug("!a_vbus_vld\n");
2688 clear_bit(B_CONN, &motg->inputs);
2689 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002690 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302691 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002692 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302693 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2694 /*
2695 * a_bus_req is de-asserted when root hub is
2696 * suspended or HNP is in progress.
2697 */
2698 pr_debug("!a_bus_req\n");
2699 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002700 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302701 if (otg->host->b_hnp_enable)
2702 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2703 A_AIDL_BDIS);
2704 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002705 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302706 } else if (!test_bit(B_CONN, &motg->inputs)) {
2707 pr_debug("!b_conn\n");
2708 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002709 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302710 if (TA_WAIT_BCON > 0)
2711 msm_otg_start_timer(motg, TA_WAIT_BCON,
2712 A_WAIT_BCON);
2713 if (msm_chg_check_aca_intr(motg))
2714 work = 1;
2715 } else if (test_bit(ID_A, &motg->inputs)) {
2716 msm_otg_del_timer(motg);
2717 msm_hsusb_vbus_power(motg, 0);
2718 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2719 msm_otg_notify_charger(motg,
2720 IDEV_ACA_CHG_MAX);
2721 else
2722 msm_otg_notify_charger(motg,
2723 IDEV_CHG_MIN - motg->mA_port);
2724 } else if (!test_bit(ID, &motg->inputs)) {
2725 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2726 motg->chg_type = USB_INVALID_CHARGER;
2727 msm_otg_notify_charger(motg, 0);
2728 msm_hsusb_vbus_power(motg, 1);
2729 }
2730 break;
2731 case OTG_STATE_A_SUSPEND:
2732 if ((test_bit(ID, &motg->inputs) &&
2733 !test_bit(ID_A, &motg->inputs)) ||
2734 test_bit(A_BUS_DROP, &motg->inputs) ||
2735 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2736 pr_debug("id_a/b/c || a_bus_drop ||"
2737 "a_aidl_bdis_tmout\n");
2738 msm_otg_del_timer(motg);
2739 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002740 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302741 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002742 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302743 if (!test_bit(ID_A, &motg->inputs))
2744 msm_hsusb_vbus_power(motg, 0);
2745 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2746 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2747 pr_debug("!a_vbus_vld\n");
2748 msm_otg_del_timer(motg);
2749 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002750 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302751 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002752 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302753 } else if (!test_bit(B_CONN, &motg->inputs) &&
2754 otg->host->b_hnp_enable) {
2755 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002756 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302757 msm_otg_host_hnp_enable(otg, 1);
2758 otg->gadget->is_a_peripheral = 1;
2759 msm_otg_start_peripheral(otg, 1);
2760 } else if (!test_bit(B_CONN, &motg->inputs) &&
2761 !otg->host->b_hnp_enable) {
2762 pr_debug("!b_conn && !b_hnp_enable");
2763 /*
2764 * bus request is dropped during suspend.
2765 * acquire again for next device.
2766 */
2767 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002768 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302769 if (TA_WAIT_BCON > 0)
2770 msm_otg_start_timer(motg, TA_WAIT_BCON,
2771 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002772 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302773 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002774 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302775 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002776 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002777 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302778 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302779 }
2780 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302781 case OTG_STATE_A_PERIPHERAL:
2782 if ((test_bit(ID, &motg->inputs) &&
2783 !test_bit(ID_A, &motg->inputs)) ||
2784 test_bit(A_BUS_DROP, &motg->inputs)) {
2785 pr_debug("id _f/b/c || a_bus_drop\n");
2786 /* Clear BIDL_ADIS timer */
2787 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002788 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302789 msm_otg_start_peripheral(otg, 0);
2790 otg->gadget->is_a_peripheral = 0;
2791 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002792 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302793 if (!test_bit(ID_A, &motg->inputs))
2794 msm_hsusb_vbus_power(motg, 0);
2795 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2796 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2797 pr_debug("!a_vbus_vld\n");
2798 /* Clear BIDL_ADIS timer */
2799 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002800 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302801 msm_otg_start_peripheral(otg, 0);
2802 otg->gadget->is_a_peripheral = 0;
2803 msm_otg_start_host(otg, 0);
2804 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2805 pr_debug("a_bidl_adis_tmout\n");
2806 msm_otg_start_peripheral(otg, 0);
2807 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002808 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302809 set_bit(A_BUS_REQ, &motg->inputs);
2810 msm_otg_host_hnp_enable(otg, 0);
2811 if (TA_WAIT_BCON > 0)
2812 msm_otg_start_timer(motg, TA_WAIT_BCON,
2813 A_WAIT_BCON);
2814 } else if (test_bit(ID_A, &motg->inputs)) {
2815 msm_hsusb_vbus_power(motg, 0);
2816 msm_otg_notify_charger(motg,
2817 IDEV_CHG_MIN - motg->mA_port);
2818 } else if (!test_bit(ID, &motg->inputs)) {
2819 msm_otg_notify_charger(motg, 0);
2820 msm_hsusb_vbus_power(motg, 1);
2821 }
2822 break;
2823 case OTG_STATE_A_WAIT_VFALL:
2824 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2825 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002826 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302827 work = 1;
2828 }
2829 break;
2830 case OTG_STATE_A_VBUS_ERR:
2831 if ((test_bit(ID, &motg->inputs) &&
2832 !test_bit(ID_A, &motg->inputs)) ||
2833 test_bit(A_BUS_DROP, &motg->inputs) ||
2834 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002835 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302836 if (!test_bit(ID_A, &motg->inputs))
2837 msm_hsusb_vbus_power(motg, 0);
2838 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2839 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2840 motg->chg_type = USB_INVALID_CHARGER;
2841 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302842 }
2843 break;
2844 default:
2845 break;
2846 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302847 if (work)
2848 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302849}
2850
2851static irqreturn_t msm_otg_irq(int irq, void *data)
2852{
2853 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002854 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302855 u32 otgsc = 0, usbsts, pc;
2856 bool work = 0;
2857 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302858
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302859 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002860 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302861 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002862 motg->async_int = irq;
Jack Phamb1aa4d82012-08-13 15:32:39 -07002863 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002864 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302865 return IRQ_HANDLED;
2866 }
2867
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002868 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302869 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302870
2871 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302872 return IRQ_NONE;
2873
2874 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302875 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302876 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302877 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302878 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302879 pr_debug("Id clear\n");
2880 /*
2881 * Assert a_bus_req to supply power on
2882 * VBUS when Micro/Mini-A cable is connected
2883 * with out user intervention.
2884 */
2885 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302886 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302887 msm_chg_enable_aca_det(motg);
2888 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302889 writel_relaxed(otgsc, USB_OTGSC);
2890 work = 1;
2891 } else if (otgsc & OTGSC_DPIS) {
2892 pr_debug("DPIS detected\n");
2893 writel_relaxed(otgsc, USB_OTGSC);
2894 set_bit(A_SRP_DET, &motg->inputs);
2895 set_bit(A_BUS_REQ, &motg->inputs);
2896 work = 1;
2897 } else if (otgsc & OTGSC_BSVIS) {
2898 writel_relaxed(otgsc, USB_OTGSC);
2899 /*
2900 * BSV interrupt comes when operating as an A-device
2901 * (VBUS on/off).
2902 * But, handle BSV when charger is removed from ACA in ID_A
2903 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002904 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302905 !test_bit(ID_A, &motg->inputs))
2906 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302907 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302908 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302909 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302910 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302911 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302912 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002913 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2914
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302915 msm_chg_check_aca_intr(motg);
2916 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302917 work = 1;
2918 } else if (usbsts & STS_PCI) {
2919 pc = readl_relaxed(USB_PORTSC);
2920 pr_debug("portsc = %x\n", pc);
2921 ret = IRQ_NONE;
2922 /*
2923 * HCD Acks PCI interrupt. We use this to switch
2924 * between different OTG states.
2925 */
2926 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002927 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302928 case OTG_STATE_A_SUSPEND:
2929 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2930 !(pc & PORTSC_CCS)) {
2931 pr_debug("B_CONN clear\n");
2932 clear_bit(B_CONN, &motg->inputs);
2933 msm_otg_del_timer(motg);
2934 }
2935 break;
2936 case OTG_STATE_A_PERIPHERAL:
2937 /*
2938 * A-peripheral observed activity on bus.
2939 * clear A_BIDL_ADIS timer.
2940 */
2941 msm_otg_del_timer(motg);
2942 work = 0;
2943 break;
2944 case OTG_STATE_B_WAIT_ACON:
2945 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2946 pr_debug("A_CONN set\n");
2947 set_bit(A_CONN, &motg->inputs);
2948 /* Clear ASE0_BRST timer */
2949 msm_otg_del_timer(motg);
2950 }
2951 break;
2952 case OTG_STATE_B_HOST:
2953 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2954 pr_debug("A_CONN clear\n");
2955 clear_bit(A_CONN, &motg->inputs);
2956 msm_otg_del_timer(motg);
2957 }
2958 break;
2959 case OTG_STATE_A_WAIT_BCON:
2960 if (TA_WAIT_BCON < 0)
2961 set_bit(A_BUS_REQ, &motg->inputs);
2962 default:
2963 work = 0;
2964 break;
2965 }
2966 } else if (usbsts & STS_URI) {
2967 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002968 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302969 case OTG_STATE_A_PERIPHERAL:
2970 /*
2971 * A-peripheral observed activity on bus.
2972 * clear A_BIDL_ADIS timer.
2973 */
2974 msm_otg_del_timer(motg);
2975 work = 0;
2976 break;
2977 default:
2978 work = 0;
2979 break;
2980 }
2981 } else if (usbsts & STS_SLI) {
2982 ret = IRQ_NONE;
2983 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002984 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302985 case OTG_STATE_B_PERIPHERAL:
2986 if (otg->gadget->b_hnp_enable) {
2987 set_bit(A_BUS_SUSPEND, &motg->inputs);
2988 set_bit(B_BUS_REQ, &motg->inputs);
2989 work = 1;
2990 }
2991 break;
2992 case OTG_STATE_A_PERIPHERAL:
2993 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2994 A_BIDL_ADIS);
2995 break;
2996 default:
2997 break;
2998 }
2999 } else if ((usbsts & PHY_ALT_INT)) {
3000 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3001 if (msm_chg_check_aca_intr(motg))
3002 work = 1;
3003 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303004 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303005 if (work)
3006 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303007
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303008 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003009}
3010
3011static void msm_otg_set_vbus_state(int online)
3012{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303013 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003014 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303015 struct usb_otg *otg = motg->phy.otg;
3016
3017 /* In A Host Mode, ignore received BSV interrupts */
3018 if (otg->phy->state >= OTG_STATE_A_IDLE)
3019 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003020
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303021 if (online) {
3022 pr_debug("PMIC: BSV set\n");
3023 set_bit(B_SESS_VLD, &motg->inputs);
3024 } else {
3025 pr_debug("PMIC: BSV clear\n");
3026 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303027 }
3028
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303029 if (!init) {
3030 init = true;
3031 complete(&pmic_vbus_init);
3032 pr_debug("PMIC: BSV init complete\n");
3033 return;
3034 }
3035
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303036 if (test_bit(MHL, &motg->inputs) ||
3037 mhl_det_in_progress) {
3038 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3039 return;
3040 }
3041
Jack Pham5ca279b2012-05-14 18:42:54 -07003042 if (atomic_read(&motg->pm_suspended))
3043 motg->sm_work_pending = true;
3044 else
3045 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003046}
3047
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303048static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003049{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303050 struct msm_otg *motg = container_of(w, struct msm_otg,
3051 pmic_id_status_work.work);
3052 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003053
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05303054 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303055 if (!test_and_set_bit(ID, &motg->inputs)) {
3056 pr_debug("PMIC: ID set\n");
3057 work = 1;
3058 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303059 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303060 if (test_and_clear_bit(ID, &motg->inputs)) {
3061 pr_debug("PMIC: ID clear\n");
3062 set_bit(A_BUS_REQ, &motg->inputs);
3063 work = 1;
3064 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303065 }
3066
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303067 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003068 if (atomic_read(&motg->pm_suspended))
3069 motg->sm_work_pending = true;
3070 else
3071 queue_work(system_nrt_wq, &motg->sm_work);
3072 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303073
3074}
3075
3076#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3077static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3078{
3079 struct msm_otg *motg = data;
3080
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303081 if (test_bit(MHL, &motg->inputs) ||
3082 mhl_det_in_progress) {
3083 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3084 return IRQ_HANDLED;
3085 }
3086
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303087 if (!aca_id_turned_on)
3088 /*schedule delayed work for 5msec for ID line state to settle*/
3089 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3090 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003091
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303092 return IRQ_HANDLED;
3093}
3094
3095static int msm_otg_mode_show(struct seq_file *s, void *unused)
3096{
3097 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003098 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303099
Steve Mucklef132c6c2012-06-06 18:30:57 -07003100 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303101 case OTG_STATE_A_HOST:
3102 seq_printf(s, "host\n");
3103 break;
3104 case OTG_STATE_B_PERIPHERAL:
3105 seq_printf(s, "peripheral\n");
3106 break;
3107 default:
3108 seq_printf(s, "none\n");
3109 break;
3110 }
3111
3112 return 0;
3113}
3114
3115static int msm_otg_mode_open(struct inode *inode, struct file *file)
3116{
3117 return single_open(file, msm_otg_mode_show, inode->i_private);
3118}
3119
3120static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3121 size_t count, loff_t *ppos)
3122{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303123 struct seq_file *s = file->private_data;
3124 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303125 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003126 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303127 int status = count;
3128 enum usb_mode_type req_mode;
3129
3130 memset(buf, 0x00, sizeof(buf));
3131
3132 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3133 status = -EFAULT;
3134 goto out;
3135 }
3136
3137 if (!strncmp(buf, "host", 4)) {
3138 req_mode = USB_HOST;
3139 } else if (!strncmp(buf, "peripheral", 10)) {
3140 req_mode = USB_PERIPHERAL;
3141 } else if (!strncmp(buf, "none", 4)) {
3142 req_mode = USB_NONE;
3143 } else {
3144 status = -EINVAL;
3145 goto out;
3146 }
3147
3148 switch (req_mode) {
3149 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003150 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303151 case OTG_STATE_A_HOST:
3152 case OTG_STATE_B_PERIPHERAL:
3153 set_bit(ID, &motg->inputs);
3154 clear_bit(B_SESS_VLD, &motg->inputs);
3155 break;
3156 default:
3157 goto out;
3158 }
3159 break;
3160 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003161 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303162 case OTG_STATE_B_IDLE:
3163 case OTG_STATE_A_HOST:
3164 set_bit(ID, &motg->inputs);
3165 set_bit(B_SESS_VLD, &motg->inputs);
3166 break;
3167 default:
3168 goto out;
3169 }
3170 break;
3171 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003172 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303173 case OTG_STATE_B_IDLE:
3174 case OTG_STATE_B_PERIPHERAL:
3175 clear_bit(ID, &motg->inputs);
3176 break;
3177 default:
3178 goto out;
3179 }
3180 break;
3181 default:
3182 goto out;
3183 }
3184
Steve Mucklef132c6c2012-06-06 18:30:57 -07003185 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303186 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303187out:
3188 return status;
3189}
3190
3191const struct file_operations msm_otg_mode_fops = {
3192 .open = msm_otg_mode_open,
3193 .read = seq_read,
3194 .write = msm_otg_mode_write,
3195 .llseek = seq_lseek,
3196 .release = single_release,
3197};
3198
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303199static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3200{
3201 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003202 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303203
Steve Mucklef132c6c2012-06-06 18:30:57 -07003204 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303205 return 0;
3206}
3207
3208static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3209{
3210 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3211}
3212
3213const struct file_operations msm_otg_state_fops = {
3214 .open = msm_otg_otg_state_open,
3215 .read = seq_read,
3216 .llseek = seq_lseek,
3217 .release = single_release,
3218};
3219
Anji jonnalad270e2d2011-08-09 11:28:32 +05303220static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3221{
3222 struct msm_otg *motg = s->private;
3223
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303224 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303225 return 0;
3226}
3227
3228static int msm_otg_chg_open(struct inode *inode, struct file *file)
3229{
3230 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3231}
3232
3233const struct file_operations msm_otg_chg_fops = {
3234 .open = msm_otg_chg_open,
3235 .read = seq_read,
3236 .llseek = seq_lseek,
3237 .release = single_release,
3238};
3239
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303240static int msm_otg_aca_show(struct seq_file *s, void *unused)
3241{
3242 if (debug_aca_enabled)
3243 seq_printf(s, "enabled\n");
3244 else
3245 seq_printf(s, "disabled\n");
3246
3247 return 0;
3248}
3249
3250static int msm_otg_aca_open(struct inode *inode, struct file *file)
3251{
3252 return single_open(file, msm_otg_aca_show, inode->i_private);
3253}
3254
3255static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3256 size_t count, loff_t *ppos)
3257{
3258 char buf[8];
3259
3260 memset(buf, 0x00, sizeof(buf));
3261
3262 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3263 return -EFAULT;
3264
3265 if (!strncmp(buf, "enable", 6))
3266 debug_aca_enabled = true;
3267 else
3268 debug_aca_enabled = false;
3269
3270 return count;
3271}
3272
3273const struct file_operations msm_otg_aca_fops = {
3274 .open = msm_otg_aca_open,
3275 .read = seq_read,
3276 .write = msm_otg_aca_write,
3277 .llseek = seq_lseek,
3278 .release = single_release,
3279};
3280
Manu Gautam8bdcc592012-03-06 11:26:06 +05303281static int msm_otg_bus_show(struct seq_file *s, void *unused)
3282{
3283 if (debug_bus_voting_enabled)
3284 seq_printf(s, "enabled\n");
3285 else
3286 seq_printf(s, "disabled\n");
3287
3288 return 0;
3289}
3290
3291static int msm_otg_bus_open(struct inode *inode, struct file *file)
3292{
3293 return single_open(file, msm_otg_bus_show, inode->i_private);
3294}
3295
3296static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3297 size_t count, loff_t *ppos)
3298{
3299 char buf[8];
3300 int ret;
3301 struct seq_file *s = file->private_data;
3302 struct msm_otg *motg = s->private;
3303
3304 memset(buf, 0x00, sizeof(buf));
3305
3306 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3307 return -EFAULT;
3308
3309 if (!strncmp(buf, "enable", 6)) {
3310 /* Do not vote here. Let OTG statemachine decide when to vote */
3311 debug_bus_voting_enabled = true;
3312 } else {
3313 debug_bus_voting_enabled = false;
3314 if (motg->bus_perf_client) {
3315 ret = msm_bus_scale_client_update_request(
3316 motg->bus_perf_client, 0);
3317 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003318 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303319 "for bus bw %d\n", __func__, ret);
3320 }
3321 }
3322
3323 return count;
3324}
3325
3326const struct file_operations msm_otg_bus_fops = {
3327 .open = msm_otg_bus_open,
3328 .read = seq_read,
3329 .write = msm_otg_bus_write,
3330 .llseek = seq_lseek,
3331 .release = single_release,
3332};
3333
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303334static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303335
3336static int msm_otg_debugfs_init(struct msm_otg *motg)
3337{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303338 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303339
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303340 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3341
3342 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3343 return -ENODEV;
3344
Anji jonnalad270e2d2011-08-09 11:28:32 +05303345 if (motg->pdata->mode == USB_OTG &&
3346 motg->pdata->otg_control == OTG_USER_CONTROL) {
3347
Manu Gautam8bdcc592012-03-06 11:26:06 +05303348 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303349 S_IWUSR, msm_otg_dbg_root, motg,
3350 &msm_otg_mode_fops);
3351
Manu Gautam8bdcc592012-03-06 11:26:06 +05303352 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303353 debugfs_remove(msm_otg_dbg_root);
3354 msm_otg_dbg_root = NULL;
3355 return -ENODEV;
3356 }
3357 }
3358
Manu Gautam8bdcc592012-03-06 11:26:06 +05303359 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303360 msm_otg_dbg_root, motg,
3361 &msm_otg_chg_fops);
3362
Manu Gautam8bdcc592012-03-06 11:26:06 +05303363 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303364 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303365 return -ENODEV;
3366 }
3367
Manu Gautam8bdcc592012-03-06 11:26:06 +05303368 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303369 msm_otg_dbg_root, motg,
3370 &msm_otg_aca_fops);
3371
Manu Gautam8bdcc592012-03-06 11:26:06 +05303372 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303373 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303374 return -ENODEV;
3375 }
3376
Manu Gautam8bdcc592012-03-06 11:26:06 +05303377 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3378 msm_otg_dbg_root, motg,
3379 &msm_otg_bus_fops);
3380
3381 if (!msm_otg_dentry) {
3382 debugfs_remove_recursive(msm_otg_dbg_root);
3383 return -ENODEV;
3384 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303385
3386 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3387 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3388
3389 if (!msm_otg_dentry) {
3390 debugfs_remove_recursive(msm_otg_dbg_root);
3391 return -ENODEV;
3392 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303393 return 0;
3394}
3395
3396static void msm_otg_debugfs_cleanup(void)
3397{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303398 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303399}
3400
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303401static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3402static struct platform_device *msm_otg_add_pdev(
3403 struct platform_device *ofdev, const char *name)
3404{
3405 struct platform_device *pdev;
3406 const struct resource *res = ofdev->resource;
3407 unsigned int num = ofdev->num_resources;
3408 int retval;
3409
3410 pdev = platform_device_alloc(name, -1);
3411 if (!pdev) {
3412 retval = -ENOMEM;
3413 goto error;
3414 }
3415
3416 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3417 pdev->dev.dma_mask = &msm_otg_dma_mask;
3418
3419 if (num) {
3420 retval = platform_device_add_resources(pdev, res, num);
3421 if (retval)
3422 goto error;
3423 }
3424
3425 retval = platform_device_add(pdev);
3426 if (retval)
3427 goto error;
3428
3429 return pdev;
3430
3431error:
3432 platform_device_put(pdev);
3433 return ERR_PTR(retval);
3434}
3435
3436static int msm_otg_setup_devices(struct platform_device *ofdev,
3437 enum usb_mode_type mode, bool init)
3438{
3439 const char *gadget_name = "msm_hsusb";
3440 const char *host_name = "msm_hsusb_host";
3441 static struct platform_device *gadget_pdev;
3442 static struct platform_device *host_pdev;
3443 int retval = 0;
3444
3445 if (!init) {
3446 if (gadget_pdev)
3447 platform_device_unregister(gadget_pdev);
3448 if (host_pdev)
3449 platform_device_unregister(host_pdev);
3450 return 0;
3451 }
3452
3453 switch (mode) {
3454 case USB_OTG:
3455 /* fall through */
3456 case USB_PERIPHERAL:
3457 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3458 if (IS_ERR(gadget_pdev)) {
3459 retval = PTR_ERR(gadget_pdev);
3460 break;
3461 }
3462 if (mode == USB_PERIPHERAL)
3463 break;
3464 /* fall through */
3465 case USB_HOST:
3466 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3467 if (IS_ERR(host_pdev)) {
3468 retval = PTR_ERR(host_pdev);
3469 if (mode == USB_OTG)
3470 platform_device_unregister(gadget_pdev);
3471 }
3472 break;
3473 default:
3474 break;
3475 }
3476
3477 return retval;
3478}
3479
3480struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3481{
3482 struct device_node *node = pdev->dev.of_node;
3483 struct msm_otg_platform_data *pdata;
3484 int len = 0;
3485
3486 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3487 if (!pdata) {
3488 pr_err("unable to allocate platform data\n");
3489 return NULL;
3490 }
3491 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3492 if (len) {
3493 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3494 if (!pdata->phy_init_seq)
3495 return NULL;
3496 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3497 pdata->phy_init_seq,
3498 len/sizeof(*pdata->phy_init_seq));
3499 }
3500 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3501 &pdata->power_budget);
3502 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3503 &pdata->mode);
3504 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3505 &pdata->otg_control);
3506 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3507 &pdata->default_mode);
3508 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3509 &pdata->phy_type);
3510 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3511 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303512 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3513 "qcom,hsusb-otg-disable-reset");
3514
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303515 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303516}
3517
3518static int __init msm_otg_probe(struct platform_device *pdev)
3519{
3520 int ret = 0;
3521 struct resource *res;
3522 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003523 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303524 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303525
3526 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303527
3528 if (pdev->dev.of_node) {
3529 dev_dbg(&pdev->dev, "device tree enabled\n");
3530 pdata = msm_otg_dt_to_pdata(pdev);
3531 if (!pdata)
3532 return -ENOMEM;
3533 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3534 if (ret) {
3535 dev_err(&pdev->dev, "devices setup failed\n");
3536 return ret;
3537 }
3538 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303539 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3540 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303541 } else {
3542 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303543 }
3544
3545 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3546 if (!motg) {
3547 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3548 return -ENOMEM;
3549 }
3550
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003551 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3552 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003553 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3554 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303555 goto free_motg;
3556 }
3557
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003558 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303559 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003560 phy = &motg->phy;
3561 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303562
3563 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303564 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3565 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3566 * PMIC can detect ACA ID_GND and generate an interrupt.
3567 */
3568 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3569 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3570 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003571 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303572 }
3573
Ofir Cohen4da266f2012-01-03 10:19:29 +02003574 /* initialize reset counter */
3575 motg->reset_counter = 0;
3576
Amit Blay02eff132011-09-21 16:46:24 +03003577 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003578 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003579 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003580 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303581
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303582 /*
3583 * Targets on which link uses asynchronous reset methodology,
3584 * free running clock is not required during the reset.
3585 */
Manu Gautam5143b252012-01-05 19:25:23 -08003586 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303587 if (IS_ERR(motg->clk))
3588 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3589 else
3590 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303591
3592 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003593 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303594 * CORE CLK must be running at >55Mhz for correct HSUSB
3595 * operation and USB core cannot tolerate frequency changes on
3596 * CORE CLK. For such USB cores, vote for maximum clk frequency
3597 * on pclk source
3598 */
Manu Gautam5143b252012-01-05 19:25:23 -08003599 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3600 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303601 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003602 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303603 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003604 goto put_clk;
3605 }
3606 clk_set_rate(motg->core_clk, INT_MAX);
3607
3608 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3609 if (IS_ERR(motg->pclk)) {
3610 dev_err(&pdev->dev, "failed to get iface_clk\n");
3611 ret = PTR_ERR(motg->pclk);
3612 goto put_core_clk;
3613 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303614
3615 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3616 if (!res) {
3617 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3618 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003619 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303620 }
3621
3622 motg->regs = ioremap(res->start, resource_size(res));
3623 if (!motg->regs) {
3624 dev_err(&pdev->dev, "ioremap failed\n");
3625 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003626 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303627 }
3628 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3629
3630 motg->irq = platform_get_irq(pdev, 0);
3631 if (!motg->irq) {
3632 dev_err(&pdev->dev, "platform_get_irq failed\n");
3633 ret = -ENODEV;
3634 goto free_regs;
3635 }
3636
Manu Gautamf8c45642012-08-10 10:20:56 -07003637 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3638 if (motg->async_irq < 0) {
3639 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3640 motg->async_irq = 0;
3641 }
3642
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003643 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003644 if (IS_ERR(motg->xo_handle)) {
3645 dev_err(&pdev->dev, "%s not able to get the handle "
3646 "to vote for TCXO D0 buffer\n", __func__);
3647 ret = PTR_ERR(motg->xo_handle);
3648 goto free_regs;
3649 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303650
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003651 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003652 if (ret) {
3653 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3654 "D0 buffer%d\n", __func__, ret);
3655 goto free_xo_handle;
3656 }
3657
Manu Gautam28b1bac2012-01-30 16:43:06 +05303658 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303659
Mayank Rana248698c2012-04-19 00:03:16 +05303660 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003661 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303662 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003663 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303664 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003665 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003666 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303667 goto devote_xo_handle;
3668 }
3669 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303670 }
3671
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003672 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303673 if (ret) {
3674 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303675 goto devote_xo_handle;
3676 }
3677
3678 ret = regulator_enable(hsusb_vddcx);
3679 if (ret) {
3680 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3681 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303682 }
3683
3684 ret = msm_hsusb_ldo_init(motg, 1);
3685 if (ret) {
3686 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303687 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303688 }
3689
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303690 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303691 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3692 "mhl_usb_hs_switch");
3693 if (IS_ERR(mhl_usb_hs_switch)) {
3694 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003695 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303696 goto free_ldo_init;
3697 }
3698 }
3699
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003700 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303701 if (ret) {
3702 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003703 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303704 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303705 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303706
3707 writel(0, USB_USBINTR);
3708 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003709 /* Ensure that above STOREs are completed before enabling interrupts */
3710 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303711
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303712 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3713 if (ret)
3714 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003715 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303716 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303717 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303718 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303719 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
kibum.leeec040442012-08-22 22:29:46 +09003720 INIT_DELAYED_WORK(&motg->check_ta_work, msm_ta_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303721 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3722 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303723 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3724 "msm_otg", motg);
3725 if (ret) {
3726 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003727 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303728 }
3729
Manu Gautamf8c45642012-08-10 10:20:56 -07003730 if (motg->async_irq) {
3731 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3732 "msm_otg", motg);
3733 if (ret) {
3734 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3735 goto free_irq;
3736 }
3737 disable_irq(motg->async_irq);
3738 }
3739
Jack Pham87f202f2012-08-06 00:24:22 -07003740 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3741 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3742
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003743 phy->init = msm_otg_reset;
3744 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003745 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303746
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003747 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303748
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003749 phy->otg->phy = &motg->phy;
3750 phy->otg->set_host = msm_otg_set_host;
3751 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003752 phy->otg->start_hnp = msm_otg_start_hnp;
3753 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003754
3755 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303756 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003757 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003758 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303759 }
3760
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303761 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303762 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003763 if (motg->pdata->pmic_id_irq) {
3764 ret = request_irq(motg->pdata->pmic_id_irq,
3765 msm_pmic_id_irq,
3766 IRQF_TRIGGER_RISING |
3767 IRQF_TRIGGER_FALLING,
3768 "msm_otg", motg);
3769 if (ret) {
3770 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003771 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003772 }
3773 } else {
3774 ret = -ENODEV;
3775 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003776 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003777 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303778 }
3779
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303780 msm_hsusb_mhl_switch_enable(motg, 1);
3781
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303782 platform_set_drvdata(pdev, motg);
3783 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003784 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303785
Anji jonnalad270e2d2011-08-09 11:28:32 +05303786 ret = msm_otg_debugfs_init(motg);
3787 if (ret)
3788 dev_dbg(&pdev->dev, "mode debugfs file is"
3789 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303790
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003791 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3792 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3793
Amit Blay58b31472011-11-18 09:39:39 +02003794 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3795 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303796 (!(motg->pdata->mode == USB_OTG) ||
3797 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003798 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303799 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003800
Amit Blay58b31472011-11-18 09:39:39 +02003801 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3802 motg->caps = ALLOW_PHY_RETENTION;
3803 }
3804
Amit Blay6fa647a2012-05-24 14:12:08 +03003805 if (motg->pdata->enable_lpm_on_dev_suspend)
3806 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3807
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003808 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303809 pm_runtime_set_active(&pdev->dev);
3810 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303811
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303812 if (motg->pdata->bus_scale_table) {
3813 motg->bus_perf_client =
3814 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3815 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003816 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303817 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303818 else
3819 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303820 }
3821
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303822 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003823
Steve Mucklef132c6c2012-06-06 18:30:57 -07003824remove_phy:
3825 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003826free_async_irq:
3827 if (motg->async_irq)
3828 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303829free_irq:
3830 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003831destroy_wlock:
3832 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303833 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003834 msm_hsusb_ldo_enable(motg, 0);
3835free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303836 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303837free_hsusb_vddcx:
3838 regulator_disable(hsusb_vddcx);
3839free_config_vddcx:
3840 regulator_set_voltage(hsusb_vddcx,
3841 vdd_val[motg->vdd_type][VDD_NONE],
3842 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003843devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303844 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003845 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003846free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003847 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303848free_regs:
3849 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003850put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303851 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303852put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003853 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303854put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303855 if (!IS_ERR(motg->clk))
3856 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003857 if (!IS_ERR(motg->phy_reset_clk))
3858 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003859free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003860 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303861free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303862 kfree(motg);
3863 return ret;
3864}
3865
3866static int __devexit msm_otg_remove(struct platform_device *pdev)
3867{
3868 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003869 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303870 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303871
3872 if (otg->host || otg->gadget)
3873 return -EBUSY;
3874
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303875 if (pdev->dev.of_node)
3876 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003877 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3878 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303879 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303880 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303881 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303882 cancel_delayed_work_sync(&motg->pmic_id_status_work);
kibum.leeec040442012-08-22 22:29:46 +09003883 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303884 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303885
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303886 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303887
3888 device_init_wakeup(&pdev->dev, 0);
3889 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003890 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303891
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303892 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003893 if (motg->pdata->pmic_id_irq)
3894 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003895 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303896 free_irq(motg->irq, motg);
3897
Jack Pham87f202f2012-08-06 00:24:22 -07003898 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3899 motg->pdata->mpm_otgsessvld_int)
3900 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3901
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303902 /*
3903 * Put PHY in low power mode.
3904 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003905 ulpi_read(otg->phy, 0x14);
3906 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303907
3908 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3909 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3910 if (readl(USB_PORTSC) & PORTSC_PHCD)
3911 break;
3912 udelay(1);
3913 cnt++;
3914 }
3915 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003916 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303917
Manu Gautam28b1bac2012-01-30 16:43:06 +05303918 clk_disable_unprepare(motg->pclk);
3919 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003920 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003921 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303922 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303923 regulator_disable(hsusb_vddcx);
3924 regulator_set_voltage(hsusb_vddcx,
3925 vdd_val[motg->vdd_type][VDD_NONE],
3926 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303927
3928 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303929 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303930
Amit Blay02eff132011-09-21 16:46:24 +03003931 if (!IS_ERR(motg->phy_reset_clk))
3932 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303933 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303934 if (!IS_ERR(motg->clk))
3935 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003936 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303937
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303938 if (motg->bus_perf_client)
3939 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303940
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003941 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303942 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303943 return 0;
3944}
3945
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303946#ifdef CONFIG_PM_RUNTIME
3947static int msm_otg_runtime_idle(struct device *dev)
3948{
3949 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003950 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303951
3952 dev_dbg(dev, "OTG runtime idle\n");
3953
Steve Mucklef132c6c2012-06-06 18:30:57 -07003954 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303955 return -EAGAIN;
3956 else
3957 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303958}
3959
3960static int msm_otg_runtime_suspend(struct device *dev)
3961{
3962 struct msm_otg *motg = dev_get_drvdata(dev);
3963
3964 dev_dbg(dev, "OTG runtime suspend\n");
3965 return msm_otg_suspend(motg);
3966}
3967
3968static int msm_otg_runtime_resume(struct device *dev)
3969{
3970 struct msm_otg *motg = dev_get_drvdata(dev);
3971
3972 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303973 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303974 return msm_otg_resume(motg);
3975}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303976#endif
3977
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303978#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303979static int msm_otg_pm_suspend(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);
3983
3984 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003985
3986 atomic_set(&motg->pm_suspended, 1);
3987 ret = msm_otg_suspend(motg);
3988 if (ret)
3989 atomic_set(&motg->pm_suspended, 0);
3990
3991 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303992}
3993
3994static int msm_otg_pm_resume(struct device *dev)
3995{
Jack Pham5ca279b2012-05-14 18:42:54 -07003996 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303997 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303998
3999 dev_dbg(dev, "OTG PM resume\n");
4000
Jack Pham5ca279b2012-05-14 18:42:54 -07004001 atomic_set(&motg->pm_suspended, 0);
Jack Phamb1aa4d82012-08-13 15:32:39 -07004002 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004003 pm_runtime_get_noresume(dev);
4004 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304005
Jack Pham5ca279b2012-05-14 18:42:54 -07004006 /* Update runtime PM status */
4007 pm_runtime_disable(dev);
4008 pm_runtime_set_active(dev);
4009 pm_runtime_enable(dev);
4010
Jack Phamb1aa4d82012-08-13 15:32:39 -07004011 if (motg->sm_work_pending) {
4012 motg->sm_work_pending = false;
4013 queue_work(system_nrt_wq, &motg->sm_work);
4014 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004015 }
4016
4017 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304018}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304019#endif
4020
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304021#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304022static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304023 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4024 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4025 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304026};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304027#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304028
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304029static struct of_device_id msm_otg_dt_match[] = {
4030 { .compatible = "qcom,hsusb-otg",
4031 },
4032 {}
4033};
4034
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304035static struct platform_driver msm_otg_driver = {
4036 .remove = __devexit_p(msm_otg_remove),
4037 .driver = {
4038 .name = DRIVER_NAME,
4039 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304040#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304041 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304042#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304043 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304044 },
4045};
4046
4047static int __init msm_otg_init(void)
4048{
4049 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4050}
4051
4052static void __exit msm_otg_exit(void)
4053{
4054 platform_driver_unregister(&msm_otg_driver);
4055}
4056
4057module_init(msm_otg_init);
4058module_exit(msm_otg_exit);
4059
4060MODULE_LICENSE("GPL v2");
4061MODULE_DESCRIPTION("MSM USB transceiver driver");