Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   2 * isp1301_omap - ISP 1301 USB transceiver, talking to OMAP OTG controller
   3 *
   4 * Copyright (C) 2004 Texas Instruments
   5 * Copyright (C) 2004 David Brownell
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 2 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20 */
  21
  22#include <linux/kernel.h>
  23#include <linux/module.h>
  24#include <linux/init.h>
  25#include <linux/slab.h>
  26#include <linux/interrupt.h>
  27#include <linux/platform_device.h>
  28#include <linux/gpio.h>
  29#include <linux/usb/ch9.h>
  30#include <linux/usb/gadget.h>
  31#include <linux/usb.h>
  32#include <linux/usb/otg.h>
  33#include <linux/i2c.h>
  34#include <linux/workqueue.h>
  35
  36#include <asm/irq.h>
  37#include <asm/mach-types.h>
  38
  39#include <plat/usb.h>
  40#include <plat/mux.h>
  41
  42
  43#ifndef	DEBUG
  44#undef	VERBOSE
  45#endif
  46
  47
  48#define	DRIVER_VERSION	"24 August 2004"
  49#define	DRIVER_NAME	(isp1301_driver.driver.name)
  50
  51MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
  52MODULE_LICENSE("GPL");
  53
  54struct isp1301 {
  55	struct otg_transceiver	otg;
  56	struct i2c_client	*client;
  57	void			(*i2c_release)(struct device *dev);
  58
  59	int			irq_type;
  60
  61	u32			last_otg_ctrl;
  62	unsigned		working:1;
  63
  64	struct timer_list	timer;
  65
  66	/* use keventd context to change the state for us */
  67	struct work_struct	work;
  68
  69	unsigned long		todo;
  70#		define WORK_UPDATE_ISP	0	/* update ISP from OTG */
  71#		define WORK_UPDATE_OTG	1	/* update OTG from ISP */
  72#		define WORK_HOST_RESUME	4	/* resume host */
  73#		define WORK_TIMER	6	/* timer fired */
  74#		define WORK_STOP	7	/* don't resubmit */
  75};
  76
  77
  78/* bits in OTG_CTRL */
  79
  80#define	OTG_XCEIV_OUTPUTS \
  81	(OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
  82#define	OTG_XCEIV_INPUTS \
  83	(OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
  84#define	OTG_CTRL_BITS \
  85	(OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
  86	/* and OTG_PULLUP is sometimes written */
  87
  88#define	OTG_CTRL_MASK	(OTG_DRIVER_SEL| \
  89	OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
  90	OTG_CTRL_BITS)
  91
  92
  93/*-------------------------------------------------------------------------*/
  94
  95/* board-specific PM hooks */
  96
  97#if defined(CONFIG_MACH_OMAP_H2) || defined(CONFIG_MACH_OMAP_H3)
  98
  99#if	defined(CONFIG_TPS65010) || defined(CONFIG_TPS65010_MODULE)
 100
 101#include <linux/i2c/tps65010.h>
 102
 103#else
 104
 105static inline int tps65010_set_vbus_draw(unsigned mA)
 106{
 107	pr_debug("tps65010: draw %d mA (STUB)\n", mA);
 108	return 0;
 109}
 110
 111#endif
 112
 113static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
 114{
 115	int status = tps65010_set_vbus_draw(mA);
 116	if (status < 0)
 117		pr_debug("  VBUS %d mA error %d\n", mA, status);
 118}
 119
 120#else
 121
 122static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
 123{
 124	/* H4 controls this by DIP switch S2.4; no soft control.
 125	 * ON means the charger is always enabled.  Leave it OFF
 126	 * unless the OTG port is used only in B-peripheral mode.
 127	 */
 128}
 129
 130#endif
 131
 132static void enable_vbus_source(struct isp1301 *isp)
 133{
 134	/* this board won't supply more than 8mA vbus power.
 135	 * some boards can switch a 100ma "unit load" (or more).
 136	 */
 137}
 138
 139
 140/* products will deliver OTG messages with LEDs, GUI, etc */
 141static inline void notresponding(struct isp1301 *isp)
 142{
 143	printk(KERN_NOTICE "OTG device not responding.\n");
 144}
 145
 146
 147/*-------------------------------------------------------------------------*/
 148
 149static struct i2c_driver isp1301_driver;
 150
 151/* smbus apis are used for portability */
 152
 153static inline u8
 154isp1301_get_u8(struct isp1301 *isp, u8 reg)
 155{
 156	return i2c_smbus_read_byte_data(isp->client, reg + 0);
 157}
 158
 159static inline int
 160isp1301_get_u16(struct isp1301 *isp, u8 reg)
 161{
 162	return i2c_smbus_read_word_data(isp->client, reg);
 163}
 164
 165static inline int
 166isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
 167{
 168	return i2c_smbus_write_byte_data(isp->client, reg + 0, bits);
 169}
 170
 171static inline int
 172isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
 173{
 174	return i2c_smbus_write_byte_data(isp->client, reg + 1, bits);
 175}
 176
 177/*-------------------------------------------------------------------------*/
 178
 179/* identification */
 180#define	ISP1301_VENDOR_ID		0x00	/* u16 read */
 181#define	ISP1301_PRODUCT_ID		0x02	/* u16 read */
 182#define	ISP1301_BCD_DEVICE		0x14	/* u16 read */
 183
 184#define	I2C_VENDOR_ID_PHILIPS		0x04cc
 185#define	I2C_PRODUCT_ID_PHILIPS_1301	0x1301
 186
 187/* operational registers */
 188#define	ISP1301_MODE_CONTROL_1		0x04	/* u8 read, set, +1 clear */
 189#	define	MC1_SPEED		(1 << 0)
 190#	define	MC1_SUSPEND		(1 << 1)
 191#	define	MC1_DAT_SE0		(1 << 2)
 192#	define	MC1_TRANSPARENT		(1 << 3)
 193#	define	MC1_BDIS_ACON_EN	(1 << 4)
 194#	define	MC1_OE_INT_EN		(1 << 5)
 195#	define	MC1_UART_EN		(1 << 6)
 196#	define	MC1_MASK		0x7f
 197#define	ISP1301_MODE_CONTROL_2		0x12	/* u8 read, set, +1 clear */
 198#	define	MC2_GLOBAL_PWR_DN	(1 << 0)
 199#	define	MC2_SPD_SUSP_CTRL	(1 << 1)
 200#	define	MC2_BI_DI		(1 << 2)
 201#	define	MC2_TRANSP_BDIR0	(1 << 3)
 202#	define	MC2_TRANSP_BDIR1	(1 << 4)
 203#	define	MC2_AUDIO_EN		(1 << 5)
 204#	define	MC2_PSW_EN		(1 << 6)
 205#	define	MC2_EN2V7		(1 << 7)
 206#define	ISP1301_OTG_CONTROL_1		0x06	/* u8 read, set, +1 clear */
 207#	define	OTG1_DP_PULLUP		(1 << 0)
 208#	define	OTG1_DM_PULLUP		(1 << 1)
 209#	define	OTG1_DP_PULLDOWN	(1 << 2)
 210#	define	OTG1_DM_PULLDOWN	(1 << 3)
 211#	define	OTG1_ID_PULLDOWN	(1 << 4)
 212#	define	OTG1_VBUS_DRV		(1 << 5)
 213#	define	OTG1_VBUS_DISCHRG	(1 << 6)
 214#	define	OTG1_VBUS_CHRG		(1 << 7)
 215#define	ISP1301_OTG_STATUS		0x10	/* u8 readonly */
 216#	define	OTG_B_SESS_END		(1 << 6)
 217#	define	OTG_B_SESS_VLD		(1 << 7)
 218
 219#define	ISP1301_INTERRUPT_SOURCE	0x08	/* u8 read */
 220#define	ISP1301_INTERRUPT_LATCH		0x0A	/* u8 read, set, +1 clear */
 221
 222#define	ISP1301_INTERRUPT_FALLING	0x0C	/* u8 read, set, +1 clear */
 223#define	ISP1301_INTERRUPT_RISING	0x0E	/* u8 read, set, +1 clear */
 224
 225/* same bitfields in all interrupt registers */
 226#	define	INTR_VBUS_VLD		(1 << 0)
 227#	define	INTR_SESS_VLD		(1 << 1)
 228#	define	INTR_DP_HI		(1 << 2)
 229#	define	INTR_ID_GND		(1 << 3)
 230#	define	INTR_DM_HI		(1 << 4)
 231#	define	INTR_ID_FLOAT		(1 << 5)
 232#	define	INTR_BDIS_ACON		(1 << 6)
 233#	define	INTR_CR_INT		(1 << 7)
 234
 235/*-------------------------------------------------------------------------*/
 236
 237static inline const char *state_name(struct isp1301 *isp)
 238{
 239	return otg_state_string(isp->otg.state);
 240}
 241
 242/*-------------------------------------------------------------------------*/
 243
 244/* NOTE:  some of this ISP1301 setup is specific to H2 boards;
 245 * not everything is guarded by board-specific checks, or even using
 246 * omap_usb_config data to deduce MC1_DAT_SE0 and MC2_BI_DI.
 247 *
 248 * ALSO:  this currently doesn't use ISP1301 low-power modes
 249 * while OTG is running.
 250 */
 251
 252static void power_down(struct isp1301 *isp)
 253{
 254	isp->otg.state = OTG_STATE_UNDEFINED;
 255
 256	// isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
 257	isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
 258
 259	isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_ID_PULLDOWN);
 260	isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
 261}
 262
 263static void power_up(struct isp1301 *isp)
 264{
 265	// isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
 266	isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
 267
 268	/* do this only when cpu is driving transceiver,
 269	 * so host won't see a low speed device...
 270	 */
 271	isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
 272}
 273
 274#define	NO_HOST_SUSPEND
 275
 276static int host_suspend(struct isp1301 *isp)
 277{
 278#ifdef	NO_HOST_SUSPEND
 279	return 0;
 280#else
 281	struct device	*dev;
 282
 283	if (!isp->otg.host)
 284		return -ENODEV;
 285
 286	/* Currently ASSUMES only the OTG port matters;
 287	 * other ports could be active...
 288	 */
 289	dev = isp->otg.host->controller;
 290	return dev->driver->suspend(dev, 3, 0);
 291#endif
 292}
 293
 294static int host_resume(struct isp1301 *isp)
 295{
 296#ifdef	NO_HOST_SUSPEND
 297	return 0;
 298#else
 299	struct device	*dev;
 300
 301	if (!isp->otg.host)
 302		return -ENODEV;
 303
 304	dev = isp->otg.host->controller;
 305	return dev->driver->resume(dev, 0);
 306#endif
 307}
 308
 309static int gadget_suspend(struct isp1301 *isp)
 310{
 311	isp->otg.gadget->b_hnp_enable = 0;
 312	isp->otg.gadget->a_hnp_support = 0;
 313	isp->otg.gadget->a_alt_hnp_support = 0;
 314	return usb_gadget_vbus_disconnect(isp->otg.gadget);
 315}
 316
 317/*-------------------------------------------------------------------------*/
 318
 319#define	TIMER_MINUTES	10
 320#define	TIMER_JIFFIES	(TIMER_MINUTES * 60 * HZ)
 321
 322/* Almost all our I2C messaging comes from a work queue's task context.
 323 * NOTE: guaranteeing certain response times might mean we shouldn't
 324 * share keventd's work queue; a realtime task might be safest.
 325 */
 326static void isp1301_defer_work(struct isp1301 *isp, int work)
 327{
 328	int status;
 329
 330	if (isp && !test_and_set_bit(work, &isp->todo)) {
 331		(void) get_device(&isp->client->dev);
 332		status = schedule_work(&isp->work);
 333		if (!status && !isp->working)
 334			dev_vdbg(&isp->client->dev,
 335				"work item %d may be lost\n", work);
 336	}
 337}
 338
 339/* called from irq handlers */
 340static void a_idle(struct isp1301 *isp, const char *tag)
 341{
 342	u32 l;
 343
 344	if (isp->otg.state == OTG_STATE_A_IDLE)
 345		return;
 346
 347	isp->otg.default_a = 1;
 348	if (isp->otg.host) {
 349		isp->otg.host->is_b_host = 0;
 350		host_suspend(isp);
 351	}
 352	if (isp->otg.gadget) {
 353		isp->otg.gadget->is_a_peripheral = 1;
 354		gadget_suspend(isp);
 355	}
 356	isp->otg.state = OTG_STATE_A_IDLE;
 357	l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
 358	omap_writel(l, OTG_CTRL);
 359	isp->last_otg_ctrl = l;
 360	pr_debug("  --> %s/%s\n", state_name(isp), tag);
 361}
 362
 363/* called from irq handlers */
 364static void b_idle(struct isp1301 *isp, const char *tag)
 365{
 366	u32 l;
 367
 368	if (isp->otg.state == OTG_STATE_B_IDLE)
 369		return;
 370
 371	isp->otg.default_a = 0;
 372	if (isp->otg.host) {
 373		isp->otg.host->is_b_host = 1;
 374		host_suspend(isp);
 375	}
 376	if (isp->otg.gadget) {
 377		isp->otg.gadget->is_a_peripheral = 0;
 378		gadget_suspend(isp);
 379	}
 380	isp->otg.state = OTG_STATE_B_IDLE;
 381	l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
 382	omap_writel(l, OTG_CTRL);
 383	isp->last_otg_ctrl = l;
 384	pr_debug("  --> %s/%s\n", state_name(isp), tag);
 385}
 386
 387static void
 388dump_regs(struct isp1301 *isp, const char *label)
 389{
 390#ifdef	DEBUG
 391	u8	ctrl = isp1301_get_u8(isp, ISP1301_OTG_CONTROL_1);
 392	u8	status = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
 393	u8	src = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
 394
 395	pr_debug("otg: %06x, %s %s, otg/%02x stat/%02x.%02x\n",
 396		omap_readl(OTG_CTRL), label, state_name(isp),
 397		ctrl, status, src);
 398	/* mode control and irq enables don't change much */
 399#endif
 400}
 401
 402/*-------------------------------------------------------------------------*/
 403
 404#ifdef	CONFIG_USB_OTG
 405
 406/*
 407 * The OMAP OTG controller handles most of the OTG state transitions.
 408 *
 409 * We translate isp1301 outputs (mostly voltage comparator status) into
 410 * OTG inputs; OTG outputs (mostly pullup/pulldown controls) and HNP state
 411 * flags into isp1301 inputs ... and infer state transitions.
 412 */
 413
 414#ifdef	VERBOSE
 415
 416static void check_state(struct isp1301 *isp, const char *tag)
 417{
 418	enum usb_otg_state	state = OTG_STATE_UNDEFINED;
 419	u8			fsm = omap_readw(OTG_TEST) & 0x0ff;
 420	unsigned		extra = 0;
 421
 422	switch (fsm) {
 423
 424	/* default-b */
 425	case 0x0:
 426		state = OTG_STATE_B_IDLE;
 427		break;
 428	case 0x3:
 429	case 0x7:
 430		extra = 1;
 431	case 0x1:
 432		state = OTG_STATE_B_PERIPHERAL;
 433		break;
 434	case 0x11:
 435		state = OTG_STATE_B_SRP_INIT;
 436		break;
 437
 438	/* extra dual-role default-b states */
 439	case 0x12:
 440	case 0x13:
 441	case 0x16:
 442		extra = 1;
 443	case 0x17:
 444		state = OTG_STATE_B_WAIT_ACON;
 445		break;
 446	case 0x34:
 447		state = OTG_STATE_B_HOST;
 448		break;
 449
 450	/* default-a */
 451	case 0x36:
 452		state = OTG_STATE_A_IDLE;
 453		break;
 454	case 0x3c:
 455		state = OTG_STATE_A_WAIT_VFALL;
 456		break;
 457	case 0x7d:
 458		state = OTG_STATE_A_VBUS_ERR;
 459		break;
 460	case 0x9e:
 461	case 0x9f:
 462		extra = 1;
 463	case 0x89:
 464		state = OTG_STATE_A_PERIPHERAL;
 465		break;
 466	case 0xb7:
 467		state = OTG_STATE_A_WAIT_VRISE;
 468		break;
 469	case 0xb8:
 470		state = OTG_STATE_A_WAIT_BCON;
 471		break;
 472	case 0xb9:
 473		state = OTG_STATE_A_HOST;
 474		break;
 475	case 0xba:
 476		state = OTG_STATE_A_SUSPEND;
 477		break;
 478	default:
 479		break;
 480	}
 481	if (isp->otg.state == state && !extra)
 482		return;
 483	pr_debug("otg: %s FSM %s/%02x, %s, %06x\n", tag,
 484		otg_state_string(state), fsm, state_name(isp),
 485		omap_readl(OTG_CTRL));
 486}
 487
 488#else
 489
 490static inline void check_state(struct isp1301 *isp, const char *tag) { }
 491
 492#endif
 493
 494/* outputs from ISP1301_INTERRUPT_SOURCE */
 495static void update_otg1(struct isp1301 *isp, u8 int_src)
 496{
 497	u32	otg_ctrl;
 498
 499	otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
 500	otg_ctrl &= ~OTG_XCEIV_INPUTS;
 501	otg_ctrl &= ~(OTG_ID|OTG_ASESSVLD|OTG_VBUSVLD);
 502
 503	if (int_src & INTR_SESS_VLD)
 504		otg_ctrl |= OTG_ASESSVLD;
 505	else if (isp->otg.state == OTG_STATE_A_WAIT_VFALL) {
 506		a_idle(isp, "vfall");
 507		otg_ctrl &= ~OTG_CTRL_BITS;
 508	}
 509	if (int_src & INTR_VBUS_VLD)
 510		otg_ctrl |= OTG_VBUSVLD;
 511	if (int_src & INTR_ID_GND) {		/* default-A */
 512		if (isp->otg.state == OTG_STATE_B_IDLE
 513				|| isp->otg.state == OTG_STATE_UNDEFINED) {
 
 514			a_idle(isp, "init");
 515			return;
 516		}
 517	} else {				/* default-B */
 518		otg_ctrl |= OTG_ID;
 519		if (isp->otg.state == OTG_STATE_A_IDLE
 520				|| isp->otg.state == OTG_STATE_UNDEFINED) {
 521			b_idle(isp, "init");
 522			return;
 523		}
 524	}
 525	omap_writel(otg_ctrl, OTG_CTRL);
 526}
 527
 528/* outputs from ISP1301_OTG_STATUS */
 529static void update_otg2(struct isp1301 *isp, u8 otg_status)
 530{
 531	u32	otg_ctrl;
 532
 533	otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
 534	otg_ctrl &= ~OTG_XCEIV_INPUTS;
 535	otg_ctrl &= ~(OTG_BSESSVLD | OTG_BSESSEND);
 536	if (otg_status & OTG_B_SESS_VLD)
 537		otg_ctrl |= OTG_BSESSVLD;
 538	else if (otg_status & OTG_B_SESS_END)
 539		otg_ctrl |= OTG_BSESSEND;
 540	omap_writel(otg_ctrl, OTG_CTRL);
 541}
 542
 543/* inputs going to ISP1301 */
 544static void otg_update_isp(struct isp1301 *isp)
 545{
 546	u32	otg_ctrl, otg_change;
 547	u8	set = OTG1_DM_PULLDOWN, clr = OTG1_DM_PULLUP;
 548
 549	otg_ctrl = omap_readl(OTG_CTRL);
 550	otg_change = otg_ctrl ^ isp->last_otg_ctrl;
 551	isp->last_otg_ctrl = otg_ctrl;
 552	otg_ctrl = otg_ctrl & OTG_XCEIV_INPUTS;
 553
 554	switch (isp->otg.state) {
 555	case OTG_STATE_B_IDLE:
 556	case OTG_STATE_B_PERIPHERAL:
 557	case OTG_STATE_B_SRP_INIT:
 558		if (!(otg_ctrl & OTG_PULLUP)) {
 559			// if (otg_ctrl & OTG_B_HNPEN) {
 560			if (isp->otg.gadget->b_hnp_enable) {
 561				isp->otg.state = OTG_STATE_B_WAIT_ACON;
 562				pr_debug("  --> b_wait_acon\n");
 563			}
 564			goto pulldown;
 565		}
 566pullup:
 567		set |= OTG1_DP_PULLUP;
 568		clr |= OTG1_DP_PULLDOWN;
 569		break;
 570	case OTG_STATE_A_SUSPEND:
 571	case OTG_STATE_A_PERIPHERAL:
 572		if (otg_ctrl & OTG_PULLUP)
 573			goto pullup;
 574		/* FALLTHROUGH */
 575	// case OTG_STATE_B_WAIT_ACON:
 576	default:
 577pulldown:
 578		set |= OTG1_DP_PULLDOWN;
 579		clr |= OTG1_DP_PULLUP;
 580		break;
 581	}
 582
 583#	define toggle(OTG,ISP) do { \
 584		if (otg_ctrl & OTG) set |= ISP; \
 585		else clr |= ISP; \
 586		} while (0)
 587
 588	if (!(isp->otg.host))
 589		otg_ctrl &= ~OTG_DRV_VBUS;
 590
 591	switch (isp->otg.state) {
 592	case OTG_STATE_A_SUSPEND:
 593		if (otg_ctrl & OTG_DRV_VBUS) {
 594			set |= OTG1_VBUS_DRV;
 595			break;
 596		}
 597		/* HNP failed for some reason (A_AIDL_BDIS timeout) */
 598		notresponding(isp);
 599
 600		/* FALLTHROUGH */
 601	case OTG_STATE_A_VBUS_ERR:
 602		isp->otg.state = OTG_STATE_A_WAIT_VFALL;
 603		pr_debug("  --> a_wait_vfall\n");
 604		/* FALLTHROUGH */
 605	case OTG_STATE_A_WAIT_VFALL:
 606		/* FIXME usbcore thinks port power is still on ... */
 607		clr |= OTG1_VBUS_DRV;
 608		break;
 609	case OTG_STATE_A_IDLE:
 610		if (otg_ctrl & OTG_DRV_VBUS) {
 611			isp->otg.state = OTG_STATE_A_WAIT_VRISE;
 612			pr_debug("  --> a_wait_vrise\n");
 613		}
 614		/* FALLTHROUGH */
 615	default:
 616		toggle(OTG_DRV_VBUS, OTG1_VBUS_DRV);
 617	}
 618
 619	toggle(OTG_PU_VBUS, OTG1_VBUS_CHRG);
 620	toggle(OTG_PD_VBUS, OTG1_VBUS_DISCHRG);
 621
 622#	undef toggle
 623
 624	isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, set);
 625	isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, clr);
 626
 627	/* HNP switch to host or peripheral; and SRP */
 628	if (otg_change & OTG_PULLUP) {
 629		u32 l;
 630
 631		switch (isp->otg.state) {
 632		case OTG_STATE_B_IDLE:
 633			if (clr & OTG1_DP_PULLUP)
 634				break;
 635			isp->otg.state = OTG_STATE_B_PERIPHERAL;
 636			pr_debug("  --> b_peripheral\n");
 637			break;
 638		case OTG_STATE_A_SUSPEND:
 639			if (clr & OTG1_DP_PULLUP)
 640				break;
 641			isp->otg.state = OTG_STATE_A_PERIPHERAL;
 642			pr_debug("  --> a_peripheral\n");
 643			break;
 644		default:
 645			break;
 646		}
 647		l = omap_readl(OTG_CTRL);
 648		l |= OTG_PULLUP;
 649		omap_writel(l, OTG_CTRL);
 650	}
 651
 652	check_state(isp, __func__);
 653	dump_regs(isp, "otg->isp1301");
 654}
 655
 656static irqreturn_t omap_otg_irq(int irq, void *_isp)
 657{
 658	u16		otg_irq = omap_readw(OTG_IRQ_SRC);
 659	u32		otg_ctrl;
 660	int		ret = IRQ_NONE;
 661	struct isp1301	*isp = _isp;
 
 662
 663	/* update ISP1301 transceiver from OTG controller */
 664	if (otg_irq & OPRT_CHG) {
 665		omap_writew(OPRT_CHG, OTG_IRQ_SRC);
 666		isp1301_defer_work(isp, WORK_UPDATE_ISP);
 667		ret = IRQ_HANDLED;
 668
 669	/* SRP to become b_peripheral failed */
 670	} else if (otg_irq & B_SRP_TMROUT) {
 671		pr_debug("otg: B_SRP_TIMEOUT, %06x\n", omap_readl(OTG_CTRL));
 672		notresponding(isp);
 673
 674		/* gadget drivers that care should monitor all kinds of
 675		 * remote wakeup (SRP, normal) using their own timer
 676		 * to give "check cable and A-device" messages.
 677		 */
 678		if (isp->otg.state == OTG_STATE_B_SRP_INIT)
 679			b_idle(isp, "srp_timeout");
 680
 681		omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
 682		ret = IRQ_HANDLED;
 683
 684	/* HNP to become b_host failed */
 685	} else if (otg_irq & B_HNP_FAIL) {
 686		pr_debug("otg: %s B_HNP_FAIL, %06x\n",
 687				state_name(isp), omap_readl(OTG_CTRL));
 688		notresponding(isp);
 689
 690		otg_ctrl = omap_readl(OTG_CTRL);
 691		otg_ctrl |= OTG_BUSDROP;
 692		otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
 693		omap_writel(otg_ctrl, OTG_CTRL);
 694
 695		/* subset of b_peripheral()... */
 696		isp->otg.state = OTG_STATE_B_PERIPHERAL;
 697		pr_debug("  --> b_peripheral\n");
 698
 699		omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
 700		ret = IRQ_HANDLED;
 701
 702	/* detect SRP from B-device ... */
 703	} else if (otg_irq & A_SRP_DETECT) {
 704		pr_debug("otg: %s SRP_DETECT, %06x\n",
 705				state_name(isp), omap_readl(OTG_CTRL));
 706
 707		isp1301_defer_work(isp, WORK_UPDATE_OTG);
 708		switch (isp->otg.state) {
 709		case OTG_STATE_A_IDLE:
 710			if (!isp->otg.host)
 711				break;
 712			isp1301_defer_work(isp, WORK_HOST_RESUME);
 713			otg_ctrl = omap_readl(OTG_CTRL);
 714			otg_ctrl |= OTG_A_BUSREQ;
 715			otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
 716					& ~OTG_XCEIV_INPUTS
 717					& OTG_CTRL_MASK;
 718			omap_writel(otg_ctrl, OTG_CTRL);
 719			break;
 720		default:
 721			break;
 722		}
 723
 724		omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
 725		ret = IRQ_HANDLED;
 726
 727	/* timer expired:  T(a_wait_bcon) and maybe T(a_wait_vrise)
 728	 * we don't track them separately
 729	 */
 730	} else if (otg_irq & A_REQ_TMROUT) {
 731		otg_ctrl = omap_readl(OTG_CTRL);
 732		pr_info("otg: BCON_TMOUT from %s, %06x\n",
 733				state_name(isp), otg_ctrl);
 734		notresponding(isp);
 735
 736		otg_ctrl |= OTG_BUSDROP;
 737		otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
 738		omap_writel(otg_ctrl, OTG_CTRL);
 739		isp->otg.state = OTG_STATE_A_WAIT_VFALL;
 740
 741		omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
 742		ret = IRQ_HANDLED;
 743
 744	/* A-supplied voltage fell too low; overcurrent */
 745	} else if (otg_irq & A_VBUS_ERR) {
 746		otg_ctrl = omap_readl(OTG_CTRL);
 747		printk(KERN_ERR "otg: %s, VBUS_ERR %04x ctrl %06x\n",
 748			state_name(isp), otg_irq, otg_ctrl);
 749
 750		otg_ctrl |= OTG_BUSDROP;
 751		otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
 752		omap_writel(otg_ctrl, OTG_CTRL);
 753		isp->otg.state = OTG_STATE_A_VBUS_ERR;
 754
 755		omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
 756		ret = IRQ_HANDLED;
 757
 758	/* switch driver; the transceiver code activates it,
 759	 * ungating the udc clock or resuming OHCI.
 760	 */
 761	} else if (otg_irq & DRIVER_SWITCH) {
 762		int	kick = 0;
 763
 764		otg_ctrl = omap_readl(OTG_CTRL);
 765		printk(KERN_NOTICE "otg: %s, SWITCH to %s, ctrl %06x\n",
 766				state_name(isp),
 767				(otg_ctrl & OTG_DRIVER_SEL)
 768					? "gadget" : "host",
 769				otg_ctrl);
 770		isp1301_defer_work(isp, WORK_UPDATE_ISP);
 771
 772		/* role is peripheral */
 773		if (otg_ctrl & OTG_DRIVER_SEL) {
 774			switch (isp->otg.state) {
 775			case OTG_STATE_A_IDLE:
 776				b_idle(isp, __func__);
 777				break;
 778			default:
 779				break;
 780			}
 781			isp1301_defer_work(isp, WORK_UPDATE_ISP);
 782
 783		/* role is host */
 784		} else {
 785			if (!(otg_ctrl & OTG_ID)) {
 786				otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
 787				omap_writel(otg_ctrl | OTG_A_BUSREQ, OTG_CTRL);
 788			}
 789
 790			if (isp->otg.host) {
 791				switch (isp->otg.state) {
 792				case OTG_STATE_B_WAIT_ACON:
 793					isp->otg.state = OTG_STATE_B_HOST;
 794					pr_debug("  --> b_host\n");
 795					kick = 1;
 796					break;
 797				case OTG_STATE_A_WAIT_BCON:
 798					isp->otg.state = OTG_STATE_A_HOST;
 799					pr_debug("  --> a_host\n");
 800					break;
 801				case OTG_STATE_A_PERIPHERAL:
 802					isp->otg.state = OTG_STATE_A_WAIT_BCON;
 803					pr_debug("  --> a_wait_bcon\n");
 804					break;
 805				default:
 806					break;
 807				}
 808				isp1301_defer_work(isp, WORK_HOST_RESUME);
 809			}
 810		}
 811
 812		omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
 813		ret = IRQ_HANDLED;
 814
 815		if (kick)
 816			usb_bus_start_enum(isp->otg.host,
 817						isp->otg.host->otg_port);
 818	}
 819
 820	check_state(isp, __func__);
 821	return ret;
 822}
 823
 824static struct platform_device *otg_dev;
 825
 826static int isp1301_otg_init(struct isp1301 *isp)
 827{
 828	u32 l;
 829
 830	if (!otg_dev)
 831		return -ENODEV;
 832
 833	dump_regs(isp, __func__);
 834	/* some of these values are board-specific... */
 835	l = omap_readl(OTG_SYSCON_2);
 836	l |= OTG_EN
 837		/* for B-device: */
 838		| SRP_GPDATA		/* 9msec Bdev D+ pulse */
 839		| SRP_GPDVBUS		/* discharge after VBUS pulse */
 840		// | (3 << 24)		/* 2msec VBUS pulse */
 841		/* for A-device: */
 842		| (0 << 20)		/* 200ms nominal A_WAIT_VRISE timer */
 843		| SRP_DPW		/* detect 167+ns SRP pulses */
 844		| SRP_DATA | SRP_VBUS	/* accept both kinds of SRP pulse */
 845		;
 846	omap_writel(l, OTG_SYSCON_2);
 847
 848	update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
 849	update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
 850
 851	check_state(isp, __func__);
 852	pr_debug("otg: %s, %s %06x\n",
 853			state_name(isp), __func__, omap_readl(OTG_CTRL));
 854
 855	omap_writew(DRIVER_SWITCH | OPRT_CHG
 856			| B_SRP_TMROUT | B_HNP_FAIL
 857			| A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT, OTG_IRQ_EN);
 858
 859	l = omap_readl(OTG_SYSCON_2);
 860	l |= OTG_EN;
 861	omap_writel(l, OTG_SYSCON_2);
 862
 863	return 0;
 864}
 865
 866static int otg_probe(struct platform_device *dev)
 867{
 868	// struct omap_usb_config *config = dev->platform_data;
 869
 870	otg_dev = dev;
 871	return 0;
 872}
 873
 874static int otg_remove(struct platform_device *dev)
 875{
 876	otg_dev = NULL;
 877	return 0;
 878}
 879
 880static struct platform_driver omap_otg_driver = {
 881	.probe		= otg_probe,
 882	.remove		= otg_remove,
 883	.driver		= {
 884		.owner	= THIS_MODULE,
 885		.name	= "omap_otg",
 886	},
 887};
 888
 889static int otg_bind(struct isp1301 *isp)
 890{
 891	int	status;
 892
 893	if (otg_dev)
 894		return -EBUSY;
 895
 896	status = platform_driver_register(&omap_otg_driver);
 897	if (status < 0)
 898		return status;
 899
 900	if (otg_dev)
 901		status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
 902				IRQF_DISABLED, DRIVER_NAME, isp);
 903	else
 904		status = -ENODEV;
 905
 906	if (status < 0)
 907		platform_driver_unregister(&omap_otg_driver);
 908	return status;
 909}
 910
 911static void otg_unbind(struct isp1301 *isp)
 912{
 913	if (!otg_dev)
 914		return;
 915	free_irq(otg_dev->resource[1].start, isp);
 916}
 917
 918#else
 919
 920/* OTG controller isn't clocked */
 921
 922#endif	/* CONFIG_USB_OTG */
 923
 924/*-------------------------------------------------------------------------*/
 925
 926static void b_peripheral(struct isp1301 *isp)
 927{
 928	u32 l;
 929
 930	l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
 931	omap_writel(l, OTG_CTRL);
 932
 933	usb_gadget_vbus_connect(isp->otg.gadget);
 934
 935#ifdef	CONFIG_USB_OTG
 936	enable_vbus_draw(isp, 8);
 937	otg_update_isp(isp);
 938#else
 939	enable_vbus_draw(isp, 100);
 940	/* UDC driver just set OTG_BSESSVLD */
 941	isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
 942	isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
 943	isp->otg.state = OTG_STATE_B_PERIPHERAL;
 944	pr_debug("  --> b_peripheral\n");
 945	dump_regs(isp, "2periph");
 946#endif
 947}
 948
 949static void isp_update_otg(struct isp1301 *isp, u8 stat)
 950{
 
 951	u8			isp_stat, isp_bstat;
 952	enum usb_otg_state	state = isp->otg.state;
 953
 954	if (stat & INTR_BDIS_ACON)
 955		pr_debug("OTG:  BDIS_ACON, %s\n", state_name(isp));
 956
 957	/* start certain state transitions right away */
 958	isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
 959	if (isp_stat & INTR_ID_GND) {
 960		if (isp->otg.default_a) {
 961			switch (state) {
 962			case OTG_STATE_B_IDLE:
 963				a_idle(isp, "idle");
 964				/* FALLTHROUGH */
 965			case OTG_STATE_A_IDLE:
 966				enable_vbus_source(isp);
 967				/* FALLTHROUGH */
 968			case OTG_STATE_A_WAIT_VRISE:
 969				/* we skip over OTG_STATE_A_WAIT_BCON, since
 970				 * the HC will transition to A_HOST (or
 971				 * A_SUSPEND!) without our noticing except
 972				 * when HNP is used.
 973				 */
 974				if (isp_stat & INTR_VBUS_VLD)
 975					isp->otg.state = OTG_STATE_A_HOST;
 976				break;
 977			case OTG_STATE_A_WAIT_VFALL:
 978				if (!(isp_stat & INTR_SESS_VLD))
 979					a_idle(isp, "vfell");
 980				break;
 981			default:
 982				if (!(isp_stat & INTR_VBUS_VLD))
 983					isp->otg.state = OTG_STATE_A_VBUS_ERR;
 984				break;
 985			}
 986			isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
 987		} else {
 988			switch (state) {
 989			case OTG_STATE_B_PERIPHERAL:
 990			case OTG_STATE_B_HOST:
 991			case OTG_STATE_B_WAIT_ACON:
 992				usb_gadget_vbus_disconnect(isp->otg.gadget);
 993				break;
 994			default:
 995				break;
 996			}
 997			if (state != OTG_STATE_A_IDLE)
 998				a_idle(isp, "id");
 999			if (isp->otg.host && state == OTG_STATE_A_IDLE)
1000				isp1301_defer_work(isp, WORK_HOST_RESUME);
1001			isp_bstat = 0;
1002		}
1003	} else {
1004		u32 l;
1005
1006		/* if user unplugged mini-A end of cable,
1007		 * don't bypass A_WAIT_VFALL.
1008		 */
1009		if (isp->otg.default_a) {
1010			switch (state) {
1011			default:
1012				isp->otg.state = OTG_STATE_A_WAIT_VFALL;
1013				break;
1014			case OTG_STATE_A_WAIT_VFALL:
1015				state = OTG_STATE_A_IDLE;
1016				/* khubd may take a while to notice and
1017				 * handle this disconnect, so don't go
1018				 * to B_IDLE quite yet.
1019				 */
1020				break;
1021			case OTG_STATE_A_IDLE:
1022				host_suspend(isp);
1023				isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
1024						MC1_BDIS_ACON_EN);
1025				isp->otg.state = OTG_STATE_B_IDLE;
1026				l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1027				l &= ~OTG_CTRL_BITS;
1028				omap_writel(l, OTG_CTRL);
1029				break;
1030			case OTG_STATE_B_IDLE:
1031				break;
1032			}
1033		}
1034		isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
1035
1036		switch (isp->otg.state) {
1037		case OTG_STATE_B_PERIPHERAL:
1038		case OTG_STATE_B_WAIT_ACON:
1039		case OTG_STATE_B_HOST:
1040			if (likely(isp_bstat & OTG_B_SESS_VLD))
1041				break;
1042			enable_vbus_draw(isp, 0);
1043#ifndef	CONFIG_USB_OTG
1044			/* UDC driver will clear OTG_BSESSVLD */
1045			isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1046						OTG1_DP_PULLDOWN);
1047			isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1048						OTG1_DP_PULLUP);
1049			dump_regs(isp, __func__);
1050#endif
1051			/* FALLTHROUGH */
1052		case OTG_STATE_B_SRP_INIT:
1053			b_idle(isp, __func__);
1054			l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
1055			omap_writel(l, OTG_CTRL);
1056			/* FALLTHROUGH */
1057		case OTG_STATE_B_IDLE:
1058			if (isp->otg.gadget && (isp_bstat & OTG_B_SESS_VLD)) {
1059#ifdef	CONFIG_USB_OTG
1060				update_otg1(isp, isp_stat);
1061				update_otg2(isp, isp_bstat);
1062#endif
1063				b_peripheral(isp);
1064			} else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
1065				isp_bstat |= OTG_B_SESS_END;
1066			break;
1067		case OTG_STATE_A_WAIT_VFALL:
1068			break;
1069		default:
1070			pr_debug("otg: unsupported b-device %s\n",
1071				state_name(isp));
1072			break;
1073		}
1074	}
1075
1076	if (state != isp->otg.state)
1077		pr_debug("  isp, %s -> %s\n",
1078				otg_state_string(state), state_name(isp));
1079
1080#ifdef	CONFIG_USB_OTG
1081	/* update the OTG controller state to match the isp1301; may
1082	 * trigger OPRT_CHG irqs for changes going to the isp1301.
1083	 */
1084	update_otg1(isp, isp_stat);
1085	update_otg2(isp, isp_bstat);
1086	check_state(isp, __func__);
1087#endif
1088
1089	dump_regs(isp, "isp1301->otg");
1090}
1091
1092/*-------------------------------------------------------------------------*/
1093
1094static u8 isp1301_clear_latch(struct isp1301 *isp)
1095{
1096	u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
1097	isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
1098	return latch;
1099}
1100
1101static void
1102isp1301_work(struct work_struct *work)
1103{
1104	struct isp1301	*isp = container_of(work, struct isp1301, work);
1105	int		stop;
1106
1107	/* implicit lock:  we're the only task using this device */
1108	isp->working = 1;
1109	do {
1110		stop = test_bit(WORK_STOP, &isp->todo);
1111
1112#ifdef	CONFIG_USB_OTG
1113		/* transfer state from otg engine to isp1301 */
1114		if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
1115			otg_update_isp(isp);
1116			put_device(&isp->client->dev);
1117		}
1118#endif
1119		/* transfer state from isp1301 to otg engine */
1120		if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
1121			u8		stat = isp1301_clear_latch(isp);
1122
1123			isp_update_otg(isp, stat);
1124			put_device(&isp->client->dev);
1125		}
1126
1127		if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
1128			u32	otg_ctrl;
1129
1130			/*
1131			 * skip A_WAIT_VRISE; hc transitions invisibly
1132			 * skip A_WAIT_BCON; same.
1133			 */
1134			switch (isp->otg.state) {
1135			case OTG_STATE_A_WAIT_BCON:
1136			case OTG_STATE_A_WAIT_VRISE:
1137				isp->otg.state = OTG_STATE_A_HOST;
1138				pr_debug("  --> a_host\n");
1139				otg_ctrl = omap_readl(OTG_CTRL);
1140				otg_ctrl |= OTG_A_BUSREQ;
1141				otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
1142						& OTG_CTRL_MASK;
1143				omap_writel(otg_ctrl, OTG_CTRL);
1144				break;
1145			case OTG_STATE_B_WAIT_ACON:
1146				isp->otg.state = OTG_STATE_B_HOST;
1147				pr_debug("  --> b_host (acon)\n");
1148				break;
1149			case OTG_STATE_B_HOST:
1150			case OTG_STATE_B_IDLE:
1151			case OTG_STATE_A_IDLE:
1152				break;
1153			default:
1154				pr_debug("  host resume in %s\n",
1155						state_name(isp));
1156			}
1157			host_resume(isp);
1158			// mdelay(10);
1159			put_device(&isp->client->dev);
1160		}
1161
1162		if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
1163#ifdef	VERBOSE
1164			dump_regs(isp, "timer");
1165			if (!stop)
1166				mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1167#endif
1168			put_device(&isp->client->dev);
1169		}
1170
1171		if (isp->todo)
1172			dev_vdbg(&isp->client->dev,
1173				"work done, todo = 0x%lx\n",
1174				isp->todo);
1175		if (stop) {
1176			dev_dbg(&isp->client->dev, "stop\n");
1177			break;
1178		}
1179	} while (isp->todo);
1180	isp->working = 0;
1181}
1182
1183static irqreturn_t isp1301_irq(int irq, void *isp)
1184{
1185	isp1301_defer_work(isp, WORK_UPDATE_OTG);
1186	return IRQ_HANDLED;
1187}
1188
1189static void isp1301_timer(unsigned long _isp)
1190{
1191	isp1301_defer_work((void *)_isp, WORK_TIMER);
1192}
1193
1194/*-------------------------------------------------------------------------*/
1195
1196static void isp1301_release(struct device *dev)
1197{
1198	struct isp1301	*isp;
1199
1200	isp = dev_get_drvdata(dev);
1201
1202	/* FIXME -- not with a "new style" driver, it doesn't!! */
1203
1204	/* ugly -- i2c hijacks our memory hook to wait_for_completion() */
1205	if (isp->i2c_release)
1206		isp->i2c_release(dev);
 
1207	kfree (isp);
1208}
1209
1210static struct isp1301 *the_transceiver;
1211
1212static int __exit isp1301_remove(struct i2c_client *i2c)
1213{
1214	struct isp1301	*isp;
1215
1216	isp = i2c_get_clientdata(i2c);
1217
1218	isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1219	isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1220	free_irq(i2c->irq, isp);
1221#ifdef	CONFIG_USB_OTG
1222	otg_unbind(isp);
1223#endif
1224	if (machine_is_omap_h2())
1225		gpio_free(2);
1226
1227	isp->timer.data = 0;
1228	set_bit(WORK_STOP, &isp->todo);
1229	del_timer_sync(&isp->timer);
1230	flush_work_sync(&isp->work);
1231
1232	put_device(&i2c->dev);
1233	the_transceiver = NULL;
1234
1235	return 0;
1236}
1237
1238/*-------------------------------------------------------------------------*/
1239
1240/* NOTE:  three modes are possible here, only one of which
1241 * will be standards-conformant on any given system:
1242 *
1243 *  - OTG mode (dual-role), required if there's a Mini-AB connector
1244 *  - HOST mode, for when there's one or more A (host) connectors
1245 *  - DEVICE mode, for when there's a B/Mini-B (device) connector
1246 *
1247 * As a rule, you won't have an isp1301 chip unless it's there to
1248 * support the OTG mode.  Other modes help testing USB controllers
1249 * in isolation from (full) OTG support, or maybe so later board
1250 * revisions can help to support those feature.
1251 */
1252
1253#ifdef	CONFIG_USB_OTG
1254
1255static int isp1301_otg_enable(struct isp1301 *isp)
1256{
1257	power_up(isp);
1258	isp1301_otg_init(isp);
1259
1260	/* NOTE:  since we don't change this, this provides
1261	 * a few more interrupts than are strictly needed.
1262	 */
1263	isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1264		INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1265	isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1266		INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1267
1268	dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
1269
1270	return 0;
1271}
1272
1273#endif
1274
1275/* add or disable the host device+driver */
1276static int
1277isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1278{
1279	struct isp1301	*isp = container_of(otg, struct isp1301, otg);
1280
1281	if (!otg || isp != the_transceiver)
1282		return -ENODEV;
1283
1284	if (!host) {
1285		omap_writew(0, OTG_IRQ_EN);
1286		power_down(isp);
1287		isp->otg.host = NULL;
1288		return 0;
1289	}
1290
1291#ifdef	CONFIG_USB_OTG
1292	isp->otg.host = host;
1293	dev_dbg(&isp->client->dev, "registered host\n");
1294	host_suspend(isp);
1295	if (isp->otg.gadget)
1296		return isp1301_otg_enable(isp);
1297	return 0;
1298
1299#elif	!defined(CONFIG_USB_GADGET_OMAP)
1300	// FIXME update its refcount
1301	isp->otg.host = host;
1302
1303	power_up(isp);
1304
1305	if (machine_is_omap_h2())
1306		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1307
1308	dev_info(&isp->client->dev, "A-Host sessions ok\n");
1309	isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1310		INTR_ID_GND);
1311	isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1312		INTR_ID_GND);
1313
1314	/* If this has a Mini-AB connector, this mode is highly
1315	 * nonstandard ... but can be handy for testing, especially with
1316	 * the Mini-A end of an OTG cable.  (Or something nonstandard
1317	 * like MiniB-to-StandardB, maybe built with a gender mender.)
1318	 */
1319	isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
1320
1321	dump_regs(isp, __func__);
1322
1323	return 0;
1324
1325#else
1326	dev_dbg(&isp->client->dev, "host sessions not allowed\n");
1327	return -EINVAL;
1328#endif
1329
1330}
1331
1332static int
1333isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
1334{
1335	struct isp1301	*isp = container_of(otg, struct isp1301, otg);
1336#ifndef	CONFIG_USB_OTG
1337	u32 l;
1338#endif
1339
1340	if (!otg || isp != the_transceiver)
1341		return -ENODEV;
1342
1343	if (!gadget) {
1344		omap_writew(0, OTG_IRQ_EN);
1345		if (!isp->otg.default_a)
1346			enable_vbus_draw(isp, 0);
1347		usb_gadget_vbus_disconnect(isp->otg.gadget);
1348		isp->otg.gadget = NULL;
1349		power_down(isp);
1350		return 0;
1351	}
1352
1353#ifdef	CONFIG_USB_OTG
1354	isp->otg.gadget = gadget;
1355	dev_dbg(&isp->client->dev, "registered gadget\n");
1356	/* gadget driver may be suspended until vbus_connect () */
1357	if (isp->otg.host)
1358		return isp1301_otg_enable(isp);
1359	return 0;
1360
1361#elif	!defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
1362	isp->otg.gadget = gadget;
1363	// FIXME update its refcount
1364
1365	l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1366	l &= ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS);
1367	l |= OTG_ID;
1368	omap_writel(l, OTG_CTRL);
1369
1370	power_up(isp);
1371	isp->otg.state = OTG_STATE_B_IDLE;
1372
1373	if (machine_is_omap_h2() || machine_is_omap_h3())
1374		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1375
1376	isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1377		INTR_SESS_VLD);
1378	isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1379		INTR_VBUS_VLD);
1380	dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
1381	dump_regs(isp, __func__);
1382
1383	/* If this has a Mini-AB connector, this mode is highly
1384	 * nonstandard ... but can be handy for testing, so long
1385	 * as you don't plug a Mini-A cable into the jack.
1386	 */
1387	if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
1388		b_peripheral(isp);
1389
1390	return 0;
1391
1392#else
1393	dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
1394	return -EINVAL;
1395#endif
1396}
1397
1398
1399/*-------------------------------------------------------------------------*/
1400
1401static int
1402isp1301_set_power(struct otg_transceiver *dev, unsigned mA)
1403{
1404	if (!the_transceiver)
1405		return -ENODEV;
1406	if (dev->state == OTG_STATE_B_PERIPHERAL)
1407		enable_vbus_draw(the_transceiver, mA);
1408	return 0;
1409}
1410
1411static int
1412isp1301_start_srp(struct otg_transceiver *dev)
1413{
1414	struct isp1301	*isp = container_of(dev, struct isp1301, otg);
1415	u32		otg_ctrl;
1416
1417	if (!dev || isp != the_transceiver
1418			|| isp->otg.state != OTG_STATE_B_IDLE)
1419		return -ENODEV;
1420
1421	otg_ctrl = omap_readl(OTG_CTRL);
1422	if (!(otg_ctrl & OTG_BSESSEND))
1423		return -EINVAL;
1424
1425	otg_ctrl |= OTG_B_BUSREQ;
1426	otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
1427	omap_writel(otg_ctrl, OTG_CTRL);
1428	isp->otg.state = OTG_STATE_B_SRP_INIT;
1429
1430	pr_debug("otg: SRP, %s ... %06x\n", state_name(isp),
1431			omap_readl(OTG_CTRL));
1432#ifdef	CONFIG_USB_OTG
1433	check_state(isp, __func__);
1434#endif
1435	return 0;
1436}
1437
1438static int
1439isp1301_start_hnp(struct otg_transceiver *dev)
1440{
1441#ifdef	CONFIG_USB_OTG
1442	struct isp1301	*isp = container_of(dev, struct isp1301, otg);
1443	u32 l;
1444
1445	if (!dev || isp != the_transceiver)
1446		return -ENODEV;
1447	if (isp->otg.default_a && (isp->otg.host == NULL
1448			|| !isp->otg.host->b_hnp_enable))
1449		return -ENOTCONN;
1450	if (!isp->otg.default_a && (isp->otg.gadget == NULL
1451			|| !isp->otg.gadget->b_hnp_enable))
1452		return -ENOTCONN;
1453
1454	/* We want hardware to manage most HNP protocol timings.
1455	 * So do this part as early as possible...
1456	 */
1457	switch (isp->otg.state) {
1458	case OTG_STATE_B_HOST:
1459		isp->otg.state = OTG_STATE_B_PERIPHERAL;
1460		/* caller will suspend next */
1461		break;
1462	case OTG_STATE_A_HOST:
1463#if 0
1464		/* autoconnect mode avoids irq latency bugs */
1465		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1466				MC1_BDIS_ACON_EN);
1467#endif
1468		/* caller must suspend then clear A_BUSREQ */
1469		usb_gadget_vbus_connect(isp->otg.gadget);
1470		l = omap_readl(OTG_CTRL);
1471		l |= OTG_A_SETB_HNPEN;
1472		omap_writel(l, OTG_CTRL);
1473
1474		break;
1475	case OTG_STATE_A_PERIPHERAL:
1476		/* initiated by B-Host suspend */
1477		break;
1478	default:
1479		return -EILSEQ;
1480	}
1481	pr_debug("otg: HNP %s, %06x ...\n",
1482		state_name(isp), omap_readl(OTG_CTRL));
1483	check_state(isp, __func__);
1484	return 0;
1485#else
1486	/* srp-only */
1487	return -EINVAL;
1488#endif
1489}
1490
1491/*-------------------------------------------------------------------------*/
1492
1493static int __devinit
1494isp1301_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1495{
1496	int			status;
1497	struct isp1301		*isp;
1498
1499	if (the_transceiver)
1500		return 0;
1501
1502	isp = kzalloc(sizeof *isp, GFP_KERNEL);
1503	if (!isp)
1504		return 0;
1505
 
 
 
 
 
 
1506	INIT_WORK(&isp->work, isp1301_work);
1507	init_timer(&isp->timer);
1508	isp->timer.function = isp1301_timer;
1509	isp->timer.data = (unsigned long) isp;
1510
1511	i2c_set_clientdata(i2c, isp);
1512	isp->client = i2c;
1513
1514	/* verify the chip (shouldn't be necessary) */
1515	status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1516	if (status != I2C_VENDOR_ID_PHILIPS) {
1517		dev_dbg(&i2c->dev, "not philips id: %d\n", status);
1518		goto fail;
1519	}
1520	status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1521	if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1522		dev_dbg(&i2c->dev, "not isp1301, %d\n", status);
1523		goto fail;
1524	}
1525	isp->i2c_release = i2c->dev.release;
1526	i2c->dev.release = isp1301_release;
1527
1528	/* initial development used chiprev 2.00 */
1529	status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
1530	dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
1531		status >> 8, status & 0xff);
1532
1533	/* make like power-on reset */
1534	isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
1535
1536	isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
1537	isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
1538
1539	isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1540				OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
1541	isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1542				~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
1543
1544	isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
1545	isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1546	isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1547
1548#ifdef	CONFIG_USB_OTG
1549	status = otg_bind(isp);
1550	if (status < 0) {
1551		dev_dbg(&i2c->dev, "can't bind OTG\n");
1552		goto fail;
1553	}
1554#endif
1555
1556	if (machine_is_omap_h2()) {
1557		/* full speed signaling by default */
1558		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1559			MC1_SPEED);
1560		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
1561			MC2_SPD_SUSP_CTRL);
1562
1563		/* IRQ wired at M14 */
1564		omap_cfg_reg(M14_1510_GPIO2);
1565		if (gpio_request(2, "isp1301") == 0)
1566			gpio_direction_input(2);
1567		isp->irq_type = IRQF_TRIGGER_FALLING;
1568	}
1569
1570	isp->irq_type |= IRQF_SAMPLE_RANDOM;
1571	status = request_irq(i2c->irq, isp1301_irq,
1572			isp->irq_type, DRIVER_NAME, isp);
1573	if (status < 0) {
1574		dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
1575				i2c->irq, status);
1576		goto fail;
1577	}
1578
1579	isp->otg.dev = &i2c->dev;
1580	isp->otg.label = DRIVER_NAME;
1581
1582	isp->otg.set_host = isp1301_set_host,
1583	isp->otg.set_peripheral = isp1301_set_peripheral,
1584	isp->otg.set_power = isp1301_set_power,
1585	isp->otg.start_srp = isp1301_start_srp,
1586	isp->otg.start_hnp = isp1301_start_hnp,
 
