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