blob: 1d9c84f4fa9b2ee969a2be36f9730d40f8fbff3a [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 Kondetie0c201f2010-12-07 17:53:55 +053043
44#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080045#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053046#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053047#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053048
49#define MSM_USB_BASE (motg->regs)
50#define DRIVER_NAME "msm_otg"
51
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053052#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053053#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053054#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
55#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
56#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
57#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
58
59#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
60#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
61#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
62#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
63
Mayank Rana248698c2012-04-19 00:03:16 +053064#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080065#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053066#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
67
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053068static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053070static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053071static bool debug_bus_voting_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072
Anji jonnala11aa5c42011-05-04 10:19:48 +053073static struct regulator *hsusb_3p3;
74static struct regulator *hsusb_1p8;
75static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053076static struct regulator *vbus_otg;
Mayank Rana9e9a2ac2012-03-24 04:05:28 +053077static struct regulator *mhl_analog_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053078static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053079
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053080static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053081static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +053082{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053083#ifdef CONFIG_USB_MSM_ACA
84 return true;
85#else
86 return debug_aca_enabled;
87#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +053088}
89
Mayank Rana248698c2012-04-19 00:03:16 +053090static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
91 { /* VDD_CX CORNER Voting */
92 [VDD_NONE] = RPM_VREG_CORNER_NONE,
93 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
94 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
95 },
96 { /* VDD_CX Voltage Voting */
97 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
98 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
99 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
100 },
101};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530102
103static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
104{
105 int rc = 0;
106
107 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700108 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530109 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200110 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530111 return PTR_ERR(hsusb_3p3);
112 }
113
114 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
115 USB_PHY_3P3_VOL_MAX);
116 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700117 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530119 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530120 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700121 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530122 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200123 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530124 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126 }
127 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
128 USB_PHY_1P8_VOL_MAX);
129 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700130 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 goto put_1p8;
133 }
134
135 return 0;
136 }
137
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140put_3p3_lpm:
141 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530142 return rc;
143}
144
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530145static int msm_hsusb_config_vddcx(int high)
146{
Mayank Rana248698c2012-04-19 00:03:16 +0530147 struct msm_otg *motg = the_msm_otg;
148 enum usb_vdd_type vdd_type = motg->vdd_type;
149 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530150 int min_vol;
151 int ret;
152
Mayank Rana248698c2012-04-19 00:03:16 +0530153 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530154 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
155 if (ret) {
156 pr_err("%s: unable to set the voltage for regulator "
157 "HSUSB_VDDCX\n", __func__);
158 return ret;
159 }
160
161 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
162
163 return ret;
164}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530165
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530167{
168 int ret = 0;
169
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530170 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530171 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
172 return -ENODEV;
173 }
174
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530175 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530176 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
177 return -ENODEV;
178 }
179
180 if (on) {
181 ret = regulator_set_optimum_mode(hsusb_1p8,
182 USB_PHY_1P8_HPM_LOAD);
183 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530185 "HSUSB_1p8\n", __func__);
186 return ret;
187 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
189 ret = regulator_enable(hsusb_1p8);
190 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700191 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192 __func__);
193 regulator_set_optimum_mode(hsusb_1p8, 0);
194 return ret;
195 }
196
Anji jonnala11aa5c42011-05-04 10:19:48 +0530197 ret = regulator_set_optimum_mode(hsusb_3p3,
198 USB_PHY_3P3_HPM_LOAD);
199 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530201 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 regulator_set_optimum_mode(hsusb_1p8, 0);
203 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530204 return ret;
205 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206
207 ret = regulator_enable(hsusb_3p3);
208 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700209 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 __func__);
211 regulator_set_optimum_mode(hsusb_3p3, 0);
212 regulator_set_optimum_mode(hsusb_1p8, 0);
213 regulator_disable(hsusb_1p8);
214 return ret;
215 }
216
Anji jonnala11aa5c42011-05-04 10:19:48 +0530217 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 ret = regulator_disable(hsusb_1p8);
219 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700220 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221 __func__);
222 return ret;
223 }
224
225 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530226 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530228 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229
230 ret = regulator_disable(hsusb_3p3);
231 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700232 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 __func__);
234 return ret;
235 }
236 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530237 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530239 "HSUSB_3p3\n", __func__);
240 }
241
242 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
243 return ret < 0 ? ret : 0;
244}
245
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530246static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
247{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530248 struct msm_otg_platform_data *pdata = motg->pdata;
249
250 if (!pdata->mhl_enable)
251 return;
252
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530253 if (!mhl_analog_switch) {
254 pr_err("%s: mhl_analog_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530255 return;
256 }
257
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530258 if (on) {
259 if (regulator_enable(mhl_analog_switch))
260 pr_err("unable to enable mhl_analog_switch\n");
261 } else {
262 regulator_disable(mhl_analog_switch);
263 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530264}
265
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200266static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530267{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200268 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530269 int cnt = 0;
270
271 /* initiate read operation */
272 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
273 USB_ULPI_VIEWPORT);
274
275 /* wait for completion */
276 while (cnt < ULPI_IO_TIMEOUT_USEC) {
277 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
278 break;
279 udelay(1);
280 cnt++;
281 }
282
283 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200284 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530285 readl(USB_ULPI_VIEWPORT));
286 return -ETIMEDOUT;
287 }
288 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
289}
290
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200291static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530292{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200293 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530294 int cnt = 0;
295
296 /* initiate write operation */
297 writel(ULPI_RUN | ULPI_WRITE |
298 ULPI_ADDR(reg) | ULPI_DATA(val),
299 USB_ULPI_VIEWPORT);
300
301 /* wait for completion */
302 while (cnt < ULPI_IO_TIMEOUT_USEC) {
303 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
304 break;
305 udelay(1);
306 cnt++;
307 }
308
309 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200310 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530311 return -ETIMEDOUT;
312 }
313 return 0;
314}
315
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200316static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530317 .read = ulpi_read,
318 .write = ulpi_write,
319};
320
321static void ulpi_init(struct msm_otg *motg)
322{
323 struct msm_otg_platform_data *pdata = motg->pdata;
324 int *seq = pdata->phy_init_seq;
325
326 if (!seq)
327 return;
328
329 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200330 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530331 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200332 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530333 seq += 2;
334 }
335}
336
337static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
338{
339 int ret;
340
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530341 if (IS_ERR(motg->clk))
342 return 0;
343
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530344 if (assert) {
345 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
346 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200347 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530348 } else {
349 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
350 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200351 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530352 }
353 return ret;
354}
355
356static int msm_otg_phy_clk_reset(struct msm_otg *motg)
357{
358 int ret;
359
Amit Blay02eff132011-09-21 16:46:24 +0300360 if (IS_ERR(motg->phy_reset_clk))
361 return 0;
362
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530363 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
364 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200365 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530366 return ret;
367 }
368 usleep_range(10000, 12000);
369 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
370 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200371 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530372 return ret;
373}
374
375static int msm_otg_phy_reset(struct msm_otg *motg)
376{
377 u32 val;
378 int ret;
379 int retries;
380
381 ret = msm_otg_link_clk_reset(motg, 1);
382 if (ret)
383 return ret;
384 ret = msm_otg_phy_clk_reset(motg);
385 if (ret)
386 return ret;
387 ret = msm_otg_link_clk_reset(motg, 0);
388 if (ret)
389 return ret;
390
391 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
392 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
393
394 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200395 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530396 ULPI_CLR(ULPI_FUNC_CTRL));
397 if (!ret)
398 break;
399 ret = msm_otg_phy_clk_reset(motg);
400 if (ret)
401 return ret;
402 }
403 if (!retries)
404 return -ETIMEDOUT;
405
406 /* This reset calibrates the phy, if the above write succeeded */
407 ret = msm_otg_phy_clk_reset(motg);
408 if (ret)
409 return ret;
410
411 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200412 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530413 if (ret != -ETIMEDOUT)
414 break;
415 ret = msm_otg_phy_clk_reset(motg);
416 if (ret)
417 return ret;
418 }
419 if (!retries)
420 return -ETIMEDOUT;
421
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200422 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530423 return 0;
424}
425
426#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530427static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530428{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530429 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530430
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530431 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530433 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530434 break;
435 udelay(1);
436 cnt++;
437 }
438 if (cnt >= LINK_RESET_TIMEOUT_USEC)
439 return -ETIMEDOUT;
440
441 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530442 writel_relaxed(0x80000000, USB_PORTSC);
443 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530444 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530445
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530446 return 0;
447}
448
Steve Mucklef132c6c2012-06-06 18:30:57 -0700449static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530450{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700451 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530452 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530453 int ret;
454 u32 val = 0;
455 u32 ulpi_val = 0;
456
Ofir Cohen4da266f2012-01-03 10:19:29 +0200457 /*
458 * USB PHY and Link reset also reset the USB BAM.
459 * Thus perform reset operation only once to avoid
460 * USB BAM reset on other cases e.g. USB cable disconnections.
461 */
462 if (pdata->disable_reset_on_disconnect) {
463 if (motg->reset_counter)
464 return 0;
465 else
466 motg->reset_counter++;
467 }
468
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530469 if (!IS_ERR(motg->clk))
470 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530471 ret = msm_otg_phy_reset(motg);
472 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700473 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530474 return ret;
475 }
476
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530477 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530478 ret = msm_otg_link_reset(motg);
479 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700480 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530481 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530482 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530483 msleep(100);
484
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530485 ulpi_init(motg);
486
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 /* Ensure that RESET operation is completed before turning off clock */
488 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530489
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530490 if (!IS_ERR(motg->clk))
491 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530492
493 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530494 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530495 if (pdata->mode == USB_OTG) {
496 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
497 val |= OTGSC_IDIE | OTGSC_BSVIE;
498 } else if (pdata->mode == USB_PERIPHERAL) {
499 ulpi_val = ULPI_INT_SESS_VALID;
500 val |= OTGSC_BSVIE;
501 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530502 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200503 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
504 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530505 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700506 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530507 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530508 /* Enable PMIC pull-up */
509 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530510 }
511
512 return 0;
513}
514
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530515static const char *timer_string(int bit)
516{
517 switch (bit) {
518 case A_WAIT_VRISE: return "a_wait_vrise";
519 case A_WAIT_VFALL: return "a_wait_vfall";
520 case B_SRP_FAIL: return "b_srp_fail";
521 case A_WAIT_BCON: return "a_wait_bcon";
522 case A_AIDL_BDIS: return "a_aidl_bdis";
523 case A_BIDL_ADIS: return "a_bidl_adis";
524 case B_ASE0_BRST: return "b_ase0_brst";
525 case A_TST_MAINT: return "a_tst_maint";
526 case B_TST_SRP: return "b_tst_srp";
527 case B_TST_CONFIG: return "b_tst_config";
528 default: return "UNDEFINED";
529 }
530}
531
532static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
533{
534 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
535
536 switch (motg->active_tmout) {
537 case A_WAIT_VRISE:
538 /* TODO: use vbus_vld interrupt */
539 set_bit(A_VBUS_VLD, &motg->inputs);
540 break;
541 case A_TST_MAINT:
542 /* OTG PET: End session after TA_TST_MAINT */
543 set_bit(A_BUS_DROP, &motg->inputs);
544 break;
545 case B_TST_SRP:
546 /*
547 * OTG PET: Initiate SRP after TB_TST_SRP of
548 * previous session end.
549 */
550 set_bit(B_BUS_REQ, &motg->inputs);
551 break;
552 case B_TST_CONFIG:
553 clear_bit(A_CONN, &motg->inputs);
554 break;
555 default:
556 set_bit(motg->active_tmout, &motg->tmouts);
557 }
558
559 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
560 queue_work(system_nrt_wq, &motg->sm_work);
561 return HRTIMER_NORESTART;
562}
563
564static void msm_otg_del_timer(struct msm_otg *motg)
565{
566 int bit = motg->active_tmout;
567
568 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
569 div_s64(ktime_to_us(hrtimer_get_remaining(
570 &motg->timer)), 1000));
571 hrtimer_cancel(&motg->timer);
572 clear_bit(bit, &motg->tmouts);
573}
574
575static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
576{
577 clear_bit(bit, &motg->tmouts);
578 motg->active_tmout = bit;
579 pr_debug("starting %s timer\n", timer_string(bit));
580 hrtimer_start(&motg->timer,
581 ktime_set(time / 1000, (time % 1000) * 1000000),
582 HRTIMER_MODE_REL);
583}
584
585static void msm_otg_init_timer(struct msm_otg *motg)
586{
587 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
588 motg->timer.function = msm_otg_timer_func;
589}
590
Steve Mucklef132c6c2012-06-06 18:30:57 -0700591static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530592{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700593 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530594
Steve Mucklef132c6c2012-06-06 18:30:57 -0700595 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530596 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700597 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530598 return -EINVAL;
599 }
600
601 pr_debug("A-Host: HNP initiated\n");
602 clear_bit(A_BUS_REQ, &motg->inputs);
603 queue_work(system_nrt_wq, &motg->sm_work);
604 return 0;
605}
606
Steve Mucklef132c6c2012-06-06 18:30:57 -0700607static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530608{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700609 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530610 u32 val;
611 int ret = 0;
612
Steve Mucklef132c6c2012-06-06 18:30:57 -0700613 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530614 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700615 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530616 ret = -EINVAL;
617 goto out;
618 }
619
620 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
621 pr_debug("initial conditions of SRP are not met. Try again"
622 "after some time\n");
623 ret = -EAGAIN;
624 goto out;
625 }
626
627 pr_debug("B-Device SRP started\n");
628
629 /*
630 * PHY won't pull D+ high unless it detects Vbus valid.
631 * Since by definition, SRP is only done when Vbus is not valid,
632 * software work-around needs to be used to spoof the PHY into
633 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
634 * VBUSVLDEXT register bits.
635 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700636 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530637 /*
638 * Harware auto assist data pulsing: Data pulse is given
639 * for 7msec; wait for vbus
640 */
641 val = readl_relaxed(USB_OTGSC);
642 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
643
644 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
645out:
646 return ret;
647}
648
Steve Mucklef132c6c2012-06-06 18:30:57 -0700649static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530650{
651 struct usb_hcd *hcd = bus_to_hcd(otg->host);
652 struct usb_device *rhub = otg->host->root_hub;
653
654 if (enable) {
655 pm_runtime_disable(&rhub->dev);
656 rhub->state = USB_STATE_NOTATTACHED;
657 hcd->driver->bus_suspend(hcd);
658 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
659 } else {
660 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700661 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530662 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
663 }
664}
665
Steve Mucklef132c6c2012-06-06 18:30:57 -0700666static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530667{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700668 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530669
Amit Blay6fa647a2012-05-24 14:12:08 +0300670 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530671 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530672
Jack Pham69e621d2012-06-25 18:48:07 -0700673 if (atomic_read(&motg->in_lpm) == suspend)
674 return 0;
675
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530676 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700677 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530678 case OTG_STATE_A_WAIT_BCON:
679 if (TA_WAIT_BCON > 0)
680 break;
681 /* fall through */
682 case OTG_STATE_A_HOST:
683 pr_debug("host bus suspend\n");
684 clear_bit(A_BUS_REQ, &motg->inputs);
685 queue_work(system_nrt_wq, &motg->sm_work);
686 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300687 case OTG_STATE_B_PERIPHERAL:
688 pr_debug("peripheral bus suspend\n");
689 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
690 break;
691 set_bit(A_BUS_SUSPEND, &motg->inputs);
692 queue_work(system_nrt_wq, &motg->sm_work);
693 break;
694
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530695 default:
696 break;
697 }
698 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700699 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530700 case OTG_STATE_A_SUSPEND:
701 /* Remote wakeup or resume */
702 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700703 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700704
705 /* ensure hardware is not in low power mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700706 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530707 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300708 case OTG_STATE_B_PERIPHERAL:
709 pr_debug("peripheral bus resume\n");
710 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
711 break;
712 clear_bit(A_BUS_SUSPEND, &motg->inputs);
713 queue_work(system_nrt_wq, &motg->sm_work);
714 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530715 default:
716 break;
717 }
718 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530719 return 0;
720}
721
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530722#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530723#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
724
725#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530726static int msm_otg_suspend(struct msm_otg *motg)
727{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200728 struct usb_phy *phy = &motg->phy;
729 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530730 struct msm_otg_platform_data *pdata = motg->pdata;
731 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300732 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530733 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800734 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530735 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530736
737 if (atomic_read(&motg->in_lpm))
738 return 0;
739
740 disable_irq(motg->irq);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700741 host_bus_suspend = phy->otg->host && !test_bit(ID, &motg->inputs);
742 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300743 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
744 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530745 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530746 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530747 * Chipidea 45-nm PHY suspend sequence:
748 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530749 * Interrupt Latch Register auto-clear feature is not present
750 * in all PHY versions. Latch register is clear on read type.
751 * Clear latch register to avoid spurious wakeup from
752 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530753 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530754 * PHY comparators are disabled when PHY enters into low power
755 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
756 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
757 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530758 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530759 * PLL is not turned off when PHY enters into low power mode (LPM).
760 * Disable PLL for maximum power savings.
761 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530762
763 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200764 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530765 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200766 ulpi_write(phy, 0x01, 0x30);
767 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530768 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530769
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530771 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530772 * PHY may take some time or even fail to enter into low power
773 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
774 * in failure case.
775 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530776 portsc = readl_relaxed(USB_PORTSC);
777 if (!(portsc & PORTSC_PHCD)) {
778 writel_relaxed(portsc | PORTSC_PHCD,
779 USB_PORTSC);
780 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
781 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
782 break;
783 udelay(1);
784 cnt++;
785 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530786 }
787
788 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200789 dev_err(phy->dev, "Unable to suspend PHY\n");
790 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530791 enable_irq(motg->irq);
792 return -ETIMEDOUT;
793 }
794
795 /*
796 * PHY has capability to generate interrupt asynchronously in low
797 * power mode (LPM). This interrupt is level triggered. So USB IRQ
798 * line must be disabled till async interrupt enable bit is cleared
799 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
800 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530801 *
802 * PHY retention mode is disallowed while entering to LPM with wall
803 * charger connected. But PHY is put into suspend mode. Hence
804 * enable asynchronous interrupt to detect charger disconnection when
805 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530806 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530807 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300808 if (host_bus_suspend || device_bus_suspend ||
809 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530810 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
811 else
812 cmd_val |= ULPI_STP_CTRL;
813 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530814
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530815 /*
816 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
817 * PHY retention and collapse can not happen with VDP_SRC enabled.
818 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300819 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
820 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200821 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
822 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
823 /* Enable PHY HV interrupts to wake MPM/Link */
824 phy_ctrl_val |=
825 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530826
Amit Blay58b31472011-11-18 09:39:39 +0200827 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530829 }
830
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 /* Ensure that above operation is completed before turning off clocks */
832 mb();
Amit Blay9b6e58b2012-06-18 13:12:49 +0300833 if (!motg->pdata->core_clk_always_on_workaround) {
834 clk_disable_unprepare(motg->pclk);
835 clk_disable_unprepare(motg->core_clk);
836 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530837
Anji jonnala7da3f262011-12-02 17:22:14 -0800838 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530839 if (!host_bus_suspend) {
840 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
841 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700842 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530843 "TCXO D0 buffer%d\n", __func__, ret);
844 else
845 motg->lpm_flags |= XO_SHUTDOWN;
846 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800847
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530848 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
849 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 msm_hsusb_ldo_enable(motg, 0);
851 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530852 }
853
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530854 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530856 msm_hsusb_mhl_switch_enable(motg, 0);
857 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858
Steve Mucklef132c6c2012-06-06 18:30:57 -0700859 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530860 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861 if (motg->pdata->pmic_id_irq)
862 enable_irq_wake(motg->pdata->pmic_id_irq);
863 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530864 if (bus)
865 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
866
867 atomic_set(&motg->in_lpm, 1);
868 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530870
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200871 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530872
873 return 0;
874}
875
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530876static int msm_otg_resume(struct msm_otg *motg)
877{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200878 struct usb_phy *phy = &motg->phy;
879 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530880 int cnt = 0;
881 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200882 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800883 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530884
885 if (!atomic_read(&motg->in_lpm))
886 return 0;
887
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530889
Anji jonnala7da3f262011-12-02 17:22:14 -0800890 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530891 if (motg->lpm_flags & XO_SHUTDOWN) {
892 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
893 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700894 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530895 "TCXO D0 buffer%d\n", __func__, ret);
896 motg->lpm_flags &= ~XO_SHUTDOWN;
897 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530898
Amit Blay9b6e58b2012-06-18 13:12:49 +0300899 if (!motg->pdata->core_clk_always_on_workaround) {
900 clk_prepare_enable(motg->core_clk);
901 clk_prepare_enable(motg->pclk);
902 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530903
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
905 msm_hsusb_ldo_enable(motg, 1);
906 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
907 }
908
909 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530910 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530911 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200912 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
913 phy_ctrl_val |= PHY_RETEN;
914 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
915 /* Disable PHY HV interrupts */
916 phy_ctrl_val &=
917 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
918 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530920 }
921
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530922 temp = readl(USB_USBCMD);
923 temp &= ~ASYNC_INTR_CTRL;
924 temp &= ~ULPI_STP_CTRL;
925 writel(temp, USB_USBCMD);
926
927 /*
928 * PHY comes out of low power mode (LPM) in case of wakeup
929 * from asynchronous interrupt.
930 */
931 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
932 goto skip_phy_resume;
933
934 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
935 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
936 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
937 break;
938 udelay(1);
939 cnt++;
940 }
941
942 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
943 /*
944 * This is a fatal error. Reset the link and
945 * PHY. USB state can not be restored. Re-insertion
946 * of USB cable is the only way to get USB working.
947 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200948 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530949 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200950 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530951 }
952
953skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700954 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530955 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956 if (motg->pdata->pmic_id_irq)
957 disable_irq_wake(motg->pdata->pmic_id_irq);
958 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530959 if (bus)
960 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
961
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530962 atomic_set(&motg->in_lpm, 0);
963
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964 if (motg->async_int) {
965 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530966 enable_irq(motg->irq);
967 }
968
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200969 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530970
971 return 0;
972}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530973#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530974
David Keitel081a3e22012-04-18 12:37:07 -0700975static int msm_otg_notify_chg_type(struct msm_otg *motg)
976{
977 static int charger_type;
978 /*
979 * TODO
980 * Unify OTG driver charger types and power supply charger types
981 */
982 if (charger_type == motg->chg_type)
983 return 0;
984
985 if (motg->chg_type == USB_SDP_CHARGER)
986 charger_type = POWER_SUPPLY_TYPE_USB;
987 else if (motg->chg_type == USB_CDP_CHARGER)
988 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
989 else if (motg->chg_type == USB_DCP_CHARGER)
990 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
991 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
992 motg->chg_type == USB_ACA_A_CHARGER ||
993 motg->chg_type == USB_ACA_B_CHARGER ||
994 motg->chg_type == USB_ACA_C_CHARGER))
995 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
996 else
997 charger_type = POWER_SUPPLY_TYPE_BATTERY;
998
999 return pm8921_set_usb_power_supply_type(charger_type);
1000}
1001
Amit Blay0f7edf72012-01-15 10:11:27 +02001002static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1003{
Amit Blay0f7edf72012-01-15 10:11:27 +02001004
Amit Blay0f7edf72012-01-15 10:11:27 +02001005 if (!psy)
1006 goto psy_not_supported;
1007
1008 if (motg->cur_power == 0 && mA > 0) {
1009 /* Enable charging */
1010 if (power_supply_set_online(psy, true))
1011 goto psy_not_supported;
1012 } else if (motg->cur_power > 0 && mA == 0) {
1013 /* Disable charging */
1014 if (power_supply_set_online(psy, false))
1015 goto psy_not_supported;
1016 return 0;
1017 }
1018 /* Set max current limit */
1019 if (power_supply_set_current_limit(psy, 1000*mA))
1020 goto psy_not_supported;
1021
1022 return 0;
1023
1024psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001025 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001026 return -ENXIO;
1027}
1028
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301029static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1030{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001031 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301032
1033 if (g && g->is_a_peripheral)
1034 return;
1035
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301036 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1037 motg->chg_type == USB_ACA_A_CHARGER ||
1038 motg->chg_type == USB_ACA_B_CHARGER ||
1039 motg->chg_type == USB_ACA_C_CHARGER) &&
1040 mA > IDEV_ACA_CHG_LIMIT)
1041 mA = IDEV_ACA_CHG_LIMIT;
1042
David Keitel081a3e22012-04-18 12:37:07 -07001043 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001044 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001045 "Failed notifying %d charger type to PMIC\n",
1046 motg->chg_type);
1047
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301048 if (motg->cur_power == mA)
1049 return;
1050
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001051 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001052
1053 /*
1054 * Use Power Supply API if supported, otherwise fallback
1055 * to legacy pm8921 API.
1056 */
1057 if (msm_otg_notify_power_supply(motg, mA))
1058 pm8921_charger_vbus_draw(mA);
1059
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301060 motg->cur_power = mA;
1061}
1062
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001063static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301064{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001065 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301066
1067 /*
1068 * Gadget driver uses set_power method to notify about the
1069 * available current based on suspend/configured states.
1070 *
1071 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1072 * states when CDP/ACA is connected.
1073 */
1074 if (motg->chg_type == USB_SDP_CHARGER)
1075 msm_otg_notify_charger(motg, mA);
1076
1077 return 0;
1078}
1079
Steve Mucklef132c6c2012-06-06 18:30:57 -07001080static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301081{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001082 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301083 struct msm_otg_platform_data *pdata = motg->pdata;
1084 struct usb_hcd *hcd;
1085
1086 if (!otg->host)
1087 return;
1088
1089 hcd = bus_to_hcd(otg->host);
1090
1091 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001092 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301093
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301094 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001095 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301096 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1097
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301098 /*
1099 * Some boards have a switch cotrolled by gpio
1100 * to enable/disable internal HUB. Enable internal
1101 * HUB before kicking the host.
1102 */
1103 if (pdata->setup_gpio)
1104 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301105 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301106 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001107 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301108
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301109 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301110 /* HCD core reset all bits of PORTSC. select ULPI phy */
1111 writel_relaxed(0x80000000, USB_PORTSC);
1112
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301113 if (pdata->setup_gpio)
1114 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301115
1116 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001117 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301118 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301119 }
1120}
1121
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001122static int msm_otg_usbdev_notify(struct notifier_block *self,
1123 unsigned long action, void *priv)
1124{
1125 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001126 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301127 struct usb_device *udev = priv;
1128
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301129 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1130 goto out;
1131
Steve Mucklef132c6c2012-06-06 18:30:57 -07001132 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301133 goto out;
1134 /*
1135 * Interested in devices connected directly to the root hub.
1136 * ACA dock can supply IDEV_CHG irrespective devices connected
1137 * on the accessory port.
1138 */
1139 if (!udev->parent || udev->parent->parent ||
1140 motg->chg_type == USB_ACA_DOCK_CHARGER)
1141 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142
1143 switch (action) {
1144 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301145 if (aca_enabled())
1146 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001147 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301148 pr_debug("B_CONN set\n");
1149 set_bit(B_CONN, &motg->inputs);
1150 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001151 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301152 /*
1153 * OTG PET: A-device must end session within
1154 * 10 sec after PET enumeration.
1155 */
1156 if (udev->quirks & USB_QUIRK_OTG_PET)
1157 msm_otg_start_timer(motg, TA_TST_MAINT,
1158 A_TST_MAINT);
1159 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301160 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162 if (udev->actconfig)
1163 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1164 else
1165 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001166 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301167 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301168 break;
1169 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001170 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1171 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301172 pr_debug("B_CONN clear\n");
1173 clear_bit(B_CONN, &motg->inputs);
1174 /*
1175 * OTG PET: A-device must end session after
1176 * PET disconnection if it is enumerated
1177 * with bcdDevice[0] = 1. USB core sets
1178 * bus->otg_vbus_off for us. clear it here.
1179 */
1180 if (udev->bus->otg_vbus_off) {
1181 udev->bus->otg_vbus_off = 0;
1182 set_bit(A_BUS_DROP, &motg->inputs);
1183 }
1184 queue_work(system_nrt_wq, &motg->sm_work);
1185 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 default:
1187 break;
1188 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301189 if (test_bit(ID_A, &motg->inputs))
1190 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1191 motg->mA_port);
1192out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001193 return NOTIFY_OK;
1194}
1195
Mayank Ranae3926882011-12-26 09:47:54 +05301196static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1197{
1198 int ret;
1199 static bool vbus_is_on;
1200
1201 if (vbus_is_on == on)
1202 return;
1203
1204 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301205 ret = motg->pdata->vbus_power(on);
1206 if (!ret)
1207 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301208 return;
1209 }
1210
1211 if (!vbus_otg) {
1212 pr_err("vbus_otg is NULL.");
1213 return;
1214 }
1215
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001216 /*
1217 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001218 * from usb before turning on the boost.
1219 * if exiting host mode disable the boost before enabling to draw
1220 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001221 */
Mayank Ranae3926882011-12-26 09:47:54 +05301222 if (on) {
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001223 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301224 ret = regulator_enable(vbus_otg);
1225 if (ret) {
1226 pr_err("unable to enable vbus_otg\n");
1227 return;
1228 }
1229 vbus_is_on = true;
1230 } else {
1231 ret = regulator_disable(vbus_otg);
1232 if (ret) {
1233 pr_err("unable to disable vbus_otg\n");
1234 return;
1235 }
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001236 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301237 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301238 }
1239}
1240
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001241static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301242{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001243 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301244 struct usb_hcd *hcd;
1245
1246 /*
1247 * Fail host registration if this board can support
1248 * only peripheral configuration.
1249 */
1250 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001251 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301252 return -ENODEV;
1253 }
1254
Mayank Ranae3926882011-12-26 09:47:54 +05301255 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001256 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301257 if (IS_ERR(vbus_otg)) {
1258 pr_err("Unable to get vbus_otg\n");
1259 return -ENODEV;
1260 }
1261 }
1262
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301263 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001264 if (otg->phy->state == OTG_STATE_A_HOST) {
1265 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001266 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301267 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301268 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301269 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001270 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301271 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301272 } else {
1273 otg->host = NULL;
1274 }
1275
1276 return 0;
1277 }
1278
1279 hcd = bus_to_hcd(host);
1280 hcd->power_budget = motg->pdata->power_budget;
1281
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301282#ifdef CONFIG_USB_OTG
1283 host->otg_port = 1;
1284#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001285 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1286 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301287 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001288 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301289
1290 /*
1291 * Kick the state machine work, if peripheral is not supported
1292 * or peripheral is already registered with us.
1293 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301294 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001295 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301296 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301297 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301298
1299 return 0;
1300}
1301
Steve Mucklef132c6c2012-06-06 18:30:57 -07001302static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301303{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301304 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001305 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301306 struct msm_otg_platform_data *pdata = motg->pdata;
1307
1308 if (!otg->gadget)
1309 return;
1310
1311 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001312 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301313 /*
1314 * Some boards have a switch cotrolled by gpio
1315 * to enable/disable internal HUB. Disable internal
1316 * HUB before kicking the gadget.
1317 */
1318 if (pdata->setup_gpio)
1319 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001320
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301321 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301322 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301323 ret = msm_bus_scale_client_update_request(
1324 motg->bus_perf_client, 1);
1325 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001326 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301327 "bus bandwidth %d\n", __func__, ret);
1328 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301329 usb_gadget_vbus_connect(otg->gadget);
1330 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001331 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301332 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301333 /* Configure BUS performance parameters to default */
1334 if (motg->bus_perf_client) {
1335 ret = msm_bus_scale_client_update_request(
1336 motg->bus_perf_client, 0);
1337 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001338 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301339 "for bus bw %d\n", __func__, ret);
1340 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301341 if (pdata->setup_gpio)
1342 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1343 }
1344
1345}
1346
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001347static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301348 struct usb_gadget *gadget)
1349{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001350 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301351
1352 /*
1353 * Fail peripheral registration if this board can support
1354 * only host configuration.
1355 */
1356 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001357 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301358 return -ENODEV;
1359 }
1360
1361 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001362 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1363 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301364 msm_otg_start_peripheral(otg, 0);
1365 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001366 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301367 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301368 } else {
1369 otg->gadget = NULL;
1370 }
1371
1372 return 0;
1373 }
1374 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001375 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301376
1377 /*
1378 * Kick the state machine work, if host is not supported
1379 * or host is already registered with us.
1380 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301381 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001382 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301383 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301384 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301385
1386 return 0;
1387}
1388
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389static bool msm_chg_aca_detect(struct msm_otg *motg)
1390{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001391 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001392 u32 int_sts;
1393 bool ret = false;
1394
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301395 if (!aca_enabled())
1396 goto out;
1397
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001398 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1399 goto out;
1400
Steve Mucklef132c6c2012-06-06 18:30:57 -07001401 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001402 switch (int_sts & 0x1C) {
1403 case 0x08:
1404 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001405 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001406 motg->chg_type = USB_ACA_A_CHARGER;
1407 motg->chg_state = USB_CHG_STATE_DETECTED;
1408 clear_bit(ID_B, &motg->inputs);
1409 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301410 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001411 ret = true;
1412 }
1413 break;
1414 case 0x0C:
1415 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001416 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417 motg->chg_type = USB_ACA_B_CHARGER;
1418 motg->chg_state = USB_CHG_STATE_DETECTED;
1419 clear_bit(ID_A, &motg->inputs);
1420 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301421 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001422 ret = true;
1423 }
1424 break;
1425 case 0x10:
1426 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001427 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001428 motg->chg_type = USB_ACA_C_CHARGER;
1429 motg->chg_state = USB_CHG_STATE_DETECTED;
1430 clear_bit(ID_A, &motg->inputs);
1431 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301432 set_bit(ID, &motg->inputs);
1433 ret = true;
1434 }
1435 break;
1436 case 0x04:
1437 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001438 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301439 motg->chg_type = USB_INVALID_CHARGER;
1440 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1441 clear_bit(ID_A, &motg->inputs);
1442 clear_bit(ID_B, &motg->inputs);
1443 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001444 ret = true;
1445 }
1446 break;
1447 default:
1448 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1449 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301450 test_and_clear_bit(ID_C, &motg->inputs) |
1451 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001452 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001453 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001454 motg->chg_type = USB_INVALID_CHARGER;
1455 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1456 }
1457 }
1458out:
1459 return ret;
1460}
1461
1462static void msm_chg_enable_aca_det(struct msm_otg *motg)
1463{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001464 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001465
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301466 if (!aca_enabled())
1467 return;
1468
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001469 switch (motg->pdata->phy_type) {
1470 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301471 /* Disable ID_GND in link and PHY */
1472 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1473 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001474 ulpi_write(phy, 0x01, 0x0C);
1475 ulpi_write(phy, 0x10, 0x0F);
1476 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301477 /* Disable PMIC ID pull-up */
1478 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301479 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001480 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301481 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001482 break;
1483 default:
1484 break;
1485 }
1486}
1487
1488static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1489{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001490 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301492 if (!aca_enabled())
1493 return;
1494
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001495 switch (motg->pdata->phy_type) {
1496 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301497 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001498 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301499 break;
1500 default:
1501 break;
1502 }
1503}
1504
1505static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1506{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001507 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301508
1509 if (!aca_enabled())
1510 return;
1511
1512 switch (motg->pdata->phy_type) {
1513 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001514 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515 break;
1516 default:
1517 break;
1518 }
1519}
1520
1521static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1522{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001523 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001524 bool ret = false;
1525
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301526 if (!aca_enabled())
1527 return ret;
1528
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001529 switch (motg->pdata->phy_type) {
1530 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001531 if (ulpi_read(phy, 0x91) & 1) {
1532 dev_dbg(phy->dev, "RID change\n");
1533 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001534 ret = msm_chg_aca_detect(motg);
1535 }
1536 default:
1537 break;
1538 }
1539 return ret;
1540}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301541
1542static void msm_otg_id_timer_func(unsigned long data)
1543{
1544 struct msm_otg *motg = (struct msm_otg *) data;
1545
1546 if (!aca_enabled())
1547 return;
1548
1549 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001550 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301551 return;
1552 }
1553
Steve Mucklef132c6c2012-06-06 18:30:57 -07001554 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301555 goto out;
1556
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301557 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001558 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301559 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301560 }
1561
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301562out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301563 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1564 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1565}
1566
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301567static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1568{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001569 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301570 u32 chg_det;
1571 bool ret = false;
1572
1573 switch (motg->pdata->phy_type) {
1574 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001575 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301576 ret = chg_det & (1 << 4);
1577 break;
1578 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001579 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301580 ret = chg_det & 1;
1581 break;
1582 default:
1583 break;
1584 }
1585 return ret;
1586}
1587
1588static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1589{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001590 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301591 u32 chg_det;
1592
1593 switch (motg->pdata->phy_type) {
1594 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001595 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301596 /* Turn off charger block */
1597 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001598 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301599 udelay(20);
1600 /* control chg block via ULPI */
1601 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001602 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301603 /* put it in host mode for enabling D- source */
1604 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001605 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301606 /* Turn on chg detect block */
1607 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001608 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301609 udelay(20);
1610 /* enable chg detection */
1611 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001612 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301613 break;
1614 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301615 /* Turn off VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001616 ulpi_write(phy, 0x3, 0x86);
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301617 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301618 /*
1619 * Configure DM as current source, DP as current sink
1620 * and enable battery charging comparators.
1621 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001622 ulpi_write(phy, 0x8, 0x85);
1623 ulpi_write(phy, 0x2, 0x85);
1624 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301625 break;
1626 default:
1627 break;
1628 }
1629}
1630
1631static bool msm_chg_check_primary_det(struct msm_otg *motg)
1632{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001633 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301634 u32 chg_det;
1635 bool ret = false;
1636
1637 switch (motg->pdata->phy_type) {
1638 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001639 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301640 ret = chg_det & (1 << 4);
1641 break;
1642 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001643 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301644 ret = chg_det & 1;
1645 break;
1646 default:
1647 break;
1648 }
1649 return ret;
1650}
1651
1652static void msm_chg_enable_primary_det(struct msm_otg *motg)
1653{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001654 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301655 u32 chg_det;
1656
1657 switch (motg->pdata->phy_type) {
1658 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001659 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301660 /* enable chg detection */
1661 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001662 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301663 break;
1664 case SNPS_28NM_INTEGRATED_PHY:
1665 /*
1666 * Configure DP as current source, DM as current sink
1667 * and enable battery charging comparators.
1668 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001669 ulpi_write(phy, 0x2, 0x85);
1670 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301671 break;
1672 default:
1673 break;
1674 }
1675}
1676
1677static bool msm_chg_check_dcd(struct msm_otg *motg)
1678{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001679 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301680 u32 line_state;
1681 bool ret = false;
1682
1683 switch (motg->pdata->phy_type) {
1684 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001685 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301686 ret = !(line_state & 1);
1687 break;
1688 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001689 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301690 ret = line_state & 2;
1691 break;
1692 default:
1693 break;
1694 }
1695 return ret;
1696}
1697
1698static void msm_chg_disable_dcd(struct msm_otg *motg)
1699{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001700 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301701 u32 chg_det;
1702
1703 switch (motg->pdata->phy_type) {
1704 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001705 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301706 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001707 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301708 break;
1709 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001710 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301711 break;
1712 default:
1713 break;
1714 }
1715}
1716
1717static void msm_chg_enable_dcd(struct msm_otg *motg)
1718{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001719 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301720 u32 chg_det;
1721
1722 switch (motg->pdata->phy_type) {
1723 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001724 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301725 /* Turn on D+ current source */
1726 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001727 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301728 break;
1729 case SNPS_28NM_INTEGRATED_PHY:
1730 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001731 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301732 break;
1733 default:
1734 break;
1735 }
1736}
1737
1738static void msm_chg_block_on(struct msm_otg *motg)
1739{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001740 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301741 u32 func_ctrl, chg_det;
1742
1743 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001744 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301745 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1746 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001747 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301748
1749 switch (motg->pdata->phy_type) {
1750 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001751 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301752 /* control chg block via ULPI */
1753 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001754 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301755 /* Turn on chg detect block */
1756 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001757 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301758 udelay(20);
1759 break;
1760 case SNPS_28NM_INTEGRATED_PHY:
1761 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001762 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301763 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001764 ulpi_write(phy, 0x1F, 0x92);
1765 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301766 udelay(100);
1767 break;
1768 default:
1769 break;
1770 }
1771}
1772
1773static void msm_chg_block_off(struct msm_otg *motg)
1774{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001775 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301776 u32 func_ctrl, chg_det;
1777
1778 switch (motg->pdata->phy_type) {
1779 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001780 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301781 /* Turn off charger block */
1782 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001783 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301784 break;
1785 case SNPS_28NM_INTEGRATED_PHY:
1786 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001787 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301788 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001789 ulpi_write(phy, 0x1F, 0x92);
1790 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301791 break;
1792 default:
1793 break;
1794 }
1795
1796 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001797 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301798 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1799 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001800 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301801}
1802
Anji jonnalad270e2d2011-08-09 11:28:32 +05301803static const char *chg_to_string(enum usb_chg_type chg_type)
1804{
1805 switch (chg_type) {
1806 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1807 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1808 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1809 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1810 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1811 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1812 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1813 default: return "INVALID_CHARGER";
1814 }
1815}
1816
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301817#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1818#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301819#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1820#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301821static void msm_chg_detect_work(struct work_struct *w)
1822{
1823 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001824 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301825 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301826 unsigned long delay;
1827
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001828 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301829 switch (motg->chg_state) {
1830 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301831 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301832 if (motg->pdata->enable_dcd)
1833 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001834 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301835 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1836 motg->dcd_retries = 0;
1837 delay = MSM_CHG_DCD_POLL_TIME;
1838 break;
1839 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001840 is_aca = msm_chg_aca_detect(motg);
1841 if (is_aca) {
1842 /*
1843 * ID_A can be ACA dock too. continue
1844 * primary detection after DCD.
1845 */
1846 if (test_bit(ID_A, &motg->inputs)) {
1847 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1848 } else {
1849 delay = 0;
1850 break;
1851 }
1852 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301853 if (motg->pdata->enable_dcd)
1854 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301855 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1856 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301857 if (motg->pdata->enable_dcd)
1858 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301859 msm_chg_enable_primary_det(motg);
1860 delay = MSM_CHG_PRIMARY_DET_TIME;
1861 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1862 } else {
1863 delay = MSM_CHG_DCD_POLL_TIME;
1864 }
1865 break;
1866 case USB_CHG_STATE_DCD_DONE:
1867 vout = msm_chg_check_primary_det(motg);
1868 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301869 if (test_bit(ID_A, &motg->inputs)) {
1870 motg->chg_type = USB_ACA_DOCK_CHARGER;
1871 motg->chg_state = USB_CHG_STATE_DETECTED;
1872 delay = 0;
1873 break;
1874 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301875 msm_chg_enable_secondary_det(motg);
1876 delay = MSM_CHG_SECONDARY_DET_TIME;
1877 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1878 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301879 if (test_bit(ID_A, &motg->inputs)) {
1880 motg->chg_type = USB_ACA_A_CHARGER;
1881 motg->chg_state = USB_CHG_STATE_DETECTED;
1882 delay = 0;
1883 break;
1884 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301885 motg->chg_type = USB_SDP_CHARGER;
1886 motg->chg_state = USB_CHG_STATE_DETECTED;
1887 delay = 0;
1888 }
1889 break;
1890 case USB_CHG_STATE_PRIMARY_DONE:
1891 vout = msm_chg_check_secondary_det(motg);
1892 if (vout)
1893 motg->chg_type = USB_DCP_CHARGER;
1894 else
1895 motg->chg_type = USB_CDP_CHARGER;
1896 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1897 /* fall through */
1898 case USB_CHG_STATE_SECONDARY_DONE:
1899 motg->chg_state = USB_CHG_STATE_DETECTED;
1900 case USB_CHG_STATE_DETECTED:
1901 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001902 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301903 /*
1904 * Spurious interrupt is seen after enabling ACA detection
1905 * due to which charger detection fails in case of PET.
1906 * Add delay of 100 microsec to avoid that.
1907 */
1908 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001909 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001910 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05301911 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301912 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301913 return;
1914 default:
1915 return;
1916 }
1917
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301918 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301919}
1920
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301921/*
1922 * We support OTG, Peripheral only and Host only configurations. In case
1923 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1924 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1925 * enabled when switch is controlled by user and default mode is supplied
1926 * by board file, which can be changed by userspace later.
1927 */
1928static void msm_otg_init_sm(struct msm_otg *motg)
1929{
1930 struct msm_otg_platform_data *pdata = motg->pdata;
1931 u32 otgsc = readl(USB_OTGSC);
1932
1933 switch (pdata->mode) {
1934 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001935 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301936 if (pdata->default_mode == USB_HOST) {
1937 clear_bit(ID, &motg->inputs);
1938 } else if (pdata->default_mode == USB_PERIPHERAL) {
1939 set_bit(ID, &motg->inputs);
1940 set_bit(B_SESS_VLD, &motg->inputs);
1941 } else {
1942 set_bit(ID, &motg->inputs);
1943 clear_bit(B_SESS_VLD, &motg->inputs);
1944 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301945 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301946 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301947 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301948 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301949 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301950 set_bit(A_BUS_REQ, &motg->inputs);
1951 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952 if (otgsc & OTGSC_BSV)
1953 set_bit(B_SESS_VLD, &motg->inputs);
1954 else
1955 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301956 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301957 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07001958 unsigned long flags;
1959 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301960 if (irq_read_line(pdata->pmic_id_irq))
1961 set_bit(ID, &motg->inputs);
1962 else
1963 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07001964 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301965 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301966 /*
1967 * VBUS initial state is reported after PMIC
1968 * driver initialization. Wait for it.
1969 */
1970 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301971 }
1972 break;
1973 case USB_HOST:
1974 clear_bit(ID, &motg->inputs);
1975 break;
1976 case USB_PERIPHERAL:
1977 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301978 if (pdata->otg_control == OTG_PHY_CONTROL) {
1979 if (otgsc & OTGSC_BSV)
1980 set_bit(B_SESS_VLD, &motg->inputs);
1981 else
1982 clear_bit(B_SESS_VLD, &motg->inputs);
1983 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1984 /*
1985 * VBUS initial state is reported after PMIC
1986 * driver initialization. Wait for it.
1987 */
1988 wait_for_completion(&pmic_vbus_init);
1989 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301990 break;
1991 default:
1992 break;
1993 }
1994}
1995
1996static void msm_otg_sm_work(struct work_struct *w)
1997{
1998 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001999 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302000 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302001
Steve Mucklef132c6c2012-06-06 18:30:57 -07002002 pm_runtime_resume(otg->phy->dev);
2003 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002004 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302005 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002006 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302007 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302008 psy = power_supply_get_by_name("usb");
2009 if (!psy)
2010 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002011 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302012 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2013 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002014 pm_runtime_put_noidle(otg->phy->dev);
2015 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302016 break;
2017 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302018 /* FALL THROUGH */
2019 case OTG_STATE_B_IDLE:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002020 if ((!test_bit(ID, &motg->inputs) ||
2021 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302022 pr_debug("!id || id_A\n");
2023 clear_bit(B_BUS_REQ, &motg->inputs);
2024 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002025 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302026 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302027 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302028 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302029 switch (motg->chg_state) {
2030 case USB_CHG_STATE_UNDEFINED:
2031 msm_chg_detect_work(&motg->chg_work.work);
2032 break;
2033 case USB_CHG_STATE_DETECTED:
2034 switch (motg->chg_type) {
2035 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302036 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002037 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302038 msm_otg_notify_charger(motg,
2039 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002040 pm_runtime_put_noidle(otg->phy->dev);
2041 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302042 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302043 case USB_ACA_B_CHARGER:
2044 msm_otg_notify_charger(motg,
2045 IDEV_ACA_CHG_MAX);
2046 /*
2047 * (ID_B --> ID_C) PHY_ALT interrupt can
2048 * not be detected in LPM.
2049 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302050 break;
2051 case USB_CDP_CHARGER:
2052 msm_otg_notify_charger(motg,
2053 IDEV_CHG_MAX);
2054 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002055 otg->phy->state =
2056 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302057 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302058 case USB_ACA_C_CHARGER:
2059 msm_otg_notify_charger(motg,
2060 IDEV_ACA_CHG_MAX);
2061 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002062 otg->phy->state =
2063 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302064 break;
2065 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002067 otg->phy->state =
2068 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302069 break;
2070 default:
2071 break;
2072 }
2073 break;
2074 default:
2075 break;
2076 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302077 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2078 pr_debug("b_sess_end && b_bus_req\n");
2079 if (msm_otg_start_srp(otg) < 0) {
2080 clear_bit(B_BUS_REQ, &motg->inputs);
2081 work = 1;
2082 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302083 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002084 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302085 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2086 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302087 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302088 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302089 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302090 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2091 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302092 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002093 msm_otg_reset(otg->phy);
2094 pm_runtime_put_noidle(otg->phy->dev);
2095 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302096 }
2097 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302098 case OTG_STATE_B_SRP_INIT:
2099 if (!test_bit(ID, &motg->inputs) ||
2100 test_bit(ID_A, &motg->inputs) ||
2101 test_bit(ID_C, &motg->inputs) ||
2102 (test_bit(B_SESS_VLD, &motg->inputs) &&
2103 !test_bit(ID_B, &motg->inputs))) {
2104 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2105 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002106 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302107 /*
2108 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2109 * bits after SRP initiation.
2110 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002111 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302112 work = 1;
2113 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2114 pr_debug("b_srp_fail\n");
2115 pr_info("A-device did not respond to SRP\n");
2116 clear_bit(B_BUS_REQ, &motg->inputs);
2117 clear_bit(B_SRP_FAIL, &motg->tmouts);
2118 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002119 ulpi_write(otg->phy, 0x0, 0x98);
2120 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302121 motg->b_last_se0_sess = jiffies;
2122 work = 1;
2123 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302124 break;
2125 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302126 if (!test_bit(ID, &motg->inputs) ||
2127 test_bit(ID_A, &motg->inputs) ||
2128 test_bit(ID_B, &motg->inputs) ||
2129 !test_bit(B_SESS_VLD, &motg->inputs)) {
2130 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302131 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2132 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302133 msm_otg_notify_charger(motg, 0);
2134 srp_reqd = otg->gadget->otg_srp_reqd;
2135 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302136 if (test_bit(ID_B, &motg->inputs))
2137 clear_bit(ID_B, &motg->inputs);
2138 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002139 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302140 motg->b_last_se0_sess = jiffies;
2141 if (srp_reqd)
2142 msm_otg_start_timer(motg,
2143 TB_TST_SRP, B_TST_SRP);
2144 else
2145 work = 1;
2146 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2147 otg->gadget->b_hnp_enable &&
2148 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2149 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2150 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2151 /* D+ pullup should not be disconnected within 4msec
2152 * after A device suspends the bus. Otherwise PET will
2153 * fail the compliance test.
2154 */
2155 udelay(1000);
2156 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002157 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302158 /*
2159 * start HCD even before A-device enable
2160 * pull-up to meet HNP timings.
2161 */
2162 otg->host->is_b_host = 1;
2163 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002164 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2165 test_bit(B_SESS_VLD, &motg->inputs)) {
2166 pr_debug("a_bus_suspend && b_sess_vld\n");
2167 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002168 pm_runtime_put_noidle(otg->phy->dev);
2169 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002170 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002171 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302172 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002173 }
2174 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302175 case OTG_STATE_B_WAIT_ACON:
2176 if (!test_bit(ID, &motg->inputs) ||
2177 test_bit(ID_A, &motg->inputs) ||
2178 test_bit(ID_B, &motg->inputs) ||
2179 !test_bit(B_SESS_VLD, &motg->inputs)) {
2180 pr_debug("!id || id_a/b || !b_sess_vld\n");
2181 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302182 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302183 * A-device is physically disconnected during
2184 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302185 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302186 msm_otg_start_host(otg, 0);
2187 otg->host->is_b_host = 0;
2188
2189 clear_bit(B_BUS_REQ, &motg->inputs);
2190 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2191 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002192 otg->phy->state = OTG_STATE_B_IDLE;
2193 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302194 work = 1;
2195 } else if (test_bit(A_CONN, &motg->inputs)) {
2196 pr_debug("a_conn\n");
2197 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002198 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302199 /*
2200 * PET disconnects D+ pullup after reset is generated
2201 * by B device in B_HOST role which is not detected by
2202 * B device. As workaorund , start timer of 300msec
2203 * and stop timer if A device is enumerated else clear
2204 * A_CONN.
2205 */
2206 msm_otg_start_timer(motg, TB_TST_CONFIG,
2207 B_TST_CONFIG);
2208 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2209 pr_debug("b_ase0_brst_tmout\n");
2210 pr_info("B HNP fail:No response from A device\n");
2211 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002212 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302213 otg->host->is_b_host = 0;
2214 clear_bit(B_ASE0_BRST, &motg->tmouts);
2215 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2216 clear_bit(B_BUS_REQ, &motg->inputs);
2217 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002218 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302219 work = 1;
2220 } else if (test_bit(ID_C, &motg->inputs)) {
2221 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2222 }
2223 break;
2224 case OTG_STATE_B_HOST:
2225 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2226 !test_bit(A_CONN, &motg->inputs) ||
2227 !test_bit(B_SESS_VLD, &motg->inputs)) {
2228 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2229 clear_bit(A_CONN, &motg->inputs);
2230 clear_bit(B_BUS_REQ, &motg->inputs);
2231 msm_otg_start_host(otg, 0);
2232 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002233 otg->phy->state = OTG_STATE_B_IDLE;
2234 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302235 work = 1;
2236 } else if (test_bit(ID_C, &motg->inputs)) {
2237 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2238 }
2239 break;
2240 case OTG_STATE_A_IDLE:
2241 otg->default_a = 1;
2242 if (test_bit(ID, &motg->inputs) &&
2243 !test_bit(ID_A, &motg->inputs)) {
2244 pr_debug("id && !id_a\n");
2245 otg->default_a = 0;
2246 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002247 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302248 del_timer_sync(&motg->id_timer);
2249 msm_otg_link_reset(motg);
2250 msm_chg_enable_aca_intr(motg);
2251 msm_otg_notify_charger(motg, 0);
2252 work = 1;
2253 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2254 (test_bit(A_SRP_DET, &motg->inputs) ||
2255 test_bit(A_BUS_REQ, &motg->inputs))) {
2256 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2257
2258 clear_bit(A_SRP_DET, &motg->inputs);
2259 /* Disable SRP detection */
2260 writel_relaxed((readl_relaxed(USB_OTGSC) &
2261 ~OTGSC_INTSTS_MASK) &
2262 ~OTGSC_DPIE, USB_OTGSC);
2263
Steve Mucklef132c6c2012-06-06 18:30:57 -07002264 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302265 /* VBUS should not be supplied before end of SRP pulse
2266 * generated by PET, if not complaince test fail.
2267 */
2268 usleep_range(10000, 12000);
2269 /* ACA: ID_A: Stop charging untill enumeration */
2270 if (test_bit(ID_A, &motg->inputs))
2271 msm_otg_notify_charger(motg, 0);
2272 else
2273 msm_hsusb_vbus_power(motg, 1);
2274 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2275 } else {
2276 pr_debug("No session requested\n");
2277 clear_bit(A_BUS_DROP, &motg->inputs);
2278 if (test_bit(ID_A, &motg->inputs)) {
2279 msm_otg_notify_charger(motg,
2280 IDEV_ACA_CHG_MAX);
2281 } else if (!test_bit(ID, &motg->inputs)) {
2282 msm_otg_notify_charger(motg, 0);
2283 /*
2284 * A-device is not providing power on VBUS.
2285 * Enable SRP detection.
2286 */
2287 writel_relaxed(0x13, USB_USBMODE);
2288 writel_relaxed((readl_relaxed(USB_OTGSC) &
2289 ~OTGSC_INTSTS_MASK) |
2290 OTGSC_DPIE, USB_OTGSC);
2291 mb();
2292 }
2293 }
2294 break;
2295 case OTG_STATE_A_WAIT_VRISE:
2296 if ((test_bit(ID, &motg->inputs) &&
2297 !test_bit(ID_A, &motg->inputs)) ||
2298 test_bit(A_BUS_DROP, &motg->inputs) ||
2299 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2300 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2301 clear_bit(A_BUS_REQ, &motg->inputs);
2302 msm_otg_del_timer(motg);
2303 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002304 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302305 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2306 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2307 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002308 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302309 if (TA_WAIT_BCON > 0)
2310 msm_otg_start_timer(motg, TA_WAIT_BCON,
2311 A_WAIT_BCON);
2312 msm_otg_start_host(otg, 1);
2313 msm_chg_enable_aca_det(motg);
2314 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302315 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302316 if (msm_chg_check_aca_intr(motg))
2317 work = 1;
2318 }
2319 break;
2320 case OTG_STATE_A_WAIT_BCON:
2321 if ((test_bit(ID, &motg->inputs) &&
2322 !test_bit(ID_A, &motg->inputs)) ||
2323 test_bit(A_BUS_DROP, &motg->inputs) ||
2324 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2325 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2326 "a_wait_bcon_tmout\n");
2327 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2328 pr_info("Device No Response\n");
2329 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2330 }
2331 msm_otg_del_timer(motg);
2332 clear_bit(A_BUS_REQ, &motg->inputs);
2333 clear_bit(B_CONN, &motg->inputs);
2334 msm_otg_start_host(otg, 0);
2335 /*
2336 * ACA: ID_A with NO accessory, just the A plug is
2337 * attached to ACA: Use IDCHG_MAX for charging
2338 */
2339 if (test_bit(ID_A, &motg->inputs))
2340 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2341 else
2342 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002343 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302344 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2345 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2346 pr_debug("!a_vbus_vld\n");
2347 clear_bit(B_CONN, &motg->inputs);
2348 msm_otg_del_timer(motg);
2349 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002350 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2351 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302352 } else if (test_bit(ID_A, &motg->inputs)) {
2353 msm_hsusb_vbus_power(motg, 0);
2354 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2355 /*
2356 * If TA_WAIT_BCON is infinite, we don;t
2357 * turn off VBUS. Enter low power mode.
2358 */
2359 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002360 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302361 } else if (!test_bit(ID, &motg->inputs)) {
2362 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302363 }
2364 break;
2365 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302366 if ((test_bit(ID, &motg->inputs) &&
2367 !test_bit(ID_A, &motg->inputs)) ||
2368 test_bit(A_BUS_DROP, &motg->inputs)) {
2369 pr_debug("id_a/b/c || a_bus_drop\n");
2370 clear_bit(B_CONN, &motg->inputs);
2371 clear_bit(A_BUS_REQ, &motg->inputs);
2372 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002373 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302374 msm_otg_start_host(otg, 0);
2375 if (!test_bit(ID_A, &motg->inputs))
2376 msm_hsusb_vbus_power(motg, 0);
2377 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2378 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2379 pr_debug("!a_vbus_vld\n");
2380 clear_bit(B_CONN, &motg->inputs);
2381 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002382 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302383 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002384 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302385 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2386 /*
2387 * a_bus_req is de-asserted when root hub is
2388 * suspended or HNP is in progress.
2389 */
2390 pr_debug("!a_bus_req\n");
2391 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002392 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302393 if (otg->host->b_hnp_enable)
2394 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2395 A_AIDL_BDIS);
2396 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002397 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302398 } else if (!test_bit(B_CONN, &motg->inputs)) {
2399 pr_debug("!b_conn\n");
2400 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002401 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302402 if (TA_WAIT_BCON > 0)
2403 msm_otg_start_timer(motg, TA_WAIT_BCON,
2404 A_WAIT_BCON);
2405 if (msm_chg_check_aca_intr(motg))
2406 work = 1;
2407 } else if (test_bit(ID_A, &motg->inputs)) {
2408 msm_otg_del_timer(motg);
2409 msm_hsusb_vbus_power(motg, 0);
2410 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2411 msm_otg_notify_charger(motg,
2412 IDEV_ACA_CHG_MAX);
2413 else
2414 msm_otg_notify_charger(motg,
2415 IDEV_CHG_MIN - motg->mA_port);
2416 } else if (!test_bit(ID, &motg->inputs)) {
2417 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2418 motg->chg_type = USB_INVALID_CHARGER;
2419 msm_otg_notify_charger(motg, 0);
2420 msm_hsusb_vbus_power(motg, 1);
2421 }
2422 break;
2423 case OTG_STATE_A_SUSPEND:
2424 if ((test_bit(ID, &motg->inputs) &&
2425 !test_bit(ID_A, &motg->inputs)) ||
2426 test_bit(A_BUS_DROP, &motg->inputs) ||
2427 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2428 pr_debug("id_a/b/c || a_bus_drop ||"
2429 "a_aidl_bdis_tmout\n");
2430 msm_otg_del_timer(motg);
2431 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002432 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302433 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002434 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302435 if (!test_bit(ID_A, &motg->inputs))
2436 msm_hsusb_vbus_power(motg, 0);
2437 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2438 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2439 pr_debug("!a_vbus_vld\n");
2440 msm_otg_del_timer(motg);
2441 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002442 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302443 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002444 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302445 } else if (!test_bit(B_CONN, &motg->inputs) &&
2446 otg->host->b_hnp_enable) {
2447 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002448 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302449 msm_otg_host_hnp_enable(otg, 1);
2450 otg->gadget->is_a_peripheral = 1;
2451 msm_otg_start_peripheral(otg, 1);
2452 } else if (!test_bit(B_CONN, &motg->inputs) &&
2453 !otg->host->b_hnp_enable) {
2454 pr_debug("!b_conn && !b_hnp_enable");
2455 /*
2456 * bus request is dropped during suspend.
2457 * acquire again for next device.
2458 */
2459 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002460 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302461 if (TA_WAIT_BCON > 0)
2462 msm_otg_start_timer(motg, TA_WAIT_BCON,
2463 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002464 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302465 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002466 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302467 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002469 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302470 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302471 }
2472 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302473 case OTG_STATE_A_PERIPHERAL:
2474 if ((test_bit(ID, &motg->inputs) &&
2475 !test_bit(ID_A, &motg->inputs)) ||
2476 test_bit(A_BUS_DROP, &motg->inputs)) {
2477 pr_debug("id _f/b/c || a_bus_drop\n");
2478 /* Clear BIDL_ADIS timer */
2479 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002480 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302481 msm_otg_start_peripheral(otg, 0);
2482 otg->gadget->is_a_peripheral = 0;
2483 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002484 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302485 if (!test_bit(ID_A, &motg->inputs))
2486 msm_hsusb_vbus_power(motg, 0);
2487 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2488 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2489 pr_debug("!a_vbus_vld\n");
2490 /* Clear BIDL_ADIS timer */
2491 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002492 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302493 msm_otg_start_peripheral(otg, 0);
2494 otg->gadget->is_a_peripheral = 0;
2495 msm_otg_start_host(otg, 0);
2496 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2497 pr_debug("a_bidl_adis_tmout\n");
2498 msm_otg_start_peripheral(otg, 0);
2499 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002500 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302501 set_bit(A_BUS_REQ, &motg->inputs);
2502 msm_otg_host_hnp_enable(otg, 0);
2503 if (TA_WAIT_BCON > 0)
2504 msm_otg_start_timer(motg, TA_WAIT_BCON,
2505 A_WAIT_BCON);
2506 } else if (test_bit(ID_A, &motg->inputs)) {
2507 msm_hsusb_vbus_power(motg, 0);
2508 msm_otg_notify_charger(motg,
2509 IDEV_CHG_MIN - motg->mA_port);
2510 } else if (!test_bit(ID, &motg->inputs)) {
2511 msm_otg_notify_charger(motg, 0);
2512 msm_hsusb_vbus_power(motg, 1);
2513 }
2514 break;
2515 case OTG_STATE_A_WAIT_VFALL:
2516 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2517 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002518 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302519 work = 1;
2520 }
2521 break;
2522 case OTG_STATE_A_VBUS_ERR:
2523 if ((test_bit(ID, &motg->inputs) &&
2524 !test_bit(ID_A, &motg->inputs)) ||
2525 test_bit(A_BUS_DROP, &motg->inputs) ||
2526 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002527 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302528 if (!test_bit(ID_A, &motg->inputs))
2529 msm_hsusb_vbus_power(motg, 0);
2530 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2531 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2532 motg->chg_type = USB_INVALID_CHARGER;
2533 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302534 }
2535 break;
2536 default:
2537 break;
2538 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302539 if (work)
2540 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302541}
2542
2543static irqreturn_t msm_otg_irq(int irq, void *data)
2544{
2545 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002546 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302547 u32 otgsc = 0, usbsts, pc;
2548 bool work = 0;
2549 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302550
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302551 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302552 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302553 disable_irq_nosync(irq);
2554 motg->async_int = 1;
Jack Pham5ca279b2012-05-14 18:42:54 -07002555 if (atomic_read(&motg->pm_suspended))
2556 motg->sm_work_pending = true;
2557 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002558 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302559 return IRQ_HANDLED;
2560 }
2561
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002562 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302563 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302564
2565 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302566 return IRQ_NONE;
2567
2568 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302569 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302570 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302571 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302572 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302573 pr_debug("Id clear\n");
2574 /*
2575 * Assert a_bus_req to supply power on
2576 * VBUS when Micro/Mini-A cable is connected
2577 * with out user intervention.
2578 */
2579 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302580 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302581 msm_chg_enable_aca_det(motg);
2582 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302583 writel_relaxed(otgsc, USB_OTGSC);
2584 work = 1;
2585 } else if (otgsc & OTGSC_DPIS) {
2586 pr_debug("DPIS detected\n");
2587 writel_relaxed(otgsc, USB_OTGSC);
2588 set_bit(A_SRP_DET, &motg->inputs);
2589 set_bit(A_BUS_REQ, &motg->inputs);
2590 work = 1;
2591 } else if (otgsc & OTGSC_BSVIS) {
2592 writel_relaxed(otgsc, USB_OTGSC);
2593 /*
2594 * BSV interrupt comes when operating as an A-device
2595 * (VBUS on/off).
2596 * But, handle BSV when charger is removed from ACA in ID_A
2597 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002598 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302599 !test_bit(ID_A, &motg->inputs))
2600 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302601 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302602 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302603 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302604 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302605 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302606 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002607 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2608
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302609 msm_chg_check_aca_intr(motg);
2610 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302611 work = 1;
2612 } else if (usbsts & STS_PCI) {
2613 pc = readl_relaxed(USB_PORTSC);
2614 pr_debug("portsc = %x\n", pc);
2615 ret = IRQ_NONE;
2616 /*
2617 * HCD Acks PCI interrupt. We use this to switch
2618 * between different OTG states.
2619 */
2620 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002621 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302622 case OTG_STATE_A_SUSPEND:
2623 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2624 !(pc & PORTSC_CCS)) {
2625 pr_debug("B_CONN clear\n");
2626 clear_bit(B_CONN, &motg->inputs);
2627 msm_otg_del_timer(motg);
2628 }
2629 break;
2630 case OTG_STATE_A_PERIPHERAL:
2631 /*
2632 * A-peripheral observed activity on bus.
2633 * clear A_BIDL_ADIS timer.
2634 */
2635 msm_otg_del_timer(motg);
2636 work = 0;
2637 break;
2638 case OTG_STATE_B_WAIT_ACON:
2639 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2640 pr_debug("A_CONN set\n");
2641 set_bit(A_CONN, &motg->inputs);
2642 /* Clear ASE0_BRST timer */
2643 msm_otg_del_timer(motg);
2644 }
2645 break;
2646 case OTG_STATE_B_HOST:
2647 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2648 pr_debug("A_CONN clear\n");
2649 clear_bit(A_CONN, &motg->inputs);
2650 msm_otg_del_timer(motg);
2651 }
2652 break;
2653 case OTG_STATE_A_WAIT_BCON:
2654 if (TA_WAIT_BCON < 0)
2655 set_bit(A_BUS_REQ, &motg->inputs);
2656 default:
2657 work = 0;
2658 break;
2659 }
2660 } else if (usbsts & STS_URI) {
2661 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002662 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302663 case OTG_STATE_A_PERIPHERAL:
2664 /*
2665 * A-peripheral observed activity on bus.
2666 * clear A_BIDL_ADIS timer.
2667 */
2668 msm_otg_del_timer(motg);
2669 work = 0;
2670 break;
2671 default:
2672 work = 0;
2673 break;
2674 }
2675 } else if (usbsts & STS_SLI) {
2676 ret = IRQ_NONE;
2677 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002678 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302679 case OTG_STATE_B_PERIPHERAL:
2680 if (otg->gadget->b_hnp_enable) {
2681 set_bit(A_BUS_SUSPEND, &motg->inputs);
2682 set_bit(B_BUS_REQ, &motg->inputs);
2683 work = 1;
2684 }
2685 break;
2686 case OTG_STATE_A_PERIPHERAL:
2687 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2688 A_BIDL_ADIS);
2689 break;
2690 default:
2691 break;
2692 }
2693 } else if ((usbsts & PHY_ALT_INT)) {
2694 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2695 if (msm_chg_check_aca_intr(motg))
2696 work = 1;
2697 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302698 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302699 if (work)
2700 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302701
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302702 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002703}
2704
2705static void msm_otg_set_vbus_state(int online)
2706{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302707 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002708 struct msm_otg *motg = the_msm_otg;
2709
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302710 if (online) {
2711 pr_debug("PMIC: BSV set\n");
2712 set_bit(B_SESS_VLD, &motg->inputs);
2713 } else {
2714 pr_debug("PMIC: BSV clear\n");
2715 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302716 }
2717
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302718 if (!init) {
2719 init = true;
2720 complete(&pmic_vbus_init);
2721 pr_debug("PMIC: BSV init complete\n");
2722 return;
2723 }
2724
Jack Pham5ca279b2012-05-14 18:42:54 -07002725 if (atomic_read(&motg->pm_suspended))
2726 motg->sm_work_pending = true;
2727 else
2728 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002729}
2730
2731static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2732{
2733 struct msm_otg *motg = data;
2734
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302735 if (aca_id_turned_on)
2736 return IRQ_HANDLED;
2737
2738 if (irq_read_line(motg->pdata->pmic_id_irq)) {
2739 pr_debug("PMIC: ID set\n");
2740 set_bit(ID, &motg->inputs);
2741 } else {
2742 pr_debug("PMIC: ID clear\n");
2743 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302744 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302745 }
2746
Steve Mucklef132c6c2012-06-06 18:30:57 -07002747 if (motg->phy.state != OTG_STATE_UNDEFINED) {
Jack Pham5ca279b2012-05-14 18:42:54 -07002748 if (atomic_read(&motg->pm_suspended))
2749 motg->sm_work_pending = true;
2750 else
2751 queue_work(system_nrt_wq, &motg->sm_work);
2752 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002753
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302754 return IRQ_HANDLED;
2755}
2756
2757static int msm_otg_mode_show(struct seq_file *s, void *unused)
2758{
2759 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002760 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302761
Steve Mucklef132c6c2012-06-06 18:30:57 -07002762 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302763 case OTG_STATE_A_HOST:
2764 seq_printf(s, "host\n");
2765 break;
2766 case OTG_STATE_B_PERIPHERAL:
2767 seq_printf(s, "peripheral\n");
2768 break;
2769 default:
2770 seq_printf(s, "none\n");
2771 break;
2772 }
2773
2774 return 0;
2775}
2776
2777static int msm_otg_mode_open(struct inode *inode, struct file *file)
2778{
2779 return single_open(file, msm_otg_mode_show, inode->i_private);
2780}
2781
2782static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2783 size_t count, loff_t *ppos)
2784{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302785 struct seq_file *s = file->private_data;
2786 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302787 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07002788 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302789 int status = count;
2790 enum usb_mode_type req_mode;
2791
2792 memset(buf, 0x00, sizeof(buf));
2793
2794 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2795 status = -EFAULT;
2796 goto out;
2797 }
2798
2799 if (!strncmp(buf, "host", 4)) {
2800 req_mode = USB_HOST;
2801 } else if (!strncmp(buf, "peripheral", 10)) {
2802 req_mode = USB_PERIPHERAL;
2803 } else if (!strncmp(buf, "none", 4)) {
2804 req_mode = USB_NONE;
2805 } else {
2806 status = -EINVAL;
2807 goto out;
2808 }
2809
2810 switch (req_mode) {
2811 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002812 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302813 case OTG_STATE_A_HOST:
2814 case OTG_STATE_B_PERIPHERAL:
2815 set_bit(ID, &motg->inputs);
2816 clear_bit(B_SESS_VLD, &motg->inputs);
2817 break;
2818 default:
2819 goto out;
2820 }
2821 break;
2822 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002823 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302824 case OTG_STATE_B_IDLE:
2825 case OTG_STATE_A_HOST:
2826 set_bit(ID, &motg->inputs);
2827 set_bit(B_SESS_VLD, &motg->inputs);
2828 break;
2829 default:
2830 goto out;
2831 }
2832 break;
2833 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002834 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302835 case OTG_STATE_B_IDLE:
2836 case OTG_STATE_B_PERIPHERAL:
2837 clear_bit(ID, &motg->inputs);
2838 break;
2839 default:
2840 goto out;
2841 }
2842 break;
2843 default:
2844 goto out;
2845 }
2846
Steve Mucklef132c6c2012-06-06 18:30:57 -07002847 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302848 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302849out:
2850 return status;
2851}
2852
2853const struct file_operations msm_otg_mode_fops = {
2854 .open = msm_otg_mode_open,
2855 .read = seq_read,
2856 .write = msm_otg_mode_write,
2857 .llseek = seq_lseek,
2858 .release = single_release,
2859};
2860
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302861static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
2862{
2863 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002864 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302865
Steve Mucklef132c6c2012-06-06 18:30:57 -07002866 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302867 return 0;
2868}
2869
2870static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
2871{
2872 return single_open(file, msm_otg_show_otg_state, inode->i_private);
2873}
2874
2875const struct file_operations msm_otg_state_fops = {
2876 .open = msm_otg_otg_state_open,
2877 .read = seq_read,
2878 .llseek = seq_lseek,
2879 .release = single_release,
2880};
2881
Anji jonnalad270e2d2011-08-09 11:28:32 +05302882static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2883{
2884 struct msm_otg *motg = s->private;
2885
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302886 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302887 return 0;
2888}
2889
2890static int msm_otg_chg_open(struct inode *inode, struct file *file)
2891{
2892 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2893}
2894
2895const struct file_operations msm_otg_chg_fops = {
2896 .open = msm_otg_chg_open,
2897 .read = seq_read,
2898 .llseek = seq_lseek,
2899 .release = single_release,
2900};
2901
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302902static int msm_otg_aca_show(struct seq_file *s, void *unused)
2903{
2904 if (debug_aca_enabled)
2905 seq_printf(s, "enabled\n");
2906 else
2907 seq_printf(s, "disabled\n");
2908
2909 return 0;
2910}
2911
2912static int msm_otg_aca_open(struct inode *inode, struct file *file)
2913{
2914 return single_open(file, msm_otg_aca_show, inode->i_private);
2915}
2916
2917static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2918 size_t count, loff_t *ppos)
2919{
2920 char buf[8];
2921
2922 memset(buf, 0x00, sizeof(buf));
2923
2924 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2925 return -EFAULT;
2926
2927 if (!strncmp(buf, "enable", 6))
2928 debug_aca_enabled = true;
2929 else
2930 debug_aca_enabled = false;
2931
2932 return count;
2933}
2934
2935const struct file_operations msm_otg_aca_fops = {
2936 .open = msm_otg_aca_open,
2937 .read = seq_read,
2938 .write = msm_otg_aca_write,
2939 .llseek = seq_lseek,
2940 .release = single_release,
2941};
2942
Manu Gautam8bdcc592012-03-06 11:26:06 +05302943static int msm_otg_bus_show(struct seq_file *s, void *unused)
2944{
2945 if (debug_bus_voting_enabled)
2946 seq_printf(s, "enabled\n");
2947 else
2948 seq_printf(s, "disabled\n");
2949
2950 return 0;
2951}
2952
2953static int msm_otg_bus_open(struct inode *inode, struct file *file)
2954{
2955 return single_open(file, msm_otg_bus_show, inode->i_private);
2956}
2957
2958static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
2959 size_t count, loff_t *ppos)
2960{
2961 char buf[8];
2962 int ret;
2963 struct seq_file *s = file->private_data;
2964 struct msm_otg *motg = s->private;
2965
2966 memset(buf, 0x00, sizeof(buf));
2967
2968 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2969 return -EFAULT;
2970
2971 if (!strncmp(buf, "enable", 6)) {
2972 /* Do not vote here. Let OTG statemachine decide when to vote */
2973 debug_bus_voting_enabled = true;
2974 } else {
2975 debug_bus_voting_enabled = false;
2976 if (motg->bus_perf_client) {
2977 ret = msm_bus_scale_client_update_request(
2978 motg->bus_perf_client, 0);
2979 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002980 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05302981 "for bus bw %d\n", __func__, ret);
2982 }
2983 }
2984
2985 return count;
2986}
2987
2988const struct file_operations msm_otg_bus_fops = {
2989 .open = msm_otg_bus_open,
2990 .read = seq_read,
2991 .write = msm_otg_bus_write,
2992 .llseek = seq_lseek,
2993 .release = single_release,
2994};
2995
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302996static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302997
2998static int msm_otg_debugfs_init(struct msm_otg *motg)
2999{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303000 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303001
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303002 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3003
3004 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3005 return -ENODEV;
3006
Anji jonnalad270e2d2011-08-09 11:28:32 +05303007 if (motg->pdata->mode == USB_OTG &&
3008 motg->pdata->otg_control == OTG_USER_CONTROL) {
3009
Manu Gautam8bdcc592012-03-06 11:26:06 +05303010 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303011 S_IWUSR, msm_otg_dbg_root, motg,
3012 &msm_otg_mode_fops);
3013
Manu Gautam8bdcc592012-03-06 11:26:06 +05303014 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303015 debugfs_remove(msm_otg_dbg_root);
3016 msm_otg_dbg_root = NULL;
3017 return -ENODEV;
3018 }
3019 }
3020
Manu Gautam8bdcc592012-03-06 11:26:06 +05303021 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303022 msm_otg_dbg_root, motg,
3023 &msm_otg_chg_fops);
3024
Manu Gautam8bdcc592012-03-06 11:26:06 +05303025 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303026 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303027 return -ENODEV;
3028 }
3029
Manu Gautam8bdcc592012-03-06 11:26:06 +05303030 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303031 msm_otg_dbg_root, motg,
3032 &msm_otg_aca_fops);
3033
Manu Gautam8bdcc592012-03-06 11:26:06 +05303034 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303035 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303036 return -ENODEV;
3037 }
3038
Manu Gautam8bdcc592012-03-06 11:26:06 +05303039 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3040 msm_otg_dbg_root, motg,
3041 &msm_otg_bus_fops);
3042
3043 if (!msm_otg_dentry) {
3044 debugfs_remove_recursive(msm_otg_dbg_root);
3045 return -ENODEV;
3046 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303047
3048 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3049 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3050
3051 if (!msm_otg_dentry) {
3052 debugfs_remove_recursive(msm_otg_dbg_root);
3053 return -ENODEV;
3054 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303055 return 0;
3056}
3057
3058static void msm_otg_debugfs_cleanup(void)
3059{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303060 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303061}
3062
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303063static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3064static struct platform_device *msm_otg_add_pdev(
3065 struct platform_device *ofdev, const char *name)
3066{
3067 struct platform_device *pdev;
3068 const struct resource *res = ofdev->resource;
3069 unsigned int num = ofdev->num_resources;
3070 int retval;
3071
3072 pdev = platform_device_alloc(name, -1);
3073 if (!pdev) {
3074 retval = -ENOMEM;
3075 goto error;
3076 }
3077
3078 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3079 pdev->dev.dma_mask = &msm_otg_dma_mask;
3080
3081 if (num) {
3082 retval = platform_device_add_resources(pdev, res, num);
3083 if (retval)
3084 goto error;
3085 }
3086
3087 retval = platform_device_add(pdev);
3088 if (retval)
3089 goto error;
3090
3091 return pdev;
3092
3093error:
3094 platform_device_put(pdev);
3095 return ERR_PTR(retval);
3096}
3097
3098static int msm_otg_setup_devices(struct platform_device *ofdev,
3099 enum usb_mode_type mode, bool init)
3100{
3101 const char *gadget_name = "msm_hsusb";
3102 const char *host_name = "msm_hsusb_host";
3103 static struct platform_device *gadget_pdev;
3104 static struct platform_device *host_pdev;
3105 int retval = 0;
3106
3107 if (!init) {
3108 if (gadget_pdev)
3109 platform_device_unregister(gadget_pdev);
3110 if (host_pdev)
3111 platform_device_unregister(host_pdev);
3112 return 0;
3113 }
3114
3115 switch (mode) {
3116 case USB_OTG:
3117 /* fall through */
3118 case USB_PERIPHERAL:
3119 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3120 if (IS_ERR(gadget_pdev)) {
3121 retval = PTR_ERR(gadget_pdev);
3122 break;
3123 }
3124 if (mode == USB_PERIPHERAL)
3125 break;
3126 /* fall through */
3127 case USB_HOST:
3128 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3129 if (IS_ERR(host_pdev)) {
3130 retval = PTR_ERR(host_pdev);
3131 if (mode == USB_OTG)
3132 platform_device_unregister(gadget_pdev);
3133 }
3134 break;
3135 default:
3136 break;
3137 }
3138
3139 return retval;
3140}
3141
3142struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3143{
3144 struct device_node *node = pdev->dev.of_node;
3145 struct msm_otg_platform_data *pdata;
3146 int len = 0;
3147
3148 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3149 if (!pdata) {
3150 pr_err("unable to allocate platform data\n");
3151 return NULL;
3152 }
3153 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3154 if (len) {
3155 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3156 if (!pdata->phy_init_seq)
3157 return NULL;
3158 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3159 pdata->phy_init_seq,
3160 len/sizeof(*pdata->phy_init_seq));
3161 }
3162 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3163 &pdata->power_budget);
3164 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3165 &pdata->mode);
3166 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3167 &pdata->otg_control);
3168 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3169 &pdata->default_mode);
3170 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3171 &pdata->phy_type);
3172 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3173 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303174 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303175}
3176
3177static int __init msm_otg_probe(struct platform_device *pdev)
3178{
3179 int ret = 0;
3180 struct resource *res;
3181 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003182 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303183 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303184
3185 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303186
3187 if (pdev->dev.of_node) {
3188 dev_dbg(&pdev->dev, "device tree enabled\n");
3189 pdata = msm_otg_dt_to_pdata(pdev);
3190 if (!pdata)
3191 return -ENOMEM;
3192 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3193 if (ret) {
3194 dev_err(&pdev->dev, "devices setup failed\n");
3195 return ret;
3196 }
3197 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303198 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3199 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303200 } else {
3201 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303202 }
3203
3204 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3205 if (!motg) {
3206 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3207 return -ENOMEM;
3208 }
3209
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003210 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3211 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003212 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3213 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303214 goto free_motg;
3215 }
3216
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003217 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303218 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003219 phy = &motg->phy;
3220 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303221
3222 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303223 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3224 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3225 * PMIC can detect ACA ID_GND and generate an interrupt.
3226 */
3227 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3228 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3229 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003230 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303231 }
3232
Ofir Cohen4da266f2012-01-03 10:19:29 +02003233 /* initialize reset counter */
3234 motg->reset_counter = 0;
3235
Amit Blay02eff132011-09-21 16:46:24 +03003236 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003237 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003238 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003239 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303240
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303241 /*
3242 * Targets on which link uses asynchronous reset methodology,
3243 * free running clock is not required during the reset.
3244 */
Manu Gautam5143b252012-01-05 19:25:23 -08003245 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303246 if (IS_ERR(motg->clk))
3247 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3248 else
3249 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303250
3251 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003252 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303253 * CORE CLK must be running at >55Mhz for correct HSUSB
3254 * operation and USB core cannot tolerate frequency changes on
3255 * CORE CLK. For such USB cores, vote for maximum clk frequency
3256 * on pclk source
3257 */
Manu Gautam5143b252012-01-05 19:25:23 -08003258 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3259 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303260 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003261 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303262 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003263 goto put_clk;
3264 }
3265 clk_set_rate(motg->core_clk, INT_MAX);
3266
3267 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3268 if (IS_ERR(motg->pclk)) {
3269 dev_err(&pdev->dev, "failed to get iface_clk\n");
3270 ret = PTR_ERR(motg->pclk);
3271 goto put_core_clk;
3272 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303273
3274 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3275 if (!res) {
3276 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3277 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003278 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303279 }
3280
3281 motg->regs = ioremap(res->start, resource_size(res));
3282 if (!motg->regs) {
3283 dev_err(&pdev->dev, "ioremap failed\n");
3284 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003285 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303286 }
3287 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3288
3289 motg->irq = platform_get_irq(pdev, 0);
3290 if (!motg->irq) {
3291 dev_err(&pdev->dev, "platform_get_irq failed\n");
3292 ret = -ENODEV;
3293 goto free_regs;
3294 }
3295
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003296 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003297 if (IS_ERR(motg->xo_handle)) {
3298 dev_err(&pdev->dev, "%s not able to get the handle "
3299 "to vote for TCXO D0 buffer\n", __func__);
3300 ret = PTR_ERR(motg->xo_handle);
3301 goto free_regs;
3302 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303303
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003304 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003305 if (ret) {
3306 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3307 "D0 buffer%d\n", __func__, ret);
3308 goto free_xo_handle;
3309 }
3310
Manu Gautam28b1bac2012-01-30 16:43:06 +05303311 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303312
Mayank Rana248698c2012-04-19 00:03:16 +05303313 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003314 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303315 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003316 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303317 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003318 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303319 goto devote_xo_handle;
3320 }
3321 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303322 }
3323
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003324 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303325 if (ret) {
3326 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303327 goto devote_xo_handle;
3328 }
3329
3330 ret = regulator_enable(hsusb_vddcx);
3331 if (ret) {
3332 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3333 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303334 }
3335
3336 ret = msm_hsusb_ldo_init(motg, 1);
3337 if (ret) {
3338 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303339 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303340 }
3341
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303342 if (pdata->mhl_enable) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003343 mhl_analog_switch = devm_regulator_get(motg->phy.dev,
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303344 "mhl_ext_3p3v");
3345 if (IS_ERR(mhl_analog_switch)) {
3346 dev_err(&pdev->dev, "Unable to get mhl_analog_switch\n");
3347 goto free_ldo_init;
3348 }
3349 }
3350
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003351 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303352 if (ret) {
3353 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003354 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303355 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303356 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303357
3358 writel(0, USB_USBINTR);
3359 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003360 /* Ensure that above STOREs are completed before enabling interrupts */
3361 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303362
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003363 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303364 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303365 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303366 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303367 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3368 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303369 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3370 "msm_otg", motg);
3371 if (ret) {
3372 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003373 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303374 }
3375
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003376 phy->init = msm_otg_reset;
3377 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003378 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303379
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003380 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303381
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003382 phy->otg->phy = &motg->phy;
3383 phy->otg->set_host = msm_otg_set_host;
3384 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003385 phy->otg->start_hnp = msm_otg_start_hnp;
3386 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003387
3388 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303389 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003390 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303391 goto free_irq;
3392 }
3393
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303394 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303395 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003396 if (motg->pdata->pmic_id_irq) {
3397 ret = request_irq(motg->pdata->pmic_id_irq,
3398 msm_pmic_id_irq,
3399 IRQF_TRIGGER_RISING |
3400 IRQF_TRIGGER_FALLING,
3401 "msm_otg", motg);
3402 if (ret) {
3403 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003404 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003405 }
3406 } else {
3407 ret = -ENODEV;
3408 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003409 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003410 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303411 }
3412
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303413 msm_hsusb_mhl_switch_enable(motg, 1);
3414
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303415 platform_set_drvdata(pdev, motg);
3416 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003417 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303418
Anji jonnalad270e2d2011-08-09 11:28:32 +05303419 ret = msm_otg_debugfs_init(motg);
3420 if (ret)
3421 dev_dbg(&pdev->dev, "mode debugfs file is"
3422 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303423
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003424 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3425 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3426
Amit Blay58b31472011-11-18 09:39:39 +02003427 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3428 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303429 (!(motg->pdata->mode == USB_OTG) ||
3430 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003431 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303432 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003433
Amit Blay58b31472011-11-18 09:39:39 +02003434 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3435 motg->caps = ALLOW_PHY_RETENTION;
3436 }
3437
Amit Blay6fa647a2012-05-24 14:12:08 +03003438 if (motg->pdata->enable_lpm_on_dev_suspend)
3439 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3440
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003441 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303442 pm_runtime_set_active(&pdev->dev);
3443 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303444
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303445 if (motg->pdata->bus_scale_table) {
3446 motg->bus_perf_client =
3447 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3448 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003449 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303450 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303451 else
3452 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303453 }
3454
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303455 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003456
Steve Mucklef132c6c2012-06-06 18:30:57 -07003457remove_phy:
3458 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303459free_irq:
3460 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003461destroy_wlock:
3462 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303463 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003464 msm_hsusb_ldo_enable(motg, 0);
3465free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303466 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303467free_hsusb_vddcx:
3468 regulator_disable(hsusb_vddcx);
3469free_config_vddcx:
3470 regulator_set_voltage(hsusb_vddcx,
3471 vdd_val[motg->vdd_type][VDD_NONE],
3472 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003473devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303474 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003475 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003476free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003477 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303478free_regs:
3479 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003480put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303481 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303482put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003483 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303484put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303485 if (!IS_ERR(motg->clk))
3486 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003487 if (!IS_ERR(motg->phy_reset_clk))
3488 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003489free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003490 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303491free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303492 kfree(motg);
3493 return ret;
3494}
3495
3496static int __devexit msm_otg_remove(struct platform_device *pdev)
3497{
3498 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003499 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303500 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303501
3502 if (otg->host || otg->gadget)
3503 return -EBUSY;
3504
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303505 if (pdev->dev.of_node)
3506 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003507 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3508 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303509 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303510 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303511 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303512
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303513 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303514
3515 device_init_wakeup(&pdev->dev, 0);
3516 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003517 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303518
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303519 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003520 if (motg->pdata->pmic_id_irq)
3521 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003522 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303523 free_irq(motg->irq, motg);
3524
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303525 /*
3526 * Put PHY in low power mode.
3527 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003528 ulpi_read(otg->phy, 0x14);
3529 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303530
3531 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3532 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3533 if (readl(USB_PORTSC) & PORTSC_PHCD)
3534 break;
3535 udelay(1);
3536 cnt++;
3537 }
3538 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003539 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303540
Manu Gautam28b1bac2012-01-30 16:43:06 +05303541 clk_disable_unprepare(motg->pclk);
3542 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003543 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003544 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303545 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303546 regulator_disable(hsusb_vddcx);
3547 regulator_set_voltage(hsusb_vddcx,
3548 vdd_val[motg->vdd_type][VDD_NONE],
3549 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303550
3551 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303552 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303553
Amit Blay02eff132011-09-21 16:46:24 +03003554 if (!IS_ERR(motg->phy_reset_clk))
3555 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303556 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303557 if (!IS_ERR(motg->clk))
3558 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003559 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303560
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303561 if (motg->bus_perf_client)
3562 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303563
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003564 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303565 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303566 return 0;
3567}
3568
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303569#ifdef CONFIG_PM_RUNTIME
3570static int msm_otg_runtime_idle(struct device *dev)
3571{
3572 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003573 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303574
3575 dev_dbg(dev, "OTG runtime idle\n");
3576
Steve Mucklef132c6c2012-06-06 18:30:57 -07003577 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303578 return -EAGAIN;
3579 else
3580 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303581}
3582
3583static int msm_otg_runtime_suspend(struct device *dev)
3584{
3585 struct msm_otg *motg = dev_get_drvdata(dev);
3586
3587 dev_dbg(dev, "OTG runtime suspend\n");
3588 return msm_otg_suspend(motg);
3589}
3590
3591static int msm_otg_runtime_resume(struct device *dev)
3592{
3593 struct msm_otg *motg = dev_get_drvdata(dev);
3594
3595 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303596 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303597 return msm_otg_resume(motg);
3598}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303599#endif
3600
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303601#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303602static int msm_otg_pm_suspend(struct device *dev)
3603{
Jack Pham5ca279b2012-05-14 18:42:54 -07003604 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303605 struct msm_otg *motg = dev_get_drvdata(dev);
3606
3607 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003608
3609 atomic_set(&motg->pm_suspended, 1);
3610 ret = msm_otg_suspend(motg);
3611 if (ret)
3612 atomic_set(&motg->pm_suspended, 0);
3613
3614 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303615}
3616
3617static int msm_otg_pm_resume(struct device *dev)
3618{
Jack Pham5ca279b2012-05-14 18:42:54 -07003619 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303620 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303621
3622 dev_dbg(dev, "OTG PM resume\n");
3623
Jack Pham5ca279b2012-05-14 18:42:54 -07003624 atomic_set(&motg->pm_suspended, 0);
3625 if (motg->sm_work_pending) {
3626 motg->sm_work_pending = false;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303627
Jack Pham5ca279b2012-05-14 18:42:54 -07003628 pm_runtime_get_noresume(dev);
3629 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303630
Jack Pham5ca279b2012-05-14 18:42:54 -07003631 /* Update runtime PM status */
3632 pm_runtime_disable(dev);
3633 pm_runtime_set_active(dev);
3634 pm_runtime_enable(dev);
3635
3636 queue_work(system_nrt_wq, &motg->sm_work);
3637 }
3638
3639 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303640}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303641#endif
3642
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303643#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303644static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303645 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3646 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3647 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303648};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303649#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303650
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303651static struct of_device_id msm_otg_dt_match[] = {
3652 { .compatible = "qcom,hsusb-otg",
3653 },
3654 {}
3655};
3656
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303657static struct platform_driver msm_otg_driver = {
3658 .remove = __devexit_p(msm_otg_remove),
3659 .driver = {
3660 .name = DRIVER_NAME,
3661 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303662#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303663 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303664#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303665 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303666 },
3667};
3668
3669static int __init msm_otg_init(void)
3670{
3671 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3672}
3673
3674static void __exit msm_otg_exit(void)
3675{
3676 platform_driver_unregister(&msm_otg_driver);
3677}
3678
3679module_init(msm_otg_init);
3680module_exit(msm_otg_exit);
3681
3682MODULE_LICENSE("GPL v2");
3683MODULE_DESCRIPTION("MSM USB transceiver driver");