blob: b5c106c24996ab124d4b7e3859e275a86a8c2811 [file] [log] [blame]
ChandanaKishori Chiluveruf19104d2014-07-22 12:41:52 +05301/* Copyright (c) 2009-2014, 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;
ChandanaKishori Chiluveru810bd8c2013-11-06 15:33:51 +05301184 int rc;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301185
1186 if (!otg->host)
1187 return;
1188
1189 hcd = bus_to_hcd(otg->host);
1190
1191 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001192 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301193
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301194 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001195 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301196 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1197
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301198 /*
1199 * Some boards have a switch cotrolled by gpio
1200 * to enable/disable internal HUB. Enable internal
1201 * HUB before kicking the host.
1202 */
1203 if (pdata->setup_gpio)
1204 pdata->setup_gpio(OTG_STATE_A_HOST);
ChandanaKishori Chiluveru810bd8c2013-11-06 15:33:51 +05301205
1206 /*
1207 * Increase 3.3V rail voltage to increase cross over voltage.
1208 * This is required to get some full speed audio headsets
1209 * working.
1210 */
1211 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MAX,
1212 USB_PHY_3P3_VOL_MAX);
1213 if (rc)
1214 dev_dbg(otg->phy->dev, "unable to increase 3.3V rail\n");
1215
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301216 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301217 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001218 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301219
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301220 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301221 /* HCD core reset all bits of PORTSC. select ULPI phy */
1222 writel_relaxed(0x80000000, USB_PORTSC);
1223
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301224 if (pdata->setup_gpio)
1225 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301226
1227 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001228 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301229 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
ChandanaKishori Chiluveru810bd8c2013-11-06 15:33:51 +05301230
1231 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
1232 USB_PHY_3P3_VOL_MAX);
1233 if (rc)
1234 dev_dbg(otg->phy->dev, "unable to restore 3.075V rail\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301235 }
1236}
1237
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238static int msm_otg_usbdev_notify(struct notifier_block *self,
1239 unsigned long action, void *priv)
1240{
1241 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001242 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301243 struct usb_device *udev = priv;
1244
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301245 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1246 goto out;
1247
Steve Mucklef132c6c2012-06-06 18:30:57 -07001248 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301249 goto out;
1250 /*
1251 * Interested in devices connected directly to the root hub.
1252 * ACA dock can supply IDEV_CHG irrespective devices connected
1253 * on the accessory port.
1254 */
1255 if (!udev->parent || udev->parent->parent ||
1256 motg->chg_type == USB_ACA_DOCK_CHARGER)
1257 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001258
1259 switch (action) {
1260 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301261 if (aca_enabled())
1262 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001263 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301264 pr_debug("B_CONN set\n");
1265 set_bit(B_CONN, &motg->inputs);
1266 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001267 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301268 /*
1269 * OTG PET: A-device must end session within
1270 * 10 sec after PET enumeration.
1271 */
1272 if (udev->quirks & USB_QUIRK_OTG_PET)
1273 msm_otg_start_timer(motg, TA_TST_MAINT,
1274 A_TST_MAINT);
1275 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301276 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278 if (udev->actconfig)
1279 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1280 else
1281 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001282 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301283 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301284 break;
1285 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001286 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1287 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301288 pr_debug("B_CONN clear\n");
1289 clear_bit(B_CONN, &motg->inputs);
1290 /*
1291 * OTG PET: A-device must end session after
1292 * PET disconnection if it is enumerated
1293 * with bcdDevice[0] = 1. USB core sets
1294 * bus->otg_vbus_off for us. clear it here.
1295 */
1296 if (udev->bus->otg_vbus_off) {
1297 udev->bus->otg_vbus_off = 0;
1298 set_bit(A_BUS_DROP, &motg->inputs);
1299 }
1300 queue_work(system_nrt_wq, &motg->sm_work);
1301 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 default:
1303 break;
1304 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301305 if (test_bit(ID_A, &motg->inputs))
1306 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1307 motg->mA_port);
1308out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001309 return NOTIFY_OK;
1310}
1311
Mayank Ranae3926882011-12-26 09:47:54 +05301312static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1313{
1314 int ret;
1315 static bool vbus_is_on;
1316
1317 if (vbus_is_on == on)
1318 return;
1319
1320 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301321 ret = motg->pdata->vbus_power(on);
1322 if (!ret)
1323 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301324 return;
1325 }
1326
1327 if (!vbus_otg) {
1328 pr_err("vbus_otg is NULL.");
1329 return;
1330 }
1331
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001332 /*
1333 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001334 * from usb before turning on the boost.
1335 * if exiting host mode disable the boost before enabling to draw
1336 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001337 */
Mayank Ranae3926882011-12-26 09:47:54 +05301338 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001339 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301340 ret = regulator_enable(vbus_otg);
1341 if (ret) {
1342 pr_err("unable to enable vbus_otg\n");
1343 return;
1344 }
1345 vbus_is_on = true;
1346 } else {
1347 ret = regulator_disable(vbus_otg);
1348 if (ret) {
1349 pr_err("unable to disable vbus_otg\n");
1350 return;
1351 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001352 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301353 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301354 }
1355}
1356
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001357static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301358{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001359 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301360 struct usb_hcd *hcd;
1361
1362 /*
1363 * Fail host registration if this board can support
1364 * only peripheral configuration.
1365 */
1366 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001367 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301368 return -ENODEV;
1369 }
1370
Devin Kimd10f03c2012-06-25 17:05:13 -07001371 if (!machine_is_apq8064_mako()) {
1372 if (!motg->pdata->vbus_power && host) {
1373 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1374 if (IS_ERR(vbus_otg)) {
1375 pr_err("Unable to get vbus_otg\n");
1376 return -ENODEV;
1377 }
Mayank Ranae3926882011-12-26 09:47:54 +05301378 }
1379 }
1380
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301381 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001382 if (otg->phy->state == OTG_STATE_A_HOST) {
1383 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001384 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301385 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301386 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301387 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001388 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301389 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301390 } else {
1391 otg->host = NULL;
1392 }
1393
1394 return 0;
1395 }
1396
1397 hcd = bus_to_hcd(host);
1398 hcd->power_budget = motg->pdata->power_budget;
1399
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301400#ifdef CONFIG_USB_OTG
1401 host->otg_port = 1;
1402#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001403 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1404 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301405 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001406 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301407
1408 /*
1409 * Kick the state machine work, if peripheral is not supported
1410 * or peripheral is already registered with us.
1411 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301412 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001413 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301414 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301415 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301416
1417 return 0;
1418}
1419
Steve Mucklef132c6c2012-06-06 18:30:57 -07001420static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301421{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301422 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001423 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301424 struct msm_otg_platform_data *pdata = motg->pdata;
1425
1426 if (!otg->gadget)
1427 return;
1428
1429 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001430 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301431 /*
1432 * Some boards have a switch cotrolled by gpio
1433 * to enable/disable internal HUB. Disable internal
1434 * HUB before kicking the gadget.
1435 */
1436 if (pdata->setup_gpio)
1437 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001438
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301439 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301440 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301441 ret = msm_bus_scale_client_update_request(
1442 motg->bus_perf_client, 1);
1443 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001444 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301445 "bus bandwidth %d\n", __func__, ret);
1446 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301447 usb_gadget_vbus_connect(otg->gadget);
1448 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001449 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301450 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301451 /* Configure BUS performance parameters to default */
1452 if (motg->bus_perf_client) {
1453 ret = msm_bus_scale_client_update_request(
1454 motg->bus_perf_client, 0);
1455 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001456 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301457 "for bus bw %d\n", __func__, ret);
1458 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301459 if (pdata->setup_gpio)
1460 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1461 }
1462
1463}
1464
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001465static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301466 struct usb_gadget *gadget)
1467{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001468 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301469
1470 /*
1471 * Fail peripheral registration if this board can support
1472 * only host configuration.
1473 */
1474 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001475 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301476 return -ENODEV;
1477 }
1478
1479 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001480 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1481 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301482 msm_otg_start_peripheral(otg, 0);
1483 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001484 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301485 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301486 } else {
1487 otg->gadget = NULL;
1488 }
1489
1490 return 0;
1491 }
1492 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001493 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301494
1495 /*
1496 * Kick the state machine work, if host is not supported
1497 * or host is already registered with us.
1498 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301499 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001500 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301501 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301502 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301503
1504 return 0;
1505}
1506
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301507static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1508{
1509 unsigned long flags;
1510 int id;
1511
1512 if (!motg->pdata->pmic_id_irq)
1513 return -ENODEV;
1514
1515 local_irq_save(flags);
1516 id = irq_read_line(motg->pdata->pmic_id_irq);
1517 local_irq_restore(flags);
1518
1519 /*
1520 * If we can not read ID line state for some reason, treat
1521 * it as float. This would prevent MHL discovery and kicking
1522 * host mode unnecessarily.
1523 */
1524 return !!id;
1525}
1526
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301527static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1528 void (*callback)(int on))
1529{
1530 struct usb_phy *phy = &motg->phy;
1531 int ret;
1532
1533 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1534 !motg->pdata->pmic_id_irq) {
1535 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1536 return -ENODEV;
1537 }
1538
1539 if (!motg->pdata->mhl_dev_name) {
1540 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1541 return -ENODEV;
1542 }
1543
1544 if (callback)
1545 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1546 callback);
1547 else
1548 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1549
1550 if (ret)
1551 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1552 motg->pdata->mhl_dev_name, ret);
1553 else
1554 motg->mhl_enabled = true;
1555
1556 return ret;
1557}
1558
1559static void msm_otg_mhl_notify_online(int on)
1560{
1561 struct msm_otg *motg = the_msm_otg;
1562 struct usb_phy *phy = &motg->phy;
1563 bool queue = false;
1564
1565 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1566
1567 if (on) {
1568 set_bit(MHL, &motg->inputs);
1569 } else {
1570 clear_bit(MHL, &motg->inputs);
1571 queue = true;
1572 }
1573
1574 if (queue && phy->state != OTG_STATE_UNDEFINED)
1575 schedule_work(&motg->sm_work);
1576}
1577
1578static bool msm_otg_is_mhl(struct msm_otg *motg)
1579{
1580 struct usb_phy *phy = &motg->phy;
1581 int is_mhl, ret;
1582
1583 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1584 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1585 /*
1586 * MHL driver calls our callback saying that MHL connected
1587 * if RID_GND is detected. But at later part of discovery
1588 * it may figure out MHL is not connected and returns
1589 * false. Hence clear MHL input here.
1590 */
1591 clear_bit(MHL, &motg->inputs);
1592 dev_dbg(phy->dev, "MHL device not found\n");
1593 return false;
1594 }
1595
1596 set_bit(MHL, &motg->inputs);
1597 dev_dbg(phy->dev, "MHL device found\n");
1598 return true;
1599}
1600
1601static bool msm_chg_mhl_detect(struct msm_otg *motg)
1602{
1603 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301604
1605 if (!motg->mhl_enabled)
1606 return false;
1607
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301608 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301609
1610 if (id)
1611 return false;
1612
1613 mhl_det_in_progress = true;
1614 ret = msm_otg_is_mhl(motg);
1615 mhl_det_in_progress = false;
1616
1617 return ret;
1618}
1619
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001620static bool msm_chg_aca_detect(struct msm_otg *motg)
1621{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001622 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623 u32 int_sts;
1624 bool ret = false;
1625
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301626 if (!aca_enabled())
1627 goto out;
1628
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001629 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1630 goto out;
1631
Steve Mucklef132c6c2012-06-06 18:30:57 -07001632 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001633 switch (int_sts & 0x1C) {
1634 case 0x08:
1635 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001636 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 motg->chg_type = USB_ACA_A_CHARGER;
1638 motg->chg_state = USB_CHG_STATE_DETECTED;
1639 clear_bit(ID_B, &motg->inputs);
1640 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301641 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001642 ret = true;
1643 }
1644 break;
1645 case 0x0C:
1646 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001647 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001648 motg->chg_type = USB_ACA_B_CHARGER;
1649 motg->chg_state = USB_CHG_STATE_DETECTED;
1650 clear_bit(ID_A, &motg->inputs);
1651 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301652 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001653 ret = true;
1654 }
1655 break;
1656 case 0x10:
1657 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001658 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001659 motg->chg_type = USB_ACA_C_CHARGER;
1660 motg->chg_state = USB_CHG_STATE_DETECTED;
1661 clear_bit(ID_A, &motg->inputs);
1662 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301663 set_bit(ID, &motg->inputs);
1664 ret = true;
1665 }
1666 break;
1667 case 0x04:
1668 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001669 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301670 motg->chg_type = USB_INVALID_CHARGER;
1671 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1672 clear_bit(ID_A, &motg->inputs);
1673 clear_bit(ID_B, &motg->inputs);
1674 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675 ret = true;
1676 }
1677 break;
1678 default:
1679 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1680 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301681 test_and_clear_bit(ID_C, &motg->inputs) |
1682 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001683 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001684 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001685 motg->chg_type = USB_INVALID_CHARGER;
1686 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1687 }
1688 }
1689out:
1690 return ret;
1691}
1692
1693static void msm_chg_enable_aca_det(struct msm_otg *motg)
1694{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001695 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301697 if (!aca_enabled())
1698 return;
1699
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001700 switch (motg->pdata->phy_type) {
1701 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301702 /* Disable ID_GND in link and PHY */
1703 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1704 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001705 ulpi_write(phy, 0x01, 0x0C);
1706 ulpi_write(phy, 0x10, 0x0F);
1707 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301708 /* Disable PMIC ID pull-up */
1709 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301710 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001711 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301712 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713 break;
1714 default:
1715 break;
1716 }
1717}
1718
1719static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1720{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001721 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301723 if (!aca_enabled())
1724 return;
1725
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001726 switch (motg->pdata->phy_type) {
1727 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301728 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001729 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301730 break;
1731 default:
1732 break;
1733 }
1734}
1735
1736static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1737{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001738 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301739
1740 if (!aca_enabled())
1741 return;
1742
1743 switch (motg->pdata->phy_type) {
1744 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001745 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001746 break;
1747 default:
1748 break;
1749 }
1750}
1751
1752static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1753{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001754 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001755 bool ret = false;
1756
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301757 if (!aca_enabled())
1758 return ret;
1759
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001760 switch (motg->pdata->phy_type) {
1761 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001762 if (ulpi_read(phy, 0x91) & 1) {
1763 dev_dbg(phy->dev, "RID change\n");
1764 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001765 ret = msm_chg_aca_detect(motg);
1766 }
1767 default:
1768 break;
1769 }
1770 return ret;
1771}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301772
1773static void msm_otg_id_timer_func(unsigned long data)
1774{
1775 struct msm_otg *motg = (struct msm_otg *) data;
1776
1777 if (!aca_enabled())
1778 return;
1779
1780 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001781 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301782 return;
1783 }
1784
Steve Mucklef132c6c2012-06-06 18:30:57 -07001785 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301786 goto out;
1787
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301788 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001789 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301790 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301791 }
1792
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301793out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301794 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1795 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1796}
1797
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301798static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1799{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001800 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301801 u32 chg_det;
1802 bool ret = false;
1803
1804 switch (motg->pdata->phy_type) {
1805 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001806 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301807 ret = chg_det & (1 << 4);
1808 break;
1809 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001810 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301811 ret = chg_det & 1;
1812 break;
1813 default:
1814 break;
1815 }
1816 return ret;
1817}
1818
1819static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1820{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001821 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301822 u32 chg_det;
1823
1824 switch (motg->pdata->phy_type) {
1825 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001826 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301827 /* Turn off charger block */
1828 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001829 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301830 udelay(20);
1831 /* control chg block via ULPI */
1832 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001833 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301834 /* put it in host mode for enabling D- source */
1835 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001836 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301837 /* Turn on chg detect block */
1838 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001839 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301840 udelay(20);
1841 /* enable chg detection */
1842 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001843 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301844 break;
1845 case SNPS_28NM_INTEGRATED_PHY:
1846 /*
1847 * Configure DM as current source, DP as current sink
1848 * and enable battery charging comparators.
1849 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001850 ulpi_write(phy, 0x8, 0x85);
1851 ulpi_write(phy, 0x2, 0x85);
1852 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301853 break;
1854 default:
1855 break;
1856 }
1857}
1858
1859static bool msm_chg_check_primary_det(struct msm_otg *motg)
1860{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001861 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301862 u32 chg_det;
1863 bool ret = false;
1864
1865 switch (motg->pdata->phy_type) {
1866 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001867 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301868 ret = chg_det & (1 << 4);
1869 break;
1870 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001871 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301872 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301873 /* Turn off VDP_SRC */
1874 ulpi_write(phy, 0x3, 0x86);
1875 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301876 break;
1877 default:
1878 break;
1879 }
1880 return ret;
1881}
1882
1883static void msm_chg_enable_primary_det(struct msm_otg *motg)
1884{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001885 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301886 u32 chg_det;
1887
1888 switch (motg->pdata->phy_type) {
1889 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001890 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301891 /* enable chg detection */
1892 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001893 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301894 break;
1895 case SNPS_28NM_INTEGRATED_PHY:
1896 /*
1897 * Configure DP as current source, DM as current sink
1898 * and enable battery charging comparators.
1899 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001900 ulpi_write(phy, 0x2, 0x85);
1901 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301902 break;
1903 default:
1904 break;
1905 }
1906}
1907
1908static bool msm_chg_check_dcd(struct msm_otg *motg)
1909{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001910 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301911 u32 line_state;
1912 bool ret = false;
1913
1914 switch (motg->pdata->phy_type) {
1915 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001916 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301917 ret = !(line_state & 1);
1918 break;
1919 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001920 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301921 ret = line_state & 2;
1922 break;
1923 default:
1924 break;
1925 }
1926 return ret;
1927}
1928
1929static void msm_chg_disable_dcd(struct msm_otg *motg)
1930{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001931 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301932 u32 chg_det;
1933
1934 switch (motg->pdata->phy_type) {
1935 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001936 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301937 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001938 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301939 break;
1940 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001941 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301942 break;
1943 default:
1944 break;
1945 }
1946}
1947
1948static void msm_chg_enable_dcd(struct msm_otg *motg)
1949{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001950 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301951 u32 chg_det;
1952
1953 switch (motg->pdata->phy_type) {
1954 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001955 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301956 /* Turn on D+ current source */
1957 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001958 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301959 break;
1960 case SNPS_28NM_INTEGRATED_PHY:
1961 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001962 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301963 break;
1964 default:
1965 break;
1966 }
1967}
1968
1969static void msm_chg_block_on(struct msm_otg *motg)
1970{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001971 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301972 u32 func_ctrl, chg_det;
1973
1974 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001975 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301976 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1977 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001978 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301979
1980 switch (motg->pdata->phy_type) {
1981 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001982 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301983 /* control chg block via ULPI */
1984 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001985 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301986 /* Turn on chg detect block */
1987 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001988 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301989 udelay(20);
1990 break;
1991 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05301992 /* disable DP and DM pull down resistors */
1993 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301994 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001995 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301996 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001997 ulpi_write(phy, 0x1F, 0x92);
1998 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301999 udelay(100);
2000 break;
2001 default:
2002 break;
2003 }
2004}
2005
2006static void msm_chg_block_off(struct msm_otg *motg)
2007{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002008 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302009 u32 func_ctrl, chg_det;
2010
2011 switch (motg->pdata->phy_type) {
2012 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002013 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302014 /* Turn off charger block */
2015 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002016 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302017 break;
2018 case SNPS_28NM_INTEGRATED_PHY:
2019 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002020 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302021 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002022 ulpi_write(phy, 0x1F, 0x92);
2023 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302024 break;
2025 default:
2026 break;
2027 }
2028
2029 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002030 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302031 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2032 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002033 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302034}
2035
Anji jonnalad270e2d2011-08-09 11:28:32 +05302036static const char *chg_to_string(enum usb_chg_type chg_type)
2037{
2038 switch (chg_type) {
2039 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2040 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2041 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2042 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2043 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2044 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2045 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302046 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302047 default: return "INVALID_CHARGER";
2048 }
2049}
kibum.leeec040442012-08-22 22:29:46 +09002050
Devin Kimd93ff032012-09-14 15:01:31 -07002051#define MSM_CHECK_TA_DELAY (5 * HZ)
2052#define PORTSC_LS (3 << 10) /* Read - Port's Line status */
kibum.leeec040442012-08-22 22:29:46 +09002053static void msm_ta_detect_work(struct work_struct *w)
2054{
Devin Kimd93ff032012-09-14 15:01:31 -07002055 struct msm_otg *motg = container_of(w, struct msm_otg, check_ta_work.work);
kibum.leeec040442012-08-22 22:29:46 +09002056 struct usb_otg *otg = motg->phy.otg;
kibum.leeec040442012-08-22 22:29:46 +09002057
Devin Kim6b8a5d12012-10-16 23:12:18 -07002058 pr_debug("msm_ta_detect_work: ta detection work\n");
Devin Kimd93ff032012-09-14 15:01:31 -07002059
2060 /* Presence of FRame Index or FRINDEX rollover implies USB communication */
2061 if( (readl(USB_FRINDEX) != 0) || ( readl(USB_USBSTS) & (1<<3) ) ) {
2062 pr_info("msm_ta_detect_work: USB exit ta detection - frindex\n");
2063 return;
2064 }
2065
2066 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
2067 pr_info("msm_ta_detect_work: ta dectection success\n");
kibum.leeec040442012-08-22 22:29:46 +09002068 /* inform to user space that SDP is no longer detected */
2069 msm_otg_notify_charger(motg, 0);
2070 motg->chg_state = USB_CHG_STATE_DETECTED;
2071 motg->chg_type = USB_DCP_CHARGER;
2072 motg->cur_power = 0;
2073 msm_otg_start_peripheral(otg, 0);
2074 otg->phy->state = OTG_STATE_B_IDLE;
2075 schedule_work(&motg->sm_work);
2076 return;
kibum.leeec040442012-08-22 22:29:46 +09002077 }
Devin Kimd93ff032012-09-14 15:01:31 -07002078 schedule_delayed_work(&motg->check_ta_work, MSM_CHECK_TA_DELAY);
kibum.leeec040442012-08-22 22:29:46 +09002079}
Anji jonnalad270e2d2011-08-09 11:28:32 +05302080
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302081#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2082#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302083#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2084#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302085static void msm_chg_detect_work(struct work_struct *w)
2086{
2087 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002088 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302089 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302090 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302091 unsigned long delay;
2092
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002093 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302094
2095 if (test_bit(MHL, &motg->inputs)) {
2096 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2097 return;
2098 }
2099
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302100 switch (motg->chg_state) {
2101 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302102 msm_chg_block_on(motg);
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302103 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002104 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302105 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302106 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302107 delay = MSM_CHG_DCD_POLL_TIME;
2108 break;
2109 case USB_CHG_STATE_WAIT_FOR_DCD:
jh.koo78df86f2012-09-04 20:13:19 +09002110 if (slimport_is_connected()) {
2111 msm_chg_block_off(motg);
2112 motg->chg_state = USB_CHG_STATE_DETECTED;
2113 motg->chg_type = USB_SDP_CHARGER;
2114 queue_work(system_nrt_wq, &motg->sm_work);
2115 return;
2116 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302117 if (msm_chg_mhl_detect(motg)) {
2118 msm_chg_block_off(motg);
2119 motg->chg_state = USB_CHG_STATE_DETECTED;
2120 motg->chg_type = USB_INVALID_CHARGER;
2121 queue_work(system_nrt_wq, &motg->sm_work);
2122 return;
2123 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002124 is_aca = msm_chg_aca_detect(motg);
2125 if (is_aca) {
2126 /*
2127 * ID_A can be ACA dock too. continue
2128 * primary detection after DCD.
2129 */
2130 if (test_bit(ID_A, &motg->inputs)) {
2131 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2132 } else {
2133 delay = 0;
2134 break;
2135 }
2136 }
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302137 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302138 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2139 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302140 if (is_dcd || tmout) {
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302141 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302142 msm_chg_enable_primary_det(motg);
2143 delay = MSM_CHG_PRIMARY_DET_TIME;
2144 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2145 } else {
2146 delay = MSM_CHG_DCD_POLL_TIME;
2147 }
2148 break;
2149 case USB_CHG_STATE_DCD_DONE:
2150 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302151 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2152 dm_vlgc = line_state & PORTSC_LS_DM;
2153 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302154 if (test_bit(ID_A, &motg->inputs)) {
2155 motg->chg_type = USB_ACA_DOCK_CHARGER;
2156 motg->chg_state = USB_CHG_STATE_DETECTED;
2157 delay = 0;
2158 break;
2159 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302160 if (line_state) { /* DP > VLGC */
2161 motg->chg_type = USB_PROPRIETARY_CHARGER;
2162 motg->chg_state = USB_CHG_STATE_DETECTED;
2163 delay = 0;
2164 } else {
2165 msm_chg_enable_secondary_det(motg);
2166 delay = MSM_CHG_SECONDARY_DET_TIME;
2167 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2168 }
2169 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302170 if (test_bit(ID_A, &motg->inputs)) {
2171 motg->chg_type = USB_ACA_A_CHARGER;
2172 motg->chg_state = USB_CHG_STATE_DETECTED;
2173 delay = 0;
2174 break;
2175 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302176
2177 if (line_state) /* DP > VLGC or/and DM > VLGC */
2178 motg->chg_type = USB_PROPRIETARY_CHARGER;
2179 else
2180 motg->chg_type = USB_SDP_CHARGER;
2181
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302182 motg->chg_state = USB_CHG_STATE_DETECTED;
2183 delay = 0;
2184 }
2185 break;
2186 case USB_CHG_STATE_PRIMARY_DONE:
2187 vout = msm_chg_check_secondary_det(motg);
2188 if (vout)
2189 motg->chg_type = USB_DCP_CHARGER;
2190 else
2191 motg->chg_type = USB_CDP_CHARGER;
2192 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2193 /* fall through */
2194 case USB_CHG_STATE_SECONDARY_DONE:
2195 motg->chg_state = USB_CHG_STATE_DETECTED;
2196 case USB_CHG_STATE_DETECTED:
2197 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002198 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302199 /*
2200 * Spurious interrupt is seen after enabling ACA detection
2201 * due to which charger detection fails in case of PET.
2202 * Add delay of 100 microsec to avoid that.
2203 */
2204 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002205 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002206 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302207 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302208 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302209 return;
2210 default:
2211 return;
2212 }
2213
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302214 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302215}
2216
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302217/*
2218 * We support OTG, Peripheral only and Host only configurations. In case
2219 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2220 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2221 * enabled when switch is controlled by user and default mode is supplied
2222 * by board file, which can be changed by userspace later.
2223 */
2224static void msm_otg_init_sm(struct msm_otg *motg)
2225{
2226 struct msm_otg_platform_data *pdata = motg->pdata;
2227 u32 otgsc = readl(USB_OTGSC);
2228
2229 switch (pdata->mode) {
2230 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002231 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302232 if (pdata->default_mode == USB_HOST) {
2233 clear_bit(ID, &motg->inputs);
2234 } else if (pdata->default_mode == USB_PERIPHERAL) {
2235 set_bit(ID, &motg->inputs);
2236 set_bit(B_SESS_VLD, &motg->inputs);
2237 } else {
2238 set_bit(ID, &motg->inputs);
2239 clear_bit(B_SESS_VLD, &motg->inputs);
2240 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302241 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302242 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302243 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302244 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302245 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302246 set_bit(A_BUS_REQ, &motg->inputs);
2247 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002248 if (otgsc & OTGSC_BSV)
2249 set_bit(B_SESS_VLD, &motg->inputs);
2250 else
2251 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302252 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302253 if (pdata->pmic_id_irq) {
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302254 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302255 set_bit(ID, &motg->inputs);
2256 else
2257 clear_bit(ID, &motg->inputs);
2258 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302259 /*
2260 * VBUS initial state is reported after PMIC
2261 * driver initialization. Wait for it.
2262 */
2263 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302264 }
2265 break;
2266 case USB_HOST:
2267 clear_bit(ID, &motg->inputs);
2268 break;
2269 case USB_PERIPHERAL:
2270 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302271 if (pdata->otg_control == OTG_PHY_CONTROL) {
2272 if (otgsc & OTGSC_BSV)
2273 set_bit(B_SESS_VLD, &motg->inputs);
2274 else
2275 clear_bit(B_SESS_VLD, &motg->inputs);
2276 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2277 /*
2278 * VBUS initial state is reported after PMIC
2279 * driver initialization. Wait for it.
2280 */
2281 wait_for_completion(&pmic_vbus_init);
2282 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302283 break;
2284 default:
2285 break;
2286 }
2287}
2288
2289static void msm_otg_sm_work(struct work_struct *w)
2290{
2291 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002292 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302293 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302294
Steve Mucklef132c6c2012-06-06 18:30:57 -07002295 pm_runtime_resume(otg->phy->dev);
2296 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002297 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302298 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002299 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302300 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302301 psy = power_supply_get_by_name("usb");
2302 if (!psy)
2303 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002304 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302305 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2306 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002307 pm_runtime_put_noidle(otg->phy->dev);
2308 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302309 break;
2310 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302311 /* FALL THROUGH */
2312 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302313 if (test_bit(MHL, &motg->inputs)) {
2314 /* allow LPM */
2315 pm_runtime_put_noidle(otg->phy->dev);
2316 pm_runtime_suspend(otg->phy->dev);
2317 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002318 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302319 pr_debug("!id || id_A\n");
jh.koo78df86f2012-09-04 20:13:19 +09002320 if (slimport_is_connected()) {
2321 work = 1;
2322 break;
2323 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302324 if (msm_chg_mhl_detect(motg)) {
2325 work = 1;
2326 break;
2327 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302328 clear_bit(B_BUS_REQ, &motg->inputs);
2329 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002330 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302331 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302332 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302333 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302334 switch (motg->chg_state) {
2335 case USB_CHG_STATE_UNDEFINED:
2336 msm_chg_detect_work(&motg->chg_work.work);
2337 break;
2338 case USB_CHG_STATE_DETECTED:
2339 switch (motg->chg_type) {
2340 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302341 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002342 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302343 /* fall through */
2344 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302345 msm_otg_notify_charger(motg,
2346 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002347 pm_runtime_put_noidle(otg->phy->dev);
2348 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302349 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302350 case USB_ACA_B_CHARGER:
2351 msm_otg_notify_charger(motg,
2352 IDEV_ACA_CHG_MAX);
2353 /*
2354 * (ID_B --> ID_C) PHY_ALT interrupt can
2355 * not be detected in LPM.
2356 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302357 break;
2358 case USB_CDP_CHARGER:
2359 msm_otg_notify_charger(motg,
2360 IDEV_CHG_MAX);
2361 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002362 otg->phy->state =
2363 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302364 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302365 case USB_ACA_C_CHARGER:
2366 msm_otg_notify_charger(motg,
2367 IDEV_ACA_CHG_MAX);
2368 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002369 otg->phy->state =
2370 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302371 break;
2372 case USB_SDP_CHARGER:
jh.koo78df86f2012-09-04 20:13:19 +09002373 if(!slimport_is_connected()) {
2374 msm_otg_start_peripheral(otg, 1);
2375 otg->phy->state =
2376 OTG_STATE_B_PERIPHERAL;
2377 }
kibum.leeec040442012-08-22 22:29:46 +09002378 schedule_delayed_work(&motg->check_ta_work,
2379 MSM_CHECK_TA_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302380 break;
2381 default:
2382 break;
2383 }
2384 break;
2385 default:
2386 break;
2387 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302388 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2389 pr_debug("b_sess_end && b_bus_req\n");
2390 if (msm_otg_start_srp(otg) < 0) {
2391 clear_bit(B_BUS_REQ, &motg->inputs);
2392 work = 1;
2393 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302394 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002395 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302396 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2397 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302398 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302399 pr_debug("chg_work cancel");
Mayank Rana9062a672013-01-23 19:26:21 +05302400 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302401 cancel_delayed_work_sync(&motg->chg_work);
kibum.leeec040442012-08-22 22:29:46 +09002402 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302403 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2404 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302405 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002406 msm_otg_reset(otg->phy);
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302407 /*
2408 * There is a small window where ID interrupt
2409 * is not monitored during ID detection circuit
2410 * switch from ACA to PMIC. Check ID state
2411 * before entering into low power mode.
2412 */
ChandanaKishori Chiluveruf19104d2014-07-22 12:41:52 +05302413 if ((motg->pdata->otg_control == OTG_PMIC_CONTROL) &&
2414 !msm_otg_read_pmic_id_state(motg)) {
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302415 pr_debug("process missed ID intr\n");
2416 clear_bit(ID, &motg->inputs);
2417 work = 1;
2418 break;
2419 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002420 pm_runtime_put_noidle(otg->phy->dev);
2421 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302422 }
2423 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302424 case OTG_STATE_B_SRP_INIT:
2425 if (!test_bit(ID, &motg->inputs) ||
2426 test_bit(ID_A, &motg->inputs) ||
2427 test_bit(ID_C, &motg->inputs) ||
2428 (test_bit(B_SESS_VLD, &motg->inputs) &&
2429 !test_bit(ID_B, &motg->inputs))) {
2430 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2431 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002432 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302433 /*
2434 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2435 * bits after SRP initiation.
2436 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002437 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302438 work = 1;
2439 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2440 pr_debug("b_srp_fail\n");
2441 pr_info("A-device did not respond to SRP\n");
2442 clear_bit(B_BUS_REQ, &motg->inputs);
2443 clear_bit(B_SRP_FAIL, &motg->tmouts);
2444 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002445 ulpi_write(otg->phy, 0x0, 0x98);
2446 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302447 motg->b_last_se0_sess = jiffies;
2448 work = 1;
2449 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302450 break;
2451 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302452 if (!test_bit(ID, &motg->inputs) ||
2453 test_bit(ID_A, &motg->inputs) ||
2454 test_bit(ID_B, &motg->inputs) ||
2455 !test_bit(B_SESS_VLD, &motg->inputs)) {
2456 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302457 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2458 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302459 msm_otg_notify_charger(motg, 0);
2460 srp_reqd = otg->gadget->otg_srp_reqd;
2461 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302462 if (test_bit(ID_B, &motg->inputs))
2463 clear_bit(ID_B, &motg->inputs);
2464 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002465 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302466 motg->b_last_se0_sess = jiffies;
2467 if (srp_reqd)
2468 msm_otg_start_timer(motg,
2469 TB_TST_SRP, B_TST_SRP);
2470 else
2471 work = 1;
2472 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2473 otg->gadget->b_hnp_enable &&
2474 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2475 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2476 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2477 /* D+ pullup should not be disconnected within 4msec
2478 * after A device suspends the bus. Otherwise PET will
2479 * fail the compliance test.
2480 */
2481 udelay(1000);
2482 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002483 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302484 /*
2485 * start HCD even before A-device enable
2486 * pull-up to meet HNP timings.
2487 */
2488 otg->host->is_b_host = 1;
2489 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002490 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2491 test_bit(B_SESS_VLD, &motg->inputs)) {
2492 pr_debug("a_bus_suspend && b_sess_vld\n");
2493 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002494 pm_runtime_put_noidle(otg->phy->dev);
2495 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002496 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002497 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302498 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002499 }
2500 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302501 case OTG_STATE_B_WAIT_ACON:
2502 if (!test_bit(ID, &motg->inputs) ||
2503 test_bit(ID_A, &motg->inputs) ||
2504 test_bit(ID_B, &motg->inputs) ||
2505 !test_bit(B_SESS_VLD, &motg->inputs)) {
2506 pr_debug("!id || id_a/b || !b_sess_vld\n");
2507 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302508 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302509 * A-device is physically disconnected during
2510 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302511 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302512 msm_otg_start_host(otg, 0);
2513 otg->host->is_b_host = 0;
2514
2515 clear_bit(B_BUS_REQ, &motg->inputs);
2516 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2517 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002518 otg->phy->state = OTG_STATE_B_IDLE;
2519 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302520 work = 1;
2521 } else if (test_bit(A_CONN, &motg->inputs)) {
2522 pr_debug("a_conn\n");
2523 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002524 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302525 /*
2526 * PET disconnects D+ pullup after reset is generated
2527 * by B device in B_HOST role which is not detected by
2528 * B device. As workaorund , start timer of 300msec
2529 * and stop timer if A device is enumerated else clear
2530 * A_CONN.
2531 */
2532 msm_otg_start_timer(motg, TB_TST_CONFIG,
2533 B_TST_CONFIG);
2534 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2535 pr_debug("b_ase0_brst_tmout\n");
2536 pr_info("B HNP fail:No response from A device\n");
2537 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002538 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302539 otg->host->is_b_host = 0;
2540 clear_bit(B_ASE0_BRST, &motg->tmouts);
2541 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2542 clear_bit(B_BUS_REQ, &motg->inputs);
2543 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002544 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302545 work = 1;
2546 } else if (test_bit(ID_C, &motg->inputs)) {
2547 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2548 }
2549 break;
2550 case OTG_STATE_B_HOST:
2551 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2552 !test_bit(A_CONN, &motg->inputs) ||
2553 !test_bit(B_SESS_VLD, &motg->inputs)) {
2554 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2555 clear_bit(A_CONN, &motg->inputs);
2556 clear_bit(B_BUS_REQ, &motg->inputs);
2557 msm_otg_start_host(otg, 0);
2558 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002559 otg->phy->state = OTG_STATE_B_IDLE;
2560 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302561 work = 1;
2562 } else if (test_bit(ID_C, &motg->inputs)) {
2563 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2564 }
2565 break;
2566 case OTG_STATE_A_IDLE:
2567 otg->default_a = 1;
2568 if (test_bit(ID, &motg->inputs) &&
2569 !test_bit(ID_A, &motg->inputs)) {
2570 pr_debug("id && !id_a\n");
2571 otg->default_a = 0;
2572 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002573 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302574 del_timer_sync(&motg->id_timer);
2575 msm_otg_link_reset(motg);
2576 msm_chg_enable_aca_intr(motg);
2577 msm_otg_notify_charger(motg, 0);
2578 work = 1;
2579 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2580 (test_bit(A_SRP_DET, &motg->inputs) ||
2581 test_bit(A_BUS_REQ, &motg->inputs))) {
2582 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2583
2584 clear_bit(A_SRP_DET, &motg->inputs);
2585 /* Disable SRP detection */
2586 writel_relaxed((readl_relaxed(USB_OTGSC) &
2587 ~OTGSC_INTSTS_MASK) &
2588 ~OTGSC_DPIE, USB_OTGSC);
2589
Steve Mucklef132c6c2012-06-06 18:30:57 -07002590 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302591 /* VBUS should not be supplied before end of SRP pulse
2592 * generated by PET, if not complaince test fail.
2593 */
2594 usleep_range(10000, 12000);
2595 /* ACA: ID_A: Stop charging untill enumeration */
2596 if (test_bit(ID_A, &motg->inputs))
2597 msm_otg_notify_charger(motg, 0);
2598 else
2599 msm_hsusb_vbus_power(motg, 1);
2600 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2601 } else {
2602 pr_debug("No session requested\n");
2603 clear_bit(A_BUS_DROP, &motg->inputs);
2604 if (test_bit(ID_A, &motg->inputs)) {
2605 msm_otg_notify_charger(motg,
2606 IDEV_ACA_CHG_MAX);
2607 } else if (!test_bit(ID, &motg->inputs)) {
2608 msm_otg_notify_charger(motg, 0);
2609 /*
2610 * A-device is not providing power on VBUS.
2611 * Enable SRP detection.
2612 */
2613 writel_relaxed(0x13, USB_USBMODE);
2614 writel_relaxed((readl_relaxed(USB_OTGSC) &
2615 ~OTGSC_INTSTS_MASK) |
2616 OTGSC_DPIE, USB_OTGSC);
2617 mb();
2618 }
2619 }
2620 break;
2621 case OTG_STATE_A_WAIT_VRISE:
2622 if ((test_bit(ID, &motg->inputs) &&
2623 !test_bit(ID_A, &motg->inputs)) ||
2624 test_bit(A_BUS_DROP, &motg->inputs) ||
2625 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2626 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2627 clear_bit(A_BUS_REQ, &motg->inputs);
2628 msm_otg_del_timer(motg);
2629 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002630 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302631 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2632 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2633 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002634 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302635 if (TA_WAIT_BCON > 0)
2636 msm_otg_start_timer(motg, TA_WAIT_BCON,
2637 A_WAIT_BCON);
2638 msm_otg_start_host(otg, 1);
2639 msm_chg_enable_aca_det(motg);
2640 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302641 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302642 if (msm_chg_check_aca_intr(motg))
2643 work = 1;
2644 }
2645 break;
2646 case OTG_STATE_A_WAIT_BCON:
2647 if ((test_bit(ID, &motg->inputs) &&
2648 !test_bit(ID_A, &motg->inputs)) ||
2649 test_bit(A_BUS_DROP, &motg->inputs) ||
2650 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2651 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2652 "a_wait_bcon_tmout\n");
2653 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2654 pr_info("Device No Response\n");
2655 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2656 }
2657 msm_otg_del_timer(motg);
2658 clear_bit(A_BUS_REQ, &motg->inputs);
2659 clear_bit(B_CONN, &motg->inputs);
2660 msm_otg_start_host(otg, 0);
2661 /*
2662 * ACA: ID_A with NO accessory, just the A plug is
2663 * attached to ACA: Use IDCHG_MAX for charging
2664 */
2665 if (test_bit(ID_A, &motg->inputs))
2666 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2667 else
2668 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002669 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302670 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2671 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2672 pr_debug("!a_vbus_vld\n");
2673 clear_bit(B_CONN, &motg->inputs);
2674 msm_otg_del_timer(motg);
2675 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002676 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2677 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302678 } else if (test_bit(ID_A, &motg->inputs)) {
2679 msm_hsusb_vbus_power(motg, 0);
2680 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2681 /*
2682 * If TA_WAIT_BCON is infinite, we don;t
2683 * turn off VBUS. Enter low power mode.
2684 */
2685 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002686 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302687 } else if (!test_bit(ID, &motg->inputs)) {
2688 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302689 }
2690 break;
2691 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302692 if ((test_bit(ID, &motg->inputs) &&
2693 !test_bit(ID_A, &motg->inputs)) ||
2694 test_bit(A_BUS_DROP, &motg->inputs)) {
2695 pr_debug("id_a/b/c || a_bus_drop\n");
2696 clear_bit(B_CONN, &motg->inputs);
2697 clear_bit(A_BUS_REQ, &motg->inputs);
2698 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002699 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302700 msm_otg_start_host(otg, 0);
2701 if (!test_bit(ID_A, &motg->inputs))
2702 msm_hsusb_vbus_power(motg, 0);
2703 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2704 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2705 pr_debug("!a_vbus_vld\n");
2706 clear_bit(B_CONN, &motg->inputs);
2707 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002708 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302709 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002710 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302711 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2712 /*
2713 * a_bus_req is de-asserted when root hub is
2714 * suspended or HNP is in progress.
2715 */
2716 pr_debug("!a_bus_req\n");
2717 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002718 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302719 if (otg->host->b_hnp_enable)
2720 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2721 A_AIDL_BDIS);
2722 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002723 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302724 } else if (!test_bit(B_CONN, &motg->inputs)) {
2725 pr_debug("!b_conn\n");
2726 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002727 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302728 if (TA_WAIT_BCON > 0)
2729 msm_otg_start_timer(motg, TA_WAIT_BCON,
2730 A_WAIT_BCON);
2731 if (msm_chg_check_aca_intr(motg))
2732 work = 1;
2733 } else if (test_bit(ID_A, &motg->inputs)) {
2734 msm_otg_del_timer(motg);
2735 msm_hsusb_vbus_power(motg, 0);
2736 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2737 msm_otg_notify_charger(motg,
2738 IDEV_ACA_CHG_MAX);
2739 else
2740 msm_otg_notify_charger(motg,
2741 IDEV_CHG_MIN - motg->mA_port);
2742 } else if (!test_bit(ID, &motg->inputs)) {
2743 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2744 motg->chg_type = USB_INVALID_CHARGER;
2745 msm_otg_notify_charger(motg, 0);
2746 msm_hsusb_vbus_power(motg, 1);
2747 }
2748 break;
2749 case OTG_STATE_A_SUSPEND:
2750 if ((test_bit(ID, &motg->inputs) &&
2751 !test_bit(ID_A, &motg->inputs)) ||
2752 test_bit(A_BUS_DROP, &motg->inputs) ||
2753 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2754 pr_debug("id_a/b/c || a_bus_drop ||"
2755 "a_aidl_bdis_tmout\n");
2756 msm_otg_del_timer(motg);
2757 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002758 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302759 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002760 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302761 if (!test_bit(ID_A, &motg->inputs))
2762 msm_hsusb_vbus_power(motg, 0);
2763 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2764 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2765 pr_debug("!a_vbus_vld\n");
2766 msm_otg_del_timer(motg);
2767 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002768 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302769 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002770 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302771 } else if (!test_bit(B_CONN, &motg->inputs) &&
2772 otg->host->b_hnp_enable) {
2773 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002774 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302775 msm_otg_host_hnp_enable(otg, 1);
2776 otg->gadget->is_a_peripheral = 1;
2777 msm_otg_start_peripheral(otg, 1);
2778 } else if (!test_bit(B_CONN, &motg->inputs) &&
2779 !otg->host->b_hnp_enable) {
2780 pr_debug("!b_conn && !b_hnp_enable");
2781 /*
2782 * bus request is dropped during suspend.
2783 * acquire again for next device.
2784 */
2785 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002786 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302787 if (TA_WAIT_BCON > 0)
2788 msm_otg_start_timer(motg, TA_WAIT_BCON,
2789 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002790 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302791 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002792 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302793 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002794 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002795 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302796 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302797 }
2798 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302799 case OTG_STATE_A_PERIPHERAL:
2800 if ((test_bit(ID, &motg->inputs) &&
2801 !test_bit(ID_A, &motg->inputs)) ||
2802 test_bit(A_BUS_DROP, &motg->inputs)) {
2803 pr_debug("id _f/b/c || a_bus_drop\n");
2804 /* Clear BIDL_ADIS timer */
2805 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002806 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302807 msm_otg_start_peripheral(otg, 0);
2808 otg->gadget->is_a_peripheral = 0;
2809 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002810 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302811 if (!test_bit(ID_A, &motg->inputs))
2812 msm_hsusb_vbus_power(motg, 0);
2813 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2814 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2815 pr_debug("!a_vbus_vld\n");
2816 /* Clear BIDL_ADIS timer */
2817 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002818 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302819 msm_otg_start_peripheral(otg, 0);
2820 otg->gadget->is_a_peripheral = 0;
2821 msm_otg_start_host(otg, 0);
2822 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2823 pr_debug("a_bidl_adis_tmout\n");
2824 msm_otg_start_peripheral(otg, 0);
2825 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002826 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302827 set_bit(A_BUS_REQ, &motg->inputs);
2828 msm_otg_host_hnp_enable(otg, 0);
2829 if (TA_WAIT_BCON > 0)
2830 msm_otg_start_timer(motg, TA_WAIT_BCON,
2831 A_WAIT_BCON);
2832 } else if (test_bit(ID_A, &motg->inputs)) {
2833 msm_hsusb_vbus_power(motg, 0);
2834 msm_otg_notify_charger(motg,
2835 IDEV_CHG_MIN - motg->mA_port);
2836 } else if (!test_bit(ID, &motg->inputs)) {
2837 msm_otg_notify_charger(motg, 0);
2838 msm_hsusb_vbus_power(motg, 1);
2839 }
2840 break;
2841 case OTG_STATE_A_WAIT_VFALL:
2842 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2843 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002844 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302845 work = 1;
2846 }
2847 break;
2848 case OTG_STATE_A_VBUS_ERR:
2849 if ((test_bit(ID, &motg->inputs) &&
2850 !test_bit(ID_A, &motg->inputs)) ||
2851 test_bit(A_BUS_DROP, &motg->inputs) ||
2852 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002853 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302854 if (!test_bit(ID_A, &motg->inputs))
2855 msm_hsusb_vbus_power(motg, 0);
2856 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2857 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2858 motg->chg_type = USB_INVALID_CHARGER;
2859 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302860 }
2861 break;
2862 default:
2863 break;
2864 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302865 if (work)
2866 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302867}
2868
2869static irqreturn_t msm_otg_irq(int irq, void *data)
2870{
2871 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002872 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302873 u32 otgsc = 0, usbsts, pc;
2874 bool work = 0;
2875 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302876
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302877 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002878 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302879 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002880 motg->async_int = irq;
Jack Phamb1aa4d82012-08-13 15:32:39 -07002881 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002882 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302883 return IRQ_HANDLED;
2884 }
2885
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002886 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302887 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302888
2889 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302890 return IRQ_NONE;
2891
2892 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302893 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302894 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302895 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302896 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302897 pr_debug("Id clear\n");
2898 /*
2899 * Assert a_bus_req to supply power on
2900 * VBUS when Micro/Mini-A cable is connected
2901 * with out user intervention.
2902 */
2903 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302904 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302905 msm_chg_enable_aca_det(motg);
2906 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302907 writel_relaxed(otgsc, USB_OTGSC);
2908 work = 1;
2909 } else if (otgsc & OTGSC_DPIS) {
2910 pr_debug("DPIS detected\n");
2911 writel_relaxed(otgsc, USB_OTGSC);
2912 set_bit(A_SRP_DET, &motg->inputs);
2913 set_bit(A_BUS_REQ, &motg->inputs);
2914 work = 1;
2915 } else if (otgsc & OTGSC_BSVIS) {
2916 writel_relaxed(otgsc, USB_OTGSC);
2917 /*
2918 * BSV interrupt comes when operating as an A-device
2919 * (VBUS on/off).
2920 * But, handle BSV when charger is removed from ACA in ID_A
2921 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002922 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302923 !test_bit(ID_A, &motg->inputs))
2924 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302925 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302926 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302927 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302928 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302929 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302930 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002931 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2932
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302933 msm_chg_check_aca_intr(motg);
2934 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302935 work = 1;
2936 } else if (usbsts & STS_PCI) {
2937 pc = readl_relaxed(USB_PORTSC);
2938 pr_debug("portsc = %x\n", pc);
2939 ret = IRQ_NONE;
2940 /*
2941 * HCD Acks PCI interrupt. We use this to switch
2942 * between different OTG states.
2943 */
2944 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002945 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302946 case OTG_STATE_A_SUSPEND:
2947 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2948 !(pc & PORTSC_CCS)) {
2949 pr_debug("B_CONN clear\n");
2950 clear_bit(B_CONN, &motg->inputs);
2951 msm_otg_del_timer(motg);
2952 }
2953 break;
2954 case OTG_STATE_A_PERIPHERAL:
2955 /*
2956 * A-peripheral observed activity on bus.
2957 * clear A_BIDL_ADIS timer.
2958 */
2959 msm_otg_del_timer(motg);
2960 work = 0;
2961 break;
2962 case OTG_STATE_B_WAIT_ACON:
2963 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2964 pr_debug("A_CONN set\n");
2965 set_bit(A_CONN, &motg->inputs);
2966 /* Clear ASE0_BRST timer */
2967 msm_otg_del_timer(motg);
2968 }
2969 break;
2970 case OTG_STATE_B_HOST:
2971 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2972 pr_debug("A_CONN clear\n");
2973 clear_bit(A_CONN, &motg->inputs);
2974 msm_otg_del_timer(motg);
2975 }
2976 break;
2977 case OTG_STATE_A_WAIT_BCON:
2978 if (TA_WAIT_BCON < 0)
2979 set_bit(A_BUS_REQ, &motg->inputs);
2980 default:
2981 work = 0;
2982 break;
2983 }
2984 } else if (usbsts & STS_URI) {
2985 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002986 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302987 case OTG_STATE_A_PERIPHERAL:
2988 /*
2989 * A-peripheral observed activity on bus.
2990 * clear A_BIDL_ADIS timer.
2991 */
2992 msm_otg_del_timer(motg);
2993 work = 0;
2994 break;
2995 default:
2996 work = 0;
2997 break;
2998 }
2999 } else if (usbsts & STS_SLI) {
3000 ret = IRQ_NONE;
3001 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003002 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303003 case OTG_STATE_B_PERIPHERAL:
3004 if (otg->gadget->b_hnp_enable) {
3005 set_bit(A_BUS_SUSPEND, &motg->inputs);
3006 set_bit(B_BUS_REQ, &motg->inputs);
3007 work = 1;
3008 }
3009 break;
3010 case OTG_STATE_A_PERIPHERAL:
3011 msm_otg_start_timer(motg, TA_BIDL_ADIS,
3012 A_BIDL_ADIS);
3013 break;
3014 default:
3015 break;
3016 }
3017 } else if ((usbsts & PHY_ALT_INT)) {
3018 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3019 if (msm_chg_check_aca_intr(motg))
3020 work = 1;
3021 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303022 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303023 if (work)
3024 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303025
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303026 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003027}
3028
3029static void msm_otg_set_vbus_state(int online)
3030{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303031 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003032 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303033 struct usb_otg *otg = motg->phy.otg;
3034
3035 /* In A Host Mode, ignore received BSV interrupts */
3036 if (otg->phy->state >= OTG_STATE_A_IDLE)
3037 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003038
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303039 if (online) {
3040 pr_debug("PMIC: BSV set\n");
3041 set_bit(B_SESS_VLD, &motg->inputs);
3042 } else {
3043 pr_debug("PMIC: BSV clear\n");
3044 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303045 }
3046
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303047 if (!init) {
3048 init = true;
3049 complete(&pmic_vbus_init);
3050 pr_debug("PMIC: BSV init complete\n");
3051 return;
3052 }
3053
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303054 if (test_bit(MHL, &motg->inputs) ||
3055 mhl_det_in_progress) {
3056 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3057 return;
3058 }
3059
Jack Pham5ca279b2012-05-14 18:42:54 -07003060 if (atomic_read(&motg->pm_suspended))
3061 motg->sm_work_pending = true;
3062 else
3063 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003064}
3065
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303066static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003067{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303068 struct msm_otg *motg = container_of(w, struct msm_otg,
3069 pmic_id_status_work.work);
3070 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003071
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05303072 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303073 if (!test_and_set_bit(ID, &motg->inputs)) {
3074 pr_debug("PMIC: ID set\n");
3075 work = 1;
3076 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303077 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303078 if (test_and_clear_bit(ID, &motg->inputs)) {
3079 pr_debug("PMIC: ID clear\n");
3080 set_bit(A_BUS_REQ, &motg->inputs);
3081 work = 1;
3082 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303083 }
3084
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303085 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003086 if (atomic_read(&motg->pm_suspended))
3087 motg->sm_work_pending = true;
3088 else
3089 queue_work(system_nrt_wq, &motg->sm_work);
3090 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303091
3092}
3093
3094#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3095static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3096{
3097 struct msm_otg *motg = data;
3098
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303099 if (test_bit(MHL, &motg->inputs) ||
3100 mhl_det_in_progress) {
3101 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3102 return IRQ_HANDLED;
3103 }
3104
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303105 if (!aca_id_turned_on)
3106 /*schedule delayed work for 5msec for ID line state to settle*/
3107 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3108 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003109
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303110 return IRQ_HANDLED;
3111}
3112
3113static int msm_otg_mode_show(struct seq_file *s, void *unused)
3114{
3115 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003116 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303117
Steve Mucklef132c6c2012-06-06 18:30:57 -07003118 switch (phy->state) {
ChandanaKishori Chiluveruf19104d2014-07-22 12:41:52 +05303119 case OTG_STATE_A_WAIT_BCON:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303120 case OTG_STATE_A_HOST:
ChandanaKishori Chiluveruf19104d2014-07-22 12:41:52 +05303121 case OTG_STATE_A_SUSPEND:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303122 seq_printf(s, "host\n");
3123 break;
ChandanaKishori Chiluveruf19104d2014-07-22 12:41:52 +05303124 case OTG_STATE_B_IDLE:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303125 case OTG_STATE_B_PERIPHERAL:
3126 seq_printf(s, "peripheral\n");
3127 break;
3128 default:
3129 seq_printf(s, "none\n");
3130 break;
3131 }
3132
3133 return 0;
3134}
3135
3136static int msm_otg_mode_open(struct inode *inode, struct file *file)
3137{
3138 return single_open(file, msm_otg_mode_show, inode->i_private);
3139}
3140
3141static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3142 size_t count, loff_t *ppos)
3143{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303144 struct seq_file *s = file->private_data;
3145 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303146 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003147 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303148 int status = count;
3149 enum usb_mode_type req_mode;
3150
3151 memset(buf, 0x00, sizeof(buf));
3152
3153 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3154 status = -EFAULT;
3155 goto out;
3156 }
3157
3158 if (!strncmp(buf, "host", 4)) {
3159 req_mode = USB_HOST;
3160 } else if (!strncmp(buf, "peripheral", 10)) {
3161 req_mode = USB_PERIPHERAL;
3162 } else if (!strncmp(buf, "none", 4)) {
3163 req_mode = USB_NONE;
3164 } else {
3165 status = -EINVAL;
3166 goto out;
3167 }
3168
3169 switch (req_mode) {
3170 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003171 switch (phy->state) {
ChandanaKishori Chiluveruf19104d2014-07-22 12:41:52 +05303172 case OTG_STATE_A_WAIT_BCON:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303173 case OTG_STATE_A_HOST:
ChandanaKishori Chiluveruf19104d2014-07-22 12:41:52 +05303174 case OTG_STATE_A_SUSPEND:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303175 case OTG_STATE_B_PERIPHERAL:
3176 set_bit(ID, &motg->inputs);
3177 clear_bit(B_SESS_VLD, &motg->inputs);
3178 break;
3179 default:
3180 goto out;
3181 }
3182 break;
3183 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003184 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303185 case OTG_STATE_B_IDLE:
ChandanaKishori Chiluveruf19104d2014-07-22 12:41:52 +05303186 case OTG_STATE_A_WAIT_BCON:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303187 case OTG_STATE_A_HOST:
ChandanaKishori Chiluveruf19104d2014-07-22 12:41:52 +05303188 case OTG_STATE_A_SUSPEND:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303189 set_bit(ID, &motg->inputs);
3190 set_bit(B_SESS_VLD, &motg->inputs);
3191 break;
3192 default:
3193 goto out;
3194 }
3195 break;
3196 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003197 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303198 case OTG_STATE_B_IDLE:
3199 case OTG_STATE_B_PERIPHERAL:
3200 clear_bit(ID, &motg->inputs);
3201 break;
3202 default:
3203 goto out;
3204 }
3205 break;
3206 default:
3207 goto out;
3208 }
3209
Steve Mucklef132c6c2012-06-06 18:30:57 -07003210 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303211 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303212out:
3213 return status;
3214}
3215
3216const struct file_operations msm_otg_mode_fops = {
3217 .open = msm_otg_mode_open,
3218 .read = seq_read,
3219 .write = msm_otg_mode_write,
3220 .llseek = seq_lseek,
3221 .release = single_release,
3222};
3223
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303224static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3225{
3226 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003227 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303228
Steve Mucklef132c6c2012-06-06 18:30:57 -07003229 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303230 return 0;
3231}
3232
3233static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3234{
3235 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3236}
3237
3238const struct file_operations msm_otg_state_fops = {
3239 .open = msm_otg_otg_state_open,
3240 .read = seq_read,
3241 .llseek = seq_lseek,
3242 .release = single_release,
3243};
3244
Anji jonnalad270e2d2011-08-09 11:28:32 +05303245static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3246{
3247 struct msm_otg *motg = s->private;
3248
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303249 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303250 return 0;
3251}
3252
3253static int msm_otg_chg_open(struct inode *inode, struct file *file)
3254{
3255 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3256}
3257
3258const struct file_operations msm_otg_chg_fops = {
3259 .open = msm_otg_chg_open,
3260 .read = seq_read,
3261 .llseek = seq_lseek,
3262 .release = single_release,
3263};
3264
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303265static int msm_otg_aca_show(struct seq_file *s, void *unused)
3266{
3267 if (debug_aca_enabled)
3268 seq_printf(s, "enabled\n");
3269 else
3270 seq_printf(s, "disabled\n");
3271
3272 return 0;
3273}
3274
3275static int msm_otg_aca_open(struct inode *inode, struct file *file)
3276{
3277 return single_open(file, msm_otg_aca_show, inode->i_private);
3278}
3279
3280static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3281 size_t count, loff_t *ppos)
3282{
3283 char buf[8];
3284
3285 memset(buf, 0x00, sizeof(buf));
3286
3287 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3288 return -EFAULT;
3289
3290 if (!strncmp(buf, "enable", 6))
3291 debug_aca_enabled = true;
3292 else
3293 debug_aca_enabled = false;
3294
3295 return count;
3296}
3297
3298const struct file_operations msm_otg_aca_fops = {
3299 .open = msm_otg_aca_open,
3300 .read = seq_read,
3301 .write = msm_otg_aca_write,
3302 .llseek = seq_lseek,
3303 .release = single_release,
3304};
3305
Manu Gautam8bdcc592012-03-06 11:26:06 +05303306static int msm_otg_bus_show(struct seq_file *s, void *unused)
3307{
3308 if (debug_bus_voting_enabled)
3309 seq_printf(s, "enabled\n");
3310 else
3311 seq_printf(s, "disabled\n");
3312
3313 return 0;
3314}
3315
3316static int msm_otg_bus_open(struct inode *inode, struct file *file)
3317{
3318 return single_open(file, msm_otg_bus_show, inode->i_private);
3319}
3320
3321static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3322 size_t count, loff_t *ppos)
3323{
3324 char buf[8];
3325 int ret;
3326 struct seq_file *s = file->private_data;
3327 struct msm_otg *motg = s->private;
3328
3329 memset(buf, 0x00, sizeof(buf));
3330
3331 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3332 return -EFAULT;
3333
3334 if (!strncmp(buf, "enable", 6)) {
3335 /* Do not vote here. Let OTG statemachine decide when to vote */
3336 debug_bus_voting_enabled = true;
3337 } else {
3338 debug_bus_voting_enabled = false;
3339 if (motg->bus_perf_client) {
3340 ret = msm_bus_scale_client_update_request(
3341 motg->bus_perf_client, 0);
3342 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003343 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303344 "for bus bw %d\n", __func__, ret);
3345 }
3346 }
3347
3348 return count;
3349}
3350
3351const struct file_operations msm_otg_bus_fops = {
3352 .open = msm_otg_bus_open,
3353 .read = seq_read,
3354 .write = msm_otg_bus_write,
3355 .llseek = seq_lseek,
3356 .release = single_release,
3357};
3358
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303359static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303360
3361static int msm_otg_debugfs_init(struct msm_otg *motg)
3362{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303363 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303364
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303365 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3366
3367 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3368 return -ENODEV;
3369
Anji jonnalad270e2d2011-08-09 11:28:32 +05303370 if (motg->pdata->mode == USB_OTG &&
3371 motg->pdata->otg_control == OTG_USER_CONTROL) {
3372
Manu Gautam8bdcc592012-03-06 11:26:06 +05303373 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303374 S_IWUSR, msm_otg_dbg_root, motg,
3375 &msm_otg_mode_fops);
3376
Manu Gautam8bdcc592012-03-06 11:26:06 +05303377 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303378 debugfs_remove(msm_otg_dbg_root);
3379 msm_otg_dbg_root = NULL;
3380 return -ENODEV;
3381 }
3382 }
3383
Manu Gautam8bdcc592012-03-06 11:26:06 +05303384 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303385 msm_otg_dbg_root, motg,
3386 &msm_otg_chg_fops);
3387
Manu Gautam8bdcc592012-03-06 11:26:06 +05303388 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303389 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303390 return -ENODEV;
3391 }
3392
Manu Gautam8bdcc592012-03-06 11:26:06 +05303393 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303394 msm_otg_dbg_root, motg,
3395 &msm_otg_aca_fops);
3396
Manu Gautam8bdcc592012-03-06 11:26:06 +05303397 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303398 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303399 return -ENODEV;
3400 }
3401
Manu Gautam8bdcc592012-03-06 11:26:06 +05303402 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3403 msm_otg_dbg_root, motg,
3404 &msm_otg_bus_fops);
3405
3406 if (!msm_otg_dentry) {
3407 debugfs_remove_recursive(msm_otg_dbg_root);
3408 return -ENODEV;
3409 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303410
3411 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3412 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3413
3414 if (!msm_otg_dentry) {
3415 debugfs_remove_recursive(msm_otg_dbg_root);
3416 return -ENODEV;
3417 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303418 return 0;
3419}
3420
3421static void msm_otg_debugfs_cleanup(void)
3422{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303423 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303424}
3425
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303426static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3427static struct platform_device *msm_otg_add_pdev(
3428 struct platform_device *ofdev, const char *name)
3429{
3430 struct platform_device *pdev;
3431 const struct resource *res = ofdev->resource;
3432 unsigned int num = ofdev->num_resources;
3433 int retval;
3434
3435 pdev = platform_device_alloc(name, -1);
3436 if (!pdev) {
3437 retval = -ENOMEM;
3438 goto error;
3439 }
3440
3441 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3442 pdev->dev.dma_mask = &msm_otg_dma_mask;
3443
3444 if (num) {
3445 retval = platform_device_add_resources(pdev, res, num);
3446 if (retval)
3447 goto error;
3448 }
3449
3450 retval = platform_device_add(pdev);
3451 if (retval)
3452 goto error;
3453
3454 return pdev;
3455
3456error:
3457 platform_device_put(pdev);
3458 return ERR_PTR(retval);
3459}
3460
3461static int msm_otg_setup_devices(struct platform_device *ofdev,
3462 enum usb_mode_type mode, bool init)
3463{
3464 const char *gadget_name = "msm_hsusb";
3465 const char *host_name = "msm_hsusb_host";
3466 static struct platform_device *gadget_pdev;
3467 static struct platform_device *host_pdev;
3468 int retval = 0;
3469
3470 if (!init) {
3471 if (gadget_pdev)
3472 platform_device_unregister(gadget_pdev);
3473 if (host_pdev)
3474 platform_device_unregister(host_pdev);
3475 return 0;
3476 }
3477
3478 switch (mode) {
3479 case USB_OTG:
3480 /* fall through */
3481 case USB_PERIPHERAL:
3482 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3483 if (IS_ERR(gadget_pdev)) {
3484 retval = PTR_ERR(gadget_pdev);
3485 break;
3486 }
3487 if (mode == USB_PERIPHERAL)
3488 break;
3489 /* fall through */
3490 case USB_HOST:
3491 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3492 if (IS_ERR(host_pdev)) {
3493 retval = PTR_ERR(host_pdev);
3494 if (mode == USB_OTG)
3495 platform_device_unregister(gadget_pdev);
3496 }
3497 break;
3498 default:
3499 break;
3500 }
3501
3502 return retval;
3503}
3504
3505struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3506{
3507 struct device_node *node = pdev->dev.of_node;
3508 struct msm_otg_platform_data *pdata;
3509 int len = 0;
3510
3511 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3512 if (!pdata) {
3513 pr_err("unable to allocate platform data\n");
3514 return NULL;
3515 }
3516 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3517 if (len) {
3518 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3519 if (!pdata->phy_init_seq)
3520 return NULL;
3521 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3522 pdata->phy_init_seq,
3523 len/sizeof(*pdata->phy_init_seq));
3524 }
3525 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3526 &pdata->power_budget);
3527 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3528 &pdata->mode);
3529 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3530 &pdata->otg_control);
3531 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3532 &pdata->default_mode);
3533 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3534 &pdata->phy_type);
3535 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3536 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303537 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3538 "qcom,hsusb-otg-disable-reset");
3539
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303540 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303541}
3542
3543static int __init msm_otg_probe(struct platform_device *pdev)
3544{
3545 int ret = 0;
3546 struct resource *res;
3547 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003548 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303549 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303550
3551 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303552
3553 if (pdev->dev.of_node) {
3554 dev_dbg(&pdev->dev, "device tree enabled\n");
3555 pdata = msm_otg_dt_to_pdata(pdev);
3556 if (!pdata)
3557 return -ENOMEM;
3558 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3559 if (ret) {
3560 dev_err(&pdev->dev, "devices setup failed\n");
3561 return ret;
3562 }
3563 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303564 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3565 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303566 } else {
3567 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303568 }
3569
3570 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3571 if (!motg) {
3572 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3573 return -ENOMEM;
3574 }
3575
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003576 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3577 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003578 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3579 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303580 goto free_motg;
3581 }
3582
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003583 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303584 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003585 phy = &motg->phy;
3586 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303587
3588 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303589 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3590 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3591 * PMIC can detect ACA ID_GND and generate an interrupt.
3592 */
3593 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3594 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3595 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003596 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303597 }
3598
Ofir Cohen4da266f2012-01-03 10:19:29 +02003599 /* initialize reset counter */
3600 motg->reset_counter = 0;
3601
Amit Blay02eff132011-09-21 16:46:24 +03003602 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003603 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003604 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003605 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303606
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303607 /*
3608 * Targets on which link uses asynchronous reset methodology,
3609 * free running clock is not required during the reset.
3610 */
Manu Gautam5143b252012-01-05 19:25:23 -08003611 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303612 if (IS_ERR(motg->clk))
3613 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3614 else
3615 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303616
3617 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003618 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303619 * CORE CLK must be running at >55Mhz for correct HSUSB
3620 * operation and USB core cannot tolerate frequency changes on
3621 * CORE CLK. For such USB cores, vote for maximum clk frequency
3622 * on pclk source
3623 */
Manu Gautam5143b252012-01-05 19:25:23 -08003624 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3625 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303626 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003627 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303628 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003629 goto put_clk;
3630 }
3631 clk_set_rate(motg->core_clk, INT_MAX);
3632
3633 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3634 if (IS_ERR(motg->pclk)) {
3635 dev_err(&pdev->dev, "failed to get iface_clk\n");
3636 ret = PTR_ERR(motg->pclk);
3637 goto put_core_clk;
3638 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303639
3640 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3641 if (!res) {
3642 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3643 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003644 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303645 }
3646
3647 motg->regs = ioremap(res->start, resource_size(res));
3648 if (!motg->regs) {
3649 dev_err(&pdev->dev, "ioremap failed\n");
3650 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003651 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303652 }
3653 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3654
3655 motg->irq = platform_get_irq(pdev, 0);
3656 if (!motg->irq) {
3657 dev_err(&pdev->dev, "platform_get_irq failed\n");
3658 ret = -ENODEV;
3659 goto free_regs;
3660 }
3661
Manu Gautamf8c45642012-08-10 10:20:56 -07003662 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3663 if (motg->async_irq < 0) {
3664 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3665 motg->async_irq = 0;
3666 }
3667
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003668 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003669 if (IS_ERR(motg->xo_handle)) {
3670 dev_err(&pdev->dev, "%s not able to get the handle "
3671 "to vote for TCXO D0 buffer\n", __func__);
3672 ret = PTR_ERR(motg->xo_handle);
3673 goto free_regs;
3674 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303675
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003676 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003677 if (ret) {
3678 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3679 "D0 buffer%d\n", __func__, ret);
3680 goto free_xo_handle;
3681 }
3682
Manu Gautam28b1bac2012-01-30 16:43:06 +05303683 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303684
Mayank Rana248698c2012-04-19 00:03:16 +05303685 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003686 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303687 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003688 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303689 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003690 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003691 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303692 goto devote_xo_handle;
3693 }
3694 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303695 }
3696
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003697 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303698 if (ret) {
3699 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303700 goto devote_xo_handle;
3701 }
3702
3703 ret = regulator_enable(hsusb_vddcx);
3704 if (ret) {
3705 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3706 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303707 }
3708
3709 ret = msm_hsusb_ldo_init(motg, 1);
3710 if (ret) {
3711 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303712 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303713 }
3714
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303715 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303716 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3717 "mhl_usb_hs_switch");
3718 if (IS_ERR(mhl_usb_hs_switch)) {
3719 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003720 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303721 goto free_ldo_init;
3722 }
3723 }
3724
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003725 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303726 if (ret) {
3727 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003728 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303729 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303730 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303731
3732 writel(0, USB_USBINTR);
3733 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003734 /* Ensure that above STOREs are completed before enabling interrupts */
3735 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303736
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303737 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3738 if (ret)
3739 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003740 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303741 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303742 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303743 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303744 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
kibum.leeec040442012-08-22 22:29:46 +09003745 INIT_DELAYED_WORK(&motg->check_ta_work, msm_ta_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303746 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3747 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303748 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3749 "msm_otg", motg);
3750 if (ret) {
3751 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003752 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303753 }
3754
Manu Gautamf8c45642012-08-10 10:20:56 -07003755 if (motg->async_irq) {
3756 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3757 "msm_otg", motg);
3758 if (ret) {
3759 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3760 goto free_irq;
3761 }
3762 disable_irq(motg->async_irq);
3763 }
3764
Jack Pham87f202f2012-08-06 00:24:22 -07003765 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3766 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3767
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003768 phy->init = msm_otg_reset;
3769 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003770 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303771
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003772 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303773
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003774 phy->otg->phy = &motg->phy;
3775 phy->otg->set_host = msm_otg_set_host;
3776 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003777 phy->otg->start_hnp = msm_otg_start_hnp;
3778 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003779
3780 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303781 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003782 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003783 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303784 }
3785
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303786 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303787 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003788 if (motg->pdata->pmic_id_irq) {
3789 ret = request_irq(motg->pdata->pmic_id_irq,
3790 msm_pmic_id_irq,
3791 IRQF_TRIGGER_RISING |
3792 IRQF_TRIGGER_FALLING,
3793 "msm_otg", motg);
3794 if (ret) {
3795 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003796 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003797 }
3798 } else {
3799 ret = -ENODEV;
3800 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003801 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003802 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303803 }
3804
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303805 msm_hsusb_mhl_switch_enable(motg, 1);
3806
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303807 platform_set_drvdata(pdev, motg);
3808 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003809 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303810
Anji jonnalad270e2d2011-08-09 11:28:32 +05303811 ret = msm_otg_debugfs_init(motg);
3812 if (ret)
3813 dev_dbg(&pdev->dev, "mode debugfs file is"
3814 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303815
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003816 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3817 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3818
Amit Blay58b31472011-11-18 09:39:39 +02003819 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3820 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303821 (!(motg->pdata->mode == USB_OTG) ||
3822 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003823 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303824 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003825
Amit Blay58b31472011-11-18 09:39:39 +02003826 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3827 motg->caps = ALLOW_PHY_RETENTION;
3828 }
3829
Amit Blay6fa647a2012-05-24 14:12:08 +03003830 if (motg->pdata->enable_lpm_on_dev_suspend)
3831 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3832
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003833 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303834 pm_runtime_set_active(&pdev->dev);
3835 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303836
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303837 if (motg->pdata->bus_scale_table) {
3838 motg->bus_perf_client =
3839 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3840 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003841 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303842 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303843 else
3844 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303845 }
3846
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303847 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003848
Steve Mucklef132c6c2012-06-06 18:30:57 -07003849remove_phy:
3850 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003851free_async_irq:
3852 if (motg->async_irq)
3853 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303854free_irq:
3855 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003856destroy_wlock:
3857 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303858 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003859 msm_hsusb_ldo_enable(motg, 0);
3860free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303861 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303862free_hsusb_vddcx:
3863 regulator_disable(hsusb_vddcx);
3864free_config_vddcx:
3865 regulator_set_voltage(hsusb_vddcx,
3866 vdd_val[motg->vdd_type][VDD_NONE],
3867 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003868devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303869 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003870 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003871free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003872 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303873free_regs:
3874 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003875put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303876 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303877put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003878 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303879put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303880 if (!IS_ERR(motg->clk))
3881 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003882 if (!IS_ERR(motg->phy_reset_clk))
3883 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003884free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003885 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303886free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303887 kfree(motg);
3888 return ret;
3889}
3890
3891static int __devexit msm_otg_remove(struct platform_device *pdev)
3892{
3893 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003894 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303895 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303896
3897 if (otg->host || otg->gadget)
3898 return -EBUSY;
3899
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303900 if (pdev->dev.of_node)
3901 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003902 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3903 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303904 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303905 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303906 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303907 cancel_delayed_work_sync(&motg->pmic_id_status_work);
kibum.leeec040442012-08-22 22:29:46 +09003908 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303909 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303910
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303911 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303912
3913 device_init_wakeup(&pdev->dev, 0);
3914 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003915 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303916
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303917 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003918 if (motg->pdata->pmic_id_irq)
3919 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003920 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303921 free_irq(motg->irq, motg);
3922
Jack Pham87f202f2012-08-06 00:24:22 -07003923 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3924 motg->pdata->mpm_otgsessvld_int)
3925 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3926
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303927 /*
3928 * Put PHY in low power mode.
3929 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003930 ulpi_read(otg->phy, 0x14);
3931 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303932
3933 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3934 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3935 if (readl(USB_PORTSC) & PORTSC_PHCD)
3936 break;
3937 udelay(1);
3938 cnt++;
3939 }
3940 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003941 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303942
Manu Gautam28b1bac2012-01-30 16:43:06 +05303943 clk_disable_unprepare(motg->pclk);
3944 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003945 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003946 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303947 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303948 regulator_disable(hsusb_vddcx);
3949 regulator_set_voltage(hsusb_vddcx,
3950 vdd_val[motg->vdd_type][VDD_NONE],
3951 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303952
3953 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303954 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303955
Amit Blay02eff132011-09-21 16:46:24 +03003956 if (!IS_ERR(motg->phy_reset_clk))
3957 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303958 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303959 if (!IS_ERR(motg->clk))
3960 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003961 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303962
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303963 if (motg->bus_perf_client)
3964 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303965
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003966 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303967 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303968 return 0;
3969}
3970
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303971#ifdef CONFIG_PM_RUNTIME
3972static int msm_otg_runtime_idle(struct device *dev)
3973{
3974 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003975 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303976
3977 dev_dbg(dev, "OTG runtime idle\n");
3978
Steve Mucklef132c6c2012-06-06 18:30:57 -07003979 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303980 return -EAGAIN;
3981 else
3982 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303983}
3984
3985static int msm_otg_runtime_suspend(struct device *dev)
3986{
3987 struct msm_otg *motg = dev_get_drvdata(dev);
3988
3989 dev_dbg(dev, "OTG runtime suspend\n");
3990 return msm_otg_suspend(motg);
3991}
3992
3993static int msm_otg_runtime_resume(struct device *dev)
3994{
3995 struct msm_otg *motg = dev_get_drvdata(dev);
3996
3997 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303998 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303999 return msm_otg_resume(motg);
4000}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304001#endif
4002
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304003#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304004static int msm_otg_pm_suspend(struct device *dev)
4005{
Jack Pham5ca279b2012-05-14 18:42:54 -07004006 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304007 struct msm_otg *motg = dev_get_drvdata(dev);
4008
4009 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07004010
4011 atomic_set(&motg->pm_suspended, 1);
4012 ret = msm_otg_suspend(motg);
4013 if (ret)
4014 atomic_set(&motg->pm_suspended, 0);
4015
4016 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304017}
4018
4019static int msm_otg_pm_resume(struct device *dev)
4020{
Jack Pham5ca279b2012-05-14 18:42:54 -07004021 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304022 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304023
4024 dev_dbg(dev, "OTG PM resume\n");
4025
Jack Pham5ca279b2012-05-14 18:42:54 -07004026 atomic_set(&motg->pm_suspended, 0);
Jack Phamb1aa4d82012-08-13 15:32:39 -07004027 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004028 pm_runtime_get_noresume(dev);
4029 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304030
Jack Pham5ca279b2012-05-14 18:42:54 -07004031 /* Update runtime PM status */
4032 pm_runtime_disable(dev);
4033 pm_runtime_set_active(dev);
4034 pm_runtime_enable(dev);
4035
Jack Phamb1aa4d82012-08-13 15:32:39 -07004036 if (motg->sm_work_pending) {
4037 motg->sm_work_pending = false;
4038 queue_work(system_nrt_wq, &motg->sm_work);
4039 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004040 }
4041
4042 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304043}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304044#endif
4045
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304046#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304047static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304048 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4049 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4050 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304051};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304052#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304053
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304054static struct of_device_id msm_otg_dt_match[] = {
4055 { .compatible = "qcom,hsusb-otg",
4056 },
4057 {}
4058};
4059
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304060static struct platform_driver msm_otg_driver = {
4061 .remove = __devexit_p(msm_otg_remove),
4062 .driver = {
4063 .name = DRIVER_NAME,
4064 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304065#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304066 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304067#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304068 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304069 },
4070};
4071
4072static int __init msm_otg_init(void)
4073{
4074 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4075}
4076
4077static void __exit msm_otg_exit(void)
4078{
4079 platform_driver_unregister(&msm_otg_driver);
4080}
4081
4082module_init(msm_otg_init);
4083module_exit(msm_otg_exit);
4084
4085MODULE_LICENSE("GPL v2");
4086MODULE_DESCRIPTION("MSM USB transceiver driver");