12aec85b2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20b56e9a7SVivek Gautam
30b56e9a7SVivek Gautam #include <linux/module.h>
40b56e9a7SVivek Gautam #include <linux/platform_device.h>
50b56e9a7SVivek Gautam #include <linux/regmap.h>
60b56e9a7SVivek Gautam
70b56e9a7SVivek Gautam #include <linux/slab.h>
80b56e9a7SVivek Gautam #include <linux/of.h>
90b56e9a7SVivek Gautam #include <linux/io.h>
100b56e9a7SVivek Gautam #include <linux/usb/phy_companion.h>
110b56e9a7SVivek Gautam #include <linux/clk.h>
120b56e9a7SVivek Gautam #include <linux/err.h>
130b56e9a7SVivek Gautam #include <linux/pm_runtime.h>
140b56e9a7SVivek Gautam #include <linux/delay.h>
150b56e9a7SVivek Gautam #include <linux/phy/phy.h>
160b56e9a7SVivek Gautam #include <linux/of_platform.h>
170b56e9a7SVivek Gautam
180b56e9a7SVivek Gautam #include <linux/mfd/syscon.h>
190b56e9a7SVivek Gautam
200b56e9a7SVivek Gautam /*
210b56e9a7SVivek Gautam * TRM has two sets of USB_CTRL registers.. The correct register bits
220b56e9a7SVivek Gautam * are in TRM section 24.9.8.2 USB_CTRL Register. The TRM documents the
230b56e9a7SVivek Gautam * phy as being SR70LX Synopsys USB 2.0 OTG nanoPHY. It also seems at
240b56e9a7SVivek Gautam * least dm816x rev c ignores writes to USB_CTRL register, but the TI
250b56e9a7SVivek Gautam * kernel is writing to those so it's possible that later revisions
260b56e9a7SVivek Gautam * have worknig USB_CTRL register.
270b56e9a7SVivek Gautam *
280b56e9a7SVivek Gautam * Also note that At least USB_CTRL register seems to be dm816x specific
290b56e9a7SVivek Gautam * according to the TRM. It's possible that USBPHY_CTRL is more generic,
300b56e9a7SVivek Gautam * but that would have to be checked against the SR70LX documentation
310b56e9a7SVivek Gautam * which does not seem to be publicly available.
320b56e9a7SVivek Gautam *
330b56e9a7SVivek Gautam * Finally, the phy on dm814x and am335x is different from dm816x.
340b56e9a7SVivek Gautam */
350b56e9a7SVivek Gautam #define DM816X_USB_CTRL_PHYCLKSRC BIT(8) /* 1 = PLL ref clock */
360b56e9a7SVivek Gautam #define DM816X_USB_CTRL_PHYSLEEP1 BIT(1) /* Enable the first phy */
370b56e9a7SVivek Gautam #define DM816X_USB_CTRL_PHYSLEEP0 BIT(0) /* Enable the second phy */
380b56e9a7SVivek Gautam
390b56e9a7SVivek Gautam #define DM816X_USBPHY_CTRL_TXRISETUNE 1
400b56e9a7SVivek Gautam #define DM816X_USBPHY_CTRL_TXVREFTUNE 0xc
410b56e9a7SVivek Gautam #define DM816X_USBPHY_CTRL_TXPREEMTUNE 0x2
420b56e9a7SVivek Gautam
430b56e9a7SVivek Gautam struct dm816x_usb_phy {
440b56e9a7SVivek Gautam struct regmap *syscon;
450b56e9a7SVivek Gautam struct device *dev;
460b56e9a7SVivek Gautam unsigned int instance;
470b56e9a7SVivek Gautam struct clk *refclk;
480b56e9a7SVivek Gautam struct usb_phy phy;
490b56e9a7SVivek Gautam unsigned int usb_ctrl; /* Shared between phy0 and phy1 */
500b56e9a7SVivek Gautam unsigned int usbphy_ctrl;
510b56e9a7SVivek Gautam };
520b56e9a7SVivek Gautam
dm816x_usb_phy_set_host(struct usb_otg * otg,struct usb_bus * host)530b56e9a7SVivek Gautam static int dm816x_usb_phy_set_host(struct usb_otg *otg, struct usb_bus *host)
540b56e9a7SVivek Gautam {
550b56e9a7SVivek Gautam otg->host = host;
560b56e9a7SVivek Gautam if (!host)
570b56e9a7SVivek Gautam otg->state = OTG_STATE_UNDEFINED;
580b56e9a7SVivek Gautam
590b56e9a7SVivek Gautam return 0;
600b56e9a7SVivek Gautam }
610b56e9a7SVivek Gautam
dm816x_usb_phy_set_peripheral(struct usb_otg * otg,struct usb_gadget * gadget)620b56e9a7SVivek Gautam static int dm816x_usb_phy_set_peripheral(struct usb_otg *otg,
630b56e9a7SVivek Gautam struct usb_gadget *gadget)
640b56e9a7SVivek Gautam {
650b56e9a7SVivek Gautam otg->gadget = gadget;
660b56e9a7SVivek Gautam if (!gadget)
670b56e9a7SVivek Gautam otg->state = OTG_STATE_UNDEFINED;
680b56e9a7SVivek Gautam
690b56e9a7SVivek Gautam return 0;
700b56e9a7SVivek Gautam }
710b56e9a7SVivek Gautam
dm816x_usb_phy_init(struct phy * x)720b56e9a7SVivek Gautam static int dm816x_usb_phy_init(struct phy *x)
730b56e9a7SVivek Gautam {
740b56e9a7SVivek Gautam struct dm816x_usb_phy *phy = phy_get_drvdata(x);
750b56e9a7SVivek Gautam unsigned int val;
760b56e9a7SVivek Gautam
770b56e9a7SVivek Gautam if (clk_get_rate(phy->refclk) != 24000000)
780b56e9a7SVivek Gautam dev_warn(phy->dev, "nonstandard phy refclk\n");
790b56e9a7SVivek Gautam
800b56e9a7SVivek Gautam /* Set PLL ref clock and put phys to sleep */
813b0163bbSVinod Koul regmap_update_bits(phy->syscon, phy->usb_ctrl,
820b56e9a7SVivek Gautam DM816X_USB_CTRL_PHYCLKSRC |
830b56e9a7SVivek Gautam DM816X_USB_CTRL_PHYSLEEP1 |
840b56e9a7SVivek Gautam DM816X_USB_CTRL_PHYSLEEP0,
850b56e9a7SVivek Gautam 0);
860b56e9a7SVivek Gautam regmap_read(phy->syscon, phy->usb_ctrl, &val);
870b56e9a7SVivek Gautam if ((val & 3) != 0)
880b56e9a7SVivek Gautam dev_info(phy->dev,
890b56e9a7SVivek Gautam "Working dm816x USB_CTRL! (0x%08x)\n",
900b56e9a7SVivek Gautam val);
910b56e9a7SVivek Gautam
920b56e9a7SVivek Gautam /*
930b56e9a7SVivek Gautam * TI kernel sets these values for "symmetrical eye diagram and
940b56e9a7SVivek Gautam * better signal quality" so let's assume somebody checked the
950b56e9a7SVivek Gautam * values with a scope and set them here too.
960b56e9a7SVivek Gautam */
970b56e9a7SVivek Gautam regmap_read(phy->syscon, phy->usbphy_ctrl, &val);
980b56e9a7SVivek Gautam val |= DM816X_USBPHY_CTRL_TXRISETUNE |
990b56e9a7SVivek Gautam DM816X_USBPHY_CTRL_TXVREFTUNE |
1000b56e9a7SVivek Gautam DM816X_USBPHY_CTRL_TXPREEMTUNE;
1010b56e9a7SVivek Gautam regmap_write(phy->syscon, phy->usbphy_ctrl, val);
1020b56e9a7SVivek Gautam
1030b56e9a7SVivek Gautam return 0;
1040b56e9a7SVivek Gautam }
1050b56e9a7SVivek Gautam
1060b56e9a7SVivek Gautam static const struct phy_ops ops = {
1070b56e9a7SVivek Gautam .init = dm816x_usb_phy_init,
1080b56e9a7SVivek Gautam .owner = THIS_MODULE,
1090b56e9a7SVivek Gautam };
1100b56e9a7SVivek Gautam
dm816x_usb_phy_runtime_suspend(struct device * dev)1110b56e9a7SVivek Gautam static int __maybe_unused dm816x_usb_phy_runtime_suspend(struct device *dev)
1120b56e9a7SVivek Gautam {
1130b56e9a7SVivek Gautam struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
1140b56e9a7SVivek Gautam unsigned int mask, val;
1150b56e9a7SVivek Gautam int error = 0;
1160b56e9a7SVivek Gautam
1170b56e9a7SVivek Gautam mask = BIT(phy->instance);
1180b56e9a7SVivek Gautam val = ~BIT(phy->instance);
1190b56e9a7SVivek Gautam error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
1200b56e9a7SVivek Gautam mask, val);
1210b56e9a7SVivek Gautam if (error)
1220b56e9a7SVivek Gautam dev_err(phy->dev, "phy%i failed to power off\n",
1230b56e9a7SVivek Gautam phy->instance);
1240b56e9a7SVivek Gautam clk_disable(phy->refclk);
1250b56e9a7SVivek Gautam
1260b56e9a7SVivek Gautam return 0;
1270b56e9a7SVivek Gautam }
1280b56e9a7SVivek Gautam
dm816x_usb_phy_runtime_resume(struct device * dev)1290b56e9a7SVivek Gautam static int __maybe_unused dm816x_usb_phy_runtime_resume(struct device *dev)
1300b56e9a7SVivek Gautam {
1310b56e9a7SVivek Gautam struct dm816x_usb_phy *phy = dev_get_drvdata(dev);
1320b56e9a7SVivek Gautam unsigned int mask, val;
1330b56e9a7SVivek Gautam int error;
1340b56e9a7SVivek Gautam
1350b56e9a7SVivek Gautam error = clk_enable(phy->refclk);
1360b56e9a7SVivek Gautam if (error)
1370b56e9a7SVivek Gautam return error;
1380b56e9a7SVivek Gautam
1390b56e9a7SVivek Gautam /*
1400b56e9a7SVivek Gautam * Note that at least dm816x rev c does not seem to do
1410b56e9a7SVivek Gautam * anything with the USB_CTRL register. But let's follow
1420b56e9a7SVivek Gautam * what the TI tree is doing in case later revisions use
1430b56e9a7SVivek Gautam * USB_CTRL.
1440b56e9a7SVivek Gautam */
1450b56e9a7SVivek Gautam mask = BIT(phy->instance);
1460b56e9a7SVivek Gautam val = BIT(phy->instance);
1470b56e9a7SVivek Gautam error = regmap_update_bits(phy->syscon, phy->usb_ctrl,
1480b56e9a7SVivek Gautam mask, val);
1490b56e9a7SVivek Gautam if (error) {
1500b56e9a7SVivek Gautam dev_err(phy->dev, "phy%i failed to power on\n",
1510b56e9a7SVivek Gautam phy->instance);
1520b56e9a7SVivek Gautam clk_disable(phy->refclk);
1530b56e9a7SVivek Gautam return error;
1540b56e9a7SVivek Gautam }
1550b56e9a7SVivek Gautam
1560b56e9a7SVivek Gautam return 0;
1570b56e9a7SVivek Gautam }
1580b56e9a7SVivek Gautam
1590b56e9a7SVivek Gautam static UNIVERSAL_DEV_PM_OPS(dm816x_usb_phy_pm_ops,
1600b56e9a7SVivek Gautam dm816x_usb_phy_runtime_suspend,
1610b56e9a7SVivek Gautam dm816x_usb_phy_runtime_resume,
1620b56e9a7SVivek Gautam NULL);
1630b56e9a7SVivek Gautam
1640b56e9a7SVivek Gautam #ifdef CONFIG_OF
1650b56e9a7SVivek Gautam static const struct of_device_id dm816x_usb_phy_id_table[] = {
1660b56e9a7SVivek Gautam {
1670b56e9a7SVivek Gautam .compatible = "ti,dm8168-usb-phy",
1680b56e9a7SVivek Gautam },
1690b56e9a7SVivek Gautam {},
1700b56e9a7SVivek Gautam };
1710b56e9a7SVivek Gautam MODULE_DEVICE_TABLE(of, dm816x_usb_phy_id_table);
1720b56e9a7SVivek Gautam #endif
1730b56e9a7SVivek Gautam
dm816x_usb_phy_probe(struct platform_device * pdev)1740b56e9a7SVivek Gautam static int dm816x_usb_phy_probe(struct platform_device *pdev)
1750b56e9a7SVivek Gautam {
1760b56e9a7SVivek Gautam struct dm816x_usb_phy *phy;
1770b56e9a7SVivek Gautam struct resource *res;
1780b56e9a7SVivek Gautam struct phy *generic_phy;
1790b56e9a7SVivek Gautam struct phy_provider *phy_provider;
1800b56e9a7SVivek Gautam struct usb_otg *otg;
1810b56e9a7SVivek Gautam const struct of_device_id *of_id;
1820b56e9a7SVivek Gautam int error;
1830b56e9a7SVivek Gautam
1840b56e9a7SVivek Gautam of_id = of_match_device(of_match_ptr(dm816x_usb_phy_id_table),
1850b56e9a7SVivek Gautam &pdev->dev);
1860b56e9a7SVivek Gautam if (!of_id)
1870b56e9a7SVivek Gautam return -EINVAL;
1880b56e9a7SVivek Gautam
1890b56e9a7SVivek Gautam phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
1900b56e9a7SVivek Gautam if (!phy)
1910b56e9a7SVivek Gautam return -ENOMEM;
1920b56e9a7SVivek Gautam
1930b56e9a7SVivek Gautam res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1940b56e9a7SVivek Gautam if (!res)
1950b56e9a7SVivek Gautam return -ENOENT;
1960b56e9a7SVivek Gautam
1970b56e9a7SVivek Gautam phy->syscon = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1980b56e9a7SVivek Gautam "syscon");
1990b56e9a7SVivek Gautam if (IS_ERR(phy->syscon))
2000b56e9a7SVivek Gautam return PTR_ERR(phy->syscon);
2010b56e9a7SVivek Gautam
2020b56e9a7SVivek Gautam /*
2030b56e9a7SVivek Gautam * According to sprs614e.pdf, the first usb_ctrl is shared and
2040b56e9a7SVivek Gautam * the second instance for usb_ctrl is reserved.. Also the
2050b56e9a7SVivek Gautam * register bits are different from earlier TRMs.
2060b56e9a7SVivek Gautam */
2070b56e9a7SVivek Gautam phy->usb_ctrl = 0x20;
2080b56e9a7SVivek Gautam phy->usbphy_ctrl = (res->start & 0xff) + 4;
2090b56e9a7SVivek Gautam if (phy->usbphy_ctrl == 0x2c)
2100b56e9a7SVivek Gautam phy->instance = 1;
2110b56e9a7SVivek Gautam
2120b56e9a7SVivek Gautam otg = devm_kzalloc(&pdev->dev, sizeof(*otg), GFP_KERNEL);
2130b56e9a7SVivek Gautam if (!otg)
2140b56e9a7SVivek Gautam return -ENOMEM;
2150b56e9a7SVivek Gautam
2160b56e9a7SVivek Gautam phy->dev = &pdev->dev;
2170b56e9a7SVivek Gautam phy->phy.dev = phy->dev;
2180b56e9a7SVivek Gautam phy->phy.label = "dm8168_usb_phy";
2190b56e9a7SVivek Gautam phy->phy.otg = otg;
2200b56e9a7SVivek Gautam phy->phy.type = USB_PHY_TYPE_USB2;
2210b56e9a7SVivek Gautam otg->set_host = dm816x_usb_phy_set_host;
2220b56e9a7SVivek Gautam otg->set_peripheral = dm816x_usb_phy_set_peripheral;
2230b56e9a7SVivek Gautam otg->usb_phy = &phy->phy;
2240b56e9a7SVivek Gautam
2250b56e9a7SVivek Gautam platform_set_drvdata(pdev, phy);
2260b56e9a7SVivek Gautam
2270b56e9a7SVivek Gautam phy->refclk = devm_clk_get(phy->dev, "refclk");
2280b56e9a7SVivek Gautam if (IS_ERR(phy->refclk))
2290b56e9a7SVivek Gautam return PTR_ERR(phy->refclk);
2300b56e9a7SVivek Gautam error = clk_prepare(phy->refclk);
2310b56e9a7SVivek Gautam if (error)
2320b56e9a7SVivek Gautam return error;
2330b56e9a7SVivek Gautam
2340b56e9a7SVivek Gautam pm_runtime_enable(phy->dev);
2350b56e9a7SVivek Gautam generic_phy = devm_phy_create(phy->dev, NULL, &ops);
236f7eedcb8SChristophe JAILLET if (IS_ERR(generic_phy)) {
237f7eedcb8SChristophe JAILLET error = PTR_ERR(generic_phy);
238f7eedcb8SChristophe JAILLET goto clk_unprepare;
239f7eedcb8SChristophe JAILLET }
2400b56e9a7SVivek Gautam
2410b56e9a7SVivek Gautam phy_set_drvdata(generic_phy, phy);
2420b56e9a7SVivek Gautam
2430b56e9a7SVivek Gautam phy_provider = devm_of_phy_provider_register(phy->dev,
2440b56e9a7SVivek Gautam of_phy_simple_xlate);
245f7eedcb8SChristophe JAILLET if (IS_ERR(phy_provider)) {
246f7eedcb8SChristophe JAILLET error = PTR_ERR(phy_provider);
247f7eedcb8SChristophe JAILLET goto clk_unprepare;
248f7eedcb8SChristophe JAILLET }
2490b56e9a7SVivek Gautam
2500b56e9a7SVivek Gautam usb_add_phy_dev(&phy->phy);
2510b56e9a7SVivek Gautam
2520b56e9a7SVivek Gautam return 0;
253f7eedcb8SChristophe JAILLET
254f7eedcb8SChristophe JAILLET clk_unprepare:
255f7eedcb8SChristophe JAILLET pm_runtime_disable(phy->dev);
256f7eedcb8SChristophe JAILLET clk_unprepare(phy->refclk);
257f7eedcb8SChristophe JAILLET return error;
2580b56e9a7SVivek Gautam }
2590b56e9a7SVivek Gautam
dm816x_usb_phy_remove(struct platform_device * pdev)260*13e1f735SUwe Kleine-König static void dm816x_usb_phy_remove(struct platform_device *pdev)
2610b56e9a7SVivek Gautam {
2620b56e9a7SVivek Gautam struct dm816x_usb_phy *phy = platform_get_drvdata(pdev);
2630b56e9a7SVivek Gautam
2640b56e9a7SVivek Gautam usb_remove_phy(&phy->phy);
2650b56e9a7SVivek Gautam pm_runtime_disable(phy->dev);
2660b56e9a7SVivek Gautam clk_unprepare(phy->refclk);
2670b56e9a7SVivek Gautam }
2680b56e9a7SVivek Gautam
2690b56e9a7SVivek Gautam static struct platform_driver dm816x_usb_phy_driver = {
2700b56e9a7SVivek Gautam .probe = dm816x_usb_phy_probe,
271*13e1f735SUwe Kleine-König .remove_new = dm816x_usb_phy_remove,
2720b56e9a7SVivek Gautam .driver = {
2730b56e9a7SVivek Gautam .name = "dm816x-usb-phy",
2740b56e9a7SVivek Gautam .pm = &dm816x_usb_phy_pm_ops,
2750b56e9a7SVivek Gautam .of_match_table = of_match_ptr(dm816x_usb_phy_id_table),
2760b56e9a7SVivek Gautam },
2770b56e9a7SVivek Gautam };
2780b56e9a7SVivek Gautam
2790b56e9a7SVivek Gautam module_platform_driver(dm816x_usb_phy_driver);
2800b56e9a7SVivek Gautam
2810b56e9a7SVivek Gautam MODULE_ALIAS("platform:dm816x_usb");
2820b56e9a7SVivek Gautam MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
2830b56e9a7SVivek Gautam MODULE_DESCRIPTION("dm816x usb phy driver");
2840b56e9a7SVivek Gautam MODULE_LICENSE("GPL v2");
285