2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation version 2.
6 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
7 * kind, whether express or implied; without even the implied warranty
8 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
16 #include <linux/slab.h>
19 #include <linux/usb/phy_companion.h>
20 #include <linux/clk.h>
21 #include <linux/err.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/delay.h>
24 #include <linux/phy/phy.h>
25 #include <linux/of_platform.h>
27 #include <linux/mfd/syscon.h>
30 * TRM has two sets of USB_CTRL registers.. The correct register bits
31 * are in TRM section 24.9.8.2 USB_CTRL Register. The TRM documents the
32 * phy as being SR70LX Synopsys USB 2.0 OTG nanoPHY. It also seems at
33 * least dm816x rev c ignores writes to USB_CTRL register, but the TI
34 * kernel is writing to those so it's possible that later revisions
35 * have worknig USB_CTRL register.
37 * Also note that At least USB_CTRL register seems to be dm816x specific
38 * according to the TRM. It's possible that USBPHY_CTRL is more generic,
39 * but that would have to be checked against the SR70LX documentation
40 * which does not seem to be publicly available.
42 * Finally, the phy on dm814x and am335x is different from dm816x.
44 #define DM816X_USB_CTRL_PHYCLKSRC BIT(8) /* 1 = PLL ref clock */
45 #define DM816X_USB_CTRL_PHYSLEEP1 BIT(1) /* Enable the first phy */
46 #define DM816X_USB_CTRL_PHYSLEEP0 BIT(0) /* Enable the second phy */
48 #define DM816X_USBPHY_CTRL_TXRISETUNE 1
49 #define DM816X_USBPHY_CTRL_TXVREFTUNE 0xc
50 #define DM816X_USBPHY_CTRL_TXPREEMTUNE 0x2
52 struct dm816x_usb_phy {
53 struct regmap *syscon;
55 unsigned int instance;
58 unsigned int usb_ctrl; /* Shared between phy0 and phy1 */
59 unsigned int usbphy_ctrl;
62 static int dm816x_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
66 otg->state = OTG_STATE_UNDEFINED;
71 static int dm816x_usb_phy_set_peripheral(struct usb_otg *otg,
72 struct usb_gadget *gadget)
76 otg->state = OTG_STATE_UNDEFINED;
81 static int dm816x_usb_phy_init(struct phy *x)
83 struct dm816x_usb_phy *phy = phy_get_drvdata(x);
87 if (clk_get_rate(phy->refclk) != 24000000)
88 dev_warn(phy->dev, "nonstandard phy refclk\n");
90 /* Set PLL ref clock and put phys to sleep */
91 error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
92 DM816X_USB_CTRL_PHYCLKSRC |
93 DM816X_USB_CTRL_PHYSLEEP1 |
94 DM816X_USB_CTRL_PHYSLEEP0,
96 regmap_read(phy->syscon, phy->usb_ctrl, &val);
99 "Working dm816x USB_CTRL! (0x%08x)\n",
103 * TI kernel sets these values for "symmetrical eye diagram and
104 * better signal quality" so let's assume somebody checked the
105 * values with a scope and set them here too.
107 regmap_read(phy->syscon, phy->usbphy_ctrl, &val);
108 val |= DM816X_USBPHY_CTRL_TXRISETUNE |
109 DM816X_USBPHY_CTRL_TXVREFTUNE |
110 DM816X_USBPHY_CTRL_TXPREEMTUNE;
111 regmap_write(phy->syscon, phy->usbphy_ctrl, val);
116 static const struct phy_ops ops = {
117 .init = dm816x_usb_phy_init,
118 .owner = THIS_MODULE,
121 static int dm816x_usb_phy_runtime_suspend(struct device *dev)
123 struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
124 unsigned int mask, val;
127 mask = BIT(phy->instance);
128 val = ~BIT(phy->instance);
129 error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
132 dev_err(phy->dev, "phy%i failed to power off\n",
134 clk_disable(phy->refclk);
139 static int dm816x_usb_phy_runtime_resume(struct device *dev)
141 struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
142 unsigned int mask, val;
145 error = clk_enable(phy->refclk);
150 * Note that at least dm816x rev c does not seem to do
151 * anything with the USB_CTRL register. But let's follow
152 * what the TI tree is doing in case later revisions use
155 mask = BIT(phy->instance);
156 val = BIT(phy->instance);
157 error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
160 dev_err(phy->dev, "phy%i failed to power on\n",
162 clk_disable(phy->refclk);
169 static UNIVERSAL_DEV_PM_OPS(dm816x_usb_phy_pm_ops,
170 dm816x_usb_phy_runtime_suspend,
171 dm816x_usb_phy_runtime_resume,
175 static const struct of_device_id dm816x_usb_phy_id_table[] = {
177 .compatible = "ti,dm8168-usb-phy",
181 MODULE_DEVICE_TABLE(of, dm816x_usb_phy_id_table);
184 static int dm816x_usb_phy_probe(struct platform_device *pdev)
186 struct dm816x_usb_phy *phy;
187 struct resource *res;
188 struct phy *generic_phy;
189 struct phy_provider *phy_provider;
191 const struct of_device_id *of_id;
192 const struct usb_phy_data *phy_data;
195 of_id = of_match_device(of_match_ptr(dm816x_usb_phy_id_table),
200 phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
204 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
208 phy->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
210 if (IS_ERR(phy->syscon))
211 return PTR_ERR(phy->syscon);
214 * According to sprs614e.pdf, the first usb_ctrl is shared and
215 * the second instance for usb_ctrl is reserved.. Also the
216 * register bits are different from earlier TRMs.
218 phy->usb_ctrl = 0x20;
219 phy->usbphy_ctrl = (res->start & 0xff) + 4;
220 if (phy->usbphy_ctrl == 0x2c)
223 phy_data = of_id->data;
225 otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
229 phy->dev = &pdev->dev;
230 phy->phy.dev = phy->dev;
231 phy->phy.label = "dm8168_usb_phy";
233 phy->phy.type = USB_PHY_TYPE_USB2;
234 otg->set_host = dm816x_usb_phy_set_host;
235 otg->set_peripheral = dm816x_usb_phy_set_peripheral;
236 otg->usb_phy = &phy->phy;
238 platform_set_drvdata(pdev, phy);
240 phy->refclk = devm_clk_get(phy->dev, "refclk");
241 if (IS_ERR(phy->refclk))
242 return PTR_ERR(phy->refclk);
243 error = clk_prepare(phy->refclk);
247 pm_runtime_enable(phy->dev);
248 generic_phy = devm_phy_create(phy->dev, NULL, &ops);
249 if (IS_ERR(generic_phy))
250 return PTR_ERR(generic_phy);
252 phy_set_drvdata(generic_phy, phy);
254 phy_provider = devm_of_phy_provider_register(phy->dev,
255 of_phy_simple_xlate);
256 if (IS_ERR(phy_provider))
257 return PTR_ERR(phy_provider);
259 usb_add_phy_dev(&phy->phy);
264 static int dm816x_usb_phy_remove(struct platform_device *pdev)
266 struct dm816x_usb_phy *phy = platform_get_drvdata(pdev);
268 usb_remove_phy(&phy->phy);
269 pm_runtime_disable(phy->dev);
270 clk_unprepare(phy->refclk);
275 static struct platform_driver dm816x_usb_phy_driver = {
276 .probe = dm816x_usb_phy_probe,
277 .remove = dm816x_usb_phy_remove,
279 .name = "dm816x-usb-phy",
280 .pm = &dm816x_usb_phy_pm_ops,
281 .of_match_table = of_match_ptr(dm816x_usb_phy_id_table),
285 module_platform_driver(dm816x_usb_phy_driver);
287 MODULE_ALIAS("platform:dm816x_usb");
288 MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
289 MODULE_DESCRIPTION("dm816x usb phy driver");
290 MODULE_LICENSE("GPL v2");