treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / musb / mediatek.c
blob6b88c2f5d970413bb76134fddce62bce5689dbb6
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2019 MediaTek Inc.
5 * Author:
6 * Min Guo <min.guo@mediatek.com>
7 * Yonglong Wu <yonglong.wu@mediatek.com>
8 */
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/module.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/usb/role.h>
16 #include <linux/usb/usb_phy_generic.h>
17 #include "musb_core.h"
18 #include "musb_dma.h"
20 #define USB_L1INTS 0x00a0
21 #define USB_L1INTM 0x00a4
22 #define MTK_MUSB_TXFUNCADDR 0x0480
24 /* MediaTek controller toggle enable and status reg */
25 #define MUSB_RXTOG 0x80
26 #define MUSB_RXTOGEN 0x82
27 #define MUSB_TXTOG 0x84
28 #define MUSB_TXTOGEN 0x86
29 #define MTK_TOGGLE_EN GENMASK(15, 0)
31 #define TX_INT_STATUS BIT(0)
32 #define RX_INT_STATUS BIT(1)
33 #define USBCOM_INT_STATUS BIT(2)
34 #define DMA_INT_STATUS BIT(3)
36 #define DMA_INTR_STATUS_MSK GENMASK(7, 0)
37 #define DMA_INTR_UNMASK_SET_MSK GENMASK(31, 24)
39 struct mtk_glue {
40 struct device *dev;
41 struct musb *musb;
42 struct platform_device *musb_pdev;
43 struct platform_device *usb_phy;
44 struct phy *phy;
45 struct usb_phy *xceiv;
46 enum phy_mode phy_mode;
47 struct clk *main;
48 struct clk *mcu;
49 struct clk *univpll;
50 enum usb_role role;
51 struct usb_role_switch *role_sw;
54 static int mtk_musb_clks_get(struct mtk_glue *glue)
56 struct device *dev = glue->dev;
58 glue->main = devm_clk_get(dev, "main");
59 if (IS_ERR(glue->main)) {
60 dev_err(dev, "fail to get main clock\n");
61 return PTR_ERR(glue->main);
64 glue->mcu = devm_clk_get(dev, "mcu");
65 if (IS_ERR(glue->mcu)) {
66 dev_err(dev, "fail to get mcu clock\n");
67 return PTR_ERR(glue->mcu);
70 glue->univpll = devm_clk_get(dev, "univpll");
71 if (IS_ERR(glue->univpll)) {
72 dev_err(dev, "fail to get univpll clock\n");
73 return PTR_ERR(glue->univpll);
76 return 0;
79 static int mtk_musb_clks_enable(struct mtk_glue *glue)
81 int ret;
83 ret = clk_prepare_enable(glue->main);
84 if (ret) {
85 dev_err(glue->dev, "failed to enable main clock\n");
86 goto err_main_clk;
89 ret = clk_prepare_enable(glue->mcu);
90 if (ret) {
91 dev_err(glue->dev, "failed to enable mcu clock\n");
92 goto err_mcu_clk;
95 ret = clk_prepare_enable(glue->univpll);
96 if (ret) {
97 dev_err(glue->dev, "failed to enable univpll clock\n");
98 goto err_univpll_clk;
101 return 0;
103 err_univpll_clk:
104 clk_disable_unprepare(glue->mcu);
105 err_mcu_clk:
106 clk_disable_unprepare(glue->main);
107 err_main_clk:
108 return ret;
111 static void mtk_musb_clks_disable(struct mtk_glue *glue)
113 clk_disable_unprepare(glue->univpll);
114 clk_disable_unprepare(glue->mcu);
115 clk_disable_unprepare(glue->main);
118 static int musb_usb_role_sx_set(struct device *dev, enum usb_role role)
120 struct mtk_glue *glue = dev_get_drvdata(dev);
121 struct musb *musb = glue->musb;
122 u8 devctl = readb(musb->mregs + MUSB_DEVCTL);
123 enum usb_role new_role;
125 if (role == glue->role)
126 return 0;
128 switch (role) {
129 case USB_ROLE_HOST:
130 musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
131 glue->phy_mode = PHY_MODE_USB_HOST;
132 new_role = USB_ROLE_HOST;
133 if (glue->role == USB_ROLE_NONE)
134 phy_power_on(glue->phy);
136 devctl |= MUSB_DEVCTL_SESSION;
137 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
138 MUSB_HST_MODE(musb);
139 break;
140 case USB_ROLE_DEVICE:
141 musb->xceiv->otg->state = OTG_STATE_B_IDLE;
142 glue->phy_mode = PHY_MODE_USB_DEVICE;
143 new_role = USB_ROLE_DEVICE;
144 devctl &= ~MUSB_DEVCTL_SESSION;
145 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
146 if (glue->role == USB_ROLE_NONE)
147 phy_power_on(glue->phy);
149 MUSB_DEV_MODE(musb);
150 break;
151 case USB_ROLE_NONE:
152 glue->phy_mode = PHY_MODE_USB_OTG;
153 new_role = USB_ROLE_NONE;
154 devctl &= ~MUSB_DEVCTL_SESSION;
155 musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
156 if (glue->role != USB_ROLE_NONE)
157 phy_power_off(glue->phy);
159 break;
160 default:
161 dev_err(glue->dev, "Invalid State\n");
162 return -EINVAL;
165 glue->role = new_role;
166 phy_set_mode(glue->phy, glue->phy_mode);
168 return 0;
171 static enum usb_role musb_usb_role_sx_get(struct device *dev)
173 struct mtk_glue *glue = dev_get_drvdata(dev);
175 return glue->role;
178 static int mtk_otg_switch_init(struct mtk_glue *glue)
180 struct usb_role_switch_desc role_sx_desc = { 0 };
182 role_sx_desc.set = musb_usb_role_sx_set;
183 role_sx_desc.get = musb_usb_role_sx_get;
184 role_sx_desc.fwnode = dev_fwnode(glue->dev);
185 glue->role_sw = usb_role_switch_register(glue->dev, &role_sx_desc);
187 return PTR_ERR_OR_ZERO(glue->role_sw);
190 static void mtk_otg_switch_exit(struct mtk_glue *glue)
192 return usb_role_switch_unregister(glue->role_sw);
195 static irqreturn_t generic_interrupt(int irq, void *__hci)
197 unsigned long flags;
198 irqreturn_t retval = IRQ_NONE;
199 struct musb *musb = __hci;
201 spin_lock_irqsave(&musb->lock, flags);
202 musb->int_usb = musb_clearb(musb->mregs, MUSB_INTRUSB);
203 musb->int_rx = musb_clearw(musb->mregs, MUSB_INTRRX);
204 musb->int_tx = musb_clearw(musb->mregs, MUSB_INTRTX);
206 if (musb->int_usb || musb->int_tx || musb->int_rx)
207 retval = musb_interrupt(musb);
209 spin_unlock_irqrestore(&musb->lock, flags);
211 return retval;
214 static irqreturn_t mtk_musb_interrupt(int irq, void *dev_id)
216 irqreturn_t retval = IRQ_NONE;
217 struct musb *musb = (struct musb *)dev_id;
218 u32 l1_ints;
220 l1_ints = musb_readl(musb->mregs, USB_L1INTS) &
221 musb_readl(musb->mregs, USB_L1INTM);
223 if (l1_ints & (TX_INT_STATUS | RX_INT_STATUS | USBCOM_INT_STATUS))
224 retval = generic_interrupt(irq, musb);
226 #if defined(CONFIG_USB_INVENTRA_DMA)
227 if (l1_ints & DMA_INT_STATUS)
228 retval = dma_controller_irq(irq, musb->dma_controller);
229 #endif
230 return retval;
233 static u32 mtk_musb_busctl_offset(u8 epnum, u16 offset)
235 return MTK_MUSB_TXFUNCADDR + offset + 8 * epnum;
238 static u8 mtk_musb_clearb(void __iomem *addr, unsigned int offset)
240 u8 data;
242 /* W1C */
243 data = musb_readb(addr, offset);
244 musb_writeb(addr, offset, data);
245 return data;
248 static u16 mtk_musb_clearw(void __iomem *addr, unsigned int offset)
250 u16 data;
252 /* W1C */
253 data = musb_readw(addr, offset);
254 musb_writew(addr, offset, data);
255 return data;
258 static int mtk_musb_set_mode(struct musb *musb, u8 mode)
260 struct device *dev = musb->controller;
261 struct mtk_glue *glue = dev_get_drvdata(dev->parent);
262 enum phy_mode new_mode;
263 enum usb_role new_role;
265 switch (mode) {
266 case MUSB_HOST:
267 new_mode = PHY_MODE_USB_HOST;
268 new_role = USB_ROLE_HOST;
269 break;
270 case MUSB_PERIPHERAL:
271 new_mode = PHY_MODE_USB_DEVICE;
272 new_role = USB_ROLE_DEVICE;
273 break;
274 case MUSB_OTG:
275 new_mode = PHY_MODE_USB_OTG;
276 new_role = USB_ROLE_NONE;
277 break;
278 default:
279 dev_err(glue->dev, "Invalid mode request\n");
280 return -EINVAL;
283 if (glue->phy_mode == new_mode)
284 return 0;
286 if (musb->port_mode != MUSB_OTG) {
287 dev_err(glue->dev, "Does not support changing modes\n");
288 return -EINVAL;
291 glue->role = new_role;
292 musb_usb_role_sx_set(dev, glue->role);
293 return 0;
296 static int mtk_musb_init(struct musb *musb)
298 struct device *dev = musb->controller;
299 struct mtk_glue *glue = dev_get_drvdata(dev->parent);
300 int ret;
302 glue->musb = musb;
303 musb->phy = glue->phy;
304 musb->xceiv = glue->xceiv;
305 musb->is_host = false;
306 musb->isr = mtk_musb_interrupt;
308 /* Set TX/RX toggle enable */
309 musb_writew(musb->mregs, MUSB_TXTOGEN, MTK_TOGGLE_EN);
310 musb_writew(musb->mregs, MUSB_RXTOGEN, MTK_TOGGLE_EN);
312 if (musb->port_mode == MUSB_OTG) {
313 ret = mtk_otg_switch_init(glue);
314 if (ret)
315 return ret;
318 ret = phy_init(glue->phy);
319 if (ret)
320 goto err_phy_init;
322 ret = phy_power_on(glue->phy);
323 if (ret)
324 goto err_phy_power_on;
326 phy_set_mode(glue->phy, glue->phy_mode);
328 #if defined(CONFIG_USB_INVENTRA_DMA)
329 musb_writel(musb->mregs, MUSB_HSDMA_INTR,
330 DMA_INTR_STATUS_MSK | DMA_INTR_UNMASK_SET_MSK);
331 #endif
332 musb_writel(musb->mregs, USB_L1INTM, TX_INT_STATUS | RX_INT_STATUS |
333 USBCOM_INT_STATUS | DMA_INT_STATUS);
334 return 0;
336 err_phy_power_on:
337 phy_exit(glue->phy);
338 err_phy_init:
339 mtk_otg_switch_exit(glue);
340 return ret;
343 static u16 mtk_musb_get_toggle(struct musb_qh *qh, int is_out)
345 struct musb *musb = qh->hw_ep->musb;
346 u8 epnum = qh->hw_ep->epnum;
347 u16 toggle;
349 toggle = musb_readw(musb->mregs, is_out ? MUSB_TXTOG : MUSB_RXTOG);
350 return toggle & (1 << epnum);
353 static u16 mtk_musb_set_toggle(struct musb_qh *qh, int is_out, struct urb *urb)
355 struct musb *musb = qh->hw_ep->musb;
356 u8 epnum = qh->hw_ep->epnum;
357 u16 value, toggle;
359 toggle = usb_gettoggle(urb->dev, qh->epnum, is_out);
361 if (is_out) {
362 value = musb_readw(musb->mregs, MUSB_TXTOG);
363 value |= toggle << epnum;
364 musb_writew(musb->mregs, MUSB_TXTOG, value);
365 } else {
366 value = musb_readw(musb->mregs, MUSB_RXTOG);
367 value |= toggle << epnum;
368 musb_writew(musb->mregs, MUSB_RXTOG, value);
371 return 0;
374 static int mtk_musb_exit(struct musb *musb)
376 struct device *dev = musb->controller;
377 struct mtk_glue *glue = dev_get_drvdata(dev->parent);
379 mtk_otg_switch_exit(glue);
380 phy_power_off(glue->phy);
381 phy_exit(glue->phy);
382 mtk_musb_clks_disable(glue);
384 pm_runtime_put_sync(dev);
385 pm_runtime_disable(dev);
386 return 0;
389 static const struct musb_platform_ops mtk_musb_ops = {
390 .quirks = MUSB_DMA_INVENTRA,
391 .init = mtk_musb_init,
392 .get_toggle = mtk_musb_get_toggle,
393 .set_toggle = mtk_musb_set_toggle,
394 .exit = mtk_musb_exit,
395 #ifdef CONFIG_USB_INVENTRA_DMA
396 .dma_init = musbhs_dma_controller_create_noirq,
397 .dma_exit = musbhs_dma_controller_destroy,
398 #endif
399 .clearb = mtk_musb_clearb,
400 .clearw = mtk_musb_clearw,
401 .busctl_offset = mtk_musb_busctl_offset,
402 .set_mode = mtk_musb_set_mode,
405 #define MTK_MUSB_MAX_EP_NUM 8
406 #define MTK_MUSB_RAM_BITS 11
408 static struct musb_fifo_cfg mtk_musb_mode_cfg[] = {
409 { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
410 { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
411 { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
412 { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
413 { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
414 { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
415 { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
416 { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
417 { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
418 { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
419 { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 1024, },
420 { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 1024, },
421 { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
422 { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 64, },
425 static const struct musb_hdrc_config mtk_musb_hdrc_config = {
426 .fifo_cfg = mtk_musb_mode_cfg,
427 .fifo_cfg_size = ARRAY_SIZE(mtk_musb_mode_cfg),
428 .multipoint = true,
429 .dyn_fifo = true,
430 .num_eps = MTK_MUSB_MAX_EP_NUM,
431 .ram_bits = MTK_MUSB_RAM_BITS,
434 static const struct platform_device_info mtk_dev_info = {
435 .name = "musb-hdrc",
436 .id = PLATFORM_DEVID_AUTO,
437 .dma_mask = DMA_BIT_MASK(32),
440 static int mtk_musb_probe(struct platform_device *pdev)
442 struct musb_hdrc_platform_data *pdata;
443 struct mtk_glue *glue;
444 struct platform_device_info pinfo;
445 struct device *dev = &pdev->dev;
446 struct device_node *np = dev->of_node;
447 int ret = -ENOMEM;
449 glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
450 if (!glue)
451 return -ENOMEM;
453 glue->dev = dev;
454 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
455 if (!pdata)
456 return -ENOMEM;
458 ret = of_platform_populate(np, NULL, NULL, dev);
459 if (ret) {
460 dev_err(dev, "failed to create child devices at %p\n", np);
461 return ret;
464 ret = mtk_musb_clks_get(glue);
465 if (ret)
466 return ret;
468 pdata->config = &mtk_musb_hdrc_config;
469 pdata->platform_ops = &mtk_musb_ops;
470 pdata->mode = usb_get_dr_mode(dev);
472 if (IS_ENABLED(CONFIG_USB_MUSB_HOST))
473 pdata->mode = USB_DR_MODE_HOST;
474 else if (IS_ENABLED(CONFIG_USB_MUSB_GADGET))
475 pdata->mode = USB_DR_MODE_PERIPHERAL;
477 switch (pdata->mode) {
478 case USB_DR_MODE_HOST:
479 glue->phy_mode = PHY_MODE_USB_HOST;
480 glue->role = USB_ROLE_HOST;
481 break;
482 case USB_DR_MODE_PERIPHERAL:
483 glue->phy_mode = PHY_MODE_USB_DEVICE;
484 glue->role = USB_ROLE_DEVICE;
485 break;
486 case USB_DR_MODE_OTG:
487 glue->phy_mode = PHY_MODE_USB_OTG;
488 glue->role = USB_ROLE_NONE;
489 break;
490 default:
491 dev_err(&pdev->dev, "Error 'dr_mode' property\n");
492 return -EINVAL;
495 glue->phy = devm_of_phy_get_by_index(dev, np, 0);
496 if (IS_ERR(glue->phy)) {
497 dev_err(dev, "fail to getting phy %ld\n",
498 PTR_ERR(glue->phy));
499 return PTR_ERR(glue->phy);
502 glue->usb_phy = usb_phy_generic_register();
503 if (IS_ERR(glue->usb_phy)) {
504 dev_err(dev, "fail to registering usb-phy %ld\n",
505 PTR_ERR(glue->usb_phy));
506 return PTR_ERR(glue->usb_phy);
509 glue->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
510 if (IS_ERR(glue->xceiv)) {
511 dev_err(dev, "fail to getting usb-phy %d\n", ret);
512 ret = PTR_ERR(glue->xceiv);
513 goto err_unregister_usb_phy;
516 platform_set_drvdata(pdev, glue);
517 pm_runtime_enable(dev);
518 pm_runtime_get_sync(dev);
520 ret = mtk_musb_clks_enable(glue);
521 if (ret)
522 goto err_enable_clk;
524 pinfo = mtk_dev_info;
525 pinfo.parent = dev;
526 pinfo.res = pdev->resource;
527 pinfo.num_res = pdev->num_resources;
528 pinfo.data = pdata;
529 pinfo.size_data = sizeof(*pdata);
531 glue->musb_pdev = platform_device_register_full(&pinfo);
532 if (IS_ERR(glue->musb_pdev)) {
533 ret = PTR_ERR(glue->musb_pdev);
534 dev_err(dev, "failed to register musb device: %d\n", ret);
535 goto err_device_register;
538 return 0;
540 err_device_register:
541 mtk_musb_clks_disable(glue);
542 err_enable_clk:
543 pm_runtime_put_sync(dev);
544 pm_runtime_disable(dev);
545 err_unregister_usb_phy:
546 usb_phy_generic_unregister(glue->usb_phy);
547 return ret;
550 static int mtk_musb_remove(struct platform_device *pdev)
552 struct mtk_glue *glue = platform_get_drvdata(pdev);
553 struct platform_device *usb_phy = glue->usb_phy;
555 platform_device_unregister(glue->musb_pdev);
556 usb_phy_generic_unregister(usb_phy);
558 return 0;
561 #ifdef CONFIG_OF
562 static const struct of_device_id mtk_musb_match[] = {
563 {.compatible = "mediatek,mtk-musb",},
566 MODULE_DEVICE_TABLE(of, mtk_musb_match);
567 #endif
569 static struct platform_driver mtk_musb_driver = {
570 .probe = mtk_musb_probe,
571 .remove = mtk_musb_remove,
572 .driver = {
573 .name = "musb-mtk",
574 .of_match_table = of_match_ptr(mtk_musb_match),
578 module_platform_driver(mtk_musb_driver);
580 MODULE_DESCRIPTION("MediaTek MUSB Glue Layer");
581 MODULE_AUTHOR("Min Guo <min.guo@mediatek.com>");
582 MODULE_LICENSE("GPL v2");