blob: da96e738a5ebf7c856e3164313f7e41a09400069 [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020042#include <linux/power_supply.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053043#include <linux/mhl_8334.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
45#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080046#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053047#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053048#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053049
50#define MSM_USB_BASE (motg->regs)
51#define DRIVER_NAME "msm_otg"
52
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053053#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053054#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053055#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
56#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
57#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
58#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
59
60#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
61#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
62#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
63#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
64
Mayank Rana248698c2012-04-19 00:03:16 +053065#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080066#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053067#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
68
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053069static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070070static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053071static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053072static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053073static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074
Anji jonnala11aa5c42011-05-04 10:19:48 +053075static struct regulator *hsusb_3p3;
76static struct regulator *hsusb_1p8;
77static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053078static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +053079static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053080static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053081
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053082static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053083static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +053084{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053085#ifdef CONFIG_USB_MSM_ACA
86 return true;
87#else
88 return debug_aca_enabled;
89#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +053090}
91
Mayank Rana248698c2012-04-19 00:03:16 +053092static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
93 { /* VDD_CX CORNER Voting */
94 [VDD_NONE] = RPM_VREG_CORNER_NONE,
95 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
96 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
97 },
98 { /* VDD_CX Voltage Voting */
99 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
100 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
101 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
102 },
103};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530104
105static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
106{
107 int rc = 0;
108
109 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700110 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530111 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200112 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530113 return PTR_ERR(hsusb_3p3);
114 }
115
116 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
117 USB_PHY_3P3_VOL_MAX);
118 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700119 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700120 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530121 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530122 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700123 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530124 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200125 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 }
129 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
130 USB_PHY_1P8_VOL_MAX);
131 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700132 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530134 goto put_1p8;
135 }
136
137 return 0;
138 }
139
Anji jonnala11aa5c42011-05-04 10:19:48 +0530140put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142put_3p3_lpm:
143 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144 return rc;
145}
146
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530147static int msm_hsusb_config_vddcx(int high)
148{
Mayank Rana248698c2012-04-19 00:03:16 +0530149 struct msm_otg *motg = the_msm_otg;
150 enum usb_vdd_type vdd_type = motg->vdd_type;
151 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530152 int min_vol;
153 int ret;
154
Mayank Rana248698c2012-04-19 00:03:16 +0530155 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530156 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
157 if (ret) {
158 pr_err("%s: unable to set the voltage for regulator "
159 "HSUSB_VDDCX\n", __func__);
160 return ret;
161 }
162
163 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
164
165 return ret;
166}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530167
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530169{
170 int ret = 0;
171
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530172 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530173 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
174 return -ENODEV;
175 }
176
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530177 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530178 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
179 return -ENODEV;
180 }
181
182 if (on) {
183 ret = regulator_set_optimum_mode(hsusb_1p8,
184 USB_PHY_1P8_HPM_LOAD);
185 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530187 "HSUSB_1p8\n", __func__);
188 return ret;
189 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
191 ret = regulator_enable(hsusb_1p8);
192 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700193 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194 __func__);
195 regulator_set_optimum_mode(hsusb_1p8, 0);
196 return ret;
197 }
198
Anji jonnala11aa5c42011-05-04 10:19:48 +0530199 ret = regulator_set_optimum_mode(hsusb_3p3,
200 USB_PHY_3P3_HPM_LOAD);
201 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530203 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 regulator_set_optimum_mode(hsusb_1p8, 0);
205 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530206 return ret;
207 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208
209 ret = regulator_enable(hsusb_3p3);
210 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700211 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 __func__);
213 regulator_set_optimum_mode(hsusb_3p3, 0);
214 regulator_set_optimum_mode(hsusb_1p8, 0);
215 regulator_disable(hsusb_1p8);
216 return ret;
217 }
218
Anji jonnala11aa5c42011-05-04 10:19:48 +0530219 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 ret = regulator_disable(hsusb_1p8);
221 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700222 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 __func__);
224 return ret;
225 }
226
227 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530228 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530230 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231
232 ret = regulator_disable(hsusb_3p3);
233 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700234 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235 __func__);
236 return ret;
237 }
238 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530239 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530241 "HSUSB_3p3\n", __func__);
242 }
243
244 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
245 return ret < 0 ? ret : 0;
246}
247
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530248static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
249{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530250 struct msm_otg_platform_data *pdata = motg->pdata;
251
252 if (!pdata->mhl_enable)
253 return;
254
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530255 if (!mhl_usb_hs_switch) {
256 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530257 return;
258 }
259
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530260 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530261 if (regulator_enable(mhl_usb_hs_switch))
262 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530263 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530264 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530265 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530266}
267
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200268static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530269{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200270 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530271 int cnt = 0;
272
273 /* initiate read operation */
274 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
275 USB_ULPI_VIEWPORT);
276
277 /* wait for completion */
278 while (cnt < ULPI_IO_TIMEOUT_USEC) {
279 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
280 break;
281 udelay(1);
282 cnt++;
283 }
284
285 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200286 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530287 readl(USB_ULPI_VIEWPORT));
288 return -ETIMEDOUT;
289 }
290 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
291}
292
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200293static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530294{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200295 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530296 int cnt = 0;
297
298 /* initiate write operation */
299 writel(ULPI_RUN | ULPI_WRITE |
300 ULPI_ADDR(reg) | ULPI_DATA(val),
301 USB_ULPI_VIEWPORT);
302
303 /* wait for completion */
304 while (cnt < ULPI_IO_TIMEOUT_USEC) {
305 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
306 break;
307 udelay(1);
308 cnt++;
309 }
310
311 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200312 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530313 return -ETIMEDOUT;
314 }
315 return 0;
316}
317
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200318static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530319 .read = ulpi_read,
320 .write = ulpi_write,
321};
322
323static void ulpi_init(struct msm_otg *motg)
324{
325 struct msm_otg_platform_data *pdata = motg->pdata;
326 int *seq = pdata->phy_init_seq;
327
328 if (!seq)
329 return;
330
331 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200332 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530333 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200334 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530335 seq += 2;
336 }
337}
338
339static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
340{
341 int ret;
342
343 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530344 if (!IS_ERR(motg->clk)) {
345 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
346 } else {
347 /* Using asynchronous block reset to the hardware */
348 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
349 clk_disable_unprepare(motg->pclk);
350 clk_disable_unprepare(motg->core_clk);
351 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
352 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530353 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200354 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530355 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530356 if (!IS_ERR(motg->clk)) {
357 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
358 } else {
359 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
360 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
361 ndelay(200);
362 clk_prepare_enable(motg->core_clk);
363 clk_prepare_enable(motg->pclk);
364 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530365 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200366 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530367 }
368 return ret;
369}
370
371static int msm_otg_phy_clk_reset(struct msm_otg *motg)
372{
373 int ret;
374
Amit Blay02eff132011-09-21 16:46:24 +0300375 if (IS_ERR(motg->phy_reset_clk))
376 return 0;
377
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530378 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
379 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200380 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530381 return ret;
382 }
383 usleep_range(10000, 12000);
384 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
385 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200386 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530387 return ret;
388}
389
390static int msm_otg_phy_reset(struct msm_otg *motg)
391{
392 u32 val;
393 int ret;
394 int retries;
395
396 ret = msm_otg_link_clk_reset(motg, 1);
397 if (ret)
398 return ret;
399 ret = msm_otg_phy_clk_reset(motg);
400 if (ret)
401 return ret;
402 ret = msm_otg_link_clk_reset(motg, 0);
403 if (ret)
404 return ret;
405
406 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
407 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
408
409 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200410 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530411 ULPI_CLR(ULPI_FUNC_CTRL));
412 if (!ret)
413 break;
414 ret = msm_otg_phy_clk_reset(motg);
415 if (ret)
416 return ret;
417 }
418 if (!retries)
419 return -ETIMEDOUT;
420
421 /* This reset calibrates the phy, if the above write succeeded */
422 ret = msm_otg_phy_clk_reset(motg);
423 if (ret)
424 return ret;
425
426 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200427 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530428 if (ret != -ETIMEDOUT)
429 break;
430 ret = msm_otg_phy_clk_reset(motg);
431 if (ret)
432 return ret;
433 }
434 if (!retries)
435 return -ETIMEDOUT;
436
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200437 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530438 return 0;
439}
440
441#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530442static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530443{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530444 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530445
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530446 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530447 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530448 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530449 break;
450 udelay(1);
451 cnt++;
452 }
453 if (cnt >= LINK_RESET_TIMEOUT_USEC)
454 return -ETIMEDOUT;
455
456 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530457 writel_relaxed(0x80000000, USB_PORTSC);
458 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530459 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530460
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530461 return 0;
462}
463
Steve Mucklef132c6c2012-06-06 18:30:57 -0700464static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530465{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700466 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530467 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530468 int ret;
469 u32 val = 0;
470 u32 ulpi_val = 0;
471
Ofir Cohen4da266f2012-01-03 10:19:29 +0200472 /*
473 * USB PHY and Link reset also reset the USB BAM.
474 * Thus perform reset operation only once to avoid
475 * USB BAM reset on other cases e.g. USB cable disconnections.
476 */
477 if (pdata->disable_reset_on_disconnect) {
478 if (motg->reset_counter)
479 return 0;
480 else
481 motg->reset_counter++;
482 }
483
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530484 if (!IS_ERR(motg->clk))
485 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530486 ret = msm_otg_phy_reset(motg);
487 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700488 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530489 return ret;
490 }
491
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530492 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530493 ret = msm_otg_link_reset(motg);
494 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700495 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530496 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530497 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530498 msleep(100);
499
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530500 ulpi_init(motg);
501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 /* Ensure that RESET operation is completed before turning off clock */
503 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530504
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530505 if (!IS_ERR(motg->clk))
506 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530507
508 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530509 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530510 if (pdata->mode == USB_OTG) {
511 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
512 val |= OTGSC_IDIE | OTGSC_BSVIE;
513 } else if (pdata->mode == USB_PERIPHERAL) {
514 ulpi_val = ULPI_INT_SESS_VALID;
515 val |= OTGSC_BSVIE;
516 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530517 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200518 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
519 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530520 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700521 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530522 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530523 /* Enable PMIC pull-up */
524 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530525 }
526
527 return 0;
528}
529
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530530static const char *timer_string(int bit)
531{
532 switch (bit) {
533 case A_WAIT_VRISE: return "a_wait_vrise";
534 case A_WAIT_VFALL: return "a_wait_vfall";
535 case B_SRP_FAIL: return "b_srp_fail";
536 case A_WAIT_BCON: return "a_wait_bcon";
537 case A_AIDL_BDIS: return "a_aidl_bdis";
538 case A_BIDL_ADIS: return "a_bidl_adis";
539 case B_ASE0_BRST: return "b_ase0_brst";
540 case A_TST_MAINT: return "a_tst_maint";
541 case B_TST_SRP: return "b_tst_srp";
542 case B_TST_CONFIG: return "b_tst_config";
543 default: return "UNDEFINED";
544 }
545}
546
547static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
548{
549 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
550
551 switch (motg->active_tmout) {
552 case A_WAIT_VRISE:
553 /* TODO: use vbus_vld interrupt */
554 set_bit(A_VBUS_VLD, &motg->inputs);
555 break;
556 case A_TST_MAINT:
557 /* OTG PET: End session after TA_TST_MAINT */
558 set_bit(A_BUS_DROP, &motg->inputs);
559 break;
560 case B_TST_SRP:
561 /*
562 * OTG PET: Initiate SRP after TB_TST_SRP of
563 * previous session end.
564 */
565 set_bit(B_BUS_REQ, &motg->inputs);
566 break;
567 case B_TST_CONFIG:
568 clear_bit(A_CONN, &motg->inputs);
569 break;
570 default:
571 set_bit(motg->active_tmout, &motg->tmouts);
572 }
573
574 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
575 queue_work(system_nrt_wq, &motg->sm_work);
576 return HRTIMER_NORESTART;
577}
578
579static void msm_otg_del_timer(struct msm_otg *motg)
580{
581 int bit = motg->active_tmout;
582
583 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
584 div_s64(ktime_to_us(hrtimer_get_remaining(
585 &motg->timer)), 1000));
586 hrtimer_cancel(&motg->timer);
587 clear_bit(bit, &motg->tmouts);
588}
589
590static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
591{
592 clear_bit(bit, &motg->tmouts);
593 motg->active_tmout = bit;
594 pr_debug("starting %s timer\n", timer_string(bit));
595 hrtimer_start(&motg->timer,
596 ktime_set(time / 1000, (time % 1000) * 1000000),
597 HRTIMER_MODE_REL);
598}
599
600static void msm_otg_init_timer(struct msm_otg *motg)
601{
602 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
603 motg->timer.function = msm_otg_timer_func;
604}
605
Steve Mucklef132c6c2012-06-06 18:30:57 -0700606static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530607{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700608 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530609
Steve Mucklef132c6c2012-06-06 18:30:57 -0700610 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530611 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700612 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530613 return -EINVAL;
614 }
615
616 pr_debug("A-Host: HNP initiated\n");
617 clear_bit(A_BUS_REQ, &motg->inputs);
618 queue_work(system_nrt_wq, &motg->sm_work);
619 return 0;
620}
621
Steve Mucklef132c6c2012-06-06 18:30:57 -0700622static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530623{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700624 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530625 u32 val;
626 int ret = 0;
627
Steve Mucklef132c6c2012-06-06 18:30:57 -0700628 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530629 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700630 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530631 ret = -EINVAL;
632 goto out;
633 }
634
635 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
636 pr_debug("initial conditions of SRP are not met. Try again"
637 "after some time\n");
638 ret = -EAGAIN;
639 goto out;
640 }
641
642 pr_debug("B-Device SRP started\n");
643
644 /*
645 * PHY won't pull D+ high unless it detects Vbus valid.
646 * Since by definition, SRP is only done when Vbus is not valid,
647 * software work-around needs to be used to spoof the PHY into
648 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
649 * VBUSVLDEXT register bits.
650 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700651 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530652 /*
653 * Harware auto assist data pulsing: Data pulse is given
654 * for 7msec; wait for vbus
655 */
656 val = readl_relaxed(USB_OTGSC);
657 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
658
659 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
660out:
661 return ret;
662}
663
Steve Mucklef132c6c2012-06-06 18:30:57 -0700664static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530665{
666 struct usb_hcd *hcd = bus_to_hcd(otg->host);
667 struct usb_device *rhub = otg->host->root_hub;
668
669 if (enable) {
670 pm_runtime_disable(&rhub->dev);
671 rhub->state = USB_STATE_NOTATTACHED;
672 hcd->driver->bus_suspend(hcd);
673 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
674 } else {
675 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700676 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530677 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
678 }
679}
680
Steve Mucklef132c6c2012-06-06 18:30:57 -0700681static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530682{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700683 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530684
Amit Blay6fa647a2012-05-24 14:12:08 +0300685 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530686 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530687
Jack Pham69e621d2012-06-25 18:48:07 -0700688 if (atomic_read(&motg->in_lpm) == suspend)
689 return 0;
690
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530691 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700692 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530693 case OTG_STATE_A_WAIT_BCON:
694 if (TA_WAIT_BCON > 0)
695 break;
696 /* fall through */
697 case OTG_STATE_A_HOST:
698 pr_debug("host bus suspend\n");
699 clear_bit(A_BUS_REQ, &motg->inputs);
700 queue_work(system_nrt_wq, &motg->sm_work);
701 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300702 case OTG_STATE_B_PERIPHERAL:
703 pr_debug("peripheral bus suspend\n");
704 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
705 break;
706 set_bit(A_BUS_SUSPEND, &motg->inputs);
707 queue_work(system_nrt_wq, &motg->sm_work);
708 break;
709
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530710 default:
711 break;
712 }
713 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700714 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530715 case OTG_STATE_A_SUSPEND:
716 /* Remote wakeup or resume */
717 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700718 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700719
720 /* ensure hardware is not in low power mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700721 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530722 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300723 case OTG_STATE_B_PERIPHERAL:
724 pr_debug("peripheral bus resume\n");
725 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
726 break;
727 clear_bit(A_BUS_SUSPEND, &motg->inputs);
728 queue_work(system_nrt_wq, &motg->sm_work);
729 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530730 default:
731 break;
732 }
733 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530734 return 0;
735}
736
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530737#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530738#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
739
740#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530741static int msm_otg_suspend(struct msm_otg *motg)
742{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200743 struct usb_phy *phy = &motg->phy;
744 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530745 struct msm_otg_platform_data *pdata = motg->pdata;
746 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300747 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530748 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800749 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530750 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530751
752 if (atomic_read(&motg->in_lpm))
753 return 0;
754
755 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530756 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
757 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700758 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300759 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
760 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530761 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530762 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530763 * Chipidea 45-nm PHY suspend sequence:
764 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530765 * Interrupt Latch Register auto-clear feature is not present
766 * in all PHY versions. Latch register is clear on read type.
767 * Clear latch register to avoid spurious wakeup from
768 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530769 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530770 * PHY comparators are disabled when PHY enters into low power
771 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
772 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
773 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530774 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530775 * PLL is not turned off when PHY enters into low power mode (LPM).
776 * Disable PLL for maximum power savings.
777 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530778
779 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200780 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530781 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200782 ulpi_write(phy, 0x01, 0x30);
783 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530784 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530785
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700786
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530787 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530788 * PHY may take some time or even fail to enter into low power
789 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
790 * in failure case.
791 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530792 portsc = readl_relaxed(USB_PORTSC);
793 if (!(portsc & PORTSC_PHCD)) {
794 writel_relaxed(portsc | PORTSC_PHCD,
795 USB_PORTSC);
796 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
797 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
798 break;
799 udelay(1);
800 cnt++;
801 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530802 }
803
804 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200805 dev_err(phy->dev, "Unable to suspend PHY\n");
806 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530807 enable_irq(motg->irq);
808 return -ETIMEDOUT;
809 }
810
811 /*
812 * PHY has capability to generate interrupt asynchronously in low
813 * power mode (LPM). This interrupt is level triggered. So USB IRQ
814 * line must be disabled till async interrupt enable bit is cleared
815 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
816 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530817 *
818 * PHY retention mode is disallowed while entering to LPM with wall
819 * charger connected. But PHY is put into suspend mode. Hence
820 * enable asynchronous interrupt to detect charger disconnection when
821 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530822 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530823 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300824 if (host_bus_suspend || device_bus_suspend ||
825 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530826 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
827 else
828 cmd_val |= ULPI_STP_CTRL;
829 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530830
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530831 /*
832 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
833 * PHY retention and collapse can not happen with VDP_SRC enabled.
834 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300835 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
836 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200837 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
838 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
839 /* Enable PHY HV interrupts to wake MPM/Link */
840 phy_ctrl_val |=
841 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530842
Amit Blay58b31472011-11-18 09:39:39 +0200843 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700844 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530845 }
846
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847 /* Ensure that above operation is completed before turning off clocks */
848 mb();
Amit Blay9b6e58b2012-06-18 13:12:49 +0300849 if (!motg->pdata->core_clk_always_on_workaround) {
850 clk_disable_unprepare(motg->pclk);
851 clk_disable_unprepare(motg->core_clk);
852 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530853
Anji jonnala7da3f262011-12-02 17:22:14 -0800854 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530855 if (!host_bus_suspend) {
856 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
857 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700858 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530859 "TCXO D0 buffer%d\n", __func__, ret);
860 else
861 motg->lpm_flags |= XO_SHUTDOWN;
862 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800863
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530864 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
865 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866 msm_hsusb_ldo_enable(motg, 0);
867 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530868 }
869
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530870 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530872 msm_hsusb_mhl_switch_enable(motg, 0);
873 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874
Steve Mucklef132c6c2012-06-06 18:30:57 -0700875 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530876 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877 if (motg->pdata->pmic_id_irq)
878 enable_irq_wake(motg->pdata->pmic_id_irq);
879 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530880 if (bus)
881 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
882
883 atomic_set(&motg->in_lpm, 1);
884 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700885 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530886
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200887 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530888
889 return 0;
890}
891
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530892static int msm_otg_resume(struct msm_otg *motg)
893{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200894 struct usb_phy *phy = &motg->phy;
895 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530896 int cnt = 0;
897 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200898 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800899 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530900
901 if (!atomic_read(&motg->in_lpm))
902 return 0;
903
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530905
Anji jonnala7da3f262011-12-02 17:22:14 -0800906 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530907 if (motg->lpm_flags & XO_SHUTDOWN) {
908 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
909 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700910 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530911 "TCXO D0 buffer%d\n", __func__, ret);
912 motg->lpm_flags &= ~XO_SHUTDOWN;
913 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530914
Amit Blay9b6e58b2012-06-18 13:12:49 +0300915 if (!motg->pdata->core_clk_always_on_workaround) {
916 clk_prepare_enable(motg->core_clk);
917 clk_prepare_enable(motg->pclk);
918 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530919
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
921 msm_hsusb_ldo_enable(motg, 1);
922 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
923 }
924
925 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530926 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530927 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200928 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
929 phy_ctrl_val |= PHY_RETEN;
930 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
931 /* Disable PHY HV interrupts */
932 phy_ctrl_val &=
933 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
934 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530936 }
937
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530938 temp = readl(USB_USBCMD);
939 temp &= ~ASYNC_INTR_CTRL;
940 temp &= ~ULPI_STP_CTRL;
941 writel(temp, USB_USBCMD);
942
943 /*
944 * PHY comes out of low power mode (LPM) in case of wakeup
945 * from asynchronous interrupt.
946 */
947 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
948 goto skip_phy_resume;
949
950 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
951 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
952 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
953 break;
954 udelay(1);
955 cnt++;
956 }
957
958 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
959 /*
960 * This is a fatal error. Reset the link and
961 * PHY. USB state can not be restored. Re-insertion
962 * of USB cable is the only way to get USB working.
963 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200964 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530965 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200966 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530967 }
968
969skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700970 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530971 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972 if (motg->pdata->pmic_id_irq)
973 disable_irq_wake(motg->pdata->pmic_id_irq);
974 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530975 if (bus)
976 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
977
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530978 atomic_set(&motg->in_lpm, 0);
979
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530980 if (motg->async_int) {
981 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530982 enable_irq(motg->irq);
983 }
984
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200985 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530986
987 return 0;
988}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530989#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530990
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -0700991static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
992{
993 if (!psy)
994 goto psy_not_supported;
995
996 if (host_mode)
997 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
998 else
999 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1000
1001psy_not_supported:
1002 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1003 return -ENXIO;
1004}
1005
David Keitel081a3e22012-04-18 12:37:07 -07001006static int msm_otg_notify_chg_type(struct msm_otg *motg)
1007{
1008 static int charger_type;
1009 /*
1010 * TODO
1011 * Unify OTG driver charger types and power supply charger types
1012 */
1013 if (charger_type == motg->chg_type)
1014 return 0;
1015
1016 if (motg->chg_type == USB_SDP_CHARGER)
1017 charger_type = POWER_SUPPLY_TYPE_USB;
1018 else if (motg->chg_type == USB_CDP_CHARGER)
1019 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301020 else if (motg->chg_type == USB_DCP_CHARGER ||
1021 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001022 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1023 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1024 motg->chg_type == USB_ACA_A_CHARGER ||
1025 motg->chg_type == USB_ACA_B_CHARGER ||
1026 motg->chg_type == USB_ACA_C_CHARGER))
1027 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1028 else
1029 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1030
1031 return pm8921_set_usb_power_supply_type(charger_type);
1032}
1033
Amit Blay0f7edf72012-01-15 10:11:27 +02001034static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1035{
Amit Blay0f7edf72012-01-15 10:11:27 +02001036
Amit Blay0f7edf72012-01-15 10:11:27 +02001037 if (!psy)
1038 goto psy_not_supported;
1039
1040 if (motg->cur_power == 0 && mA > 0) {
1041 /* Enable charging */
1042 if (power_supply_set_online(psy, true))
1043 goto psy_not_supported;
1044 } else if (motg->cur_power > 0 && mA == 0) {
1045 /* Disable charging */
1046 if (power_supply_set_online(psy, false))
1047 goto psy_not_supported;
1048 return 0;
1049 }
1050 /* Set max current limit */
1051 if (power_supply_set_current_limit(psy, 1000*mA))
1052 goto psy_not_supported;
1053
1054 return 0;
1055
1056psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001057 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001058 return -ENXIO;
1059}
1060
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301061static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1062{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001063 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301064
1065 if (g && g->is_a_peripheral)
1066 return;
1067
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301068 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1069 motg->chg_type == USB_ACA_A_CHARGER ||
1070 motg->chg_type == USB_ACA_B_CHARGER ||
1071 motg->chg_type == USB_ACA_C_CHARGER) &&
1072 mA > IDEV_ACA_CHG_LIMIT)
1073 mA = IDEV_ACA_CHG_LIMIT;
1074
David Keitel081a3e22012-04-18 12:37:07 -07001075 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001076 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001077 "Failed notifying %d charger type to PMIC\n",
1078 motg->chg_type);
1079
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301080 if (motg->cur_power == mA)
1081 return;
1082
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001083 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001084
1085 /*
1086 * Use Power Supply API if supported, otherwise fallback
1087 * to legacy pm8921 API.
1088 */
1089 if (msm_otg_notify_power_supply(motg, mA))
1090 pm8921_charger_vbus_draw(mA);
1091
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301092 motg->cur_power = mA;
1093}
1094
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001095static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301096{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001097 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301098
1099 /*
1100 * Gadget driver uses set_power method to notify about the
1101 * available current based on suspend/configured states.
1102 *
1103 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1104 * states when CDP/ACA is connected.
1105 */
1106 if (motg->chg_type == USB_SDP_CHARGER)
1107 msm_otg_notify_charger(motg, mA);
1108
1109 return 0;
1110}
1111
Steve Mucklef132c6c2012-06-06 18:30:57 -07001112static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301113{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001114 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301115 struct msm_otg_platform_data *pdata = motg->pdata;
1116 struct usb_hcd *hcd;
1117
1118 if (!otg->host)
1119 return;
1120
1121 hcd = bus_to_hcd(otg->host);
1122
1123 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001124 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301125
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301126 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001127 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301128 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1129
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301130 /*
1131 * Some boards have a switch cotrolled by gpio
1132 * to enable/disable internal HUB. Enable internal
1133 * HUB before kicking the host.
1134 */
1135 if (pdata->setup_gpio)
1136 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301137 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301138 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001139 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301140
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301141 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301142 /* HCD core reset all bits of PORTSC. select ULPI phy */
1143 writel_relaxed(0x80000000, USB_PORTSC);
1144
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301145 if (pdata->setup_gpio)
1146 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301147
1148 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001149 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301150 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301151 }
1152}
1153
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154static int msm_otg_usbdev_notify(struct notifier_block *self,
1155 unsigned long action, void *priv)
1156{
1157 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001158 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301159 struct usb_device *udev = priv;
1160
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301161 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1162 goto out;
1163
Steve Mucklef132c6c2012-06-06 18:30:57 -07001164 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301165 goto out;
1166 /*
1167 * Interested in devices connected directly to the root hub.
1168 * ACA dock can supply IDEV_CHG irrespective devices connected
1169 * on the accessory port.
1170 */
1171 if (!udev->parent || udev->parent->parent ||
1172 motg->chg_type == USB_ACA_DOCK_CHARGER)
1173 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001174
1175 switch (action) {
1176 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301177 if (aca_enabled())
1178 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001179 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301180 pr_debug("B_CONN set\n");
1181 set_bit(B_CONN, &motg->inputs);
1182 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001183 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301184 /*
1185 * OTG PET: A-device must end session within
1186 * 10 sec after PET enumeration.
1187 */
1188 if (udev->quirks & USB_QUIRK_OTG_PET)
1189 msm_otg_start_timer(motg, TA_TST_MAINT,
1190 A_TST_MAINT);
1191 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301192 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194 if (udev->actconfig)
1195 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1196 else
1197 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001198 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301199 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301200 break;
1201 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001202 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1203 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301204 pr_debug("B_CONN clear\n");
1205 clear_bit(B_CONN, &motg->inputs);
1206 /*
1207 * OTG PET: A-device must end session after
1208 * PET disconnection if it is enumerated
1209 * with bcdDevice[0] = 1. USB core sets
1210 * bus->otg_vbus_off for us. clear it here.
1211 */
1212 if (udev->bus->otg_vbus_off) {
1213 udev->bus->otg_vbus_off = 0;
1214 set_bit(A_BUS_DROP, &motg->inputs);
1215 }
1216 queue_work(system_nrt_wq, &motg->sm_work);
1217 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218 default:
1219 break;
1220 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301221 if (test_bit(ID_A, &motg->inputs))
1222 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1223 motg->mA_port);
1224out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225 return NOTIFY_OK;
1226}
1227
Mayank Ranae3926882011-12-26 09:47:54 +05301228static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1229{
1230 int ret;
1231 static bool vbus_is_on;
1232
1233 if (vbus_is_on == on)
1234 return;
1235
1236 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301237 ret = motg->pdata->vbus_power(on);
1238 if (!ret)
1239 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301240 return;
1241 }
1242
1243 if (!vbus_otg) {
1244 pr_err("vbus_otg is NULL.");
1245 return;
1246 }
1247
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001248 /*
1249 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001250 * from usb before turning on the boost.
1251 * if exiting host mode disable the boost before enabling to draw
1252 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001253 */
Mayank Ranae3926882011-12-26 09:47:54 +05301254 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001255 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301256 ret = regulator_enable(vbus_otg);
1257 if (ret) {
1258 pr_err("unable to enable vbus_otg\n");
1259 return;
1260 }
1261 vbus_is_on = true;
1262 } else {
1263 ret = regulator_disable(vbus_otg);
1264 if (ret) {
1265 pr_err("unable to disable vbus_otg\n");
1266 return;
1267 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001268 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301269 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301270 }
1271}
1272
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001273static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301274{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001275 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301276 struct usb_hcd *hcd;
1277
1278 /*
1279 * Fail host registration if this board can support
1280 * only peripheral configuration.
1281 */
1282 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001283 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301284 return -ENODEV;
1285 }
1286
Mayank Ranae3926882011-12-26 09:47:54 +05301287 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001288 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301289 if (IS_ERR(vbus_otg)) {
1290 pr_err("Unable to get vbus_otg\n");
1291 return -ENODEV;
1292 }
1293 }
1294
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301295 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001296 if (otg->phy->state == OTG_STATE_A_HOST) {
1297 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001298 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301299 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301300 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301301 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001302 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301303 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301304 } else {
1305 otg->host = NULL;
1306 }
1307
1308 return 0;
1309 }
1310
1311 hcd = bus_to_hcd(host);
1312 hcd->power_budget = motg->pdata->power_budget;
1313
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301314#ifdef CONFIG_USB_OTG
1315 host->otg_port = 1;
1316#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1318 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301319 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001320 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301321
1322 /*
1323 * Kick the state machine work, if peripheral is not supported
1324 * or peripheral is already registered with us.
1325 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301326 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001327 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301328 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301329 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301330
1331 return 0;
1332}
1333
Steve Mucklef132c6c2012-06-06 18:30:57 -07001334static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301335{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301336 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001337 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301338 struct msm_otg_platform_data *pdata = motg->pdata;
1339
1340 if (!otg->gadget)
1341 return;
1342
1343 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001344 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301345 /*
1346 * Some boards have a switch cotrolled by gpio
1347 * to enable/disable internal HUB. Disable internal
1348 * HUB before kicking the gadget.
1349 */
1350 if (pdata->setup_gpio)
1351 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001352
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301353 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301354 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301355 ret = msm_bus_scale_client_update_request(
1356 motg->bus_perf_client, 1);
1357 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001358 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301359 "bus bandwidth %d\n", __func__, ret);
1360 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301361 usb_gadget_vbus_connect(otg->gadget);
1362 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001363 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301364 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301365 /* Configure BUS performance parameters to default */
1366 if (motg->bus_perf_client) {
1367 ret = msm_bus_scale_client_update_request(
1368 motg->bus_perf_client, 0);
1369 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001370 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301371 "for bus bw %d\n", __func__, ret);
1372 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301373 if (pdata->setup_gpio)
1374 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1375 }
1376
1377}
1378
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001379static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301380 struct usb_gadget *gadget)
1381{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001382 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301383
1384 /*
1385 * Fail peripheral registration if this board can support
1386 * only host configuration.
1387 */
1388 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001389 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301390 return -ENODEV;
1391 }
1392
1393 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001394 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1395 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301396 msm_otg_start_peripheral(otg, 0);
1397 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001398 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301399 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301400 } else {
1401 otg->gadget = NULL;
1402 }
1403
1404 return 0;
1405 }
1406 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001407 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301408
1409 /*
1410 * Kick the state machine work, if host is not supported
1411 * or host is already registered with us.
1412 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301413 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001414 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301415 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301416 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301417
1418 return 0;
1419}
1420
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301421static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1422 void (*callback)(int on))
1423{
1424 struct usb_phy *phy = &motg->phy;
1425 int ret;
1426
1427 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1428 !motg->pdata->pmic_id_irq) {
1429 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1430 return -ENODEV;
1431 }
1432
1433 if (!motg->pdata->mhl_dev_name) {
1434 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1435 return -ENODEV;
1436 }
1437
1438 if (callback)
1439 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1440 callback);
1441 else
1442 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1443
1444 if (ret)
1445 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1446 motg->pdata->mhl_dev_name, ret);
1447 else
1448 motg->mhl_enabled = true;
1449
1450 return ret;
1451}
1452
1453static void msm_otg_mhl_notify_online(int on)
1454{
1455 struct msm_otg *motg = the_msm_otg;
1456 struct usb_phy *phy = &motg->phy;
1457 bool queue = false;
1458
1459 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1460
1461 if (on) {
1462 set_bit(MHL, &motg->inputs);
1463 } else {
1464 clear_bit(MHL, &motg->inputs);
1465 queue = true;
1466 }
1467
1468 if (queue && phy->state != OTG_STATE_UNDEFINED)
1469 schedule_work(&motg->sm_work);
1470}
1471
1472static bool msm_otg_is_mhl(struct msm_otg *motg)
1473{
1474 struct usb_phy *phy = &motg->phy;
1475 int is_mhl, ret;
1476
1477 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1478 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1479 /*
1480 * MHL driver calls our callback saying that MHL connected
1481 * if RID_GND is detected. But at later part of discovery
1482 * it may figure out MHL is not connected and returns
1483 * false. Hence clear MHL input here.
1484 */
1485 clear_bit(MHL, &motg->inputs);
1486 dev_dbg(phy->dev, "MHL device not found\n");
1487 return false;
1488 }
1489
1490 set_bit(MHL, &motg->inputs);
1491 dev_dbg(phy->dev, "MHL device found\n");
1492 return true;
1493}
1494
1495static bool msm_chg_mhl_detect(struct msm_otg *motg)
1496{
1497 bool ret, id;
1498 unsigned long flags;
1499
1500 if (!motg->mhl_enabled)
1501 return false;
1502
1503 local_irq_save(flags);
1504 id = irq_read_line(motg->pdata->pmic_id_irq);
1505 local_irq_restore(flags);
1506
1507 if (id)
1508 return false;
1509
1510 mhl_det_in_progress = true;
1511 ret = msm_otg_is_mhl(motg);
1512 mhl_det_in_progress = false;
1513
1514 return ret;
1515}
1516
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001517static bool msm_chg_aca_detect(struct msm_otg *motg)
1518{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001519 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001520 u32 int_sts;
1521 bool ret = false;
1522
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301523 if (!aca_enabled())
1524 goto out;
1525
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001526 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1527 goto out;
1528
Steve Mucklef132c6c2012-06-06 18:30:57 -07001529 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001530 switch (int_sts & 0x1C) {
1531 case 0x08:
1532 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001533 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001534 motg->chg_type = USB_ACA_A_CHARGER;
1535 motg->chg_state = USB_CHG_STATE_DETECTED;
1536 clear_bit(ID_B, &motg->inputs);
1537 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301538 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001539 ret = true;
1540 }
1541 break;
1542 case 0x0C:
1543 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001544 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001545 motg->chg_type = USB_ACA_B_CHARGER;
1546 motg->chg_state = USB_CHG_STATE_DETECTED;
1547 clear_bit(ID_A, &motg->inputs);
1548 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301549 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001550 ret = true;
1551 }
1552 break;
1553 case 0x10:
1554 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001555 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001556 motg->chg_type = USB_ACA_C_CHARGER;
1557 motg->chg_state = USB_CHG_STATE_DETECTED;
1558 clear_bit(ID_A, &motg->inputs);
1559 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301560 set_bit(ID, &motg->inputs);
1561 ret = true;
1562 }
1563 break;
1564 case 0x04:
1565 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001566 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301567 motg->chg_type = USB_INVALID_CHARGER;
1568 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1569 clear_bit(ID_A, &motg->inputs);
1570 clear_bit(ID_B, &motg->inputs);
1571 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001572 ret = true;
1573 }
1574 break;
1575 default:
1576 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1577 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301578 test_and_clear_bit(ID_C, &motg->inputs) |
1579 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001580 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001581 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001582 motg->chg_type = USB_INVALID_CHARGER;
1583 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1584 }
1585 }
1586out:
1587 return ret;
1588}
1589
1590static void msm_chg_enable_aca_det(struct msm_otg *motg)
1591{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001592 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001593
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301594 if (!aca_enabled())
1595 return;
1596
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001597 switch (motg->pdata->phy_type) {
1598 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301599 /* Disable ID_GND in link and PHY */
1600 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1601 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001602 ulpi_write(phy, 0x01, 0x0C);
1603 ulpi_write(phy, 0x10, 0x0F);
1604 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301605 /* Disable PMIC ID pull-up */
1606 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301607 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001608 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301609 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001610 break;
1611 default:
1612 break;
1613 }
1614}
1615
1616static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1617{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001618 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001619
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301620 if (!aca_enabled())
1621 return;
1622
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623 switch (motg->pdata->phy_type) {
1624 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301625 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001626 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301627 break;
1628 default:
1629 break;
1630 }
1631}
1632
1633static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1634{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001635 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301636
1637 if (!aca_enabled())
1638 return;
1639
1640 switch (motg->pdata->phy_type) {
1641 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001642 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001643 break;
1644 default:
1645 break;
1646 }
1647}
1648
1649static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1650{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001651 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652 bool ret = false;
1653
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301654 if (!aca_enabled())
1655 return ret;
1656
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001657 switch (motg->pdata->phy_type) {
1658 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001659 if (ulpi_read(phy, 0x91) & 1) {
1660 dev_dbg(phy->dev, "RID change\n");
1661 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001662 ret = msm_chg_aca_detect(motg);
1663 }
1664 default:
1665 break;
1666 }
1667 return ret;
1668}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301669
1670static void msm_otg_id_timer_func(unsigned long data)
1671{
1672 struct msm_otg *motg = (struct msm_otg *) data;
1673
1674 if (!aca_enabled())
1675 return;
1676
1677 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001678 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301679 return;
1680 }
1681
Steve Mucklef132c6c2012-06-06 18:30:57 -07001682 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301683 goto out;
1684
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301685 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001686 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301687 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301688 }
1689
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301690out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301691 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1692 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1693}
1694
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301695static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1696{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001697 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301698 u32 chg_det;
1699 bool ret = false;
1700
1701 switch (motg->pdata->phy_type) {
1702 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001703 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301704 ret = chg_det & (1 << 4);
1705 break;
1706 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001707 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301708 ret = chg_det & 1;
1709 break;
1710 default:
1711 break;
1712 }
1713 return ret;
1714}
1715
1716static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1717{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001718 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301719 u32 chg_det;
1720
1721 switch (motg->pdata->phy_type) {
1722 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001723 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301724 /* Turn off charger block */
1725 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001726 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301727 udelay(20);
1728 /* control chg block via ULPI */
1729 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001730 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301731 /* put it in host mode for enabling D- source */
1732 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001733 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301734 /* Turn on chg detect block */
1735 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001736 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301737 udelay(20);
1738 /* enable chg detection */
1739 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001740 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301741 break;
1742 case SNPS_28NM_INTEGRATED_PHY:
1743 /*
1744 * Configure DM as current source, DP as current sink
1745 * and enable battery charging comparators.
1746 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001747 ulpi_write(phy, 0x8, 0x85);
1748 ulpi_write(phy, 0x2, 0x85);
1749 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301750 break;
1751 default:
1752 break;
1753 }
1754}
1755
1756static bool msm_chg_check_primary_det(struct msm_otg *motg)
1757{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001758 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301759 u32 chg_det;
1760 bool ret = false;
1761
1762 switch (motg->pdata->phy_type) {
1763 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001764 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301765 ret = chg_det & (1 << 4);
1766 break;
1767 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001768 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301769 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301770 /* Turn off VDP_SRC */
1771 ulpi_write(phy, 0x3, 0x86);
1772 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301773 break;
1774 default:
1775 break;
1776 }
1777 return ret;
1778}
1779
1780static void msm_chg_enable_primary_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
1785 switch (motg->pdata->phy_type) {
1786 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001787 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301788 /* enable chg detection */
1789 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001790 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301791 break;
1792 case SNPS_28NM_INTEGRATED_PHY:
1793 /*
1794 * Configure DP as current source, DM as current sink
1795 * and enable battery charging comparators.
1796 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001797 ulpi_write(phy, 0x2, 0x85);
1798 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301799 break;
1800 default:
1801 break;
1802 }
1803}
1804
1805static bool msm_chg_check_dcd(struct msm_otg *motg)
1806{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001807 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301808 u32 line_state;
1809 bool ret = false;
1810
1811 switch (motg->pdata->phy_type) {
1812 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001813 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301814 ret = !(line_state & 1);
1815 break;
1816 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001817 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301818 ret = line_state & 2;
1819 break;
1820 default:
1821 break;
1822 }
1823 return ret;
1824}
1825
1826static void msm_chg_disable_dcd(struct msm_otg *motg)
1827{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001828 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301829 u32 chg_det;
1830
1831 switch (motg->pdata->phy_type) {
1832 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001833 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301834 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001835 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301836 break;
1837 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001838 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301839 break;
1840 default:
1841 break;
1842 }
1843}
1844
1845static void msm_chg_enable_dcd(struct msm_otg *motg)
1846{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001847 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301848 u32 chg_det;
1849
1850 switch (motg->pdata->phy_type) {
1851 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001852 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301853 /* Turn on D+ current source */
1854 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001855 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301856 break;
1857 case SNPS_28NM_INTEGRATED_PHY:
1858 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001859 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301860 break;
1861 default:
1862 break;
1863 }
1864}
1865
1866static void msm_chg_block_on(struct msm_otg *motg)
1867{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001868 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301869 u32 func_ctrl, chg_det;
1870
1871 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001872 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301873 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1874 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001875 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301876
1877 switch (motg->pdata->phy_type) {
1878 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001879 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301880 /* control chg block via ULPI */
1881 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001882 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301883 /* Turn on chg detect block */
1884 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001885 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301886 udelay(20);
1887 break;
1888 case SNPS_28NM_INTEGRATED_PHY:
1889 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001890 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301891 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001892 ulpi_write(phy, 0x1F, 0x92);
1893 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301894 udelay(100);
1895 break;
1896 default:
1897 break;
1898 }
1899}
1900
1901static void msm_chg_block_off(struct msm_otg *motg)
1902{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001903 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301904 u32 func_ctrl, chg_det;
1905
1906 switch (motg->pdata->phy_type) {
1907 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001908 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301909 /* Turn off charger block */
1910 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001911 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301912 break;
1913 case SNPS_28NM_INTEGRATED_PHY:
1914 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001915 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301916 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001917 ulpi_write(phy, 0x1F, 0x92);
1918 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301919 break;
1920 default:
1921 break;
1922 }
1923
1924 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001925 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301926 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1927 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001928 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301929}
1930
Anji jonnalad270e2d2011-08-09 11:28:32 +05301931static const char *chg_to_string(enum usb_chg_type chg_type)
1932{
1933 switch (chg_type) {
1934 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1935 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1936 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1937 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1938 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1939 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1940 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301941 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05301942 default: return "INVALID_CHARGER";
1943 }
1944}
1945
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301946#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1947#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301948#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1949#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301950static void msm_chg_detect_work(struct work_struct *w)
1951{
1952 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001953 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301954 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301955 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301956 unsigned long delay;
1957
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001958 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301959
1960 if (test_bit(MHL, &motg->inputs)) {
1961 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
1962 return;
1963 }
1964
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301965 switch (motg->chg_state) {
1966 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301967 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301968 if (motg->pdata->enable_dcd)
1969 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001970 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301971 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1972 motg->dcd_retries = 0;
1973 delay = MSM_CHG_DCD_POLL_TIME;
1974 break;
1975 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301976 if (msm_chg_mhl_detect(motg)) {
1977 msm_chg_block_off(motg);
1978 motg->chg_state = USB_CHG_STATE_DETECTED;
1979 motg->chg_type = USB_INVALID_CHARGER;
1980 queue_work(system_nrt_wq, &motg->sm_work);
1981 return;
1982 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983 is_aca = msm_chg_aca_detect(motg);
1984 if (is_aca) {
1985 /*
1986 * ID_A can be ACA dock too. continue
1987 * primary detection after DCD.
1988 */
1989 if (test_bit(ID_A, &motg->inputs)) {
1990 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1991 } else {
1992 delay = 0;
1993 break;
1994 }
1995 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301996 if (motg->pdata->enable_dcd)
1997 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301998 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1999 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302000 if (motg->pdata->enable_dcd)
2001 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302002 msm_chg_enable_primary_det(motg);
2003 delay = MSM_CHG_PRIMARY_DET_TIME;
2004 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2005 } else {
2006 delay = MSM_CHG_DCD_POLL_TIME;
2007 }
2008 break;
2009 case USB_CHG_STATE_DCD_DONE:
2010 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302011 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2012 dm_vlgc = line_state & PORTSC_LS_DM;
2013 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302014 if (test_bit(ID_A, &motg->inputs)) {
2015 motg->chg_type = USB_ACA_DOCK_CHARGER;
2016 motg->chg_state = USB_CHG_STATE_DETECTED;
2017 delay = 0;
2018 break;
2019 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302020 if (line_state) { /* DP > VLGC */
2021 motg->chg_type = USB_PROPRIETARY_CHARGER;
2022 motg->chg_state = USB_CHG_STATE_DETECTED;
2023 delay = 0;
2024 } else {
2025 msm_chg_enable_secondary_det(motg);
2026 delay = MSM_CHG_SECONDARY_DET_TIME;
2027 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2028 }
2029 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302030 if (test_bit(ID_A, &motg->inputs)) {
2031 motg->chg_type = USB_ACA_A_CHARGER;
2032 motg->chg_state = USB_CHG_STATE_DETECTED;
2033 delay = 0;
2034 break;
2035 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302036
2037 if (line_state) /* DP > VLGC or/and DM > VLGC */
2038 motg->chg_type = USB_PROPRIETARY_CHARGER;
2039 else
2040 motg->chg_type = USB_SDP_CHARGER;
2041
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302042 motg->chg_state = USB_CHG_STATE_DETECTED;
2043 delay = 0;
2044 }
2045 break;
2046 case USB_CHG_STATE_PRIMARY_DONE:
2047 vout = msm_chg_check_secondary_det(motg);
2048 if (vout)
2049 motg->chg_type = USB_DCP_CHARGER;
2050 else
2051 motg->chg_type = USB_CDP_CHARGER;
2052 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2053 /* fall through */
2054 case USB_CHG_STATE_SECONDARY_DONE:
2055 motg->chg_state = USB_CHG_STATE_DETECTED;
2056 case USB_CHG_STATE_DETECTED:
2057 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002058 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302059 /*
2060 * Spurious interrupt is seen after enabling ACA detection
2061 * due to which charger detection fails in case of PET.
2062 * Add delay of 100 microsec to avoid that.
2063 */
2064 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002065 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002066 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302067 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302068 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302069 return;
2070 default:
2071 return;
2072 }
2073
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302074 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302075}
2076
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302077/*
2078 * We support OTG, Peripheral only and Host only configurations. In case
2079 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2080 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2081 * enabled when switch is controlled by user and default mode is supplied
2082 * by board file, which can be changed by userspace later.
2083 */
2084static void msm_otg_init_sm(struct msm_otg *motg)
2085{
2086 struct msm_otg_platform_data *pdata = motg->pdata;
2087 u32 otgsc = readl(USB_OTGSC);
2088
2089 switch (pdata->mode) {
2090 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002091 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302092 if (pdata->default_mode == USB_HOST) {
2093 clear_bit(ID, &motg->inputs);
2094 } else if (pdata->default_mode == USB_PERIPHERAL) {
2095 set_bit(ID, &motg->inputs);
2096 set_bit(B_SESS_VLD, &motg->inputs);
2097 } else {
2098 set_bit(ID, &motg->inputs);
2099 clear_bit(B_SESS_VLD, &motg->inputs);
2100 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302101 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302102 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302103 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302104 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302105 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302106 set_bit(A_BUS_REQ, &motg->inputs);
2107 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002108 if (otgsc & OTGSC_BSV)
2109 set_bit(B_SESS_VLD, &motg->inputs);
2110 else
2111 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302112 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302113 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07002114 unsigned long flags;
2115 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302116 if (irq_read_line(pdata->pmic_id_irq))
2117 set_bit(ID, &motg->inputs);
2118 else
2119 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07002120 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302121 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302122 /*
2123 * VBUS initial state is reported after PMIC
2124 * driver initialization. Wait for it.
2125 */
2126 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302127 }
2128 break;
2129 case USB_HOST:
2130 clear_bit(ID, &motg->inputs);
2131 break;
2132 case USB_PERIPHERAL:
2133 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302134 if (pdata->otg_control == OTG_PHY_CONTROL) {
2135 if (otgsc & OTGSC_BSV)
2136 set_bit(B_SESS_VLD, &motg->inputs);
2137 else
2138 clear_bit(B_SESS_VLD, &motg->inputs);
2139 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2140 /*
2141 * VBUS initial state is reported after PMIC
2142 * driver initialization. Wait for it.
2143 */
2144 wait_for_completion(&pmic_vbus_init);
2145 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302146 break;
2147 default:
2148 break;
2149 }
2150}
2151
2152static void msm_otg_sm_work(struct work_struct *w)
2153{
2154 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002155 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302156 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302157
Steve Mucklef132c6c2012-06-06 18:30:57 -07002158 pm_runtime_resume(otg->phy->dev);
2159 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002160 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302161 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002162 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302163 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302164 psy = power_supply_get_by_name("usb");
2165 if (!psy)
2166 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002167 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302168 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2169 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002170 pm_runtime_put_noidle(otg->phy->dev);
2171 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302172 break;
2173 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302174 /* FALL THROUGH */
2175 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302176 if (test_bit(MHL, &motg->inputs)) {
2177 /* allow LPM */
2178 pm_runtime_put_noidle(otg->phy->dev);
2179 pm_runtime_suspend(otg->phy->dev);
2180 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002181 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302182 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302183 if (msm_chg_mhl_detect(motg)) {
2184 work = 1;
2185 break;
2186 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302187 clear_bit(B_BUS_REQ, &motg->inputs);
2188 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002189 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302190 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302191 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302192 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302193 switch (motg->chg_state) {
2194 case USB_CHG_STATE_UNDEFINED:
2195 msm_chg_detect_work(&motg->chg_work.work);
2196 break;
2197 case USB_CHG_STATE_DETECTED:
2198 switch (motg->chg_type) {
2199 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302200 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002201 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302202 /* fall through */
2203 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302204 msm_otg_notify_charger(motg,
2205 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002206 pm_runtime_put_noidle(otg->phy->dev);
2207 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302208 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302209 case USB_ACA_B_CHARGER:
2210 msm_otg_notify_charger(motg,
2211 IDEV_ACA_CHG_MAX);
2212 /*
2213 * (ID_B --> ID_C) PHY_ALT interrupt can
2214 * not be detected in LPM.
2215 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302216 break;
2217 case USB_CDP_CHARGER:
2218 msm_otg_notify_charger(motg,
2219 IDEV_CHG_MAX);
2220 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002221 otg->phy->state =
2222 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302223 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302224 case USB_ACA_C_CHARGER:
2225 msm_otg_notify_charger(motg,
2226 IDEV_ACA_CHG_MAX);
2227 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002228 otg->phy->state =
2229 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302230 break;
2231 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302232 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002233 otg->phy->state =
2234 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302235 break;
2236 default:
2237 break;
2238 }
2239 break;
2240 default:
2241 break;
2242 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302243 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2244 pr_debug("b_sess_end && b_bus_req\n");
2245 if (msm_otg_start_srp(otg) < 0) {
2246 clear_bit(B_BUS_REQ, &motg->inputs);
2247 work = 1;
2248 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302249 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002250 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302251 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2252 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302253 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302254 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302255 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302256 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2257 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302258 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002259 msm_otg_reset(otg->phy);
2260 pm_runtime_put_noidle(otg->phy->dev);
2261 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302262 }
2263 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302264 case OTG_STATE_B_SRP_INIT:
2265 if (!test_bit(ID, &motg->inputs) ||
2266 test_bit(ID_A, &motg->inputs) ||
2267 test_bit(ID_C, &motg->inputs) ||
2268 (test_bit(B_SESS_VLD, &motg->inputs) &&
2269 !test_bit(ID_B, &motg->inputs))) {
2270 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2271 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002272 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302273 /*
2274 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2275 * bits after SRP initiation.
2276 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002277 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302278 work = 1;
2279 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2280 pr_debug("b_srp_fail\n");
2281 pr_info("A-device did not respond to SRP\n");
2282 clear_bit(B_BUS_REQ, &motg->inputs);
2283 clear_bit(B_SRP_FAIL, &motg->tmouts);
2284 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002285 ulpi_write(otg->phy, 0x0, 0x98);
2286 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302287 motg->b_last_se0_sess = jiffies;
2288 work = 1;
2289 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302290 break;
2291 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302292 if (!test_bit(ID, &motg->inputs) ||
2293 test_bit(ID_A, &motg->inputs) ||
2294 test_bit(ID_B, &motg->inputs) ||
2295 !test_bit(B_SESS_VLD, &motg->inputs)) {
2296 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302297 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2298 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302299 msm_otg_notify_charger(motg, 0);
2300 srp_reqd = otg->gadget->otg_srp_reqd;
2301 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302302 if (test_bit(ID_B, &motg->inputs))
2303 clear_bit(ID_B, &motg->inputs);
2304 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002305 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302306 motg->b_last_se0_sess = jiffies;
2307 if (srp_reqd)
2308 msm_otg_start_timer(motg,
2309 TB_TST_SRP, B_TST_SRP);
2310 else
2311 work = 1;
2312 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2313 otg->gadget->b_hnp_enable &&
2314 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2315 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2316 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2317 /* D+ pullup should not be disconnected within 4msec
2318 * after A device suspends the bus. Otherwise PET will
2319 * fail the compliance test.
2320 */
2321 udelay(1000);
2322 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002323 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302324 /*
2325 * start HCD even before A-device enable
2326 * pull-up to meet HNP timings.
2327 */
2328 otg->host->is_b_host = 1;
2329 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002330 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2331 test_bit(B_SESS_VLD, &motg->inputs)) {
2332 pr_debug("a_bus_suspend && b_sess_vld\n");
2333 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002334 pm_runtime_put_noidle(otg->phy->dev);
2335 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002336 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002337 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302338 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002339 }
2340 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302341 case OTG_STATE_B_WAIT_ACON:
2342 if (!test_bit(ID, &motg->inputs) ||
2343 test_bit(ID_A, &motg->inputs) ||
2344 test_bit(ID_B, &motg->inputs) ||
2345 !test_bit(B_SESS_VLD, &motg->inputs)) {
2346 pr_debug("!id || id_a/b || !b_sess_vld\n");
2347 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302348 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302349 * A-device is physically disconnected during
2350 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302351 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302352 msm_otg_start_host(otg, 0);
2353 otg->host->is_b_host = 0;
2354
2355 clear_bit(B_BUS_REQ, &motg->inputs);
2356 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2357 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002358 otg->phy->state = OTG_STATE_B_IDLE;
2359 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302360 work = 1;
2361 } else if (test_bit(A_CONN, &motg->inputs)) {
2362 pr_debug("a_conn\n");
2363 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002364 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302365 /*
2366 * PET disconnects D+ pullup after reset is generated
2367 * by B device in B_HOST role which is not detected by
2368 * B device. As workaorund , start timer of 300msec
2369 * and stop timer if A device is enumerated else clear
2370 * A_CONN.
2371 */
2372 msm_otg_start_timer(motg, TB_TST_CONFIG,
2373 B_TST_CONFIG);
2374 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2375 pr_debug("b_ase0_brst_tmout\n");
2376 pr_info("B HNP fail:No response from A device\n");
2377 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002378 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302379 otg->host->is_b_host = 0;
2380 clear_bit(B_ASE0_BRST, &motg->tmouts);
2381 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2382 clear_bit(B_BUS_REQ, &motg->inputs);
2383 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002384 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302385 work = 1;
2386 } else if (test_bit(ID_C, &motg->inputs)) {
2387 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2388 }
2389 break;
2390 case OTG_STATE_B_HOST:
2391 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2392 !test_bit(A_CONN, &motg->inputs) ||
2393 !test_bit(B_SESS_VLD, &motg->inputs)) {
2394 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2395 clear_bit(A_CONN, &motg->inputs);
2396 clear_bit(B_BUS_REQ, &motg->inputs);
2397 msm_otg_start_host(otg, 0);
2398 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002399 otg->phy->state = OTG_STATE_B_IDLE;
2400 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302401 work = 1;
2402 } else if (test_bit(ID_C, &motg->inputs)) {
2403 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2404 }
2405 break;
2406 case OTG_STATE_A_IDLE:
2407 otg->default_a = 1;
2408 if (test_bit(ID, &motg->inputs) &&
2409 !test_bit(ID_A, &motg->inputs)) {
2410 pr_debug("id && !id_a\n");
2411 otg->default_a = 0;
2412 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002413 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302414 del_timer_sync(&motg->id_timer);
2415 msm_otg_link_reset(motg);
2416 msm_chg_enable_aca_intr(motg);
2417 msm_otg_notify_charger(motg, 0);
2418 work = 1;
2419 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2420 (test_bit(A_SRP_DET, &motg->inputs) ||
2421 test_bit(A_BUS_REQ, &motg->inputs))) {
2422 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2423
2424 clear_bit(A_SRP_DET, &motg->inputs);
2425 /* Disable SRP detection */
2426 writel_relaxed((readl_relaxed(USB_OTGSC) &
2427 ~OTGSC_INTSTS_MASK) &
2428 ~OTGSC_DPIE, USB_OTGSC);
2429
Steve Mucklef132c6c2012-06-06 18:30:57 -07002430 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302431 /* VBUS should not be supplied before end of SRP pulse
2432 * generated by PET, if not complaince test fail.
2433 */
2434 usleep_range(10000, 12000);
2435 /* ACA: ID_A: Stop charging untill enumeration */
2436 if (test_bit(ID_A, &motg->inputs))
2437 msm_otg_notify_charger(motg, 0);
2438 else
2439 msm_hsusb_vbus_power(motg, 1);
2440 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2441 } else {
2442 pr_debug("No session requested\n");
2443 clear_bit(A_BUS_DROP, &motg->inputs);
2444 if (test_bit(ID_A, &motg->inputs)) {
2445 msm_otg_notify_charger(motg,
2446 IDEV_ACA_CHG_MAX);
2447 } else if (!test_bit(ID, &motg->inputs)) {
2448 msm_otg_notify_charger(motg, 0);
2449 /*
2450 * A-device is not providing power on VBUS.
2451 * Enable SRP detection.
2452 */
2453 writel_relaxed(0x13, USB_USBMODE);
2454 writel_relaxed((readl_relaxed(USB_OTGSC) &
2455 ~OTGSC_INTSTS_MASK) |
2456 OTGSC_DPIE, USB_OTGSC);
2457 mb();
2458 }
2459 }
2460 break;
2461 case OTG_STATE_A_WAIT_VRISE:
2462 if ((test_bit(ID, &motg->inputs) &&
2463 !test_bit(ID_A, &motg->inputs)) ||
2464 test_bit(A_BUS_DROP, &motg->inputs) ||
2465 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2466 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2467 clear_bit(A_BUS_REQ, &motg->inputs);
2468 msm_otg_del_timer(motg);
2469 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002470 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302471 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2472 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2473 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002474 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302475 if (TA_WAIT_BCON > 0)
2476 msm_otg_start_timer(motg, TA_WAIT_BCON,
2477 A_WAIT_BCON);
2478 msm_otg_start_host(otg, 1);
2479 msm_chg_enable_aca_det(motg);
2480 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302481 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302482 if (msm_chg_check_aca_intr(motg))
2483 work = 1;
2484 }
2485 break;
2486 case OTG_STATE_A_WAIT_BCON:
2487 if ((test_bit(ID, &motg->inputs) &&
2488 !test_bit(ID_A, &motg->inputs)) ||
2489 test_bit(A_BUS_DROP, &motg->inputs) ||
2490 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2491 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2492 "a_wait_bcon_tmout\n");
2493 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2494 pr_info("Device No Response\n");
2495 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2496 }
2497 msm_otg_del_timer(motg);
2498 clear_bit(A_BUS_REQ, &motg->inputs);
2499 clear_bit(B_CONN, &motg->inputs);
2500 msm_otg_start_host(otg, 0);
2501 /*
2502 * ACA: ID_A with NO accessory, just the A plug is
2503 * attached to ACA: Use IDCHG_MAX for charging
2504 */
2505 if (test_bit(ID_A, &motg->inputs))
2506 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2507 else
2508 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002509 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302510 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2511 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2512 pr_debug("!a_vbus_vld\n");
2513 clear_bit(B_CONN, &motg->inputs);
2514 msm_otg_del_timer(motg);
2515 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002516 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2517 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302518 } else if (test_bit(ID_A, &motg->inputs)) {
2519 msm_hsusb_vbus_power(motg, 0);
2520 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2521 /*
2522 * If TA_WAIT_BCON is infinite, we don;t
2523 * turn off VBUS. Enter low power mode.
2524 */
2525 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002526 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302527 } else if (!test_bit(ID, &motg->inputs)) {
2528 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302529 }
2530 break;
2531 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302532 if ((test_bit(ID, &motg->inputs) &&
2533 !test_bit(ID_A, &motg->inputs)) ||
2534 test_bit(A_BUS_DROP, &motg->inputs)) {
2535 pr_debug("id_a/b/c || a_bus_drop\n");
2536 clear_bit(B_CONN, &motg->inputs);
2537 clear_bit(A_BUS_REQ, &motg->inputs);
2538 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002539 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302540 msm_otg_start_host(otg, 0);
2541 if (!test_bit(ID_A, &motg->inputs))
2542 msm_hsusb_vbus_power(motg, 0);
2543 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2544 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2545 pr_debug("!a_vbus_vld\n");
2546 clear_bit(B_CONN, &motg->inputs);
2547 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002548 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302549 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002550 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302551 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2552 /*
2553 * a_bus_req is de-asserted when root hub is
2554 * suspended or HNP is in progress.
2555 */
2556 pr_debug("!a_bus_req\n");
2557 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002558 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302559 if (otg->host->b_hnp_enable)
2560 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2561 A_AIDL_BDIS);
2562 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002563 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302564 } else if (!test_bit(B_CONN, &motg->inputs)) {
2565 pr_debug("!b_conn\n");
2566 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002567 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302568 if (TA_WAIT_BCON > 0)
2569 msm_otg_start_timer(motg, TA_WAIT_BCON,
2570 A_WAIT_BCON);
2571 if (msm_chg_check_aca_intr(motg))
2572 work = 1;
2573 } else if (test_bit(ID_A, &motg->inputs)) {
2574 msm_otg_del_timer(motg);
2575 msm_hsusb_vbus_power(motg, 0);
2576 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2577 msm_otg_notify_charger(motg,
2578 IDEV_ACA_CHG_MAX);
2579 else
2580 msm_otg_notify_charger(motg,
2581 IDEV_CHG_MIN - motg->mA_port);
2582 } else if (!test_bit(ID, &motg->inputs)) {
2583 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2584 motg->chg_type = USB_INVALID_CHARGER;
2585 msm_otg_notify_charger(motg, 0);
2586 msm_hsusb_vbus_power(motg, 1);
2587 }
2588 break;
2589 case OTG_STATE_A_SUSPEND:
2590 if ((test_bit(ID, &motg->inputs) &&
2591 !test_bit(ID_A, &motg->inputs)) ||
2592 test_bit(A_BUS_DROP, &motg->inputs) ||
2593 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2594 pr_debug("id_a/b/c || a_bus_drop ||"
2595 "a_aidl_bdis_tmout\n");
2596 msm_otg_del_timer(motg);
2597 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002598 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302599 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002600 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302601 if (!test_bit(ID_A, &motg->inputs))
2602 msm_hsusb_vbus_power(motg, 0);
2603 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2604 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2605 pr_debug("!a_vbus_vld\n");
2606 msm_otg_del_timer(motg);
2607 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002608 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302609 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002610 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302611 } else if (!test_bit(B_CONN, &motg->inputs) &&
2612 otg->host->b_hnp_enable) {
2613 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002614 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302615 msm_otg_host_hnp_enable(otg, 1);
2616 otg->gadget->is_a_peripheral = 1;
2617 msm_otg_start_peripheral(otg, 1);
2618 } else if (!test_bit(B_CONN, &motg->inputs) &&
2619 !otg->host->b_hnp_enable) {
2620 pr_debug("!b_conn && !b_hnp_enable");
2621 /*
2622 * bus request is dropped during suspend.
2623 * acquire again for next device.
2624 */
2625 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002626 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302627 if (TA_WAIT_BCON > 0)
2628 msm_otg_start_timer(motg, TA_WAIT_BCON,
2629 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002630 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302631 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002632 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302633 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002634 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002635 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302636 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302637 }
2638 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302639 case OTG_STATE_A_PERIPHERAL:
2640 if ((test_bit(ID, &motg->inputs) &&
2641 !test_bit(ID_A, &motg->inputs)) ||
2642 test_bit(A_BUS_DROP, &motg->inputs)) {
2643 pr_debug("id _f/b/c || a_bus_drop\n");
2644 /* Clear BIDL_ADIS timer */
2645 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002646 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302647 msm_otg_start_peripheral(otg, 0);
2648 otg->gadget->is_a_peripheral = 0;
2649 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002650 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302651 if (!test_bit(ID_A, &motg->inputs))
2652 msm_hsusb_vbus_power(motg, 0);
2653 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2654 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2655 pr_debug("!a_vbus_vld\n");
2656 /* Clear BIDL_ADIS timer */
2657 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002658 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302659 msm_otg_start_peripheral(otg, 0);
2660 otg->gadget->is_a_peripheral = 0;
2661 msm_otg_start_host(otg, 0);
2662 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2663 pr_debug("a_bidl_adis_tmout\n");
2664 msm_otg_start_peripheral(otg, 0);
2665 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002666 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302667 set_bit(A_BUS_REQ, &motg->inputs);
2668 msm_otg_host_hnp_enable(otg, 0);
2669 if (TA_WAIT_BCON > 0)
2670 msm_otg_start_timer(motg, TA_WAIT_BCON,
2671 A_WAIT_BCON);
2672 } else if (test_bit(ID_A, &motg->inputs)) {
2673 msm_hsusb_vbus_power(motg, 0);
2674 msm_otg_notify_charger(motg,
2675 IDEV_CHG_MIN - motg->mA_port);
2676 } else if (!test_bit(ID, &motg->inputs)) {
2677 msm_otg_notify_charger(motg, 0);
2678 msm_hsusb_vbus_power(motg, 1);
2679 }
2680 break;
2681 case OTG_STATE_A_WAIT_VFALL:
2682 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2683 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002684 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302685 work = 1;
2686 }
2687 break;
2688 case OTG_STATE_A_VBUS_ERR:
2689 if ((test_bit(ID, &motg->inputs) &&
2690 !test_bit(ID_A, &motg->inputs)) ||
2691 test_bit(A_BUS_DROP, &motg->inputs) ||
2692 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002693 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302694 if (!test_bit(ID_A, &motg->inputs))
2695 msm_hsusb_vbus_power(motg, 0);
2696 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2697 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2698 motg->chg_type = USB_INVALID_CHARGER;
2699 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302700 }
2701 break;
2702 default:
2703 break;
2704 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302705 if (work)
2706 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302707}
2708
2709static irqreturn_t msm_otg_irq(int irq, void *data)
2710{
2711 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002712 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302713 u32 otgsc = 0, usbsts, pc;
2714 bool work = 0;
2715 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302716
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302717 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302718 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302719 disable_irq_nosync(irq);
2720 motg->async_int = 1;
Jack Pham5ca279b2012-05-14 18:42:54 -07002721 if (atomic_read(&motg->pm_suspended))
2722 motg->sm_work_pending = true;
2723 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002724 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302725 return IRQ_HANDLED;
2726 }
2727
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002728 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302729 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302730
2731 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302732 return IRQ_NONE;
2733
2734 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302735 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302736 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302737 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302738 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302739 pr_debug("Id clear\n");
2740 /*
2741 * Assert a_bus_req to supply power on
2742 * VBUS when Micro/Mini-A cable is connected
2743 * with out user intervention.
2744 */
2745 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302746 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302747 msm_chg_enable_aca_det(motg);
2748 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302749 writel_relaxed(otgsc, USB_OTGSC);
2750 work = 1;
2751 } else if (otgsc & OTGSC_DPIS) {
2752 pr_debug("DPIS detected\n");
2753 writel_relaxed(otgsc, USB_OTGSC);
2754 set_bit(A_SRP_DET, &motg->inputs);
2755 set_bit(A_BUS_REQ, &motg->inputs);
2756 work = 1;
2757 } else if (otgsc & OTGSC_BSVIS) {
2758 writel_relaxed(otgsc, USB_OTGSC);
2759 /*
2760 * BSV interrupt comes when operating as an A-device
2761 * (VBUS on/off).
2762 * But, handle BSV when charger is removed from ACA in ID_A
2763 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002764 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302765 !test_bit(ID_A, &motg->inputs))
2766 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302767 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302768 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302769 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302770 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302771 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302772 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002773 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2774
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302775 msm_chg_check_aca_intr(motg);
2776 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302777 work = 1;
2778 } else if (usbsts & STS_PCI) {
2779 pc = readl_relaxed(USB_PORTSC);
2780 pr_debug("portsc = %x\n", pc);
2781 ret = IRQ_NONE;
2782 /*
2783 * HCD Acks PCI interrupt. We use this to switch
2784 * between different OTG states.
2785 */
2786 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002787 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302788 case OTG_STATE_A_SUSPEND:
2789 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2790 !(pc & PORTSC_CCS)) {
2791 pr_debug("B_CONN clear\n");
2792 clear_bit(B_CONN, &motg->inputs);
2793 msm_otg_del_timer(motg);
2794 }
2795 break;
2796 case OTG_STATE_A_PERIPHERAL:
2797 /*
2798 * A-peripheral observed activity on bus.
2799 * clear A_BIDL_ADIS timer.
2800 */
2801 msm_otg_del_timer(motg);
2802 work = 0;
2803 break;
2804 case OTG_STATE_B_WAIT_ACON:
2805 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2806 pr_debug("A_CONN set\n");
2807 set_bit(A_CONN, &motg->inputs);
2808 /* Clear ASE0_BRST timer */
2809 msm_otg_del_timer(motg);
2810 }
2811 break;
2812 case OTG_STATE_B_HOST:
2813 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2814 pr_debug("A_CONN clear\n");
2815 clear_bit(A_CONN, &motg->inputs);
2816 msm_otg_del_timer(motg);
2817 }
2818 break;
2819 case OTG_STATE_A_WAIT_BCON:
2820 if (TA_WAIT_BCON < 0)
2821 set_bit(A_BUS_REQ, &motg->inputs);
2822 default:
2823 work = 0;
2824 break;
2825 }
2826 } else if (usbsts & STS_URI) {
2827 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002828 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302829 case OTG_STATE_A_PERIPHERAL:
2830 /*
2831 * A-peripheral observed activity on bus.
2832 * clear A_BIDL_ADIS timer.
2833 */
2834 msm_otg_del_timer(motg);
2835 work = 0;
2836 break;
2837 default:
2838 work = 0;
2839 break;
2840 }
2841 } else if (usbsts & STS_SLI) {
2842 ret = IRQ_NONE;
2843 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002844 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302845 case OTG_STATE_B_PERIPHERAL:
2846 if (otg->gadget->b_hnp_enable) {
2847 set_bit(A_BUS_SUSPEND, &motg->inputs);
2848 set_bit(B_BUS_REQ, &motg->inputs);
2849 work = 1;
2850 }
2851 break;
2852 case OTG_STATE_A_PERIPHERAL:
2853 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2854 A_BIDL_ADIS);
2855 break;
2856 default:
2857 break;
2858 }
2859 } else if ((usbsts & PHY_ALT_INT)) {
2860 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2861 if (msm_chg_check_aca_intr(motg))
2862 work = 1;
2863 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302864 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302865 if (work)
2866 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302867
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302868 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002869}
2870
2871static void msm_otg_set_vbus_state(int online)
2872{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302873 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002874 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05302875 struct usb_otg *otg = motg->phy.otg;
2876
2877 /* In A Host Mode, ignore received BSV interrupts */
2878 if (otg->phy->state >= OTG_STATE_A_IDLE)
2879 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002880
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302881 if (online) {
2882 pr_debug("PMIC: BSV set\n");
2883 set_bit(B_SESS_VLD, &motg->inputs);
2884 } else {
2885 pr_debug("PMIC: BSV clear\n");
2886 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302887 }
2888
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302889 if (!init) {
2890 init = true;
2891 complete(&pmic_vbus_init);
2892 pr_debug("PMIC: BSV init complete\n");
2893 return;
2894 }
2895
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302896 if (test_bit(MHL, &motg->inputs) ||
2897 mhl_det_in_progress) {
2898 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
2899 return;
2900 }
2901
Jack Pham5ca279b2012-05-14 18:42:54 -07002902 if (atomic_read(&motg->pm_suspended))
2903 motg->sm_work_pending = true;
2904 else
2905 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002906}
2907
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302908static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002909{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302910 struct msm_otg *motg = container_of(w, struct msm_otg,
2911 pmic_id_status_work.work);
2912 int work = 0;
2913 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002914
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302915 local_irq_save(flags);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302916 if (irq_read_line(motg->pdata->pmic_id_irq)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302917 if (!test_and_set_bit(ID, &motg->inputs)) {
2918 pr_debug("PMIC: ID set\n");
2919 work = 1;
2920 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302921 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302922 if (test_and_clear_bit(ID, &motg->inputs)) {
2923 pr_debug("PMIC: ID clear\n");
2924 set_bit(A_BUS_REQ, &motg->inputs);
2925 work = 1;
2926 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302927 }
2928
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302929 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07002930 if (atomic_read(&motg->pm_suspended))
2931 motg->sm_work_pending = true;
2932 else
2933 queue_work(system_nrt_wq, &motg->sm_work);
2934 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302935 local_irq_restore(flags);
2936
2937}
2938
2939#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
2940static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2941{
2942 struct msm_otg *motg = data;
2943
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302944 if (test_bit(MHL, &motg->inputs) ||
2945 mhl_det_in_progress) {
2946 pr_debug("PMIC: Id interrupt ignored in MHL\n");
2947 return IRQ_HANDLED;
2948 }
2949
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302950 if (!aca_id_turned_on)
2951 /*schedule delayed work for 5msec for ID line state to settle*/
2952 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
2953 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002954
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302955 return IRQ_HANDLED;
2956}
2957
2958static int msm_otg_mode_show(struct seq_file *s, void *unused)
2959{
2960 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002961 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302962
Steve Mucklef132c6c2012-06-06 18:30:57 -07002963 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302964 case OTG_STATE_A_HOST:
2965 seq_printf(s, "host\n");
2966 break;
2967 case OTG_STATE_B_PERIPHERAL:
2968 seq_printf(s, "peripheral\n");
2969 break;
2970 default:
2971 seq_printf(s, "none\n");
2972 break;
2973 }
2974
2975 return 0;
2976}
2977
2978static int msm_otg_mode_open(struct inode *inode, struct file *file)
2979{
2980 return single_open(file, msm_otg_mode_show, inode->i_private);
2981}
2982
2983static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2984 size_t count, loff_t *ppos)
2985{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302986 struct seq_file *s = file->private_data;
2987 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302988 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07002989 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302990 int status = count;
2991 enum usb_mode_type req_mode;
2992
2993 memset(buf, 0x00, sizeof(buf));
2994
2995 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2996 status = -EFAULT;
2997 goto out;
2998 }
2999
3000 if (!strncmp(buf, "host", 4)) {
3001 req_mode = USB_HOST;
3002 } else if (!strncmp(buf, "peripheral", 10)) {
3003 req_mode = USB_PERIPHERAL;
3004 } else if (!strncmp(buf, "none", 4)) {
3005 req_mode = USB_NONE;
3006 } else {
3007 status = -EINVAL;
3008 goto out;
3009 }
3010
3011 switch (req_mode) {
3012 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003013 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303014 case OTG_STATE_A_HOST:
3015 case OTG_STATE_B_PERIPHERAL:
3016 set_bit(ID, &motg->inputs);
3017 clear_bit(B_SESS_VLD, &motg->inputs);
3018 break;
3019 default:
3020 goto out;
3021 }
3022 break;
3023 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003024 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303025 case OTG_STATE_B_IDLE:
3026 case OTG_STATE_A_HOST:
3027 set_bit(ID, &motg->inputs);
3028 set_bit(B_SESS_VLD, &motg->inputs);
3029 break;
3030 default:
3031 goto out;
3032 }
3033 break;
3034 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003035 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303036 case OTG_STATE_B_IDLE:
3037 case OTG_STATE_B_PERIPHERAL:
3038 clear_bit(ID, &motg->inputs);
3039 break;
3040 default:
3041 goto out;
3042 }
3043 break;
3044 default:
3045 goto out;
3046 }
3047
Steve Mucklef132c6c2012-06-06 18:30:57 -07003048 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303049 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303050out:
3051 return status;
3052}
3053
3054const struct file_operations msm_otg_mode_fops = {
3055 .open = msm_otg_mode_open,
3056 .read = seq_read,
3057 .write = msm_otg_mode_write,
3058 .llseek = seq_lseek,
3059 .release = single_release,
3060};
3061
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303062static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3063{
3064 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003065 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303066
Steve Mucklef132c6c2012-06-06 18:30:57 -07003067 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303068 return 0;
3069}
3070
3071static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3072{
3073 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3074}
3075
3076const struct file_operations msm_otg_state_fops = {
3077 .open = msm_otg_otg_state_open,
3078 .read = seq_read,
3079 .llseek = seq_lseek,
3080 .release = single_release,
3081};
3082
Anji jonnalad270e2d2011-08-09 11:28:32 +05303083static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3084{
3085 struct msm_otg *motg = s->private;
3086
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303087 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303088 return 0;
3089}
3090
3091static int msm_otg_chg_open(struct inode *inode, struct file *file)
3092{
3093 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3094}
3095
3096const struct file_operations msm_otg_chg_fops = {
3097 .open = msm_otg_chg_open,
3098 .read = seq_read,
3099 .llseek = seq_lseek,
3100 .release = single_release,
3101};
3102
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303103static int msm_otg_aca_show(struct seq_file *s, void *unused)
3104{
3105 if (debug_aca_enabled)
3106 seq_printf(s, "enabled\n");
3107 else
3108 seq_printf(s, "disabled\n");
3109
3110 return 0;
3111}
3112
3113static int msm_otg_aca_open(struct inode *inode, struct file *file)
3114{
3115 return single_open(file, msm_otg_aca_show, inode->i_private);
3116}
3117
3118static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3119 size_t count, loff_t *ppos)
3120{
3121 char buf[8];
3122
3123 memset(buf, 0x00, sizeof(buf));
3124
3125 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3126 return -EFAULT;
3127
3128 if (!strncmp(buf, "enable", 6))
3129 debug_aca_enabled = true;
3130 else
3131 debug_aca_enabled = false;
3132
3133 return count;
3134}
3135
3136const struct file_operations msm_otg_aca_fops = {
3137 .open = msm_otg_aca_open,
3138 .read = seq_read,
3139 .write = msm_otg_aca_write,
3140 .llseek = seq_lseek,
3141 .release = single_release,
3142};
3143
Manu Gautam8bdcc592012-03-06 11:26:06 +05303144static int msm_otg_bus_show(struct seq_file *s, void *unused)
3145{
3146 if (debug_bus_voting_enabled)
3147 seq_printf(s, "enabled\n");
3148 else
3149 seq_printf(s, "disabled\n");
3150
3151 return 0;
3152}
3153
3154static int msm_otg_bus_open(struct inode *inode, struct file *file)
3155{
3156 return single_open(file, msm_otg_bus_show, inode->i_private);
3157}
3158
3159static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3160 size_t count, loff_t *ppos)
3161{
3162 char buf[8];
3163 int ret;
3164 struct seq_file *s = file->private_data;
3165 struct msm_otg *motg = s->private;
3166
3167 memset(buf, 0x00, sizeof(buf));
3168
3169 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3170 return -EFAULT;
3171
3172 if (!strncmp(buf, "enable", 6)) {
3173 /* Do not vote here. Let OTG statemachine decide when to vote */
3174 debug_bus_voting_enabled = true;
3175 } else {
3176 debug_bus_voting_enabled = false;
3177 if (motg->bus_perf_client) {
3178 ret = msm_bus_scale_client_update_request(
3179 motg->bus_perf_client, 0);
3180 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003181 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303182 "for bus bw %d\n", __func__, ret);
3183 }
3184 }
3185
3186 return count;
3187}
3188
3189const struct file_operations msm_otg_bus_fops = {
3190 .open = msm_otg_bus_open,
3191 .read = seq_read,
3192 .write = msm_otg_bus_write,
3193 .llseek = seq_lseek,
3194 .release = single_release,
3195};
3196
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303197static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303198
3199static int msm_otg_debugfs_init(struct msm_otg *motg)
3200{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303201 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303202
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303203 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3204
3205 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3206 return -ENODEV;
3207
Anji jonnalad270e2d2011-08-09 11:28:32 +05303208 if (motg->pdata->mode == USB_OTG &&
3209 motg->pdata->otg_control == OTG_USER_CONTROL) {
3210
Manu Gautam8bdcc592012-03-06 11:26:06 +05303211 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303212 S_IWUSR, msm_otg_dbg_root, motg,
3213 &msm_otg_mode_fops);
3214
Manu Gautam8bdcc592012-03-06 11:26:06 +05303215 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303216 debugfs_remove(msm_otg_dbg_root);
3217 msm_otg_dbg_root = NULL;
3218 return -ENODEV;
3219 }
3220 }
3221
Manu Gautam8bdcc592012-03-06 11:26:06 +05303222 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303223 msm_otg_dbg_root, motg,
3224 &msm_otg_chg_fops);
3225
Manu Gautam8bdcc592012-03-06 11:26:06 +05303226 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303227 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303228 return -ENODEV;
3229 }
3230
Manu Gautam8bdcc592012-03-06 11:26:06 +05303231 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303232 msm_otg_dbg_root, motg,
3233 &msm_otg_aca_fops);
3234
Manu Gautam8bdcc592012-03-06 11:26:06 +05303235 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303236 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303237 return -ENODEV;
3238 }
3239
Manu Gautam8bdcc592012-03-06 11:26:06 +05303240 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3241 msm_otg_dbg_root, motg,
3242 &msm_otg_bus_fops);
3243
3244 if (!msm_otg_dentry) {
3245 debugfs_remove_recursive(msm_otg_dbg_root);
3246 return -ENODEV;
3247 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303248
3249 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3250 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3251
3252 if (!msm_otg_dentry) {
3253 debugfs_remove_recursive(msm_otg_dbg_root);
3254 return -ENODEV;
3255 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303256 return 0;
3257}
3258
3259static void msm_otg_debugfs_cleanup(void)
3260{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303261 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303262}
3263
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303264static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3265static struct platform_device *msm_otg_add_pdev(
3266 struct platform_device *ofdev, const char *name)
3267{
3268 struct platform_device *pdev;
3269 const struct resource *res = ofdev->resource;
3270 unsigned int num = ofdev->num_resources;
3271 int retval;
3272
3273 pdev = platform_device_alloc(name, -1);
3274 if (!pdev) {
3275 retval = -ENOMEM;
3276 goto error;
3277 }
3278
3279 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3280 pdev->dev.dma_mask = &msm_otg_dma_mask;
3281
3282 if (num) {
3283 retval = platform_device_add_resources(pdev, res, num);
3284 if (retval)
3285 goto error;
3286 }
3287
3288 retval = platform_device_add(pdev);
3289 if (retval)
3290 goto error;
3291
3292 return pdev;
3293
3294error:
3295 platform_device_put(pdev);
3296 return ERR_PTR(retval);
3297}
3298
3299static int msm_otg_setup_devices(struct platform_device *ofdev,
3300 enum usb_mode_type mode, bool init)
3301{
3302 const char *gadget_name = "msm_hsusb";
3303 const char *host_name = "msm_hsusb_host";
3304 static struct platform_device *gadget_pdev;
3305 static struct platform_device *host_pdev;
3306 int retval = 0;
3307
3308 if (!init) {
3309 if (gadget_pdev)
3310 platform_device_unregister(gadget_pdev);
3311 if (host_pdev)
3312 platform_device_unregister(host_pdev);
3313 return 0;
3314 }
3315
3316 switch (mode) {
3317 case USB_OTG:
3318 /* fall through */
3319 case USB_PERIPHERAL:
3320 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3321 if (IS_ERR(gadget_pdev)) {
3322 retval = PTR_ERR(gadget_pdev);
3323 break;
3324 }
3325 if (mode == USB_PERIPHERAL)
3326 break;
3327 /* fall through */
3328 case USB_HOST:
3329 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3330 if (IS_ERR(host_pdev)) {
3331 retval = PTR_ERR(host_pdev);
3332 if (mode == USB_OTG)
3333 platform_device_unregister(gadget_pdev);
3334 }
3335 break;
3336 default:
3337 break;
3338 }
3339
3340 return retval;
3341}
3342
3343struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3344{
3345 struct device_node *node = pdev->dev.of_node;
3346 struct msm_otg_platform_data *pdata;
3347 int len = 0;
3348
3349 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3350 if (!pdata) {
3351 pr_err("unable to allocate platform data\n");
3352 return NULL;
3353 }
3354 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3355 if (len) {
3356 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3357 if (!pdata->phy_init_seq)
3358 return NULL;
3359 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3360 pdata->phy_init_seq,
3361 len/sizeof(*pdata->phy_init_seq));
3362 }
3363 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3364 &pdata->power_budget);
3365 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3366 &pdata->mode);
3367 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3368 &pdata->otg_control);
3369 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3370 &pdata->default_mode);
3371 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3372 &pdata->phy_type);
3373 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3374 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303375 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303376}
3377
3378static int __init msm_otg_probe(struct platform_device *pdev)
3379{
3380 int ret = 0;
3381 struct resource *res;
3382 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003383 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303384 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303385
3386 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303387
3388 if (pdev->dev.of_node) {
3389 dev_dbg(&pdev->dev, "device tree enabled\n");
3390 pdata = msm_otg_dt_to_pdata(pdev);
3391 if (!pdata)
3392 return -ENOMEM;
3393 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3394 if (ret) {
3395 dev_err(&pdev->dev, "devices setup failed\n");
3396 return ret;
3397 }
3398 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303399 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3400 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303401 } else {
3402 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303403 }
3404
3405 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3406 if (!motg) {
3407 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3408 return -ENOMEM;
3409 }
3410
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003411 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3412 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003413 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3414 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303415 goto free_motg;
3416 }
3417
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003418 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303419 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003420 phy = &motg->phy;
3421 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303422
3423 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303424 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3425 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3426 * PMIC can detect ACA ID_GND and generate an interrupt.
3427 */
3428 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3429 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3430 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003431 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303432 }
3433
Ofir Cohen4da266f2012-01-03 10:19:29 +02003434 /* initialize reset counter */
3435 motg->reset_counter = 0;
3436
Amit Blay02eff132011-09-21 16:46:24 +03003437 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003438 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003439 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003440 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303441
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303442 /*
3443 * Targets on which link uses asynchronous reset methodology,
3444 * free running clock is not required during the reset.
3445 */
Manu Gautam5143b252012-01-05 19:25:23 -08003446 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303447 if (IS_ERR(motg->clk))
3448 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3449 else
3450 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303451
3452 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003453 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303454 * CORE CLK must be running at >55Mhz for correct HSUSB
3455 * operation and USB core cannot tolerate frequency changes on
3456 * CORE CLK. For such USB cores, vote for maximum clk frequency
3457 * on pclk source
3458 */
Manu Gautam5143b252012-01-05 19:25:23 -08003459 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3460 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303461 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003462 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303463 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003464 goto put_clk;
3465 }
3466 clk_set_rate(motg->core_clk, INT_MAX);
3467
3468 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3469 if (IS_ERR(motg->pclk)) {
3470 dev_err(&pdev->dev, "failed to get iface_clk\n");
3471 ret = PTR_ERR(motg->pclk);
3472 goto put_core_clk;
3473 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303474
3475 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3476 if (!res) {
3477 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3478 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003479 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303480 }
3481
3482 motg->regs = ioremap(res->start, resource_size(res));
3483 if (!motg->regs) {
3484 dev_err(&pdev->dev, "ioremap failed\n");
3485 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003486 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303487 }
3488 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3489
3490 motg->irq = platform_get_irq(pdev, 0);
3491 if (!motg->irq) {
3492 dev_err(&pdev->dev, "platform_get_irq failed\n");
3493 ret = -ENODEV;
3494 goto free_regs;
3495 }
3496
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003497 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003498 if (IS_ERR(motg->xo_handle)) {
3499 dev_err(&pdev->dev, "%s not able to get the handle "
3500 "to vote for TCXO D0 buffer\n", __func__);
3501 ret = PTR_ERR(motg->xo_handle);
3502 goto free_regs;
3503 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303504
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003505 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003506 if (ret) {
3507 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3508 "D0 buffer%d\n", __func__, ret);
3509 goto free_xo_handle;
3510 }
3511
Manu Gautam28b1bac2012-01-30 16:43:06 +05303512 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303513
Mayank Rana248698c2012-04-19 00:03:16 +05303514 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003515 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303516 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003517 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303518 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003519 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003520 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303521 goto devote_xo_handle;
3522 }
3523 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303524 }
3525
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003526 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303527 if (ret) {
3528 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303529 goto devote_xo_handle;
3530 }
3531
3532 ret = regulator_enable(hsusb_vddcx);
3533 if (ret) {
3534 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3535 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303536 }
3537
3538 ret = msm_hsusb_ldo_init(motg, 1);
3539 if (ret) {
3540 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303541 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303542 }
3543
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303544 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303545 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3546 "mhl_usb_hs_switch");
3547 if (IS_ERR(mhl_usb_hs_switch)) {
3548 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003549 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303550 goto free_ldo_init;
3551 }
3552 }
3553
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003554 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303555 if (ret) {
3556 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003557 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303558 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303559 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303560
3561 writel(0, USB_USBINTR);
3562 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003563 /* Ensure that above STOREs are completed before enabling interrupts */
3564 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303565
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303566 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3567 if (ret)
3568 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003569 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303570 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303571 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303572 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303573 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303574 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3575 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303576 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3577 "msm_otg", motg);
3578 if (ret) {
3579 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003580 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303581 }
3582
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003583 phy->init = msm_otg_reset;
3584 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003585 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303586
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003587 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303588
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003589 phy->otg->phy = &motg->phy;
3590 phy->otg->set_host = msm_otg_set_host;
3591 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003592 phy->otg->start_hnp = msm_otg_start_hnp;
3593 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003594
3595 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303596 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003597 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303598 goto free_irq;
3599 }
3600
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303601 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303602 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003603 if (motg->pdata->pmic_id_irq) {
3604 ret = request_irq(motg->pdata->pmic_id_irq,
3605 msm_pmic_id_irq,
3606 IRQF_TRIGGER_RISING |
3607 IRQF_TRIGGER_FALLING,
3608 "msm_otg", motg);
3609 if (ret) {
3610 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003611 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003612 }
3613 } else {
3614 ret = -ENODEV;
3615 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003616 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003617 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303618 }
3619
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303620 msm_hsusb_mhl_switch_enable(motg, 1);
3621
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303622 platform_set_drvdata(pdev, motg);
3623 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003624 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303625
Anji jonnalad270e2d2011-08-09 11:28:32 +05303626 ret = msm_otg_debugfs_init(motg);
3627 if (ret)
3628 dev_dbg(&pdev->dev, "mode debugfs file is"
3629 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303630
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003631 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3632 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3633
Amit Blay58b31472011-11-18 09:39:39 +02003634 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3635 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303636 (!(motg->pdata->mode == USB_OTG) ||
3637 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003638 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303639 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003640
Amit Blay58b31472011-11-18 09:39:39 +02003641 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3642 motg->caps = ALLOW_PHY_RETENTION;
3643 }
3644
Amit Blay6fa647a2012-05-24 14:12:08 +03003645 if (motg->pdata->enable_lpm_on_dev_suspend)
3646 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3647
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003648 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303649 pm_runtime_set_active(&pdev->dev);
3650 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303651
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303652 if (motg->pdata->bus_scale_table) {
3653 motg->bus_perf_client =
3654 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3655 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003656 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303657 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303658 else
3659 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303660 }
3661
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303662 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003663
Steve Mucklef132c6c2012-06-06 18:30:57 -07003664remove_phy:
3665 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303666free_irq:
3667 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003668destroy_wlock:
3669 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303670 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003671 msm_hsusb_ldo_enable(motg, 0);
3672free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303673 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303674free_hsusb_vddcx:
3675 regulator_disable(hsusb_vddcx);
3676free_config_vddcx:
3677 regulator_set_voltage(hsusb_vddcx,
3678 vdd_val[motg->vdd_type][VDD_NONE],
3679 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003680devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303681 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003682 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003683free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003684 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303685free_regs:
3686 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003687put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303688 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303689put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003690 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303691put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303692 if (!IS_ERR(motg->clk))
3693 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003694 if (!IS_ERR(motg->phy_reset_clk))
3695 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003696free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003697 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303698free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303699 kfree(motg);
3700 return ret;
3701}
3702
3703static int __devexit msm_otg_remove(struct platform_device *pdev)
3704{
3705 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003706 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303707 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303708
3709 if (otg->host || otg->gadget)
3710 return -EBUSY;
3711
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303712 if (pdev->dev.of_node)
3713 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003714 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3715 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303716 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303717 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303718 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303719 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303720 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303721
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303722 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303723
3724 device_init_wakeup(&pdev->dev, 0);
3725 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003726 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303727
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303728 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003729 if (motg->pdata->pmic_id_irq)
3730 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003731 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303732 free_irq(motg->irq, motg);
3733
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303734 /*
3735 * Put PHY in low power mode.
3736 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003737 ulpi_read(otg->phy, 0x14);
3738 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303739
3740 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3741 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3742 if (readl(USB_PORTSC) & PORTSC_PHCD)
3743 break;
3744 udelay(1);
3745 cnt++;
3746 }
3747 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003748 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303749
Manu Gautam28b1bac2012-01-30 16:43:06 +05303750 clk_disable_unprepare(motg->pclk);
3751 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003752 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003753 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303754 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303755 regulator_disable(hsusb_vddcx);
3756 regulator_set_voltage(hsusb_vddcx,
3757 vdd_val[motg->vdd_type][VDD_NONE],
3758 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303759
3760 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303761 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303762
Amit Blay02eff132011-09-21 16:46:24 +03003763 if (!IS_ERR(motg->phy_reset_clk))
3764 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303765 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303766 if (!IS_ERR(motg->clk))
3767 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003768 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303769
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303770 if (motg->bus_perf_client)
3771 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303772
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003773 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303774 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303775 return 0;
3776}
3777
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303778#ifdef CONFIG_PM_RUNTIME
3779static int msm_otg_runtime_idle(struct device *dev)
3780{
3781 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003782 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303783
3784 dev_dbg(dev, "OTG runtime idle\n");
3785
Steve Mucklef132c6c2012-06-06 18:30:57 -07003786 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303787 return -EAGAIN;
3788 else
3789 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303790}
3791
3792static int msm_otg_runtime_suspend(struct device *dev)
3793{
3794 struct msm_otg *motg = dev_get_drvdata(dev);
3795
3796 dev_dbg(dev, "OTG runtime suspend\n");
3797 return msm_otg_suspend(motg);
3798}
3799
3800static int msm_otg_runtime_resume(struct device *dev)
3801{
3802 struct msm_otg *motg = dev_get_drvdata(dev);
3803
3804 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303805 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303806 return msm_otg_resume(motg);
3807}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303808#endif
3809
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303810#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303811static int msm_otg_pm_suspend(struct device *dev)
3812{
Jack Pham5ca279b2012-05-14 18:42:54 -07003813 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303814 struct msm_otg *motg = dev_get_drvdata(dev);
3815
3816 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003817
3818 atomic_set(&motg->pm_suspended, 1);
3819 ret = msm_otg_suspend(motg);
3820 if (ret)
3821 atomic_set(&motg->pm_suspended, 0);
3822
3823 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303824}
3825
3826static int msm_otg_pm_resume(struct device *dev)
3827{
Jack Pham5ca279b2012-05-14 18:42:54 -07003828 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303829 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303830
3831 dev_dbg(dev, "OTG PM resume\n");
3832
Jack Pham5ca279b2012-05-14 18:42:54 -07003833 atomic_set(&motg->pm_suspended, 0);
3834 if (motg->sm_work_pending) {
3835 motg->sm_work_pending = false;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303836
Jack Pham5ca279b2012-05-14 18:42:54 -07003837 pm_runtime_get_noresume(dev);
3838 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303839
Jack Pham5ca279b2012-05-14 18:42:54 -07003840 /* Update runtime PM status */
3841 pm_runtime_disable(dev);
3842 pm_runtime_set_active(dev);
3843 pm_runtime_enable(dev);
3844
3845 queue_work(system_nrt_wq, &motg->sm_work);
3846 }
3847
3848 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303849}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303850#endif
3851
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303852#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303853static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303854 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3855 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3856 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303857};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303858#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303859
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303860static struct of_device_id msm_otg_dt_match[] = {
3861 { .compatible = "qcom,hsusb-otg",
3862 },
3863 {}
3864};
3865
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303866static struct platform_driver msm_otg_driver = {
3867 .remove = __devexit_p(msm_otg_remove),
3868 .driver = {
3869 .name = DRIVER_NAME,
3870 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303871#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303872 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303873#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303874 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303875 },
3876};
3877
3878static int __init msm_otg_init(void)
3879{
3880 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3881}
3882
3883static void __exit msm_otg_exit(void)
3884{
3885 platform_driver_unregister(&msm_otg_driver);
3886}
3887
3888module_init(msm_otg_init);
3889module_exit(msm_otg_exit);
3890
3891MODULE_LICENSE("GPL v2");
3892MODULE_DESCRIPTION("MSM USB transceiver driver");