1 // SPDX-License-Identifier: GPL-2.0-only
3 * HDMI driver for OMAP5
5 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
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>
27 #include <linux/gpio.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/component.h>
31 #include <linux/of_graph.h>
32 #include <sound/omap-hdmi-audio.h>
35 #include "hdmi5_core.h"
38 static int hdmi_runtime_get(struct omap_hdmi
*hdmi
)
42 DSSDBG("hdmi_runtime_get\n");
44 r
= pm_runtime_get_sync(&hdmi
->pdev
->dev
);
52 static void hdmi_runtime_put(struct omap_hdmi
*hdmi
)
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 omap_hdmi
*hdmi
= data
;
65 struct hdmi_wp_data
*wp
= &hdmi
->wp
;
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_power_on_core(struct omap_hdmi
*hdmi
)
113 r
= regulator_enable(hdmi
->vdda_reg
);
117 r
= hdmi_runtime_get(hdmi
);
119 goto err_runtime_get
;
121 /* Make selection of HDMI in DSS */
122 dss_select_hdmi_venc_clk_source(hdmi
->dss
, DSS_HDMI_M_PCLK
);
124 hdmi
->core_enabled
= true;
129 regulator_disable(hdmi
->vdda_reg
);
134 static void hdmi_power_off_core(struct omap_hdmi
*hdmi
)
136 hdmi
->core_enabled
= false;
138 hdmi_runtime_put(hdmi
);
139 regulator_disable(hdmi
->vdda_reg
);
142 static int hdmi_power_on_full(struct omap_hdmi
*hdmi
)
145 const struct videomode
*vm
;
146 struct dss_pll_clock_info hdmi_cinfo
= { 0 };
149 r
= hdmi_power_on_core(hdmi
);
155 DSSDBG("hdmi_power_on hactive= %d vactive = %d\n", vm
->hactive
,
159 if (vm
->flags
& DISPLAY_FLAGS_DOUBLECLK
)
162 /* DSS_HDMI_TCLK is bitclk / 10 */
165 dss_pll_calc_b(&hdmi
->pll
.pll
, clk_get_rate(hdmi
->pll
.pll
.clkin
),
168 /* disable and clear irqs */
169 hdmi_wp_clear_irqenable(&hdmi
->wp
, 0xffffffff);
170 hdmi_wp_set_irqstatus(&hdmi
->wp
,
171 hdmi_wp_get_irqstatus(&hdmi
->wp
));
173 r
= dss_pll_enable(&hdmi
->pll
.pll
);
175 DSSERR("Failed to enable PLL\n");
179 r
= dss_pll_set_config(&hdmi
->pll
.pll
, &hdmi_cinfo
);
181 DSSERR("Failed to configure PLL\n");
185 r
= hdmi_phy_configure(&hdmi
->phy
, hdmi_cinfo
.clkdco
,
186 hdmi_cinfo
.clkout
[0]);
188 DSSDBG("Failed to start PHY\n");
192 r
= hdmi_wp_set_phy_pwr(&hdmi
->wp
, HDMI_PHYPWRCMD_LDOON
);
196 hdmi5_configure(&hdmi
->core
, &hdmi
->wp
, &hdmi
->cfg
);
198 r
= dss_mgr_enable(&hdmi
->output
);
202 r
= hdmi_wp_video_start(&hdmi
->wp
);
206 hdmi_wp_set_irqenable(&hdmi
->wp
,
207 HDMI_IRQ_LINK_CONNECT
| HDMI_IRQ_LINK_DISCONNECT
);
212 dss_mgr_disable(&hdmi
->output
);
214 hdmi_wp_set_phy_pwr(&hdmi
->wp
, HDMI_PHYPWRCMD_OFF
);
218 dss_pll_disable(&hdmi
->pll
.pll
);
220 hdmi_power_off_core(hdmi
);
224 static void hdmi_power_off_full(struct omap_hdmi
*hdmi
)
226 hdmi_wp_clear_irqenable(&hdmi
->wp
, 0xffffffff);
228 hdmi_wp_video_stop(&hdmi
->wp
);
230 dss_mgr_disable(&hdmi
->output
);
232 hdmi_wp_set_phy_pwr(&hdmi
->wp
, HDMI_PHYPWRCMD_OFF
);
234 dss_pll_disable(&hdmi
->pll
.pll
);
236 hdmi_power_off_core(hdmi
);
239 static void hdmi_display_set_timings(struct omap_dss_device
*dssdev
,
240 const struct drm_display_mode
*mode
)
242 struct omap_hdmi
*hdmi
= dssdev_to_hdmi(dssdev
);
244 mutex_lock(&hdmi
->lock
);
246 drm_display_mode_to_videomode(mode
, &hdmi
->cfg
.vm
);
248 dispc_set_tv_pclk(hdmi
->dss
->dispc
, mode
->clock
* 1000);
250 mutex_unlock(&hdmi
->lock
);
253 static int hdmi_dump_regs(struct seq_file
*s
, void *p
)
255 struct omap_hdmi
*hdmi
= s
->private;
257 mutex_lock(&hdmi
->lock
);
259 if (hdmi_runtime_get(hdmi
)) {
260 mutex_unlock(&hdmi
->lock
);
264 hdmi_wp_dump(&hdmi
->wp
, s
);
265 hdmi_pll_dump(&hdmi
->pll
, s
);
266 hdmi_phy_dump(&hdmi
->phy
, s
);
267 hdmi5_core_dump(&hdmi
->core
, s
);
269 hdmi_runtime_put(hdmi
);
270 mutex_unlock(&hdmi
->lock
);
274 static int read_edid(struct omap_hdmi
*hdmi
, u8
*buf
, int len
)
279 mutex_lock(&hdmi
->lock
);
281 r
= hdmi_runtime_get(hdmi
);
284 idlemode
= REG_GET(hdmi
->wp
.base
, HDMI_WP_SYSCONFIG
, 3, 2);
286 REG_FLD_MOD(hdmi
->wp
.base
, HDMI_WP_SYSCONFIG
, 1, 3, 2);
288 r
= hdmi5_read_edid(&hdmi
->core
, buf
, len
);
290 REG_FLD_MOD(hdmi
->wp
.base
, HDMI_WP_SYSCONFIG
, idlemode
, 3, 2);
292 hdmi_runtime_put(hdmi
);
293 mutex_unlock(&hdmi
->lock
);
298 static void hdmi_start_audio_stream(struct omap_hdmi
*hd
)
300 REG_FLD_MOD(hd
->wp
.base
, HDMI_WP_SYSCONFIG
, 1, 3, 2);
301 hdmi_wp_audio_enable(&hd
->wp
, true);
302 hdmi_wp_audio_core_req_enable(&hd
->wp
, true);
305 static void hdmi_stop_audio_stream(struct omap_hdmi
*hd
)
307 hdmi_wp_audio_core_req_enable(&hd
->wp
, false);
308 hdmi_wp_audio_enable(&hd
->wp
, false);
309 REG_FLD_MOD(hd
->wp
.base
, HDMI_WP_SYSCONFIG
, hd
->wp_idlemode
, 3, 2);
312 static void hdmi_display_enable(struct omap_dss_device
*dssdev
)
314 struct omap_hdmi
*hdmi
= dssdev_to_hdmi(dssdev
);
318 DSSDBG("ENTER hdmi_display_enable\n");
320 mutex_lock(&hdmi
->lock
);
322 r
= hdmi_power_on_full(hdmi
);
324 DSSERR("failed to power on device\n");
328 if (hdmi
->audio_configured
) {
329 r
= hdmi5_audio_config(&hdmi
->core
, &hdmi
->wp
,
331 hdmi
->cfg
.vm
.pixelclock
);
333 DSSERR("Error restoring audio configuration: %d", r
);
334 hdmi
->audio_abort_cb(&hdmi
->pdev
->dev
);
335 hdmi
->audio_configured
= false;
339 spin_lock_irqsave(&hdmi
->audio_playing_lock
, flags
);
340 if (hdmi
->audio_configured
&& hdmi
->audio_playing
)
341 hdmi_start_audio_stream(hdmi
);
342 hdmi
->display_enabled
= true;
343 spin_unlock_irqrestore(&hdmi
->audio_playing_lock
, flags
);
346 mutex_unlock(&hdmi
->lock
);
349 static void hdmi_display_disable(struct omap_dss_device
*dssdev
)
351 struct omap_hdmi
*hdmi
= dssdev_to_hdmi(dssdev
);
354 DSSDBG("Enter hdmi_display_disable\n");
356 mutex_lock(&hdmi
->lock
);
358 spin_lock_irqsave(&hdmi
->audio_playing_lock
, flags
);
359 hdmi_stop_audio_stream(hdmi
);
360 hdmi
->display_enabled
= false;
361 spin_unlock_irqrestore(&hdmi
->audio_playing_lock
, flags
);
363 hdmi_power_off_full(hdmi
);
365 mutex_unlock(&hdmi
->lock
);
368 static int hdmi_core_enable(struct omap_hdmi
*hdmi
)
372 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
374 mutex_lock(&hdmi
->lock
);
376 r
= hdmi_power_on_core(hdmi
);
378 DSSERR("failed to power on device\n");
382 mutex_unlock(&hdmi
->lock
);
386 mutex_unlock(&hdmi
->lock
);
390 static void hdmi_core_disable(struct omap_hdmi
*hdmi
)
392 DSSDBG("Enter omapdss_hdmi_core_disable\n");
394 mutex_lock(&hdmi
->lock
);
396 hdmi_power_off_core(hdmi
);
398 mutex_unlock(&hdmi
->lock
);
401 static int hdmi_connect(struct omap_dss_device
*src
,
402 struct omap_dss_device
*dst
)
404 return omapdss_device_connect(dst
->dss
, dst
, dst
->next
);
407 static void hdmi_disconnect(struct omap_dss_device
*src
,
408 struct omap_dss_device
*dst
)
410 omapdss_device_disconnect(dst
, dst
->next
);
413 static int hdmi_read_edid(struct omap_dss_device
*dssdev
,
416 struct omap_hdmi
*hdmi
= dssdev_to_hdmi(dssdev
);
420 need_enable
= hdmi
->core_enabled
== false;
423 r
= hdmi_core_enable(hdmi
);
428 r
= read_edid(hdmi
, edid
, len
);
431 hdmi_core_disable(hdmi
);
436 static int hdmi_set_infoframe(struct omap_dss_device
*dssdev
,
437 const struct hdmi_avi_infoframe
*avi
)
439 struct omap_hdmi
*hdmi
= dssdev_to_hdmi(dssdev
);
441 hdmi
->cfg
.infoframe
= *avi
;
445 static int hdmi_set_hdmi_mode(struct omap_dss_device
*dssdev
,
448 struct omap_hdmi
*hdmi
= dssdev_to_hdmi(dssdev
);
450 hdmi
->cfg
.hdmi_dvi_mode
= hdmi_mode
? HDMI_HDMI
: HDMI_DVI
;
454 static const struct omap_dss_device_ops hdmi_ops
= {
455 .connect
= hdmi_connect
,
456 .disconnect
= hdmi_disconnect
,
458 .enable
= hdmi_display_enable
,
459 .disable
= hdmi_display_disable
,
461 .set_timings
= hdmi_display_set_timings
,
463 .read_edid
= hdmi_read_edid
,
466 .set_infoframe
= hdmi_set_infoframe
,
467 .set_hdmi_mode
= hdmi_set_hdmi_mode
,
471 /* -----------------------------------------------------------------------------
475 static int hdmi_audio_startup(struct device
*dev
,
476 void (*abort_cb
)(struct device
*dev
))
478 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
480 mutex_lock(&hd
->lock
);
482 WARN_ON(hd
->audio_abort_cb
!= NULL
);
484 hd
->audio_abort_cb
= abort_cb
;
486 mutex_unlock(&hd
->lock
);
491 static int hdmi_audio_shutdown(struct device
*dev
)
493 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
495 mutex_lock(&hd
->lock
);
496 hd
->audio_abort_cb
= NULL
;
497 hd
->audio_configured
= false;
498 hd
->audio_playing
= false;
499 mutex_unlock(&hd
->lock
);
504 static int hdmi_audio_start(struct device
*dev
)
506 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
509 spin_lock_irqsave(&hd
->audio_playing_lock
, flags
);
511 if (hd
->display_enabled
) {
512 if (!hdmi_mode_has_audio(&hd
->cfg
))
513 DSSERR("%s: Video mode does not support audio\n",
515 hdmi_start_audio_stream(hd
);
517 hd
->audio_playing
= true;
519 spin_unlock_irqrestore(&hd
->audio_playing_lock
, flags
);
523 static void hdmi_audio_stop(struct device
*dev
)
525 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
528 if (!hdmi_mode_has_audio(&hd
->cfg
))
529 DSSERR("%s: Video mode does not support audio\n", __func__
);
531 spin_lock_irqsave(&hd
->audio_playing_lock
, flags
);
533 if (hd
->display_enabled
)
534 hdmi_stop_audio_stream(hd
);
535 hd
->audio_playing
= false;
537 spin_unlock_irqrestore(&hd
->audio_playing_lock
, flags
);
540 static int hdmi_audio_config(struct device
*dev
,
541 struct omap_dss_audio
*dss_audio
)
543 struct omap_hdmi
*hd
= dev_get_drvdata(dev
);
546 mutex_lock(&hd
->lock
);
548 if (hd
->display_enabled
) {
549 ret
= hdmi5_audio_config(&hd
->core
, &hd
->wp
, dss_audio
,
550 hd
->cfg
.vm
.pixelclock
);
555 hd
->audio_configured
= true;
556 hd
->audio_config
= *dss_audio
;
558 mutex_unlock(&hd
->lock
);
563 static const struct omap_hdmi_audio_ops hdmi_audio_ops
= {
564 .audio_startup
= hdmi_audio_startup
,
565 .audio_shutdown
= hdmi_audio_shutdown
,
566 .audio_start
= hdmi_audio_start
,
567 .audio_stop
= hdmi_audio_stop
,
568 .audio_config
= hdmi_audio_config
,
571 static int hdmi_audio_register(struct omap_hdmi
*hdmi
)
573 struct omap_hdmi_audio_pdata pdata
= {
574 .dev
= &hdmi
->pdev
->dev
,
576 .audio_dma_addr
= hdmi_wp_get_audio_dma_addr(&hdmi
->wp
),
577 .ops
= &hdmi_audio_ops
,
580 hdmi
->audio_pdev
= platform_device_register_data(
581 &hdmi
->pdev
->dev
, "omap-hdmi-audio", PLATFORM_DEVID_AUTO
,
582 &pdata
, sizeof(pdata
));
584 if (IS_ERR(hdmi
->audio_pdev
))
585 return PTR_ERR(hdmi
->audio_pdev
);
587 hdmi_runtime_get(hdmi
);
589 REG_GET(hdmi
->wp
.base
, HDMI_WP_SYSCONFIG
, 3, 2);
590 hdmi_runtime_put(hdmi
);
595 /* -----------------------------------------------------------------------------
596 * Component Bind & Unbind
599 static int hdmi5_bind(struct device
*dev
, struct device
*master
, void *data
)
601 struct dss_device
*dss
= dss_get_device(master
);
602 struct omap_hdmi
*hdmi
= dev_get_drvdata(dev
);
607 r
= hdmi_pll_init(dss
, hdmi
->pdev
, &hdmi
->pll
, &hdmi
->wp
);
611 r
= hdmi_audio_register(hdmi
);
613 DSSERR("Registering HDMI audio failed %d\n", r
);
617 hdmi
->debugfs
= dss_debugfs_create_file(dss
, "hdmi", hdmi_dump_regs
,
623 hdmi_pll_uninit(&hdmi
->pll
);
627 static void hdmi5_unbind(struct device
*dev
, struct device
*master
, void *data
)
629 struct omap_hdmi
*hdmi
= dev_get_drvdata(dev
);
631 dss_debugfs_remove_file(hdmi
->debugfs
);
633 if (hdmi
->audio_pdev
)
634 platform_device_unregister(hdmi
->audio_pdev
);
636 hdmi_pll_uninit(&hdmi
->pll
);
639 static const struct component_ops hdmi5_component_ops
= {
641 .unbind
= hdmi5_unbind
,
644 /* -----------------------------------------------------------------------------
645 * Probe & Remove, Suspend & Resume
648 static int hdmi5_init_output(struct omap_hdmi
*hdmi
)
650 struct omap_dss_device
*out
= &hdmi
->output
;
653 out
->dev
= &hdmi
->pdev
->dev
;
654 out
->id
= OMAP_DSS_OUTPUT_HDMI
;
655 out
->type
= OMAP_DISPLAY_TYPE_HDMI
;
656 out
->name
= "hdmi.0";
657 out
->dispc_channel
= OMAP_DSS_CHANNEL_DIGIT
;
658 out
->ops
= &hdmi_ops
;
659 out
->owner
= THIS_MODULE
;
660 out
->of_ports
= BIT(0);
661 out
->ops_flags
= OMAP_DSS_DEVICE_OP_EDID
;
663 r
= omapdss_device_init_output(out
);
667 omapdss_device_register(out
);
672 static void hdmi5_uninit_output(struct omap_hdmi
*hdmi
)
674 struct omap_dss_device
*out
= &hdmi
->output
;
676 omapdss_device_unregister(out
);
677 omapdss_device_cleanup_output(out
);
680 static int hdmi5_probe_of(struct omap_hdmi
*hdmi
)
682 struct platform_device
*pdev
= hdmi
->pdev
;
683 struct device_node
*node
= pdev
->dev
.of_node
;
684 struct device_node
*ep
;
687 ep
= of_graph_get_endpoint_by_regs(node
, 0, 0);
691 r
= hdmi_parse_lanes_of(pdev
, ep
, &hdmi
->phy
);
696 static int hdmi5_probe(struct platform_device
*pdev
)
698 struct omap_hdmi
*hdmi
;
702 hdmi
= kzalloc(sizeof(*hdmi
), GFP_KERNEL
);
708 dev_set_drvdata(&pdev
->dev
, hdmi
);
710 mutex_init(&hdmi
->lock
);
711 spin_lock_init(&hdmi
->audio_playing_lock
);
713 r
= hdmi5_probe_of(hdmi
);
717 r
= hdmi_wp_init(pdev
, &hdmi
->wp
, 5);
721 r
= hdmi_phy_init(pdev
, &hdmi
->phy
, 5);
725 r
= hdmi5_core_init(pdev
, &hdmi
->core
);
729 irq
= platform_get_irq(pdev
, 0);
731 DSSERR("platform_get_irq failed\n");
736 r
= devm_request_threaded_irq(&pdev
->dev
, irq
,
737 NULL
, hdmi_irq_handler
,
738 IRQF_ONESHOT
, "OMAP HDMI", hdmi
);
740 DSSERR("HDMI IRQ request failed\n");
744 hdmi
->vdda_reg
= devm_regulator_get(&pdev
->dev
, "vdda");
745 if (IS_ERR(hdmi
->vdda_reg
)) {
746 r
= PTR_ERR(hdmi
->vdda_reg
);
747 if (r
!= -EPROBE_DEFER
)
748 DSSERR("can't get VDDA regulator\n");
752 pm_runtime_enable(&pdev
->dev
);
754 r
= hdmi5_init_output(hdmi
);
758 r
= component_add(&pdev
->dev
, &hdmi5_component_ops
);
760 goto err_uninit_output
;
765 hdmi5_uninit_output(hdmi
);
767 pm_runtime_disable(&pdev
->dev
);
773 static int hdmi5_remove(struct platform_device
*pdev
)
775 struct omap_hdmi
*hdmi
= platform_get_drvdata(pdev
);
777 component_del(&pdev
->dev
, &hdmi5_component_ops
);
779 hdmi5_uninit_output(hdmi
);
781 pm_runtime_disable(&pdev
->dev
);
787 static const struct of_device_id hdmi_of_match
[] = {
788 { .compatible
= "ti,omap5-hdmi", },
789 { .compatible
= "ti,dra7-hdmi", },
793 struct platform_driver omapdss_hdmi5hw_driver
= {
794 .probe
= hdmi5_probe
,
795 .remove
= hdmi5_remove
,
797 .name
= "omapdss_hdmi5",
798 .of_match_table
= hdmi_of_match
,
799 .suppress_bind_attrs
= true,