]>
Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
06fb0137 KD |
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> | |
06fb0137 KD |
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> | |
e0ed4082 | 14 | #include <linux/of_device.h> |
06fb0137 KD |
15 | #include <linux/phy/phy.h> |
16 | #include <linux/platform_device.h> | |
17 | #include <linux/spinlock.h> | |
18 | #include "phy-samsung-usb2.h" | |
19 | ||
20 | static 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); | |
a007ddba MS |
28 | |
29 | if (drv->vbus) { | |
30 | ret = regulator_enable(drv->vbus); | |
31 | if (ret) | |
32 | goto err_regulator; | |
33 | } | |
34 | ||
06fb0137 KD |
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); | |
7a504c93 AL |
45 | if (ret) |
46 | goto err_power_on; | |
06fb0137 KD |
47 | } |
48 | ||
49 | return 0; | |
50 | ||
7a504c93 AL |
51 | err_power_on: |
52 | clk_disable_unprepare(drv->ref_clk); | |
06fb0137 KD |
53 | err_instance_clk: |
54 | clk_disable_unprepare(drv->clk); | |
55 | err_main_clk: | |
a007ddba MS |
56 | if (drv->vbus) |
57 | regulator_disable(drv->vbus); | |
58 | err_regulator: | |
06fb0137 KD |
59 | return ret; |
60 | } | |
61 | ||
62 | static 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; | |
a007ddba | 66 | int ret = 0; |
06fb0137 KD |
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); | |
7a504c93 AL |
74 | if (ret) |
75 | return ret; | |
06fb0137 KD |
76 | } |
77 | clk_disable_unprepare(drv->ref_clk); | |
78 | clk_disable_unprepare(drv->clk); | |
a007ddba MS |
79 | if (drv->vbus) |
80 | ret = regulator_disable(drv->vbus); | |
81 | ||
82 | return ret; | |
06fb0137 KD |
83 | } |
84 | ||
4a9e5ca1 | 85 | static const struct phy_ops samsung_usb2_phy_ops = { |
06fb0137 KD |
86 | .power_on = samsung_usb2_phy_power_on, |
87 | .power_off = samsung_usb2_phy_power_off, | |
88 | .owner = THIS_MODULE, | |
89 | }; | |
90 | ||
91 | static 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 | ||
106 | static const struct of_device_id samsung_usb2_phy_of_match[] = { | |
016e0d3c MS |
107 | #ifdef CONFIG_PHY_EXYNOS4X12_USB2 |
108 | { | |
109 | .compatible = "samsung,exynos3250-usb2-phy", | |
110 | .data = &exynos3250_usb2_phy_config, | |
111 | }, | |
112 | #endif | |
06fb0137 KD |
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 | }, | |
64bf2b23 KD |
124 | #endif |
125 | #ifdef CONFIG_PHY_EXYNOS5250_USB2 | |
126 | { | |
127 | .compatible = "samsung,exynos5250-usb2-phy", | |
128 | .data = &exynos5250_usb2_phy_config, | |
129 | }, | |
b3345d7c LT |
130 | #endif |
131 | #ifdef CONFIG_PHY_S5PV210_USB2 | |
132 | { | |
133 | .compatible = "samsung,s5pv210-usb2-phy", | |
134 | .data = &s5pv210_usb2_phy_config, | |
135 | }, | |
06fb0137 KD |
136 | #endif |
137 | { }, | |
138 | }; | |
bf5baf95 | 139 | MODULE_DEVICE_TABLE(of, samsung_usb2_phy_of_match); |
06fb0137 KD |
140 | |
141 | static int samsung_usb2_phy_probe(struct platform_device *pdev) | |
142 | { | |
06fb0137 KD |
143 | const struct samsung_usb2_phy_config *cfg; |
144 | struct device *dev = &pdev->dev; | |
145 | struct phy_provider *phy_provider; | |
146 | struct resource *mem; | |
147 | struct samsung_usb2_phy_driver *drv; | |
148 | int i, ret; | |
149 | ||
150 | if (!pdev->dev.of_node) { | |
151 | dev_err(dev, "This driver is required to be instantiated from device tree\n"); | |
152 | return -EINVAL; | |
153 | } | |
154 | ||
e0ed4082 CY |
155 | cfg = of_device_get_match_data(dev); |
156 | if (!cfg) | |
06fb0137 | 157 | return -EINVAL; |
06fb0137 KD |
158 | |
159 | drv = devm_kzalloc(dev, sizeof(struct samsung_usb2_phy_driver) + | |
160 | cfg->num_phys * sizeof(struct samsung_usb2_phy_instance), | |
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 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
172 | drv->reg_phy = devm_ioremap_resource(dev, mem); | |
173 | if (IS_ERR(drv->reg_phy)) { | |
174 | dev_err(dev, "Failed to map register memory (phy)\n"); | |
175 | return PTR_ERR(drv->reg_phy); | |
176 | } | |
177 | ||
178 | drv->reg_pmu = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, | |
179 | "samsung,pmureg-phandle"); | |
180 | if (IS_ERR(drv->reg_pmu)) { | |
181 | dev_err(dev, "Failed to map PMU registers (via syscon)\n"); | |
182 | return PTR_ERR(drv->reg_pmu); | |
183 | } | |
184 | ||
185 | if (drv->cfg->has_mode_switch) { | |
186 | drv->reg_sys = syscon_regmap_lookup_by_phandle( | |
187 | pdev->dev.of_node, "samsung,sysreg-phandle"); | |
188 | if (IS_ERR(drv->reg_sys)) { | |
189 | dev_err(dev, "Failed to map system registers (via syscon)\n"); | |
190 | return PTR_ERR(drv->reg_sys); | |
191 | } | |
192 | } | |
193 | ||
194 | drv->clk = devm_clk_get(dev, "phy"); | |
195 | if (IS_ERR(drv->clk)) { | |
196 | dev_err(dev, "Failed to get clock of phy controller\n"); | |
197 | return PTR_ERR(drv->clk); | |
198 | } | |
199 | ||
200 | drv->ref_clk = devm_clk_get(dev, "ref"); | |
201 | if (IS_ERR(drv->ref_clk)) { | |
202 | dev_err(dev, "Failed to get reference clock for the phy controller\n"); | |
203 | return PTR_ERR(drv->ref_clk); | |
204 | } | |
205 | ||
206 | drv->ref_rate = clk_get_rate(drv->ref_clk); | |
207 | if (drv->cfg->rate_to_clk) { | |
208 | ret = drv->cfg->rate_to_clk(drv->ref_rate, &drv->ref_reg_val); | |
209 | if (ret) | |
210 | return ret; | |
211 | } | |
212 | ||
a007ddba MS |
213 | drv->vbus = devm_regulator_get(dev, "vbus"); |
214 | if (IS_ERR(drv->vbus)) { | |
215 | ret = PTR_ERR(drv->vbus); | |
216 | if (ret == -EPROBE_DEFER) | |
217 | return ret; | |
218 | drv->vbus = NULL; | |
219 | } | |
220 | ||
06fb0137 KD |
221 | for (i = 0; i < drv->cfg->num_phys; i++) { |
222 | char *label = drv->cfg->phys[i].label; | |
223 | struct samsung_usb2_phy_instance *p = &drv->instances[i]; | |
224 | ||
225 | dev_dbg(dev, "Creating phy \"%s\"\n", label); | |
dbc98635 | 226 | p->phy = devm_phy_create(dev, NULL, &samsung_usb2_phy_ops); |
06fb0137 KD |
227 | if (IS_ERR(p->phy)) { |
228 | dev_err(drv->dev, "Failed to create usb2_phy \"%s\"\n", | |
229 | label); | |
230 | return PTR_ERR(p->phy); | |
231 | } | |
232 | ||
233 | p->cfg = &drv->cfg->phys[i]; | |
234 | p->drv = drv; | |
235 | phy_set_bus_width(p->phy, 8); | |
236 | phy_set_drvdata(p->phy, p); | |
237 | } | |
238 | ||
239 | phy_provider = devm_of_phy_provider_register(dev, | |
240 | samsung_usb2_phy_xlate); | |
241 | if (IS_ERR(phy_provider)) { | |
242 | dev_err(drv->dev, "Failed to register phy provider\n"); | |
243 | return PTR_ERR(phy_provider); | |
244 | } | |
245 | ||
246 | return 0; | |
247 | } | |
248 | ||
249 | static struct platform_driver samsung_usb2_phy_driver = { | |
250 | .probe = samsung_usb2_phy_probe, | |
251 | .driver = { | |
252 | .of_match_table = samsung_usb2_phy_of_match, | |
253 | .name = "samsung-usb2-phy", | |
06fb0137 KD |
254 | } |
255 | }; | |
256 | ||
257 | module_platform_driver(samsung_usb2_phy_driver); | |
258 | MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC USB PHY driver"); | |
259 | MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>"); | |
260 | MODULE_LICENSE("GPL v2"); | |
261 | MODULE_ALIAS("platform:samsung-usb2-phy"); |