blob: 1e0e10dd6ba51904b34bb32a9155cba7a5173f8f [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 Lindgren70c494c2012-09-19 10:46:56 -070039#include <mach/mux.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Tony Lindgrenb924b202012-06-04 00:56:15 -070041#include <mach/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Greg Kroah-Hartman523e5312013-06-28 11:32:55 -070043#undef VERBOSE
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45
46#define DRIVER_VERSION "24 August 2004"
David Brownell25da3832007-08-14 18:37:14 +020047#define DRIVER_NAME (isp1301_driver.driver.name)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49MODULE_DESCRIPTION("ISP1301 USB OTG Transceiver Driver");
50MODULE_LICENSE("GPL");
51
52struct isp1301 {
Heikki Krogerus819d1c72012-02-13 13:24:08 +020053 struct usb_phy phy;
Felipe Balbia5f08a32008-10-14 17:30:02 +020054 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 void (*i2c_release)(struct device *dev);
56
David Brownell25da3832007-08-14 18:37:14 +020057 int irq_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 u32 last_otg_ctrl;
60 unsigned working:1;
61
62 struct timer_list timer;
63
64 /* use keventd context to change the state for us */
65 struct work_struct work;
David Brownell25da3832007-08-14 18:37:14 +020066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned long todo;
68# define WORK_UPDATE_ISP 0 /* update ISP from OTG */
69# define WORK_UPDATE_OTG 1 /* update OTG from ISP */
70# define WORK_HOST_RESUME 4 /* resume host */
71# define WORK_TIMER 6 /* timer fired */
72# define WORK_STOP 7 /* don't resubmit */
73};
74
75
Tony Lindgrenf35ae632008-07-03 12:24:43 +030076/* bits in OTG_CTRL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78#define OTG_XCEIV_OUTPUTS \
79 (OTG_ASESSVLD|OTG_BSESSEND|OTG_BSESSVLD|OTG_VBUSVLD|OTG_ID)
80#define OTG_XCEIV_INPUTS \
81 (OTG_PULLDOWN|OTG_PULLUP|OTG_DRV_VBUS|OTG_PD_VBUS|OTG_PU_VBUS|OTG_PU_ID)
82#define OTG_CTRL_BITS \
83 (OTG_A_BUSREQ|OTG_A_SETB_HNPEN|OTG_B_BUSREQ|OTG_B_HNPEN|OTG_BUSDROP)
84 /* and OTG_PULLUP is sometimes written */
85
86#define OTG_CTRL_MASK (OTG_DRIVER_SEL| \
87 OTG_XCEIV_OUTPUTS|OTG_XCEIV_INPUTS| \
88 OTG_CTRL_BITS)
89
90
91/*-------------------------------------------------------------------------*/
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* board-specific PM hooks */
94
David Brownelld1846b02008-11-28 15:24:38 +010095#if defined(CONFIG_MACH_OMAP_H2) || defined(CONFIG_MACH_OMAP_H3)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97#if defined(CONFIG_TPS65010) || defined(CONFIG_TPS65010_MODULE)
98
David Brownell6d16bfb2008-01-27 18:14:49 +010099#include <linux/i2c/tps65010.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101#else
102
103static inline int tps65010_set_vbus_draw(unsigned mA)
104{
105 pr_debug("tps65010: draw %d mA (STUB)\n", mA);
106 return 0;
107}
108
109#endif
110
111static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
112{
113 int status = tps65010_set_vbus_draw(mA);
114 if (status < 0)
115 pr_debug(" VBUS %d mA error %d\n", mA, status);
116}
117
Anand Gadiyard77282c2009-08-24 20:14:45 +0530118#else
David Brownelld1846b02008-11-28 15:24:38 +0100119
120static void enable_vbus_draw(struct isp1301 *isp, unsigned mA)
121{
122 /* H4 controls this by DIP switch S2.4; no soft control.
123 * ON means the charger is always enabled. Leave it OFF
124 * unless the OTG port is used only in B-peripheral mode.
125 */
126}
127
Anand Gadiyard77282c2009-08-24 20:14:45 +0530128#endif
129
David Brownelld1846b02008-11-28 15:24:38 +0100130static void enable_vbus_source(struct isp1301 *isp)
131{
132 /* this board won't supply more than 8mA vbus power.
133 * some boards can switch a 100ma "unit load" (or more).
134 */
135}
136
137
138/* products will deliver OTG messages with LEDs, GUI, etc */
139static inline void notresponding(struct isp1301 *isp)
140{
141 printk(KERN_NOTICE "OTG device not responding.\n");
142}
143
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*-------------------------------------------------------------------------*/
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147static struct i2c_driver isp1301_driver;
148
149/* smbus apis are used for portability */
150
151static inline u8
152isp1301_get_u8(struct isp1301 *isp, u8 reg)
153{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200154 return i2c_smbus_read_byte_data(isp->client, reg + 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157static inline int
158isp1301_get_u16(struct isp1301 *isp, u8 reg)
159{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200160 return i2c_smbus_read_word_data(isp->client, reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163static inline int
164isp1301_set_bits(struct isp1301 *isp, u8 reg, u8 bits)
165{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200166 return i2c_smbus_write_byte_data(isp->client, reg + 0, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
169static inline int
170isp1301_clear_bits(struct isp1301 *isp, u8 reg, u8 bits)
171{
Felipe Balbia5f08a32008-10-14 17:30:02 +0200172 return i2c_smbus_write_byte_data(isp->client, reg + 1, bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175/*-------------------------------------------------------------------------*/
176
177/* identification */
178#define ISP1301_VENDOR_ID 0x00 /* u16 read */
179#define ISP1301_PRODUCT_ID 0x02 /* u16 read */
180#define ISP1301_BCD_DEVICE 0x14 /* u16 read */
181
182#define I2C_VENDOR_ID_PHILIPS 0x04cc
183#define I2C_PRODUCT_ID_PHILIPS_1301 0x1301
184
185/* operational registers */
186#define ISP1301_MODE_CONTROL_1 0x04 /* u8 read, set, +1 clear */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300187# define MC1_SPEED (1 << 0)
188# define MC1_SUSPEND (1 << 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189# define MC1_DAT_SE0 (1 << 2)
190# define MC1_TRANSPARENT (1 << 3)
191# define MC1_BDIS_ACON_EN (1 << 4)
192# define MC1_OE_INT_EN (1 << 5)
193# define MC1_UART_EN (1 << 6)
194# define MC1_MASK 0x7f
195#define ISP1301_MODE_CONTROL_2 0x12 /* u8 read, set, +1 clear */
196# define MC2_GLOBAL_PWR_DN (1 << 0)
197# define MC2_SPD_SUSP_CTRL (1 << 1)
198# define MC2_BI_DI (1 << 2)
199# define MC2_TRANSP_BDIR0 (1 << 3)
200# define MC2_TRANSP_BDIR1 (1 << 4)
201# define MC2_AUDIO_EN (1 << 5)
202# define MC2_PSW_EN (1 << 6)
203# define MC2_EN2V7 (1 << 7)
204#define ISP1301_OTG_CONTROL_1 0x06 /* u8 read, set, +1 clear */
205# define OTG1_DP_PULLUP (1 << 0)
206# define OTG1_DM_PULLUP (1 << 1)
207# define OTG1_DP_PULLDOWN (1 << 2)
208# define OTG1_DM_PULLDOWN (1 << 3)
209# define OTG1_ID_PULLDOWN (1 << 4)
210# define OTG1_VBUS_DRV (1 << 5)
211# define OTG1_VBUS_DISCHRG (1 << 6)
212# define OTG1_VBUS_CHRG (1 << 7)
213#define ISP1301_OTG_STATUS 0x10 /* u8 readonly */
214# define OTG_B_SESS_END (1 << 6)
215# define OTG_B_SESS_VLD (1 << 7)
216
217#define ISP1301_INTERRUPT_SOURCE 0x08 /* u8 read */
218#define ISP1301_INTERRUPT_LATCH 0x0A /* u8 read, set, +1 clear */
219
220#define ISP1301_INTERRUPT_FALLING 0x0C /* u8 read, set, +1 clear */
221#define ISP1301_INTERRUPT_RISING 0x0E /* u8 read, set, +1 clear */
222
223/* same bitfields in all interrupt registers */
224# define INTR_VBUS_VLD (1 << 0)
225# define INTR_SESS_VLD (1 << 1)
226# define INTR_DP_HI (1 << 2)
227# define INTR_ID_GND (1 << 3)
228# define INTR_DM_HI (1 << 4)
229# define INTR_ID_FLOAT (1 << 5)
230# define INTR_BDIS_ACON (1 << 6)
231# define INTR_CR_INT (1 << 7)
232
233/*-------------------------------------------------------------------------*/
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235static inline const char *state_name(struct isp1301 *isp)
236{
Antoine Tenarte47d9252014-10-30 18:41:13 +0100237 return usb_otg_state_string(isp->phy.otg->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*-------------------------------------------------------------------------*/
241
242/* NOTE: some of this ISP1301 setup is specific to H2 boards;
243 * not everything is guarded by board-specific checks, or even using
244 * omap_usb_config data to deduce MC1_DAT_SE0 and MC2_BI_DI.
245 *
246 * ALSO: this currently doesn't use ISP1301 low-power modes
247 * while OTG is running.
248 */
249
250static void power_down(struct isp1301 *isp)
251{
Antoine Tenarte47d9252014-10-30 18:41:13 +0100252 isp->phy.otg->state = OTG_STATE_UNDEFINED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 // isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300255 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_ID_PULLDOWN);
258 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
259}
260
261static void power_up(struct isp1301 *isp)
262{
263 // isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, MC2_GLOBAL_PWR_DN);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300264 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_SUSPEND);
David Brownell25da3832007-08-14 18:37:14 +0200265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 /* do this only when cpu is driving transceiver,
267 * so host won't see a low speed device...
268 */
269 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
270}
271
272#define NO_HOST_SUSPEND
273
274static int host_suspend(struct isp1301 *isp)
275{
276#ifdef NO_HOST_SUSPEND
277 return 0;
278#else
279 struct device *dev;
280
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200281 if (!isp->phy.otg->host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return -ENODEV;
283
284 /* Currently ASSUMES only the OTG port matters;
285 * other ports could be active...
286 */
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200287 dev = isp->phy.otg->host->controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return dev->driver->suspend(dev, 3, 0);
289#endif
290}
291
292static int host_resume(struct isp1301 *isp)
293{
294#ifdef NO_HOST_SUSPEND
295 return 0;
296#else
297 struct device *dev;
298
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200299 if (!isp->phy.otg->host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return -ENODEV;
301
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200302 dev = isp->phy.otg->host->controller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return dev->driver->resume(dev, 0);
304#endif
305}
306
307static int gadget_suspend(struct isp1301 *isp)
308{
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200309 isp->phy.otg->gadget->b_hnp_enable = 0;
310 isp->phy.otg->gadget->a_hnp_support = 0;
311 isp->phy.otg->gadget->a_alt_hnp_support = 0;
312 return usb_gadget_vbus_disconnect(isp->phy.otg->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315/*-------------------------------------------------------------------------*/
316
317#define TIMER_MINUTES 10
318#define TIMER_JIFFIES (TIMER_MINUTES * 60 * HZ)
319
320/* Almost all our I2C messaging comes from a work queue's task context.
321 * NOTE: guaranteeing certain response times might mean we shouldn't
322 * share keventd's work queue; a realtime task might be safest.
323 */
David Brownelld1846b02008-11-28 15:24:38 +0100324static void isp1301_defer_work(struct isp1301 *isp, int work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
326 int status;
327
328 if (isp && !test_and_set_bit(work, &isp->todo)) {
Felipe Balbia5f08a32008-10-14 17:30:02 +0200329 (void) get_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 status = schedule_work(&isp->work);
331 if (!status && !isp->working)
Felipe Balbia5f08a32008-10-14 17:30:02 +0200332 dev_vdbg(&isp->client->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 "work item %d may be lost\n", work);
334 }
335}
336
337/* called from irq handlers */
338static void a_idle(struct isp1301 *isp, const char *tag)
339{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300340 u32 l;
341
Antoine Tenarte47d9252014-10-30 18:41:13 +0100342 if (isp->phy.otg->state == OTG_STATE_A_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return;
344
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200345 isp->phy.otg->default_a = 1;
346 if (isp->phy.otg->host) {
347 isp->phy.otg->host->is_b_host = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 host_suspend(isp);
349 }
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200350 if (isp->phy.otg->gadget) {
351 isp->phy.otg->gadget->is_a_peripheral = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 gadget_suspend(isp);
353 }
Antoine Tenarte47d9252014-10-30 18:41:13 +0100354 isp->phy.otg->state = OTG_STATE_A_IDLE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300355 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
356 omap_writel(l, OTG_CTRL);
357 isp->last_otg_ctrl = l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 pr_debug(" --> %s/%s\n", state_name(isp), tag);
359}
360
361/* called from irq handlers */
362static void b_idle(struct isp1301 *isp, const char *tag)
363{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300364 u32 l;
365
Antoine Tenarte47d9252014-10-30 18:41:13 +0100366 if (isp->phy.otg->state == OTG_STATE_B_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return;
368
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200369 isp->phy.otg->default_a = 0;
370 if (isp->phy.otg->host) {
371 isp->phy.otg->host->is_b_host = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 host_suspend(isp);
373 }
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200374 if (isp->phy.otg->gadget) {
375 isp->phy.otg->gadget->is_a_peripheral = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 gadget_suspend(isp);
377 }
Antoine Tenarte47d9252014-10-30 18:41:13 +0100378 isp->phy.otg->state = OTG_STATE_B_IDLE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300379 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
380 omap_writel(l, OTG_CTRL);
381 isp->last_otg_ctrl = l;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 pr_debug(" --> %s/%s\n", state_name(isp), tag);
383}
384
385static void
386dump_regs(struct isp1301 *isp, const char *label)
387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 u8 ctrl = isp1301_get_u8(isp, ISP1301_OTG_CONTROL_1);
389 u8 status = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
390 u8 src = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
391
392 pr_debug("otg: %06x, %s %s, otg/%02x stat/%02x.%02x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300393 omap_readl(OTG_CTRL), label, state_name(isp),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ctrl, status, src);
395 /* mode control and irq enables don't change much */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/*-------------------------------------------------------------------------*/
399
400#ifdef CONFIG_USB_OTG
401
402/*
403 * The OMAP OTG controller handles most of the OTG state transitions.
404 *
405 * We translate isp1301 outputs (mostly voltage comparator status) into
406 * OTG inputs; OTG outputs (mostly pullup/pulldown controls) and HNP state
407 * flags into isp1301 inputs ... and infer state transitions.
408 */
409
410#ifdef VERBOSE
411
412static void check_state(struct isp1301 *isp, const char *tag)
413{
414 enum usb_otg_state state = OTG_STATE_UNDEFINED;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300415 u8 fsm = omap_readw(OTG_TEST) & 0x0ff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned extra = 0;
417
418 switch (fsm) {
419
420 /* default-b */
421 case 0x0:
422 state = OTG_STATE_B_IDLE;
423 break;
424 case 0x3:
425 case 0x7:
426 extra = 1;
427 case 0x1:
428 state = OTG_STATE_B_PERIPHERAL;
429 break;
430 case 0x11:
431 state = OTG_STATE_B_SRP_INIT;
432 break;
433
434 /* extra dual-role default-b states */
435 case 0x12:
436 case 0x13:
437 case 0x16:
438 extra = 1;
439 case 0x17:
440 state = OTG_STATE_B_WAIT_ACON;
441 break;
442 case 0x34:
443 state = OTG_STATE_B_HOST;
444 break;
445
446 /* default-a */
447 case 0x36:
448 state = OTG_STATE_A_IDLE;
449 break;
450 case 0x3c:
451 state = OTG_STATE_A_WAIT_VFALL;
452 break;
453 case 0x7d:
454 state = OTG_STATE_A_VBUS_ERR;
455 break;
456 case 0x9e:
457 case 0x9f:
458 extra = 1;
459 case 0x89:
460 state = OTG_STATE_A_PERIPHERAL;
461 break;
462 case 0xb7:
463 state = OTG_STATE_A_WAIT_VRISE;
464 break;
465 case 0xb8:
466 state = OTG_STATE_A_WAIT_BCON;
467 break;
468 case 0xb9:
469 state = OTG_STATE_A_HOST;
470 break;
471 case 0xba:
472 state = OTG_STATE_A_SUSPEND;
473 break;
474 default:
475 break;
476 }
Antoine Tenarte47d9252014-10-30 18:41:13 +0100477 if (isp->phy.otg->state == state && !extra)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return;
479 pr_debug("otg: %s FSM %s/%02x, %s, %06x\n", tag,
Felipe Balbi42c0bf12013-03-07 10:39:57 +0200480 usb_otg_state_string(state), fsm, state_name(isp),
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300481 omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484#else
485
486static inline void check_state(struct isp1301 *isp, const char *tag) { }
487
488#endif
489
490/* outputs from ISP1301_INTERRUPT_SOURCE */
491static void update_otg1(struct isp1301 *isp, u8 int_src)
492{
493 u32 otg_ctrl;
494
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300495 otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
496 otg_ctrl &= ~OTG_XCEIV_INPUTS;
497 otg_ctrl &= ~(OTG_ID|OTG_ASESSVLD|OTG_VBUSVLD);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (int_src & INTR_SESS_VLD)
500 otg_ctrl |= OTG_ASESSVLD;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100501 else if (isp->phy.otg->state == OTG_STATE_A_WAIT_VFALL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 a_idle(isp, "vfall");
503 otg_ctrl &= ~OTG_CTRL_BITS;
504 }
505 if (int_src & INTR_VBUS_VLD)
506 otg_ctrl |= OTG_VBUSVLD;
507 if (int_src & INTR_ID_GND) { /* default-A */
Antoine Tenarte47d9252014-10-30 18:41:13 +0100508 if (isp->phy.otg->state == OTG_STATE_B_IDLE
509 || isp->phy.otg->state
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200510 == OTG_STATE_UNDEFINED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 a_idle(isp, "init");
512 return;
513 }
514 } else { /* default-B */
515 otg_ctrl |= OTG_ID;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100516 if (isp->phy.otg->state == OTG_STATE_A_IDLE
517 || isp->phy.otg->state == OTG_STATE_UNDEFINED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 b_idle(isp, "init");
519 return;
520 }
521 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300522 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525/* outputs from ISP1301_OTG_STATUS */
526static void update_otg2(struct isp1301 *isp, u8 otg_status)
527{
528 u32 otg_ctrl;
529
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300530 otg_ctrl = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
531 otg_ctrl &= ~OTG_XCEIV_INPUTS;
532 otg_ctrl &= ~(OTG_BSESSVLD | OTG_BSESSEND);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (otg_status & OTG_B_SESS_VLD)
534 otg_ctrl |= OTG_BSESSVLD;
535 else if (otg_status & OTG_B_SESS_END)
536 otg_ctrl |= OTG_BSESSEND;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300537 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/* inputs going to ISP1301 */
541static void otg_update_isp(struct isp1301 *isp)
542{
543 u32 otg_ctrl, otg_change;
544 u8 set = OTG1_DM_PULLDOWN, clr = OTG1_DM_PULLUP;
545
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300546 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 otg_change = otg_ctrl ^ isp->last_otg_ctrl;
548 isp->last_otg_ctrl = otg_ctrl;
549 otg_ctrl = otg_ctrl & OTG_XCEIV_INPUTS;
550
Antoine Tenarte47d9252014-10-30 18:41:13 +0100551 switch (isp->phy.otg->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 case OTG_STATE_B_IDLE:
553 case OTG_STATE_B_PERIPHERAL:
554 case OTG_STATE_B_SRP_INIT:
555 if (!(otg_ctrl & OTG_PULLUP)) {
556 // if (otg_ctrl & OTG_B_HNPEN) {
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200557 if (isp->phy.otg->gadget->b_hnp_enable) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100558 isp->phy.otg->state = OTG_STATE_B_WAIT_ACON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 pr_debug(" --> b_wait_acon\n");
560 }
561 goto pulldown;
562 }
563pullup:
564 set |= OTG1_DP_PULLUP;
565 clr |= OTG1_DP_PULLDOWN;
566 break;
567 case OTG_STATE_A_SUSPEND:
568 case OTG_STATE_A_PERIPHERAL:
569 if (otg_ctrl & OTG_PULLUP)
570 goto pullup;
571 /* FALLTHROUGH */
572 // case OTG_STATE_B_WAIT_ACON:
573 default:
574pulldown:
575 set |= OTG1_DP_PULLDOWN;
576 clr |= OTG1_DP_PULLUP;
577 break;
578 }
579
580# define toggle(OTG,ISP) do { \
581 if (otg_ctrl & OTG) set |= ISP; \
582 else clr |= ISP; \
583 } while (0)
584
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200585 if (!(isp->phy.otg->host))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 otg_ctrl &= ~OTG_DRV_VBUS;
587
Antoine Tenarte47d9252014-10-30 18:41:13 +0100588 switch (isp->phy.otg->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 case OTG_STATE_A_SUSPEND:
590 if (otg_ctrl & OTG_DRV_VBUS) {
591 set |= OTG1_VBUS_DRV;
592 break;
593 }
594 /* HNP failed for some reason (A_AIDL_BDIS timeout) */
595 notresponding(isp);
596
597 /* FALLTHROUGH */
598 case OTG_STATE_A_VBUS_ERR:
Antoine Tenarte47d9252014-10-30 18:41:13 +0100599 isp->phy.otg->state = OTG_STATE_A_WAIT_VFALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 pr_debug(" --> a_wait_vfall\n");
601 /* FALLTHROUGH */
602 case OTG_STATE_A_WAIT_VFALL:
603 /* FIXME usbcore thinks port power is still on ... */
604 clr |= OTG1_VBUS_DRV;
605 break;
606 case OTG_STATE_A_IDLE:
607 if (otg_ctrl & OTG_DRV_VBUS) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100608 isp->phy.otg->state = OTG_STATE_A_WAIT_VRISE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 pr_debug(" --> a_wait_vrise\n");
610 }
611 /* FALLTHROUGH */
612 default:
613 toggle(OTG_DRV_VBUS, OTG1_VBUS_DRV);
614 }
615
616 toggle(OTG_PU_VBUS, OTG1_VBUS_CHRG);
617 toggle(OTG_PD_VBUS, OTG1_VBUS_DISCHRG);
618
619# undef toggle
620
621 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, set);
622 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, clr);
623
624 /* HNP switch to host or peripheral; and SRP */
625 if (otg_change & OTG_PULLUP) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300626 u32 l;
627
Antoine Tenarte47d9252014-10-30 18:41:13 +0100628 switch (isp->phy.otg->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 case OTG_STATE_B_IDLE:
630 if (clr & OTG1_DP_PULLUP)
631 break;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100632 isp->phy.otg->state = OTG_STATE_B_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 pr_debug(" --> b_peripheral\n");
634 break;
635 case OTG_STATE_A_SUSPEND:
636 if (clr & OTG1_DP_PULLUP)
637 break;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100638 isp->phy.otg->state = OTG_STATE_A_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 pr_debug(" --> a_peripheral\n");
640 break;
641 default:
642 break;
643 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300644 l = omap_readl(OTG_CTRL);
645 l |= OTG_PULLUP;
646 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
648
Harvey Harrison08882d22008-04-22 22:16:47 +0200649 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 dump_regs(isp, "otg->isp1301");
651}
652
David Howells7d12e782006-10-05 14:55:46 +0100653static irqreturn_t omap_otg_irq(int irq, void *_isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300655 u16 otg_irq = omap_readw(OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 u32 otg_ctrl;
657 int ret = IRQ_NONE;
658 struct isp1301 *isp = _isp;
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200659 struct usb_otg *otg = isp->phy.otg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Joe Perches7c9d4402011-06-23 11:39:20 -0700661 /* update ISP1301 transceiver from OTG controller */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (otg_irq & OPRT_CHG) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300663 omap_writew(OPRT_CHG, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 isp1301_defer_work(isp, WORK_UPDATE_ISP);
665 ret = IRQ_HANDLED;
666
667 /* SRP to become b_peripheral failed */
668 } else if (otg_irq & B_SRP_TMROUT) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300669 pr_debug("otg: B_SRP_TIMEOUT, %06x\n", omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 notresponding(isp);
671
672 /* gadget drivers that care should monitor all kinds of
673 * remote wakeup (SRP, normal) using their own timer
674 * to give "check cable and A-device" messages.
675 */
Antoine Tenarte47d9252014-10-30 18:41:13 +0100676 if (isp->phy.otg->state == OTG_STATE_B_SRP_INIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 b_idle(isp, "srp_timeout");
678
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300679 omap_writew(B_SRP_TMROUT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 ret = IRQ_HANDLED;
681
682 /* HNP to become b_host failed */
683 } else if (otg_irq & B_HNP_FAIL) {
684 pr_debug("otg: %s B_HNP_FAIL, %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300685 state_name(isp), omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 notresponding(isp);
687
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300688 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 otg_ctrl |= OTG_BUSDROP;
690 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300691 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* subset of b_peripheral()... */
Antoine Tenarte47d9252014-10-30 18:41:13 +0100694 isp->phy.otg->state = OTG_STATE_B_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 pr_debug(" --> b_peripheral\n");
696
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300697 omap_writew(B_HNP_FAIL, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ret = IRQ_HANDLED;
699
700 /* detect SRP from B-device ... */
701 } else if (otg_irq & A_SRP_DETECT) {
702 pr_debug("otg: %s SRP_DETECT, %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300703 state_name(isp), omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 isp1301_defer_work(isp, WORK_UPDATE_OTG);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100706 switch (isp->phy.otg->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 case OTG_STATE_A_IDLE:
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200708 if (!otg->host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 break;
710 isp1301_defer_work(isp, WORK_HOST_RESUME);
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300711 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 otg_ctrl |= OTG_A_BUSREQ;
713 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
714 & ~OTG_XCEIV_INPUTS
715 & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300716 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 break;
718 default:
719 break;
720 }
721
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300722 omap_writew(A_SRP_DETECT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ret = IRQ_HANDLED;
724
725 /* timer expired: T(a_wait_bcon) and maybe T(a_wait_vrise)
726 * we don't track them separately
727 */
728 } else if (otg_irq & A_REQ_TMROUT) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300729 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 pr_info("otg: BCON_TMOUT from %s, %06x\n",
731 state_name(isp), otg_ctrl);
732 notresponding(isp);
733
734 otg_ctrl |= OTG_BUSDROP;
735 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300736 omap_writel(otg_ctrl, OTG_CTRL);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100737 isp->phy.otg->state = OTG_STATE_A_WAIT_VFALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300739 omap_writew(A_REQ_TMROUT, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 ret = IRQ_HANDLED;
741
742 /* A-supplied voltage fell too low; overcurrent */
743 } else if (otg_irq & A_VBUS_ERR) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300744 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 printk(KERN_ERR "otg: %s, VBUS_ERR %04x ctrl %06x\n",
746 state_name(isp), otg_irq, otg_ctrl);
747
748 otg_ctrl |= OTG_BUSDROP;
749 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300750 omap_writel(otg_ctrl, OTG_CTRL);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100751 isp->phy.otg->state = OTG_STATE_A_VBUS_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300753 omap_writew(A_VBUS_ERR, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 ret = IRQ_HANDLED;
755
Joe Perches7c9d4402011-06-23 11:39:20 -0700756 /* switch driver; the transceiver code activates it,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 * ungating the udc clock or resuming OHCI.
758 */
759 } else if (otg_irq & DRIVER_SWITCH) {
760 int kick = 0;
761
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300762 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 printk(KERN_NOTICE "otg: %s, SWITCH to %s, ctrl %06x\n",
764 state_name(isp),
765 (otg_ctrl & OTG_DRIVER_SEL)
766 ? "gadget" : "host",
767 otg_ctrl);
768 isp1301_defer_work(isp, WORK_UPDATE_ISP);
769
770 /* role is peripheral */
771 if (otg_ctrl & OTG_DRIVER_SEL) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100772 switch (isp->phy.otg->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 case OTG_STATE_A_IDLE:
Harvey Harrison08882d22008-04-22 22:16:47 +0200774 b_idle(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
776 default:
777 break;
778 }
779 isp1301_defer_work(isp, WORK_UPDATE_ISP);
780
781 /* role is host */
782 } else {
783 if (!(otg_ctrl & OTG_ID)) {
David Brownell25da3832007-08-14 18:37:14 +0200784 otg_ctrl &= OTG_CTRL_MASK & ~OTG_XCEIV_INPUTS;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300785 omap_writel(otg_ctrl | OTG_A_BUSREQ, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200788 if (otg->host) {
Antoine Tenarte47d9252014-10-30 18:41:13 +0100789 switch (isp->phy.otg->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 case OTG_STATE_B_WAIT_ACON:
Antoine Tenarte47d9252014-10-30 18:41:13 +0100791 isp->phy.otg->state = OTG_STATE_B_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 pr_debug(" --> b_host\n");
793 kick = 1;
794 break;
795 case OTG_STATE_A_WAIT_BCON:
Antoine Tenarte47d9252014-10-30 18:41:13 +0100796 isp->phy.otg->state = OTG_STATE_A_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 pr_debug(" --> a_host\n");
798 break;
799 case OTG_STATE_A_PERIPHERAL:
Antoine Tenarte47d9252014-10-30 18:41:13 +0100800 isp->phy.otg->state = OTG_STATE_A_WAIT_BCON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 pr_debug(" --> a_wait_bcon\n");
802 break;
803 default:
804 break;
805 }
806 isp1301_defer_work(isp, WORK_HOST_RESUME);
807 }
808 }
809
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300810 omap_writew(DRIVER_SWITCH, OTG_IRQ_SRC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 ret = IRQ_HANDLED;
812
813 if (kick)
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200814 usb_bus_start_enum(otg->host, otg->host->otg_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
Harvey Harrison08882d22008-04-22 22:16:47 +0200817 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return ret;
819}
820
821static struct platform_device *otg_dev;
822
Felipe Balbi465f8292009-12-15 23:19:52 +0200823static int isp1301_otg_init(struct isp1301 *isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300825 u32 l;
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (!otg_dev)
828 return -ENODEV;
829
Harvey Harrison08882d22008-04-22 22:16:47 +0200830 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* some of these values are board-specific... */
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300832 l = omap_readl(OTG_SYSCON_2);
833 l |= OTG_EN
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* for B-device: */
835 | SRP_GPDATA /* 9msec Bdev D+ pulse */
836 | SRP_GPDVBUS /* discharge after VBUS pulse */
837 // | (3 << 24) /* 2msec VBUS pulse */
838 /* for A-device: */
839 | (0 << 20) /* 200ms nominal A_WAIT_VRISE timer */
840 | SRP_DPW /* detect 167+ns SRP pulses */
841 | SRP_DATA | SRP_VBUS /* accept both kinds of SRP pulse */
842 ;
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300843 omap_writel(l, OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
846 update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
847
Harvey Harrison08882d22008-04-22 22:16:47 +0200848 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 pr_debug("otg: %s, %s %06x\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300850 state_name(isp), __func__, omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300852 omap_writew(DRIVER_SWITCH | OPRT_CHG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 | B_SRP_TMROUT | B_HNP_FAIL
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300854 | A_VBUS_ERR | A_SRP_DETECT | A_REQ_TMROUT, OTG_IRQ_EN);
855
856 l = omap_readl(OTG_SYSCON_2);
857 l |= OTG_EN;
858 omap_writel(l, OTG_SYSCON_2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 return 0;
861}
862
Russell King3ae5eae2005-11-09 22:32:44 +0000863static int otg_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 // struct omap_usb_config *config = dev->platform_data;
866
Russell King3ae5eae2005-11-09 22:32:44 +0000867 otg_dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
870
Russell King3ae5eae2005-11-09 22:32:44 +0000871static int otg_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
David Brownelld1846b02008-11-28 15:24:38 +0100873 otg_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return 0;
875}
876
David Brownelld1846b02008-11-28 15:24:38 +0100877static struct platform_driver omap_otg_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 .probe = otg_probe,
Russell King3ae5eae2005-11-09 22:32:44 +0000879 .remove = otg_remove,
880 .driver = {
Russell King3ae5eae2005-11-09 22:32:44 +0000881 .name = "omap_otg",
882 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883};
884
885static int otg_bind(struct isp1301 *isp)
886{
887 int status;
888
889 if (otg_dev)
890 return -EBUSY;
891
Russell King3ae5eae2005-11-09 22:32:44 +0000892 status = platform_driver_register(&omap_otg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (status < 0)
894 return status;
895
896 if (otg_dev)
897 status = request_irq(otg_dev->resource[1].start, omap_otg_irq,
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800898 0, DRIVER_NAME, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 else
900 status = -ENODEV;
901
902 if (status < 0)
Russell King3ae5eae2005-11-09 22:32:44 +0000903 platform_driver_unregister(&omap_otg_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return status;
905}
906
907static void otg_unbind(struct isp1301 *isp)
908{
909 if (!otg_dev)
910 return;
911 free_irq(otg_dev->resource[1].start, isp);
912}
913
914#else
915
916/* OTG controller isn't clocked */
917
918#endif /* CONFIG_USB_OTG */
919
920/*-------------------------------------------------------------------------*/
921
922static void b_peripheral(struct isp1301 *isp)
923{
Tony Lindgrenf35ae632008-07-03 12:24:43 +0300924 u32 l;
925
926 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
927 omap_writel(l, OTG_CTRL);
928
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200929 usb_gadget_vbus_connect(isp->phy.otg->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931#ifdef CONFIG_USB_OTG
932 enable_vbus_draw(isp, 8);
933 otg_update_isp(isp);
934#else
935 enable_vbus_draw(isp, 100);
936 /* UDC driver just set OTG_BSESSVLD */
937 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLUP);
938 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_DP_PULLDOWN);
Antoine Tenarte47d9252014-10-30 18:41:13 +0100939 isp->phy.otg->state = OTG_STATE_B_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 pr_debug(" --> b_peripheral\n");
941 dump_regs(isp, "2periph");
942#endif
943}
944
945static void isp_update_otg(struct isp1301 *isp, u8 stat)
946{
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200947 struct usb_otg *otg = isp->phy.otg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 u8 isp_stat, isp_bstat;
Antoine Tenarte47d9252014-10-30 18:41:13 +0100949 enum usb_otg_state state = isp->phy.otg->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (stat & INTR_BDIS_ACON)
952 pr_debug("OTG: BDIS_ACON, %s\n", state_name(isp));
953
954 /* start certain state transitions right away */
955 isp_stat = isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE);
956 if (isp_stat & INTR_ID_GND) {
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200957 if (otg->default_a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 switch (state) {
959 case OTG_STATE_B_IDLE:
960 a_idle(isp, "idle");
961 /* FALLTHROUGH */
962 case OTG_STATE_A_IDLE:
963 enable_vbus_source(isp);
964 /* FALLTHROUGH */
965 case OTG_STATE_A_WAIT_VRISE:
966 /* we skip over OTG_STATE_A_WAIT_BCON, since
967 * the HC will transition to A_HOST (or
968 * A_SUSPEND!) without our noticing except
969 * when HNP is used.
970 */
971 if (isp_stat & INTR_VBUS_VLD)
Antoine Tenarte47d9252014-10-30 18:41:13 +0100972 isp->phy.otg->state = OTG_STATE_A_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 break;
974 case OTG_STATE_A_WAIT_VFALL:
975 if (!(isp_stat & INTR_SESS_VLD))
976 a_idle(isp, "vfell");
977 break;
978 default:
979 if (!(isp_stat & INTR_VBUS_VLD))
Antoine Tenarte47d9252014-10-30 18:41:13 +0100980 isp->phy.otg->state = OTG_STATE_A_VBUS_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 }
983 isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
984 } else {
985 switch (state) {
986 case OTG_STATE_B_PERIPHERAL:
987 case OTG_STATE_B_HOST:
988 case OTG_STATE_B_WAIT_ACON:
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200989 usb_gadget_vbus_disconnect(otg->gadget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 break;
991 default:
992 break;
993 }
994 if (state != OTG_STATE_A_IDLE)
995 a_idle(isp, "id");
Heikki Krogerus819d1c72012-02-13 13:24:08 +0200996 if (otg->host && state == OTG_STATE_A_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 isp1301_defer_work(isp, WORK_HOST_RESUME);
998 isp_bstat = 0;
999 }
1000 } else {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001001 u32 l;
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* if user unplugged mini-A end of cable,
1004 * don't bypass A_WAIT_VFALL.
1005 */
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001006 if (otg->default_a) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 switch (state) {
1008 default:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001009 isp->phy.otg->state = OTG_STATE_A_WAIT_VFALL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 break;
1011 case OTG_STATE_A_WAIT_VFALL:
1012 state = OTG_STATE_A_IDLE;
Petr Mladek37ebb542014-09-19 17:32:23 +02001013 /* hub_wq may take a while to notice and
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 * handle this disconnect, so don't go
1015 * to B_IDLE quite yet.
1016 */
1017 break;
1018 case OTG_STATE_A_IDLE:
1019 host_suspend(isp);
1020 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1,
1021 MC1_BDIS_ACON_EN);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001022 isp->phy.otg->state = OTG_STATE_B_IDLE;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001023 l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1024 l &= ~OTG_CTRL_BITS;
1025 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 break;
1027 case OTG_STATE_B_IDLE:
1028 break;
1029 }
1030 }
1031 isp_bstat = isp1301_get_u8(isp, ISP1301_OTG_STATUS);
1032
Antoine Tenarte47d9252014-10-30 18:41:13 +01001033 switch (isp->phy.otg->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 case OTG_STATE_B_PERIPHERAL:
1035 case OTG_STATE_B_WAIT_ACON:
1036 case OTG_STATE_B_HOST:
1037 if (likely(isp_bstat & OTG_B_SESS_VLD))
1038 break;
1039 enable_vbus_draw(isp, 0);
1040#ifndef CONFIG_USB_OTG
1041 /* UDC driver will clear OTG_BSESSVLD */
1042 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1043 OTG1_DP_PULLDOWN);
1044 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1045 OTG1_DP_PULLUP);
Harvey Harrison08882d22008-04-22 22:16:47 +02001046 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047#endif
1048 /* FALLTHROUGH */
1049 case OTG_STATE_B_SRP_INIT:
Harvey Harrison08882d22008-04-22 22:16:47 +02001050 b_idle(isp, __func__);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001051 l = omap_readl(OTG_CTRL) & OTG_XCEIV_OUTPUTS;
1052 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* FALLTHROUGH */
1054 case OTG_STATE_B_IDLE:
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001055 if (otg->gadget && (isp_bstat & OTG_B_SESS_VLD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056#ifdef CONFIG_USB_OTG
1057 update_otg1(isp, isp_stat);
1058 update_otg2(isp, isp_bstat);
1059#endif
1060 b_peripheral(isp);
1061 } else if (!(isp_stat & (INTR_VBUS_VLD|INTR_SESS_VLD)))
1062 isp_bstat |= OTG_B_SESS_END;
1063 break;
1064 case OTG_STATE_A_WAIT_VFALL:
1065 break;
1066 default:
1067 pr_debug("otg: unsupported b-device %s\n",
1068 state_name(isp));
1069 break;
1070 }
1071 }
1072
Antoine Tenarte47d9252014-10-30 18:41:13 +01001073 if (state != isp->phy.otg->state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 pr_debug(" isp, %s -> %s\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +02001075 usb_otg_state_string(state), state_name(isp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077#ifdef CONFIG_USB_OTG
1078 /* update the OTG controller state to match the isp1301; may
1079 * trigger OPRT_CHG irqs for changes going to the isp1301.
1080 */
1081 update_otg1(isp, isp_stat);
1082 update_otg2(isp, isp_bstat);
Harvey Harrison08882d22008-04-22 22:16:47 +02001083 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084#endif
1085
1086 dump_regs(isp, "isp1301->otg");
1087}
1088
1089/*-------------------------------------------------------------------------*/
1090
1091static u8 isp1301_clear_latch(struct isp1301 *isp)
1092{
1093 u8 latch = isp1301_get_u8(isp, ISP1301_INTERRUPT_LATCH);
1094 isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, latch);
1095 return latch;
1096}
1097
1098static void
David Brownell25da3832007-08-14 18:37:14 +02001099isp1301_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
David Brownell25da3832007-08-14 18:37:14 +02001101 struct isp1301 *isp = container_of(work, struct isp1301, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 int stop;
1103
1104 /* implicit lock: we're the only task using this device */
1105 isp->working = 1;
1106 do {
1107 stop = test_bit(WORK_STOP, &isp->todo);
1108
1109#ifdef CONFIG_USB_OTG
1110 /* transfer state from otg engine to isp1301 */
1111 if (test_and_clear_bit(WORK_UPDATE_ISP, &isp->todo)) {
1112 otg_update_isp(isp);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001113 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
1115#endif
1116 /* transfer state from isp1301 to otg engine */
1117 if (test_and_clear_bit(WORK_UPDATE_OTG, &isp->todo)) {
1118 u8 stat = isp1301_clear_latch(isp);
1119
1120 isp_update_otg(isp, stat);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001121 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123
1124 if (test_and_clear_bit(WORK_HOST_RESUME, &isp->todo)) {
1125 u32 otg_ctrl;
1126
1127 /*
1128 * skip A_WAIT_VRISE; hc transitions invisibly
1129 * skip A_WAIT_BCON; same.
1130 */
Antoine Tenarte47d9252014-10-30 18:41:13 +01001131 switch (isp->phy.otg->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 case OTG_STATE_A_WAIT_BCON:
1133 case OTG_STATE_A_WAIT_VRISE:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001134 isp->phy.otg->state = OTG_STATE_A_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 pr_debug(" --> a_host\n");
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001136 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 otg_ctrl |= OTG_A_BUSREQ;
1138 otg_ctrl &= ~(OTG_BUSDROP|OTG_B_BUSREQ)
1139 & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001140 omap_writel(otg_ctrl, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
1142 case OTG_STATE_B_WAIT_ACON:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001143 isp->phy.otg->state = OTG_STATE_B_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 pr_debug(" --> b_host (acon)\n");
1145 break;
1146 case OTG_STATE_B_HOST:
1147 case OTG_STATE_B_IDLE:
1148 case OTG_STATE_A_IDLE:
1149 break;
1150 default:
1151 pr_debug(" host resume in %s\n",
1152 state_name(isp));
1153 }
1154 host_resume(isp);
1155 // mdelay(10);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001156 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158
1159 if (test_and_clear_bit(WORK_TIMER, &isp->todo)) {
1160#ifdef VERBOSE
1161 dump_regs(isp, "timer");
1162 if (!stop)
1163 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1164#endif
Felipe Balbia5f08a32008-10-14 17:30:02 +02001165 put_device(&isp->client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
1167
1168 if (isp->todo)
Felipe Balbia5f08a32008-10-14 17:30:02 +02001169 dev_vdbg(&isp->client->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 "work done, todo = 0x%lx\n",
1171 isp->todo);
1172 if (stop) {
Felipe Balbia5f08a32008-10-14 17:30:02 +02001173 dev_dbg(&isp->client->dev, "stop\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
1175 }
1176 } while (isp->todo);
1177 isp->working = 0;
1178}
1179
David Howells7d12e782006-10-05 14:55:46 +01001180static irqreturn_t isp1301_irq(int irq, void *isp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 isp1301_defer_work(isp, WORK_UPDATE_OTG);
1183 return IRQ_HANDLED;
1184}
1185
1186static void isp1301_timer(unsigned long _isp)
1187{
1188 isp1301_defer_work((void *)_isp, WORK_TIMER);
1189}
1190
1191/*-------------------------------------------------------------------------*/
1192
1193static void isp1301_release(struct device *dev)
1194{
1195 struct isp1301 *isp;
1196
Felipe Balbia5f08a32008-10-14 17:30:02 +02001197 isp = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
David Brownelld1846b02008-11-28 15:24:38 +01001199 /* FIXME -- not with a "new style" driver, it doesn't!! */
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* ugly -- i2c hijacks our memory hook to wait_for_completion() */
1202 if (isp->i2c_release)
1203 isp->i2c_release(dev);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001204 kfree(isp->phy.otg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 kfree (isp);
1206}
1207
1208static struct isp1301 *the_transceiver;
1209
Dmitry Torokhov39d35682013-02-24 00:55:07 -08001210static int isp1301_remove(struct i2c_client *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
1212 struct isp1301 *isp;
1213
Felipe Balbia5f08a32008-10-14 17:30:02 +02001214 isp = i2c_get_clientdata(i2c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1217 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
Jean Delvare9df013b2008-10-14 17:30:02 +02001218 free_irq(i2c->irq, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219#ifdef CONFIG_USB_OTG
1220 otg_unbind(isp);
1221#endif
1222 if (machine_is_omap_h2())
David Brownelld1846b02008-11-28 15:24:38 +01001223 gpio_free(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 isp->timer.data = 0;
1226 set_bit(WORK_STOP, &isp->todo);
1227 del_timer_sync(&isp->timer);
Tejun Heo43829732012-08-20 14:51:24 -07001228 flush_work(&isp->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 put_device(&i2c->dev);
David Brownelld1846b02008-11-28 15:24:38 +01001231 the_transceiver = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Jean Delvare9df013b2008-10-14 17:30:02 +02001233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236/*-------------------------------------------------------------------------*/
1237
1238/* NOTE: three modes are possible here, only one of which
1239 * will be standards-conformant on any given system:
1240 *
1241 * - OTG mode (dual-role), required if there's a Mini-AB connector
1242 * - HOST mode, for when there's one or more A (host) connectors
1243 * - DEVICE mode, for when there's a B/Mini-B (device) connector
1244 *
1245 * As a rule, you won't have an isp1301 chip unless it's there to
David Brownell25da3832007-08-14 18:37:14 +02001246 * support the OTG mode. Other modes help testing USB controllers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 * in isolation from (full) OTG support, or maybe so later board
1248 * revisions can help to support those feature.
1249 */
1250
1251#ifdef CONFIG_USB_OTG
1252
1253static int isp1301_otg_enable(struct isp1301 *isp)
1254{
1255 power_up(isp);
Felipe Balbi465f8292009-12-15 23:19:52 +02001256 isp1301_otg_init(isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 /* NOTE: since we don't change this, this provides
1259 * a few more interrupts than are strictly needed.
1260 */
1261 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001262 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001264 INTR_VBUS_VLD | INTR_SESS_VLD | INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Felipe Balbia5f08a32008-10-14 17:30:02 +02001266 dev_info(&isp->client->dev, "ready for dual-role USB ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 return 0;
1269}
1270
1271#endif
1272
1273/* add or disable the host device+driver */
1274static int
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001275isp1301_set_host(struct usb_otg *otg, struct usb_bus *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001277 struct isp1301 *isp = container_of(otg->usb_phy, struct isp1301, phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Aaro Koskinen8b42a742013-12-21 20:37:37 +02001279 if (isp != the_transceiver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return -ENODEV;
1281
1282 if (!host) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001283 omap_writew(0, OTG_IRQ_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 power_down(isp);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001285 otg->host = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return 0;
1287 }
1288
1289#ifdef CONFIG_USB_OTG
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001290 otg->host = host;
Felipe Balbia5f08a32008-10-14 17:30:02 +02001291 dev_dbg(&isp->client->dev, "registered host\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 host_suspend(isp);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001293 if (otg->gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return isp1301_otg_enable(isp);
1295 return 0;
1296
Paul Bolle77c2f022014-05-16 12:00:57 +02001297#elif !IS_ENABLED(CONFIG_USB_OMAP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 // FIXME update its refcount
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001299 otg->host = host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 power_up(isp);
1302
1303 if (machine_is_omap_h2())
1304 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1305
Felipe Balbia5f08a32008-10-14 17:30:02 +02001306 dev_info(&isp->client->dev, "A-Host sessions ok\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001308 INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001310 INTR_ID_GND);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 /* If this has a Mini-AB connector, this mode is highly
1313 * nonstandard ... but can be handy for testing, especially with
1314 * the Mini-A end of an OTG cable. (Or something nonstandard
1315 * like MiniB-to-StandardB, maybe built with a gender mender.)
1316 */
1317 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1, OTG1_VBUS_DRV);
1318
Harvey Harrison08882d22008-04-22 22:16:47 +02001319 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 return 0;
1322
1323#else
Felipe Balbia5f08a32008-10-14 17:30:02 +02001324 dev_dbg(&isp->client->dev, "host sessions not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return -EINVAL;
1326#endif
1327
1328}
1329
1330static int
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001331isp1301_set_peripheral(struct usb_otg *otg, struct usb_gadget *gadget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001333 struct isp1301 *isp = container_of(otg->usb_phy, struct isp1301, phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Aaro Koskinen8b42a742013-12-21 20:37:37 +02001335 if (isp != the_transceiver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return -ENODEV;
1337
1338 if (!gadget) {
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001339 omap_writew(0, OTG_IRQ_EN);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001340 if (!otg->default_a)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 enable_vbus_draw(isp, 0);
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001342 usb_gadget_vbus_disconnect(otg->gadget);
1343 otg->gadget = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 power_down(isp);
1345 return 0;
1346 }
1347
1348#ifdef CONFIG_USB_OTG
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001349 otg->gadget = gadget;
Felipe Balbia5f08a32008-10-14 17:30:02 +02001350 dev_dbg(&isp->client->dev, "registered gadget\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /* gadget driver may be suspended until vbus_connect () */
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001352 if (otg->host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return isp1301_otg_enable(isp);
1354 return 0;
1355
1356#elif !defined(CONFIG_USB_OHCI_HCD) && !defined(CONFIG_USB_OHCI_HCD_MODULE)
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001357 otg->gadget = gadget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 // FIXME update its refcount
1359
Paul Walmsley79d66802012-04-20 15:29:20 -06001360 {
1361 u32 l;
1362
1363 l = omap_readl(OTG_CTRL) & OTG_CTRL_MASK;
1364 l &= ~(OTG_XCEIV_OUTPUTS|OTG_CTRL_BITS);
1365 l |= OTG_ID;
1366 omap_writel(l, OTG_CTRL);
1367 }
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 power_up(isp);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001370 isp->phy.otg->state = OTG_STATE_B_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
David Brownelld1846b02008-11-28 15:24:38 +01001372 if (machine_is_omap_h2() || machine_is_omap_h3())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1, MC1_DAT_SE0);
1374
1375 isp1301_set_bits(isp, ISP1301_INTERRUPT_RISING,
David Brownell25da3832007-08-14 18:37:14 +02001376 INTR_SESS_VLD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 isp1301_set_bits(isp, ISP1301_INTERRUPT_FALLING,
David Brownell25da3832007-08-14 18:37:14 +02001378 INTR_VBUS_VLD);
Felipe Balbia5f08a32008-10-14 17:30:02 +02001379 dev_info(&isp->client->dev, "B-Peripheral sessions ok\n");
Harvey Harrison08882d22008-04-22 22:16:47 +02001380 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 /* If this has a Mini-AB connector, this mode is highly
1383 * nonstandard ... but can be handy for testing, so long
1384 * as you don't plug a Mini-A cable into the jack.
1385 */
1386 if (isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE) & INTR_VBUS_VLD)
1387 b_peripheral(isp);
1388
1389 return 0;
1390
1391#else
Felipe Balbia5f08a32008-10-14 17:30:02 +02001392 dev_dbg(&isp->client->dev, "peripheral sessions not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -EINVAL;
1394#endif
1395}
1396
1397
1398/*-------------------------------------------------------------------------*/
1399
1400static int
Heikki Krogerus86753812012-02-13 13:24:02 +02001401isp1301_set_power(struct usb_phy *dev, unsigned mA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
1403 if (!the_transceiver)
1404 return -ENODEV;
Antoine Tenarte47d9252014-10-30 18:41:13 +01001405 if (dev->otg->state == OTG_STATE_B_PERIPHERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 enable_vbus_draw(the_transceiver, mA);
1407 return 0;
1408}
1409
1410static int
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001411isp1301_start_srp(struct usb_otg *otg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001413 struct isp1301 *isp = container_of(otg->usb_phy, struct isp1301, phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 u32 otg_ctrl;
1415
Antoine Tenarte47d9252014-10-30 18:41:13 +01001416 if (isp != the_transceiver || isp->phy.otg->state != OTG_STATE_B_IDLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -ENODEV;
1418
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001419 otg_ctrl = omap_readl(OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (!(otg_ctrl & OTG_BSESSEND))
1421 return -EINVAL;
1422
1423 otg_ctrl |= OTG_B_BUSREQ;
1424 otg_ctrl &= ~OTG_A_BUSREQ & OTG_CTRL_MASK;
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001425 omap_writel(otg_ctrl, OTG_CTRL);
Antoine Tenarte47d9252014-10-30 18:41:13 +01001426 isp->phy.otg->state = OTG_STATE_B_SRP_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001428 pr_debug("otg: SRP, %s ... %06x\n", state_name(isp),
1429 omap_readl(OTG_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430#ifdef CONFIG_USB_OTG
Harvey Harrison08882d22008-04-22 22:16:47 +02001431 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#endif
1433 return 0;
1434}
1435
1436static int
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001437isp1301_start_hnp(struct usb_otg *otg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439#ifdef CONFIG_USB_OTG
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001440 struct isp1301 *isp = container_of(otg->usb_phy, struct isp1301, phy);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001441 u32 l;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Aaro Koskinen8b42a742013-12-21 20:37:37 +02001443 if (isp != the_transceiver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 return -ENODEV;
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001445 if (otg->default_a && (otg->host == NULL || !otg->host->b_hnp_enable))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 return -ENOTCONN;
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001447 if (!otg->default_a && (otg->gadget == NULL
1448 || !otg->gadget->b_hnp_enable))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return -ENOTCONN;
1450
1451 /* We want hardware to manage most HNP protocol timings.
1452 * So do this part as early as possible...
1453 */
Antoine Tenarte47d9252014-10-30 18:41:13 +01001454 switch (isp->phy.otg->state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 case OTG_STATE_B_HOST:
Antoine Tenarte47d9252014-10-30 18:41:13 +01001456 isp->phy.otg->state = OTG_STATE_B_PERIPHERAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 /* caller will suspend next */
1458 break;
1459 case OTG_STATE_A_HOST:
1460#if 0
1461 /* autoconnect mode avoids irq latency bugs */
1462 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
1463 MC1_BDIS_ACON_EN);
1464#endif
1465 /* caller must suspend then clear A_BUSREQ */
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001466 usb_gadget_vbus_connect(otg->gadget);
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001467 l = omap_readl(OTG_CTRL);
1468 l |= OTG_A_SETB_HNPEN;
1469 omap_writel(l, OTG_CTRL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 break;
1472 case OTG_STATE_A_PERIPHERAL:
1473 /* initiated by B-Host suspend */
1474 break;
1475 default:
1476 return -EILSEQ;
1477 }
1478 pr_debug("otg: HNP %s, %06x ...\n",
Tony Lindgrenf35ae632008-07-03 12:24:43 +03001479 state_name(isp), omap_readl(OTG_CTRL));
Harvey Harrison08882d22008-04-22 22:16:47 +02001480 check_state(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return 0;
1482#else
1483 /* srp-only */
1484 return -EINVAL;
1485#endif
1486}
1487
1488/*-------------------------------------------------------------------------*/
1489
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001490static int
David Brownelld1846b02008-11-28 15:24:38 +01001491isp1301_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
1493 int status;
1494 struct isp1301 *isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 if (the_transceiver)
1497 return 0;
1498
Pekka Enberg82ca76b2005-09-06 15:18:35 -07001499 isp = kzalloc(sizeof *isp, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (!isp)
1501 return 0;
1502
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001503 isp->phy.otg = kzalloc(sizeof *isp->phy.otg, GFP_KERNEL);
1504 if (!isp->phy.otg) {
1505 kfree(isp);
1506 return 0;
1507 }
1508
David Brownell25da3832007-08-14 18:37:14 +02001509 INIT_WORK(&isp->work, isp1301_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 init_timer(&isp->timer);
1511 isp->timer.function = isp1301_timer;
1512 isp->timer.data = (unsigned long) isp;
1513
Jean Delvare9df013b2008-10-14 17:30:02 +02001514 i2c_set_clientdata(i2c, isp);
1515 isp->client = i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001517 /* verify the chip (shouldn't be necessary) */
Jean Delvare9df013b2008-10-14 17:30:02 +02001518 status = isp1301_get_u16(isp, ISP1301_VENDOR_ID);
1519 if (status != I2C_VENDOR_ID_PHILIPS) {
1520 dev_dbg(&i2c->dev, "not philips id: %d\n", status);
1521 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Jean Delvare9df013b2008-10-14 17:30:02 +02001523 status = isp1301_get_u16(isp, ISP1301_PRODUCT_ID);
1524 if (status != I2C_PRODUCT_ID_PHILIPS_1301) {
1525 dev_dbg(&i2c->dev, "not isp1301, %d\n", status);
1526 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528 isp->i2c_release = i2c->dev.release;
1529 i2c->dev.release = isp1301_release;
1530
1531 /* initial development used chiprev 2.00 */
1532 status = i2c_smbus_read_word_data(i2c, ISP1301_BCD_DEVICE);
1533 dev_info(&i2c->dev, "chiprev %x.%02x, driver " DRIVER_VERSION "\n",
1534 status >> 8, status & 0xff);
1535
1536 /* make like power-on reset */
1537 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_1, MC1_MASK);
1538
1539 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2, MC2_BI_DI);
1540 isp1301_clear_bits(isp, ISP1301_MODE_CONTROL_2, ~MC2_BI_DI);
1541
1542 isp1301_set_bits(isp, ISP1301_OTG_CONTROL_1,
1543 OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
1544 isp1301_clear_bits(isp, ISP1301_OTG_CONTROL_1,
1545 ~(OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN));
1546
1547 isp1301_clear_bits(isp, ISP1301_INTERRUPT_LATCH, ~0);
1548 isp1301_clear_bits(isp, ISP1301_INTERRUPT_FALLING, ~0);
1549 isp1301_clear_bits(isp, ISP1301_INTERRUPT_RISING, ~0);
1550
1551#ifdef CONFIG_USB_OTG
1552 status = otg_bind(isp);
1553 if (status < 0) {
1554 dev_dbg(&i2c->dev, "can't bind OTG\n");
Jean Delvare9df013b2008-10-14 17:30:02 +02001555 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557#endif
1558
1559 if (machine_is_omap_h2()) {
1560 /* full speed signaling by default */
1561 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_1,
Dmitry Baryshkovcbbcb9b2008-08-07 16:29:24 +04001562 MC1_SPEED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 isp1301_set_bits(isp, ISP1301_MODE_CONTROL_2,
1564 MC2_SPD_SUSP_CTRL);
1565
1566 /* IRQ wired at M14 */
1567 omap_cfg_reg(M14_1510_GPIO2);
David Brownell25da3832007-08-14 18:37:14 +02001568 if (gpio_request(2, "isp1301") == 0)
1569 gpio_direction_input(2);
1570 isp->irq_type = IRQF_TRIGGER_FALLING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572
Jean Delvare9df013b2008-10-14 17:30:02 +02001573 status = request_irq(i2c->irq, isp1301_irq,
David Brownell25da3832007-08-14 18:37:14 +02001574 isp->irq_type, DRIVER_NAME, isp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (status < 0) {
1576 dev_dbg(&i2c->dev, "can't get IRQ %d, err %d\n",
Jean Delvare9df013b2008-10-14 17:30:02 +02001577 i2c->irq, status);
1578 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 }
1580
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001581 isp->phy.dev = &i2c->dev;
1582 isp->phy.label = DRIVER_NAME;
1583 isp->phy.set_power = isp1301_set_power,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Antoine Tenart19c1eac2014-10-30 18:41:14 +01001585 isp->phy.otg->usb_phy = &isp->phy;
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001586 isp->phy.otg->set_host = isp1301_set_host,
1587 isp->phy.otg->set_peripheral = isp1301_set_peripheral,
1588 isp->phy.otg->start_srp = isp1301_start_srp,
1589 isp->phy.otg->start_hnp = isp1301_start_hnp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 enable_vbus_draw(isp, 0);
1592 power_down(isp);
1593 the_transceiver = isp;
1594
1595#ifdef CONFIG_USB_OTG
1596 update_otg1(isp, isp1301_get_u8(isp, ISP1301_INTERRUPT_SOURCE));
1597 update_otg2(isp, isp1301_get_u8(isp, ISP1301_OTG_STATUS));
1598#endif
1599
Harvey Harrison08882d22008-04-22 22:16:47 +02001600 dump_regs(isp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602#ifdef VERBOSE
1603 mod_timer(&isp->timer, jiffies + TIMER_JIFFIES);
1604 dev_dbg(&i2c->dev, "scheduled timer, %d min\n", TIMER_MINUTES);
1605#endif
1606
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301607 status = usb_add_phy(&isp->phy, USB_PHY_TYPE_USB2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (status < 0)
1609 dev_err(&i2c->dev, "can't register transceiver, %d\n",
1610 status);
1611
1612 return 0;
Jean Delvare9df013b2008-10-14 17:30:02 +02001613
1614fail:
Heikki Krogerus819d1c72012-02-13 13:24:08 +02001615 kfree(isp->phy.otg);
Jean Delvare9df013b2008-10-14 17:30:02 +02001616 kfree(isp);
1617 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Jean Delvare9df013b2008-10-14 17:30:02 +02001620static const struct i2c_device_id isp1301_id[] = {
1621 { "isp1301_omap", 0 },
1622 { }
1623};
1624MODULE_DEVICE_TABLE(i2c, isp1301_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626static struct i2c_driver isp1301_driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +01001627 .driver = {
Laurent Riffarda9718b02005-11-26 20:36:00 +01001628 .name = "isp1301_omap",
1629 },
Jean Delvare9df013b2008-10-14 17:30:02 +02001630 .probe = isp1301_probe,
Dmitry Torokhov39d35682013-02-24 00:55:07 -08001631 .remove = isp1301_remove,
Jean Delvare9df013b2008-10-14 17:30:02 +02001632 .id_table = isp1301_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633};
1634
1635/*-------------------------------------------------------------------------*/
1636
1637static int __init isp_init(void)
1638{
1639 return i2c_add_driver(&isp1301_driver);
1640}
Viral Mehta94a82482010-04-06 11:51:00 +05301641subsys_initcall(isp_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643static void __exit isp_exit(void)
1644{
1645 if (the_transceiver)
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +05301646 usb_remove_phy(&the_transceiver->phy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 i2c_del_driver(&isp1301_driver);
1648}
1649module_exit(isp_exit);
1650