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/
6 * Mythri pk <mythripk@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_get_sync(&hdmi.pdev->dev);
43 pm_runtime_put_sync(&hdmi.pdev->dev);
50 static void hdmi_runtime_put(void)
54 DSSDBG("hdmi_runtime_put\n");
56 r = pm_runtime_put_sync(&hdmi.pdev->dev);
57 WARN_ON(r < 0 && r != -ENOSYS);
60 static irqreturn_t hdmi_irq_handler(int irq, void *data)
62 struct hdmi_wp_data *wp = data;
65 irqstatus = hdmi_wp_get_irqstatus(wp);
66 hdmi_wp_set_irqstatus(wp, irqstatus);
68 if ((irqstatus & HDMI_IRQ_LINK_CONNECT) &&
69 irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
71 * If we get both connect and disconnect interrupts at the same
72 * time, turn off the PHY, clear interrupts, and restart, which
73 * raises connect interrupt if a cable is connected, or nothing
74 * if cable is not connected.
76 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_OFF);
78 hdmi_wp_set_irqstatus(wp, HDMI_IRQ_LINK_CONNECT |
79 HDMI_IRQ_LINK_DISCONNECT);
81 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
82 } else if (irqstatus & HDMI_IRQ_LINK_CONNECT) {
83 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_TXON);
84 } else if (irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
85 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
91 static int hdmi_init_regulator(void)
93 struct regulator *reg;
95 if (hdmi.vdda_reg != NULL)
98 reg = devm_regulator_get(&hdmi.pdev->dev, "vdda");
101 if (PTR_ERR(reg) != -EPROBE_DEFER)
102 DSSERR("can't get VDDA regulator\n");
111 static int hdmi_power_on_core(struct omap_dss_device *dssdev)
115 r = regulator_enable(hdmi.vdda_reg);
119 r = hdmi_runtime_get();
121 goto err_runtime_get;
123 /* Make selection of HDMI in DSS */
124 dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK);
126 hdmi.core_enabled = true;
131 regulator_disable(hdmi.vdda_reg);
136 static void hdmi_power_off_core(struct omap_dss_device *dssdev)
138 hdmi.core_enabled = false;
141 regulator_disable(hdmi.vdda_reg);
144 static int hdmi_power_on_full(struct omap_dss_device *dssdev)
147 struct omap_video_timings *p;
148 struct omap_overlay_manager *mgr = hdmi.output.manager;
149 struct hdmi_wp_data *wp = &hdmi.wp;
150 struct dss_pll_clock_info hdmi_cinfo = { 0 };
152 r = hdmi_power_on_core(dssdev);
156 /* disable and clear irqs */
157 hdmi_wp_clear_irqenable(wp, 0xffffffff);
158 hdmi_wp_set_irqstatus(wp, 0xffffffff);
160 p = &hdmi.cfg.timings;
162 DSSDBG("hdmi_power_on x_res= %d y_res = %d\n", p->x_res, p->y_res);
164 hdmi_pll_compute(&hdmi.pll, p->pixelclock, &hdmi_cinfo);
166 r = dss_pll_enable(&hdmi.pll.pll);
168 DSSERR("Failed to enable PLL\n");
172 r = dss_pll_set_config(&hdmi.pll.pll, &hdmi_cinfo);
174 DSSERR("Failed to configure PLL\n");
178 r = hdmi_phy_configure(&hdmi.phy, hdmi_cinfo.clkdco,
179 hdmi_cinfo.clkout[0]);
181 DSSDBG("Failed to configure PHY\n");
185 r = hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
189 hdmi4_configure(&hdmi.core, &hdmi.wp, &hdmi.cfg);
191 /* bypass TV gamma table */
192 dispc_enable_gamma_table(0);
195 dss_mgr_set_timings(mgr, p);
197 r = hdmi_wp_video_start(&hdmi.wp);
201 r = dss_mgr_enable(mgr);
205 hdmi_wp_set_irqenable(wp,
206 HDMI_IRQ_LINK_CONNECT | HDMI_IRQ_LINK_DISCONNECT);
211 hdmi_wp_video_stop(&hdmi.wp);
213 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
217 dss_pll_disable(&hdmi.pll.pll);
219 hdmi_power_off_core(dssdev);
223 static void hdmi_power_off_full(struct omap_dss_device *dssdev)
225 struct omap_overlay_manager *mgr = hdmi.output.manager;
227 hdmi_wp_clear_irqenable(&hdmi.wp, 0xffffffff);
229 dss_mgr_disable(mgr);
231 hdmi_wp_video_stop(&hdmi.wp);
233 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
235 dss_pll_disable(&hdmi.pll.pll);
237 hdmi_power_off_core(dssdev);
240 static int hdmi_display_check_timing(struct omap_dss_device *dssdev,
241 struct omap_video_timings *timings)
243 struct omap_dss_device *out = &hdmi.output;
245 if (!dispc_mgr_timings_ok(out->dispc_channel, timings))
251 static void hdmi_display_set_timing(struct omap_dss_device *dssdev,
252 struct omap_video_timings *timings)
254 mutex_lock(&hdmi.lock);
256 hdmi.cfg.timings = *timings;
258 dispc_set_tv_pclk(timings->pixelclock);
260 mutex_unlock(&hdmi.lock);
263 static void hdmi_display_get_timings(struct omap_dss_device *dssdev,
264 struct omap_video_timings *timings)
266 *timings = hdmi.cfg.timings;
269 static void hdmi_dump_regs(struct seq_file *s)
271 mutex_lock(&hdmi.lock);
273 if (hdmi_runtime_get()) {
274 mutex_unlock(&hdmi.lock);
278 hdmi_wp_dump(&hdmi.wp, s);
279 hdmi_pll_dump(&hdmi.pll, s);
280 hdmi_phy_dump(&hdmi.phy, s);
281 hdmi4_core_dump(&hdmi.core, s);
284 mutex_unlock(&hdmi.lock);
287 static int read_edid(u8 *buf, int len)
291 mutex_lock(&hdmi.lock);
293 r = hdmi_runtime_get();
296 r = hdmi4_read_edid(&hdmi.core, buf, len);
299 mutex_unlock(&hdmi.lock);
304 static void hdmi_start_audio_stream(struct omap_hdmi *hd)
306 hdmi_wp_audio_enable(&hd->wp, true);
307 hdmi4_audio_start(&hd->core, &hd->wp);
310 static void hdmi_stop_audio_stream(struct omap_hdmi *hd)
312 hdmi4_audio_stop(&hd->core, &hd->wp);
313 hdmi_wp_audio_enable(&hd->wp, false);
316 static int hdmi_display_enable(struct omap_dss_device *dssdev)
318 struct omap_dss_device *out = &hdmi.output;
322 DSSDBG("ENTER hdmi_display_enable\n");
324 mutex_lock(&hdmi.lock);
326 if (out->manager == NULL) {
327 DSSERR("failed to enable display: no output/manager\n");
332 r = hdmi_power_on_full(dssdev);
334 DSSERR("failed to power on device\n");
338 if (hdmi.audio_configured) {
339 r = hdmi4_audio_config(&hdmi.core, &hdmi.wp, &hdmi.audio_config,
340 hdmi.cfg.timings.pixelclock);
342 DSSERR("Error restoring audio configuration: %d", r);
343 hdmi.audio_abort_cb(&hdmi.pdev->dev);
344 hdmi.audio_configured = false;
348 spin_lock_irqsave(&hdmi.audio_playing_lock, flags);
349 if (hdmi.audio_configured && hdmi.audio_playing)
350 hdmi_start_audio_stream(&hdmi);
351 hdmi.display_enabled = true;
352 spin_unlock_irqrestore(&hdmi.audio_playing_lock, flags);
354 mutex_unlock(&hdmi.lock);
358 mutex_unlock(&hdmi.lock);
362 static void hdmi_display_disable(struct omap_dss_device *dssdev)
366 DSSDBG("Enter hdmi_display_disable\n");
368 mutex_lock(&hdmi.lock);
370 spin_lock_irqsave(&hdmi.audio_playing_lock, flags);
371 hdmi_stop_audio_stream(&hdmi);
372 hdmi.display_enabled = false;
373 spin_unlock_irqrestore(&hdmi.audio_playing_lock, flags);
375 hdmi_power_off_full(dssdev);
377 mutex_unlock(&hdmi.lock);
380 static int hdmi_core_enable(struct omap_dss_device *dssdev)
384 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
386 mutex_lock(&hdmi.lock);
388 r = hdmi_power_on_core(dssdev);
390 DSSERR("failed to power on device\n");
394 mutex_unlock(&hdmi.lock);
398 mutex_unlock(&hdmi.lock);
402 static void hdmi_core_disable(struct omap_dss_device *dssdev)
404 DSSDBG("Enter omapdss_hdmi_core_disable\n");
406 mutex_lock(&hdmi.lock);
408 hdmi_power_off_core(dssdev);
410 mutex_unlock(&hdmi.lock);
413 static int hdmi_connect(struct omap_dss_device *dssdev,
414 struct omap_dss_device *dst)
416 struct omap_overlay_manager *mgr;
419 r = hdmi_init_regulator();
423 mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
427 r = dss_mgr_connect(mgr, dssdev);
431 r = omapdss_output_set_device(dssdev, dst);
433 DSSERR("failed to connect output to new device: %s\n",
435 dss_mgr_disconnect(mgr, dssdev);
442 static void hdmi_disconnect(struct omap_dss_device *dssdev,
443 struct omap_dss_device *dst)
445 WARN_ON(dst != dssdev->dst);
447 if (dst != dssdev->dst)
450 omapdss_output_unset_device(dssdev);
453 dss_mgr_disconnect(dssdev->manager, dssdev);
456 static int hdmi_read_edid(struct omap_dss_device *dssdev,
459 bool need_enable = !hdmi.core_enabled;
463 r = hdmi_core_enable(dssdev);
468 r = read_edid(edid, len);
471 hdmi_core_disable(dssdev);
476 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
477 const struct hdmi_avi_infoframe *avi)
479 hdmi.cfg.infoframe = *avi;
483 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
486 hdmi.cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
490 static const struct omapdss_hdmi_ops hdmi_ops = {
491 .connect = hdmi_connect,
492 .disconnect = hdmi_disconnect,
494 .enable = hdmi_display_enable,
495 .disable = hdmi_display_disable,
497 .check_timings = hdmi_display_check_timing,
498 .set_timings = hdmi_display_set_timing,
499 .get_timings = hdmi_display_get_timings,
501 .read_edid = hdmi_read_edid,
502 .set_infoframe = hdmi_set_infoframe,
503 .set_hdmi_mode = hdmi_set_hdmi_mode,
506 static void hdmi_init_output(struct platform_device *pdev)
508 struct omap_dss_device *out = &hdmi.output;
510 out->dev = &pdev->dev;
511 out->id = OMAP_DSS_OUTPUT_HDMI;
512 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
513 out->name = "hdmi.0";
514 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
515 out->ops.hdmi = &hdmi_ops;
516 out->owner = THIS_MODULE;
518 omapdss_register_output(out);
521 static void hdmi_uninit_output(struct platform_device *pdev)
523 struct omap_dss_device *out = &hdmi.output;
525 omapdss_unregister_output(out);
528 static int hdmi_probe_of(struct platform_device *pdev)
530 struct device_node *node = pdev->dev.of_node;
531 struct device_node *ep;
534 ep = omapdss_of_get_first_endpoint(node);
538 r = hdmi_parse_lanes_of(pdev, ep, &hdmi.phy);
550 /* Audio callbacks */
551 static int hdmi_audio_startup(struct device *dev,
552 void (*abort_cb)(struct device *dev))
554 struct omap_hdmi *hd = dev_get_drvdata(dev);
557 mutex_lock(&hd->lock);
559 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
564 hd->audio_abort_cb = abort_cb;
567 mutex_unlock(&hd->lock);
572 static int hdmi_audio_shutdown(struct device *dev)
574 struct omap_hdmi *hd = dev_get_drvdata(dev);
576 mutex_lock(&hd->lock);
577 hd->audio_abort_cb = NULL;
578 hd->audio_configured = false;
579 hd->audio_playing = false;
580 mutex_unlock(&hd->lock);
585 static int hdmi_audio_start(struct device *dev)
587 struct omap_hdmi *hd = dev_get_drvdata(dev);
590 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
592 spin_lock_irqsave(&hd->audio_playing_lock, flags);
594 if (hd->display_enabled)
595 hdmi_start_audio_stream(hd);
596 hd->audio_playing = true;
598 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
602 static void hdmi_audio_stop(struct device *dev)
604 struct omap_hdmi *hd = dev_get_drvdata(dev);
607 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
609 spin_lock_irqsave(&hd->audio_playing_lock, flags);
611 if (hd->display_enabled)
612 hdmi_stop_audio_stream(hd);
613 hd->audio_playing = false;
615 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
618 static int hdmi_audio_config(struct device *dev,
619 struct omap_dss_audio *dss_audio)
621 struct omap_hdmi *hd = dev_get_drvdata(dev);
624 mutex_lock(&hd->lock);
626 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
631 ret = hdmi4_audio_config(&hd->core, &hd->wp, dss_audio,
632 hd->cfg.timings.pixelclock);
634 hd->audio_configured = true;
635 hd->audio_config = *dss_audio;
638 mutex_unlock(&hd->lock);
643 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
644 .audio_startup = hdmi_audio_startup,
645 .audio_shutdown = hdmi_audio_shutdown,
646 .audio_start = hdmi_audio_start,
647 .audio_stop = hdmi_audio_stop,
648 .audio_config = hdmi_audio_config,
651 static int hdmi_audio_register(struct device *dev)
653 struct omap_hdmi_audio_pdata pdata = {
656 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi.wp),
657 .ops = &hdmi_audio_ops,
660 hdmi.audio_pdev = platform_device_register_data(
661 dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
662 &pdata, sizeof(pdata));
664 return PTR_ERR_OR_ZERO(hdmi.audio_pdev);
667 /* HDMI HW IP initialisation */
668 static int hdmi4_bind(struct device *dev, struct device *master, void *data)
670 struct platform_device *pdev = to_platform_device(dev);
675 platform_set_drvdata(pdev, &hdmi);
677 mutex_init(&hdmi.lock);
678 spin_lock_init(&hdmi.audio_playing_lock);
680 if (pdev->dev.of_node) {
681 r = hdmi_probe_of(pdev);
686 r = hdmi_wp_init(pdev, &hdmi.wp);
690 r = hdmi_pll_init(pdev, &hdmi.pll, &hdmi.wp);
694 r = hdmi_phy_init(pdev, &hdmi.phy);
698 r = hdmi4_core_init(pdev, &hdmi.core);
702 irq = platform_get_irq(pdev, 0);
704 DSSERR("platform_get_irq failed\n");
709 r = devm_request_threaded_irq(&pdev->dev, irq,
710 NULL, hdmi_irq_handler,
711 IRQF_ONESHOT, "OMAP HDMI", &hdmi.wp);
713 DSSERR("HDMI IRQ request failed\n");
717 pm_runtime_enable(&pdev->dev);
719 hdmi_init_output(pdev);
721 r = hdmi_audio_register(&pdev->dev);
723 DSSERR("Registering HDMI audio failed\n");
724 hdmi_uninit_output(pdev);
725 pm_runtime_disable(&pdev->dev);
729 dss_debugfs_create_file("hdmi", hdmi_dump_regs);
733 hdmi_pll_uninit(&hdmi.pll);
737 static void hdmi4_unbind(struct device *dev, struct device *master, void *data)
739 struct platform_device *pdev = to_platform_device(dev);
742 platform_device_unregister(hdmi.audio_pdev);
744 hdmi_uninit_output(pdev);
746 hdmi_pll_uninit(&hdmi.pll);
748 pm_runtime_disable(&pdev->dev);
751 static const struct component_ops hdmi4_component_ops = {
753 .unbind = hdmi4_unbind,
756 static int hdmi4_probe(struct platform_device *pdev)
758 return component_add(&pdev->dev, &hdmi4_component_ops);
761 static int hdmi4_remove(struct platform_device *pdev)
763 component_del(&pdev->dev, &hdmi4_component_ops);
767 static int hdmi_runtime_suspend(struct device *dev)
774 static int hdmi_runtime_resume(struct device *dev)
778 r = dispc_runtime_get();
785 static const struct dev_pm_ops hdmi_pm_ops = {
786 .runtime_suspend = hdmi_runtime_suspend,
787 .runtime_resume = hdmi_runtime_resume,
790 static const struct of_device_id hdmi_of_match[] = {
791 { .compatible = "ti,omap4-hdmi", },
795 static struct platform_driver omapdss_hdmihw_driver = {
796 .probe = hdmi4_probe,
797 .remove = hdmi4_remove,
799 .name = "omapdss_hdmi",
801 .of_match_table = hdmi_of_match,
802 .suppress_bind_attrs = true,
806 int __init hdmi4_init_platform_driver(void)
808 return platform_driver_register(&omapdss_hdmihw_driver);
811 void hdmi4_uninit_platform_driver(void)
813 platform_driver_unregister(&omapdss_hdmihw_driver);