1587
1588	enable_vbus_draw(isp, 0);
1589	power_down(isp);
1590	the_transceiver = isp;
1591
1592#ifdef	CONFIG_USB_OTG
1593	update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
1594	update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
1595#endif
1596
1597	dump_regs(isp, __func__);
1598
1599#ifdef	VERBOSE
1600	mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1601	dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
1602#endif
1603
1604	status = otg_set_transceiver(&isp->otg);
1605	if (status < 0)
1606		dev_err(&i2c->dev, "can't register transceiver, %d\n",
1607			status);
1608
1609	return 0;
1610
1611fail:
 
1612	kfree(isp);
1613	return -ENODEV;
1614}
1615
1616static const struct i2c_device_id isp1301_id[] = {
1617	{ "isp1301_omap", 0 },
1618	{ }
1619};
1620MODULE_DEVICE_TABLE(i2c, isp1301_id);
1621
1622static struct i2c_driver isp1301_driver = {
1623	.driver = {
1624		.name	= "isp1301_omap",
1625	},
1626	.probe		= isp1301_probe,
1627	.remove		= __exit_p(isp1301_remove),
1628	.id_table	= isp1301_id,
1629};
1630
1631/*-------------------------------------------------------------------------*/
1632
1633static int __init isp_init(void)
1634{
1635	return i2c_add_driver(&isp1301_driver);
1636}
1637subsys_initcall(isp_init);
1638
1639static void __exit isp_exit(void)
1640{
1641	if (the_transceiver)
1642		otg_set_transceiver(NULL);
1643	i2c_del_driver(&isp1301_driver);
1644}
1645module_exit(isp_exit);
1646
v3.5.6
   1/*
   2 * isp1301_omap - ISP 1301 USB transceiver, talking to OMAP OTG controller
   3 *
   4 * Copyright (C) 2004 Texas Instruments
   5 * Copyright (C) 2004 David Brownell
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 2 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20 */
  21
  22#include <linux/kernel.h>
  23#include <linux/module.h>
  24#include <linux/init.h>
  25#include <linux/slab.h>
  26#include <linux/interrupt.h>
  27#include <linux/platform_device.h>
  28#include <linux/gpio.h>
  29#include <linux/usb/ch9.h>
  30#include <linux/usb/gadget.h>
  31#include <linux/usb.h>
  32#include <linux/usb/otg.h>
  33#include <linux/i2c.h>
  34#include <linux/workqueue.h>
  35
  36#include <asm/irq.h>
  37#include <asm/mach-types.h>
  38
  39#include <plat/usb.h>
  40#include <plat/mux.h>
  41
  42
  43#ifndef	DEBUG
  44#undef	VERBOSE
  45#endif
  46
  47
  48#define	DRIVER_VERSION	"24 August 2004"
  49#define	DRIVER_NAME	(isp1301_driver.driver.name)
  50
  51MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
  52MODULE_LICENSE("GPL");
  53
  54struct isp1301 {
  55	struct usb_phy		phy;
  56	struct i2c_client	*client;
  57	void			(*i2c_release)(struct device *dev);
  58
  59	int			irq_type;
  60
  61	u32			last_otg_ctrl;
  62	unsigned		working:1;
  63
  64	struct timer_list	timer;
  65
  66	/* use keventd context to change the state for us */
  67	struct work_struct	work;
  68
  69	unsigned long		todo;
  70#		define WORK_UPDATE_ISP	0	/* update ISP from OTG */
  71#		define WORK_UPDATE_OTG	1	/* update OTG from ISP */
  72#		define WORK_HOST_RESUME	4	/* resume host */
  73#		define WORK_TIMER	6	/* timer fired */
  74#		define WORK_STOP	7	/* don't resubmit */
  75};
  76
  77
  78/* bits in OTG_CTRL */
  79
  80#define	OTG_XCEIV_OUTPUTS \
  81	(OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
  82#define	OTG_XCEIV_INPUTS \
  83	(OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
  84#define	OTG_CTRL_BITS \
  85	(OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
  86	/* and OTG_PULLUP is sometimes written */
  87
  88#define	OTG_CTRL_MASK	(OTG_DRIVER_SEL| \
  89	OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
  90	OTG_CTRL_BITS)
  91
  92
  93/*-------------------------------------------------------------------------*/
  94
  95/* board-specific PM hooks */
  96
  97#if defined(CONFIG_MACH_OMAP_H2) || defined(CONFIG_MACH_OMAP_H3)
  98
  99#if	defined(CONFIG_TPS65010) || defined(CONFIG_TPS65010_MODULE)
 100
 101#include <linux/i2c/tps65010.h>
 102
 103#else
 104
 105static inline int tps65010_set_vbus_draw(unsigned mA)
 106{
 107	pr_debug("tps65010: draw %d mA (STUB)\n", mA);
 108	return 0;
 109}
 110
 111#endif
 112
 113static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
 114{
 115	int status = tps65010_set_vbus_draw(mA);
 116	if (status < 0)
 117		pr_debug("  VBUS %d mA error %d\n", mA, status);
 118}
 119
 120#else
 121
 122static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
 123{
 124	/* H4 controls this by DIP switch S2.4; no soft control.
 125	 * ON means the charger is always enabled.  Leave it OFF
 126	 * unless the OTG port is used only in B-peripheral mode.
 127	 */
 128}
 129
 130#endif
 131
 132static void enable_vbus_source(struct isp1301 *isp)
 133{
 134	/* this board won't supply more than 8mA vbus power.
 135	 * some boards can switch a 100ma "unit load" (or more).
 136	 */
 137}
 138
 139
 140/* products will deliver OTG messages with LEDs, GUI, etc */
 141static inline void notresponding(struct isp1301 *isp)
 142{
 143	printk(KERN_NOTICE "OTG device not responding.\n");
 144}
 145
 146
 147/*-------------------------------------------------------------------------*/
 148
 149static struct i2c_driver isp1301_driver;
 150
 151/* smbus apis are used for portability */
 152
 153static inline u8
 154isp1301_get_u8(struct isp1301 *isp, u8 reg)
 155{
 156	return i2c_smbus_read_byte_data(isp->client, reg + 0);
 157}
 158
 159static inline int
 160isp1301_get_u16(struct isp1301 *isp, u8 reg)
 161{
 162	return i2c_smbus_read_word_data(isp->client, reg);
 163}
 164
 165static inline int
 166isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
 167{
 168	return i2c_smbus_write_byte_data(isp->client, reg + 0, bits);
 169}
 170
 171static inline int
 172isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
 173{
 174	return i2c_smbus_write_byte_data(isp->client, reg + 1, bits);
 175}
 176
 177/*-------------------------------------------------------------------------*/
 178
 179/* identification */
 180#define	ISP1301_VENDOR_ID		0x00	/* u16 read */
 181#define	ISP1301_PRODUCT_ID		0x02	/* u16 read */
 182#define	ISP1301_BCD_DEVICE		0x14	/* u16 read */
 183
 184#define	I2C_VENDOR_ID_PHILIPS		0x04cc
 185#define	I2C_PRODUCT_ID_PHILIPS_1301	0x1301
 186
 187/* operational registers */
 188#define	ISP1301_MODE_CONTROL_1		0x04	/* u8 read, set, +1 clear */
 189#	define	MC1_SPEED		(1 << 0)
 190#	define	MC1_SUSPEND		(1 << 1)
 191#	define	MC1_DAT_SE0		(1 << 2)
 192#	define	MC1_TRANSPARENT		(1 << 3)
 193#	define	MC1_BDIS_ACON_EN	(1 << 4)
 194#	define	MC1_OE_INT_EN		(1 << 5)
 195#	define	MC1_UART_EN		(1 << 6)
 196#	define	MC1_MASK		0x7f
 197#define	ISP1301_MODE_CONTROL_2		0x12	/* u8 read, set, +1 clear */
 198#	define	MC2_GLOBAL_PWR_DN	(1 << 0)
 199#	define	MC2_SPD_SUSP_CTRL	(1 << 1)
 200#	define	MC2_BI_DI		(1 << 2)
 201#	define	MC2_TRANSP_BDIR0	(1 << 3)
 202#	define	MC2_TRANSP_BDIR1	(1 << 4)
 203#	define	MC2_AUDIO_EN		(1 << 5)
 204#	define	MC2_PSW_EN		(1 << 6)
 205#	define	MC2_EN2V7		(1 << 7)
 206#define	ISP1301_OTG_CONTROL_1		0x06	/* u8 read, set, +1 clear */
 207#	define	OTG1_DP_PULLUP		(1 << 0)
 208#	define	OTG1_DM_PULLUP		(1 << 1)
 209#	define	OTG1_DP_PULLDOWN	(1 << 2)
 210#	define	OTG1_DM_PULLDOWN	(1 << 3)
 211#	define	OTG1_ID_PULLDOWN	(1 << 4)
 212#	define	OTG1_VBUS_DRV		(1 << 5)
 213#	define	OTG1_VBUS_DISCHRG	(1 << 6)
 214#	define	OTG1_VBUS_CHRG		(1 << 7)
 215#define	ISP1301_OTG_STATUS		0x10	/* u8 readonly */
 216#	define	OTG_B_SESS_END		(1 << 6)
 217#	define	OTG_B_SESS_VLD		(1 << 7)
 218
 219#define	ISP1301_INTERRUPT_SOURCE	0x08	/* u8 read */
 220#define	ISP1301_INTERRUPT_LATCH		0x0A	/* u8 read, set, +1 clear */
 221
 222#define	ISP1301_INTERRUPT_FALLING	0x0C	/* u8 read, set, +1 clear */
 223#define	ISP1301_INTERRUPT_RISING	0x0E	/* u8 read, set, +1 clear */
 224
 225/* same bitfields in all interrupt registers */
 226#	define	INTR_VBUS_VLD		(1 << 0)
 227#	define	INTR_SESS_VLD		(1 << 1)
 228#	define	INTR_DP_HI		(1 << 2)
 229#	define	INTR_ID_GND		(1 << 3)
 230#	define	INTR_DM_HI		(1 << 4)
 231#	define	INTR_ID_FLOAT		(1 << 5)
 232#	define	INTR_BDIS_ACON		(1 << 6)
 233#	define	INTR_CR_INT		(1 << 7)
 234
 235/*-------------------------------------------------------------------------*/
 236
 237static inline const char *state_name(struct isp1301 *isp)
 238{
 239	return otg_state_string(isp->phy.state);
 240}
 241
 242/*-------------------------------------------------------------------------*/
 243
 244/* NOTE:  some of this ISP1301 setup is specific to H2 boards;
 245 * not everything is guarded by board-specific checks, or even using
 246 * omap_usb_config data to deduce MC1_DAT_SE0 and MC2_BI_DI.
 247 *
 248 * ALSO:  this currently doesn't use ISP1301 low-power modes
 249 * while OTG is running.
 250 */
 251
 252static void power_down(struct isp1301 *isp)
 253{
 254	isp->phy.state = OTG_STATE_UNDEFINED;
 255
 256	// isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
 257	isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
 258
 259	isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_ID_PULLDOWN);
 260	isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
 261}
 262
 263static void power_up(struct isp1301 *isp)
 264{
 265	// isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
 266	isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
 267
 268	/* do this only when cpu is driving transceiver,
 269	 * so host won't see a low speed device...
 270	 */
 271	isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
 272}
 273
 274#define	NO_HOST_SUSPEND
 275
 276static int host_suspend(struct isp1301 *isp)
 277{
 278#ifdef	NO_HOST_SUSPEND
 279	return 0;
 280#else
 281	struct device	*dev;
 282
 283	if (!isp->phy.otg->host)
 284		return -ENODEV;
 285
 286	/* Currently ASSUMES only the OTG port matters;
 287	 * other ports could be active...
 288	 */
 289	dev = isp->phy.otg->host->controller;
 290	return dev->driver->suspend(dev, 3, 0);
 291#endif
 292}
 293
 294static int host_resume(struct isp1301 *isp)
 295{
 296#ifdef	NO_HOST_SUSPEND
 297	return 0;
 298#else
 299	struct device	*dev;
 300
 301	if (!isp->phy.otg->host)
 302		return -ENODEV;
 303
 304	dev = isp->phy.otg->host->controller;
 305	return dev->driver->resume(dev, 0);
 306#endif
 307}
 308
 309static int gadget_suspend(struct isp1301 *isp)
 310{
 311	isp->phy.otg->gadget->b_hnp_enable = 0;
 312	isp->phy.otg->gadget->a_hnp_support = 0;
 313	isp->phy.otg->gadget->a_alt_hnp_support = 0;
 314	return usb_gadget_vbus_disconnect(isp->phy.otg->gadget);
 315}
 316
 317/*-------------------------------------------------------------------------*/
 318
 319#define	TIMER_MINUTES	10
 320#define	TIMER_JIFFIES	(TIMER_MINUTES * 60 * HZ)
 321
 322/* Almost all our I2C messaging comes from a work queue's task context.
 323 * NOTE: guaranteeing certain response times might mean we shouldn't
 324 * share keventd's work queue; a realtime task might be safest.
 325 */
 326static void isp1301_defer_work(struct isp1301 *isp, int work)
 327{
 328	int status;
 329
 330	if (isp && !test_and_set_bit(work, &isp->todo)) {
 331		(void) get_device(&isp->client->dev);
 332		status = schedule_work(&isp->work);
 333		if (!status && !isp->working)
 334			dev_vdbg(&isp->client->dev,
 335				"work item %d may be lost\n", work);
 336	}
 337}
 338
 339/* called from irq handlers */
 340static void a_idle(struct isp1301 *isp, const char *tag)
 341{
 342	u32 l;
 343
 344	if (isp->phy.state == OTG_STATE_A_IDLE)
 345		return;
 346
 347	isp->phy.otg->default_a = 1;
 348	if (isp->phy.otg->host) {
 349		isp->phy.otg->host->is_b_host = 0;
 350		host_suspend(isp);
 351	}
 352	if (isp->phy.otg->gadget) {
 353		isp->phy.otg->gadget->is_a_peripheral = 1;
 354		gadget_suspend(isp);
 355	}
 356	isp->phy.state = OTG_STATE_A_IDLE;
 357	l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
 358	omap_writel(l, OTG_CTRL);
 359	isp->last_otg_ctrl = l;
 360	pr_debug("  --> %s/%s\n", state_name(isp), tag);
 361}
 362
 363/* called from irq handlers */
 364static void b_idle(struct isp1301 *isp, const char *tag)
 365{
 366	u32 l;
 367
 368	if (isp->phy.state == OTG_STATE_B_IDLE)
 369		return;
 370
 371	isp->phy.otg->default_a = 0;
 372	if (isp->phy.otg->host) {
 373		isp->phy.otg->host->is_b_host = 1;
 374		host_suspend(isp);
 375	}
 376	if (isp->phy.otg->gadget) {
 377		isp->phy.otg->gadget->is_a_peripheral = 0;
 378		gadget_suspend(isp);
 379	}
 380	isp->phy.state = OTG_STATE_B_IDLE;
 381	l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
 382	omap_writel(l, OTG_CTRL);
 383	isp->last_otg_ctrl = l;
 384	pr_debug("  --> %s/%s\n", state_name(isp), tag);
 385}
 386
 387static void
 388dump_regs(struct isp1301 *isp, const char *label)
 389{
 390#ifdef	DEBUG
 391	u8	ctrl = isp1301_get_u8(isp, ISP1301_OTG_CONTROL_1);
 392	u8	status = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
 393	u8	src = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
 394
 395	pr_debug("otg: %06x, %s %s, otg/%02x stat/%02x.%02x\n",
 396		omap_readl(OTG_CTRL), label, state_name(isp),
 397		ctrl, status, src);
 398	/* mode control and irq enables don't change much */
 399#endif
 400}
 401
 402/*-------------------------------------------------------------------------*/
 403
 404#ifdef	CONFIG_USB_OTG
 405
 406/*
 407 * The OMAP OTG controller handles most of the OTG state transitions.
 408 *
 409 * We translate isp1301 outputs (mostly voltage comparator status) into
 410 * OTG inputs; OTG outputs (mostly pullup/pulldown controls) and HNP state
 411 * flags into isp1301 inputs ... and infer state transitions.
 412 */
 413
 414#ifdef	VERBOSE
 415
 416static void check_state(struct isp1301 *isp, const char *tag)
 417{
 418	enum usb_otg_state	state = OTG_STATE_UNDEFINED;
 419	u8			fsm = omap_readw(OTG_TEST) & 0x0ff;
 420	unsigned		extra = 0;
 421
 422	switch (fsm) {
 423
 424	/* default-b */
 425	case 0x0:
 426		state = OTG_STATE_B_IDLE;
 427		break;
 428	case 0x3:
 429	case 0x7:
 430		extra = 1;
 431	case 0x1:
 432		state = OTG_STATE_B_PERIPHERAL;
 433		break;
 434	case 0x11:
 435		state = OTG_STATE_B_SRP_INIT;
 436		break;
 437
 438	/* extra dual-role default-b states */
 439	case 0x12:
 440	case 0x13:
 441	case 0x16:
 442		extra = 1;
 443	case 0x17:
 444		state = OTG_STATE_B_WAIT_ACON;
 445		break;
 446	case 0x34:
 447		state = OTG_STATE_B_HOST;
 448		break;
 449
 450	/* default-a */
 451	case 0x36:
 452		state = OTG_STATE_A_IDLE;
 453		break;
 454	case 0x3c:
 455		state = OTG_STATE_A_WAIT_VFALL;
 456		break;
 457	case 0x7d:
 458		state = OTG_STATE_A_VBUS_ERR;
 459		break;
 460	case 0x9e:
 461	case 0x9f:
 462		extra = 1;
 463	case 0x89:
 464		state = OTG_STATE_A_PERIPHERAL;
 465		break;
 466	case 0xb7:
 467		state = OTG_STATE_A_WAIT_VRISE;
 468		break;
 469	case 0xb8:
 470		state = OTG_STATE_A_WAIT_BCON;
 471		break;
 472	case 0xb9:
 473		state = OTG_STATE_A_HOST;
 474		break;
 475	case 0xba:
 476		state = OTG_STATE_A_SUSPEND;
 477		break;
 478	default:
 479		break;
 480	}
 481	if (isp->phy.state == state && !extra)
 482		return;
 483	pr_debug("otg: %s FSM %s/%02x, %s, %06x\n", tag,
 484		otg_state_string(state), fsm, state_name(isp),
 485		omap_readl(OTG_CTRL));
 486}
 487
 488#else
 489
 490static inline void check_state(struct isp1301 *isp, const char *tag) { }
 491
 492#endif
 493
 494/* outputs from ISP1301_INTERRUPT_SOURCE */
 495static void update_otg1(struct isp1301 *isp, u8 int_src)
 496{
 497	u32	otg_ctrl;
 498
 499	otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
 500	otg_ctrl &= ~OTG_XCEIV_INPUTS;
 501	otg_ctrl &= ~(OTG_ID|OTG_ASESSVLD|OTG_VBUSVLD);
 502
 503	if (int_src & INTR_SESS_VLD)
 504		otg_ctrl |= OTG_ASESSVLD;
 505	else if (isp->phy.state == OTG_STATE_A_WAIT_VFALL) {
 506		a_idle(isp, "vfall");
 507		otg_ctrl &= ~OTG_CTRL_BITS;
 508	}
 509	if (int_src & INTR_VBUS_VLD)
 510		otg_ctrl |= OTG_VBUSVLD;
 511	if (int_src & INTR_ID_GND) {		/* default-A */
 512		if (isp->phy.state == OTG_STATE_B_IDLE
 513				|| isp->phy.state
 514					== OTG_STATE_UNDEFINED) {
 515			a_idle(isp, "init");
 516			return;
 517		}
 518	} else {				/* default-B */
 519		otg_ctrl |= OTG_ID;
 520		if (isp->phy.state == OTG_STATE_A_IDLE
 521			|| isp->phy.state == OTG_STATE_UNDEFINED) {
 522			b_idle(isp, "init");
 523			return;
 524		}
 525	}
 526	omap_writel(otg_ctrl, OTG_CTRL);
 527}
 528
 529/* outputs from ISP1301_OTG_STATUS */
 530static void update_otg2(struct isp1301 *isp, u8 otg_status)
 531{
 532	u32	otg_ctrl;
 533
 534	otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
 535	otg_ctrl &= ~OTG_XCEIV_INPUTS;
 536	otg_ctrl &= ~(OTG_BSESSVLD | OTG_BSESSEND);
 537	if (otg_status & OTG_B_SESS_VLD)
 538		otg_ctrl |= OTG_BSESSVLD;
 539	else if (otg_status & OTG_B_SESS_END)
 540		otg_ctrl |= OTG_BSESSEND;
 541	omap_writel(otg_ctrl, OTG_CTRL);
 542}
 543
 544/* inputs going to ISP1301 */
 545static void otg_update_isp(struct isp1301 *isp)
 546{
 547	u32	otg_ctrl, otg_change;
 548	u8	set = OTG1_DM_PULLDOWN, clr = OTG1_DM_PULLUP;
 549
 550	otg_ctrl = omap_readl(OTG_CTRL);
 551	otg_change = otg_ctrl ^ isp->last_otg_ctrl;
 552	isp->last_otg_ctrl = otg_ctrl;
 553	otg_ctrl = otg_ctrl & OTG_XCEIV_INPUTS;
 554
 555	switch (isp->phy.state) {
 556	case OTG_STATE_B_IDLE:
 557	case OTG_STATE_B_PERIPHERAL:
 558	case OTG_STATE_B_SRP_INIT:
 559		if (!(otg_ctrl & OTG_PULLUP)) {
 560			// if (otg_ctrl & OTG_B_HNPEN) {
 561			if (isp->phy.otg->gadget->b_hnp_enable) {
 562				isp->phy.state = OTG_STATE_B_WAIT_ACON;
 563				pr_debug("  --> b_wait_acon\n");
 564			}
 565			goto pulldown;
 566		}
 567pullup:
 568		set |= OTG1_DP_PULLUP;
 569		clr |= OTG1_DP_PULLDOWN;
 570		break;
 571	case OTG_STATE_A_SUSPEND:
 572	case OTG_STATE_A_PERIPHERAL:
 573		if (otg_ctrl & OTG_PULLUP)
 574			goto pullup;
 575		/* FALLTHROUGH */
 576	// case OTG_STATE_B_WAIT_ACON:
 577	default:
 578pulldown:
 579		set |= OTG1_DP_PULLDOWN;
 580		clr |= OTG1_DP_PULLUP;
 581		break;
 582	}
 583
 584#	define toggle(OTG,ISP) do { \
 585		if (otg_ctrl & OTG) set |= ISP; \
 586		else clr |= ISP; \
 587		} while (0)
 588
 589	if (!(isp->phy.otg->host))
 590		otg_ctrl &= ~OTG_DRV_VBUS;
 591
 592	switch (isp->phy.state) {
 593	case OTG_STATE_A_SUSPEND:
 594		if (otg_ctrl & OTG_DRV_VBUS) {
 595			set |= OTG1_VBUS_DRV;
 596			break;
 597		}
 598		/* HNP failed for some reason (A_AIDL_BDIS timeout) */
 599		notresponding(isp);
 600
 601		/* FALLTHROUGH */
 602	case OTG_STATE_A_VBUS_ERR:
 603		isp->phy.state = OTG_STATE_A_WAIT_VFALL;
 604		pr_debug("  --> a_wait_vfall\n");
 605		/* FALLTHROUGH */
 606	case OTG_STATE_A_WAIT_VFALL:
 607		/* FIXME usbcore thinks port power is still on ... */
 608		clr |= OTG1_VBUS_DRV;
 609		break;
 610	case OTG_STATE_A_IDLE:
 611		if (otg_ctrl & OTG_DRV_VBUS) {
 612			isp->phy.state = OTG_STATE_A_WAIT_VRISE;
 613			pr_debug("  --> a_wait_vrise\n");
 614		}
 615		/* FALLTHROUGH */
 616	default:
 617		toggle(OTG_DRV_VBUS, OTG1_VBUS_DRV);
 618	}
 619
 620	toggle(OTG_PU_VBUS, OTG1_VBUS_CHRG);
 621	toggle(OTG_PD_VBUS, OTG1_VBUS_DISCHRG);
 622
 623#	undef toggle
 624
 625	isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, set);
 626	isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, clr);
 627
 628	/* HNP switch to host or peripheral; and SRP */
 629	if (otg_change & OTG_PULLUP) {
 630		u32 l;
 631
 632		switch (isp->phy.state) {
 633		case OTG_STATE_B_IDLE:
 634			if (clr & OTG1_DP_PULLUP)
 635				break;
 636			isp->phy.state = OTG_STATE_B_PERIPHERAL;
 637			pr_debug("  --> b_peripheral\n");
 638			break;
 639		case OTG_STATE_A_SUSPEND:
 640			if (clr & OTG1_DP_PULLUP)
 641				break;
 642			isp->phy.state = OTG_STATE_A_PERIPHERAL;
 643			pr_debug("  --> a_peripheral\n");
 644			break;
 645		default:
 646			break;
 647		}
 648		l = omap_readl(OTG_CTRL);
 649		l |= OTG_PULLUP;
 650		omap_writel(l, OTG_CTRL);
 651	}
 652
 653	check_state(isp, __func__);
 654	dump_regs(isp, "otg->isp1301");
 655}
 656
 657static irqreturn_t omap_otg_irq(int irq, void *_isp)
 658{
 659	u16		otg_irq = omap_readw(OTG_IRQ_SRC);
 660	u32		otg_ctrl;
 661	int		ret = IRQ_NONE;
 662	struct isp1301	*isp = _isp;
 663	struct usb_otg	*otg = isp->phy.otg;
 664
 665	/* update ISP1301 transceiver from OTG controller */
 666	if (otg_irq & OPRT_CHG) {
 667		omap_writew(OPRT_CHG, OTG_IRQ_SRC);
 668		isp1301_defer_work(isp, WORK_UPDATE_ISP);
 669		ret = IRQ_HANDLED;
 670
 671	/* SRP to become b_peripheral failed */
 672	} else if (otg_irq & B_SRP_TMROUT) {
 673		pr_debug("otg: B_SRP_TIMEOUT, %06x\n", omap_readl(OTG_CTRL));
 674		notresponding(isp);
 675
 676		/* gadget drivers that care should monitor all kinds of
 677		 * remote wakeup (SRP, normal) using their own timer
 678		 * to give "check cable and A-device" messages.
 679		 */
 680		if (isp->phy.state == OTG_STATE_B_SRP_INIT)
 681			b_idle(isp, "srp_timeout");
 682
 683		omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
 684		ret = IRQ_HANDLED;
 685
 686	/* HNP to become b_host failed */
 687	} else if (otg_irq & B_HNP_FAIL) {
 688		pr_debug("otg: %s B_HNP_FAIL, %06x\n",
 689				state_name(isp), omap_readl(OTG_CTRL));
 690		notresponding(isp);
 691
 692		otg_ctrl = omap_readl(OTG_CTRL);
 693		otg_ctrl |= OTG_BUSDROP;
 694		otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
 695		omap_writel(otg_ctrl, OTG_CTRL);
 696
 697		/* subset of b_peripheral()... */
 698		isp->phy.state = OTG_STATE_B_PERIPHERAL;
 699		pr_debug("  --> b_peripheral\n");
 700
 701		omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
 702		ret = IRQ_HANDLED;
 703
 704	/* detect SRP from B-device ... */
 705	} else if (otg_irq & A_SRP_DETECT) {
 706		pr_debug("otg: %s SRP_DETECT, %06x\n",
 707				state_name(isp), omap_readl(OTG_CTRL));
 708
 709		isp1301_defer_work(isp, WORK_UPDATE_OTG);
 710		switch (isp->phy.state) {
 711		case OTG_STATE_A_IDLE:
 712			if (!otg->host)
 713				break;
 714			isp1301_defer_work(isp, WORK_HOST_RESUME);
 715			otg_ctrl = omap_readl(OTG_CTRL);
 716			otg_ctrl |= OTG_A_BUSREQ;
 717			otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
 718					& ~OTG_XCEIV_INPUTS
 719					& OTG_CTRL_MASK;
 720			omap_writel(otg_ctrl, OTG_CTRL);
 721			break;
 722		default:
 723			break;
 724		}
 725
 726		omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
 727		ret = IRQ_HANDLED;
 728
 729	/* timer expired:  T(a_wait_bcon) and maybe T(a_wait_vrise)
 730	 * we don't track them separately
 731	 */
 732	} else if (otg_irq & A_REQ_TMROUT) {
 733		otg_ctrl = omap_readl(OTG_CTRL);
 734		pr_info("otg: BCON_TMOUT from %s, %06x\n",
 735				state_name(isp), otg_ctrl);
 736		notresponding(isp);
 737
 738		otg_ctrl |= OTG_BUSDROP;
 739		otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
 740		omap_writel(otg_ctrl, OTG_CTRL);
 741		isp->phy.state = OTG_STATE_A_WAIT_VFALL;
 742
 743		omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
 744		ret = IRQ_HANDLED;
 745
 746	/* A-supplied voltage fell too low; overcurrent */
 747	} else if (otg_irq & A_VBUS_ERR) {
 748		otg_ctrl = omap_readl(OTG_CTRL);
 749		printk(KERN_ERR "otg: %s, VBUS_ERR %04x ctrl %06x\n",
 750			state_name(isp), otg_irq, otg_ctrl);
 751
 752		otg_ctrl |= OTG_BUSDROP;
 753		otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
 754		omap_writel(otg_ctrl, OTG_CTRL);
 755		isp->phy.state = OTG_STATE_A_VBUS_ERR;
 756
 757		omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
 758		ret = IRQ_HANDLED;
 759
 760	/* switch driver; the transceiver code activates it,
 761	 * ungating the udc clock or resuming OHCI.
 762	 */
 763	} else if (otg_irq & DRIVER_SWITCH) {
 764		int	kick = 0;
 765
 766		otg_ctrl = omap_readl(OTG_CTRL);
 767		printk(KERN_NOTICE "otg: %s, SWITCH to %s, ctrl %06x\n",
 768				state_name(isp),
 769				(otg_ctrl & OTG_DRIVER_SEL)
 770					? "gadget" : "host",
 771				otg_ctrl);
 772		isp1301_defer_work(isp, WORK_UPDATE_ISP);
 773
 774		/* role is peripheral */
 775		if (otg_ctrl & OTG_DRIVER_SEL) {
 776			switch (isp->phy.state) {
 777			case OTG_STATE_A_IDLE:
 778				b_idle(isp, __func__);
 779				break;
 780			default:
 781				break;
 782			}
 783			isp1301_defer_work(isp, WORK_UPDATE_ISP);
 784
 785		/* role is host */
 786		} else {
 787			if (!(otg_ctrl & OTG_ID)) {
 788				otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
 789				omap_writel(otg_ctrl | OTG_A_BUSREQ, OTG_CTRL);
 790			}
 791
 792			if (otg->host) {
 793				switch (isp->phy.state) {
 794				case OTG_STATE_B_WAIT_ACON:
 795					isp->phy.state = OTG_STATE_B_HOST;
 796					pr_debug("  --> b_host\n");
 797					kick = 1;
 798					break;
 799				case OTG_STATE_A_WAIT_BCON:
 800					isp->phy.state = OTG_STATE_A_HOST;
 801					pr_debug("  --> a_host\n");
 802					break;
 803				case OTG_STATE_A_PERIPHERAL:
 804					isp->phy.state = OTG_STATE_A_WAIT_BCON;
 805					pr_debug("  --> a_wait_bcon\n");
 806					break;
 807				default:
 808					break;
 809				}
 810				isp1301_defer_work(isp, WORK_HOST_RESUME);
 811			}
 812		}
 813
 814		omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
 815		ret = IRQ_HANDLED;
 816
 817		if (kick)
 818			usb_bus_start_enum(otg->host, otg->host->otg_port);
 
 819	}
 820
 821	check_state(isp, __func__);
 822	return ret;
 823}
 824
 825static struct platform_device *otg_dev;
 826
 827static int isp1301_otg_init(struct isp1301 *isp)
 828{
 829	u32 l;
 830
 831	if (!otg_dev)
 832		return -ENODEV;
 833
 834	dump_regs(isp, __func__);
 835	/* some of these values are board-specific... */
 836	l = omap_readl(OTG_SYSCON_2);
 837	l |= OTG_EN
 838		/* for B-device: */
 839		| SRP_GPDATA		/* 9msec Bdev D+ pulse */
 840		| SRP_GPDVBUS		/* discharge after VBUS pulse */
 841		// | (3 << 24)		/* 2msec VBUS pulse */
 842		/* for A-device: */
 843		| (0 << 20)		/* 200ms nominal A_WAIT_VRISE timer */
 844		| SRP_DPW		/* detect 167+ns SRP pulses */
 845		| SRP_DATA | SRP_VBUS	/* accept both kinds of SRP pulse */
 846		;
 847	omap_writel(l, OTG_SYSCON_2);
 848
 849	update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
 850	update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
 851
 852	check_state(isp, __func__);
 853	pr_debug("otg: %s, %s %06x\n",
 854			state_name(isp), __func__, omap_readl(OTG_CTRL));
 855
 856	omap_writew(DRIVER_SWITCH | OPRT_CHG
 857			| B_SRP_TMROUT | B_HNP_FAIL
 858			| A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT, OTG_IRQ_EN);
 859
 860	l = omap_readl(OTG_SYSCON_2);
 861	l |= OTG_EN;
 862	omap_writel(l, OTG_SYSCON_2);
 863
 864	return 0;
 865}
 866
 867static int otg_probe(struct platform_device *dev)
 868{
 869	// struct omap_usb_config *config = dev->platform_data;
 870
 871	otg_dev = dev;
 872	return 0;
 873}
 874
 875static int otg_remove(struct platform_device *dev)
 876{
 877	otg_dev = NULL;
 878	return 0;
 879}
 880
 881static struct platform_driver omap_otg_driver = {
 882	.probe		= otg_probe,
 883	.remove		= otg_remove,
 884	.driver		= {
 885		.owner	= THIS_MODULE,
 886		.name	= "omap_otg",
 887	},
 888};
 889
 890static int otg_bind(struct isp1301 *isp)
 891{
 892	int	status;
 893
 894	if (otg_dev)
 895		return -EBUSY;
 896
 897	status = platform_driver_register(&omap_otg_driver);
 898	if (status < 0)
 899		return status;
 900
 901	if (otg_dev)
 902		status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
 903				0, DRIVER_NAME, isp);
 904	else
 905		status = -ENODEV;
 906
 907	if (status < 0)
 908		platform_driver_unregister(&omap_otg_driver);
 909	return status;
 910}
 911
 912static void otg_unbind(struct isp1301 *isp)
 913{
 914	if (!otg_dev)
 915		return;
 916	free_irq(otg_dev->resource[1].start, isp);
 917}
 918
 919#else
 920
 921/* OTG controller isn't clocked */
 922
 923#endif	/* CONFIG_USB_OTG */
 924
 925/*-------------------------------------------------------------------------*/
 926
 927static void b_peripheral(struct isp1301 *isp)
 928{
 929	u32 l;
 930
 931	l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
 932	omap_writel(l, OTG_CTRL);
 933
 934	usb_gadget_vbus_connect(isp->phy.otg->gadget);
 935
 936#ifdef	CONFIG_USB_OTG
 937	enable_vbus_draw(isp, 8);
 938	otg_update_isp(isp);
 939#else
 940	enable_vbus_draw(isp, 100);
 941	/* UDC driver just set OTG_BSESSVLD */
 942	isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
 943	isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
 944	isp->phy.state = OTG_STATE_B_PERIPHERAL;
 945	pr_debug("  --> b_peripheral\n");
 946	dump_regs(isp, "2periph");
 947#endif
 948}
 949
 950static void isp_update_otg(struct isp1301 *isp, u8 stat)
 951{
 952	struct usb_otg		*otg = isp->phy.otg;
 953	u8			isp_stat, isp_bstat;
 954	enum usb_otg_state	state = isp->phy.state;
 955
 956	if (stat & INTR_BDIS_ACON)
 957		pr_debug("OTG:  BDIS_ACON, %s\n", state_name(isp));
 958
 959	/* start certain state transitions right away */
 960	isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
 961	if (isp_stat & INTR_ID_GND) {
 962		if (otg->default_a) {
 963			switch (state) {
 964			case OTG_STATE_B_IDLE:
 965				a_idle(isp, "idle");
 966				/* FALLTHROUGH */
 967			case OTG_STATE_A_IDLE:
 968				enable_vbus_source(isp);
 969				/* FALLTHROUGH */
 970			case OTG_STATE_A_WAIT_VRISE:
 971				/* we skip over OTG_STATE_A_WAIT_BCON, since
 972				 * the HC will transition to A_HOST (or
 973				 * A_SUSPEND!) without our noticing except
 974				 * when HNP is used.
 975				 */
 976				if (isp_stat & INTR_VBUS_VLD)
 977					isp->phy.state = OTG_STATE_A_HOST;
 978				break;
 979			case OTG_STATE_A_WAIT_VFALL:
 980				if (!(isp_stat & INTR_SESS_VLD))
 981					a_idle(isp, "vfell");
 982				break;
 983			default:
 984				if (!(isp_stat & INTR_VBUS_VLD))
 985					isp->phy.state = OTG_STATE_A_VBUS_ERR;
 986				break;
 987			}
 988			isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
 989		} else {
 990			switch (state) {
 991			case OTG_STATE_B_PERIPHERAL:
 992			case OTG_STATE_B_HOST:
 993			case OTG_STATE_B_WAIT_ACON:
 994				usb_gadget_vbus_disconnect(otg->gadget);
 995				break;
 996			default:
 997				break;
 998			}
 999			if (state != OTG_STATE_A_IDLE)
1000				a_idle(isp, "id");
1001			if (otg->host && state == OTG_STATE_A_IDLE)
1002				isp1301_defer_work(isp, WORK_HOST_RESUME);
1003			isp_bstat = 0;
1004		}
1005	} else {
1006		u32 l;
1007
1008		/* if user unplugged mini-A end of cable,
1009		 * don't bypass A_WAIT_VFALL.
1010		 */
1011		if (otg->default_a) {
1012			switch (state) {
1013			default:
1014				isp->phy.state = OTG_STATE_A_WAIT_VFALL;
1015				break;
1016			case OTG_STATE_A_WAIT_VFALL:
1017				state = OTG_STATE_A_IDLE;
1018				/* khubd may take a while to notice and
1019				 * handle this disconnect, so don't go
1020				 * to B_IDLE quite yet.
1021				 */
1022				break;
1023			case OTG_STATE_A_IDLE:
1024				host_suspend(isp);
1025				isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
1026						MC1_BDIS_ACON_EN);
1027				isp->phy.state = OTG_STATE_B_IDLE;
1028				l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1029				l &= ~OTG_CTRL_BITS;
1030				omap_writel(l, OTG_CTRL);
1031				break;
1032			case OTG_STATE_B_IDLE:
1033				break;
1034			}
1035		}
1036		isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
1037
1038		switch (isp->phy.state) {
1039		case OTG_STATE_B_PERIPHERAL:
1040		case OTG_STATE_B_WAIT_ACON:
1041		case OTG_STATE_B_HOST:
1042			if (likely(isp_bstat & OTG_B_SESS_VLD))
1043				break;
1044			enable_vbus_draw(isp, 0);
1045#ifndef	CONFIG_USB_OTG
1046			/* UDC driver will clear OTG_BSESSVLD */
1047			isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1048						OTG1_DP_PULLDOWN);
1049			isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1050						OTG1_DP_PULLUP);
1051			dump_regs(isp, __func__);
1052#endif
1053			/* FALLTHROUGH */
1054		case OTG_STATE_B_SRP_INIT:
1055			b_idle(isp, __func__);
1056			l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
1057			omap_writel(l, OTG_CTRL);
1058			/* FALLTHROUGH */
1059		case OTG_STATE_B_IDLE:
1060			if (otg->gadget && (isp_bstat & OTG_B_SESS_VLD)) {
1061#ifdef	CONFIG_USB_OTG
1062				update_otg1(isp, isp_stat);
1063				update_otg2(isp, isp_bstat);
1064#endif
1065				b_peripheral(isp);
1066			} else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
1067				isp_bstat |= OTG_B_SESS_END;
1068			break;
1069		case OTG_STATE_A_WAIT_VFALL:
1070			break;
1071		default:
1072			pr_debug("otg: unsupported b-device %s\n",
1073				state_name(isp));
1074			break;
1075		}
1076	}
1077
1078	if (state != isp->phy.state)
1079		pr_debug("  isp, %s -> %s\n",
1080				otg_state_string(state), state_name(isp));
1081
1082#ifdef	CONFIG_USB_OTG
1083	/* update the OTG controller state to match the isp1301; may
1084	 * trigger OPRT_CHG irqs for changes going to the isp1301.
1085	 */
1086	update_otg1(isp, isp_stat);
1087	update_otg2(isp, isp_bstat);
1088	check_state(isp, __func__);
1089#endif
1090
1091	dump_regs(isp, "isp1301->otg");
1092}
1093
1094/*-------------------------------------------------------------------------*/
1095
1096static u8 isp1301_clear_latch(struct isp1301 *isp)
1097{
1098	u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
1099	isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
1100	return latch;
1101}
1102
1103static void
1104isp1301_work(struct work_struct *work)
1105{
1106	struct isp1301	*isp = container_of(work, struct isp1301, work);
1107	int		stop;
1108
1109	/* implicit lock:  we're the only task using this device */
1110	isp->working = 1;
1111	do {
1112		stop = test_bit(WORK_STOP, &isp->todo);
1113
1114#ifdef	CONFIG_USB_OTG
1115		/* transfer state from otg engine to isp1301 */
1116		if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
1117			otg_update_isp(isp);
1118			put_device(&isp->client->dev);
1119		}
1120#endif
1121		/* transfer state from isp1301 to otg engine */
1122		if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
1123			u8		stat = isp1301_clear_latch(isp);
1124
1125			isp_update_otg(isp, stat);
1126			put_device(&isp->client->dev);
1127		}
1128
1129		if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
1130			u32	otg_ctrl;
1131
1132			/*
1133			 * skip A_WAIT_VRISE; hc transitions invisibly
1134			 * skip A_WAIT_BCON; same.
1135			 */
1136			switch (isp->phy.state) {
1137			case OTG_STATE_A_WAIT_BCON:
1138			case OTG_STATE_A_WAIT_VRISE:
1139				isp->phy.state = OTG_STATE_A_HOST;
1140				pr_debug("  --> a_host\n");
1141				otg_ctrl = omap_readl(OTG_CTRL);
1142				otg_ctrl |= OTG_A_BUSREQ;
1143				otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
1144						& OTG_CTRL_MASK;
1145				omap_writel(otg_ctrl, OTG_CTRL);
1146				break;
1147			case OTG_STATE_B_WAIT_ACON:
1148				isp->phy.state = OTG_STATE_B_HOST;
1149				pr_debug("  --> b_host (acon)\n");
1150				break;
1151			case OTG_STATE_B_HOST:
1152			case OTG_STATE_B_IDLE:
1153			case OTG_STATE_A_IDLE:
1154				break;
1155			default:
1156				pr_debug("  host resume in %s\n",
1157						state_name(isp));
1158			}
1159			host_resume(isp);
1160			// mdelay(10);
1161			put_device(&isp->client->dev);
1162		}
1163
1164		if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
1165#ifdef	VERBOSE
1166			dump_regs(isp, "timer");
1167			if (!stop)
1168				mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1169#endif
1170			put_device(&isp->client->dev);
1171		}
1172
1173		if (isp->todo)
1174			dev_vdbg(&isp->client->dev,
1175				"work done, todo = 0x%lx\n",
1176				isp->todo);
1177		if (stop) {
1178			dev_dbg(&isp->client->dev, "stop\n");
1179			break;
1180		}
1181	} while (isp->todo);
1182	isp->working = 0;
1183}
1184
1185static irqreturn_t isp1301_irq(int irq, void *isp)
1186{
1187	isp1301_defer_work(isp, WORK_UPDATE_OTG);
1188	return IRQ_HANDLED;
1189}
1190
1191static void isp1301_timer(unsigned long _isp)
1192{
1193	isp1301_defer_work((void *)_isp, WORK_TIMER);
1194}
1195
1196/*-------------------------------------------------------------------------*/
1197
1198static void isp1301_release(struct device *dev)
1199{
1200	struct isp1301	*isp;
1201
1202	isp = dev_get_drvdata(dev);
1203
1204	/* FIXME -- not with a "new style" driver, it doesn't!! */
1205
1206	/* ugly -- i2c hijacks our memory hook to wait_for_completion() */
1207	if (isp->i2c_release)
1208		isp->i2c_release(dev);
1209	kfree(isp->phy.otg);
1210	kfree (isp);
1211}
1212
1213static struct isp1301 *the_transceiver;
1214
1215static int __exit isp1301_remove(struct i2c_client *i2c)
1216{
1217	struct isp1301	*isp;
1218
1219	isp = i2c_get_clientdata(i2c);
1220
1221	isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1222	isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1223	free_irq(i2c->irq, isp);
1224#ifdef	CONFIG_USB_OTG
1225	otg_unbind(isp);
1226#endif
1227	if (machine_is_omap_h2())
1228		gpio_free(2);
1229
1230	isp->timer.data = 0;
1231	set_bit(WORK_STOP, &isp->todo);
1232	del_timer_sync(&isp->timer);
1233	flush_work_sync(&isp->work);
1234
1235	put_device(&i2c->dev);
1236	the_transceiver = NULL;
1237
1238	return 0;
1239}
1240
1241/*-------------------------------------------------------------------------*/
1242
1243/* NOTE:  three modes are possible here, only one of which
1244 * will be standards-conformant on any given system:
1245 *
1246 *  - OTG mode (dual-role), required if there's a Mini-AB connector
1247 *  - HOST mode, for when there's one or more A (host) connectors
1248 *  - DEVICE mode, for when there's a B/Mini-B (device) connector
1249 *
1250 * As a rule, you won't have an isp1301 chip unless it's there to
1251 * support the OTG mode.  Other modes help testing USB controllers
1252 * in isolation from (full) OTG support, or maybe so later board
1253 * revisions can help to support those feature.
1254 */
1255
1256#ifdef	CONFIG_USB_OTG
1257
1258static int isp1301_otg_enable(struct isp1301 *isp)
1259{
1260	power_up(isp);
1261	isp1301_otg_init(isp);
1262
1263	/* NOTE:  since we don't change this, this provides
1264	 * a few more interrupts than are strictly needed.
1265	 */
1266	isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1267		INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1268	isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1269		INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
1270
1271	dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
1272
1273	return 0;
1274}
1275
1276#endif
1277
1278/* add or disable the host device+driver */
1279static int
1280isp1301_set_host(struct usb_otg *otg, struct usb_bus *host)
1281{
1282	struct isp1301	*isp = container_of(otg->phy, struct isp1301, phy);
1283
1284	if (!otg || isp != the_transceiver)
1285		return -ENODEV;
1286
1287	if (!host) {
1288		omap_writew(0, OTG_IRQ_EN);
1289		power_down(isp);
1290		otg->host = NULL;
1291		return 0;
1292	}
1293
1294#ifdef	CONFIG_USB_OTG
1295	otg->host = host;
1296	dev_dbg(&isp->client->dev, "registered host\n");
1297	host_suspend(isp);
1298	if (otg->gadget)
1299		return isp1301_otg_enable(isp);
1300	return 0;
1301
1302#elif	!defined(CONFIG_USB_GADGET_OMAP)
1303	// FIXME update its refcount
1304	otg->host = host;
1305
1306	power_up(isp);
1307
1308	if (machine_is_omap_h2())
1309		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1310
1311	dev_info(&isp->client->dev, "A-Host sessions ok\n");
1312	isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1313		INTR_ID_GND);
1314	isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1315		INTR_ID_GND);
1316
1317	/* If this has a Mini-AB connector, this mode is highly
1318	 * nonstandard ... but can be handy for testing, especially with
1319	 * the Mini-A end of an OTG cable.  (Or something nonstandard
1320	 * like MiniB-to-StandardB, maybe built with a gender mender.)
1321	 */
1322	isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
1323
1324	dump_regs(isp, __func__);
1325
1326	return 0;
1327
1328#else
1329	dev_dbg(&isp->client->dev, "host sessions not allowed\n");
1330	return -EINVAL;
1331#endif
1332
1333}
1334
1335static int
1336isp1301_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
1337{
1338	struct isp1301	*isp = container_of(otg->phy, struct isp1301, phy);
1339#ifndef	CONFIG_USB_OTG
1340	u32 l;
1341#endif
1342
1343	if (!otg || isp != the_transceiver)
1344		return -ENODEV;
1345
1346	if (!gadget) {
1347		omap_writew(0, OTG_IRQ_EN);
1348		if (!otg->default_a)
1349			enable_vbus_draw(isp, 0);
1350		usb_gadget_vbus_disconnect(otg->gadget);
1351		otg->gadget = NULL;
1352		power_down(isp);
1353		return 0;
1354	}
1355
1356#ifdef	CONFIG_USB_OTG
1357	otg->gadget = gadget;
1358	dev_dbg(&isp->client->dev, "registered gadget\n");
1359	/* gadget driver may be suspended until vbus_connect () */
1360	if (otg->host)
1361		return isp1301_otg_enable(isp);
1362	return 0;
1363
1364#elif	!defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
1365	otg->gadget = gadget;
1366	// FIXME update its refcount
1367
1368	l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1369	l &= ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS);
1370	l |= OTG_ID;
1371	omap_writel(l, OTG_CTRL);
1372
1373	power_up(isp);
1374	isp->phy.state = OTG_STATE_B_IDLE;
1375
1376	if (machine_is_omap_h2() || machine_is_omap_h3())
1377		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1378
1379	isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
1380		INTR_SESS_VLD);
1381	isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
1382		INTR_VBUS_VLD);
1383	dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
1384	dump_regs(isp, __func__);
1385
1386	/* If this has a Mini-AB connector, this mode is highly
1387	 * nonstandard ... but can be handy for testing, so long
1388	 * as you don't plug a Mini-A cable into the jack.
1389	 */
1390	if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
1391		b_peripheral(isp);
1392
1393	return 0;
1394
1395#else
1396	dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
1397	return -EINVAL;
1398#endif
1399}
1400
1401
1402/*-------------------------------------------------------------------------*/
1403
1404static int
1405isp1301_set_power(struct usb_phy *dev, unsigned mA)
1406{
1407	if (!the_transceiver)
1408		return -ENODEV;
1409	if (dev->state == OTG_STATE_B_PERIPHERAL)
1410		enable_vbus_draw(the_transceiver, mA);
1411	return 0;
1412}
1413
1414static int
1415isp1301_start_srp(struct usb_otg *otg)
1416{
1417	struct isp1301	*isp = container_of(otg->phy, struct isp1301, phy);
1418	u32		otg_ctrl;
1419
1420	if (!otg || isp != the_transceiver
1421			|| isp->phy.state != OTG_STATE_B_IDLE)
1422		return -ENODEV;
1423
1424	otg_ctrl = omap_readl(OTG_CTRL);
1425	if (!(otg_ctrl & OTG_BSESSEND))
1426		return -EINVAL;
1427
1428	otg_ctrl |= OTG_B_BUSREQ;
1429	otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
1430	omap_writel(otg_ctrl, OTG_CTRL);
1431	isp->phy.state = OTG_STATE_B_SRP_INIT;
1432
1433	pr_debug("otg: SRP, %s ... %06x\n", state_name(isp),
1434			omap_readl(OTG_CTRL));
1435#ifdef	CONFIG_USB_OTG
1436	check_state(isp, __func__);
1437#endif
1438	return 0;
1439}
1440
1441static int
1442isp1301_start_hnp(struct usb_otg *otg)
1443{
1444#ifdef	CONFIG_USB_OTG
1445	struct isp1301	*isp = container_of(otg->phy, struct isp1301, phy);
1446	u32 l;
1447
1448	if (!otg || isp != the_transceiver)
1449		return -ENODEV;
1450	if (otg->default_a && (otg->host == NULL || !otg->host->b_hnp_enable))
 
1451		return -ENOTCONN;
1452	if (!otg->default_a && (otg->gadget == NULL
1453			|| !otg->gadget->b_hnp_enable))
1454		return -ENOTCONN;
1455
1456	/* We want hardware to manage most HNP protocol timings.
1457	 * So do this part as early as possible...
1458	 */
1459	switch (isp->phy.state) {
1460	case OTG_STATE_B_HOST:
1461		isp->phy.state = OTG_STATE_B_PERIPHERAL;
1462		/* caller will suspend next */
1463		break;
1464	case OTG_STATE_A_HOST:
1465#if 0
1466		/* autoconnect mode avoids irq latency bugs */
1467		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1468				MC1_BDIS_ACON_EN);
1469#endif
1470		/* caller must suspend then clear A_BUSREQ */
1471		usb_gadget_vbus_connect(otg->gadget);
1472		l = omap_readl(OTG_CTRL);
1473		l |= OTG_A_SETB_HNPEN;
1474		omap_writel(l, OTG_CTRL);
1475
1476		break;
1477	case OTG_STATE_A_PERIPHERAL:
1478		/* initiated by B-Host suspend */
1479		break;
1480	default:
1481		return -EILSEQ;
1482	}
1483	pr_debug("otg: HNP %s, %06x ...\n",
1484		state_name(isp), omap_readl(OTG_CTRL));
1485	check_state(isp, __func__);
1486	return 0;
1487#else
1488	/* srp-only */
1489	return -EINVAL;
1490#endif
1491}
1492
1493/*-------------------------------------------------------------------------*/
1494
1495static int __devinit
1496isp1301_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1497{
1498	int			status;
1499	struct isp1301		*isp;
1500
1501	if (the_transceiver)
1502		return 0;
1503
1504	isp = kzalloc(sizeof *isp, GFP_KERNEL);
1505	if (!isp)
1506		return 0;
1507
1508	isp->phy.otg = kzalloc(sizeof *isp->phy.otg, GFP_KERNEL);
1509	if (!isp->phy.otg) {
1510		kfree(isp);
1511		return 0;
1512	}
1513
1514	INIT_WORK(&isp->work, isp1301_work);
1515	init_timer(&isp->timer);
1516	isp->timer.function = isp1301_timer;
1517	isp->timer.data = (unsigned long) isp;
1518
1519	i2c_set_clientdata(i2c, isp);
1520	isp->client = i2c;
1521
1522	/* verify the chip (shouldn't be necessary) */
1523	status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1524	if (status != I2C_VENDOR_ID_PHILIPS) {
1525		dev_dbg(&i2c->dev, "not philips id: %d\n", status);
1526		goto fail;
1527	}
1528	status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1529	if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1530		dev_dbg(&i2c->dev, "not isp1301, %d\n", status);
1531		goto fail;
1532	}
1533	isp->i2c_release = i2c->dev.release;
1534	i2c->dev.release = isp1301_release;
1535
1536	/* initial development used chiprev 2.00 */
1537	status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
1538	dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
1539		status >> 8, status & 0xff);
1540
1541	/* make like power-on reset */
1542	isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
1543
1544	isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
1545	isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
1546
1547	isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1548				OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
1549	isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1550				~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
1551
1552	isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
1553	isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1554	isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1555
1556#ifdef	CONFIG_USB_OTG
1557	status = otg_bind(isp);
1558	if (status < 0) {
1559		dev_dbg(&i2c->dev, "can't bind OTG\n");
1560		goto fail;
1561	}
1562#endif
1563
1564	if (machine_is_omap_h2()) {
1565		/* full speed signaling by default */
1566		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1567			MC1_SPEED);
1568		isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
1569			MC2_SPD_SUSP_CTRL);
1570
1571		/* IRQ wired at M14 */
1572		omap_cfg_reg(M14_1510_GPIO2);
1573		if (gpio_request(2, "isp1301") == 0)
1574			gpio_direction_input(2);
1575		isp->irq_type = IRQF_TRIGGER_FALLING;
1576	}
1577
1578	isp->irq_type |= IRQF_SAMPLE_RANDOM;
1579	status = request_irq(i2c->irq, isp1301_irq,
1580			isp->irq_type, DRIVER_NAME, isp);
1581	if (status < 0) {
1582		dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
1583				i2c->irq, status);
1584		goto fail;
1585	}
1586
1587	isp->phy.dev = &i2c->dev;
1588	isp->phy.label = DRIVER_NAME;
1589	isp->phy.set_power = isp1301_set_power,
1590
1591	isp->phy.otg->phy = &isp->phy;
1592	isp->phy.otg->set_host = isp1301_set_host,
1593	isp->phy.otg->set_peripheral = isp1301_set_peripheral,
1594	isp->phy.otg->start_srp = isp1301_start_srp,
1595	isp->phy.otg->start_hnp = isp1301_start_hnp,
1596
1597	enable_vbus_draw(isp, 0);
1598	power_down(isp);
1599	the_transceiver = isp;
1600
1601#ifdef	CONFIG_USB_OTG
1602	update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
1603	update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
1604#endif
1605
1606	dump_regs(isp, __func__);
1607
1608#ifdef	VERBOSE
1609	mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1610	dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
1611#endif
1612
1613	status = usb_set_transceiver(&isp->phy);
1614	if (status < 0)
1615		dev_err(&i2c->dev, "can't register transceiver, %d\n",
1616			status);
1617
1618	return 0;
1619
1620fail:
1621	kfree(isp->phy.otg);
1622	kfree(isp);
1623	return -ENODEV;
1624}
1625
1626static const struct i2c_device_id isp1301_id[] = {
1627	{ "isp1301_omap", 0 },
1628	{ }
1629};
1630MODULE_DEVICE_TABLE(i2c, isp1301_id);
1631
1632static struct i2c_driver isp1301_driver = {
1633	.driver = {
1634		.name	= "isp1301_omap",
1635	},
1636	.probe		= isp1301_probe,
1637	.remove		= __exit_p(isp1301_remove),
1638	.id_table	= isp1301_id,
1639};
1640
1641/*-------------------------------------------------------------------------*/
1642
1643static int __init isp_init(void)
1644{
1645	return i2c_add_driver(&isp1301_driver);
1646}
1647subsys_initcall(isp_init);
1648
1649static void __exit isp_exit(void)
1650{
1651	if (the_transceiver)
1652		usb_set_transceiver(NULL);
1653	i2c_del_driver(&isp1301_driver);
1654}
1655module_exit(isp_exit);
1656