blob: 70cf5d7bca48ed25ee08aa6e5fa40dec52b4268a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * isp1301_omap - ISP 1301 USB transceiver, talking to OMAP OTG controller
3 *
4 * Copyright (C) 2004 Texas Instruments
5 * Copyright (C) 2004 David Brownell
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <linux/interrupt.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010027#include <linux/platform_device.h>
David Brownelld1846b02008-11-28 15:24:38 +010028#include <linux/gpio.h>
David Brownell5f848132006-12-16 15:34:53 -080029#include <linux/usb/ch9.h>
David Brownell187426e2007-12-12 13:45:25 +010030#include <linux/usb/gadget.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/usb.h>
David Brownell3a16f7b2006-06-29 12:27:23 -070032#include <linux/usb/otg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/i2c.h>
34#include <linux/workqueue.h>
35
36#include <asm/irq.h>
David Brownelld1846b02008-11-28 15:24:38 +010037#include <asm/mach-types.h>
38
Tony Lindgrence491cf2009-10-20 09:40:47 -070039#include <plat/usb.h>
40#include <plat/mux.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42
43#ifndef DEBUG
44#undef VERBOSE
45#endif
46
47
48#define DRIVER_VERSION "24 August 2004"
David Brownell25da3832007-08-14 18:37:14 +020049#define DRIVER_NAME (isp1301_driver.driver.name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
52MODULE_LICENSE("GPL");
53
54struct isp1301 {
Heikki Krogerus819d1c72012-02-13 13:24:08 +020055 struct usb_phy phy;
Felipe Balbia5f08a32008-10-14 17:30:02 +020056 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 void (*i2c_release)(struct device *dev);
58
David Brownell25da3832007-08-14 18:37:14 +020059 int irq_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 u32 last_otg_ctrl;
62 unsigned working:1;
63
64 struct timer_list timer;
65
66 /* use keventd context to change the state for us */
67 struct work_struct work;
David Brownell25da3832007-08-14 18:37:14 +020068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned long todo;
70# define WORK_UPDATE_ISP 0 /* update ISP from OTG */
71# define WORK_UPDATE_OTG 1 /* update OTG from ISP */
72# define WORK_HOST_RESUME 4 /* resume host */
73# define WORK_TIMER 6 /* timer fired */
74# define WORK_STOP 7 /* don't resubmit */
75};
76
77
Tony Lindgrenf35ae632008-07-03 12:24:43 +030078/* bits in OTG_CTRL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define OTG_XCEIV_OUTPUTS \
81 (OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
82#define OTG_XCEIV_INPUTS \
83 (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
84#define OTG_CTRL_BITS \
85 (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
86 /* and OTG_PULLUP is sometimes written */
87
88#define OTG_CTRL_MASK (OTG_DRIVER_SEL| \
89 OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
90 OTG_CTRL_BITS)
91
92
93/*-------------------------------------------------------------------------*/
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* board-specific PM hooks */
96
David Brownelld1846b02008-11-28 15:24:38 +010097#if defined(CONFIG_MACH_OMAP_H2) || defined(CONFIG_MACH_OMAP_H3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#if defined(CONFIG_TPS65010) || defined(CONFIG_TPS65010_MODULE)
100
David Brownell6d16bfb2008-01-27 18:14:49 +0100101#include <linux/i2c/tps65010.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#else
104
105static inline int tps65010_set_vbus_draw(unsigned mA)
106{
107 pr_debug("tps65010: draw %d mA (STUB)\n", mA);
108 return 0;
109}
110
111#endif
112
113static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
114{
115 int status = tps65010_set_vbus_draw(mA);
116 if (status < 0)
117 pr_debug(" VBUS %d mA error %d\n", mA, status);
118}
119
Anand Gadiyard77282c2009-08-24 20:14:45 +0530120#else
David Brownelld1846b02008-11-28 15:24:38 +0100121
122static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
123{
124 /* H4 controls this by DIP switch S2.4; no soft control.
125 * ON means the charger is always enabled. Leave it OFF
126 * unless the OTG port is used only in B-peripheral mode.
127 */
128}
129
Anand Gadiyard77282c2009-08-24 20:14:45 +0530130#endif
131
David Brownelld1846b02008-11-28 15:24:38 +0100132static void enable_vbus_source(struct isp1301 *isp)
133{
134 /* this board won't supply more than 8mA vbus power.
135 * some boards can switch a 100ma "unit load" (or more).
136 */
137}
138
139
140/* products will deliver OTG messages with LEDs, GUI, etc */
141static inline void notresponding(struct isp1301 *isp)
142{
143 printk(KERN_NOTICE "OTG device not responding.\n");
144}
145
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*-------------------------------------------------------------------------*/
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static struct i2c_driver isp1301_driver;
150
151/* smbus apis are used for portability */
152
153static inline u8
154isp1301_get_u8(struct isp1301 *isp, u8 reg)
155{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200156 return i2c_smbus_read_byte_data(isp->client, reg + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159static inline int
160isp1301_get_u16(struct isp1301 *isp, u8 reg)
161{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200162 return i2c_smbus_read_word_data(isp->client, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static inline int
166isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
167{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200168 return i2c_smbus_write_byte_data(isp->client, reg + 0, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171static inline int
172isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
173{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200174 return i2c_smbus_write_byte_data(isp->client, reg + 1, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177/*-------------------------------------------------------------------------*/
178
179/* identification */
180#define ISP1301_VENDOR_ID 0x00 /* u16 read */
181#define ISP1301_PRODUCT_ID 0x02 /* u16 read */
182#define ISP1301_BCD_DEVICE 0x14 /* u16 read */
183
184#define I2C_VENDOR_ID_PHILIPS 0x04cc
185#define I2C_PRODUCT_ID_PHILIPS_1301 0x1301
186
187/* operational registers */
188#define ISP1301_MODE_CONTROL_1 0x04 /* u8 read, set, +1 clear */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300189# define MC1_SPEED (1 << 0)
190# define MC1_SUSPEND (1 << 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191# define MC1_DAT_SE0 (1 << 2)
192# define MC1_TRANSPARENT (1 << 3)
193# define MC1_BDIS_ACON_EN (1 << 4)
194# define MC1_OE_INT_EN (1 << 5)
195# define MC1_UART_EN (1 << 6)
196# define MC1_MASK 0x7f
197#define ISP1301_MODE_CONTROL_2 0x12 /* u8 read, set, +1 clear */
198# define MC2_GLOBAL_PWR_DN (1 << 0)
199# define MC2_SPD_SUSP_CTRL (1 << 1)
200# define MC2_BI_DI (1 << 2)
201# define MC2_TRANSP_BDIR0 (1 << 3)
202# define MC2_TRANSP_BDIR1 (1 << 4)
203# define MC2_AUDIO_EN (1 << 5)
204# define MC2_PSW_EN (1 << 6)
205# define MC2_EN2V7 (1 << 7)
206#define ISP1301_OTG_CONTROL_1 0x06 /* u8 read, set, +1 clear */
207# define OTG1_DP_PULLUP (1 << 0)
208# define OTG1_DM_PULLUP (1 << 1)
209# define OTG1_DP_PULLDOWN (1 << 2)
210# define OTG1_DM_PULLDOWN (1 << 3)
211# define OTG1_ID_PULLDOWN (1 << 4)
212# define OTG1_VBUS_DRV (1 << 5)
213# define OTG1_VBUS_DISCHRG (1 << 6)
214# define OTG1_VBUS_CHRG (1 << 7)
215#define ISP1301_OTG_STATUS 0x10 /* u8 readonly */
216# define OTG_B_SESS_END (1 << 6)
217# define OTG_B_SESS_VLD (1 << 7)
218
219#define ISP1301_INTERRUPT_SOURCE 0x08 /* u8 read */
220#define ISP1301_INTERRUPT_LATCH 0x0A /* u8 read, set, +1 clear */
221
222#define ISP1301_INTERRUPT_FALLING 0x0C /* u8 read, set, +1 clear */
223#define ISP1301_INTERRUPT_RISING 0x0E /* u8 read, set, +1 clear */
224
225/* same bitfields in all interrupt registers */
226# define INTR_VBUS_VLD (1 << 0)
227# define INTR_SESS_VLD (1 << 1)
228# define INTR_DP_HI (1 << 2)
229# define INTR_ID_GND (1 << 3)
230# define INTR_DM_HI (1 << 4)
231# define INTR_ID_FLOAT (1 << 5)
232# define INTR_BDIS_ACON (1 << 6)
233# define INTR_CR_INT (1 << 7)
234
235/*-------------------------------------------------------------------------*/
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static inline const char *state_name(struct isp1301 *isp)
238{
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200239 return otg_state_string(isp->phy.state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/*-------------------------------------------------------------------------*/
243
244/* NOTE: some of this ISP1301 setup is specific to H2 boards;
245 * not everything is guarded by board-specific checks, or even using
246 * omap_usb_config data to deduce MC1_DAT_SE0 and MC2_BI_DI.
247 *
248 * ALSO: this currently doesn't use ISP1301 low-power modes
249 * while OTG is running.
250 */
251
252static void power_down(struct isp1301 *isp)
253{
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200254 isp->phy.state = OTG_STATE_UNDEFINED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 // isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300257 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_ID_PULLDOWN);
260 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
261}
262
263static void power_up(struct isp1301 *isp)
264{
265 // isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300266 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
David Brownell25da3832007-08-14 18:37:14 +0200267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 /* do this only when cpu is driving transceiver,
269 * so host won't see a low speed device...
270 */
271 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
272}
273
274#define NO_HOST_SUSPEND
275
276static int host_suspend(struct isp1301 *isp)
277{
278#ifdef NO_HOST_SUSPEND
279 return 0;
280#else
281 struct device *dev;
282
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200283 if (!isp->phy.otg->host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return -ENODEV;
285
286 /* Currently ASSUMES only the OTG port matters;
287 * other ports could be active...
288 */
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200289 dev = isp->phy.otg->host->controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 return dev->driver->suspend(dev, 3, 0);
291#endif
292}
293
294static int host_resume(struct isp1301 *isp)
295{
296#ifdef NO_HOST_SUSPEND
297 return 0;
298#else
299 struct device *dev;
300
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200301 if (!isp->phy.otg->host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return -ENODEV;
303
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200304 dev = isp->phy.otg->host->controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return dev->driver->resume(dev, 0);
306#endif
307}
308
309static int gadget_suspend(struct isp1301 *isp)
310{
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200311 isp->phy.otg->gadget->b_hnp_enable = 0;
312 isp->phy.otg->gadget->a_hnp_support = 0;
313 isp->phy.otg->gadget->a_alt_hnp_support = 0;
314 return usb_gadget_vbus_disconnect(isp->phy.otg->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317/*-------------------------------------------------------------------------*/
318
319#define TIMER_MINUTES 10
320#define TIMER_JIFFIES (TIMER_MINUTES * 60 * HZ)
321
322/* Almost all our I2C messaging comes from a work queue's task context.
323 * NOTE: guaranteeing certain response times might mean we shouldn't
324 * share keventd's work queue; a realtime task might be safest.
325 */
David Brownelld1846b02008-11-28 15:24:38 +0100326static void isp1301_defer_work(struct isp1301 *isp, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 int status;
329
330 if (isp && !test_and_set_bit(work, &isp->todo)) {
Felipe Balbia5f08a32008-10-14 17:30:02 +0200331 (void) get_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 status = schedule_work(&isp->work);
333 if (!status && !isp->working)
Felipe Balbia5f08a32008-10-14 17:30:02 +0200334 dev_vdbg(&isp->client->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 "work item %d may be lost\n", work);
336 }
337}
338
339/* called from irq handlers */
340static void a_idle(struct isp1301 *isp, const char *tag)
341{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300342 u32 l;
343
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200344 if (isp->phy.state == OTG_STATE_A_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return;
346
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200347 isp->phy.otg->default_a = 1;
348 if (isp->phy.otg->host) {
349 isp->phy.otg->host->is_b_host = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 host_suspend(isp);
351 }
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200352 if (isp->phy.otg->gadget) {
353 isp->phy.otg->gadget->is_a_peripheral = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 gadget_suspend(isp);
355 }
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200356 isp->phy.state = OTG_STATE_A_IDLE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300357 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
358 omap_writel(l, OTG_CTRL);
359 isp->last_otg_ctrl = l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 pr_debug(" --> %s/%s\n", state_name(isp), tag);
361}
362
363/* called from irq handlers */
364static void b_idle(struct isp1301 *isp, const char *tag)
365{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300366 u32 l;
367
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200368 if (isp->phy.state == OTG_STATE_B_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return;
370
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200371 isp->phy.otg->default_a = 0;
372 if (isp->phy.otg->host) {
373 isp->phy.otg->host->is_b_host = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 host_suspend(isp);
375 }
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200376 if (isp->phy.otg->gadget) {
377 isp->phy.otg->gadget->is_a_peripheral = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 gadget_suspend(isp);
379 }
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200380 isp->phy.state = OTG_STATE_B_IDLE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300381 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
382 omap_writel(l, OTG_CTRL);
383 isp->last_otg_ctrl = l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 pr_debug(" --> %s/%s\n", state_name(isp), tag);
385}
386
387static void
388dump_regs(struct isp1301 *isp, const char *label)
389{
390#ifdef DEBUG
391 u8 ctrl = isp1301_get_u8(isp, ISP1301_OTG_CONTROL_1);
392 u8 status = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
393 u8 src = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
394
395 pr_debug("otg: %06x, %s %s, otg/%02x stat/%02x.%02x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300396 omap_readl(OTG_CTRL), label, state_name(isp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 ctrl, status, src);
398 /* mode control and irq enables don't change much */
399#endif
400}
401
402/*-------------------------------------------------------------------------*/
403
404#ifdef CONFIG_USB_OTG
405
406/*
407 * The OMAP OTG controller handles most of the OTG state transitions.
408 *
409 * We translate isp1301 outputs (mostly voltage comparator status) into
410 * OTG inputs; OTG outputs (mostly pullup/pulldown controls) and HNP state
411 * flags into isp1301 inputs ... and infer state transitions.
412 */
413
414#ifdef VERBOSE
415
416static void check_state(struct isp1301 *isp, const char *tag)
417{
418 enum usb_otg_state state = OTG_STATE_UNDEFINED;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300419 u8 fsm = omap_readw(OTG_TEST) & 0x0ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 unsigned extra = 0;
421
422 switch (fsm) {
423
424 /* default-b */
425 case 0x0:
426 state = OTG_STATE_B_IDLE;
427 break;
428 case 0x3:
429 case 0x7:
430 extra = 1;
431 case 0x1:
432 state = OTG_STATE_B_PERIPHERAL;
433 break;
434 case 0x11:
435 state = OTG_STATE_B_SRP_INIT;
436 break;
437
438 /* extra dual-role default-b states */
439 case 0x12:
440 case 0x13:
441 case 0x16:
442 extra = 1;
443 case 0x17:
444 state = OTG_STATE_B_WAIT_ACON;
445 break;
446 case 0x34:
447 state = OTG_STATE_B_HOST;
448 break;
449
450 /* default-a */
451 case 0x36:
452 state = OTG_STATE_A_IDLE;
453 break;
454 case 0x3c:
455 state = OTG_STATE_A_WAIT_VFALL;
456 break;
457 case 0x7d:
458 state = OTG_STATE_A_VBUS_ERR;
459 break;
460 case 0x9e:
461 case 0x9f:
462 extra = 1;
463 case 0x89:
464 state = OTG_STATE_A_PERIPHERAL;
465 break;
466 case 0xb7:
467 state = OTG_STATE_A_WAIT_VRISE;
468 break;
469 case 0xb8:
470 state = OTG_STATE_A_WAIT_BCON;
471 break;
472 case 0xb9:
473 state = OTG_STATE_A_HOST;
474 break;
475 case 0xba:
476 state = OTG_STATE_A_SUSPEND;
477 break;
478 default:
479 break;
480 }
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200481 if (isp->phy.state == state && !extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return;
483 pr_debug("otg: %s FSM %s/%02x, %s, %06x\n", tag,
Anatolij Gustschin3dacdf12011-04-15 16:18:38 +0200484 otg_state_string(state), fsm, state_name(isp),
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300485 omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488#else
489
490static inline void check_state(struct isp1301 *isp, const char *tag) { }
491
492#endif
493
494/* outputs from ISP1301_INTERRUPT_SOURCE */
495static void update_otg1(struct isp1301 *isp, u8 int_src)
496{
497 u32 otg_ctrl;
498
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300499 otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
500 otg_ctrl &= ~OTG_XCEIV_INPUTS;
501 otg_ctrl &= ~(OTG_ID|OTG_ASESSVLD|OTG_VBUSVLD);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (int_src & INTR_SESS_VLD)
504 otg_ctrl |= OTG_ASESSVLD;
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200505 else if (isp->phy.state == OTG_STATE_A_WAIT_VFALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 a_idle(isp, "vfall");
507 otg_ctrl &= ~OTG_CTRL_BITS;
508 }
509 if (int_src & INTR_VBUS_VLD)
510 otg_ctrl |= OTG_VBUSVLD;
511 if (int_src & INTR_ID_GND) { /* default-A */
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200512 if (isp->phy.state == OTG_STATE_B_IDLE
513 || isp->phy.state
514 == OTG_STATE_UNDEFINED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 a_idle(isp, "init");
516 return;
517 }
518 } else { /* default-B */
519 otg_ctrl |= OTG_ID;
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200520 if (isp->phy.state == OTG_STATE_A_IDLE
521 || isp->phy.state == OTG_STATE_UNDEFINED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 b_idle(isp, "init");
523 return;
524 }
525 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300526 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529/* outputs from ISP1301_OTG_STATUS */
530static void update_otg2(struct isp1301 *isp, u8 otg_status)
531{
532 u32 otg_ctrl;
533
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300534 otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
535 otg_ctrl &= ~OTG_XCEIV_INPUTS;
536 otg_ctrl &= ~(OTG_BSESSVLD | OTG_BSESSEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (otg_status & OTG_B_SESS_VLD)
538 otg_ctrl |= OTG_BSESSVLD;
539 else if (otg_status & OTG_B_SESS_END)
540 otg_ctrl |= OTG_BSESSEND;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300541 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/* inputs going to ISP1301 */
545static void otg_update_isp(struct isp1301 *isp)
546{
547 u32 otg_ctrl, otg_change;
548 u8 set = OTG1_DM_PULLDOWN, clr = OTG1_DM_PULLUP;
549
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300550 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 otg_change = otg_ctrl ^ isp->last_otg_ctrl;
552 isp->last_otg_ctrl = otg_ctrl;
553 otg_ctrl = otg_ctrl & OTG_XCEIV_INPUTS;
554
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200555 switch (isp->phy.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 case OTG_STATE_B_IDLE:
557 case OTG_STATE_B_PERIPHERAL:
558 case OTG_STATE_B_SRP_INIT:
559 if (!(otg_ctrl & OTG_PULLUP)) {
560 // if (otg_ctrl & OTG_B_HNPEN) {
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200561 if (isp->phy.otg->gadget->b_hnp_enable) {
562 isp->phy.state = OTG_STATE_B_WAIT_ACON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 pr_debug(" --> b_wait_acon\n");
564 }
565 goto pulldown;
566 }
567pullup:
568 set |= OTG1_DP_PULLUP;
569 clr |= OTG1_DP_PULLDOWN;
570 break;
571 case OTG_STATE_A_SUSPEND:
572 case OTG_STATE_A_PERIPHERAL:
573 if (otg_ctrl & OTG_PULLUP)
574 goto pullup;
575 /* FALLTHROUGH */
576 // case OTG_STATE_B_WAIT_ACON:
577 default:
578pulldown:
579 set |= OTG1_DP_PULLDOWN;
580 clr |= OTG1_DP_PULLUP;
581 break;
582 }
583
584# define toggle(OTG,ISP) do { \
585 if (otg_ctrl & OTG) set |= ISP; \
586 else clr |= ISP; \
587 } while (0)
588
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200589 if (!(isp->phy.otg->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 otg_ctrl &= ~OTG_DRV_VBUS;
591
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200592 switch (isp->phy.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 case OTG_STATE_A_SUSPEND:
594 if (otg_ctrl & OTG_DRV_VBUS) {
595 set |= OTG1_VBUS_DRV;
596 break;
597 }
598 /* HNP failed for some reason (A_AIDL_BDIS timeout) */
599 notresponding(isp);
600
601 /* FALLTHROUGH */
602 case OTG_STATE_A_VBUS_ERR:
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200603 isp->phy.state = OTG_STATE_A_WAIT_VFALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 pr_debug(" --> a_wait_vfall\n");
605 /* FALLTHROUGH */
606 case OTG_STATE_A_WAIT_VFALL:
607 /* FIXME usbcore thinks port power is still on ... */
608 clr |= OTG1_VBUS_DRV;
609 break;
610 case OTG_STATE_A_IDLE:
611 if (otg_ctrl & OTG_DRV_VBUS) {
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200612 isp->phy.state = OTG_STATE_A_WAIT_VRISE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 pr_debug(" --> a_wait_vrise\n");
614 }
615 /* FALLTHROUGH */
616 default:
617 toggle(OTG_DRV_VBUS, OTG1_VBUS_DRV);
618 }
619
620 toggle(OTG_PU_VBUS, OTG1_VBUS_CHRG);
621 toggle(OTG_PD_VBUS, OTG1_VBUS_DISCHRG);
622
623# undef toggle
624
625 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, set);
626 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, clr);
627
628 /* HNP switch to host or peripheral; and SRP */
629 if (otg_change & OTG_PULLUP) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300630 u32 l;
631
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200632 switch (isp->phy.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 case OTG_STATE_B_IDLE:
634 if (clr & OTG1_DP_PULLUP)
635 break;
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200636 isp->phy.state = OTG_STATE_B_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 pr_debug(" --> b_peripheral\n");
638 break;
639 case OTG_STATE_A_SUSPEND:
640 if (clr & OTG1_DP_PULLUP)
641 break;
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200642 isp->phy.state = OTG_STATE_A_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 pr_debug(" --> a_peripheral\n");
644 break;
645 default:
646 break;
647 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300648 l = omap_readl(OTG_CTRL);
649 l |= OTG_PULLUP;
650 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Harvey Harrison08882d22008-04-22 22:16:47 +0200653 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 dump_regs(isp, "otg->isp1301");
655}
656
David Howells7d12e782006-10-05 14:55:46 +0100657static irqreturn_t omap_otg_irq(int irq, void *_isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300659 u16 otg_irq = omap_readw(OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 u32 otg_ctrl;
661 int ret = IRQ_NONE;
662 struct isp1301 *isp = _isp;
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200663 struct usb_otg *otg = isp->phy.otg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Joe Perches7c9d4402011-06-23 11:39:20 -0700665 /* update ISP1301 transceiver from OTG controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (otg_irq & OPRT_CHG) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300667 omap_writew(OPRT_CHG, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 isp1301_defer_work(isp, WORK_UPDATE_ISP);
669 ret = IRQ_HANDLED;
670
671 /* SRP to become b_peripheral failed */
672 } else if (otg_irq & B_SRP_TMROUT) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300673 pr_debug("otg: B_SRP_TIMEOUT, %06x\n", omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 notresponding(isp);
675
676 /* gadget drivers that care should monitor all kinds of
677 * remote wakeup (SRP, normal) using their own timer
678 * to give "check cable and A-device" messages.
679 */
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200680 if (isp->phy.state == OTG_STATE_B_SRP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 b_idle(isp, "srp_timeout");
682
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300683 omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 ret = IRQ_HANDLED;
685
686 /* HNP to become b_host failed */
687 } else if (otg_irq & B_HNP_FAIL) {
688 pr_debug("otg: %s B_HNP_FAIL, %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300689 state_name(isp), omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 notresponding(isp);
691
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300692 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 otg_ctrl |= OTG_BUSDROP;
694 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300695 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* subset of b_peripheral()... */
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200698 isp->phy.state = OTG_STATE_B_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 pr_debug(" --> b_peripheral\n");
700
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300701 omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 ret = IRQ_HANDLED;
703
704 /* detect SRP from B-device ... */
705 } else if (otg_irq & A_SRP_DETECT) {
706 pr_debug("otg: %s SRP_DETECT, %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300707 state_name(isp), omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 isp1301_defer_work(isp, WORK_UPDATE_OTG);
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200710 switch (isp->phy.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 case OTG_STATE_A_IDLE:
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200712 if (!otg->host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 break;
714 isp1301_defer_work(isp, WORK_HOST_RESUME);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300715 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 otg_ctrl |= OTG_A_BUSREQ;
717 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
718 & ~OTG_XCEIV_INPUTS
719 & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300720 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 break;
722 default:
723 break;
724 }
725
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300726 omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 ret = IRQ_HANDLED;
728
729 /* timer expired: T(a_wait_bcon) and maybe T(a_wait_vrise)
730 * we don't track them separately
731 */
732 } else if (otg_irq & A_REQ_TMROUT) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300733 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 pr_info("otg: BCON_TMOUT from %s, %06x\n",
735 state_name(isp), otg_ctrl);
736 notresponding(isp);
737
738 otg_ctrl |= OTG_BUSDROP;
739 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300740 omap_writel(otg_ctrl, OTG_CTRL);
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200741 isp->phy.state = OTG_STATE_A_WAIT_VFALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300743 omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 ret = IRQ_HANDLED;
745
746 /* A-supplied voltage fell too low; overcurrent */
747 } else if (otg_irq & A_VBUS_ERR) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300748 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 printk(KERN_ERR "otg: %s, VBUS_ERR %04x ctrl %06x\n",
750 state_name(isp), otg_irq, otg_ctrl);
751
752 otg_ctrl |= OTG_BUSDROP;
753 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300754 omap_writel(otg_ctrl, OTG_CTRL);
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200755 isp->phy.state = OTG_STATE_A_VBUS_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300757 omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 ret = IRQ_HANDLED;
759
Joe Perches7c9d4402011-06-23 11:39:20 -0700760 /* switch driver; the transceiver code activates it,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * ungating the udc clock or resuming OHCI.
762 */
763 } else if (otg_irq & DRIVER_SWITCH) {
764 int kick = 0;
765
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300766 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 printk(KERN_NOTICE "otg: %s, SWITCH to %s, ctrl %06x\n",
768 state_name(isp),
769 (otg_ctrl & OTG_DRIVER_SEL)
770 ? "gadget" : "host",
771 otg_ctrl);
772 isp1301_defer_work(isp, WORK_UPDATE_ISP);
773
774 /* role is peripheral */
775 if (otg_ctrl & OTG_DRIVER_SEL) {
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200776 switch (isp->phy.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 case OTG_STATE_A_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200778 b_idle(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 break;
780 default:
781 break;
782 }
783 isp1301_defer_work(isp, WORK_UPDATE_ISP);
784
785 /* role is host */
786 } else {
787 if (!(otg_ctrl & OTG_ID)) {
David Brownell25da3832007-08-14 18:37:14 +0200788 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300789 omap_writel(otg_ctrl | OTG_A_BUSREQ, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200792 if (otg->host) {
793 switch (isp->phy.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 case OTG_STATE_B_WAIT_ACON:
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200795 isp->phy.state = OTG_STATE_B_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 pr_debug(" --> b_host\n");
797 kick = 1;
798 break;
799 case OTG_STATE_A_WAIT_BCON:
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200800 isp->phy.state = OTG_STATE_A_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 pr_debug(" --> a_host\n");
802 break;
803 case OTG_STATE_A_PERIPHERAL:
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200804 isp->phy.state = OTG_STATE_A_WAIT_BCON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 pr_debug(" --> a_wait_bcon\n");
806 break;
807 default:
808 break;
809 }
810 isp1301_defer_work(isp, WORK_HOST_RESUME);
811 }
812 }
813
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300814 omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 ret = IRQ_HANDLED;
816
817 if (kick)
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200818 usb_bus_start_enum(otg->host, otg->host->otg_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
820
Harvey Harrison08882d22008-04-22 22:16:47 +0200821 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return ret;
823}
824
825static struct platform_device *otg_dev;
826
Felipe Balbi465f8292009-12-15 23:19:52 +0200827static int isp1301_otg_init(struct isp1301 *isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300829 u32 l;
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (!otg_dev)
832 return -ENODEV;
833
Harvey Harrison08882d22008-04-22 22:16:47 +0200834 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* some of these values are board-specific... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300836 l = omap_readl(OTG_SYSCON_2);
837 l |= OTG_EN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /* for B-device: */
839 | SRP_GPDATA /* 9msec Bdev D+ pulse */
840 | SRP_GPDVBUS /* discharge after VBUS pulse */
841 // | (3 << 24) /* 2msec VBUS pulse */
842 /* for A-device: */
843 | (0 << 20) /* 200ms nominal A_WAIT_VRISE timer */
844 | SRP_DPW /* detect 167+ns SRP pulses */
845 | SRP_DATA | SRP_VBUS /* accept both kinds of SRP pulse */
846 ;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300847 omap_writel(l, OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
850 update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
851
Harvey Harrison08882d22008-04-22 22:16:47 +0200852 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 pr_debug("otg: %s, %s %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300854 state_name(isp), __func__, omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300856 omap_writew(DRIVER_SWITCH | OPRT_CHG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 | B_SRP_TMROUT | B_HNP_FAIL
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300858 | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT, OTG_IRQ_EN);
859
860 l = omap_readl(OTG_SYSCON_2);
861 l |= OTG_EN;
862 omap_writel(l, OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 return 0;
865}
866
Russell King3ae5eae2005-11-09 22:32:44 +0000867static int otg_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 // struct omap_usb_config *config = dev->platform_data;
870
Russell King3ae5eae2005-11-09 22:32:44 +0000871 otg_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return 0;
873}
874
Russell King3ae5eae2005-11-09 22:32:44 +0000875static int otg_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
David Brownelld1846b02008-11-28 15:24:38 +0100877 otg_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879}
880
David Brownelld1846b02008-11-28 15:24:38 +0100881static struct platform_driver omap_otg_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 .probe = otg_probe,
Russell King3ae5eae2005-11-09 22:32:44 +0000883 .remove = otg_remove,
884 .driver = {
885 .owner = THIS_MODULE,
886 .name = "omap_otg",
887 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888};
889
890static int otg_bind(struct isp1301 *isp)
891{
892 int status;
893
894 if (otg_dev)
895 return -EBUSY;
896
Russell King3ae5eae2005-11-09 22:32:44 +0000897 status = platform_driver_register(&omap_otg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (status < 0)
899 return status;
900
901 if (otg_dev)
902 status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800903 0, DRIVER_NAME, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 else
905 status = -ENODEV;
906
907 if (status < 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000908 platform_driver_unregister(&omap_otg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return status;
910}
911
912static void otg_unbind(struct isp1301 *isp)
913{
914 if (!otg_dev)
915 return;
916 free_irq(otg_dev->resource[1].start, isp);
917}
918
919#else
920
921/* OTG controller isn't clocked */
922
923#endif /* CONFIG_USB_OTG */
924
925/*-------------------------------------------------------------------------*/
926
927static void b_peripheral(struct isp1301 *isp)
928{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300929 u32 l;
930
931 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
932 omap_writel(l, OTG_CTRL);
933
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200934 usb_gadget_vbus_connect(isp->phy.otg->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936#ifdef CONFIG_USB_OTG
937 enable_vbus_draw(isp, 8);
938 otg_update_isp(isp);
939#else
940 enable_vbus_draw(isp, 100);
941 /* UDC driver just set OTG_BSESSVLD */
942 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
943 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200944 isp->phy.state = OTG_STATE_B_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 pr_debug(" --> b_peripheral\n");
946 dump_regs(isp, "2periph");
947#endif
948}
949
950static void isp_update_otg(struct isp1301 *isp, u8 stat)
951{
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200952 struct usb_otg *otg = isp->phy.otg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 u8 isp_stat, isp_bstat;
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200954 enum usb_otg_state state = isp->phy.state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 if (stat & INTR_BDIS_ACON)
957 pr_debug("OTG: BDIS_ACON, %s\n", state_name(isp));
958
959 /* start certain state transitions right away */
960 isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
961 if (isp_stat & INTR_ID_GND) {
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200962 if (otg->default_a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 switch (state) {
964 case OTG_STATE_B_IDLE:
965 a_idle(isp, "idle");
966 /* FALLTHROUGH */
967 case OTG_STATE_A_IDLE:
968 enable_vbus_source(isp);
969 /* FALLTHROUGH */
970 case OTG_STATE_A_WAIT_VRISE:
971 /* we skip over OTG_STATE_A_WAIT_BCON, since
972 * the HC will transition to A_HOST (or
973 * A_SUSPEND!) without our noticing except
974 * when HNP is used.
975 */
976 if (isp_stat & INTR_VBUS_VLD)
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200977 isp->phy.state = OTG_STATE_A_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 break;
979 case OTG_STATE_A_WAIT_VFALL:
980 if (!(isp_stat & INTR_SESS_VLD))
981 a_idle(isp, "vfell");
982 break;
983 default:
984 if (!(isp_stat & INTR_VBUS_VLD))
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200985 isp->phy.state = OTG_STATE_A_VBUS_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 break;
987 }
988 isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
989 } else {
990 switch (state) {
991 case OTG_STATE_B_PERIPHERAL:
992 case OTG_STATE_B_HOST:
993 case OTG_STATE_B_WAIT_ACON:
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200994 usb_gadget_vbus_disconnect(otg->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
996 default:
997 break;
998 }
999 if (state != OTG_STATE_A_IDLE)
1000 a_idle(isp, "id");
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001001 if (otg->host && state == OTG_STATE_A_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 isp1301_defer_work(isp, WORK_HOST_RESUME);
1003 isp_bstat = 0;
1004 }
1005 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001006 u32 l;
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* if user unplugged mini-A end of cable,
1009 * don't bypass A_WAIT_VFALL.
1010 */
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001011 if (otg->default_a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 switch (state) {
1013 default:
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001014 isp->phy.state = OTG_STATE_A_WAIT_VFALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 break;
1016 case OTG_STATE_A_WAIT_VFALL:
1017 state = OTG_STATE_A_IDLE;
1018 /* khubd may take a while to notice and
1019 * handle this disconnect, so don't go
1020 * to B_IDLE quite yet.
1021 */
1022 break;
1023 case OTG_STATE_A_IDLE:
1024 host_suspend(isp);
1025 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
1026 MC1_BDIS_ACON_EN);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001027 isp->phy.state = OTG_STATE_B_IDLE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001028 l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1029 l &= ~OTG_CTRL_BITS;
1030 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 break;
1032 case OTG_STATE_B_IDLE:
1033 break;
1034 }
1035 }
1036 isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
1037
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001038 switch (isp->phy.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 case OTG_STATE_B_PERIPHERAL:
1040 case OTG_STATE_B_WAIT_ACON:
1041 case OTG_STATE_B_HOST:
1042 if (likely(isp_bstat & OTG_B_SESS_VLD))
1043 break;
1044 enable_vbus_draw(isp, 0);
1045#ifndef CONFIG_USB_OTG
1046 /* UDC driver will clear OTG_BSESSVLD */
1047 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1048 OTG1_DP_PULLDOWN);
1049 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1050 OTG1_DP_PULLUP);
Harvey Harrison08882d22008-04-22 22:16:47 +02001051 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052#endif
1053 /* FALLTHROUGH */
1054 case OTG_STATE_B_SRP_INIT:
Harvey Harrison08882d22008-04-22 22:16:47 +02001055 b_idle(isp, __func__);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001056 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
1057 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* FALLTHROUGH */
1059 case OTG_STATE_B_IDLE:
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001060 if (otg->gadget && (isp_bstat & OTG_B_SESS_VLD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061#ifdef CONFIG_USB_OTG
1062 update_otg1(isp, isp_stat);
1063 update_otg2(isp, isp_bstat);
1064#endif
1065 b_peripheral(isp);
1066 } else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
1067 isp_bstat |= OTG_B_SESS_END;
1068 break;
1069 case OTG_STATE_A_WAIT_VFALL:
1070 break;
1071 default:
1072 pr_debug("otg: unsupported b-device %s\n",
1073 state_name(isp));
1074 break;
1075 }
1076 }
1077
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001078 if (state != isp->phy.state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 pr_debug(" isp, %s -> %s\n",
Anatolij Gustschin3dacdf12011-04-15 16:18:38 +02001080 otg_state_string(state), state_name(isp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082#ifdef CONFIG_USB_OTG
1083 /* update the OTG controller state to match the isp1301; may
1084 * trigger OPRT_CHG irqs for changes going to the isp1301.
1085 */
1086 update_otg1(isp, isp_stat);
1087 update_otg2(isp, isp_bstat);
Harvey Harrison08882d22008-04-22 22:16:47 +02001088 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089#endif
1090
1091 dump_regs(isp, "isp1301->otg");
1092}
1093
1094/*-------------------------------------------------------------------------*/
1095
1096static u8 isp1301_clear_latch(struct isp1301 *isp)
1097{
1098 u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
1099 isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
1100 return latch;
1101}
1102
1103static void
David Brownell25da3832007-08-14 18:37:14 +02001104isp1301_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
David Brownell25da3832007-08-14 18:37:14 +02001106 struct isp1301 *isp = container_of(work, struct isp1301, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 int stop;
1108
1109 /* implicit lock: we're the only task using this device */
1110 isp->working = 1;
1111 do {
1112 stop = test_bit(WORK_STOP, &isp->todo);
1113
1114#ifdef CONFIG_USB_OTG
1115 /* transfer state from otg engine to isp1301 */
1116 if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
1117 otg_update_isp(isp);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001118 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120#endif
1121 /* transfer state from isp1301 to otg engine */
1122 if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
1123 u8 stat = isp1301_clear_latch(isp);
1124
1125 isp_update_otg(isp, stat);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001126 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
1129 if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
1130 u32 otg_ctrl;
1131
1132 /*
1133 * skip A_WAIT_VRISE; hc transitions invisibly
1134 * skip A_WAIT_BCON; same.
1135 */
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001136 switch (isp->phy.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 case OTG_STATE_A_WAIT_BCON:
1138 case OTG_STATE_A_WAIT_VRISE:
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001139 isp->phy.state = OTG_STATE_A_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 pr_debug(" --> a_host\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001141 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 otg_ctrl |= OTG_A_BUSREQ;
1143 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
1144 & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001145 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 break;
1147 case OTG_STATE_B_WAIT_ACON:
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001148 isp->phy.state = OTG_STATE_B_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 pr_debug(" --> b_host (acon)\n");
1150 break;
1151 case OTG_STATE_B_HOST:
1152 case OTG_STATE_B_IDLE:
1153 case OTG_STATE_A_IDLE:
1154 break;
1155 default:
1156 pr_debug(" host resume in %s\n",
1157 state_name(isp));
1158 }
1159 host_resume(isp);
1160 // mdelay(10);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001161 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163
1164 if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
1165#ifdef VERBOSE
1166 dump_regs(isp, "timer");
1167 if (!stop)
1168 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1169#endif
Felipe Balbia5f08a32008-10-14 17:30:02 +02001170 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
1173 if (isp->todo)
Felipe Balbia5f08a32008-10-14 17:30:02 +02001174 dev_vdbg(&isp->client->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 "work done, todo = 0x%lx\n",
1176 isp->todo);
1177 if (stop) {
Felipe Balbia5f08a32008-10-14 17:30:02 +02001178 dev_dbg(&isp->client->dev, "stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180 }
1181 } while (isp->todo);
1182 isp->working = 0;
1183}
1184
David Howells7d12e782006-10-05 14:55:46 +01001185static irqreturn_t isp1301_irq(int irq, void *isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 isp1301_defer_work(isp, WORK_UPDATE_OTG);
1188 return IRQ_HANDLED;
1189}
1190
1191static void isp1301_timer(unsigned long _isp)
1192{
1193 isp1301_defer_work((void *)_isp, WORK_TIMER);
1194}
1195
1196/*-------------------------------------------------------------------------*/
1197
1198static void isp1301_release(struct device *dev)
1199{
1200 struct isp1301 *isp;
1201
Felipe Balbia5f08a32008-10-14 17:30:02 +02001202 isp = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
David Brownelld1846b02008-11-28 15:24:38 +01001204 /* FIXME -- not with a "new style" driver, it doesn't!! */
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
1207 if (isp->i2c_release)
1208 isp->i2c_release(dev);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001209 kfree(isp->phy.otg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 kfree (isp);
1211}
1212
1213static struct isp1301 *the_transceiver;
1214
Jean Delvare9df013b2008-10-14 17:30:02 +02001215static int __exit isp1301_remove(struct i2c_client *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 struct isp1301 *isp;
1218
Felipe Balbia5f08a32008-10-14 17:30:02 +02001219 isp = i2c_get_clientdata(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1222 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
Jean Delvare9df013b2008-10-14 17:30:02 +02001223 free_irq(i2c->irq, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224#ifdef CONFIG_USB_OTG
1225 otg_unbind(isp);
1226#endif
1227 if (machine_is_omap_h2())
David Brownelld1846b02008-11-28 15:24:38 +01001228 gpio_free(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 isp->timer.data = 0;
1231 set_bit(WORK_STOP, &isp->todo);
1232 del_timer_sync(&isp->timer);
Tejun Heo569ff2d2010-12-24 16:14:20 +01001233 flush_work_sync(&isp->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 put_device(&i2c->dev);
David Brownelld1846b02008-11-28 15:24:38 +01001236 the_transceiver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Jean Delvare9df013b2008-10-14 17:30:02 +02001238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
1241/*-------------------------------------------------------------------------*/
1242
1243/* NOTE: three modes are possible here, only one of which
1244 * will be standards-conformant on any given system:
1245 *
1246 * - OTG mode (dual-role), required if there's a Mini-AB connector
1247 * - HOST mode, for when there's one or more A (host) connectors
1248 * - DEVICE mode, for when there's a B/Mini-B (device) connector
1249 *
1250 * As a rule, you won't have an isp1301 chip unless it's there to
David Brownell25da3832007-08-14 18:37:14 +02001251 * support the OTG mode. Other modes help testing USB controllers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 * in isolation from (full) OTG support, or maybe so later board
1253 * revisions can help to support those feature.
1254 */
1255
1256#ifdef CONFIG_USB_OTG
1257
1258static int isp1301_otg_enable(struct isp1301 *isp)
1259{
1260 power_up(isp);
Felipe Balbi465f8292009-12-15 23:19:52 +02001261 isp1301_otg_init(isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /* NOTE: since we don't change this, this provides
1264 * a few more interrupts than are strictly needed.
1265 */
1266 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001267 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001269 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Felipe Balbia5f08a32008-10-14 17:30:02 +02001271 dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 return 0;
1274}
1275
1276#endif
1277
1278/* add or disable the host device+driver */
1279static int
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001280isp1301_set_host(struct usb_otg *otg, struct usb_bus *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001282 struct isp1301 *isp = container_of(otg->phy, struct isp1301, phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 if (!otg || isp != the_transceiver)
1285 return -ENODEV;
1286
1287 if (!host) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001288 omap_writew(0, OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 power_down(isp);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001290 otg->host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return 0;
1292 }
1293
1294#ifdef CONFIG_USB_OTG
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001295 otg->host = host;
Felipe Balbia5f08a32008-10-14 17:30:02 +02001296 dev_dbg(&isp->client->dev, "registered host\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 host_suspend(isp);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001298 if (otg->gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return isp1301_otg_enable(isp);
1300 return 0;
1301
1302#elif !defined(CONFIG_USB_GADGET_OMAP)
1303 // FIXME update its refcount
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001304 otg->host = host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 power_up(isp);
1307
1308 if (machine_is_omap_h2())
1309 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1310
Felipe Balbia5f08a32008-10-14 17:30:02 +02001311 dev_info(&isp->client->dev, "A-Host sessions ok\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001313 INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001315 INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* If this has a Mini-AB connector, this mode is highly
1318 * nonstandard ... but can be handy for testing, especially with
1319 * the Mini-A end of an OTG cable. (Or something nonstandard
1320 * like MiniB-to-StandardB, maybe built with a gender mender.)
1321 */
1322 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
1323
Harvey Harrison08882d22008-04-22 22:16:47 +02001324 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 return 0;
1327
1328#else
Felipe Balbia5f08a32008-10-14 17:30:02 +02001329 dev_dbg(&isp->client->dev, "host sessions not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return -EINVAL;
1331#endif
1332
1333}
1334
1335static int
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001336isp1301_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001338 struct isp1301 *isp = container_of(otg->phy, struct isp1301, phy);
David Brownelld1846b02008-11-28 15:24:38 +01001339#ifndef CONFIG_USB_OTG
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001340 u32 l;
David Brownelld1846b02008-11-28 15:24:38 +01001341#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343 if (!otg || isp != the_transceiver)
1344 return -ENODEV;
1345
1346 if (!gadget) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001347 omap_writew(0, OTG_IRQ_EN);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001348 if (!otg->default_a)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 enable_vbus_draw(isp, 0);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001350 usb_gadget_vbus_disconnect(otg->gadget);
1351 otg->gadget = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 power_down(isp);
1353 return 0;
1354 }
1355
1356#ifdef CONFIG_USB_OTG
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001357 otg->gadget = gadget;
Felipe Balbia5f08a32008-10-14 17:30:02 +02001358 dev_dbg(&isp->client->dev, "registered gadget\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /* gadget driver may be suspended until vbus_connect () */
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001360 if (otg->host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return isp1301_otg_enable(isp);
1362 return 0;
1363
1364#elif !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001365 otg->gadget = gadget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 // FIXME update its refcount
1367
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001368 l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1369 l &= ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS);
1370 l |= OTG_ID;
1371 omap_writel(l, OTG_CTRL);
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 power_up(isp);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001374 isp->phy.state = OTG_STATE_B_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
David Brownelld1846b02008-11-28 15:24:38 +01001376 if (machine_is_omap_h2() || machine_is_omap_h3())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1378
1379 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001380 INTR_SESS_VLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001382 INTR_VBUS_VLD);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001383 dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
Harvey Harrison08882d22008-04-22 22:16:47 +02001384 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /* If this has a Mini-AB connector, this mode is highly
1387 * nonstandard ... but can be handy for testing, so long
1388 * as you don't plug a Mini-A cable into the jack.
1389 */
1390 if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
1391 b_peripheral(isp);
1392
1393 return 0;
1394
1395#else
Felipe Balbia5f08a32008-10-14 17:30:02 +02001396 dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return -EINVAL;
1398#endif
1399}
1400
1401
1402/*-------------------------------------------------------------------------*/
1403
1404static int
Heikki Krogerus86753812012-02-13 13:24:02 +02001405isp1301_set_power(struct usb_phy *dev, unsigned mA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 if (!the_transceiver)
1408 return -ENODEV;
1409 if (dev->state == OTG_STATE_B_PERIPHERAL)
1410 enable_vbus_draw(the_transceiver, mA);
1411 return 0;
1412}
1413
1414static int
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001415isp1301_start_srp(struct usb_otg *otg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001417 struct isp1301 *isp = container_of(otg->phy, struct isp1301, phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 u32 otg_ctrl;
1419
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001420 if (!otg || isp != the_transceiver
1421 || isp->phy.state != OTG_STATE_B_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return -ENODEV;
1423
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001424 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (!(otg_ctrl & OTG_BSESSEND))
1426 return -EINVAL;
1427
1428 otg_ctrl |= OTG_B_BUSREQ;
1429 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001430 omap_writel(otg_ctrl, OTG_CTRL);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001431 isp->phy.state = OTG_STATE_B_SRP_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001433 pr_debug("otg: SRP, %s ... %06x\n", state_name(isp),
1434 omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435#ifdef CONFIG_USB_OTG
Harvey Harrison08882d22008-04-22 22:16:47 +02001436 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437#endif
1438 return 0;
1439}
1440
1441static int
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001442isp1301_start_hnp(struct usb_otg *otg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444#ifdef CONFIG_USB_OTG
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001445 struct isp1301 *isp = container_of(otg->phy, struct isp1301, phy);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001446 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001448 if (!otg || isp != the_transceiver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -ENODEV;
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001450 if (otg->default_a && (otg->host == NULL || !otg->host->b_hnp_enable))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 return -ENOTCONN;
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001452 if (!otg->default_a && (otg->gadget == NULL
1453 || !otg->gadget->b_hnp_enable))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -ENOTCONN;
1455
1456 /* We want hardware to manage most HNP protocol timings.
1457 * So do this part as early as possible...
1458 */
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001459 switch (isp->phy.state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 case OTG_STATE_B_HOST:
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001461 isp->phy.state = OTG_STATE_B_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* caller will suspend next */
1463 break;
1464 case OTG_STATE_A_HOST:
1465#if 0
1466 /* autoconnect mode avoids irq latency bugs */
1467 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1468 MC1_BDIS_ACON_EN);
1469#endif
1470 /* caller must suspend then clear A_BUSREQ */
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001471 usb_gadget_vbus_connect(otg->gadget);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001472 l = omap_readl(OTG_CTRL);
1473 l |= OTG_A_SETB_HNPEN;
1474 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 break;
1477 case OTG_STATE_A_PERIPHERAL:
1478 /* initiated by B-Host suspend */
1479 break;
1480 default:
1481 return -EILSEQ;
1482 }
1483 pr_debug("otg: HNP %s, %06x ...\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001484 state_name(isp), omap_readl(OTG_CTRL));
Harvey Harrison08882d22008-04-22 22:16:47 +02001485 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return 0;
1487#else
1488 /* srp-only */
1489 return -EINVAL;
1490#endif
1491}
1492
1493/*-------------------------------------------------------------------------*/
1494
Uwe Kleine-Königa7e2a892011-02-09 21:40:10 +01001495static int __devinit
David Brownelld1846b02008-11-28 15:24:38 +01001496isp1301_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 int status;
1499 struct isp1301 *isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 if (the_transceiver)
1502 return 0;
1503
Pekka Enberg82ca76b2005-09-06 15:18:35 -07001504 isp = kzalloc(sizeof *isp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (!isp)
1506 return 0;
1507
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001508 isp->phy.otg = kzalloc(sizeof *isp->phy.otg, GFP_KERNEL);
1509 if (!isp->phy.otg) {
1510 kfree(isp);
1511 return 0;
1512 }
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
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001522 /* verify the chip (shouldn't be necessary) */
Jean Delvare9df013b2008-10-14 17:30:02 +02001523 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
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001587 isp->phy.dev = &i2c->dev;
1588 isp->phy.label = DRIVER_NAME;
1589 isp->phy.set_power = isp1301_set_power,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001591 isp->phy.otg->phy = &isp->phy;
1592 isp->phy.otg->set_host = isp1301_set_host,
1593 isp->phy.otg->set_peripheral = isp1301_set_peripheral,
1594 isp->phy.otg->start_srp = isp1301_start_srp,
1595 isp->phy.otg->start_hnp = isp1301_start_hnp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 enable_vbus_draw(isp, 0);
1598 power_down(isp);
1599 the_transceiver = isp;
1600
1601#ifdef CONFIG_USB_OTG
1602 update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
1603 update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
1604#endif
1605
Harvey Harrison08882d22008-04-22 22:16:47 +02001606 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608#ifdef VERBOSE
1609 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1610 dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
1611#endif
1612
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001613 status = usb_set_transceiver(&isp->phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if (status < 0)
1615 dev_err(&i2c->dev, "can't register transceiver, %d\n",
1616 status);
1617
1618 return 0;
Jean Delvare9df013b2008-10-14 17:30:02 +02001619
1620fail:
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001621 kfree(isp->phy.otg);
Jean Delvare9df013b2008-10-14 17:30:02 +02001622 kfree(isp);
1623 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
Jean Delvare9df013b2008-10-14 17:30:02 +02001626static const struct i2c_device_id isp1301_id[] = {
1627 { "isp1301_omap", 0 },
1628 { }
1629};
1630MODULE_DEVICE_TABLE(i2c, isp1301_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632static struct i2c_driver isp1301_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +01001633 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +01001634 .name = "isp1301_omap",
1635 },
Jean Delvare9df013b2008-10-14 17:30:02 +02001636 .probe = isp1301_probe,
1637 .remove = __exit_p(isp1301_remove),
1638 .id_table = isp1301_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639};
1640
1641/*-------------------------------------------------------------------------*/
1642
1643static int __init isp_init(void)
1644{
1645 return i2c_add_driver(&isp1301_driver);
1646}
Viral Mehta94a82482010-04-06 11:51:00 +05301647subsys_initcall(isp_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649static void __exit isp_exit(void)
1650{
1651 if (the_transceiver)
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001652 usb_set_transceiver(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 i2c_del_driver(&isp1301_driver);
1654}
1655module_exit(isp_exit);
1656