msm_otg.c 41.9 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 21 22 23 24 25 26 27 28 29 30 31
 *
 * 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>
#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>
32
#include <linux/pm_runtime.h>
33 34 35 36 37 38 39 40

#include <linux/usb.h>
#include <linux/usb/otg.h>
#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>
41
#include <linux/regulator/consumer.h>
42 43 44 45 46 47 48

#include <mach/clk.h>

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

#define ULPI_IO_TIMEOUT_USEC	(10 * 1000)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

#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 */

static struct regulator *hsusb_3p3;
static struct regulator *hsusb_1p8;
static struct regulator *hsusb_vddcx;

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

	if (init) {
72
		hsusb_vddcx = regulator_get(motg->phy.dev, "HSUSB_VDDCX");
73
		if (IS_ERR(hsusb_vddcx)) {
74
			dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
75 76 77 78 79 80 81
			return PTR_ERR(hsusb_vddcx);
		}

		ret = regulator_set_voltage(hsusb_vddcx,
				USB_PHY_VDD_DIG_VOL_MIN,
				USB_PHY_VDD_DIG_VOL_MAX);
		if (ret) {
82
			dev_err(motg->phy.dev, "unable to set the voltage "
83 84 85 86 87 88 89
					"for hsusb vddcx\n");
			regulator_put(hsusb_vddcx);
			return ret;
		}

		ret = regulator_enable(hsusb_vddcx);
		if (ret) {
90
			dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
91 92 93 94
			regulator_put(hsusb_vddcx);
		}
	} else {
		ret = regulator_set_voltage(hsusb_vddcx, 0,
95
			USB_PHY_VDD_DIG_VOL_MAX);
96
		if (ret)
97
			dev_err(motg->phy.dev, "unable to set the voltage "
98 99 100
					"for hsusb vddcx\n");
		ret = regulator_disable(hsusb_vddcx);
		if (ret)
101
			dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
102 103 104 105 106 107 108 109 110 111 112 113

		regulator_put(hsusb_vddcx);
	}

	return ret;
}

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

	if (init) {
114
		hsusb_3p3 = regulator_get(motg->phy.dev, "HSUSB_3p3");
115
		if (IS_ERR(hsusb_3p3)) {
116
			dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
117 118 119 120 121 122
			return PTR_ERR(hsusb_3p3);
		}

		rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
				USB_PHY_3P3_VOL_MAX);
		if (rc) {
123
			dev_err(motg->phy.dev, "unable to set voltage level "
124 125 126 127 128
					"for hsusb 3p3\n");
			goto put_3p3;
		}
		rc = regulator_enable(hsusb_3p3);
		if (rc) {
129
			dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
130 131
			goto put_3p3;
		}
132
		hsusb_1p8 = regulator_get(motg->phy.dev, "HSUSB_1p8");
133
		if (IS_ERR(hsusb_1p8)) {
134
			dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
135 136 137 138 139 140
			rc = PTR_ERR(hsusb_1p8);
			goto disable_3p3;
		}
		rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
				USB_PHY_1P8_VOL_MAX);
		if (rc) {
141
			dev_err(motg->phy.dev, "unable to set voltage level "
142 143 144 145 146
					"for hsusb 1p8\n");
			goto put_1p8;
		}
		rc = regulator_enable(hsusb_1p8);
		if (rc) {
147
			dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
			goto put_1p8;
		}

		return 0;
	}

	regulator_disable(hsusb_1p8);
put_1p8:
	regulator_put(hsusb_1p8);
disable_3p3:
	regulator_disable(hsusb_3p3);
put_3p3:
	regulator_put(hsusb_3p3);
	return rc;
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
#ifdef CONFIG_PM_SLEEP
#define USB_PHY_SUSP_DIG_VOL  500000
static int msm_hsusb_config_vddcx(int high)
{
	int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
	int min_vol;
	int ret;

	if (high)
		min_vol = USB_PHY_VDD_DIG_VOL_MIN;
	else
		min_vol = USB_PHY_SUSP_DIG_VOL;

	ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
	if (ret) {
		pr_err("%s: unable to set the voltage for regulator "
			"HSUSB_VDDCX\n", __func__);
		return ret;
	}

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

	return ret;
}
#endif

