| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2005-2007 by Texas Instruments | 
 | 3 |  * Some code has been taken from tusb6010.c | 
 | 4 |  * Copyrights for that are attributable to: | 
 | 5 |  * Copyright (C) 2006 Nokia Corporation | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 6 |  * Tony Lindgren <tony@atomide.com> | 
 | 7 |  * | 
 | 8 |  * This file is part of the Inventra Controller Driver for Linux. | 
 | 9 |  * | 
 | 10 |  * The Inventra Controller Driver for Linux is free software; you | 
 | 11 |  * can redistribute it and/or modify it under the terms of the GNU | 
 | 12 |  * General Public License version 2 as published by the Free Software | 
 | 13 |  * Foundation. | 
 | 14 |  * | 
 | 15 |  * The Inventra Controller Driver for Linux is distributed in | 
 | 16 |  * the hope that it will be useful, but WITHOUT ANY WARRANTY; | 
 | 17 |  * without even the implied warranty of MERCHANTABILITY or | 
 | 18 |  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public | 
 | 19 |  * License for more details. | 
 | 20 |  * | 
 | 21 |  * You should have received a copy of the GNU General Public License | 
 | 22 |  * along with The Inventra Controller Driver for Linux ; if not, | 
 | 23 |  * write to the Free Software Foundation, Inc., 59 Temple Place, | 
 | 24 |  * Suite 330, Boston, MA  02111-1307  USA | 
 | 25 |  * | 
 | 26 |  */ | 
 | 27 | #include <linux/module.h> | 
 | 28 | #include <linux/kernel.h> | 
 | 29 | #include <linux/sched.h> | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 30 | #include <linux/init.h> | 
 | 31 | #include <linux/list.h> | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 32 | #include <linux/io.h> | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 33 | #include <linux/of.h> | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 34 | #include <linux/platform_device.h> | 
 | 35 | #include <linux/dma-mapping.h> | 
| Hema HK | 207b0e1 | 2011-02-17 12:07:22 +0530 | [diff] [blame] | 36 | #include <linux/pm_runtime.h> | 
 | 37 | #include <linux/err.h> | 
| NeilBrown | 12a19b5 | 2012-08-13 12:32:58 +1000 | [diff] [blame] | 38 | #include <linux/delay.h> | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 39 | #include <linux/usb/musb-omap.h> | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 40 | #include <linux/usb/omap_control_usb.h> | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 41 |  | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 42 | #include "musb_core.h" | 
 | 43 | #include "omap2430.h" | 
 | 44 |  | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 45 | struct omap2430_glue { | 
 | 46 | 	struct device		*dev; | 
 | 47 | 	struct platform_device	*musb; | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 48 | 	enum omap_musb_vbus_id_status status; | 
| Kishon Vijay Abraham I | 1e5acb8 | 2012-06-22 17:40:51 +0530 | [diff] [blame] | 49 | 	struct work_struct	omap_musb_mailbox_work; | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 50 | 	struct device		*control_otghs; | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 51 | }; | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 52 | #define glue_to_musb(g)		platform_get_drvdata(g->musb) | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 53 |  | 
| Aaro Koskinen | 4b58ed1 | 2013-03-05 13:04:24 +0200 | [diff] [blame] | 54 | static struct omap2430_glue	*_glue; | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 55 |  | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 56 | static struct timer_list musb_idle_timer; | 
 | 57 |  | 
 | 58 | static void musb_do_idle(unsigned long _musb) | 
 | 59 | { | 
 | 60 | 	struct musb	*musb = (void *)_musb; | 
 | 61 | 	unsigned long	flags; | 
 | 62 | 	u8	power; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 63 | 	u8	devctl; | 
 | 64 |  | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 65 | 	spin_lock_irqsave(&musb->lock, flags); | 
 | 66 |  | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 67 | 	switch (musb->xceiv->state) { | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 68 | 	case OTG_STATE_A_WAIT_BCON: | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 69 |  | 
 | 70 | 		devctl = musb_readb(musb->mregs, MUSB_DEVCTL); | 
 | 71 | 		if (devctl & MUSB_DEVCTL_BDEVICE) { | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 72 | 			musb->xceiv->state = OTG_STATE_B_IDLE; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 73 | 			MUSB_DEV_MODE(musb); | 
 | 74 | 		} else { | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 75 | 			musb->xceiv->state = OTG_STATE_A_IDLE; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 76 | 			MUSB_HST_MODE(musb); | 
 | 77 | 		} | 
 | 78 | 		break; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 79 | 	case OTG_STATE_A_SUSPEND: | 
 | 80 | 		/* finish RESUME signaling? */ | 
 | 81 | 		if (musb->port1_status & MUSB_PORT_STAT_RESUME) { | 
 | 82 | 			power = musb_readb(musb->mregs, MUSB_POWER); | 
 | 83 | 			power &= ~MUSB_POWER_RESUME; | 
| Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 84 | 			dev_dbg(musb->controller, "root port resume stopped, power %02x\n", power); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 85 | 			musb_writeb(musb->mregs, MUSB_POWER, power); | 
 | 86 | 			musb->is_active = 1; | 
 | 87 | 			musb->port1_status &= ~(USB_PORT_STAT_SUSPEND | 
 | 88 | 						| MUSB_PORT_STAT_RESUME); | 
 | 89 | 			musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16; | 
| Daniel Mack | 8b125df | 2013-04-10 21:55:50 +0200 | [diff] [blame] | 90 | 			usb_hcd_poll_rh_status(musb->hcd); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 91 | 			/* NOTE: it might really be A_WAIT_BCON ... */ | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 92 | 			musb->xceiv->state = OTG_STATE_A_HOST; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 93 | 		} | 
 | 94 | 		break; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 95 | 	case OTG_STATE_A_HOST: | 
 | 96 | 		devctl = musb_readb(musb->mregs, MUSB_DEVCTL); | 
 | 97 | 		if (devctl &  MUSB_DEVCTL_BDEVICE) | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 98 | 			musb->xceiv->state = OTG_STATE_B_IDLE; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 99 | 		else | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 100 | 			musb->xceiv->state = OTG_STATE_A_WAIT_BCON; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 101 | 	default: | 
 | 102 | 		break; | 
 | 103 | 	} | 
 | 104 | 	spin_unlock_irqrestore(&musb->lock, flags); | 
 | 105 | } | 
 | 106 |  | 
 | 107 |  | 
| Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 108 | static void omap2430_musb_try_idle(struct musb *musb, unsigned long timeout) | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 109 | { | 
 | 110 | 	unsigned long		default_timeout = jiffies + msecs_to_jiffies(3); | 
 | 111 | 	static unsigned long	last_timer; | 
 | 112 |  | 
 | 113 | 	if (timeout == 0) | 
 | 114 | 		timeout = default_timeout; | 
 | 115 |  | 
 | 116 | 	/* Never idle if active, or when VBUS timeout is not set as host */ | 
 | 117 | 	if (musb->is_active || ((musb->a_wait_bcon == 0) | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 118 | 			&& (musb->xceiv->state == OTG_STATE_A_WAIT_BCON))) { | 
| Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 119 | 		dev_dbg(musb->controller, "%s active, deleting timer\n", | 
| Felipe Balbi | 42c0bf1 | 2013-03-07 10:39:57 +0200 | [diff] [blame] | 120 | 			usb_otg_state_string(musb->xceiv->state)); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 121 | 		del_timer(&musb_idle_timer); | 
 | 122 | 		last_timer = jiffies; | 
 | 123 | 		return; | 
 | 124 | 	} | 
 | 125 |  | 
 | 126 | 	if (time_after(last_timer, timeout)) { | 
 | 127 | 		if (!timer_pending(&musb_idle_timer)) | 
 | 128 | 			last_timer = timeout; | 
 | 129 | 		else { | 
| Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 130 | 			dev_dbg(musb->controller, "Longer idle timer already pending, ignoring\n"); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 131 | 			return; | 
 | 132 | 		} | 
 | 133 | 	} | 
 | 134 | 	last_timer = timeout; | 
 | 135 |  | 
| Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 136 | 	dev_dbg(musb->controller, "%s inactive, for idle timer for %lu ms\n", | 
| Felipe Balbi | 42c0bf1 | 2013-03-07 10:39:57 +0200 | [diff] [blame] | 137 | 		usb_otg_state_string(musb->xceiv->state), | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 138 | 		(unsigned long)jiffies_to_msecs(timeout - jiffies)); | 
 | 139 | 	mod_timer(&musb_idle_timer, timeout); | 
 | 140 | } | 
 | 141 |  | 
| Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 142 | static void omap2430_musb_set_vbus(struct musb *musb, int is_on) | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 143 | { | 
| Heikki Krogerus | d445b6d | 2012-02-13 13:24:15 +0200 | [diff] [blame] | 144 | 	struct usb_otg	*otg = musb->xceiv->otg; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 145 | 	u8		devctl; | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 146 | 	unsigned long timeout = jiffies + msecs_to_jiffies(1000); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 147 | 	/* HDRC controls CPEN, but beware current surges during device | 
 | 148 | 	 * connect.  They can trigger transient overcurrent conditions | 
 | 149 | 	 * that must be ignored. | 
 | 150 | 	 */ | 
 | 151 |  | 
 | 152 | 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL); | 
 | 153 |  | 
 | 154 | 	if (is_on) { | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 155 | 		if (musb->xceiv->state == OTG_STATE_A_IDLE) { | 
| NeilBrown | 12a19b5 | 2012-08-13 12:32:58 +1000 | [diff] [blame] | 156 | 			int loops = 100; | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 157 | 			/* start the session */ | 
 | 158 | 			devctl |= MUSB_DEVCTL_SESSION; | 
 | 159 | 			musb_writeb(musb->mregs, MUSB_DEVCTL, devctl); | 
 | 160 | 			/* | 
 | 161 | 			 * Wait for the musb to set as A device to enable the | 
 | 162 | 			 * VBUS | 
 | 163 | 			 */ | 
 | 164 | 			while (musb_readb(musb->mregs, MUSB_DEVCTL) & 0x80) { | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 165 |  | 
| NeilBrown | 12a19b5 | 2012-08-13 12:32:58 +1000 | [diff] [blame] | 166 | 				mdelay(5); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 167 | 				cpu_relax(); | 
 | 168 |  | 
| NeilBrown | 12a19b5 | 2012-08-13 12:32:58 +1000 | [diff] [blame] | 169 | 				if (time_after(jiffies, timeout) | 
 | 170 | 				    || loops-- <= 0) { | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 171 | 					dev_err(musb->controller, | 
 | 172 | 					"configured as A device timeout"); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 173 | 					break; | 
 | 174 | 				} | 
 | 175 | 			} | 
 | 176 |  | 
| Kishon Vijay Abraham I | bb467cf | 2013-03-14 11:53:56 +0530 | [diff] [blame] | 177 | 			otg_set_vbus(otg, 1); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 178 | 		} else { | 
 | 179 | 			musb->is_active = 1; | 
| Heikki Krogerus | d445b6d | 2012-02-13 13:24:15 +0200 | [diff] [blame] | 180 | 			otg->default_a = 1; | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 181 | 			musb->xceiv->state = OTG_STATE_A_WAIT_VRISE; | 
 | 182 | 			devctl |= MUSB_DEVCTL_SESSION; | 
 | 183 | 			MUSB_HST_MODE(musb); | 
 | 184 | 		} | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 185 | 	} else { | 
 | 186 | 		musb->is_active = 0; | 
 | 187 |  | 
 | 188 | 		/* NOTE:  we're skipping A_WAIT_VFALL -> A_IDLE and | 
 | 189 | 		 * jumping right to B_IDLE... | 
 | 190 | 		 */ | 
 | 191 |  | 
| Heikki Krogerus | d445b6d | 2012-02-13 13:24:15 +0200 | [diff] [blame] | 192 | 		otg->default_a = 0; | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 193 | 		musb->xceiv->state = OTG_STATE_B_IDLE; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 194 | 		devctl &= ~MUSB_DEVCTL_SESSION; | 
 | 195 |  | 
 | 196 | 		MUSB_DEV_MODE(musb); | 
 | 197 | 	} | 
 | 198 | 	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl); | 
 | 199 |  | 
