1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/module.h>
4 #include <linux/platform_device.h>
5 #include <linux/regmap.h>
7 #include <linux/slab.h>
10 #include <linux/usb/phy_companion.h>
11 #include <linux/clk.h>
12 #include <linux/err.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/delay.h>
15 #include <linux/phy/phy.h>
17 #include <linux/mfd/syscon.h>
20 * TRM has two sets of USB_CTRL registers.. The correct register bits
21 * are in TRM section 24.9.8.2 USB_CTRL Register. The TRM documents the
22 * phy as being SR70LX Synopsys USB 2.0 OTG nanoPHY. It also seems at
23 * least dm816x rev c ignores writes to USB_CTRL register, but the TI
24 * kernel is writing to those so it's possible that later revisions
25 * have worknig USB_CTRL register.
27 * Also note that At least USB_CTRL register seems to be dm816x specific
28 * according to the TRM. It's possible that USBPHY_CTRL is more generic,
29 * but that would have to be checked against the SR70LX documentation
30 * which does not seem to be publicly available.
32 * Finally, the phy on dm814x and am335x is different from dm816x.
34 #define DM816X_USB_CTRL_PHYCLKSRC BIT(8) /* 1 = PLL ref clock */
35 #define DM816X_USB_CTRL_PHYSLEEP1 BIT(1) /* Enable the first phy */
36 #define DM816X_USB_CTRL_PHYSLEEP0 BIT(0) /* Enable the second phy */
38 #define DM816X_USBPHY_CTRL_TXRISETUNE 1
39 #define DM816X_USBPHY_CTRL_TXVREFTUNE 0xc
40 #define DM816X_USBPHY_CTRL_TXPREEMTUNE 0x2
42 struct dm816x_usb_phy {
43 struct regmap *syscon;
45 unsigned int instance;
48 unsigned int usb_ctrl; /* Shared between phy0 and phy1 */
49 unsigned int usbphy_ctrl;
52 static int dm816x_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
56 otg->state = OTG_STATE_UNDEFINED;
61 static int dm816x_usb_phy_set_peripheral(struct usb_otg *otg,
62 struct usb_gadget *gadget)
66 otg->state = OTG_STATE_UNDEFINED;
71 static int dm816x_usb_phy_init(struct phy *x)
73 struct dm816x_usb_phy *phy = phy_get_drvdata(x);
76 if (clk_get_rate(phy->refclk) != 24000000)
77 dev_warn(phy->dev, "nonstandard phy refclk\n");
79 /* Set PLL ref clock and put phys to sleep */
80 regmap_update_bits(phy->syscon, phy->usb_ctrl,
81 DM816X_USB_CTRL_PHYCLKSRC |
82 DM816X_USB_CTRL_PHYSLEEP1 |
83 DM816X_USB_CTRL_PHYSLEEP0,
85 regmap_read(phy->syscon, phy->usb_ctrl, &val);
88 "Working dm816x USB_CTRL! (0x%08x)\n",
92 * TI kernel sets these values for "symmetrical eye diagram and
93 * better signal quality" so let's assume somebody checked the
94 * values with a scope and set them here too.
96 regmap_read(phy->syscon, phy->usbphy_ctrl, &val);
97 val |= DM816X_USBPHY_CTRL_TXRISETUNE |
98 DM816X_USBPHY_CTRL_TXVREFTUNE |
99 DM816X_USBPHY_CTRL_TXPREEMTUNE;
100 regmap_write(phy->syscon, phy->usbphy_ctrl, val);
105 static const struct phy_ops ops = {
106 .init = dm816x_usb_phy_init,
107 .owner = THIS_MODULE,
110 static int __maybe_unused dm816x_usb_phy_runtime_suspend(struct device *dev)
112 struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
113 unsigned int mask, val;
116 mask = BIT(phy->instance);
117 val = ~BIT(phy->instance);
118 error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
121 dev_err(phy->dev, "phy%i failed to power off\n",
123 clk_disable(phy->refclk);
128 static int __maybe_unused dm816x_usb_phy_runtime_resume(struct device *dev)
130 struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
131 unsigned int mask, val;
134 error = clk_enable(phy->refclk);
139 * Note that at least dm816x rev c does not seem to do
140 * anything with the USB_CTRL register. But let's follow
141 * what the TI tree is doing in case later revisions use
144 mask = BIT(phy->instance);
145 val = BIT(phy->instance);
146 error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
149 dev_err(phy->dev, "phy%i failed to power on\n",
151 clk_disable(phy->refclk);
158 static UNIVERSAL_DEV_PM_OPS(dm816x_usb_phy_pm_ops,
159 dm816x_usb_phy_runtime_suspend,
160 dm816x_usb_phy_runtime_resume,
163 static const struct of_device_id dm816x_usb_phy_id_table[] = {
165 .compatible = "ti,dm8168-usb-phy",
169 MODULE_DEVICE_TABLE(of, dm816x_usb_phy_id_table);
171 static int dm816x_usb_phy_probe(struct platform_device *pdev)
173 struct dm816x_usb_phy *phy;
174 struct resource *res;
175 struct phy *generic_phy;
176 struct phy_provider *phy_provider;
180 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
184 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
188 phy->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
190 if (IS_ERR(phy->syscon))
191 return PTR_ERR(phy->syscon);
194 * According to sprs614e.pdf, the first usb_ctrl is shared and
195 * the second instance for usb_ctrl is reserved.. Also the
196 * register bits are different from earlier TRMs.
198 phy->usb_ctrl = 0x20;
199 phy->usbphy_ctrl = (res->start & 0xff) + 4;
200 if (phy->usbphy_ctrl == 0x2c)
203 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
207 phy->dev = &pdev->dev;
208 phy->phy.dev = phy->dev;
209 phy->phy.label = "dm8168_usb_phy";
211 phy->phy.type = USB_PHY_TYPE_USB2;
212 otg->set_host = dm816x_usb_phy_set_host;
213 otg->set_peripheral = dm816x_usb_phy_set_peripheral;
214 otg->usb_phy = &phy->phy;
216 platform_set_drvdata(pdev, phy);
218 phy->refclk = devm_clk_get(phy->dev, "refclk");
219 if (IS_ERR(phy->refclk))
220 return PTR_ERR(phy->refclk);
221 error = clk_prepare(phy->refclk);
225 pm_runtime_enable(phy->dev);
226 generic_phy = devm_phy_create(phy->dev, NULL, &ops);
227 if (IS_ERR(generic_phy)) {
228 error = PTR_ERR(generic_phy);
232 phy_set_drvdata(generic_phy, phy);
234 phy_provider = devm_of_phy_provider_register(phy->dev,
235 of_phy_simple_xlate);
236 if (IS_ERR(phy_provider)) {
237 error = PTR_ERR(phy_provider);
241 usb_add_phy_dev(&phy->phy);
246 pm_runtime_disable(phy->dev);
247 clk_unprepare(phy->refclk);
251 static void dm816x_usb_phy_remove(struct platform_device *pdev)
253 struct dm816x_usb_phy *phy = platform_get_drvdata(pdev);
255 usb_remove_phy(&phy->phy);
256 pm_runtime_disable(phy->dev);
257 clk_unprepare(phy->refclk);
260 static struct platform_driver dm816x_usb_phy_driver = {
261 .probe = dm816x_usb_phy_probe,
262 .remove_new = dm816x_usb_phy_remove,
264 .name = "dm816x-usb-phy",
265 .pm = &dm816x_usb_phy_pm_ops,
266 .of_match_table = dm816x_usb_phy_id_table,
270 module_platform_driver(dm816x_usb_phy_driver);
272 MODULE_ALIAS("platform:dm816x_usb");
273 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
274 MODULE_DESCRIPTION("dm816x usb phy driver");
275 MODULE_LICENSE("GPL v2");