phy-msm-usb.c 48.4 KB
Newer Older
1
/* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 */

#include <linux/module.h>
#include <linux/device.h>
21
#include <linux/gpio/consumer.h>
22 23 24 25 26 27 28 29 30 31 32
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/uaccess.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
33
#include <linux/pm_runtime.h>
34 35
#include <linux/of.h>
#include <linux/of_device.h>
36
#include <linux/reboot.h>
37
#include <linux/reset.h>
38 39 40

#include <linux/usb.h>
#include <linux/usb/otg.h>
41
#include <linux/usb/of.h>
42 43 44 45 46
#include <linux/usb/ulpi.h>
#include <linux/usb/gadget.h>
#include <linux/usb/hcd.h>
#include <linux/usb/msm_hsusb.h>
#include <linux/usb/msm_hsusb_hw.h>
47
#include <linux/regulator/consumer.h>
48 49 50 51 52

#define MSM_USB_BASE	(motg->regs)
#define DRIVER_NAME	"msm_otg"

#define ULPI_IO_TIMEOUT_USEC	(10 * 1000)
53
#define LINK_RESET_TIMEOUT_USEC	(250 * 1000)
54 55 56 57 58 59 60 61 62 63 64 65 66

#define USB_PHY_3P3_VOL_MIN	3050000 /* uV */
#define USB_PHY_3P3_VOL_MAX	3300000 /* uV */
#define USB_PHY_3P3_HPM_LOAD	50000	/* uA */
#define USB_PHY_3P3_LPM_LOAD	4000	/* uA */

#define USB_PHY_1P8_VOL_MIN	1800000 /* uV */
#define USB_PHY_1P8_VOL_MAX	1800000 /* uV */
#define USB_PHY_1P8_HPM_LOAD	50000	/* uA */
#define USB_PHY_1P8_LPM_LOAD	4000	/* uA */

#define USB_PHY_VDD_DIG_VOL_MIN	1000000 /* uV */
#define USB_PHY_VDD_DIG_VOL_MAX	1320000 /* uV */
67 68 69 70 71 72 73
#define USB_PHY_SUSP_DIG_VOL	500000  /* uV */

enum vdd_levels {
	VDD_LEVEL_NONE = 0,
	VDD_LEVEL_MIN,
	VDD_LEVEL_MAX,
};
74 75 76 77 78 79

static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
{
	int ret = 0;

	if (init) {
80
		ret = regulator_set_voltage(motg->vddcx,
81 82
				motg->vdd_levels[VDD_LEVEL_MIN],
				motg->vdd_levels[VDD_LEVEL_MAX]);
83
		if (ret) {
84
			dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
85 86 87
			return ret;
		}

88
		ret = regulator_enable(motg->vddcx);
89
		if (ret)
90
			dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
91
	} else {
92
		ret = regulator_set_voltage(motg->vddcx, 0,
93
				motg->vdd_levels[VDD_LEVEL_MAX]);
94
		if (ret)
95
			dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
96
		ret = regulator_disable(motg->vddcx);
97
		if (ret)
98
			dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
99 100 101 102 103 104 105 106 107 108
	}

	return ret;
}

static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
{
	int rc = 0;

	if (init) {
109
		rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN,
110 111
				USB_PHY_3P3_VOL_MAX);
		if (rc) {
112
			dev_err(motg->phy.dev, "Cannot set v3p3 voltage\n");
113
			goto exit;
114
		}
115
		rc = regulator_enable(motg->v3p3);
116
		if (rc) {
117
			dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
118
			goto exit;
119
		}
120
		rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN,
121 122
				USB_PHY_1P8_VOL_MAX);
		if (rc) {
123
			dev_err(motg->phy.dev, "Cannot set v1p8 voltage\n");
124
			goto disable_3p3;
125
		}
126
		rc = regulator_enable(motg->v1p8);
127
		if (rc) {
128
			dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
129
			goto disable_3p3;
130 131 132 133 134
		}

		return 0;
	}

135
	regulator_disable(motg->v1p8);
136
disable_3p3:
137
	regulator_disable(motg->v3p3);
138
exit:
139 140 141
	return rc;
}

142
static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
143 144 145 146
{
	int ret = 0;

	if (on) {
147
		ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_HPM_LOAD);
148
		if (ret < 0) {
149
			pr_err("Could not set HPM for v1p8\n");
150 151
			return ret;
		}
152
		ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_HPM_LOAD);
153
		if (ret < 0) {
154
			pr_err("Could not set HPM for v3p3\n");
155
			regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
156 157 158
			return ret;
		}
	} else {
159
		ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
160
		if (ret < 0)
161
			pr_err("Could not set LPM for v1p8\n");
162
		ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_LPM_LOAD);
163
		if (ret < 0)
164
			pr_err("Could not set LPM for v3p3\n");
165 166 167 168 169 170
	}

	pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
	return ret < 0 ? ret : 0;
}

171
static int ulpi_read(struct usb_phy *phy, u32 reg)
172
{
173
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	int cnt = 0;

	/* initiate read operation */
	writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
	       USB_ULPI_VIEWPORT);

	/* wait for completion */
	while (cnt < ULPI_IO_TIMEOUT_USEC) {
		if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
			break;
		udelay(1);
		cnt++;
	}

	if (cnt >= ULPI_IO_TIMEOUT_USEC) {
189
		dev_err(phy->dev, "ulpi_read: timeout %08x\n",
190 191 192 193 194 195
			readl(USB_ULPI_VIEWPORT));
		return -ETIMEDOUT;
	}
	return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
}

196
static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
197
{
198
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
	int cnt = 0;

	/* initiate write operation */
	writel(ULPI_RUN | ULPI_WRITE |
	       ULPI_ADDR(reg) | ULPI_DATA(val),
	       USB_ULPI_VIEWPORT);

	/* wait for completion */
	while (cnt < ULPI_IO_TIMEOUT_USEC) {
		if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
			break;
		udelay(1);
		cnt++;
	}

	if (cnt >= ULPI_IO_TIMEOUT_USEC) {
215
		dev_err(phy->dev, "ulpi_write: timeout\n");
216 217 218 219 220
		return -ETIMEDOUT;
	}
	return 0;
}

221
static struct usb_phy_io_ops msm_otg_io_ops = {
222 223 224 225 226 227 228
	.read = ulpi_read,
	.write = ulpi_write,
};

static void ulpi_init(struct msm_otg *motg)
{
	struct msm_otg_platform_data *pdata = motg->pdata;
229 230
	int *seq = pdata->phy_init_seq, idx;
	u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
231

232 233 234
	for (idx = 0; idx < pdata->phy_init_sz; idx++) {
		if (seq[idx] == -1)
			continue;
235

236
		dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
237 238
				seq[idx], addr + idx);
		ulpi_write(&motg->phy, seq[idx], addr + idx);
239 240 241
	}
}

242 243 244
static int msm_phy_notify_disconnect(struct usb_phy *phy,
				   enum usb_device_speed speed)
{
245
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
246 247
	int val;

248 249 250 251 252
	if (motg->manual_pullup) {
		val = ULPI_MISC_A_VBUSVLDEXT | ULPI_MISC_A_VBUSVLDEXTSEL;
		usb_phy_io_write(phy, val, ULPI_CLR(ULPI_MISC_A));
	}

253 254 255 256 257 258 259 260 261 262 263 264
	/*
	 * Put the transceiver in non-driving mode. Otherwise host
	 * may not detect soft-disconnection.
	 */
	val = ulpi_read(phy, ULPI_FUNC_CTRL);
	val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
	val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
	ulpi_write(phy, val, ULPI_FUNC_CTRL);

	return 0;
}

265 266
static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
{
267
	int ret;
268

S
Stephen Boyd 已提交
269
	if (assert)
270 271 272
		ret = reset_control_assert(motg->link_rst);
	else
		ret = reset_control_deassert(motg->link_rst);
273 274 275 276

	if (ret)
		dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
			assert ? "assert" : "deassert");
277 278 279 280 281 282

	return ret;
}

static int msm_otg_phy_clk_reset(struct msm_otg *motg)
{
283
	int ret = 0;
284

S
Stephen Boyd 已提交
285
	if (motg->phy_rst)
286
		ret = reset_control_reset(motg->phy_rst);
287

288
	if (ret)
289 290
		dev_err(motg->phy.dev, "usb phy clk reset failed\n");

291 292 293
	return ret;
}