| Felipe Balbi | 5c8a86e | 2011-05-11 12:44:08 +0300 | [diff] [blame] | 200 | 	dev_dbg(musb->controller, "VBUS %s, devctl %02x " | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 201 | 		/* otg %3x conf %08x prcm %08x */ "\n", | 
| Felipe Balbi | 42c0bf1 | 2013-03-07 10:39:57 +0200 | [diff] [blame] | 202 | 		usb_otg_state_string(musb->xceiv->state), | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 203 | 		musb_readb(musb->mregs, MUSB_DEVCTL)); | 
 | 204 | } | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 205 |  | 
| Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 206 | static int omap2430_musb_set_mode(struct musb *musb, u8 musb_mode) | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 207 | { | 
 | 208 | 	u8	devctl = musb_readb(musb->mregs, MUSB_DEVCTL); | 
 | 209 |  | 
 | 210 | 	devctl |= MUSB_DEVCTL_SESSION; | 
 | 211 | 	musb_writeb(musb->mregs, MUSB_DEVCTL, devctl); | 
 | 212 |  | 
| David Brownell | 96a274d | 2008-11-24 13:06:47 +0200 | [diff] [blame] | 213 | 	return 0; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 214 | } | 
 | 215 |  | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 216 | static inline void omap2430_low_level_exit(struct musb *musb) | 
 | 217 | { | 
 | 218 | 	u32 l; | 
 | 219 |  | 
 | 220 | 	/* in any role */ | 
 | 221 | 	l = musb_readl(musb->mregs, OTG_FORCESTDBY); | 
 | 222 | 	l |= ENABLEFORCE;	/* enable MSTANDBY */ | 
 | 223 | 	musb_writel(musb->mregs, OTG_FORCESTDBY, l); | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 224 | } | 
 | 225 |  | 
 | 226 | static inline void omap2430_low_level_init(struct musb *musb) | 
 | 227 | { | 
 | 228 | 	u32 l; | 
 | 229 |  | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 230 | 	l = musb_readl(musb->mregs, OTG_FORCESTDBY); | 
 | 231 | 	l &= ~ENABLEFORCE;	/* disable MSTANDBY */ | 
 | 232 | 	musb_writel(musb->mregs, OTG_FORCESTDBY, l); | 
 | 233 | } | 
 | 234 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 235 | void omap_musb_mailbox(enum omap_musb_vbus_id_status status) | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 236 | { | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 237 | 	struct omap2430_glue	*glue = _glue; | 
| Vikram Pandita | 712d8ef | 2011-08-12 07:38:51 -0700 | [diff] [blame] | 238 |  | 
| Aaro Koskinen | f8c4b0e | 2013-03-05 13:04:23 +0200 | [diff] [blame] | 239 | 	if (!glue) { | 
 | 240 | 		pr_err("%s: musb core is not yet initialized\n", __func__); | 
 | 241 | 		return; | 
 | 242 | 	} | 
 | 243 | 	glue->status = status; | 
 | 244 |  | 
 | 245 | 	if (!glue_to_musb(glue)) { | 
| Aaro Koskinen | 80ab72e | 2012-12-23 01:24:51 +0200 | [diff] [blame] | 246 | 		pr_err("%s: musb core is not yet ready\n", __func__); | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 247 | 		return; | 
 | 248 | 	} | 
 | 249 |  | 
| Kishon Vijay Abraham I | 1e5acb8 | 2012-06-22 17:40:51 +0530 | [diff] [blame] | 250 | 	schedule_work(&glue->omap_musb_mailbox_work); | 
| Vikram Pandita | 712d8ef | 2011-08-12 07:38:51 -0700 | [diff] [blame] | 251 | } | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 252 | EXPORT_SYMBOL_GPL(omap_musb_mailbox); | 
| Vikram Pandita | 712d8ef | 2011-08-12 07:38:51 -0700 | [diff] [blame] | 253 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 254 | static void omap_musb_set_mailbox(struct omap2430_glue *glue) | 
| Vikram Pandita | 712d8ef | 2011-08-12 07:38:51 -0700 | [diff] [blame] | 255 | { | 
| Kishon Vijay Abraham I | 1e5acb8 | 2012-06-22 17:40:51 +0530 | [diff] [blame] | 256 | 	struct musb *musb = glue_to_musb(glue); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 257 | 	struct device *dev = musb->controller; | 
 | 258 | 	struct musb_hdrc_platform_data *pdata = dev->platform_data; | 
 | 259 | 	struct omap_musb_board_data *data = pdata->board_data; | 
| Kishon Vijay Abraham I | c83a854 | 2012-06-22 17:40:53 +0530 | [diff] [blame] | 260 | 	struct usb_otg *otg = musb->xceiv->otg; | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 261 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 262 | 	switch (glue->status) { | 
 | 263 | 	case OMAP_MUSB_ID_GROUND: | 
 | 264 | 		dev_dbg(dev, "ID GND\n"); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 265 |  | 
| Kishon Vijay Abraham I | c83a854 | 2012-06-22 17:40:53 +0530 | [diff] [blame] | 266 | 		otg->default_a = true; | 
 | 267 | 		musb->xceiv->state = OTG_STATE_A_IDLE; | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 268 | 		musb->xceiv->last_event = USB_EVENT_ID; | 
| Felipe Balbi | 032ec49 | 2011-11-24 15:46:26 +0200 | [diff] [blame] | 269 | 		if (musb->gadget_driver) { | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 270 | 			pm_runtime_get_sync(dev); | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 271 | 			omap_control_usb_set_mode(glue->control_otghs, | 
 | 272 | 				USB_MODE_HOST); | 
| Hema HK | 70045c5 | 2011-02-28 15:05:29 +0530 | [diff] [blame] | 273 | 			omap2430_musb_set_vbus(musb, 1); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 274 | 		} | 
 | 275 | 		break; | 
 | 276 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 277 | 	case OMAP_MUSB_VBUS_VALID: | 
 | 278 | 		dev_dbg(dev, "VBUS Connect\n"); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 279 |  | 
| Kishon Vijay Abraham I | c83a854 | 2012-06-22 17:40:53 +0530 | [diff] [blame] | 280 | 		otg->default_a = false; | 
 | 281 | 		musb->xceiv->state = OTG_STATE_B_IDLE; | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 282 | 		musb->xceiv->last_event = USB_EVENT_VBUS; | 
| Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 283 | 		if (musb->gadget_driver) | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 284 | 			pm_runtime_get_sync(dev); | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 285 | 		omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 286 | 		break; | 
 | 287 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 288 | 	case OMAP_MUSB_ID_FLOAT: | 
 | 289 | 	case OMAP_MUSB_VBUS_OFF: | 
 | 290 | 		dev_dbg(dev, "VBUS Disconnect\n"); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 291 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 292 | 		musb->xceiv->last_event = USB_EVENT_NONE; | 
| Felipe Balbi | 032ec49 | 2011-11-24 15:46:26 +0200 | [diff] [blame] | 293 | 		if (musb->gadget_driver) { | 
| Grazvydas Ignotas | 2c1fe89 | 2013-03-24 17:36:54 +0200 | [diff] [blame] | 294 | 			omap2430_musb_set_vbus(musb, 0); | 
| Felipe Balbi | 032ec49 | 2011-11-24 15:46:26 +0200 | [diff] [blame] | 295 | 			pm_runtime_mark_last_busy(dev); | 
 | 296 | 			pm_runtime_put_autosuspend(dev); | 
 | 297 | 		} | 
| Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 298 |  | 
| Kishon Vijay Abraham I | bb467cf | 2013-03-14 11:53:56 +0530 | [diff] [blame] | 299 | 		if (data->interface_type == MUSB_INTERFACE_UTMI) | 
 | 300 | 			otg_set_vbus(musb->xceiv->otg, 0); | 
 | 301 |  | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 302 | 		omap_control_usb_set_mode(glue->control_otghs, | 
 | 303 | 			USB_MODE_DISCONNECT); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 304 | 		break; | 
 | 305 | 	default: | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 306 | 		dev_dbg(dev, "ID float\n"); | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 307 | 	} | 
| Hema HK | 594632e | 2010-12-10 18:10:51 +0530 | [diff] [blame] | 308 | } | 
 | 309 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 310 |  | 
 | 311 | static void omap_musb_mailbox_work(struct work_struct *mailbox_work) | 
 | 312 | { | 
 | 313 | 	struct omap2430_glue *glue = container_of(mailbox_work, | 
 | 314 | 				struct omap2430_glue, omap_musb_mailbox_work); | 
 | 315 | 	omap_musb_set_mailbox(glue); | 
 | 316 | } | 
 | 317 |  | 
