18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * phy-da8xx-usb - TI DaVinci DA8xx USB PHY driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 David Lechner <david@lechnology.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/clk.h>
98c2ecf20Sopenharmony_ci#include <linux/io.h>
108c2ecf20Sopenharmony_ci#include <linux/of.h>
118c2ecf20Sopenharmony_ci#include <linux/mfd/da8xx-cfgchip.h>
128c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
158c2ecf20Sopenharmony_ci#include <linux/platform_data/phy-da8xx-usb.h>
168c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
178c2ecf20Sopenharmony_ci#include <linux/regmap.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define PHY_INIT_BITS	(CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN)
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistruct da8xx_usb_phy {
228c2ecf20Sopenharmony_ci	struct phy_provider	*phy_provider;
238c2ecf20Sopenharmony_ci	struct phy		*usb11_phy;
248c2ecf20Sopenharmony_ci	struct phy		*usb20_phy;
258c2ecf20Sopenharmony_ci	struct clk		*usb11_clk;
268c2ecf20Sopenharmony_ci	struct clk		*usb20_clk;
278c2ecf20Sopenharmony_ci	struct regmap		*regmap;
288c2ecf20Sopenharmony_ci};
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic int da8xx_usb11_phy_power_on(struct phy *phy)
318c2ecf20Sopenharmony_ci{
328c2ecf20Sopenharmony_ci	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
338c2ecf20Sopenharmony_ci	int ret;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(d_phy->usb11_clk);
368c2ecf20Sopenharmony_ci	if (ret)
378c2ecf20Sopenharmony_ci		return ret;
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM,
408c2ecf20Sopenharmony_ci			  CFGCHIP2_USB1SUSPENDM);
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	return 0;
438c2ecf20Sopenharmony_ci}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistatic int da8xx_usb11_phy_power_off(struct phy *phy)
468c2ecf20Sopenharmony_ci{
478c2ecf20Sopenharmony_ci	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM, 0);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	clk_disable_unprepare(d_phy->usb11_clk);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	return 0;
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic const struct phy_ops da8xx_usb11_phy_ops = {
578c2ecf20Sopenharmony_ci	.power_on	= da8xx_usb11_phy_power_on,
588c2ecf20Sopenharmony_ci	.power_off	= da8xx_usb11_phy_power_off,
598c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic int da8xx_usb20_phy_power_on(struct phy *phy)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
658c2ecf20Sopenharmony_ci	int ret;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(d_phy->usb20_clk);
688c2ecf20Sopenharmony_ci	if (ret)
698c2ecf20Sopenharmony_ci		return ret;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN, 0);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return 0;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic int da8xx_usb20_phy_power_off(struct phy *phy)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN,
818c2ecf20Sopenharmony_ci			  CFGCHIP2_OTGPWRDN);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	clk_disable_unprepare(d_phy->usb20_clk);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	return 0;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic int da8xx_usb20_phy_set_mode(struct phy *phy,
898c2ecf20Sopenharmony_ci				    enum phy_mode mode, int submode)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
928c2ecf20Sopenharmony_ci	u32 val;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	switch (mode) {
958c2ecf20Sopenharmony_ci	case PHY_MODE_USB_HOST:		/* Force VBUS valid, ID = 0 */
968c2ecf20Sopenharmony_ci		val = CFGCHIP2_OTGMODE_FORCE_HOST;
978c2ecf20Sopenharmony_ci		break;
988c2ecf20Sopenharmony_ci	case PHY_MODE_USB_DEVICE:	/* Force VBUS valid, ID = 1 */
998c2ecf20Sopenharmony_ci		val = CFGCHIP2_OTGMODE_FORCE_DEVICE;
1008c2ecf20Sopenharmony_ci		break;
1018c2ecf20Sopenharmony_ci	case PHY_MODE_USB_OTG:	/* Don't override the VBUS/ID comparators */
1028c2ecf20Sopenharmony_ci		val = CFGCHIP2_OTGMODE_NO_OVERRIDE;
1038c2ecf20Sopenharmony_ci		break;
1048c2ecf20Sopenharmony_ci	default:
1058c2ecf20Sopenharmony_ci		return -EINVAL;
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK,
1098c2ecf20Sopenharmony_ci			  val);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	return 0;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic const struct phy_ops da8xx_usb20_phy_ops = {
1158c2ecf20Sopenharmony_ci	.power_on	= da8xx_usb20_phy_power_on,
1168c2ecf20Sopenharmony_ci	.power_off	= da8xx_usb20_phy_power_off,
1178c2ecf20Sopenharmony_ci	.set_mode	= da8xx_usb20_phy_set_mode,
1188c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
1198c2ecf20Sopenharmony_ci};
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic struct phy *da8xx_usb_phy_of_xlate(struct device *dev,
1228c2ecf20Sopenharmony_ci					 struct of_phandle_args *args)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (!d_phy)
1278c2ecf20Sopenharmony_ci		return ERR_PTR(-ENODEV);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	switch (args->args[0]) {
1308c2ecf20Sopenharmony_ci	case 0:
1318c2ecf20Sopenharmony_ci		return d_phy->usb20_phy;
1328c2ecf20Sopenharmony_ci	case 1:
1338c2ecf20Sopenharmony_ci		return d_phy->usb11_phy;
1348c2ecf20Sopenharmony_ci	default:
1358c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
1368c2ecf20Sopenharmony_ci	}
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic int da8xx_usb_phy_probe(struct platform_device *pdev)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct device		*dev = &pdev->dev;
1428c2ecf20Sopenharmony_ci	struct da8xx_usb_phy_platform_data *pdata = dev->platform_data;
1438c2ecf20Sopenharmony_ci	struct device_node	*node = dev->of_node;
1448c2ecf20Sopenharmony_ci	struct da8xx_usb_phy	*d_phy;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	d_phy = devm_kzalloc(dev, sizeof(*d_phy), GFP_KERNEL);
1478c2ecf20Sopenharmony_ci	if (!d_phy)
1488c2ecf20Sopenharmony_ci		return -ENOMEM;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if (pdata)
1518c2ecf20Sopenharmony_ci		d_phy->regmap = pdata->cfgchip;
1528c2ecf20Sopenharmony_ci	else
1538c2ecf20Sopenharmony_ci		d_phy->regmap = syscon_regmap_lookup_by_compatible(
1548c2ecf20Sopenharmony_ci							"ti,da830-cfgchip");
1558c2ecf20Sopenharmony_ci	if (IS_ERR(d_phy->regmap)) {
1568c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to get syscon\n");
1578c2ecf20Sopenharmony_ci		return PTR_ERR(d_phy->regmap);
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	d_phy->usb11_clk = devm_clk_get(dev, "usb1_clk48");
1618c2ecf20Sopenharmony_ci	if (IS_ERR(d_phy->usb11_clk)) {
1628c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to get usb1_clk48\n");
1638c2ecf20Sopenharmony_ci		return PTR_ERR(d_phy->usb11_clk);
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	d_phy->usb20_clk = devm_clk_get(dev, "usb0_clk48");
1678c2ecf20Sopenharmony_ci	if (IS_ERR(d_phy->usb20_clk)) {
1688c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to get usb0_clk48\n");
1698c2ecf20Sopenharmony_ci		return PTR_ERR(d_phy->usb20_clk);
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	d_phy->usb11_phy = devm_phy_create(dev, node, &da8xx_usb11_phy_ops);
1738c2ecf20Sopenharmony_ci	if (IS_ERR(d_phy->usb11_phy)) {
1748c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to create usb11 phy\n");
1758c2ecf20Sopenharmony_ci		return PTR_ERR(d_phy->usb11_phy);
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	d_phy->usb20_phy = devm_phy_create(dev, node, &da8xx_usb20_phy_ops);
1798c2ecf20Sopenharmony_ci	if (IS_ERR(d_phy->usb20_phy)) {
1808c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to create usb20 phy\n");
1818c2ecf20Sopenharmony_ci		return PTR_ERR(d_phy->usb20_phy);
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, d_phy);
1858c2ecf20Sopenharmony_ci	phy_set_drvdata(d_phy->usb11_phy, d_phy);
1868c2ecf20Sopenharmony_ci	phy_set_drvdata(d_phy->usb20_phy, d_phy);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (node) {
1898c2ecf20Sopenharmony_ci		d_phy->phy_provider = devm_of_phy_provider_register(dev,
1908c2ecf20Sopenharmony_ci							da8xx_usb_phy_of_xlate);
1918c2ecf20Sopenharmony_ci		if (IS_ERR(d_phy->phy_provider)) {
1928c2ecf20Sopenharmony_ci			dev_err(dev, "Failed to create phy provider\n");
1938c2ecf20Sopenharmony_ci			return PTR_ERR(d_phy->phy_provider);
1948c2ecf20Sopenharmony_ci		}
1958c2ecf20Sopenharmony_ci	} else {
1968c2ecf20Sopenharmony_ci		int ret;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci		ret = phy_create_lookup(d_phy->usb11_phy, "usb-phy",
1998c2ecf20Sopenharmony_ci					"ohci-da8xx");
2008c2ecf20Sopenharmony_ci		if (ret)
2018c2ecf20Sopenharmony_ci			dev_warn(dev, "Failed to create usb11 phy lookup\n");
2028c2ecf20Sopenharmony_ci		ret = phy_create_lookup(d_phy->usb20_phy, "usb-phy",
2038c2ecf20Sopenharmony_ci					"musb-da8xx");
2048c2ecf20Sopenharmony_ci		if (ret)
2058c2ecf20Sopenharmony_ci			dev_warn(dev, "Failed to create usb20 phy lookup\n");
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	regmap_write_bits(d_phy->regmap, CFGCHIP(2),
2098c2ecf20Sopenharmony_ci			  PHY_INIT_BITS, PHY_INIT_BITS);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	return 0;
2128c2ecf20Sopenharmony_ci}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_cistatic int da8xx_usb_phy_remove(struct platform_device *pdev)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	struct da8xx_usb_phy *d_phy = platform_get_drvdata(pdev);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	if (!pdev->dev.of_node) {
2198c2ecf20Sopenharmony_ci		phy_remove_lookup(d_phy->usb20_phy, "usb-phy", "musb-da8xx");
2208c2ecf20Sopenharmony_ci		phy_remove_lookup(d_phy->usb11_phy, "usb-phy", "ohci-da8xx");
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	return 0;
2248c2ecf20Sopenharmony_ci}
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_cistatic const struct of_device_id da8xx_usb_phy_ids[] = {
2278c2ecf20Sopenharmony_ci	{ .compatible = "ti,da830-usb-phy" },
2288c2ecf20Sopenharmony_ci	{ }
2298c2ecf20Sopenharmony_ci};
2308c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, da8xx_usb_phy_ids);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic struct platform_driver da8xx_usb_phy_driver = {
2338c2ecf20Sopenharmony_ci	.probe	= da8xx_usb_phy_probe,
2348c2ecf20Sopenharmony_ci	.remove	= da8xx_usb_phy_remove,
2358c2ecf20Sopenharmony_ci	.driver	= {
2368c2ecf20Sopenharmony_ci		.name	= "da8xx-usb-phy",
2378c2ecf20Sopenharmony_ci		.of_match_table = da8xx_usb_phy_ids,
2388c2ecf20Sopenharmony_ci	},
2398c2ecf20Sopenharmony_ci};
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_cimodule_platform_driver(da8xx_usb_phy_driver);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:da8xx-usb-phy");
2448c2ecf20Sopenharmony_ciMODULE_AUTHOR("David Lechner <david@lechnology.com>");
2458c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("TI DA8xx USB PHY driver");
2468c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
247