294
static int msm_link_reset(struct msm_otg *motg)
295 296 297 298 299 300 301 302
{
	u32 val;
	int ret;

	ret = msm_otg_link_clk_reset(motg, 1);
	if (ret)
		return ret;

303 304
	/* wait for 1ms delay as suggested in HPG. */
	usleep_range(1000, 1200);
305

306
	ret = msm_otg_link_clk_reset(motg, 0);
307 308 309
	if (ret)
		return ret;

310 311 312
	if (motg->phy_number)
		writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);

313
	/* put transceiver in serial mode as part of reset */
314
	val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
315
	writel(val | PORTSC_PTS_SERIAL, USB_PORTSC);
316

317 318 319
	return 0;
}

320
static int msm_otg_reset(struct usb_phy *phy)
321
{
322
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
323 324 325 326 327 328 329 330 331 332 333 334
	int cnt = 0;

	writel(USBCMD_RESET, USB_USBCMD);
	while (cnt < LINK_RESET_TIMEOUT_USEC) {
		if (!(readl(USB_USBCMD) & USBCMD_RESET))
			break;
		udelay(1);
		cnt++;
	}
	if (cnt >= LINK_RESET_TIMEOUT_USEC)
		return -ETIMEDOUT;

335 336 337
	/* select ULPI phy and clear other status/control bits in PORTSC */
	writel(PORTSC_PTS_ULPI, USB_PORTSC);

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
	writel(0x0, USB_AHBBURST);
	writel(0x08, USB_AHBMODE);

	if (motg->phy_number)
		writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
	return 0;
}

static void msm_phy_reset(struct msm_otg *motg)
{
	void __iomem *addr;

	if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY) {
		msm_otg_phy_clk_reset(motg);
		return;
	}

	addr = USB_PHY_CTRL;
	if (motg->phy_number)
		addr = USB_PHY_CTRL2;

	/* Assert USB PHY_POR */
	writel(readl(addr) | PHY_POR_ASSERT, addr);

	/*
	 * wait for minimum 10 microseconds as suggested in HPG.
	 * Use a slightly larger value since the exact value didn't
	 * work 100% of the time.
	 */
	udelay(12);

	/* Deassert USB PHY_POR */
	writel(readl(addr) & ~PHY_POR_ASSERT, addr);
}

static int msm_usb_reset(struct usb_phy *phy)
{
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
	int ret;

	if (!IS_ERR(motg->core_clk))
		clk_prepare_enable(motg->core_clk);

	ret = msm_link_reset(motg);
	if (ret) {
		dev_err(phy->dev, "phy_reset failed\n");
		return ret;
	}

	ret = msm_otg_reset(&motg->phy);
	if (ret) {
		dev_err(phy->dev, "link reset failed\n");
		return ret;
	}
392 393 394

	msleep(100);

395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	/* Reset USB PHY after performing USB Link RESET */
	msm_phy_reset(motg);

	if (!IS_ERR(motg->core_clk))
		clk_disable_unprepare(motg->core_clk);

	return 0;
}

static int msm_phy_init(struct usb_phy *phy)
{
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
	struct msm_otg_platform_data *pdata = motg->pdata;
	u32 val, ulpi_val = 0;

	/* Program USB PHY Override registers. */
	ulpi_init(motg);

	/*
	 * It is recommended in HPG to reset USB PHY after programming
	 * USB PHY Override registers.
	 */
	msm_phy_reset(motg);
418 419 420

	if (pdata->otg_control == OTG_PHY_CONTROL) {
		val = readl(USB_OTGSC);
421
		if (pdata->mode == USB_DR_MODE_OTG) {
422 423
			ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
			val |= OTGSC_IDIE | OTGSC_BSVIE;
424
		} else if (pdata->mode == USB_DR_MODE_PERIPHERAL) {
425 426 427 428
			ulpi_val = ULPI_INT_SESS_VALID;
			val |= OTGSC_BSVIE;
		}
		writel(val, USB_OTGSC);
429 430
		ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
		ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
431 432
	}

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
	if (motg->manual_pullup) {
		val = ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT;
		ulpi_write(phy, val, ULPI_SET(ULPI_MISC_A));

		val = readl(USB_GENCONFIG_2);
		val |= GENCONFIG_2_SESS_VLD_CTRL_EN;
		writel(val, USB_GENCONFIG_2);

		val = readl(USB_USBCMD);
		val |= USBCMD_SESS_VLD_CTRL;
		writel(val, USB_USBCMD);

		val = ulpi_read(phy, ULPI_FUNC_CTRL);
		val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
		val |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
		ulpi_write(phy, val, ULPI_FUNC_CTRL);
	}

451 452 453
	if (motg->phy_number)
		writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);

454 455 456
	return 0;
}

457
#define PHY_SUSPEND_TIMEOUT_USEC	(500 * 1000)
458 459
#define PHY_RESUME_TIMEOUT_USEC	(100 * 1000)

460 461
#ifdef CONFIG_PM

462
static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
463
{
464
	int max_vol = motg->vdd_levels[VDD_LEVEL_MAX];
465 466 467 468
	int min_vol;
	int ret;

	if (high)
469
		min_vol = motg->vdd_levels[VDD_LEVEL_MIN];
470
	else
471
		min_vol = motg->vdd_levels[VDD_LEVEL_NONE];
472

473
	ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
474
	if (ret) {
475
		pr_err("Cannot set vddcx voltage\n");
476 477 478 479 480 481 482 483
		return ret;
	}

	pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);

	return ret;
}

484 485
static int msm_otg_suspend(struct msm_otg *motg)
{
486 487
	struct usb_phy *phy = &motg->phy;
	struct usb_bus *bus = phy->otg->host;
488
	struct msm_otg_platform_data *pdata = motg->pdata;
489
	void __iomem *addr;
490 491 492 493 494 495 496
	int cnt = 0;

	if (atomic_read(&motg->in_lpm))
		return 0;

	disable_irq(motg->irq);
	/*
497 498
	 * Chipidea 45-nm PHY suspend sequence:
	 *
499 500 501 502
	 * Interrupt Latch Register auto-clear feature is not present
	 * in all PHY versions. Latch register is clear on read type.
	 * Clear latch register to avoid spurious wakeup from
	 * low power mode (LPM).
503
	 *
504 505 506 507
	 * PHY comparators are disabled when PHY enters into low power
	 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
	 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
	 * PHY comparators. This save significant amount of power.
508
	 *
509 510 511
	 * PLL is not turned off when PHY enters into low power mode (LPM).
	 * Disable PLL for maximum power savings.
	 */
512 513

	if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
514
		ulpi_read(phy, 0x14);
515
		if (pdata->otg_control == OTG_PHY_CONTROL)
516 517
			ulpi_write(phy, 0x01, 0x30);
		ulpi_write(phy, 0x08, 0x09);
518
	}
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533

	/*
	 * PHY may take some time or even fail to enter into low power
	 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
	 * in failure case.
	 */
	writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
	while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
		if (readl(USB_PORTSC) & PORTSC_PHCD)
			break;
		udelay(1);
		cnt++;
	}

	if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
534 535
		dev_err(phy->dev, "Unable to suspend PHY\n");
		msm_otg_reset(phy);
536 537 538 539 540 541 542 543 544 545 546 547 548
		enable_irq(motg->irq);
		return -ETIMEDOUT;
	}

	/*
	 * PHY has capability to generate interrupt asynchronously in low
	 * power mode (LPM). This interrupt is level triggered. So USB IRQ
	 * line must be disabled till async interrupt enable bit is cleared
	 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
	 * block data communication from PHY.
	 */
	writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);

549 550 551 552
	addr = USB_PHY_CTRL;
	if (motg->phy_number)
		addr = USB_PHY_CTRL2;

553 554
	if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
			motg->pdata->otg_control == OTG_PMIC_CONTROL)
555
		writel(readl(addr) | PHY_RETEN, addr);
556

557 558
	clk_disable_unprepare(motg->pclk);
	clk_disable_unprepare(motg->clk);
559
	if (!IS_ERR(motg->core_clk))
560
		clk_disable_unprepare(motg->core_clk);