| Philippe De Swert | baef653 | 2012-11-06 15:32:13 +0200 | [diff] [blame] | 318 | static irqreturn_t omap2430_musb_interrupt(int irq, void *__hci) | 
 | 319 | { | 
 | 320 | 	unsigned long   flags; | 
 | 321 | 	irqreturn_t     retval = IRQ_NONE; | 
 | 322 | 	struct musb     *musb = __hci; | 
 | 323 |  | 
 | 324 | 	spin_lock_irqsave(&musb->lock, flags); | 
 | 325 |  | 
 | 326 | 	musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB); | 
 | 327 | 	musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX); | 
 | 328 | 	musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX); | 
 | 329 |  | 
 | 330 | 	if (musb->int_usb || musb->int_tx || musb->int_rx) | 
 | 331 | 		retval = musb_interrupt(musb); | 
 | 332 |  | 
 | 333 | 	spin_unlock_irqrestore(&musb->lock, flags); | 
 | 334 |  | 
 | 335 | 	return retval; | 
 | 336 | } | 
 | 337 |  | 
| Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 338 | static int omap2430_musb_init(struct musb *musb) | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 339 | { | 
| Shubhrajyoti D | ad57969 | 2012-03-22 12:48:06 +0530 | [diff] [blame] | 340 | 	u32 l; | 
 | 341 | 	int status = 0; | 
| Hema Kalliguddi | ea65df5 | 2010-09-22 19:27:40 -0500 | [diff] [blame] | 342 | 	struct device *dev = musb->controller; | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 343 | 	struct omap2430_glue *glue = dev_get_drvdata(dev->parent); | 
| Hema Kalliguddi | ea65df5 | 2010-09-22 19:27:40 -0500 | [diff] [blame] | 344 | 	struct musb_hdrc_platform_data *plat = dev->platform_data; | 
 | 345 | 	struct omap_musb_board_data *data = plat->board_data; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 346 |  | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 347 | 	/* We require some kind of external transceiver, hooked | 
 | 348 | 	 * up through ULPI.  TWL4030-family PMICs include one, | 
 | 349 | 	 * which needs a driver, drivers aren't always needed. | 
 | 350 | 	 */ | 
| Kishon Vijay Abraham I | b16604f | 2013-01-25 08:03:26 +0530 | [diff] [blame] | 351 | 	if (dev->parent->of_node) | 
 | 352 | 		musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent, | 
 | 353 | 		    "usb-phy", 0); | 
 | 354 | 	else | 
 | 355 | 		musb->xceiv = devm_usb_get_phy_dev(dev, 0); | 
 | 356 |  | 
