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/device.h> 10 #include <linux/mfd/syscon.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/platform_device.h> 14 #include <linux/regmap.h> 15 #include <linux/usb.h> 16 #include <linux/usb/otg.h> 17 18 #include "fotg210.h" 19 20 /* 21 * Gemini-specific initialization function, only executed on the 22 * Gemini SoC using the global misc control register. 23 * 24 * The gemini USB blocks are connected to either Mini-A (host mode) or 25 * Mini-B (peripheral mode) plugs. There is no role switch support on the 26 * Gemini SoC, just either-or. 27 */ 28 #define GEMINI_GLOBAL_MISC_CTRL 0x30 29 #define GEMINI_MISC_USB0_WAKEUP BIT(14) 30 #define GEMINI_MISC_USB1_WAKEUP BIT(15) 31 #define GEMINI_MISC_USB0_VBUS_ON BIT(22) 32 #define GEMINI_MISC_USB1_VBUS_ON BIT(23) 33 #define GEMINI_MISC_USB0_MINI_B BIT(29) 34 #define GEMINI_MISC_USB1_MINI_B BIT(30) 35 36 static int fotg210_gemini_init(struct device *dev, struct resource *res, 37 enum usb_dr_mode mode) 38 { 39 struct device_node *np = dev->of_node; 40 struct regmap *map; 41 bool wakeup; 42 u32 mask, val; 43 int ret; 44 45 map = syscon_regmap_lookup_by_phandle(np, "syscon"); 46 if (IS_ERR(map)) { 47 dev_err(dev, "no syscon\n"); 48 return PTR_ERR(map); 49 } 50 wakeup = of_property_read_bool(np, "wakeup-source"); 51 52 /* 53 * Figure out if this is USB0 or USB1 by simply checking the 54 * physical base address. 55 */ 56 mask = 0; 57 if (res->start == 0x69000000) { 58 mask = GEMINI_MISC_USB1_VBUS_ON | GEMINI_MISC_USB1_MINI_B | 59 GEMINI_MISC_USB1_WAKEUP; 60 if (mode == USB_DR_MODE_HOST) 61 val = GEMINI_MISC_USB1_VBUS_ON; 62 else 63 val = GEMINI_MISC_USB1_MINI_B; 64 if (wakeup) 65 val |= GEMINI_MISC_USB1_WAKEUP; 66 } else { 67 mask = GEMINI_MISC_USB0_VBUS_ON | GEMINI_MISC_USB0_MINI_B | 68 GEMINI_MISC_USB0_WAKEUP; 69 if (mode == USB_DR_MODE_HOST) 70 val = GEMINI_MISC_USB0_VBUS_ON; 71 else 72 val = GEMINI_MISC_USB0_MINI_B; 73 if (wakeup) 74 val |= GEMINI_MISC_USB0_WAKEUP; 75 } 76 77 ret = regmap_update_bits(map, GEMINI_GLOBAL_MISC_CTRL, mask, val); 78 if (ret) { 79 dev_err(dev, "failed to initialize Gemini PHY\n"); 80 return ret; 81 } 82 83 dev_info(dev, "initialized Gemini PHY in %s mode\n", 84 (mode == USB_DR_MODE_HOST) ? "host" : "gadget"); 85 return 0; 86 } 87 88 static int fotg210_probe(struct platform_device *pdev) 89 { 90 struct device *dev = &pdev->dev; 91 enum usb_dr_mode mode; 92 int ret; 93 94 mode = usb_get_dr_mode(dev); 95 96 if (of_device_is_compatible(dev->of_node, "cortina,gemini-usb")) { 97 struct resource *res; 98 99 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 100 ret = fotg210_gemini_init(dev, res, mode); 101 if (ret) 102 return ret; 103 } 104 105 if (mode == USB_DR_MODE_PERIPHERAL) 106 ret = fotg210_udc_probe(pdev); 107 else 108 ret = fotg210_hcd_probe(pdev); 109 110 return ret; 111 } 112 113 static int fotg210_remove(struct platform_device *pdev) 114 { 115 struct device *dev = &pdev->dev; 116 enum usb_dr_mode mode; 117 118 mode = usb_get_dr_mode(dev); 119 120 if (mode == USB_DR_MODE_PERIPHERAL) 121 fotg210_udc_remove(pdev); 122 else 123 fotg210_hcd_remove(pdev); 124 125 return 0; 126 } 127 128 #ifdef CONFIG_OF 129 static const struct of_device_id fotg210_of_match[] = { 130 { .compatible = "faraday,fotg210" }, 131 {}, 132 }; 133 MODULE_DEVICE_TABLE(of, fotg210_of_match); 134 #endif 135 136 static struct platform_driver fotg210_driver = { 137 .driver = { 138 .name = "fotg210", 139 .of_match_table = of_match_ptr(fotg210_of_match), 140 }, 141 .probe = fotg210_probe, 142 .remove = fotg210_remove, 143 }; 144 145 static int __init fotg210_init(void) 146 { 147 if (usb_disabled()) 148 return -ENODEV; 149 150 if (IS_ENABLED(CONFIG_USB_FOTG210_HCD)) 151 fotg210_hcd_init(); 152 return platform_driver_register(&fotg210_driver); 153 } 154 module_init(fotg210_init); 155 156 static void __exit fotg210_cleanup(void) 157 { 158 platform_driver_unregister(&fotg210_driver); 159 if (IS_ENABLED(CONFIG_USB_FOTG210_HCD)) 160 fotg210_hcd_cleanup(); 161 } 162 module_exit(fotg210_cleanup); 163 164 MODULE_AUTHOR("Yuan-Hsin Chen, Feng-Hsin Chiang"); 165 MODULE_LICENSE("GPL"); 166 MODULE_DESCRIPTION("FOTG210 Dual Role Controller Driver"); 167