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