Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Samsung SoC USB 1.1/2.0 PHY driver
4 *
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * Author: Kamil Debski <k.debski@samsung.com>
7 */
8
9#include <linux/clk.h>
10#include <linux/mfd/syscon.h>
11#include <linux/module.h>
12#include <linux/of.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"
19
20static int samsung_usb2_phy_power_on(struct phy *phy)
21{
22 struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
23 struct samsung_usb2_phy_driver *drv = inst->drv;
24 int ret;
25
26 dev_dbg(drv->dev, "Request to power_on \"%s\" usb phy\n",
27 inst->cfg->label);
28
29 if (drv->vbus) {
30 ret = regulator_enable(drv->vbus);
31 if (ret)
32 goto err_regulator;
33 }
34
35 ret = clk_prepare_enable(drv->clk);
36 if (ret)
37 goto err_main_clk;
38 ret = clk_prepare_enable(drv->ref_clk);
39 if (ret)
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);
45 if (ret)
46 goto err_power_on;
47 }
48
49 return 0;
50
51err_power_on:
52 clk_disable_unprepare(drv->ref_clk);
53err_instance_clk:
54 clk_disable_unprepare(drv->clk);
55err_main_clk:
56 if (drv->vbus)
57 regulator_disable(drv->vbus);
58err_regulator:
59 return ret;
60}
61
62static int samsung_usb2_phy_power_off(struct phy *phy)
63{
64 struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
65 struct samsung_usb2_phy_driver *drv = inst->drv;
66 int ret = 0;
67
68 dev_dbg(drv->dev, "Request to power_off \"%s\" usb phy\n",
69 inst->cfg->label);
70 if (inst->cfg->power_off) {
71 spin_lock(&drv->lock);
72 ret = inst->cfg->power_off(inst);
73 spin_unlock(&drv->lock);
74 if (ret)
75 return ret;
76 }
77 clk_disable_unprepare(drv->ref_clk);
78 clk_disable_unprepare(drv->clk);
79 if (drv->vbus)
80 ret = regulator_disable(drv->vbus);
81
82 return ret;
83}
84
85static const struct phy_ops samsung_usb2_phy_ops = {
86 .power_on = samsung_usb2_phy_power_on,
87 .power_off = samsung_usb2_phy_power_off,
88 .owner = THIS_MODULE,
89};
90
91static struct phy *samsung_usb2_phy_xlate(struct device *dev,
92 struct of_phandle_args *args)
93{
94 struct samsung_usb2_phy_driver *drv;
95
96 drv = dev_get_drvdata(dev);
97 if (!drv)
98 return ERR_PTR(-EINVAL);
99
100 if (WARN_ON(args->args[0] >= drv->cfg->num_phys))
101 return ERR_PTR(-ENODEV);
102
103 return drv->instances[args->args[0]].phy;
104}
105
106static const struct of_device_id samsung_usb2_phy_of_match[] = {
107#ifdef CONFIG_PHY_EXYNOS4X12_USB2
108 {
109 .compatible = "samsung,exynos3250-usb2-phy",
110 .data = &exynos3250_usb2_phy_config,
111 },
112#endif
113#ifdef CONFIG_PHY_EXYNOS4210_USB2
114 {
115 .compatible = "samsung,exynos4210-usb2-phy",
116 .data = &exynos4210_usb2_phy_config,
117 },
118#endif
119#ifdef CONFIG_PHY_EXYNOS4X12_USB2
120 {
121 .compatible = "samsung,exynos4x12-usb2-phy",
122 .data = &exynos4x12_usb2_phy_config,
123 },
124#endif
125#ifdef CONFIG_PHY_EXYNOS5250_USB2
126 {
127 .compatible = "samsung,exynos5250-usb2-phy",
128 .data = &exynos5250_usb2_phy_config,
129 },
130 {
131 .compatible = "samsung,exynos5420-usb2-phy",
132 .data = &exynos5420_usb2_phy_config,
133 },
134#endif
135#ifdef CONFIG_PHY_S5PV210_USB2
136 {
137 .compatible = "samsung,s5pv210-usb2-phy",
138 .data = &s5pv210_usb2_phy_config,
139 },
140#endif
141 { },
142};
143MODULE_DEVICE_TABLE(of, samsung_usb2_phy_of_match);
144
145static int samsung_usb2_phy_probe(struct platform_device *pdev)
146{
147 const struct samsung_usb2_phy_config *cfg;
148 struct device *dev = &pdev->dev;
149 struct phy_provider *phy_provider;
150 struct samsung_usb2_phy_driver *drv;
151 int i, ret;
152
153 if (!pdev->dev.of_node) {
154 dev_err(dev, "This driver is required to be instantiated from device tree\n");
155 return -EINVAL;
156 }
157
158 cfg = of_device_get_match_data(dev);
159 if (!cfg)
160 return -EINVAL;
161
162 drv = devm_kzalloc(dev, struct_size(drv, instances, cfg->num_phys),
163 GFP_KERNEL);
164 if (!drv)
165 return -ENOMEM;
166
167 dev_set_drvdata(dev, drv);
168 spin_lock_init(&drv->lock);
169
170 drv->cfg = cfg;
171 drv->dev = dev;
172
173 drv->reg_phy = devm_platform_ioremap_resource(pdev, 0);
174 if (IS_ERR(drv->reg_phy)) {
175 dev_err(dev, "Failed to map register memory (phy)\n");
176 return PTR_ERR(drv->reg_phy);
177 }
178
179 drv->reg_pmu = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
180 "samsung,pmureg-phandle");
181 if (IS_ERR(drv->reg_pmu)) {
182 dev_err(dev, "Failed to map PMU registers (via syscon)\n");
183 return PTR_ERR(drv->reg_pmu);
184 }
185
186 if (drv->cfg->has_mode_switch) {
187 drv->reg_sys = syscon_regmap_lookup_by_phandle(
188 pdev->dev.of_node, "samsung,sysreg-phandle");
189 if (IS_ERR(drv->reg_sys)) {
190 dev_err(dev, "Failed to map system registers (via syscon)\n");
191 return PTR_ERR(drv->reg_sys);
192 }
193 }
194
195 drv->clk = devm_clk_get(dev, "phy");
196 if (IS_ERR(drv->clk)) {
197 dev_err(dev, "Failed to get clock of phy controller\n");
198 return PTR_ERR(drv->clk);
199 }
200
201 drv->ref_clk = devm_clk_get(dev, "ref");
202 if (IS_ERR(drv->ref_clk)) {
203 dev_err(dev, "Failed to get reference clock for the phy controller\n");
204 return PTR_ERR(drv->ref_clk);
205 }
206
207 drv->ref_rate = clk_get_rate(drv->ref_clk);
208 if (drv->cfg->rate_to_clk) {
209 ret = drv->cfg->rate_to_clk(drv->ref_rate, &drv->ref_reg_val);
210 if (ret)
211 return ret;
212 }
213
214 drv->vbus = devm_regulator_get(dev, "vbus");
215 if (IS_ERR(drv->vbus)) {
216 ret = PTR_ERR(drv->vbus);
217 if (ret == -EPROBE_DEFER)
218 return ret;
219 drv->vbus = NULL;
220 }
221
222 for (i = 0; i < drv->cfg->num_phys; i++) {
223 char *label = drv->cfg->phys[i].label;
224 struct samsung_usb2_phy_instance *p = &drv->instances[i];
225
226 dev_dbg(dev, "Creating phy \"%s\"\n", label);
227 p->phy = devm_phy_create(dev, NULL, &samsung_usb2_phy_ops);
228 if (IS_ERR(p->phy)) {
229 dev_err(drv->dev, "Failed to create usb2_phy \"%s\"\n",
230 label);
231 return PTR_ERR(p->phy);
232 }
233
234 p->cfg = &drv->cfg->phys[i];
235 p->drv = drv;
236 phy_set_bus_width(p->phy, 8);
237 phy_set_drvdata(p->phy, p);
238 }
239
240 phy_provider = devm_of_phy_provider_register(dev,
241 samsung_usb2_phy_xlate);
242 if (IS_ERR(phy_provider)) {
243 dev_err(drv->dev, "Failed to register phy provider\n");
244 return PTR_ERR(phy_provider);
245 }
246
247 return 0;
248}
249
250static struct platform_driver samsung_usb2_phy_driver = {
251 .probe = samsung_usb2_phy_probe,
252 .driver = {
253 .of_match_table = samsung_usb2_phy_of_match,
254 .name = "samsung-usb2-phy",
255 .suppress_bind_attrs = true,
256 }
257};
258
259module_platform_driver(samsung_usb2_phy_driver);
260MODULE_DESCRIPTION("Samsung S5P/Exynos SoC USB PHY driver");
261MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
262MODULE_LICENSE("GPL v2");
263MODULE_ALIAS("platform:samsung-usb2-phy");
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Samsung SoC USB 1.1/2.0 PHY driver
4 *
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * Author: Kamil Debski <k.debski@samsung.com>
7 */
8
9#include <linux/clk.h>
10#include <linux/mfd/syscon.h>
11#include <linux/module.h>
12#include <linux/of.h>
13#include <linux/phy/phy.h>
14#include <linux/platform_device.h>
15#include <linux/spinlock.h>
16#include "phy-samsung-usb2.h"
17
18static int samsung_usb2_phy_power_on(struct phy *phy)
19{
20 struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
21 struct samsung_usb2_phy_driver *drv = inst->drv;
22 int ret;
23
24 dev_dbg(drv->dev, "Request to power_on \"%s\" usb phy\n",
25 inst->cfg->label);
26
27 if (drv->vbus) {
28 ret = regulator_enable(drv->vbus);
29 if (ret)
30 goto err_regulator;
31 }
32
33 ret = clk_prepare_enable(drv->clk);
34 if (ret)
35 goto err_main_clk;
36 ret = clk_prepare_enable(drv->ref_clk);
37 if (ret)
38 goto err_instance_clk;
39 if (inst->cfg->power_on) {
40 spin_lock(&drv->lock);
41 ret = inst->cfg->power_on(inst);
42 spin_unlock(&drv->lock);
43 if (ret)
44 goto err_power_on;
45 }
46
47 return 0;
48
49err_power_on:
50 clk_disable_unprepare(drv->ref_clk);
51err_instance_clk:
52 clk_disable_unprepare(drv->clk);
53err_main_clk:
54 if (drv->vbus)
55 regulator_disable(drv->vbus);
56err_regulator:
57 return ret;
58}
59
60static int samsung_usb2_phy_power_off(struct phy *phy)
61{
62 struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
63 struct samsung_usb2_phy_driver *drv = inst->drv;
64 int ret = 0;
65
66 dev_dbg(drv->dev, "Request to power_off \"%s\" usb phy\n",
67 inst->cfg->label);
68 if (inst->cfg->power_off) {
69 spin_lock(&drv->lock);
70 ret = inst->cfg->power_off(inst);
71 spin_unlock(&drv->lock);
72 if (ret)
73 return ret;
74 }
75 clk_disable_unprepare(drv->ref_clk);
76 clk_disable_unprepare(drv->clk);
77 if (drv->vbus)
78 ret = regulator_disable(drv->vbus);
79
80 return ret;
81}
82
83static const struct phy_ops samsung_usb2_phy_ops = {
84 .power_on = samsung_usb2_phy_power_on,
85 .power_off = samsung_usb2_phy_power_off,
86 .owner = THIS_MODULE,
87};
88
89static struct phy *samsung_usb2_phy_xlate(struct device *dev,
90 const struct of_phandle_args *args)
91{
92 struct samsung_usb2_phy_driver *drv;
93
94 drv = dev_get_drvdata(dev);
95 if (!drv)
96 return ERR_PTR(-EINVAL);
97
98 if (WARN_ON(args->args[0] >= drv->cfg->num_phys))
99 return ERR_PTR(-ENODEV);
100
101 return drv->instances[args->args[0]].phy;
102}
103
104static const struct of_device_id samsung_usb2_phy_of_match[] = {
105#ifdef CONFIG_PHY_EXYNOS4X12_USB2
106 {
107 .compatible = "samsung,exynos3250-usb2-phy",
108 .data = &exynos3250_usb2_phy_config,
109 },
110#endif
111#ifdef CONFIG_PHY_EXYNOS4210_USB2
112 {
113 .compatible = "samsung,exynos4210-usb2-phy",
114 .data = &exynos4210_usb2_phy_config,
115 },
116#endif
117#ifdef CONFIG_PHY_EXYNOS4X12_USB2
118 {
119 .compatible = "samsung,exynos4x12-usb2-phy",
120 .data = &exynos4x12_usb2_phy_config,
121 },
122#endif
123#ifdef CONFIG_PHY_EXYNOS5250_USB2
124 {
125 .compatible = "samsung,exynos5250-usb2-phy",
126 .data = &exynos5250_usb2_phy_config,
127 },
128 {
129 .compatible = "samsung,exynos5420-usb2-phy",
130 .data = &exynos5420_usb2_phy_config,
131 },
132#endif
133#ifdef CONFIG_PHY_S5PV210_USB2
134 {
135 .compatible = "samsung,s5pv210-usb2-phy",
136 .data = &s5pv210_usb2_phy_config,
137 },
138#endif
139 { },
140};
141MODULE_DEVICE_TABLE(of, samsung_usb2_phy_of_match);
142
143static int samsung_usb2_phy_probe(struct platform_device *pdev)
144{
145 const struct samsung_usb2_phy_config *cfg;
146 struct device *dev = &pdev->dev;
147 struct phy_provider *phy_provider;
148 struct samsung_usb2_phy_driver *drv;
149 int i, ret;
150
151 if (!pdev->dev.of_node) {
152 dev_err(dev, "This driver is required to be instantiated from device tree\n");
153 return -EINVAL;
154 }
155
156 cfg = of_device_get_match_data(dev);
157 if (!cfg)
158 return -EINVAL;
159
160 drv = devm_kzalloc(dev, struct_size(drv, instances, cfg->num_phys),
161 GFP_KERNEL);
162 if (!drv)
163 return -ENOMEM;
164
165 dev_set_drvdata(dev, drv);
166 spin_lock_init(&drv->lock);
167
168 drv->cfg = cfg;
169 drv->dev = dev;
170
171 drv->reg_phy = devm_platform_ioremap_resource(pdev, 0);
172 if (IS_ERR(drv->reg_phy)) {
173 dev_err(dev, "Failed to map register memory (phy)\n");
174 return PTR_ERR(drv->reg_phy);
175 }
176
177 drv->reg_pmu = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
178 "samsung,pmureg-phandle");
179 if (IS_ERR(drv->reg_pmu)) {
180 dev_err(dev, "Failed to map PMU registers (via syscon)\n");
181 return PTR_ERR(drv->reg_pmu);
182 }
183
184 if (drv->cfg->has_mode_switch) {
185 drv->reg_sys = syscon_regmap_lookup_by_phandle(
186 pdev->dev.of_node, "samsung,sysreg-phandle");
187 if (IS_ERR(drv->reg_sys)) {
188 dev_err(dev, "Failed to map system registers (via syscon)\n");
189 return PTR_ERR(drv->reg_sys);
190 }
191 }
192
193 drv->clk = devm_clk_get(dev, "phy");
194 if (IS_ERR(drv->clk)) {
195 dev_err(dev, "Failed to get clock of phy controller\n");
196 return PTR_ERR(drv->clk);
197 }
198
199 drv->ref_clk = devm_clk_get(dev, "ref");
200 if (IS_ERR(drv->ref_clk)) {
201 dev_err(dev, "Failed to get reference clock for the phy controller\n");
202 return PTR_ERR(drv->ref_clk);
203 }
204
205 drv->ref_rate = clk_get_rate(drv->ref_clk);
206 if (drv->cfg->rate_to_clk) {
207 ret = drv->cfg->rate_to_clk(drv->ref_rate, &drv->ref_reg_val);
208 if (ret)
209 return ret;
210 }
211
212 drv->vbus = devm_regulator_get(dev, "vbus");
213 if (IS_ERR(drv->vbus)) {
214 ret = PTR_ERR(drv->vbus);
215 if (ret == -EPROBE_DEFER)
216 return ret;
217 drv->vbus = NULL;
218 }
219
220 for (i = 0; i < drv->cfg->num_phys; i++) {
221 char *label = drv->cfg->phys[i].label;
222 struct samsung_usb2_phy_instance *p = &drv->instances[i];
223
224 dev_dbg(dev, "Creating phy \"%s\"\n", label);
225 p->phy = devm_phy_create(dev, NULL, &samsung_usb2_phy_ops);
226 if (IS_ERR(p->phy)) {
227 dev_err(drv->dev, "Failed to create usb2_phy \"%s\"\n",
228 label);
229 return PTR_ERR(p->phy);
230 }
231
232 p->cfg = &drv->cfg->phys[i];
233 p->drv = drv;
234 phy_set_bus_width(p->phy, 8);
235 phy_set_drvdata(p->phy, p);
236 }
237
238 phy_provider = devm_of_phy_provider_register(dev,
239 samsung_usb2_phy_xlate);
240 if (IS_ERR(phy_provider)) {
241 dev_err(drv->dev, "Failed to register phy provider\n");
242 return PTR_ERR(phy_provider);
243 }
244
245 return 0;
246}
247
248static struct platform_driver samsung_usb2_phy_driver = {
249 .probe = samsung_usb2_phy_probe,
250 .driver = {
251 .of_match_table = samsung_usb2_phy_of_match,
252 .name = "samsung-usb2-phy",
253 .suppress_bind_attrs = true,
254 }
255};
256
257module_platform_driver(samsung_usb2_phy_driver);
258MODULE_DESCRIPTION("Samsung S5P/Exynos SoC USB PHY driver");
259MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
260MODULE_LICENSE("GPL v2");
261MODULE_ALIAS("platform:samsung-usb2-phy");