Linux Audio

Check our new training course

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