2 * Allwinner sun4i MUSB Glue Layer
7 * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/clk.h>
21 #include <linux/err.h>
22 #include <linux/extcon.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
27 #include <linux/phy/phy-sun4i-usb.h>
28 #include <linux/platform_device.h>
29 #include <linux/reset.h>
30 #include <linux/soc/sunxi/sunxi_sram.h>
31 #include <linux/usb/musb.h>
32 #include <linux/usb/of.h>
33 #include <linux/usb/usb_phy_generic.h>
34 #include <linux/workqueue.h>
35 #include "musb_core.h"
38 * Register offsets, note sunxi musb has a different layout then most
39 * musb implementations, we translate the layout in musb_readb & friends.
41 #define SUNXI_MUSB_POWER 0x0040
42 #define SUNXI_MUSB_DEVCTL 0x0041
43 #define SUNXI_MUSB_INDEX 0x0042
44 #define SUNXI_MUSB_VEND0 0x0043
45 #define SUNXI_MUSB_INTRTX 0x0044
46 #define SUNXI_MUSB_INTRRX 0x0046
47 #define SUNXI_MUSB_INTRTXE 0x0048
48 #define SUNXI_MUSB_INTRRXE 0x004a
49 #define SUNXI_MUSB_INTRUSB 0x004c
50 #define SUNXI_MUSB_INTRUSBE 0x0050
51 #define SUNXI_MUSB_FRAME 0x0054
52 #define SUNXI_MUSB_TXFIFOSZ 0x0090
53 #define SUNXI_MUSB_TXFIFOADD 0x0092
54 #define SUNXI_MUSB_RXFIFOSZ 0x0094
55 #define SUNXI_MUSB_RXFIFOADD 0x0096
56 #define SUNXI_MUSB_FADDR 0x0098
57 #define SUNXI_MUSB_TXFUNCADDR 0x0098
58 #define SUNXI_MUSB_TXHUBADDR 0x009a
59 #define SUNXI_MUSB_TXHUBPORT 0x009b
60 #define SUNXI_MUSB_RXFUNCADDR 0x009c
61 #define SUNXI_MUSB_RXHUBADDR 0x009e
62 #define SUNXI_MUSB_RXHUBPORT 0x009f
63 #define SUNXI_MUSB_CONFIGDATA 0x00c0
66 #define SUNXI_MUSB_VEND0_PIO_MODE 0
69 #define SUNXI_MUSB_FL_ENABLED 0
70 #define SUNXI_MUSB_FL_HOSTMODE 1
71 #define SUNXI_MUSB_FL_HOSTMODE_PEND 2
72 #define SUNXI_MUSB_FL_VBUS_ON 3
73 #define SUNXI_MUSB_FL_PHY_ON 4
74 #define SUNXI_MUSB_FL_HAS_SRAM 5
75 #define SUNXI_MUSB_FL_HAS_RESET 6
76 #define SUNXI_MUSB_FL_NO_CONFIGDATA 7
77 #define SUNXI_MUSB_FL_PHY_MODE_PEND 8
79 /* Our read/write methods need access and do not get passed in a musb ref :| */
80 static struct musb *sunxi_musb;
85 struct platform_device *musb_pdev;
87 struct reset_control *rst;
89 struct platform_device *usb_phy;
90 struct usb_phy *xceiv;
91 enum phy_mode phy_mode;
93 struct work_struct work;
94 struct extcon_dev *extcon;
95 struct notifier_block host_nb;
98 /* phy_power_on / off may sleep, so we use a workqueue */
99 static void sunxi_musb_work(struct work_struct *work)
101 struct sunxi_glue *glue = container_of(work, struct sunxi_glue, work);
102 bool vbus_on, phy_on;
104 if (!test_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
107 if (test_and_clear_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags)) {
108 struct musb *musb = glue->musb;
112 spin_lock_irqsave(&musb->lock, flags);
114 devctl = readb(musb->mregs + SUNXI_MUSB_DEVCTL);
115 if (test_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags)) {
116 set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
117 musb->xceiv->otg->default_a = 1;
118 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
120 devctl |= MUSB_DEVCTL_SESSION;
122 clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
123 musb->xceiv->otg->default_a = 0;
124 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
126 devctl &= ~MUSB_DEVCTL_SESSION;
128 writeb(devctl, musb->mregs + SUNXI_MUSB_DEVCTL);
130 spin_unlock_irqrestore(&musb->lock, flags);
133 vbus_on = test_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
134 phy_on = test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
136 if (phy_on != vbus_on) {
138 phy_power_on(glue->phy);
139 set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
141 phy_power_off(glue->phy);
142 clear_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
146 if (test_and_clear_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags))
147 phy_set_mode(glue->phy, glue->phy_mode);
150 static void sunxi_musb_set_vbus(struct musb *musb, int is_on)
152 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
155 set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
156 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
158 clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
161 schedule_work(&glue->work);
164 static void sunxi_musb_pre_root_reset_end(struct musb *musb)
166 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
168 sun4i_usb_phy_set_squelch_detect(glue->phy, false);
171 static void sunxi_musb_post_root_reset_end(struct musb *musb)
173 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
175 sun4i_usb_phy_set_squelch_detect(glue->phy, true);
178 static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
180 struct musb *musb = __hci;
183 spin_lock_irqsave(&musb->lock, flags);
185 musb->int_usb = readb(musb->mregs + SUNXI_MUSB_INTRUSB);
187 writeb(musb->int_usb, musb->mregs + SUNXI_MUSB_INTRUSB);
189 if ((musb->int_usb & MUSB_INTR_RESET) && !is_host_active(musb)) {
190 /* ep0 FADDR must be 0 when (re)entering peripheral mode */
191 musb_ep_select(musb->mregs, 0);
192 musb_writeb(musb->mregs, MUSB_FADDR, 0);
195 musb->int_tx = readw(musb->mregs + SUNXI_MUSB_INTRTX);
197 writew(musb->int_tx, musb->mregs + SUNXI_MUSB_INTRTX);
199 musb->int_rx = readw(musb->mregs + SUNXI_MUSB_INTRRX);
201 writew(musb->int_rx, musb->mregs + SUNXI_MUSB_INTRRX);
203 musb_interrupt(musb);
205 spin_unlock_irqrestore(&musb->lock, flags);
210 static int sunxi_musb_host_notifier(struct notifier_block *nb,
211 unsigned long event, void *ptr)
213 struct sunxi_glue *glue = container_of(nb, struct sunxi_glue, host_nb);
216 set_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
218 clear_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
220 set_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags);
221 schedule_work(&glue->work);
226 static int sunxi_musb_init(struct musb *musb)
228 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
232 musb->phy = glue->phy;
233 musb->xceiv = glue->xceiv;
235 if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags)) {
236 ret = sunxi_sram_claim(musb->controller->parent);
241 ret = clk_prepare_enable(glue->clk);
243 goto error_sram_release;
245 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
246 ret = reset_control_deassert(glue->rst);
248 goto error_clk_disable;
251 writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
253 /* Register notifier before calling phy_init() */
254 ret = extcon_register_notifier(glue->extcon, EXTCON_USB_HOST,
257 goto error_reset_assert;
259 ret = phy_init(glue->phy);
261 goto error_unregister_notifier;
263 musb->isr = sunxi_musb_interrupt;
265 /* Stop the musb-core from doing runtime pm (not supported on sunxi) */
266 pm_runtime_get(musb->controller);
270 error_unregister_notifier:
271 extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
274 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
275 reset_control_assert(glue->rst);
277 clk_disable_unprepare(glue->clk);
279 if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
280 sunxi_sram_release(musb->controller->parent);
284 static int sunxi_musb_exit(struct musb *musb)
286 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
288 pm_runtime_put(musb->controller);
290 cancel_work_sync(&glue->work);
291 if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
292 phy_power_off(glue->phy);
296 extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
299 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
300 reset_control_assert(glue->rst);
302 clk_disable_unprepare(glue->clk);
303 if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
304 sunxi_sram_release(musb->controller->parent);
309 static void sunxi_musb_enable(struct musb *musb)
311 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
315 /* musb_core does not call us in a balanced manner */
316 if (test_and_set_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
319 schedule_work(&glue->work);
322 static void sunxi_musb_disable(struct musb *musb)
324 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
326 clear_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags);
329 static struct dma_controller *
330 sunxi_musb_dma_controller_create(struct musb *musb, void __iomem *base)
335 static void sunxi_musb_dma_controller_destroy(struct dma_controller *c)
339 static int sunxi_musb_set_mode(struct musb *musb, u8 mode)
341 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
342 enum phy_mode new_mode;
346 new_mode = PHY_MODE_USB_HOST;
348 case MUSB_PERIPHERAL:
349 new_mode = PHY_MODE_USB_DEVICE;
352 new_mode = PHY_MODE_USB_OTG;
355 dev_err(musb->controller->parent,
356 "Error requested mode not supported by this kernel\n");
360 if (glue->phy_mode == new_mode)
363 if (musb->port_mode != MUSB_PORT_MODE_DUAL_ROLE) {
364 dev_err(musb->controller->parent,
365 "Error changing modes is only supported in dual role mode\n");
369 if (musb->port1_status & USB_PORT_STAT_ENABLE)
370 musb_root_disconnect(musb);
373 * phy_set_mode may sleep, and we're called with a spinlock held,
374 * so let sunxi_musb_work deal with it.
376 glue->phy_mode = new_mode;
377 set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
378 schedule_work(&glue->work);
383 static int sunxi_musb_recover(struct musb *musb)
385 struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
388 * Schedule a phy_set_mode with the current glue->phy_mode value,
389 * this will force end the current session.
391 set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
392 schedule_work(&glue->work);
398 * sunxi musb register layout
399 * 0x00 - 0x17 fifo regs, 1 long per fifo
400 * 0x40 - 0x57 generic control regs (power - frame)
401 * 0x80 - 0x8f ep control regs (addressed through hw_ep->regs, indexed)
402 * 0x90 - 0x97 fifo control regs (indexed)
403 * 0x98 - 0x9f multipoint / busctl regs (indexed)
404 * 0xc0 configdata reg
407 static u32 sunxi_musb_fifo_offset(u8 epnum)
412 static u32 sunxi_musb_ep_offset(u8 epnum, u16 offset)
414 WARN_ONCE(offset != 0,
415 "sunxi_musb_ep_offset called with non 0 offset\n");
417 return 0x80; /* indexed, so ignore epnum */
420 static u32 sunxi_musb_busctl_offset(u8 epnum, u16 offset)
422 return SUNXI_MUSB_TXFUNCADDR + offset;
425 static u8 sunxi_musb_readb(const void __iomem *addr, unsigned offset)
427 struct sunxi_glue *glue;
429 if (addr == sunxi_musb->mregs) {
430 /* generic control or fifo control reg access */
433 return readb(addr + SUNXI_MUSB_FADDR);
435 return readb(addr + SUNXI_MUSB_POWER);
437 return readb(addr + SUNXI_MUSB_INTRUSB);
439 return readb(addr + SUNXI_MUSB_INTRUSBE);
441 return readb(addr + SUNXI_MUSB_INDEX);
443 return 0; /* No testmode on sunxi */
445 return readb(addr + SUNXI_MUSB_DEVCTL);
447 return readb(addr + SUNXI_MUSB_TXFIFOSZ);
449 return readb(addr + SUNXI_MUSB_RXFIFOSZ);
450 case MUSB_CONFIGDATA + 0x10: /* See musb_read_configdata() */
451 glue = dev_get_drvdata(sunxi_musb->controller->parent);
452 /* A33 saves a reg, and we get to hardcode this */
453 if (test_bit(SUNXI_MUSB_FL_NO_CONFIGDATA,
457 return readb(addr + SUNXI_MUSB_CONFIGDATA);
458 /* Offset for these is fixed by sunxi_musb_busctl_offset() */
459 case SUNXI_MUSB_TXFUNCADDR:
460 case SUNXI_MUSB_TXHUBADDR:
461 case SUNXI_MUSB_TXHUBPORT:
462 case SUNXI_MUSB_RXFUNCADDR:
463 case SUNXI_MUSB_RXHUBADDR:
464 case SUNXI_MUSB_RXHUBPORT:
465 /* multipoint / busctl reg access */
466 return readb(addr + offset);
468 dev_err(sunxi_musb->controller->parent,
469 "Error unknown readb offset %u\n", offset);
472 } else if (addr == (sunxi_musb->mregs + 0x80)) {
473 /* ep control reg access */
474 /* sunxi has a 2 byte hole before the txtype register */
475 if (offset >= MUSB_TXTYPE)
477 return readb(addr + offset);
480 dev_err(sunxi_musb->controller->parent,
481 "Error unknown readb at 0x%x bytes offset\n",
482 (int)(addr - sunxi_musb->mregs));
486 static void sunxi_musb_writeb(void __iomem *addr, unsigned offset, u8 data)
488 if (addr == sunxi_musb->mregs) {
489 /* generic control or fifo control reg access */
492 return writeb(data, addr + SUNXI_MUSB_FADDR);
494 return writeb(data, addr + SUNXI_MUSB_POWER);
496 return writeb(data, addr + SUNXI_MUSB_INTRUSB);
498 return writeb(data, addr + SUNXI_MUSB_INTRUSBE);
500 return writeb(data, addr + SUNXI_MUSB_INDEX);
503 dev_warn(sunxi_musb->controller->parent,
504 "sunxi-musb does not have testmode\n");
507 return writeb(data, addr + SUNXI_MUSB_DEVCTL);
509 return writeb(data, addr + SUNXI_MUSB_TXFIFOSZ);
511 return writeb(data, addr + SUNXI_MUSB_RXFIFOSZ);
512 /* Offset for these is fixed by sunxi_musb_busctl_offset() */
513 case SUNXI_MUSB_TXFUNCADDR:
514 case SUNXI_MUSB_TXHUBADDR:
515 case SUNXI_MUSB_TXHUBPORT:
516 case SUNXI_MUSB_RXFUNCADDR:
517 case SUNXI_MUSB_RXHUBADDR:
518 case SUNXI_MUSB_RXHUBPORT:
519 /* multipoint / busctl reg access */
520 return writeb(data, addr + offset);
522 dev_err(sunxi_musb->controller->parent,
523 "Error unknown writeb offset %u\n", offset);
526 } else if (addr == (sunxi_musb->mregs + 0x80)) {
527 /* ep control reg access */
528 if (offset >= MUSB_TXTYPE)
530 return writeb(data, addr + offset);
533 dev_err(sunxi_musb->controller->parent,
534 "Error unknown writeb at 0x%x bytes offset\n",
535 (int)(addr - sunxi_musb->mregs));
538 static u16 sunxi_musb_readw(const void __iomem *addr, unsigned offset)
540 if (addr == sunxi_musb->mregs) {
541 /* generic control or fifo control reg access */
544 return readw(addr + SUNXI_MUSB_INTRTX);
546 return readw(addr + SUNXI_MUSB_INTRRX);
548 return readw(addr + SUNXI_MUSB_INTRTXE);
550 return readw(addr + SUNXI_MUSB_INTRRXE);
552 return readw(addr + SUNXI_MUSB_FRAME);
554 return readw(addr + SUNXI_MUSB_TXFIFOADD);
556 return readw(addr + SUNXI_MUSB_RXFIFOADD);
558 return 0; /* sunxi musb version is not known */
560 dev_err(sunxi_musb->controller->parent,
561 "Error unknown readw offset %u\n", offset);
564 } else if (addr == (sunxi_musb->mregs + 0x80)) {
565 /* ep control reg access */
566 return readw(addr + offset);
569 dev_err(sunxi_musb->controller->parent,
570 "Error unknown readw at 0x%x bytes offset\n",
571 (int)(addr - sunxi_musb->mregs));
575 static void sunxi_musb_writew(void __iomem *addr, unsigned offset, u16 data)
577 if (addr == sunxi_musb->mregs) {
578 /* generic control or fifo control reg access */
581 return writew(data, addr + SUNXI_MUSB_INTRTX);
583 return writew(data, addr + SUNXI_MUSB_INTRRX);
585 return writew(data, addr + SUNXI_MUSB_INTRTXE);
587 return writew(data, addr + SUNXI_MUSB_INTRRXE);
589 return writew(data, addr + SUNXI_MUSB_FRAME);
591 return writew(data, addr + SUNXI_MUSB_TXFIFOADD);
593 return writew(data, addr + SUNXI_MUSB_RXFIFOADD);
595 dev_err(sunxi_musb->controller->parent,
596 "Error unknown writew offset %u\n", offset);
599 } else if (addr == (sunxi_musb->mregs + 0x80)) {
600 /* ep control reg access */
601 return writew(data, addr + offset);
604 dev_err(sunxi_musb->controller->parent,
605 "Error unknown writew at 0x%x bytes offset\n",
606 (int)(addr - sunxi_musb->mregs));
609 static const struct musb_platform_ops sunxi_musb_ops = {
610 .quirks = MUSB_INDEXED_EP,
611 .init = sunxi_musb_init,
612 .exit = sunxi_musb_exit,
613 .enable = sunxi_musb_enable,
614 .disable = sunxi_musb_disable,
615 .fifo_offset = sunxi_musb_fifo_offset,
616 .ep_offset = sunxi_musb_ep_offset,
617 .busctl_offset = sunxi_musb_busctl_offset,
618 .readb = sunxi_musb_readb,
619 .writeb = sunxi_musb_writeb,
620 .readw = sunxi_musb_readw,
621 .writew = sunxi_musb_writew,
622 .dma_init = sunxi_musb_dma_controller_create,
623 .dma_exit = sunxi_musb_dma_controller_destroy,
624 .set_mode = sunxi_musb_set_mode,
625 .recover = sunxi_musb_recover,
626 .set_vbus = sunxi_musb_set_vbus,
627 .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
628 .post_root_reset_end = sunxi_musb_post_root_reset_end,
631 /* Allwinner OTG supports up to 5 endpoints */
632 #define SUNXI_MUSB_MAX_EP_NUM 6
633 #define SUNXI_MUSB_RAM_BITS 11
635 static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
636 MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
637 MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
638 MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
639 MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
640 MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
641 MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
642 MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
643 MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
644 MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
645 MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
648 static struct musb_hdrc_config sunxi_musb_hdrc_config = {
649 .fifo_cfg = sunxi_musb_mode_cfg,
650 .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
654 .num_eps = SUNXI_MUSB_MAX_EP_NUM,
655 .ram_bits = SUNXI_MUSB_RAM_BITS,
659 static int sunxi_musb_probe(struct platform_device *pdev)
661 struct musb_hdrc_platform_data pdata;
662 struct platform_device_info pinfo;
663 struct sunxi_glue *glue;
664 struct device_node *np = pdev->dev.of_node;
668 dev_err(&pdev->dev, "Error no device tree node found\n");
672 glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
676 memset(&pdata, 0, sizeof(pdata));
677 switch (usb_get_dr_mode(&pdev->dev)) {
678 #if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
679 case USB_DR_MODE_HOST:
680 pdata.mode = MUSB_PORT_MODE_HOST;
681 glue->phy_mode = PHY_MODE_USB_HOST;
684 #if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_GADGET
685 case USB_DR_MODE_PERIPHERAL:
686 pdata.mode = MUSB_PORT_MODE_GADGET;
687 glue->phy_mode = PHY_MODE_USB_DEVICE;
690 #ifdef CONFIG_USB_MUSB_DUAL_ROLE
691 case USB_DR_MODE_OTG:
692 pdata.mode = MUSB_PORT_MODE_DUAL_ROLE;
693 glue->phy_mode = PHY_MODE_USB_OTG;
697 dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
700 pdata.platform_ops = &sunxi_musb_ops;
701 pdata.config = &sunxi_musb_hdrc_config;
703 glue->dev = &pdev->dev;
704 INIT_WORK(&glue->work, sunxi_musb_work);
705 glue->host_nb.notifier_call = sunxi_musb_host_notifier;
707 if (of_device_is_compatible(np, "allwinner,sun4i-a10-musb"))
708 set_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags);
710 if (of_device_is_compatible(np, "allwinner,sun6i-a31-musb"))
711 set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
713 if (of_device_is_compatible(np, "allwinner,sun8i-a33-musb")) {
714 set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
715 set_bit(SUNXI_MUSB_FL_NO_CONFIGDATA, &glue->flags);
718 glue->clk = devm_clk_get(&pdev->dev, NULL);
719 if (IS_ERR(glue->clk)) {
720 dev_err(&pdev->dev, "Error getting clock: %ld\n",
722 return PTR_ERR(glue->clk);
725 if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
726 glue->rst = devm_reset_control_get(&pdev->dev, NULL);
727 if (IS_ERR(glue->rst)) {
728 if (PTR_ERR(glue->rst) == -EPROBE_DEFER)
729 return -EPROBE_DEFER;
730 dev_err(&pdev->dev, "Error getting reset %ld\n",
732 return PTR_ERR(glue->rst);
736 glue->extcon = extcon_get_edev_by_phandle(&pdev->dev, 0);
737 if (IS_ERR(glue->extcon)) {
738 if (PTR_ERR(glue->extcon) == -EPROBE_DEFER)
739 return -EPROBE_DEFER;
740 dev_err(&pdev->dev, "Invalid or missing extcon\n");
741 return PTR_ERR(glue->extcon);
744 glue->phy = devm_phy_get(&pdev->dev, "usb");
745 if (IS_ERR(glue->phy)) {
746 if (PTR_ERR(glue->phy) == -EPROBE_DEFER)
747 return -EPROBE_DEFER;
748 dev_err(&pdev->dev, "Error getting phy %ld\n",
750 return PTR_ERR(glue->phy);
753 glue->usb_phy = usb_phy_generic_register();
754 if (IS_ERR(glue->usb_phy)) {
755 dev_err(&pdev->dev, "Error registering usb-phy %ld\n",
756 PTR_ERR(glue->usb_phy));
757 return PTR_ERR(glue->usb_phy);
760 glue->xceiv = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
761 if (IS_ERR(glue->xceiv)) {
762 ret = PTR_ERR(glue->xceiv);
763 dev_err(&pdev->dev, "Error getting usb-phy %d\n", ret);
764 goto err_unregister_usb_phy;
767 platform_set_drvdata(pdev, glue);
769 memset(&pinfo, 0, sizeof(pinfo));
770 pinfo.name = "musb-hdrc";
771 pinfo.id = PLATFORM_DEVID_AUTO;
772 pinfo.parent = &pdev->dev;
773 pinfo.res = pdev->resource;
774 pinfo.num_res = pdev->num_resources;
776 pinfo.size_data = sizeof(pdata);
778 glue->musb_pdev = platform_device_register_full(&pinfo);
779 if (IS_ERR(glue->musb_pdev)) {
780 ret = PTR_ERR(glue->musb_pdev);
781 dev_err(&pdev->dev, "Error registering musb dev: %d\n", ret);
782 goto err_unregister_usb_phy;
787 err_unregister_usb_phy:
788 usb_phy_generic_unregister(glue->usb_phy);
792 static int sunxi_musb_remove(struct platform_device *pdev)
794 struct sunxi_glue *glue = platform_get_drvdata(pdev);
795 struct platform_device *usb_phy = glue->usb_phy;
797 platform_device_unregister(glue->musb_pdev);
798 usb_phy_generic_unregister(usb_phy);
803 static const struct of_device_id sunxi_musb_match[] = {
804 { .compatible = "allwinner,sun4i-a10-musb", },
805 { .compatible = "allwinner,sun6i-a31-musb", },
806 { .compatible = "allwinner,sun8i-a33-musb", },
809 MODULE_DEVICE_TABLE(of, sunxi_musb_match);
811 static struct platform_driver sunxi_musb_driver = {
812 .probe = sunxi_musb_probe,
813 .remove = sunxi_musb_remove,
815 .name = "musb-sunxi",
816 .of_match_table = sunxi_musb_match,
819 module_platform_driver(sunxi_musb_driver);
821 MODULE_DESCRIPTION("Allwinner sunxi MUSB Glue Layer");
823 MODULE_LICENSE("GPL v2");