]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/phy/ti/phy-omap-usb2.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 157
[mirror_ubuntu-hirsute-kernel.git] / drivers / phy / ti / phy-omap-usb2.c
CommitLineData
c942fddf 1// SPDX-License-Identifier: GPL-2.0-or-later
657b306a
KVA
2/*
3 * omap-usb2.c - USB PHY, talking to musb controller in OMAP.
4 *
5 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com
657b306a 6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
657b306a
KVA
7 */
8
9#include <linux/module.h>
10#include <linux/platform_device.h>
11#include <linux/slab.h>
12#include <linux/of.h>
13#include <linux/io.h>
6284be23 14#include <linux/phy/omap_usb.h>
657b306a
KVA
15#include <linux/usb/phy_companion.h>
16#include <linux/clk.h>
17#include <linux/err.h>
18#include <linux/pm_runtime.h>
19#include <linux/delay.h>
14da699b 20#include <linux/phy/omap_control_phy.h>
5d93d1e7 21#include <linux/phy/phy.h>
9955a783
KVA
22#include <linux/mfd/syscon.h>
23#include <linux/regmap.h>
478b6c74 24#include <linux/of_platform.h>
657b306a 25
7e472402
AB
26#define USB2PHY_DISCON_BYP_LATCH (1 << 31)
27#define USB2PHY_ANA_CONFIG1 0x4c
28
6777cee3
RQ
29#define AM654_USB2_OTG_PD BIT(8)
30#define AM654_USB2_VBUS_DET_EN BIT(5)
31#define AM654_USB2_VBUSVALID_DET_EN BIT(4)
32
657b306a
KVA
33/**
34 * omap_usb2_set_comparator - links the comparator present in the sytem with
35 * this phy
36 * @comparator - the companion phy(comparator) for this phy
37 *
38 * The phy companion driver should call this API passing the phy_companion
39 * filled with set_vbus and start_srp to be used by usb phy.
40 *
41 * For use by phy companion driver
42 */
43int omap_usb2_set_comparator(struct phy_companion *comparator)
44{
45 struct omap_usb *phy;
46 struct usb_phy *x = usb_get_phy(USB_PHY_TYPE_USB2);
47
48 if (IS_ERR(x))
49 return -ENODEV;
50
51 phy = phy_to_omapusb(x);
52 phy->comparator = comparator;
53 return 0;
54}
55EXPORT_SYMBOL_GPL(omap_usb2_set_comparator);
56
657b306a
KVA
57static int omap_usb_set_vbus(struct usb_otg *otg, bool enabled)
58{
19c1eac2 59 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
657b306a
KVA
60
61 if (!phy->comparator)
62 return -ENODEV;
63
64 return phy->comparator->set_vbus(phy->comparator, enabled);
65}
66
67static int omap_usb_start_srp(struct usb_otg *otg)
68{
19c1eac2 69 struct omap_usb *phy = phy_to_omapusb(otg->usb_phy);
657b306a
KVA
70
71 if (!phy->comparator)
72 return -ENODEV;
73
74 return phy->comparator->start_srp(phy->comparator);
75}
76
77static int omap_usb_set_host(struct usb_otg *otg, struct usb_bus *host)
78{
657b306a
KVA
79 otg->host = host;
80 if (!host)
e47d9254 81 otg->state = OTG_STATE_UNDEFINED;
657b306a
KVA
82
83 return 0;
84}
85
86static int omap_usb_set_peripheral(struct usb_otg *otg,
87 struct usb_gadget *gadget)
88{
657b306a
KVA
89 otg->gadget = gadget;
90 if (!gadget)
e47d9254 91 otg->state = OTG_STATE_UNDEFINED;
657b306a
KVA
92
93 return 0;
94}
95
9955a783
KVA
96static int omap_usb_phy_power(struct omap_usb *phy, int on)
97{
98 u32 val;
99 int ret;
100
101 if (!phy->syscon_phy_power) {
102 omap_control_phy_power(phy->control_dev, on);
103 return 0;
104 }
105
106 if (on)
107 val = phy->power_on;
108 else
109 val = phy->power_off;
110
111 ret = regmap_update_bits(phy->syscon_phy_power, phy->power_reg,
112 phy->mask, val);
113 return ret;
114}
115
5d93d1e7
KVA
116static int omap_usb_power_off(struct phy *x)
117{
118 struct omap_usb *phy = phy_get_drvdata(x);
119
9955a783 120 return omap_usb_phy_power(phy, false);
5d93d1e7
KVA
121}
122
123static int omap_usb_power_on(struct phy *x)
124{
125 struct omap_usb *phy = phy_get_drvdata(x);
126
9955a783 127 return omap_usb_phy_power(phy, true);
5d93d1e7
KVA
128}
129
80fc6660
SN
130static int omap_usb2_disable_clocks(struct omap_usb *phy)
131{
ed31ee7c 132 clk_disable_unprepare(phy->wkupclk);
80fc6660 133 if (!IS_ERR(phy->optclk))
ed31ee7c 134 clk_disable_unprepare(phy->optclk);
80fc6660
SN
135
136 return 0;
137}
138
139static int omap_usb2_enable_clocks(struct omap_usb *phy)
140{
141 int ret;
142
ed31ee7c 143 ret = clk_prepare_enable(phy->wkupclk);
80fc6660
SN
144 if (ret < 0) {
145 dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
146 goto err0;
147 }
148
149 if (!IS_ERR(phy->optclk)) {
ed31ee7c 150 ret = clk_prepare_enable(phy->optclk);
80fc6660
SN
151 if (ret < 0) {
152 dev_err(phy->dev, "Failed to enable optclk %d\n", ret);
153 goto err1;
154 }
155 }
156
157 return 0;
158
159err1:
160 clk_disable(phy->wkupclk);
161
162err0:
163 return ret;
164}
165
7e472402
AB
166static int omap_usb_init(struct phy *x)
167{
168 struct omap_usb *phy = phy_get_drvdata(x);
169 u32 val;
170
80fc6660
SN
171 omap_usb2_enable_clocks(phy);
172
7e472402
AB
173 if (phy->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
174 /*
175 *
176 * Reduce the sensitivity of internal PHY by enabling the
177 * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
178 * resolves issues with certain devices which can otherwise
179 * be prone to false disconnects.
180 *
181 */
182 val = omap_usb_readl(phy->phy_base, USB2PHY_ANA_CONFIG1);
183 val |= USB2PHY_DISCON_BYP_LATCH;
184 omap_usb_writel(phy->phy_base, USB2PHY_ANA_CONFIG1, val);
185 }
186
187 return 0;
188}
189
80fc6660
SN
190static int omap_usb_exit(struct phy *x)
191{
192 struct omap_usb *phy = phy_get_drvdata(x);
193
194 return omap_usb2_disable_clocks(phy);
195}
196
4a9e5ca1 197static const struct phy_ops ops = {
7e472402 198 .init = omap_usb_init,
80fc6660 199 .exit = omap_usb_exit,
5d93d1e7
KVA
200 .power_on = omap_usb_power_on,
201 .power_off = omap_usb_power_off,
202 .owner = THIS_MODULE,
203};
204
09a0168d
GC
205static const struct usb_phy_data omap_usb2_data = {
206 .label = "omap_usb2",
207 .flags = OMAP_USB2_HAS_START_SRP | OMAP_USB2_HAS_SET_VBUS,
9955a783
KVA
208 .mask = OMAP_DEV_PHY_PD,
209 .power_off = OMAP_DEV_PHY_PD,
09a0168d
GC
210};
211
db68e80f
GC
212static const struct usb_phy_data omap5_usb2_data = {
213 .label = "omap5_usb2",
214 .flags = 0,
9955a783
KVA
215 .mask = OMAP_DEV_PHY_PD,
216 .power_off = OMAP_DEV_PHY_PD,
db68e80f
GC
217};
218
7e472402
AB
219static const struct usb_phy_data dra7x_usb2_data = {
220 .label = "dra7x_usb2",
221 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
9955a783
KVA
222 .mask = OMAP_DEV_PHY_PD,
223 .power_off = OMAP_DEV_PHY_PD,
224};
225
226static const struct usb_phy_data dra7x_usb2_phy2_data = {
227 .label = "dra7x_usb2_phy2",
228 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
229 .mask = OMAP_USB2_PHY_PD,
230 .power_off = OMAP_USB2_PHY_PD,
7e472402
AB
231};
232
09a0168d
GC
233static const struct usb_phy_data am437x_usb2_data = {
234 .label = "am437x_usb2",
235 .flags = 0,
9955a783
KVA
236 .mask = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD |
237 AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
238 .power_on = AM437X_USB2_OTGVDET_EN | AM437X_USB2_OTGSESSEND_EN,
239 .power_off = AM437X_USB2_PHY_PD | AM437X_USB2_OTG_PD,
09a0168d
GC
240};
241
6777cee3
RQ
242static const struct usb_phy_data am654_usb2_data = {
243 .label = "am654_usb2",
244 .flags = OMAP_USB2_CALIBRATE_FALSE_DISCONNECT,
245 .mask = AM654_USB2_OTG_PD | AM654_USB2_VBUS_DET_EN |
246 AM654_USB2_VBUSVALID_DET_EN,
247 .power_on = AM654_USB2_VBUS_DET_EN | AM654_USB2_VBUSVALID_DET_EN,
248 .power_off = AM654_USB2_OTG_PD,
249};
250
09a0168d
GC
251static const struct of_device_id omap_usb2_id_table[] = {
252 {
253 .compatible = "ti,omap-usb2",
254 .data = &omap_usb2_data,
255 },
db68e80f
GC
256 {
257 .compatible = "ti,omap5-usb2",
258 .data = &omap5_usb2_data,
259 },
7e472402
AB
260 {
261 .compatible = "ti,dra7x-usb2",
262 .data = &dra7x_usb2_data,
263 },
9955a783
KVA
264 {
265 .compatible = "ti,dra7x-usb2-phy2",
266 .data = &dra7x_usb2_phy2_data,
267 },
09a0168d
GC
268 {
269 .compatible = "ti,am437x-usb2",
270 .data = &am437x_usb2_data,
271 },
6777cee3
RQ
272 {
273 .compatible = "ti,am654-usb2",
274 .data = &am654_usb2_data,
275 },
09a0168d
GC
276 {},
277};
278MODULE_DEVICE_TABLE(of, omap_usb2_id_table);
09a0168d 279
41ac7b3a 280static int omap_usb2_probe(struct platform_device *pdev)
657b306a 281{
478b6c74
RQ
282 struct omap_usb *phy;
283 struct phy *generic_phy;
7e472402 284 struct resource *res;
478b6c74
RQ
285 struct phy_provider *phy_provider;
286 struct usb_otg *otg;
287 struct device_node *node = pdev->dev.of_node;
288 struct device_node *control_node;
289 struct platform_device *control_pdev;
09a0168d
GC
290 const struct of_device_id *of_id;
291 struct usb_phy_data *phy_data;
292
52a4a72a 293 of_id = of_match_device(omap_usb2_id_table, &pdev->dev);
478b6c74 294
09a0168d 295 if (!of_id)
478b6c74 296 return -EINVAL;
657b306a 297
09a0168d
GC
298 phy_data = (struct usb_phy_data *)of_id->data;
299
657b306a 300 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
0b68253d 301 if (!phy)
657b306a 302 return -ENOMEM;
657b306a
KVA
303
304 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
0b68253d 305 if (!otg)
657b306a 306 return -ENOMEM;
657b306a
KVA
307
308 phy->dev = &pdev->dev;
309
310 phy->phy.dev = phy->dev;
09a0168d 311 phy->phy.label = phy_data->label;
657b306a 312 phy->phy.otg = otg;
c11747f6 313 phy->phy.type = USB_PHY_TYPE_USB2;
9955a783
KVA
314 phy->mask = phy_data->mask;
315 phy->power_on = phy_data->power_on;
316 phy->power_off = phy_data->power_off;
657b306a 317
7e472402
AB
318 if (phy_data->flags & OMAP_USB2_CALIBRATE_FALSE_DISCONNECT) {
319 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
320 phy->phy_base = devm_ioremap_resource(&pdev->dev, res);
3df9fcd5
HS
321 if (IS_ERR(phy->phy_base))
322 return PTR_ERR(phy->phy_base);
7e472402
AB
323 phy->flags |= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT;
324 }
325
9955a783
KVA
326 phy->syscon_phy_power = syscon_regmap_lookup_by_phandle(node,
327 "syscon-phy-power");
328 if (IS_ERR(phy->syscon_phy_power)) {
329 dev_dbg(&pdev->dev,
330 "can't get syscon-phy-power, using control device\n");
331 phy->syscon_phy_power = NULL;
332
333 control_node = of_parse_phandle(node, "ctrl-module", 0);
334 if (!control_node) {
335 dev_err(&pdev->dev,
336 "Failed to get control device phandle\n");
337 return -EINVAL;
338 }
339
340 control_pdev = of_find_device_by_node(control_node);
341 if (!control_pdev) {
342 dev_err(&pdev->dev, "Failed to get control device\n");
343 return -EINVAL;
344 }
345 phy->control_dev = &control_pdev->dev;
346 } else {
347 if (of_property_read_u32_index(node,
348 "syscon-phy-power", 1,
349 &phy->power_reg)) {
350 dev_err(&pdev->dev,
351 "couldn't get power reg. offset\n");
352 return -EINVAL;
353 }
657b306a
KVA
354 }
355
64fe1891 356
9f1d8ed2 357 phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
657b306a 358 if (IS_ERR(phy->wkupclk)) {
ed31ee7c
RQ
359 if (PTR_ERR(phy->wkupclk) == -EPROBE_DEFER)
360 return -EPROBE_DEFER;
361
362 dev_warn(&pdev->dev, "unable to get wkupclk %ld, trying old name\n",
363 PTR_ERR(phy->wkupclk));
9f1d8ed2 364 phy->wkupclk = devm_clk_get(phy->dev, "usb_phy_cm_clk32k");
ed31ee7c 365
9f1d8ed2 366 if (IS_ERR(phy->wkupclk)) {
ed31ee7c
RQ
367 if (PTR_ERR(phy->wkupclk) != -EPROBE_DEFER)
368 dev_err(&pdev->dev, "unable to get usb_phy_cm_clk32k\n");
9f1d8ed2
RQ
369 return PTR_ERR(phy->wkupclk);
370 } else {
371 dev_warn(&pdev->dev,
372 "found usb_phy_cm_clk32k, please fix DTS\n");
373 }
657b306a 374 }
657b306a 375
9f1d8ed2
RQ
376 phy->optclk = devm_clk_get(phy->dev, "refclk");
377 if (IS_ERR(phy->optclk)) {
ed31ee7c
RQ
378 if (PTR_ERR(phy->optclk) == -EPROBE_DEFER)
379 return -EPROBE_DEFER;
380
9f1d8ed2
RQ
381 dev_dbg(&pdev->dev, "unable to get refclk, trying old name\n");
382 phy->optclk = devm_clk_get(phy->dev, "usb_otg_ss_refclk960m");
ed31ee7c 383
9f1d8ed2 384 if (IS_ERR(phy->optclk)) {
ed31ee7c
RQ
385 if (PTR_ERR(phy->optclk) != -EPROBE_DEFER) {
386 dev_dbg(&pdev->dev,
387 "unable to get usb_otg_ss_refclk960m\n");
388 }
9f1d8ed2
RQ
389 } else {
390 dev_warn(&pdev->dev,
391 "found usb_otg_ss_refclk960m, please fix DTS\n");
392 }
9f1d8ed2 393 }
0c4c8bbb 394
ed31ee7c
RQ
395 otg->set_host = omap_usb_set_host;
396 otg->set_peripheral = omap_usb_set_peripheral;
397 if (phy_data->flags & OMAP_USB2_HAS_SET_VBUS)
398 otg->set_vbus = omap_usb_set_vbus;
399 if (phy_data->flags & OMAP_USB2_HAS_START_SRP)
400 otg->start_srp = omap_usb_start_srp;
401 otg->usb_phy = &phy->phy;
402
403 platform_set_drvdata(pdev, phy);
404 pm_runtime_enable(phy->dev);
405
406 generic_phy = devm_phy_create(phy->dev, NULL, &ops);
407 if (IS_ERR(generic_phy)) {
408 pm_runtime_disable(phy->dev);
409 return PTR_ERR(generic_phy);
410 }
411
412 phy_set_drvdata(generic_phy, phy);
413 omap_usb_power_off(generic_phy);
414
415 phy_provider = devm_of_phy_provider_register(phy->dev,
416 of_phy_simple_xlate);
417 if (IS_ERR(phy_provider)) {
418 pm_runtime_disable(phy->dev);
419 return PTR_ERR(phy_provider);
420 }
421
b1ff3231 422
c11747f6 423 usb_add_phy_dev(&phy->phy);
657b306a 424
657b306a
KVA
425 return 0;
426}
427
fb4e98ab 428static int omap_usb2_remove(struct platform_device *pdev)
657b306a
KVA
429{
430 struct omap_usb *phy = platform_get_drvdata(pdev);
431
657b306a 432 usb_remove_phy(&phy->phy);
eb82a3d8 433 pm_runtime_disable(phy->dev);
657b306a
KVA
434
435 return 0;
436}
437
657b306a
KVA
438static struct platform_driver omap_usb2_driver = {
439 .probe = omap_usb2_probe,
7690417d 440 .remove = omap_usb2_remove,
657b306a
KVA
441 .driver = {
442 .name = "omap-usb2",
52a4a72a 443 .of_match_table = omap_usb2_id_table,
657b306a
KVA
444 },
445};
446
447module_platform_driver(omap_usb2_driver);
448
dd64ad38 449MODULE_ALIAS("platform:omap_usb2");
657b306a
KVA
450MODULE_AUTHOR("Texas Instruments Inc.");
451MODULE_DESCRIPTION("OMAP USB2 phy driver");
452MODULE_LICENSE("GPL v2");