blob: 0aecaad063b8de3349c825f3a48925cbd26ce826 [file] [log] [blame]
Manu Gautam91223e02011-11-08 15:27:22 +05301/* ehci-msm2.c - HSUSB Host Controller Driver Implementation
2 *
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05303 * Copyright (c) 2008-2013, The Linux Foundation. All rights reserved.
Manu Gautam91223e02011-11-08 15:27:22 +05304 *
5 * Partly derived from ehci-fsl.c and ehci-hcd.c
6 * Copyright (c) 2000-2004 by David Brownell
7 * Copyright (c) 2005 MontaVista Software
8 *
9 * All source code in this file is licensed under the following license except
10 * where indicated.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, you can find it at http://www.fsf.org
23 */
24
25#include <linux/platform_device.h>
26#include <linux/clk.h>
27#include <linux/err.h>
Hemant Kumar153f9972013-06-19 16:49:45 -070028#include <linux/pm_wakeup.h>
Manu Gautam91223e02011-11-08 15:27:22 +053029#include <linux/pm_runtime.h>
30#include <linux/regulator/consumer.h>
Hemant Kumara7d531d2013-06-18 22:12:18 -070031#include <linux/gpio.h>
32#include <linux/of_gpio.h>
Hemant Kumar17011ce2013-06-10 17:40:08 -070033#include <linux/irq.h>
Manu Gautam91223e02011-11-08 15:27:22 +053034
35#include <linux/usb/ulpi.h>
36#include <linux/usb/msm_hsusb_hw.h>
37#include <linux/usb/msm_hsusb.h>
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +053038#include <linux/of.h>
Manu Gautam91223e02011-11-08 15:27:22 +053039#include <mach/clk.h>
Hemant Kumar8c0f2a82012-05-03 19:17:26 -070040#include <mach/msm_xo.h>
Manu Gautam91223e02011-11-08 15:27:22 +053041#include <mach/msm_iomap.h>
Rajkumar Raghupathyad8d68e2013-06-06 17:48:39 +053042#include <linux/debugfs.h>
Manu Gautam91223e02011-11-08 15:27:22 +053043
44#define MSM_USB_BASE (hcd->regs)
45
Hemant Kumar8c0f2a82012-05-03 19:17:26 -070046#define PDEV_NAME_LEN 20
47
Manu Gautam91223e02011-11-08 15:27:22 +053048struct msm_hcd {
49 struct ehci_hcd ehci;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +053050 spinlock_t wakeup_lock;
Manu Gautam91223e02011-11-08 15:27:22 +053051 struct device *dev;
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +053052 struct clk *xo_clk;
Manu Gautam91223e02011-11-08 15:27:22 +053053 struct clk *iface_clk;
54 struct clk *core_clk;
55 struct clk *alt_core_clk;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +053056 struct clk *phy_sleep_clk;
Manu Gautam91223e02011-11-08 15:27:22 +053057 struct regulator *hsusb_vddcx;
58 struct regulator *hsusb_3p3;
59 struct regulator *hsusb_1p8;
60 struct regulator *vbus;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -070061 struct msm_xo_voter *xo_handle;
Manu Gautam91223e02011-11-08 15:27:22 +053062 bool async_int;
Hemant Kumar56925352012-02-13 16:59:52 -080063 bool vbus_on;
Manu Gautam91223e02011-11-08 15:27:22 +053064 atomic_t in_lpm;
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +053065 int pmic_gpio_dp_irq;
66 bool pmic_gpio_dp_irq_enabled;
67 uint32_t pmic_gpio_int_cnt;
68 atomic_t pm_usage_cnt;
Hemant Kumar153f9972013-06-19 16:49:45 -070069 struct wakeup_source ws;
Chiranjeevi Velempatice169582012-08-25 14:15:41 +053070 struct work_struct phy_susp_fail_work;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +053071 int async_irq;
72 bool async_irq_enabled;
73 uint32_t async_int_cnt;
Hemant Kumara7d531d2013-06-18 22:12:18 -070074 int resume_gpio;
Hemant Kumar17011ce2013-06-10 17:40:08 -070075
76 int wakeup_int_cnt;
77 bool wakeup_irq_enabled;
78 int wakeup_irq;
Manu Gautam91223e02011-11-08 15:27:22 +053079};
80
81static inline struct msm_hcd *hcd_to_mhcd(struct usb_hcd *hcd)
82{
83 return (struct msm_hcd *) (hcd->hcd_priv);
84}
85
86static inline struct usb_hcd *mhcd_to_hcd(struct msm_hcd *mhcd)
87{
88 return container_of((void *) mhcd, struct usb_hcd, hcd_priv);
89}
90
91#define HSUSB_PHY_3P3_VOL_MIN 3050000 /* uV */
92#define HSUSB_PHY_3P3_VOL_MAX 3300000 /* uV */
93#define HSUSB_PHY_3P3_HPM_LOAD 50000 /* uA */
94
95#define HSUSB_PHY_1P8_VOL_MIN 1800000 /* uV */
96#define HSUSB_PHY_1P8_VOL_MAX 1800000 /* uV */
97#define HSUSB_PHY_1P8_HPM_LOAD 50000 /* uA */
98
99#define HSUSB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
100#define HSUSB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
101#define HSUSB_PHY_VDD_DIG_LOAD 49360 /* uA */
102
103static int msm_ehci_init_vddcx(struct msm_hcd *mhcd, int init)
104{
105 int ret = 0;
106
107 if (!init)
108 goto disable_reg;
109
Mayank Rana2f416c22012-03-24 05:23:25 +0530110 mhcd->hsusb_vddcx = devm_regulator_get(mhcd->dev, "HSUSB_VDDCX");
Manu Gautam91223e02011-11-08 15:27:22 +0530111 if (IS_ERR(mhcd->hsusb_vddcx)) {
112 dev_err(mhcd->dev, "unable to get ehci vddcx\n");
113 return PTR_ERR(mhcd->hsusb_vddcx);
114 }
115
116 ret = regulator_set_voltage(mhcd->hsusb_vddcx,
117 HSUSB_PHY_VDD_DIG_VOL_MIN,
118 HSUSB_PHY_VDD_DIG_VOL_MAX);
119 if (ret) {
120 dev_err(mhcd->dev, "unable to set the voltage"
121 "for ehci vddcx\n");
Mayank Rana2f416c22012-03-24 05:23:25 +0530122 return ret;
Manu Gautam91223e02011-11-08 15:27:22 +0530123 }
124
125 ret = regulator_set_optimum_mode(mhcd->hsusb_vddcx,
126 HSUSB_PHY_VDD_DIG_LOAD);
127 if (ret < 0) {
128 dev_err(mhcd->dev, "%s: Unable to set optimum mode of the"
129 " regulator: VDDCX\n", __func__);
130 goto reg_optimum_mode_err;
131 }
132
133 ret = regulator_enable(mhcd->hsusb_vddcx);
134 if (ret) {
135 dev_err(mhcd->dev, "unable to enable ehci vddcx\n");
136 goto reg_enable_err;
137 }
138
139 return 0;
140
141disable_reg:
142 regulator_disable(mhcd->hsusb_vddcx);
143reg_enable_err:
144 regulator_set_optimum_mode(mhcd->hsusb_vddcx, 0);
145reg_optimum_mode_err:
146 regulator_set_voltage(mhcd->hsusb_vddcx, 0,
147 HSUSB_PHY_VDD_DIG_VOL_MIN);
Manu Gautam91223e02011-11-08 15:27:22 +0530148 return ret;
149
150}
151
152static int msm_ehci_ldo_init(struct msm_hcd *mhcd, int init)
153{
154 int rc = 0;
155
156 if (!init)
157 goto put_1p8;
158
Mayank Rana2f416c22012-03-24 05:23:25 +0530159 mhcd->hsusb_3p3 = devm_regulator_get(mhcd->dev, "HSUSB_3p3");
Manu Gautam91223e02011-11-08 15:27:22 +0530160 if (IS_ERR(mhcd->hsusb_3p3)) {
161 dev_err(mhcd->dev, "unable to get hsusb 3p3\n");
162 return PTR_ERR(mhcd->hsusb_3p3);
163 }
164
165 rc = regulator_set_voltage(mhcd->hsusb_3p3,
166 HSUSB_PHY_3P3_VOL_MIN, HSUSB_PHY_3P3_VOL_MAX);
167 if (rc) {
168 dev_err(mhcd->dev, "unable to set voltage level for"
169 "hsusb 3p3\n");
Mayank Rana2f416c22012-03-24 05:23:25 +0530170 return rc;
Manu Gautam91223e02011-11-08 15:27:22 +0530171 }
Mayank Rana2f416c22012-03-24 05:23:25 +0530172 mhcd->hsusb_1p8 = devm_regulator_get(mhcd->dev, "HSUSB_1p8");
Manu Gautam91223e02011-11-08 15:27:22 +0530173 if (IS_ERR(mhcd->hsusb_1p8)) {
174 dev_err(mhcd->dev, "unable to get hsusb 1p8\n");
175 rc = PTR_ERR(mhcd->hsusb_1p8);
176 goto put_3p3_lpm;
177 }
178 rc = regulator_set_voltage(mhcd->hsusb_1p8,
179 HSUSB_PHY_1P8_VOL_MIN, HSUSB_PHY_1P8_VOL_MAX);
180 if (rc) {
181 dev_err(mhcd->dev, "unable to set voltage level for"
182 "hsusb 1p8\n");
183 goto put_1p8;
184 }
185
186 return 0;
187
188put_1p8:
189 regulator_set_voltage(mhcd->hsusb_1p8, 0, HSUSB_PHY_1P8_VOL_MAX);
Manu Gautam91223e02011-11-08 15:27:22 +0530190put_3p3_lpm:
191 regulator_set_voltage(mhcd->hsusb_3p3, 0, HSUSB_PHY_3P3_VOL_MAX);
Manu Gautam91223e02011-11-08 15:27:22 +0530192
193 return rc;
194}
195
196#ifdef CONFIG_PM_SLEEP
Hemant Kumar441356be2012-02-13 16:12:49 -0800197#define HSUSB_PHY_SUSP_DIG_VOL_P50 500000
198#define HSUSB_PHY_SUSP_DIG_VOL_P75 750000
Manu Gautam91223e02011-11-08 15:27:22 +0530199static int msm_ehci_config_vddcx(struct msm_hcd *mhcd, int high)
200{
Hemant Kumar441356be2012-02-13 16:12:49 -0800201 struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530202 int max_vol = HSUSB_PHY_VDD_DIG_VOL_MAX;
203 int min_vol;
204 int ret;
205
Hemant Kumar441356be2012-02-13 16:12:49 -0800206 pdata = mhcd->dev->platform_data;
207
Manu Gautam91223e02011-11-08 15:27:22 +0530208 if (high)
209 min_vol = HSUSB_PHY_VDD_DIG_VOL_MIN;
Hemant Kumar441356be2012-02-13 16:12:49 -0800210 else if (pdata && pdata->dock_connect_irq &&
211 !irq_read_line(pdata->dock_connect_irq))
212 min_vol = HSUSB_PHY_SUSP_DIG_VOL_P75;
Manu Gautam91223e02011-11-08 15:27:22 +0530213 else
Hemant Kumar441356be2012-02-13 16:12:49 -0800214 min_vol = HSUSB_PHY_SUSP_DIG_VOL_P50;
Manu Gautam91223e02011-11-08 15:27:22 +0530215
216 ret = regulator_set_voltage(mhcd->hsusb_vddcx, min_vol, max_vol);
217 if (ret) {
218 dev_err(mhcd->dev, "%s: unable to set the voltage of regulator"
219 " HSUSB_VDDCX\n", __func__);
220 return ret;
221 }
222
223 dev_dbg(mhcd->dev, "%s: min_vol:%d max_vol:%d\n", __func__, min_vol,
224 max_vol);
225
226 return ret;
227}
228#else
229static int msm_ehci_config_vddcx(struct msm_hcd *mhcd, int high)
230{
231 return 0;
232}
233#endif
234
Manu Gautam91223e02011-11-08 15:27:22 +0530235static void msm_ehci_vbus_power(struct msm_hcd *mhcd, bool on)
236{
237 int ret;
238
239 if (!mhcd->vbus) {
240 pr_err("vbus is NULL.");
241 return;
242 }
Hemant Kumar56925352012-02-13 16:59:52 -0800243
244 if (mhcd->vbus_on == on)
245 return;
246
Manu Gautam91223e02011-11-08 15:27:22 +0530247 if (on) {
248 ret = regulator_enable(mhcd->vbus);
249 if (ret) {
250 pr_err("unable to enable vbus\n");
251 return;
252 }
Hemant Kumar56925352012-02-13 16:59:52 -0800253 mhcd->vbus_on = true;
Manu Gautam91223e02011-11-08 15:27:22 +0530254 } else {
255 ret = regulator_disable(mhcd->vbus);
256 if (ret) {
257 pr_err("unable to disable vbus\n");
258 return;
259 }
Hemant Kumar56925352012-02-13 16:59:52 -0800260 mhcd->vbus_on = false;
Manu Gautam91223e02011-11-08 15:27:22 +0530261 }
262}
263
Hemant Kumar56925352012-02-13 16:59:52 -0800264static irqreturn_t msm_ehci_dock_connect_irq(int irq, void *data)
265{
266 const struct msm_usb_host_platform_data *pdata;
267 struct msm_hcd *mhcd = data;
268 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
269
270 pdata = mhcd->dev->platform_data;
271
272 if (atomic_read(&mhcd->in_lpm))
273 usb_hcd_resume_root_hub(hcd);
274
275 if (irq_read_line(pdata->dock_connect_irq)) {
276 dev_dbg(mhcd->dev, "%s:Dock removed disable vbus\n", __func__);
277 msm_ehci_vbus_power(mhcd, 0);
278 } else {
279 dev_dbg(mhcd->dev, "%s:Dock connected enable vbus\n", __func__);
280 msm_ehci_vbus_power(mhcd, 1);
281 }
282
283 return IRQ_HANDLED;
284}
285
286static int msm_ehci_init_vbus(struct msm_hcd *mhcd, int init)
287{
288 int rc = 0;
289 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
290 const struct msm_usb_host_platform_data *pdata;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530291 int ret = 0;
Hemant Kumar56925352012-02-13 16:59:52 -0800292
293 pdata = mhcd->dev->platform_data;
294
295 if (!init) {
Hemant Kumar56925352012-02-13 16:59:52 -0800296 if (pdata && pdata->dock_connect_irq)
297 free_irq(pdata->dock_connect_irq, mhcd);
298 return rc;
299 }
300
Mayank Rana2f416c22012-03-24 05:23:25 +0530301 mhcd->vbus = devm_regulator_get(mhcd->dev, "vbus");
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530302 ret = PTR_ERR(mhcd->vbus);
303 if (ret == -EPROBE_DEFER) {
304 pr_debug("failed to get vbus handle, defer probe\n");
305 return ret;
306 } else if (IS_ERR(mhcd->vbus)) {
Hemant Kumar56925352012-02-13 16:59:52 -0800307 pr_err("Unable to get vbus\n");
308 return -ENODEV;
309 }
310
311 if (pdata) {
312 hcd->power_budget = pdata->power_budget;
313
314 if (pdata->dock_connect_irq) {
315 rc = request_threaded_irq(pdata->dock_connect_irq, NULL,
316 msm_ehci_dock_connect_irq,
317 IRQF_TRIGGER_FALLING |
318 IRQF_TRIGGER_RISING |
319 IRQF_ONESHOT, "msm_ehci_host", mhcd);
320 if (!rc)
321 enable_irq_wake(pdata->dock_connect_irq);
322 }
323 }
324 return rc;
325}
326
Manu Gautam91223e02011-11-08 15:27:22 +0530327static int msm_ehci_ldo_enable(struct msm_hcd *mhcd, int on)
328{
329 int ret = 0;
330
331 if (IS_ERR(mhcd->hsusb_1p8)) {
332 dev_err(mhcd->dev, "%s: HSUSB_1p8 is not initialized\n",
333 __func__);
334 return -ENODEV;
335 }
336
337 if (IS_ERR(mhcd->hsusb_3p3)) {
338 dev_err(mhcd->dev, "%s: HSUSB_3p3 is not initialized\n",
339 __func__);
340 return -ENODEV;
341 }
342
343 if (on) {
344 ret = regulator_set_optimum_mode(mhcd->hsusb_1p8,
345 HSUSB_PHY_1P8_HPM_LOAD);
346 if (ret < 0) {
347 dev_err(mhcd->dev, "%s: Unable to set HPM of the"
348 " regulator: HSUSB_1p8\n", __func__);
349 return ret;
350 }
351
352 ret = regulator_enable(mhcd->hsusb_1p8);
353 if (ret) {
354 dev_err(mhcd->dev, "%s: unable to enable the hsusb"
355 " 1p8\n", __func__);
356 regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
357 return ret;
358 }
359
360 ret = regulator_set_optimum_mode(mhcd->hsusb_3p3,
361 HSUSB_PHY_3P3_HPM_LOAD);
362 if (ret < 0) {
363 dev_err(mhcd->dev, "%s: Unable to set HPM of the "
364 "regulator: HSUSB_3p3\n", __func__);
365 regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
366 regulator_disable(mhcd->hsusb_1p8);
367 return ret;
368 }
369
370 ret = regulator_enable(mhcd->hsusb_3p3);
371 if (ret) {
372 dev_err(mhcd->dev, "%s: unable to enable the "
373 "hsusb 3p3\n", __func__);
374 regulator_set_optimum_mode(mhcd->hsusb_3p3, 0);
375 regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
376 regulator_disable(mhcd->hsusb_1p8);
377 return ret;
378 }
379
380 } else {
381 ret = regulator_disable(mhcd->hsusb_1p8);
382 if (ret) {
383 dev_err(mhcd->dev, "%s: unable to disable the "
384 "hsusb 1p8\n", __func__);
385 return ret;
386 }
387
388 ret = regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
389 if (ret < 0)
390 dev_err(mhcd->dev, "%s: Unable to set LPM of the "
391 "regulator: HSUSB_1p8\n", __func__);
392
393 ret = regulator_disable(mhcd->hsusb_3p3);
394 if (ret) {
395 dev_err(mhcd->dev, "%s: unable to disable the "
396 "hsusb 3p3\n", __func__);
397 return ret;
398 }
399 ret = regulator_set_optimum_mode(mhcd->hsusb_3p3, 0);
400 if (ret < 0)
401 dev_err(mhcd->dev, "%s: Unable to set LPM of the "
402 "regulator: HSUSB_3p3\n", __func__);
403 }
404
405 dev_dbg(mhcd->dev, "reg (%s)\n", on ? "HPM" : "LPM");
406
407 return ret < 0 ? ret : 0;
408}
409
410
411#define ULPI_IO_TIMEOUT_USECS (10 * 1000)
412static int msm_ulpi_read(struct msm_hcd *mhcd, u32 reg)
413{
414 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
415 unsigned long timeout;
416
417 /* initiate read operation */
418 writel_relaxed(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
419 USB_ULPI_VIEWPORT);
420
421 /* wait for completion */
422 timeout = jiffies + usecs_to_jiffies(ULPI_IO_TIMEOUT_USECS);
423 while (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN) {
424 if (time_after(jiffies, timeout)) {
425 dev_err(mhcd->dev, "msm_ulpi_read: timeout %08x\n",
426 readl_relaxed(USB_ULPI_VIEWPORT));
427 return -ETIMEDOUT;
428 }
429 udelay(1);
430 }
431
432 return ULPI_DATA_READ(readl_relaxed(USB_ULPI_VIEWPORT));
433}
434
435
436static int msm_ulpi_write(struct msm_hcd *mhcd, u32 val, u32 reg)
437{
438 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
439 unsigned long timeout;
440
441 /* initiate write operation */
442 writel_relaxed(ULPI_RUN | ULPI_WRITE |
443 ULPI_ADDR(reg) | ULPI_DATA(val),
444 USB_ULPI_VIEWPORT);
445
446 /* wait for completion */
447 timeout = jiffies + usecs_to_jiffies(ULPI_IO_TIMEOUT_USECS);
448 while (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN) {
449 if (time_after(jiffies, timeout)) {
450 dev_err(mhcd->dev, "msm_ulpi_write: timeout\n");
451 return -ETIMEDOUT;
452 }
453 udelay(1);
454 }
455
456 return 0;
457}
458
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530459/**
460 * Do hard reset to USB hardware block using one of reset methodology based
461 * on availablity of alt_core_clk. There are two kinds of hardware resets.
462 * 1. Conventional synchronous reset where clocks to blocks to be ON while
463 * issuing the reset. 2. Asynchronous reset which requires clocks to be OFF.
464 */
Manu Gautam91223e02011-11-08 15:27:22 +0530465static int msm_ehci_link_clk_reset(struct msm_hcd *mhcd, bool assert)
466{
467 int ret;
468
469 if (assert) {
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530470 if (!IS_ERR(mhcd->alt_core_clk)) {
471 ret = clk_reset(mhcd->alt_core_clk, CLK_RESET_ASSERT);
472 } else {
473 /* Using asynchronous block reset to the hardware */
474 clk_disable(mhcd->iface_clk);
475 clk_disable(mhcd->core_clk);
476 ret = clk_reset(mhcd->core_clk, CLK_RESET_ASSERT);
477 }
Manu Gautam91223e02011-11-08 15:27:22 +0530478 if (ret)
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530479 dev_err(mhcd->dev, "usb clk assert failed\n");
Manu Gautam91223e02011-11-08 15:27:22 +0530480 } else {
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530481 if (!IS_ERR(mhcd->alt_core_clk)) {
482 ret = clk_reset(mhcd->alt_core_clk, CLK_RESET_DEASSERT);
483 } else {
484 ret = clk_reset(mhcd->core_clk, CLK_RESET_DEASSERT);
485 ndelay(200);
486 clk_enable(mhcd->core_clk);
487 clk_enable(mhcd->iface_clk);
488 }
Manu Gautam91223e02011-11-08 15:27:22 +0530489 if (ret)
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530490 dev_err(mhcd->dev, "usb clk deassert failed\n");
Manu Gautam91223e02011-11-08 15:27:22 +0530491 }
492
493 return ret;
494}
495
496static int msm_ehci_phy_reset(struct msm_hcd *mhcd)
497{
498 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530499 struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530500 u32 val;
501 int ret;
502 int retries;
503
504 ret = msm_ehci_link_clk_reset(mhcd, 1);
505 if (ret)
506 return ret;
507
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530508 usleep_range(10, 12);
Manu Gautam91223e02011-11-08 15:27:22 +0530509
510 ret = msm_ehci_link_clk_reset(mhcd, 0);
511 if (ret)
512 return ret;
513
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530514 pdata = mhcd->dev->platform_data;
515 if (pdata && pdata->use_sec_phy)
516 /* select secondary phy if offset is set for USB operation */
517 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
518 USB_PHY_CTRL2);
Manu Gautam91223e02011-11-08 15:27:22 +0530519 val = readl_relaxed(USB_PORTSC) & ~PORTSC_PTS_MASK;
520 writel_relaxed(val | PORTSC_PTS_ULPI, USB_PORTSC);
521
522 for (retries = 3; retries > 0; retries--) {
523 ret = msm_ulpi_write(mhcd, ULPI_FUNC_CTRL_SUSPENDM,
524 ULPI_CLR(ULPI_FUNC_CTRL));
525 if (!ret)
526 break;
527 }
528 if (!retries)
529 return -ETIMEDOUT;
530
531 /* Wakeup the PHY with a reg-access for calibration */
532 for (retries = 3; retries > 0; retries--) {
533 ret = msm_ulpi_read(mhcd, ULPI_DEBUG);
534 if (ret != -ETIMEDOUT)
535 break;
536 }
537 if (!retries)
538 return -ETIMEDOUT;
539
540 dev_info(mhcd->dev, "phy_reset: success\n");
541
542 return 0;
543}
544
545#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
546static int msm_hsusb_reset(struct msm_hcd *mhcd)
547{
548 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530549 struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530550 unsigned long timeout;
551 int ret;
552
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530553 if (!IS_ERR(mhcd->alt_core_clk))
554 clk_prepare_enable(mhcd->alt_core_clk);
555
Manu Gautam91223e02011-11-08 15:27:22 +0530556 ret = msm_ehci_phy_reset(mhcd);
557 if (ret) {
558 dev_err(mhcd->dev, "phy_reset failed\n");
559 return ret;
560 }
561
562 writel_relaxed(USBCMD_RESET, USB_USBCMD);
563
564 timeout = jiffies + usecs_to_jiffies(LINK_RESET_TIMEOUT_USEC);
565 while (readl_relaxed(USB_USBCMD) & USBCMD_RESET) {
566 if (time_after(jiffies, timeout))
567 return -ETIMEDOUT;
568 udelay(1);
569 }
570
571 /* select ULPI phy */
572 writel_relaxed(0x80000000, USB_PORTSC);
573
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530574 pdata = mhcd->dev->platform_data;
575 if (pdata && pdata->use_sec_phy)
576 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
577 USB_PHY_CTRL2);
578
Manu Gautam91223e02011-11-08 15:27:22 +0530579 msleep(100);
580
581 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530582 writel_relaxed(0x08, USB_AHBMODE);
Manu Gautam91223e02011-11-08 15:27:22 +0530583
584 /* Ensure that RESET operation is completed before turning off clock */
585 mb();
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530586
587 if (!IS_ERR(mhcd->alt_core_clk))
588 clk_disable_unprepare(mhcd->alt_core_clk);
Manu Gautam91223e02011-11-08 15:27:22 +0530589
590 /*rising edge interrupts with Dp rise and fall enabled*/
591 msm_ulpi_write(mhcd, ULPI_INT_DP, ULPI_USB_INT_EN_RISE);
592 msm_ulpi_write(mhcd, ULPI_INT_DP, ULPI_USB_INT_EN_FALL);
593
594 /*Clear the PHY interrupts by reading the PHY interrupt latch register*/
595 msm_ulpi_read(mhcd, ULPI_USB_INT_LATCH);
596
597 return 0;
598}
599
Chiranjeevi Velempatice169582012-08-25 14:15:41 +0530600static void msm_ehci_phy_susp_fail_work(struct work_struct *w)
601{
602 struct msm_hcd *mhcd = container_of(w, struct msm_hcd,
603 phy_susp_fail_work);
604 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
605
606 msm_ehci_vbus_power(mhcd, 0);
607 usb_remove_hcd(hcd);
608 msm_hsusb_reset(mhcd);
609 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
610 msm_ehci_vbus_power(mhcd, 1);
611}
612
Hemant Kumar8d3aca32013-06-05 18:53:54 -0700613#define PHY_SUSP_TIMEOUT_MSEC 500
614#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
Manu Gautam91223e02011-11-08 15:27:22 +0530615
616#ifdef CONFIG_PM_SLEEP
617static int msm_ehci_suspend(struct msm_hcd *mhcd)
618{
619 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
620 unsigned long timeout;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700621 int ret;
Manu Gautam91223e02011-11-08 15:27:22 +0530622 u32 portsc;
623
624 if (atomic_read(&mhcd->in_lpm)) {
625 dev_dbg(mhcd->dev, "%s called in lpm\n", __func__);
626 return 0;
627 }
628
629 disable_irq(hcd->irq);
630
Hemant Kumar17011ce2013-06-10 17:40:08 -0700631 /* make sure we don't race against a remote wakeup */
632 if (test_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags) ||
633 readl_relaxed(USB_PORTSC) & PORT_RESUME) {
634 dev_dbg(mhcd->dev, "wakeup pending, aborting suspend\n");
635 enable_irq(hcd->irq);
636 return -EBUSY;
637 }
638
Hemant Kumar8d3aca32013-06-05 18:53:54 -0700639 /* If port is enabled wait 5ms for PHCD to come up. Reset PHY
640 * and link if it fails to do so.
641 * If port is not enabled set the PHCD bit and poll for it to
642 * come up with in 500ms. Reset phy and link if it fails to do so.
Manu Gautam91223e02011-11-08 15:27:22 +0530643 */
644 portsc = readl_relaxed(USB_PORTSC);
Hemant Kumar8d3aca32013-06-05 18:53:54 -0700645 if (portsc & PORT_PE) {
Manu Gautam91223e02011-11-08 15:27:22 +0530646
Hemant Kumar8d3aca32013-06-05 18:53:54 -0700647 usleep_range(5000, 5000);
648
649 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD)) {
650 dev_err(mhcd->dev,
651 "Unable to suspend PHY. portsc: %8x\n",
652 readl_relaxed(USB_PORTSC));
653 goto reset_phy_and_link;
654 }
655 } else {
656 writel_relaxed(portsc | PORTSC_PHCD, USB_PORTSC);
657
658 timeout = jiffies + msecs_to_jiffies(PHY_SUSP_TIMEOUT_MSEC);
Manu Gautam91223e02011-11-08 15:27:22 +0530659 while (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD)) {
660 if (time_after(jiffies, timeout)) {
Hemant Kumar8d3aca32013-06-05 18:53:54 -0700661 dev_err(mhcd->dev,
662 "Unable to suspend PHY. portsc: %8x\n",
663 readl_relaxed(USB_PORTSC));
664 goto reset_phy_and_link;
Manu Gautam91223e02011-11-08 15:27:22 +0530665 }
Hemant Kumar8d3aca32013-06-05 18:53:54 -0700666 usleep_range(10000, 10000);
Manu Gautam91223e02011-11-08 15:27:22 +0530667 }
668 }
669
670 /*
671 * PHY has capability to generate interrupt asynchronously in low
672 * power mode (LPM). This interrupt is level triggered. So USB IRQ
673 * line must be disabled till async interrupt enable bit is cleared
674 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
Hemant Kumar17011ce2013-06-10 17:40:08 -0700675 * block data communication from PHY. Enable asynchronous interrupt
676 * only when wakeup gpio IRQ is not present.
Manu Gautam91223e02011-11-08 15:27:22 +0530677 */
Hemant Kumar17011ce2013-06-10 17:40:08 -0700678 if (mhcd->wakeup_irq)
679 writel_relaxed(readl_relaxed(USB_USBCMD) | ULPI_STP_CTRL,
680 USB_USBCMD);
681 else
682 writel_relaxed(readl_relaxed(USB_USBCMD) | ASYNC_INTR_CTRL |
Manu Gautam91223e02011-11-08 15:27:22 +0530683 ULPI_STP_CTRL, USB_USBCMD);
684
685 /*
686 * Ensure that hardware is put in low power mode before
687 * clocks are turned OFF and VDD is allowed to minimize.
688 */
689 mb();
690
691 clk_disable_unprepare(mhcd->iface_clk);
692 clk_disable_unprepare(mhcd->core_clk);
693
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700694 /* usb phy does not require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +0530695 if (!IS_ERR(mhcd->xo_clk)) {
696 clk_disable_unprepare(mhcd->xo_clk);
697 } else {
698 ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_OFF);
699 if (ret)
700 dev_err(mhcd->dev, "%s failed to devote for TCXO %d\n",
701 __func__, ret);
702 }
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700703
Manu Gautam91223e02011-11-08 15:27:22 +0530704 msm_ehci_config_vddcx(mhcd, 0);
705
706 atomic_set(&mhcd->in_lpm, 1);
707 enable_irq(hcd->irq);
Hemant Kumar17011ce2013-06-10 17:40:08 -0700708
709 if (mhcd->wakeup_irq) {
710 mhcd->wakeup_irq_enabled = 1;
711 enable_irq_wake(mhcd->wakeup_irq);
712 enable_irq(mhcd->wakeup_irq);
713 }
714
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +0530715 if (mhcd->pmic_gpio_dp_irq) {
716 mhcd->pmic_gpio_dp_irq_enabled = 1;
717 enable_irq_wake(mhcd->pmic_gpio_dp_irq);
718 enable_irq(mhcd->pmic_gpio_dp_irq);
719 }
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530720 if (mhcd->async_irq) {
721 mhcd->async_irq_enabled = 1;
722 enable_irq_wake(mhcd->async_irq);
723 enable_irq(mhcd->async_irq);
724 }
Hemant Kumar153f9972013-06-19 16:49:45 -0700725 pm_relax(mhcd->dev);
Manu Gautam91223e02011-11-08 15:27:22 +0530726
727 dev_info(mhcd->dev, "EHCI USB in low power mode\n");
728
729 return 0;
Hemant Kumar8d3aca32013-06-05 18:53:54 -0700730
731reset_phy_and_link:
732 schedule_work(&mhcd->phy_susp_fail_work);
733 return -ETIMEDOUT;
Manu Gautam91223e02011-11-08 15:27:22 +0530734}
735
736static int msm_ehci_resume(struct msm_hcd *mhcd)
737{
738 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
739 unsigned long timeout;
740 unsigned temp;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700741 int ret;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530742 unsigned long flags;
Manu Gautam91223e02011-11-08 15:27:22 +0530743
744 if (!atomic_read(&mhcd->in_lpm)) {
745 dev_dbg(mhcd->dev, "%s called in !in_lpm\n", __func__);
746 return 0;
747 }
748
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +0530749 if (mhcd->pmic_gpio_dp_irq_enabled) {
750 disable_irq_wake(mhcd->pmic_gpio_dp_irq);
751 disable_irq_nosync(mhcd->pmic_gpio_dp_irq);
752 mhcd->pmic_gpio_dp_irq_enabled = 0;
753 }
Hemant Kumar17011ce2013-06-10 17:40:08 -0700754
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530755 spin_lock_irqsave(&mhcd->wakeup_lock, flags);
756 if (mhcd->async_irq_enabled) {
757 disable_irq_wake(mhcd->async_irq);
758 disable_irq_nosync(mhcd->async_irq);
759 mhcd->async_irq_enabled = 0;
760 }
Hemant Kumar17011ce2013-06-10 17:40:08 -0700761
762 if (mhcd->wakeup_irq) {
763 if (mhcd->wakeup_irq_enabled) {
764 disable_irq_wake(mhcd->wakeup_irq);
765 disable_irq_nosync(mhcd->wakeup_irq);
766 mhcd->wakeup_irq_enabled = 0;
767 }
768 }
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530769 spin_unlock_irqrestore(&mhcd->wakeup_lock, flags);
770
Hemant Kumar153f9972013-06-19 16:49:45 -0700771 pm_stay_awake(mhcd->dev);
Manu Gautam91223e02011-11-08 15:27:22 +0530772
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700773 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +0530774 if (!IS_ERR(mhcd->xo_clk)) {
775 clk_prepare_enable(mhcd->xo_clk);
776 } else {
777 ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_ON);
778 if (ret)
779 dev_err(mhcd->dev, "%s failed to vote for TCXO D0 %d\n",
780 __func__, ret);
781 }
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700782
Manu Gautam91223e02011-11-08 15:27:22 +0530783 clk_prepare_enable(mhcd->core_clk);
784 clk_prepare_enable(mhcd->iface_clk);
785
786 msm_ehci_config_vddcx(mhcd, 1);
787
788 temp = readl_relaxed(USB_USBCMD);
789 temp &= ~ASYNC_INTR_CTRL;
790 temp &= ~ULPI_STP_CTRL;
791 writel_relaxed(temp, USB_USBCMD);
792
793 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD))
794 goto skip_phy_resume;
795
796 temp = readl_relaxed(USB_PORTSC) & ~PORTSC_PHCD;
797 writel_relaxed(temp, USB_PORTSC);
798
799 timeout = jiffies + usecs_to_jiffies(PHY_RESUME_TIMEOUT_USEC);
800 while ((readl_relaxed(USB_PORTSC) & PORTSC_PHCD) ||
801 !(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_SYNC_STATE)) {
802 if (time_after(jiffies, timeout)) {
803 /*This is a fatal error. Reset the link and PHY*/
804 dev_err(mhcd->dev, "Unable to resume USB. Resetting the h/w\n");
805 msm_hsusb_reset(mhcd);
806 break;
807 }
808 udelay(1);
809 }
810
811skip_phy_resume:
812
Chiranjeevi Velempati35d46ab2012-07-18 20:36:53 +0530813 usb_hcd_resume_root_hub(hcd);
Manu Gautam91223e02011-11-08 15:27:22 +0530814 atomic_set(&mhcd->in_lpm, 0);
815
816 if (mhcd->async_int) {
817 mhcd->async_int = false;
818 pm_runtime_put_noidle(mhcd->dev);
819 enable_irq(hcd->irq);
820 }
821
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +0530822 if (atomic_read(&mhcd->pm_usage_cnt)) {
823 atomic_set(&mhcd->pm_usage_cnt, 0);
824 pm_runtime_put_noidle(mhcd->dev);
825 }
826
Manu Gautam91223e02011-11-08 15:27:22 +0530827 dev_info(mhcd->dev, "EHCI USB exited from low power mode\n");
828
829 return 0;
830}
831#endif
832
833static irqreturn_t msm_ehci_irq(struct usb_hcd *hcd)
834{
835 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
836
837 if (atomic_read(&mhcd->in_lpm)) {
Hemant Kumar17011ce2013-06-10 17:40:08 -0700838 dev_dbg(mhcd->dev, "phy async intr\n");
Manu Gautam91223e02011-11-08 15:27:22 +0530839 disable_irq_nosync(hcd->irq);
840 mhcd->async_int = true;
841 pm_runtime_get(mhcd->dev);
842 return IRQ_HANDLED;
843 }
844
845 return ehci_irq(hcd);
846}
847
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530848static irqreturn_t msm_async_irq(int irq, void *data)
849{
850 struct msm_hcd *mhcd = data;
851 int ret;
852
853 mhcd->async_int_cnt++;
854 dev_dbg(mhcd->dev, "%s: hsusb host remote wakeup interrupt cnt: %u\n",
855 __func__, mhcd->async_int_cnt);
856
Hemant Kumar153f9972013-06-19 16:49:45 -0700857 pm_stay_awake(mhcd->dev);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530858
859 spin_lock(&mhcd->wakeup_lock);
860 if (mhcd->async_irq_enabled) {
861 mhcd->async_irq_enabled = 0;
862 disable_irq_wake(irq);
863 disable_irq_nosync(irq);
864 }
865 spin_unlock(&mhcd->wakeup_lock);
866
867 if (!atomic_read(&mhcd->pm_usage_cnt)) {
868 ret = pm_runtime_get(mhcd->dev);
869 if ((ret == 1) || (ret == -EINPROGRESS))
870 pm_runtime_put_noidle(mhcd->dev);
871 else
872 atomic_set(&mhcd->pm_usage_cnt, 1);
873 }
874
875 return IRQ_HANDLED;
876}
877
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +0530878static irqreturn_t msm_ehci_host_wakeup_irq(int irq, void *data)
879{
880
881 struct msm_hcd *mhcd = data;
882
883 mhcd->pmic_gpio_int_cnt++;
884 dev_dbg(mhcd->dev, "%s: hsusb host remote wakeup interrupt cnt: %u\n",
885 __func__, mhcd->pmic_gpio_int_cnt);
886
887
Hemant Kumar153f9972013-06-19 16:49:45 -0700888 pm_stay_awake(mhcd->dev);
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +0530889
890 if (mhcd->pmic_gpio_dp_irq_enabled) {
891 mhcd->pmic_gpio_dp_irq_enabled = 0;
892 disable_irq_wake(irq);
893 disable_irq_nosync(irq);
894 }
895
896 if (!atomic_read(&mhcd->pm_usage_cnt)) {
897 atomic_set(&mhcd->pm_usage_cnt, 1);
898 pm_runtime_get(mhcd->dev);
899 }
900
901 return IRQ_HANDLED;
902}
903
Manu Gautam91223e02011-11-08 15:27:22 +0530904static int msm_ehci_reset(struct usb_hcd *hcd)
905{
906 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530907 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
908 struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530909 int retval;
910
911 ehci->caps = USB_CAPLENGTH;
912 ehci->regs = USB_CAPLENGTH +
913 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
914 dbg_hcs_params(ehci, "reset");
915 dbg_hcc_params(ehci, "reset");
916
917 /* cache the data to minimize the chip reads*/
918 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
919
920 hcd->has_tt = 1;
921 ehci->sbrn = HCD_USB2;
922
923 retval = ehci_halt(ehci);
924 if (retval)
925 return retval;
926
927 /* data structure init */
928 retval = ehci_init(hcd);
929 if (retval)
930 return retval;
931
932 retval = ehci_reset(ehci);
933 if (retval)
934 return retval;
935
936 /* bursts of unspecified length. */
937 writel_relaxed(0, USB_AHBBURST);
938 /* Use the AHB transactor */
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530939 writel_relaxed(0x08, USB_AHBMODE);
Manu Gautam91223e02011-11-08 15:27:22 +0530940 /* Disable streaming mode and select host mode */
941 writel_relaxed(0x13, USB_USBMODE);
942
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530943 pdata = mhcd->dev->platform_data;
944 if (pdata && pdata->use_sec_phy)
945 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
946 USB_PHY_CTRL2);
947
Manu Gautam91223e02011-11-08 15:27:22 +0530948 ehci_port_power(ehci, 1);
949 return 0;
950}
951
Hemant Kumara7d531d2013-06-18 22:12:18 -0700952static int msm_ehci_bus_resume_with_gpio(struct usb_hcd *hcd)
953{
954 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
955 int ret;
956
957 gpio_direction_output(mhcd->resume_gpio, 1);
958
959 ret = ehci_bus_resume(hcd);
960
961 gpio_direction_output(mhcd->resume_gpio, 0);
962
963 return ret;
964}
965
Rajkumar Raghupathyad8d68e2013-06-06 17:48:39 +0530966#if defined(CONFIG_DEBUG_FS)
967static u32 addr;
968#define BUF_SIZE 32
969static ssize_t debug_read_phy_data(struct file *file, char __user *ubuf,
970 size_t count, loff_t *ppos)
971{
972 struct msm_hcd *mhcd = file->private_data;
973 char *kbuf;
974 size_t c = 0;
975 u32 data = 0;
976 int ret = 0;
977
978 kbuf = kzalloc(sizeof(char) * BUF_SIZE, GFP_KERNEL);
979 pm_runtime_get(mhcd->dev);
980 data = msm_ulpi_read(mhcd, addr);
981 pm_runtime_put(mhcd->dev);
982 if (data < 0) {
983 dev_err(mhcd->dev,
984 "%s(): ulpi read timeout\n", __func__);
985 return -ETIMEDOUT;
986 }
987
988 c = scnprintf(kbuf, BUF_SIZE, "addr: 0x%x: data: 0x%x\n", addr, data);
989
990 ret = simple_read_from_buffer(ubuf, count, ppos, kbuf, c);
991
992 kfree(kbuf);
993
994 return ret;
995}
996
997static ssize_t debug_write_phy_data(struct file *file, const char __user *buf,
998 size_t count, loff_t *ppos)
999{
1000 struct msm_hcd *mhcd = file->private_data;
1001 char kbuf[10];
1002 u32 data = 0;
1003
1004 memset(kbuf, 0, 10);
1005
1006 if (copy_from_user(kbuf, buf, count > 10 ? 10 : count))
1007 return -EFAULT;
1008
1009 if (sscanf(kbuf, "%x", &data) != 1)
1010 return -EINVAL;
1011
1012 pm_runtime_get(mhcd->dev);
1013 if (msm_ulpi_write(mhcd, data, addr) < 0) {
1014 dev_err(mhcd->dev,
1015 "%s(): ulpi write timeout\n", __func__);
1016 return -ETIMEDOUT;
1017 }
1018 pm_runtime_put(mhcd->dev);
1019
1020 return count;
1021}
1022
1023static ssize_t debug_phy_write_addr(struct file *file, const char __user *buf,
1024 size_t count, loff_t *ppos)
1025{
1026 char kbuf[10];
1027 u32 temp;
1028
1029 memset(kbuf, 0, 10);
1030
1031 if (copy_from_user(kbuf, buf, count > 10 ? 10 : count))
1032 return -EFAULT;
1033
1034 if (sscanf(kbuf, "%x", &temp) != 1)
1035 return -EINVAL;
1036
1037 if (temp > 0x3F)
1038 return -EINVAL;
1039
1040 addr = temp;
1041
1042 return count;
1043}
1044
1045static int debug_open(struct inode *inode, struct file *file)
1046{
1047 file->private_data = inode->i_private;
1048 return 0;
1049}
1050
1051const struct file_operations debug_rw_phy_ops = {
1052 .open = debug_open,
1053 .read = debug_read_phy_data,
1054 .write = debug_write_phy_data,
1055};
1056
1057const struct file_operations debug_write_phy_ops = {
1058 .open = debug_open,
1059 .write = debug_phy_write_addr,
1060};
1061
1062static struct dentry *dent_ehci;
1063
1064static int ehci_debugfs_init(struct msm_hcd *mhcd)
1065{
1066 struct dentry *debug_phy_data;
1067 struct dentry *debug_phy_addr;
1068
1069 dent_ehci = debugfs_create_dir(dev_name(mhcd->dev), 0);
1070 if (IS_ERR(dent_ehci))
1071 return -ENOENT;
1072
1073 debug_phy_data = debugfs_create_file("phy_reg_data", 0666,
1074 dent_ehci, mhcd, &debug_rw_phy_ops);
1075 if (!debug_phy_data) {
1076 debugfs_remove(dent_ehci);
1077 return -ENOENT;
1078 }
1079
1080 debug_phy_addr = debugfs_create_file("phy_reg_addr", 0666,
1081 dent_ehci, mhcd, &debug_write_phy_ops);
1082 if (!debug_phy_addr) {
1083 debugfs_remove_recursive(dent_ehci);
1084 return -ENOENT;
1085 }
1086 return 0;
1087}
1088#else
1089static int ehci_debugfs_init(struct msm_hcd *mhcd)
1090{
1091 return 0;
1092}
1093#endif
1094
Manu Gautam91223e02011-11-08 15:27:22 +05301095static struct hc_driver msm_hc2_driver = {
1096 .description = hcd_name,
1097 .product_desc = "Qualcomm EHCI Host Controller",
1098 .hcd_priv_size = sizeof(struct msm_hcd),
1099
1100 /*
1101 * generic hardware linkage
1102 */
1103 .irq = msm_ehci_irq,
1104 .flags = HCD_USB2 | HCD_MEMORY,
1105
1106 .reset = msm_ehci_reset,
1107 .start = ehci_run,
1108
1109 .stop = ehci_stop,
1110 .shutdown = ehci_shutdown,
1111
1112 /*
1113 * managing i/o requests and associated device resources
1114 */
1115 .urb_enqueue = ehci_urb_enqueue,
1116 .urb_dequeue = ehci_urb_dequeue,
1117 .endpoint_disable = ehci_endpoint_disable,
1118 .endpoint_reset = ehci_endpoint_reset,
1119 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
1120
1121 /*
1122 * scheduling support
1123 */
1124 .get_frame_number = ehci_get_frame,
1125
1126 /*
1127 * root hub support
1128 */
1129 .hub_status_data = ehci_hub_status_data,
1130 .hub_control = ehci_hub_control,
1131 .relinquish_port = ehci_relinquish_port,
1132 .port_handed_over = ehci_port_handed_over,
1133
1134 /*
1135 * PM support
1136 */
1137 .bus_suspend = ehci_bus_suspend,
1138 .bus_resume = ehci_bus_resume,
1139};
1140
Hemant Kumar17011ce2013-06-10 17:40:08 -07001141static irqreturn_t msm_hsusb_wakeup_irq(int irq, void *data)
1142{
1143 struct msm_hcd *mhcd = data;
1144 int ret;
1145
1146 mhcd->wakeup_int_cnt++;
1147 dev_dbg(mhcd->dev, "%s: hsic remote wakeup interrupt cnt: %u\n",
1148 __func__, mhcd->wakeup_int_cnt);
1149
1150 pm_stay_awake(mhcd->dev);
1151
1152 spin_lock(&mhcd->wakeup_lock);
1153 if (mhcd->wakeup_irq_enabled) {
1154 mhcd->wakeup_irq_enabled = 0;
1155 disable_irq_wake(irq);
1156 disable_irq_nosync(irq);
1157 }
1158 spin_unlock(&mhcd->wakeup_lock);
1159
1160 if (!atomic_read(&mhcd->pm_usage_cnt)) {
1161 ret = pm_runtime_get(mhcd->dev);
1162 /*
1163 * controller runtime resume can race with us.
1164 * if we are active (ret == 1) or resuming
1165 * (ret == -EINPROGRESS), decrement the
1166 * PM usage counter before returning.
1167 */
1168 if ((ret == 1) || (ret == -EINPROGRESS)) {
1169 pm_runtime_put_noidle(mhcd->dev);
1170 } else {
1171 /* Let khubd know of hub port status change */
1172 if (mhcd->ehci.no_selective_suspend)
1173 mhcd->ehci.suspended_ports = 1;
1174 atomic_set(&mhcd->pm_usage_cnt, 1);
1175 }
1176 }
1177
1178 return IRQ_HANDLED;
1179}
1180
Manu Gautam91223e02011-11-08 15:27:22 +05301181static int msm_ehci_init_clocks(struct msm_hcd *mhcd, u32 init)
1182{
1183 int ret = 0;
1184
1185 if (!init)
1186 goto put_clocks;
1187
1188 /* 60MHz alt_core_clk is for LINK to be used during PHY RESET */
1189 mhcd->alt_core_clk = clk_get(mhcd->dev, "alt_core_clk");
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301190 if (IS_ERR(mhcd->alt_core_clk))
1191 dev_dbg(mhcd->dev, "failed to get alt_core_clk\n");
1192 else
1193 clk_set_rate(mhcd->alt_core_clk, 60000000);
Manu Gautam91223e02011-11-08 15:27:22 +05301194
1195 /* iface_clk is required for data transfers */
1196 mhcd->iface_clk = clk_get(mhcd->dev, "iface_clk");
1197 if (IS_ERR(mhcd->iface_clk)) {
1198 dev_err(mhcd->dev, "failed to get iface_clk\n");
1199 ret = PTR_ERR(mhcd->iface_clk);
1200 goto put_alt_core_clk;
1201 }
1202
1203 /* Link's protocol engine is based on pclk which must
1204 * be running >55Mhz and frequency should also not change.
1205 * Hence, vote for maximum clk frequency on its source
1206 */
1207 mhcd->core_clk = clk_get(mhcd->dev, "core_clk");
1208 if (IS_ERR(mhcd->core_clk)) {
1209 dev_err(mhcd->dev, "failed to get core_clk\n");
1210 ret = PTR_ERR(mhcd->core_clk);
1211 goto put_iface_clk;
1212 }
1213 clk_set_rate(mhcd->core_clk, INT_MAX);
1214
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301215 mhcd->phy_sleep_clk = clk_get(mhcd->dev, "sleep_clk");
1216 if (IS_ERR(mhcd->phy_sleep_clk))
1217 dev_dbg(mhcd->dev, "failed to get sleep_clk\n");
1218 else
1219 clk_prepare_enable(mhcd->phy_sleep_clk);
1220
Manu Gautam91223e02011-11-08 15:27:22 +05301221 clk_prepare_enable(mhcd->core_clk);
1222 clk_prepare_enable(mhcd->iface_clk);
1223
1224 return 0;
1225
1226put_clocks:
Jack Pham35fba312013-01-21 19:19:21 -08001227 if (!atomic_read(&mhcd->in_lpm)) {
1228 clk_disable_unprepare(mhcd->iface_clk);
1229 clk_disable_unprepare(mhcd->core_clk);
1230 }
Manu Gautam91223e02011-11-08 15:27:22 +05301231 clk_put(mhcd->core_clk);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301232 if (!IS_ERR(mhcd->phy_sleep_clk)) {
1233 clk_disable_unprepare(mhcd->phy_sleep_clk);
1234 clk_put(mhcd->phy_sleep_clk);
1235 }
Manu Gautam91223e02011-11-08 15:27:22 +05301236put_iface_clk:
1237 clk_put(mhcd->iface_clk);
1238put_alt_core_clk:
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301239 if (!IS_ERR(mhcd->alt_core_clk))
1240 clk_put(mhcd->alt_core_clk);
Manu Gautam91223e02011-11-08 15:27:22 +05301241
1242 return ret;
1243}
1244
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301245struct msm_usb_host_platform_data *ehci_msm2_dt_to_pdata(
1246 struct platform_device *pdev)
1247{
1248 struct device_node *node = pdev->dev.of_node;
1249 struct msm_usb_host_platform_data *pdata;
1250
1251 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1252 if (!pdata) {
1253 dev_err(&pdev->dev, "unable to allocate platform data\n");
1254 return NULL;
1255 }
1256
1257 pdata->use_sec_phy = of_property_read_bool(node,
1258 "qcom,usb2-enable-hsphy2");
1259 of_property_read_u32(node, "qcom,usb2-power-budget",
1260 &pdata->power_budget);
Hemant Kumarb8fc9592013-04-17 19:29:39 -07001261 pdata->no_selective_suspend = of_property_read_bool(node,
1262 "qcom,no-selective-suspend");
Hemant Kumara7d531d2013-06-18 22:12:18 -07001263 pdata->resume_gpio = of_get_named_gpio(node, "qcom,resume-gpio", 0);
1264 if (pdata->resume_gpio < 0)
1265 pdata->resume_gpio = 0;
1266
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301267 return pdata;
1268}
1269
1270static u64 ehci_msm_dma_mask = DMA_BIT_MASK(64);
Manu Gautam91223e02011-11-08 15:27:22 +05301271static int __devinit ehci_msm2_probe(struct platform_device *pdev)
1272{
1273 struct usb_hcd *hcd;
1274 struct resource *res;
1275 struct msm_hcd *mhcd;
Hemant Kumar56925352012-02-13 16:59:52 -08001276 const struct msm_usb_host_platform_data *pdata;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001277 char pdev_name[PDEV_NAME_LEN];
Manu Gautam91223e02011-11-08 15:27:22 +05301278 int ret;
1279
1280 dev_dbg(&pdev->dev, "ehci_msm2 probe\n");
1281
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301282 if (pdev->dev.of_node) {
1283 dev_dbg(&pdev->dev, "device tree enabled\n");
1284 pdev->dev.platform_data = ehci_msm2_dt_to_pdata(pdev);
1285 }
1286
1287 if (!pdev->dev.platform_data)
1288 dev_dbg(&pdev->dev, "No platform data given\n");
1289
Hemant Kumara7d531d2013-06-18 22:12:18 -07001290 pdata = pdev->dev.platform_data;
1291
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301292 if (!pdev->dev.dma_mask)
1293 pdev->dev.dma_mask = &ehci_msm_dma_mask;
1294 if (!pdev->dev.coherent_dma_mask)
1295 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1296
Manu Gautam91223e02011-11-08 15:27:22 +05301297 hcd = usb_create_hcd(&msm_hc2_driver, &pdev->dev,
1298 dev_name(&pdev->dev));
1299 if (!hcd) {
1300 dev_err(&pdev->dev, "Unable to create HCD\n");
1301 return -ENOMEM;
1302 }
1303
Manu Gautam578656b2013-03-19 15:30:08 +05301304 hcd_to_bus(hcd)->skip_resume = true;
1305
Manu Gautam91223e02011-11-08 15:27:22 +05301306 hcd->irq = platform_get_irq(pdev, 0);
1307 if (hcd->irq < 0) {
1308 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
1309 ret = hcd->irq;
1310 goto put_hcd;
1311 }
1312
1313 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1314 if (!res) {
1315 dev_err(&pdev->dev, "Unable to get memory resource\n");
1316 ret = -ENODEV;
1317 goto put_hcd;
1318 }
1319
1320 hcd->rsrc_start = res->start;
1321 hcd->rsrc_len = resource_size(res);
1322 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
1323 if (!hcd->regs) {
1324 dev_err(&pdev->dev, "ioremap failed\n");
1325 ret = -ENOMEM;
1326 goto put_hcd;
1327 }
1328
1329 mhcd = hcd_to_mhcd(hcd);
1330 mhcd->dev = &pdev->dev;
1331
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301332 spin_lock_init(&mhcd->wakeup_lock);
1333
1334 mhcd->async_irq = platform_get_irq_byname(pdev, "async_irq");
1335 if (mhcd->async_irq < 0) {
1336 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
1337 mhcd->async_irq = 0;
1338 } else {
1339 ret = request_irq(mhcd->async_irq, msm_async_irq,
1340 IRQF_TRIGGER_RISING, "msm_ehci_host", mhcd);
1341 if (ret) {
1342 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
1343 goto unmap;
1344 }
1345 disable_irq(mhcd->async_irq);
1346 }
1347
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001348 snprintf(pdev_name, PDEV_NAME_LEN, "%s.%d", pdev->name, pdev->id);
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301349 mhcd->xo_clk = clk_get(&pdev->dev, "xo");
1350 if (!IS_ERR(mhcd->xo_clk)) {
1351 ret = clk_prepare_enable(mhcd->xo_clk);
1352 } else {
1353 mhcd->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, pdev_name);
1354 if (IS_ERR(mhcd->xo_handle)) {
1355 dev_err(&pdev->dev, "%s fail to get handle for X0 D0\n",
1356 __func__);
1357 ret = PTR_ERR(mhcd->xo_handle);
1358 goto free_async_irq;
1359 } else {
1360 ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_ON);
1361 }
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001362 }
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001363 if (ret) {
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301364 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
1365 __func__, ret);
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001366 goto free_xo_handle;
1367 }
1368
Hemant Kumara7d531d2013-06-18 22:12:18 -07001369 if (pdata && pdata->resume_gpio) {
1370 mhcd->resume_gpio = pdata->resume_gpio;
1371 ret = gpio_request(mhcd->resume_gpio, "hsusb_resume");
1372 if (ret) {
1373 dev_err(&pdev->dev,
1374 "resume gpio(%d) request failed:%d\n",
1375 mhcd->resume_gpio, ret);
1376 mhcd->resume_gpio = 0;
1377 } else {
1378 msm_hc2_driver.bus_resume =
1379 msm_ehci_bus_resume_with_gpio;
1380 }
1381 }
1382
Hemant Kumar17011ce2013-06-10 17:40:08 -07001383 spin_lock_init(&mhcd->wakeup_lock);
1384
Manu Gautam91223e02011-11-08 15:27:22 +05301385 ret = msm_ehci_init_clocks(mhcd, 1);
1386 if (ret) {
1387 dev_err(&pdev->dev, "unable to initialize clocks\n");
1388 ret = -ENODEV;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001389 goto devote_xo_handle;
Manu Gautam91223e02011-11-08 15:27:22 +05301390 }
1391
1392 ret = msm_ehci_init_vddcx(mhcd, 1);
1393 if (ret) {
1394 dev_err(&pdev->dev, "unable to initialize VDDCX\n");
1395 ret = -ENODEV;
1396 goto deinit_clocks;
1397 }
1398
1399 ret = msm_ehci_config_vddcx(mhcd, 1);
1400 if (ret) {
1401 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1402 goto deinit_vddcx;
1403 }
1404
1405 ret = msm_ehci_ldo_init(mhcd, 1);
1406 if (ret) {
1407 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1408 goto deinit_vddcx;
1409 }
1410
1411 ret = msm_ehci_ldo_enable(mhcd, 1);
1412 if (ret) {
1413 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1414 goto deinit_ldo;
1415 }
1416
1417 ret = msm_ehci_init_vbus(mhcd, 1);
1418 if (ret) {
1419 dev_err(&pdev->dev, "unable to get vbus\n");
1420 goto disable_ldo;
1421 }
1422
1423 ret = msm_hsusb_reset(mhcd);
1424 if (ret) {
1425 dev_err(&pdev->dev, "hsusb PHY initialization failed\n");
1426 goto vbus_deinit;
1427 }
1428
1429 ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
1430 if (ret) {
1431 dev_err(&pdev->dev, "unable to register HCD\n");
1432 goto vbus_deinit;
1433 }
1434
Hemant Kumar56925352012-02-13 16:59:52 -08001435 pdata = mhcd->dev->platform_data;
1436 if (pdata && (!pdata->dock_connect_irq ||
1437 !irq_read_line(pdata->dock_connect_irq)))
1438 msm_ehci_vbus_power(mhcd, 1);
Manu Gautam91223e02011-11-08 15:27:22 +05301439
Hemant Kumarb8fc9592013-04-17 19:29:39 -07001440 /* For peripherals directly conneted to downstream port of root hub
1441 * and require to drive suspend and resume by controller driver instead
1442 * of root hub.
1443 */
1444 if (pdata)
1445 mhcd->ehci.no_selective_suspend = pdata->no_selective_suspend;
1446
Hemant Kumar17011ce2013-06-10 17:40:08 -07001447 mhcd->wakeup_irq = platform_get_irq_byname(pdev, "wakeup_irq");
1448 if (mhcd->wakeup_irq > 0) {
1449 dev_dbg(&pdev->dev, "wakeup irq:%d\n", mhcd->wakeup_irq);
1450
1451 irq_set_status_flags(mhcd->wakeup_irq, IRQ_NOAUTOEN);
1452 ret = request_irq(mhcd->wakeup_irq, msm_hsusb_wakeup_irq,
1453 IRQF_TRIGGER_HIGH,
1454 "msm_hsusb_wakeup", mhcd);
1455 if (ret) {
1456 dev_err(&pdev->dev, "request_irq(%d) failed:%d\n",
1457 mhcd->wakeup_irq, ret);
1458 mhcd->wakeup_irq = 0;
1459 }
1460 } else {
1461 mhcd->wakeup_irq = 0;
1462 }
1463
Manu Gautam91223e02011-11-08 15:27:22 +05301464 device_init_wakeup(&pdev->dev, 1);
Hemant Kumar153f9972013-06-19 16:49:45 -07001465 wakeup_source_init(&mhcd->ws, dev_name(&pdev->dev));
1466 pm_stay_awake(mhcd->dev);
Chiranjeevi Velempatice169582012-08-25 14:15:41 +05301467 INIT_WORK(&mhcd->phy_susp_fail_work, msm_ehci_phy_susp_fail_work);
Manu Gautam91223e02011-11-08 15:27:22 +05301468 /*
1469 * This pdev->dev is assigned parent of root-hub by USB core,
1470 * hence, runtime framework automatically calls this driver's
1471 * runtime APIs based on root-hub's state.
1472 */
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +05301473 /* configure pmic_gpio_irq for D+ change */
1474 if (pdata && pdata->pmic_gpio_dp_irq)
1475 mhcd->pmic_gpio_dp_irq = pdata->pmic_gpio_dp_irq;
1476 if (mhcd->pmic_gpio_dp_irq) {
1477 ret = request_threaded_irq(mhcd->pmic_gpio_dp_irq, NULL,
1478 msm_ehci_host_wakeup_irq,
1479 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1480 "msm_ehci_host_wakeup", mhcd);
1481 if (!ret) {
1482 disable_irq_nosync(mhcd->pmic_gpio_dp_irq);
1483 } else {
1484 dev_err(&pdev->dev, "request_irq(%d) failed: %d\n",
1485 mhcd->pmic_gpio_dp_irq, ret);
1486 mhcd->pmic_gpio_dp_irq = 0;
1487 }
1488 }
Manu Gautam91223e02011-11-08 15:27:22 +05301489 pm_runtime_set_active(&pdev->dev);
1490 pm_runtime_enable(&pdev->dev);
1491
Rajkumar Raghupathyad8d68e2013-06-06 17:48:39 +05301492 if (ehci_debugfs_init(mhcd) < 0)
1493 dev_err(mhcd->dev, "%s: debugfs init failed\n", __func__);
1494
Manu Gautam91223e02011-11-08 15:27:22 +05301495 return 0;
1496
1497vbus_deinit:
1498 msm_ehci_init_vbus(mhcd, 0);
1499disable_ldo:
1500 msm_ehci_ldo_enable(mhcd, 0);
1501deinit_ldo:
1502 msm_ehci_ldo_init(mhcd, 0);
1503deinit_vddcx:
1504 msm_ehci_init_vddcx(mhcd, 0);
1505deinit_clocks:
1506 msm_ehci_init_clocks(mhcd, 0);
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001507devote_xo_handle:
Hemant Kumara7d531d2013-06-18 22:12:18 -07001508 if (mhcd->resume_gpio)
1509 gpio_free(mhcd->resume_gpio);
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301510 if (!IS_ERR(mhcd->xo_clk))
1511 clk_disable_unprepare(mhcd->xo_clk);
1512 else
1513 msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_OFF);
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001514free_xo_handle:
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301515 if (!IS_ERR(mhcd->xo_clk))
1516 clk_put(mhcd->xo_clk);
1517 else
1518 msm_xo_put(mhcd->xo_handle);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301519free_async_irq:
1520 if (mhcd->async_irq)
1521 free_irq(mhcd->async_irq, mhcd);
Manu Gautam91223e02011-11-08 15:27:22 +05301522unmap:
1523 iounmap(hcd->regs);
1524put_hcd:
1525 usb_put_hcd(hcd);
1526
1527 return ret;
1528}
1529
1530static int __devexit ehci_msm2_remove(struct platform_device *pdev)
1531{
1532 struct usb_hcd *hcd = platform_get_drvdata(pdev);
1533 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1534
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +05301535 if (mhcd->pmic_gpio_dp_irq) {
1536 if (mhcd->pmic_gpio_dp_irq_enabled)
1537 disable_irq_wake(mhcd->pmic_gpio_dp_irq);
1538 free_irq(mhcd->pmic_gpio_dp_irq, mhcd);
1539 }
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301540 if (mhcd->async_irq) {
1541 if (mhcd->async_irq_enabled)
1542 disable_irq_wake(mhcd->async_irq);
1543 free_irq(mhcd->async_irq, mhcd);
1544 }
Hemant Kumara7d531d2013-06-18 22:12:18 -07001545
Hemant Kumar17011ce2013-06-10 17:40:08 -07001546 if (mhcd->wakeup_irq) {
1547 if (mhcd->wakeup_irq_enabled)
1548 disable_irq_wake(mhcd->wakeup_irq);
1549 free_irq(mhcd->wakeup_irq, mhcd);
1550 }
1551
Hemant Kumara7d531d2013-06-18 22:12:18 -07001552 if (mhcd->resume_gpio)
1553 gpio_free(mhcd->resume_gpio);
1554
Manu Gautam91223e02011-11-08 15:27:22 +05301555 device_init_wakeup(&pdev->dev, 0);
Manu Gautam91223e02011-11-08 15:27:22 +05301556 pm_runtime_set_suspended(&pdev->dev);
1557
1558 usb_remove_hcd(hcd);
Hemant Kumar56925352012-02-13 16:59:52 -08001559
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301560 if (!IS_ERR(mhcd->xo_clk)) {
1561 clk_disable_unprepare(mhcd->xo_clk);
1562 clk_put(mhcd->xo_clk);
1563 } else {
1564 msm_xo_put(mhcd->xo_handle);
1565 }
Manu Gautam91223e02011-11-08 15:27:22 +05301566 msm_ehci_vbus_power(mhcd, 0);
1567 msm_ehci_init_vbus(mhcd, 0);
1568 msm_ehci_ldo_enable(mhcd, 0);
1569 msm_ehci_ldo_init(mhcd, 0);
1570 msm_ehci_init_vddcx(mhcd, 0);
1571
1572 msm_ehci_init_clocks(mhcd, 0);
Hemant Kumar153f9972013-06-19 16:49:45 -07001573 wakeup_source_trash(&mhcd->ws);
Manu Gautam91223e02011-11-08 15:27:22 +05301574 iounmap(hcd->regs);
1575 usb_put_hcd(hcd);
1576
Rajkumar Raghupathyad8d68e2013-06-06 17:48:39 +05301577#if defined(CONFIG_DEBUG_FS)
1578 debugfs_remove_recursive(dent_ehci);
1579#endif
Manu Gautam91223e02011-11-08 15:27:22 +05301580 return 0;
1581}
1582
1583#ifdef CONFIG_PM_SLEEP
1584static int ehci_msm2_pm_suspend(struct device *dev)
1585{
1586 struct usb_hcd *hcd = dev_get_drvdata(dev);
1587 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1588
1589 dev_dbg(dev, "ehci-msm2 PM suspend\n");
1590
1591 if (device_may_wakeup(dev))
1592 enable_irq_wake(hcd->irq);
1593
1594 return msm_ehci_suspend(mhcd);
1595
1596}
1597
1598static int ehci_msm2_pm_resume(struct device *dev)
1599{
1600 int ret;
1601 struct usb_hcd *hcd = dev_get_drvdata(dev);
1602 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1603
1604 dev_dbg(dev, "ehci-msm2 PM resume\n");
1605
1606 if (device_may_wakeup(dev))
1607 disable_irq_wake(hcd->irq);
1608
1609 ret = msm_ehci_resume(mhcd);
1610 if (ret)
1611 return ret;
1612
1613 /* Bring the device to full powered state upon system resume */
1614 pm_runtime_disable(dev);
1615 pm_runtime_set_active(dev);
1616 pm_runtime_enable(dev);
1617
1618 return 0;
1619}
1620#endif
1621
1622#ifdef CONFIG_PM_RUNTIME
1623static int ehci_msm2_runtime_idle(struct device *dev)
1624{
1625 dev_dbg(dev, "EHCI runtime idle\n");
1626
1627 return 0;
1628}
1629
1630static int ehci_msm2_runtime_suspend(struct device *dev)
1631{
1632 struct usb_hcd *hcd = dev_get_drvdata(dev);
1633 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1634
1635 dev_dbg(dev, "EHCI runtime suspend\n");
1636 return msm_ehci_suspend(mhcd);
1637}
1638
1639static int ehci_msm2_runtime_resume(struct device *dev)
1640{
1641 struct usb_hcd *hcd = dev_get_drvdata(dev);
1642 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1643
1644 dev_dbg(dev, "EHCI runtime resume\n");
1645 return msm_ehci_resume(mhcd);
1646}
1647#endif
1648
1649#ifdef CONFIG_PM
1650static const struct dev_pm_ops ehci_msm2_dev_pm_ops = {
1651 SET_SYSTEM_SLEEP_PM_OPS(ehci_msm2_pm_suspend, ehci_msm2_pm_resume)
1652 SET_RUNTIME_PM_OPS(ehci_msm2_runtime_suspend, ehci_msm2_runtime_resume,
1653 ehci_msm2_runtime_idle)
1654};
1655#endif
1656
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301657static const struct of_device_id ehci_msm2_dt_match[] = {
1658 { .compatible = "qcom,ehci-host",
1659 },
1660 {}
1661};
1662
Manu Gautam91223e02011-11-08 15:27:22 +05301663static struct platform_driver ehci_msm2_driver = {
1664 .probe = ehci_msm2_probe,
1665 .remove = __devexit_p(ehci_msm2_remove),
1666 .driver = {
1667 .name = "msm_ehci_host",
1668#ifdef CONFIG_PM
1669 .pm = &ehci_msm2_dev_pm_ops,
1670#endif
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301671 .of_match_table = ehci_msm2_dt_match,
Manu Gautam91223e02011-11-08 15:27:22 +05301672 },
1673};