1*f0c6d776SInochi Amaoto // SPDX-License-Identifier: GPL-2.0
2*f0c6d776SInochi Amaoto /*
3*f0c6d776SInochi Amaoto * Copyright (C) 2025 Inochi Amaoto <inochiama@outlook.com>
4*f0c6d776SInochi Amaoto */
5*f0c6d776SInochi Amaoto
6*f0c6d776SInochi Amaoto #include <linux/clk.h>
7*f0c6d776SInochi Amaoto #include <linux/bitfield.h>
8*f0c6d776SInochi Amaoto #include <linux/debugfs.h>
9*f0c6d776SInochi Amaoto #include <linux/kernel.h>
10*f0c6d776SInochi Amaoto #include <linux/mfd/syscon.h>
11*f0c6d776SInochi Amaoto #include <linux/module.h>
12*f0c6d776SInochi Amaoto #include <linux/of.h>
13*f0c6d776SInochi Amaoto #include <linux/of_address.h>
14*f0c6d776SInochi Amaoto #include <linux/of_gpio.h>
15*f0c6d776SInochi Amaoto #include <linux/platform_device.h>
16*f0c6d776SInochi Amaoto #include <linux/phy/phy.h>
17*f0c6d776SInochi Amaoto #include <linux/regmap.h>
18*f0c6d776SInochi Amaoto #include <linux/spinlock.h>
19*f0c6d776SInochi Amaoto
20*f0c6d776SInochi Amaoto #define REG_USB_PHY_CTRL 0x048
21*f0c6d776SInochi Amaoto
22*f0c6d776SInochi Amaoto #define PHY_VBUS_POWER_EN BIT(0)
23*f0c6d776SInochi Amaoto #define PHY_VBUS_POWER BIT(1)
24*f0c6d776SInochi Amaoto #define PHY_ID_OVERWRITE_EN BIT(6)
25*f0c6d776SInochi Amaoto #define PHY_ID_OVERWRITE_MODE BIT(7)
26*f0c6d776SInochi Amaoto #define PHY_ID_OVERWRITE_MODE_HOST FIELD_PREP(BIT(7), 0)
27*f0c6d776SInochi Amaoto #define PHY_ID_OVERWRITE_MODE_DEVICE FIELD_PREP(BIT(7), 1)
28*f0c6d776SInochi Amaoto
29*f0c6d776SInochi Amaoto #define PHY_APP_CLK_RATE 125000000
30*f0c6d776SInochi Amaoto #define PHY_LPM_CLK_RATE 12000000
31*f0c6d776SInochi Amaoto #define PHY_STB_CLK_RATE 333334
32*f0c6d776SInochi Amaoto
33*f0c6d776SInochi Amaoto struct cv1800_usb_phy {
34*f0c6d776SInochi Amaoto struct phy *phy;
35*f0c6d776SInochi Amaoto struct regmap *syscon;
36*f0c6d776SInochi Amaoto spinlock_t lock;
37*f0c6d776SInochi Amaoto struct clk *usb_app_clk;
38*f0c6d776SInochi Amaoto struct clk *usb_lpm_clk;
39*f0c6d776SInochi Amaoto struct clk *usb_stb_clk;
40*f0c6d776SInochi Amaoto bool support_otg;
41*f0c6d776SInochi Amaoto };
42*f0c6d776SInochi Amaoto
cv1800_usb_phy_set_mode(struct phy * _phy,enum phy_mode mode,int submode)43*f0c6d776SInochi Amaoto static int cv1800_usb_phy_set_mode(struct phy *_phy,
44*f0c6d776SInochi Amaoto enum phy_mode mode, int submode)
45*f0c6d776SInochi Amaoto {
46*f0c6d776SInochi Amaoto struct cv1800_usb_phy *phy = phy_get_drvdata(_phy);
47*f0c6d776SInochi Amaoto unsigned int regval = 0;
48*f0c6d776SInochi Amaoto int ret;
49*f0c6d776SInochi Amaoto
50*f0c6d776SInochi Amaoto dev_info(&phy->phy->dev, "set mode %d", (int)mode);
51*f0c6d776SInochi Amaoto
52*f0c6d776SInochi Amaoto switch (mode) {
53*f0c6d776SInochi Amaoto case PHY_MODE_USB_DEVICE:
54*f0c6d776SInochi Amaoto regval = PHY_ID_OVERWRITE_EN | PHY_ID_OVERWRITE_MODE_DEVICE;
55*f0c6d776SInochi Amaoto regmap_clear_bits(phy->syscon, REG_USB_PHY_CTRL, PHY_VBUS_POWER);
56*f0c6d776SInochi Amaoto break;
57*f0c6d776SInochi Amaoto case PHY_MODE_USB_HOST:
58*f0c6d776SInochi Amaoto regval = PHY_ID_OVERWRITE_EN | PHY_ID_OVERWRITE_MODE_HOST;
59*f0c6d776SInochi Amaoto regmap_set_bits(phy->syscon, REG_USB_PHY_CTRL, PHY_VBUS_POWER);
60*f0c6d776SInochi Amaoto break;
61*f0c6d776SInochi Amaoto case PHY_MODE_USB_OTG:
62*f0c6d776SInochi Amaoto if (!phy->support_otg)
63*f0c6d776SInochi Amaoto return 0;
64*f0c6d776SInochi Amaoto
65*f0c6d776SInochi Amaoto ret = regmap_read(phy->syscon, REG_USB_PHY_CTRL, ®val);
66*f0c6d776SInochi Amaoto if (ret)
67*f0c6d776SInochi Amaoto return ret;
68*f0c6d776SInochi Amaoto
69*f0c6d776SInochi Amaoto regval = FIELD_GET(PHY_ID_OVERWRITE_MODE, regval);
70*f0c6d776SInochi Amaoto break;
71*f0c6d776SInochi Amaoto default:
72*f0c6d776SInochi Amaoto return -EINVAL;
73*f0c6d776SInochi Amaoto }
74*f0c6d776SInochi Amaoto
75*f0c6d776SInochi Amaoto return regmap_update_bits(phy->syscon, REG_USB_PHY_CTRL,
76*f0c6d776SInochi Amaoto PHY_ID_OVERWRITE_EN | PHY_ID_OVERWRITE_MODE,
77*f0c6d776SInochi Amaoto regval);
78*f0c6d776SInochi Amaoto }
79*f0c6d776SInochi Amaoto
cv1800_usb_phy_set_clock(struct cv1800_usb_phy * phy)80*f0c6d776SInochi Amaoto static int cv1800_usb_phy_set_clock(struct cv1800_usb_phy *phy)
81*f0c6d776SInochi Amaoto {
82*f0c6d776SInochi Amaoto int ret;
83*f0c6d776SInochi Amaoto
84*f0c6d776SInochi Amaoto ret = clk_set_rate(phy->usb_app_clk, PHY_APP_CLK_RATE);
85*f0c6d776SInochi Amaoto if (ret)
86*f0c6d776SInochi Amaoto return ret;
87*f0c6d776SInochi Amaoto
88*f0c6d776SInochi Amaoto ret = clk_set_rate(phy->usb_lpm_clk, PHY_LPM_CLK_RATE);
89*f0c6d776SInochi Amaoto if (ret)
90*f0c6d776SInochi Amaoto return ret;
91*f0c6d776SInochi Amaoto
92*f0c6d776SInochi Amaoto return clk_set_rate(phy->usb_stb_clk, PHY_STB_CLK_RATE);
93*f0c6d776SInochi Amaoto }
94*f0c6d776SInochi Amaoto
95*f0c6d776SInochi Amaoto static const struct phy_ops cv1800_usb_phy_ops = {
96*f0c6d776SInochi Amaoto .set_mode = cv1800_usb_phy_set_mode,
97*f0c6d776SInochi Amaoto .owner = THIS_MODULE,
98*f0c6d776SInochi Amaoto };
99*f0c6d776SInochi Amaoto
cv1800_usb_phy_probe(struct platform_device * pdev)100*f0c6d776SInochi Amaoto static int cv1800_usb_phy_probe(struct platform_device *pdev)
101*f0c6d776SInochi Amaoto {
102*f0c6d776SInochi Amaoto struct device *dev = &pdev->dev;
103*f0c6d776SInochi Amaoto struct device *parent = dev->parent;
104*f0c6d776SInochi Amaoto struct cv1800_usb_phy *phy;
105*f0c6d776SInochi Amaoto struct phy_provider *phy_provider;
106*f0c6d776SInochi Amaoto int ret;
107*f0c6d776SInochi Amaoto
108*f0c6d776SInochi Amaoto if (!parent)
109*f0c6d776SInochi Amaoto return -ENODEV;
110*f0c6d776SInochi Amaoto
111*f0c6d776SInochi Amaoto phy = devm_kmalloc(dev, sizeof(*phy), GFP_KERNEL);
112*f0c6d776SInochi Amaoto if (!phy)
113*f0c6d776SInochi Amaoto return -ENOMEM;
114*f0c6d776SInochi Amaoto
115*f0c6d776SInochi Amaoto phy->syscon = syscon_node_to_regmap(parent->of_node);
116*f0c6d776SInochi Amaoto if (IS_ERR_OR_NULL(phy->syscon))
117*f0c6d776SInochi Amaoto return -ENODEV;
118*f0c6d776SInochi Amaoto
119*f0c6d776SInochi Amaoto phy->support_otg = false;
120*f0c6d776SInochi Amaoto
121*f0c6d776SInochi Amaoto spin_lock_init(&phy->lock);
122*f0c6d776SInochi Amaoto
123*f0c6d776SInochi Amaoto phy->usb_app_clk = devm_clk_get_enabled(dev, "app");
124*f0c6d776SInochi Amaoto if (IS_ERR(phy->usb_app_clk))
125*f0c6d776SInochi Amaoto return dev_err_probe(dev, PTR_ERR(phy->usb_app_clk),
126*f0c6d776SInochi Amaoto "Failed to get app clock\n");
127*f0c6d776SInochi Amaoto
128*f0c6d776SInochi Amaoto phy->usb_lpm_clk = devm_clk_get_enabled(dev, "lpm");
129*f0c6d776SInochi Amaoto if (IS_ERR(phy->usb_lpm_clk))
130*f0c6d776SInochi Amaoto return dev_err_probe(dev, PTR_ERR(phy->usb_lpm_clk),
131*f0c6d776SInochi Amaoto "Failed to get lpm clock\n");
132*f0c6d776SInochi Amaoto
133*f0c6d776SInochi Amaoto phy->usb_stb_clk = devm_clk_get_enabled(dev, "stb");
134*f0c6d776SInochi Amaoto if (IS_ERR(phy->usb_stb_clk))
135*f0c6d776SInochi Amaoto return dev_err_probe(dev, PTR_ERR(phy->usb_stb_clk),
136*f0c6d776SInochi Amaoto "Failed to get stb clock\n");
137*f0c6d776SInochi Amaoto
138*f0c6d776SInochi Amaoto phy->phy = devm_phy_create(dev, NULL, &cv1800_usb_phy_ops);
139*f0c6d776SInochi Amaoto if (IS_ERR(phy->phy))
140*f0c6d776SInochi Amaoto return dev_err_probe(dev, PTR_ERR(phy->phy),
141*f0c6d776SInochi Amaoto "Failed to create phy\n");
142*f0c6d776SInochi Amaoto
143*f0c6d776SInochi Amaoto ret = cv1800_usb_phy_set_clock(phy);
144*f0c6d776SInochi Amaoto if (ret)
145*f0c6d776SInochi Amaoto return ret;
146*f0c6d776SInochi Amaoto
147*f0c6d776SInochi Amaoto phy_set_drvdata(phy->phy, phy);
148*f0c6d776SInochi Amaoto phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
149*f0c6d776SInochi Amaoto
150*f0c6d776SInochi Amaoto return PTR_ERR_OR_ZERO(phy_provider);
151*f0c6d776SInochi Amaoto }
152*f0c6d776SInochi Amaoto
153*f0c6d776SInochi Amaoto static const struct of_device_id cv1800_usb_phy_ids[] = {
154*f0c6d776SInochi Amaoto { .compatible = "sophgo,cv1800b-usb2-phy" },
155*f0c6d776SInochi Amaoto { },
156*f0c6d776SInochi Amaoto };
157*f0c6d776SInochi Amaoto MODULE_DEVICE_TABLE(of, cv1800_usb_phy_ids);
158*f0c6d776SInochi Amaoto
159*f0c6d776SInochi Amaoto static struct platform_driver cv1800_usb_phy_driver = {
160*f0c6d776SInochi Amaoto .probe = cv1800_usb_phy_probe,
161*f0c6d776SInochi Amaoto .driver = {
162*f0c6d776SInochi Amaoto .name = "cv1800-usb2-phy",
163*f0c6d776SInochi Amaoto .of_match_table = cv1800_usb_phy_ids,
164*f0c6d776SInochi Amaoto },
165*f0c6d776SInochi Amaoto };
166*f0c6d776SInochi Amaoto module_platform_driver(cv1800_usb_phy_driver);
167*f0c6d776SInochi Amaoto
168*f0c6d776SInochi Amaoto MODULE_AUTHOR("Inochi Amaoto <inochiama@outlook.com>");
169*f0c6d776SInochi Amaoto MODULE_DESCRIPTION("CV1800/SG2000 SoC USB 2.0 PHY driver");
170*f0c6d776SInochi Amaoto MODULE_LICENSE("GPL");
171