18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Rockchip DP PHY driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2016 FuZhou Rockchip Co., Ltd.
68c2ecf20Sopenharmony_ci * Author: Yakir Yang <ykk@@rock-chips.com>
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/clk.h>
108c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/of.h>
138c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
158c2ecf20Sopenharmony_ci#include <linux/regmap.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define GRF_SOC_CON12                           0x0274
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define GRF_EDP_REF_CLK_SEL_INTER_HIWORD_MASK   BIT(20)
208c2ecf20Sopenharmony_ci#define GRF_EDP_REF_CLK_SEL_INTER               BIT(4)
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define GRF_EDP_PHY_SIDDQ_HIWORD_MASK           BIT(21)
238c2ecf20Sopenharmony_ci#define GRF_EDP_PHY_SIDDQ_ON                    0
248c2ecf20Sopenharmony_ci#define GRF_EDP_PHY_SIDDQ_OFF                   BIT(5)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct rockchip_dp_phy {
278c2ecf20Sopenharmony_ci	struct device  *dev;
288c2ecf20Sopenharmony_ci	struct regmap  *grf;
298c2ecf20Sopenharmony_ci	struct clk     *phy_24m;
308c2ecf20Sopenharmony_ci};
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistatic int rockchip_set_phy_state(struct phy *phy, bool enable)
338c2ecf20Sopenharmony_ci{
348c2ecf20Sopenharmony_ci	struct rockchip_dp_phy *dp = phy_get_drvdata(phy);
358c2ecf20Sopenharmony_ci	int ret;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (enable) {
388c2ecf20Sopenharmony_ci		ret = regmap_write(dp->grf, GRF_SOC_CON12,
398c2ecf20Sopenharmony_ci				   GRF_EDP_PHY_SIDDQ_HIWORD_MASK |
408c2ecf20Sopenharmony_ci				   GRF_EDP_PHY_SIDDQ_ON);
418c2ecf20Sopenharmony_ci		if (ret < 0) {
428c2ecf20Sopenharmony_ci			dev_err(dp->dev, "Can't enable PHY power %d\n", ret);
438c2ecf20Sopenharmony_ci			return ret;
448c2ecf20Sopenharmony_ci		}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci		ret = clk_prepare_enable(dp->phy_24m);
478c2ecf20Sopenharmony_ci	} else {
488c2ecf20Sopenharmony_ci		clk_disable_unprepare(dp->phy_24m);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci		ret = regmap_write(dp->grf, GRF_SOC_CON12,
518c2ecf20Sopenharmony_ci				   GRF_EDP_PHY_SIDDQ_HIWORD_MASK |
528c2ecf20Sopenharmony_ci				   GRF_EDP_PHY_SIDDQ_OFF);
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return ret;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic int rockchip_dp_phy_power_on(struct phy *phy)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	return rockchip_set_phy_state(phy, true);
618c2ecf20Sopenharmony_ci}
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistatic int rockchip_dp_phy_power_off(struct phy *phy)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	return rockchip_set_phy_state(phy, false);
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic const struct phy_ops rockchip_dp_phy_ops = {
698c2ecf20Sopenharmony_ci	.power_on	= rockchip_dp_phy_power_on,
708c2ecf20Sopenharmony_ci	.power_off	= rockchip_dp_phy_power_off,
718c2ecf20Sopenharmony_ci	.owner		= THIS_MODULE,
728c2ecf20Sopenharmony_ci};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic int rockchip_dp_phy_probe(struct platform_device *pdev)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
778c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
788c2ecf20Sopenharmony_ci	struct phy_provider *phy_provider;
798c2ecf20Sopenharmony_ci	struct rockchip_dp_phy *dp;
808c2ecf20Sopenharmony_ci	struct phy *phy;
818c2ecf20Sopenharmony_ci	int ret;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (!np)
848c2ecf20Sopenharmony_ci		return -ENODEV;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	if (!dev->parent || !dev->parent->of_node)
878c2ecf20Sopenharmony_ci		return -ENODEV;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
908c2ecf20Sopenharmony_ci	if (!dp)
918c2ecf20Sopenharmony_ci		return -ENOMEM;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	dp->dev = dev;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	dp->phy_24m = devm_clk_get(dev, "24m");
968c2ecf20Sopenharmony_ci	if (IS_ERR(dp->phy_24m)) {
978c2ecf20Sopenharmony_ci		dev_err(dev, "cannot get clock 24m\n");
988c2ecf20Sopenharmony_ci		return PTR_ERR(dp->phy_24m);
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	ret = clk_set_rate(dp->phy_24m, 24000000);
1028c2ecf20Sopenharmony_ci	if (ret < 0) {
1038c2ecf20Sopenharmony_ci		dev_err(dp->dev, "cannot set clock phy_24m %d\n", ret);
1048c2ecf20Sopenharmony_ci		return ret;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	dp->grf = syscon_node_to_regmap(dev->parent->of_node);
1088c2ecf20Sopenharmony_ci	if (IS_ERR(dp->grf)) {
1098c2ecf20Sopenharmony_ci		dev_err(dev, "rk3288-dp needs the General Register Files syscon\n");
1108c2ecf20Sopenharmony_ci		return PTR_ERR(dp->grf);
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	ret = regmap_write(dp->grf, GRF_SOC_CON12, GRF_EDP_REF_CLK_SEL_INTER |
1148c2ecf20Sopenharmony_ci			   GRF_EDP_REF_CLK_SEL_INTER_HIWORD_MASK);
1158c2ecf20Sopenharmony_ci	if (ret != 0) {
1168c2ecf20Sopenharmony_ci		dev_err(dp->dev, "Could not config GRF edp ref clk: %d\n", ret);
1178c2ecf20Sopenharmony_ci		return ret;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	phy = devm_phy_create(dev, np, &rockchip_dp_phy_ops);
1218c2ecf20Sopenharmony_ci	if (IS_ERR(phy)) {
1228c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create phy\n");
1238c2ecf20Sopenharmony_ci		return PTR_ERR(phy);
1248c2ecf20Sopenharmony_ci	}
1258c2ecf20Sopenharmony_ci	phy_set_drvdata(phy, dp);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci	return PTR_ERR_OR_ZERO(phy_provider);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_cistatic const struct of_device_id rockchip_dp_phy_dt_ids[] = {
1338c2ecf20Sopenharmony_ci	{ .compatible = "rockchip,rk3288-dp-phy" },
1348c2ecf20Sopenharmony_ci	{}
1358c2ecf20Sopenharmony_ci};
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, rockchip_dp_phy_dt_ids);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic struct platform_driver rockchip_dp_phy_driver = {
1408c2ecf20Sopenharmony_ci	.probe		= rockchip_dp_phy_probe,
1418c2ecf20Sopenharmony_ci	.driver		= {
1428c2ecf20Sopenharmony_ci		.name	= "rockchip-dp-phy",
1438c2ecf20Sopenharmony_ci		.of_match_table = rockchip_dp_phy_dt_ids,
1448c2ecf20Sopenharmony_ci	},
1458c2ecf20Sopenharmony_ci};
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cimodule_platform_driver(rockchip_dp_phy_driver);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ciMODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
1508c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Rockchip DP PHY driver");
1518c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
152