xref: /openbmc/linux/drivers/gpu/drm/msm/hdmi/hdmi.c (revision c819e2cf)
1 /*
2  * Copyright (C) 2013 Red Hat
3  * Author: Rob Clark <robdclark@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <linux/of_irq.h>
19 #include "hdmi.h"
20 
21 void hdmi_set_mode(struct hdmi *hdmi, bool power_on)
22 {
23 	uint32_t ctrl = 0;
24 
25 	if (power_on) {
26 		ctrl |= HDMI_CTRL_ENABLE;
27 		if (!hdmi->hdmi_mode) {
28 			ctrl |= HDMI_CTRL_HDMI;
29 			hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
30 			ctrl &= ~HDMI_CTRL_HDMI;
31 		} else {
32 			ctrl |= HDMI_CTRL_HDMI;
33 		}
34 	} else {
35 		ctrl = HDMI_CTRL_HDMI;
36 	}
37 
38 	hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
39 	DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
40 			power_on ? "Enable" : "Disable", ctrl);
41 }
42 
43 static irqreturn_t hdmi_irq(int irq, void *dev_id)
44 {
45 	struct hdmi *hdmi = dev_id;
46 
47 	/* Process HPD: */
48 	hdmi_connector_irq(hdmi->connector);
49 
50 	/* Process DDC: */
51 	hdmi_i2c_irq(hdmi->i2c);
52 
53 	/* TODO audio.. */
54 
55 	return IRQ_HANDLED;
56 }
57 
58 static void hdmi_destroy(struct hdmi *hdmi)
59 {
60 	struct hdmi_phy *phy = hdmi->phy;
61 
62 	if (phy)
63 		phy->funcs->destroy(phy);
64 
65 	if (hdmi->i2c)
66 		hdmi_i2c_destroy(hdmi->i2c);
67 
68 	platform_set_drvdata(hdmi->pdev, NULL);
69 }
70 
71 /* construct hdmi at bind/probe time, grab all the resources.  If
72  * we are to EPROBE_DEFER we want to do it here, rather than later
73  * at modeset_init() time
74  */
75 static struct hdmi *hdmi_init(struct platform_device *pdev)
76 {
77 	struct hdmi_platform_config *config = pdev->dev.platform_data;
78 	struct hdmi *hdmi = NULL;
79 	int i, ret;
80 
81 	hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
82 	if (!hdmi) {
83 		ret = -ENOMEM;
84 		goto fail;
85 	}
86 
87 	hdmi->pdev = pdev;
88 	hdmi->config = config;
89 
90 	/* not sure about which phy maps to which msm.. probably I miss some */
91 	if (config->phy_init)
92 		hdmi->phy = config->phy_init(hdmi);
93 	else
94 		hdmi->phy = ERR_PTR(-ENXIO);
95 
96 	if (IS_ERR(hdmi->phy)) {
97 		ret = PTR_ERR(hdmi->phy);
98 		dev_err(&pdev->dev, "failed to load phy: %d\n", ret);
99 		hdmi->phy = NULL;
100 		goto fail;
101 	}
102 
103 	hdmi->mmio = msm_ioremap(pdev, config->mmio_name, "HDMI");
104 	if (IS_ERR(hdmi->mmio)) {
105 		ret = PTR_ERR(hdmi->mmio);
106 		goto fail;
107 	}
108 
109 	BUG_ON(config->hpd_reg_cnt > ARRAY_SIZE(hdmi->hpd_regs));
110 	for (i = 0; i < config->hpd_reg_cnt; i++) {
111 		struct regulator *reg;
112 
113 		reg = devm_regulator_get(&pdev->dev,
114 				config->hpd_reg_names[i]);
115 		if (IS_ERR(reg)) {
116 			ret = PTR_ERR(reg);
117 			dev_err(&pdev->dev, "failed to get hpd regulator: %s (%d)\n",
118 					config->hpd_reg_names[i], ret);
119 			goto fail;
120 		}
121 
122 		hdmi->hpd_regs[i] = reg;
123 	}
124 
125 	BUG_ON(config->pwr_reg_cnt > ARRAY_SIZE(hdmi->pwr_regs));
126 	for (i = 0; i < config->pwr_reg_cnt; i++) {
127 		struct regulator *reg;
128 
129 		reg = devm_regulator_get(&pdev->dev,
130 				config->pwr_reg_names[i]);
131 		if (IS_ERR(reg)) {
132 			ret = PTR_ERR(reg);
133 			dev_err(&pdev->dev, "failed to get pwr regulator: %s (%d)\n",
134 					config->pwr_reg_names[i], ret);
135 			goto fail;
136 		}
137 
138 		hdmi->pwr_regs[i] = reg;
139 	}
140 
141 	BUG_ON(config->hpd_clk_cnt > ARRAY_SIZE(hdmi->hpd_clks));
142 	for (i = 0; i < config->hpd_clk_cnt; i++) {
143 		struct clk *clk;
144 
145 		clk = devm_clk_get(&pdev->dev, config->hpd_clk_names[i]);
146 		if (IS_ERR(clk)) {
147 			ret = PTR_ERR(clk);
148 			dev_err(&pdev->dev, "failed to get hpd clk: %s (%d)\n",
149 					config->hpd_clk_names[i], ret);
150 			goto fail;
151 		}
152 
153 		hdmi->hpd_clks[i] = clk;
154 	}
155 
156 	BUG_ON(config->pwr_clk_cnt > ARRAY_SIZE(hdmi->pwr_clks));
157 	for (i = 0; i < config->pwr_clk_cnt; i++) {
158 		struct clk *clk;
159 
160 		clk = devm_clk_get(&pdev->dev, config->pwr_clk_names[i]);
161 		if (IS_ERR(clk)) {
162 			ret = PTR_ERR(clk);
163 			dev_err(&pdev->dev, "failed to get pwr clk: %s (%d)\n",
164 					config->pwr_clk_names[i], ret);
165 			goto fail;
166 		}
167 
168 		hdmi->pwr_clks[i] = clk;
169 	}
170 
171 	hdmi->i2c = hdmi_i2c_init(hdmi);
172 	if (IS_ERR(hdmi->i2c)) {
173 		ret = PTR_ERR(hdmi->i2c);
174 		dev_err(&pdev->dev, "failed to get i2c: %d\n", ret);
175 		hdmi->i2c = NULL;
176 		goto fail;
177 	}
178 
179 	return hdmi;
180 
181 fail:
182 	if (hdmi)
183 		hdmi_destroy(hdmi);
184 
185 	return ERR_PTR(ret);
186 }
187 
188 /* Second part of initialization, the drm/kms level modeset_init,
189  * constructs/initializes mode objects, etc, is called from master
190  * driver (not hdmi sub-device's probe/bind!)
191  *
192  * Any resource (regulator/clk/etc) which could be missing at boot
193  * should be handled in hdmi_init() so that failure happens from
194  * hdmi sub-device's probe.
195  */
196 int hdmi_modeset_init(struct hdmi *hdmi,
197 		struct drm_device *dev, struct drm_encoder *encoder)
198 {
199 	struct msm_drm_private *priv = dev->dev_private;
200 	struct platform_device *pdev = hdmi->pdev;
201 	int ret;
202 
203 	hdmi->dev = dev;
204 	hdmi->encoder = encoder;
205 
206 	hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
207 
208 	hdmi->bridge = hdmi_bridge_init(hdmi);
209 	if (IS_ERR(hdmi->bridge)) {
210 		ret = PTR_ERR(hdmi->bridge);
211 		dev_err(dev->dev, "failed to create HDMI bridge: %d\n", ret);
212 		hdmi->bridge = NULL;
213 		goto fail;
214 	}
215 
216 	hdmi->connector = hdmi_connector_init(hdmi);
217 	if (IS_ERR(hdmi->connector)) {
218 		ret = PTR_ERR(hdmi->connector);
219 		dev_err(dev->dev, "failed to create HDMI connector: %d\n", ret);
220 		hdmi->connector = NULL;
221 		goto fail;
222 	}
223 
224 	hdmi->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
225 	if (hdmi->irq < 0) {
226 		ret = hdmi->irq;
227 		dev_err(dev->dev, "failed to get irq: %d\n", ret);
228 		goto fail;
229 	}
230 
231 	ret = devm_request_irq(&pdev->dev, hdmi->irq,
232 			hdmi_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
233 			"hdmi_isr", hdmi);
234 	if (ret < 0) {
235 		dev_err(dev->dev, "failed to request IRQ%u: %d\n",
236 				hdmi->irq, ret);
237 		goto fail;
238 	}
239 
240 	encoder->bridge = hdmi->bridge;
241 
242 	priv->bridges[priv->num_bridges++]       = hdmi->bridge;
243 	priv->connectors[priv->num_connectors++] = hdmi->connector;
244 
245 	platform_set_drvdata(pdev, hdmi);
246 
247 	return 0;
248 
249 fail:
250 	/* bridge/connector are normally destroyed by drm: */
251 	if (hdmi->bridge) {
252 		hdmi->bridge->funcs->destroy(hdmi->bridge);
253 		hdmi->bridge = NULL;
254 	}
255 	if (hdmi->connector) {
256 		hdmi->connector->funcs->destroy(hdmi->connector);
257 		hdmi->connector = NULL;
258 	}
259 
260 	return ret;
261 }
262 
263 /*
264  * The hdmi device:
265  */
266 
267 #include <linux/of_gpio.h>
268 
269 #ifdef CONFIG_OF
270 static int get_gpio(struct device *dev, struct device_node *of_node, const char *name)
271 {
272 	int gpio = of_get_named_gpio(of_node, name, 0);
273 	if (gpio < 0) {
274 		char name2[32];
275 		snprintf(name2, sizeof(name2), "%s-gpio", name);
276 		gpio = of_get_named_gpio(of_node, name2, 0);
277 		if (gpio < 0) {
278 			dev_err(dev, "failed to get gpio: %s (%d)\n",
279 					name, gpio);
280 			gpio = -1;
281 		}
282 	}
283 	return gpio;
284 }
285 #endif
286 
287 static int hdmi_bind(struct device *dev, struct device *master, void *data)
288 {
289 	struct drm_device *drm = dev_get_drvdata(master);
290 	struct msm_drm_private *priv = drm->dev_private;
291 	static struct hdmi_platform_config config = {};
292 	struct hdmi *hdmi;
293 #ifdef CONFIG_OF
294 	struct device_node *of_node = dev->of_node;
295 
296 	if (of_device_is_compatible(of_node, "qcom,hdmi-tx-8074")) {
297 		static const char *hpd_reg_names[] = {"hpd-gdsc", "hpd-5v"};
298 		static const char *pwr_reg_names[] = {"core-vdda", "core-vcc"};
299 		static const char *hpd_clk_names[] = {"iface_clk", "core_clk", "mdp_core_clk"};
300 		static unsigned long hpd_clk_freq[] = {0, 19200000, 0};
301 		static const char *pwr_clk_names[] = {"extp_clk", "alt_iface_clk"};
302 		config.phy_init      = hdmi_phy_8x74_init;
303 		config.hpd_reg_names = hpd_reg_names;
304 		config.hpd_reg_cnt   = ARRAY_SIZE(hpd_reg_names);
305 		config.pwr_reg_names = pwr_reg_names;
306 		config.pwr_reg_cnt   = ARRAY_SIZE(pwr_reg_names);
307 		config.hpd_clk_names = hpd_clk_names;
308 		config.hpd_freq      = hpd_clk_freq;
309 		config.hpd_clk_cnt   = ARRAY_SIZE(hpd_clk_names);
310 		config.pwr_clk_names = pwr_clk_names;
311 		config.pwr_clk_cnt   = ARRAY_SIZE(pwr_clk_names);
312 	} else if (of_device_is_compatible(of_node, "qcom,hdmi-tx-8960")) {
313 		static const char *hpd_clk_names[] = {"core_clk", "master_iface_clk", "slave_iface_clk"};
314 		static const char *hpd_reg_names[] = {"core-vdda", "hdmi-mux"};
315 		config.phy_init      = hdmi_phy_8960_init;
316 		config.hpd_reg_names = hpd_reg_names;
317 		config.hpd_reg_cnt   = ARRAY_SIZE(hpd_reg_names);
318 		config.hpd_clk_names = hpd_clk_names;
319 		config.hpd_clk_cnt   = ARRAY_SIZE(hpd_clk_names);
320 	} else if (of_device_is_compatible(of_node, "qcom,hdmi-tx-8660")) {
321 		config.phy_init      = hdmi_phy_8x60_init;
322 	} else {
323 		dev_err(dev, "unknown phy: %s\n", of_node->name);
324 	}
325 
326 	config.mmio_name     = "core_physical";
327 	config.ddc_clk_gpio  = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-clk");
328 	config.ddc_data_gpio = get_gpio(dev, of_node, "qcom,hdmi-tx-ddc-data");
329 	config.hpd_gpio      = get_gpio(dev, of_node, "qcom,hdmi-tx-hpd");
330 	config.mux_en_gpio   = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-en");
331 	config.mux_sel_gpio  = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-sel");
332 	config.mux_lpm_gpio  = get_gpio(dev, of_node, "qcom,hdmi-tx-mux-lpm");
333 
334 #else
335 	static const char *hpd_clk_names[] = {
336 			"core_clk", "master_iface_clk", "slave_iface_clk",
337 	};
338 	if (cpu_is_apq8064()) {
339 		static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
340 		config.phy_init      = hdmi_phy_8960_init;
341 		config.mmio_name     = "hdmi_msm_hdmi_addr";
342 		config.hpd_reg_names = hpd_reg_names;
343 		config.hpd_reg_cnt   = ARRAY_SIZE(hpd_reg_names);
344 		config.hpd_clk_names = hpd_clk_names;
345 		config.hpd_clk_cnt   = ARRAY_SIZE(hpd_clk_names);
346 		config.ddc_clk_gpio  = 70;
347 		config.ddc_data_gpio = 71;
348 		config.hpd_gpio      = 72;
349 		config.mux_en_gpio   = -1;
350 		config.mux_sel_gpio  = -1;
351 	} else if (cpu_is_msm8960() || cpu_is_msm8960ab()) {
352 		static const char *hpd_reg_names[] = {"8921_hdmi_mvs"};
353 		config.phy_init      = hdmi_phy_8960_init;
354 		config.mmio_name     = "hdmi_msm_hdmi_addr";
355 		config.hpd_reg_names = hpd_reg_names;
356 		config.hpd_reg_cnt   = ARRAY_SIZE(hpd_reg_names);
357 		config.hpd_clk_names = hpd_clk_names;
358 		config.hpd_clk_cnt   = ARRAY_SIZE(hpd_clk_names);
359 		config.ddc_clk_gpio  = 100;
360 		config.ddc_data_gpio = 101;
361 		config.hpd_gpio      = 102;
362 		config.mux_en_gpio   = -1;
363 		config.mux_sel_gpio  = -1;
364 	} else if (cpu_is_msm8x60()) {
365 		static const char *hpd_reg_names[] = {
366 				"8901_hdmi_mvs", "8901_mpp0"
367 		};
368 		config.phy_init      = hdmi_phy_8x60_init;
369 		config.mmio_name     = "hdmi_msm_hdmi_addr";
370 		config.hpd_reg_names = hpd_reg_names;
371 		config.hpd_reg_cnt   = ARRAY_SIZE(hpd_reg_names);
372 		config.hpd_clk_names = hpd_clk_names;
373 		config.hpd_clk_cnt   = ARRAY_SIZE(hpd_clk_names);
374 		config.ddc_clk_gpio  = 170;
375 		config.ddc_data_gpio = 171;
376 		config.hpd_gpio      = 172;
377 		config.mux_en_gpio   = -1;
378 		config.mux_sel_gpio  = -1;
379 	}
380 #endif
381 	dev->platform_data = &config;
382 	hdmi = hdmi_init(to_platform_device(dev));
383 	if (IS_ERR(hdmi))
384 		return PTR_ERR(hdmi);
385 	priv->hdmi = hdmi;
386 	return 0;
387 }
388 
389 static void hdmi_unbind(struct device *dev, struct device *master,
390 		void *data)
391 {
392 	struct drm_device *drm = dev_get_drvdata(master);
393 	struct msm_drm_private *priv = drm->dev_private;
394 	if (priv->hdmi) {
395 		hdmi_destroy(priv->hdmi);
396 		priv->hdmi = NULL;
397 	}
398 }
399 
400 static const struct component_ops hdmi_ops = {
401 		.bind   = hdmi_bind,
402 		.unbind = hdmi_unbind,
403 };
404 
405 static int hdmi_dev_probe(struct platform_device *pdev)
406 {
407 	return component_add(&pdev->dev, &hdmi_ops);
408 }
409 
410 static int hdmi_dev_remove(struct platform_device *pdev)
411 {
412 	component_del(&pdev->dev, &hdmi_ops);
413 	return 0;
414 }
415 
416 static const struct of_device_id dt_match[] = {
417 	{ .compatible = "qcom,hdmi-tx-8074" },
418 	{ .compatible = "qcom,hdmi-tx-8960" },
419 	{ .compatible = "qcom,hdmi-tx-8660" },
420 	{}
421 };
422 
423 static struct platform_driver hdmi_driver = {
424 	.probe = hdmi_dev_probe,
425 	.remove = hdmi_dev_remove,
426 	.driver = {
427 		.name = "hdmi_msm",
428 		.of_match_table = dt_match,
429 	},
430 };
431 
432 void __init hdmi_register(void)
433 {
434 	platform_driver_register(&hdmi_driver);
435 }
436 
437 void __exit hdmi_unregister(void)
438 {
439 	platform_driver_unregister(&hdmi_driver);
440 }
441