190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
static int msm_hsusb_ldo_set_mode(int on)
{
	int ret = 0;

	if (!hsusb_1p8 || IS_ERR(hsusb_1p8)) {
		pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
		return -ENODEV;
	}

	if (!hsusb_3p3 || IS_ERR(hsusb_3p3)) {
		pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
		return -ENODEV;
	}

	if (on) {
		ret = regulator_set_optimum_mode(hsusb_1p8,
				USB_PHY_1P8_HPM_LOAD);
		if (ret < 0) {
			pr_err("%s: Unable to set HPM of the regulator "
				"HSUSB_1p8\n", __func__);
			return ret;
		}
		ret = regulator_set_optimum_mode(hsusb_3p3,
				USB_PHY_3P3_HPM_LOAD);
		if (ret < 0) {
			pr_err("%s: Unable to set HPM of the regulator "
				"HSUSB_3p3\n", __func__);
			regulator_set_optimum_mode(hsusb_1p8,
				USB_PHY_1P8_LPM_LOAD);
			return ret;
		}
	} else {
		ret = regulator_set_optimum_mode(hsusb_1p8,
				USB_PHY_1P8_LPM_LOAD);
		if (ret < 0)
			pr_err("%s: Unable to set LPM of the regulator "
				"HSUSB_1p8\n", __func__);
		ret = regulator_set_optimum_mode(hsusb_3p3,
				USB_PHY_3P3_LPM_LOAD);
		if (ret < 0)
			pr_err("%s: Unable to set LPM of the regulator "
				"HSUSB_3p3\n", __func__);
	}

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

238
static int ulpi_read(struct usb_phy *phy, u32 reg)
239
{
240
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	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) {
256
		dev_err(phy->dev, "ulpi_read: timeout %08x\n",
257 258 259 260 261 262
			readl(USB_ULPI_VIEWPORT));
		return -ETIMEDOUT;
	}
	return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
}

263
static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
264
{
265
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
	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) {
282
		dev_err(phy->dev, "ulpi_write: timeout\n");
283 284 285 286 287
		return -ETIMEDOUT;
	}
	return 0;
}

288
static struct usb_phy_io_ops msm_otg_io_ops = {
289 290 291 292 293 294 295 296 297 298 299 300 301
	.read = ulpi_read,
	.write = ulpi_write,
};

static void ulpi_init(struct msm_otg *motg)
{
	struct msm_otg_platform_data *pdata = motg->pdata;
	int *seq = pdata->phy_init_seq;

	if (!seq)
		return;

	while (seq[0] >= 0) {
302
		dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
303
				seq[0], seq[1]);
304
		ulpi_write(&motg->phy, seq[0], seq[1]);
305 306 307 308 309 310 311 312 313 314 315
		seq += 2;
	}
}

static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
{
	int ret;

	if (assert) {
		ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
		if (ret)
316
			dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
317 318 319
	} else {
		ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
		if (ret)
320
			dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
321 322 323 324 325 326 327 328 329 330
	}
	return ret;
}

static int msm_otg_phy_clk_reset(struct msm_otg *motg)
{
	int ret;

	ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
	if (ret) {
331
		dev_err(motg->phy.dev, "usb phy clk assert failed\n");
332 333 334 335 336
		return ret;
	}
	usleep_range(10000, 12000);
	ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
	if (ret)
337
		dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	return ret;
}

static int msm_otg_phy_reset(struct msm_otg *motg)
{
	u32 val;
	int ret;
	int retries;

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

	val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
	writel(val | PORTSC_PTS_ULPI, USB_PORTSC);

	for (retries = 3; retries > 0; retries--) {
361
		ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
				ULPI_CLR(ULPI_FUNC_CTRL));
		if (!ret)
			break;
		ret = msm_otg_phy_clk_reset(motg);
		if (ret)
			return ret;
	}
	if (!retries)
		return -ETIMEDOUT;

	/* This reset calibrates the phy, if the above write succeeded */
	ret = msm_otg_phy_clk_reset(motg);
	if (ret)
		return ret;

	for (retries = 3; retries > 0; retries--) {
378
		ret = ulpi_read(&motg->phy, ULPI_DEBUG);
379 380 381 382 383 384 385 386 387
		if (ret != -ETIMEDOUT)
			break;
		ret = msm_otg_phy_clk_reset(motg);
		if (ret)
			return ret;
	}
	if (!retries)
		return -ETIMEDOUT;

388
	dev_info(motg->phy.dev, "phy_reset: success\n");
389 390 391 392
	return 0;
}

#define LINK_RESET_TIMEOUT_USEC		(250 * 1000)
393
static int msm_otg_reset(struct usb_phy *phy)
394
{
395
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
396 397 398 399 400 401 402 403
	struct msm_otg_platform_data *pdata = motg->pdata;
	int cnt = 0;
	int ret;
	u32 val = 0;
	u32 ulpi_val = 0;

	ret = msm_otg_phy_reset(motg);
	if (ret) {
404
		dev_err(phy->dev, "phy_reset failed\n");
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
		return ret;
	}

	ulpi_init(motg);

	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;

	/* select ULPI phy */
	writel(0x80000000, USB_PORTSC);

	msleep(100);

	writel(0x0, USB_AHBBURST);
	writel(0x00, USB_AHBMODE);

	if (pdata->otg_control == OTG_PHY_CONTROL) {
		val = readl(USB_OTGSC);
		if (pdata->mode == USB_OTG) {
			ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
			val |= OTGSC_IDIE | OTGSC_BSVIE;
		} else if (pdata->mode == USB_PERIPHERAL) {
			ulpi_val = ULPI_INT_SESS_VALID;
			val |= OTGSC_BSVIE;
		}
		writel(val, USB_OTGSC);
438 439
		ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
		ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
440 441 442 443 444
	}

	return 0;
}

445
#define PHY_SUSPEND_TIMEOUT_USEC	(500 * 1000)
446 447 448
#define PHY_RESUME_TIMEOUT_USEC	(100 * 1000)

