1 // SPDX-License-Identifier: GPL-2.0-only
3 * HDMI driver for OMAP5
5 * Copyright (C) 2014 Texas Instruments Incorporated
10 * Archit Taneja <archit@ti.com>
11 * Tomi Valkeinen <tomi.valkeinen@ti.com>
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/of_graph.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/component.h>
31 #include <video/omapfb_dss.h>
32 #include <sound/omap-hdmi-audio.h>
34 #include "hdmi5_core.h"
36 #include "dss_features.h"
38 static struct omap_hdmi hdmi
;
40 static int hdmi_runtime_get(void)
44 DSSDBG("hdmi_runtime_get\n");
46 r
= pm_runtime_resume_and_get(&hdmi
.pdev
->dev
);
53 static void hdmi_runtime_put(void)
57 DSSDBG("hdmi_runtime_put\n");
59 r
= pm_runtime_put_sync(&hdmi
.pdev
->dev
);
60 WARN_ON(r
< 0 && r
!= -ENOSYS
);
63 static irqreturn_t
hdmi_irq_handler(int irq
, void *data
)
65 struct hdmi_wp_data
*wp
= data
;
68 irqstatus
= hdmi_wp_get_irqstatus(wp
);
69 hdmi_wp_set_irqstatus(wp
, irqstatus
);
71 if ((irqstatus
& HDMI_IRQ_LINK_CONNECT
) &&
72 irqstatus
& HDMI_IRQ_LINK_DISCONNECT
) {
75 * If we get both connect and disconnect interrupts at the same
76 * time, turn off the PHY, clear interrupts, and restart, which
77 * raises connect interrupt if a cable is connected, or nothing
78 * if cable is not connected.
81 hdmi_wp_set_phy_pwr(wp
, HDMI_PHYPWRCMD_OFF
);
84 * We always get bogus CONNECT & DISCONNECT interrupts when
85 * setting the PHY to LDOON. To ignore those, we force the RXDET
86 * line to 0 until the PHY power state has been changed.
88 v
= hdmi_read_reg(hdmi
.phy
.base
, HDMI_TXPHY_PAD_CFG_CTRL
);
89 v
= FLD_MOD(v
, 1, 15, 15); /* FORCE_RXDET_HIGH */
90 v
= FLD_MOD(v
, 0, 14, 7); /* RXDET_LINE */
91 hdmi_write_reg(hdmi
.phy
.base
, HDMI_TXPHY_PAD_CFG_CTRL
, v
);
93 hdmi_wp_set_irqstatus(wp
, HDMI_IRQ_LINK_CONNECT
|
94 HDMI_IRQ_LINK_DISCONNECT
);
96 hdmi_wp_set_phy_pwr(wp
, HDMI_PHYPWRCMD_LDOON
);
98 REG_FLD_MOD(hdmi
.phy
.base
, HDMI_TXPHY_PAD_CFG_CTRL
, 0, 15, 15);
100 } else if (irqstatus
& HDMI_IRQ_LINK_CONNECT
) {
101 hdmi_wp_set_phy_pwr(wp
, HDMI_PHYPWRCMD_TXON
);
102 } else if (irqstatus
& HDMI_IRQ_LINK_DISCONNECT
) {
103 hdmi_wp_set_phy_pwr(wp
, HDMI_PHYPWRCMD_LDOON
);
109 static int hdmi_init_regulator(void)
111 struct regulator
*reg
;
113 if (hdmi
.vdda_reg
!= NULL
)
116 reg
= devm_regulator_get(&hdmi
.pdev
->dev
, "vdda");
118 DSSERR("can't get VDDA regulator\n");
127 static int hdmi_power_on_core(struct omap_dss_device
*dssdev
)
131 r
= regulator_enable(hdmi
.vdda_reg
);
135 r
= hdmi_runtime_get();
137 goto err_runtime_get
;
139 /* Make selection of HDMI in DSS */
140 dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK
);
142 hdmi
.core_enabled
= true;
147 regulator_disable(hdmi
.vdda_reg
);
152 static void hdmi_power_off_core(struct omap_dss_device
*dssdev
)
154 hdmi
.core_enabled
= false;
157 regulator_disable(hdmi
.vdda_reg
);
160 static int hdmi_power_on_full(struct omap_dss_device
*dssdev
)
163 struct omap_video_timings
*p
;
164 struct omap_overlay_manager
*mgr
= hdmi
.output
.manager
;
165 struct dss_pll_clock_info hdmi_cinfo
= { 0 };
167 r
= hdmi_power_on_core(dssdev
);
171 p
= &hdmi
.cfg
.timings
;
173 DSSDBG("hdmi_power_on x_res= %d y_res = %d\n", p
->x_res
, p
->y_res
);
175 hdmi_pll_compute(&hdmi
.pll
, p
->pixelclock
, &hdmi_cinfo
);
177 /* disable and clear irqs */
178 hdmi_wp_clear_irqenable(&hdmi
.wp
, 0xffffffff);
179 hdmi_wp_set_irqstatus(&hdmi
.wp
,
180 hdmi_wp_get_irqstatus(&hdmi
.wp
));
182 r
= dss_pll_enable(&hdmi
.pll
.pll
);
184 DSSERR("Failed to enable PLL\n");
188 r
= dss_pll_set_config(&hdmi
.pll
.pll
, &hdmi_cinfo
);
190 DSSERR("Failed to configure PLL\n");
194 r
= hdmi_phy_configure(&hdmi
.phy
, hdmi_cinfo
.clkdco
,
195 hdmi_cinfo
.clkout
[0]);
197 DSSDBG("Failed to start PHY\n");
201 r
= hdmi_wp_set_phy_pwr(&hdmi
.wp
, HDMI_PHYPWRCMD_LDOON
);
205 hdmi5_configure(&hdmi
.core
, &hdmi
.wp
, &hdmi
.cfg
);
207 /* bypass TV gamma table */
208 dispc_enable_gamma_table(0);
211 dss_mgr_set_timings(mgr
, p
);
213 r
= hdmi_wp_video_start(&hdmi
.wp
);
217 r
= dss_mgr_enable(mgr
);
221 hdmi_wp_set_irqenable(&hdmi
.wp
,
222 HDMI_IRQ_LINK_CONNECT
| HDMI_IRQ_LINK_DISCONNECT
);
227 hdmi_wp_video_stop(&hdmi
.wp
);
229 hdmi_wp_set_phy_pwr(&hdmi
.wp
, HDMI_PHYPWRCMD_OFF
);
233 dss_pll_disable(&hdmi
.pll
.pll
);
235 hdmi_power_off_core(dssdev
);
239 static void hdmi_power_off_full(struct omap_dss_device
*dssdev
)
241 struct omap_overlay_manager
*mgr
= hdmi
.output
.manager
;
243 hdmi_wp_clear_irqenable(&hdmi
.wp
, 0xffffffff);
245 dss_mgr_disable(mgr
);
247 hdmi_wp_video_stop(&hdmi
.wp
);
249 hdmi_wp_set_phy_pwr(&hdmi
.wp
, HDMI_PHYPWRCMD_OFF
);
251 dss_pll_disable(&hdmi
.pll
.pll
);
253 hdmi_power_off_core(dssdev
);
256 static int hdmi_display_check_timing(struct omap_dss_device
*dssdev
,
257 struct omap_video_timings
*timings
)
259 struct omap_dss_device
*out
= &hdmi
.output
;
261 /* TODO: proper interlace support */
262 if (timings
->interlace
)
265 if (!dispc_mgr_timings_ok(out
->dispc_channel
, timings
))
271 static void hdmi_display_set_timing(struct omap_dss_device
*dssdev
,
272 struct omap_video_timings
*timings
)
274 mutex_lock(&hdmi
.lock
);
276 hdmi
.cfg
.timings
= *timings
;
278 dispc_set_tv_pclk(timings
->pixelclock
);
280 mutex_unlock(&hdmi
.lock
);
283 static void hdmi_display_get_timings(struct omap_dss_device
*dssdev
,
284 struct omap_video_timings
*timings
)
286 *timings
= hdmi
.cfg
.timings
;
289 static void hdmi_dump_regs(struct seq_file
*s
)
291 mutex_lock(&hdmi
.lock
);
293 if (hdmi_runtime_get()) {
294 mutex_unlock(&hdmi
.lock
);
298 hdmi_wp_dump(&hdmi
.wp
, s
);
299 hdmi_pll_dump(&hdmi
.pll
, s
);
300 hdmi_phy_dump(&hdmi
.phy
, s
);
301 hdmi5_core_dump(&hdmi
.core
, s
);
304 mutex_unlock(&hdmi
.lock
);
307 static int read_edid(u8
*buf
, int len
)
312 mutex_lock(&hdmi
.lock
);
314 r
= hdmi_runtime_get();
317 idlemode
= REG_GET(hdmi
.wp
.base
, HDMI_WP_SYSCONFIG
, 3, 2);
319 REG_FLD_MOD(hdmi
.wp
.base
, HDMI_WP_SYSCONFIG
, 1, 3, 2);
321 r
= hdmi5_read_edid(&hdmi
.core
, buf
, len
);
323 REG_FLD_MOD(hdmi
.wp
.base
, HDMI_WP_SYSCONFIG
, idlemode
, 3, 2);
326 mutex_unlock(&hdmi
.lock
);
331 static void hdmi_start_audio_stream(struct omap_hdmi
*hd
)
333 REG_FLD_MOD(hdmi
.wp
.base
, HDMI_WP_SYSCONFIG
, 1, 3, 2);
334 hdmi_wp_audio_enable(&hd
->wp
, true);
335 hdmi_wp_audio_core_req_enable(&hd
->wp
, true);
338 static void hdmi_stop_audio_stream(struct omap_hdmi
*hd
)
340 hdmi_wp_audio_core_req_enable(&hd
->wp
, false);
341 hdmi_wp_audio_enable(&hd
->wp
, false);
342 REG_FLD_MOD(hd
->wp
.base
, HDMI_WP_SYSCONFIG
, hd
->wp_idlemode
, 3, 2);
345 static int hdmi_display_enable(struct omap_dss_device
*dssdev
)
347 struct omap_dss_device
*out
= &hdmi
.output
;
351 DSSDBG("ENTER hdmi_display_enable\n");
353 mutex_lock(&hdmi
.lock
);
355 if (out
->manager
== NULL
) {
356 DSSERR("failed to enable display: no output/manager\n");
361 r
= hdmi_power_on_full(dssdev
);
363 DSSERR("failed to power on device\n");
367 if (hdmi
.audio_configured
) {
368 r
= hdmi5_audio_config(&hdmi
.core
, &hdmi
.wp
, &hdmi
.audio_config
,
369 hdmi
.cfg
.timings
.pixelclock
);
371 DSSERR("Error restoring audio configuration: %d", r
);
372 hdmi
.audio_abort_cb(&hdmi
.pdev
->dev
);
373 hdmi
.audio_configured
= false;
377 spin_lock_irqsave(&hdmi
.audio_playing_lock
, flags
);
378 if (hdmi
.audio_configured
&& hdmi
.audio_playing
)
379 hdmi_start_audio_stream(&hdmi
);
380 hdmi
.display_enabled
= true;
381 spin_unlock_irqrestore(&hdmi
.audio_playing_lock
, flags
);
383 mutex_unlock(&hdmi
.lock
);
387 mutex_unlock(&hdmi
.lock
);
391 static void hdmi_display_disable(struct omap_dss_device
*dssdev
)
395 DSSDBG("Enter hdmi_display_disable\n");
397 mutex_lock(&hdmi
.lock
);
399 spin_lock_irqsave(&hdmi
.audio_playing_lock
, flags
);
400 hdmi_stop_audio_stream(&hdmi
);
401 hdmi
.display_enabled
= false;
402 spin_unlock_irqrestore(&hdmi
.audio_playing_lock
, flags
);
404 hdmi_power_off_full(dssdev
);
406 mutex_unlock(&hdmi
.lock
);
409 static int hdmi_core_enable(struct omap_dss_device
*dssdev
)
413 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
415 mutex_lock(&hdmi
.lock
);
417 r
= hdmi_power_on_core(dssdev
);
419 DSSERR("failed to power on device\n");
423 mutex_unlock(&hdmi
.lock
);
427 mutex_unlock(&hdmi
.lock
);
431 static void hdmi_core_disable(struct omap_dss_device
*dssdev
)
433 DSSDBG("Enter omapdss_hdmi_core_disable\n");
435 mutex_lock(&hdmi
.lock
);
437 hdmi_power_off_core(dssdev
);
439 mutex_unlock(&hdmi
.lock
);
442 static int hdmi_connect(struct omap_dss_device
*dssdev
,
443 struct omap_dss_device
*dst
)
445 struct omap_overlay_manager
*mgr
;
448 r
= hdmi_init_regulator();
452 mgr
= omap_dss_get_overlay_manager(dssdev
->dispc_channel
);
456 r
= dss_mgr_connect(mgr
, dssdev
);
460 r
= omapdss_output_set_device(dssdev
, dst
);
462 DSSERR("failed to connect output to new device: %s\n",
464 dss_mgr_disconnect(mgr
, dssdev
);
471 static void hdmi_disconnect(struct omap_dss_device
*dssdev
,
472 struct omap_dss_device
*dst
)
474 WARN_ON(dst
!= dssdev
->dst
);
476 if (dst
!= dssdev
->dst
)
479 omapdss_output_unset_device(dssdev
);
482 dss_mgr_disconnect(dssdev
->manager
, dssdev
);
485 static int hdmi_read_edid(struct omap_dss_device
*dssdev
,
491 need_enable
= hdmi
.core_enabled
== false;
494 r
= hdmi_core_enable(dssdev
);
499 r
= read_edid(edid
, len
);
502 hdmi_core_disable(dssdev
);
507 static int hdmi_set_infoframe(struct omap_dss_device
*dssdev
,
508 const struct hdmi_avi_infoframe
*avi
)
510 hdmi
.cfg
.infoframe
= *avi
;
514 static int hdmi_set_hdmi_mode(struct omap_dss_device
*dssdev
,
517 hdmi
.cfg
.hdmi_dvi_mode
= hdmi_mode
? HDMI_HDMI
: HDMI_DVI
;
521 static const struct omapdss_hdmi_ops hdmi_ops
= {
522 .connect
= hdmi_connect
,
523 .disconnect
= hdmi_disconnect
,
525 .enable
= hdmi_display_enable
,
526 .disable
= hdmi_display_disable
,
528 .check_timings
= hdmi_display_check_timing
,
529 .set_timings
= hdmi_display_set_timing
,
530 .get_timings
= hdmi_display_get_timings
,
532 .read_edid
= hdmi_read_edid
,
533 .set_infoframe
= hdmi_set_infoframe
,
534 .set_hdmi_mode
= hdmi_set_hdmi_mode
,
537 static void hdmi_init_output(struct platform_device
*pdev
)
539 struct omap_dss_device
*out
= &hdmi
.output
;
541 out
->dev
= &pdev
->dev
;
542 out
->id
= OMAP_DSS_OUTPUT_HDMI
;
543 out
->output_type
= OMAP_DISPLAY_TYPE_HDMI
;
544 out
->name
= "hdmi.0";
545 out
->dispc_channel
= OMAP_DSS_CHANNEL_DIGIT
;
546 out
->ops
.hdmi
= &hdmi_ops
;
547 out
->owner
= THIS_MODULE
;
549 omapdss_register_output(out
);
552 static void hdmi_uninit_output(struct platform_device
*pdev
)
554 struct omap_dss_device
*out
= &hdmi
.output
;
556 omapdss_unregister_output(out
);
559 static int hdmi_probe_of(struct platform_device
*pdev
)
561 struct device_node
*node
= pdev
->dev
.of_node
;
562 struct device_node
*ep
;
565 ep
= of_graph_get_endpoint_by_regs(node
, 0, -1);
569 r
= hdmi_parse_lanes_of(pdev
, ep
, &hdmi
.phy
);
581 /* Audio callbacks */
582 static int hdmi_audio_startup(struct device
*dev
,
583 void (*abort_cb
)(struct device
*dev
))
585 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
588 mutex_lock(&hd
->lock
);
590 if (!hdmi_mode_has_audio(&hd
->cfg
) || !hd
->display_enabled
) {
595 hd
->audio_abort_cb
= abort_cb
;
598 mutex_unlock(&hd
->lock
);
603 static int hdmi_audio_shutdown(struct device
*dev
)
605 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
607 mutex_lock(&hd
->lock
);
608 hd
->audio_abort_cb
= NULL
;
609 hd
->audio_configured
= false;
610 hd
->audio_playing
= false;
611 mutex_unlock(&hd
->lock
);
616 static int hdmi_audio_start(struct device
*dev
)
618 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
621 WARN_ON(!hdmi_mode_has_audio(&hd
->cfg
));
623 spin_lock_irqsave(&hd
->audio_playing_lock
, flags
);
625 if (hd
->display_enabled
)
626 hdmi_start_audio_stream(hd
);
627 hd
->audio_playing
= true;
629 spin_unlock_irqrestore(&hd
->audio_playing_lock
, flags
);
633 static void hdmi_audio_stop(struct device
*dev
)
635 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
638 WARN_ON(!hdmi_mode_has_audio(&hd
->cfg
));
640 spin_lock_irqsave(&hd
->audio_playing_lock
, flags
);
642 if (hd
->display_enabled
)
643 hdmi_stop_audio_stream(hd
);
644 hd
->audio_playing
= false;
646 spin_unlock_irqrestore(&hd
->audio_playing_lock
, flags
);
649 static int hdmi_audio_config(struct device
*dev
,
650 struct omap_dss_audio
*dss_audio
)
652 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
655 mutex_lock(&hd
->lock
);
657 if (!hdmi_mode_has_audio(&hd
->cfg
) || !hd
->display_enabled
) {
662 ret
= hdmi5_audio_config(&hd
->core
, &hd
->wp
, dss_audio
,
663 hd
->cfg
.timings
.pixelclock
);
666 hd
->audio_configured
= true;
667 hd
->audio_config
= *dss_audio
;
670 mutex_unlock(&hd
->lock
);
675 static const struct omap_hdmi_audio_ops hdmi_audio_ops
= {
676 .audio_startup
= hdmi_audio_startup
,
677 .audio_shutdown
= hdmi_audio_shutdown
,
678 .audio_start
= hdmi_audio_start
,
679 .audio_stop
= hdmi_audio_stop
,
680 .audio_config
= hdmi_audio_config
,
683 static int hdmi_audio_register(struct device
*dev
)
685 struct omap_hdmi_audio_pdata pdata
= {
688 .audio_dma_addr
= hdmi_wp_get_audio_dma_addr(&hdmi
.wp
),
689 .ops
= &hdmi_audio_ops
,
692 hdmi
.audio_pdev
= platform_device_register_data(
693 dev
, "omap-hdmi-audio", PLATFORM_DEVID_AUTO
,
694 &pdata
, sizeof(pdata
));
696 if (IS_ERR(hdmi
.audio_pdev
))
697 return PTR_ERR(hdmi
.audio_pdev
);
701 REG_GET(hdmi
.wp
.base
, HDMI_WP_SYSCONFIG
, 3, 2);
707 /* HDMI HW IP initialisation */
708 static int hdmi5_bind(struct device
*dev
, struct device
*master
, void *data
)
710 struct platform_device
*pdev
= to_platform_device(dev
);
715 platform_set_drvdata(pdev
, &hdmi
);
717 mutex_init(&hdmi
.lock
);
718 spin_lock_init(&hdmi
.audio_playing_lock
);
720 if (pdev
->dev
.of_node
) {
721 r
= hdmi_probe_of(pdev
);
726 r
= hdmi_wp_init(pdev
, &hdmi
.wp
);
730 r
= hdmi_pll_init(pdev
, &hdmi
.pll
, &hdmi
.wp
);
734 r
= hdmi_phy_init(pdev
, &hdmi
.phy
);
738 r
= hdmi5_core_init(pdev
, &hdmi
.core
);
742 irq
= platform_get_irq(pdev
, 0);
744 DSSERR("platform_get_irq failed\n");
749 r
= devm_request_threaded_irq(&pdev
->dev
, irq
,
750 NULL
, hdmi_irq_handler
,
751 IRQF_ONESHOT
, "OMAP HDMI", &hdmi
.wp
);
753 DSSERR("HDMI IRQ request failed\n");
757 pm_runtime_enable(&pdev
->dev
);
759 hdmi_init_output(pdev
);
761 r
= hdmi_audio_register(&pdev
->dev
);
763 DSSERR("Registering HDMI audio failed %d\n", r
);
764 hdmi_uninit_output(pdev
);
765 pm_runtime_disable(&pdev
->dev
);
769 dss_debugfs_create_file("hdmi", hdmi_dump_regs
);
773 hdmi_pll_uninit(&hdmi
.pll
);
777 static void hdmi5_unbind(struct device
*dev
, struct device
*master
, void *data
)
779 struct platform_device
*pdev
= to_platform_device(dev
);
782 platform_device_unregister(hdmi
.audio_pdev
);
784 hdmi_uninit_output(pdev
);
786 hdmi_pll_uninit(&hdmi
.pll
);
788 pm_runtime_disable(&pdev
->dev
);
791 static const struct component_ops hdmi5_component_ops
= {
793 .unbind
= hdmi5_unbind
,
796 static int hdmi5_probe(struct platform_device
*pdev
)
798 return component_add(&pdev
->dev
, &hdmi5_component_ops
);
801 static void hdmi5_remove(struct platform_device
*pdev
)
803 component_del(&pdev
->dev
, &hdmi5_component_ops
);
806 static int hdmi_runtime_suspend(struct device
*dev
)
813 static int hdmi_runtime_resume(struct device
*dev
)
817 r
= dispc_runtime_get();
824 static const struct dev_pm_ops hdmi_pm_ops
= {
825 .runtime_suspend
= hdmi_runtime_suspend
,
826 .runtime_resume
= hdmi_runtime_resume
,
829 static const struct of_device_id hdmi_of_match
[] = {
830 { .compatible
= "ti,omap5-hdmi", },
831 { .compatible
= "ti,dra7-hdmi", },
835 static struct platform_driver omapdss_hdmihw_driver
= {
836 .probe
= hdmi5_probe
,
837 .remove
= hdmi5_remove
,
839 .name
= "omapdss_hdmi5",
841 .of_match_table
= hdmi_of_match
,
842 .suppress_bind_attrs
= true,
846 int __init
hdmi5_init_platform_driver(void)
848 return platform_driver_register(&omapdss_hdmihw_driver
);
851 void hdmi5_uninit_platform_driver(void)
853 platform_driver_unregister(&omapdss_hdmihw_driver
);