1 From 03e4b585ac947e2d422bedf03179bbfec3aca3cf Mon Sep 17 00:00:00 2001
2 From: Linus Walleij <linus.walleij@linaro.org>
3 Date: Mon, 14 Nov 2022 12:51:59 +0100
4 Subject: [PATCH 09/29] fotg210-udc: Support optional external PHY
6 This adds support for an optional external PHY to the FOTG210
9 Tested with the GPIO VBUS PHY driver on the Gemini SoC.
11 Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
12 Link: https://lore.kernel.org/r/20221114115201.302887-2-linus.walleij@linaro.org
13 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
15 --- a/drivers/usb/fotg210/fotg210-udc.c
16 +++ b/drivers/usb/fotg210/fotg210-udc.c
18 #include <linux/platform_device.h>
19 #include <linux/usb/ch9.h>
20 #include <linux/usb/gadget.h>
21 +#include <linux/usb/otg.h>
22 +#include <linux/usb/phy.h>
25 #include "fotg210-udc.h"
26 @@ -1022,10 +1024,18 @@ static int fotg210_udc_start(struct usb_
28 struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
32 /* hook up the driver */
33 fotg210->driver = driver;
35 + if (!IS_ERR_OR_NULL(fotg210->phy)) {
36 + ret = otg_set_peripheral(fotg210->phy->otg,
39 + dev_err(fotg210->dev, "can't bind to phy\n");
42 /* enable device global interrupt */
43 value = ioread32(fotg210->reg + FOTG210_DMCR);
44 value |= DMCR_GLINT_EN;
45 @@ -1067,6 +1077,9 @@ static int fotg210_udc_stop(struct usb_g
46 struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
49 + if (!IS_ERR_OR_NULL(fotg210->phy))
50 + return otg_set_peripheral(fotg210->phy->otg, NULL);
52 spin_lock_irqsave(&fotg210->lock, flags);
54 fotg210_init(fotg210);
55 @@ -1082,12 +1095,50 @@ static const struct usb_gadget_ops fotg2
56 .udc_stop = fotg210_udc_stop,
60 + * fotg210_phy_event - Called by phy upon VBus event
61 + * @nb: notifier block
62 + * @action: phy action, is vbus connect or disconnect
63 + * @data: the usb_gadget structure in fotg210
65 + * Called by the USB Phy when a cable connect or disconnect is sensed.
67 + * Returns NOTIFY_OK or NOTIFY_DONE
69 +static int fotg210_phy_event(struct notifier_block *nb, unsigned long action,
72 + struct usb_gadget *gadget = data;
78 + case USB_EVENT_VBUS:
79 + usb_gadget_vbus_connect(gadget);
81 + case USB_EVENT_NONE:
82 + usb_gadget_vbus_disconnect(gadget);
89 +static struct notifier_block fotg210_phy_notifier = {
90 + .notifier_call = fotg210_phy_event,
93 int fotg210_udc_remove(struct platform_device *pdev)
95 struct fotg210_udc *fotg210 = platform_get_drvdata(pdev);
98 usb_del_gadget_udc(&fotg210->gadget);
99 + if (!IS_ERR_OR_NULL(fotg210->phy)) {
100 + usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier);
101 + usb_put_phy(fotg210->phy);
103 iounmap(fotg210->reg);
104 free_irq(platform_get_irq(pdev, 0), fotg210);
106 @@ -1127,6 +1178,22 @@ int fotg210_udc_probe(struct platform_de
110 + fotg210->dev = dev;
112 + fotg210->phy = devm_usb_get_phy_by_phandle(dev->parent, "usb-phy", 0);
113 + if (IS_ERR(fotg210->phy)) {
114 + ret = PTR_ERR(fotg210->phy);
115 + if (ret == -EPROBE_DEFER)
117 + dev_info(dev, "no PHY found\n");
118 + fotg210->phy = NULL;
120 + ret = usb_phy_init(fotg210->phy);
123 + dev_info(dev, "found and initialized PHY\n");
126 for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
127 _ep[i] = kzalloc(sizeof(struct fotg210_ep), GFP_KERNEL);
129 @@ -1200,6 +1267,9 @@ int fotg210_udc_probe(struct platform_de
133 + if (!IS_ERR_OR_NULL(fotg210->phy))
134 + usb_register_notifier(fotg210->phy, &fotg210_phy_notifier);
136 ret = usb_add_gadget_udc(dev, &fotg210->gadget);
139 @@ -1209,6 +1279,8 @@ int fotg210_udc_probe(struct platform_de
143 + if (!IS_ERR_OR_NULL(fotg210->phy))
144 + usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier);
145 free_irq(ires->start, fotg210);
148 --- a/drivers/usb/fotg210/fotg210-udc.h
149 +++ b/drivers/usb/fotg210/fotg210-udc.h
150 @@ -234,6 +234,8 @@ struct fotg210_udc {
152 unsigned long irq_trigger;
154 + struct device *dev;
155 + struct usb_phy *phy;
156 struct usb_gadget gadget;
157 struct usb_gadget_driver *driver;