#ifdef CONFIG_PM_SLEEP
449 450
static int msm_otg_suspend(struct msm_otg *motg)
{
451 452
	struct usb_phy *phy = &motg->phy;
	struct usb_bus *bus = phy->otg->host;
453 454 455 456 457 458 459 460
	struct msm_otg_platform_data *pdata = motg->pdata;
	int cnt = 0;

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

	disable_irq(motg->irq);
	/*
461 462
	 * Chipidea 45-nm PHY suspend sequence:
	 *
463 464 465 466
	 * 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).
467
	 *
468 469 470 471
	 * 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.
472
	 *
473 474 475
	 * PLL is not turned off when PHY enters into low power mode (LPM).
	 * Disable PLL for maximum power savings.
	 */
476 477

	if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
478
		ulpi_read(phy, 0x14);
479
		if (pdata->otg_control == OTG_PHY_CONTROL)
480 481
			ulpi_write(phy, 0x01, 0x30);
		ulpi_write(phy, 0x08, 0x09);
482
	}
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

	/*
	 * 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) {
498 499
		dev_err(phy->dev, "Unable to suspend PHY\n");
		msm_otg_reset(phy);
500 501 502 503 504 505 506 507 508 509 510 511 512
		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);

513 514 515 516
	if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
			motg->pdata->otg_control == OTG_PMIC_CONTROL)
		writel(readl(USB_PHY_CTRL) | PHY_RETEN, USB_PHY_CTRL);

517 518 519 520 521
	clk_disable(motg->pclk);
	clk_disable(motg->clk);
	if (motg->core_clk)
		clk_disable(motg->core_clk);

522 523 524
	if (!IS_ERR(motg->pclk_src))
		clk_disable(motg->pclk_src);

525 526 527 528 529 530
	if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
			motg->pdata->otg_control == OTG_PMIC_CONTROL) {
		msm_hsusb_ldo_set_mode(0);
		msm_hsusb_config_vddcx(0);
	}

531
	if (device_may_wakeup(phy->dev))
532 533 534 535 536 537 538
		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);

539
	dev_info(phy->dev, "USB in low power mode\n");
540 541 542 543 544 545

	return 0;
}

static int msm_otg_resume(struct msm_otg *motg)
{
546 547
	struct usb_phy *phy = &motg->phy;
	struct usb_bus *bus = phy->otg->host;
548 549 550 551 552 553
	int cnt = 0;
	unsigned temp;

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

554 555 556
	if (!IS_ERR(motg->pclk_src))
		clk_enable(motg->pclk_src);

557 558 559 560 561
	clk_enable(motg->pclk);
	clk_enable(motg->clk);
	if (motg->core_clk)
		clk_enable(motg->core_clk);

562 563 564 565 566 567 568
	if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
			motg->pdata->otg_control == OTG_PMIC_CONTROL) {
		msm_hsusb_ldo_set_mode(1);
		msm_hsusb_config_vddcx(1);
		writel(readl(USB_PHY_CTRL) & ~PHY_RETEN, USB_PHY_CTRL);
	}

569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	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.
		 */
595
		dev_err(phy->dev, "Unable to resume USB."
596
				"Re-plugin the cable\n");
597
		msm_otg_reset(phy);
598 599 600
	}

skip_phy_resume:
601
	if (device_may_wakeup(phy->dev))
602 603 604 605
		disable_irq_wake(motg->irq);
	if (bus)
		set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);

606 607
	atomic_set(&motg->in_lpm, 0);

608 609
	if (motg->async_int) {
		motg->async_int = 0;
610
		pm_runtime_put(phy->dev);
611 612 613
		enable_irq(motg->irq);
	}

614
	dev_info(phy->dev, "USB exited from low power mode\n");
615 616 617

	return 0;
}
618
#endif
619