561

562 563
	if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
			motg->pdata->otg_control == OTG_PMIC_CONTROL) {
564 565
		msm_hsusb_ldo_set_mode(motg, 0);
		msm_hsusb_config_vddcx(motg, 0);
566 567
	}

568
	if (device_may_wakeup(phy->dev))
569 570 571 572 573 574 575
		enable_irq_wake(motg->irq);
	if (bus)
		clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);

	atomic_set(&motg->in_lpm, 1);
	enable_irq(motg->irq);

576
	dev_info(phy->dev, "USB in low power mode\n");
577 578 579 580 581 582

	return 0;
}

static int msm_otg_resume(struct msm_otg *motg)
{
583 584
	struct usb_phy *phy = &motg->phy;
	struct usb_bus *bus = phy->otg->host;
585
	void __iomem *addr;
586 587 588 589 590 591
	int cnt = 0;
	unsigned temp;

	if (!atomic_read(&motg->in_lpm))
		return 0;

592 593
	clk_prepare_enable(motg->pclk);
	clk_prepare_enable(motg->clk);
594
	if (!IS_ERR(motg->core_clk))
595
		clk_prepare_enable(motg->core_clk);
596

597 598
	if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
			motg->pdata->otg_control == OTG_PMIC_CONTROL) {
599 600 601 602 603

		addr = USB_PHY_CTRL;
		if (motg->phy_number)
			addr = USB_PHY_CTRL2;

604 605
		msm_hsusb_ldo_set_mode(motg, 1);
		msm_hsusb_config_vddcx(motg, 1);
606
		writel(readl(addr) & ~PHY_RETEN, addr);
607 608
	}

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	temp = readl(USB_USBCMD);
	temp &= ~ASYNC_INTR_CTRL;
	temp &= ~ULPI_STP_CTRL;
	writel(temp, USB_USBCMD);

	/*
	 * PHY comes out of low power mode (LPM) in case of wakeup
	 * from asynchronous interrupt.
	 */
	if (!(readl(USB_PORTSC) & PORTSC_PHCD))
		goto skip_phy_resume;

	writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
	while (cnt < PHY_RESUME_TIMEOUT_USEC) {
		if (!(readl(USB_PORTSC) & PORTSC_PHCD))
			break;
		udelay(1);
		cnt++;
	}

	if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
		/*
		 * This is a fatal error. Reset the link and
		 * PHY. USB state can not be restored. Re-insertion
		 * of USB cable is the only way to get USB working.
		 */
635
		dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n");
636
		msm_otg_reset(phy);
637 638 639
	}

skip_phy_resume:
640
	if (device_may_wakeup(phy->dev))
641 642 643 644
		disable_irq_wake(motg->irq);
	if (bus)
		set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);

645 646
	atomic_set(&motg->in_lpm, 0);

647 648
	if (motg->async_int) {
		motg->async_int = 0;
649
		pm_runtime_put(phy->dev);
650 651 652
		enable_irq(motg->irq);
	}

653
	dev_info(phy->dev, "USB exited from low power mode\n");
654 655 656

	return 0;
}
657
#endif
658

659 660 661 662 663 664
static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
{
	if (motg->cur_power == mA)
		return;

	/* TODO: Notify PMIC about available current */
665
	dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
666 667 668
	motg->cur_power = mA;
}

669
static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
670
{
671
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
672 673 674 675 676 677 678 679 680 681 682 683 684 685

	/*
	 * Gadget driver uses set_power method to notify about the
	 * available current based on suspend/configured states.
	 *
	 * IDEV_CHG can be drawn irrespective of suspend/un-configured
	 * states when CDP/ACA is connected.
	 */
	if (motg->chg_type == USB_SDP_CHARGER)
		msm_otg_notify_charger(motg, mA);

	return 0;
}

686
static void msm_otg_start_host(struct usb_phy *phy, int on)
687
{
688
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
689 690 691
	struct msm_otg_platform_data *pdata = motg->pdata;
	struct usb_hcd *hcd;

692
	if (!phy->otg->host)
693 694
		return;

695
	hcd = bus_to_hcd(phy->otg->host);
696 697

	if (on) {
698
		dev_dbg(phy->dev, "host on\n");
699 700 701 702 703 704 705 706 707 708 709 710

		if (pdata->vbus_power)
			pdata->vbus_power(1);
		/*
		 * Some boards have a switch cotrolled by gpio
		 * to enable/disable internal HUB. Enable internal
		 * HUB before kicking the host.
		 */
		if (pdata->setup_gpio)
			pdata->setup_gpio(OTG_STATE_A_HOST);
#ifdef CONFIG_USB
		usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
711
		device_wakeup_enable(hcd->self.controller);
712 713
#endif
	} else {
714
		dev_dbg(phy->dev, "host off\n");
715 716 717 718 719 720 721 722 723 724 725

#ifdef CONFIG_USB
		usb_remove_hcd(hcd);
#endif
		if (pdata->setup_gpio)
			pdata->setup_gpio(OTG_STATE_UNDEFINED);
		if (pdata->vbus_power)
			pdata->vbus_power(0);
	}
}

726
static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
727
{
A
Antoine Tenart 已提交
728
	struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
729 730 731 732 733 734
	struct usb_hcd *hcd;

	/*
	 * Fail host registration if this board can support
	 * only peripheral configuration.
	 */
735
	if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
A
Antoine Tenart 已提交
736
		dev_info(otg->usb_phy->dev, "Host mode is not supported\n");
737 738 739 740
		return -ENODEV;
	}

	if (!host) {
741
		if (otg->state == OTG_STATE_A_HOST) {
A
Antoine Tenart 已提交
742 743
			pm_runtime_get_sync(otg->usb_phy->dev);
			msm_otg_start_host(otg->usb_phy, 0);
744
			otg->host = NULL;
745
			otg->state = OTG_STATE_UNDEFINED;
746 747 748 749 750 751 752 753 754 755 756 757
			schedule_work(&motg->sm_work);
		} else {
			otg->host = NULL;
		}

		return 0;
	}

	hcd = bus_to_hcd(host);
	hcd->power_budget = motg->pdata->power_budget;

	otg->host = host;
A
Antoine Tenart 已提交
758
	dev_dbg(otg->usb_phy->dev, "host driver registered w/ tranceiver\n");
759 760 761 762 763

	/*
	 * Kick the state machine work, if peripheral is not supported
	 * or peripheral is already registered with us.
	 */
764
	if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) {
A
Antoine Tenart 已提交
765
		pm_runtime_get_sync(otg->usb_phy->dev);
766
		schedule_work(&motg->sm_work);
767
	}
768 769 770 771

	return 0;
}

772
static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
773
{
774
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
775 776
	struct msm_otg_platform_data *pdata = motg->pdata;

777
	if (!phy->otg->gadget)
778 779 780
		return;

	if (on) {
781
		dev_dbg(phy->dev, "gadget on\n");
782 783 784 785 786 787 788
		/*
		 * Some boards have a switch cotrolled by gpio
		 * to enable/disable internal HUB. Disable internal
		 * HUB before kicking the gadget.
		 */
		if (pdata->setup_gpio)
			pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
789
		usb_gadget_vbus_connect(phy->otg->gadget);
790
	} else {
791 792
		dev_dbg(phy->dev, "gadget off\n");
		usb_gadget_vbus_disconnect(phy->otg->gadget);
793 794 795 796 797 798
		if (pdata->setup_gpio)
			pdata->setup_gpio(OTG_STATE_UNDEFINED);
	}

}

799 800
static int msm_otg_set_peripheral(struct usb_otg *otg,
					struct usb_gadget *gadget)
