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