xref: /OK3568_Linux_fs/kernel/drivers/phy/phy-lpc18xx-usb-otg.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * PHY driver for NXP LPC18xx/43xx internal USB OTG PHY
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/clk.h>
9*4882a593Smuzhiyun #include <linux/err.h>
10*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/of.h>
13*4882a593Smuzhiyun #include <linux/phy/phy.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun #include <linux/regmap.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /* USB OTG PHY register offset and bit in CREG */
18*4882a593Smuzhiyun #define LPC18XX_CREG_CREG0		0x004
19*4882a593Smuzhiyun #define LPC18XX_CREG_CREG0_USB0PHY	BIT(5)
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun struct lpc18xx_usb_otg_phy {
22*4882a593Smuzhiyun 	struct phy *phy;
23*4882a593Smuzhiyun 	struct clk *clk;
24*4882a593Smuzhiyun 	struct regmap *reg;
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
lpc18xx_usb_otg_phy_init(struct phy * phy)27*4882a593Smuzhiyun static int lpc18xx_usb_otg_phy_init(struct phy *phy)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
30*4882a593Smuzhiyun 	int ret;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	/* The PHY must be clocked at 480 MHz */
33*4882a593Smuzhiyun 	ret = clk_set_rate(lpc->clk, 480000000);
34*4882a593Smuzhiyun 	if (ret)
35*4882a593Smuzhiyun 		return ret;
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	return clk_prepare(lpc->clk);
38*4882a593Smuzhiyun }
39*4882a593Smuzhiyun 
lpc18xx_usb_otg_phy_exit(struct phy * phy)40*4882a593Smuzhiyun static int lpc18xx_usb_otg_phy_exit(struct phy *phy)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	clk_unprepare(lpc->clk);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	return 0;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
lpc18xx_usb_otg_phy_power_on(struct phy * phy)49*4882a593Smuzhiyun static int lpc18xx_usb_otg_phy_power_on(struct phy *phy)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
52*4882a593Smuzhiyun 	int ret;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	ret = clk_enable(lpc->clk);
55*4882a593Smuzhiyun 	if (ret)
56*4882a593Smuzhiyun 		return ret;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	/* The bit in CREG is cleared to enable the PHY */
59*4882a593Smuzhiyun 	ret = regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
60*4882a593Smuzhiyun 				  LPC18XX_CREG_CREG0_USB0PHY, 0);
61*4882a593Smuzhiyun 	if (ret) {
62*4882a593Smuzhiyun 		clk_disable(lpc->clk);
63*4882a593Smuzhiyun 		return ret;
64*4882a593Smuzhiyun 	}
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	return 0;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
lpc18xx_usb_otg_phy_power_off(struct phy * phy)69*4882a593Smuzhiyun static int lpc18xx_usb_otg_phy_power_off(struct phy *phy)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
72*4882a593Smuzhiyun 	int ret;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	ret = regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
75*4882a593Smuzhiyun 				 LPC18XX_CREG_CREG0_USB0PHY,
76*4882a593Smuzhiyun 				 LPC18XX_CREG_CREG0_USB0PHY);
77*4882a593Smuzhiyun 	if (ret)
78*4882a593Smuzhiyun 		return ret;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	clk_disable(lpc->clk);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	return 0;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun static const struct phy_ops lpc18xx_usb_otg_phy_ops = {
86*4882a593Smuzhiyun 	.init		= lpc18xx_usb_otg_phy_init,
87*4882a593Smuzhiyun 	.exit		= lpc18xx_usb_otg_phy_exit,
88*4882a593Smuzhiyun 	.power_on	= lpc18xx_usb_otg_phy_power_on,
89*4882a593Smuzhiyun 	.power_off	= lpc18xx_usb_otg_phy_power_off,
90*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
91*4882a593Smuzhiyun };
92*4882a593Smuzhiyun 
lpc18xx_usb_otg_phy_probe(struct platform_device * pdev)93*4882a593Smuzhiyun static int lpc18xx_usb_otg_phy_probe(struct platform_device *pdev)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun 	struct phy_provider *phy_provider;
96*4882a593Smuzhiyun 	struct lpc18xx_usb_otg_phy *lpc;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	lpc = devm_kzalloc(&pdev->dev, sizeof(*lpc), GFP_KERNEL);
99*4882a593Smuzhiyun 	if (!lpc)
100*4882a593Smuzhiyun 		return -ENOMEM;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	lpc->reg = syscon_node_to_regmap(pdev->dev.of_node->parent);
103*4882a593Smuzhiyun 	if (IS_ERR(lpc->reg)) {
104*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to get syscon\n");
105*4882a593Smuzhiyun 		return PTR_ERR(lpc->reg);
106*4882a593Smuzhiyun 	}
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	lpc->clk = devm_clk_get(&pdev->dev, NULL);
109*4882a593Smuzhiyun 	if (IS_ERR(lpc->clk)) {
110*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to get clock\n");
111*4882a593Smuzhiyun 		return PTR_ERR(lpc->clk);
112*4882a593Smuzhiyun 	}
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	lpc->phy = devm_phy_create(&pdev->dev, NULL, &lpc18xx_usb_otg_phy_ops);
115*4882a593Smuzhiyun 	if (IS_ERR(lpc->phy)) {
116*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to create PHY\n");
117*4882a593Smuzhiyun 		return PTR_ERR(lpc->phy);
118*4882a593Smuzhiyun 	}
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun 	phy_set_drvdata(lpc->phy, lpc);
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	phy_provider = devm_of_phy_provider_register(&pdev->dev,
123*4882a593Smuzhiyun 						     of_phy_simple_xlate);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	return PTR_ERR_OR_ZERO(phy_provider);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun static const struct of_device_id lpc18xx_usb_otg_phy_match[] = {
129*4882a593Smuzhiyun 	{ .compatible = "nxp,lpc1850-usb-otg-phy" },
130*4882a593Smuzhiyun 	{ }
131*4882a593Smuzhiyun };
132*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, lpc18xx_usb_otg_phy_match);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun static struct platform_driver lpc18xx_usb_otg_phy_driver = {
135*4882a593Smuzhiyun 	.probe		= lpc18xx_usb_otg_phy_probe,
136*4882a593Smuzhiyun 	.driver		= {
137*4882a593Smuzhiyun 		.name	= "lpc18xx-usb-otg-phy",
138*4882a593Smuzhiyun 		.of_match_table = lpc18xx_usb_otg_phy_match,
139*4882a593Smuzhiyun 	},
140*4882a593Smuzhiyun };
141*4882a593Smuzhiyun module_platform_driver(lpc18xx_usb_otg_phy_driver);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
144*4882a593Smuzhiyun MODULE_DESCRIPTION("NXP LPC18xx/43xx USB OTG PHY driver");
145*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
146