1 // SPDX-License-Identifier: GPL-2.0-only
3 * Samsung SoC USB 1.1/2.0 PHY driver
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * Author: Kamil Debski <k.debski@samsung.com>
10 #include <linux/mfd/syscon.h>
11 #include <linux/module.h>
13 #include <linux/of_address.h>
14 #include <linux/of_device.h>
15 #include <linux/phy/phy.h>
16 #include <linux/platform_device.h>
17 #include <linux/spinlock.h>
18 #include "phy-samsung-usb2.h"
20 static int samsung_usb2_phy_power_on(struct phy *phy)
22 struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
23 struct samsung_usb2_phy_driver *drv = inst->drv;
26 dev_dbg(drv->dev, "Request to power_on \"%s\" usb phy\n",
30 ret = regulator_enable(drv->vbus);
35 ret = clk_prepare_enable(drv->clk);
38 ret = clk_prepare_enable(drv->ref_clk);
40 goto err_instance_clk;
41 if (inst->cfg->power_on) {
42 spin_lock(&drv->lock);
43 ret = inst->cfg->power_on(inst);
44 spin_unlock(&drv->lock);
52 clk_disable_unprepare(drv->ref_clk);
54 clk_disable_unprepare(drv->clk);
57 regulator_disable(drv->vbus);
62 static int samsung_usb2_phy_power_off(struct phy *phy)
64 struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
65 struct samsung_usb2_phy_driver *drv = inst->drv;
68 dev_dbg(drv->dev, "Request to power_off \"%s\" usb phy\n",
70 if (inst->cfg->power_off) {
71 spin_lock(&drv->lock);
72 ret = inst->cfg->power_off(inst);
73 spin_unlock(&drv->lock);
77 clk_disable_unprepare(drv->ref_clk);
78 clk_disable_unprepare(drv->clk);
80 ret = regulator_disable(drv->vbus);
85 static const struct phy_ops samsung_usb2_phy_ops = {
86 .power_on = samsung_usb2_phy_power_on,
87 .power_off = samsung_usb2_phy_power_off,
91 static struct phy *samsung_usb2_phy_xlate(struct device *dev,
92 struct of_phandle_args *args)
94 struct samsung_usb2_phy_driver *drv;
96 drv = dev_get_drvdata(dev);
98 return ERR_PTR(-EINVAL);
100 if (WARN_ON(args->args[0] >= drv->cfg->num_phys))
101 return ERR_PTR(-ENODEV);
103 return drv->instances[args->args[0]].phy;
106 static const struct of_device_id samsung_usb2_phy_of_match[] = {
107 #ifdef CONFIG_PHY_EXYNOS4X12_USB2
109 .compatible = "samsung,exynos3250-usb2-phy",
110 .data = &exynos3250_usb2_phy_config,
113 #ifdef CONFIG_PHY_EXYNOS4210_USB2
115 .compatible = "samsung,exynos4210-usb2-phy",
116 .data = &exynos4210_usb2_phy_config,
119 #ifdef CONFIG_PHY_EXYNOS4X12_USB2
121 .compatible = "samsung,exynos4x12-usb2-phy",
122 .data = &exynos4x12_usb2_phy_config,
125 #ifdef CONFIG_PHY_EXYNOS5250_USB2
127 .compatible = "samsung,exynos5250-usb2-phy",
128 .data = &exynos5250_usb2_phy_config,
131 #ifdef CONFIG_PHY_EXYNOS5420_USB2
133 .compatible = "samsung,exynos5420-usb2-phy",
134 .data = &exynos5420_usb2_phy_config,
137 #ifdef CONFIG_PHY_S5PV210_USB2
139 .compatible = "samsung,s5pv210-usb2-phy",
140 .data = &s5pv210_usb2_phy_config,
145 MODULE_DEVICE_TABLE(of, samsung_usb2_phy_of_match);
147 static int samsung_usb2_phy_probe(struct platform_device *pdev)
149 const struct samsung_usb2_phy_config *cfg;
150 struct device *dev = &pdev->dev;
151 struct phy_provider *phy_provider;
152 struct samsung_usb2_phy_driver *drv;
155 if (!pdev->dev.of_node) {
156 dev_err(dev, "This driver is required to be instantiated from device tree\n");
160 cfg = of_device_get_match_data(dev);
164 drv = devm_kzalloc(dev, struct_size(drv, instances, cfg->num_phys),
169 dev_set_drvdata(dev, drv);
170 spin_lock_init(&drv->lock);
175 drv->reg_phy = devm_platform_ioremap_resource(pdev, 0);
176 if (IS_ERR(drv->reg_phy)) {
177 dev_err(dev, "Failed to map register memory (phy)\n");
178 return PTR_ERR(drv->reg_phy);
181 drv->reg_pmu = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
182 "samsung,pmureg-phandle");
183 if (IS_ERR(drv->reg_pmu)) {
184 dev_err(dev, "Failed to map PMU registers (via syscon)\n");
185 return PTR_ERR(drv->reg_pmu);
188 if (drv->cfg->has_mode_switch) {
189 drv->reg_sys = syscon_regmap_lookup_by_phandle(
190 pdev->dev.of_node, "samsung,sysreg-phandle");
191 if (IS_ERR(drv->reg_sys)) {
192 dev_err(dev, "Failed to map system registers (via syscon)\n");
193 return PTR_ERR(drv->reg_sys);
197 drv->clk = devm_clk_get(dev, "phy");
198 if (IS_ERR(drv->clk)) {
199 dev_err(dev, "Failed to get clock of phy controller\n");
200 return PTR_ERR(drv->clk);
203 drv->ref_clk = devm_clk_get(dev, "ref");
204 if (IS_ERR(drv->ref_clk)) {
205 dev_err(dev, "Failed to get reference clock for the phy controller\n");
206 return PTR_ERR(drv->ref_clk);
209 drv->ref_rate = clk_get_rate(drv->ref_clk);
210 if (drv->cfg->rate_to_clk) {
211 ret = drv->cfg->rate_to_clk(drv->ref_rate, &drv->ref_reg_val);
216 drv->vbus = devm_regulator_get(dev, "vbus");
217 if (IS_ERR(drv->vbus)) {
218 ret = PTR_ERR(drv->vbus);
219 if (ret == -EPROBE_DEFER)
224 for (i = 0; i < drv->cfg->num_phys; i++) {
225 char *label = drv->cfg->phys[i].label;
226 struct samsung_usb2_phy_instance *p = &drv->instances[i];
228 dev_dbg(dev, "Creating phy \"%s\"\n", label);
229 p->phy = devm_phy_create(dev, NULL, &samsung_usb2_phy_ops);
230 if (IS_ERR(p->phy)) {
231 dev_err(drv->dev, "Failed to create usb2_phy \"%s\"\n",
233 return PTR_ERR(p->phy);
236 p->cfg = &drv->cfg->phys[i];
238 phy_set_bus_width(p->phy, 8);
239 phy_set_drvdata(p->phy, p);
242 phy_provider = devm_of_phy_provider_register(dev,
243 samsung_usb2_phy_xlate);
244 if (IS_ERR(phy_provider)) {
245 dev_err(drv->dev, "Failed to register phy provider\n");
246 return PTR_ERR(phy_provider);
252 static struct platform_driver samsung_usb2_phy_driver = {
253 .probe = samsung_usb2_phy_probe,
255 .of_match_table = samsung_usb2_phy_of_match,
256 .name = "samsung-usb2-phy",
257 .suppress_bind_attrs = true,
261 module_platform_driver(samsung_usb2_phy_driver);
262 MODULE_DESCRIPTION("Samsung S5P/Exynos SoC USB PHY driver");
263 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
264 MODULE_LICENSE("GPL v2");
265 MODULE_ALIAS("platform:samsung-usb2-phy");