xref: /openbmc/linux/drivers/gpu/drm/msm/hdmi/hdmi.c (revision e98e7a82)
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 	if (!hdmi->workq) {
124 		ret = -ENOMEM;
125 		goto fail;
126 	}
127 
128 	hdmi->i2c = msm_hdmi_i2c_init(hdmi);
129 	if (IS_ERR(hdmi->i2c)) {
130 		ret = PTR_ERR(hdmi->i2c);
131 		DRM_DEV_ERROR(&pdev->dev, "failed to get i2c: %d\n", ret);
132 		hdmi->i2c = NULL;
133 		goto fail;
134 	}
135 
136 	hdmi->hdcp_ctrl = msm_hdmi_hdcp_init(hdmi);
137 	if (IS_ERR(hdmi->hdcp_ctrl)) {
138 		dev_warn(&pdev->dev, "failed to init hdcp: disabled\n");
139 		hdmi->hdcp_ctrl = NULL;
140 	}
141 
142 	return 0;
143 
144 fail:
145 	msm_hdmi_destroy(hdmi);
146 
147 	return ret;
148 }
149 
150 /* Second part of initialization, the drm/kms level modeset_init,
151  * constructs/initializes mode objects, etc, is called from master
152  * driver (not hdmi sub-device's probe/bind!)
153  *
154  * Any resource (regulator/clk/etc) which could be missing at boot
155  * should be handled in msm_hdmi_init() so that failure happens from
156  * hdmi sub-device's probe.
157  */
158 int msm_hdmi_modeset_init(struct hdmi *hdmi,
159 		struct drm_device *dev, struct drm_encoder *encoder)
160 {
161 	struct msm_drm_private *priv = dev->dev_private;
162 	int ret;
163 
164 	if (priv->num_bridges == ARRAY_SIZE(priv->bridges)) {
165 		DRM_DEV_ERROR(dev->dev, "too many bridges\n");
166 		return -ENOSPC;
167 	}
168 
169 	hdmi->dev = dev;
170 	hdmi->encoder = encoder;
171 
172 	hdmi_audio_infoframe_init(&hdmi->audio.infoframe);
173 
174 	hdmi->bridge = msm_hdmi_bridge_init(hdmi);
175 	if (IS_ERR(hdmi->bridge)) {
176 		ret = PTR_ERR(hdmi->bridge);
177 		DRM_DEV_ERROR(dev->dev, "failed to create HDMI bridge: %d\n", ret);
178 		hdmi->bridge = NULL;
179 		goto fail;
180 	}
181 
182 	if (hdmi->next_bridge) {
183 		ret = drm_bridge_attach(hdmi->encoder, hdmi->next_bridge, hdmi->bridge,
184 					DRM_BRIDGE_ATTACH_NO_CONNECTOR);
185 		if (ret) {
186 			DRM_DEV_ERROR(dev->dev, "failed to attach next HDMI bridge: %d\n", ret);
187 			goto fail;
188 		}
189 	}
190 
191 	hdmi->connector = drm_bridge_connector_init(hdmi->dev, encoder);
192 	if (IS_ERR(hdmi->connector)) {
193 		ret = PTR_ERR(hdmi->connector);
194 		DRM_DEV_ERROR(dev->dev, "failed to create HDMI connector: %d\n", ret);
195 		hdmi->connector = NULL;
196 		goto fail;
197 	}
198 
199 	drm_connector_attach_encoder(hdmi->connector, hdmi->encoder);
200 
201 	ret = devm_request_irq(dev->dev, hdmi->irq,
202 			msm_hdmi_irq, IRQF_TRIGGER_HIGH,
203 			"hdmi_isr", hdmi);
204 	if (ret < 0) {
205 		DRM_DEV_ERROR(dev->dev, "failed to request IRQ%u: %d\n",
206 				hdmi->irq, ret);
207 		goto fail;
208 	}
209 
210 	ret = msm_hdmi_hpd_enable(hdmi->bridge);
211 	if (ret < 0) {
212 		DRM_DEV_ERROR(&hdmi->pdev->dev, "failed to enable HPD: %d\n", ret);
213 		goto fail;
214 	}
215 
216 	priv->bridges[priv->num_bridges++]       = hdmi->bridge;
217 
218 	return 0;
219 
220 fail:
221 	/* bridge is normally destroyed by drm: */
222 	if (hdmi->bridge) {
223 		msm_hdmi_bridge_destroy(hdmi->bridge);
224 		hdmi->bridge = NULL;
225 	}
226 	if (hdmi->connector) {
227 		hdmi->connector->funcs->destroy(hdmi->connector);
228 		hdmi->connector = NULL;
229 	}
230 
231 	return ret;
232 }
233 
234 /*
235  * The hdmi device:
236  */
237 
238 #define HDMI_CFG(item, entry) \
239 	.item ## _names = item ##_names_ ## entry, \
240 	.item ## _cnt   = ARRAY_SIZE(item ## _names_ ## entry)
241 
242 static const char *hpd_reg_names_8960[] = {"core-vdda"};
243 static const char *hpd_clk_names_8960[] = {"core", "master_iface", "slave_iface"};
244 
245 static const struct hdmi_platform_config hdmi_tx_8960_config = {
246 		HDMI_CFG(hpd_reg, 8960),
247 		HDMI_CFG(hpd_clk, 8960),
248 };
249 
250 static const char *pwr_reg_names_8x74[] = {"core-vdda", "core-vcc"};
251 static const char *pwr_clk_names_8x74[] = {"extp", "alt_iface"};
252 static const char *hpd_clk_names_8x74[] = {"iface", "core", "mdp_core"};
253 static unsigned long hpd_clk_freq_8x74[] = {0, 19200000, 0};
254 
255 static const struct hdmi_platform_config hdmi_tx_8974_config = {
256 		HDMI_CFG(pwr_reg, 8x74),
257 		HDMI_CFG(pwr_clk, 8x74),
258 		HDMI_CFG(hpd_clk, 8x74),
259 		.hpd_freq      = hpd_clk_freq_8x74,
260 };
261 
262 /*
263  * HDMI audio codec callbacks
264  */
265 static int msm_hdmi_audio_hw_params(struct device *dev, void *data,
266 				    struct hdmi_codec_daifmt *daifmt,
267 				    struct hdmi_codec_params *params)
268 {
269 	struct hdmi *hdmi = dev_get_drvdata(dev);
270 	unsigned int chan;
271 	unsigned int channel_allocation = 0;
272 	unsigned int rate;
273 	unsigned int level_shift  = 0; /* 0dB */
274 	bool down_mix = false;
275 
276 	DRM_DEV_DEBUG(dev, "%u Hz, %d bit, %d channels\n", params->sample_rate,
277 		 params->sample_width, params->cea.channels);
278 
279 	switch (params->cea.channels) {
280 	case 2:
281 		/* FR and FL speakers */
282 		channel_allocation  = 0;
283 		chan = MSM_HDMI_AUDIO_CHANNEL_2;
284 		break;
285 	case 4:
286 		/* FC, LFE, FR and FL speakers */
287 		channel_allocation  = 0x3;
288 		chan = MSM_HDMI_AUDIO_CHANNEL_4;
289 		break;
290 	case 6:
291 		/* RR, RL, FC, LFE, FR and FL speakers */
292 		channel_allocation  = 0x0B;
293 		chan = MSM_HDMI_AUDIO_CHANNEL_6;
294 		break;
295 	case 8:
296 		/* FRC, FLC, RR, RL, FC, LFE, FR and FL speakers */
297 		channel_allocation  = 0x1F;
298 		chan = MSM_HDMI_AUDIO_CHANNEL_8;
299 		break;
300 	default:
301 		return -EINVAL;
302 	}
303 
304 	switch (params->sample_rate) {
305 	case 32000:
306 		rate = HDMI_SAMPLE_RATE_32KHZ;
307 		break;
308 	case 44100:
309 		rate = HDMI_SAMPLE_RATE_44_1KHZ;
310 		break;
311 	case 48000:
312 		rate = HDMI_SAMPLE_RATE_48KHZ;
313 		break;
314 	case 88200:
315 		rate = HDMI_SAMPLE_RATE_88_2KHZ;
316 		break;
317 	case 96000:
318 		rate = HDMI_SAMPLE_RATE_96KHZ;
319 		break;
320 	case 176400:
321 		rate = HDMI_SAMPLE_RATE_176_4KHZ;
322 		break;
323 	case 192000:
324 		rate = HDMI_SAMPLE_RATE_192KHZ;
325 		break;
326 	default:
327 		DRM_DEV_ERROR(dev, "rate[%d] not supported!\n",
328 			params->sample_rate);
329 		return -EINVAL;
330 	}
331 
332 	msm_hdmi_audio_set_sample_rate(hdmi, rate);
333 	msm_hdmi_audio_info_setup(hdmi, 1, chan, channel_allocation,
334 			      level_shift, down_mix);
335 
336 	return 0;
337 }
338 
339 static void msm_hdmi_audio_shutdown(struct device *dev, void *data)
340 {
341 	struct hdmi *hdmi = dev_get_drvdata(dev);
342 
343 	msm_hdmi_audio_info_setup(hdmi, 0, 0, 0, 0, 0);
344 }
345 
346 static const struct hdmi_codec_ops msm_hdmi_audio_codec_ops = {
347 	.hw_params = msm_hdmi_audio_hw_params,
348 	.audio_shutdown = msm_hdmi_audio_shutdown,
349 };
350 
351 static struct hdmi_codec_pdata codec_data = {
352 	.ops = &msm_hdmi_audio_codec_ops,
353 	.max_i2s_channels = 8,
354 	.i2s = 1,
355 };
356 
357 static int msm_hdmi_register_audio_driver(struct hdmi *hdmi, struct device *dev)
358 {
359 	hdmi->audio_pdev = platform_device_register_data(dev,
360 							 HDMI_CODEC_DRV_NAME,
361 							 PLATFORM_DEVID_AUTO,
362 							 &codec_data,
363 							 sizeof(codec_data));
364 	return PTR_ERR_OR_ZERO(hdmi->audio_pdev);
365 }
366 
367 static int msm_hdmi_bind(struct device *dev, struct device *master, void *data)
368 {
369 	struct msm_drm_private *priv = dev_get_drvdata(master);
370 	struct hdmi *hdmi = dev_get_drvdata(dev);
371 	int err;
372 
373 	err = msm_hdmi_init(hdmi);
374 	if (err)
375 		return err;
376 	priv->hdmi = hdmi;
377 
378 	err = msm_hdmi_register_audio_driver(hdmi, dev);
379 	if (err) {
380 		DRM_ERROR("Failed to attach an audio codec %d\n", err);
381 		hdmi->audio_pdev = NULL;
382 	}
383 
384 	return 0;
385 }
386 
387 static void msm_hdmi_unbind(struct device *dev, struct device *master,
388 		void *data)
389 {
390 	struct msm_drm_private *priv = dev_get_drvdata(master);
391 
392 	if (priv->hdmi) {
393 		if (priv->hdmi->audio_pdev)
394 			platform_device_unregister(priv->hdmi->audio_pdev);
395 
396 		msm_hdmi_destroy(priv->hdmi);
397 		priv->hdmi = NULL;
398 	}
399 }
400 
401 static const struct component_ops msm_hdmi_ops = {
402 		.bind   = msm_hdmi_bind,
403 		.unbind = msm_hdmi_unbind,
404 };
405 
406 static int msm_hdmi_dev_probe(struct platform_device *pdev)
407 {
408 	const struct hdmi_platform_config *config;
409 	struct device *dev = &pdev->dev;
410 	struct hdmi *hdmi;
411 	struct resource *res;
412 	int i, ret;
413 
414 	config = of_device_get_match_data(dev);
415 	if (!config)
416 		return -EINVAL;
417 
418 	hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
419 	if (!hdmi)
420 		return -ENOMEM;
421 
422 	hdmi->pdev = pdev;
423 	hdmi->config = config;
424 	spin_lock_init(&hdmi->reg_lock);
425 
426 	ret = drm_of_find_panel_or_bridge(pdev->dev.of_node, 1, 0, NULL, &hdmi->next_bridge);
427 	if (ret && ret != -ENODEV)
428 		return ret;
429 
430 	hdmi->mmio = msm_ioremap(pdev, "core_physical");
431 	if (IS_ERR(hdmi->mmio))
432 		return PTR_ERR(hdmi->mmio);
433 
434 	/* HDCP needs physical address of hdmi register */
435 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
436 		"core_physical");
437 	if (!res)
438 		return -EINVAL;
439 	hdmi->mmio_phy_addr = res->start;
440 
441 	hdmi->qfprom_mmio = msm_ioremap(pdev, "qfprom_physical");
442 	if (IS_ERR(hdmi->qfprom_mmio)) {
443 		DRM_DEV_INFO(&pdev->dev, "can't find qfprom resource\n");
444 		hdmi->qfprom_mmio = NULL;
445 	}
446 
447 	hdmi->irq = platform_get_irq(pdev, 0);
448 	if (hdmi->irq < 0)
449 		return hdmi->irq;
450 
451 	hdmi->hpd_regs = devm_kcalloc(&pdev->dev,
452 				      config->hpd_reg_cnt,
453 				      sizeof(hdmi->hpd_regs[0]),
454 				      GFP_KERNEL);
455 	if (!hdmi->hpd_regs)
456 		return -ENOMEM;
457 
458 	for (i = 0; i < config->hpd_reg_cnt; i++)
459 		hdmi->hpd_regs[i].supply = config->hpd_reg_names[i];
460 
461 	ret = devm_regulator_bulk_get(&pdev->dev, config->hpd_reg_cnt, hdmi->hpd_regs);
462 	if (ret)
463 		return dev_err_probe(dev, ret, "failed to get hpd regulators\n");
464 
465 	hdmi->pwr_regs = devm_kcalloc(&pdev->dev,
466 				      config->pwr_reg_cnt,
467 				      sizeof(hdmi->pwr_regs[0]),
468 				      GFP_KERNEL);
469 	if (!hdmi->pwr_regs)
470 		return -ENOMEM;
471 
472 	for (i = 0; i < config->pwr_reg_cnt; i++)
473 		hdmi->pwr_regs[i].supply = config->pwr_reg_names[i];
474 
475 	ret = devm_regulator_bulk_get(&pdev->dev, config->pwr_reg_cnt, hdmi->pwr_regs);
476 	if (ret)
477 		return dev_err_probe(dev, ret, "failed to get pwr regulators\n");
478 
479 	hdmi->hpd_clks = devm_kcalloc(&pdev->dev,
480 				      config->hpd_clk_cnt,
481 				      sizeof(hdmi->hpd_clks[0]),
482 				      GFP_KERNEL);
483 	if (!hdmi->hpd_clks)
484 		return -ENOMEM;
485 
486 	for (i = 0; i < config->hpd_clk_cnt; i++) {
487 		struct clk *clk;
488 
489 		clk = msm_clk_get(pdev, config->hpd_clk_names[i]);
490 		if (IS_ERR(clk))
491 			return dev_err_probe(dev, PTR_ERR(clk),
492 					     "failed to get hpd clk: %s\n",
493 					     config->hpd_clk_names[i]);
494 
495 		hdmi->hpd_clks[i] = clk;
496 	}
497 
498 	hdmi->pwr_clks = devm_kcalloc(&pdev->dev,
499 				      config->pwr_clk_cnt,
500 				      sizeof(hdmi->pwr_clks[0]),
501 				      GFP_KERNEL);
502 	if (!hdmi->pwr_clks)
503 		return -ENOMEM;
504 
505 	for (i = 0; i < config->pwr_clk_cnt; i++) {
506 		struct clk *clk;
507 
508 		clk = msm_clk_get(pdev, config->pwr_clk_names[i]);
509 		if (IS_ERR(clk))
510 			return dev_err_probe(dev, PTR_ERR(clk),
511 					     "failed to get pwr clk: %s\n",
512 					     config->pwr_clk_names[i]);
513 
514 		hdmi->pwr_clks[i] = clk;
515 	}
516 
517 	hdmi->hpd_gpiod = devm_gpiod_get_optional(&pdev->dev, "hpd", GPIOD_IN);
518 	/* This will catch e.g. -EPROBE_DEFER */
519 	if (IS_ERR(hdmi->hpd_gpiod))
520 		return dev_err_probe(dev, PTR_ERR(hdmi->hpd_gpiod),
521 				     "failed to get hpd gpio\n");
522 
523 	if (!hdmi->hpd_gpiod)
524 		DBG("failed to get HPD gpio");
525 
526 	if (hdmi->hpd_gpiod)
527 		gpiod_set_consumer_name(hdmi->hpd_gpiod, "HDMI_HPD");
528 
529 	ret = msm_hdmi_get_phy(hdmi);
530 	if (ret) {
531 		DRM_DEV_ERROR(&pdev->dev, "failed to get phy\n");
532 		return ret;
533 	}
534 
535 	ret = devm_pm_runtime_enable(&pdev->dev);
536 	if (ret)
537 		goto err_put_phy;
538 
539 	platform_set_drvdata(pdev, hdmi);
540 
541 	ret = component_add(&pdev->dev, &msm_hdmi_ops);
542 	if (ret)
543 		goto err_put_phy;
544 
545 	return 0;
546 
547 err_put_phy:
548 	msm_hdmi_put_phy(hdmi);
549 	return ret;
550 }
551 
552 static int msm_hdmi_dev_remove(struct platform_device *pdev)
553 {
554 	struct hdmi *hdmi = dev_get_drvdata(&pdev->dev);
555 
556 	component_del(&pdev->dev, &msm_hdmi_ops);
557 
558 	msm_hdmi_put_phy(hdmi);
559 
560 	return 0;
561 }
562 
563 static const struct of_device_id msm_hdmi_dt_match[] = {
564 	{ .compatible = "qcom,hdmi-tx-8996", .data = &hdmi_tx_8974_config },
565 	{ .compatible = "qcom,hdmi-tx-8994", .data = &hdmi_tx_8974_config },
566 	{ .compatible = "qcom,hdmi-tx-8084", .data = &hdmi_tx_8974_config },
567 	{ .compatible = "qcom,hdmi-tx-8974", .data = &hdmi_tx_8974_config },
568 	{ .compatible = "qcom,hdmi-tx-8960", .data = &hdmi_tx_8960_config },
569 	{ .compatible = "qcom,hdmi-tx-8660", .data = &hdmi_tx_8960_config },
570 	{}
571 };
572 
573 static struct platform_driver msm_hdmi_driver = {
574 	.probe = msm_hdmi_dev_probe,
575 	.remove = msm_hdmi_dev_remove,
576 	.driver = {
577 		.name = "hdmi_msm",
578 		.of_match_table = msm_hdmi_dt_match,
579 	},
580 };
581 
582 void __init msm_hdmi_register(void)
583 {
584 	msm_hdmi_phy_driver_register();
585 	platform_driver_register(&msm_hdmi_driver);
586 }
587 
588 void __exit msm_hdmi_unregister(void)
589 {
590 	platform_driver_unregister(&msm_hdmi_driver);
591 	msm_hdmi_phy_driver_unregister();
592 }
593