620 621 622 623 624 625
static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
{
	if (motg->cur_power == mA)
		return;

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

630
static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
631
{
632
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
633 634 635 636 637 638 639 640 641 642 643 644 645 646

	/*
	 * 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;
}

647
static void msm_otg_start_host(struct usb_phy *phy, int on)
648
{
649
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
650 651 652
	struct msm_otg_platform_data *pdata = motg->pdata;
	struct usb_hcd *hcd;

653
	if (!phy->otg->host)
654 655
		return;

656
	hcd = bus_to_hcd(phy->otg->host);
657 658

	if (on) {
659
		dev_dbg(phy->dev, "host on\n");
660 661 662 663 664 665 666 667 668 669 670 671 672 673

		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);
#endif
	} else {
674
		dev_dbg(phy->dev, "host off\n");
675 676 677 678 679 680 681 682 683 684 685

#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);
	}
}

686
static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
687
{
688
	struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
689 690 691 692 693 694 695
	struct usb_hcd *hcd;

	/*
	 * Fail host registration if this board can support
	 * only peripheral configuration.
	 */
	if (motg->pdata->mode == USB_PERIPHERAL) {
696
		dev_info(otg->phy->dev, "Host mode is not supported\n");
697 698 699 700
		return -ENODEV;
	}

	if (!host) {
701 702 703
		if (otg->phy->state == OTG_STATE_A_HOST) {
			pm_runtime_get_sync(otg->phy->dev);
			msm_otg_start_host(otg->phy, 0);
704
			otg->host = NULL;
705
			otg->phy->state = OTG_STATE_UNDEFINED;
706 707 708 709 710 711 712 713 714 715 716 717
			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;
718
	dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
719 720 721 722 723

	/*
	 * Kick the state machine work, if peripheral is not supported
	 * or peripheral is already registered with us.
	 */
724
	if (motg->pdata->mode == USB_HOST || otg->gadget) {
725
		pm_runtime_get_sync(otg->phy->dev);
726
		schedule_work(&motg->sm_work);
727
	}
728 729 730 731

	return 0;
}

732
static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
733
{
734
	struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
735 736
	struct msm_otg_platform_data *pdata = motg->pdata;

737
	if (!phy->otg->gadget)
738 739 740
		return;

	if (on) {
741
		dev_dbg(phy->dev, "gadget on\n");
742 743 744 745 746 747 748
		/*
		 * 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);
749
		usb_gadget_vbus_connect(phy->otg->gadget);
750
	} else {
751 752
		dev_dbg(phy->dev, "gadget off\n");
		usb_gadget_vbus_disconnect(phy->otg->gadget);
753 754 755 756 757 758
		if (pdata->setup_gpio)
			pdata->setup_gpio(OTG_STATE_UNDEFINED);
	}

}

759 760
static int msm_otg_set_peripheral(struct usb_otg *otg,
					struct usb_gadget *gadget)
761
{
762
	struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
763 764 765 766 767 768

	/*
	 * Fail peripheral registration if this board can support
	 * only host configuration.
	 */
	if (motg->pdata->mode == USB_HOST) {
769
		dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
770 771 772 773
		return -ENODEV;
	}

	if (!gadget) {
774 775 776
		if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
			pm_runtime_get_sync(otg->phy->dev);
			msm_otg_start_peripheral(otg->phy, 0);
777
			otg->gadget = NULL;
778
			otg->phy->state = OTG_STATE_UNDEFINED;
779 780 781 782 783 784 785 786
			schedule_work(&motg->sm_work);
		} else {
			otg->gadget = NULL;
		}

		return 0;
	}
	otg->gadget = gadget;
787
	dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
788 789 790 791 792

	/*
	 * Kick the state machine work, if host is not supported
	 * or host is already registered with us.
	 */
793
	if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
794
		pm_runtime_get_sync(otg->phy->dev);
795
		schedule_work(&motg->sm_work);
796
	}
797 798 799 800

	return 0;
}

801 802
static bool msm_chg_check_secondary_det(struct msm_otg *motg)
{
803
	struct usb_phy *phy = &motg->phy;
804 805 806 807 808
	u32 chg_det;
	bool ret = false;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
809
		chg_det = ulpi_read(phy, 0x34);
810 811 812
		ret = chg_det & (1 << 4);
		break;
	case SNPS_28NM_INTEGRATED_PHY:
813
		chg_det = ulpi_read(phy, 0x87);
814 815 816 817 818 819 820 821 822 823
		ret = chg_det & 1;
		break;
	default:
		break;
	}
	return ret;
}

static void msm_chg_enable_secondary_det(struct msm_otg *motg)
{
824
	struct usb_phy *phy = &motg->phy;
825 826 827 828
	u32 chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
829
		chg_det = ulpi_read(phy, 0x34);
830 831
		/* Turn off charger block */
		chg_det |= ~(1 << 1);
832
		ulpi_write(phy, chg_det, 0x34);
833 834 835
		udelay(20);
		/* control chg block via ULPI */
		chg_det &= ~(1 << 3);
836
		ulpi_write(phy, chg_det, 0x34);
837 838
		/* put it in host mode for enabling D- source */
		chg_det &= ~(1 << 2);
839
		ulpi_write(phy, chg_det, 0x34);
840 841
		/* Turn on chg detect block */
		chg_det &= ~(1 << 1);
842
		ulpi_write(phy, chg_det, 0x34);
843 844 845
		udelay(20);
		/* enable chg detection */
		chg_det &= ~(1 << 0);
846
		ulpi_write(phy, chg_det, 0x34);
847 848 849 850 851 852
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/*
		 * Configure DM as current source, DP as current sink
		 * and enable battery charging comparators.
		 */
853 854 855
		ulpi_write(phy, 0x8, 0x85);
		ulpi_write(phy, 0x2, 0x85);
		ulpi_write(phy, 0x1, 0x85);
856 857 858 859 860 861 862 863
		break;
	default:
		break;
	}
}

static bool msm_chg_check_primary_det(struct msm_otg *motg)
{
864
	struct usb_phy *phy = &motg->phy;
865 866 867 868 869
	u32 chg_det;
	bool ret = false;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
870
		chg_det = ulpi_read(phy, 0x34);
871 872 873
		ret = chg_det & (1 << 4);
		break;
	case SNPS_28NM_INTEGRATED_PHY:
874
		chg_det = ulpi_read(phy, 0x87);
875 876 877 878 879 880 881 882 883 884
		ret = chg_det & 1;
		break;
	default:
		break;
	}
	return ret;
}

