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/of_graph.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/component.h>
26 #include <video/omapfb_dss.h>
27 #include <sound/omap-hdmi-audio.h>
29 #include "hdmi4_core.h"
31 #include "dss_features.h"
34 static struct omap_hdmi hdmi
;
36 static int hdmi_runtime_get(void)
40 DSSDBG("hdmi_runtime_get\n");
42 r
= pm_runtime_resume_and_get(&hdmi
.pdev
->dev
);
49 static void hdmi_runtime_put(void)
53 DSSDBG("hdmi_runtime_put\n");
55 r
= pm_runtime_put_sync(&hdmi
.pdev
->dev
);
56 WARN_ON(r
< 0 && r
!= -ENOSYS
);
59 static irqreturn_t
hdmi_irq_handler(int irq
, void *data
)
61 struct hdmi_wp_data
*wp
= data
;
64 irqstatus
= hdmi_wp_get_irqstatus(wp
);
65 hdmi_wp_set_irqstatus(wp
, irqstatus
);
67 if ((irqstatus
& HDMI_IRQ_LINK_CONNECT
) &&
68 irqstatus
& HDMI_IRQ_LINK_DISCONNECT
) {
70 * If we get both connect and disconnect interrupts at the same
71 * time, turn off the PHY, clear interrupts, and restart, which
72 * raises connect interrupt if a cable is connected, or nothing
73 * if cable is not connected.
75 hdmi_wp_set_phy_pwr(wp
, HDMI_PHYPWRCMD_OFF
);
77 hdmi_wp_set_irqstatus(wp
, HDMI_IRQ_LINK_CONNECT
|
78 HDMI_IRQ_LINK_DISCONNECT
);
80 hdmi_wp_set_phy_pwr(wp
, HDMI_PHYPWRCMD_LDOON
);
81 } else if (irqstatus
& HDMI_IRQ_LINK_CONNECT
) {
82 hdmi_wp_set_phy_pwr(wp
, HDMI_PHYPWRCMD_TXON
);
83 } else if (irqstatus
& HDMI_IRQ_LINK_DISCONNECT
) {
84 hdmi_wp_set_phy_pwr(wp
, HDMI_PHYPWRCMD_LDOON
);
90 static int hdmi_init_regulator(void)
92 struct regulator
*reg
;
94 if (hdmi
.vdda_reg
!= NULL
)
97 reg
= devm_regulator_get(&hdmi
.pdev
->dev
, "vdda");
100 if (PTR_ERR(reg
) != -EPROBE_DEFER
)
101 DSSERR("can't get VDDA regulator\n");
110 static int hdmi_power_on_core(struct omap_dss_device
*dssdev
)
114 r
= regulator_enable(hdmi
.vdda_reg
);
118 r
= hdmi_runtime_get();
120 goto err_runtime_get
;
122 /* Make selection of HDMI in DSS */
123 dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK
);
125 hdmi
.core_enabled
= true;
130 regulator_disable(hdmi
.vdda_reg
);
135 static void hdmi_power_off_core(struct omap_dss_device
*dssdev
)
137 hdmi
.core_enabled
= false;
140 regulator_disable(hdmi
.vdda_reg
);
143 static int hdmi_power_on_full(struct omap_dss_device
*dssdev
)
146 struct omap_video_timings
*p
;
147 struct omap_overlay_manager
*mgr
= hdmi
.output
.manager
;
148 struct hdmi_wp_data
*wp
= &hdmi
.wp
;
149 struct dss_pll_clock_info hdmi_cinfo
= { 0 };
151 r
= hdmi_power_on_core(dssdev
);
155 /* disable and clear irqs */
156 hdmi_wp_clear_irqenable(wp
, 0xffffffff);
157 hdmi_wp_set_irqstatus(wp
, 0xffffffff);
159 p
= &hdmi
.cfg
.timings
;
161 DSSDBG("hdmi_power_on x_res= %d y_res = %d\n", p
->x_res
, p
->y_res
);
163 hdmi_pll_compute(&hdmi
.pll
, p
->pixelclock
, &hdmi_cinfo
);
165 r
= dss_pll_enable(&hdmi
.pll
.pll
);
167 DSSERR("Failed to enable PLL\n");
171 r
= dss_pll_set_config(&hdmi
.pll
.pll
, &hdmi_cinfo
);
173 DSSERR("Failed to configure PLL\n");
177 r
= hdmi_phy_configure(&hdmi
.phy
, hdmi_cinfo
.clkdco
,
178 hdmi_cinfo
.clkout
[0]);
180 DSSDBG("Failed to configure PHY\n");
184 r
= hdmi_wp_set_phy_pwr(wp
, HDMI_PHYPWRCMD_LDOON
);
188 hdmi4_configure(&hdmi
.core
, &hdmi
.wp
, &hdmi
.cfg
);
190 /* bypass TV gamma table */
191 dispc_enable_gamma_table(0);
194 dss_mgr_set_timings(mgr
, p
);
196 r
= hdmi_wp_video_start(&hdmi
.wp
);
200 r
= dss_mgr_enable(mgr
);
204 hdmi_wp_set_irqenable(wp
,
205 HDMI_IRQ_LINK_CONNECT
| HDMI_IRQ_LINK_DISCONNECT
);
210 hdmi_wp_video_stop(&hdmi
.wp
);
212 hdmi_wp_set_phy_pwr(&hdmi
.wp
, HDMI_PHYPWRCMD_OFF
);
216 dss_pll_disable(&hdmi
.pll
.pll
);
218 hdmi_power_off_core(dssdev
);
222 static void hdmi_power_off_full(struct omap_dss_device
*dssdev
)
224 struct omap_overlay_manager
*mgr
= hdmi
.output
.manager
;
226 hdmi_wp_clear_irqenable(&hdmi
.wp
, 0xffffffff);
228 dss_mgr_disable(mgr
);
230 hdmi_wp_video_stop(&hdmi
.wp
);
232 hdmi_wp_set_phy_pwr(&hdmi
.wp
, HDMI_PHYPWRCMD_OFF
);
234 dss_pll_disable(&hdmi
.pll
.pll
);
236 hdmi_power_off_core(dssdev
);
239 static int hdmi_display_check_timing(struct omap_dss_device
*dssdev
,
240 struct omap_video_timings
*timings
)
242 struct omap_dss_device
*out
= &hdmi
.output
;
244 if (!dispc_mgr_timings_ok(out
->dispc_channel
, timings
))
250 static void hdmi_display_set_timing(struct omap_dss_device
*dssdev
,
251 struct omap_video_timings
*timings
)
253 mutex_lock(&hdmi
.lock
);
255 hdmi
.cfg
.timings
= *timings
;
257 dispc_set_tv_pclk(timings
->pixelclock
);
259 mutex_unlock(&hdmi
.lock
);
262 static void hdmi_display_get_timings(struct omap_dss_device
*dssdev
,
263 struct omap_video_timings
*timings
)
265 *timings
= hdmi
.cfg
.timings
;
268 static void hdmi_dump_regs(struct seq_file
*s
)
270 mutex_lock(&hdmi
.lock
);
272 if (hdmi_runtime_get()) {
273 mutex_unlock(&hdmi
.lock
);
277 hdmi_wp_dump(&hdmi
.wp
, s
);
278 hdmi_pll_dump(&hdmi
.pll
, s
);
279 hdmi_phy_dump(&hdmi
.phy
, s
);
280 hdmi4_core_dump(&hdmi
.core
, s
);
283 mutex_unlock(&hdmi
.lock
);
286 static int read_edid(u8
*buf
, int len
)
290 mutex_lock(&hdmi
.lock
);
292 r
= hdmi_runtime_get();
295 r
= hdmi4_read_edid(&hdmi
.core
, buf
, len
);
298 mutex_unlock(&hdmi
.lock
);
303 static void hdmi_start_audio_stream(struct omap_hdmi
*hd
)
305 hdmi_wp_audio_enable(&hd
->wp
, true);
306 hdmi4_audio_start(&hd
->core
, &hd
->wp
);
309 static void hdmi_stop_audio_stream(struct omap_hdmi
*hd
)
311 hdmi4_audio_stop(&hd
->core
, &hd
->wp
);
312 hdmi_wp_audio_enable(&hd
->wp
, false);
315 static int hdmi_display_enable(struct omap_dss_device
*dssdev
)
317 struct omap_dss_device
*out
= &hdmi
.output
;
321 DSSDBG("ENTER hdmi_display_enable\n");
323 mutex_lock(&hdmi
.lock
);
325 if (out
->manager
== NULL
) {
326 DSSERR("failed to enable display: no output/manager\n");
331 r
= hdmi_power_on_full(dssdev
);
333 DSSERR("failed to power on device\n");
337 if (hdmi
.audio_configured
) {
338 r
= hdmi4_audio_config(&hdmi
.core
, &hdmi
.wp
, &hdmi
.audio_config
,
339 hdmi
.cfg
.timings
.pixelclock
);
341 DSSERR("Error restoring audio configuration: %d", r
);
342 hdmi
.audio_abort_cb(&hdmi
.pdev
->dev
);
343 hdmi
.audio_configured
= false;
347 spin_lock_irqsave(&hdmi
.audio_playing_lock
, flags
);
348 if (hdmi
.audio_configured
&& hdmi
.audio_playing
)
349 hdmi_start_audio_stream(&hdmi
);
350 hdmi
.display_enabled
= true;
351 spin_unlock_irqrestore(&hdmi
.audio_playing_lock
, flags
);
353 mutex_unlock(&hdmi
.lock
);
357 mutex_unlock(&hdmi
.lock
);
361 static void hdmi_display_disable(struct omap_dss_device
*dssdev
)
365 DSSDBG("Enter hdmi_display_disable\n");
367 mutex_lock(&hdmi
.lock
);
369 spin_lock_irqsave(&hdmi
.audio_playing_lock
, flags
);
370 hdmi_stop_audio_stream(&hdmi
);
371 hdmi
.display_enabled
= false;
372 spin_unlock_irqrestore(&hdmi
.audio_playing_lock
, flags
);
374 hdmi_power_off_full(dssdev
);
376 mutex_unlock(&hdmi
.lock
);
379 static int hdmi_core_enable(struct omap_dss_device
*dssdev
)
383 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
385 mutex_lock(&hdmi
.lock
);
387 r
= hdmi_power_on_core(dssdev
);
389 DSSERR("failed to power on device\n");
393 mutex_unlock(&hdmi
.lock
);
397 mutex_unlock(&hdmi
.lock
);
401 static void hdmi_core_disable(struct omap_dss_device
*dssdev
)
403 DSSDBG("Enter omapdss_hdmi_core_disable\n");
405 mutex_lock(&hdmi
.lock
);
407 hdmi_power_off_core(dssdev
);
409 mutex_unlock(&hdmi
.lock
);
412 static int hdmi_connect(struct omap_dss_device
*dssdev
,
413 struct omap_dss_device
*dst
)
415 struct omap_overlay_manager
*mgr
;
418 r
= hdmi_init_regulator();
422 mgr
= omap_dss_get_overlay_manager(dssdev
->dispc_channel
);
426 r
= dss_mgr_connect(mgr
, dssdev
);
430 r
= omapdss_output_set_device(dssdev
, dst
);
432 DSSERR("failed to connect output to new device: %s\n",
434 dss_mgr_disconnect(mgr
, dssdev
);
441 static void hdmi_disconnect(struct omap_dss_device
*dssdev
,
442 struct omap_dss_device
*dst
)
444 WARN_ON(dst
!= dssdev
->dst
);
446 if (dst
!= dssdev
->dst
)
449 omapdss_output_unset_device(dssdev
);
452 dss_mgr_disconnect(dssdev
->manager
, dssdev
);
455 static int hdmi_read_edid(struct omap_dss_device
*dssdev
,
458 bool need_enable
= !hdmi
.core_enabled
;
462 r
= hdmi_core_enable(dssdev
);
467 r
= read_edid(edid
, len
);
470 hdmi_core_disable(dssdev
);
475 static int hdmi_set_infoframe(struct omap_dss_device
*dssdev
,
476 const struct hdmi_avi_infoframe
*avi
)
478 hdmi
.cfg
.infoframe
= *avi
;
482 static int hdmi_set_hdmi_mode(struct omap_dss_device
*dssdev
,
485 hdmi
.cfg
.hdmi_dvi_mode
= hdmi_mode
? HDMI_HDMI
: HDMI_DVI
;
489 static const struct omapdss_hdmi_ops hdmi_ops
= {
490 .connect
= hdmi_connect
,
491 .disconnect
= hdmi_disconnect
,
493 .enable
= hdmi_display_enable
,
494 .disable
= hdmi_display_disable
,
496 .check_timings
= hdmi_display_check_timing
,
497 .set_timings
= hdmi_display_set_timing
,
498 .get_timings
= hdmi_display_get_timings
,
500 .read_edid
= hdmi_read_edid
,
501 .set_infoframe
= hdmi_set_infoframe
,
502 .set_hdmi_mode
= hdmi_set_hdmi_mode
,
505 static void hdmi_init_output(struct platform_device
*pdev
)
507 struct omap_dss_device
*out
= &hdmi
.output
;
509 out
->dev
= &pdev
->dev
;
510 out
->id
= OMAP_DSS_OUTPUT_HDMI
;
511 out
->output_type
= OMAP_DISPLAY_TYPE_HDMI
;
512 out
->name
= "hdmi.0";
513 out
->dispc_channel
= OMAP_DSS_CHANNEL_DIGIT
;
514 out
->ops
.hdmi
= &hdmi_ops
;
515 out
->owner
= THIS_MODULE
;
517 omapdss_register_output(out
);
520 static void hdmi_uninit_output(struct platform_device
*pdev
)
522 struct omap_dss_device
*out
= &hdmi
.output
;
524 omapdss_unregister_output(out
);
527 static int hdmi_probe_of(struct platform_device
*pdev
)
529 struct device_node
*node
= pdev
->dev
.of_node
;
530 struct device_node
*ep
;
533 ep
= of_graph_get_endpoint_by_regs(node
, 0, -1);
537 r
= hdmi_parse_lanes_of(pdev
, ep
, &hdmi
.phy
);
549 /* Audio callbacks */
550 static int hdmi_audio_startup(struct device
*dev
,
551 void (*abort_cb
)(struct device
*dev
))
553 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
556 mutex_lock(&hd
->lock
);
558 if (!hdmi_mode_has_audio(&hd
->cfg
) || !hd
->display_enabled
) {
563 hd
->audio_abort_cb
= abort_cb
;
566 mutex_unlock(&hd
->lock
);
571 static int hdmi_audio_shutdown(struct device
*dev
)
573 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
575 mutex_lock(&hd
->lock
);
576 hd
->audio_abort_cb
= NULL
;
577 hd
->audio_configured
= false;
578 hd
->audio_playing
= false;
579 mutex_unlock(&hd
->lock
);
584 static int hdmi_audio_start(struct device
*dev
)
586 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
589 WARN_ON(!hdmi_mode_has_audio(&hd
->cfg
));
591 spin_lock_irqsave(&hd
->audio_playing_lock
, flags
);
593 if (hd
->display_enabled
)
594 hdmi_start_audio_stream(hd
);
595 hd
->audio_playing
= true;
597 spin_unlock_irqrestore(&hd
->audio_playing_lock
, flags
);
601 static void hdmi_audio_stop(struct device
*dev
)
603 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
606 WARN_ON(!hdmi_mode_has_audio(&hd
->cfg
));
608 spin_lock_irqsave(&hd
->audio_playing_lock
, flags
);
610 if (hd
->display_enabled
)
611 hdmi_stop_audio_stream(hd
);
612 hd
->audio_playing
= false;
614 spin_unlock_irqrestore(&hd
->audio_playing_lock
, flags
);
617 static int hdmi_audio_config(struct device
*dev
,
618 struct omap_dss_audio
*dss_audio
)
620 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
623 mutex_lock(&hd
->lock
);
625 if (!hdmi_mode_has_audio(&hd
->cfg
) || !hd
->display_enabled
) {
630 ret
= hdmi4_audio_config(&hd
->core
, &hd
->wp
, dss_audio
,
631 hd
->cfg
.timings
.pixelclock
);
633 hd
->audio_configured
= true;
634 hd
->audio_config
= *dss_audio
;
637 mutex_unlock(&hd
->lock
);
642 static const struct omap_hdmi_audio_ops hdmi_audio_ops
= {
643 .audio_startup
= hdmi_audio_startup
,
644 .audio_shutdown
= hdmi_audio_shutdown
,
645 .audio_start
= hdmi_audio_start
,
646 .audio_stop
= hdmi_audio_stop
,
647 .audio_config
= hdmi_audio_config
,
650 static int hdmi_audio_register(struct device
*dev
)
652 struct omap_hdmi_audio_pdata pdata
= {
655 .audio_dma_addr
= hdmi_wp_get_audio_dma_addr(&hdmi
.wp
),
656 .ops
= &hdmi_audio_ops
,
659 hdmi
.audio_pdev
= platform_device_register_data(
660 dev
, "omap-hdmi-audio", PLATFORM_DEVID_AUTO
,
661 &pdata
, sizeof(pdata
));
663 return PTR_ERR_OR_ZERO(hdmi
.audio_pdev
);
666 /* HDMI HW IP initialisation */
667 static int hdmi4_bind(struct device
*dev
, struct device
*master
, void *data
)
669 struct platform_device
*pdev
= to_platform_device(dev
);
674 platform_set_drvdata(pdev
, &hdmi
);
676 mutex_init(&hdmi
.lock
);
677 spin_lock_init(&hdmi
.audio_playing_lock
);
679 if (pdev
->dev
.of_node
) {
680 r
= hdmi_probe_of(pdev
);
685 r
= hdmi_wp_init(pdev
, &hdmi
.wp
);
689 r
= hdmi_pll_init(pdev
, &hdmi
.pll
, &hdmi
.wp
);
693 r
= hdmi_phy_init(pdev
, &hdmi
.phy
);
697 r
= hdmi4_core_init(pdev
, &hdmi
.core
);
701 irq
= platform_get_irq(pdev
, 0);
703 DSSERR("platform_get_irq failed\n");
708 r
= devm_request_threaded_irq(&pdev
->dev
, irq
,
709 NULL
, hdmi_irq_handler
,
710 IRQF_ONESHOT
, "OMAP HDMI", &hdmi
.wp
);
712 DSSERR("HDMI IRQ request failed\n");
716 pm_runtime_enable(&pdev
->dev
);
718 hdmi_init_output(pdev
);
720 r
= hdmi_audio_register(&pdev
->dev
);
722 DSSERR("Registering HDMI audio failed\n");
723 hdmi_uninit_output(pdev
);
724 pm_runtime_disable(&pdev
->dev
);
728 dss_debugfs_create_file("hdmi", hdmi_dump_regs
);
732 hdmi_pll_uninit(&hdmi
.pll
);
736 static void hdmi4_unbind(struct device
*dev
, struct device
*master
, void *data
)
738 struct platform_device
*pdev
= to_platform_device(dev
);
741 platform_device_unregister(hdmi
.audio_pdev
);
743 hdmi_uninit_output(pdev
);
745 hdmi_pll_uninit(&hdmi
.pll
);
747 pm_runtime_disable(&pdev
->dev
);
750 static const struct component_ops hdmi4_component_ops
= {
752 .unbind
= hdmi4_unbind
,
755 static int hdmi4_probe(struct platform_device
*pdev
)
757 return component_add(&pdev
->dev
, &hdmi4_component_ops
);
760 static void hdmi4_remove(struct platform_device
*pdev
)
762 component_del(&pdev
->dev
, &hdmi4_component_ops
);
765 static int hdmi_runtime_suspend(struct device
*dev
)
772 static int hdmi_runtime_resume(struct device
*dev
)
776 r
= dispc_runtime_get();
783 static const struct dev_pm_ops hdmi_pm_ops
= {
784 .runtime_suspend
= hdmi_runtime_suspend
,
785 .runtime_resume
= hdmi_runtime_resume
,
788 static const struct of_device_id hdmi_of_match
[] = {
789 { .compatible
= "ti,omap4-hdmi", },
793 static struct platform_driver omapdss_hdmihw_driver
= {
794 .probe
= hdmi4_probe
,
795 .remove
= hdmi4_remove
,
797 .name
= "omapdss_hdmi",
799 .of_match_table
= hdmi_of_match
,
800 .suppress_bind_attrs
= true,
804 int __init
hdmi4_init_platform_driver(void)
806 return platform_driver_register(&omapdss_hdmihw_driver
);
809 void hdmi4_uninit_platform_driver(void)
811 platform_driver_unregister(&omapdss_hdmihw_driver
);