801
{
A
Antoine Tenart 已提交
802
	struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
803 804 805 806 807

	/*
	 * Fail peripheral registration if this board can support
	 * only host configuration.
	 */
808
	if (motg->pdata->mode == USB_DR_MODE_HOST) {
A
Antoine Tenart 已提交
809
		dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
810 811 812 813
		return -ENODEV;
	}

	if (!gadget) {
814
		if (otg->state == OTG_STATE_B_PERIPHERAL) {
A
Antoine Tenart 已提交
815 816
			pm_runtime_get_sync(otg->usb_phy->dev);
			msm_otg_start_peripheral(otg->usb_phy, 0);
817
			otg->gadget = NULL;
818
			otg->state = OTG_STATE_UNDEFINED;
819 820 821 822 823 824 825 826
			schedule_work(&motg->sm_work);
		} else {
			otg->gadget = NULL;
		}

		return 0;
	}
	otg->gadget = gadget;
A
Antoine Tenart 已提交
827 828
	dev_dbg(otg->usb_phy->dev,
		"peripheral driver registered w/ tranceiver\n");
829 830 831 832 833

	/*
	 * Kick the state machine work, if host is not supported
	 * or host is already registered with us.
	 */
834
	if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
A
Antoine Tenart 已提交
835
		pm_runtime_get_sync(otg->usb_phy->dev);
836
		schedule_work(&motg->sm_work);
837
	}
838 839 840 841

	return 0;
}

842 843
static bool msm_chg_check_secondary_det(struct msm_otg *motg)
{
844
	struct usb_phy *phy = &motg->phy;
845 846 847 848 849
	u32 chg_det;
	bool ret = false;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
850
		chg_det = ulpi_read(phy, 0x34);
851 852 853
		ret = chg_det & (1 << 4);
		break;
	case SNPS_28NM_INTEGRATED_PHY:
854
		chg_det = ulpi_read(phy, 0x87);
855 856 857 858 859 860 861 862 863 864
		ret = chg_det & 1;
		break;
	default:
		break;
	}
	return ret;
}

static void msm_chg_enable_secondary_det(struct msm_otg *motg)
{
865
	struct usb_phy *phy = &motg->phy;
866 867 868 869
	u32 chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
870
		chg_det = ulpi_read(phy, 0x34);
871 872
		/* Turn off charger block */
		chg_det |= ~(1 << 1);
873
		ulpi_write(phy, chg_det, 0x34);
874 875 876
		udelay(20);
		/* control chg block via ULPI */
		chg_det &= ~(1 << 3);
877
		ulpi_write(phy, chg_det, 0x34);
878 879
		/* put it in host mode for enabling D- source */
		chg_det &= ~(1 << 2);
880
		ulpi_write(phy, chg_det, 0x34);
881 882
		/* Turn on chg detect block */
		chg_det &= ~(1 << 1);
883
		ulpi_write(phy, chg_det, 0x34);
884 885 886
		udelay(20);
		/* enable chg detection */
		chg_det &= ~(1 << 0);
887
		ulpi_write(phy, chg_det, 0x34);
888 889 890 891 892 893
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/*
		 * Configure DM as current source, DP as current sink
		 * and enable battery charging comparators.
		 */
894 895 896
		ulpi_write(phy, 0x8, 0x85);
		ulpi_write(phy, 0x2, 0x85);
		ulpi_write(phy, 0x1, 0x85);
897 898 899 900 901 902 903 904
		break;
	default:
		break;
	}
}

static bool msm_chg_check_primary_det(struct msm_otg *motg)
{
905
	struct usb_phy *phy = &motg->phy;
906 907 908 909 910
	u32 chg_det;
	bool ret = false;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
911
		chg_det = ulpi_read(phy, 0x34);
912 913 914
		ret = chg_det & (1 << 4);
		break;
	case SNPS_28NM_INTEGRATED_PHY:
915
		chg_det = ulpi_read(phy, 0x87);
916 917 918 919 920 921 922 923 924 925
		ret = chg_det & 1;
		break;
	default:
		break;
	}
	return ret;
}

static void msm_chg_enable_primary_det(struct msm_otg *motg)
{
926
	struct usb_phy *phy = &motg->phy;
927 928 929 930
	u32 chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
931
		chg_det = ulpi_read(phy, 0x34);
932 933
		/* enable chg detection */
		chg_det &= ~(1 << 0);
934
		ulpi_write(phy, chg_det, 0x34);
935 936 937 938 939 940
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/*
		 * Configure DP as current source, DM as current sink
		 * and enable battery charging comparators.
		 */
941 942
		ulpi_write(phy, 0x2, 0x85);
		ulpi_write(phy, 0x1, 0x85);
943 944 945 946 947 948 949 950
		break;
	default:
		break;
	}
}

static bool msm_chg_check_dcd(struct msm_otg *motg)
{
951
	struct usb_phy *phy = &motg->phy;
952 953 954 955 956
	u32 line_state;
	bool ret = false;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
957
		line_state = ulpi_read(phy, 0x15);
958 959 960
		ret = !(line_state & 1);
		break;
	case SNPS_28NM_INTEGRATED_PHY:
961
		line_state = ulpi_read(phy, 0x87);
962 963 964 965 966 967 968 969 970 971
		ret = line_state & 2;
		break;
	default:
		break;
	}
	return ret;
}

static void msm_chg_disable_dcd(struct msm_otg *motg)
{
972
	struct usb_phy *phy = &motg->phy;
973 974 975 976
	u32 chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
977
		chg_det = ulpi_read(phy, 0x34);
978
		chg_det &= ~(1 << 5);
979
		ulpi_write(phy, chg_det, 0x34);
980 981
		break;
	case SNPS_28NM_INTEGRATED_PHY:
982
		ulpi_write(phy, 0x10, 0x86);
983 984 985 986 987 988 989 990
		break;
	default:
		break;
	}
}

static void msm_chg_enable_dcd(struct msm_otg *motg)
{
991
	struct usb_phy *phy = &motg->phy;
992 993 994 995
	u32 chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
996
		chg_det = ulpi_read(phy, 0x34);
997 998
		/* Turn on D+ current source */
		chg_det |= (1 << 5);
999
		ulpi_write(phy, chg_det, 0x34);
1000 1001 1002
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/* Data contact detection enable */
1003
		ulpi_write(phy, 0x10, 0x85);
1004 1005 1006 1007 1008 1009 1010 1011
		break;
	default:
		break;
	}
}

static void msm_chg_block_on(struct msm_otg *motg)
{
1012
	struct usb_phy *phy = &motg->phy;
1013 1014 1015
	u32 func_ctrl, chg_det;

	/* put the controller in non-driving mode */
1016
	func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
1017 1018
	func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
	func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1019
	ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
1020 1021 1022

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
1023
		chg_det = ulpi_read(phy, 0x34);
1024 1025
		/* control chg block via ULPI */
		chg_det &= ~(1 << 3);
1026
		ulpi_write(phy, chg_det, 0x34);
1027 1028
		/* Turn on chg detect block */
		chg_det &= ~(1 << 1);
1029
		ulpi_write(phy, chg_det, 0x34);
1030 1031 1032 1033
		udelay(20);
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/* Clear charger detecting control bits */
1034
		ulpi_write(phy, 0x3F, 0x86);
1035
		/* Clear alt interrupt latch and enable bits */
1036 1037
		ulpi_write(phy, 0x1F, 0x92);
		ulpi_write(phy, 0x1F, 0x95);
1038 1039 1040 1041 1042 1043 1044 1045 1046
		udelay(100);
		break;
	default:
		break;
	}
}

static void msm_chg_block_off(struct msm_otg *motg)
{
1047
	struct usb_phy *phy = &motg->phy;
1048 1049 1050 1051
	u32 func_ctrl, chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
1052
		chg_det = ulpi_read(phy, 0x34);
1053 1054
		/* Turn off charger block */
		chg_det |= ~(1 << 1);
1055
		ulpi_write(phy, chg_det, 0x34);
1056 1057 1058
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/* Clear charger detecting control bits */
1059
		ulpi_write(phy, 0x3F, 0x86);
1060
		/* Clear alt interrupt latch and enable bits */
1061 1062
		ulpi_write(phy, 0x1F, 0x92);
		ulpi_write(phy, 0x1F, 0x95);
1063 1064 1065 1066 1067 1068
		break;
	default:
		break;
	}

	/* put the controller in normal mode */
1069
	func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
1070 1071
	func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
	func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1072
	ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
1073 1074 1075 1076 1077 1078 1079 1080 1081
}

