1*84a14ae8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
20b56e9a7SVivek Gautam /*
30b56e9a7SVivek Gautam  * Rockchip DP PHY driver
40b56e9a7SVivek Gautam  *
50b56e9a7SVivek Gautam  * Copyright (C) 2016 FuZhou Rockchip Co., Ltd.
60b56e9a7SVivek Gautam  * Author: Yakir Yang <ykk@@rock-chips.com>
70b56e9a7SVivek Gautam  */
80b56e9a7SVivek Gautam 
90b56e9a7SVivek Gautam #include <linux/clk.h>
100b56e9a7SVivek Gautam #include <linux/mfd/syscon.h>
110b56e9a7SVivek Gautam #include <linux/module.h>
120b56e9a7SVivek Gautam #include <linux/of.h>
130b56e9a7SVivek Gautam #include <linux/phy/phy.h>
140b56e9a7SVivek Gautam #include <linux/platform_device.h>
150b56e9a7SVivek Gautam #include <linux/regmap.h>
160b56e9a7SVivek Gautam 
170b56e9a7SVivek Gautam #define GRF_SOC_CON12                           0x0274
180b56e9a7SVivek Gautam 
190b56e9a7SVivek Gautam #define GRF_EDP_REF_CLK_SEL_INTER_HIWORD_MASK   BIT(20)
200b56e9a7SVivek Gautam #define GRF_EDP_REF_CLK_SEL_INTER               BIT(4)
210b56e9a7SVivek Gautam 
220b56e9a7SVivek Gautam #define GRF_EDP_PHY_SIDDQ_HIWORD_MASK           BIT(21)
230b56e9a7SVivek Gautam #define GRF_EDP_PHY_SIDDQ_ON                    0
240b56e9a7SVivek Gautam #define GRF_EDP_PHY_SIDDQ_OFF                   BIT(5)
250b56e9a7SVivek Gautam 
260b56e9a7SVivek Gautam struct rockchip_dp_phy {
270b56e9a7SVivek Gautam 	struct device  *dev;
280b56e9a7SVivek Gautam 	struct regmap  *grf;
290b56e9a7SVivek Gautam 	struct clk     *phy_24m;
300b56e9a7SVivek Gautam };
310b56e9a7SVivek Gautam 
rockchip_set_phy_state(struct phy * phy,bool enable)320b56e9a7SVivek Gautam static int rockchip_set_phy_state(struct phy *phy, bool enable)
330b56e9a7SVivek Gautam {
340b56e9a7SVivek Gautam 	struct rockchip_dp_phy *dp = phy_get_drvdata(phy);
350b56e9a7SVivek Gautam 	int ret;
360b56e9a7SVivek Gautam 
370b56e9a7SVivek Gautam 	if (enable) {
380b56e9a7SVivek Gautam 		ret = regmap_write(dp->grf, GRF_SOC_CON12,
390b56e9a7SVivek Gautam 				   GRF_EDP_PHY_SIDDQ_HIWORD_MASK |
400b56e9a7SVivek Gautam 				   GRF_EDP_PHY_SIDDQ_ON);
410b56e9a7SVivek Gautam 		if (ret < 0) {
420b56e9a7SVivek Gautam 			dev_err(dp->dev, "Can't enable PHY power %d\n", ret);
430b56e9a7SVivek Gautam 			return ret;
440b56e9a7SVivek Gautam 		}
450b56e9a7SVivek Gautam 
460b56e9a7SVivek Gautam 		ret = clk_prepare_enable(dp->phy_24m);
470b56e9a7SVivek Gautam 	} else {
480b56e9a7SVivek Gautam 		clk_disable_unprepare(dp->phy_24m);
490b56e9a7SVivek Gautam 
500b56e9a7SVivek Gautam 		ret = regmap_write(dp->grf, GRF_SOC_CON12,
510b56e9a7SVivek Gautam 				   GRF_EDP_PHY_SIDDQ_HIWORD_MASK |
520b56e9a7SVivek Gautam 				   GRF_EDP_PHY_SIDDQ_OFF);
530b56e9a7SVivek Gautam 	}
540b56e9a7SVivek Gautam 
550b56e9a7SVivek Gautam 	return ret;
560b56e9a7SVivek Gautam }
570b56e9a7SVivek Gautam 
rockchip_dp_phy_power_on(struct phy * phy)580b56e9a7SVivek Gautam static int rockchip_dp_phy_power_on(struct phy *phy)
590b56e9a7SVivek Gautam {
600b56e9a7SVivek Gautam 	return rockchip_set_phy_state(phy, true);
610b56e9a7SVivek Gautam }
620b56e9a7SVivek Gautam 
rockchip_dp_phy_power_off(struct phy * phy)630b56e9a7SVivek Gautam static int rockchip_dp_phy_power_off(struct phy *phy)
640b56e9a7SVivek Gautam {
650b56e9a7SVivek Gautam 	return rockchip_set_phy_state(phy, false);
660b56e9a7SVivek Gautam }
670b56e9a7SVivek Gautam 
680b56e9a7SVivek Gautam static const struct phy_ops rockchip_dp_phy_ops = {
690b56e9a7SVivek Gautam 	.power_on	= rockchip_dp_phy_power_on,
700b56e9a7SVivek Gautam 	.power_off	= rockchip_dp_phy_power_off,
710b56e9a7SVivek Gautam 	.owner		= THIS_MODULE,
720b56e9a7SVivek Gautam };
730b56e9a7SVivek Gautam 
rockchip_dp_phy_probe(struct platform_device * pdev)740b56e9a7SVivek Gautam static int rockchip_dp_phy_probe(struct platform_device *pdev)
750b56e9a7SVivek Gautam {
760b56e9a7SVivek Gautam 	struct device *dev = &pdev->dev;
770b56e9a7SVivek Gautam 	struct device_node *np = dev->of_node;
780b56e9a7SVivek Gautam 	struct phy_provider *phy_provider;
790b56e9a7SVivek Gautam 	struct rockchip_dp_phy *dp;
800b56e9a7SVivek Gautam 	struct phy *phy;
810b56e9a7SVivek Gautam 	int ret;
820b56e9a7SVivek Gautam 
830b56e9a7SVivek Gautam 	if (!np)
840b56e9a7SVivek Gautam 		return -ENODEV;
850b56e9a7SVivek Gautam 
860b56e9a7SVivek Gautam 	if (!dev->parent || !dev->parent->of_node)
870b56e9a7SVivek Gautam 		return -ENODEV;
880b56e9a7SVivek Gautam 
890b56e9a7SVivek Gautam 	dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
900b56e9a7SVivek Gautam 	if (!dp)
910b56e9a7SVivek Gautam 		return -ENOMEM;
920b56e9a7SVivek Gautam 
930b56e9a7SVivek Gautam 	dp->dev = dev;
940b56e9a7SVivek Gautam 
950b56e9a7SVivek Gautam 	dp->phy_24m = devm_clk_get(dev, "24m");
960b56e9a7SVivek Gautam 	if (IS_ERR(dp->phy_24m)) {
970b56e9a7SVivek Gautam 		dev_err(dev, "cannot get clock 24m\n");
980b56e9a7SVivek Gautam 		return PTR_ERR(dp->phy_24m);
990b56e9a7SVivek Gautam 	}
1000b56e9a7SVivek Gautam 
1010b56e9a7SVivek Gautam 	ret = clk_set_rate(dp->phy_24m, 24000000);
1020b56e9a7SVivek Gautam 	if (ret < 0) {
1030b56e9a7SVivek Gautam 		dev_err(dp->dev, "cannot set clock phy_24m %d\n", ret);
1040b56e9a7SVivek Gautam 		return ret;
1050b56e9a7SVivek Gautam 	}
1060b56e9a7SVivek Gautam 
1070b56e9a7SVivek Gautam 	dp->grf = syscon_node_to_regmap(dev->parent->of_node);
1080b56e9a7SVivek Gautam 	if (IS_ERR(dp->grf)) {
1090b56e9a7SVivek Gautam 		dev_err(dev, "rk3288-dp needs the General Register Files syscon\n");
1100b56e9a7SVivek Gautam 		return PTR_ERR(dp->grf);
1110b56e9a7SVivek Gautam 	}
1120b56e9a7SVivek Gautam 
1130b56e9a7SVivek Gautam 	ret = regmap_write(dp->grf, GRF_SOC_CON12, GRF_EDP_REF_CLK_SEL_INTER |
1140b56e9a7SVivek Gautam 			   GRF_EDP_REF_CLK_SEL_INTER_HIWORD_MASK);
1150b56e9a7SVivek Gautam 	if (ret != 0) {
1160b56e9a7SVivek Gautam 		dev_err(dp->dev, "Could not config GRF edp ref clk: %d\n", ret);
1170b56e9a7SVivek Gautam 		return ret;
1180b56e9a7SVivek Gautam 	}
1190b56e9a7SVivek Gautam 
1200b56e9a7SVivek Gautam 	phy = devm_phy_create(dev, np, &rockchip_dp_phy_ops);
1210b56e9a7SVivek Gautam 	if (IS_ERR(phy)) {
1220b56e9a7SVivek Gautam 		dev_err(dev, "failed to create phy\n");
1230b56e9a7SVivek Gautam 		return PTR_ERR(phy);
1240b56e9a7SVivek Gautam 	}
1250b56e9a7SVivek Gautam 	phy_set_drvdata(phy, dp);
1260b56e9a7SVivek Gautam 
1270b56e9a7SVivek Gautam 	phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
1280b56e9a7SVivek Gautam 
1290b56e9a7SVivek Gautam 	return PTR_ERR_OR_ZERO(phy_provider);
1300b56e9a7SVivek Gautam }
1310b56e9a7SVivek Gautam 
1320b56e9a7SVivek Gautam static const struct of_device_id rockchip_dp_phy_dt_ids[] = {
1330b56e9a7SVivek Gautam 	{ .compatible = "rockchip,rk3288-dp-phy" },
1340b56e9a7SVivek Gautam 	{}
1350b56e9a7SVivek Gautam };
1360b56e9a7SVivek Gautam 
1370b56e9a7SVivek Gautam MODULE_DEVICE_TABLE(of, rockchip_dp_phy_dt_ids);
1380b56e9a7SVivek Gautam 
1390b56e9a7SVivek Gautam static struct platform_driver rockchip_dp_phy_driver = {
1400b56e9a7SVivek Gautam 	.probe		= rockchip_dp_phy_probe,
1410b56e9a7SVivek Gautam 	.driver		= {
1420b56e9a7SVivek Gautam 		.name	= "rockchip-dp-phy",
1430b56e9a7SVivek Gautam 		.of_match_table = rockchip_dp_phy_dt_ids,
1440b56e9a7SVivek Gautam 	},
1450b56e9a7SVivek Gautam };
1460b56e9a7SVivek Gautam 
1470b56e9a7SVivek Gautam module_platform_driver(rockchip_dp_phy_driver);
1480b56e9a7SVivek Gautam 
1490b56e9a7SVivek Gautam MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
1500b56e9a7SVivek Gautam MODULE_DESCRIPTION("Rockchip DP PHY driver");
1510b56e9a7SVivek Gautam MODULE_LICENSE("GPL v2");
152