blob: 556ae99487f8974662eab0f297aeab76aa4b7d51 [file] [log] [blame]
Pavankumar Kondeti39504502013-01-04 12:28:10 +05301/* Copyright (c) 2009-2013, Linux Foundation. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020042#include <linux/power_supply.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053043#include <linux/mhl_8334.h>
jh.koo78df86f2012-09-04 20:13:19 +090044#include <linux/slimport.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045
Devin Kimd10f03c2012-06-25 17:05:13 -070046#include <asm/mach-types.h>
47
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053048#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070049#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080050#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053051#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053052#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053053
54#define MSM_USB_BASE (motg->regs)
55#define DRIVER_NAME "msm_otg"
56
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053057#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053058#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053059#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
60#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
61#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
62#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
63
64#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
65#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
66#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
67#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
68
Mayank Rana248698c2012-04-19 00:03:16 +053069#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080070#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053071#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
72
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053073static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053075static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053076static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053077static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070078
Anji jonnala11aa5c42011-05-04 10:19:48 +053079static struct regulator *hsusb_3p3;
80static struct regulator *hsusb_1p8;
81static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053082static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +053083static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053084static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053085
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053086static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053087static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +053088{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053089#ifdef CONFIG_USB_MSM_ACA
90 return true;
91#else
92 return debug_aca_enabled;
93#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +053094}
95
Mayank Rana248698c2012-04-19 00:03:16 +053096static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
97 { /* VDD_CX CORNER Voting */
98 [VDD_NONE] = RPM_VREG_CORNER_NONE,
99 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
100 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
101 },
102 { /* VDD_CX Voltage Voting */
103 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
104 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
105 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
106 },
107};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530108
109static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
110{
111 int rc = 0;
112
113 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700114 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530115 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200116 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530117 return PTR_ERR(hsusb_3p3);
118 }
119
120 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
121 USB_PHY_3P3_VOL_MAX);
122 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700123 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530125 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700127 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200129 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530130 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 }
133 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
134 USB_PHY_1P8_VOL_MAX);
135 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700136 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138 goto put_1p8;
139 }
140
141 return 0;
142 }
143
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146put_3p3_lpm:
147 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148 return rc;
149}
150
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530151static int msm_hsusb_config_vddcx(int high)
152{
Mayank Rana248698c2012-04-19 00:03:16 +0530153 struct msm_otg *motg = the_msm_otg;
154 enum usb_vdd_type vdd_type = motg->vdd_type;
155 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530156 int min_vol;
157 int ret;
158
Mayank Rana248698c2012-04-19 00:03:16 +0530159 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530160 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
161 if (ret) {
162 pr_err("%s: unable to set the voltage for regulator "
163 "HSUSB_VDDCX\n", __func__);
164 return ret;
165 }
166
167 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
168
169 return ret;
170}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530171
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530173{
174 int ret = 0;
175
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530176 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530177 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
178 return -ENODEV;
179 }
180
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530181 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530182 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
183 return -ENODEV;
184 }
185
186 if (on) {
187 ret = regulator_set_optimum_mode(hsusb_1p8,
188 USB_PHY_1P8_HPM_LOAD);
189 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530191 "HSUSB_1p8\n", __func__);
192 return ret;
193 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195 ret = regulator_enable(hsusb_1p8);
196 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700197 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198 __func__);
199 regulator_set_optimum_mode(hsusb_1p8, 0);
200 return ret;
201 }
202
Anji jonnala11aa5c42011-05-04 10:19:48 +0530203 ret = regulator_set_optimum_mode(hsusb_3p3,
204 USB_PHY_3P3_HPM_LOAD);
205 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208 regulator_set_optimum_mode(hsusb_1p8, 0);
209 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530210 return ret;
211 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212
213 ret = regulator_enable(hsusb_3p3);
214 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700215 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 __func__);
217 regulator_set_optimum_mode(hsusb_3p3, 0);
218 regulator_set_optimum_mode(hsusb_1p8, 0);
219 regulator_disable(hsusb_1p8);
220 return ret;
221 }
222
Anji jonnala11aa5c42011-05-04 10:19:48 +0530223 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 ret = regulator_disable(hsusb_1p8);
225 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700226 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 __func__);
228 return ret;
229 }
230
231 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530234 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235
236 ret = regulator_disable(hsusb_3p3);
237 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700238 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239 __func__);
240 return ret;
241 }
242 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530243 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700244 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530245 "HSUSB_3p3\n", __func__);
246 }
247
248 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
249 return ret < 0 ? ret : 0;
250}
251
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530252static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
253{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530254 struct msm_otg_platform_data *pdata = motg->pdata;
255
256 if (!pdata->mhl_enable)
257 return;
258
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530259 if (!mhl_usb_hs_switch) {
260 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530261 return;
262 }
263
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530264 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530265 if (regulator_enable(mhl_usb_hs_switch))
266 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530267 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530268 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530269 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530270}
271
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200272static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530273{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200274 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530275 int cnt = 0;
276
277 /* initiate read operation */
278 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
279 USB_ULPI_VIEWPORT);
280
281 /* wait for completion */
282 while (cnt < ULPI_IO_TIMEOUT_USEC) {
283 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
284 break;
285 udelay(1);
286 cnt++;
287 }
288
289 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200290 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530291 readl(USB_ULPI_VIEWPORT));
292 return -ETIMEDOUT;
293 }
294 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
295}
296
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200297static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530298{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200299 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530300 int cnt = 0;
301
302 /* initiate write operation */
303 writel(ULPI_RUN | ULPI_WRITE |
304 ULPI_ADDR(reg) | ULPI_DATA(val),
305 USB_ULPI_VIEWPORT);
306
307 /* wait for completion */
308 while (cnt < ULPI_IO_TIMEOUT_USEC) {
309 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
310 break;
311 udelay(1);
312 cnt++;
313 }
314
315 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200316 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530317 return -ETIMEDOUT;
318 }
319 return 0;
320}
321
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200322static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530323 .read = ulpi_read,
324 .write = ulpi_write,
325};
326
327static void ulpi_init(struct msm_otg *motg)
328{
329 struct msm_otg_platform_data *pdata = motg->pdata;
330 int *seq = pdata->phy_init_seq;
331
332 if (!seq)
333 return;
334
335 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200336 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530337 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200338 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530339 seq += 2;
340 }
341}
342
343static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
344{
345 int ret;
346
347 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530348 if (!IS_ERR(motg->clk)) {
349 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
350 } else {
351 /* Using asynchronous block reset to the hardware */
352 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
353 clk_disable_unprepare(motg->pclk);
354 clk_disable_unprepare(motg->core_clk);
355 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
356 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530357 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200358 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530359 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530360 if (!IS_ERR(motg->clk)) {
361 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
362 } else {
363 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
364 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
365 ndelay(200);
366 clk_prepare_enable(motg->core_clk);
367 clk_prepare_enable(motg->pclk);
368 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530369 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200370 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530371 }
372 return ret;
373}
374
375static int msm_otg_phy_clk_reset(struct msm_otg *motg)
376{
377 int ret;
378
Amit Blay02eff132011-09-21 16:46:24 +0300379 if (IS_ERR(motg->phy_reset_clk))
380 return 0;
381
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530382 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
383 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200384 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530385 return ret;
386 }
387 usleep_range(10000, 12000);
388 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
389 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200390 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530391 return ret;
392}
393
394static int msm_otg_phy_reset(struct msm_otg *motg)
395{
396 u32 val;
397 int ret;
398 int retries;
399
400 ret = msm_otg_link_clk_reset(motg, 1);
401 if (ret)
402 return ret;
403 ret = msm_otg_phy_clk_reset(motg);
404 if (ret)
405 return ret;
406 ret = msm_otg_link_clk_reset(motg, 0);
407 if (ret)
408 return ret;
409
410 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
411 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
412
413 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200414 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530415 ULPI_CLR(ULPI_FUNC_CTRL));
416 if (!ret)
417 break;
418 ret = msm_otg_phy_clk_reset(motg);
419 if (ret)
420 return ret;
421 }
422 if (!retries)
423 return -ETIMEDOUT;
424
425 /* This reset calibrates the phy, if the above write succeeded */
426 ret = msm_otg_phy_clk_reset(motg);
427 if (ret)
428 return ret;
429
430 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200431 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 if (ret != -ETIMEDOUT)
433 break;
434 ret = msm_otg_phy_clk_reset(motg);
435 if (ret)
436 return ret;
437 }
438 if (!retries)
439 return -ETIMEDOUT;
440
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200441 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530442 return 0;
443}
444
445#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530446static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530447{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530448 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530449
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530450 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530451 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530452 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530453 break;
454 udelay(1);
455 cnt++;
456 }
457 if (cnt >= LINK_RESET_TIMEOUT_USEC)
458 return -ETIMEDOUT;
459
460 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530461 writel_relaxed(0x80000000, USB_PORTSC);
462 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530463 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530464
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530465 return 0;
466}
467
Steve Mucklef132c6c2012-06-06 18:30:57 -0700468static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530469{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700470 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530471 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530472 int ret;
473 u32 val = 0;
474 u32 ulpi_val = 0;
475
Ofir Cohen4da266f2012-01-03 10:19:29 +0200476 /*
477 * USB PHY and Link reset also reset the USB BAM.
478 * Thus perform reset operation only once to avoid
479 * USB BAM reset on other cases e.g. USB cable disconnections.
480 */
481 if (pdata->disable_reset_on_disconnect) {
482 if (motg->reset_counter)
483 return 0;
484 else
485 motg->reset_counter++;
486 }
487
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530488 if (!IS_ERR(motg->clk))
489 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530490 ret = msm_otg_phy_reset(motg);
491 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700492 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530493 return ret;
494 }
495
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530496 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530497 ret = msm_otg_link_reset(motg);
498 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700499 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530500 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530501 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530502 msleep(100);
503
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530504 ulpi_init(motg);
505
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700506 /* Ensure that RESET operation is completed before turning off clock */
507 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530508
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530509 if (!IS_ERR(motg->clk))
510 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530511
512 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530513 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530514 if (pdata->mode == USB_OTG) {
515 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
516 val |= OTGSC_IDIE | OTGSC_BSVIE;
517 } else if (pdata->mode == USB_PERIPHERAL) {
518 ulpi_val = ULPI_INT_SESS_VALID;
519 val |= OTGSC_BSVIE;
520 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530521 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200522 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
523 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530524 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700525 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530526 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530527 /* Enable PMIC pull-up */
528 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530529 }
530
531 return 0;
532}
533
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530534static const char *timer_string(int bit)
535{
536 switch (bit) {
537 case A_WAIT_VRISE: return "a_wait_vrise";
538 case A_WAIT_VFALL: return "a_wait_vfall";
539 case B_SRP_FAIL: return "b_srp_fail";
540 case A_WAIT_BCON: return "a_wait_bcon";
541 case A_AIDL_BDIS: return "a_aidl_bdis";
542 case A_BIDL_ADIS: return "a_bidl_adis";
543 case B_ASE0_BRST: return "b_ase0_brst";
544 case A_TST_MAINT: return "a_tst_maint";
545 case B_TST_SRP: return "b_tst_srp";
546 case B_TST_CONFIG: return "b_tst_config";
547 default: return "UNDEFINED";
548 }
549}
550
551static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
552{
553 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
554
555 switch (motg->active_tmout) {
556 case A_WAIT_VRISE:
557 /* TODO: use vbus_vld interrupt */
558 set_bit(A_VBUS_VLD, &motg->inputs);
559 break;
560 case A_TST_MAINT:
561 /* OTG PET: End session after TA_TST_MAINT */
562 set_bit(A_BUS_DROP, &motg->inputs);
563 break;
564 case B_TST_SRP:
565 /*
566 * OTG PET: Initiate SRP after TB_TST_SRP of
567 * previous session end.
568 */
569 set_bit(B_BUS_REQ, &motg->inputs);
570 break;
571 case B_TST_CONFIG:
572 clear_bit(A_CONN, &motg->inputs);
573 break;
574 default:
575 set_bit(motg->active_tmout, &motg->tmouts);
576 }
577
578 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
579 queue_work(system_nrt_wq, &motg->sm_work);
580 return HRTIMER_NORESTART;
581}
582
583static void msm_otg_del_timer(struct msm_otg *motg)
584{
585 int bit = motg->active_tmout;
586
587 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
588 div_s64(ktime_to_us(hrtimer_get_remaining(
589 &motg->timer)), 1000));
590 hrtimer_cancel(&motg->timer);
591 clear_bit(bit, &motg->tmouts);
592}
593
594static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
595{
596 clear_bit(bit, &motg->tmouts);
597 motg->active_tmout = bit;
598 pr_debug("starting %s timer\n", timer_string(bit));
599 hrtimer_start(&motg->timer,
600 ktime_set(time / 1000, (time % 1000) * 1000000),
601 HRTIMER_MODE_REL);
602}
603
604static void msm_otg_init_timer(struct msm_otg *motg)
605{
606 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
607 motg->timer.function = msm_otg_timer_func;
608}
609
Steve Mucklef132c6c2012-06-06 18:30:57 -0700610static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530611{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700612 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530613
Steve Mucklef132c6c2012-06-06 18:30:57 -0700614 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530615 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700616 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530617 return -EINVAL;
618 }
619
620 pr_debug("A-Host: HNP initiated\n");
621 clear_bit(A_BUS_REQ, &motg->inputs);
622 queue_work(system_nrt_wq, &motg->sm_work);
623 return 0;
624}
625
Steve Mucklef132c6c2012-06-06 18:30:57 -0700626static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530627{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700628 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530629 u32 val;
630 int ret = 0;
631
Steve Mucklef132c6c2012-06-06 18:30:57 -0700632 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530633 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700634 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530635 ret = -EINVAL;
636 goto out;
637 }
638
639 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
640 pr_debug("initial conditions of SRP are not met. Try again"
641 "after some time\n");
642 ret = -EAGAIN;
643 goto out;
644 }
645
646 pr_debug("B-Device SRP started\n");
647
648 /*
649 * PHY won't pull D+ high unless it detects Vbus valid.
650 * Since by definition, SRP is only done when Vbus is not valid,
651 * software work-around needs to be used to spoof the PHY into
652 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
653 * VBUSVLDEXT register bits.
654 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700655 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530656 /*
657 * Harware auto assist data pulsing: Data pulse is given
658 * for 7msec; wait for vbus
659 */
660 val = readl_relaxed(USB_OTGSC);
661 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
662
663 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
664out:
665 return ret;
666}
667
Steve Mucklef132c6c2012-06-06 18:30:57 -0700668static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530669{
670 struct usb_hcd *hcd = bus_to_hcd(otg->host);
671 struct usb_device *rhub = otg->host->root_hub;
672
673 if (enable) {
674 pm_runtime_disable(&rhub->dev);
675 rhub->state = USB_STATE_NOTATTACHED;
676 hcd->driver->bus_suspend(hcd);
677 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
678 } else {
679 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700680 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530681 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
682 }
683}
684
Steve Mucklef132c6c2012-06-06 18:30:57 -0700685static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530686{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700687 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530688
Amit Blay6fa647a2012-05-24 14:12:08 +0300689 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530690 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530691
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530692 /*
693 * UDC and HCD call usb_phy_set_suspend() to enter/exit LPM
694 * during bus suspend/resume. Update the relevant state
695 * machine inputs and trigger LPM entry/exit. Checking
696 * in_lpm flag would avoid unnecessary work scheduling.
697 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530698 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700699 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530700 case OTG_STATE_A_WAIT_BCON:
701 if (TA_WAIT_BCON > 0)
702 break;
703 /* fall through */
704 case OTG_STATE_A_HOST:
705 pr_debug("host bus suspend\n");
706 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530707 if (!atomic_read(&motg->in_lpm))
708 queue_work(system_nrt_wq, &motg->sm_work);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530709 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300710 case OTG_STATE_B_PERIPHERAL:
711 pr_debug("peripheral bus suspend\n");
712 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
713 break;
714 set_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530715 if (!atomic_read(&motg->in_lpm))
716 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300717 break;
718
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530719 default:
720 break;
721 }
722 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700723 switch (phy->state) {
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530724 case OTG_STATE_A_WAIT_BCON:
725 /* Remote wakeup or resume */
726 set_bit(A_BUS_REQ, &motg->inputs);
727 /* ensure hardware is not in low power mode */
728 if (atomic_read(&motg->in_lpm))
729 pm_runtime_resume(phy->dev);
730 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530731 case OTG_STATE_A_SUSPEND:
732 /* Remote wakeup or resume */
733 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700734 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700735
736 /* ensure hardware is not in low power mode */
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530737 if (atomic_read(&motg->in_lpm))
738 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530739 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300740 case OTG_STATE_B_PERIPHERAL:
741 pr_debug("peripheral bus resume\n");
742 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
743 break;
744 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Pavankumar Kondeti572092f2013-01-10 16:41:57 +0530745 if (atomic_read(&motg->in_lpm))
746 queue_work(system_nrt_wq, &motg->sm_work);
Amit Blay6fa647a2012-05-24 14:12:08 +0300747 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530748 default:
749 break;
750 }
751 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530752 return 0;
753}
754
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530755#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530756#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
757
758#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530759static int msm_otg_suspend(struct msm_otg *motg)
760{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200761 struct usb_phy *phy = &motg->phy;
762 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763 struct msm_otg_platform_data *pdata = motg->pdata;
764 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300765 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530766 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800767 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530768 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530769
770 if (atomic_read(&motg->in_lpm))
771 return 0;
772
773 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530774 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
775 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700776 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300777 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
778 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530779 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham6ff217b2012-08-13 15:34:20 -0700780
Pavankumar Kondetib91ab712012-10-22 13:21:19 +0530781 /*
782 * Abort suspend when,
783 * 1. charging detection in progress due to cable plug-in
784 * 2. host mode activation in progress due to Micro-A cable insertion
785 */
786
787 if ((test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
788 !dcp) || test_bit(A_BUS_REQ, &motg->inputs)) {
Jack Pham6ff217b2012-08-13 15:34:20 -0700789 enable_irq(motg->irq);
790 return -EBUSY;
791 }
792
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530793 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530794 * Chipidea 45-nm PHY suspend sequence:
795 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530796 * Interrupt Latch Register auto-clear feature is not present
797 * in all PHY versions. Latch register is clear on read type.
798 * Clear latch register to avoid spurious wakeup from
799 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530800 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530801 * PHY comparators are disabled when PHY enters into low power
802 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
803 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
804 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530805 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530806 * PLL is not turned off when PHY enters into low power mode (LPM).
807 * Disable PLL for maximum power savings.
808 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530809
810 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200811 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530812 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200813 ulpi_write(phy, 0x01, 0x30);
814 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530815 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530816
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530818 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530819 * PHY may take some time or even fail to enter into low power
820 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
821 * in failure case.
822 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530823 portsc = readl_relaxed(USB_PORTSC);
824 if (!(portsc & PORTSC_PHCD)) {
825 writel_relaxed(portsc | PORTSC_PHCD,
826 USB_PORTSC);
827 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
828 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
829 break;
830 udelay(1);
831 cnt++;
832 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530833 }
834
835 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200836 dev_err(phy->dev, "Unable to suspend PHY\n");
837 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530838 enable_irq(motg->irq);
839 return -ETIMEDOUT;
840 }
841
842 /*
843 * PHY has capability to generate interrupt asynchronously in low
844 * power mode (LPM). This interrupt is level triggered. So USB IRQ
845 * line must be disabled till async interrupt enable bit is cleared
846 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
847 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530848 *
849 * PHY retention mode is disallowed while entering to LPM with wall
850 * charger connected. But PHY is put into suspend mode. Hence
851 * enable asynchronous interrupt to detect charger disconnection when
852 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530853 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530854 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300855 if (host_bus_suspend || device_bus_suspend ||
856 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530857 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
858 else
859 cmd_val |= ULPI_STP_CTRL;
860 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530861
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530862 /*
863 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
864 * PHY retention and collapse can not happen with VDP_SRC enabled.
865 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300866 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
867 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200868 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
869 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
870 /* Enable PHY HV interrupts to wake MPM/Link */
871 phy_ctrl_val |=
872 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530873
Amit Blay58b31472011-11-18 09:39:39 +0200874 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530876 }
877
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 /* Ensure that above operation is completed before turning off clocks */
879 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300880 /* Consider clocks on workaround flag only in case of bus suspend */
881 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
882 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
883 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300884 clk_disable_unprepare(motg->pclk);
885 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300886 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300887 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530888
Anji jonnala7da3f262011-12-02 17:22:14 -0800889 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530890 if (!host_bus_suspend) {
891 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
892 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700893 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530894 "TCXO D0 buffer%d\n", __func__, ret);
895 else
896 motg->lpm_flags |= XO_SHUTDOWN;
897 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800898
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530899 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
900 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700901 msm_hsusb_ldo_enable(motg, 0);
902 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530903 }
904
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530905 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530907 msm_hsusb_mhl_switch_enable(motg, 0);
908 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909
Steve Mucklef132c6c2012-06-06 18:30:57 -0700910 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530911 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700912 if (motg->async_irq)
913 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 if (motg->pdata->pmic_id_irq)
915 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700916 if (pdata->otg_control == OTG_PHY_CONTROL &&
917 pdata->mpm_otgsessvld_int)
918 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530920 if (bus)
921 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
922
923 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -0700924 /* Enable ASYNC IRQ (if present) during LPM */
925 if (motg->async_irq)
926 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530927 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700928 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530929
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200930 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530931
932 return 0;
933}
934
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530935static int msm_otg_resume(struct msm_otg *motg)
936{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200937 struct usb_phy *phy = &motg->phy;
938 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700939 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530940 int cnt = 0;
941 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200942 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800943 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530944
945 if (!atomic_read(&motg->in_lpm))
946 return 0;
947
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530949
Anji jonnala7da3f262011-12-02 17:22:14 -0800950 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530951 if (motg->lpm_flags & XO_SHUTDOWN) {
952 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
953 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700954 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530955 "TCXO D0 buffer%d\n", __func__, ret);
956 motg->lpm_flags &= ~XO_SHUTDOWN;
957 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530958
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300959 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300960 clk_prepare_enable(motg->core_clk);
961 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300962 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300963 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
966 msm_hsusb_ldo_enable(motg, 1);
967 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
968 }
969
970 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530971 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530972 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200973 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
974 phy_ctrl_val |= PHY_RETEN;
975 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
976 /* Disable PHY HV interrupts */
977 phy_ctrl_val &=
978 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
979 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530981 }
982
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530983 temp = readl(USB_USBCMD);
984 temp &= ~ASYNC_INTR_CTRL;
985 temp &= ~ULPI_STP_CTRL;
986 writel(temp, USB_USBCMD);
987
988 /*
989 * PHY comes out of low power mode (LPM) in case of wakeup
990 * from asynchronous interrupt.
991 */
992 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
993 goto skip_phy_resume;
994
995 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
996 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
997 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
998 break;
999 udelay(1);
1000 cnt++;
1001 }
1002
1003 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
1004 /*
1005 * This is a fatal error. Reset the link and
1006 * PHY. USB state can not be restored. Re-insertion
1007 * of USB cable is the only way to get USB working.
1008 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001009 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301010 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001011 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301012 }
1013
1014skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001015 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301016 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07001017 if (motg->async_irq)
1018 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001019 if (motg->pdata->pmic_id_irq)
1020 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -07001021 if (pdata->otg_control == OTG_PHY_CONTROL &&
1022 pdata->mpm_otgsessvld_int)
1023 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001024 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301025 if (bus)
1026 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1027
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301028 atomic_set(&motg->in_lpm, 0);
1029
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301030 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001031 /* Match the disable_irq call from ISR */
1032 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301033 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301034 }
1035
Manu Gautamf8c45642012-08-10 10:20:56 -07001036 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1037 if (motg->async_irq)
1038 disable_irq(motg->async_irq);
1039
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001040 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301041
1042 return 0;
1043}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301044#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301045
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001046static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1047{
Pavankumar Kondeti84fec152012-11-09 20:51:36 +05301048 int ret;
1049
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001050 if (!psy)
1051 goto psy_not_supported;
1052
Pavankumar Kondeti84fec152012-11-09 20:51:36 +05301053 if (host_mode) {
1054 ret = power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1055 } else {
1056 ret = power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1057 /*
1058 * VBUS comparator is disabled by PMIC charging driver
1059 * when SYSTEM scope is selected. For ID_GND->ID_A
1060 * transition, give 50 msec delay so that PMIC charger
1061 * driver detect the VBUS and ready for accepting
1062 * charging current value from USB.
1063 */
1064 if (test_bit(ID_A, &motg->inputs))
1065 msleep(50);
1066 }
1067 return ret;
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001068
1069psy_not_supported:
1070 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1071 return -ENXIO;
1072}
1073
David Keitel081a3e22012-04-18 12:37:07 -07001074static int msm_otg_notify_chg_type(struct msm_otg *motg)
1075{
roy.parkd063ef32012-08-30 09:37:38 -07001076 int charger_type;
David Keitel081a3e22012-04-18 12:37:07 -07001077 /*
1078 * TODO
1079 * Unify OTG driver charger types and power supply charger types
1080 */
David Keitel081a3e22012-04-18 12:37:07 -07001081
1082 if (motg->chg_type == USB_SDP_CHARGER)
1083 charger_type = POWER_SUPPLY_TYPE_USB;
1084 else if (motg->chg_type == USB_CDP_CHARGER)
1085 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301086 else if (motg->chg_type == USB_DCP_CHARGER ||
1087 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001088 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1089 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1090 motg->chg_type == USB_ACA_A_CHARGER ||
1091 motg->chg_type == USB_ACA_B_CHARGER ||
1092 motg->chg_type == USB_ACA_C_CHARGER))
1093 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1094 else
Abhijeet Dharmapurikar999ee572012-08-28 19:33:49 -07001095 charger_type = POWER_SUPPLY_TYPE_UNKNOWN;
David Keitel081a3e22012-04-18 12:37:07 -07001096
1097 return pm8921_set_usb_power_supply_type(charger_type);
1098}
1099
Amit Blay0f7edf72012-01-15 10:11:27 +02001100static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1101{
Amit Blay0f7edf72012-01-15 10:11:27 +02001102
Amit Blay0f7edf72012-01-15 10:11:27 +02001103 if (!psy)
1104 goto psy_not_supported;
1105
1106 if (motg->cur_power == 0 && mA > 0) {
1107 /* Enable charging */
1108 if (power_supply_set_online(psy, true))
1109 goto psy_not_supported;
1110 } else if (motg->cur_power > 0 && mA == 0) {
1111 /* Disable charging */
1112 if (power_supply_set_online(psy, false))
1113 goto psy_not_supported;
1114 return 0;
1115 }
1116 /* Set max current limit */
1117 if (power_supply_set_current_limit(psy, 1000*mA))
1118 goto psy_not_supported;
1119
1120 return 0;
1121
1122psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001123 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001124 return -ENXIO;
1125}
1126
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301127static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1128{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001129 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301130
1131 if (g && g->is_a_peripheral)
1132 return;
1133
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301134 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1135 motg->chg_type == USB_ACA_A_CHARGER ||
1136 motg->chg_type == USB_ACA_B_CHARGER ||
1137 motg->chg_type == USB_ACA_C_CHARGER) &&
1138 mA > IDEV_ACA_CHG_LIMIT)
1139 mA = IDEV_ACA_CHG_LIMIT;
1140
David Keitel081a3e22012-04-18 12:37:07 -07001141 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001142 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001143 "Failed notifying %d charger type to PMIC\n",
1144 motg->chg_type);
1145
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301146 if (motg->cur_power == mA)
1147 return;
1148
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001149 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001150
Xiaozhe Shi7c229e52013-01-22 09:46:45 -08001151 /*
1152 * Use Power Supply API if supported, otherwise fallback
1153 * to legacy pm8921 API.
1154 */
1155 if (msm_otg_notify_power_supply(motg, mA))
1156 pm8921_charger_vbus_draw(mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001157
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301158 motg->cur_power = mA;
1159}
1160
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001161static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301162{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001163 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301164
1165 /*
1166 * Gadget driver uses set_power method to notify about the
1167 * available current based on suspend/configured states.
1168 *
1169 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1170 * states when CDP/ACA is connected.
1171 */
1172 if (motg->chg_type == USB_SDP_CHARGER)
1173 msm_otg_notify_charger(motg, mA);
1174
1175 return 0;
1176}
1177
Steve Mucklef132c6c2012-06-06 18:30:57 -07001178static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301179{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001180 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301181 struct msm_otg_platform_data *pdata = motg->pdata;
1182 struct usb_hcd *hcd;
1183
1184 if (!otg->host)
1185 return;
1186
1187 hcd = bus_to_hcd(otg->host);
1188
1189 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001190 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301191
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301192 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001193 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301194 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1195
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301196 /*
1197 * Some boards have a switch cotrolled by gpio
1198 * to enable/disable internal HUB. Enable internal
1199 * HUB before kicking the host.
1200 */
1201 if (pdata->setup_gpio)
1202 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301203 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301204 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001205 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301206
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301207 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301208 /* HCD core reset all bits of PORTSC. select ULPI phy */
1209 writel_relaxed(0x80000000, USB_PORTSC);
1210
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301211 if (pdata->setup_gpio)
1212 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301213
1214 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001215 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301216 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301217 }
1218}
1219
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220static int msm_otg_usbdev_notify(struct notifier_block *self,
1221 unsigned long action, void *priv)
1222{
1223 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001224 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301225 struct usb_device *udev = priv;
1226
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301227 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1228 goto out;
1229
Steve Mucklef132c6c2012-06-06 18:30:57 -07001230 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301231 goto out;
1232 /*
1233 * Interested in devices connected directly to the root hub.
1234 * ACA dock can supply IDEV_CHG irrespective devices connected
1235 * on the accessory port.
1236 */
1237 if (!udev->parent || udev->parent->parent ||
1238 motg->chg_type == USB_ACA_DOCK_CHARGER)
1239 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240
1241 switch (action) {
1242 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301243 if (aca_enabled())
1244 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001245 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301246 pr_debug("B_CONN set\n");
1247 set_bit(B_CONN, &motg->inputs);
1248 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001249 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301250 /*
1251 * OTG PET: A-device must end session within
1252 * 10 sec after PET enumeration.
1253 */
1254 if (udev->quirks & USB_QUIRK_OTG_PET)
1255 msm_otg_start_timer(motg, TA_TST_MAINT,
1256 A_TST_MAINT);
1257 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301258 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260 if (udev->actconfig)
1261 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1262 else
1263 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001264 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301265 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301266 break;
1267 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001268 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1269 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301270 pr_debug("B_CONN clear\n");
1271 clear_bit(B_CONN, &motg->inputs);
1272 /*
1273 * OTG PET: A-device must end session after
1274 * PET disconnection if it is enumerated
1275 * with bcdDevice[0] = 1. USB core sets
1276 * bus->otg_vbus_off for us. clear it here.
1277 */
1278 if (udev->bus->otg_vbus_off) {
1279 udev->bus->otg_vbus_off = 0;
1280 set_bit(A_BUS_DROP, &motg->inputs);
1281 }
1282 queue_work(system_nrt_wq, &motg->sm_work);
1283 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001284 default:
1285 break;
1286 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301287 if (test_bit(ID_A, &motg->inputs))
1288 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1289 motg->mA_port);
1290out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291 return NOTIFY_OK;
1292}
1293
Mayank Ranae3926882011-12-26 09:47:54 +05301294static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1295{
1296 int ret;
1297 static bool vbus_is_on;
1298
1299 if (vbus_is_on == on)
1300 return;
1301
1302 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301303 ret = motg->pdata->vbus_power(on);
1304 if (!ret)
1305 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301306 return;
1307 }
1308
1309 if (!vbus_otg) {
1310 pr_err("vbus_otg is NULL.");
1311 return;
1312 }
1313
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001314 /*
1315 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001316 * from usb before turning on the boost.
1317 * if exiting host mode disable the boost before enabling to draw
1318 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001319 */
Mayank Ranae3926882011-12-26 09:47:54 +05301320 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001321 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301322 ret = regulator_enable(vbus_otg);
1323 if (ret) {
1324 pr_err("unable to enable vbus_otg\n");
1325 return;
1326 }
1327 vbus_is_on = true;
1328 } else {
1329 ret = regulator_disable(vbus_otg);
1330 if (ret) {
1331 pr_err("unable to disable vbus_otg\n");
1332 return;
1333 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001334 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301335 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301336 }
1337}
1338
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001339static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301340{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001341 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301342 struct usb_hcd *hcd;
1343
1344 /*
1345 * Fail host registration if this board can support
1346 * only peripheral configuration.
1347 */
1348 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001349 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301350 return -ENODEV;
1351 }
1352
Devin Kimd10f03c2012-06-25 17:05:13 -07001353 if (!machine_is_apq8064_mako()) {
1354 if (!motg->pdata->vbus_power && host) {
1355 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
1356 if (IS_ERR(vbus_otg)) {
1357 pr_err("Unable to get vbus_otg\n");
1358 return -ENODEV;
1359 }
Mayank Ranae3926882011-12-26 09:47:54 +05301360 }
1361 }
1362
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301363 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001364 if (otg->phy->state == OTG_STATE_A_HOST) {
1365 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001366 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301367 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301368 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301369 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001370 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301371 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301372 } else {
1373 otg->host = NULL;
1374 }
1375
1376 return 0;
1377 }
1378
1379 hcd = bus_to_hcd(host);
1380 hcd->power_budget = motg->pdata->power_budget;
1381
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301382#ifdef CONFIG_USB_OTG
1383 host->otg_port = 1;
1384#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1386 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301387 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001388 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301389
1390 /*
1391 * Kick the state machine work, if peripheral is not supported
1392 * or peripheral is already registered with us.
1393 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301394 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001395 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301396 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301397 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301398
1399 return 0;
1400}
1401
Steve Mucklef132c6c2012-06-06 18:30:57 -07001402static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301403{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301404 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001405 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301406 struct msm_otg_platform_data *pdata = motg->pdata;
1407
1408 if (!otg->gadget)
1409 return;
1410
1411 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001412 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301413 /*
1414 * Some boards have a switch cotrolled by gpio
1415 * to enable/disable internal HUB. Disable internal
1416 * HUB before kicking the gadget.
1417 */
1418 if (pdata->setup_gpio)
1419 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001420
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301421 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301422 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301423 ret = msm_bus_scale_client_update_request(
1424 motg->bus_perf_client, 1);
1425 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001426 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301427 "bus bandwidth %d\n", __func__, ret);
1428 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301429 usb_gadget_vbus_connect(otg->gadget);
1430 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001431 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301432 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301433 /* Configure BUS performance parameters to default */
1434 if (motg->bus_perf_client) {
1435 ret = msm_bus_scale_client_update_request(
1436 motg->bus_perf_client, 0);
1437 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001438 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301439 "for bus bw %d\n", __func__, ret);
1440 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301441 if (pdata->setup_gpio)
1442 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1443 }
1444
1445}
1446
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001447static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301448 struct usb_gadget *gadget)
1449{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001450 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301451
1452 /*
1453 * Fail peripheral registration if this board can support
1454 * only host configuration.
1455 */
1456 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001457 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301458 return -ENODEV;
1459 }
1460
1461 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001462 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1463 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301464 msm_otg_start_peripheral(otg, 0);
1465 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001466 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301467 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301468 } else {
1469 otg->gadget = NULL;
1470 }
1471
1472 return 0;
1473 }
1474 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001475 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301476
1477 /*
1478 * Kick the state machine work, if host is not supported
1479 * or host is already registered with us.
1480 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301481 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001482 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301483 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301484 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301485
1486 return 0;
1487}
1488
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301489static bool msm_otg_read_pmic_id_state(struct msm_otg *motg)
1490{
1491 unsigned long flags;
1492 int id;
1493
1494 if (!motg->pdata->pmic_id_irq)
1495 return -ENODEV;
1496
1497 local_irq_save(flags);
1498 id = irq_read_line(motg->pdata->pmic_id_irq);
1499 local_irq_restore(flags);
1500
1501 /*
1502 * If we can not read ID line state for some reason, treat
1503 * it as float. This would prevent MHL discovery and kicking
1504 * host mode unnecessarily.
1505 */
1506 return !!id;
1507}
1508
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301509static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1510 void (*callback)(int on))
1511{
1512 struct usb_phy *phy = &motg->phy;
1513 int ret;
1514
1515 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1516 !motg->pdata->pmic_id_irq) {
1517 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1518 return -ENODEV;
1519 }
1520
1521 if (!motg->pdata->mhl_dev_name) {
1522 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1523 return -ENODEV;
1524 }
1525
1526 if (callback)
1527 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1528 callback);
1529 else
1530 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1531
1532 if (ret)
1533 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1534 motg->pdata->mhl_dev_name, ret);
1535 else
1536 motg->mhl_enabled = true;
1537
1538 return ret;
1539}
1540
1541static void msm_otg_mhl_notify_online(int on)
1542{
1543 struct msm_otg *motg = the_msm_otg;
1544 struct usb_phy *phy = &motg->phy;
1545 bool queue = false;
1546
1547 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1548
1549 if (on) {
1550 set_bit(MHL, &motg->inputs);
1551 } else {
1552 clear_bit(MHL, &motg->inputs);
1553 queue = true;
1554 }
1555
1556 if (queue && phy->state != OTG_STATE_UNDEFINED)
1557 schedule_work(&motg->sm_work);
1558}
1559
1560static bool msm_otg_is_mhl(struct msm_otg *motg)
1561{
1562 struct usb_phy *phy = &motg->phy;
1563 int is_mhl, ret;
1564
1565 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1566 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1567 /*
1568 * MHL driver calls our callback saying that MHL connected
1569 * if RID_GND is detected. But at later part of discovery
1570 * it may figure out MHL is not connected and returns
1571 * false. Hence clear MHL input here.
1572 */
1573 clear_bit(MHL, &motg->inputs);
1574 dev_dbg(phy->dev, "MHL device not found\n");
1575 return false;
1576 }
1577
1578 set_bit(MHL, &motg->inputs);
1579 dev_dbg(phy->dev, "MHL device found\n");
1580 return true;
1581}
1582
1583static bool msm_chg_mhl_detect(struct msm_otg *motg)
1584{
1585 bool ret, id;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301586
1587 if (!motg->mhl_enabled)
1588 return false;
1589
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05301590 id = msm_otg_read_pmic_id_state(motg);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301591
1592 if (id)
1593 return false;
1594
1595 mhl_det_in_progress = true;
1596 ret = msm_otg_is_mhl(motg);
1597 mhl_det_in_progress = false;
1598
1599 return ret;
1600}
1601
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001602static bool msm_chg_aca_detect(struct msm_otg *motg)
1603{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001604 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001605 u32 int_sts;
1606 bool ret = false;
1607
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301608 if (!aca_enabled())
1609 goto out;
1610
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001611 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1612 goto out;
1613
Steve Mucklef132c6c2012-06-06 18:30:57 -07001614 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001615 switch (int_sts & 0x1C) {
1616 case 0x08:
1617 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001618 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001619 motg->chg_type = USB_ACA_A_CHARGER;
1620 motg->chg_state = USB_CHG_STATE_DETECTED;
1621 clear_bit(ID_B, &motg->inputs);
1622 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301623 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001624 ret = true;
1625 }
1626 break;
1627 case 0x0C:
1628 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001629 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001630 motg->chg_type = USB_ACA_B_CHARGER;
1631 motg->chg_state = USB_CHG_STATE_DETECTED;
1632 clear_bit(ID_A, &motg->inputs);
1633 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301634 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001635 ret = true;
1636 }
1637 break;
1638 case 0x10:
1639 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001640 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001641 motg->chg_type = USB_ACA_C_CHARGER;
1642 motg->chg_state = USB_CHG_STATE_DETECTED;
1643 clear_bit(ID_A, &motg->inputs);
1644 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301645 set_bit(ID, &motg->inputs);
1646 ret = true;
1647 }
1648 break;
1649 case 0x04:
1650 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001651 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301652 motg->chg_type = USB_INVALID_CHARGER;
1653 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1654 clear_bit(ID_A, &motg->inputs);
1655 clear_bit(ID_B, &motg->inputs);
1656 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001657 ret = true;
1658 }
1659 break;
1660 default:
1661 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1662 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301663 test_and_clear_bit(ID_C, &motg->inputs) |
1664 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001666 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001667 motg->chg_type = USB_INVALID_CHARGER;
1668 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1669 }
1670 }
1671out:
1672 return ret;
1673}
1674
1675static void msm_chg_enable_aca_det(struct msm_otg *motg)
1676{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001677 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001678
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301679 if (!aca_enabled())
1680 return;
1681
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001682 switch (motg->pdata->phy_type) {
1683 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301684 /* Disable ID_GND in link and PHY */
1685 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1686 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001687 ulpi_write(phy, 0x01, 0x0C);
1688 ulpi_write(phy, 0x10, 0x0F);
1689 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301690 /* Disable PMIC ID pull-up */
1691 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301692 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001693 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301694 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001695 break;
1696 default:
1697 break;
1698 }
1699}
1700
1701static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1702{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001703 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001704
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301705 if (!aca_enabled())
1706 return;
1707
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001708 switch (motg->pdata->phy_type) {
1709 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301710 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001711 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301712 break;
1713 default:
1714 break;
1715 }
1716}
1717
1718static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1719{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001720 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301721
1722 if (!aca_enabled())
1723 return;
1724
1725 switch (motg->pdata->phy_type) {
1726 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001727 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001728 break;
1729 default:
1730 break;
1731 }
1732}
1733
1734static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1735{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001736 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001737 bool ret = false;
1738
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301739 if (!aca_enabled())
1740 return ret;
1741
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742 switch (motg->pdata->phy_type) {
1743 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001744 if (ulpi_read(phy, 0x91) & 1) {
1745 dev_dbg(phy->dev, "RID change\n");
1746 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001747 ret = msm_chg_aca_detect(motg);
1748 }
1749 default:
1750 break;
1751 }
1752 return ret;
1753}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301754
1755static void msm_otg_id_timer_func(unsigned long data)
1756{
1757 struct msm_otg *motg = (struct msm_otg *) data;
1758
1759 if (!aca_enabled())
1760 return;
1761
1762 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001763 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301764 return;
1765 }
1766
Steve Mucklef132c6c2012-06-06 18:30:57 -07001767 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301768 goto out;
1769
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301770 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001771 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301772 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301773 }
1774
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301775out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301776 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1777 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1778}
1779
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301780static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1781{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001782 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301783 u32 chg_det;
1784 bool ret = false;
1785
1786 switch (motg->pdata->phy_type) {
1787 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001788 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301789 ret = chg_det & (1 << 4);
1790 break;
1791 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001792 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301793 ret = chg_det & 1;
1794 break;
1795 default:
1796 break;
1797 }
1798 return ret;
1799}
1800
1801static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1802{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001803 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301804 u32 chg_det;
1805
1806 switch (motg->pdata->phy_type) {
1807 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001808 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301809 /* Turn off charger block */
1810 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001811 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301812 udelay(20);
1813 /* control chg block via ULPI */
1814 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001815 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301816 /* put it in host mode for enabling D- source */
1817 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001818 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301819 /* Turn on chg detect block */
1820 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001821 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301822 udelay(20);
1823 /* enable chg detection */
1824 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001825 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301826 break;
1827 case SNPS_28NM_INTEGRATED_PHY:
1828 /*
1829 * Configure DM as current source, DP as current sink
1830 * and enable battery charging comparators.
1831 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001832 ulpi_write(phy, 0x8, 0x85);
1833 ulpi_write(phy, 0x2, 0x85);
1834 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301835 break;
1836 default:
1837 break;
1838 }
1839}
1840
1841static bool msm_chg_check_primary_det(struct msm_otg *motg)
1842{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001843 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301844 u32 chg_det;
1845 bool ret = false;
1846
1847 switch (motg->pdata->phy_type) {
1848 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001849 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301850 ret = chg_det & (1 << 4);
1851 break;
1852 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001853 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301854 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301855 /* Turn off VDP_SRC */
1856 ulpi_write(phy, 0x3, 0x86);
1857 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301858 break;
1859 default:
1860 break;
1861 }
1862 return ret;
1863}
1864
1865static void msm_chg_enable_primary_det(struct msm_otg *motg)
1866{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001867 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301868 u32 chg_det;
1869
1870 switch (motg->pdata->phy_type) {
1871 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001872 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301873 /* enable chg detection */
1874 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001875 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301876 break;
1877 case SNPS_28NM_INTEGRATED_PHY:
1878 /*
1879 * Configure DP as current source, DM as current sink
1880 * and enable battery charging comparators.
1881 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001882 ulpi_write(phy, 0x2, 0x85);
1883 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301884 break;
1885 default:
1886 break;
1887 }
1888}
1889
1890static bool msm_chg_check_dcd(struct msm_otg *motg)
1891{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001892 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301893 u32 line_state;
1894 bool ret = false;
1895
1896 switch (motg->pdata->phy_type) {
1897 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001898 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301899 ret = !(line_state & 1);
1900 break;
1901 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001902 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301903 ret = line_state & 2;
1904 break;
1905 default:
1906 break;
1907 }
1908 return ret;
1909}
1910
1911static void msm_chg_disable_dcd(struct msm_otg *motg)
1912{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001913 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301914 u32 chg_det;
1915
1916 switch (motg->pdata->phy_type) {
1917 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001918 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301919 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001920 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301921 break;
1922 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001923 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301924 break;
1925 default:
1926 break;
1927 }
1928}
1929
1930static void msm_chg_enable_dcd(struct msm_otg *motg)
1931{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001932 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301933 u32 chg_det;
1934
1935 switch (motg->pdata->phy_type) {
1936 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001937 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301938 /* Turn on D+ current source */
1939 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001940 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301941 break;
1942 case SNPS_28NM_INTEGRATED_PHY:
1943 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001944 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301945 break;
1946 default:
1947 break;
1948 }
1949}
1950
1951static void msm_chg_block_on(struct msm_otg *motg)
1952{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001953 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301954 u32 func_ctrl, chg_det;
1955
1956 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001957 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301958 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1959 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001960 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301961
1962 switch (motg->pdata->phy_type) {
1963 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001964 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301965 /* control chg block via ULPI */
1966 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001967 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301968 /* Turn on chg detect block */
1969 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001970 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301971 udelay(20);
1972 break;
1973 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05301974 /* disable DP and DM pull down resistors */
1975 ulpi_write(phy, 0x6, 0xC);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301976 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001977 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301978 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001979 ulpi_write(phy, 0x1F, 0x92);
1980 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301981 udelay(100);
1982 break;
1983 default:
1984 break;
1985 }
1986}
1987
1988static void msm_chg_block_off(struct msm_otg *motg)
1989{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001990 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301991 u32 func_ctrl, chg_det;
1992
1993 switch (motg->pdata->phy_type) {
1994 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001995 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301996 /* Turn off charger block */
1997 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001998 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301999 break;
2000 case SNPS_28NM_INTEGRATED_PHY:
2001 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002002 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302003 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002004 ulpi_write(phy, 0x1F, 0x92);
2005 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302006 break;
2007 default:
2008 break;
2009 }
2010
2011 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002012 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302013 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
2014 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002015 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302016}
2017
Anji jonnalad270e2d2011-08-09 11:28:32 +05302018static const char *chg_to_string(enum usb_chg_type chg_type)
2019{
2020 switch (chg_type) {
2021 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
2022 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
2023 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
2024 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
2025 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
2026 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
2027 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302028 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05302029 default: return "INVALID_CHARGER";
2030 }
2031}
kibum.leeec040442012-08-22 22:29:46 +09002032
Devin Kimd93ff032012-09-14 15:01:31 -07002033#define MSM_CHECK_TA_DELAY (5 * HZ)
2034#define PORTSC_LS (3 << 10) /* Read - Port's Line status */
kibum.leeec040442012-08-22 22:29:46 +09002035static void msm_ta_detect_work(struct work_struct *w)
2036{
Devin Kimd93ff032012-09-14 15:01:31 -07002037 struct msm_otg *motg = container_of(w, struct msm_otg, check_ta_work.work);
kibum.leeec040442012-08-22 22:29:46 +09002038 struct usb_otg *otg = motg->phy.otg;
kibum.leeec040442012-08-22 22:29:46 +09002039
Devin Kim6b8a5d12012-10-16 23:12:18 -07002040 pr_debug("msm_ta_detect_work: ta detection work\n");
Devin Kimd93ff032012-09-14 15:01:31 -07002041
2042 /* Presence of FRame Index or FRINDEX rollover implies USB communication */
2043 if( (readl(USB_FRINDEX) != 0) || ( readl(USB_USBSTS) & (1<<3) ) ) {
2044 pr_info("msm_ta_detect_work: USB exit ta detection - frindex\n");
2045 return;
2046 }
2047
2048 if ((readl(USB_PORTSC) & PORTSC_LS) == PORTSC_LS) {
2049 pr_info("msm_ta_detect_work: ta dectection success\n");
kibum.leeec040442012-08-22 22:29:46 +09002050 /* inform to user space that SDP is no longer detected */
2051 msm_otg_notify_charger(motg, 0);
2052 motg->chg_state = USB_CHG_STATE_DETECTED;
2053 motg->chg_type = USB_DCP_CHARGER;
2054 motg->cur_power = 0;
2055 msm_otg_start_peripheral(otg, 0);
2056 otg->phy->state = OTG_STATE_B_IDLE;
2057 schedule_work(&motg->sm_work);
2058 return;
kibum.leeec040442012-08-22 22:29:46 +09002059 }
Devin Kimd93ff032012-09-14 15:01:31 -07002060 schedule_delayed_work(&motg->check_ta_work, MSM_CHECK_TA_DELAY);
kibum.leeec040442012-08-22 22:29:46 +09002061}
Anji jonnalad270e2d2011-08-09 11:28:32 +05302062
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302063#define MSM_CHG_DCD_TIMEOUT (750 * HZ/1000) /* 750 msec */
2064#define MSM_CHG_DCD_POLL_TIME (50 * HZ/1000) /* 50 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302065#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
2066#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302067static void msm_chg_detect_work(struct work_struct *w)
2068{
2069 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002070 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302071 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302072 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302073 unsigned long delay;
2074
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002075 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302076
2077 if (test_bit(MHL, &motg->inputs)) {
2078 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
2079 return;
2080 }
2081
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302082 switch (motg->chg_state) {
2083 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302084 msm_chg_block_on(motg);
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302085 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002086 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302087 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302088 motg->dcd_time = 0;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302089 delay = MSM_CHG_DCD_POLL_TIME;
2090 break;
2091 case USB_CHG_STATE_WAIT_FOR_DCD:
jh.koo78df86f2012-09-04 20:13:19 +09002092 if (slimport_is_connected()) {
2093 msm_chg_block_off(motg);
2094 motg->chg_state = USB_CHG_STATE_DETECTED;
2095 motg->chg_type = USB_SDP_CHARGER;
2096 queue_work(system_nrt_wq, &motg->sm_work);
2097 return;
2098 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302099 if (msm_chg_mhl_detect(motg)) {
2100 msm_chg_block_off(motg);
2101 motg->chg_state = USB_CHG_STATE_DETECTED;
2102 motg->chg_type = USB_INVALID_CHARGER;
2103 queue_work(system_nrt_wq, &motg->sm_work);
2104 return;
2105 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002106 is_aca = msm_chg_aca_detect(motg);
2107 if (is_aca) {
2108 /*
2109 * ID_A can be ACA dock too. continue
2110 * primary detection after DCD.
2111 */
2112 if (test_bit(ID_A, &motg->inputs)) {
2113 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2114 } else {
2115 delay = 0;
2116 break;
2117 }
2118 }
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302119 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondeti39504502013-01-04 12:28:10 +05302120 motg->dcd_time += MSM_CHG_DCD_POLL_TIME;
2121 tmout = motg->dcd_time >= MSM_CHG_DCD_TIMEOUT;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302122 if (is_dcd || tmout) {
Pavankumar Kondetic7b9b83f2012-10-05 13:21:45 +05302123 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302124 msm_chg_enable_primary_det(motg);
2125 delay = MSM_CHG_PRIMARY_DET_TIME;
2126 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2127 } else {
2128 delay = MSM_CHG_DCD_POLL_TIME;
2129 }
2130 break;
2131 case USB_CHG_STATE_DCD_DONE:
2132 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302133 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2134 dm_vlgc = line_state & PORTSC_LS_DM;
2135 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302136 if (test_bit(ID_A, &motg->inputs)) {
2137 motg->chg_type = USB_ACA_DOCK_CHARGER;
2138 motg->chg_state = USB_CHG_STATE_DETECTED;
2139 delay = 0;
2140 break;
2141 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302142 if (line_state) { /* DP > VLGC */
2143 motg->chg_type = USB_PROPRIETARY_CHARGER;
2144 motg->chg_state = USB_CHG_STATE_DETECTED;
2145 delay = 0;
2146 } else {
2147 msm_chg_enable_secondary_det(motg);
2148 delay = MSM_CHG_SECONDARY_DET_TIME;
2149 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2150 }
2151 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302152 if (test_bit(ID_A, &motg->inputs)) {
2153 motg->chg_type = USB_ACA_A_CHARGER;
2154 motg->chg_state = USB_CHG_STATE_DETECTED;
2155 delay = 0;
2156 break;
2157 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302158
2159 if (line_state) /* DP > VLGC or/and DM > VLGC */
2160 motg->chg_type = USB_PROPRIETARY_CHARGER;
2161 else
2162 motg->chg_type = USB_SDP_CHARGER;
2163
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302164 motg->chg_state = USB_CHG_STATE_DETECTED;
2165 delay = 0;
2166 }
2167 break;
2168 case USB_CHG_STATE_PRIMARY_DONE:
2169 vout = msm_chg_check_secondary_det(motg);
2170 if (vout)
2171 motg->chg_type = USB_DCP_CHARGER;
2172 else
2173 motg->chg_type = USB_CDP_CHARGER;
2174 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2175 /* fall through */
2176 case USB_CHG_STATE_SECONDARY_DONE:
2177 motg->chg_state = USB_CHG_STATE_DETECTED;
2178 case USB_CHG_STATE_DETECTED:
2179 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002180 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302181 /*
2182 * Spurious interrupt is seen after enabling ACA detection
2183 * due to which charger detection fails in case of PET.
2184 * Add delay of 100 microsec to avoid that.
2185 */
2186 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002187 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002188 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302189 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302190 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302191 return;
2192 default:
2193 return;
2194 }
2195
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302196 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302197}
2198
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302199/*
2200 * We support OTG, Peripheral only and Host only configurations. In case
2201 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2202 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2203 * enabled when switch is controlled by user and default mode is supplied
2204 * by board file, which can be changed by userspace later.
2205 */
2206static void msm_otg_init_sm(struct msm_otg *motg)
2207{
2208 struct msm_otg_platform_data *pdata = motg->pdata;
2209 u32 otgsc = readl(USB_OTGSC);
2210
2211 switch (pdata->mode) {
2212 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002213 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302214 if (pdata->default_mode == USB_HOST) {
2215 clear_bit(ID, &motg->inputs);
2216 } else if (pdata->default_mode == USB_PERIPHERAL) {
2217 set_bit(ID, &motg->inputs);
2218 set_bit(B_SESS_VLD, &motg->inputs);
2219 } else {
2220 set_bit(ID, &motg->inputs);
2221 clear_bit(B_SESS_VLD, &motg->inputs);
2222 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302223 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302224 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302225 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302226 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302227 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302228 set_bit(A_BUS_REQ, &motg->inputs);
2229 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002230 if (otgsc & OTGSC_BSV)
2231 set_bit(B_SESS_VLD, &motg->inputs);
2232 else
2233 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302234 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302235 if (pdata->pmic_id_irq) {
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302236 if (msm_otg_read_pmic_id_state(motg))
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302237 set_bit(ID, &motg->inputs);
2238 else
2239 clear_bit(ID, &motg->inputs);
2240 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302241 /*
2242 * VBUS initial state is reported after PMIC
2243 * driver initialization. Wait for it.
2244 */
2245 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302246 }
2247 break;
2248 case USB_HOST:
2249 clear_bit(ID, &motg->inputs);
2250 break;
2251 case USB_PERIPHERAL:
2252 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302253 if (pdata->otg_control == OTG_PHY_CONTROL) {
2254 if (otgsc & OTGSC_BSV)
2255 set_bit(B_SESS_VLD, &motg->inputs);
2256 else
2257 clear_bit(B_SESS_VLD, &motg->inputs);
2258 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2259 /*
2260 * VBUS initial state is reported after PMIC
2261 * driver initialization. Wait for it.
2262 */
2263 wait_for_completion(&pmic_vbus_init);
2264 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302265 break;
2266 default:
2267 break;
2268 }
2269}
2270
2271static void msm_otg_sm_work(struct work_struct *w)
2272{
2273 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002274 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302275 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302276
Steve Mucklef132c6c2012-06-06 18:30:57 -07002277 pm_runtime_resume(otg->phy->dev);
2278 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002279 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302280 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002281 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302282 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302283 psy = power_supply_get_by_name("usb");
2284 if (!psy)
2285 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002286 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302287 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2288 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002289 pm_runtime_put_noidle(otg->phy->dev);
2290 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302291 break;
2292 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302293 /* FALL THROUGH */
2294 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302295 if (test_bit(MHL, &motg->inputs)) {
2296 /* allow LPM */
2297 pm_runtime_put_noidle(otg->phy->dev);
2298 pm_runtime_suspend(otg->phy->dev);
2299 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002300 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302301 pr_debug("!id || id_A\n");
jh.koo78df86f2012-09-04 20:13:19 +09002302 if (slimport_is_connected()) {
2303 work = 1;
2304 break;
2305 }
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302306 if (msm_chg_mhl_detect(motg)) {
2307 work = 1;
2308 break;
2309 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302310 clear_bit(B_BUS_REQ, &motg->inputs);
2311 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002312 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302313 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302314 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302315 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302316 switch (motg->chg_state) {
2317 case USB_CHG_STATE_UNDEFINED:
2318 msm_chg_detect_work(&motg->chg_work.work);
2319 break;
2320 case USB_CHG_STATE_DETECTED:
2321 switch (motg->chg_type) {
2322 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302323 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002324 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302325 /* fall through */
2326 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302327 msm_otg_notify_charger(motg,
2328 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002329 pm_runtime_put_noidle(otg->phy->dev);
2330 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302331 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302332 case USB_ACA_B_CHARGER:
2333 msm_otg_notify_charger(motg,
2334 IDEV_ACA_CHG_MAX);
2335 /*
2336 * (ID_B --> ID_C) PHY_ALT interrupt can
2337 * not be detected in LPM.
2338 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302339 break;
2340 case USB_CDP_CHARGER:
2341 msm_otg_notify_charger(motg,
2342 IDEV_CHG_MAX);
2343 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002344 otg->phy->state =
2345 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302346 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302347 case USB_ACA_C_CHARGER:
2348 msm_otg_notify_charger(motg,
2349 IDEV_ACA_CHG_MAX);
2350 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002351 otg->phy->state =
2352 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302353 break;
2354 case USB_SDP_CHARGER:
jh.koo78df86f2012-09-04 20:13:19 +09002355 if(!slimport_is_connected()) {
2356 msm_otg_start_peripheral(otg, 1);
2357 otg->phy->state =
2358 OTG_STATE_B_PERIPHERAL;
2359 }
kibum.leeec040442012-08-22 22:29:46 +09002360 schedule_delayed_work(&motg->check_ta_work,
2361 MSM_CHECK_TA_DELAY);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302362 break;
2363 default:
2364 break;
2365 }
2366 break;
2367 default:
2368 break;
2369 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302370 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2371 pr_debug("b_sess_end && b_bus_req\n");
2372 if (msm_otg_start_srp(otg) < 0) {
2373 clear_bit(B_BUS_REQ, &motg->inputs);
2374 work = 1;
2375 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302376 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002377 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302378 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2379 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302380 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302381 pr_debug("chg_work cancel");
Mayank Rana9062a672013-01-23 19:26:21 +05302382 clear_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302383 cancel_delayed_work_sync(&motg->chg_work);
kibum.leeec040442012-08-22 22:29:46 +09002384 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302385 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2386 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302387 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002388 msm_otg_reset(otg->phy);
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05302389 /*
2390 * There is a small window where ID interrupt
2391 * is not monitored during ID detection circuit
2392 * switch from ACA to PMIC. Check ID state
2393 * before entering into low power mode.
2394 */
2395 if (!msm_otg_read_pmic_id_state(motg)) {
2396 pr_debug("process missed ID intr\n");
2397 clear_bit(ID, &motg->inputs);
2398 work = 1;
2399 break;
2400 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002401 pm_runtime_put_noidle(otg->phy->dev);
2402 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302403 }
2404 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302405 case OTG_STATE_B_SRP_INIT:
2406 if (!test_bit(ID, &motg->inputs) ||
2407 test_bit(ID_A, &motg->inputs) ||
2408 test_bit(ID_C, &motg->inputs) ||
2409 (test_bit(B_SESS_VLD, &motg->inputs) &&
2410 !test_bit(ID_B, &motg->inputs))) {
2411 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2412 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002413 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302414 /*
2415 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2416 * bits after SRP initiation.
2417 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002418 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302419 work = 1;
2420 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2421 pr_debug("b_srp_fail\n");
2422 pr_info("A-device did not respond to SRP\n");
2423 clear_bit(B_BUS_REQ, &motg->inputs);
2424 clear_bit(B_SRP_FAIL, &motg->tmouts);
2425 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002426 ulpi_write(otg->phy, 0x0, 0x98);
2427 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302428 motg->b_last_se0_sess = jiffies;
2429 work = 1;
2430 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302431 break;
2432 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302433 if (!test_bit(ID, &motg->inputs) ||
2434 test_bit(ID_A, &motg->inputs) ||
2435 test_bit(ID_B, &motg->inputs) ||
2436 !test_bit(B_SESS_VLD, &motg->inputs)) {
2437 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302438 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2439 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302440 msm_otg_notify_charger(motg, 0);
2441 srp_reqd = otg->gadget->otg_srp_reqd;
2442 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302443 if (test_bit(ID_B, &motg->inputs))
2444 clear_bit(ID_B, &motg->inputs);
2445 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002446 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302447 motg->b_last_se0_sess = jiffies;
2448 if (srp_reqd)
2449 msm_otg_start_timer(motg,
2450 TB_TST_SRP, B_TST_SRP);
2451 else
2452 work = 1;
2453 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2454 otg->gadget->b_hnp_enable &&
2455 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2456 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2457 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2458 /* D+ pullup should not be disconnected within 4msec
2459 * after A device suspends the bus. Otherwise PET will
2460 * fail the compliance test.
2461 */
2462 udelay(1000);
2463 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002464 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302465 /*
2466 * start HCD even before A-device enable
2467 * pull-up to meet HNP timings.
2468 */
2469 otg->host->is_b_host = 1;
2470 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002471 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2472 test_bit(B_SESS_VLD, &motg->inputs)) {
2473 pr_debug("a_bus_suspend && b_sess_vld\n");
2474 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002475 pm_runtime_put_noidle(otg->phy->dev);
2476 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002477 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002478 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302479 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002480 }
2481 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302482 case OTG_STATE_B_WAIT_ACON:
2483 if (!test_bit(ID, &motg->inputs) ||
2484 test_bit(ID_A, &motg->inputs) ||
2485 test_bit(ID_B, &motg->inputs) ||
2486 !test_bit(B_SESS_VLD, &motg->inputs)) {
2487 pr_debug("!id || id_a/b || !b_sess_vld\n");
2488 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302489 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302490 * A-device is physically disconnected during
2491 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302492 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302493 msm_otg_start_host(otg, 0);
2494 otg->host->is_b_host = 0;
2495
2496 clear_bit(B_BUS_REQ, &motg->inputs);
2497 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2498 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002499 otg->phy->state = OTG_STATE_B_IDLE;
2500 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302501 work = 1;
2502 } else if (test_bit(A_CONN, &motg->inputs)) {
2503 pr_debug("a_conn\n");
2504 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002505 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302506 /*
2507 * PET disconnects D+ pullup after reset is generated
2508 * by B device in B_HOST role which is not detected by
2509 * B device. As workaorund , start timer of 300msec
2510 * and stop timer if A device is enumerated else clear
2511 * A_CONN.
2512 */
2513 msm_otg_start_timer(motg, TB_TST_CONFIG,
2514 B_TST_CONFIG);
2515 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2516 pr_debug("b_ase0_brst_tmout\n");
2517 pr_info("B HNP fail:No response from A device\n");
2518 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002519 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302520 otg->host->is_b_host = 0;
2521 clear_bit(B_ASE0_BRST, &motg->tmouts);
2522 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2523 clear_bit(B_BUS_REQ, &motg->inputs);
2524 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002525 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302526 work = 1;
2527 } else if (test_bit(ID_C, &motg->inputs)) {
2528 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2529 }
2530 break;
2531 case OTG_STATE_B_HOST:
2532 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2533 !test_bit(A_CONN, &motg->inputs) ||
2534 !test_bit(B_SESS_VLD, &motg->inputs)) {
2535 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2536 clear_bit(A_CONN, &motg->inputs);
2537 clear_bit(B_BUS_REQ, &motg->inputs);
2538 msm_otg_start_host(otg, 0);
2539 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002540 otg->phy->state = OTG_STATE_B_IDLE;
2541 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302542 work = 1;
2543 } else if (test_bit(ID_C, &motg->inputs)) {
2544 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2545 }
2546 break;
2547 case OTG_STATE_A_IDLE:
2548 otg->default_a = 1;
2549 if (test_bit(ID, &motg->inputs) &&
2550 !test_bit(ID_A, &motg->inputs)) {
2551 pr_debug("id && !id_a\n");
2552 otg->default_a = 0;
2553 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002554 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302555 del_timer_sync(&motg->id_timer);
2556 msm_otg_link_reset(motg);
2557 msm_chg_enable_aca_intr(motg);
2558 msm_otg_notify_charger(motg, 0);
2559 work = 1;
2560 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2561 (test_bit(A_SRP_DET, &motg->inputs) ||
2562 test_bit(A_BUS_REQ, &motg->inputs))) {
2563 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2564
2565 clear_bit(A_SRP_DET, &motg->inputs);
2566 /* Disable SRP detection */
2567 writel_relaxed((readl_relaxed(USB_OTGSC) &
2568 ~OTGSC_INTSTS_MASK) &
2569 ~OTGSC_DPIE, USB_OTGSC);
2570
Steve Mucklef132c6c2012-06-06 18:30:57 -07002571 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302572 /* VBUS should not be supplied before end of SRP pulse
2573 * generated by PET, if not complaince test fail.
2574 */
2575 usleep_range(10000, 12000);
2576 /* ACA: ID_A: Stop charging untill enumeration */
2577 if (test_bit(ID_A, &motg->inputs))
2578 msm_otg_notify_charger(motg, 0);
2579 else
2580 msm_hsusb_vbus_power(motg, 1);
2581 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2582 } else {
2583 pr_debug("No session requested\n");
2584 clear_bit(A_BUS_DROP, &motg->inputs);
2585 if (test_bit(ID_A, &motg->inputs)) {
2586 msm_otg_notify_charger(motg,
2587 IDEV_ACA_CHG_MAX);
2588 } else if (!test_bit(ID, &motg->inputs)) {
2589 msm_otg_notify_charger(motg, 0);
2590 /*
2591 * A-device is not providing power on VBUS.
2592 * Enable SRP detection.
2593 */
2594 writel_relaxed(0x13, USB_USBMODE);
2595 writel_relaxed((readl_relaxed(USB_OTGSC) &
2596 ~OTGSC_INTSTS_MASK) |
2597 OTGSC_DPIE, USB_OTGSC);
2598 mb();
2599 }
2600 }
2601 break;
2602 case OTG_STATE_A_WAIT_VRISE:
2603 if ((test_bit(ID, &motg->inputs) &&
2604 !test_bit(ID_A, &motg->inputs)) ||
2605 test_bit(A_BUS_DROP, &motg->inputs) ||
2606 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2607 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2608 clear_bit(A_BUS_REQ, &motg->inputs);
2609 msm_otg_del_timer(motg);
2610 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002611 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302612 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2613 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2614 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002615 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302616 if (TA_WAIT_BCON > 0)
2617 msm_otg_start_timer(motg, TA_WAIT_BCON,
2618 A_WAIT_BCON);
2619 msm_otg_start_host(otg, 1);
2620 msm_chg_enable_aca_det(motg);
2621 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302622 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302623 if (msm_chg_check_aca_intr(motg))
2624 work = 1;
2625 }
2626 break;
2627 case OTG_STATE_A_WAIT_BCON:
2628 if ((test_bit(ID, &motg->inputs) &&
2629 !test_bit(ID_A, &motg->inputs)) ||
2630 test_bit(A_BUS_DROP, &motg->inputs) ||
2631 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2632 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2633 "a_wait_bcon_tmout\n");
2634 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2635 pr_info("Device No Response\n");
2636 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2637 }
2638 msm_otg_del_timer(motg);
2639 clear_bit(A_BUS_REQ, &motg->inputs);
2640 clear_bit(B_CONN, &motg->inputs);
2641 msm_otg_start_host(otg, 0);
2642 /*
2643 * ACA: ID_A with NO accessory, just the A plug is
2644 * attached to ACA: Use IDCHG_MAX for charging
2645 */
2646 if (test_bit(ID_A, &motg->inputs))
2647 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2648 else
2649 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002650 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302651 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2652 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2653 pr_debug("!a_vbus_vld\n");
2654 clear_bit(B_CONN, &motg->inputs);
2655 msm_otg_del_timer(motg);
2656 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002657 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2658 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302659 } else if (test_bit(ID_A, &motg->inputs)) {
2660 msm_hsusb_vbus_power(motg, 0);
2661 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2662 /*
2663 * If TA_WAIT_BCON is infinite, we don;t
2664 * turn off VBUS. Enter low power mode.
2665 */
2666 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002667 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302668 } else if (!test_bit(ID, &motg->inputs)) {
2669 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302670 }
2671 break;
2672 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302673 if ((test_bit(ID, &motg->inputs) &&
2674 !test_bit(ID_A, &motg->inputs)) ||
2675 test_bit(A_BUS_DROP, &motg->inputs)) {
2676 pr_debug("id_a/b/c || a_bus_drop\n");
2677 clear_bit(B_CONN, &motg->inputs);
2678 clear_bit(A_BUS_REQ, &motg->inputs);
2679 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002680 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302681 msm_otg_start_host(otg, 0);
2682 if (!test_bit(ID_A, &motg->inputs))
2683 msm_hsusb_vbus_power(motg, 0);
2684 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2685 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2686 pr_debug("!a_vbus_vld\n");
2687 clear_bit(B_CONN, &motg->inputs);
2688 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002689 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302690 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002691 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302692 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2693 /*
2694 * a_bus_req is de-asserted when root hub is
2695 * suspended or HNP is in progress.
2696 */
2697 pr_debug("!a_bus_req\n");
2698 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002699 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302700 if (otg->host->b_hnp_enable)
2701 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2702 A_AIDL_BDIS);
2703 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002704 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302705 } else if (!test_bit(B_CONN, &motg->inputs)) {
2706 pr_debug("!b_conn\n");
2707 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002708 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302709 if (TA_WAIT_BCON > 0)
2710 msm_otg_start_timer(motg, TA_WAIT_BCON,
2711 A_WAIT_BCON);
2712 if (msm_chg_check_aca_intr(motg))
2713 work = 1;
2714 } else if (test_bit(ID_A, &motg->inputs)) {
2715 msm_otg_del_timer(motg);
2716 msm_hsusb_vbus_power(motg, 0);
2717 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2718 msm_otg_notify_charger(motg,
2719 IDEV_ACA_CHG_MAX);
2720 else
2721 msm_otg_notify_charger(motg,
2722 IDEV_CHG_MIN - motg->mA_port);
2723 } else if (!test_bit(ID, &motg->inputs)) {
2724 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2725 motg->chg_type = USB_INVALID_CHARGER;
2726 msm_otg_notify_charger(motg, 0);
2727 msm_hsusb_vbus_power(motg, 1);
2728 }
2729 break;
2730 case OTG_STATE_A_SUSPEND:
2731 if ((test_bit(ID, &motg->inputs) &&
2732 !test_bit(ID_A, &motg->inputs)) ||
2733 test_bit(A_BUS_DROP, &motg->inputs) ||
2734 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2735 pr_debug("id_a/b/c || a_bus_drop ||"
2736 "a_aidl_bdis_tmout\n");
2737 msm_otg_del_timer(motg);
2738 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002739 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302740 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002741 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302742 if (!test_bit(ID_A, &motg->inputs))
2743 msm_hsusb_vbus_power(motg, 0);
2744 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2745 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2746 pr_debug("!a_vbus_vld\n");
2747 msm_otg_del_timer(motg);
2748 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002749 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302750 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002751 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302752 } else if (!test_bit(B_CONN, &motg->inputs) &&
2753 otg->host->b_hnp_enable) {
2754 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002755 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302756 msm_otg_host_hnp_enable(otg, 1);
2757 otg->gadget->is_a_peripheral = 1;
2758 msm_otg_start_peripheral(otg, 1);
2759 } else if (!test_bit(B_CONN, &motg->inputs) &&
2760 !otg->host->b_hnp_enable) {
2761 pr_debug("!b_conn && !b_hnp_enable");
2762 /*
2763 * bus request is dropped during suspend.
2764 * acquire again for next device.
2765 */
2766 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002767 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302768 if (TA_WAIT_BCON > 0)
2769 msm_otg_start_timer(motg, TA_WAIT_BCON,
2770 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002771 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302772 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002773 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302774 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002775 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002776 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302777 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302778 }
2779 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302780 case OTG_STATE_A_PERIPHERAL:
2781 if ((test_bit(ID, &motg->inputs) &&
2782 !test_bit(ID_A, &motg->inputs)) ||
2783 test_bit(A_BUS_DROP, &motg->inputs)) {
2784 pr_debug("id _f/b/c || a_bus_drop\n");
2785 /* Clear BIDL_ADIS timer */
2786 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002787 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302788 msm_otg_start_peripheral(otg, 0);
2789 otg->gadget->is_a_peripheral = 0;
2790 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002791 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302792 if (!test_bit(ID_A, &motg->inputs))
2793 msm_hsusb_vbus_power(motg, 0);
2794 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2795 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2796 pr_debug("!a_vbus_vld\n");
2797 /* Clear BIDL_ADIS timer */
2798 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002799 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302800 msm_otg_start_peripheral(otg, 0);
2801 otg->gadget->is_a_peripheral = 0;
2802 msm_otg_start_host(otg, 0);
2803 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2804 pr_debug("a_bidl_adis_tmout\n");
2805 msm_otg_start_peripheral(otg, 0);
2806 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002807 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302808 set_bit(A_BUS_REQ, &motg->inputs);
2809 msm_otg_host_hnp_enable(otg, 0);
2810 if (TA_WAIT_BCON > 0)
2811 msm_otg_start_timer(motg, TA_WAIT_BCON,
2812 A_WAIT_BCON);
2813 } else if (test_bit(ID_A, &motg->inputs)) {
2814 msm_hsusb_vbus_power(motg, 0);
2815 msm_otg_notify_charger(motg,
2816 IDEV_CHG_MIN - motg->mA_port);
2817 } else if (!test_bit(ID, &motg->inputs)) {
2818 msm_otg_notify_charger(motg, 0);
2819 msm_hsusb_vbus_power(motg, 1);
2820 }
2821 break;
2822 case OTG_STATE_A_WAIT_VFALL:
2823 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2824 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002825 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302826 work = 1;
2827 }
2828 break;
2829 case OTG_STATE_A_VBUS_ERR:
2830 if ((test_bit(ID, &motg->inputs) &&
2831 !test_bit(ID_A, &motg->inputs)) ||
2832 test_bit(A_BUS_DROP, &motg->inputs) ||
2833 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002834 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302835 if (!test_bit(ID_A, &motg->inputs))
2836 msm_hsusb_vbus_power(motg, 0);
2837 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2838 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2839 motg->chg_type = USB_INVALID_CHARGER;
2840 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302841 }
2842 break;
2843 default:
2844 break;
2845 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302846 if (work)
2847 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302848}
2849
2850static irqreturn_t msm_otg_irq(int irq, void *data)
2851{
2852 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002853 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302854 u32 otgsc = 0, usbsts, pc;
2855 bool work = 0;
2856 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302857
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302858 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002859 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302860 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002861 motg->async_int = irq;
Jack Phamb1aa4d82012-08-13 15:32:39 -07002862 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002863 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302864 return IRQ_HANDLED;
2865 }
2866
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002867 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302868 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302869
2870 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302871 return IRQ_NONE;
2872
2873 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302874 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302875 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302876 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302877 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302878 pr_debug("Id clear\n");
2879 /*
2880 * Assert a_bus_req to supply power on
2881 * VBUS when Micro/Mini-A cable is connected
2882 * with out user intervention.
2883 */
2884 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302885 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302886 msm_chg_enable_aca_det(motg);
2887 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302888 writel_relaxed(otgsc, USB_OTGSC);
2889 work = 1;
2890 } else if (otgsc & OTGSC_DPIS) {
2891 pr_debug("DPIS detected\n");
2892 writel_relaxed(otgsc, USB_OTGSC);
2893 set_bit(A_SRP_DET, &motg->inputs);
2894 set_bit(A_BUS_REQ, &motg->inputs);
2895 work = 1;
2896 } else if (otgsc & OTGSC_BSVIS) {
2897 writel_relaxed(otgsc, USB_OTGSC);
2898 /*
2899 * BSV interrupt comes when operating as an A-device
2900 * (VBUS on/off).
2901 * But, handle BSV when charger is removed from ACA in ID_A
2902 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002903 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302904 !test_bit(ID_A, &motg->inputs))
2905 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302906 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302907 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302908 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302909 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302910 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302911 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002912 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2913
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302914 msm_chg_check_aca_intr(motg);
2915 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302916 work = 1;
2917 } else if (usbsts & STS_PCI) {
2918 pc = readl_relaxed(USB_PORTSC);
2919 pr_debug("portsc = %x\n", pc);
2920 ret = IRQ_NONE;
2921 /*
2922 * HCD Acks PCI interrupt. We use this to switch
2923 * between different OTG states.
2924 */
2925 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002926 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302927 case OTG_STATE_A_SUSPEND:
2928 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2929 !(pc & PORTSC_CCS)) {
2930 pr_debug("B_CONN clear\n");
2931 clear_bit(B_CONN, &motg->inputs);
2932 msm_otg_del_timer(motg);
2933 }
2934 break;
2935 case OTG_STATE_A_PERIPHERAL:
2936 /*
2937 * A-peripheral observed activity on bus.
2938 * clear A_BIDL_ADIS timer.
2939 */
2940 msm_otg_del_timer(motg);
2941 work = 0;
2942 break;
2943 case OTG_STATE_B_WAIT_ACON:
2944 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2945 pr_debug("A_CONN set\n");
2946 set_bit(A_CONN, &motg->inputs);
2947 /* Clear ASE0_BRST timer */
2948 msm_otg_del_timer(motg);
2949 }
2950 break;
2951 case OTG_STATE_B_HOST:
2952 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2953 pr_debug("A_CONN clear\n");
2954 clear_bit(A_CONN, &motg->inputs);
2955 msm_otg_del_timer(motg);
2956 }
2957 break;
2958 case OTG_STATE_A_WAIT_BCON:
2959 if (TA_WAIT_BCON < 0)
2960 set_bit(A_BUS_REQ, &motg->inputs);
2961 default:
2962 work = 0;
2963 break;
2964 }
2965 } else if (usbsts & STS_URI) {
2966 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002967 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302968 case OTG_STATE_A_PERIPHERAL:
2969 /*
2970 * A-peripheral observed activity on bus.
2971 * clear A_BIDL_ADIS timer.
2972 */
2973 msm_otg_del_timer(motg);
2974 work = 0;
2975 break;
2976 default:
2977 work = 0;
2978 break;
2979 }
2980 } else if (usbsts & STS_SLI) {
2981 ret = IRQ_NONE;
2982 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002983 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302984 case OTG_STATE_B_PERIPHERAL:
2985 if (otg->gadget->b_hnp_enable) {
2986 set_bit(A_BUS_SUSPEND, &motg->inputs);
2987 set_bit(B_BUS_REQ, &motg->inputs);
2988 work = 1;
2989 }
2990 break;
2991 case OTG_STATE_A_PERIPHERAL:
2992 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2993 A_BIDL_ADIS);
2994 break;
2995 default:
2996 break;
2997 }
2998 } else if ((usbsts & PHY_ALT_INT)) {
2999 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
3000 if (msm_chg_check_aca_intr(motg))
3001 work = 1;
3002 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303003 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303004 if (work)
3005 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303006
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303007 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003008}
3009
3010static void msm_otg_set_vbus_state(int online)
3011{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303012 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003013 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05303014 struct usb_otg *otg = motg->phy.otg;
3015
3016 /* In A Host Mode, ignore received BSV interrupts */
3017 if (otg->phy->state >= OTG_STATE_A_IDLE)
3018 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003019
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303020 if (online) {
3021 pr_debug("PMIC: BSV set\n");
3022 set_bit(B_SESS_VLD, &motg->inputs);
3023 } else {
3024 pr_debug("PMIC: BSV clear\n");
3025 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303026 }
3027
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303028 if (!init) {
3029 init = true;
3030 complete(&pmic_vbus_init);
3031 pr_debug("PMIC: BSV init complete\n");
3032 return;
3033 }
3034
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303035 if (test_bit(MHL, &motg->inputs) ||
3036 mhl_det_in_progress) {
3037 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
3038 return;
3039 }
3040
Jack Pham5ca279b2012-05-14 18:42:54 -07003041 if (atomic_read(&motg->pm_suspended))
3042 motg->sm_work_pending = true;
3043 else
3044 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003045}
3046
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303047static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003048{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303049 struct msm_otg *motg = container_of(w, struct msm_otg,
3050 pmic_id_status_work.work);
3051 int work = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003052
Pavankumar Kondeti7ccbdc92012-11-06 16:45:10 +05303053 if (msm_otg_read_pmic_id_state(motg)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303054 if (!test_and_set_bit(ID, &motg->inputs)) {
3055 pr_debug("PMIC: ID set\n");
3056 work = 1;
3057 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303058 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303059 if (test_and_clear_bit(ID, &motg->inputs)) {
3060 pr_debug("PMIC: ID clear\n");
3061 set_bit(A_BUS_REQ, &motg->inputs);
3062 work = 1;
3063 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05303064 }
3065
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303066 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003067 if (atomic_read(&motg->pm_suspended))
3068 motg->sm_work_pending = true;
3069 else
3070 queue_work(system_nrt_wq, &motg->sm_work);
3071 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303072
3073}
3074
3075#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
3076static irqreturn_t msm_pmic_id_irq(int irq, void *data)
3077{
3078 struct msm_otg *motg = data;
3079
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303080 if (test_bit(MHL, &motg->inputs) ||
3081 mhl_det_in_progress) {
3082 pr_debug("PMIC: Id interrupt ignored in MHL\n");
3083 return IRQ_HANDLED;
3084 }
3085
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303086 if (!aca_id_turned_on)
3087 /*schedule delayed work for 5msec for ID line state to settle*/
3088 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
3089 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003090
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303091 return IRQ_HANDLED;
3092}
3093
3094static int msm_otg_mode_show(struct seq_file *s, void *unused)
3095{
3096 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003097 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303098
Steve Mucklef132c6c2012-06-06 18:30:57 -07003099 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303100 case OTG_STATE_A_HOST:
3101 seq_printf(s, "host\n");
3102 break;
3103 case OTG_STATE_B_PERIPHERAL:
3104 seq_printf(s, "peripheral\n");
3105 break;
3106 default:
3107 seq_printf(s, "none\n");
3108 break;
3109 }
3110
3111 return 0;
3112}
3113
3114static int msm_otg_mode_open(struct inode *inode, struct file *file)
3115{
3116 return single_open(file, msm_otg_mode_show, inode->i_private);
3117}
3118
3119static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3120 size_t count, loff_t *ppos)
3121{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303122 struct seq_file *s = file->private_data;
3123 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303124 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003125 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303126 int status = count;
3127 enum usb_mode_type req_mode;
3128
3129 memset(buf, 0x00, sizeof(buf));
3130
3131 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3132 status = -EFAULT;
3133 goto out;
3134 }
3135
3136 if (!strncmp(buf, "host", 4)) {
3137 req_mode = USB_HOST;
3138 } else if (!strncmp(buf, "peripheral", 10)) {
3139 req_mode = USB_PERIPHERAL;
3140 } else if (!strncmp(buf, "none", 4)) {
3141 req_mode = USB_NONE;
3142 } else {
3143 status = -EINVAL;
3144 goto out;
3145 }
3146
3147 switch (req_mode) {
3148 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003149 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303150 case OTG_STATE_A_HOST:
3151 case OTG_STATE_B_PERIPHERAL:
3152 set_bit(ID, &motg->inputs);
3153 clear_bit(B_SESS_VLD, &motg->inputs);
3154 break;
3155 default:
3156 goto out;
3157 }
3158 break;
3159 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003160 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303161 case OTG_STATE_B_IDLE:
3162 case OTG_STATE_A_HOST:
3163 set_bit(ID, &motg->inputs);
3164 set_bit(B_SESS_VLD, &motg->inputs);
3165 break;
3166 default:
3167 goto out;
3168 }
3169 break;
3170 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003171 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303172 case OTG_STATE_B_IDLE:
3173 case OTG_STATE_B_PERIPHERAL:
3174 clear_bit(ID, &motg->inputs);
3175 break;
3176 default:
3177 goto out;
3178 }
3179 break;
3180 default:
3181 goto out;
3182 }
3183
Steve Mucklef132c6c2012-06-06 18:30:57 -07003184 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303185 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303186out:
3187 return status;
3188}
3189
3190const struct file_operations msm_otg_mode_fops = {
3191 .open = msm_otg_mode_open,
3192 .read = seq_read,
3193 .write = msm_otg_mode_write,
3194 .llseek = seq_lseek,
3195 .release = single_release,
3196};
3197
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303198static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3199{
3200 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003201 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303202
Steve Mucklef132c6c2012-06-06 18:30:57 -07003203 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303204 return 0;
3205}
3206
3207static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3208{
3209 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3210}
3211
3212const struct file_operations msm_otg_state_fops = {
3213 .open = msm_otg_otg_state_open,
3214 .read = seq_read,
3215 .llseek = seq_lseek,
3216 .release = single_release,
3217};
3218
Anji jonnalad270e2d2011-08-09 11:28:32 +05303219static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3220{
3221 struct msm_otg *motg = s->private;
3222
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303223 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303224 return 0;
3225}
3226
3227static int msm_otg_chg_open(struct inode *inode, struct file *file)
3228{
3229 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3230}
3231
3232const struct file_operations msm_otg_chg_fops = {
3233 .open = msm_otg_chg_open,
3234 .read = seq_read,
3235 .llseek = seq_lseek,
3236 .release = single_release,
3237};
3238
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303239static int msm_otg_aca_show(struct seq_file *s, void *unused)
3240{
3241 if (debug_aca_enabled)
3242 seq_printf(s, "enabled\n");
3243 else
3244 seq_printf(s, "disabled\n");
3245
3246 return 0;
3247}
3248
3249static int msm_otg_aca_open(struct inode *inode, struct file *file)
3250{
3251 return single_open(file, msm_otg_aca_show, inode->i_private);
3252}
3253
3254static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3255 size_t count, loff_t *ppos)
3256{
3257 char buf[8];
3258
3259 memset(buf, 0x00, sizeof(buf));
3260
3261 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3262 return -EFAULT;
3263
3264 if (!strncmp(buf, "enable", 6))
3265 debug_aca_enabled = true;
3266 else
3267 debug_aca_enabled = false;
3268
3269 return count;
3270}
3271
3272const struct file_operations msm_otg_aca_fops = {
3273 .open = msm_otg_aca_open,
3274 .read = seq_read,
3275 .write = msm_otg_aca_write,
3276 .llseek = seq_lseek,
3277 .release = single_release,
3278};
3279
Manu Gautam8bdcc592012-03-06 11:26:06 +05303280static int msm_otg_bus_show(struct seq_file *s, void *unused)
3281{
3282 if (debug_bus_voting_enabled)
3283 seq_printf(s, "enabled\n");
3284 else
3285 seq_printf(s, "disabled\n");
3286
3287 return 0;
3288}
3289
3290static int msm_otg_bus_open(struct inode *inode, struct file *file)
3291{
3292 return single_open(file, msm_otg_bus_show, inode->i_private);
3293}
3294
3295static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3296 size_t count, loff_t *ppos)
3297{
3298 char buf[8];
3299 int ret;
3300 struct seq_file *s = file->private_data;
3301 struct msm_otg *motg = s->private;
3302
3303 memset(buf, 0x00, sizeof(buf));
3304
3305 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3306 return -EFAULT;
3307
3308 if (!strncmp(buf, "enable", 6)) {
3309 /* Do not vote here. Let OTG statemachine decide when to vote */
3310 debug_bus_voting_enabled = true;
3311 } else {
3312 debug_bus_voting_enabled = false;
3313 if (motg->bus_perf_client) {
3314 ret = msm_bus_scale_client_update_request(
3315 motg->bus_perf_client, 0);
3316 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003317 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303318 "for bus bw %d\n", __func__, ret);
3319 }
3320 }
3321
3322 return count;
3323}
3324
3325const struct file_operations msm_otg_bus_fops = {
3326 .open = msm_otg_bus_open,
3327 .read = seq_read,
3328 .write = msm_otg_bus_write,
3329 .llseek = seq_lseek,
3330 .release = single_release,
3331};
3332
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303333static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303334
3335static int msm_otg_debugfs_init(struct msm_otg *motg)
3336{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303337 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303338
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303339 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3340
3341 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3342 return -ENODEV;
3343
Anji jonnalad270e2d2011-08-09 11:28:32 +05303344 if (motg->pdata->mode == USB_OTG &&
3345 motg->pdata->otg_control == OTG_USER_CONTROL) {
3346
Manu Gautam8bdcc592012-03-06 11:26:06 +05303347 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303348 S_IWUSR, msm_otg_dbg_root, motg,
3349 &msm_otg_mode_fops);
3350
Manu Gautam8bdcc592012-03-06 11:26:06 +05303351 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303352 debugfs_remove(msm_otg_dbg_root);
3353 msm_otg_dbg_root = NULL;
3354 return -ENODEV;
3355 }
3356 }
3357
Manu Gautam8bdcc592012-03-06 11:26:06 +05303358 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303359 msm_otg_dbg_root, motg,
3360 &msm_otg_chg_fops);
3361
Manu Gautam8bdcc592012-03-06 11:26:06 +05303362 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303363 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303364 return -ENODEV;
3365 }
3366
Manu Gautam8bdcc592012-03-06 11:26:06 +05303367 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303368 msm_otg_dbg_root, motg,
3369 &msm_otg_aca_fops);
3370
Manu Gautam8bdcc592012-03-06 11:26:06 +05303371 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303372 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303373 return -ENODEV;
3374 }
3375
Manu Gautam8bdcc592012-03-06 11:26:06 +05303376 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3377 msm_otg_dbg_root, motg,
3378 &msm_otg_bus_fops);
3379
3380 if (!msm_otg_dentry) {
3381 debugfs_remove_recursive(msm_otg_dbg_root);
3382 return -ENODEV;
3383 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303384
3385 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3386 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3387
3388 if (!msm_otg_dentry) {
3389 debugfs_remove_recursive(msm_otg_dbg_root);
3390 return -ENODEV;
3391 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303392 return 0;
3393}
3394
3395static void msm_otg_debugfs_cleanup(void)
3396{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303397 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303398}
3399
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303400static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3401static struct platform_device *msm_otg_add_pdev(
3402 struct platform_device *ofdev, const char *name)
3403{
3404 struct platform_device *pdev;
3405 const struct resource *res = ofdev->resource;
3406 unsigned int num = ofdev->num_resources;
3407 int retval;
3408
3409 pdev = platform_device_alloc(name, -1);
3410 if (!pdev) {
3411 retval = -ENOMEM;
3412 goto error;
3413 }
3414
3415 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3416 pdev->dev.dma_mask = &msm_otg_dma_mask;
3417
3418 if (num) {
3419 retval = platform_device_add_resources(pdev, res, num);
3420 if (retval)
3421 goto error;
3422 }
3423
3424 retval = platform_device_add(pdev);
3425 if (retval)
3426 goto error;
3427
3428 return pdev;
3429
3430error:
3431 platform_device_put(pdev);
3432 return ERR_PTR(retval);
3433}
3434
3435static int msm_otg_setup_devices(struct platform_device *ofdev,
3436 enum usb_mode_type mode, bool init)
3437{
3438 const char *gadget_name = "msm_hsusb";
3439 const char *host_name = "msm_hsusb_host";
3440 static struct platform_device *gadget_pdev;
3441 static struct platform_device *host_pdev;
3442 int retval = 0;
3443
3444 if (!init) {
3445 if (gadget_pdev)
3446 platform_device_unregister(gadget_pdev);
3447 if (host_pdev)
3448 platform_device_unregister(host_pdev);
3449 return 0;
3450 }
3451
3452 switch (mode) {
3453 case USB_OTG:
3454 /* fall through */
3455 case USB_PERIPHERAL:
3456 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3457 if (IS_ERR(gadget_pdev)) {
3458 retval = PTR_ERR(gadget_pdev);
3459 break;
3460 }
3461 if (mode == USB_PERIPHERAL)
3462 break;
3463 /* fall through */
3464 case USB_HOST:
3465 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3466 if (IS_ERR(host_pdev)) {
3467 retval = PTR_ERR(host_pdev);
3468 if (mode == USB_OTG)
3469 platform_device_unregister(gadget_pdev);
3470 }
3471 break;
3472 default:
3473 break;
3474 }
3475
3476 return retval;
3477}
3478
3479struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3480{
3481 struct device_node *node = pdev->dev.of_node;
3482 struct msm_otg_platform_data *pdata;
3483 int len = 0;
3484
3485 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3486 if (!pdata) {
3487 pr_err("unable to allocate platform data\n");
3488 return NULL;
3489 }
3490 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3491 if (len) {
3492 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3493 if (!pdata->phy_init_seq)
3494 return NULL;
3495 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3496 pdata->phy_init_seq,
3497 len/sizeof(*pdata->phy_init_seq));
3498 }
3499 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3500 &pdata->power_budget);
3501 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3502 &pdata->mode);
3503 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3504 &pdata->otg_control);
3505 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3506 &pdata->default_mode);
3507 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3508 &pdata->phy_type);
3509 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3510 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303511 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3512 "qcom,hsusb-otg-disable-reset");
3513
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303514 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303515}
3516
3517static int __init msm_otg_probe(struct platform_device *pdev)
3518{
3519 int ret = 0;
3520 struct resource *res;
3521 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003522 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303523 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303524
3525 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303526
3527 if (pdev->dev.of_node) {
3528 dev_dbg(&pdev->dev, "device tree enabled\n");
3529 pdata = msm_otg_dt_to_pdata(pdev);
3530 if (!pdata)
3531 return -ENOMEM;
3532 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3533 if (ret) {
3534 dev_err(&pdev->dev, "devices setup failed\n");
3535 return ret;
3536 }
3537 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303538 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3539 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303540 } else {
3541 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303542 }
3543
3544 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3545 if (!motg) {
3546 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3547 return -ENOMEM;
3548 }
3549
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003550 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3551 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003552 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3553 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303554 goto free_motg;
3555 }
3556
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003557 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303558 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003559 phy = &motg->phy;
3560 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303561
3562 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303563 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3564 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3565 * PMIC can detect ACA ID_GND and generate an interrupt.
3566 */
3567 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3568 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3569 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003570 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303571 }
3572
Ofir Cohen4da266f2012-01-03 10:19:29 +02003573 /* initialize reset counter */
3574 motg->reset_counter = 0;
3575
Amit Blay02eff132011-09-21 16:46:24 +03003576 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003577 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003578 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003579 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303580
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303581 /*
3582 * Targets on which link uses asynchronous reset methodology,
3583 * free running clock is not required during the reset.
3584 */
Manu Gautam5143b252012-01-05 19:25:23 -08003585 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303586 if (IS_ERR(motg->clk))
3587 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3588 else
3589 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303590
3591 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003592 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303593 * CORE CLK must be running at >55Mhz for correct HSUSB
3594 * operation and USB core cannot tolerate frequency changes on
3595 * CORE CLK. For such USB cores, vote for maximum clk frequency
3596 * on pclk source
3597 */
Manu Gautam5143b252012-01-05 19:25:23 -08003598 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3599 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303600 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003601 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303602 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003603 goto put_clk;
3604 }
3605 clk_set_rate(motg->core_clk, INT_MAX);
3606
3607 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3608 if (IS_ERR(motg->pclk)) {
3609 dev_err(&pdev->dev, "failed to get iface_clk\n");
3610 ret = PTR_ERR(motg->pclk);
3611 goto put_core_clk;
3612 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303613
3614 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3615 if (!res) {
3616 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3617 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003618 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303619 }
3620
3621 motg->regs = ioremap(res->start, resource_size(res));
3622 if (!motg->regs) {
3623 dev_err(&pdev->dev, "ioremap failed\n");
3624 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003625 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303626 }
3627 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3628
3629 motg->irq = platform_get_irq(pdev, 0);
3630 if (!motg->irq) {
3631 dev_err(&pdev->dev, "platform_get_irq failed\n");
3632 ret = -ENODEV;
3633 goto free_regs;
3634 }
3635
Manu Gautamf8c45642012-08-10 10:20:56 -07003636 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3637 if (motg->async_irq < 0) {
3638 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3639 motg->async_irq = 0;
3640 }
3641
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003642 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003643 if (IS_ERR(motg->xo_handle)) {
3644 dev_err(&pdev->dev, "%s not able to get the handle "
3645 "to vote for TCXO D0 buffer\n", __func__);
3646 ret = PTR_ERR(motg->xo_handle);
3647 goto free_regs;
3648 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303649
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003650 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003651 if (ret) {
3652 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3653 "D0 buffer%d\n", __func__, ret);
3654 goto free_xo_handle;
3655 }
3656
Manu Gautam28b1bac2012-01-30 16:43:06 +05303657 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303658
Mayank Rana248698c2012-04-19 00:03:16 +05303659 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003660 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303661 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003662 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303663 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003664 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003665 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303666 goto devote_xo_handle;
3667 }
3668 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303669 }
3670
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003671 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303672 if (ret) {
3673 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303674 goto devote_xo_handle;
3675 }
3676
3677 ret = regulator_enable(hsusb_vddcx);
3678 if (ret) {
3679 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3680 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303681 }
3682
3683 ret = msm_hsusb_ldo_init(motg, 1);
3684 if (ret) {
3685 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303686 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303687 }
3688
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303689 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303690 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3691 "mhl_usb_hs_switch");
3692 if (IS_ERR(mhl_usb_hs_switch)) {
3693 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003694 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303695 goto free_ldo_init;
3696 }
3697 }
3698
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003699 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303700 if (ret) {
3701 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003702 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303703 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303704 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303705
3706 writel(0, USB_USBINTR);
3707 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003708 /* Ensure that above STOREs are completed before enabling interrupts */
3709 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303710
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303711 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3712 if (ret)
3713 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003714 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303715 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303716 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303717 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303718 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
kibum.leeec040442012-08-22 22:29:46 +09003719 INIT_DELAYED_WORK(&motg->check_ta_work, msm_ta_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303720 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3721 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303722 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3723 "msm_otg", motg);
3724 if (ret) {
3725 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003726 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303727 }
3728
Manu Gautamf8c45642012-08-10 10:20:56 -07003729 if (motg->async_irq) {
3730 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3731 "msm_otg", motg);
3732 if (ret) {
3733 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3734 goto free_irq;
3735 }
3736 disable_irq(motg->async_irq);
3737 }
3738
Jack Pham87f202f2012-08-06 00:24:22 -07003739 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3740 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3741
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003742 phy->init = msm_otg_reset;
3743 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003744 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303745
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003746 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303747
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003748 phy->otg->phy = &motg->phy;
3749 phy->otg->set_host = msm_otg_set_host;
3750 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003751 phy->otg->start_hnp = msm_otg_start_hnp;
3752 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003753
3754 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303755 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003756 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003757 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303758 }
3759
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303760 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303761 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003762 if (motg->pdata->pmic_id_irq) {
3763 ret = request_irq(motg->pdata->pmic_id_irq,
3764 msm_pmic_id_irq,
3765 IRQF_TRIGGER_RISING |
3766 IRQF_TRIGGER_FALLING,
3767 "msm_otg", motg);
3768 if (ret) {
3769 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003770 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003771 }
3772 } else {
3773 ret = -ENODEV;
3774 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003775 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003776 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303777 }
3778
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303779 msm_hsusb_mhl_switch_enable(motg, 1);
3780
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303781 platform_set_drvdata(pdev, motg);
3782 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003783 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303784
Anji jonnalad270e2d2011-08-09 11:28:32 +05303785 ret = msm_otg_debugfs_init(motg);
3786 if (ret)
3787 dev_dbg(&pdev->dev, "mode debugfs file is"
3788 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303789
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003790 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3791 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3792
Amit Blay58b31472011-11-18 09:39:39 +02003793 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3794 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303795 (!(motg->pdata->mode == USB_OTG) ||
3796 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003797 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303798 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003799
Amit Blay58b31472011-11-18 09:39:39 +02003800 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3801 motg->caps = ALLOW_PHY_RETENTION;
3802 }
3803
Amit Blay6fa647a2012-05-24 14:12:08 +03003804 if (motg->pdata->enable_lpm_on_dev_suspend)
3805 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3806
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003807 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303808 pm_runtime_set_active(&pdev->dev);
3809 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303810
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303811 if (motg->pdata->bus_scale_table) {
3812 motg->bus_perf_client =
3813 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3814 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003815 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303816 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303817 else
3818 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303819 }
3820
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303821 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003822
Steve Mucklef132c6c2012-06-06 18:30:57 -07003823remove_phy:
3824 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003825free_async_irq:
3826 if (motg->async_irq)
3827 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303828free_irq:
3829 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003830destroy_wlock:
3831 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303832 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003833 msm_hsusb_ldo_enable(motg, 0);
3834free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303835 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303836free_hsusb_vddcx:
3837 regulator_disable(hsusb_vddcx);
3838free_config_vddcx:
3839 regulator_set_voltage(hsusb_vddcx,
3840 vdd_val[motg->vdd_type][VDD_NONE],
3841 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003842devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303843 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003844 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003845free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003846 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303847free_regs:
3848 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003849put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303850 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303851put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003852 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303853put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303854 if (!IS_ERR(motg->clk))
3855 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003856 if (!IS_ERR(motg->phy_reset_clk))
3857 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003858free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003859 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303860free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303861 kfree(motg);
3862 return ret;
3863}
3864
3865static int __devexit msm_otg_remove(struct platform_device *pdev)
3866{
3867 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003868 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303869 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303870
3871 if (otg->host || otg->gadget)
3872 return -EBUSY;
3873
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303874 if (pdev->dev.of_node)
3875 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003876 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3877 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303878 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303879 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303880 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303881 cancel_delayed_work_sync(&motg->pmic_id_status_work);
kibum.leeec040442012-08-22 22:29:46 +09003882 cancel_delayed_work_sync(&motg->check_ta_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303883 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303884
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303885 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303886
3887 device_init_wakeup(&pdev->dev, 0);
3888 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003889 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303890
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303891 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003892 if (motg->pdata->pmic_id_irq)
3893 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003894 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303895 free_irq(motg->irq, motg);
3896
Jack Pham87f202f2012-08-06 00:24:22 -07003897 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3898 motg->pdata->mpm_otgsessvld_int)
3899 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3900
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303901 /*
3902 * Put PHY in low power mode.
3903 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003904 ulpi_read(otg->phy, 0x14);
3905 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303906
3907 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3908 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3909 if (readl(USB_PORTSC) & PORTSC_PHCD)
3910 break;
3911 udelay(1);
3912 cnt++;
3913 }
3914 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003915 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303916
Manu Gautam28b1bac2012-01-30 16:43:06 +05303917 clk_disable_unprepare(motg->pclk);
3918 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003919 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003920 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303921 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303922 regulator_disable(hsusb_vddcx);
3923 regulator_set_voltage(hsusb_vddcx,
3924 vdd_val[motg->vdd_type][VDD_NONE],
3925 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303926
3927 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303928 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303929
Amit Blay02eff132011-09-21 16:46:24 +03003930 if (!IS_ERR(motg->phy_reset_clk))
3931 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303932 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303933 if (!IS_ERR(motg->clk))
3934 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003935 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303936
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303937 if (motg->bus_perf_client)
3938 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303939
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003940 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303941 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303942 return 0;
3943}
3944
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303945#ifdef CONFIG_PM_RUNTIME
3946static int msm_otg_runtime_idle(struct device *dev)
3947{
3948 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003949 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303950
3951 dev_dbg(dev, "OTG runtime idle\n");
3952
Steve Mucklef132c6c2012-06-06 18:30:57 -07003953 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303954 return -EAGAIN;
3955 else
3956 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303957}
3958
3959static int msm_otg_runtime_suspend(struct device *dev)
3960{
3961 struct msm_otg *motg = dev_get_drvdata(dev);
3962
3963 dev_dbg(dev, "OTG runtime suspend\n");
3964 return msm_otg_suspend(motg);
3965}
3966
3967static int msm_otg_runtime_resume(struct device *dev)
3968{
3969 struct msm_otg *motg = dev_get_drvdata(dev);
3970
3971 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303972 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303973 return msm_otg_resume(motg);
3974}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303975#endif
3976
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303977#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303978static int msm_otg_pm_suspend(struct device *dev)
3979{
Jack Pham5ca279b2012-05-14 18:42:54 -07003980 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303981 struct msm_otg *motg = dev_get_drvdata(dev);
3982
3983 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003984
3985 atomic_set(&motg->pm_suspended, 1);
3986 ret = msm_otg_suspend(motg);
3987 if (ret)
3988 atomic_set(&motg->pm_suspended, 0);
3989
3990 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303991}
3992
3993static int msm_otg_pm_resume(struct device *dev)
3994{
Jack Pham5ca279b2012-05-14 18:42:54 -07003995 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303996 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303997
3998 dev_dbg(dev, "OTG PM resume\n");
3999
Jack Pham5ca279b2012-05-14 18:42:54 -07004000 atomic_set(&motg->pm_suspended, 0);
Jack Phamb1aa4d82012-08-13 15:32:39 -07004001 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07004002 pm_runtime_get_noresume(dev);
4003 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304004
Jack Pham5ca279b2012-05-14 18:42:54 -07004005 /* Update runtime PM status */
4006 pm_runtime_disable(dev);
4007 pm_runtime_set_active(dev);
4008 pm_runtime_enable(dev);
4009
Jack Phamb1aa4d82012-08-13 15:32:39 -07004010 if (motg->sm_work_pending) {
4011 motg->sm_work_pending = false;
4012 queue_work(system_nrt_wq, &motg->sm_work);
4013 }
Jack Pham5ca279b2012-05-14 18:42:54 -07004014 }
4015
4016 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304017}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304018#endif
4019
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304020#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304021static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304022 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
4023 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
4024 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304025};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304026#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304027
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304028static struct of_device_id msm_otg_dt_match[] = {
4029 { .compatible = "qcom,hsusb-otg",
4030 },
4031 {}
4032};
4033
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304034static struct platform_driver msm_otg_driver = {
4035 .remove = __devexit_p(msm_otg_remove),
4036 .driver = {
4037 .name = DRIVER_NAME,
4038 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304039#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05304040 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05304041#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05304042 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05304043 },
4044};
4045
4046static int __init msm_otg_init(void)
4047{
4048 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
4049}
4050
4051static void __exit msm_otg_exit(void)
4052{
4053 platform_driver_unregister(&msm_otg_driver);
4054}
4055
4056module_init(msm_otg_init);
4057module_exit(msm_otg_exit);
4058
4059MODULE_LICENSE("GPL v2");
4060MODULE_DESCRIPTION("MSM USB transceiver driver");