#define MSM_CHG_DCD_POLL_TIME		(100 * HZ/1000) /* 100 msec */
#define MSM_CHG_DCD_MAX_RETRIES		6 /* Tdcd_tmout = 6 * 100 msec */
#define MSM_CHG_PRIMARY_DET_TIME	(40 * HZ/1000) /* TVDPSRC_ON */
#define MSM_CHG_SECONDARY_DET_TIME	(40 * HZ/1000) /* TVDMSRC_ON */
static void msm_chg_detect_work(struct work_struct *w)
{
	struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1082
	struct usb_phy *phy = &motg->phy;
1083 1084 1085
	bool is_dcd, tmout, vout;
	unsigned long delay;

1086
	dev_dbg(phy->dev, "chg detection work\n");
1087 1088
	switch (motg->chg_state) {
	case USB_CHG_STATE_UNDEFINED:
1089
		pm_runtime_get_sync(phy->dev);
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
		msm_chg_block_on(motg);
		msm_chg_enable_dcd(motg);
		motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
		motg->dcd_retries = 0;
		delay = MSM_CHG_DCD_POLL_TIME;
		break;
	case USB_CHG_STATE_WAIT_FOR_DCD:
		is_dcd = msm_chg_check_dcd(motg);
		tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
		if (is_dcd || tmout) {
			msm_chg_disable_dcd(motg);
			msm_chg_enable_primary_det(motg);
			delay = MSM_CHG_PRIMARY_DET_TIME;
			motg->chg_state = USB_CHG_STATE_DCD_DONE;
		} else {
			delay = MSM_CHG_DCD_POLL_TIME;
		}
		break;
	case USB_CHG_STATE_DCD_DONE:
		vout = msm_chg_check_primary_det(motg);
		if (vout) {
			msm_chg_enable_secondary_det(motg);
			delay = MSM_CHG_SECONDARY_DET_TIME;
			motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
		} else {
			motg->chg_type = USB_SDP_CHARGER;
			motg->chg_state = USB_CHG_STATE_DETECTED;
			delay = 0;
		}
		break;
	case USB_CHG_STATE_PRIMARY_DONE:
		vout = msm_chg_check_secondary_det(motg);
		if (vout)
			motg->chg_type = USB_DCP_CHARGER;
		else
			motg->chg_type = USB_CDP_CHARGER;
		motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
		/* fall through */
	case USB_CHG_STATE_SECONDARY_DONE:
		motg->chg_state = USB_CHG_STATE_DETECTED;
	case USB_CHG_STATE_DETECTED:
		msm_chg_block_off(motg);
1132
		dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
1133 1134 1135 1136 1137 1138 1139 1140 1141
		schedule_work(&motg->sm_work);
		return;
	default:
		return;
	}

	schedule_delayed_work(&motg->chg_work, delay);
}

1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
/*
 * We support OTG, Peripheral only and Host only configurations. In case
 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
 * enabled when switch is controlled by user and default mode is supplied
 * by board file, which can be changed by userspace later.
 */
static void msm_otg_init_sm(struct msm_otg *motg)
{
	struct msm_otg_platform_data *pdata = motg->pdata;
	u32 otgsc = readl(USB_OTGSC);

	switch (pdata->mode) {
1155
	case USB_DR_MODE_OTG:
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
		if (pdata->otg_control == OTG_PHY_CONTROL) {
			if (otgsc & OTGSC_ID)
				set_bit(ID, &motg->inputs);
			else
				clear_bit(ID, &motg->inputs);

			if (otgsc & OTGSC_BSV)
				set_bit(B_SESS_VLD, &motg->inputs);
			else
				clear_bit(B_SESS_VLD, &motg->inputs);
		} else if (pdata->otg_control == OTG_USER_CONTROL) {
				set_bit(ID, &motg->inputs);
				clear_bit(B_SESS_VLD, &motg->inputs);
		}
		break;
1171
	case USB_DR_MODE_HOST:
1172 1173
		clear_bit(ID, &motg->inputs);
		break;
1174
	case USB_DR_MODE_PERIPHERAL:
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
		set_bit(ID, &motg->inputs);
		if (otgsc & OTGSC_BSV)
			set_bit(B_SESS_VLD, &motg->inputs);
		else
			clear_bit(B_SESS_VLD, &motg->inputs);
		break;
	default:
		break;
	}
}

static void msm_otg_sm_work(struct work_struct *w)
{
	struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1189
	struct usb_otg *otg = motg->phy.otg;
1190

1191
	switch (otg->state) {
1192
	case OTG_STATE_UNDEFINED:
A
Antoine Tenart 已提交
1193 1194
		dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
		msm_otg_reset(otg->usb_phy);
1195
		msm_otg_init_sm(motg);
1196
		otg->state = OTG_STATE_B_IDLE;
1197 1198
		/* FALL THROUGH */
	case OTG_STATE_B_IDLE:
A
Antoine Tenart 已提交
1199
		dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
1200 1201 1202
		if (!test_bit(ID, &motg->inputs) && otg->host) {
			/* disable BSV bit */
			writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
A
Antoine Tenart 已提交
1203
			msm_otg_start_host(otg->usb_phy, 1);
1204
			otg->state = OTG_STATE_A_HOST;
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
		} else if (test_bit(B_SESS_VLD, &motg->inputs)) {
			switch (motg->chg_state) {
			case USB_CHG_STATE_UNDEFINED:
				msm_chg_detect_work(&motg->chg_work.work);
				break;
			case USB_CHG_STATE_DETECTED:
				switch (motg->chg_type) {
				case USB_DCP_CHARGER:
					msm_otg_notify_charger(motg,
							IDEV_CHG_MAX);
					break;
				case USB_CDP_CHARGER:
					msm_otg_notify_charger(motg,
							IDEV_CHG_MAX);
A
Antoine Tenart 已提交
1219 1220
					msm_otg_start_peripheral(otg->usb_phy,
								 1);
1221
					otg->state
1222
						= OTG_STATE_B_PERIPHERAL;
1223 1224 1225
					break;
				case USB_SDP_CHARGER:
					msm_otg_notify_charger(motg, IUNIT);
A
Antoine Tenart 已提交
1226 1227
					msm_otg_start_peripheral(otg->usb_phy,
								 1);
1228
					otg->state
1229
						= OTG_STATE_B_PERIPHERAL;
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
					break;
				default:
					break;
				}
				break;
			default:
				break;
			}
		} else {
			/*
			 * If charger detection work is pending, decrement
			 * the pm usage counter to balance with the one that
			 * is incremented in charger detection work.
			 */
			if (cancel_delayed_work_sync(&motg->chg_work)) {
A
Antoine Tenart 已提交
1245 1246
				pm_runtime_put_sync(otg->usb_phy->dev);
				msm_otg_reset(otg->usb_phy);
1247 1248 1249 1250
			}
			msm_otg_notify_charger(motg, 0);
			motg->chg_state = USB_CHG_STATE_UNDEFINED;
			motg->chg_type = USB_INVALID_CHARGER;
1251
		}
1252

1253
		if (otg->state == OTG_STATE_B_IDLE)
A
Antoine Tenart 已提交
1254
			pm_runtime_put_sync(otg->usb_phy->dev);
1255 1256
		break;
	case OTG_STATE_B_PERIPHERAL:
A
Antoine Tenart 已提交
1257
		dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
1258 1259
		if (!test_bit(B_SESS_VLD, &motg->inputs) ||
				!test_bit(ID, &motg->inputs)) {
1260
			msm_otg_notify_charger(motg, 0);
A
Antoine Tenart 已提交
1261
			msm_otg_start_peripheral(otg->usb_phy, 0);
1262 1263
			motg->chg_state = USB_CHG_STATE_UNDEFINED;
			motg->chg_type = USB_INVALID_CHARGER;
1264
			otg->state = OTG_STATE_B_IDLE;
A
Antoine Tenart 已提交
1265
			msm_otg_reset(otg->usb_phy);
1266 1267 1268 1269
			schedule_work(w);
		}
		break;
	case OTG_STATE_A_HOST:
A
Antoine Tenart 已提交
1270
		dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
1271
		if (test_bit(ID, &motg->inputs)) {
A
Antoine Tenart 已提交
1272
			msm_otg_start_host(otg->usb_phy, 0);
1273
			otg->state = OTG_STATE_B_IDLE;
A
Antoine Tenart 已提交
1274
			msm_otg_reset(otg->usb_phy);
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
			schedule_work(w);
		}
		break;
	default:
		break;
	}
}