| Felipe Balbi | a90199b | 2013-03-15 10:57:40 +0200 | [diff] [blame] | 357 | 	if (IS_ERR(musb->xceiv)) { | 
 | 358 | 		status = PTR_ERR(musb->xceiv); | 
 | 359 |  | 
 | 360 | 		if (status == -ENXIO) | 
 | 361 | 			return status; | 
 | 362 |  | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 363 | 		pr_err("HS USB OTG: no transceiver configured\n"); | 
| Ming Lei | 25736e0 | 2013-01-04 23:13:58 +0800 | [diff] [blame] | 364 | 		return -EPROBE_DEFER; | 
| David Brownell | 84e250f | 2009-03-31 12:30:04 -0700 | [diff] [blame] | 365 | 	} | 
 | 366 |  | 
| Philippe De Swert | baef653 | 2012-11-06 15:32:13 +0200 | [diff] [blame] | 367 | 	musb->isr = omap2430_musb_interrupt; | 
 | 368 |  | 
| Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 369 | 	status = pm_runtime_get_sync(dev); | 
 | 370 | 	if (status < 0) { | 
| Shubhrajyoti D | ad57969 | 2012-03-22 12:48:06 +0530 | [diff] [blame] | 371 | 		dev_err(dev, "pm_runtime_get_sync FAILED %d\n", status); | 
| Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 372 | 		goto err1; | 
 | 373 | 	} | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 374 |  | 
| Felipe Balbi | 8573e6a | 2010-01-21 15:33:53 +0200 | [diff] [blame] | 375 | 	l = musb_readl(musb->mregs, OTG_INTERFSEL); | 
| Maulik Mankad | de2e1b0 | 2010-03-12 10:29:07 +0200 | [diff] [blame] | 376 |  | 
 | 377 | 	if (data->interface_type == MUSB_INTERFACE_UTMI) { | 
 | 378 | 		/* OMAP4 uses Internal PHY GS70 which uses UTMI interface */ | 
 | 379 | 		l &= ~ULPI_12PIN;       /* Disable ULPI */ | 
 | 380 | 		l |= UTMI_8BIT;         /* Enable UTMI  */ | 
 | 381 | 	} else { | 
 | 382 | 		l |= ULPI_12PIN; | 
 | 383 | 	} | 
 | 384 |  | 
| Felipe Balbi | 8573e6a | 2010-01-21 15:33:53 +0200 | [diff] [blame] | 385 | 	musb_writel(musb->mregs, OTG_INTERFSEL, l); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 386 |  | 
 | 387 | 	pr_debug("HS USB OTG: revision 0x%x, sysconfig 0x%02x, " | 
 | 388 | 			"sysstatus 0x%x, intrfsel 0x%x, simenable  0x%x\n", | 
| Felipe Balbi | 8573e6a | 2010-01-21 15:33:53 +0200 | [diff] [blame] | 389 | 			musb_readl(musb->mregs, OTG_REVISION), | 
 | 390 | 			musb_readl(musb->mregs, OTG_SYSCONFIG), | 
 | 391 | 			musb_readl(musb->mregs, OTG_SYSSTATUS), | 
 | 392 | 			musb_readl(musb->mregs, OTG_INTERFSEL), | 
 | 393 | 			musb_readl(musb->mregs, OTG_SIMENABLE)); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 394 |  | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 395 | 	setup_timer(&musb_idle_timer, musb_do_idle, (unsigned long) musb); | 
 | 396 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 397 | 	if (glue->status != OMAP_MUSB_UNKNOWN) | 
 | 398 | 		omap_musb_set_mailbox(glue); | 
 | 399 |  | 
| Kishon Vijay Abraham I | 3bf6db9b | 2013-03-14 11:53:58 +0530 | [diff] [blame] | 400 | 	usb_phy_init(musb->xceiv); | 
 | 401 |  | 
| Grazvydas Ignotas | c04352a | 2012-02-04 19:43:51 +0200 | [diff] [blame] | 402 | 	pm_runtime_put_noidle(musb->controller); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 403 | 	return 0; | 
| Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 404 |  | 
 | 405 | err1: | 
| Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 406 | 	return status; | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 407 | } | 
 | 408 |  | 
| Hema HK | 002eda1 | 2011-02-17 12:06:10 +0530 | [diff] [blame] | 409 | static void omap2430_musb_enable(struct musb *musb) | 
 | 410 | { | 
 | 411 | 	u8		devctl; | 
 | 412 | 	unsigned long timeout = jiffies + msecs_to_jiffies(1000); | 
 | 413 | 	struct device *dev = musb->controller; | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 414 | 	struct omap2430_glue *glue = dev_get_drvdata(dev->parent); | 
| Hema HK | 002eda1 | 2011-02-17 12:06:10 +0530 | [diff] [blame] | 415 | 	struct musb_hdrc_platform_data *pdata = dev->platform_data; | 
 | 416 | 	struct omap_musb_board_data *data = pdata->board_data; | 
 | 417 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 418 | 	switch (glue->status) { | 
| Hema HK | 002eda1 | 2011-02-17 12:06:10 +0530 | [diff] [blame] | 419 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 420 | 	case OMAP_MUSB_ID_GROUND: | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 421 | 		omap_control_usb_set_mode(glue->control_otghs, USB_MODE_HOST); | 
| Felipe Contreras | 08dec56 | 2011-12-19 22:17:50 +0200 | [diff] [blame] | 422 | 		if (data->interface_type != MUSB_INTERFACE_UTMI) | 
 | 423 | 			break; | 
 | 424 | 		devctl = musb_readb(musb->mregs, MUSB_DEVCTL); | 
 | 425 | 		/* start the session */ | 
 | 426 | 		devctl |= MUSB_DEVCTL_SESSION; | 
 | 427 | 		musb_writeb(musb->mregs, MUSB_DEVCTL, devctl); | 
 | 428 | 		while (musb_readb(musb->mregs, MUSB_DEVCTL) & | 
 | 429 | 				MUSB_DEVCTL_BDEVICE) { | 
 | 430 | 			cpu_relax(); | 
| Hema HK | 002eda1 | 2011-02-17 12:06:10 +0530 | [diff] [blame] | 431 |  | 
| Felipe Contreras | 08dec56 | 2011-12-19 22:17:50 +0200 | [diff] [blame] | 432 | 			if (time_after(jiffies, timeout)) { | 
 | 433 | 				dev_err(dev, "configured as A device timeout"); | 
 | 434 | 				break; | 
| Hema HK | 002eda1 | 2011-02-17 12:06:10 +0530 | [diff] [blame] | 435 | 			} | 
 | 436 | 		} | 
 | 437 | 		break; | 
 | 438 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 439 | 	case OMAP_MUSB_VBUS_VALID: | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 440 | 		omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE); | 
| Hema HK | 002eda1 | 2011-02-17 12:06:10 +0530 | [diff] [blame] | 441 | 		break; | 
 | 442 |  | 
 | 443 | 	default: | 
 | 444 | 		break; | 
 | 445 | 	} | 
 | 446 | } | 
 | 447 |  | 
 | 448 | static void omap2430_musb_disable(struct musb *musb) | 
 | 449 | { | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 450 | 	struct device *dev = musb->controller; | 
 | 451 | 	struct omap2430_glue *glue = dev_get_drvdata(dev->parent); | 
 | 452 |  | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 453 | 	if (glue->status != OMAP_MUSB_UNKNOWN) | 
 | 454 | 		omap_control_usb_set_mode(glue->control_otghs, | 
 | 455 | 			USB_MODE_DISCONNECT); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 456 | } | 
 | 457 |  | 
| Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 458 | static int omap2430_musb_exit(struct musb *musb) | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 459 | { | 
| Johan Hovold | 19b9a83 | 2011-02-11 16:57:08 +0100 | [diff] [blame] | 460 | 	del_timer_sync(&musb_idle_timer); | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 461 |  | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 462 | 	omap2430_low_level_exit(musb); | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 463 |  | 
| Felipe Balbi | 550a737 | 2008-07-24 12:27:36 +0300 | [diff] [blame] | 464 | 	return 0; | 
 | 465 | } | 
| Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 466 |  | 
| Felipe Balbi | f7ec943 | 2010-12-02 09:48:58 +0200 | [diff] [blame] | 467 | static const struct musb_platform_ops omap2430_ops = { | 
| Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 468 | 	.init		= omap2430_musb_init, | 
 | 469 | 	.exit		= omap2430_musb_exit, | 
 | 470 |  | 
| Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 471 | 	.set_mode	= omap2430_musb_set_mode, | 
 | 472 | 	.try_idle	= omap2430_musb_try_idle, | 
 | 473 |  | 
 | 474 | 	.set_vbus	= omap2430_musb_set_vbus, | 
| Hema HK | 002eda1 | 2011-02-17 12:06:10 +0530 | [diff] [blame] | 475 |  | 
 | 476 | 	.enable		= omap2430_musb_enable, | 
 | 477 | 	.disable	= omap2430_musb_disable, | 
| Felipe Balbi | 743411b | 2010-12-01 13:22:05 +0200 | [diff] [blame] | 478 | }; | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 479 |  | 
 | 480 | static u64 omap2430_dmamask = DMA_BIT_MASK(32); | 
 | 481 |  | 
| Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 482 | static int omap2430_probe(struct platform_device *pdev) | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 483 | { | 
| Felipe Balbi | 09fc7d2 | 2013-04-24 17:21:42 +0300 | [diff] [blame] | 484 | 	struct resource musb_resouces[2]; | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 485 | 	struct musb_hdrc_platform_data	*pdata = pdev->dev.platform_data; | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 486 | 	struct omap_musb_board_data	*data; | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 487 | 	struct platform_device		*musb; | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 488 | 	struct omap2430_glue		*glue; | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 489 | 	struct device_node		*np = pdev->dev.of_node; | 
 | 490 | 	struct musb_hdrc_config		*config; | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 491 | 	int				ret = -ENOMEM; | 
 | 492 |  | 
| Kishon Vijay Abraham I | b1183c2 | 2012-06-22 17:40:54 +0530 | [diff] [blame] | 493 | 	glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL); | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 494 | 	if (!glue) { | 
 | 495 | 		dev_err(&pdev->dev, "failed to allocate glue context\n"); | 
 | 496 | 		goto err0; | 
 | 497 | 	} | 
 | 498 |  | 
| Sebastian Andrzej Siewior | 2f77116 | 2012-10-31 16:12:43 +0100 | [diff] [blame] | 499 | 	musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO); | 
 | 500 | 	if (!musb) { | 
 | 501 | 		dev_err(&pdev->dev, "failed to allocate musb device\n"); | 
| Kishon Vijay Abraham I | b1183c2 | 2012-06-22 17:40:54 +0530 | [diff] [blame] | 502 | 		goto err0; | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 503 | 	} | 
 | 504 |  | 
 | 505 | 	musb->dev.parent		= &pdev->dev; | 
 | 506 | 	musb->dev.dma_mask		= &omap2430_dmamask; | 
 | 507 | 	musb->dev.coherent_dma_mask	= omap2430_dmamask; | 
 | 508 |  | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 509 | 	glue->dev			= &pdev->dev; | 
 | 510 | 	glue->musb			= musb; | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 511 | 	glue->status			= OMAP_MUSB_UNKNOWN; | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 512 |  | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 513 | 	if (np) { | 
 | 514 | 		pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); | 
 | 515 | 		if (!pdata) { | 
 | 516 | 			dev_err(&pdev->dev, | 
 | 517 | 				"failed to allocate musb platfrom data\n"); | 
| Sebastian Andrzej Siewior | 2f77116 | 2012-10-31 16:12:43 +0100 | [diff] [blame] | 518 | 			goto err2; | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 519 | 		} | 
 | 520 |  | 
 | 521 | 		data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); | 
 | 522 | 		if (!data) { | 
 | 523 | 			dev_err(&pdev->dev, | 
| Sergei Shtylyov | 8df4ce7 | 2013-01-08 22:13:40 +0300 | [diff] [blame] | 524 | 				"failed to allocate musb board data\n"); | 
| Sebastian Andrzej Siewior | 2f77116 | 2012-10-31 16:12:43 +0100 | [diff] [blame] | 525 | 			goto err2; | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 526 | 		} | 
 | 527 |  | 
 | 528 | 		config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL); | 
