1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * omap-usb2.c - USB PHY, talking to USB controller on TI SoCs.
5 * Copyright (C) 2012-2020 Texas Instruments Incorporated - http://www.ti.com
6 * Author: Kishon Vijay Abraham I <kishon@ti.com>
10 #include <linux/delay.h>
11 #include <linux/err.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/module.h>
16 #include <linux/of_platform.h>
17 #include <linux/phy/omap_control_phy.h>
18 #include <linux/phy/omap_usb.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/property.h>
23 #include <linux/regmap.h>
24 #include <linux/slab.h>
25 #include <linux/sys_soc.h>
26 #include <linux/usb/phy_companion.h>
28 #define USB2PHY_ANA_CONFIG1 0x4c
29 #define USB2PHY_DISCON_BYP_LATCH BIT(31)
31 #define USB2PHY_CHRG_DET 0x14
32 #define USB2PHY_CHRG_DET_USE_CHG_DET_REG BIT(29)
33 #define USB2PHY_CHRG_DET_DIS_CHG_DET BIT(28)
35 /* SoC Specific USB2_OTG register definitions */
36 #define AM654_USB2_OTG_PD BIT(8)
37 #define AM654_USB2_VBUS_DET_EN BIT(5)
38 #define AM654_USB2_VBUSVALID_DET_EN BIT(4)
40 #define OMAP_DEV_PHY_PD BIT(0)
41 #define OMAP_USB2_PHY_PD BIT(28)
43 #define AM437X_USB2_PHY_PD BIT(0)
44 #define AM437X_USB2_OTG_PD BIT(1)
45 #define AM437X_USB2_OTGVDET_EN BIT(19)
46 #define AM437X_USB2_OTGSESSEND_EN BIT(20)
49 #define OMAP_USB2_HAS_START_SRP BIT(0)
50 #define OMAP_USB2_HAS_SET_VBUS BIT(1)
51 #define OMAP_USB2_CALIBRATE_FALSE_DISCONNECT BIT(2)
52 #define OMAP_USB2_DISABLE_CHRG_DET BIT(3)
56 struct phy_companion
*comparator
;
57 void __iomem
*pll_ctrl_base
;
58 void __iomem
*phy_base
;
60 struct device
*control_dev
;
64 struct regmap
*syscon_phy_power
; /* ctrl. reg. acces */
65 unsigned int power_reg
; /* power reg. index within syscon */
71 #define phy_to_omapusb(x) container_of((x), struct omap_usb, phy)
81 static inline u32
omap_usb_readl(void __iomem
*addr
, unsigned int offset
)
83 return __raw_readl(addr
+ offset
);
86 static inline void omap_usb_writel(void __iomem
*addr
, unsigned int offset
,
89 __raw_writel(data
, addr
+ offset
);
93 * omap_usb2_set_comparator() - links the comparator present in the system with this phy
95 * @comparator: the companion phy(comparator) for this phy
97 * The phy companion driver should call this API passing the phy_companion
98 * filled with set_vbus and start_srp to be used by usb phy.
100 * For use by phy companion driver
102 int omap_usb2_set_comparator(struct phy_companion
*comparator
)
104 struct omap_usb
*phy
;
105 struct usb_phy
*x
= usb_get_phy(USB_PHY_TYPE_USB2
);
110 phy
= phy_to_omapusb(x
);
111 phy
->comparator
= comparator
;
114 EXPORT_SYMBOL_GPL(omap_usb2_set_comparator
);
116 static int omap_usb_set_vbus(struct usb_otg
*otg
, bool enabled
)
118 struct omap_usb
*phy
= phy_to_omapusb(otg
->usb_phy
);
120 if (!phy
->comparator
|| !phy
->comparator
->set_vbus
)
123 return phy
->comparator
->set_vbus(phy
->comparator
, enabled
);
126 static int omap_usb_start_srp(struct usb_otg
*otg
)
128 struct omap_usb
*phy
= phy_to_omapusb(otg
->usb_phy
);
130 if (!phy
->comparator
|| !phy
->comparator
->start_srp
)
133 return phy
->comparator
->start_srp(phy
->comparator
);
136 static int omap_usb_set_host(struct usb_otg
*otg
, struct usb_bus
*host
)
140 otg
->state
= OTG_STATE_UNDEFINED
;
145 static int omap_usb_set_peripheral(struct usb_otg
*otg
,
146 struct usb_gadget
*gadget
)
148 otg
->gadget
= gadget
;
150 otg
->state
= OTG_STATE_UNDEFINED
;
155 static int omap_usb_phy_power(struct omap_usb
*phy
, int on
)
160 if (!phy
->syscon_phy_power
) {
161 omap_control_phy_power(phy
->control_dev
, on
);
168 val
= phy
->power_off
;
170 ret
= regmap_update_bits(phy
->syscon_phy_power
, phy
->power_reg
,
175 static int omap_usb_power_off(struct phy
*x
)
177 struct omap_usb
*phy
= phy_get_drvdata(x
);
179 return omap_usb_phy_power(phy
, false);
182 static int omap_usb_power_on(struct phy
*x
)
184 struct omap_usb
*phy
= phy_get_drvdata(x
);
186 return omap_usb_phy_power(phy
, true);
189 static int omap_usb2_disable_clocks(struct omap_usb
*phy
)
191 clk_disable_unprepare(phy
->wkupclk
);
192 if (!IS_ERR(phy
->optclk
))
193 clk_disable_unprepare(phy
->optclk
);
198 static int omap_usb2_enable_clocks(struct omap_usb
*phy
)
202 ret
= clk_prepare_enable(phy
->wkupclk
);
204 dev_err(phy
->dev
, "Failed to enable wkupclk %d\n", ret
);
208 if (!IS_ERR(phy
->optclk
)) {
209 ret
= clk_prepare_enable(phy
->optclk
);
211 dev_err(phy
->dev
, "Failed to enable optclk %d\n", ret
);
219 clk_disable_unprepare(phy
->wkupclk
);
225 static int omap_usb_init(struct phy
*x
)
227 struct omap_usb
*phy
= phy_get_drvdata(x
);
230 omap_usb2_enable_clocks(phy
);
232 if (phy
->flags
& OMAP_USB2_CALIBRATE_FALSE_DISCONNECT
) {
235 * Reduce the sensitivity of internal PHY by enabling the
236 * DISCON_BYP_LATCH of the USB2PHY_ANA_CONFIG1 register. This
237 * resolves issues with certain devices which can otherwise
238 * be prone to false disconnects.
241 val
= omap_usb_readl(phy
->phy_base
, USB2PHY_ANA_CONFIG1
);
242 val
|= USB2PHY_DISCON_BYP_LATCH
;
243 omap_usb_writel(phy
->phy_base
, USB2PHY_ANA_CONFIG1
, val
);
246 if (phy
->flags
& OMAP_USB2_DISABLE_CHRG_DET
) {
247 val
= omap_usb_readl(phy
->phy_base
, USB2PHY_CHRG_DET
);
248 val
|= USB2PHY_CHRG_DET_USE_CHG_DET_REG
|
249 USB2PHY_CHRG_DET_DIS_CHG_DET
;
250 omap_usb_writel(phy
->phy_base
, USB2PHY_CHRG_DET
, val
);
256 static int omap_usb_exit(struct phy
*x
)
258 struct omap_usb
*phy
= phy_get_drvdata(x
);
260 return omap_usb2_disable_clocks(phy
);
263 static const struct phy_ops ops
= {
264 .init
= omap_usb_init
,
265 .exit
= omap_usb_exit
,
266 .power_on
= omap_usb_power_on
,
267 .power_off
= omap_usb_power_off
,
268 .owner
= THIS_MODULE
,
271 static const struct usb_phy_data omap_usb2_data
= {
272 .label
= "omap_usb2",
273 .flags
= OMAP_USB2_HAS_START_SRP
| OMAP_USB2_HAS_SET_VBUS
,
274 .mask
= OMAP_DEV_PHY_PD
,
275 .power_off
= OMAP_DEV_PHY_PD
,
278 static const struct usb_phy_data omap5_usb2_data
= {
279 .label
= "omap5_usb2",
281 .mask
= OMAP_DEV_PHY_PD
,
282 .power_off
= OMAP_DEV_PHY_PD
,
285 static const struct usb_phy_data dra7x_usb2_data
= {
286 .label
= "dra7x_usb2",
287 .flags
= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT
,
288 .mask
= OMAP_DEV_PHY_PD
,
289 .power_off
= OMAP_DEV_PHY_PD
,
292 static const struct usb_phy_data dra7x_usb2_phy2_data
= {
293 .label
= "dra7x_usb2_phy2",
294 .flags
= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT
,
295 .mask
= OMAP_USB2_PHY_PD
,
296 .power_off
= OMAP_USB2_PHY_PD
,
299 static const struct usb_phy_data am437x_usb2_data
= {
300 .label
= "am437x_usb2",
302 .mask
= AM437X_USB2_PHY_PD
| AM437X_USB2_OTG_PD
|
303 AM437X_USB2_OTGVDET_EN
| AM437X_USB2_OTGSESSEND_EN
,
304 .power_on
= AM437X_USB2_OTGVDET_EN
| AM437X_USB2_OTGSESSEND_EN
,
305 .power_off
= AM437X_USB2_PHY_PD
| AM437X_USB2_OTG_PD
,
308 static const struct usb_phy_data am654_usb2_data
= {
309 .label
= "am654_usb2",
310 .flags
= OMAP_USB2_CALIBRATE_FALSE_DISCONNECT
,
311 .mask
= AM654_USB2_OTG_PD
| AM654_USB2_VBUS_DET_EN
|
312 AM654_USB2_VBUSVALID_DET_EN
,
313 .power_on
= AM654_USB2_VBUS_DET_EN
| AM654_USB2_VBUSVALID_DET_EN
,
314 .power_off
= AM654_USB2_OTG_PD
,
317 static const struct of_device_id omap_usb2_id_table
[] = {
319 .compatible
= "ti,omap-usb2",
320 .data
= &omap_usb2_data
,
323 .compatible
= "ti,omap5-usb2",
324 .data
= &omap5_usb2_data
,
327 .compatible
= "ti,dra7x-usb2",
328 .data
= &dra7x_usb2_data
,
331 .compatible
= "ti,dra7x-usb2-phy2",
332 .data
= &dra7x_usb2_phy2_data
,
335 .compatible
= "ti,am437x-usb2",
336 .data
= &am437x_usb2_data
,
339 .compatible
= "ti,am654-usb2",
340 .data
= &am654_usb2_data
,
344 MODULE_DEVICE_TABLE(of
, omap_usb2_id_table
);
346 static void omap_usb2_init_errata(struct omap_usb
*phy
)
348 static const struct soc_device_attribute am65x_sr10_soc_devices
[] = {
349 { .family
= "AM65X", .revision
= "SR1.0" },
354 * Errata i2075: USB2PHY: USB2PHY Charger Detect is Enabled by
355 * Default Without VBUS Presence.
357 * AM654x SR1.0 has a silicon bug due to which D+ is pulled high after
358 * POR, which could cause enumeration failure with some USB hubs.
359 * Disabling the USB2_PHY Charger Detect function will put D+
360 * into the normal state.
362 if (soc_device_match(am65x_sr10_soc_devices
))
363 phy
->flags
|= OMAP_USB2_DISABLE_CHRG_DET
;
366 static int omap_usb2_probe(struct platform_device
*pdev
)
368 struct omap_usb
*phy
;
369 struct phy
*generic_phy
;
370 struct phy_provider
*phy_provider
;
372 struct device_node
*node
= pdev
->dev
.of_node
;
373 struct device_node
*control_node
;
374 struct platform_device
*control_pdev
;
375 const struct usb_phy_data
*phy_data
;
377 phy_data
= device_get_match_data(&pdev
->dev
);
381 phy
= devm_kzalloc(&pdev
->dev
, sizeof(*phy
), GFP_KERNEL
);
385 otg
= devm_kzalloc(&pdev
->dev
, sizeof(*otg
), GFP_KERNEL
);
389 phy
->dev
= &pdev
->dev
;
391 phy
->phy
.dev
= phy
->dev
;
392 phy
->phy
.label
= phy_data
->label
;
394 phy
->phy
.type
= USB_PHY_TYPE_USB2
;
395 phy
->mask
= phy_data
->mask
;
396 phy
->power_on
= phy_data
->power_on
;
397 phy
->power_off
= phy_data
->power_off
;
398 phy
->flags
= phy_data
->flags
;
400 omap_usb2_init_errata(phy
);
402 phy
->phy_base
= devm_platform_ioremap_resource(pdev
, 0);
403 if (IS_ERR(phy
->phy_base
))
404 return PTR_ERR(phy
->phy_base
);
406 phy
->syscon_phy_power
= syscon_regmap_lookup_by_phandle(node
,
408 if (IS_ERR(phy
->syscon_phy_power
)) {
410 "can't get syscon-phy-power, using control device\n");
411 phy
->syscon_phy_power
= NULL
;
413 control_node
= of_parse_phandle(node
, "ctrl-module", 0);
416 "Failed to get control device phandle\n");
420 control_pdev
= of_find_device_by_node(control_node
);
422 dev_err(&pdev
->dev
, "Failed to get control device\n");
425 phy
->control_dev
= &control_pdev
->dev
;
427 if (of_property_read_u32_index(node
,
428 "syscon-phy-power", 1,
431 "couldn't get power reg. offset\n");
436 phy
->wkupclk
= devm_clk_get(phy
->dev
, "wkupclk");
437 if (IS_ERR(phy
->wkupclk
)) {
438 if (PTR_ERR(phy
->wkupclk
) == -EPROBE_DEFER
)
439 return -EPROBE_DEFER
;
441 dev_warn(&pdev
->dev
, "unable to get wkupclk %ld, trying old name\n",
442 PTR_ERR(phy
->wkupclk
));
443 phy
->wkupclk
= devm_clk_get(phy
->dev
, "usb_phy_cm_clk32k");
445 if (IS_ERR(phy
->wkupclk
))
446 return dev_err_probe(&pdev
->dev
, PTR_ERR(phy
->wkupclk
),
447 "unable to get usb_phy_cm_clk32k\n");
450 "found usb_phy_cm_clk32k, please fix DTS\n");
453 phy
->optclk
= devm_clk_get(phy
->dev
, "refclk");
454 if (IS_ERR(phy
->optclk
)) {
455 if (PTR_ERR(phy
->optclk
) == -EPROBE_DEFER
)
456 return -EPROBE_DEFER
;
458 dev_dbg(&pdev
->dev
, "unable to get refclk, trying old name\n");
459 phy
->optclk
= devm_clk_get(phy
->dev
, "usb_otg_ss_refclk960m");
461 if (IS_ERR(phy
->optclk
)) {
462 if (PTR_ERR(phy
->optclk
) != -EPROBE_DEFER
) {
464 "unable to get usb_otg_ss_refclk960m\n");
468 "found usb_otg_ss_refclk960m, please fix DTS\n");
472 otg
->set_host
= omap_usb_set_host
;
473 otg
->set_peripheral
= omap_usb_set_peripheral
;
474 if (phy_data
->flags
& OMAP_USB2_HAS_SET_VBUS
)
475 otg
->set_vbus
= omap_usb_set_vbus
;
476 if (phy_data
->flags
& OMAP_USB2_HAS_START_SRP
)
477 otg
->start_srp
= omap_usb_start_srp
;
478 otg
->usb_phy
= &phy
->phy
;
480 platform_set_drvdata(pdev
, phy
);
481 pm_runtime_enable(phy
->dev
);
483 generic_phy
= devm_phy_create(phy
->dev
, NULL
, &ops
);
484 if (IS_ERR(generic_phy
)) {
485 pm_runtime_disable(phy
->dev
);
486 return PTR_ERR(generic_phy
);
489 phy_set_drvdata(generic_phy
, phy
);
490 omap_usb_power_off(generic_phy
);
492 phy_provider
= devm_of_phy_provider_register(phy
->dev
,
493 of_phy_simple_xlate
);
494 if (IS_ERR(phy_provider
)) {
495 pm_runtime_disable(phy
->dev
);
496 return PTR_ERR(phy_provider
);
499 usb_add_phy_dev(&phy
->phy
);
504 static void omap_usb2_remove(struct platform_device
*pdev
)
506 struct omap_usb
*phy
= platform_get_drvdata(pdev
);
508 usb_remove_phy(&phy
->phy
);
509 pm_runtime_disable(phy
->dev
);
512 static struct platform_driver omap_usb2_driver
= {
513 .probe
= omap_usb2_probe
,
514 .remove
= omap_usb2_remove
,
517 .of_match_table
= omap_usb2_id_table
,
521 module_platform_driver(omap_usb2_driver
);
523 MODULE_ALIAS("platform:omap_usb2");
524 MODULE_AUTHOR("Texas Instruments Inc.");
525 MODULE_DESCRIPTION("OMAP USB2 phy driver");
526 MODULE_LICENSE("GPL v2");