static irqreturn_t msm_otg_irq(int irq, void *data)
{
	struct msm_otg *motg = data;
1286
	struct usb_phy *phy = &motg->phy;
1287 1288
	u32 otgsc = 0;

1289 1290 1291
	if (atomic_read(&motg->in_lpm)) {
		disable_irq_nosync(irq);
		motg->async_int = 1;
1292
		pm_runtime_get(phy->dev);
1293 1294 1295
		return IRQ_HANDLED;
	}

1296 1297 1298 1299 1300 1301 1302 1303 1304
	otgsc = readl(USB_OTGSC);
	if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
		return IRQ_NONE;

	if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
		if (otgsc & OTGSC_ID)
			set_bit(ID, &motg->inputs);
		else
			clear_bit(ID, &motg->inputs);
1305 1306
		dev_dbg(phy->dev, "ID set/clear\n");
		pm_runtime_get_noresume(phy->dev);
1307 1308 1309 1310 1311
	} else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
		if (otgsc & OTGSC_BSV)
			set_bit(B_SESS_VLD, &motg->inputs);
		else
			clear_bit(B_SESS_VLD, &motg->inputs);
1312 1313
		dev_dbg(phy->dev, "BSV set/clear\n");
		pm_runtime_get_noresume(phy->dev);
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
	}

	writel(otgsc, USB_OTGSC);
	schedule_work(&motg->sm_work);
	return IRQ_HANDLED;
}

static int msm_otg_mode_show(struct seq_file *s, void *unused)
{
	struct msm_otg *motg = s->private;
1324
	struct usb_otg *otg = motg->phy.otg;
1325

1326
	switch (otg->state) {
1327
	case OTG_STATE_A_HOST:
1328
		seq_puts(s, "host\n");
1329 1330
		break;
	case OTG_STATE_B_PERIPHERAL:
1331
		seq_puts(s, "peripheral\n");
1332 1333
		break;
	default:
1334
		seq_puts(s, "none\n");
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		break;
	}

	return 0;
}

static int msm_otg_mode_open(struct inode *inode, struct file *file)
{
	return single_open(file, msm_otg_mode_show, inode->i_private);
}

static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
				size_t count, loff_t *ppos)
{
1349 1350
	struct seq_file *s = file->private_data;
	struct msm_otg *motg = s->private;
1351
	char buf[16];
1352
	struct usb_otg *otg = motg->phy.otg;
1353
	int status = count;
1354
	enum usb_dr_mode req_mode;
1355 1356 1357 1358 1359 1360 1361 1362 1363

	memset(buf, 0x00, sizeof(buf));

	if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
		status = -EFAULT;
		goto out;
	}

	if (!strncmp(buf, "host", 4)) {
1364
		req_mode = USB_DR_MODE_HOST;
1365
	} else if (!strncmp(buf, "peripheral", 10)) {
1366
		req_mode = USB_DR_MODE_PERIPHERAL;
1367
	} else if (!strncmp(buf, "none", 4)) {
1368
		req_mode = USB_DR_MODE_UNKNOWN;
1369 1370 1371 1372 1373 1374
	} else {
		status = -EINVAL;
		goto out;
	}

	switch (req_mode) {
1375
	case USB_DR_MODE_UNKNOWN:
1376
		switch (otg->state) {
1377 1378 1379 1380 1381 1382 1383 1384 1385
		case OTG_STATE_A_HOST:
		case OTG_STATE_B_PERIPHERAL:
			set_bit(ID, &motg->inputs);
			clear_bit(B_SESS_VLD, &motg->inputs);
			break;
		default:
			goto out;
		}
		break;
1386
	case USB_DR_MODE_PERIPHERAL:
1387
		switch (otg->state) {
1388 1389 1390 1391 1392 1393 1394 1395 1396
		case OTG_STATE_B_IDLE:
		case OTG_STATE_A_HOST:
			set_bit(ID, &motg->inputs);
			set_bit(B_SESS_VLD, &motg->inputs);
			break;
		default:
			goto out;
		}
		break;
1397
	case USB_DR_MODE_HOST:
1398
		switch (otg->state) {
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
		case OTG_STATE_B_IDLE:
		case OTG_STATE_B_PERIPHERAL:
			clear_bit(ID, &motg->inputs);
			break;
		default:
			goto out;
		}
		break;
	default:
		goto out;
	}

A
Antoine Tenart 已提交
1411
	pm_runtime_get_sync(otg->usb_phy->dev);
1412 1413 1414 1415 1416
	schedule_work(&motg->sm_work);
out:
	return status;
}

1417
static const struct file_operations msm_otg_mode_fops = {
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
	.open = msm_otg_mode_open,
	.read = seq_read,
	.write = msm_otg_mode_write,
	.llseek = seq_lseek,
	.release = single_release,
};

static struct dentry *msm_otg_dbg_root;
static struct dentry *msm_otg_dbg_mode;

static int msm_otg_debugfs_init(struct msm_otg *motg)
{
	msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);

	if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
		return -ENODEV;

	msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
				msm_otg_dbg_root, motg, &msm_otg_mode_fops);
	if (!msm_otg_dbg_mode) {
		debugfs_remove(msm_otg_dbg_root);
		msm_otg_dbg_root = NULL;
		return -ENODEV;
	}

	return 0;
}

static void msm_otg_debugfs_cleanup(void)
{
	debugfs_remove(msm_otg_dbg_mode);
	debugfs_remove(msm_otg_dbg_root);
}

1452
static const struct of_device_id msm_otg_dt_match[] = {
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
	{
		.compatible = "qcom,usb-otg-ci",
		.data = (void *) CI_45NM_INTEGRATED_PHY
	},
	{
		.compatible = "qcom,usb-otg-snps",
		.data = (void *) SNPS_28NM_INTEGRATED_PHY
	},
	{ }
};
MODULE_DEVICE_TABLE(of, msm_otg_dt_match);

1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
static int msm_otg_vbus_notifier(struct notifier_block *nb, unsigned long event,
				void *ptr)
{
	struct msm_usb_cable *vbus = container_of(nb, struct msm_usb_cable, nb);
	struct msm_otg *motg = container_of(vbus, struct msm_otg, vbus);

	if (event)
		set_bit(B_SESS_VLD, &motg->inputs);
	else
		clear_bit(B_SESS_VLD, &motg->inputs);

1476 1477 1478 1479 1480 1481 1482 1483
	if (test_bit(B_SESS_VLD, &motg->inputs)) {
		/* Switch D+/D- lines to Device connector */
		gpiod_set_value_cansleep(motg->switch_gpio, 0);
	} else {
		/* Switch D+/D- lines to Hub */
		gpiod_set_value_cansleep(motg->switch_gpio, 1);
	}

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
	schedule_work(&motg->sm_work);

	return NOTIFY_DONE;
}

static int msm_otg_id_notifier(struct notifier_block *nb, unsigned long event,
				void *ptr)
{
	struct msm_usb_cable *id = container_of(nb, struct msm_usb_cable, nb);
	struct msm_otg *motg = container_of(id, struct msm_otg, id);

	if (event)
		clear_bit(ID, &motg->inputs);
	else
		set_bit(ID, &motg->inputs);

	schedule_work(&motg->sm_work);

	return NOTIFY_DONE;
}

1505 1506 1507
static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
{
	struct msm_otg_platform_data *pdata;
1508
	struct extcon_dev *ext_id, *ext_vbus;
1509 1510 1511
	struct device_node *node = pdev->dev.of_node;
	struct property *prop;
	int len, ret, words;
1512
	u32 val, tmp[3];
1513 1514 1515 1516 1517 1518 1519

	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata)
		return -ENOMEM;

	motg->pdata = pdata;

1520 1521 1522
	pdata->phy_type = (enum msm_usb_phy_type)of_device_get_match_data(&pdev->dev);
	if (!pdata->phy_type)
		return 1;
1523

1524 1525 1526 1527 1528 1529
	motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
	if (IS_ERR(motg->link_rst))
		return PTR_ERR(motg->link_rst);

	motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
	if (IS_ERR(motg->phy_rst))
1530
		motg->phy_rst = NULL;
1531

1532
	pdata->mode = usb_get_dr_mode(&pdev->dev);