static void msm_chg_enable_primary_det(struct msm_otg *motg)
{
885
	struct usb_phy *phy = &motg->phy;
886 887 888 889
	u32 chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
890
		chg_det = ulpi_read(phy, 0x34);
891 892
		/* enable chg detection */
		chg_det &= ~(1 << 0);
893
		ulpi_write(phy, chg_det, 0x34);
894 895 896 897 898 899
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/*
		 * Configure DP as current source, DM as current sink
		 * and enable battery charging comparators.
		 */
900 901
		ulpi_write(phy, 0x2, 0x85);
		ulpi_write(phy, 0x1, 0x85);
902 903 904 905 906 907 908 909
		break;
	default:
		break;
	}
}

static bool msm_chg_check_dcd(struct msm_otg *motg)
{
910
	struct usb_phy *phy = &motg->phy;
911 912 913 914 915
	u32 line_state;
	bool ret = false;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
916
		line_state = ulpi_read(phy, 0x15);
917 918 919
		ret = !(line_state & 1);
		break;
	case SNPS_28NM_INTEGRATED_PHY:
920
		line_state = ulpi_read(phy, 0x87);
921 922 923 924 925 926 927 928 929 930
		ret = line_state & 2;
		break;
	default:
		break;
	}
	return ret;
}

static void msm_chg_disable_dcd(struct msm_otg *motg)
{
931
	struct usb_phy *phy = &motg->phy;
932 933 934 935
	u32 chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
936
		chg_det = ulpi_read(phy, 0x34);
937
		chg_det &= ~(1 << 5);
938
		ulpi_write(phy, chg_det, 0x34);
939 940
		break;
	case SNPS_28NM_INTEGRATED_PHY:
941
		ulpi_write(phy, 0x10, 0x86);
942 943 944 945 946 947 948 949
		break;
	default:
		break;
	}
}

static void msm_chg_enable_dcd(struct msm_otg *motg)
{
950
	struct usb_phy *phy = &motg->phy;
951 952 953 954
	u32 chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
955
		chg_det = ulpi_read(phy, 0x34);
956 957
		/* Turn on D+ current source */
		chg_det |= (1 << 5);
958
		ulpi_write(phy, chg_det, 0x34);
959 960 961
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/* Data contact detection enable */
962
		ulpi_write(phy, 0x10, 0x85);
963 964 965 966 967 968 969 970
		break;
	default:
		break;
	}
}

static void msm_chg_block_on(struct msm_otg *motg)
{
971
	struct usb_phy *phy = &motg->phy;
972 973 974
	u32 func_ctrl, chg_det;

	/* put the controller in non-driving mode */
975
	func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
976 977
	func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
	func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
978
	ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
979 980 981

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
982
		chg_det = ulpi_read(phy, 0x34);
983 984
		/* control chg block via ULPI */
		chg_det &= ~(1 << 3);
985
		ulpi_write(phy, chg_det, 0x34);
986 987
		/* Turn on chg detect block */
		chg_det &= ~(1 << 1);
988
		ulpi_write(phy, chg_det, 0x34);
989 990 991 992
		udelay(20);
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/* Clear charger detecting control bits */
993
		ulpi_write(phy, 0x3F, 0x86);
994
		/* Clear alt interrupt latch and enable bits */
995 996
		ulpi_write(phy, 0x1F, 0x92);
		ulpi_write(phy, 0x1F, 0x95);
997 998 999 1000 1001 1002 1003 1004 1005
		udelay(100);
		break;
	default:
		break;
	}
}

static void msm_chg_block_off(struct msm_otg *motg)
{
1006
	struct usb_phy *phy = &motg->phy;
1007 1008 1009 1010
	u32 func_ctrl, chg_det;

	switch (motg->pdata->phy_type) {
	case CI_45NM_INTEGRATED_PHY:
1011
		chg_det = ulpi_read(phy, 0x34);
1012 1013
		/* Turn off charger block */
		chg_det |= ~(1 << 1);
1014
		ulpi_write(phy, chg_det, 0x34);
1015 1016 1017
		break;
	case SNPS_28NM_INTEGRATED_PHY:
		/* Clear charger detecting control bits */
1018
		ulpi_write(phy, 0x3F, 0x86);
1019
		/* Clear alt interrupt latch and enable bits */
1020 1021
		ulpi_write(phy, 0x1F, 0x92);
		ulpi_write(phy, 0x1F, 0x95);
1022 1023 1024 1025 1026 1027
		break;
	default:
		break;
	}

	/* put the controller in normal mode */
1028
	func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
1029 1030
	func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
	func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1031
	ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
1032 1033 1034 1035 1036 1037 1038 1039 1040
}

#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);
1041
	struct usb_phy *phy = &motg->phy;
1042 1043 1044
	bool is_dcd, tmout, vout;
	unsigned long delay;

1045
	dev_dbg(phy->dev, "chg detection work\n");
1046 1047
	switch (motg->chg_state) {
	case USB_CHG_STATE_UNDEFINED:
1048
		pm_runtime_get_sync(phy->dev);
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
		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);
1091
		dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
