blob: e25700f44b6f603650d1057e1674d7a8617cff25 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010027#include <linux/platform_device.h>
David Brownelld1846b02008-11-28 15:24:38 +010028#include <linux/gpio.h>
David Brownell5f848132006-12-16 15:34:53 -080029#include <linux/usb/ch9.h>
David Brownell187426e2007-12-12 13:45:25 +010030#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/usb.h>
David Brownell3a16f7b2006-06-29 12:27:23 -070032#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/i2c.h>
34#include <linux/workqueue.h>
35
36#include <asm/irq.h>
David Brownelld1846b02008-11-28 15:24:38 +010037#include <asm/mach-types.h>
38
Tony Lindgrence491cf2009-10-20 09:40:47 -070039#include <plat/usb.h>
40#include <plat/mux.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42
43#ifndef DEBUG
44#undef VERBOSE
45#endif
46
47
48#define DRIVER_VERSION "24 August 2004"
David Brownell25da3832007-08-14 18:37:14 +020049#define DRIVER_NAME (isp1301_driver.driver.name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
52MODULE_LICENSE("GPL");
53
54struct isp1301 {
55 struct otg_transceiver otg;
Felipe Balbia5f08a32008-10-14 17:30:02 +020056 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 void (*i2c_release)(struct device *dev);
58
David Brownell25da3832007-08-14 18:37:14 +020059 int irq_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
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;
David Brownell25da3832007-08-14 18:37:14 +020068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 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
Tony Lindgrenf35ae632008-07-03 12:24:43 +030078/* bits in OTG_CTRL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* board-specific PM hooks */
96
David Brownelld1846b02008-11-28 15:24:38 +010097#if defined(CONFIG_MACH_OMAP_H2) || defined(CONFIG_MACH_OMAP_H3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#if defined(CONFIG_TPS65010) || defined(CONFIG_TPS65010_MODULE)
100
David Brownell6d16bfb2008-01-27 18:14:49 +0100101#include <linux/i2c/tps65010.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
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
Anand Gadiyard77282c2009-08-24 20:14:45 +0530120#else
David Brownelld1846b02008-11-28 15:24:38 +0100121
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
Anand Gadiyard77282c2009-08-24 20:14:45 +0530130#endif
131
David Brownelld1846b02008-11-28 15:24:38 +0100132static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*-------------------------------------------------------------------------*/
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static 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{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200156 return i2c_smbus_read_byte_data(isp->client, reg + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static inline int
160isp1301_get_u16(struct isp1301 *isp, u8 reg)
161{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200162 return i2c_smbus_read_word_data(isp->client, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static inline int
166isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
167{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200168 return i2c_smbus_write_byte_data(isp->client, reg + 0, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static inline int
172isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
173{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200174 return i2c_smbus_write_byte_data(isp->client, reg + 1, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
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 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300189# define MC1_SPEED (1 << 0)
190# define MC1_SUSPEND (1 << 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191# 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/*-------------------------------------------------------------------------*/
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);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300277 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
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);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300286 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
David Brownell25da3832007-08-14 18:37:14 +0200287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* 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 */
David Brownelld1846b02008-11-28 15:24:38 +0100346static void isp1301_defer_work(struct isp1301 *isp, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 int status;
349
350 if (isp && !test_and_set_bit(work, &isp->todo)) {
Felipe Balbia5f08a32008-10-14 17:30:02 +0200351 (void) get_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 status = schedule_work(&isp->work);
353 if (!status && !isp->working)
Felipe Balbia5f08a32008-10-14 17:30:02 +0200354 dev_vdbg(&isp->client->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 "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{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300362 u32 l;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300377 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
378 omap_writel(l, OTG_CTRL);
379 isp->last_otg_ctrl = l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 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{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300386 u32 l;
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300401 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
402 omap_writel(l, OTG_CTRL);
403 isp->last_otg_ctrl = l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 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",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300416 omap_readl(OTG_CTRL), label, state_name(isp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300439 u8 fsm = omap_readw(OTG_TEST) & 0x0ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 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,
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300504 state_string(state), fsm, state_name(isp),
505 omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
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
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300519 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 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 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300545 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548/* outputs from ISP1301_OTG_STATUS */
549static void update_otg2(struct isp1301 *isp, u8 otg_status)
550{
551 u32 otg_ctrl;
552
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300553 otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
554 otg_ctrl &= ~OTG_XCEIV_INPUTS;
555 otg_ctrl &= ~(OTG_BSESSVLD | OTG_BSESSEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300560 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
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
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300569 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 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 /* FIXME usbcore thinks port power is still on ... */
627 clr |= OTG1_VBUS_DRV;
628 break;
629 case OTG_STATE_A_IDLE:
630 if (otg_ctrl & OTG_DRV_VBUS) {
631 isp->otg.state = OTG_STATE_A_WAIT_VRISE;
632 pr_debug(" --> a_wait_vrise\n");
633 }
634 /* FALLTHROUGH */
635 default:
636 toggle(OTG_DRV_VBUS, OTG1_VBUS_DRV);
637 }
638
639 toggle(OTG_PU_VBUS, OTG1_VBUS_CHRG);
640 toggle(OTG_PD_VBUS, OTG1_VBUS_DISCHRG);
641
642# undef toggle
643
644 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, set);
645 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, clr);
646
647 /* HNP switch to host or peripheral; and SRP */
648 if (otg_change & OTG_PULLUP) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300649 u32 l;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 switch (isp->otg.state) {
652 case OTG_STATE_B_IDLE:
653 if (clr & OTG1_DP_PULLUP)
654 break;
655 isp->otg.state = OTG_STATE_B_PERIPHERAL;
656 pr_debug(" --> b_peripheral\n");
657 break;
658 case OTG_STATE_A_SUSPEND:
659 if (clr & OTG1_DP_PULLUP)
660 break;
661 isp->otg.state = OTG_STATE_A_PERIPHERAL;
662 pr_debug(" --> a_peripheral\n");
663 break;
664 default:
665 break;
666 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300667 l = omap_readl(OTG_CTRL);
668 l |= OTG_PULLUP;
669 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
671
Harvey Harrison08882d22008-04-22 22:16:47 +0200672 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 dump_regs(isp, "otg->isp1301");
674}
675
David Howells7d12e782006-10-05 14:55:46 +0100676static irqreturn_t omap_otg_irq(int irq, void *_isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300678 u16 otg_irq = omap_readw(OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 u32 otg_ctrl;
680 int ret = IRQ_NONE;
681 struct isp1301 *isp = _isp;
682
683 /* update ISP1301 transciever from OTG controller */
684 if (otg_irq & OPRT_CHG) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300685 omap_writew(OPRT_CHG, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 isp1301_defer_work(isp, WORK_UPDATE_ISP);
687 ret = IRQ_HANDLED;
688
689 /* SRP to become b_peripheral failed */
690 } else if (otg_irq & B_SRP_TMROUT) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300691 pr_debug("otg: B_SRP_TIMEOUT, %06x\n", omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 notresponding(isp);
693
694 /* gadget drivers that care should monitor all kinds of
695 * remote wakeup (SRP, normal) using their own timer
696 * to give "check cable and A-device" messages.
697 */
698 if (isp->otg.state == OTG_STATE_B_SRP_INIT)
699 b_idle(isp, "srp_timeout");
700
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300701 omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ret = IRQ_HANDLED;
703
704 /* HNP to become b_host failed */
705 } else if (otg_irq & B_HNP_FAIL) {
706 pr_debug("otg: %s B_HNP_FAIL, %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300707 state_name(isp), omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 notresponding(isp);
709
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300710 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 otg_ctrl |= OTG_BUSDROP;
712 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300713 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 /* subset of b_peripheral()... */
716 isp->otg.state = OTG_STATE_B_PERIPHERAL;
717 pr_debug(" --> b_peripheral\n");
718
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300719 omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ret = IRQ_HANDLED;
721
722 /* detect SRP from B-device ... */
723 } else if (otg_irq & A_SRP_DETECT) {
724 pr_debug("otg: %s SRP_DETECT, %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300725 state_name(isp), omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 isp1301_defer_work(isp, WORK_UPDATE_OTG);
728 switch (isp->otg.state) {
729 case OTG_STATE_A_IDLE:
730 if (!isp->otg.host)
731 break;
732 isp1301_defer_work(isp, WORK_HOST_RESUME);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300733 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 otg_ctrl |= OTG_A_BUSREQ;
735 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
736 & ~OTG_XCEIV_INPUTS
737 & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300738 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
740 default:
741 break;
742 }
743
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300744 omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 ret = IRQ_HANDLED;
746
747 /* timer expired: T(a_wait_bcon) and maybe T(a_wait_vrise)
748 * we don't track them separately
749 */
750 } else if (otg_irq & A_REQ_TMROUT) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300751 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 pr_info("otg: BCON_TMOUT from %s, %06x\n",
753 state_name(isp), otg_ctrl);
754 notresponding(isp);
755
756 otg_ctrl |= OTG_BUSDROP;
757 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300758 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 isp->otg.state = OTG_STATE_A_WAIT_VFALL;
760
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300761 omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 ret = IRQ_HANDLED;
763
764 /* A-supplied voltage fell too low; overcurrent */
765 } else if (otg_irq & A_VBUS_ERR) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300766 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 printk(KERN_ERR "otg: %s, VBUS_ERR %04x ctrl %06x\n",
768 state_name(isp), otg_irq, otg_ctrl);
769
770 otg_ctrl |= OTG_BUSDROP;
771 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300772 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 isp->otg.state = OTG_STATE_A_VBUS_ERR;
774
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300775 omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 ret = IRQ_HANDLED;
777
778 /* switch driver; the transciever code activates it,
779 * ungating the udc clock or resuming OHCI.
780 */
781 } else if (otg_irq & DRIVER_SWITCH) {
782 int kick = 0;
783
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300784 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 printk(KERN_NOTICE "otg: %s, SWITCH to %s, ctrl %06x\n",
786 state_name(isp),
787 (otg_ctrl & OTG_DRIVER_SEL)
788 ? "gadget" : "host",
789 otg_ctrl);
790 isp1301_defer_work(isp, WORK_UPDATE_ISP);
791
792 /* role is peripheral */
793 if (otg_ctrl & OTG_DRIVER_SEL) {
794 switch (isp->otg.state) {
795 case OTG_STATE_A_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200796 b_idle(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 break;
798 default:
799 break;
800 }
801 isp1301_defer_work(isp, WORK_UPDATE_ISP);
802
803 /* role is host */
804 } else {
805 if (!(otg_ctrl & OTG_ID)) {
David Brownell25da3832007-08-14 18:37:14 +0200806 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300807 omap_writel(otg_ctrl | OTG_A_BUSREQ, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 if (isp->otg.host) {
811 switch (isp->otg.state) {
812 case OTG_STATE_B_WAIT_ACON:
813 isp->otg.state = OTG_STATE_B_HOST;
814 pr_debug(" --> b_host\n");
815 kick = 1;
816 break;
817 case OTG_STATE_A_WAIT_BCON:
818 isp->otg.state = OTG_STATE_A_HOST;
819 pr_debug(" --> a_host\n");
820 break;
821 case OTG_STATE_A_PERIPHERAL:
822 isp->otg.state = OTG_STATE_A_WAIT_BCON;
823 pr_debug(" --> a_wait_bcon\n");
824 break;
825 default:
826 break;
827 }
828 isp1301_defer_work(isp, WORK_HOST_RESUME);
829 }
830 }
831
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300832 omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ret = IRQ_HANDLED;
834
835 if (kick)
836 usb_bus_start_enum(isp->otg.host,
837 isp->otg.host->otg_port);
838 }
839
Harvey Harrison08882d22008-04-22 22:16:47 +0200840 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return ret;
842}
843
844static struct platform_device *otg_dev;
845
Felipe Balbi465f8292009-12-15 23:19:52 +0200846static int isp1301_otg_init(struct isp1301 *isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300848 u32 l;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!otg_dev)
851 return -ENODEV;
852
Harvey Harrison08882d22008-04-22 22:16:47 +0200853 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* some of these values are board-specific... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300855 l = omap_readl(OTG_SYSCON_2);
856 l |= OTG_EN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 /* for B-device: */
858 | SRP_GPDATA /* 9msec Bdev D+ pulse */
859 | SRP_GPDVBUS /* discharge after VBUS pulse */
860 // | (3 << 24) /* 2msec VBUS pulse */
861 /* for A-device: */
862 | (0 << 20) /* 200ms nominal A_WAIT_VRISE timer */
863 | SRP_DPW /* detect 167+ns SRP pulses */
864 | SRP_DATA | SRP_VBUS /* accept both kinds of SRP pulse */
865 ;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300866 omap_writel(l, OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
869 update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
870
Harvey Harrison08882d22008-04-22 22:16:47 +0200871 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 pr_debug("otg: %s, %s %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300873 state_name(isp), __func__, omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300875 omap_writew(DRIVER_SWITCH | OPRT_CHG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 | B_SRP_TMROUT | B_HNP_FAIL
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300877 | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT, OTG_IRQ_EN);
878
879 l = omap_readl(OTG_SYSCON_2);
880 l |= OTG_EN;
881 omap_writel(l, OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 return 0;
884}
885
Russell King3ae5eae2005-11-09 22:32:44 +0000886static int otg_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 // struct omap_usb_config *config = dev->platform_data;
889
Russell King3ae5eae2005-11-09 22:32:44 +0000890 otg_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return 0;
892}
893
Russell King3ae5eae2005-11-09 22:32:44 +0000894static int otg_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
David Brownelld1846b02008-11-28 15:24:38 +0100896 otg_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
898}
899
David Brownelld1846b02008-11-28 15:24:38 +0100900static struct platform_driver omap_otg_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 .probe = otg_probe,
Russell King3ae5eae2005-11-09 22:32:44 +0000902 .remove = otg_remove,
903 .driver = {
904 .owner = THIS_MODULE,
905 .name = "omap_otg",
906 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907};
908
909static int otg_bind(struct isp1301 *isp)
910{
911 int status;
912
913 if (otg_dev)
914 return -EBUSY;
915
Russell King3ae5eae2005-11-09 22:32:44 +0000916 status = platform_driver_register(&omap_otg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (status < 0)
918 return status;
919
920 if (otg_dev)
921 status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700922 IRQF_DISABLED, DRIVER_NAME, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 else
924 status = -ENODEV;
925
926 if (status < 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000927 platform_driver_unregister(&omap_otg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return status;
929}
930
931static void otg_unbind(struct isp1301 *isp)
932{
933 if (!otg_dev)
934 return;
935 free_irq(otg_dev->resource[1].start, isp);
936}
937
938#else
939
940/* OTG controller isn't clocked */
941
942#endif /* CONFIG_USB_OTG */
943
944/*-------------------------------------------------------------------------*/
945
946static void b_peripheral(struct isp1301 *isp)
947{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300948 u32 l;
949
950 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
951 omap_writel(l, OTG_CTRL);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 usb_gadget_vbus_connect(isp->otg.gadget);
954
955#ifdef CONFIG_USB_OTG
956 enable_vbus_draw(isp, 8);
957 otg_update_isp(isp);
958#else
959 enable_vbus_draw(isp, 100);
960 /* UDC driver just set OTG_BSESSVLD */
961 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
962 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
963 isp->otg.state = OTG_STATE_B_PERIPHERAL;
964 pr_debug(" --> b_peripheral\n");
965 dump_regs(isp, "2periph");
966#endif
967}
968
969static void isp_update_otg(struct isp1301 *isp, u8 stat)
970{
971 u8 isp_stat, isp_bstat;
972 enum usb_otg_state state = isp->otg.state;
973
974 if (stat & INTR_BDIS_ACON)
975 pr_debug("OTG: BDIS_ACON, %s\n", state_name(isp));
976
977 /* start certain state transitions right away */
978 isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
979 if (isp_stat & INTR_ID_GND) {
980 if (isp->otg.default_a) {
981 switch (state) {
982 case OTG_STATE_B_IDLE:
983 a_idle(isp, "idle");
984 /* FALLTHROUGH */
985 case OTG_STATE_A_IDLE:
986 enable_vbus_source(isp);
987 /* FALLTHROUGH */
988 case OTG_STATE_A_WAIT_VRISE:
989 /* we skip over OTG_STATE_A_WAIT_BCON, since
990 * the HC will transition to A_HOST (or
991 * A_SUSPEND!) without our noticing except
992 * when HNP is used.
993 */
994 if (isp_stat & INTR_VBUS_VLD)
995 isp->otg.state = OTG_STATE_A_HOST;
996 break;
997 case OTG_STATE_A_WAIT_VFALL:
998 if (!(isp_stat & INTR_SESS_VLD))
999 a_idle(isp, "vfell");
1000 break;
1001 default:
1002 if (!(isp_stat & INTR_VBUS_VLD))
1003 isp->otg.state = OTG_STATE_A_VBUS_ERR;
1004 break;
1005 }
1006 isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
1007 } else {
1008 switch (state) {
1009 case OTG_STATE_B_PERIPHERAL:
1010 case OTG_STATE_B_HOST:
1011 case OTG_STATE_B_WAIT_ACON:
1012 usb_gadget_vbus_disconnect(isp->otg.gadget);
1013 break;
1014 default:
1015 break;
1016 }
1017 if (state != OTG_STATE_A_IDLE)
1018 a_idle(isp, "id");
1019 if (isp->otg.host && state == OTG_STATE_A_IDLE)
1020 isp1301_defer_work(isp, WORK_HOST_RESUME);
1021 isp_bstat = 0;
1022 }
1023 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001024 u32 l;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /* if user unplugged mini-A end of cable,
1027 * don't bypass A_WAIT_VFALL.
1028 */
1029 if (isp->otg.default_a) {
1030 switch (state) {
1031 default:
1032 isp->otg.state = OTG_STATE_A_WAIT_VFALL;
1033 break;
1034 case OTG_STATE_A_WAIT_VFALL:
1035 state = OTG_STATE_A_IDLE;
1036 /* khubd may take a while to notice and
1037 * handle this disconnect, so don't go
1038 * to B_IDLE quite yet.
1039 */
1040 break;
1041 case OTG_STATE_A_IDLE:
1042 host_suspend(isp);
1043 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
1044 MC1_BDIS_ACON_EN);
1045 isp->otg.state = OTG_STATE_B_IDLE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001046 l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1047 l &= ~OTG_CTRL_BITS;
1048 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
1050 case OTG_STATE_B_IDLE:
1051 break;
1052 }
1053 }
1054 isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
1055
1056 switch (isp->otg.state) {
1057 case OTG_STATE_B_PERIPHERAL:
1058 case OTG_STATE_B_WAIT_ACON:
1059 case OTG_STATE_B_HOST:
1060 if (likely(isp_bstat & OTG_B_SESS_VLD))
1061 break;
1062 enable_vbus_draw(isp, 0);
1063#ifndef CONFIG_USB_OTG
1064 /* UDC driver will clear OTG_BSESSVLD */
1065 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1066 OTG1_DP_PULLDOWN);
1067 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1068 OTG1_DP_PULLUP);
Harvey Harrison08882d22008-04-22 22:16:47 +02001069 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070#endif
1071 /* FALLTHROUGH */
1072 case OTG_STATE_B_SRP_INIT:
Harvey Harrison08882d22008-04-22 22:16:47 +02001073 b_idle(isp, __func__);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001074 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
1075 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /* FALLTHROUGH */
1077 case OTG_STATE_B_IDLE:
1078 if (isp->otg.gadget && (isp_bstat & OTG_B_SESS_VLD)) {
1079#ifdef CONFIG_USB_OTG
1080 update_otg1(isp, isp_stat);
1081 update_otg2(isp, isp_bstat);
1082#endif
1083 b_peripheral(isp);
1084 } else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
1085 isp_bstat |= OTG_B_SESS_END;
1086 break;
1087 case OTG_STATE_A_WAIT_VFALL:
1088 break;
1089 default:
1090 pr_debug("otg: unsupported b-device %s\n",
1091 state_name(isp));
1092 break;
1093 }
1094 }
1095
1096 if (state != isp->otg.state)
1097 pr_debug(" isp, %s -> %s\n",
1098 state_string(state), state_name(isp));
1099
1100#ifdef CONFIG_USB_OTG
1101 /* update the OTG controller state to match the isp1301; may
1102 * trigger OPRT_CHG irqs for changes going to the isp1301.
1103 */
1104 update_otg1(isp, isp_stat);
1105 update_otg2(isp, isp_bstat);
Harvey Harrison08882d22008-04-22 22:16:47 +02001106 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107#endif
1108
1109 dump_regs(isp, "isp1301->otg");
1110}
1111
1112/*-------------------------------------------------------------------------*/
1113
1114static u8 isp1301_clear_latch(struct isp1301 *isp)
1115{
1116 u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
1117 isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
1118 return latch;
1119}
1120
1121static void
David Brownell25da3832007-08-14 18:37:14 +02001122isp1301_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
David Brownell25da3832007-08-14 18:37:14 +02001124 struct isp1301 *isp = container_of(work, struct isp1301, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int stop;
1126
1127 /* implicit lock: we're the only task using this device */
1128 isp->working = 1;
1129 do {
1130 stop = test_bit(WORK_STOP, &isp->todo);
1131
1132#ifdef CONFIG_USB_OTG
1133 /* transfer state from otg engine to isp1301 */
1134 if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
1135 otg_update_isp(isp);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001136 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138#endif
1139 /* transfer state from isp1301 to otg engine */
1140 if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
1141 u8 stat = isp1301_clear_latch(isp);
1142
1143 isp_update_otg(isp, stat);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001144 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
1148 u32 otg_ctrl;
1149
1150 /*
1151 * skip A_WAIT_VRISE; hc transitions invisibly
1152 * skip A_WAIT_BCON; same.
1153 */
1154 switch (isp->otg.state) {
1155 case OTG_STATE_A_WAIT_BCON:
1156 case OTG_STATE_A_WAIT_VRISE:
1157 isp->otg.state = OTG_STATE_A_HOST;
1158 pr_debug(" --> a_host\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001159 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 otg_ctrl |= OTG_A_BUSREQ;
1161 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
1162 & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001163 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
1165 case OTG_STATE_B_WAIT_ACON:
1166 isp->otg.state = OTG_STATE_B_HOST;
1167 pr_debug(" --> b_host (acon)\n");
1168 break;
1169 case OTG_STATE_B_HOST:
1170 case OTG_STATE_B_IDLE:
1171 case OTG_STATE_A_IDLE:
1172 break;
1173 default:
1174 pr_debug(" host resume in %s\n",
1175 state_name(isp));
1176 }
1177 host_resume(isp);
1178 // mdelay(10);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001179 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
1182 if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
1183#ifdef VERBOSE
1184 dump_regs(isp, "timer");
1185 if (!stop)
1186 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1187#endif
Felipe Balbia5f08a32008-10-14 17:30:02 +02001188 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
1190
1191 if (isp->todo)
Felipe Balbia5f08a32008-10-14 17:30:02 +02001192 dev_vdbg(&isp->client->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 "work done, todo = 0x%lx\n",
1194 isp->todo);
1195 if (stop) {
Felipe Balbia5f08a32008-10-14 17:30:02 +02001196 dev_dbg(&isp->client->dev, "stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
1198 }
1199 } while (isp->todo);
1200 isp->working = 0;
1201}
1202
David Howells7d12e782006-10-05 14:55:46 +01001203static irqreturn_t isp1301_irq(int irq, void *isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 isp1301_defer_work(isp, WORK_UPDATE_OTG);
1206 return IRQ_HANDLED;
1207}
1208
1209static void isp1301_timer(unsigned long _isp)
1210{
1211 isp1301_defer_work((void *)_isp, WORK_TIMER);
1212}
1213
1214/*-------------------------------------------------------------------------*/
1215
1216static void isp1301_release(struct device *dev)
1217{
1218 struct isp1301 *isp;
1219
Felipe Balbia5f08a32008-10-14 17:30:02 +02001220 isp = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
David Brownelld1846b02008-11-28 15:24:38 +01001222 /* FIXME -- not with a "new style" driver, it doesn't!! */
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
1225 if (isp->i2c_release)
1226 isp->i2c_release(dev);
1227 kfree (isp);
1228}
1229
1230static struct isp1301 *the_transceiver;
1231
Jean Delvare9df013b2008-10-14 17:30:02 +02001232static int __exit isp1301_remove(struct i2c_client *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 struct isp1301 *isp;
1235
Felipe Balbia5f08a32008-10-14 17:30:02 +02001236 isp = i2c_get_clientdata(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1239 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
Jean Delvare9df013b2008-10-14 17:30:02 +02001240 free_irq(i2c->irq, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241#ifdef CONFIG_USB_OTG
1242 otg_unbind(isp);
1243#endif
1244 if (machine_is_omap_h2())
David Brownelld1846b02008-11-28 15:24:38 +01001245 gpio_free(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 isp->timer.data = 0;
1248 set_bit(WORK_STOP, &isp->todo);
1249 del_timer_sync(&isp->timer);
Tejun Heo569ff2d2010-12-24 16:14:20 +01001250 flush_work_sync(&isp->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 put_device(&i2c->dev);
David Brownelld1846b02008-11-28 15:24:38 +01001253 the_transceiver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Jean Delvare9df013b2008-10-14 17:30:02 +02001255 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
1258/*-------------------------------------------------------------------------*/
1259
1260/* NOTE: three modes are possible here, only one of which
1261 * will be standards-conformant on any given system:
1262 *
1263 * - OTG mode (dual-role), required if there's a Mini-AB connector
1264 * - HOST mode, for when there's one or more A (host) connectors
1265 * - DEVICE mode, for when there's a B/Mini-B (device) connector
1266 *
1267 * As a rule, you won't have an isp1301 chip unless it's there to
David Brownell25da3832007-08-14 18:37:14 +02001268 * support the OTG mode. Other modes help testing USB controllers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * in isolation from (full) OTG support, or maybe so later board
1270 * revisions can help to support those feature.
1271 */
1272
1273#ifdef CONFIG_USB_OTG
1274
1275static int isp1301_otg_enable(struct isp1301 *isp)
1276{
1277 power_up(isp);
Felipe Balbi465f8292009-12-15 23:19:52 +02001278 isp1301_otg_init(isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /* NOTE: since we don't change this, this provides
1281 * a few more interrupts than are strictly needed.
1282 */
1283 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001284 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001286 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Felipe Balbia5f08a32008-10-14 17:30:02 +02001288 dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 return 0;
1291}
1292
1293#endif
1294
1295/* add or disable the host device+driver */
1296static int
1297isp1301_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1298{
1299 struct isp1301 *isp = container_of(otg, struct isp1301, otg);
1300
1301 if (!otg || isp != the_transceiver)
1302 return -ENODEV;
1303
1304 if (!host) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001305 omap_writew(0, OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 power_down(isp);
David Brownelld1846b02008-11-28 15:24:38 +01001307 isp->otg.host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return 0;
1309 }
1310
1311#ifdef CONFIG_USB_OTG
1312 isp->otg.host = host;
Felipe Balbia5f08a32008-10-14 17:30:02 +02001313 dev_dbg(&isp->client->dev, "registered host\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 host_suspend(isp);
1315 if (isp->otg.gadget)
1316 return isp1301_otg_enable(isp);
1317 return 0;
1318
1319#elif !defined(CONFIG_USB_GADGET_OMAP)
1320 // FIXME update its refcount
1321 isp->otg.host = host;
1322
1323 power_up(isp);
1324
1325 if (machine_is_omap_h2())
1326 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1327
Felipe Balbia5f08a32008-10-14 17:30:02 +02001328 dev_info(&isp->client->dev, "A-Host sessions ok\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001330 INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001332 INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* If this has a Mini-AB connector, this mode is highly
1335 * nonstandard ... but can be handy for testing, especially with
1336 * the Mini-A end of an OTG cable. (Or something nonstandard
1337 * like MiniB-to-StandardB, maybe built with a gender mender.)
1338 */
1339 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
1340
Harvey Harrison08882d22008-04-22 22:16:47 +02001341 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 return 0;
1344
1345#else
Felipe Balbia5f08a32008-10-14 17:30:02 +02001346 dev_dbg(&isp->client->dev, "host sessions not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EINVAL;
1348#endif
1349
1350}
1351
1352static int
1353isp1301_set_peripheral(struct otg_transceiver *otg, struct usb_gadget *gadget)
1354{
1355 struct isp1301 *isp = container_of(otg, struct isp1301, otg);
David Brownelld1846b02008-11-28 15:24:38 +01001356#ifndef CONFIG_USB_OTG
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001357 u32 l;
David Brownelld1846b02008-11-28 15:24:38 +01001358#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 if (!otg || isp != the_transceiver)
1361 return -ENODEV;
1362
1363 if (!gadget) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001364 omap_writew(0, OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 if (!isp->otg.default_a)
1366 enable_vbus_draw(isp, 0);
1367 usb_gadget_vbus_disconnect(isp->otg.gadget);
David Brownelld1846b02008-11-28 15:24:38 +01001368 isp->otg.gadget = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 power_down(isp);
1370 return 0;
1371 }
1372
1373#ifdef CONFIG_USB_OTG
1374 isp->otg.gadget = gadget;
Felipe Balbia5f08a32008-10-14 17:30:02 +02001375 dev_dbg(&isp->client->dev, "registered gadget\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* gadget driver may be suspended until vbus_connect () */
1377 if (isp->otg.host)
1378 return isp1301_otg_enable(isp);
1379 return 0;
1380
1381#elif !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
1382 isp->otg.gadget = gadget;
1383 // FIXME update its refcount
1384
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001385 l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1386 l &= ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS);
1387 l |= OTG_ID;
1388 omap_writel(l, OTG_CTRL);
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 power_up(isp);
1391 isp->otg.state = OTG_STATE_B_IDLE;
1392
David Brownelld1846b02008-11-28 15:24:38 +01001393 if (machine_is_omap_h2() || machine_is_omap_h3())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1395
1396 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001397 INTR_SESS_VLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001399 INTR_VBUS_VLD);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001400 dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
Harvey Harrison08882d22008-04-22 22:16:47 +02001401 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 /* If this has a Mini-AB connector, this mode is highly
1404 * nonstandard ... but can be handy for testing, so long
1405 * as you don't plug a Mini-A cable into the jack.
1406 */
1407 if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
1408 b_peripheral(isp);
1409
1410 return 0;
1411
1412#else
Felipe Balbia5f08a32008-10-14 17:30:02 +02001413 dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EINVAL;
1415#endif
1416}
1417
1418
1419/*-------------------------------------------------------------------------*/
1420
1421static int
1422isp1301_set_power(struct otg_transceiver *dev, unsigned mA)
1423{
1424 if (!the_transceiver)
1425 return -ENODEV;
1426 if (dev->state == OTG_STATE_B_PERIPHERAL)
1427 enable_vbus_draw(the_transceiver, mA);
1428 return 0;
1429}
1430
1431static int
1432isp1301_start_srp(struct otg_transceiver *dev)
1433{
1434 struct isp1301 *isp = container_of(dev, struct isp1301, otg);
1435 u32 otg_ctrl;
1436
1437 if (!dev || isp != the_transceiver
1438 || isp->otg.state != OTG_STATE_B_IDLE)
1439 return -ENODEV;
1440
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001441 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (!(otg_ctrl & OTG_BSESSEND))
1443 return -EINVAL;
1444
1445 otg_ctrl |= OTG_B_BUSREQ;
1446 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001447 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 isp->otg.state = OTG_STATE_B_SRP_INIT;
1449
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001450 pr_debug("otg: SRP, %s ... %06x\n", state_name(isp),
1451 omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452#ifdef CONFIG_USB_OTG
Harvey Harrison08882d22008-04-22 22:16:47 +02001453 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454#endif
1455 return 0;
1456}
1457
1458static int
1459isp1301_start_hnp(struct otg_transceiver *dev)
1460{
1461#ifdef CONFIG_USB_OTG
1462 struct isp1301 *isp = container_of(dev, struct isp1301, otg);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001463 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 if (!dev || isp != the_transceiver)
1466 return -ENODEV;
1467 if (isp->otg.default_a && (isp->otg.host == NULL
1468 || !isp->otg.host->b_hnp_enable))
1469 return -ENOTCONN;
1470 if (!isp->otg.default_a && (isp->otg.gadget == NULL
1471 || !isp->otg.gadget->b_hnp_enable))
1472 return -ENOTCONN;
1473
1474 /* We want hardware to manage most HNP protocol timings.
1475 * So do this part as early as possible...
1476 */
1477 switch (isp->otg.state) {
1478 case OTG_STATE_B_HOST:
1479 isp->otg.state = OTG_STATE_B_PERIPHERAL;
1480 /* caller will suspend next */
1481 break;
1482 case OTG_STATE_A_HOST:
1483#if 0
1484 /* autoconnect mode avoids irq latency bugs */
1485 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1486 MC1_BDIS_ACON_EN);
1487#endif
1488 /* caller must suspend then clear A_BUSREQ */
1489 usb_gadget_vbus_connect(isp->otg.gadget);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001490 l = omap_readl(OTG_CTRL);
1491 l |= OTG_A_SETB_HNPEN;
1492 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 break;
1495 case OTG_STATE_A_PERIPHERAL:
1496 /* initiated by B-Host suspend */
1497 break;
1498 default:
1499 return -EILSEQ;
1500 }
1501 pr_debug("otg: HNP %s, %06x ...\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001502 state_name(isp), omap_readl(OTG_CTRL));
Harvey Harrison08882d22008-04-22 22:16:47 +02001503 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 return 0;
1505#else
1506 /* srp-only */
1507 return -EINVAL;
1508#endif
1509}
1510
1511/*-------------------------------------------------------------------------*/
1512
Uwe Kleine-Königa7e2a892011-02-09 21:40:10 +01001513static int __devinit
David Brownelld1846b02008-11-28 15:24:38 +01001514isp1301_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 int status;
1517 struct isp1301 *isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
1519 if (the_transceiver)
1520 return 0;
1521
Pekka Enberg82ca76b2005-09-06 15:18:35 -07001522 isp = kzalloc(sizeof *isp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (!isp)
1524 return 0;
1525
David Brownell25da3832007-08-14 18:37:14 +02001526 INIT_WORK(&isp->work, isp1301_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 init_timer(&isp->timer);
1528 isp->timer.function = isp1301_timer;
1529 isp->timer.data = (unsigned long) isp;
1530
Jean Delvare9df013b2008-10-14 17:30:02 +02001531 i2c_set_clientdata(i2c, isp);
1532 isp->client = i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001534 /* verify the chip (shouldn't be necessary) */
Jean Delvare9df013b2008-10-14 17:30:02 +02001535 status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1536 if (status != I2C_VENDOR_ID_PHILIPS) {
1537 dev_dbg(&i2c->dev, "not philips id: %d\n", status);
1538 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Jean Delvare9df013b2008-10-14 17:30:02 +02001540 status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1541 if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1542 dev_dbg(&i2c->dev, "not isp1301, %d\n", status);
1543 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
1545 isp->i2c_release = i2c->dev.release;
1546 i2c->dev.release = isp1301_release;
1547
1548 /* initial development used chiprev 2.00 */
1549 status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
1550 dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
1551 status >> 8, status & 0xff);
1552
1553 /* make like power-on reset */
1554 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
1555
1556 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
1557 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
1558
1559 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1560 OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
1561 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1562 ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
1563
1564 isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
1565 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1566 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1567
1568#ifdef CONFIG_USB_OTG
1569 status = otg_bind(isp);
1570 if (status < 0) {
1571 dev_dbg(&i2c->dev, "can't bind OTG\n");
Jean Delvare9df013b2008-10-14 17:30:02 +02001572 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 }
1574#endif
1575
1576 if (machine_is_omap_h2()) {
1577 /* full speed signaling by default */
1578 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
Dmitry Baryshkovcbbcb9b2008-08-07 16:29:24 +04001579 MC1_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
1581 MC2_SPD_SUSP_CTRL);
1582
1583 /* IRQ wired at M14 */
1584 omap_cfg_reg(M14_1510_GPIO2);
David Brownell25da3832007-08-14 18:37:14 +02001585 if (gpio_request(2, "isp1301") == 0)
1586 gpio_direction_input(2);
1587 isp->irq_type = IRQF_TRIGGER_FALLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589
David Brownell25da3832007-08-14 18:37:14 +02001590 isp->irq_type |= IRQF_SAMPLE_RANDOM;
Jean Delvare9df013b2008-10-14 17:30:02 +02001591 status = request_irq(i2c->irq, isp1301_irq,
David Brownell25da3832007-08-14 18:37:14 +02001592 isp->irq_type, DRIVER_NAME, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (status < 0) {
1594 dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
Jean Delvare9df013b2008-10-14 17:30:02 +02001595 i2c->irq, status);
1596 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598
Jean Delvare9df013b2008-10-14 17:30:02 +02001599 isp->otg.dev = &i2c->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 isp->otg.label = DRIVER_NAME;
1601
1602 isp->otg.set_host = isp1301_set_host,
1603 isp->otg.set_peripheral = isp1301_set_peripheral,
1604 isp->otg.set_power = isp1301_set_power,
1605 isp->otg.start_srp = isp1301_start_srp,
1606 isp->otg.start_hnp = isp1301_start_hnp,
1607
1608 enable_vbus_draw(isp, 0);
1609 power_down(isp);
1610 the_transceiver = isp;
1611
1612#ifdef CONFIG_USB_OTG
1613 update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
1614 update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
1615#endif
1616
Harvey Harrison08882d22008-04-22 22:16:47 +02001617 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
1619#ifdef VERBOSE
1620 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1621 dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
1622#endif
1623
1624 status = otg_set_transceiver(&isp->otg);
1625 if (status < 0)
1626 dev_err(&i2c->dev, "can't register transceiver, %d\n",
1627 status);
1628
1629 return 0;
Jean Delvare9df013b2008-10-14 17:30:02 +02001630
1631fail:
1632 kfree(isp);
1633 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Jean Delvare9df013b2008-10-14 17:30:02 +02001636static const struct i2c_device_id isp1301_id[] = {
1637 { "isp1301_omap", 0 },
1638 { }
1639};
1640MODULE_DEVICE_TABLE(i2c, isp1301_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642static struct i2c_driver isp1301_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +01001643 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +01001644 .name = "isp1301_omap",
1645 },
Jean Delvare9df013b2008-10-14 17:30:02 +02001646 .probe = isp1301_probe,
1647 .remove = __exit_p(isp1301_remove),
1648 .id_table = isp1301_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649};
1650
1651/*-------------------------------------------------------------------------*/
1652
1653static int __init isp_init(void)
1654{
1655 return i2c_add_driver(&isp1301_driver);
1656}
Viral Mehta94a82482010-04-06 11:51:00 +05301657subsys_initcall(isp_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659static void __exit isp_exit(void)
1660{
1661 if (the_transceiver)
David Brownelld1846b02008-11-28 15:24:38 +01001662 otg_set_transceiver(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 i2c_del_driver(&isp1301_driver);
1664}
1665module_exit(isp_exit);
1666