blob: d3e92f22e9dfb6a81f6e3a578e790d033c471a96 [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)
82{
83#ifdef CONFIG_USB_MSM_ACA
84 return true;
85#else
86 return debug_aca_enabled;
87#endif
88}
89
Mayank Rana248698c2012-04-19 00:03:16 +053090enum usb_vdd_value {
91 VDD_NONE = 0,
92 VDD_MIN,
93 VDD_MAX,
94 VDD_VAL_MAX,
95};
Anji jonnala11aa5c42011-05-04 10:19:48 +053096
Mayank Rana248698c2012-04-19 00:03:16 +053097static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
98 { /* VDD_CX CORNER Voting */
99 [VDD_NONE] = RPM_VREG_CORNER_NONE,
100 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
101 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
102 },
103 { /* VDD_CX Voltage Voting */
104 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
105 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
106 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
107 },
108};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530109
110static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
111{
112 int rc = 0;
113
114 if (init) {
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530115 hsusb_3p3 = devm_regulator_get(motg->otg.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530116 if (IS_ERR(hsusb_3p3)) {
117 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
118 return PTR_ERR(hsusb_3p3);
119 }
120
121 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
122 USB_PHY_3P3_VOL_MAX);
123 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124 dev_err(motg->otg.dev, "unable to set voltage level for"
125 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530126 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530127 }
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530128 hsusb_1p8 = devm_regulator_get(motg->otg.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530129 if (IS_ERR(hsusb_1p8)) {
130 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
131 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133 }
134 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
135 USB_PHY_1P8_VOL_MAX);
136 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 dev_err(motg->otg.dev, "unable to set voltage level for"
138 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530139 goto put_1p8;
140 }
141
142 return 0;
143 }
144
Anji jonnala11aa5c42011-05-04 10:19:48 +0530145put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700147put_3p3_lpm:
148 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530149 return rc;
150}
151
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530152static int msm_hsusb_config_vddcx(int high)
153{
Mayank Rana248698c2012-04-19 00:03:16 +0530154 struct msm_otg *motg = the_msm_otg;
155 enum usb_vdd_type vdd_type = motg->vdd_type;
156 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530157 int min_vol;
158 int ret;
159
Mayank Rana248698c2012-04-19 00:03:16 +0530160 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530161 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
162 if (ret) {
163 pr_err("%s: unable to set the voltage for regulator "
164 "HSUSB_VDDCX\n", __func__);
165 return ret;
166 }
167
168 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
169
170 return ret;
171}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530172
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530174{
175 int ret = 0;
176
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530177 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530178 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
179 return -ENODEV;
180 }
181
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530182 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530183 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
184 return -ENODEV;
185 }
186
187 if (on) {
188 ret = regulator_set_optimum_mode(hsusb_1p8,
189 USB_PHY_1P8_HPM_LOAD);
190 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530192 "HSUSB_1p8\n", __func__);
193 return ret;
194 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195
196 ret = regulator_enable(hsusb_1p8);
197 if (ret) {
198 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
199 __func__);
200 regulator_set_optimum_mode(hsusb_1p8, 0);
201 return ret;
202 }
203
Anji jonnala11aa5c42011-05-04 10:19:48 +0530204 ret = regulator_set_optimum_mode(hsusb_3p3,
205 USB_PHY_3P3_HPM_LOAD);
206 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530208 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209 regulator_set_optimum_mode(hsusb_1p8, 0);
210 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530211 return ret;
212 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213
214 ret = regulator_enable(hsusb_3p3);
215 if (ret) {
216 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
217 __func__);
218 regulator_set_optimum_mode(hsusb_3p3, 0);
219 regulator_set_optimum_mode(hsusb_1p8, 0);
220 regulator_disable(hsusb_1p8);
221 return ret;
222 }
223
Anji jonnala11aa5c42011-05-04 10:19:48 +0530224 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225 ret = regulator_disable(hsusb_1p8);
226 if (ret) {
227 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
228 __func__);
229 return ret;
230 }
231
232 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530233 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530235 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236
237 ret = regulator_disable(hsusb_3p3);
238 if (ret) {
239 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
240 __func__);
241 return ret;
242 }
243 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530244 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530246 "HSUSB_3p3\n", __func__);
247 }
248
249 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
250 return ret < 0 ? ret : 0;
251}
252
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530253static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
254{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530255 struct msm_otg_platform_data *pdata = motg->pdata;
256
257 if (!pdata->mhl_enable)
258 return;
259
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530260 if (!mhl_analog_switch) {
261 pr_err("%s: mhl_analog_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530262 return;
263 }
264
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530265 if (on) {
266 if (regulator_enable(mhl_analog_switch))
267 pr_err("unable to enable mhl_analog_switch\n");
268 } else {
269 regulator_disable(mhl_analog_switch);
270 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530271}
272
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530273static int ulpi_read(struct otg_transceiver *otg, u32 reg)
274{
275 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
276 int cnt = 0;
277
278 /* initiate read operation */
279 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
280 USB_ULPI_VIEWPORT);
281
282 /* wait for completion */
283 while (cnt < ULPI_IO_TIMEOUT_USEC) {
284 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
285 break;
286 udelay(1);
287 cnt++;
288 }
289
290 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
291 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
292 readl(USB_ULPI_VIEWPORT));
293 return -ETIMEDOUT;
294 }
295 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
296}
297
298static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
299{
300 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
301 int cnt = 0;
302
303 /* initiate write operation */
304 writel(ULPI_RUN | ULPI_WRITE |
305 ULPI_ADDR(reg) | ULPI_DATA(val),
306 USB_ULPI_VIEWPORT);
307
308 /* wait for completion */
309 while (cnt < ULPI_IO_TIMEOUT_USEC) {
310 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
311 break;
312 udelay(1);
313 cnt++;
314 }
315
316 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
317 dev_err(otg->dev, "ulpi_write: timeout\n");
318 return -ETIMEDOUT;
319 }
320 return 0;
321}
322
323static struct otg_io_access_ops msm_otg_io_ops = {
324 .read = ulpi_read,
325 .write = ulpi_write,
326};
327
328static void ulpi_init(struct msm_otg *motg)
329{
330 struct msm_otg_platform_data *pdata = motg->pdata;
331 int *seq = pdata->phy_init_seq;
332
333 if (!seq)
334 return;
335
336 while (seq[0] >= 0) {
337 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
338 seq[0], seq[1]);
339 ulpi_write(&motg->otg, seq[0], seq[1]);
340 seq += 2;
341 }
342}
343
344static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
345{
346 int ret;
347
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530348 if (IS_ERR(motg->clk))
349 return 0;
350
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530351 if (assert) {
352 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
353 if (ret)
354 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
355 } else {
356 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
357 if (ret)
358 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
359 }
360 return ret;
361}
362
363static int msm_otg_phy_clk_reset(struct msm_otg *motg)
364{
365 int ret;
366
Amit Blay02eff132011-09-21 16:46:24 +0300367 if (IS_ERR(motg->phy_reset_clk))
368 return 0;
369
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530370 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
371 if (ret) {
372 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
373 return ret;
374 }
375 usleep_range(10000, 12000);
376 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
377 if (ret)
378 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
379 return ret;
380}
381
382static int msm_otg_phy_reset(struct msm_otg *motg)
383{
384 u32 val;
385 int ret;
386 int retries;
387
388 ret = msm_otg_link_clk_reset(motg, 1);
389 if (ret)
390 return ret;
391 ret = msm_otg_phy_clk_reset(motg);
392 if (ret)
393 return ret;
394 ret = msm_otg_link_clk_reset(motg, 0);
395 if (ret)
396 return ret;
397
398 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
399 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
400
401 for (retries = 3; retries > 0; retries--) {
402 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
403 ULPI_CLR(ULPI_FUNC_CTRL));
404 if (!ret)
405 break;
406 ret = msm_otg_phy_clk_reset(motg);
407 if (ret)
408 return ret;
409 }
410 if (!retries)
411 return -ETIMEDOUT;
412
413 /* This reset calibrates the phy, if the above write succeeded */
414 ret = msm_otg_phy_clk_reset(motg);
415 if (ret)
416 return ret;
417
418 for (retries = 3; retries > 0; retries--) {
419 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
420 if (ret != -ETIMEDOUT)
421 break;
422 ret = msm_otg_phy_clk_reset(motg);
423 if (ret)
424 return ret;
425 }
426 if (!retries)
427 return -ETIMEDOUT;
428
429 dev_info(motg->otg.dev, "phy_reset: success\n");
430 return 0;
431}
432
433#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530434static int msm_otg_link_reset(struct msm_otg *motg)
435{
436 int cnt = 0;
437
438 writel_relaxed(USBCMD_RESET, USB_USBCMD);
439 while (cnt < LINK_RESET_TIMEOUT_USEC) {
440 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
441 break;
442 udelay(1);
443 cnt++;
444 }
445 if (cnt >= LINK_RESET_TIMEOUT_USEC)
446 return -ETIMEDOUT;
447
448 /* select ULPI phy */
449 writel_relaxed(0x80000000, USB_PORTSC);
450 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530451 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530452
453 return 0;
454}
455
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530456static int msm_otg_reset(struct otg_transceiver *otg)
457{
458 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
459 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530460 int ret;
461 u32 val = 0;
462 u32 ulpi_val = 0;
463
Ofir Cohen4da266f2012-01-03 10:19:29 +0200464 /*
465 * USB PHY and Link reset also reset the USB BAM.
466 * Thus perform reset operation only once to avoid
467 * USB BAM reset on other cases e.g. USB cable disconnections.
468 */
469 if (pdata->disable_reset_on_disconnect) {
470 if (motg->reset_counter)
471 return 0;
472 else
473 motg->reset_counter++;
474 }
475
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530476 if (!IS_ERR(motg->clk))
477 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530478 ret = msm_otg_phy_reset(motg);
479 if (ret) {
480 dev_err(otg->dev, "phy_reset failed\n");
481 return ret;
482 }
483
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530484 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530485 ret = msm_otg_link_reset(motg);
486 if (ret) {
487 dev_err(otg->dev, "link reset failed\n");
488 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530489 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530490 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530491
492 ulpi_init(motg);
493
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700494 /* Ensure that RESET operation is completed before turning off clock */
495 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530496
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530497 if (!IS_ERR(motg->clk))
498 clk_disable_unprepare(motg->clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530500 if (pdata->otg_control == OTG_PHY_CONTROL) {
501 val = readl_relaxed(USB_OTGSC);
502 if (pdata->mode == USB_OTG) {
503 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
504 val |= OTGSC_IDIE | OTGSC_BSVIE;
505 } else if (pdata->mode == USB_PERIPHERAL) {
506 ulpi_val = ULPI_INT_SESS_VALID;
507 val |= OTGSC_BSVIE;
508 }
509 writel_relaxed(val, USB_OTGSC);
510 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
511 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530512 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530513 ulpi_write(otg, OTG_COMP_DISABLE,
514 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530515 /* Enable PMIC pull-up */
516 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530517 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530519 return 0;
520}
521
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530522static const char *timer_string(int bit)
523{
524 switch (bit) {
525 case A_WAIT_VRISE: return "a_wait_vrise";
526 case A_WAIT_VFALL: return "a_wait_vfall";
527 case B_SRP_FAIL: return "b_srp_fail";
528 case A_WAIT_BCON: return "a_wait_bcon";
529 case A_AIDL_BDIS: return "a_aidl_bdis";
530 case A_BIDL_ADIS: return "a_bidl_adis";
531 case B_ASE0_BRST: return "b_ase0_brst";
532 case A_TST_MAINT: return "a_tst_maint";
533 case B_TST_SRP: return "b_tst_srp";
534 case B_TST_CONFIG: return "b_tst_config";
535 default: return "UNDEFINED";
536 }
537}
538
539static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
540{
541 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
542
543 switch (motg->active_tmout) {
544 case A_WAIT_VRISE:
545 /* TODO: use vbus_vld interrupt */
546 set_bit(A_VBUS_VLD, &motg->inputs);
547 break;
548 case A_TST_MAINT:
549 /* OTG PET: End session after TA_TST_MAINT */
550 set_bit(A_BUS_DROP, &motg->inputs);
551 break;
552 case B_TST_SRP:
553 /*
554 * OTG PET: Initiate SRP after TB_TST_SRP of
555 * previous session end.
556 */
557 set_bit(B_BUS_REQ, &motg->inputs);
558 break;
559 case B_TST_CONFIG:
560 clear_bit(A_CONN, &motg->inputs);
561 break;
562 default:
563 set_bit(motg->active_tmout, &motg->tmouts);
564 }
565
566 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
567 queue_work(system_nrt_wq, &motg->sm_work);
568 return HRTIMER_NORESTART;
569}
570
571static void msm_otg_del_timer(struct msm_otg *motg)
572{
573 int bit = motg->active_tmout;
574
575 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
576 div_s64(ktime_to_us(hrtimer_get_remaining(
577 &motg->timer)), 1000));
578 hrtimer_cancel(&motg->timer);
579 clear_bit(bit, &motg->tmouts);
580}
581
582static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
583{
584 clear_bit(bit, &motg->tmouts);
585 motg->active_tmout = bit;
586 pr_debug("starting %s timer\n", timer_string(bit));
587 hrtimer_start(&motg->timer,
588 ktime_set(time / 1000, (time % 1000) * 1000000),
589 HRTIMER_MODE_REL);
590}
591
592static void msm_otg_init_timer(struct msm_otg *motg)
593{
594 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
595 motg->timer.function = msm_otg_timer_func;
596}
597
598static int msm_otg_start_hnp(struct otg_transceiver *otg)
599{
600 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
601
602 if (otg->state != OTG_STATE_A_HOST) {
603 pr_err("HNP can not be initiated in %s state\n",
604 otg_state_string(otg->state));
605 return -EINVAL;
606 }
607
608 pr_debug("A-Host: HNP initiated\n");
609 clear_bit(A_BUS_REQ, &motg->inputs);
610 queue_work(system_nrt_wq, &motg->sm_work);
611 return 0;
612}
613
614static int msm_otg_start_srp(struct otg_transceiver *otg)
615{
616 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
617 u32 val;
618 int ret = 0;
619
620 if (otg->state != OTG_STATE_B_IDLE) {
621 pr_err("SRP can not be initiated in %s state\n",
622 otg_state_string(otg->state));
623 ret = -EINVAL;
624 goto out;
625 }
626
627 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
628 pr_debug("initial conditions of SRP are not met. Try again"
629 "after some time\n");
630 ret = -EAGAIN;
631 goto out;
632 }
633
634 pr_debug("B-Device SRP started\n");
635
636 /*
637 * PHY won't pull D+ high unless it detects Vbus valid.
638 * Since by definition, SRP is only done when Vbus is not valid,
639 * software work-around needs to be used to spoof the PHY into
640 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
641 * VBUSVLDEXT register bits.
642 */
643 ulpi_write(otg, 0x03, 0x97);
644 /*
645 * Harware auto assist data pulsing: Data pulse is given
646 * for 7msec; wait for vbus
647 */
648 val = readl_relaxed(USB_OTGSC);
649 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
650
651 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
652out:
653 return ret;
654}
655
656static void msm_otg_host_hnp_enable(struct otg_transceiver *otg, bool enable)
657{
658 struct usb_hcd *hcd = bus_to_hcd(otg->host);
659 struct usb_device *rhub = otg->host->root_hub;
660
661 if (enable) {
662 pm_runtime_disable(&rhub->dev);
663 rhub->state = USB_STATE_NOTATTACHED;
664 hcd->driver->bus_suspend(hcd);
665 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
666 } else {
667 usb_remove_hcd(hcd);
668 msm_otg_reset(otg);
669 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
670 }
671}
672
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530673static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
674{
675 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
676
Amit Blay6fa647a2012-05-24 14:12:08 +0300677 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530678 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530679
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530680 if (suspend) {
681 switch (otg->state) {
682 case OTG_STATE_A_WAIT_BCON:
683 if (TA_WAIT_BCON > 0)
684 break;
685 /* fall through */
686 case OTG_STATE_A_HOST:
687 pr_debug("host bus suspend\n");
688 clear_bit(A_BUS_REQ, &motg->inputs);
689 queue_work(system_nrt_wq, &motg->sm_work);
690 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300691 case OTG_STATE_B_PERIPHERAL:
692 pr_debug("peripheral bus suspend\n");
693 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
694 break;
695 set_bit(A_BUS_SUSPEND, &motg->inputs);
696 queue_work(system_nrt_wq, &motg->sm_work);
697 break;
698
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530699 default:
700 break;
701 }
702 } else {
703 switch (otg->state) {
704 case OTG_STATE_A_SUSPEND:
705 /* Remote wakeup or resume */
706 set_bit(A_BUS_REQ, &motg->inputs);
707 otg->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700708
709 /* ensure hardware is not in low power mode */
710 pm_runtime_resume(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530711 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300712 case OTG_STATE_B_PERIPHERAL:
713 pr_debug("peripheral bus resume\n");
714 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
715 break;
716 clear_bit(A_BUS_SUSPEND, &motg->inputs);
717 queue_work(system_nrt_wq, &motg->sm_work);
718 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530719 default:
720 break;
721 }
722 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530723 return 0;
724}
725
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530726#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530727#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
728
729#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530730static int msm_otg_suspend(struct msm_otg *motg)
731{
732 struct otg_transceiver *otg = &motg->otg;
733 struct usb_bus *bus = otg->host;
734 struct msm_otg_platform_data *pdata = motg->pdata;
735 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300736 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530737 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800738 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530739 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530740
741 if (atomic_read(&motg->in_lpm))
742 return 0;
743
744 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530745 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +0300746 device_bus_suspend = otg->gadget && test_bit(ID, &motg->inputs) &&
747 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
748 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530749 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530750 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530751 * Chipidea 45-nm PHY suspend sequence:
752 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530753 * Interrupt Latch Register auto-clear feature is not present
754 * in all PHY versions. Latch register is clear on read type.
755 * Clear latch register to avoid spurious wakeup from
756 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530757 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530758 * PHY comparators are disabled when PHY enters into low power
759 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
760 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
761 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530762 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763 * PLL is not turned off when PHY enters into low power mode (LPM).
764 * Disable PLL for maximum power savings.
765 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530766
767 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
768 ulpi_read(otg, 0x14);
769 if (pdata->otg_control == OTG_PHY_CONTROL)
770 ulpi_write(otg, 0x01, 0x30);
771 ulpi_write(otg, 0x08, 0x09);
772 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530773
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700774
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530775 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530776 * PHY may take some time or even fail to enter into low power
777 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
778 * in failure case.
779 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530780 portsc = readl_relaxed(USB_PORTSC);
781 if (!(portsc & PORTSC_PHCD)) {
782 writel_relaxed(portsc | PORTSC_PHCD,
783 USB_PORTSC);
784 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
785 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
786 break;
787 udelay(1);
788 cnt++;
789 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530790 }
791
792 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
793 dev_err(otg->dev, "Unable to suspend PHY\n");
794 msm_otg_reset(otg);
795 enable_irq(motg->irq);
796 return -ETIMEDOUT;
797 }
798
799 /*
800 * PHY has capability to generate interrupt asynchronously in low
801 * power mode (LPM). This interrupt is level triggered. So USB IRQ
802 * line must be disabled till async interrupt enable bit is cleared
803 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
804 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530805 *
806 * PHY retention mode is disallowed while entering to LPM with wall
807 * charger connected. But PHY is put into suspend mode. Hence
808 * enable asynchronous interrupt to detect charger disconnection when
809 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530810 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530811 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300812 if (host_bus_suspend || device_bus_suspend ||
813 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530814 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
815 else
816 cmd_val |= ULPI_STP_CTRL;
817 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530818
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530819 /*
820 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
821 * PHY retention and collapse can not happen with VDP_SRC enabled.
822 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300823 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
824 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200825 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
826 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
827 /* Enable PHY HV interrupts to wake MPM/Link */
828 phy_ctrl_val |=
829 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
830
831 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700832 motg->lpm_flags |= PHY_RETENTIONED;
833 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530834
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 /* Ensure that above operation is completed before turning off clocks */
836 mb();
Manu Gautam28b1bac2012-01-30 16:43:06 +0530837 clk_disable_unprepare(motg->pclk);
838 clk_disable_unprepare(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530839
Anji jonnala7da3f262011-12-02 17:22:14 -0800840 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530841 if (!host_bus_suspend) {
842 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
843 if (ret)
844 dev_err(otg->dev, "%s failed to devote for "
845 "TCXO D0 buffer%d\n", __func__, ret);
846 else
847 motg->lpm_flags |= XO_SHUTDOWN;
848 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800849
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530850 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
851 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 msm_hsusb_ldo_enable(motg, 0);
853 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530854 }
855
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530856 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700857 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530858 msm_hsusb_mhl_switch_enable(motg, 0);
859 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700860
861 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530862 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700863 if (motg->pdata->pmic_id_irq)
864 enable_irq_wake(motg->pdata->pmic_id_irq);
865 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530866 if (bus)
867 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
868
869 atomic_set(&motg->in_lpm, 1);
870 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530872
873 dev_info(otg->dev, "USB in low power mode\n");
874
875 return 0;
876}
877
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530878static int msm_otg_resume(struct msm_otg *motg)
879{
880 struct otg_transceiver *otg = &motg->otg;
881 struct usb_bus *bus = otg->host;
882 int cnt = 0;
883 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200884 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800885 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530886
887 if (!atomic_read(&motg->in_lpm))
888 return 0;
889
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700890 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800891
892 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530893 if (motg->lpm_flags & XO_SHUTDOWN) {
894 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
895 if (ret)
896 dev_err(otg->dev, "%s failed to vote for "
897 "TCXO D0 buffer%d\n", __func__, ret);
898 motg->lpm_flags &= ~XO_SHUTDOWN;
899 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800900
Manu Gautam28b1bac2012-01-30 16:43:06 +0530901 clk_prepare_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200902
Manu Gautam28b1bac2012-01-30 16:43:06 +0530903 clk_prepare_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530904
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
906 msm_hsusb_ldo_enable(motg, 1);
907 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
908 }
909
910 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530911 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530912 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200913 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
914 phy_ctrl_val |= PHY_RETEN;
915 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
916 /* Disable PHY HV interrupts */
917 phy_ctrl_val &=
918 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
919 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700920 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530921 }
922
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530923 temp = readl(USB_USBCMD);
924 temp &= ~ASYNC_INTR_CTRL;
925 temp &= ~ULPI_STP_CTRL;
926 writel(temp, USB_USBCMD);
927
928 /*
929 * PHY comes out of low power mode (LPM) in case of wakeup
930 * from asynchronous interrupt.
931 */
932 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
933 goto skip_phy_resume;
934
935 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
936 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
937 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
938 break;
939 udelay(1);
940 cnt++;
941 }
942
943 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
944 /*
945 * This is a fatal error. Reset the link and
946 * PHY. USB state can not be restored. Re-insertion
947 * of USB cable is the only way to get USB working.
948 */
949 dev_err(otg->dev, "Unable to resume USB."
950 "Re-plugin the cable\n");
951 msm_otg_reset(otg);
952 }
953
954skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700955 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530956 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957 if (motg->pdata->pmic_id_irq)
958 disable_irq_wake(motg->pdata->pmic_id_irq);
959 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530960 if (bus)
961 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
962
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530963 atomic_set(&motg->in_lpm, 0);
964
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530965 if (motg->async_int) {
966 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530967 enable_irq(motg->irq);
968 }
969
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530970 dev_info(otg->dev, "USB exited from low power mode\n");
971
972 return 0;
973}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530974#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530975
David Keitel081a3e22012-04-18 12:37:07 -0700976static int msm_otg_notify_chg_type(struct msm_otg *motg)
977{
978 static int charger_type;
979 /*
980 * TODO
981 * Unify OTG driver charger types and power supply charger types
982 */
983 if (charger_type == motg->chg_type)
984 return 0;
985
986 if (motg->chg_type == USB_SDP_CHARGER)
987 charger_type = POWER_SUPPLY_TYPE_USB;
988 else if (motg->chg_type == USB_CDP_CHARGER)
989 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
990 else if (motg->chg_type == USB_DCP_CHARGER)
991 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
992 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
993 motg->chg_type == USB_ACA_A_CHARGER ||
994 motg->chg_type == USB_ACA_B_CHARGER ||
995 motg->chg_type == USB_ACA_C_CHARGER))
996 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
997 else
998 charger_type = POWER_SUPPLY_TYPE_BATTERY;
999
1000 return pm8921_set_usb_power_supply_type(charger_type);
1001}
1002
Amit Blay0f7edf72012-01-15 10:11:27 +02001003static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1004{
Amit Blay0f7edf72012-01-15 10:11:27 +02001005
Amit Blay0f7edf72012-01-15 10:11:27 +02001006 if (!psy)
1007 goto psy_not_supported;
1008
1009 if (motg->cur_power == 0 && mA > 0) {
1010 /* Enable charging */
1011 if (power_supply_set_online(psy, true))
1012 goto psy_not_supported;
1013 } else if (motg->cur_power > 0 && mA == 0) {
1014 /* Disable charging */
1015 if (power_supply_set_online(psy, false))
1016 goto psy_not_supported;
1017 return 0;
1018 }
1019 /* Set max current limit */
1020 if (power_supply_set_current_limit(psy, 1000*mA))
1021 goto psy_not_supported;
1022
1023 return 0;
1024
1025psy_not_supported:
1026 dev_dbg(motg->otg.dev, "Power Supply doesn't support USB charger\n");
1027 return -ENXIO;
1028}
1029
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301030static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1031{
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301032 struct usb_gadget *g = motg->otg.gadget;
1033
1034 if (g && g->is_a_peripheral)
1035 return;
1036
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301037 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1038 motg->chg_type == USB_ACA_A_CHARGER ||
1039 motg->chg_type == USB_ACA_B_CHARGER ||
1040 motg->chg_type == USB_ACA_C_CHARGER) &&
1041 mA > IDEV_ACA_CHG_LIMIT)
1042 mA = IDEV_ACA_CHG_LIMIT;
1043
David Keitel081a3e22012-04-18 12:37:07 -07001044 if (msm_otg_notify_chg_type(motg))
1045 dev_err(motg->otg.dev,
1046 "Failed notifying %d charger type to PMIC\n",
1047 motg->chg_type);
1048
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301049 if (motg->cur_power == mA)
1050 return;
1051
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301052 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001053
1054 /*
1055 * Use Power Supply API if supported, otherwise fallback
1056 * to legacy pm8921 API.
1057 */
1058 if (msm_otg_notify_power_supply(motg, mA))
1059 pm8921_charger_vbus_draw(mA);
1060
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301061 motg->cur_power = mA;
1062}
1063
1064static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
1065{
1066 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1067
1068 /*
1069 * Gadget driver uses set_power method to notify about the
1070 * available current based on suspend/configured states.
1071 *
1072 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1073 * states when CDP/ACA is connected.
1074 */
1075 if (motg->chg_type == USB_SDP_CHARGER)
1076 msm_otg_notify_charger(motg, mA);
1077
1078 return 0;
1079}
1080
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301081static void msm_otg_start_host(struct otg_transceiver *otg, int on)
1082{
1083 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1084 struct msm_otg_platform_data *pdata = motg->pdata;
1085 struct usb_hcd *hcd;
1086
1087 if (!otg->host)
1088 return;
1089
1090 hcd = bus_to_hcd(otg->host);
1091
1092 if (on) {
1093 dev_dbg(otg->dev, "host on\n");
1094
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301095 if (pdata->otg_control == OTG_PHY_CONTROL)
1096 ulpi_write(otg, OTG_COMP_DISABLE,
1097 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1098
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301099 /*
1100 * Some boards have a switch cotrolled by gpio
1101 * to enable/disable internal HUB. Enable internal
1102 * HUB before kicking the host.
1103 */
1104 if (pdata->setup_gpio)
1105 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301106 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301107 } else {
1108 dev_dbg(otg->dev, "host off\n");
1109
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301110 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301111 /* HCD core reset all bits of PORTSC. select ULPI phy */
1112 writel_relaxed(0x80000000, USB_PORTSC);
1113
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301114 if (pdata->setup_gpio)
1115 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301116
1117 if (pdata->otg_control == OTG_PHY_CONTROL)
1118 ulpi_write(otg, OTG_COMP_DISABLE,
1119 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301120 }
1121}
1122
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001123static int msm_otg_usbdev_notify(struct notifier_block *self,
1124 unsigned long action, void *priv)
1125{
1126 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301127 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301128 struct usb_device *udev = priv;
1129
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301130 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1131 goto out;
1132
1133 if (udev->bus != motg->otg.host)
1134 goto out;
1135 /*
1136 * Interested in devices connected directly to the root hub.
1137 * ACA dock can supply IDEV_CHG irrespective devices connected
1138 * on the accessory port.
1139 */
1140 if (!udev->parent || udev->parent->parent ||
1141 motg->chg_type == USB_ACA_DOCK_CHARGER)
1142 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143
1144 switch (action) {
1145 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301146 if (aca_enabled())
1147 usb_disable_autosuspend(udev);
1148 if (otg->state == OTG_STATE_A_WAIT_BCON) {
1149 pr_debug("B_CONN set\n");
1150 set_bit(B_CONN, &motg->inputs);
1151 msm_otg_del_timer(motg);
1152 otg->state = OTG_STATE_A_HOST;
1153 /*
1154 * OTG PET: A-device must end session within
1155 * 10 sec after PET enumeration.
1156 */
1157 if (udev->quirks & USB_QUIRK_OTG_PET)
1158 msm_otg_start_timer(motg, TA_TST_MAINT,
1159 A_TST_MAINT);
1160 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301161 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001163 if (udev->actconfig)
1164 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1165 else
1166 motg->mA_port = IUNIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301167 if (otg->state == OTG_STATE_B_HOST)
1168 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301169 break;
1170 case USB_DEVICE_REMOVE:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301171 if ((otg->state == OTG_STATE_A_HOST) ||
1172 (otg->state == OTG_STATE_A_SUSPEND)) {
1173 pr_debug("B_CONN clear\n");
1174 clear_bit(B_CONN, &motg->inputs);
1175 /*
1176 * OTG PET: A-device must end session after
1177 * PET disconnection if it is enumerated
1178 * with bcdDevice[0] = 1. USB core sets
1179 * bus->otg_vbus_off for us. clear it here.
1180 */
1181 if (udev->bus->otg_vbus_off) {
1182 udev->bus->otg_vbus_off = 0;
1183 set_bit(A_BUS_DROP, &motg->inputs);
1184 }
1185 queue_work(system_nrt_wq, &motg->sm_work);
1186 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187 default:
1188 break;
1189 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301190 if (test_bit(ID_A, &motg->inputs))
1191 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1192 motg->mA_port);
1193out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194 return NOTIFY_OK;
1195}
1196
Mayank Ranae3926882011-12-26 09:47:54 +05301197static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1198{
1199 int ret;
1200 static bool vbus_is_on;
1201
1202 if (vbus_is_on == on)
1203 return;
1204
1205 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301206 ret = motg->pdata->vbus_power(on);
1207 if (!ret)
1208 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301209 return;
1210 }
1211
1212 if (!vbus_otg) {
1213 pr_err("vbus_otg is NULL.");
1214 return;
1215 }
1216
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001217 /*
1218 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001219 * from usb before turning on the boost.
1220 * if exiting host mode disable the boost before enabling to draw
1221 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001222 */
Mayank Ranae3926882011-12-26 09:47:54 +05301223 if (on) {
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001224 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301225 ret = regulator_enable(vbus_otg);
1226 if (ret) {
1227 pr_err("unable to enable vbus_otg\n");
1228 return;
1229 }
1230 vbus_is_on = true;
1231 } else {
1232 ret = regulator_disable(vbus_otg);
1233 if (ret) {
1234 pr_err("unable to disable vbus_otg\n");
1235 return;
1236 }
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001237 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301238 vbus_is_on = false;
1239 }
1240}
1241
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301242static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1243{
1244 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1245 struct usb_hcd *hcd;
1246
1247 /*
1248 * Fail host registration if this board can support
1249 * only peripheral configuration.
1250 */
1251 if (motg->pdata->mode == USB_PERIPHERAL) {
1252 dev_info(otg->dev, "Host mode is not supported\n");
1253 return -ENODEV;
1254 }
1255
Mayank Ranae3926882011-12-26 09:47:54 +05301256 if (!motg->pdata->vbus_power && host) {
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05301257 vbus_otg = devm_regulator_get(motg->otg.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301258 if (IS_ERR(vbus_otg)) {
1259 pr_err("Unable to get vbus_otg\n");
1260 return -ENODEV;
1261 }
1262 }
1263
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301264 if (!host) {
1265 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301266 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001267 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301268 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301269 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301270 otg->host = NULL;
1271 otg->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301272 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301273 } else {
1274 otg->host = NULL;
1275 }
1276
1277 return 0;
1278 }
1279
1280 hcd = bus_to_hcd(host);
1281 hcd->power_budget = motg->pdata->power_budget;
1282
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301283#ifdef CONFIG_USB_OTG
1284 host->otg_port = 1;
1285#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1287 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301288 otg->host = host;
1289 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1290
1291 /*
1292 * Kick the state machine work, if peripheral is not supported
1293 * or peripheral is already registered with us.
1294 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301295 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1296 pm_runtime_get_sync(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301297 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301298 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301299
1300 return 0;
1301}
1302
1303static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1304{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301305 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301306 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1307 struct msm_otg_platform_data *pdata = motg->pdata;
1308
1309 if (!otg->gadget)
1310 return;
1311
1312 if (on) {
1313 dev_dbg(otg->dev, "gadget on\n");
1314 /*
1315 * Some boards have a switch cotrolled by gpio
1316 * to enable/disable internal HUB. Disable internal
1317 * HUB before kicking the gadget.
1318 */
1319 if (pdata->setup_gpio)
1320 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001321
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301322 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301323 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301324 ret = msm_bus_scale_client_update_request(
1325 motg->bus_perf_client, 1);
1326 if (ret)
1327 dev_err(motg->otg.dev, "%s: Failed to vote for "
1328 "bus bandwidth %d\n", __func__, ret);
1329 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301330 usb_gadget_vbus_connect(otg->gadget);
1331 } else {
1332 dev_dbg(otg->dev, "gadget off\n");
1333 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301334 /* Configure BUS performance parameters to default */
1335 if (motg->bus_perf_client) {
1336 ret = msm_bus_scale_client_update_request(
1337 motg->bus_perf_client, 0);
1338 if (ret)
1339 dev_err(motg->otg.dev, "%s: Failed to devote "
1340 "for bus bw %d\n", __func__, ret);
1341 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301342 if (pdata->setup_gpio)
1343 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1344 }
1345
1346}
1347
1348static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1349 struct usb_gadget *gadget)
1350{
1351 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1352
1353 /*
1354 * Fail peripheral registration if this board can support
1355 * only host configuration.
1356 */
1357 if (motg->pdata->mode == USB_HOST) {
1358 dev_info(otg->dev, "Peripheral mode is not supported\n");
1359 return -ENODEV;
1360 }
1361
1362 if (!gadget) {
1363 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301364 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301365 msm_otg_start_peripheral(otg, 0);
1366 otg->gadget = NULL;
1367 otg->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301368 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301369 } else {
1370 otg->gadget = NULL;
1371 }
1372
1373 return 0;
1374 }
1375 otg->gadget = gadget;
1376 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1377
1378 /*
1379 * Kick the state machine work, if host is not supported
1380 * or host is already registered with us.
1381 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301382 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1383 pm_runtime_get_sync(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301384 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301385 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301386
1387 return 0;
1388}
1389
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001390static bool msm_chg_aca_detect(struct msm_otg *motg)
1391{
1392 struct otg_transceiver *otg = &motg->otg;
1393 u32 int_sts;
1394 bool ret = false;
1395
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301396 if (!aca_enabled())
1397 goto out;
1398
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001399 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1400 goto out;
1401
1402 int_sts = ulpi_read(otg, 0x87);
1403 switch (int_sts & 0x1C) {
1404 case 0x08:
1405 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1406 dev_dbg(otg->dev, "ID_A\n");
1407 motg->chg_type = USB_ACA_A_CHARGER;
1408 motg->chg_state = USB_CHG_STATE_DETECTED;
1409 clear_bit(ID_B, &motg->inputs);
1410 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301411 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001412 ret = true;
1413 }
1414 break;
1415 case 0x0C:
1416 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1417 dev_dbg(otg->dev, "ID_B\n");
1418 motg->chg_type = USB_ACA_B_CHARGER;
1419 motg->chg_state = USB_CHG_STATE_DETECTED;
1420 clear_bit(ID_A, &motg->inputs);
1421 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301422 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001423 ret = true;
1424 }
1425 break;
1426 case 0x10:
1427 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1428 dev_dbg(otg->dev, "ID_C\n");
1429 motg->chg_type = USB_ACA_C_CHARGER;
1430 motg->chg_state = USB_CHG_STATE_DETECTED;
1431 clear_bit(ID_A, &motg->inputs);
1432 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301433 set_bit(ID, &motg->inputs);
1434 ret = true;
1435 }
1436 break;
1437 case 0x04:
1438 if (test_and_clear_bit(ID, &motg->inputs)) {
1439 dev_dbg(otg->dev, "ID_GND\n");
1440 motg->chg_type = USB_INVALID_CHARGER;
1441 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1442 clear_bit(ID_A, &motg->inputs);
1443 clear_bit(ID_B, &motg->inputs);
1444 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445 ret = true;
1446 }
1447 break;
1448 default:
1449 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1450 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301451 test_and_clear_bit(ID_C, &motg->inputs) |
1452 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001453 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301454 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001455 motg->chg_type = USB_INVALID_CHARGER;
1456 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1457 }
1458 }
1459out:
1460 return ret;
1461}
1462
1463static void msm_chg_enable_aca_det(struct msm_otg *motg)
1464{
1465 struct otg_transceiver *otg = &motg->otg;
1466
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301467 if (!aca_enabled())
1468 return;
1469
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001470 switch (motg->pdata->phy_type) {
1471 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301472 /* Disable ID_GND in link and PHY */
1473 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1474 OTGSC_IDIE), USB_OTGSC);
1475 ulpi_write(otg, 0x01, 0x0C);
1476 ulpi_write(otg, 0x10, 0x0F);
1477 ulpi_write(otg, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301478 /* Disable PMIC ID pull-up */
1479 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301480 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001481 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301482 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001483 break;
1484 default:
1485 break;
1486 }
1487}
1488
1489static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1490{
1491 struct otg_transceiver *otg = &motg->otg;
1492
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301493 if (!aca_enabled())
1494 return;
1495
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001496 switch (motg->pdata->phy_type) {
1497 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301498 /* Enable ACA Detection interrupt (on any RID change) */
1499 ulpi_write(otg, 0x01, 0x94);
1500 break;
1501 default:
1502 break;
1503 }
1504}
1505
1506static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1507{
1508 struct otg_transceiver *otg = &motg->otg;
1509
1510 if (!aca_enabled())
1511 return;
1512
1513 switch (motg->pdata->phy_type) {
1514 case SNPS_28NM_INTEGRATED_PHY:
1515 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001516 break;
1517 default:
1518 break;
1519 }
1520}
1521
1522static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1523{
1524 struct otg_transceiver *otg = &motg->otg;
1525 bool ret = false;
1526
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301527 if (!aca_enabled())
1528 return ret;
1529
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001530 switch (motg->pdata->phy_type) {
1531 case SNPS_28NM_INTEGRATED_PHY:
1532 if (ulpi_read(otg, 0x91) & 1) {
1533 dev_dbg(otg->dev, "RID change\n");
1534 ulpi_write(otg, 0x01, 0x92);
1535 ret = msm_chg_aca_detect(motg);
1536 }
1537 default:
1538 break;
1539 }
1540 return ret;
1541}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301542
1543static void msm_otg_id_timer_func(unsigned long data)
1544{
1545 struct msm_otg *motg = (struct msm_otg *) data;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301546 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301547
1548 if (!aca_enabled())
1549 return;
1550
1551 if (atomic_read(&motg->in_lpm)) {
1552 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1553 return;
1554 }
1555
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301556 if (otg->state == OTG_STATE_A_SUSPEND)
1557 goto out;
1558
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301559 if (msm_chg_check_aca_intr(motg)) {
1560 dev_dbg(motg->otg.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301561 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301562 }
1563
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301564out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301565 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1566 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1567}
1568
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301569static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1570{
1571 struct otg_transceiver *otg = &motg->otg;
1572 u32 chg_det;
1573 bool ret = false;
1574
1575 switch (motg->pdata->phy_type) {
1576 case CI_45NM_INTEGRATED_PHY:
1577 chg_det = ulpi_read(otg, 0x34);
1578 ret = chg_det & (1 << 4);
1579 break;
1580 case SNPS_28NM_INTEGRATED_PHY:
1581 chg_det = ulpi_read(otg, 0x87);
1582 ret = chg_det & 1;
1583 break;
1584 default:
1585 break;
1586 }
1587 return ret;
1588}
1589
1590static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1591{
1592 struct otg_transceiver *otg = &motg->otg;
1593 u32 chg_det;
1594
1595 switch (motg->pdata->phy_type) {
1596 case CI_45NM_INTEGRATED_PHY:
1597 chg_det = ulpi_read(otg, 0x34);
1598 /* Turn off charger block */
1599 chg_det |= ~(1 << 1);
1600 ulpi_write(otg, chg_det, 0x34);
1601 udelay(20);
1602 /* control chg block via ULPI */
1603 chg_det &= ~(1 << 3);
1604 ulpi_write(otg, chg_det, 0x34);
1605 /* put it in host mode for enabling D- source */
1606 chg_det &= ~(1 << 2);
1607 ulpi_write(otg, chg_det, 0x34);
1608 /* Turn on chg detect block */
1609 chg_det &= ~(1 << 1);
1610 ulpi_write(otg, chg_det, 0x34);
1611 udelay(20);
1612 /* enable chg detection */
1613 chg_det &= ~(1 << 0);
1614 ulpi_write(otg, chg_det, 0x34);
1615 break;
1616 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301617 /* Turn off VDP_SRC */
1618 ulpi_write(otg, 0x3, 0x86);
1619 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301620 /*
1621 * Configure DM as current source, DP as current sink
1622 * and enable battery charging comparators.
1623 */
1624 ulpi_write(otg, 0x8, 0x85);
1625 ulpi_write(otg, 0x2, 0x85);
1626 ulpi_write(otg, 0x1, 0x85);
1627 break;
1628 default:
1629 break;
1630 }
1631}
1632
1633static bool msm_chg_check_primary_det(struct msm_otg *motg)
1634{
1635 struct otg_transceiver *otg = &motg->otg;
1636 u32 chg_det;
1637 bool ret = false;
1638
1639 switch (motg->pdata->phy_type) {
1640 case CI_45NM_INTEGRATED_PHY:
1641 chg_det = ulpi_read(otg, 0x34);
1642 ret = chg_det & (1 << 4);
1643 break;
1644 case SNPS_28NM_INTEGRATED_PHY:
1645 chg_det = ulpi_read(otg, 0x87);
1646 ret = chg_det & 1;
1647 break;
1648 default:
1649 break;
1650 }
1651 return ret;
1652}
1653
1654static void msm_chg_enable_primary_det(struct msm_otg *motg)
1655{
1656 struct otg_transceiver *otg = &motg->otg;
1657 u32 chg_det;
1658
1659 switch (motg->pdata->phy_type) {
1660 case CI_45NM_INTEGRATED_PHY:
1661 chg_det = ulpi_read(otg, 0x34);
1662 /* enable chg detection */
1663 chg_det &= ~(1 << 0);
1664 ulpi_write(otg, chg_det, 0x34);
1665 break;
1666 case SNPS_28NM_INTEGRATED_PHY:
1667 /*
1668 * Configure DP as current source, DM as current sink
1669 * and enable battery charging comparators.
1670 */
1671 ulpi_write(otg, 0x2, 0x85);
1672 ulpi_write(otg, 0x1, 0x85);
1673 break;
1674 default:
1675 break;
1676 }
1677}
1678
1679static bool msm_chg_check_dcd(struct msm_otg *motg)
1680{
1681 struct otg_transceiver *otg = &motg->otg;
1682 u32 line_state;
1683 bool ret = false;
1684
1685 switch (motg->pdata->phy_type) {
1686 case CI_45NM_INTEGRATED_PHY:
1687 line_state = ulpi_read(otg, 0x15);
1688 ret = !(line_state & 1);
1689 break;
1690 case SNPS_28NM_INTEGRATED_PHY:
1691 line_state = ulpi_read(otg, 0x87);
1692 ret = line_state & 2;
1693 break;
1694 default:
1695 break;
1696 }
1697 return ret;
1698}
1699
1700static void msm_chg_disable_dcd(struct msm_otg *motg)
1701{
1702 struct otg_transceiver *otg = &motg->otg;
1703 u32 chg_det;
1704
1705 switch (motg->pdata->phy_type) {
1706 case CI_45NM_INTEGRATED_PHY:
1707 chg_det = ulpi_read(otg, 0x34);
1708 chg_det &= ~(1 << 5);
1709 ulpi_write(otg, chg_det, 0x34);
1710 break;
1711 case SNPS_28NM_INTEGRATED_PHY:
1712 ulpi_write(otg, 0x10, 0x86);
1713 break;
1714 default:
1715 break;
1716 }
1717}
1718
1719static void msm_chg_enable_dcd(struct msm_otg *motg)
1720{
1721 struct otg_transceiver *otg = &motg->otg;
1722 u32 chg_det;
1723
1724 switch (motg->pdata->phy_type) {
1725 case CI_45NM_INTEGRATED_PHY:
1726 chg_det = ulpi_read(otg, 0x34);
1727 /* Turn on D+ current source */
1728 chg_det |= (1 << 5);
1729 ulpi_write(otg, chg_det, 0x34);
1730 break;
1731 case SNPS_28NM_INTEGRATED_PHY:
1732 /* Data contact detection enable */
1733 ulpi_write(otg, 0x10, 0x85);
1734 break;
1735 default:
1736 break;
1737 }
1738}
1739
1740static void msm_chg_block_on(struct msm_otg *motg)
1741{
1742 struct otg_transceiver *otg = &motg->otg;
1743 u32 func_ctrl, chg_det;
1744
1745 /* put the controller in non-driving mode */
1746 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1747 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1748 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1749 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1750
1751 switch (motg->pdata->phy_type) {
1752 case CI_45NM_INTEGRATED_PHY:
1753 chg_det = ulpi_read(otg, 0x34);
1754 /* control chg block via ULPI */
1755 chg_det &= ~(1 << 3);
1756 ulpi_write(otg, chg_det, 0x34);
1757 /* Turn on chg detect block */
1758 chg_det &= ~(1 << 1);
1759 ulpi_write(otg, chg_det, 0x34);
1760 udelay(20);
1761 break;
1762 case SNPS_28NM_INTEGRATED_PHY:
1763 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301764 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301765 /* Clear alt interrupt latch and enable bits */
1766 ulpi_write(otg, 0x1F, 0x92);
1767 ulpi_write(otg, 0x1F, 0x95);
1768 udelay(100);
1769 break;
1770 default:
1771 break;
1772 }
1773}
1774
1775static void msm_chg_block_off(struct msm_otg *motg)
1776{
1777 struct otg_transceiver *otg = &motg->otg;
1778 u32 func_ctrl, chg_det;
1779
1780 switch (motg->pdata->phy_type) {
1781 case CI_45NM_INTEGRATED_PHY:
1782 chg_det = ulpi_read(otg, 0x34);
1783 /* Turn off charger block */
1784 chg_det |= ~(1 << 1);
1785 ulpi_write(otg, chg_det, 0x34);
1786 break;
1787 case SNPS_28NM_INTEGRATED_PHY:
1788 /* Clear charger detecting control bits */
1789 ulpi_write(otg, 0x3F, 0x86);
1790 /* Clear alt interrupt latch and enable bits */
1791 ulpi_write(otg, 0x1F, 0x92);
1792 ulpi_write(otg, 0x1F, 0x95);
1793 break;
1794 default:
1795 break;
1796 }
1797
1798 /* put the controller in normal mode */
1799 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1800 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1801 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1802 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1803}
1804
Anji jonnalad270e2d2011-08-09 11:28:32 +05301805static const char *chg_to_string(enum usb_chg_type chg_type)
1806{
1807 switch (chg_type) {
1808 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1809 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1810 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1811 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1812 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1813 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1814 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1815 default: return "INVALID_CHARGER";
1816 }
1817}
1818
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301819#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1820#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301821#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1822#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301823static void msm_chg_detect_work(struct work_struct *w)
1824{
1825 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1826 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301827 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301828 unsigned long delay;
1829
1830 dev_dbg(otg->dev, "chg detection work\n");
1831 switch (motg->chg_state) {
1832 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301833 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301834 if (motg->pdata->enable_dcd)
1835 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001836 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301837 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1838 motg->dcd_retries = 0;
1839 delay = MSM_CHG_DCD_POLL_TIME;
1840 break;
1841 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 is_aca = msm_chg_aca_detect(motg);
1843 if (is_aca) {
1844 /*
1845 * ID_A can be ACA dock too. continue
1846 * primary detection after DCD.
1847 */
1848 if (test_bit(ID_A, &motg->inputs)) {
1849 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1850 } else {
1851 delay = 0;
1852 break;
1853 }
1854 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301855 if (motg->pdata->enable_dcd)
1856 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301857 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1858 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301859 if (motg->pdata->enable_dcd)
1860 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301861 msm_chg_enable_primary_det(motg);
1862 delay = MSM_CHG_PRIMARY_DET_TIME;
1863 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1864 } else {
1865 delay = MSM_CHG_DCD_POLL_TIME;
1866 }
1867 break;
1868 case USB_CHG_STATE_DCD_DONE:
1869 vout = msm_chg_check_primary_det(motg);
1870 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301871 if (test_bit(ID_A, &motg->inputs)) {
1872 motg->chg_type = USB_ACA_DOCK_CHARGER;
1873 motg->chg_state = USB_CHG_STATE_DETECTED;
1874 delay = 0;
1875 break;
1876 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301877 msm_chg_enable_secondary_det(motg);
1878 delay = MSM_CHG_SECONDARY_DET_TIME;
1879 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1880 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301881 if (test_bit(ID_A, &motg->inputs)) {
1882 motg->chg_type = USB_ACA_A_CHARGER;
1883 motg->chg_state = USB_CHG_STATE_DETECTED;
1884 delay = 0;
1885 break;
1886 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301887 motg->chg_type = USB_SDP_CHARGER;
1888 motg->chg_state = USB_CHG_STATE_DETECTED;
1889 delay = 0;
1890 }
1891 break;
1892 case USB_CHG_STATE_PRIMARY_DONE:
1893 vout = msm_chg_check_secondary_det(motg);
1894 if (vout)
1895 motg->chg_type = USB_DCP_CHARGER;
1896 else
1897 motg->chg_type = USB_CDP_CHARGER;
1898 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1899 /* fall through */
1900 case USB_CHG_STATE_SECONDARY_DONE:
1901 motg->chg_state = USB_CHG_STATE_DETECTED;
1902 case USB_CHG_STATE_DETECTED:
1903 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001904 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301905 /*
1906 * Spurious interrupt is seen after enabling ACA detection
1907 * due to which charger detection fails in case of PET.
1908 * Add delay of 100 microsec to avoid that.
1909 */
1910 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001911 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301912 dev_dbg(otg->dev, "chg_type = %s\n",
1913 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301914 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301915 return;
1916 default:
1917 return;
1918 }
1919
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301920 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301921}
1922
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301923/*
1924 * We support OTG, Peripheral only and Host only configurations. In case
1925 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1926 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1927 * enabled when switch is controlled by user and default mode is supplied
1928 * by board file, which can be changed by userspace later.
1929 */
1930static void msm_otg_init_sm(struct msm_otg *motg)
1931{
1932 struct msm_otg_platform_data *pdata = motg->pdata;
1933 u32 otgsc = readl(USB_OTGSC);
1934
1935 switch (pdata->mode) {
1936 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001937 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301938 if (pdata->default_mode == USB_HOST) {
1939 clear_bit(ID, &motg->inputs);
1940 } else if (pdata->default_mode == USB_PERIPHERAL) {
1941 set_bit(ID, &motg->inputs);
1942 set_bit(B_SESS_VLD, &motg->inputs);
1943 } else {
1944 set_bit(ID, &motg->inputs);
1945 clear_bit(B_SESS_VLD, &motg->inputs);
1946 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301947 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301948 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301949 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301950 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301951 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301952 set_bit(A_BUS_REQ, &motg->inputs);
1953 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001954 if (otgsc & OTGSC_BSV)
1955 set_bit(B_SESS_VLD, &motg->inputs);
1956 else
1957 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301958 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301959 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07001960 unsigned long flags;
1961 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301962 if (irq_read_line(pdata->pmic_id_irq))
1963 set_bit(ID, &motg->inputs);
1964 else
1965 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07001966 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301967 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301968 /*
1969 * VBUS initial state is reported after PMIC
1970 * driver initialization. Wait for it.
1971 */
1972 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301973 }
1974 break;
1975 case USB_HOST:
1976 clear_bit(ID, &motg->inputs);
1977 break;
1978 case USB_PERIPHERAL:
1979 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301980 if (pdata->otg_control == OTG_PHY_CONTROL) {
1981 if (otgsc & OTGSC_BSV)
1982 set_bit(B_SESS_VLD, &motg->inputs);
1983 else
1984 clear_bit(B_SESS_VLD, &motg->inputs);
1985 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1986 /*
1987 * VBUS initial state is reported after PMIC
1988 * driver initialization. Wait for it.
1989 */
1990 wait_for_completion(&pmic_vbus_init);
1991 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301992 break;
1993 default:
1994 break;
1995 }
1996}
1997
1998static void msm_otg_sm_work(struct work_struct *w)
1999{
2000 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
2001 struct otg_transceiver *otg = &motg->otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302002 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302003
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302004 pm_runtime_resume(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302005 pr_debug("%s work\n", otg_state_string(otg->state));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302006 switch (otg->state) {
2007 case OTG_STATE_UNDEFINED:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302008 msm_otg_reset(otg);
2009 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302010 psy = power_supply_get_by_name("usb");
2011 if (!psy)
2012 pr_err("couldn't get usb power supply\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302013 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302014 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2015 test_bit(ID, &motg->inputs)) {
2016 pm_runtime_put_noidle(otg->dev);
2017 pm_runtime_suspend(otg->dev);
2018 break;
2019 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302020 /* FALL THROUGH */
2021 case OTG_STATE_B_IDLE:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002022 if ((!test_bit(ID, &motg->inputs) ||
2023 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302024 pr_debug("!id || id_A\n");
2025 clear_bit(B_BUS_REQ, &motg->inputs);
2026 set_bit(A_BUS_REQ, &motg->inputs);
2027 otg->state = OTG_STATE_A_IDLE;
2028 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302029 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302030 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302031 switch (motg->chg_state) {
2032 case USB_CHG_STATE_UNDEFINED:
2033 msm_chg_detect_work(&motg->chg_work.work);
2034 break;
2035 case USB_CHG_STATE_DETECTED:
2036 switch (motg->chg_type) {
2037 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302038 /* Enable VDP_SRC */
2039 ulpi_write(otg, 0x2, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302040 msm_otg_notify_charger(motg,
2041 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302042 pm_runtime_put_noidle(otg->dev);
2043 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302044 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302045 case USB_ACA_B_CHARGER:
2046 msm_otg_notify_charger(motg,
2047 IDEV_ACA_CHG_MAX);
2048 /*
2049 * (ID_B --> ID_C) PHY_ALT interrupt can
2050 * not be detected in LPM.
2051 */
2052 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302053 case USB_CDP_CHARGER:
2054 msm_otg_notify_charger(motg,
2055 IDEV_CHG_MAX);
2056 msm_otg_start_peripheral(otg, 1);
2057 otg->state = OTG_STATE_B_PERIPHERAL;
2058 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302059 case USB_ACA_C_CHARGER:
2060 msm_otg_notify_charger(motg,
2061 IDEV_ACA_CHG_MAX);
2062 msm_otg_start_peripheral(otg, 1);
2063 otg->state = OTG_STATE_B_PERIPHERAL;
2064 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302065 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066 msm_otg_start_peripheral(otg, 1);
2067 otg->state = OTG_STATE_B_PERIPHERAL;
2068 break;
2069 default:
2070 break;
2071 }
2072 break;
2073 default:
2074 break;
2075 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302076 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2077 pr_debug("b_sess_end && b_bus_req\n");
2078 if (msm_otg_start_srp(otg) < 0) {
2079 clear_bit(B_BUS_REQ, &motg->inputs);
2080 work = 1;
2081 break;
2082 }
2083 otg->state = OTG_STATE_B_SRP_INIT;
2084 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2085 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302086 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302087 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302088 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302089 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2090 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302091 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302092 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302093 pm_runtime_put_noidle(otg->dev);
2094 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302095 }
2096 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302097 case OTG_STATE_B_SRP_INIT:
2098 if (!test_bit(ID, &motg->inputs) ||
2099 test_bit(ID_A, &motg->inputs) ||
2100 test_bit(ID_C, &motg->inputs) ||
2101 (test_bit(B_SESS_VLD, &motg->inputs) &&
2102 !test_bit(ID_B, &motg->inputs))) {
2103 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2104 msm_otg_del_timer(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002105 otg->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302106 /*
2107 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2108 * bits after SRP initiation.
2109 */
2110 ulpi_write(otg, 0x0, 0x98);
2111 work = 1;
2112 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2113 pr_debug("b_srp_fail\n");
2114 pr_info("A-device did not respond to SRP\n");
2115 clear_bit(B_BUS_REQ, &motg->inputs);
2116 clear_bit(B_SRP_FAIL, &motg->tmouts);
2117 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
2118 ulpi_write(otg, 0x0, 0x98);
2119 otg->state = OTG_STATE_B_IDLE;
2120 motg->b_last_se0_sess = jiffies;
2121 work = 1;
2122 }
2123 break;
2124 case OTG_STATE_B_PERIPHERAL:
2125 if (!test_bit(ID, &motg->inputs) ||
2126 test_bit(ID_A, &motg->inputs) ||
2127 test_bit(ID_B, &motg->inputs) ||
2128 !test_bit(B_SESS_VLD, &motg->inputs)) {
2129 pr_debug("!id || id_a/b || !b_sess_vld\n");
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302130 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2131 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302132 msm_otg_notify_charger(motg, 0);
2133 srp_reqd = otg->gadget->otg_srp_reqd;
2134 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302135 if (test_bit(ID_B, &motg->inputs))
2136 clear_bit(ID_B, &motg->inputs);
2137 clear_bit(B_BUS_REQ, &motg->inputs);
2138 otg->state = OTG_STATE_B_IDLE;
2139 motg->b_last_se0_sess = jiffies;
2140 if (srp_reqd)
2141 msm_otg_start_timer(motg,
2142 TB_TST_SRP, B_TST_SRP);
2143 else
2144 work = 1;
2145 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2146 otg->gadget->b_hnp_enable &&
2147 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2148 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2149 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2150 /* D+ pullup should not be disconnected within 4msec
2151 * after A device suspends the bus. Otherwise PET will
2152 * fail the compliance test.
2153 */
2154 udelay(1000);
2155 msm_otg_start_peripheral(otg, 0);
2156 otg->state = OTG_STATE_B_WAIT_ACON;
2157 /*
2158 * start HCD even before A-device enable
2159 * pull-up to meet HNP timings.
2160 */
2161 otg->host->is_b_host = 1;
2162 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002163 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2164 test_bit(B_SESS_VLD, &motg->inputs)) {
2165 pr_debug("a_bus_suspend && b_sess_vld\n");
2166 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
2167 pm_runtime_put_noidle(otg->dev);
2168 pm_runtime_suspend(otg->dev);
2169 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002170 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302171 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172 }
2173 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302174 case OTG_STATE_B_WAIT_ACON:
2175 if (!test_bit(ID, &motg->inputs) ||
2176 test_bit(ID_A, &motg->inputs) ||
2177 test_bit(ID_B, &motg->inputs) ||
2178 !test_bit(B_SESS_VLD, &motg->inputs)) {
2179 pr_debug("!id || id_a/b || !b_sess_vld\n");
2180 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302181 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302182 * A-device is physically disconnected during
2183 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302184 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302185 msm_otg_start_host(otg, 0);
2186 otg->host->is_b_host = 0;
2187
2188 clear_bit(B_BUS_REQ, &motg->inputs);
2189 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2190 motg->b_last_se0_sess = jiffies;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302191 otg->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302192 msm_otg_reset(otg);
2193 work = 1;
2194 } else if (test_bit(A_CONN, &motg->inputs)) {
2195 pr_debug("a_conn\n");
2196 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2197 otg->state = OTG_STATE_B_HOST;
2198 /*
2199 * PET disconnects D+ pullup after reset is generated
2200 * by B device in B_HOST role which is not detected by
2201 * B device. As workaorund , start timer of 300msec
2202 * and stop timer if A device is enumerated else clear
2203 * A_CONN.
2204 */
2205 msm_otg_start_timer(motg, TB_TST_CONFIG,
2206 B_TST_CONFIG);
2207 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2208 pr_debug("b_ase0_brst_tmout\n");
2209 pr_info("B HNP fail:No response from A device\n");
2210 msm_otg_start_host(otg, 0);
2211 msm_otg_reset(otg);
2212 otg->host->is_b_host = 0;
2213 clear_bit(B_ASE0_BRST, &motg->tmouts);
2214 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2215 clear_bit(B_BUS_REQ, &motg->inputs);
2216 otg_send_event(OTG_EVENT_HNP_FAILED);
2217 otg->state = OTG_STATE_B_IDLE;
2218 work = 1;
2219 } else if (test_bit(ID_C, &motg->inputs)) {
2220 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2221 }
2222 break;
2223 case OTG_STATE_B_HOST:
2224 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2225 !test_bit(A_CONN, &motg->inputs) ||
2226 !test_bit(B_SESS_VLD, &motg->inputs)) {
2227 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2228 clear_bit(A_CONN, &motg->inputs);
2229 clear_bit(B_BUS_REQ, &motg->inputs);
2230 msm_otg_start_host(otg, 0);
2231 otg->host->is_b_host = 0;
2232 otg->state = OTG_STATE_B_IDLE;
2233 msm_otg_reset(otg);
2234 work = 1;
2235 } else if (test_bit(ID_C, &motg->inputs)) {
2236 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2237 }
2238 break;
2239 case OTG_STATE_A_IDLE:
2240 otg->default_a = 1;
2241 if (test_bit(ID, &motg->inputs) &&
2242 !test_bit(ID_A, &motg->inputs)) {
2243 pr_debug("id && !id_a\n");
2244 otg->default_a = 0;
2245 clear_bit(A_BUS_DROP, &motg->inputs);
2246 otg->state = OTG_STATE_B_IDLE;
2247 del_timer_sync(&motg->id_timer);
2248 msm_otg_link_reset(motg);
2249 msm_chg_enable_aca_intr(motg);
2250 msm_otg_notify_charger(motg, 0);
2251 work = 1;
2252 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2253 (test_bit(A_SRP_DET, &motg->inputs) ||
2254 test_bit(A_BUS_REQ, &motg->inputs))) {
2255 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2256
2257 clear_bit(A_SRP_DET, &motg->inputs);
2258 /* Disable SRP detection */
2259 writel_relaxed((readl_relaxed(USB_OTGSC) &
2260 ~OTGSC_INTSTS_MASK) &
2261 ~OTGSC_DPIE, USB_OTGSC);
2262
2263 otg->state = OTG_STATE_A_WAIT_VRISE;
2264 /* VBUS should not be supplied before end of SRP pulse
2265 * generated by PET, if not complaince test fail.
2266 */
2267 usleep_range(10000, 12000);
2268 /* ACA: ID_A: Stop charging untill enumeration */
2269 if (test_bit(ID_A, &motg->inputs))
2270 msm_otg_notify_charger(motg, 0);
2271 else
2272 msm_hsusb_vbus_power(motg, 1);
2273 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2274 } else {
2275 pr_debug("No session requested\n");
2276 clear_bit(A_BUS_DROP, &motg->inputs);
2277 if (test_bit(ID_A, &motg->inputs)) {
2278 msm_otg_notify_charger(motg,
2279 IDEV_ACA_CHG_MAX);
2280 } else if (!test_bit(ID, &motg->inputs)) {
2281 msm_otg_notify_charger(motg, 0);
2282 /*
2283 * A-device is not providing power on VBUS.
2284 * Enable SRP detection.
2285 */
2286 writel_relaxed(0x13, USB_USBMODE);
2287 writel_relaxed((readl_relaxed(USB_OTGSC) &
2288 ~OTGSC_INTSTS_MASK) |
2289 OTGSC_DPIE, USB_OTGSC);
2290 mb();
2291 }
2292 }
2293 break;
2294 case OTG_STATE_A_WAIT_VRISE:
2295 if ((test_bit(ID, &motg->inputs) &&
2296 !test_bit(ID_A, &motg->inputs)) ||
2297 test_bit(A_BUS_DROP, &motg->inputs) ||
2298 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2299 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2300 clear_bit(A_BUS_REQ, &motg->inputs);
2301 msm_otg_del_timer(motg);
2302 msm_hsusb_vbus_power(motg, 0);
2303 otg->state = OTG_STATE_A_WAIT_VFALL;
2304 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2305 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2306 pr_debug("a_vbus_vld\n");
2307 otg->state = OTG_STATE_A_WAIT_BCON;
2308 if (TA_WAIT_BCON > 0)
2309 msm_otg_start_timer(motg, TA_WAIT_BCON,
2310 A_WAIT_BCON);
2311 msm_otg_start_host(otg, 1);
2312 msm_chg_enable_aca_det(motg);
2313 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302314 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302315 if (msm_chg_check_aca_intr(motg))
2316 work = 1;
2317 }
2318 break;
2319 case OTG_STATE_A_WAIT_BCON:
2320 if ((test_bit(ID, &motg->inputs) &&
2321 !test_bit(ID_A, &motg->inputs)) ||
2322 test_bit(A_BUS_DROP, &motg->inputs) ||
2323 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2324 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2325 "a_wait_bcon_tmout\n");
2326 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2327 pr_info("Device No Response\n");
2328 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2329 }
2330 msm_otg_del_timer(motg);
2331 clear_bit(A_BUS_REQ, &motg->inputs);
2332 clear_bit(B_CONN, &motg->inputs);
2333 msm_otg_start_host(otg, 0);
2334 /*
2335 * ACA: ID_A with NO accessory, just the A plug is
2336 * attached to ACA: Use IDCHG_MAX for charging
2337 */
2338 if (test_bit(ID_A, &motg->inputs))
2339 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2340 else
2341 msm_hsusb_vbus_power(motg, 0);
2342 otg->state = OTG_STATE_A_WAIT_VFALL;
2343 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2344 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2345 pr_debug("!a_vbus_vld\n");
2346 clear_bit(B_CONN, &motg->inputs);
2347 msm_otg_del_timer(motg);
2348 msm_otg_start_host(otg, 0);
2349 otg->state = OTG_STATE_A_VBUS_ERR;
2350 msm_otg_reset(otg);
2351 } else if (test_bit(ID_A, &motg->inputs)) {
2352 msm_hsusb_vbus_power(motg, 0);
2353 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2354 /*
2355 * If TA_WAIT_BCON is infinite, we don;t
2356 * turn off VBUS. Enter low power mode.
2357 */
2358 if (TA_WAIT_BCON < 0)
2359 pm_runtime_put_sync(otg->dev);
2360 } else if (!test_bit(ID, &motg->inputs)) {
2361 msm_hsusb_vbus_power(motg, 1);
2362 }
2363 break;
2364 case OTG_STATE_A_HOST:
2365 if ((test_bit(ID, &motg->inputs) &&
2366 !test_bit(ID_A, &motg->inputs)) ||
2367 test_bit(A_BUS_DROP, &motg->inputs)) {
2368 pr_debug("id_a/b/c || a_bus_drop\n");
2369 clear_bit(B_CONN, &motg->inputs);
2370 clear_bit(A_BUS_REQ, &motg->inputs);
2371 msm_otg_del_timer(motg);
2372 otg->state = OTG_STATE_A_WAIT_VFALL;
2373 msm_otg_start_host(otg, 0);
2374 if (!test_bit(ID_A, &motg->inputs))
2375 msm_hsusb_vbus_power(motg, 0);
2376 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2377 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2378 pr_debug("!a_vbus_vld\n");
2379 clear_bit(B_CONN, &motg->inputs);
2380 msm_otg_del_timer(motg);
2381 otg->state = OTG_STATE_A_VBUS_ERR;
2382 msm_otg_start_host(otg, 0);
2383 msm_otg_reset(otg);
2384 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2385 /*
2386 * a_bus_req is de-asserted when root hub is
2387 * suspended or HNP is in progress.
2388 */
2389 pr_debug("!a_bus_req\n");
2390 msm_otg_del_timer(motg);
2391 otg->state = OTG_STATE_A_SUSPEND;
2392 if (otg->host->b_hnp_enable)
2393 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2394 A_AIDL_BDIS);
2395 else
2396 pm_runtime_put_sync(otg->dev);
2397 } else if (!test_bit(B_CONN, &motg->inputs)) {
2398 pr_debug("!b_conn\n");
2399 msm_otg_del_timer(motg);
2400 otg->state = OTG_STATE_A_WAIT_BCON;
2401 if (TA_WAIT_BCON > 0)
2402 msm_otg_start_timer(motg, TA_WAIT_BCON,
2403 A_WAIT_BCON);
2404 if (msm_chg_check_aca_intr(motg))
2405 work = 1;
2406 } else if (test_bit(ID_A, &motg->inputs)) {
2407 msm_otg_del_timer(motg);
2408 msm_hsusb_vbus_power(motg, 0);
2409 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2410 msm_otg_notify_charger(motg,
2411 IDEV_ACA_CHG_MAX);
2412 else
2413 msm_otg_notify_charger(motg,
2414 IDEV_CHG_MIN - motg->mA_port);
2415 } else if (!test_bit(ID, &motg->inputs)) {
2416 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2417 motg->chg_type = USB_INVALID_CHARGER;
2418 msm_otg_notify_charger(motg, 0);
2419 msm_hsusb_vbus_power(motg, 1);
2420 }
2421 break;
2422 case OTG_STATE_A_SUSPEND:
2423 if ((test_bit(ID, &motg->inputs) &&
2424 !test_bit(ID_A, &motg->inputs)) ||
2425 test_bit(A_BUS_DROP, &motg->inputs) ||
2426 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2427 pr_debug("id_a/b/c || a_bus_drop ||"
2428 "a_aidl_bdis_tmout\n");
2429 msm_otg_del_timer(motg);
2430 clear_bit(B_CONN, &motg->inputs);
2431 otg->state = OTG_STATE_A_WAIT_VFALL;
2432 msm_otg_start_host(otg, 0);
2433 msm_otg_reset(otg);
2434 if (!test_bit(ID_A, &motg->inputs))
2435 msm_hsusb_vbus_power(motg, 0);
2436 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2437 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2438 pr_debug("!a_vbus_vld\n");
2439 msm_otg_del_timer(motg);
2440 clear_bit(B_CONN, &motg->inputs);
2441 otg->state = OTG_STATE_A_VBUS_ERR;
2442 msm_otg_start_host(otg, 0);
2443 msm_otg_reset(otg);
2444 } else if (!test_bit(B_CONN, &motg->inputs) &&
2445 otg->host->b_hnp_enable) {
2446 pr_debug("!b_conn && b_hnp_enable");
2447 otg->state = OTG_STATE_A_PERIPHERAL;
2448 msm_otg_host_hnp_enable(otg, 1);
2449 otg->gadget->is_a_peripheral = 1;
2450 msm_otg_start_peripheral(otg, 1);
2451 } else if (!test_bit(B_CONN, &motg->inputs) &&
2452 !otg->host->b_hnp_enable) {
2453 pr_debug("!b_conn && !b_hnp_enable");
2454 /*
2455 * bus request is dropped during suspend.
2456 * acquire again for next device.
2457 */
2458 set_bit(A_BUS_REQ, &motg->inputs);
2459 otg->state = OTG_STATE_A_WAIT_BCON;
2460 if (TA_WAIT_BCON > 0)
2461 msm_otg_start_timer(motg, TA_WAIT_BCON,
2462 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002463 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302464 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002465 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302466 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002467 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002468 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302469 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302470 }
2471 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302472 case OTG_STATE_A_PERIPHERAL:
2473 if ((test_bit(ID, &motg->inputs) &&
2474 !test_bit(ID_A, &motg->inputs)) ||
2475 test_bit(A_BUS_DROP, &motg->inputs)) {
2476 pr_debug("id _f/b/c || a_bus_drop\n");
2477 /* Clear BIDL_ADIS timer */
2478 msm_otg_del_timer(motg);
2479 otg->state = OTG_STATE_A_WAIT_VFALL;
2480 msm_otg_start_peripheral(otg, 0);
2481 otg->gadget->is_a_peripheral = 0;
2482 msm_otg_start_host(otg, 0);
2483 msm_otg_reset(otg);
2484 if (!test_bit(ID_A, &motg->inputs))
2485 msm_hsusb_vbus_power(motg, 0);
2486 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2487 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2488 pr_debug("!a_vbus_vld\n");
2489 /* Clear BIDL_ADIS timer */
2490 msm_otg_del_timer(motg);
2491 otg->state = OTG_STATE_A_VBUS_ERR;
2492 msm_otg_start_peripheral(otg, 0);
2493 otg->gadget->is_a_peripheral = 0;
2494 msm_otg_start_host(otg, 0);
2495 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2496 pr_debug("a_bidl_adis_tmout\n");
2497 msm_otg_start_peripheral(otg, 0);
2498 otg->gadget->is_a_peripheral = 0;
2499 otg->state = OTG_STATE_A_WAIT_BCON;
2500 set_bit(A_BUS_REQ, &motg->inputs);
2501 msm_otg_host_hnp_enable(otg, 0);
2502 if (TA_WAIT_BCON > 0)
2503 msm_otg_start_timer(motg, TA_WAIT_BCON,
2504 A_WAIT_BCON);
2505 } else if (test_bit(ID_A, &motg->inputs)) {
2506 msm_hsusb_vbus_power(motg, 0);
2507 msm_otg_notify_charger(motg,
2508 IDEV_CHG_MIN - motg->mA_port);
2509 } else if (!test_bit(ID, &motg->inputs)) {
2510 msm_otg_notify_charger(motg, 0);
2511 msm_hsusb_vbus_power(motg, 1);
2512 }
2513 break;
2514 case OTG_STATE_A_WAIT_VFALL:
2515 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2516 clear_bit(A_VBUS_VLD, &motg->inputs);
2517 otg->state = OTG_STATE_A_IDLE;
2518 work = 1;
2519 }
2520 break;
2521 case OTG_STATE_A_VBUS_ERR:
2522 if ((test_bit(ID, &motg->inputs) &&
2523 !test_bit(ID_A, &motg->inputs)) ||
2524 test_bit(A_BUS_DROP, &motg->inputs) ||
2525 test_bit(A_CLR_ERR, &motg->inputs)) {
2526 otg->state = OTG_STATE_A_WAIT_VFALL;
2527 if (!test_bit(ID_A, &motg->inputs))
2528 msm_hsusb_vbus_power(motg, 0);
2529 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2530 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2531 motg->chg_type = USB_INVALID_CHARGER;
2532 msm_otg_notify_charger(motg, 0);
2533 }
2534 break;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302535 default:
2536 break;
2537 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302538 if (work)
2539 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302540}
2541
2542static irqreturn_t msm_otg_irq(int irq, void *data)
2543{
2544 struct msm_otg *motg = data;
2545 struct otg_transceiver *otg = &motg->otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302546 u32 otgsc = 0, usbsts, pc;
2547 bool work = 0;
2548 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302549
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302550 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302551 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302552 disable_irq_nosync(irq);
2553 motg->async_int = 1;
Jack Pham5ca279b2012-05-14 18:42:54 -07002554 if (atomic_read(&motg->pm_suspended))
2555 motg->sm_work_pending = true;
2556 else
2557 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302558 return IRQ_HANDLED;
2559 }
2560
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002561 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302562 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302563
2564 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302565 return IRQ_NONE;
2566
2567 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302568 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302569 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302570 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302571 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302572 pr_debug("Id clear\n");
2573 /*
2574 * Assert a_bus_req to supply power on
2575 * VBUS when Micro/Mini-A cable is connected
2576 * with out user intervention.
2577 */
2578 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302579 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302580 msm_chg_enable_aca_det(motg);
2581 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302582 writel_relaxed(otgsc, USB_OTGSC);
2583 work = 1;
2584 } else if (otgsc & OTGSC_DPIS) {
2585 pr_debug("DPIS detected\n");
2586 writel_relaxed(otgsc, USB_OTGSC);
2587 set_bit(A_SRP_DET, &motg->inputs);
2588 set_bit(A_BUS_REQ, &motg->inputs);
2589 work = 1;
2590 } else if (otgsc & OTGSC_BSVIS) {
2591 writel_relaxed(otgsc, USB_OTGSC);
2592 /*
2593 * BSV interrupt comes when operating as an A-device
2594 * (VBUS on/off).
2595 * But, handle BSV when charger is removed from ACA in ID_A
2596 */
2597 if ((otg->state >= OTG_STATE_A_IDLE) &&
2598 !test_bit(ID_A, &motg->inputs))
2599 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302600 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302601 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302602 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302603 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302604 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302605 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002606 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2607
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302608 msm_chg_check_aca_intr(motg);
2609 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302610 work = 1;
2611 } else if (usbsts & STS_PCI) {
2612 pc = readl_relaxed(USB_PORTSC);
2613 pr_debug("portsc = %x\n", pc);
2614 ret = IRQ_NONE;
2615 /*
2616 * HCD Acks PCI interrupt. We use this to switch
2617 * between different OTG states.
2618 */
2619 work = 1;
2620 switch (otg->state) {
2621 case OTG_STATE_A_SUSPEND:
2622 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2623 !(pc & PORTSC_CCS)) {
2624 pr_debug("B_CONN clear\n");
2625 clear_bit(B_CONN, &motg->inputs);
2626 msm_otg_del_timer(motg);
2627 }
2628 break;
2629 case OTG_STATE_A_PERIPHERAL:
2630 /*
2631 * A-peripheral observed activity on bus.
2632 * clear A_BIDL_ADIS timer.
2633 */
2634 msm_otg_del_timer(motg);
2635 work = 0;
2636 break;
2637 case OTG_STATE_B_WAIT_ACON:
2638 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2639 pr_debug("A_CONN set\n");
2640 set_bit(A_CONN, &motg->inputs);
2641 /* Clear ASE0_BRST timer */
2642 msm_otg_del_timer(motg);
2643 }
2644 break;
2645 case OTG_STATE_B_HOST:
2646 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2647 pr_debug("A_CONN clear\n");
2648 clear_bit(A_CONN, &motg->inputs);
2649 msm_otg_del_timer(motg);
2650 }
2651 break;
2652 case OTG_STATE_A_WAIT_BCON:
2653 if (TA_WAIT_BCON < 0)
2654 set_bit(A_BUS_REQ, &motg->inputs);
2655 default:
2656 work = 0;
2657 break;
2658 }
2659 } else if (usbsts & STS_URI) {
2660 ret = IRQ_NONE;
2661 switch (otg->state) {
2662 case OTG_STATE_A_PERIPHERAL:
2663 /*
2664 * A-peripheral observed activity on bus.
2665 * clear A_BIDL_ADIS timer.
2666 */
2667 msm_otg_del_timer(motg);
2668 work = 0;
2669 break;
2670 default:
2671 work = 0;
2672 break;
2673 }
2674 } else if (usbsts & STS_SLI) {
2675 ret = IRQ_NONE;
2676 work = 0;
2677 switch (otg->state) {
2678 case OTG_STATE_B_PERIPHERAL:
2679 if (otg->gadget->b_hnp_enable) {
2680 set_bit(A_BUS_SUSPEND, &motg->inputs);
2681 set_bit(B_BUS_REQ, &motg->inputs);
2682 work = 1;
2683 }
2684 break;
2685 case OTG_STATE_A_PERIPHERAL:
2686 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2687 A_BIDL_ADIS);
2688 break;
2689 default:
2690 break;
2691 }
2692 } else if ((usbsts & PHY_ALT_INT)) {
2693 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2694 if (msm_chg_check_aca_intr(motg))
2695 work = 1;
2696 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302697 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302698 if (work)
2699 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302700
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302701 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002702}
2703
2704static void msm_otg_set_vbus_state(int online)
2705{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302706 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002707 struct msm_otg *motg = the_msm_otg;
2708
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302709 if (online) {
2710 pr_debug("PMIC: BSV set\n");
2711 set_bit(B_SESS_VLD, &motg->inputs);
2712 } else {
2713 pr_debug("PMIC: BSV clear\n");
2714 clear_bit(B_SESS_VLD, &motg->inputs);
2715 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002716
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302717 if (!init) {
2718 init = true;
2719 complete(&pmic_vbus_init);
2720 pr_debug("PMIC: BSV init complete\n");
2721 return;
2722 }
2723
Jack Pham5ca279b2012-05-14 18:42:54 -07002724 if (atomic_read(&motg->pm_suspended))
2725 motg->sm_work_pending = true;
2726 else
2727 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002728}
2729
2730static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2731{
2732 struct msm_otg *motg = data;
2733
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302734 if (aca_id_turned_on)
2735 return IRQ_HANDLED;
2736
2737 if (irq_read_line(motg->pdata->pmic_id_irq)) {
2738 pr_debug("PMIC: ID set\n");
2739 set_bit(ID, &motg->inputs);
2740 } else {
2741 pr_debug("PMIC: ID clear\n");
2742 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302743 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302744 }
2745
Jack Pham5ca279b2012-05-14 18:42:54 -07002746 if (motg->otg.state != OTG_STATE_UNDEFINED) {
2747 if (atomic_read(&motg->pm_suspended))
2748 motg->sm_work_pending = true;
2749 else
2750 queue_work(system_nrt_wq, &motg->sm_work);
2751 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002752
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302753 return IRQ_HANDLED;
2754}
2755
2756static int msm_otg_mode_show(struct seq_file *s, void *unused)
2757{
2758 struct msm_otg *motg = s->private;
2759 struct otg_transceiver *otg = &motg->otg;
2760
2761 switch (otg->state) {
2762 case OTG_STATE_A_HOST:
2763 seq_printf(s, "host\n");
2764 break;
2765 case OTG_STATE_B_PERIPHERAL:
2766 seq_printf(s, "peripheral\n");
2767 break;
2768 default:
2769 seq_printf(s, "none\n");
2770 break;
2771 }
2772
2773 return 0;
2774}
2775
2776static int msm_otg_mode_open(struct inode *inode, struct file *file)
2777{
2778 return single_open(file, msm_otg_mode_show, inode->i_private);
2779}
2780
2781static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2782 size_t count, loff_t *ppos)
2783{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302784 struct seq_file *s = file->private_data;
2785 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302786 char buf[16];
2787 struct otg_transceiver *otg = &motg->otg;
2788 int status = count;
2789 enum usb_mode_type req_mode;
2790
2791 memset(buf, 0x00, sizeof(buf));
2792
2793 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2794 status = -EFAULT;
2795 goto out;
2796 }
2797
2798 if (!strncmp(buf, "host", 4)) {
2799 req_mode = USB_HOST;
2800 } else if (!strncmp(buf, "peripheral", 10)) {
2801 req_mode = USB_PERIPHERAL;
2802 } else if (!strncmp(buf, "none", 4)) {
2803 req_mode = USB_NONE;
2804 } else {
2805 status = -EINVAL;
2806 goto out;
2807 }
2808
2809 switch (req_mode) {
2810 case USB_NONE:
2811 switch (otg->state) {
2812 case OTG_STATE_A_HOST:
2813 case OTG_STATE_B_PERIPHERAL:
2814 set_bit(ID, &motg->inputs);
2815 clear_bit(B_SESS_VLD, &motg->inputs);
2816 break;
2817 default:
2818 goto out;
2819 }
2820 break;
2821 case USB_PERIPHERAL:
2822 switch (otg->state) {
2823 case OTG_STATE_B_IDLE:
2824 case OTG_STATE_A_HOST:
2825 set_bit(ID, &motg->inputs);
2826 set_bit(B_SESS_VLD, &motg->inputs);
2827 break;
2828 default:
2829 goto out;
2830 }
2831 break;
2832 case USB_HOST:
2833 switch (otg->state) {
2834 case OTG_STATE_B_IDLE:
2835 case OTG_STATE_B_PERIPHERAL:
2836 clear_bit(ID, &motg->inputs);
2837 break;
2838 default:
2839 goto out;
2840 }
2841 break;
2842 default:
2843 goto out;
2844 }
2845
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302846 pm_runtime_resume(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302847 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302848out:
2849 return status;
2850}
2851
2852const struct file_operations msm_otg_mode_fops = {
2853 .open = msm_otg_mode_open,
2854 .read = seq_read,
2855 .write = msm_otg_mode_write,
2856 .llseek = seq_lseek,
2857 .release = single_release,
2858};
2859
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302860static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
2861{
2862 struct msm_otg *motg = s->private;
2863 struct otg_transceiver *otg = &motg->otg;
2864
2865 seq_printf(s, "%s\n", otg_state_string(otg->state));
2866 return 0;
2867}
2868
2869static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
2870{
2871 return single_open(file, msm_otg_show_otg_state, inode->i_private);
2872}
2873
2874const struct file_operations msm_otg_state_fops = {
2875 .open = msm_otg_otg_state_open,
2876 .read = seq_read,
2877 .llseek = seq_lseek,
2878 .release = single_release,
2879};
2880
Anji jonnalad270e2d2011-08-09 11:28:32 +05302881static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2882{
2883 struct msm_otg *motg = s->private;
2884
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302885 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302886 return 0;
2887}
2888
2889static int msm_otg_chg_open(struct inode *inode, struct file *file)
2890{
2891 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2892}
2893
2894const struct file_operations msm_otg_chg_fops = {
2895 .open = msm_otg_chg_open,
2896 .read = seq_read,
2897 .llseek = seq_lseek,
2898 .release = single_release,
2899};
2900
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302901static int msm_otg_aca_show(struct seq_file *s, void *unused)
2902{
2903 if (debug_aca_enabled)
2904 seq_printf(s, "enabled\n");
2905 else
2906 seq_printf(s, "disabled\n");
2907
2908 return 0;
2909}
2910
2911static int msm_otg_aca_open(struct inode *inode, struct file *file)
2912{
2913 return single_open(file, msm_otg_aca_show, inode->i_private);
2914}
2915
2916static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2917 size_t count, loff_t *ppos)
2918{
2919 char buf[8];
2920
2921 memset(buf, 0x00, sizeof(buf));
2922
2923 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2924 return -EFAULT;
2925
2926 if (!strncmp(buf, "enable", 6))
2927 debug_aca_enabled = true;
2928 else
2929 debug_aca_enabled = false;
2930
2931 return count;
2932}
2933
2934const struct file_operations msm_otg_aca_fops = {
2935 .open = msm_otg_aca_open,
2936 .read = seq_read,
2937 .write = msm_otg_aca_write,
2938 .llseek = seq_lseek,
2939 .release = single_release,
2940};
2941
Manu Gautam8bdcc592012-03-06 11:26:06 +05302942static int msm_otg_bus_show(struct seq_file *s, void *unused)
2943{
2944 if (debug_bus_voting_enabled)
2945 seq_printf(s, "enabled\n");
2946 else
2947 seq_printf(s, "disabled\n");
2948
2949 return 0;
2950}
2951
2952static int msm_otg_bus_open(struct inode *inode, struct file *file)
2953{
2954 return single_open(file, msm_otg_bus_show, inode->i_private);
2955}
2956
2957static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
2958 size_t count, loff_t *ppos)
2959{
2960 char buf[8];
2961 int ret;
2962 struct seq_file *s = file->private_data;
2963 struct msm_otg *motg = s->private;
2964
2965 memset(buf, 0x00, sizeof(buf));
2966
2967 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2968 return -EFAULT;
2969
2970 if (!strncmp(buf, "enable", 6)) {
2971 /* Do not vote here. Let OTG statemachine decide when to vote */
2972 debug_bus_voting_enabled = true;
2973 } else {
2974 debug_bus_voting_enabled = false;
2975 if (motg->bus_perf_client) {
2976 ret = msm_bus_scale_client_update_request(
2977 motg->bus_perf_client, 0);
2978 if (ret)
2979 dev_err(motg->otg.dev, "%s: Failed to devote "
2980 "for bus bw %d\n", __func__, ret);
2981 }
2982 }
2983
2984 return count;
2985}
2986
2987const struct file_operations msm_otg_bus_fops = {
2988 .open = msm_otg_bus_open,
2989 .read = seq_read,
2990 .write = msm_otg_bus_write,
2991 .llseek = seq_lseek,
2992 .release = single_release,
2993};
2994
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302995static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302996
2997static int msm_otg_debugfs_init(struct msm_otg *motg)
2998{
Manu Gautam8bdcc592012-03-06 11:26:06 +05302999 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303000
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303001 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3002
3003 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3004 return -ENODEV;
3005
Anji jonnalad270e2d2011-08-09 11:28:32 +05303006 if (motg->pdata->mode == USB_OTG &&
3007 motg->pdata->otg_control == OTG_USER_CONTROL) {
3008
Manu Gautam8bdcc592012-03-06 11:26:06 +05303009 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303010 S_IWUSR, msm_otg_dbg_root, motg,
3011 &msm_otg_mode_fops);
3012
Manu Gautam8bdcc592012-03-06 11:26:06 +05303013 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303014 debugfs_remove(msm_otg_dbg_root);
3015 msm_otg_dbg_root = NULL;
3016 return -ENODEV;
3017 }
3018 }
3019
Manu Gautam8bdcc592012-03-06 11:26:06 +05303020 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303021 msm_otg_dbg_root, motg,
3022 &msm_otg_chg_fops);
3023
Manu Gautam8bdcc592012-03-06 11:26:06 +05303024 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303025 debugfs_remove_recursive(msm_otg_dbg_root);
3026 return -ENODEV;
3027 }
3028
Manu Gautam8bdcc592012-03-06 11:26:06 +05303029 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303030 msm_otg_dbg_root, motg,
3031 &msm_otg_aca_fops);
3032
Manu Gautam8bdcc592012-03-06 11:26:06 +05303033 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303034 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303035 return -ENODEV;
3036 }
3037
Manu Gautam8bdcc592012-03-06 11:26:06 +05303038 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3039 msm_otg_dbg_root, motg,
3040 &msm_otg_bus_fops);
3041
3042 if (!msm_otg_dentry) {
3043 debugfs_remove_recursive(msm_otg_dbg_root);
3044 return -ENODEV;
3045 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303046
3047 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3048 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3049
3050 if (!msm_otg_dentry) {
3051 debugfs_remove_recursive(msm_otg_dbg_root);
3052 return -ENODEV;
3053 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303054 return 0;
3055}
3056
3057static void msm_otg_debugfs_cleanup(void)
3058{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303059 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303060}
3061
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303062static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3063static struct platform_device *msm_otg_add_pdev(
3064 struct platform_device *ofdev, const char *name)
3065{
3066 struct platform_device *pdev;
3067 const struct resource *res = ofdev->resource;
3068 unsigned int num = ofdev->num_resources;
3069 int retval;
3070
3071 pdev = platform_device_alloc(name, -1);
3072 if (!pdev) {
3073 retval = -ENOMEM;
3074 goto error;
3075 }
3076
3077 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3078 pdev->dev.dma_mask = &msm_otg_dma_mask;
3079
3080 if (num) {
3081 retval = platform_device_add_resources(pdev, res, num);
3082 if (retval)
3083 goto error;
3084 }
3085
3086 retval = platform_device_add(pdev);
3087 if (retval)
3088 goto error;
3089
3090 return pdev;
3091
3092error:
3093 platform_device_put(pdev);
3094 return ERR_PTR(retval);
3095}
3096
3097static int msm_otg_setup_devices(struct platform_device *ofdev,
3098 enum usb_mode_type mode, bool init)
3099{
3100 const char *gadget_name = "msm_hsusb";
3101 const char *host_name = "msm_hsusb_host";
3102 static struct platform_device *gadget_pdev;
3103 static struct platform_device *host_pdev;
3104 int retval = 0;
3105
3106 if (!init) {
3107 if (gadget_pdev)
3108 platform_device_unregister(gadget_pdev);
3109 if (host_pdev)
3110 platform_device_unregister(host_pdev);
3111 return 0;
3112 }
3113
3114 switch (mode) {
3115 case USB_OTG:
3116 /* fall through */
3117 case USB_PERIPHERAL:
3118 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3119 if (IS_ERR(gadget_pdev)) {
3120 retval = PTR_ERR(gadget_pdev);
3121 break;
3122 }
3123 if (mode == USB_PERIPHERAL)
3124 break;
3125 /* fall through */
3126 case USB_HOST:
3127 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3128 if (IS_ERR(host_pdev)) {
3129 retval = PTR_ERR(host_pdev);
3130 if (mode == USB_OTG)
3131 platform_device_unregister(gadget_pdev);
3132 }
3133 break;
3134 default:
3135 break;
3136 }
3137
3138 return retval;
3139}
3140
3141struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3142{
3143 struct device_node *node = pdev->dev.of_node;
3144 struct msm_otg_platform_data *pdata;
3145 int len = 0;
3146
3147 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3148 if (!pdata) {
3149 pr_err("unable to allocate platform data\n");
3150 return NULL;
3151 }
3152 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3153 if (len) {
3154 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3155 if (!pdata->phy_init_seq)
3156 return NULL;
3157 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3158 pdata->phy_init_seq,
3159 len/sizeof(*pdata->phy_init_seq));
3160 }
3161 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3162 &pdata->power_budget);
3163 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3164 &pdata->mode);
3165 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3166 &pdata->otg_control);
3167 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3168 &pdata->default_mode);
3169 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3170 &pdata->phy_type);
3171 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3172 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303173 return pdata;
3174}
3175
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303176static int __init msm_otg_probe(struct platform_device *pdev)
3177{
3178 int ret = 0;
3179 struct resource *res;
3180 struct msm_otg *motg;
3181 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303182 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303183
3184 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303185
3186 if (pdev->dev.of_node) {
3187 dev_dbg(&pdev->dev, "device tree enabled\n");
3188 pdata = msm_otg_dt_to_pdata(pdev);
3189 if (!pdata)
3190 return -ENOMEM;
3191 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3192 if (ret) {
3193 dev_err(&pdev->dev, "devices setup failed\n");
3194 return ret;
3195 }
3196 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303197 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3198 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303199 } else {
3200 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303201 }
3202
3203 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3204 if (!motg) {
3205 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3206 return -ENOMEM;
3207 }
3208
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003209 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303210 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303211 otg = &motg->otg;
3212 otg->dev = &pdev->dev;
3213
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303214 /*
3215 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3216 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3217 * PMIC can detect ACA ID_GND and generate an interrupt.
3218 */
3219 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3220 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3221 ret = -EINVAL;
3222 goto free_motg;
3223 }
3224
Ofir Cohen4da266f2012-01-03 10:19:29 +02003225 /* initialize reset counter */
3226 motg->reset_counter = 0;
3227
Amit Blay02eff132011-09-21 16:46:24 +03003228 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003229 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003230 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003231 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303232
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303233 /*
3234 * Targets on which link uses asynchronous reset methodology,
3235 * free running clock is not required during the reset.
3236 */
Manu Gautam5143b252012-01-05 19:25:23 -08003237 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303238 if (IS_ERR(motg->clk))
3239 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3240 else
3241 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303242
3243 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003244 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303245 * CORE CLK must be running at >55Mhz for correct HSUSB
3246 * operation and USB core cannot tolerate frequency changes on
3247 * CORE CLK. For such USB cores, vote for maximum clk frequency
3248 * on pclk source
3249 */
Manu Gautam5143b252012-01-05 19:25:23 -08003250 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3251 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303252 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003253 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303254 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003255 goto put_clk;
3256 }
3257 clk_set_rate(motg->core_clk, INT_MAX);
3258
3259 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3260 if (IS_ERR(motg->pclk)) {
3261 dev_err(&pdev->dev, "failed to get iface_clk\n");
3262 ret = PTR_ERR(motg->pclk);
3263 goto put_core_clk;
3264 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303265
3266 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3267 if (!res) {
3268 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3269 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003270 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303271 }
3272
3273 motg->regs = ioremap(res->start, resource_size(res));
3274 if (!motg->regs) {
3275 dev_err(&pdev->dev, "ioremap failed\n");
3276 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003277 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303278 }
3279 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3280
3281 motg->irq = platform_get_irq(pdev, 0);
3282 if (!motg->irq) {
3283 dev_err(&pdev->dev, "platform_get_irq failed\n");
3284 ret = -ENODEV;
3285 goto free_regs;
3286 }
3287
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003288 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003289 if (IS_ERR(motg->xo_handle)) {
3290 dev_err(&pdev->dev, "%s not able to get the handle "
3291 "to vote for TCXO D0 buffer\n", __func__);
3292 ret = PTR_ERR(motg->xo_handle);
3293 goto free_regs;
3294 }
3295
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003296 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003297 if (ret) {
3298 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3299 "D0 buffer%d\n", __func__, ret);
3300 goto free_xo_handle;
3301 }
3302
Manu Gautam28b1bac2012-01-30 16:43:06 +05303303 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303304
Mayank Rana248698c2012-04-19 00:03:16 +05303305 motg->vdd_type = VDDCX_CORNER;
3306 hsusb_vddcx = devm_regulator_get(motg->otg.dev, "hsusb_vdd_dig");
3307 if (IS_ERR(hsusb_vddcx)) {
3308 hsusb_vddcx = devm_regulator_get(motg->otg.dev, "HSUSB_VDDCX");
3309 if (IS_ERR(hsusb_vddcx)) {
3310 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
3311 goto devote_xo_handle;
3312 }
3313 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303314 }
3315
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003316 ret = msm_hsusb_config_vddcx(1);
3317 if (ret) {
3318 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303319 goto devote_xo_handle;
3320 }
3321
3322 ret = regulator_enable(hsusb_vddcx);
3323 if (ret) {
3324 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3325 goto free_config_vddcx;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003326 }
3327
Anji jonnala11aa5c42011-05-04 10:19:48 +05303328 ret = msm_hsusb_ldo_init(motg, 1);
3329 if (ret) {
3330 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303331 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303332 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003333
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303334 if (pdata->mhl_enable) {
3335 mhl_analog_switch = devm_regulator_get(motg->otg.dev,
3336 "mhl_ext_3p3v");
3337 if (IS_ERR(mhl_analog_switch)) {
3338 dev_err(&pdev->dev, "Unable to get mhl_analog_switch\n");
3339 goto free_ldo_init;
3340 }
3341 }
3342
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003343 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303344 if (ret) {
3345 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003346 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303347 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303348 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303349
3350 writel(0, USB_USBINTR);
3351 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003352 /* Ensure that above STOREs are completed before enabling interrupts */
3353 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303354
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003355 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303356 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303357 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303358 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303359 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3360 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303361 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3362 "msm_otg", motg);
3363 if (ret) {
3364 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003365 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303366 }
3367
3368 otg->init = msm_otg_reset;
3369 otg->set_host = msm_otg_set_host;
3370 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303371 otg->set_power = msm_otg_set_power;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303372 otg->start_hnp = msm_otg_start_hnp;
3373 otg->start_srp = msm_otg_start_srp;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303374 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303375
3376 otg->io_ops = &msm_otg_io_ops;
3377
3378 ret = otg_set_transceiver(&motg->otg);
3379 if (ret) {
3380 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
3381 goto free_irq;
3382 }
3383
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303384 if (motg->pdata->mode == USB_OTG &&
3385 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003386 if (motg->pdata->pmic_id_irq) {
3387 ret = request_irq(motg->pdata->pmic_id_irq,
3388 msm_pmic_id_irq,
3389 IRQF_TRIGGER_RISING |
3390 IRQF_TRIGGER_FALLING,
3391 "msm_otg", motg);
3392 if (ret) {
3393 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
3394 goto remove_otg;
3395 }
3396 } else {
3397 ret = -ENODEV;
3398 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
3399 goto remove_otg;
3400 }
3401 }
3402
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303403 msm_hsusb_mhl_switch_enable(motg, 1);
3404
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303405 platform_set_drvdata(pdev, motg);
3406 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003407 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303408
Anji jonnalad270e2d2011-08-09 11:28:32 +05303409 ret = msm_otg_debugfs_init(motg);
3410 if (ret)
3411 dev_dbg(&pdev->dev, "mode debugfs file is"
3412 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303413
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003414 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3415 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3416
Amit Blay58b31472011-11-18 09:39:39 +02003417 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3418 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303419 (!(motg->pdata->mode == USB_OTG) ||
3420 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003421 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303422 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003423
Amit Blay58b31472011-11-18 09:39:39 +02003424 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3425 motg->caps = ALLOW_PHY_RETENTION;
3426 }
3427
Amit Blay6fa647a2012-05-24 14:12:08 +03003428 if (motg->pdata->enable_lpm_on_dev_suspend)
3429 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3430
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003431 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303432 pm_runtime_set_active(&pdev->dev);
3433 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303434
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303435 if (motg->pdata->bus_scale_table) {
3436 motg->bus_perf_client =
3437 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3438 if (!motg->bus_perf_client)
3439 dev_err(motg->otg.dev, "%s: Failed to register BUS "
3440 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303441 else
3442 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303443 }
3444
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303445 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003446
3447remove_otg:
3448 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303449free_irq:
3450 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003451destroy_wlock:
3452 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303453 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003454 msm_hsusb_ldo_enable(motg, 0);
3455free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303456 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303457free_hsusb_vddcx:
3458 regulator_disable(hsusb_vddcx);
3459free_config_vddcx:
3460 regulator_set_voltage(hsusb_vddcx,
3461 vdd_val[motg->vdd_type][VDD_NONE],
3462 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003463devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303464 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003465 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003466free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003467 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303468free_regs:
3469 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003470put_pclk:
3471 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303472put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003473 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303474put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303475 if (!IS_ERR(motg->clk))
3476 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003477 if (!IS_ERR(motg->phy_reset_clk))
3478 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303479free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303480 kfree(motg);
3481 return ret;
3482}
3483
3484static int __devexit msm_otg_remove(struct platform_device *pdev)
3485{
3486 struct msm_otg *motg = platform_get_drvdata(pdev);
3487 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303488 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303489
3490 if (otg->host || otg->gadget)
3491 return -EBUSY;
3492
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303493 if (pdev->dev.of_node)
3494 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003495 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3496 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303497 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303498 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303499 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303500
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303501 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303502
3503 device_init_wakeup(&pdev->dev, 0);
3504 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003505 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303506
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303507 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003508 if (motg->pdata->pmic_id_irq)
3509 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303510 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303511 free_irq(motg->irq, motg);
3512
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303513 /*
3514 * Put PHY in low power mode.
3515 */
3516 ulpi_read(otg, 0x14);
3517 ulpi_write(otg, 0x08, 0x09);
3518
3519 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3520 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3521 if (readl(USB_PORTSC) & PORTSC_PHCD)
3522 break;
3523 udelay(1);
3524 cnt++;
3525 }
3526 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
3527 dev_err(otg->dev, "Unable to suspend PHY\n");
3528
Manu Gautam28b1bac2012-01-30 16:43:06 +05303529 clk_disable_unprepare(motg->pclk);
3530 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003531 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003532 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303533 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303534 regulator_disable(hsusb_vddcx);
3535 regulator_set_voltage(hsusb_vddcx,
3536 vdd_val[motg->vdd_type][VDD_NONE],
3537 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303538
3539 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303540 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303541
Amit Blay02eff132011-09-21 16:46:24 +03003542 if (!IS_ERR(motg->phy_reset_clk))
3543 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303544 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303545 if (!IS_ERR(motg->clk))
3546 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003547 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303548
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303549 if (motg->bus_perf_client)
3550 msm_bus_scale_unregister_client(motg->bus_perf_client);
3551
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05303552 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303553 return 0;
3554}
3555
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303556#ifdef CONFIG_PM_RUNTIME
3557static int msm_otg_runtime_idle(struct device *dev)
3558{
3559 struct msm_otg *motg = dev_get_drvdata(dev);
3560 struct otg_transceiver *otg = &motg->otg;
3561
3562 dev_dbg(dev, "OTG runtime idle\n");
3563
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303564 if (otg->state == OTG_STATE_UNDEFINED)
3565 return -EAGAIN;
3566 else
3567 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303568}
3569
3570static int msm_otg_runtime_suspend(struct device *dev)
3571{
3572 struct msm_otg *motg = dev_get_drvdata(dev);
3573
3574 dev_dbg(dev, "OTG runtime suspend\n");
3575 return msm_otg_suspend(motg);
3576}
3577
3578static int msm_otg_runtime_resume(struct device *dev)
3579{
3580 struct msm_otg *motg = dev_get_drvdata(dev);
3581
3582 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303583 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303584 return msm_otg_resume(motg);
3585}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303586#endif
3587
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303588#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303589static int msm_otg_pm_suspend(struct device *dev)
3590{
Jack Pham5ca279b2012-05-14 18:42:54 -07003591 int ret = 0;
Jack Phamd44455a2012-04-10 16:23:49 -07003592 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303593
3594 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003595
3596 atomic_set(&motg->pm_suspended, 1);
3597 ret = msm_otg_suspend(motg);
3598 if (ret)
3599 atomic_set(&motg->pm_suspended, 0);
3600
3601 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303602}
3603
3604static int msm_otg_pm_resume(struct device *dev)
3605{
Jack Pham5ca279b2012-05-14 18:42:54 -07003606 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303607 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303608
3609 dev_dbg(dev, "OTG PM resume\n");
3610
Jack Pham5ca279b2012-05-14 18:42:54 -07003611 atomic_set(&motg->pm_suspended, 0);
3612 if (motg->sm_work_pending) {
3613 motg->sm_work_pending = false;
Jack Phamd44455a2012-04-10 16:23:49 -07003614
Jack Pham5ca279b2012-05-14 18:42:54 -07003615 pm_runtime_get_noresume(dev);
3616 ret = msm_otg_resume(motg);
3617
3618 /* Update runtime PM status */
3619 pm_runtime_disable(dev);
3620 pm_runtime_set_active(dev);
3621 pm_runtime_enable(dev);
3622
3623 queue_work(system_nrt_wq, &motg->sm_work);
3624 }
3625
3626 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303627}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303628#endif
3629
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303630#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303631static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303632 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3633 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3634 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303635};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303636#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303637
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303638static struct of_device_id msm_otg_dt_match[] = {
3639 { .compatible = "qcom,hsusb-otg",
3640 },
3641 {}
3642};
3643
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303644static struct platform_driver msm_otg_driver = {
3645 .remove = __devexit_p(msm_otg_remove),
3646 .driver = {
3647 .name = DRIVER_NAME,
3648 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303649#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303650 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303651#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303652 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303653 },
3654};
3655
3656static int __init msm_otg_init(void)
3657{
3658 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3659}
3660
3661static void __exit msm_otg_exit(void)
3662{
3663 platform_driver_unregister(&msm_otg_driver);
3664}
3665
3666module_init(msm_otg_init);
3667module_exit(msm_otg_exit);
3668
3669MODULE_LICENSE("GPL v2");
3670MODULE_DESCRIPTION("MSM USB transceiver driver");