1533 1534 1535 1536 1537 1538 1539 1540
	if (pdata->mode == USB_DR_MODE_UNKNOWN)
		pdata->mode = USB_DR_MODE_OTG;

	pdata->otg_control = OTG_PHY_CONTROL;
	if (!of_property_read_u32(node, "qcom,otg-control", &val))
		if (val == OTG_PMIC_CONTROL)
			pdata->otg_control = val;

1541 1542 1543
	if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
		motg->phy_number = val;

1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
	motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
	motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
	motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;

	if (of_get_property(node, "qcom,vdd-levels", &len) &&
	    len == sizeof(tmp)) {
		of_property_read_u32_array(node, "qcom,vdd-levels",
					   tmp, len / sizeof(*tmp));
		motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
		motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
		motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
	}

1557 1558
	motg->manual_pullup = of_property_read_bool(node, "qcom,manual-pullup");

1559 1560 1561 1562 1563
	motg->switch_gpio = devm_gpiod_get_optional(&pdev->dev, "switch",
						    GPIOD_OUT_LOW);
	if (IS_ERR(motg->switch_gpio))
		return PTR_ERR(motg->switch_gpio);

1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
	ext_id = ERR_PTR(-ENODEV);
	ext_vbus = ERR_PTR(-ENODEV);
	if (of_property_read_bool(node, "extcon")) {

		/* Each one of them is not mandatory */
		ext_vbus = extcon_get_edev_by_phandle(&pdev->dev, 0);
		if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
			return PTR_ERR(ext_vbus);

		ext_id = extcon_get_edev_by_phandle(&pdev->dev, 1);
		if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
			return PTR_ERR(ext_id);
	}

	if (!IS_ERR(ext_vbus)) {
1579
		motg->vbus.extcon = ext_vbus;
1580
		motg->vbus.nb.notifier_call = msm_otg_vbus_notifier;
1581 1582
		ret = extcon_register_notifier(ext_vbus, EXTCON_USB,
						&motg->vbus.nb);
1583 1584 1585 1586 1587
		if (ret < 0) {
			dev_err(&pdev->dev, "register VBUS notifier failed\n");
			return ret;
		}

1588
		ret = extcon_get_cable_state_(ext_vbus, EXTCON_USB);
1589 1590 1591 1592 1593 1594 1595
		if (ret)
			set_bit(B_SESS_VLD, &motg->inputs);
		else
			clear_bit(B_SESS_VLD, &motg->inputs);
	}

	if (!IS_ERR(ext_id)) {
1596
		motg->id.extcon = ext_id;
1597
		motg->id.nb.notifier_call = msm_otg_id_notifier;
1598 1599
		ret = extcon_register_notifier(ext_id, EXTCON_USB_HOST,
						&motg->id.nb);
1600 1601
		if (ret < 0) {
			dev_err(&pdev->dev, "register ID notifier failed\n");
1602 1603
			extcon_unregister_notifier(motg->vbus.extcon,
						   EXTCON_USB, &motg->vbus.nb);
1604 1605 1606
			return ret;
		}

1607
		ret = extcon_get_cable_state_(ext_id, EXTCON_USB_HOST);
1608 1609 1610 1611 1612 1613
		if (ret)
			clear_bit(ID, &motg->inputs);
		else
			set_bit(ID, &motg->inputs);
	}

1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
	prop = of_find_property(node, "qcom,phy-init-sequence", &len);
	if (!prop || !len)
		return 0;

	words = len / sizeof(u32);

	if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
		dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
		return 0;
	}

	pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
1626
	if (!pdata->phy_init_seq)
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
		return 0;

	ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
					 pdata->phy_init_seq, words);
	if (!ret)
		pdata->phy_init_sz = words;

	return 0;
}

1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
static int msm_otg_reboot_notify(struct notifier_block *this,
				 unsigned long code, void *unused)
{
	struct msm_otg *motg = container_of(this, struct msm_otg, reboot);

	/*
	 * Ensure that D+/D- lines are routed to uB connector, so
	 * we could load bootloader/kernel at next reboot
	 */
	gpiod_set_value_cansleep(motg->switch_gpio, 0);
	return NOTIFY_DONE;
}

1650
static int msm_otg_probe(struct platform_device *pdev)
1651
{
1652
	struct regulator_bulk_data regs[3];
1653
	int ret = 0;
1654 1655
	struct device_node *np = pdev->dev.of_node;
	struct msm_otg_platform_data *pdata;
1656 1657
	struct resource *res;
	struct msm_otg *motg;
1658
	struct usb_phy *phy;
1659
	void __iomem *phy_select;
1660

1661
	motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
1662
	if (!motg)
1663 1664
		return -ENOMEM;

1665 1666
	motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
				     GFP_KERNEL);
1667
	if (!motg->phy.otg)
1668
		return -ENOMEM;
1669 1670 1671

	phy = &motg->phy;
	phy->dev = &pdev->dev;
1672

1673
	motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
1674 1675
	if (IS_ERR(motg->clk)) {
		dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
1676
		return PTR_ERR(motg->clk);
1677
	}
1678 1679 1680 1681 1682

	/*
	 * If USB Core is running its protocol engine based on CORE CLK,
	 * CORE CLK  must be running at >55Mhz for correct HSUSB
	 * operation and USB core cannot tolerate frequency changes on
1683
	 * CORE CLK.
1684
	 */
1685
	motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
1686 1687
	if (IS_ERR(motg->pclk)) {
		dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
1688
		return PTR_ERR(motg->pclk);
1689 1690 1691 1692 1693 1694 1695
	}

	/*
	 * USB core clock is not present on all MSM chips. This
	 * clock is introduced to remove the dependency on AXI
	 * bus frequency.
	 */
1696 1697
	motg->core_clk = devm_clk_get(&pdev->dev,
				      np ? "alt_core" : "usb_hs_core_clk");
1698 1699

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1700 1701 1702 1703 1704
	if (!res)
		return -EINVAL;
	motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
	if (!motg->regs)
		return -ENOMEM;
1705

1706 1707 1708 1709 1710 1711 1712 1713 1714
	pdata = dev_get_platdata(&pdev->dev);
	if (!pdata) {
		if (!np)
			return -ENXIO;
		ret = msm_otg_read_dt(pdev, motg);
		if (ret)
			return ret;
	}

1715 1716 1717 1718 1719 1720 1721
	/*
	 * NOTE: The PHYs can be multiplexed between the chipidea controller
	 * and the dwc3 controller, using a single bit. It is important that
	 * the dwc3 driver does not set this bit in an incompatible way.
	 */
	if (motg->phy_number) {
		phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
1722 1723 1724 1725
		if (!phy_select) {
			ret = -ENOMEM;
			goto unregister_extcon;
		}
1726
		/* Enable second PHY with the OTG port */
1727
		writel(0x1, phy_select);
1728 1729
	}

1730 1731 1732
	dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);

	motg->irq = platform_get_irq(pdev, 0);
1733
	if (motg->irq < 0) {
1734
		dev_err(&pdev->dev, "platform_get_irq failed\n");
1735 1736
		ret = motg->irq;
		goto unregister_extcon;
1737 1738
	}

1739 1740 1741
	regs[0].supply = "vddcx";
	regs[1].supply = "v3p3";
	regs[2].supply = "v1p8";
1742 1743 1744

	ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
	if (ret)
1745
		goto unregister_extcon;
1746 1747 1748 1749 1750 1751

	motg->vddcx = regs[0].consumer;
	motg->v3p3  = regs[1].consumer;
	motg->v1p8  = regs[2].consumer;

	clk_set_rate(motg->clk, 60000000);
1752

1753 1754
	clk_prepare_enable(motg->clk);
	clk_prepare_enable(motg->pclk);
1755

1756 1757 1758
	if (!IS_ERR(motg->core_clk))
		clk_prepare_enable(motg->core_clk);

1759 1760 1761
	ret = msm_hsusb_init_vddcx(motg, 1);
	if (ret) {
		dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1762
		goto disable_clks;
1763 1764 1765 1766 1767
	}

	ret = msm_hsusb_ldo_init(motg, 1);
	if (ret) {
		dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1768
		goto disable_vddcx;
1769
	}
1770
	ret = msm_hsusb_ldo_set_mode(motg, 1);
