xref: /openbmc/linux/drivers/gpu/drm/msm/hdmi/hdmi.c (revision 6b43538f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014 The Linux Foundation. All rights reserved.
4  * Copyright (C) 2013 Red Hat
5  * Author: Rob Clark <robdclark@gmail.com>
6  */
7 
8 #include <linux/of_irq.h>
9 #include <linux/of_gpio.h>
10 
11 #include <drm/drm_bridge_connector.h>
12 #include <drm/drm_of.h>
13 
14 #include <sound/hdmi-codec.h>
15 #include "hdmi.h"
16 
17 void msm_hdmi_set_mode(struct hdmi *hdmi, bool power_on)
18 {
19 	uint32_t ctrl = 0;
20 	unsigned long flags;
21 
22 	spin_lock_irqsave(&hdmi->reg_lock, flags);
23 	if (power_on) {
24 		ctrl |= HDMI_CTRL_ENABLE;
25 		if (!hdmi->hdmi_mode) {
26 			ctrl |= HDMI_CTRL_HDMI;
27 			hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
28 			ctrl &= ~HDMI_CTRL_HDMI;
29 		} else {
30 			ctrl |= HDMI_CTRL_HDMI;
31 		}
32 	} else {
33 		ctrl = HDMI_CTRL_HDMI;
34 	}
35 
36 	hdmi_write(hdmi, REG_HDMI_CTRL, ctrl);
37 	spin_unlock_irqrestore(&hdmi->reg_lock, flags);
38 	DBG("HDMI Core: %s, HDMI_CTRL=0x%08x",
39 			power_on ? "Enable" : "Disable", ctrl);
40 }
41 
42 static irqreturn_t msm_hdmi_irq(int irq, void *dev_id)
43 {
44 	struct hdmi *hdmi = dev_id;
45 
46 	/* Process HPD: */
47 	msm_hdmi_hpd_irq(hdmi->bridge);
48 
49 	/* Process DDC: */
50 	msm_hdmi_i2c_irq(hdmi->i2c);
51 
52 	/* Process HDCP: */
53 	if (hdmi->hdcp_ctrl)
54 		msm_hdmi_hdcp_irq(hdmi->hdcp_ctrl);
55 
56 	/* TODO audio.. */
57 
58 	return IRQ_HANDLED;
59 }
60 
61 static void msm_hdmi_destroy(struct hdmi *hdmi)
62 {
63 	/*
64 	 * at this point, hpd has been disabled,
65 	 * after flush workq, it's safe to deinit hdcp
66 	 */
67 	if (hdmi->workq)
68 		destroy_workqueue(hdmi->workq);
69 	msm_hdmi_hdcp_destroy(hdmi);
70 
71 	if (hdmi->i2c)
72 		msm_hdmi_i2c_destroy(hdmi->i2c);
73 }
74 
75 static void msm_hdmi_put_phy(struct hdmi *hdmi)
76 {
77 	if (hdmi->phy_dev) {
78 		put_device(hdmi->phy_dev);
79 		hdmi->phy = NULL;
80 		hdmi->phy_dev = NULL;
81 	}
82 }
83 
84 static int msm_hdmi_get_phy(struct hdmi *hdmi)
85 {
86 	struct platform_device *pdev = hdmi->pdev;
87 	struct platform_device *phy_pdev;
88 	struct device_node *phy_node;
89 
90 	phy_node = of_parse_phandle(pdev->dev.of_node, "phys", 0);
91 	if (!phy_node) {
92 		DRM_DEV_ERROR(&pdev->dev, "cannot find phy device\n");
93 		return -ENXIO;
94 	}
95 
96 	phy_pdev = of_find_device_by_node(phy_node);
97 	of_node_put(phy_node);
98 
99 	if (!phy_pdev)
100 		return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n");
101 
102 	hdmi->phy = platform_get_drvdata(phy_pdev);
103 	if (!hdmi->phy) {
104 		put_device(&phy_pdev->dev);
105 		return dev_err_probe(&pdev->dev, -EPROBE_DEFER, "phy driver is not ready\n");
106 	}
107 
108 	hdmi->phy_dev = &phy_pdev->dev;
109 
110 	return 0;
111 }
112 
113 /* construct hdmi at bind/probe time, grab all the resources.  If
114  * we are to EPROBE_DEFER we want to do it here, rather than later
115  * at modeset_init() time
116  */
117 static int msm_hdmi_init(struct hdmi *hdmi)
118 {
119 	struct platform_device *pdev = hdmi->pdev;
120 	int ret;
121 
122 	hdmi->workq = alloc_ordered_workqueue("msm_hdmi", 0);
123 
124 	hdmi->i2c = msm_hdmi_i2c_init(hdmi);
125 	if (IS_ERR(hdmi->i2c)) {
126 		ret = PTR_ERR(hdmi->i2c);
127 		DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret);
128 		hdmi->i2c = NULL;
129 		goto fail;
130 	}
131 
132 	hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
133 	if (IS_ERR(hdmi->hdcp_ctrl)) {
134 		dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
135 		hdmi->hdcp_ctrl = NULL;
136 	}
137 
138 	return 0;
139 
140 fail:
141 	msm_hdmi_destroy(hdmi);
142 
143 	return ret;
144 }
145 
146 /* Second part of initialization, the drm/kms level modeset_init,
147  * constructs/initializes mode objects, etc, is called from master
148  * driver (not hdmi sub-device's probe/bind!)
149  *
150  * Any resource (regulator/clk/etc) which could be missing at boot
151  * should be handled in msm_hdmi_init() so that failure happens from
152  * hdmi sub-device's probe.
153  */
154 int msm_hdmi_modeset_init(struct hdmi *hdmi,
155 		struct drm_device *dev, struct drm_encoder *encoder)
156 {
157 	struct msm_drm_private *priv = dev->dev_private;
158 	int ret;
159 
160 	if (priv->num_bridges == ARRAY_SIZE(priv->bridges)) {
161 		DRM_DEV_ERROR(dev->dev, "too many bridges\n");
162 		return -ENOSPC;
163 	}
164 
165 	hdmi->dev = dev;
166 	hdmi->encoder = encoder;
167 
168 	hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
169 
170 	hdmi->bridge = msm_hdmi_bridge_init(hdmi);
171 	if (IS_ERR(hdmi->bridge)) {
172 		ret = PTR_ERR(hdmi->bridge);
173 		DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
174 		hdmi->bridge = NULL;
175 		goto fail;
176 	}
177 
178 	if (hdmi->next_bridge) {
179 		ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge,
180 					DRM_BRIDGE_ATTACH_NO_CONNECTOR);
181 		if (ret) {
182 			DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret);
183 			goto fail;
184 		}
185 	}
186 
187 	hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
188 	if (IS_ERR(hdmi->connector)) {
189 		ret = PTR_ERR(hdmi->connector);
190 		DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
191 		hdmi->connector = NULL;
192 		goto fail;
193 	}
194 
195 	drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
196 
197 	ret = devm_request_irq(dev->dev, hdmi->irq,
198 			msm_hdmi_irq, IRQF_TRIGGER_HIGH,
199 			"hdmi_isr", hdmi);
200 	if (ret < 0) {
201 		DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
202 				hdmi->irq, ret);
203 		goto fail;
204 	}
205 
206 	drm_bridge_connector_enable_hpd(hdmi->connector);
207 
208 	ret = msm_hdmi_hpd_enable(hdmi->bridge);
209 	if (ret < 0) {
210 		DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
211 		goto fail;
212 	}
213 
214 	priv->bridges[priv->num_bridges++]       = hdmi->bridge;
215 
216 	return 0;
217 
218 fail:
219 	/* bridge is normally destroyed by drm: */
220 	if (hdmi->bridge) {
221 		msm_hdmi_bridge_destroy(hdmi->bridge);
222 		hdmi->bridge = NULL;
223 	}
224 	if (hdmi->connector) {
225 		hdmi->connector->funcs->destroy(hdmi->connector);
226 		hdmi->connector = NULL;
227 	}
228 
229 	return ret;
230 }
231 
232 /*
233  * The hdmi device:
234  */
235 
236 #define HDMI_CFG(item, entry) \
237 	.item ## _names = item ##_names_ ## entry, \
238 	.item ## _cnt   = ARRAY_SIZE(item ## _names_ ## entry)
239 
240 static const char *hpd_reg_names_8960[] = {"core-vdda"};
241 static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
242 
243 static const struct hdmi_platform_config hdmi_tx_8960_config = {
244 		HDMI_CFG(hpd_reg, 8960),
245 		HDMI_CFG(hpd_clk, 8960),
246 };
247 
248 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
249 static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
250 static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
251 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
252 
253 static const struct hdmi_platform_config hdmi_tx_8974_config = {
254 		HDMI_CFG(pwr_reg, 8x74),
255 		HDMI_CFG(pwr_clk, 8x74),
256 		HDMI_CFG(hpd_clk, 8x74),
257 		.hpd_freq      = hpd_clk_freq_8x74,
258 };
259 
260 /*
261  * HDMI audio codec callbacks
262  */
263 static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
264 				    struct hdmi_codec_daifmt *daifmt,
265 				    struct hdmi_codec_params *params)
266 {
267 	struct hdmi *hdmi = dev_get_drvdata(dev);
268 	unsigned int chan;
269 	unsigned int channel_allocation = 0;
270 	unsigned int rate;
271 	unsigned int level_shift  = 0; /* 0dB */
272 	bool down_mix = false;
273 
274 	DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
275 		 params->sample_width, params->cea.channels);
276 
277 	switch (params->cea.channels) {
278 	case 2:
279 		/* FR and FL speakers */
280 		channel_allocation  = 0;
281 		chan = MSM_HDMI_AUDIO_CHANNEL_2;
282 		break;
283 	case 4:
284 		/* FC, LFE, FR and FL speakers */
285 		channel_allocation  = 0x3;
286 		chan = MSM_HDMI_AUDIO_CHANNEL_4;
287 		break;
288 	case 6:
289 		/* RR, RL, FC, LFE, FR and FL speakers */
290 		channel_allocation  = 0x0B;
291 		chan = MSM_HDMI_AUDIO_CHANNEL_6;
292 		break;
293 	case 8:
294 		/* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
295 		channel_allocation  = 0x1F;
296 		chan = MSM_HDMI_AUDIO_CHANNEL_8;
297 		break;
298 	default:
299 		return -EINVAL;
300 	}
301 
302 	switch (params->sample_rate) {
303 	case 32000:
304 		rate = HDMI_SAMPLE_RATE_32KHZ;
305 		break;
306 	case 44100:
307 		rate = HDMI_SAMPLE_RATE_44_1KHZ;
308 		break;
309 	case 48000:
310 		rate = HDMI_SAMPLE_RATE_48KHZ;
311 		break;
312 	case 88200:
313 		rate = HDMI_SAMPLE_RATE_88_2KHZ;
314 		break;
315 	case 96000:
316 		rate = HDMI_SAMPLE_RATE_96KHZ;
317 		break;
318 	case 176400:
319 		rate = HDMI_SAMPLE_RATE_176_4KHZ;
320 		break;
321 	case 192000:
322 		rate = HDMI_SAMPLE_RATE_192KHZ;
323 		break;
324 	default:
325 		DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
326 			params->sample_rate);
327 		return -EINVAL;
328 	}
329 
330 	msm_hdmi_audio_set_sample_rate(hdmi, rate);
331 	msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
332 			      level_shift, down_mix);
333 
334 	return 0;
335 }
336 
337 static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
338 {
339 	struct hdmi *hdmi = dev_get_drvdata(dev);
340 
341 	msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
342 }
343 
344 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
345 	.hw_params = msm_hdmi_audio_hw_params,
346 	.audio_shutdown = msm_hdmi_audio_shutdown,
347 };
348 
349 static struct hdmi_codec_pdata codec_data = {
350 	.ops = &msm_hdmi_audio_codec_ops,
351 	.max_i2s_channels = 8,
352 	.i2s = 1,
353 };
354 
355 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
356 {
357 	hdmi->audio_pdev = platform_device_register_data(dev,
358 							 HDMI_CODEC_DRV_NAME,
359 							 PLATFORM_DEVID_AUTO,
360 							 &codec_data,
361 							 sizeof(codec_data));
362 	return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
363 }
364 
365 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
366 {
367 	struct msm_drm_private *priv = dev_get_drvdata(master);
368 	struct hdmi *hdmi = dev_get_drvdata(dev);
369 	int err;
370 
371 	err = msm_hdmi_init(hdmi);
372 	if (err)
373 		return err;
374 	priv->hdmi = hdmi;
375 
376 	err = msm_hdmi_register_audio_driver(hdmi, dev);
377 	if (err) {
378 		DRM_ERROR("Failed to attach an audio codec %d\n", err);
379 		hdmi->audio_pdev = NULL;
380 	}
381 
382 	return 0;
383 }
384 
385 static void msm_hdmi_unbind(struct device *dev, struct device *master,
386 		void *data)
387 {
388 	struct msm_drm_private *priv = dev_get_drvdata(master);
389 
390 	if (priv->hdmi) {
391 		if (priv->hdmi->audio_pdev)
392 			platform_device_unregister(priv->hdmi->audio_pdev);
393 
394 		msm_hdmi_destroy(priv->hdmi);
395 		priv->hdmi = NULL;
396 	}
397 }
398 
399 static const struct component_ops msm_hdmi_ops = {
400 		.bind   = msm_hdmi_bind,
401 		.unbind = msm_hdmi_unbind,
402 };
403 
404 static int msm_hdmi_dev_probe(struct platform_device *pdev)
405 {
406 	const struct hdmi_platform_config *config;
407 	struct device *dev = &pdev->dev;
408 	struct hdmi *hdmi;
409 	struct resource *res;
410 	int i, ret;
411 
412 	config = of_device_get_match_data(dev);
413 	if (!config)
414 		return -EINVAL;
415 
416 	hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
417 	if (!hdmi)
418 		return -ENOMEM;
419 
420 	hdmi->pdev = pdev;
421 	hdmi->config = config;
422 	spin_lock_init(&hdmi->reg_lock);
423 
424 	ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge);
425 	if (ret && ret != -ENODEV)
426 		return ret;
427 
428 	hdmi->mmio = msm_ioremap(pdev, "core_physical");
429 	if (IS_ERR(hdmi->mmio))
430 		return PTR_ERR(hdmi->mmio);
431 
432 	/* HDCP needs physical address of hdmi register */
433 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
434 		"core_physical");
435 	if (!res)
436 		return -EINVAL;
437 	hdmi->mmio_phy_addr = res->start;
438 
439 	hdmi->qfprom_mmio = msm_ioremap(pdev, "qfprom_physical");
440 	if (IS_ERR(hdmi->qfprom_mmio)) {
441 		DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
442 		hdmi->qfprom_mmio = NULL;
443 	}
444 
445 	hdmi->irq = platform_get_irq(pdev, 0);
446 	if (hdmi->irq < 0)
447 		return hdmi->irq;
448 
449 	hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
450 				      config->hpd_reg_cnt,
451 				      sizeof(hdmi->hpd_regs[0]),
452 				      GFP_KERNEL);
453 	if (!hdmi->hpd_regs)
454 		return -ENOMEM;
455 
456 	for (i = 0; i < config->hpd_reg_cnt; i++)
457 		hdmi->hpd_regs[i].supply = config->hpd_reg_names[i];
458 
459 	ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs);
460 	if (ret)
461 		return dev_err_probe(dev, ret, "failed to get hpd regulators\n");
462 
463 	hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
464 				      config->pwr_reg_cnt,
465 				      sizeof(hdmi->pwr_regs[0]),
466 				      GFP_KERNEL);
467 	if (!hdmi->pwr_regs)
468 		return -ENOMEM;
469 
470 	for (i = 0; i < config->pwr_reg_cnt; i++)
471 		hdmi->pwr_regs[i].supply = config->pwr_reg_names[i];
472 
473 	ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
474 	if (ret)
475 		return dev_err_probe(dev, ret, "failed to get pwr regulators\n");
476 
477 	hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
478 				      config->hpd_clk_cnt,
479 				      sizeof(hdmi->hpd_clks[0]),
480 				      GFP_KERNEL);
481 	if (!hdmi->hpd_clks)
482 		return -ENOMEM;
483 
484 	for (i = 0; i < config->hpd_clk_cnt; i++) {
485 		struct clk *clk;
486 
487 		clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
488 		if (IS_ERR(clk))
489 			return dev_err_probe(dev, PTR_ERR(clk),
490 					     "failed to get hpd clk: %s\n",
491 					     config->hpd_clk_names[i]);
492 
493 		hdmi->hpd_clks[i] = clk;
494 	}
495 
496 	hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
497 				      config->pwr_clk_cnt,
498 				      sizeof(hdmi->pwr_clks[0]),
499 				      GFP_KERNEL);
500 	if (!hdmi->pwr_clks)
501 		return -ENOMEM;
502 
503 	for (i = 0; i < config->pwr_clk_cnt; i++) {
504 		struct clk *clk;
505 
506 		clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
507 		if (IS_ERR(clk))
508 			return dev_err_probe(dev, PTR_ERR(clk),
509 					     "failed to get pwr clk: %s\n",
510 					     config->pwr_clk_names[i]);
511 
512 		hdmi->pwr_clks[i] = clk;
513 	}
514 
515 	hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
516 	/* This will catch e.g. -EPROBE_DEFER */
517 	if (IS_ERR(hdmi->hpd_gpiod))
518 		return dev_err_probe(dev, PTR_ERR(hdmi->hpd_gpiod),
519 				     "failed to get hpd gpio\n");
520 
521 	if (!hdmi->hpd_gpiod)
522 		DBG("failed to get HPD gpio");
523 
524 	if (hdmi->hpd_gpiod)
525 		gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD");
526 
527 	ret = msm_hdmi_get_phy(hdmi);
528 	if (ret) {
529 		DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
530 		return ret;
531 	}
532 
533 	ret = devm_pm_runtime_enable(&pdev->dev);
534 	if (ret)
535 		return ret;
536 
537 	platform_set_drvdata(pdev, hdmi);
538 
539 	return component_add(&pdev->dev, &msm_hdmi_ops);
540 }
541 
542 static int msm_hdmi_dev_remove(struct platform_device *pdev)
543 {
544 	struct hdmi *hdmi = dev_get_drvdata(&pdev->dev);
545 
546 	component_del(&pdev->dev, &msm_hdmi_ops);
547 
548 	msm_hdmi_put_phy(hdmi);
549 
550 	return 0;
551 }
552 
553 static const struct of_device_id msm_hdmi_dt_match[] = {
554 	{ .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config },
555 	{ .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config },
556 	{ .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config },
557 	{ .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
558 	{ .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
559 	{ .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config },
560 	{}
561 };
562 
563 static struct platform_driver msm_hdmi_driver = {
564 	.probe = msm_hdmi_dev_probe,
565 	.remove = msm_hdmi_dev_remove,
566 	.driver = {
567 		.name = "hdmi_msm",
568 		.of_match_table = msm_hdmi_dt_match,
569 	},
570 };
571 
572 void __init msm_hdmi_register(void)
573 {
574 	msm_hdmi_phy_driver_register();
575 	platform_driver_register(&msm_hdmi_driver);
576 }
577 
578 void __exit msm_hdmi_unregister(void)
579 {
580 	platform_driver_unregister(&msm_hdmi_driver);
581 	msm_hdmi_phy_driver_unregister();
582 }
583