1 // SPDX-License-Identifier: GPL-2.0-only
3 * HDMI driver for OMAP5
5 * Copyright (C) 2014 Texas Instruments Incorporated
14 #define DSS_SUBSYS_NAME "HDMI"
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/err.h>
20 #include <linux/interrupt.h>
21 #include <linux/mutex.h>
22 #include <linux/delay.h>
23 #include <linux/string.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/clk.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/component.h>
30 #include <video/omapfb_dss.h>
31 #include <sound/omap-hdmi-audio.h>
33 #include "hdmi5_core.h"
35 #include "dss_features.h"
37 static struct omap_hdmi hdmi;
39 static int hdmi_runtime_get(void)
43 DSSDBG("hdmi_runtime_get\n");
45 r = pm_runtime_resume_and_get(&hdmi.pdev->dev);
52 static void hdmi_runtime_put(void)
56 DSSDBG("hdmi_runtime_put\n");
58 r = pm_runtime_put_sync(&hdmi.pdev->dev);
59 WARN_ON(r < 0 && r != -ENOSYS);
62 static irqreturn_t hdmi_irq_handler(int irq, void *data)
64 struct hdmi_wp_data *wp = data;
67 irqstatus = hdmi_wp_get_irqstatus(wp);
68 hdmi_wp_set_irqstatus(wp, irqstatus);
70 if ((irqstatus & HDMI_IRQ_LINK_CONNECT) &&
71 irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
74 * If we get both connect and disconnect interrupts at the same
75 * time, turn off the PHY, clear interrupts, and restart, which
76 * raises connect interrupt if a cable is connected, or nothing
77 * if cable is not connected.
80 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_OFF);
83 * We always get bogus CONNECT & DISCONNECT interrupts when
84 * setting the PHY to LDOON. To ignore those, we force the RXDET
85 * line to 0 until the PHY power state has been changed.
87 v = hdmi_read_reg(hdmi.phy.base, HDMI_TXPHY_PAD_CFG_CTRL);
88 v = FLD_MOD(v, 1, 15, 15); /* FORCE_RXDET_HIGH */
89 v = FLD_MOD(v, 0, 14, 7); /* RXDET_LINE */
90 hdmi_write_reg(hdmi.phy.base, HDMI_TXPHY_PAD_CFG_CTRL, v);
92 hdmi_wp_set_irqstatus(wp, HDMI_IRQ_LINK_CONNECT |
93 HDMI_IRQ_LINK_DISCONNECT);
95 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
97 REG_FLD_MOD(hdmi.phy.base, HDMI_TXPHY_PAD_CFG_CTRL, 0, 15, 15);
99 } else if (irqstatus & HDMI_IRQ_LINK_CONNECT) {
100 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_TXON);
101 } else if (irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
102 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
108 static int hdmi_init_regulator(void)
110 struct regulator *reg;
112 if (hdmi.vdda_reg != NULL)
115 reg = devm_regulator_get(&hdmi.pdev->dev, "vdda");
117 DSSERR("can't get VDDA regulator\n");
126 static int hdmi_power_on_core(struct omap_dss_device *dssdev)
130 r = regulator_enable(hdmi.vdda_reg);
134 r = hdmi_runtime_get();
136 goto err_runtime_get;
138 /* Make selection of HDMI in DSS */
139 dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK);
141 hdmi.core_enabled = true;
146 regulator_disable(hdmi.vdda_reg);
151 static void hdmi_power_off_core(struct omap_dss_device *dssdev)
153 hdmi.core_enabled = false;
156 regulator_disable(hdmi.vdda_reg);
159 static int hdmi_power_on_full(struct omap_dss_device *dssdev)
162 struct omap_video_timings *p;
163 struct omap_overlay_manager *mgr = hdmi.output.manager;
164 struct dss_pll_clock_info hdmi_cinfo = { 0 };
166 r = hdmi_power_on_core(dssdev);
170 p = &hdmi.cfg.timings;
172 DSSDBG("hdmi_power_on x_res= %d y_res = %d\n", p->x_res, p->y_res);
174 hdmi_pll_compute(&hdmi.pll, p->pixelclock, &hdmi_cinfo);
176 /* disable and clear irqs */
177 hdmi_wp_clear_irqenable(&hdmi.wp, 0xffffffff);
178 hdmi_wp_set_irqstatus(&hdmi.wp,
179 hdmi_wp_get_irqstatus(&hdmi.wp));
181 r = dss_pll_enable(&hdmi.pll.pll);
183 DSSERR("Failed to enable PLL\n");
187 r = dss_pll_set_config(&hdmi.pll.pll, &hdmi_cinfo);
189 DSSERR("Failed to configure PLL\n");
193 r = hdmi_phy_configure(&hdmi.phy, hdmi_cinfo.clkdco,
194 hdmi_cinfo.clkout[0]);
196 DSSDBG("Failed to start PHY\n");
200 r = hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_LDOON);
204 hdmi5_configure(&hdmi.core, &hdmi.wp, &hdmi.cfg);
206 /* bypass TV gamma table */
207 dispc_enable_gamma_table(0);
210 dss_mgr_set_timings(mgr, p);
212 r = hdmi_wp_video_start(&hdmi.wp);
216 r = dss_mgr_enable(mgr);
220 hdmi_wp_set_irqenable(&hdmi.wp,
221 HDMI_IRQ_LINK_CONNECT | HDMI_IRQ_LINK_DISCONNECT);
226 hdmi_wp_video_stop(&hdmi.wp);
228 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
232 dss_pll_disable(&hdmi.pll.pll);
234 hdmi_power_off_core(dssdev);
238 static void hdmi_power_off_full(struct omap_dss_device *dssdev)
240 struct omap_overlay_manager *mgr = hdmi.output.manager;
242 hdmi_wp_clear_irqenable(&hdmi.wp, 0xffffffff);
244 dss_mgr_disable(mgr);
246 hdmi_wp_video_stop(&hdmi.wp);
248 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
250 dss_pll_disable(&hdmi.pll.pll);
252 hdmi_power_off_core(dssdev);
255 static int hdmi_display_check_timing(struct omap_dss_device *dssdev,
256 struct omap_video_timings *timings)
258 struct omap_dss_device *out = &hdmi.output;
260 /* TODO: proper interlace support */
261 if (timings->interlace)
264 if (!dispc_mgr_timings_ok(out->dispc_channel, timings))
270 static void hdmi_display_set_timing(struct omap_dss_device *dssdev,
271 struct omap_video_timings *timings)
273 mutex_lock(&hdmi.lock);
275 hdmi.cfg.timings = *timings;
277 dispc_set_tv_pclk(timings->pixelclock);
279 mutex_unlock(&hdmi.lock);
282 static void hdmi_display_get_timings(struct omap_dss_device *dssdev,
283 struct omap_video_timings *timings)
285 *timings = hdmi.cfg.timings;
288 static void hdmi_dump_regs(struct seq_file *s)
290 mutex_lock(&hdmi.lock);
292 if (hdmi_runtime_get()) {
293 mutex_unlock(&hdmi.lock);
297 hdmi_wp_dump(&hdmi.wp, s);
298 hdmi_pll_dump(&hdmi.pll, s);
299 hdmi_phy_dump(&hdmi.phy, s);
300 hdmi5_core_dump(&hdmi.core, s);
303 mutex_unlock(&hdmi.lock);
306 static int read_edid(u8 *buf, int len)
311 mutex_lock(&hdmi.lock);
313 r = hdmi_runtime_get();
316 idlemode = REG_GET(hdmi.wp.base, HDMI_WP_SYSCONFIG, 3, 2);
318 REG_FLD_MOD(hdmi.wp.base, HDMI_WP_SYSCONFIG, 1, 3, 2);
320 r = hdmi5_read_edid(&hdmi.core, buf, len);
322 REG_FLD_MOD(hdmi.wp.base, HDMI_WP_SYSCONFIG, idlemode, 3, 2);
325 mutex_unlock(&hdmi.lock);
330 static void hdmi_start_audio_stream(struct omap_hdmi *hd)
332 REG_FLD_MOD(hdmi.wp.base, HDMI_WP_SYSCONFIG, 1, 3, 2);
333 hdmi_wp_audio_enable(&hd->wp, true);
334 hdmi_wp_audio_core_req_enable(&hd->wp, true);
337 static void hdmi_stop_audio_stream(struct omap_hdmi *hd)
339 hdmi_wp_audio_core_req_enable(&hd->wp, false);
340 hdmi_wp_audio_enable(&hd->wp, false);
341 REG_FLD_MOD(hd->wp.base, HDMI_WP_SYSCONFIG, hd->wp_idlemode, 3, 2);
344 static int hdmi_display_enable(struct omap_dss_device *dssdev)
346 struct omap_dss_device *out = &hdmi.output;
350 DSSDBG("ENTER hdmi_display_enable\n");
352 mutex_lock(&hdmi.lock);
354 if (out->manager == NULL) {
355 DSSERR("failed to enable display: no output/manager\n");
360 r = hdmi_power_on_full(dssdev);
362 DSSERR("failed to power on device\n");
366 if (hdmi.audio_configured) {
367 r = hdmi5_audio_config(&hdmi.core, &hdmi.wp, &hdmi.audio_config,
368 hdmi.cfg.timings.pixelclock);
370 DSSERR("Error restoring audio configuration: %d", r);
371 hdmi.audio_abort_cb(&hdmi.pdev->dev);
372 hdmi.audio_configured = false;
376 spin_lock_irqsave(&hdmi.audio_playing_lock, flags);
377 if (hdmi.audio_configured && hdmi.audio_playing)
378 hdmi_start_audio_stream(&hdmi);
379 hdmi.display_enabled = true;
380 spin_unlock_irqrestore(&hdmi.audio_playing_lock, flags);
382 mutex_unlock(&hdmi.lock);
386 mutex_unlock(&hdmi.lock);
390 static void hdmi_display_disable(struct omap_dss_device *dssdev)
394 DSSDBG("Enter hdmi_display_disable\n");
396 mutex_lock(&hdmi.lock);
398 spin_lock_irqsave(&hdmi.audio_playing_lock, flags);
399 hdmi_stop_audio_stream(&hdmi);
400 hdmi.display_enabled = false;
401 spin_unlock_irqrestore(&hdmi.audio_playing_lock, flags);
403 hdmi_power_off_full(dssdev);
405 mutex_unlock(&hdmi.lock);
408 static int hdmi_core_enable(struct omap_dss_device *dssdev)
412 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
414 mutex_lock(&hdmi.lock);
416 r = hdmi_power_on_core(dssdev);
418 DSSERR("failed to power on device\n");
422 mutex_unlock(&hdmi.lock);
426 mutex_unlock(&hdmi.lock);
430 static void hdmi_core_disable(struct omap_dss_device *dssdev)
432 DSSDBG("Enter omapdss_hdmi_core_disable\n");
434 mutex_lock(&hdmi.lock);
436 hdmi_power_off_core(dssdev);
438 mutex_unlock(&hdmi.lock);
441 static int hdmi_connect(struct omap_dss_device *dssdev,
442 struct omap_dss_device *dst)
444 struct omap_overlay_manager *mgr;
447 r = hdmi_init_regulator();
451 mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
455 r = dss_mgr_connect(mgr, dssdev);
459 r = omapdss_output_set_device(dssdev, dst);
461 DSSERR("failed to connect output to new device: %s\n",
463 dss_mgr_disconnect(mgr, dssdev);
470 static void hdmi_disconnect(struct omap_dss_device *dssdev,
471 struct omap_dss_device *dst)
473 WARN_ON(dst != dssdev->dst);
475 if (dst != dssdev->dst)
478 omapdss_output_unset_device(dssdev);
481 dss_mgr_disconnect(dssdev->manager, dssdev);
484 static int hdmi_read_edid(struct omap_dss_device *dssdev,
490 need_enable = hdmi.core_enabled == false;
493 r = hdmi_core_enable(dssdev);
498 r = read_edid(edid, len);
501 hdmi_core_disable(dssdev);
506 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
507 const struct hdmi_avi_infoframe *avi)
509 hdmi.cfg.infoframe = *avi;
513 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
516 hdmi.cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
520 static const struct omapdss_hdmi_ops hdmi_ops = {
521 .connect = hdmi_connect,
522 .disconnect = hdmi_disconnect,
524 .enable = hdmi_display_enable,
525 .disable = hdmi_display_disable,
527 .check_timings = hdmi_display_check_timing,
528 .set_timings = hdmi_display_set_timing,
529 .get_timings = hdmi_display_get_timings,
531 .read_edid = hdmi_read_edid,
532 .set_infoframe = hdmi_set_infoframe,
533 .set_hdmi_mode = hdmi_set_hdmi_mode,
536 static void hdmi_init_output(struct platform_device *pdev)
538 struct omap_dss_device *out = &hdmi.output;
540 out->dev = &pdev->dev;
541 out->id = OMAP_DSS_OUTPUT_HDMI;
542 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
543 out->name = "hdmi.0";
544 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
545 out->ops.hdmi = &hdmi_ops;
546 out->owner = THIS_MODULE;
548 omapdss_register_output(out);
551 static void hdmi_uninit_output(struct platform_device *pdev)
553 struct omap_dss_device *out = &hdmi.output;
555 omapdss_unregister_output(out);
558 static int hdmi_probe_of(struct platform_device *pdev)
560 struct device_node *node = pdev->dev.of_node;
561 struct device_node *ep;
564 ep = omapdss_of_get_first_endpoint(node);
568 r = hdmi_parse_lanes_of(pdev, ep, &hdmi.phy);
580 /* Audio callbacks */
581 static int hdmi_audio_startup(struct device *dev,
582 void (*abort_cb)(struct device *dev))
584 struct omap_hdmi *hd = dev_get_drvdata(dev);
587 mutex_lock(&hd->lock);
589 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
594 hd->audio_abort_cb = abort_cb;
597 mutex_unlock(&hd->lock);
602 static int hdmi_audio_shutdown(struct device *dev)
604 struct omap_hdmi *hd = dev_get_drvdata(dev);
606 mutex_lock(&hd->lock);
607 hd->audio_abort_cb = NULL;
608 hd->audio_configured = false;
609 hd->audio_playing = false;
610 mutex_unlock(&hd->lock);
615 static int hdmi_audio_start(struct device *dev)
617 struct omap_hdmi *hd = dev_get_drvdata(dev);
620 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
622 spin_lock_irqsave(&hd->audio_playing_lock, flags);
624 if (hd->display_enabled)
625 hdmi_start_audio_stream(hd);
626 hd->audio_playing = true;
628 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
632 static void hdmi_audio_stop(struct device *dev)
634 struct omap_hdmi *hd = dev_get_drvdata(dev);
637 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
639 spin_lock_irqsave(&hd->audio_playing_lock, flags);
641 if (hd->display_enabled)
642 hdmi_stop_audio_stream(hd);
643 hd->audio_playing = false;
645 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
648 static int hdmi_audio_config(struct device *dev,
649 struct omap_dss_audio *dss_audio)
651 struct omap_hdmi *hd = dev_get_drvdata(dev);
654 mutex_lock(&hd->lock);
656 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
661 ret = hdmi5_audio_config(&hd->core, &hd->wp, dss_audio,
662 hd->cfg.timings.pixelclock);
665 hd->audio_configured = true;
666 hd->audio_config = *dss_audio;
669 mutex_unlock(&hd->lock);
674 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
675 .audio_startup = hdmi_audio_startup,
676 .audio_shutdown = hdmi_audio_shutdown,
677 .audio_start = hdmi_audio_start,
678 .audio_stop = hdmi_audio_stop,
679 .audio_config = hdmi_audio_config,
682 static int hdmi_audio_register(struct device *dev)
684 struct omap_hdmi_audio_pdata pdata = {
687 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi.wp),
688 .ops = &hdmi_audio_ops,
691 hdmi.audio_pdev = platform_device_register_data(
692 dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
693 &pdata, sizeof(pdata));
695 if (IS_ERR(hdmi.audio_pdev))
696 return PTR_ERR(hdmi.audio_pdev);
700 REG_GET(hdmi.wp.base, HDMI_WP_SYSCONFIG, 3, 2);
706 /* HDMI HW IP initialisation */
707 static int hdmi5_bind(struct device *dev, struct device *master, void *data)
709 struct platform_device *pdev = to_platform_device(dev);
714 platform_set_drvdata(pdev, &hdmi);
716 mutex_init(&hdmi.lock);
717 spin_lock_init(&hdmi.audio_playing_lock);
719 if (pdev->dev.of_node) {
720 r = hdmi_probe_of(pdev);
725 r = hdmi_wp_init(pdev, &hdmi.wp);
729 r = hdmi_pll_init(pdev, &hdmi.pll, &hdmi.wp);
733 r = hdmi_phy_init(pdev, &hdmi.phy);
737 r = hdmi5_core_init(pdev, &hdmi.core);
741 irq = platform_get_irq(pdev, 0);
743 DSSERR("platform_get_irq failed\n");
748 r = devm_request_threaded_irq(&pdev->dev, irq,
749 NULL, hdmi_irq_handler,
750 IRQF_ONESHOT, "OMAP HDMI", &hdmi.wp);
752 DSSERR("HDMI IRQ request failed\n");
756 pm_runtime_enable(&pdev->dev);
758 hdmi_init_output(pdev);
760 r = hdmi_audio_register(&pdev->dev);
762 DSSERR("Registering HDMI audio failed %d\n", r);
763 hdmi_uninit_output(pdev);
764 pm_runtime_disable(&pdev->dev);
768 dss_debugfs_create_file("hdmi", hdmi_dump_regs);
772 hdmi_pll_uninit(&hdmi.pll);
776 static void hdmi5_unbind(struct device *dev, struct device *master, void *data)
778 struct platform_device *pdev = to_platform_device(dev);
781 platform_device_unregister(hdmi.audio_pdev);
783 hdmi_uninit_output(pdev);
785 hdmi_pll_uninit(&hdmi.pll);
787 pm_runtime_disable(&pdev->dev);
790 static const struct component_ops hdmi5_component_ops = {
792 .unbind = hdmi5_unbind,
795 static int hdmi5_probe(struct platform_device *pdev)
797 return component_add(&pdev->dev, &hdmi5_component_ops);
800 static void hdmi5_remove(struct platform_device *pdev)
802 component_del(&pdev->dev, &hdmi5_component_ops);
805 static int hdmi_runtime_suspend(struct device *dev)
812 static int hdmi_runtime_resume(struct device *dev)
816 r = dispc_runtime_get();
823 static const struct dev_pm_ops hdmi_pm_ops = {
824 .runtime_suspend = hdmi_runtime_suspend,
825 .runtime_resume = hdmi_runtime_resume,
828 static const struct of_device_id hdmi_of_match[] = {
829 { .compatible = "ti,omap5-hdmi", },
830 { .compatible = "ti,dra7-hdmi", },
834 static struct platform_driver omapdss_hdmihw_driver = {
835 .probe = hdmi5_probe,
836 .remove_new = hdmi5_remove,
838 .name = "omapdss_hdmi5",
840 .of_match_table = hdmi_of_match,
841 .suppress_bind_attrs = true,
845 int __init hdmi5_init_platform_driver(void)
847 return platform_driver_register(&omapdss_hdmihw_driver);
850 void hdmi5_uninit_platform_driver(void)
852 platform_driver_unregister(&omapdss_hdmihw_driver);