| 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/string_choices.h> |
| 17 |
#include <linux/usb.h> |
| 18 |
#include <linux/usb/otg.h> |
| 19 |
|
| 20 |
#include "fotg210.h" |
| 21 |
|
| 22 |
/* Role Register 0x80 */ |
| 23 |
#define FOTG210_RR 0x80 |
| 24 |
#define FOTG210_RR_ID BIT(21) /* 1 = B-device, 0 = A-device */ |
| 25 |
#define FOTG210_RR_CROLE BIT(20) /* 1 = device, 0 = host */ |
| 26 |
|
| 27 |
/* |
| 28 |
* Gemini-specific initialization function, only executed on the |
| 29 |
* Gemini SoC using the global misc control register. |
| 30 |
* |
| 31 |
* The gemini USB blocks are connected to either Mini-A (host mode) or |
| 32 |
* Mini-B (peripheral mode) plugs. There is no role switch support on the |
| 33 |
* Gemini SoC, just either-or. |
| 34 |
*/ |
| 35 |
#define GEMINI_GLOBAL_MISC_CTRL 0x30 |
| 36 |
#define GEMINI_MISC_USB0_WAKEUP BIT(14) |
| 37 |
#define GEMINI_MISC_USB1_WAKEUP BIT(15) |
| 38 |
#define GEMINI_MISC_USB0_VBUS_ON BIT(22) |
| 39 |
#define GEMINI_MISC_USB1_VBUS_ON BIT(23) |
| 40 |
#define GEMINI_MISC_USB0_MINI_B BIT(29) |
| 41 |
#define GEMINI_MISC_USB1_MINI_B BIT(30) |
| 42 |
|
| 43 |
static int fotg210_gemini_init(struct fotg210 *fotg, struct resource *res, |
| 44 |
enum usb_dr_mode mode) |
| 45 |
{ |
| 46 |
struct device *dev = fotg->dev; |
| 47 |
struct device_node *np = dev->of_node; |
| 48 |
struct regmap *map; |
| 49 |
bool wakeup; |
| 50 |
u32 mask, val; |
| 51 |
int ret; |
| 52 |
|
| 53 |
map = syscon_regmap_lookup_by_phandle(np, "syscon"); |
| 54 |
if (IS_ERR(map)) |
| 55 |
return dev_err_probe(dev, PTR_ERR(map), "no syscon\n"); |
| 56 |
fotg->map = map; |
| 57 |
wakeup = of_property_read_bool(np, "wakeup-source"); |
| 58 |
|
| 59 |
/* |
| 60 |
* Figure out if this is USB0 or USB1 by simply checking the |
| 61 |
* physical base address. |
| 62 |
*/ |
| 63 |
mask = 0; |
| 64 |
if (res->start == 0x69000000) { |
| 65 |
fotg->port = GEMINI_PORT_1; |
| 66 |
mask = GEMINI_MISC_USB1_VBUS_ON | GEMINI_MISC_USB1_MINI_B | |
| 67 |
GEMINI_MISC_USB1_WAKEUP; |
| 68 |
if (mode == USB_DR_MODE_HOST) |
| 69 |
val = GEMINI_MISC_USB1_VBUS_ON; |
| 70 |
else |
| 71 |
val = GEMINI_MISC_USB1_MINI_B; |
| 72 |
if (wakeup) |
| 73 |
val |= GEMINI_MISC_USB1_WAKEUP; |
| 74 |
} else { |
| 75 |
fotg->port = GEMINI_PORT_0; |
| 76 |
mask = GEMINI_MISC_USB0_VBUS_ON | GEMINI_MISC_USB0_MINI_B | |
| 77 |
GEMINI_MISC_USB0_WAKEUP; |
| 78 |
if (mode == USB_DR_MODE_HOST) |
| 79 |
val = GEMINI_MISC_USB0_VBUS_ON; |
| 80 |
else |
| 81 |
val = GEMINI_MISC_USB0_MINI_B; |
| 82 |
if (wakeup) |
| 83 |
val |= GEMINI_MISC_USB0_WAKEUP; |
| 84 |
} |
| 85 |
|
| 86 |
ret = regmap_update_bits(map, GEMINI_GLOBAL_MISC_CTRL, mask, val); |
| 87 |
if (ret) { |
| 88 |
dev_err(dev, "failed to initialize Gemini PHY\n"); |
| 89 |
return ret; |
| 90 |
} |
| 91 |
|
| 92 |
dev_info(dev, "initialized Gemini PHY in %s mode\n", |
| 93 |
(mode == USB_DR_MODE_HOST) ? "host" : "gadget"); |
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* fotg210_vbus() - Called by gadget driver to enable/disable VBUS |
| 99 |
* @fotg: pointer to a private fotg210 object |
| 100 |
* @enable: true to enable VBUS, false to disable VBUS |
| 101 |
*/ |
| 102 |
void fotg210_vbus(struct fotg210 *fotg, bool enable) |
| 103 |
{ |
| 104 |
u32 mask; |
| 105 |
u32 val; |
| 106 |
int ret; |
| 107 |
|
| 108 |
switch (fotg->port) { |
| 109 |
case GEMINI_PORT_0: |
| 110 |
mask = GEMINI_MISC_USB0_VBUS_ON; |
| 111 |
val = enable ? GEMINI_MISC_USB0_VBUS_ON : 0; |
| 112 |
break; |
| 113 |
case GEMINI_PORT_1: |
| 114 |
mask = GEMINI_MISC_USB1_VBUS_ON; |
| 115 |
val = enable ? GEMINI_MISC_USB1_VBUS_ON : 0; |
| 116 |
break; |
| 117 |
default: |
| 118 |
return; |
| 119 |
} |
| 120 |
ret = regmap_update_bits(fotg->map, GEMINI_GLOBAL_MISC_CTRL, mask, val); |
| 121 |
if (ret) |
| 122 |
dev_err(fotg->dev, "failed to %s VBUS\n", |
| 123 |
str_enable_disable(enable)); |
| 124 |
dev_info(fotg->dev, "%s: %s VBUS\n", __func__, str_enable_disable(enable)); |
| 125 |
} |
| 126 |
|
| 127 |
static int fotg210_probe(struct platform_device *pdev) |
| 128 |
{ |
| 129 |
struct device *dev = &pdev->dev; |
| 130 |
enum usb_dr_mode mode; |
| 131 |
struct fotg210 *fotg; |
| 132 |
u32 val; |
| 133 |
int ret; |
| 134 |
|
| 135 |
fotg = devm_kzalloc(dev, sizeof(*fotg), GFP_KERNEL); |
| 136 |
if (!fotg) |
| 137 |
return -ENOMEM; |
| 138 |
fotg->dev = dev; |
| 139 |
|
| 140 |
fotg->base = devm_platform_get_and_ioremap_resource(pdev, 0, &fotg->res); |
| 141 |
if (IS_ERR(fotg->base)) |
| 142 |
return PTR_ERR(fotg->base); |
| 143 |
|
| 144 |
fotg->pclk = devm_clk_get_optional_enabled(dev, "PCLK"); |
| 145 |
if (IS_ERR(fotg->pclk)) |
| 146 |
return PTR_ERR(fotg->pclk); |
| 147 |
|
| 148 |
mode = usb_get_dr_mode(dev); |
| 149 |
|
| 150 |
if (of_device_is_compatible(dev->of_node, "cortina,gemini-usb")) { |
| 151 |
ret = fotg210_gemini_init(fotg, fotg->res, mode); |
| 152 |
if (ret) |
| 153 |
return ret; |
| 154 |
} |
| 155 |
|
| 156 |
val = readl(fotg->base + FOTG210_RR); |
| 157 |
if (mode == USB_DR_MODE_PERIPHERAL) { |
| 158 |
if (!(val & FOTG210_RR_CROLE)) |
| 159 |
dev_err(dev, "block not in device role\n"); |
| 160 |
ret = fotg210_udc_probe(pdev, fotg); |
| 161 |
} else { |
| 162 |
if (val & FOTG210_RR_CROLE) |
| 163 |
dev_err(dev, "block not in host role\n"); |
| 164 |
ret = fotg210_hcd_probe(pdev, fotg); |
| 165 |
} |
| 166 |
|
| 167 |
return ret; |
| 168 |
} |
| 169 |
|
| 170 |
static void fotg210_remove(struct platform_device *pdev) |
| 171 |
{ |
| 172 |
struct device *dev = &pdev->dev; |
| 173 |
enum usb_dr_mode mode; |
| 174 |
|
| 175 |
mode = usb_get_dr_mode(dev); |
| 176 |
|
| 177 |
if (mode == USB_DR_MODE_PERIPHERAL) |
| 178 |
fotg210_udc_remove(pdev); |
| 179 |
else |
| 180 |
fotg210_hcd_remove(pdev); |
| 181 |
} |
| 182 |
|
| 183 |
#ifdef CONFIG_OF |
| 184 |
static const struct of_device_id fotg210_of_match[] = { |
| 185 |
{ .compatible = "faraday,fotg200" }, |
| 186 |
{ .compatible = "faraday,fotg210" }, |
| 187 |
/* TODO: can we also handle FUSB220? */ |
| 188 |
{}, |
| 189 |
}; |
| 190 |
MODULE_DEVICE_TABLE(of, fotg210_of_match); |
| 191 |
#endif |
| 192 |
|
| 193 |
static struct platform_driver fotg210_driver = { |
| 194 |
.driver = { |
| 195 |
.name = "fotg210", |
| 196 |
.of_match_table = of_match_ptr(fotg210_of_match), |
| 197 |
}, |
| 198 |
.probe = fotg210_probe, |
| 199 |
.remove = fotg210_remove, |
| 200 |
}; |
| 201 |
|
| 202 |
static int __init fotg210_init(void) |
| 203 |
{ |
| 204 |
if (IS_ENABLED(CONFIG_USB_FOTG210_HCD) && !usb_disabled()) |
| 205 |
fotg210_hcd_init(); |
| 206 |
return platform_driver_register(&fotg210_driver); |
| 207 |
} |
| 208 |
module_init(fotg210_init); |
| 209 |
|
| 210 |
static void __exit fotg210_cleanup(void) |
| 211 |
{ |
| 212 |
platform_driver_unregister(&fotg210_driver); |
| 213 |
if (IS_ENABLED(CONFIG_USB_FOTG210_HCD)) |
| 214 |
fotg210_hcd_cleanup(); |
| 215 |
} |
| 216 |
module_exit(fotg210_cleanup); |
| 217 |
|
| 218 |
MODULE_AUTHOR("Yuan-Hsin Chen, Feng-Hsin Chiang"); |
| 219 |
MODULE_LICENSE("GPL"); |
| 220 |
MODULE_DESCRIPTION("FOTG210 Dual Role Controller Driver"); |