| Sergei Shtylyov | b37457d | 2013-01-08 22:11:14 +0300 | [diff] [blame] | 529 | 		if (!config) { | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 530 | 			dev_err(&pdev->dev, | 
 | 531 | 				"failed to allocate musb hdrc config\n"); | 
| Sebastian Andrzej Siewior | 2f77116 | 2012-10-31 16:12:43 +0100 | [diff] [blame] | 532 | 			goto err2; | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 533 | 		} | 
 | 534 |  | 
 | 535 | 		of_property_read_u32(np, "mode", (u32 *)&pdata->mode); | 
| Kishon Vijay Abraham I | eee44da | 2013-03-07 18:51:46 +0530 | [diff] [blame] | 536 | 		of_property_read_u32(np, "interface-type", | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 537 | 						(u32 *)&data->interface_type); | 
| Kishon Vijay Abraham I | eee44da | 2013-03-07 18:51:46 +0530 | [diff] [blame] | 538 | 		of_property_read_u32(np, "num-eps", (u32 *)&config->num_eps); | 
 | 539 | 		of_property_read_u32(np, "ram-bits", (u32 *)&config->ram_bits); | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 540 | 		of_property_read_u32(np, "power", (u32 *)&pdata->power); | 
 | 541 | 		config->multipoint = of_property_read_bool(np, "multipoint"); | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 542 | 		pdata->has_mailbox = of_property_read_bool(np, | 
 | 543 | 		    "ti,has-mailbox"); | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 544 |  | 
 | 545 | 		pdata->board_data	= data; | 
 | 546 | 		pdata->config		= config; | 
 | 547 | 	} | 
 | 548 |  | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 549 | 	if (pdata->has_mailbox) { | 
 | 550 | 		glue->control_otghs = omap_get_control_dev(); | 
 | 551 | 		if (IS_ERR(glue->control_otghs)) { | 
 | 552 | 			dev_vdbg(&pdev->dev, "Failed to get control device\n"); | 
| Wei Yongjun | 4b7e450 | 2013-04-26 14:27:09 +0800 | [diff] [blame] | 553 | 			ret = PTR_ERR(glue->control_otghs); | 
 | 554 | 			goto err2; | 
| Kishon Vijay Abraham I | ca784be | 2013-01-25 15:54:00 +0530 | [diff] [blame] | 555 | 		} | 
 | 556 | 	} else { | 
 | 557 | 		glue->control_otghs = ERR_PTR(-ENODEV); | 
 | 558 | 	} | 
| Felipe Balbi | f7ec943 | 2010-12-02 09:48:58 +0200 | [diff] [blame] | 559 | 	pdata->platform_ops		= &omap2430_ops; | 
 | 560 |  | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 561 | 	platform_set_drvdata(pdev, glue); | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 562 |  | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 563 | 	/* | 
 | 564 | 	 * REVISIT if we ever have two instances of the wrapper, we will be | 
 | 565 | 	 * in big trouble | 
 | 566 | 	 */ | 
 | 567 | 	_glue	= glue; | 
 | 568 |  | 
| Kishon Vijay Abraham I | 1e5acb8 | 2012-06-22 17:40:51 +0530 | [diff] [blame] | 569 | 	INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work); | 
 | 570 |  | 
| Felipe Balbi | 09fc7d2 | 2013-04-24 17:21:42 +0300 | [diff] [blame] | 571 | 	memset(musb_resouces, 0x00, sizeof(*musb_resources) * | 
 | 572 | 			ARRAY_SIZE(musb_resources)); | 
 | 573 |  | 
 | 574 | 	musb_resources[0].name = pdev->resource[0].name; | 
 | 575 | 	musb_resources[0].start = pdev->resource[0].start; | 
 | 576 | 	musb_resources[0].end = pdev->resource[0].end; | 
 | 577 | 	musb_resources[0].flags = pdev->resource[0].flags; | 
 | 578 |  | 
 | 579 | 	musb_resources[1].name = pdev->resource[1].name; | 
 | 580 | 	musb_resources[1].start = pdev->resource[1].start; | 
 | 581 | 	musb_resources[1].end = pdev->resource[1].end; | 
 | 582 | 	musb_resources[1].flags = pdev->resource[1].flags; | 
 | 583 |  | 
 | 584 | 	ret = platform_device_add_resources(musb, musb_resources, | 
 | 585 | 			ARRAY_SIZE(musb_resources)); | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 586 | 	if (ret) { | 
 | 587 | 		dev_err(&pdev->dev, "failed to add resources\n"); | 
| B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 588 | 		goto err2; | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 589 | 	} | 
 | 590 |  | 
 | 591 | 	ret = platform_device_add_data(musb, pdata, sizeof(*pdata)); | 
 | 592 | 	if (ret) { | 
 | 593 | 		dev_err(&pdev->dev, "failed to add platform_data\n"); | 
| B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 594 | 		goto err2; | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 595 | 	} | 
 | 596 |  | 
| Kishon Vijay Abraham I | 3006dc8 | 2012-03-21 21:30:20 +0530 | [diff] [blame] | 597 | 	pm_runtime_enable(&pdev->dev); | 
 | 598 |  | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 599 | 	ret = platform_device_add(musb); | 
 | 600 | 	if (ret) { | 
 | 601 | 		dev_err(&pdev->dev, "failed to register musb device\n"); | 
| B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 602 | 		goto err2; | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 603 | 	} | 
 | 604 |  | 
 | 605 | 	return 0; | 
 | 606 |  | 
| B, Ravi | 65b3d52 | 2012-08-31 11:09:49 +0000 | [diff] [blame] | 607 | err2: | 
| Kishon Vijay Abraham I | b1183c2 | 2012-06-22 17:40:54 +0530 | [diff] [blame] | 608 | 	platform_device_put(musb); | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 609 |  | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 610 | err0: | 
 | 611 | 	return ret; | 
 | 612 | } | 
 | 613 |  | 
| Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 614 | static int omap2430_remove(struct platform_device *pdev) | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 615 | { | 
| Felipe Balbi | a3cee12 | 2010-12-02 09:27:29 +0200 | [diff] [blame] | 616 | 	struct omap2430_glue		*glue = platform_get_drvdata(pdev); | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 617 |  | 
| Kishon Vijay Abraham I | 1e5acb8 | 2012-06-22 17:40:51 +0530 | [diff] [blame] | 618 | 	cancel_work_sync(&glue->omap_musb_mailbox_work); | 
| Kishon Vijay Abraham I | f039df5 | 2012-08-02 15:30:07 +0530 | [diff] [blame] | 619 | 	platform_device_unregister(glue->musb); | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 620 |  | 
 | 621 | 	return 0; | 
 | 622 | } | 
 | 623 |  | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 624 | #ifdef CONFIG_PM | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 625 |  | 
| Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 626 | static int omap2430_runtime_suspend(struct device *dev) | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 627 | { | 
 | 628 | 	struct omap2430_glue		*glue = dev_get_drvdata(dev); | 
 | 629 | 	struct musb			*musb = glue_to_musb(glue); | 
 | 630 |  | 
| Vladimir Zapolskiy | afb76df | 2011-12-23 18:37:18 +0200 | [diff] [blame] | 631 | 	if (musb) { | 
 | 632 | 		musb->context.otg_interfsel = musb_readl(musb->mregs, | 
 | 633 | 				OTG_INTERFSEL); | 
| Hema HK | e25bec1 | 2011-09-07 09:19:24 -0700 | [diff] [blame] | 634 |  | 
| Vladimir Zapolskiy | afb76df | 2011-12-23 18:37:18 +0200 | [diff] [blame] | 635 | 		omap2430_low_level_exit(musb); | 
 | 636 | 		usb_phy_set_suspend(musb->xceiv, 1); | 
 | 637 | 	} | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 638 |  | 
 | 639 | 	return 0; | 
 | 640 | } | 
 | 641 |  | 
| Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 642 | static int omap2430_runtime_resume(struct device *dev) | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 643 | { | 
 | 644 | 	struct omap2430_glue		*glue = dev_get_drvdata(dev); | 
 | 645 | 	struct musb			*musb = glue_to_musb(glue); | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 646 |  | 
| Vladimir Zapolskiy | afb76df | 2011-12-23 18:37:18 +0200 | [diff] [blame] | 647 | 	if (musb) { | 
 | 648 | 		omap2430_low_level_init(musb); | 
 | 649 | 		musb_writel(musb->mregs, OTG_INTERFSEL, | 
 | 650 | 				musb->context.otg_interfsel); | 
| Hema HK | e25bec1 | 2011-09-07 09:19:24 -0700 | [diff] [blame] | 651 |  | 
| Vladimir Zapolskiy | afb76df | 2011-12-23 18:37:18 +0200 | [diff] [blame] | 652 | 		usb_phy_set_suspend(musb->xceiv, 0); | 
 | 653 | 	} | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 654 |  | 
 | 655 | 	return 0; | 
 | 656 | } | 
 | 657 |  | 
 | 658 | static struct dev_pm_ops omap2430_pm_ops = { | 
| Hema HK | 7acc619 | 2011-02-28 14:19:34 +0530 | [diff] [blame] | 659 | 	.runtime_suspend = omap2430_runtime_suspend, | 
 | 660 | 	.runtime_resume = omap2430_runtime_resume, | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 661 | }; | 
 | 662 |  | 
 | 663 | #define DEV_PM_OPS	(&omap2430_pm_ops) | 
 | 664 | #else | 
 | 665 | #define DEV_PM_OPS	NULL | 
 | 666 | #endif | 
 | 667 |  | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 668 | #ifdef CONFIG_OF | 
 | 669 | static const struct of_device_id omap2430_id_table[] = { | 
 | 670 | 	{ | 
 | 671 | 		.compatible = "ti,omap4-musb" | 
 | 672 | 	}, | 
 | 673 | 	{ | 
 | 674 | 		.compatible = "ti,omap3-musb" | 
 | 675 | 	}, | 
 | 676 | 	{}, | 
 | 677 | }; | 
 | 678 | MODULE_DEVICE_TABLE(of, omap2430_id_table); | 
 | 679 | #endif | 
 | 680 |  | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 681 | static struct platform_driver omap2430_driver = { | 
| Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 682 | 	.probe		= omap2430_probe, | 
| Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 683 | 	.remove		= omap2430_remove, | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 684 | 	.driver		= { | 
 | 685 | 		.name	= "musb-omap2430", | 
| Felipe Balbi | c20aebb | 2010-12-02 12:44:40 +0200 | [diff] [blame] | 686 | 		.pm	= DEV_PM_OPS, | 
| Kishon Vijay Abraham I | 00a0b1d | 2012-09-11 14:39:40 +0530 | [diff] [blame] | 687 | 		.of_match_table = of_match_ptr(omap2430_id_table), | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 688 | 	}, | 
 | 689 | }; | 
 | 690 |  | 
 | 691 | MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer"); | 
 | 692 | MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>"); | 
 | 693 | MODULE_LICENSE("GPL v2"); | 
 | 694 |  | 
 | 695 | static int __init omap2430_init(void) | 
 | 696 | { | 
| Felipe Balbi | e9e8c85 | 2012-01-26 12:40:23 +0200 | [diff] [blame] | 697 | 	return platform_driver_register(&omap2430_driver); | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 698 | } | 
| Kishon Vijay Abraham I | c972143 | 2012-06-22 17:40:52 +0530 | [diff] [blame] | 699 | subsys_initcall(omap2430_init); | 
| Felipe Balbi | dc09886 | 2010-12-01 15:01:11 +0200 | [diff] [blame] | 700 |  | 
 | 701 | static void __exit omap2430_exit(void) | 
 | 702 | { | 
 | 703 | 	platform_driver_unregister(&omap2430_driver); | 
 | 704 | } | 
 | 705 | module_exit(omap2430_exit); |