1092 1093 1094 1095 1096 1097 1098 1099 1100
		schedule_work(&motg->sm_work);
		return;
	default:
		return;
	}

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

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 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 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) {
	case USB_OTG:
		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) {
			if (pdata->default_mode == USB_HOST) {
				clear_bit(ID, &motg->inputs);
			} else if (pdata->default_mode == USB_PERIPHERAL) {
				set_bit(ID, &motg->inputs);
				set_bit(B_SESS_VLD, &motg->inputs);
			} else {
				set_bit(ID, &motg->inputs);
				clear_bit(B_SESS_VLD, &motg->inputs);
			}
		}
		break;
	case USB_HOST:
		clear_bit(ID, &motg->inputs);
		break;
	case USB_PERIPHERAL:
		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);
1155
	struct usb_otg *otg = motg->phy.otg;
1156

1157
	switch (otg->phy->state) {
1158
	case OTG_STATE_UNDEFINED:
1159 1160
		dev_dbg(otg->phy->dev, "OTG_STATE_UNDEFINED state\n");
		msm_otg_reset(otg->phy);
1161
		msm_otg_init_sm(motg);
1162
		otg->phy->state = OTG_STATE_B_IDLE;
1163 1164
		/* FALL THROUGH */
	case OTG_STATE_B_IDLE:
1165
		dev_dbg(otg->phy->dev, "OTG_STATE_B_IDLE state\n");
1166 1167 1168
		if (!test_bit(ID, &motg->inputs) && otg->host) {
			/* disable BSV bit */
			writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
1169 1170
			msm_otg_start_host(otg->phy, 1);
			otg->phy->state = OTG_STATE_A_HOST;
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
		} 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);
1185 1186 1187
					msm_otg_start_peripheral(otg->phy, 1);
					otg->phy->state
						= OTG_STATE_B_PERIPHERAL;
1188 1189 1190
					break;
				case USB_SDP_CHARGER:
					msm_otg_notify_charger(motg, IUNIT);
1191 1192 1193
					msm_otg_start_peripheral(otg->phy, 1);
					otg->phy->state
						= OTG_STATE_B_PERIPHERAL;
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
					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)) {
1209 1210
				pm_runtime_put_sync(otg->phy->dev);
				msm_otg_reset(otg->phy);
1211 1212 1213 1214
			}
			msm_otg_notify_charger(motg, 0);
			motg->chg_state = USB_CHG_STATE_UNDEFINED;
			motg->chg_type = USB_INVALID_CHARGER;
1215
		}
1216
		pm_runtime_put_sync(otg->phy->dev);
1217 1218
		break;
	case OTG_STATE_B_PERIPHERAL:
1219
		dev_dbg(otg->phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
1220 1221
		if (!test_bit(B_SESS_VLD, &motg->inputs) ||
				!test_bit(ID, &motg->inputs)) {
1222
			msm_otg_notify_charger(motg, 0);
1223
			msm_otg_start_peripheral(otg->phy, 0);
1224 1225
			motg->chg_state = USB_CHG_STATE_UNDEFINED;
			motg->chg_type = USB_INVALID_CHARGER;
1226 1227
			otg->phy->state = OTG_STATE_B_IDLE;
			msm_otg_reset(otg->phy);
1228 1229 1230 1231
			schedule_work(w);
		}
		break;
	case OTG_STATE_A_HOST:
1232
		dev_dbg(otg->phy->dev, "OTG_STATE_A_HOST state\n");
1233
		if (test_bit(ID, &motg->inputs)) {
1234 1235 1236
			msm_otg_start_host(otg->phy, 0);
			otg->phy->state = OTG_STATE_B_IDLE;
			msm_otg_reset(otg->phy);
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
			schedule_work(w);
		}
		break;
	default:
		break;
	}
}

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

1251 1252 1253
	if (atomic_read(&motg->in_lpm)) {
		disable_irq_nosync(irq);
		motg->async_int = 1;
1254
		pm_runtime_get(phy->dev);
1255 1256 1257
		return IRQ_HANDLED;
	}

1258 1259 1260 1261 1262 1263 1264 1265 1266
	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);
1267 1268
		dev_dbg(phy->dev, "ID set/clear\n");
		pm_runtime_get_noresume(phy->dev);
1269 1270 1271 1272 1273
	} 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);
1274 1275
		dev_dbg(phy->dev, "BSV set/clear\n");
		pm_runtime_get_noresume(phy->dev);
1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	}

	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;
1286
	struct usb_otg *otg = motg->phy.otg;
1287

