xref: /linux/drivers/gpu/drm/msm/hdmi/hdmi_phy.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2016, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/of.h>
7 #include <linux/platform_device.h>
8 
9 #include "hdmi.h"
10 
11 static int msm_hdmi_phy_resource_init(struct hdmi_phy *phy)
12 {
13 	struct hdmi_phy_cfg *cfg = phy->cfg;
14 	struct device *dev = &phy->pdev->dev;
15 	int i, ret;
16 
17 	phy->regs = devm_kcalloc(dev, cfg->num_regs, sizeof(phy->regs[0]),
18 				 GFP_KERNEL);
19 	if (!phy->regs)
20 		return -ENOMEM;
21 
22 	phy->clks = devm_kcalloc(dev, cfg->num_clks, sizeof(phy->clks[0]),
23 				 GFP_KERNEL);
24 	if (!phy->clks)
25 		return -ENOMEM;
26 
27 	for (i = 0; i < cfg->num_regs; i++)
28 		phy->regs[i].supply = cfg->reg_names[i];
29 
30 	ret = devm_regulator_bulk_get(dev, cfg->num_regs, phy->regs);
31 	if (ret) {
32 		if (ret != -EPROBE_DEFER)
33 			DRM_DEV_ERROR(dev, "failed to get phy regulators: %d\n", ret);
34 
35 		return ret;
36 	}
37 
38 	for (i = 0; i < cfg->num_clks; i++) {
39 		struct clk *clk;
40 
41 		clk = msm_clk_get(phy->pdev, cfg->clk_names[i]);
42 		if (IS_ERR(clk)) {
43 			ret = PTR_ERR(clk);
44 			DRM_DEV_ERROR(dev, "failed to get phy clock: %s (%d)\n",
45 				cfg->clk_names[i], ret);
46 			return ret;
47 		}
48 
49 		phy->clks[i] = clk;
50 	}
51 
52 	return 0;
53 }
54 
55 int msm_hdmi_phy_resource_enable(struct hdmi_phy *phy)
56 {
57 	struct hdmi_phy_cfg *cfg = phy->cfg;
58 	struct device *dev = &phy->pdev->dev;
59 	int i, ret = 0;
60 
61 	ret = pm_runtime_resume_and_get(dev);
62 	if (ret) {
63 		DRM_DEV_ERROR(dev, "runtime resume failed: %d\n", ret);
64 		return ret;
65 	}
66 
67 	ret = regulator_bulk_enable(cfg->num_regs, phy->regs);
68 	if (ret) {
69 		DRM_DEV_ERROR(dev, "failed to enable regulators: (%d)\n", ret);
70 		goto err_pm_put;
71 	}
72 
73 	for (i = 0; i < cfg->num_clks; i++) {
74 		ret = clk_prepare_enable(phy->clks[i]);
75 		if (ret) {
76 			DRM_DEV_ERROR(dev, "failed to enable clock: %s (%d)\n",
77 				cfg->clk_names[i], ret);
78 			goto err_clk_unprepare;
79 		}
80 	}
81 
82 	return 0;
83 
84 err_clk_unprepare:
85 	for (; i > 0; i--)
86 		clk_disable_unprepare(phy->clks[i - 1]);
87 	regulator_bulk_disable(cfg->num_regs, phy->regs);
88 
89 err_pm_put:
90 	pm_runtime_put_sync(dev);
91 
92 	return ret;
93 }
94 
95 void msm_hdmi_phy_resource_disable(struct hdmi_phy *phy)
96 {
97 	struct hdmi_phy_cfg *cfg = phy->cfg;
98 	struct device *dev = &phy->pdev->dev;
99 	int i;
100 
101 	for (i = cfg->num_clks - 1; i >= 0; i--)
102 		clk_disable_unprepare(phy->clks[i]);
103 
104 	regulator_bulk_disable(cfg->num_regs, phy->regs);
105 
106 	pm_runtime_put_sync(dev);
107 }
108 
109 void msm_hdmi_phy_powerup(struct hdmi_phy *phy, unsigned long pixclock)
110 {
111 	if (!phy || !phy->cfg->powerup)
112 		return;
113 
114 	phy->cfg->powerup(phy, pixclock);
115 }
116 
117 void msm_hdmi_phy_powerdown(struct hdmi_phy *phy)
118 {
119 	if (!phy || !phy->cfg->powerdown)
120 		return;
121 
122 	phy->cfg->powerdown(phy);
123 }
124 
125 static int msm_hdmi_phy_pll_init(struct platform_device *pdev,
126 			     enum hdmi_phy_type type)
127 {
128 	int ret;
129 
130 	switch (type) {
131 	case MSM_HDMI_PHY_8960:
132 		ret = msm_hdmi_pll_8960_init(pdev);
133 		break;
134 	case MSM_HDMI_PHY_8996:
135 		ret = msm_hdmi_pll_8996_init(pdev);
136 		break;
137 	case MSM_HDMI_PHY_8998:
138 		ret = msm_hdmi_pll_8998_init(pdev);
139 		break;
140 	/*
141 	 * we don't have PLL support for these, don't report an error for now
142 	 */
143 	case MSM_HDMI_PHY_8x60:
144 	case MSM_HDMI_PHY_8x74:
145 	default:
146 		ret = 0;
147 		break;
148 	}
149 
150 	return ret;
151 }
152 
153 static int msm_hdmi_phy_probe(struct platform_device *pdev)
154 {
155 	struct device *dev = &pdev->dev;
156 	struct hdmi_phy *phy;
157 	int ret;
158 
159 	phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
160 	if (!phy)
161 		return -ENODEV;
162 
163 	phy->cfg = (struct hdmi_phy_cfg *)of_device_get_match_data(dev);
164 	if (!phy->cfg)
165 		return -ENODEV;
166 
167 	phy->mmio = msm_ioremap(pdev, "hdmi_phy");
168 	if (IS_ERR(phy->mmio)) {
169 		DRM_DEV_ERROR(dev, "%s: failed to map phy base\n", __func__);
170 		return -ENOMEM;
171 	}
172 
173 	phy->pdev = pdev;
174 
175 	ret = msm_hdmi_phy_resource_init(phy);
176 	if (ret)
177 		return ret;
178 
179 	pm_runtime_enable(&pdev->dev);
180 
181 	ret = msm_hdmi_phy_resource_enable(phy);
182 	if (ret)
183 		goto err_pm_disable;
184 
185 	ret = msm_hdmi_phy_pll_init(pdev, phy->cfg->type);
186 	if (ret) {
187 		DRM_DEV_ERROR(dev, "couldn't init PLL\n");
188 		msm_hdmi_phy_resource_disable(phy);
189 		goto err_pm_disable;
190 	}
191 
192 	msm_hdmi_phy_resource_disable(phy);
193 
194 	platform_set_drvdata(pdev, phy);
195 
196 	return 0;
197 
198 err_pm_disable:
199 	pm_runtime_disable(dev);
200 	return ret;
201 }
202 
203 static void msm_hdmi_phy_remove(struct platform_device *pdev)
204 {
205 	pm_runtime_disable(&pdev->dev);
206 }
207 
208 static const struct of_device_id msm_hdmi_phy_dt_match[] = {
209 	{ .compatible = "qcom,hdmi-phy-8660",
210 	  .data = &msm_hdmi_phy_8x60_cfg },
211 	{ .compatible = "qcom,hdmi-phy-8960",
212 	  .data = &msm_hdmi_phy_8960_cfg },
213 	{ .compatible = "qcom,hdmi-phy-8974",
214 	  .data = &msm_hdmi_phy_8x74_cfg },
215 	{ .compatible = "qcom,hdmi-phy-8084",
216 	  .data = &msm_hdmi_phy_8x74_cfg },
217 	{ .compatible = "qcom,hdmi-phy-8996",
218 	  .data = &msm_hdmi_phy_8996_cfg },
219 	{ .compatible = "qcom,hdmi-phy-8998",
220 	  .data = &msm_hdmi_phy_8998_cfg },
221 	{}
222 };
223 MODULE_DEVICE_TABLE(of, msm_hdmi_phy_dt_match);
224 
225 static struct platform_driver msm_hdmi_phy_platform_driver = {
226 	.probe      = msm_hdmi_phy_probe,
227 	.remove     = msm_hdmi_phy_remove,
228 	.driver     = {
229 		.name   = "msm_hdmi_phy",
230 		.of_match_table = msm_hdmi_phy_dt_match,
231 	},
232 };
233 
234 void __init msm_hdmi_phy_driver_register(void)
235 {
236 	platform_driver_register(&msm_hdmi_phy_platform_driver);
237 }
238 
239 void __exit msm_hdmi_phy_driver_unregister(void)
240 {
241 	platform_driver_unregister(&msm_hdmi_phy_platform_driver);
242 }
243