1 // SPDX-License-Identifier: GPL-2.0-only
3 * HDMI interface DSS driver for TI's OMAP4 family of SoCs.
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com/
9 #define DSS_SUBSYS_NAME "HDMI"
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/err.h>
15 #include <linux/interrupt.h>
16 #include <linux/mutex.h>
17 #include <linux/delay.h>
18 #include <linux/string.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/clk.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/component.h>
25 #include <video/omapfb_dss.h>
26 #include <sound/omap-hdmi-audio.h>
28 #include "hdmi4_core.h"
30 #include "dss_features.h"
33 static struct omap_hdmi hdmi;
35 static int hdmi_runtime_get(void)
39 DSSDBG("hdmi_runtime_get\n");
41 r = pm_runtime_resume_and_get(&hdmi.pdev->dev);
48 static void hdmi_runtime_put(void)
52 DSSDBG("hdmi_runtime_put\n");
54 r = pm_runtime_put_sync(&hdmi.pdev->dev);
55 WARN_ON(r < 0 && r != -ENOSYS);
58 static irqreturn_t hdmi_irq_handler(int irq, void *data)
60 struct hdmi_wp_data *wp = data;
63 irqstatus = hdmi_wp_get_irqstatus(wp);
64 hdmi_wp_set_irqstatus(wp, irqstatus);
66 if ((irqstatus & HDMI_IRQ_LINK_CONNECT) &&
67 irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
69 * If we get both connect and disconnect interrupts at the same
70 * time, turn off the PHY, clear interrupts, and restart, which
71 * raises connect interrupt if a cable is connected, or nothing
72 * if cable is not connected.
74 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_OFF);
76 hdmi_wp_set_irqstatus(wp, HDMI_IRQ_LINK_CONNECT |
77 HDMI_IRQ_LINK_DISCONNECT);
79 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
80 } else if (irqstatus & HDMI_IRQ_LINK_CONNECT) {
81 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_TXON);
82 } else if (irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
83 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
89 static int hdmi_init_regulator(void)
91 struct regulator *reg;
93 if (hdmi.vdda_reg != NULL)
96 reg = devm_regulator_get(&hdmi.pdev->dev, "vdda");
99 if (PTR_ERR(reg) != -EPROBE_DEFER)
100 DSSERR("can't get VDDA regulator\n");
109 static int hdmi_power_on_core(struct omap_dss_device *dssdev)
113 r = regulator_enable(hdmi.vdda_reg);
117 r = hdmi_runtime_get();
119 goto err_runtime_get;
121 /* Make selection of HDMI in DSS */
122 dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK);
124 hdmi.core_enabled = true;
129 regulator_disable(hdmi.vdda_reg);
134 static void hdmi_power_off_core(struct omap_dss_device *dssdev)
136 hdmi.core_enabled = false;
139 regulator_disable(hdmi.vdda_reg);
142 static int hdmi_power_on_full(struct omap_dss_device *dssdev)
145 struct omap_video_timings *p;
146 struct omap_overlay_manager *mgr = hdmi.output.manager;
147 struct hdmi_wp_data *wp = &hdmi.wp;
148 struct dss_pll_clock_info hdmi_cinfo = { 0 };
150 r = hdmi_power_on_core(dssdev);
154 /* disable and clear irqs */
155 hdmi_wp_clear_irqenable(wp, 0xffffffff);
156 hdmi_wp_set_irqstatus(wp, 0xffffffff);
158 p = &hdmi.cfg.timings;
160 DSSDBG("hdmi_power_on x_res= %d y_res = %d\n", p->x_res, p->y_res);
162 hdmi_pll_compute(&hdmi.pll, p->pixelclock, &hdmi_cinfo);
164 r = dss_pll_enable(&hdmi.pll.pll);
166 DSSERR("Failed to enable PLL\n");
170 r = dss_pll_set_config(&hdmi.pll.pll, &hdmi_cinfo);
172 DSSERR("Failed to configure PLL\n");
176 r = hdmi_phy_configure(&hdmi.phy, hdmi_cinfo.clkdco,
177 hdmi_cinfo.clkout[0]);
179 DSSDBG("Failed to configure PHY\n");
183 r = hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
187 hdmi4_configure(&hdmi.core, &hdmi.wp, &hdmi.cfg);
189 /* bypass TV gamma table */
190 dispc_enable_gamma_table(0);
193 dss_mgr_set_timings(mgr, p);
195 r = hdmi_wp_video_start(&hdmi.wp);
199 r = dss_mgr_enable(mgr);
203 hdmi_wp_set_irqenable(wp,
204 HDMI_IRQ_LINK_CONNECT | HDMI_IRQ_LINK_DISCONNECT);
209 hdmi_wp_video_stop(&hdmi.wp);
211 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
215 dss_pll_disable(&hdmi.pll.pll);
217 hdmi_power_off_core(dssdev);
221 static void hdmi_power_off_full(struct omap_dss_device *dssdev)
223 struct omap_overlay_manager *mgr = hdmi.output.manager;
225 hdmi_wp_clear_irqenable(&hdmi.wp, 0xffffffff);
227 dss_mgr_disable(mgr);
229 hdmi_wp_video_stop(&hdmi.wp);
231 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
233 dss_pll_disable(&hdmi.pll.pll);
235 hdmi_power_off_core(dssdev);
238 static int hdmi_display_check_timing(struct omap_dss_device *dssdev,
239 struct omap_video_timings *timings)
241 struct omap_dss_device *out = &hdmi.output;
243 if (!dispc_mgr_timings_ok(out->dispc_channel, timings))
249 static void hdmi_display_set_timing(struct omap_dss_device *dssdev,
250 struct omap_video_timings *timings)
252 mutex_lock(&hdmi.lock);
254 hdmi.cfg.timings = *timings;
256 dispc_set_tv_pclk(timings->pixelclock);
258 mutex_unlock(&hdmi.lock);
261 static void hdmi_display_get_timings(struct omap_dss_device *dssdev,
262 struct omap_video_timings *timings)
264 *timings = hdmi.cfg.timings;
267 static void hdmi_dump_regs(struct seq_file *s)
269 mutex_lock(&hdmi.lock);
271 if (hdmi_runtime_get()) {
272 mutex_unlock(&hdmi.lock);
276 hdmi_wp_dump(&hdmi.wp, s);
277 hdmi_pll_dump(&hdmi.pll, s);
278 hdmi_phy_dump(&hdmi.phy, s);
279 hdmi4_core_dump(&hdmi.core, s);
282 mutex_unlock(&hdmi.lock);
285 static int read_edid(u8 *buf, int len)
289 mutex_lock(&hdmi.lock);
291 r = hdmi_runtime_get();
294 r = hdmi4_read_edid(&hdmi.core, buf, len);
297 mutex_unlock(&hdmi.lock);
302 static void hdmi_start_audio_stream(struct omap_hdmi *hd)
304 hdmi_wp_audio_enable(&hd->wp, true);
305 hdmi4_audio_start(&hd->core, &hd->wp);
308 static void hdmi_stop_audio_stream(struct omap_hdmi *hd)
310 hdmi4_audio_stop(&hd->core, &hd->wp);
311 hdmi_wp_audio_enable(&hd->wp, false);
314 static int hdmi_display_enable(struct omap_dss_device *dssdev)
316 struct omap_dss_device *out = &hdmi.output;
320 DSSDBG("ENTER hdmi_display_enable\n");
322 mutex_lock(&hdmi.lock);
324 if (out->manager == NULL) {
325 DSSERR("failed to enable display: no output/manager\n");
330 r = hdmi_power_on_full(dssdev);
332 DSSERR("failed to power on device\n");
336 if (hdmi.audio_configured) {
337 r = hdmi4_audio_config(&hdmi.core, &hdmi.wp, &hdmi.audio_config,
338 hdmi.cfg.timings.pixelclock);
340 DSSERR("Error restoring audio configuration: %d", r);
341 hdmi.audio_abort_cb(&hdmi.pdev->dev);
342 hdmi.audio_configured = false;
346 spin_lock_irqsave(&hdmi.audio_playing_lock, flags);
347 if (hdmi.audio_configured && hdmi.audio_playing)
348 hdmi_start_audio_stream(&hdmi);
349 hdmi.display_enabled = true;
350 spin_unlock_irqrestore(&hdmi.audio_playing_lock, flags);
352 mutex_unlock(&hdmi.lock);
356 mutex_unlock(&hdmi.lock);
360 static void hdmi_display_disable(struct omap_dss_device *dssdev)
364 DSSDBG("Enter hdmi_display_disable\n");
366 mutex_lock(&hdmi.lock);
368 spin_lock_irqsave(&hdmi.audio_playing_lock, flags);
369 hdmi_stop_audio_stream(&hdmi);
370 hdmi.display_enabled = false;
371 spin_unlock_irqrestore(&hdmi.audio_playing_lock, flags);
373 hdmi_power_off_full(dssdev);
375 mutex_unlock(&hdmi.lock);
378 static int hdmi_core_enable(struct omap_dss_device *dssdev)
382 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
384 mutex_lock(&hdmi.lock);
386 r = hdmi_power_on_core(dssdev);
388 DSSERR("failed to power on device\n");
392 mutex_unlock(&hdmi.lock);
396 mutex_unlock(&hdmi.lock);
400 static void hdmi_core_disable(struct omap_dss_device *dssdev)
402 DSSDBG("Enter omapdss_hdmi_core_disable\n");
404 mutex_lock(&hdmi.lock);
406 hdmi_power_off_core(dssdev);
408 mutex_unlock(&hdmi.lock);
411 static int hdmi_connect(struct omap_dss_device *dssdev,
412 struct omap_dss_device *dst)
414 struct omap_overlay_manager *mgr;
417 r = hdmi_init_regulator();
421 mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
425 r = dss_mgr_connect(mgr, dssdev);
429 r = omapdss_output_set_device(dssdev, dst);
431 DSSERR("failed to connect output to new device: %s\n",
433 dss_mgr_disconnect(mgr, dssdev);
440 static void hdmi_disconnect(struct omap_dss_device *dssdev,
441 struct omap_dss_device *dst)
443 WARN_ON(dst != dssdev->dst);
445 if (dst != dssdev->dst)
448 omapdss_output_unset_device(dssdev);
451 dss_mgr_disconnect(dssdev->manager, dssdev);
454 static int hdmi_read_edid(struct omap_dss_device *dssdev,
457 bool need_enable = !hdmi.core_enabled;
461 r = hdmi_core_enable(dssdev);
466 r = read_edid(edid, len);
469 hdmi_core_disable(dssdev);
474 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
475 const struct hdmi_avi_infoframe *avi)
477 hdmi.cfg.infoframe = *avi;
481 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
484 hdmi.cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
488 static const struct omapdss_hdmi_ops hdmi_ops = {
489 .connect = hdmi_connect,
490 .disconnect = hdmi_disconnect,
492 .enable = hdmi_display_enable,
493 .disable = hdmi_display_disable,
495 .check_timings = hdmi_display_check_timing,
496 .set_timings = hdmi_display_set_timing,
497 .get_timings = hdmi_display_get_timings,
499 .read_edid = hdmi_read_edid,
500 .set_infoframe = hdmi_set_infoframe,
501 .set_hdmi_mode = hdmi_set_hdmi_mode,
504 static void hdmi_init_output(struct platform_device *pdev)
506 struct omap_dss_device *out = &hdmi.output;
508 out->dev = &pdev->dev;
509 out->id = OMAP_DSS_OUTPUT_HDMI;
510 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
511 out->name = "hdmi.0";
512 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
513 out->ops.hdmi = &hdmi_ops;
514 out->owner = THIS_MODULE;
516 omapdss_register_output(out);
519 static void hdmi_uninit_output(struct platform_device *pdev)
521 struct omap_dss_device *out = &hdmi.output;
523 omapdss_unregister_output(out);
526 static int hdmi_probe_of(struct platform_device *pdev)
528 struct device_node *node = pdev->dev.of_node;
529 struct device_node *ep;
532 ep = omapdss_of_get_first_endpoint(node);
536 r = hdmi_parse_lanes_of(pdev, ep, &hdmi.phy);
548 /* Audio callbacks */
549 static int hdmi_audio_startup(struct device *dev,
550 void (*abort_cb)(struct device *dev))
552 struct omap_hdmi *hd = dev_get_drvdata(dev);
555 mutex_lock(&hd->lock);
557 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
562 hd->audio_abort_cb = abort_cb;
565 mutex_unlock(&hd->lock);
570 static int hdmi_audio_shutdown(struct device *dev)
572 struct omap_hdmi *hd = dev_get_drvdata(dev);
574 mutex_lock(&hd->lock);
575 hd->audio_abort_cb = NULL;
576 hd->audio_configured = false;
577 hd->audio_playing = false;
578 mutex_unlock(&hd->lock);
583 static int hdmi_audio_start(struct device *dev)
585 struct omap_hdmi *hd = dev_get_drvdata(dev);
588 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
590 spin_lock_irqsave(&hd->audio_playing_lock, flags);
592 if (hd->display_enabled)
593 hdmi_start_audio_stream(hd);
594 hd->audio_playing = true;
596 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
600 static void hdmi_audio_stop(struct device *dev)
602 struct omap_hdmi *hd = dev_get_drvdata(dev);
605 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
607 spin_lock_irqsave(&hd->audio_playing_lock, flags);
609 if (hd->display_enabled)
610 hdmi_stop_audio_stream(hd);
611 hd->audio_playing = false;
613 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
616 static int hdmi_audio_config(struct device *dev,
617 struct omap_dss_audio *dss_audio)
619 struct omap_hdmi *hd = dev_get_drvdata(dev);
622 mutex_lock(&hd->lock);
624 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
629 ret = hdmi4_audio_config(&hd->core, &hd->wp, dss_audio,
630 hd->cfg.timings.pixelclock);
632 hd->audio_configured = true;
633 hd->audio_config = *dss_audio;
636 mutex_unlock(&hd->lock);
641 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
642 .audio_startup = hdmi_audio_startup,
643 .audio_shutdown = hdmi_audio_shutdown,
644 .audio_start = hdmi_audio_start,
645 .audio_stop = hdmi_audio_stop,
646 .audio_config = hdmi_audio_config,
649 static int hdmi_audio_register(struct device *dev)
651 struct omap_hdmi_audio_pdata pdata = {
654 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi.wp),
655 .ops = &hdmi_audio_ops,
658 hdmi.audio_pdev = platform_device_register_data(
659 dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
660 &pdata, sizeof(pdata));
662 return PTR_ERR_OR_ZERO(hdmi.audio_pdev);
665 /* HDMI HW IP initialisation */
666 static int hdmi4_bind(struct device *dev, struct device *master, void *data)
668 struct platform_device *pdev = to_platform_device(dev);
673 platform_set_drvdata(pdev, &hdmi);
675 mutex_init(&hdmi.lock);
676 spin_lock_init(&hdmi.audio_playing_lock);
678 if (pdev->dev.of_node) {
679 r = hdmi_probe_of(pdev);
684 r = hdmi_wp_init(pdev, &hdmi.wp);
688 r = hdmi_pll_init(pdev, &hdmi.pll, &hdmi.wp);
692 r = hdmi_phy_init(pdev, &hdmi.phy);
696 r = hdmi4_core_init(pdev, &hdmi.core);
700 irq = platform_get_irq(pdev, 0);
702 DSSERR("platform_get_irq failed\n");
707 r = devm_request_threaded_irq(&pdev->dev, irq,
708 NULL, hdmi_irq_handler,
709 IRQF_ONESHOT, "OMAP HDMI", &hdmi.wp);
711 DSSERR("HDMI IRQ request failed\n");
715 pm_runtime_enable(&pdev->dev);
717 hdmi_init_output(pdev);
719 r = hdmi_audio_register(&pdev->dev);
721 DSSERR("Registering HDMI audio failed\n");
722 hdmi_uninit_output(pdev);
723 pm_runtime_disable(&pdev->dev);
727 dss_debugfs_create_file("hdmi", hdmi_dump_regs);
731 hdmi_pll_uninit(&hdmi.pll);
735 static void hdmi4_unbind(struct device *dev, struct device *master, void *data)
737 struct platform_device *pdev = to_platform_device(dev);
740 platform_device_unregister(hdmi.audio_pdev);
742 hdmi_uninit_output(pdev);
744 hdmi_pll_uninit(&hdmi.pll);
746 pm_runtime_disable(&pdev->dev);
749 static const struct component_ops hdmi4_component_ops = {
751 .unbind = hdmi4_unbind,
754 static int hdmi4_probe(struct platform_device *pdev)
756 return component_add(&pdev->dev, &hdmi4_component_ops);
759 static void hdmi4_remove(struct platform_device *pdev)
761 component_del(&pdev->dev, &hdmi4_component_ops);
764 static int hdmi_runtime_suspend(struct device *dev)
771 static int hdmi_runtime_resume(struct device *dev)
775 r = dispc_runtime_get();
782 static const struct dev_pm_ops hdmi_pm_ops = {
783 .runtime_suspend = hdmi_runtime_suspend,
784 .runtime_resume = hdmi_runtime_resume,
787 static const struct of_device_id hdmi_of_match[] = {
788 { .compatible = "ti,omap4-hdmi", },
792 static struct platform_driver omapdss_hdmihw_driver = {
793 .probe = hdmi4_probe,
794 .remove_new = hdmi4_remove,
796 .name = "omapdss_hdmi",
798 .of_match_table = hdmi_of_match,
799 .suppress_bind_attrs = true,
803 int __init hdmi4_init_platform_driver(void)
805 return platform_driver_register(&omapdss_hdmihw_driver);
808 void hdmi4_uninit_platform_driver(void)
810 platform_driver_unregister(&omapdss_hdmihw_driver);