blob: d62c78b97cadb3f0947c90a535a539550973cd59 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Felipe Balbi550a7372008-07-24 12:27:36 +03002/*
3 * Copyright (C) 2005-2007 by Texas Instruments
4 * Some code has been taken from tusb6010.c
5 * Copyrights for that are attributable to:
6 * Copyright (C) 2006 Nokia Corporation
Felipe Balbi550a7372008-07-24 12:27:36 +03007 * Tony Lindgren <tony@atomide.com>
8 *
9 * This file is part of the Inventra Controller Driver for Linux.
Felipe Balbi550a7372008-07-24 12:27:36 +030010 */
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/sched.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030014#include <linux/init.h>
15#include <linux/list.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030016#include <linux/io.h>
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +053017#include <linux/of.h>
Felipe Balbidc098862010-12-01 15:01:11 +020018#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
Hema HK207b0e12011-02-17 12:07:22 +053020#include <linux/pm_runtime.h>
21#include <linux/err.h>
NeilBrown12a19b52012-08-13 12:32:58 +100022#include <linux/delay.h>
Tony Lindgren80555552015-11-30 21:37:12 -080023#include <linux/usb/musb.h>
Kishon Vijay Abraham I14da6992014-03-06 16:38:37 +020024#include <linux/phy/omap_control_phy.h>
Roger Quadros8934d3e2013-10-03 18:12:34 +030025#include <linux/of_platform.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030026
Felipe Balbi550a7372008-07-24 12:27:36 +030027#include "musb_core.h"
28#include "omap2430.h"
29
Felipe Balbia3cee122010-12-02 09:27:29 +020030struct omap2430_glue {
31 struct device *dev;
32 struct platform_device *musb;
Tony Lindgren80555552015-11-30 21:37:12 -080033 enum musb_vbus_id_status status;
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +053034 struct work_struct omap_musb_mailbox_work;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +053035 struct device *control_otghs;
Felipe Balbia3cee122010-12-02 09:27:29 +020036};
Felipe Balbic20aebb2010-12-02 12:44:40 +020037#define glue_to_musb(g) platform_get_drvdata(g->musb)
Felipe Balbia3cee122010-12-02 09:27:29 +020038
Aaro Koskinen4b58ed12013-03-05 13:04:24 +020039static struct omap2430_glue *_glue;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053040
Felipe Balbic20aebb2010-12-02 12:44:40 +020041static inline void omap2430_low_level_exit(struct musb *musb)
42{
43 u32 l;
44
45 /* in any role */
46 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
47 l |= ENABLEFORCE; /* enable MSTANDBY */
48 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
Felipe Balbic20aebb2010-12-02 12:44:40 +020049}
50
51static inline void omap2430_low_level_init(struct musb *musb)
52{
53 u32 l;
54
Felipe Balbic20aebb2010-12-02 12:44:40 +020055 l = musb_readl(musb->mregs, OTG_FORCESTDBY);
56 l &= ~ENABLEFORCE; /* disable MSTANDBY */
57 musb_writel(musb->mregs, OTG_FORCESTDBY, l);
58}
59
Tony Lindgren12b7db22016-05-31 10:05:19 -050060static int omap2430_musb_mailbox(enum musb_vbus_id_status status)
Hema HK594632e2010-12-10 18:10:51 +053061{
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053062 struct omap2430_glue *glue = _glue;
Vikram Pandita712d8ef2011-08-12 07:38:51 -070063
Aaro Koskinenf8c4b0e2013-03-05 13:04:23 +020064 if (!glue) {
65 pr_err("%s: musb core is not yet initialized\n", __func__);
Tony Lindgren12b7db22016-05-31 10:05:19 -050066 return -EPROBE_DEFER;
Aaro Koskinenf8c4b0e2013-03-05 13:04:23 +020067 }
68 glue->status = status;
69
70 if (!glue_to_musb(glue)) {
Aaro Koskinen80ab72e2012-12-23 01:24:51 +020071 pr_err("%s: musb core is not yet ready\n", __func__);
Tony Lindgren12b7db22016-05-31 10:05:19 -050072 return -EPROBE_DEFER;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053073 }
74
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +053075 schedule_work(&glue->omap_musb_mailbox_work);
Tony Lindgren12b7db22016-05-31 10:05:19 -050076
77 return 0;
Vikram Pandita712d8ef2011-08-12 07:38:51 -070078}
79
Tony Lindgren98827102020-01-15 07:25:32 -060080/*
81 * HDRC controls CPEN, but beware current surges during device connect.
82 * They can trigger transient overcurrent conditions that must be ignored.
83 *
84 * Note that we're skipping A_WAIT_VFALL -> A_IDLE and jumping right to B_IDLE
85 * as set by musb_set_peripheral().
86 */
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053087static void omap_musb_set_mailbox(struct omap2430_glue *glue)
Vikram Pandita712d8ef2011-08-12 07:38:51 -070088{
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +053089 struct musb *musb = glue_to_musb(glue);
Tony Lindgren98827102020-01-15 07:25:32 -060090 int error;
Tony Lindgren21f77be2016-05-31 10:05:15 -050091
Tony Lindgrenae909fe2016-09-12 21:48:30 -050092 pm_runtime_get_sync(musb->controller);
Tony Lindgren98827102020-01-15 07:25:32 -060093
94 dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
95 usb_otg_state_string(musb->xceiv->otg->state),
96 musb_readb(musb->mregs, MUSB_DEVCTL));
97
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +053098 switch (glue->status) {
Tony Lindgren80555552015-11-30 21:37:12 -080099 case MUSB_ID_GROUND:
Tony Lindgrenae909fe2016-09-12 21:48:30 -0500100 dev_dbg(musb->controller, "ID GND\n");
Tony Lindgren15f11222020-01-15 07:25:28 -0600101 switch (musb->xceiv->otg->state) {
Tony Lindgren98827102020-01-15 07:25:32 -0600102 case OTG_STATE_A_IDLE:
103 error = musb_set_host(musb);
104 if (error)
105 break;
106 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
107 /* Fall through */
Tony Lindgren15f11222020-01-15 07:25:28 -0600108 case OTG_STATE_A_WAIT_VRISE:
109 case OTG_STATE_A_WAIT_BCON:
110 case OTG_STATE_A_HOST:
Tony Lindgren15f11222020-01-15 07:25:28 -0600111 /*
112 * On multiple ID ground interrupts just keep enabling
113 * VBUS. At least cpcap VBUS shuts down otherwise.
114 */
Tony Lindgren98827102020-01-15 07:25:32 -0600115 otg_set_vbus(musb->xceiv->otg, 1);
Tony Lindgren15f11222020-01-15 07:25:28 -0600116 break;
117 default:
118 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
119 musb->xceiv->last_event = USB_EVENT_ID;
120 if (musb->gadget_driver) {
121 omap_control_usb_set_mode(glue->control_otghs,
122 USB_MODE_HOST);
Tony Lindgren98827102020-01-15 07:25:32 -0600123 otg_set_vbus(musb->xceiv->otg, 1);
Tony Lindgren15f11222020-01-15 07:25:28 -0600124 }
125 break;
Hema HK594632e2010-12-10 18:10:51 +0530126 }
127 break;
128
Tony Lindgren80555552015-11-30 21:37:12 -0800129 case MUSB_VBUS_VALID:
Tony Lindgrenae909fe2016-09-12 21:48:30 -0500130 dev_dbg(musb->controller, "VBUS Connect\n");
Hema HK594632e2010-12-10 18:10:51 +0530131
Antoine Tenarte47d9252014-10-30 18:41:13 +0100132 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530133 musb->xceiv->last_event = USB_EVENT_VBUS;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530134 omap_control_usb_set_mode(glue->control_otghs, USB_MODE_DEVICE);
Hema HK594632e2010-12-10 18:10:51 +0530135 break;
136
Tony Lindgren80555552015-11-30 21:37:12 -0800137 case MUSB_ID_FLOAT:
138 case MUSB_VBUS_OFF:
Tony Lindgrenae909fe2016-09-12 21:48:30 -0500139 dev_dbg(musb->controller, "VBUS Disconnect\n");
Hema HK594632e2010-12-10 18:10:51 +0530140
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530141 musb->xceiv->last_event = USB_EVENT_NONE;
Tony Lindgren98827102020-01-15 07:25:32 -0600142 musb_set_peripheral(musb);
143 otg_set_vbus(musb->xceiv->otg, 0);
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530144 omap_control_usb_set_mode(glue->control_otghs,
145 USB_MODE_DISCONNECT);
Hema HK594632e2010-12-10 18:10:51 +0530146 break;
147 default:
Tony Lindgrenae909fe2016-09-12 21:48:30 -0500148 dev_dbg(musb->controller, "ID float\n");
Hema HK594632e2010-12-10 18:10:51 +0530149 }
Tony Lindgrenae909fe2016-09-12 21:48:30 -0500150 pm_runtime_mark_last_busy(musb->controller);
151 pm_runtime_put_autosuspend(musb->controller);
Pali Rohár6fa71782013-09-18 19:03:34 +0200152 atomic_notifier_call_chain(&musb->xceiv->notifier,
153 musb->xceiv->last_event, NULL);
Hema HK594632e2010-12-10 18:10:51 +0530154}
155
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530156
157static void omap_musb_mailbox_work(struct work_struct *mailbox_work)
158{
159 struct omap2430_glue *glue = container_of(mailbox_work,
160 struct omap2430_glue, omap_musb_mailbox_work);
Felipe Balbi8b2bc2c2014-04-07 10:58:01 -0500161
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530162 omap_musb_set_mailbox(glue);
163}
164
Philippe De Swertbaef6532012-11-06 15:32:13 +0200165static irqreturn_t omap2430_musb_interrupt(int irq, void *__hci)
166{
167 unsigned long flags;
168 irqreturn_t retval = IRQ_NONE;
169 struct musb *musb = __hci;
170
171 spin_lock_irqsave(&musb->lock, flags);
172
173 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
174 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
175 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
176
177 if (musb->int_usb || musb->int_tx || musb->int_rx)
178 retval = musb_interrupt(musb);
179
180 spin_unlock_irqrestore(&musb->lock, flags);
181
182 return retval;
183}
184
Felipe Balbi743411b2010-12-01 13:22:05 +0200185static int omap2430_musb_init(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300186{
Shubhrajyoti Dad579692012-03-22 12:48:06 +0530187 u32 l;
188 int status = 0;
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500189 struct device *dev = musb->controller;
Jingoo Hanc1a7d672013-07-30 17:03:12 +0900190 struct musb_hdrc_platform_data *plat = dev_get_platdata(dev);
Hema Kalliguddiea65df52010-09-22 19:27:40 -0500191 struct omap_musb_board_data *data = plat->board_data;
Felipe Balbi550a7372008-07-24 12:27:36 +0300192
David Brownell84e250f2009-03-31 12:30:04 -0700193 /* We require some kind of external transceiver, hooked
194 * up through ULPI. TWL4030-family PMICs include one,
195 * which needs a driver, drivers aren't always needed.
196 */
Johan Hovold5ee17872018-04-18 11:26:22 +0200197 musb->phy = devm_phy_get(dev->parent, "usb2-phy");
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530198
Johan Hovold5ee17872018-04-18 11:26:22 +0200199 /* We can't totally remove musb->xceiv as of now because
200 * musb core uses xceiv.state and xceiv.otg. Once we have
201 * a separate state machine to handle otg, these can be moved
202 * out of xceiv and then we can start using the generic PHY
203 * framework
204 */
205 musb->xceiv = devm_usb_get_phy_by_phandle(dev->parent, "usb-phy", 0);
Kishon Vijay Abraham Ib16604f2013-01-25 08:03:26 +0530206
Felipe Balbia90199b2013-03-15 10:57:40 +0200207 if (IS_ERR(musb->xceiv)) {
208 status = PTR_ERR(musb->xceiv);
209
210 if (status == -ENXIO)
211 return status;
212
Ladislav Michldc66ba82016-11-21 08:59:26 -0600213 dev_dbg(dev, "HS USB OTG: no transceiver configured\n");
Ming Lei25736e02013-01-04 23:13:58 +0800214 return -EPROBE_DEFER;
David Brownell84e250f2009-03-31 12:30:04 -0700215 }
216
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530217 if (IS_ERR(musb->phy)) {
Ladislav Michl71127a72016-11-21 08:59:25 -0600218 dev_err(dev, "HS USB OTG: no PHY configured\n");
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530219 return PTR_ERR(musb->phy);
220 }
Philippe De Swertbaef6532012-11-06 15:32:13 +0200221 musb->isr = omap2430_musb_interrupt;
Tony Lindgrena83e17d2016-05-31 10:05:16 -0500222 phy_init(musb->phy);
Tony Lindgrend8e5f0e2016-10-19 12:03:39 -0500223 phy_power_on(musb->phy);
Philippe De Swertbaef6532012-11-06 15:32:13 +0200224
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200225 l = musb_readl(musb->mregs, OTG_INTERFSEL);
Maulik Mankadde2e1b02010-03-12 10:29:07 +0200226
227 if (data->interface_type == MUSB_INTERFACE_UTMI) {
228 /* OMAP4 uses Internal PHY GS70 which uses UTMI interface */
229 l &= ~ULPI_12PIN; /* Disable ULPI */
230 l |= UTMI_8BIT; /* Enable UTMI */
231 } else {
232 l |= ULPI_12PIN;
233 }
234
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200235 musb_writel(musb->mregs, OTG_INTERFSEL, l);
Felipe Balbi550a7372008-07-24 12:27:36 +0300236
Ladislav Michl71127a72016-11-21 08:59:25 -0600237 dev_dbg(dev, "HS USB OTG: revision 0x%x, sysconfig 0x%02x, "
Felipe Balbi550a7372008-07-24 12:27:36 +0300238 "sysstatus 0x%x, intrfsel 0x%x, simenable 0x%x\n",
Felipe Balbi8573e6a2010-01-21 15:33:53 +0200239 musb_readl(musb->mregs, OTG_REVISION),
240 musb_readl(musb->mregs, OTG_SYSCONFIG),
241 musb_readl(musb->mregs, OTG_SYSSTATUS),
242 musb_readl(musb->mregs, OTG_INTERFSEL),
243 musb_readl(musb->mregs, OTG_SIMENABLE));
Felipe Balbi550a7372008-07-24 12:27:36 +0300244
Felipe Balbi550a7372008-07-24 12:27:36 +0300245 return 0;
246}
247
Hema HK002eda12011-02-17 12:06:10 +0530248static void omap2430_musb_enable(struct musb *musb)
249{
Hema HK002eda12011-02-17 12:06:10 +0530250 struct device *dev = musb->controller;
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530251 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Tony Lindgrena83e17d2016-05-31 10:05:16 -0500252
Tony Lindgren8b359cbc2020-01-15 07:25:31 -0600253 if (glue->status == MUSB_UNKNOWN)
254 glue->status = MUSB_VBUS_OFF;
255 omap_musb_set_mailbox(glue);
Hema HK002eda12011-02-17 12:06:10 +0530256}
257
258static void omap2430_musb_disable(struct musb *musb)
259{
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530260 struct device *dev = musb->controller;
261 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
262
Tony Lindgren80555552015-11-30 21:37:12 -0800263 if (glue->status != MUSB_UNKNOWN)
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530264 omap_control_usb_set_mode(glue->control_otghs,
265 USB_MODE_DISCONNECT);
Felipe Balbi550a7372008-07-24 12:27:36 +0300266}
267
Felipe Balbi743411b2010-12-01 13:22:05 +0200268static int omap2430_musb_exit(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300269{
Tony Lindgrena83e17d2016-05-31 10:05:16 -0500270 struct device *dev = musb->controller;
271 struct omap2430_glue *glue = dev_get_drvdata(dev->parent);
Felipe Balbi550a7372008-07-24 12:27:36 +0300272
Felipe Balbic20aebb2010-12-02 12:44:40 +0200273 omap2430_low_level_exit(musb);
Tony Lindgrend8e5f0e2016-10-19 12:03:39 -0500274 phy_power_off(musb->phy);
Kishon Vijay Abraham I3e3101d2013-09-27 11:53:30 +0530275 phy_exit(musb->phy);
Tony Lindgrena83e17d2016-05-31 10:05:16 -0500276 musb->phy = NULL;
277 cancel_work_sync(&glue->omap_musb_mailbox_work);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200278
Felipe Balbi550a7372008-07-24 12:27:36 +0300279 return 0;
280}
Felipe Balbi743411b2010-12-01 13:22:05 +0200281
Felipe Balbif7ec9432010-12-02 09:48:58 +0200282static const struct musb_platform_ops omap2430_ops = {
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700283 .quirks = MUSB_DMA_INVENTRA,
Tony Lindgren7f6283e2015-05-01 12:29:28 -0700284#ifdef CONFIG_USB_INVENTRA_DMA
285 .dma_init = musbhs_dma_controller_create,
286 .dma_exit = musbhs_dma_controller_destroy,
287#endif
Felipe Balbi743411b2010-12-01 13:22:05 +0200288 .init = omap2430_musb_init,
289 .exit = omap2430_musb_exit,
290
Hema HK002eda12011-02-17 12:06:10 +0530291 .enable = omap2430_musb_enable,
292 .disable = omap2430_musb_disable,
Tony Lindgren80555552015-11-30 21:37:12 -0800293
294 .phy_callback = omap2430_musb_mailbox,
Felipe Balbi743411b2010-12-01 13:22:05 +0200295};
Felipe Balbidc098862010-12-01 15:01:11 +0200296
297static u64 omap2430_dmamask = DMA_BIT_MASK(32);
298
Bill Pemberton41ac7b32012-11-19 13:21:48 -0500299static int omap2430_probe(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200300{
Kishon Vijay Abraham Ic1f01be2013-07-17 10:51:22 +0300301 struct resource musb_resources[3];
Jingoo Hanc1a7d672013-07-30 17:03:12 +0900302 struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530303 struct omap_musb_board_data *data;
Felipe Balbidc098862010-12-01 15:01:11 +0200304 struct platform_device *musb;
Felipe Balbia3cee122010-12-02 09:27:29 +0200305 struct omap2430_glue *glue;
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530306 struct device_node *np = pdev->dev.of_node;
307 struct musb_hdrc_config *config;
Johan Hovold5ee17872018-04-18 11:26:22 +0200308 struct device_node *control_node;
309 struct platform_device *control_pdev;
Tony Lindgren606bf4d2015-02-04 06:28:49 -0800310 int ret = -ENOMEM, val;
Felipe Balbidc098862010-12-01 15:01:11 +0200311
Johan Hovold5ee17872018-04-18 11:26:22 +0200312 if (!np)
313 return -ENODEV;
314
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530315 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
Peter Chenaf1bdfc2014-10-14 15:56:08 +0800316 if (!glue)
Felipe Balbia3cee122010-12-02 09:27:29 +0200317 goto err0;
Felipe Balbia3cee122010-12-02 09:27:29 +0200318
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100319 musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
320 if (!musb) {
321 dev_err(&pdev->dev, "failed to allocate musb device\n");
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530322 goto err0;
Felipe Balbidc098862010-12-01 15:01:11 +0200323 }
324
325 musb->dev.parent = &pdev->dev;
326 musb->dev.dma_mask = &omap2430_dmamask;
327 musb->dev.coherent_dma_mask = omap2430_dmamask;
328
Felipe Balbia3cee122010-12-02 09:27:29 +0200329 glue->dev = &pdev->dev;
330 glue->musb = musb;
Tony Lindgren80555552015-11-30 21:37:12 -0800331 glue->status = MUSB_UNKNOWN;
Roger Quadros8934d3e2013-10-03 18:12:34 +0300332 glue->control_otghs = ERR_PTR(-ENODEV);
Felipe Balbia3cee122010-12-02 09:27:29 +0200333
Johan Hovold5ee17872018-04-18 11:26:22 +0200334 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
335 if (!pdata)
336 goto err2;
Roger Quadros8934d3e2013-10-03 18:12:34 +0300337
Johan Hovold5ee17872018-04-18 11:26:22 +0200338 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
339 if (!data)
340 goto err2;
341
342 config = devm_kzalloc(&pdev->dev, sizeof(*config), GFP_KERNEL);
343 if (!config)
344 goto err2;
345
346 of_property_read_u32(np, "mode", (u32 *)&pdata->mode);
347 of_property_read_u32(np, "interface-type",
348 (u32 *)&data->interface_type);
349 of_property_read_u32(np, "num-eps", (u32 *)&config->num_eps);
350 of_property_read_u32(np, "ram-bits", (u32 *)&config->ram_bits);
351 of_property_read_u32(np, "power", (u32 *)&pdata->power);
352
353 ret = of_property_read_u32(np, "multipoint", &val);
354 if (!ret && val)
355 config->multipoint = true;
356
357 pdata->board_data = data;
358 pdata->config = config;
359
360 control_node = of_parse_phandle(np, "ctrl-module", 0);
361 if (control_node) {
362 control_pdev = of_find_device_by_node(control_node);
363 if (!control_pdev) {
364 dev_err(&pdev->dev, "Failed to get control device\n");
365 ret = -EINVAL;
Sebastian Andrzej Siewior2f771162012-10-31 16:12:43 +0100366 goto err2;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530367 }
Johan Hovold5ee17872018-04-18 11:26:22 +0200368 glue->control_otghs = &control_pdev->dev;
Kishon Vijay Abraham Ica784be2013-01-25 15:54:00 +0530369 }
Johan Hovold5ee17872018-04-18 11:26:22 +0200370
Felipe Balbif7ec9432010-12-02 09:48:58 +0200371 pdata->platform_ops = &omap2430_ops;
372
Felipe Balbia3cee122010-12-02 09:27:29 +0200373 platform_set_drvdata(pdev, glue);
Felipe Balbidc098862010-12-01 15:01:11 +0200374
Kishon Vijay Abraham Ic9721432012-06-22 17:40:52 +0530375 /*
376 * REVISIT if we ever have two instances of the wrapper, we will be
377 * in big trouble
378 */
379 _glue = glue;
380
Kishon Vijay Abraham I1e5acb82012-06-22 17:40:51 +0530381 INIT_WORK(&glue->omap_musb_mailbox_work, omap_musb_mailbox_work);
382
Sebastian Andrzej Siewior3a0ddc72013-06-25 09:41:02 +0200383 memset(musb_resources, 0x00, sizeof(*musb_resources) *
Felipe Balbi09fc7d22013-04-24 17:21:42 +0300384 ARRAY_SIZE(musb_resources));
385
386 musb_resources[0].name = pdev->resource[0].name;
387 musb_resources[0].start = pdev->resource[0].start;
388 musb_resources[0].end = pdev->resource[0].end;
389 musb_resources[0].flags = pdev->resource[0].flags;
390
391 musb_resources[1].name = pdev->resource[1].name;
392 musb_resources[1].start = pdev->resource[1].start;
393 musb_resources[1].end = pdev->resource[1].end;
394 musb_resources[1].flags = pdev->resource[1].flags;
395
Kishon Vijay Abraham Ic1f01be2013-07-17 10:51:22 +0300396 musb_resources[2].name = pdev->resource[2].name;
397 musb_resources[2].start = pdev->resource[2].start;
398 musb_resources[2].end = pdev->resource[2].end;
399 musb_resources[2].flags = pdev->resource[2].flags;
400
Felipe Balbi09fc7d22013-04-24 17:21:42 +0300401 ret = platform_device_add_resources(musb, musb_resources,
402 ARRAY_SIZE(musb_resources));
Felipe Balbidc098862010-12-01 15:01:11 +0200403 if (ret) {
404 dev_err(&pdev->dev, "failed to add resources\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000405 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200406 }
407
408 ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
409 if (ret) {
410 dev_err(&pdev->dev, "failed to add platform_data\n");
B, Ravi65b3d522012-08-31 11:09:49 +0000411 goto err2;
Felipe Balbidc098862010-12-01 15:01:11 +0200412 }
413
Tony Lindgren87326e82016-05-31 10:05:20 -0500414 pm_runtime_enable(glue->dev);
Kishon Vijay Abraham I3006dc82012-03-21 21:30:20 +0530415
Felipe Balbidc098862010-12-01 15:01:11 +0200416 ret = platform_device_add(musb);
417 if (ret) {
418 dev_err(&pdev->dev, "failed to register musb device\n");
Tony Lindgren536d5992016-11-16 13:21:25 -0600419 goto err3;
Felipe Balbidc098862010-12-01 15:01:11 +0200420 }
421
422 return 0;
423
Tony Lindgren536d5992016-11-16 13:21:25 -0600424err3:
425 pm_runtime_disable(glue->dev);
426
B, Ravi65b3d522012-08-31 11:09:49 +0000427err2:
Kishon Vijay Abraham Ib1183c22012-06-22 17:40:54 +0530428 platform_device_put(musb);
Felipe Balbia3cee122010-12-02 09:27:29 +0200429
Felipe Balbidc098862010-12-01 15:01:11 +0200430err0:
431 return ret;
432}
433
Bill Pembertonfb4e98a2012-11-19 13:26:20 -0500434static int omap2430_remove(struct platform_device *pdev)
Felipe Balbidc098862010-12-01 15:01:11 +0200435{
Tony Lindgren21f77be2016-05-31 10:05:15 -0500436 struct omap2430_glue *glue = platform_get_drvdata(pdev);
Felipe Balbidc098862010-12-01 15:01:11 +0200437
Kishon Vijay Abraham If039df52012-08-02 15:30:07 +0530438 platform_device_unregister(glue->musb);
Tony Lindgren03e43522015-11-30 21:37:13 -0800439 pm_runtime_disable(glue->dev);
Felipe Balbidc098862010-12-01 15:01:11 +0200440
441 return 0;
442}
443
Felipe Balbic20aebb2010-12-02 12:44:40 +0200444#ifdef CONFIG_PM
Felipe Balbic20aebb2010-12-02 12:44:40 +0200445
Hema HK7acc6192011-02-28 14:19:34 +0530446static int omap2430_runtime_suspend(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200447{
448 struct omap2430_glue *glue = dev_get_drvdata(dev);
449 struct musb *musb = glue_to_musb(glue);
450
Tony Lindgrena83e17d2016-05-31 10:05:16 -0500451 if (!musb)
452 return 0;
Hema HKe25bec12011-09-07 09:19:24 -0700453
Tony Lindgrena83e17d2016-05-31 10:05:16 -0500454 musb->context.otg_interfsel = musb_readl(musb->mregs,
455 OTG_INTERFSEL);
456
457 omap2430_low_level_exit(musb);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200458
Tony Lindgren10ac7e72019-04-30 09:59:36 -0500459 phy_power_off(musb->phy);
460 phy_exit(musb->phy);
461
Felipe Balbic20aebb2010-12-02 12:44:40 +0200462 return 0;
463}
464
Hema HK7acc6192011-02-28 14:19:34 +0530465static int omap2430_runtime_resume(struct device *dev)
Felipe Balbic20aebb2010-12-02 12:44:40 +0200466{
467 struct omap2430_glue *glue = dev_get_drvdata(dev);
468 struct musb *musb = glue_to_musb(glue);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200469
Tony Lindgren8f2279d2015-10-28 10:16:04 -0700470 if (!musb)
Tony Lindgrena83e17d2016-05-31 10:05:16 -0500471 return 0;
Tony Lindgren8f2279d2015-10-28 10:16:04 -0700472
Tony Lindgren10ac7e72019-04-30 09:59:36 -0500473 phy_init(musb->phy);
474 phy_power_on(musb->phy);
475
Tony Lindgren8f2279d2015-10-28 10:16:04 -0700476 omap2430_low_level_init(musb);
477 musb_writel(musb->mregs, OTG_INTERFSEL,
478 musb->context.otg_interfsel);
Felipe Balbic20aebb2010-12-02 12:44:40 +0200479
Tony Lindgrence3ab652020-01-15 07:25:27 -0600480 /* Wait for musb to get oriented. Otherwise we can get babble */
481 usleep_range(200000, 250000);
482
Felipe Balbic20aebb2010-12-02 12:44:40 +0200483 return 0;
484}
485
Bhumika Goyalc0927fe2017-02-01 21:30:26 -0600486static const struct dev_pm_ops omap2430_pm_ops = {
Hema HK7acc6192011-02-28 14:19:34 +0530487 .runtime_suspend = omap2430_runtime_suspend,
488 .runtime_resume = omap2430_runtime_resume,
Felipe Balbic20aebb2010-12-02 12:44:40 +0200489};
490
491#define DEV_PM_OPS (&omap2430_pm_ops)
492#else
493#define DEV_PM_OPS NULL
494#endif
495
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530496#ifdef CONFIG_OF
497static const struct of_device_id omap2430_id_table[] = {
498 {
499 .compatible = "ti,omap4-musb"
500 },
501 {
502 .compatible = "ti,omap3-musb"
503 },
504 {},
505};
506MODULE_DEVICE_TABLE(of, omap2430_id_table);
507#endif
508
Felipe Balbidc098862010-12-01 15:01:11 +0200509static struct platform_driver omap2430_driver = {
Felipe Balbie9e8c852012-01-26 12:40:23 +0200510 .probe = omap2430_probe,
Bill Pemberton76904172012-11-19 13:21:08 -0500511 .remove = omap2430_remove,
Felipe Balbidc098862010-12-01 15:01:11 +0200512 .driver = {
513 .name = "musb-omap2430",
Felipe Balbic20aebb2010-12-02 12:44:40 +0200514 .pm = DEV_PM_OPS,
Kishon Vijay Abraham I00a0b1d2012-09-11 14:39:40 +0530515 .of_match_table = of_match_ptr(omap2430_id_table),
Felipe Balbidc098862010-12-01 15:01:11 +0200516 },
517};
518
Tony Lindgrenaec373c2016-05-31 10:05:22 -0500519module_platform_driver(omap2430_driver);
520
Felipe Balbidc098862010-12-01 15:01:11 +0200521MODULE_DESCRIPTION("OMAP2PLUS MUSB Glue Layer");
522MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
523MODULE_LICENSE("GPL v2");