1 /*
2 * Samsung SoC USB 1.1/2.0 PHY driver
3 *
4 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
5 * Author: Kamil Debski <k.debski@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12 #include <linux/clk.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/spinlock.h>
21 #include "phy-samsung-usb2.h"
22
samsung_usb2_phy_power_on(struct phy * phy)23 static int samsung_usb2_phy_power_on(struct phy *phy)
24 {
25 struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
26 struct samsung_usb2_phy_driver *drv = inst->drv;
27 int ret;
28
29 dev_dbg(drv->dev, "Request to power_on \"%s\" usb phy\n",
30 inst->cfg->label);
31
32 if (drv->vbus) {
33 ret = regulator_enable(drv->vbus);
34 if (ret)
35 goto err_regulator;
36 }
37
38 ret = clk_prepare_enable(drv->clk);
39 if (ret)
40 goto err_main_clk;
41 ret = clk_prepare_enable(drv->ref_clk);
42 if (ret)
43 goto err_instance_clk;
44 if (inst->cfg->power_on) {
45 spin_lock(&drv->lock);
46 ret = inst->cfg->power_on(inst);
47 spin_unlock(&drv->lock);
48 if (ret)
49 goto err_power_on;
50 }
51
52 return 0;
53
54 err_power_on:
55 clk_disable_unprepare(drv->ref_clk);
56 err_instance_clk:
57 clk_disable_unprepare(drv->clk);
58 err_main_clk:
59 if (drv->vbus)
60 regulator_disable(drv->vbus);
61 err_regulator:
62 return ret;
63 }
64
samsung_usb2_phy_power_off(struct phy * phy)65 static int samsung_usb2_phy_power_off(struct phy *phy)
66 {
67 struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
68 struct samsung_usb2_phy_driver *drv = inst->drv;
69 int ret = 0;
70
71 dev_dbg(drv->dev, "Request to power_off \"%s\" usb phy\n",
72 inst->cfg->label);
73 if (inst->cfg->power_off) {
74 spin_lock(&drv->lock);
75 ret = inst->cfg->power_off(inst);
76 spin_unlock(&drv->lock);
77 if (ret)
78 return ret;
79 }
80 clk_disable_unprepare(drv->ref_clk);
81 clk_disable_unprepare(drv->clk);
82 if (drv->vbus)
83 ret = regulator_disable(drv->vbus);
84
85 return ret;
86 }
87
88 static const struct phy_ops samsung_usb2_phy_ops = {
89 .power_on = samsung_usb2_phy_power_on,
90 .power_off = samsung_usb2_phy_power_off,
91 .owner = THIS_MODULE,
92 };
93
samsung_usb2_phy_xlate(struct device * dev,struct of_phandle_args * args)94 static struct phy *samsung_usb2_phy_xlate(struct device *dev,
95 struct of_phandle_args *args)
96 {
97 struct samsung_usb2_phy_driver *drv;
98
99 drv = dev_get_drvdata(dev);
100 if (!drv)
101 return ERR_PTR(-EINVAL);
102
103 if (WARN_ON(args->args[0] >= drv->cfg->num_phys))
104 return ERR_PTR(-ENODEV);
105
106 return drv->instances[args->args[0]].phy;
107 }
108
109 static const struct of_device_id samsung_usb2_phy_of_match[] = {
110 #ifdef CONFIG_PHY_EXYNOS4X12_USB2
111 {
112 .compatible = "samsung,exynos3250-usb2-phy",
113 .data = &exynos3250_usb2_phy_config,
114 },
115 #endif
116 #ifdef CONFIG_PHY_EXYNOS4210_USB2
117 {
118 .compatible = "samsung,exynos4210-usb2-phy",
119 .data = &exynos4210_usb2_phy_config,
120 },
121 #endif
122 #ifdef CONFIG_PHY_EXYNOS4X12_USB2
123 {
124 .compatible = "samsung,exynos4x12-usb2-phy",
125 .data = &exynos4x12_usb2_phy_config,
126 },
127 #endif
128 #ifdef CONFIG_PHY_EXYNOS5250_USB2
129 {
130 .compatible = "samsung,exynos5250-usb2-phy",
131 .data = &exynos5250_usb2_phy_config,
132 },
133 #endif
134 #ifdef CONFIG_PHY_S5PV210_USB2
135 {
136 .compatible = "samsung,s5pv210-usb2-phy",
137 .data = &s5pv210_usb2_phy_config,
138 },
139 #endif
140 { },
141 };
142 MODULE_DEVICE_TABLE(of, samsung_usb2_phy_of_match);
143
samsung_usb2_phy_probe(struct platform_device * pdev)144 static int samsung_usb2_phy_probe(struct platform_device *pdev)
145 {
146 const struct samsung_usb2_phy_config *cfg;
147 struct device *dev = &pdev->dev;
148 struct phy_provider *phy_provider;
149 struct resource *mem;
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, sizeof(struct samsung_usb2_phy_driver) +
163 cfg->num_phys * sizeof(struct samsung_usb2_phy_instance),
164 GFP_KERNEL);
165 if (!drv)
166 return -ENOMEM;
167
168 dev_set_drvdata(dev, drv);
169 spin_lock_init(&drv->lock);
170
171 drv->cfg = cfg;
172 drv->dev = dev;
173
174 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
175 drv->reg_phy = devm_ioremap_resource(dev, mem);
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);
179 }
180
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);
186 }
187
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);
194 }
195 }
196
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);
201 }
202
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);
207 }
208
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);
212 if (ret)
213 return ret;
214 }
215
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)
220 return ret;
221 drv->vbus = NULL;
222 }
223
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];
227
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",
232 label);
233 return PTR_ERR(p->phy);
234 }
235
236 p->cfg = &drv->cfg->phys[i];
237 p->drv = drv;
238 phy_set_bus_width(p->phy, 8);
239 phy_set_drvdata(p->phy, p);
240 }
241
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);
247 }
248
249 return 0;
250 }
251
252 static struct platform_driver samsung_usb2_phy_driver = {
253 .probe = samsung_usb2_phy_probe,
254 .driver = {
255 .of_match_table = samsung_usb2_phy_of_match,
256 .name = "samsung-usb2-phy",
257 }
258 };
259
260 module_platform_driver(samsung_usb2_phy_driver);
261 MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC USB PHY driver");
262 MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
263 MODULE_LICENSE("GPL v2");
264 MODULE_ALIAS("platform:samsung-usb2-phy");
265