1288
	switch (otg->phy->state) {
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
	case OTG_STATE_A_HOST:
		seq_printf(s, "host\n");
		break;
	case OTG_STATE_B_PERIPHERAL:
		seq_printf(s, "peripheral\n");
		break;
	default:
		seq_printf(s, "none\n");
		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)
{
1311 1312
	struct seq_file *s = file->private_data;
	struct msm_otg *motg = s->private;
1313
	char buf[16];
1314
	struct usb_otg *otg = motg->phy.otg;
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
	int status = count;
	enum usb_mode_type req_mode;

	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)) {
		req_mode = USB_HOST;
	} else if (!strncmp(buf, "peripheral", 10)) {
		req_mode = USB_PERIPHERAL;
	} else if (!strncmp(buf, "none", 4)) {
		req_mode = USB_NONE;
	} else {
		status = -EINVAL;
		goto out;
	}

	switch (req_mode) {
	case USB_NONE:
1338
		switch (otg->phy->state) {
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
		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;
	case USB_PERIPHERAL:
1349
		switch (otg->phy->state) {
1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
		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;
	case USB_HOST:
1360
		switch (otg->phy->state) {
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
		case OTG_STATE_B_IDLE:
		case OTG_STATE_B_PERIPHERAL:
			clear_bit(ID, &motg->inputs);
			break;
		default:
			goto out;
		}
		break;
	default:
		goto out;
	}

1373
	pm_runtime_get_sync(otg->phy->dev);
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	schedule_work(&motg->sm_work);
out:
	return status;
}

const struct file_operations msm_otg_mode_fops = {
	.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);
}

static int __init msm_otg_probe(struct platform_device *pdev)
{
	int ret = 0;
	struct resource *res;
	struct msm_otg *motg;
1419
	struct usb_phy *phy;
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432

	dev_info(&pdev->dev, "msm_otg probe\n");
	if (!pdev->dev.platform_data) {
		dev_err(&pdev->dev, "No platform data given. Bailing out\n");
		return -ENODEV;
	}

	motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
	if (!motg) {
		dev_err(&pdev->dev, "unable to allocate msm_otg\n");
		return -ENOMEM;
	}

1433 1434 1435 1436 1437 1438
	motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
	if (!motg->phy.otg) {
		dev_err(&pdev->dev, "unable to allocate msm_otg\n");
		return -ENOMEM;
	}

1439
	motg->pdata = pdev->dev.platform_data;
1440 1441
	phy = &motg->phy;
	phy->dev = &pdev->dev;
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455

	motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
	if (IS_ERR(motg->phy_reset_clk)) {
		dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
		ret = PTR_ERR(motg->phy_reset_clk);
		goto free_motg;
	}

	motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
	if (IS_ERR(motg->clk)) {
		dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
		ret = PTR_ERR(motg->clk);
		goto put_phy_reset_clk;
	}
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
	clk_set_rate(motg->clk, 60000000);

	/*
	 * 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
	 * CORE CLK. For such USB cores, vote for maximum clk frequency
	 * on pclk source
	 */
	 if (motg->pdata->pclk_src_name) {
		motg->pclk_src = clk_get(&pdev->dev,
			motg->pdata->pclk_src_name);
		if (IS_ERR(motg->pclk_src))
			goto put_clk;
		clk_set_rate(motg->pclk_src, INT_MAX);
		clk_enable(motg->pclk_src);
	} else
		motg->pclk_src = ERR_PTR(-ENOENT);

1475 1476 1477 1478 1479

	motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
	if (IS_ERR(motg->pclk)) {
		dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
		ret = PTR_ERR(motg->pclk);
1480
		goto put_pclk_src;
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
	}

	/*
	 * USB core clock is not present on all MSM chips. This
	 * clock is introduced to remove the dependency on AXI
	 * bus frequency.
	 */
	motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
	if (IS_ERR(motg->core_clk))
		motg->core_clk = NULL;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "failed to get platform resource mem\n");
		ret = -ENODEV;
		goto put_core_clk;
	}

	motg->regs = ioremap(res->start, resource_size(res));
	if (!motg->regs) {
		dev_err(&pdev->dev, "ioremap failed\n");
		ret = -ENOMEM;
		goto put_core_clk;
	}
	dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);

	motg->irq = platform_get_irq(pdev, 0);
	if (!motg->irq) {
		dev_err(&pdev->dev, "platform_get_irq failed\n");
		ret = -ENODEV;
		goto free_regs;
	}

	clk_enable(motg->clk);
	clk_enable(motg->pclk);
1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533

	ret = msm_hsusb_init_vddcx(motg, 1);
	if (ret) {
		dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
		goto free_regs;
	}

	ret = msm_hsusb_ldo_init(motg, 1);
	if (ret) {
		dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
		goto vddcx_exit;
	}
	ret = msm_hsusb_ldo_set_mode(1);
	if (ret) {
		dev_err(&pdev->dev, "hsusb vreg enable failed\n");
		goto ldo_exit;
	}

1534 1535 1536 1537 1538 1539 1540
	if (motg->core_clk)
		clk_enable(motg->core_clk);

	writel(0, USB_USBINTR);
	writel(0, USB_OTGSC);

	INIT_WORK(&motg->sm_work, msm_otg_sm_work);
1541
	INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
1542 1543 1544 1545 1546 1547 1548
	ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
					"msm_otg", motg);
	if (ret) {
		dev_err(&pdev->dev, "request irq failed\n");
		goto disable_clks;
	}

1549 1550 1551 1552
	phy->init = msm_otg_reset;
	phy->set_power = msm_otg_set_power;

	phy->io_ops = &msm_otg_io_ops;
1553

1554 1555 1556
	phy->otg->phy = &motg->phy;
	phy->otg->set_host = msm_otg_set_host;
	phy->otg->set_peripheral = msm_otg_set_peripheral;
