Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / usb / fotg210 / fotg210-core.c
blob49f25a70b32ebdd8bdad672d31342d5f89c4a502
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Central probing code for the FOTG210 dual role driver
4 * We register one driver for the hardware and then we decide
5 * whether to proceed with probing the host or the peripheral
6 * driver.
7 */
8 #include <linux/bitops.h>
9 #include <linux/clk.h>
10 #include <linux/device.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/platform_device.h>
15 #include <linux/regmap.h>
16 #include <linux/usb.h>
17 #include <linux/usb/otg.h>
19 #include "fotg210.h"
21 /* Role Register 0x80 */
22 #define FOTG210_RR 0x80
23 #define FOTG210_RR_ID BIT(21) /* 1 = B-device, 0 = A-device */
24 #define FOTG210_RR_CROLE BIT(20) /* 1 = device, 0 = host */
27 * Gemini-specific initialization function, only executed on the
28 * Gemini SoC using the global misc control register.
30 * The gemini USB blocks are connected to either Mini-A (host mode) or
31 * Mini-B (peripheral mode) plugs. There is no role switch support on the
32 * Gemini SoC, just either-or.
34 #define GEMINI_GLOBAL_MISC_CTRL 0x30
35 #define GEMINI_MISC_USB0_WAKEUP BIT(14)
36 #define GEMINI_MISC_USB1_WAKEUP BIT(15)
37 #define GEMINI_MISC_USB0_VBUS_ON BIT(22)
38 #define GEMINI_MISC_USB1_VBUS_ON BIT(23)
39 #define GEMINI_MISC_USB0_MINI_B BIT(29)
40 #define GEMINI_MISC_USB1_MINI_B BIT(30)
42 static int fotg210_gemini_init(struct fotg210 *fotg, struct resource *res,
43 enum usb_dr_mode mode)
45 struct device *dev = fotg->dev;
46 struct device_node *np = dev->of_node;
47 struct regmap *map;
48 bool wakeup;
49 u32 mask, val;
50 int ret;
52 map = syscon_regmap_lookup_by_phandle(np, "syscon");
53 if (IS_ERR(map))
54 return dev_err_probe(dev, PTR_ERR(map), "no syscon\n");
55 fotg->map = map;
56 wakeup = of_property_read_bool(np, "wakeup-source");
59 * Figure out if this is USB0 or USB1 by simply checking the
60 * physical base address.
62 mask = 0;
63 if (res->start == 0x69000000) {
64 fotg->port = GEMINI_PORT_1;
65 mask = GEMINI_MISC_USB1_VBUS_ON | GEMINI_MISC_USB1_MINI_B |
66 GEMINI_MISC_USB1_WAKEUP;
67 if (mode == USB_DR_MODE_HOST)
68 val = GEMINI_MISC_USB1_VBUS_ON;
69 else
70 val = GEMINI_MISC_USB1_MINI_B;
71 if (wakeup)
72 val |= GEMINI_MISC_USB1_WAKEUP;
73 } else {
74 fotg->port = GEMINI_PORT_0;
75 mask = GEMINI_MISC_USB0_VBUS_ON | GEMINI_MISC_USB0_MINI_B |
76 GEMINI_MISC_USB0_WAKEUP;
77 if (mode == USB_DR_MODE_HOST)
78 val = GEMINI_MISC_USB0_VBUS_ON;
79 else
80 val = GEMINI_MISC_USB0_MINI_B;
81 if (wakeup)
82 val |= GEMINI_MISC_USB0_WAKEUP;
85 ret = regmap_update_bits(map, GEMINI_GLOBAL_MISC_CTRL, mask, val);
86 if (ret) {
87 dev_err(dev, "failed to initialize Gemini PHY\n");
88 return ret;
91 dev_info(dev, "initialized Gemini PHY in %s mode\n",
92 (mode == USB_DR_MODE_HOST) ? "host" : "gadget");
93 return 0;
96 /**
97 * fotg210_vbus() - Called by gadget driver to enable/disable VBUS
98 * @fotg: pointer to a private fotg210 object
99 * @enable: true to enable VBUS, false to disable VBUS
101 void fotg210_vbus(struct fotg210 *fotg, bool enable)
103 u32 mask;
104 u32 val;
105 int ret;
107 switch (fotg->port) {
108 case GEMINI_PORT_0:
109 mask = GEMINI_MISC_USB0_VBUS_ON;
110 val = enable ? GEMINI_MISC_USB0_VBUS_ON : 0;
111 break;
112 case GEMINI_PORT_1:
113 mask = GEMINI_MISC_USB1_VBUS_ON;
114 val = enable ? GEMINI_MISC_USB1_VBUS_ON : 0;
115 break;
116 default:
117 return;
119 ret = regmap_update_bits(fotg->map, GEMINI_GLOBAL_MISC_CTRL, mask, val);
120 if (ret)
121 dev_err(fotg->dev, "failed to %s VBUS\n",
122 enable ? "enable" : "disable");
123 dev_info(fotg->dev, "%s: %s VBUS\n", __func__, enable ? "enable" : "disable");
126 static int fotg210_probe(struct platform_device *pdev)
128 struct device *dev = &pdev->dev;
129 enum usb_dr_mode mode;
130 struct fotg210 *fotg;
131 u32 val;
132 int ret;
134 fotg = devm_kzalloc(dev, sizeof(*fotg), GFP_KERNEL);
135 if (!fotg)
136 return -ENOMEM;
137 fotg->dev = dev;
139 fotg->base = devm_platform_get_and_ioremap_resource(pdev, 0, &fotg->res);
140 if (IS_ERR(fotg->base))
141 return PTR_ERR(fotg->base);
143 fotg->pclk = devm_clk_get_optional_enabled(dev, "PCLK");
144 if (IS_ERR(fotg->pclk))
145 return PTR_ERR(fotg->pclk);
147 mode = usb_get_dr_mode(dev);
149 if (of_device_is_compatible(dev->of_node, "cortina,gemini-usb")) {
150 ret = fotg210_gemini_init(fotg, fotg->res, mode);
151 if (ret)
152 return ret;
155 val = readl(fotg->base + FOTG210_RR);
156 if (mode == USB_DR_MODE_PERIPHERAL) {
157 if (!(val & FOTG210_RR_CROLE))
158 dev_err(dev, "block not in device role\n");
159 ret = fotg210_udc_probe(pdev, fotg);
160 } else {
161 if (val & FOTG210_RR_CROLE)
162 dev_err(dev, "block not in host role\n");
163 ret = fotg210_hcd_probe(pdev, fotg);
166 return ret;
169 static void fotg210_remove(struct platform_device *pdev)
171 struct device *dev = &pdev->dev;
172 enum usb_dr_mode mode;
174 mode = usb_get_dr_mode(dev);
176 if (mode == USB_DR_MODE_PERIPHERAL)
177 fotg210_udc_remove(pdev);
178 else
179 fotg210_hcd_remove(pdev);
182 #ifdef CONFIG_OF
183 static const struct of_device_id fotg210_of_match[] = {
184 { .compatible = "faraday,fotg200" },
185 { .compatible = "faraday,fotg210" },
186 /* TODO: can we also handle FUSB220? */
189 MODULE_DEVICE_TABLE(of, fotg210_of_match);
190 #endif
192 static struct platform_driver fotg210_driver = {
193 .driver = {
194 .name = "fotg210",
195 .of_match_table = of_match_ptr(fotg210_of_match),
197 .probe = fotg210_probe,
198 .remove = fotg210_remove,
201 static int __init fotg210_init(void)
203 if (IS_ENABLED(CONFIG_USB_FOTG210_HCD) && !usb_disabled())
204 fotg210_hcd_init();
205 return platform_driver_register(&fotg210_driver);
207 module_init(fotg210_init);
209 static void __exit fotg210_cleanup(void)
211 platform_driver_unregister(&fotg210_driver);
212 if (IS_ENABLED(CONFIG_USB_FOTG210_HCD))
213 fotg210_hcd_cleanup();
215 module_exit(fotg210_cleanup);
217 MODULE_AUTHOR("Yuan-Hsin Chen, Feng-Hsin Chiang");
218 MODULE_LICENSE("GPL");
219 MODULE_DESCRIPTION("FOTG210 Dual Role Controller Driver");