]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/usb/musb/da8xx.c
USB: add SPDX identifiers to all remaining files in drivers/usb/
[mirror_ubuntu-jammy-kernel.git] / drivers / usb / musb / da8xx.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: GPL-2.0
3ee076de
SS
2/*
3 * Texas Instruments DA8xx/OMAP-L1x "glue layer"
4 *
5 * Copyright (c) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
6 *
7 * Based on the DaVinci "glue layer" code.
8 * Copyright (C) 2005-2006 by Texas Instruments
9 *
35bd67b2
PK
10 * DT support
11 * Copyright (c) 2016 Petr Kulhavy <petr@barix.com>
12 *
3ee076de
SS
13 * This file is part of the Inventra Controller Driver for Linux.
14 *
15 * The Inventra Controller Driver for Linux is free software; you
16 * can redistribute it and/or modify it under the terms of the GNU
17 * General Public License version 2 as published by the Free Software
18 * Foundation.
19 *
20 * The Inventra Controller Driver for Linux is distributed in
21 * the hope that it will be useful, but WITHOUT ANY WARRANTY;
22 * without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
24 * License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with The Inventra Controller Driver for Linux ; if not,
28 * write to the Free Software Foundation, Inc., 59 Temple Place,
29 * Suite 330, Boston, MA 02111-1307 USA
30 *
31 */
32
ab570da2 33#include <linux/module.h>
3ee076de 34#include <linux/clk.h>
ded017ee 35#include <linux/err.h>
3ee076de 36#include <linux/io.h>
d6299b6e 37#include <linux/of_platform.h>
947c49af 38#include <linux/phy/phy.h>
8ceae51e
FB
39#include <linux/platform_device.h>
40#include <linux/dma-mapping.h>
d7078df6 41#include <linux/usb/usb_phy_generic.h>
3ee076de 42
3ee076de
SS
43#include "musb_core.h"
44
45/*
46 * DA8XX specific definitions
47 */
48
49/* USB 2.0 OTG module registers */
50#define DA8XX_USB_REVISION_REG 0x00
51#define DA8XX_USB_CTRL_REG 0x04
52#define DA8XX_USB_STAT_REG 0x08
53#define DA8XX_USB_EMULATION_REG 0x0c
3ee076de 54#define DA8XX_USB_SRP_FIX_TIME_REG 0x18
3ee076de
SS
55#define DA8XX_USB_INTR_SRC_REG 0x20
56#define DA8XX_USB_INTR_SRC_SET_REG 0x24
57#define DA8XX_USB_INTR_SRC_CLEAR_REG 0x28
58#define DA8XX_USB_INTR_MASK_REG 0x2c
59#define DA8XX_USB_INTR_MASK_SET_REG 0x30
60#define DA8XX_USB_INTR_MASK_CLEAR_REG 0x34
61#define DA8XX_USB_INTR_SRC_MASKED_REG 0x38
62#define DA8XX_USB_END_OF_INTR_REG 0x3c
63#define DA8XX_USB_GENERIC_RNDIS_EP_SIZE_REG(n) (0x50 + (((n) - 1) << 2))
64
65/* Control register bits */
66#define DA8XX_SOFT_RESET_MASK 1
67
68#define DA8XX_USB_TX_EP_MASK 0x1f /* EP0 + 4 Tx EPs */
69#define DA8XX_USB_RX_EP_MASK 0x1e /* 4 Rx EPs */
70
71/* USB interrupt register bits */
72#define DA8XX_INTR_USB_SHIFT 16
73#define DA8XX_INTR_USB_MASK (0x1ff << DA8XX_INTR_USB_SHIFT) /* 8 Mentor */
74 /* interrupts and DRVVBUS interrupt */
75#define DA8XX_INTR_DRVVBUS 0x100
76#define DA8XX_INTR_RX_SHIFT 8
77#define DA8XX_INTR_RX_MASK (DA8XX_USB_RX_EP_MASK << DA8XX_INTR_RX_SHIFT)
78#define DA8XX_INTR_TX_SHIFT 0
79#define DA8XX_INTR_TX_MASK (DA8XX_USB_TX_EP_MASK << DA8XX_INTR_TX_SHIFT)
80
81#define DA8XX_MENTOR_CORE_OFFSET 0x400
82
e6480faa
FB
83struct da8xx_glue {
84 struct device *dev;
85 struct platform_device *musb;
947c49af 86 struct platform_device *usb_phy;
03491761 87 struct clk *clk;
947c49af 88 struct phy *phy;
e6480faa
FB
89};
90
3ee076de
SS
91/*
92 * Because we don't set CTRL.UINT, it's "important" to:
93 * - not read/write INTRUSB/INTRUSBE (except during
94 * initial setup, as a workaround);
95 * - use INTSET/INTCLR instead.
96 */
97
98/**
743411b3 99 * da8xx_musb_enable - enable interrupts
3ee076de 100 */
743411b3 101static void da8xx_musb_enable(struct musb *musb)
3ee076de
SS
102{
103 void __iomem *reg_base = musb->ctrl_base;
104 u32 mask;
105
106 /* Workaround: setup IRQs through both register sets. */
107 mask = ((musb->epmask & DA8XX_USB_TX_EP_MASK) << DA8XX_INTR_TX_SHIFT) |
108 ((musb->epmask & DA8XX_USB_RX_EP_MASK) << DA8XX_INTR_RX_SHIFT) |
109 DA8XX_INTR_USB_MASK;
110 musb_writel(reg_base, DA8XX_USB_INTR_MASK_SET_REG, mask);
111
112 /* Force the DRVVBUS IRQ so we can start polling for ID change. */
032ec49f
FB
113 musb_writel(reg_base, DA8XX_USB_INTR_SRC_SET_REG,
114 DA8XX_INTR_DRVVBUS << DA8XX_INTR_USB_SHIFT);
3ee076de
SS
115}
116
117/**
743411b3 118 * da8xx_musb_disable - disable HDRC and flush interrupts
3ee076de 119 */
743411b3 120static void da8xx_musb_disable(struct musb *musb)
3ee076de
SS
121{
122 void __iomem *reg_base = musb->ctrl_base;
123
124 musb_writel(reg_base, DA8XX_USB_INTR_MASK_CLEAR_REG,
125 DA8XX_INTR_USB_MASK |
126 DA8XX_INTR_TX_MASK | DA8XX_INTR_RX_MASK);
3ee076de
SS
127 musb_writel(reg_base, DA8XX_USB_END_OF_INTR_REG, 0);
128}
129
62285963 130#define portstate(stmt) stmt
3ee076de 131
743411b3 132static void da8xx_musb_set_vbus(struct musb *musb, int is_on)
3ee076de
SS
133{
134 WARN_ON(is_on && is_peripheral_active(musb));
135}
136
137#define POLL_SECONDS 2
138
05678497 139static void otg_timer(struct timer_list *t)
3ee076de 140{
05678497 141 struct musb *musb = from_timer(musb, t, dev_timer);
3ee076de
SS
142 void __iomem *mregs = musb->mregs;
143 u8 devctl;
144 unsigned long flags;
145
146 /*
147 * We poll because DaVinci's won't expose several OTG-critical
148 * status change events (from the transceiver) otherwise.
149 */
150 devctl = musb_readb(mregs, MUSB_DEVCTL);
5c8a86e1 151 dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl,
e47d9254 152 usb_otg_state_string(musb->xceiv->otg->state));
3ee076de
SS
153
154 spin_lock_irqsave(&musb->lock, flags);
e47d9254 155 switch (musb->xceiv->otg->state) {
3ee076de
SS
156 case OTG_STATE_A_WAIT_BCON:
157 devctl &= ~MUSB_DEVCTL_SESSION;
158 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
159
160 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
161 if (devctl & MUSB_DEVCTL_BDEVICE) {
e47d9254 162 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
3ee076de
SS
163 MUSB_DEV_MODE(musb);
164 } else {
e47d9254 165 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
3ee076de
SS
166 MUSB_HST_MODE(musb);
167 }
168 break;
169 case OTG_STATE_A_WAIT_VFALL:
170 /*
171 * Wait till VBUS falls below SessionEnd (~0.2 V); the 1.3
172 * RTL seems to mis-handle session "start" otherwise (or in
173 * our case "recover"), in routine "VBUS was valid by the time
174 * VBUSERR got reported during enumeration" cases.
175 */
176 if (devctl & MUSB_DEVCTL_VBUS) {
05678497 177 mod_timer(&musb->dev_timer, jiffies + POLL_SECONDS * HZ);
3ee076de
SS
178 break;
179 }
e47d9254 180 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
3ee076de
SS
181 musb_writel(musb->ctrl_base, DA8XX_USB_INTR_SRC_SET_REG,
182 MUSB_INTR_VBUSERROR << DA8XX_INTR_USB_SHIFT);
183 break;
184 case OTG_STATE_B_IDLE:
3ee076de
SS
185 /*
186 * There's no ID-changed IRQ, so we have no good way to tell
187 * when to switch to the A-Default state machine (by setting
188 * the DEVCTL.Session bit).
189 *
190 * Workaround: whenever we're in B_IDLE, try setting the
191 * session flag every few seconds. If it works, ID was
192 * grounded and we're now in the A-Default state machine.
193 *
194 * NOTE: setting the session flag is _supposed_ to trigger
195 * SRP but clearly it doesn't.
196 */
197 musb_writeb(mregs, MUSB_DEVCTL, devctl | MUSB_DEVCTL_SESSION);
198 devctl = musb_readb(mregs, MUSB_DEVCTL);
199 if (devctl & MUSB_DEVCTL_BDEVICE)
05678497 200 mod_timer(&musb->dev_timer, jiffies + POLL_SECONDS * HZ);
3ee076de 201 else
e47d9254 202 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
3ee076de
SS
203 break;
204 default:
205 break;
206 }
207 spin_unlock_irqrestore(&musb->lock, flags);
208}
209
743411b3 210static void da8xx_musb_try_idle(struct musb *musb, unsigned long timeout)
3ee076de
SS
211{
212 static unsigned long last_timer;
213
3ee076de
SS
214 if (timeout == 0)
215 timeout = jiffies + msecs_to_jiffies(3);
216
217 /* Never idle if active, or when VBUS timeout is not set as host */
218 if (musb->is_active || (musb->a_wait_bcon == 0 &&
e47d9254 219 musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON)) {
5c8a86e1 220 dev_dbg(musb->controller, "%s active, deleting timer\n",
e47d9254 221 usb_otg_state_string(musb->xceiv->otg->state));
05678497 222 del_timer(&musb->dev_timer);
3ee076de
SS
223 last_timer = jiffies;
224 return;
225 }
226
05678497 227 if (time_after(last_timer, timeout) && timer_pending(&musb->dev_timer)) {
5c8a86e1 228 dev_dbg(musb->controller, "Longer idle timer already pending, ignoring...\n");
3ee076de
SS
229 return;
230 }
231 last_timer = timeout;
232
5c8a86e1 233 dev_dbg(musb->controller, "%s inactive, starting idle timer for %u ms\n",
e47d9254 234 usb_otg_state_string(musb->xceiv->otg->state),
3df00453 235 jiffies_to_msecs(timeout - jiffies));
05678497 236 mod_timer(&musb->dev_timer, timeout);
3ee076de
SS
237}
238
743411b3 239static irqreturn_t da8xx_musb_interrupt(int irq, void *hci)
3ee076de
SS
240{
241 struct musb *musb = hci;
242 void __iomem *reg_base = musb->ctrl_base;
d445b6da 243 struct usb_otg *otg = musb->xceiv->otg;
3ee076de
SS
244 unsigned long flags;
245 irqreturn_t ret = IRQ_NONE;
246 u32 status;
247
248 spin_lock_irqsave(&musb->lock, flags);
249
250 /*
251 * NOTE: DA8XX shadows the Mentor IRQs. Don't manage them through
252 * the Mentor registers (except for setup), use the TI ones and EOI.
253 */
254
255 /* Acknowledge and handle non-CPPI interrupts */
256 status = musb_readl(reg_base, DA8XX_USB_INTR_SRC_MASKED_REG);
257 if (!status)
258 goto eoi;
259
260 musb_writel(reg_base, DA8XX_USB_INTR_SRC_CLEAR_REG, status);
5c8a86e1 261 dev_dbg(musb->controller, "USB IRQ %08x\n", status);
3ee076de
SS
262
263 musb->int_rx = (status & DA8XX_INTR_RX_MASK) >> DA8XX_INTR_RX_SHIFT;
264 musb->int_tx = (status & DA8XX_INTR_TX_MASK) >> DA8XX_INTR_TX_SHIFT;
265 musb->int_usb = (status & DA8XX_INTR_USB_MASK) >> DA8XX_INTR_USB_SHIFT;
266
267 /*
268 * DRVVBUS IRQs are the only proxy we have (a very poor one!) for
269 * DA8xx's missing ID change IRQ. We need an ID change IRQ to
270 * switch appropriately between halves of the OTG state machine.
271 * Managing DEVCTL.Session per Mentor docs requires that we know its
272 * value but DEVCTL.BDevice is invalid without DEVCTL.Session set.
273 * Also, DRVVBUS pulses for SRP (but not at 5 V)...
274 */
275 if (status & (DA8XX_INTR_DRVVBUS << DA8XX_INTR_USB_SHIFT)) {
276 int drvvbus = musb_readl(reg_base, DA8XX_USB_STAT_REG);
277 void __iomem *mregs = musb->mregs;
278 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
279 int err;
280
db9e5161 281 err = musb->int_usb & MUSB_INTR_VBUSERROR;
3ee076de
SS
282 if (err) {
283 /*
284 * The Mentor core doesn't debounce VBUS as needed
285 * to cope with device connect current spikes. This
286 * means it's not uncommon for bus-powered devices
287 * to get VBUS errors during enumeration.
288 *
289 * This is a workaround, but newer RTL from Mentor
290 * seems to allow a better one: "re"-starting sessions
291 * without waiting for VBUS to stop registering in
292 * devctl.
293 */
294 musb->int_usb &= ~MUSB_INTR_VBUSERROR;
e47d9254 295 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VFALL;
05678497 296 mod_timer(&musb->dev_timer, jiffies + POLL_SECONDS * HZ);
3ee076de 297 WARNING("VBUS error workaround (delay coming)\n");
032ec49f 298 } else if (drvvbus) {
3ee076de 299 MUSB_HST_MODE(musb);
d445b6da 300 otg->default_a = 1;
e47d9254 301 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
3ee076de 302 portstate(musb->port1_status |= USB_PORT_STAT_POWER);
05678497 303 del_timer(&musb->dev_timer);
3ee076de
SS
304 } else {
305 musb->is_active = 0;
306 MUSB_DEV_MODE(musb);
d445b6da 307 otg->default_a = 0;
e47d9254 308 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
3ee076de
SS
309 portstate(musb->port1_status &= ~USB_PORT_STAT_POWER);
310 }
311
5c8a86e1 312 dev_dbg(musb->controller, "VBUS %s (%s)%s, devctl %02x\n",
3ee076de 313 drvvbus ? "on" : "off",
e47d9254 314 usb_otg_state_string(musb->xceiv->otg->state),
3ee076de
SS
315 err ? " ERROR" : "",
316 devctl);
317 ret = IRQ_HANDLED;
318 }
319
320 if (musb->int_tx || musb->int_rx || musb->int_usb)
321 ret |= musb_interrupt(musb);
322
323 eoi:
324 /* EOI needs to be written for the IRQ to be re-asserted. */
325 if (ret == IRQ_HANDLED || status)
326 musb_writel(reg_base, DA8XX_USB_END_OF_INTR_REG, 0);
327
328 /* Poll for ID change */
e47d9254 329 if (musb->xceiv->otg->state == OTG_STATE_B_IDLE)
05678497 330 mod_timer(&musb->dev_timer, jiffies + POLL_SECONDS * HZ);
3ee076de
SS
331
332 spin_unlock_irqrestore(&musb->lock, flags);
333
334 return ret;
335}
336
743411b3 337static int da8xx_musb_set_mode(struct musb *musb, u8 musb_mode)
3ee076de 338{
947c49af
DL
339 struct da8xx_glue *glue = dev_get_drvdata(musb->controller->parent);
340 enum phy_mode phy_mode;
3ee076de 341
7ccf6294
AB
342 /*
343 * The PHY has some issues when it is forced in device or host mode.
344 * Unless the user request another mode, configure the PHY in OTG mode.
345 */
346 if (!musb->is_initialized)
347 return phy_set_mode(glue->phy, PHY_MODE_USB_OTG);
348
3ee076de 349 switch (musb_mode) {
3ee076de 350 case MUSB_HOST: /* Force VBUS valid, ID = 0 */
947c49af 351 phy_mode = PHY_MODE_USB_HOST;
3ee076de 352 break;
3ee076de 353 case MUSB_PERIPHERAL: /* Force VBUS valid, ID = 1 */
947c49af 354 phy_mode = PHY_MODE_USB_DEVICE;
3ee076de 355 break;
3ee076de 356 case MUSB_OTG: /* Don't override the VBUS/ID comparators */
947c49af 357 phy_mode = PHY_MODE_USB_OTG;
3ee076de 358 break;
3ee076de 359 default:
947c49af 360 return -EINVAL;
3ee076de
SS
361 }
362
947c49af 363 return phy_set_mode(glue->phy, phy_mode);
3ee076de
SS
364}
365
743411b3 366static int da8xx_musb_init(struct musb *musb)
3ee076de 367{
947c49af 368 struct da8xx_glue *glue = dev_get_drvdata(musb->controller->parent);
3ee076de
SS
369 void __iomem *reg_base = musb->ctrl_base;
370 u32 rev;
25736e0c 371 int ret = -ENODEV;
3ee076de
SS
372
373 musb->mregs += DA8XX_MENTOR_CORE_OFFSET;
374
09721ba6
AB
375 ret = clk_prepare_enable(glue->clk);
376 if (ret) {
377 dev_err(glue->dev, "failed to enable clock\n");
378 return ret;
379 }
380
3ee076de
SS
381 /* Returns zero if e.g. not clocked */
382 rev = musb_readl(reg_base, DA8XX_USB_REVISION_REG);
383 if (!rev)
384 goto fail;
385
662dca54 386 musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
25736e0c
ML
387 if (IS_ERR_OR_NULL(musb->xceiv)) {
388 ret = -EPROBE_DEFER;
3ee076de 389 goto fail;
25736e0c 390 }
3ee076de 391
05678497 392 timer_setup(&musb->dev_timer, otg_timer, 0);
3ee076de 393
3ee076de
SS
394 /* Reset the controller */
395 musb_writel(reg_base, DA8XX_USB_CTRL_REG, DA8XX_SOFT_RESET_MASK);
396
397 /* Start the on-chip PHY and its PLL. */
947c49af
DL
398 ret = phy_init(glue->phy);
399 if (ret) {
400 dev_err(glue->dev, "Failed to init phy.\n");
09721ba6 401 goto fail;
947c49af
DL
402 }
403
404 ret = phy_power_on(glue->phy);
405 if (ret) {
406 dev_err(glue->dev, "Failed to power on phy.\n");
407 goto err_phy_power_on;
408 }
3ee076de
SS
409
410 msleep(5);
411
412 /* NOTE: IRQs are in mixed mode, not bypass to pure MUSB */
947c49af 413 pr_debug("DA8xx OTG revision %08x, control %02x\n", rev,
3ee076de
SS
414 musb_readb(reg_base, DA8XX_USB_CTRL_REG));
415
743411b3 416 musb->isr = da8xx_musb_interrupt;
3ee076de 417 return 0;
947c49af
DL
418
419err_phy_power_on:
420 phy_exit(glue->phy);
3ee076de 421fail:
09721ba6 422 clk_disable_unprepare(glue->clk);
25736e0c 423 return ret;
3ee076de
SS
424}
425
743411b3 426static int da8xx_musb_exit(struct musb *musb)
3ee076de 427{
947c49af
DL
428 struct da8xx_glue *glue = dev_get_drvdata(musb->controller->parent);
429
05678497 430 del_timer_sync(&musb->dev_timer);
3ee076de 431
947c49af
DL
432 phy_power_off(glue->phy);
433 phy_exit(glue->phy);
434 clk_disable_unprepare(glue->clk);
3ee076de 435
721002ec 436 usb_put_phy(musb->xceiv);
3ee076de 437
3ee076de
SS
438 return 0;
439}
743411b3 440
35bd67b2
PK
441static inline u8 get_vbus_power(struct device *dev)
442{
443 struct regulator *vbus_supply;
444 int current_uA;
445
446 vbus_supply = regulator_get_optional(dev, "vbus");
447 if (IS_ERR(vbus_supply))
448 return 255;
449 current_uA = regulator_get_current_limit(vbus_supply);
450 regulator_put(vbus_supply);
451 if (current_uA <= 0 || current_uA > 510000)
452 return 255;
453 return current_uA / 1000 / 2;
454}
455
d6299b6e
AB
456#ifdef CONFIG_USB_TI_CPPI41_DMA
457static void da8xx_dma_controller_callback(struct dma_controller *c)
458{
459 struct musb *musb = c->musb;
460 void __iomem *reg_base = musb->ctrl_base;
461
462 musb_writel(reg_base, DA8XX_USB_END_OF_INTR_REG, 0);
463}
464
465static struct dma_controller *
466da8xx_dma_controller_create(struct musb *musb, void __iomem *base)
467{
468 struct dma_controller *controller;
469
470 controller = cppi41_dma_controller_create(musb, base);
471 if (IS_ERR_OR_NULL(controller))
472 return controller;
473
474 controller->dma_callback = da8xx_dma_controller_callback;
475
476 return controller;
477}
478#endif
479
f7ec9437 480static const struct musb_platform_ops da8xx_ops = {
d6299b6e 481 .quirks = MUSB_INDEXED_EP | MUSB_PRESERVE_SESSION |
593bc462 482 MUSB_DMA_CPPI41 | MUSB_DA8XX,
743411b3
FB
483 .init = da8xx_musb_init,
484 .exit = da8xx_musb_exit,
485
8a77f05a 486 .fifo_mode = 2,
d6299b6e
AB
487#ifdef CONFIG_USB_TI_CPPI41_DMA
488 .dma_init = da8xx_dma_controller_create,
489 .dma_exit = cppi41_dma_controller_destroy,
490#endif
743411b3
FB
491 .enable = da8xx_musb_enable,
492 .disable = da8xx_musb_disable,
493
494 .set_mode = da8xx_musb_set_mode,
495 .try_idle = da8xx_musb_try_idle,
496
497 .set_vbus = da8xx_musb_set_vbus,
498};
8ceae51e 499
af384875
RK
500static const struct platform_device_info da8xx_dev_info = {
501 .name = "musb-hdrc",
502 .id = PLATFORM_DEVID_AUTO,
503 .dma_mask = DMA_BIT_MASK(32),
504};
8ceae51e 505
35bd67b2
PK
506static const struct musb_hdrc_config da8xx_config = {
507 .ram_bits = 10,
508 .num_eps = 5,
509 .multipoint = 1,
510};
511
9f41ebfb 512static struct of_dev_auxdata da8xx_auxdata_lookup[] = {
d6299b6e
AB
513 OF_DEV_AUXDATA("ti,da830-cppi41", 0x01e01000, "cppi41-dmaengine",
514 NULL),
515 {}
516};
517
41ac7b3a 518static int da8xx_probe(struct platform_device *pdev)
8ceae51e 519{
09fc7d22 520 struct resource musb_resources[2];
c1a7d67c 521 struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
e6480faa 522 struct da8xx_glue *glue;
af384875 523 struct platform_device_info pinfo;
03491761 524 struct clk *clk;
35bd67b2 525 struct device_node *np = pdev->dev.of_node;
d458fe9a 526 int ret;
03491761 527
d458fe9a 528 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
92c0c490 529 if (!glue)
d458fe9a 530 return -ENOMEM;
e6480faa 531
d458fe9a 532 clk = devm_clk_get(&pdev->dev, "usb20");
03491761
FB
533 if (IS_ERR(clk)) {
534 dev_err(&pdev->dev, "failed to get clock\n");
d458fe9a 535 return PTR_ERR(clk);
03491761
FB
536 }
537
947c49af
DL
538 glue->phy = devm_phy_get(&pdev->dev, "usb-phy");
539 if (IS_ERR(glue->phy)) {
355f1a39
DL
540 if (PTR_ERR(glue->phy) != -EPROBE_DEFER)
541 dev_err(&pdev->dev, "failed to get phy\n");
947c49af 542 return PTR_ERR(glue->phy);
03491761
FB
543 }
544
e6480faa 545 glue->dev = &pdev->dev;
03491761 546 glue->clk = clk;
e6480faa 547
35bd67b2
PK
548 if (IS_ENABLED(CONFIG_OF) && np) {
549 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
550 if (!pdata)
551 return -ENOMEM;
552
553 pdata->config = &da8xx_config;
554 pdata->mode = musb_get_mode(&pdev->dev);
555 pdata->power = get_vbus_power(&pdev->dev);
556 }
557
f7ec9437
FB
558 pdata->platform_ops = &da8xx_ops;
559
947c49af 560 glue->usb_phy = usb_phy_generic_register();
984f3be5
AB
561 ret = PTR_ERR_OR_ZERO(glue->usb_phy);
562 if (ret) {
947c49af 563 dev_err(&pdev->dev, "failed to register usb_phy\n");
984f3be5 564 return ret;
2f36ff69 565 }
e6480faa 566 platform_set_drvdata(pdev, glue);
8ceae51e 567
d6299b6e
AB
568 ret = of_platform_populate(pdev->dev.of_node, NULL,
569 da8xx_auxdata_lookup, &pdev->dev);
570 if (ret)
571 return ret;
572
09fc7d22
FB
573 memset(musb_resources, 0x00, sizeof(*musb_resources) *
574 ARRAY_SIZE(musb_resources));
575
576 musb_resources[0].name = pdev->resource[0].name;
577 musb_resources[0].start = pdev->resource[0].start;
578 musb_resources[0].end = pdev->resource[0].end;
579 musb_resources[0].flags = pdev->resource[0].flags;
580
581 musb_resources[1].name = pdev->resource[1].name;
582 musb_resources[1].start = pdev->resource[1].start;
583 musb_resources[1].end = pdev->resource[1].end;
584 musb_resources[1].flags = pdev->resource[1].flags;
585
af384875
RK
586 pinfo = da8xx_dev_info;
587 pinfo.parent = &pdev->dev;
588 pinfo.res = musb_resources;
589 pinfo.num_res = ARRAY_SIZE(musb_resources);
590 pinfo.data = pdata;
591 pinfo.size_data = sizeof(*pdata);
592
984f3be5
AB
593 glue->musb = platform_device_register_full(&pinfo);
594 ret = PTR_ERR_OR_ZERO(glue->musb);
595 if (ret) {
af384875 596 dev_err(&pdev->dev, "failed to register musb device: %d\n", ret);
947c49af 597 usb_phy_generic_unregister(glue->usb_phy);
8ceae51e
FB
598 }
599
984f3be5 600 return ret;
8ceae51e
FB
601}
602
fb4e98ab 603static int da8xx_remove(struct platform_device *pdev)
8ceae51e 604{
e6480faa 605 struct da8xx_glue *glue = platform_get_drvdata(pdev);
8ceae51e 606
b59e906c 607 platform_device_unregister(glue->musb);
947c49af 608 usb_phy_generic_unregister(glue->usb_phy);
8ceae51e
FB
609
610 return 0;
611}
612
71f5a0ad
AB
613#ifdef CONFIG_PM_SLEEP
614static int da8xx_suspend(struct device *dev)
615{
616 int ret;
617 struct da8xx_glue *glue = dev_get_drvdata(dev);
618
619 ret = phy_power_off(glue->phy);
620 if (ret)
621 return ret;
622 clk_disable_unprepare(glue->clk);
623
624 return 0;
625}
626
627static int da8xx_resume(struct device *dev)
628{
629 int ret;
630 struct da8xx_glue *glue = dev_get_drvdata(dev);
631
632 ret = clk_prepare_enable(glue->clk);
633 if (ret)
634 return ret;
635 return phy_power_on(glue->phy);
636}
637#endif
638
639static SIMPLE_DEV_PM_OPS(da8xx_pm_ops, da8xx_suspend, da8xx_resume);
640
35bd67b2
PK
641#ifdef CONFIG_OF
642static const struct of_device_id da8xx_id_table[] = {
643 {
644 .compatible = "ti,da830-musb",
645 },
646 {},
647};
648MODULE_DEVICE_TABLE(of, da8xx_id_table);
649#endif
650
8ceae51e 651static struct platform_driver da8xx_driver = {
e9e8c85e 652 .probe = da8xx_probe,
7690417d 653 .remove = da8xx_remove,
8ceae51e
FB
654 .driver = {
655 .name = "musb-da8xx",
71f5a0ad 656 .pm = &da8xx_pm_ops,
35bd67b2 657 .of_match_table = of_match_ptr(da8xx_id_table),
8ceae51e
FB
658 },
659};
660
661MODULE_DESCRIPTION("DA8xx/OMAP-L1x MUSB Glue Layer");
662MODULE_AUTHOR("Sergei Shtylyov <sshtylyov@ru.mvista.com>");
663MODULE_LICENSE("GPL v2");
0b07734d 664module_platform_driver(da8xx_driver);