1557

1558
	ret = usb_add_phy(&motg->phy, USB_PHY_TYPE_USB2);
1559
	if (ret) {
1560
		dev_err(&pdev->dev, "usb_add_phy failed\n");
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
		goto free_irq;
	}

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

	if (motg->pdata->mode == USB_OTG &&
			motg->pdata->otg_control == OTG_USER_CONTROL) {
		ret = msm_otg_debugfs_init(motg);
		if (ret)
			dev_dbg(&pdev->dev, "mode debugfs file is"
					"not available\n");
	}

1575 1576
	pm_runtime_set_active(&pdev->dev);
	pm_runtime_enable(&pdev->dev);
1577

1578
	return 0;
1579 1580 1581 1582 1583
free_irq:
	free_irq(motg->irq, motg);
disable_clks:
	clk_disable(motg->pclk);
	clk_disable(motg->clk);
1584 1585 1586 1587
ldo_exit:
	msm_hsusb_ldo_init(motg, 0);
vddcx_exit:
	msm_hsusb_init_vddcx(motg, 0);
1588 1589 1590 1591 1592 1593
free_regs:
	iounmap(motg->regs);
put_core_clk:
	if (motg->core_clk)
		clk_put(motg->core_clk);
	clk_put(motg->pclk);
1594 1595 1596 1597 1598
put_pclk_src:
	if (!IS_ERR(motg->pclk_src)) {
		clk_disable(motg->pclk_src);
		clk_put(motg->pclk_src);
	}
1599 1600 1601 1602 1603
put_clk:
	clk_put(motg->clk);
put_phy_reset_clk:
	clk_put(motg->phy_reset_clk);
free_motg:
1604
	kfree(motg->phy.otg);
1605 1606 1607 1608 1609 1610 1611
	kfree(motg);
	return ret;
}

static int __devexit msm_otg_remove(struct platform_device *pdev)
{
	struct msm_otg *motg = platform_get_drvdata(pdev);
1612
	struct usb_phy *phy = &motg->phy;
1613
	int cnt = 0;
1614

1615
	if (phy->otg->host || phy->otg->gadget)
1616 1617 1618
		return -EBUSY;

	msm_otg_debugfs_cleanup();
1619
	cancel_delayed_work_sync(&motg->chg_work);
1620
	cancel_work_sync(&motg->sm_work);
1621

1622
	pm_runtime_resume(&pdev->dev);
1623

1624
	device_init_wakeup(&pdev->dev, 0);
1625
	pm_runtime_disable(&pdev->dev);
1626

1627
	usb_remove_phy(phy);
1628 1629
	free_irq(motg->irq, motg);

1630 1631 1632
	/*
	 * Put PHY in low power mode.
	 */
1633 1634
	ulpi_read(phy, 0x14);
	ulpi_write(phy, 0x08, 0x09);
1635 1636 1637 1638 1639 1640 1641 1642 1643

	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)
1644
		dev_err(phy->dev, "Unable to suspend PHY\n");
1645

1646 1647 1648 1649
	clk_disable(motg->pclk);
	clk_disable(motg->clk);
	if (motg->core_clk)
		clk_disable(motg->core_clk);
1650 1651 1652 1653
	if (!IS_ERR(motg->pclk_src)) {
		clk_disable(motg->pclk_src);
		clk_put(motg->pclk_src);
	}
1654
	msm_hsusb_ldo_init(motg, 0);
1655 1656

	iounmap(motg->regs);
1657
	pm_runtime_set_suspended(&pdev->dev);
1658 1659 1660 1661 1662 1663 1664

	clk_put(motg->phy_reset_clk);
	clk_put(motg->pclk);
	clk_put(motg->clk);
	if (motg->core_clk)
		clk_put(motg->core_clk);

1665
	kfree(motg->phy.otg);
1666 1667 1668 1669 1670
	kfree(motg);

	return 0;
}

1671 1672 1673 1674
#ifdef CONFIG_PM_RUNTIME
static int msm_otg_runtime_idle(struct device *dev)
{
	struct msm_otg *motg = dev_get_drvdata(dev);
1675
	struct usb_otg *otg = motg->phy.otg;
1676 1677 1678 1679 1680 1681 1682 1683 1684

	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.
	 */
1685
	if (otg->phy->state != OTG_STATE_UNDEFINED)
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
		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

1708
#ifdef CONFIG_PM_SLEEP
1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
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

1740
#ifdef CONFIG_PM
1741
static const struct dev_pm_ops msm_otg_dev_pm_ops = {
1742 1743 1744
	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)
1745
};
1746
#endif
1747

1748 1749 1750 1751 1752
static struct platform_driver msm_otg_driver = {
	.remove = __devexit_p(msm_otg_remove),
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
1753
#ifdef CONFIG_PM
1754
		.pm = &msm_otg_dev_pm_ops,
1755
#endif
1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
	},
};

static int __init msm_otg_init(void)
{
	return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
}

static void __exit msm_otg_exit(void)
{
	platform_driver_unregister(&msm_otg_driver);
}

module_init(msm_otg_init);
module_exit(msm_otg_exit);

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