blob: 28902ebd553905aedd2507d32870d97ea7023ade [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * isp1301_omap - ISP 1301 USB transceiver, talking to OMAP OTG controller
3 *
4 * Copyright (C) 2004 Texas Instruments
5 * Copyright (C) 2004 David Brownell
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010027#include <linux/platform_device.h>
David Brownell5f848132006-12-16 15:34:53 -080028#include <linux/usb/ch9.h>
David Brownell187426e2007-12-12 13:45:25 +010029#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/usb.h>
David Brownell3a16f7b2006-06-29 12:27:23 -070031#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/i2c.h>
33#include <linux/workqueue.h>
34
35#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010036#include <mach/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38
39#ifndef DEBUG
40#undef VERBOSE
41#endif
42
43
44#define DRIVER_VERSION "24 August 2004"
David Brownell25da3832007-08-14 18:37:14 +020045#define DRIVER_NAME (isp1301_driver.driver.name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
48MODULE_LICENSE("GPL");
49
50struct isp1301 {
51 struct otg_transceiver otg;
Felipe Balbia5f08a32008-10-14 17:30:02 +020052 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 void (*i2c_release)(struct device *dev);
54
David Brownell25da3832007-08-14 18:37:14 +020055 int irq_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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;
David Brownell25da3832007-08-14 18:37:14 +020064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 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
Tony Lindgrenf35ae632008-07-03 12:24:43 +030074/* bits in OTG_CTRL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
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
David Brownell25da3832007-08-14 18:37:14 +020095#include <asm/gpio.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010096#include <mach/mux.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <asm/mach-types.h>
98
99
100#if defined(CONFIG_TPS65010) || defined(CONFIG_TPS65010_MODULE)
101
David Brownell6d16bfb2008-01-27 18:14:49 +0100102#include <linux/i2c/tps65010.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static 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{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200147 return i2c_smbus_read_byte_data(isp->client, reg + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150static inline int
151isp1301_get_u16(struct isp1301 *isp, u8 reg)
152{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200153 return i2c_smbus_read_word_data(isp->client, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156static inline int
157isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
158{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200159 return i2c_smbus_write_byte_data(isp->client, reg + 0, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162static inline int
163isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
164{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200165 return i2c_smbus_write_byte_data(isp->client, reg + 1, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
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 */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300180# define MC1_SPEED (1 << 0)
181# define MC1_SUSPEND (1 << 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182# 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/*-------------------------------------------------------------------------*/
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);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300268 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
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);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300277 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
David Brownell25da3832007-08-14 18:37:14 +0200278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 /* 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)) {
Felipe Balbia5f08a32008-10-14 17:30:02 +0200343 (void) get_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 status = schedule_work(&isp->work);
345 if (!status && !isp->working)
Felipe Balbia5f08a32008-10-14 17:30:02 +0200346 dev_vdbg(&isp->client->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 "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{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300354 u32 l;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300369 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
370 omap_writel(l, OTG_CTRL);
371 isp->last_otg_ctrl = l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 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{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300378 u32 l;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300393 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
394 omap_writel(l, OTG_CTRL);
395 isp->last_otg_ctrl = l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 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",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300408 omap_readl(OTG_CTRL), label, state_name(isp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300431 u8 fsm = omap_readw(OTG_TEST) & 0x0ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 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,
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300496 state_string(state), fsm, state_name(isp),
497 omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
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
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300511 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 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 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300538 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/* outputs from ISP1301_OTG_STATUS */
542static void update_otg2(struct isp1301 *isp, u8 otg_status)
543{
544 u32 otg_ctrl;
545
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300546 otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
547 otg_ctrl &= ~OTG_XCEIV_INPUTS;
548 otg_ctrl &= ~(OTG_BSESSVLD | OTG_BSESSEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300553 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
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
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300562 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 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) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300642 u32 l;
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 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 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300660 l = omap_readl(OTG_CTRL);
661 l |= OTG_PULLUP;
662 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Harvey Harrison08882d22008-04-22 22:16:47 +0200665 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 dump_regs(isp, "otg->isp1301");
667}
668
David Howells7d12e782006-10-05 14:55:46 +0100669static irqreturn_t omap_otg_irq(int irq, void *_isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300671 u16 otg_irq = omap_readw(OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 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) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300678 omap_writew(OPRT_CHG, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 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) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300684 pr_debug("otg: B_SRP_TIMEOUT, %06x\n", omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 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
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300694 omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 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",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300700 state_name(isp), omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 notresponding(isp);
702
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300703 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 otg_ctrl |= OTG_BUSDROP;
705 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300706 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 /* subset of b_peripheral()... */
709 isp->otg.state = OTG_STATE_B_PERIPHERAL;
710 pr_debug(" --> b_peripheral\n");
711
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300712 omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 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",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300718 state_name(isp), omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
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);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300726 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 otg_ctrl |= OTG_A_BUSREQ;
728 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
729 & ~OTG_XCEIV_INPUTS
730 & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300731 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 break;
733 default:
734 break;
735 }
736
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300737 omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 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) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300744 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300751 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 isp->otg.state = OTG_STATE_A_WAIT_VFALL;
753
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300754 omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 ret = IRQ_HANDLED;
756
757 /* A-supplied voltage fell too low; overcurrent */
758 } else if (otg_irq & A_VBUS_ERR) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300759 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300765 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 isp->otg.state = OTG_STATE_A_VBUS_ERR;
767
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300768 omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 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
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300777 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 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:
Harvey Harrison08882d22008-04-22 22:16:47 +0200789 b_idle(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 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)) {
David Brownell25da3832007-08-14 18:37:14 +0200799 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300800 omap_writel(otg_ctrl | OTG_A_BUSREQ, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
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
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300825 omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ret = IRQ_HANDLED;
827
828 if (kick)
829 usb_bus_start_enum(isp->otg.host,
830 isp->otg.host->otg_port);
831 }
832
Harvey Harrison08882d22008-04-22 22:16:47 +0200833 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return ret;
835}
836
837static struct platform_device *otg_dev;
838
839static int otg_init(struct isp1301 *isp)
840{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300841 u32 l;
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (!otg_dev)
844 return -ENODEV;
845
Harvey Harrison08882d22008-04-22 22:16:47 +0200846 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /* some of these values are board-specific... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300848 l = omap_readl(OTG_SYSCON_2);
849 l |= OTG_EN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* 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 ;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300859 omap_writel(l, OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
862 update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
863
Harvey Harrison08882d22008-04-22 22:16:47 +0200864 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 pr_debug("otg: %s, %s %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300866 state_name(isp), __func__, omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300868 omap_writew(DRIVER_SWITCH | OPRT_CHG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 | B_SRP_TMROUT | B_HNP_FAIL
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300870 | 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 return 0;
877}
878
Russell King3ae5eae2005-11-09 22:32:44 +0000879static int otg_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880{
881 // struct omap_usb_config *config = dev->platform_data;
882
Russell King3ae5eae2005-11-09 22:32:44 +0000883 otg_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 0;
885}
886
Russell King3ae5eae2005-11-09 22:32:44 +0000887static int otg_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 otg_dev = 0;
890 return 0;
891}
892
Russell King3ae5eae2005-11-09 22:32:44 +0000893struct platform_driver omap_otg_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .probe = otg_probe,
Russell King3ae5eae2005-11-09 22:32:44 +0000895 .remove = otg_remove,
896 .driver = {
897 .owner = THIS_MODULE,
898 .name = "omap_otg",
899 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900};
901
902static int otg_bind(struct isp1301 *isp)
903{
904 int status;
905
906 if (otg_dev)
907 return -EBUSY;
908
Russell King3ae5eae2005-11-09 22:32:44 +0000909 status = platform_driver_register(&omap_otg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (status < 0)
911 return status;
912
913 if (otg_dev)
914 status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700915 IRQF_DISABLED, DRIVER_NAME, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 else
917 status = -ENODEV;
918
919 if (status < 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000920 platform_driver_unregister(&omap_otg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 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{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300941 u32 l;
942
943 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
944 omap_writel(l, OTG_CTRL);
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 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 {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001017 u32 l;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 /* 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;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001039 l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1040 l &= ~OTG_CTRL_BITS;
1041 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 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);
Harvey Harrison08882d22008-04-22 22:16:47 +02001062 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063#endif
1064 /* FALLTHROUGH */
1065 case OTG_STATE_B_SRP_INIT:
Harvey Harrison08882d22008-04-22 22:16:47 +02001066 b_idle(isp, __func__);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001067 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
1068 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* 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);
Harvey Harrison08882d22008-04-22 22:16:47 +02001099 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100#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
David Brownell25da3832007-08-14 18:37:14 +02001115isp1301_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
David Brownell25da3832007-08-14 18:37:14 +02001117 struct isp1301 *isp = container_of(work, struct isp1301, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 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);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001129 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
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);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001137 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
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");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001152 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 otg_ctrl |= OTG_A_BUSREQ;
1154 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
1155 & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001156 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 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);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001172 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 }
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
Felipe Balbia5f08a32008-10-14 17:30:02 +02001181 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
1184 if (isp->todo)
Felipe Balbia5f08a32008-10-14 17:30:02 +02001185 dev_vdbg(&isp->client->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 "work done, todo = 0x%lx\n",
1187 isp->todo);
1188 if (stop) {
Felipe Balbia5f08a32008-10-14 17:30:02 +02001189 dev_dbg(&isp->client->dev, "stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 break;
1191 }
1192 } while (isp->todo);
1193 isp->working = 0;
1194}
1195
David Howells7d12e782006-10-05 14:55:46 +01001196static irqreturn_t isp1301_irq(int irq, void *isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
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
Felipe Balbia5f08a32008-10-14 17:30:02 +02001213 isp = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
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
Jean Delvare9df013b2008-10-14 17:30:02 +02001223static int __exit isp1301_remove(struct i2c_client *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 struct isp1301 *isp;
1226
Felipe Balbia5f08a32008-10-14 17:30:02 +02001227 isp = i2c_get_clientdata(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1230 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
Jean Delvare9df013b2008-10-14 17:30:02 +02001231 free_irq(i2c->irq, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232#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
Jean Delvare9df013b2008-10-14 17:30:02 +02001246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
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
David Brownell25da3832007-08-14 18:37:14 +02001259 * support the OTG mode. Other modes help testing USB controllers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 * 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,
David Brownell25da3832007-08-14 18:37:14 +02001275 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001277 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Felipe Balbia5f08a32008-10-14 17:30:02 +02001279 dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
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) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001296 omap_writew(0, OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 power_down(isp);
1298 isp->otg.host = 0;
1299 return 0;
1300 }
1301
1302#ifdef CONFIG_USB_OTG
1303 isp->otg.host = host;
Felipe Balbia5f08a32008-10-14 17:30:02 +02001304 dev_dbg(&isp->client->dev, "registered host\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 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
Felipe Balbia5f08a32008-10-14 17:30:02 +02001319 dev_info(&isp->client->dev, "A-Host sessions ok\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001321 INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001323 INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
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
Harvey Harrison08882d22008-04-22 22:16:47 +02001332 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 return 0;
1335
1336#else
Felipe Balbia5f08a32008-10-14 17:30:02 +02001337 dev_dbg(&isp->client->dev, "host sessions not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 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);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001347 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 if (!otg || isp != the_transceiver)
1350 return -ENODEV;
1351
1352 if (!gadget) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001353 omap_writew(0, OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 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;
Felipe Balbia5f08a32008-10-14 17:30:02 +02001364 dev_dbg(&isp->client->dev, "registered gadget\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /* 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
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001374 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 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,
David Brownell25da3832007-08-14 18:37:14 +02001386 INTR_SESS_VLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001388 INTR_VBUS_VLD);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001389 dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
Harvey Harrison08882d22008-04-22 22:16:47 +02001390 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
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
Felipe Balbia5f08a32008-10-14 17:30:02 +02001402 dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 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
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001430 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (!(otg_ctrl & OTG_BSESSEND))
1432 return -EINVAL;
1433
1434 otg_ctrl |= OTG_B_BUSREQ;
1435 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001436 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 isp->otg.state = OTG_STATE_B_SRP_INIT;
1438
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001439 pr_debug("otg: SRP, %s ... %06x\n", state_name(isp),
1440 omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441#ifdef CONFIG_USB_OTG
Harvey Harrison08882d22008-04-22 22:16:47 +02001442 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443#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);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001452 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
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);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001479 l = omap_readl(OTG_CTRL);
1480 l |= OTG_A_SETB_HNPEN;
1481 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
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",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001491 state_name(isp), omap_readl(OTG_CTRL));
Harvey Harrison08882d22008-04-22 22:16:47 +02001492 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 return 0;
1494#else
1495 /* srp-only */
1496 return -EINVAL;
1497#endif
1498}
1499
1500/*-------------------------------------------------------------------------*/
1501
Jean Delvare9df013b2008-10-14 17:30:02 +02001502static int __init isp1301_probe(struct i2c_client *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 int status;
1505 struct isp1301 *isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 if (the_transceiver)
1508 return 0;
1509
Pekka Enberg82ca76b2005-09-06 15:18:35 -07001510 isp = kzalloc(sizeof *isp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 if (!isp)
1512 return 0;
1513
David Brownell25da3832007-08-14 18:37:14 +02001514 INIT_WORK(&isp->work, isp1301_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 init_timer(&isp->timer);
1516 isp->timer.function = isp1301_timer;
1517 isp->timer.data = (unsigned long) isp;
1518
Jean Delvare9df013b2008-10-14 17:30:02 +02001519 i2c_set_clientdata(i2c, isp);
1520 isp->client = i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Jean Delvare9df013b2008-10-14 17:30:02 +02001522 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Jean Delvare9df013b2008-10-14 17:30:02 +02001528 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
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");
Jean Delvare9df013b2008-10-14 17:30:02 +02001560 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 }
1562#endif
1563
1564 if (machine_is_omap_h2()) {
1565 /* full speed signaling by default */
1566 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
Dmitry Baryshkovcbbcb9b2008-08-07 16:29:24 +04001567 MC1_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 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);
David Brownell25da3832007-08-14 18:37:14 +02001573 if (gpio_request(2, "isp1301") == 0)
1574 gpio_direction_input(2);
1575 isp->irq_type = IRQF_TRIGGER_FALLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
David Brownell25da3832007-08-14 18:37:14 +02001578 isp->irq_type |= IRQF_SAMPLE_RANDOM;
Jean Delvare9df013b2008-10-14 17:30:02 +02001579 status = request_irq(i2c->irq, isp1301_irq,
David Brownell25da3832007-08-14 18:37:14 +02001580 isp->irq_type, DRIVER_NAME, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (status < 0) {
1582 dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
Jean Delvare9df013b2008-10-14 17:30:02 +02001583 i2c->irq, status);
1584 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Jean Delvare9df013b2008-10-14 17:30:02 +02001587 isp->otg.dev = &i2c->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 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
Harvey Harrison08882d22008-04-22 22:16:47 +02001605 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
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;
Jean Delvare9df013b2008-10-14 17:30:02 +02001618
1619fail:
1620 kfree(isp);
1621 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622}
1623
Jean Delvare9df013b2008-10-14 17:30:02 +02001624static const struct i2c_device_id isp1301_id[] = {
1625 { "isp1301_omap", 0 },
1626 { }
1627};
1628MODULE_DEVICE_TABLE(i2c, isp1301_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630static struct i2c_driver isp1301_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +01001631 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +01001632 .name = "isp1301_omap",
1633 },
Jean Delvare9df013b2008-10-14 17:30:02 +02001634 .probe = isp1301_probe,
1635 .remove = __exit_p(isp1301_remove),
1636 .id_table = isp1301_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637};
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