1771 1772
	if (ret) {
		dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1773
		goto disable_ldo;
1774 1775
	}

1776 1777 1778 1779
	writel(0, USB_USBINTR);
	writel(0, USB_OTGSC);

	INIT_WORK(&motg->sm_work, msm_otg_sm_work);
1780
	INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
1781
	ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
1782 1783 1784
					"msm_otg", motg);
	if (ret) {
		dev_err(&pdev->dev, "request irq failed\n");
1785
		goto disable_ldo;
1786 1787
	}

1788
	phy->init = msm_phy_init;
1789
	phy->set_power = msm_otg_set_power;
1790
	phy->notify_disconnect = msm_phy_notify_disconnect;
1791
	phy->type = USB_PHY_TYPE_USB2;
1792 1793

	phy->io_ops = &msm_otg_io_ops;
1794

A
Antoine Tenart 已提交
1795
	phy->otg->usb_phy = &motg->phy;
1796 1797
	phy->otg->set_host = msm_otg_set_host;
	phy->otg->set_peripheral = msm_otg_set_peripheral;
1798

1799 1800
	msm_usb_reset(phy);

1801
	ret = usb_add_phy_dev(&motg->phy);
1802
	if (ret) {
1803
		dev_err(&pdev->dev, "usb_add_phy failed\n");
1804
		goto disable_ldo;
1805 1806 1807 1808 1809
	}

	platform_set_drvdata(pdev, motg);
	device_init_wakeup(&pdev->dev, 1);

1810
	if (motg->pdata->mode == USB_DR_MODE_OTG &&
1811
		motg->pdata->otg_control == OTG_USER_CONTROL) {
1812 1813
		ret = msm_otg_debugfs_init(motg);
		if (ret)
1814
			dev_dbg(&pdev->dev, "Can not create mode change file\n");
1815 1816
	}

1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827
	if (test_bit(B_SESS_VLD, &motg->inputs)) {
		/* Switch D+/D- lines to Device connector */
		gpiod_set_value_cansleep(motg->switch_gpio, 0);
	} else {
		/* Switch D+/D- lines to Hub */
		gpiod_set_value_cansleep(motg->switch_gpio, 1);
	}

	motg->reboot.notifier_call = msm_otg_reboot_notify;
	register_reboot_notifier(&motg->reboot);

1828 1829
	pm_runtime_set_active(&pdev->dev);
	pm_runtime_enable(&pdev->dev);
1830

1831
	return 0;
1832 1833 1834 1835 1836

disable_ldo:
	msm_hsusb_ldo_init(motg, 0);
disable_vddcx:
	msm_hsusb_init_vddcx(motg, 0);
1837
disable_clks:
1838 1839
	clk_disable_unprepare(motg->pclk);
	clk_disable_unprepare(motg->clk);
1840 1841
	if (!IS_ERR(motg->core_clk))
		clk_disable_unprepare(motg->core_clk);
1842 1843 1844 1845 1846 1847
unregister_extcon:
	extcon_unregister_notifier(motg->id.extcon,
				   EXTCON_USB_HOST, &motg->id.nb);
	extcon_unregister_notifier(motg->vbus.extcon,
				   EXTCON_USB, &motg->vbus.nb);

1848 1849 1850
	return ret;
}

B
Bill Pemberton 已提交
1851
static int msm_otg_remove(struct platform_device *pdev)
1852 1853
{
	struct msm_otg *motg = platform_get_drvdata(pdev);
1854
	struct usb_phy *phy = &motg->phy;
1855
	int cnt = 0;
1856

1857
	if (phy->otg->host || phy->otg->gadget)
1858 1859
		return -EBUSY;

1860 1861 1862 1863 1864 1865 1866 1867
	unregister_reboot_notifier(&motg->reboot);

	/*
	 * Ensure that D+/D- lines are routed to uB connector, so
	 * we could load bootloader/kernel at next reboot
	 */
	gpiod_set_value_cansleep(motg->switch_gpio, 0);

1868 1869
	extcon_unregister_notifier(motg->id.extcon, EXTCON_USB_HOST, &motg->id.nb);
	extcon_unregister_notifier(motg->vbus.extcon, EXTCON_USB, &motg->vbus.nb);
1870

1871
	msm_otg_debugfs_cleanup();
1872
	cancel_delayed_work_sync(&motg->chg_work);
1873
	cancel_work_sync(&motg->sm_work);
1874

1875
	pm_runtime_resume(&pdev->dev);
1876

1877
	device_init_wakeup(&pdev->dev, 0);
1878
	pm_runtime_disable(&pdev->dev);
1879

1880
	usb_remove_phy(phy);
1881
	disable_irq(motg->irq);
1882

1883 1884 1885
	/*
	 * Put PHY in low power mode.
	 */
1886 1887
	ulpi_read(phy, 0x14);
	ulpi_write(phy, 0x08, 0x09);
1888 1889 1890 1891 1892 1893 1894 1895 1896

	writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
	while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
		if (readl(USB_PORTSC) & PORTSC_PHCD)
			break;
		udelay(1);
		cnt++;
	}
	if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
1897
		dev_err(phy->dev, "Unable to suspend PHY\n");
1898

1899 1900
	clk_disable_unprepare(motg->pclk);
	clk_disable_unprepare(motg->clk);
1901
	if (!IS_ERR(motg->core_clk))
1902
		clk_disable_unprepare(motg->core_clk);
1903
	msm_hsusb_ldo_init(motg, 0);
1904

1905
	pm_runtime_set_suspended(&pdev->dev);
1906 1907 1908 1909

	return 0;
}

1910
#ifdef CONFIG_PM
1911 1912 1913
static int msm_otg_runtime_idle(struct device *dev)
{
	struct msm_otg *motg = dev_get_drvdata(dev);
1914
	struct usb_otg *otg = motg->phy.otg;
1915 1916 1917 1918 1919 1920 1921 1922 1923

	dev_dbg(dev, "OTG runtime idle\n");

	/*
	 * It is observed some times that a spurious interrupt
	 * comes when PHY is put into LPM immediately after PHY reset.
	 * This 1 sec delay also prevents entering into LPM immediately
	 * after asynchronous interrupt.
	 */
1924
	if (otg->state != OTG_STATE_UNDEFINED)
1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946
		pm_schedule_suspend(dev, 1000);

	return -EAGAIN;
}

static int msm_otg_runtime_suspend(struct device *dev)
{
	struct msm_otg *motg = dev_get_drvdata(dev);

	dev_dbg(dev, "OTG runtime suspend\n");
	return msm_otg_suspend(motg);
}

static int msm_otg_runtime_resume(struct device *dev)
{
	struct msm_otg *motg = dev_get_drvdata(dev);

	dev_dbg(dev, "OTG runtime resume\n");
	return msm_otg_resume(motg);
}
#endif

1947
#ifdef CONFIG_PM_SLEEP
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
static int msm_otg_pm_suspend(struct device *dev)
{
	struct msm_otg *motg = dev_get_drvdata(dev);

	dev_dbg(dev, "OTG PM suspend\n");
	return msm_otg_suspend(motg);
}

static int msm_otg_pm_resume(struct device *dev)
{
	struct msm_otg *motg = dev_get_drvdata(dev);
	int ret;

	dev_dbg(dev, "OTG PM resume\n");

	ret = msm_otg_resume(motg);
	if (ret)
		return ret;

	/*
	 * Runtime PM Documentation recommends bringing the
	 * device to full powered state upon resume.
	 */
	pm_runtime_disable(dev);
	pm_runtime_set_active(dev);
	pm_runtime_enable(dev);

	return 0;
}
#endif

static const struct dev_pm_ops msm_otg_dev_pm_ops = {
1980 1981 1982
	SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
	SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
				msm_otg_runtime_idle)
1983 1984
};

1985
static struct platform_driver msm_otg_driver = {
1986
	.probe = msm_otg_probe,
B
Bill Pemberton 已提交
1987
	.remove = msm_otg_remove,
1988 1989
	.driver = {
		.name = DRIVER_NAME,
1990
		.pm = &msm_otg_dev_pm_ops,
1991
		.of_match_table = msm_otg_dt_match,
1992 1993 1994
	},
};

1995
module_platform_driver(msm_otg_driver);
1996 1997 1998

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("MSM USB transceiver driver");