2 * HDMI interface DSS driver for TI's OMAP4 family of SoCs.
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com/
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #define DSS_SUBSYS_NAME "HDMI"
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/err.h>
27 #include <linux/interrupt.h>
28 #include <linux/mutex.h>
29 #include <linux/delay.h>
30 #include <linux/string.h>
31 #include <linux/platform_device.h>
32 #include <linux/pm_runtime.h>
33 #include <linux/clk.h>
34 #include <linux/gpio.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/component.h>
38 #include <linux/of_graph.h>
39 #include <sound/omap-hdmi-audio.h>
40 #include <media/cec.h>
43 #include "hdmi4_core.h"
44 #include "hdmi4_cec.h"
48 static int hdmi_runtime_get(struct omap_hdmi *hdmi)
52 DSSDBG("hdmi_runtime_get\n");
54 r = pm_runtime_get_sync(&hdmi->pdev->dev);
62 static void hdmi_runtime_put(struct omap_hdmi *hdmi)
66 DSSDBG("hdmi_runtime_put\n");
68 r = pm_runtime_put_sync(&hdmi->pdev->dev);
69 WARN_ON(r < 0 && r != -ENOSYS);
72 static irqreturn_t hdmi_irq_handler(int irq, void *data)
74 struct omap_hdmi *hdmi = data;
75 struct hdmi_wp_data *wp = &hdmi->wp;
78 irqstatus = hdmi_wp_get_irqstatus(wp);
79 hdmi_wp_set_irqstatus(wp, irqstatus);
81 if ((irqstatus & HDMI_IRQ_LINK_CONNECT) &&
82 irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
84 * If we get both connect and disconnect interrupts at the same
85 * time, turn off the PHY, clear interrupts, and restart, which
86 * raises connect interrupt if a cable is connected, or nothing
87 * if cable is not connected.
89 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_OFF);
91 hdmi_wp_set_irqstatus(wp, HDMI_IRQ_LINK_CONNECT |
92 HDMI_IRQ_LINK_DISCONNECT);
94 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
95 } else if (irqstatus & HDMI_IRQ_LINK_CONNECT) {
96 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_TXON);
97 } else if (irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
98 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
100 if (irqstatus & HDMI_IRQ_CORE) {
101 u32 intr4 = hdmi_read_reg(hdmi->core.base, HDMI_CORE_SYS_INTR4);
103 hdmi_write_reg(hdmi->core.base, HDMI_CORE_SYS_INTR4, intr4);
105 hdmi4_cec_irq(&hdmi->core);
111 static int hdmi_init_regulator(struct omap_hdmi *hdmi)
113 struct regulator *reg;
115 if (hdmi->vdda_reg != NULL)
118 reg = devm_regulator_get(&hdmi->pdev->dev, "vdda");
121 if (PTR_ERR(reg) != -EPROBE_DEFER)
122 DSSERR("can't get VDDA regulator\n");
126 hdmi->vdda_reg = reg;
131 static int hdmi_power_on_core(struct omap_hdmi *hdmi)
135 if (hdmi->core.core_pwr_cnt++)
138 r = regulator_enable(hdmi->vdda_reg);
142 r = hdmi_runtime_get(hdmi);
144 goto err_runtime_get;
146 hdmi4_core_powerdown_disable(&hdmi->core);
148 /* Make selection of HDMI in DSS */
149 dss_select_hdmi_venc_clk_source(hdmi->dss, DSS_HDMI_M_PCLK);
151 hdmi->core_enabled = true;
156 regulator_disable(hdmi->vdda_reg);
158 hdmi->core.core_pwr_cnt--;
163 static void hdmi_power_off_core(struct omap_hdmi *hdmi)
165 if (--hdmi->core.core_pwr_cnt)
168 hdmi->core_enabled = false;
170 hdmi_runtime_put(hdmi);
171 regulator_disable(hdmi->vdda_reg);
174 static int hdmi_power_on_full(struct omap_hdmi *hdmi)
177 struct videomode *vm;
178 struct hdmi_wp_data *wp = &hdmi->wp;
179 struct dss_pll_clock_info hdmi_cinfo = { 0 };
182 r = hdmi_power_on_core(hdmi);
186 /* disable and clear irqs */
187 hdmi_wp_clear_irqenable(wp, ~HDMI_IRQ_CORE);
188 hdmi_wp_set_irqstatus(wp, ~HDMI_IRQ_CORE);
192 DSSDBG("hdmi_power_on hactive= %d vactive = %d\n", vm->hactive,
196 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
199 /* DSS_HDMI_TCLK is bitclk / 10 */
202 dss_pll_calc_b(&hdmi->pll.pll, clk_get_rate(hdmi->pll.pll.clkin),
205 r = dss_pll_enable(&hdmi->pll.pll);
207 DSSERR("Failed to enable PLL\n");
211 r = dss_pll_set_config(&hdmi->pll.pll, &hdmi_cinfo);
213 DSSERR("Failed to configure PLL\n");
217 r = hdmi_phy_configure(&hdmi->phy, hdmi_cinfo.clkdco,
218 hdmi_cinfo.clkout[0]);
220 DSSDBG("Failed to configure PHY\n");
224 r = hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
228 hdmi4_configure(&hdmi->core, &hdmi->wp, &hdmi->cfg);
231 dss_mgr_set_timings(&hdmi->output, vm);
233 r = dss_mgr_enable(&hdmi->output);
237 r = hdmi_wp_video_start(&hdmi->wp);
241 hdmi_wp_set_irqenable(wp,
242 HDMI_IRQ_LINK_CONNECT | HDMI_IRQ_LINK_DISCONNECT);
247 dss_mgr_disable(&hdmi->output);
249 hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_OFF);
253 dss_pll_disable(&hdmi->pll.pll);
255 hdmi_power_off_core(hdmi);
259 static void hdmi_power_off_full(struct omap_hdmi *hdmi)
261 hdmi_wp_clear_irqenable(&hdmi->wp, ~HDMI_IRQ_CORE);
263 hdmi_wp_video_stop(&hdmi->wp);
265 dss_mgr_disable(&hdmi->output);
267 hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_OFF);
269 dss_pll_disable(&hdmi->pll.pll);
271 hdmi_power_off_core(hdmi);
274 static int hdmi_display_check_timing(struct omap_dss_device *dssdev,
275 struct videomode *vm)
277 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
279 if (!dispc_mgr_timings_ok(hdmi->dss->dispc, dssdev->dispc_channel, vm))
285 static void hdmi_display_set_timing(struct omap_dss_device *dssdev,
286 struct videomode *vm)
288 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
290 mutex_lock(&hdmi->lock);
294 dispc_set_tv_pclk(hdmi->dss->dispc, vm->pixelclock);
296 mutex_unlock(&hdmi->lock);
299 static void hdmi_display_get_timings(struct omap_dss_device *dssdev,
300 struct videomode *vm)
302 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
307 static int hdmi_dump_regs(struct seq_file *s, void *p)
309 struct omap_hdmi *hdmi = s->private;
311 mutex_lock(&hdmi->lock);
313 if (hdmi_runtime_get(hdmi)) {
314 mutex_unlock(&hdmi->lock);
318 hdmi_wp_dump(&hdmi->wp, s);
319 hdmi_pll_dump(&hdmi->pll, s);
320 hdmi_phy_dump(&hdmi->phy, s);
321 hdmi4_core_dump(&hdmi->core, s);
323 hdmi_runtime_put(hdmi);
324 mutex_unlock(&hdmi->lock);
328 static int read_edid(struct omap_hdmi *hdmi, u8 *buf, int len)
332 mutex_lock(&hdmi->lock);
334 r = hdmi_runtime_get(hdmi);
337 r = hdmi4_read_edid(&hdmi->core, buf, len);
339 hdmi_runtime_put(hdmi);
340 mutex_unlock(&hdmi->lock);
345 static void hdmi_start_audio_stream(struct omap_hdmi *hd)
347 hdmi_wp_audio_enable(&hd->wp, true);
348 hdmi4_audio_start(&hd->core, &hd->wp);
351 static void hdmi_stop_audio_stream(struct omap_hdmi *hd)
353 hdmi4_audio_stop(&hd->core, &hd->wp);
354 hdmi_wp_audio_enable(&hd->wp, false);
357 static int hdmi_display_enable(struct omap_dss_device *dssdev)
359 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
363 DSSDBG("ENTER hdmi_display_enable\n");
365 mutex_lock(&hdmi->lock);
367 if (!dssdev->dispc_channel_connected) {
368 DSSERR("failed to enable display: no output/manager\n");
373 r = hdmi_power_on_full(hdmi);
375 DSSERR("failed to power on device\n");
379 if (hdmi->audio_configured) {
380 r = hdmi4_audio_config(&hdmi->core, &hdmi->wp,
382 hdmi->cfg.vm.pixelclock);
384 DSSERR("Error restoring audio configuration: %d", r);
385 hdmi->audio_abort_cb(&hdmi->pdev->dev);
386 hdmi->audio_configured = false;
390 spin_lock_irqsave(&hdmi->audio_playing_lock, flags);
391 if (hdmi->audio_configured && hdmi->audio_playing)
392 hdmi_start_audio_stream(hdmi);
393 hdmi->display_enabled = true;
394 spin_unlock_irqrestore(&hdmi->audio_playing_lock, flags);
396 mutex_unlock(&hdmi->lock);
400 mutex_unlock(&hdmi->lock);
404 static void hdmi_display_disable(struct omap_dss_device *dssdev)
406 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
409 DSSDBG("Enter hdmi_display_disable\n");
411 mutex_lock(&hdmi->lock);
413 spin_lock_irqsave(&hdmi->audio_playing_lock, flags);
414 hdmi_stop_audio_stream(hdmi);
415 hdmi->display_enabled = false;
416 spin_unlock_irqrestore(&hdmi->audio_playing_lock, flags);
418 hdmi_power_off_full(hdmi);
420 mutex_unlock(&hdmi->lock);
423 int hdmi4_core_enable(struct hdmi_core_data *core)
425 struct omap_hdmi *hdmi = container_of(core, struct omap_hdmi, core);
428 DSSDBG("ENTER omapdss_hdmi4_core_enable\n");
430 mutex_lock(&hdmi->lock);
432 r = hdmi_power_on_core(hdmi);
434 DSSERR("failed to power on device\n");
438 mutex_unlock(&hdmi->lock);
442 mutex_unlock(&hdmi->lock);
446 void hdmi4_core_disable(struct hdmi_core_data *core)
448 struct omap_hdmi *hdmi = container_of(core, struct omap_hdmi, core);
450 DSSDBG("Enter omapdss_hdmi4_core_disable\n");
452 mutex_lock(&hdmi->lock);
454 hdmi_power_off_core(hdmi);
456 mutex_unlock(&hdmi->lock);
459 static int hdmi_connect(struct omap_dss_device *dssdev,
460 struct omap_dss_device *dst)
462 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
465 r = hdmi_init_regulator(hdmi);
469 r = dss_mgr_connect(&hdmi->output, dssdev);
473 r = omapdss_output_set_device(dssdev, dst);
475 DSSERR("failed to connect output to new device: %s\n",
477 dss_mgr_disconnect(&hdmi->output, dssdev);
484 static void hdmi_disconnect(struct omap_dss_device *dssdev,
485 struct omap_dss_device *dst)
487 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
489 WARN_ON(dst != dssdev->dst);
491 if (dst != dssdev->dst)
494 omapdss_output_unset_device(dssdev);
496 dss_mgr_disconnect(&hdmi->output, dssdev);
499 static int hdmi_read_edid(struct omap_dss_device *dssdev,
502 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
506 need_enable = hdmi->core_enabled == false;
509 r = hdmi4_core_enable(&hdmi->core);
514 r = read_edid(hdmi, edid, len);
516 hdmi4_cec_set_phys_addr(&hdmi->core,
517 cec_get_edid_phys_addr(edid, r, NULL));
519 hdmi4_cec_set_phys_addr(&hdmi->core, CEC_PHYS_ADDR_INVALID);
521 hdmi4_core_disable(&hdmi->core);
526 static void hdmi_lost_hotplug(struct omap_dss_device *dssdev)
528 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
530 hdmi4_cec_set_phys_addr(&hdmi->core, CEC_PHYS_ADDR_INVALID);
533 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
534 const struct hdmi_avi_infoframe *avi)
536 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
538 hdmi->cfg.infoframe = *avi;
542 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
545 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
547 hdmi->cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
551 static const struct omapdss_hdmi_ops hdmi_ops = {
552 .connect = hdmi_connect,
553 .disconnect = hdmi_disconnect,
555 .enable = hdmi_display_enable,
556 .disable = hdmi_display_disable,
558 .check_timings = hdmi_display_check_timing,
559 .set_timings = hdmi_display_set_timing,
560 .get_timings = hdmi_display_get_timings,
562 .read_edid = hdmi_read_edid,
563 .lost_hotplug = hdmi_lost_hotplug,
564 .set_infoframe = hdmi_set_infoframe,
565 .set_hdmi_mode = hdmi_set_hdmi_mode,
568 static void hdmi_init_output(struct omap_hdmi *hdmi)
570 struct omap_dss_device *out = &hdmi->output;
572 out->dev = &hdmi->pdev->dev;
573 out->id = OMAP_DSS_OUTPUT_HDMI;
574 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
575 out->name = "hdmi.0";
576 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
577 out->ops.hdmi = &hdmi_ops;
578 out->owner = THIS_MODULE;
580 omapdss_register_output(out);
583 static void hdmi_uninit_output(struct omap_hdmi *hdmi)
585 struct omap_dss_device *out = &hdmi->output;
587 omapdss_unregister_output(out);
590 static int hdmi_probe_of(struct omap_hdmi *hdmi)
592 struct platform_device *pdev = hdmi->pdev;
593 struct device_node *node = pdev->dev.of_node;
594 struct device_node *ep;
597 ep = of_graph_get_endpoint_by_regs(node, 0, 0);
601 r = hdmi_parse_lanes_of(pdev, ep, &hdmi->phy);
613 /* Audio callbacks */
614 static int hdmi_audio_startup(struct device *dev,
615 void (*abort_cb)(struct device *dev))
617 struct omap_hdmi *hd = dev_get_drvdata(dev);
619 mutex_lock(&hd->lock);
621 WARN_ON(hd->audio_abort_cb != NULL);
623 hd->audio_abort_cb = abort_cb;
625 mutex_unlock(&hd->lock);
630 static int hdmi_audio_shutdown(struct device *dev)
632 struct omap_hdmi *hd = dev_get_drvdata(dev);
634 mutex_lock(&hd->lock);
635 hd->audio_abort_cb = NULL;
636 hd->audio_configured = false;
637 hd->audio_playing = false;
638 mutex_unlock(&hd->lock);
643 static int hdmi_audio_start(struct device *dev)
645 struct omap_hdmi *hd = dev_get_drvdata(dev);
648 spin_lock_irqsave(&hd->audio_playing_lock, flags);
650 if (hd->display_enabled) {
651 if (!hdmi_mode_has_audio(&hd->cfg))
652 DSSERR("%s: Video mode does not support audio\n",
654 hdmi_start_audio_stream(hd);
656 hd->audio_playing = true;
658 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
662 static void hdmi_audio_stop(struct device *dev)
664 struct omap_hdmi *hd = dev_get_drvdata(dev);
667 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
669 spin_lock_irqsave(&hd->audio_playing_lock, flags);
671 if (hd->display_enabled)
672 hdmi_stop_audio_stream(hd);
673 hd->audio_playing = false;
675 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
678 static int hdmi_audio_config(struct device *dev,
679 struct omap_dss_audio *dss_audio)
681 struct omap_hdmi *hd = dev_get_drvdata(dev);
684 mutex_lock(&hd->lock);
686 if (hd->display_enabled) {
687 ret = hdmi4_audio_config(&hd->core, &hd->wp, dss_audio,
688 hd->cfg.vm.pixelclock);
693 hd->audio_configured = true;
694 hd->audio_config = *dss_audio;
696 mutex_unlock(&hd->lock);
701 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
702 .audio_startup = hdmi_audio_startup,
703 .audio_shutdown = hdmi_audio_shutdown,
704 .audio_start = hdmi_audio_start,
705 .audio_stop = hdmi_audio_stop,
706 .audio_config = hdmi_audio_config,
709 static int hdmi_audio_register(struct omap_hdmi *hdmi)
711 struct omap_hdmi_audio_pdata pdata = {
712 .dev = &hdmi->pdev->dev,
714 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi->wp),
715 .ops = &hdmi_audio_ops,
718 hdmi->audio_pdev = platform_device_register_data(
719 &hdmi->pdev->dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
720 &pdata, sizeof(pdata));
722 if (IS_ERR(hdmi->audio_pdev))
723 return PTR_ERR(hdmi->audio_pdev);
728 /* HDMI HW IP initialisation */
729 static int hdmi4_bind(struct device *dev, struct device *master, void *data)
731 struct platform_device *pdev = to_platform_device(dev);
732 struct dss_device *dss = dss_get_device(master);
733 struct omap_hdmi *hdmi;
737 hdmi = kzalloc(sizeof(*hdmi), GFP_KERNEL);
743 dev_set_drvdata(&pdev->dev, hdmi);
745 mutex_init(&hdmi->lock);
746 spin_lock_init(&hdmi->audio_playing_lock);
748 r = hdmi_probe_of(hdmi);
752 r = hdmi_wp_init(pdev, &hdmi->wp, 4);
756 r = hdmi_pll_init(dss, pdev, &hdmi->pll, &hdmi->wp);
760 r = hdmi_phy_init(pdev, &hdmi->phy, 4);
764 r = hdmi4_core_init(pdev, &hdmi->core);
768 r = hdmi4_cec_init(pdev, &hdmi->core, &hdmi->wp);
772 irq = platform_get_irq(pdev, 0);
774 DSSERR("platform_get_irq failed\n");
779 r = devm_request_threaded_irq(&pdev->dev, irq,
780 NULL, hdmi_irq_handler,
781 IRQF_ONESHOT, "OMAP HDMI", hdmi);
783 DSSERR("HDMI IRQ request failed\n");
787 pm_runtime_enable(&pdev->dev);
789 hdmi_init_output(hdmi);
791 r = hdmi_audio_register(hdmi);
793 DSSERR("Registering HDMI audio failed\n");
794 hdmi_uninit_output(hdmi);
795 pm_runtime_disable(&pdev->dev);
799 hdmi->debugfs = dss_debugfs_create_file(dss, "hdmi", hdmi_dump_regs,
805 hdmi_pll_uninit(&hdmi->pll);
811 static void hdmi4_unbind(struct device *dev, struct device *master, void *data)
813 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
815 dss_debugfs_remove_file(hdmi->debugfs);
817 if (hdmi->audio_pdev)
818 platform_device_unregister(hdmi->audio_pdev);
820 hdmi_uninit_output(hdmi);
822 hdmi4_cec_uninit(&hdmi->core);
824 hdmi_pll_uninit(&hdmi->pll);
826 pm_runtime_disable(dev);
831 static const struct component_ops hdmi4_component_ops = {
833 .unbind = hdmi4_unbind,
836 static int hdmi4_probe(struct platform_device *pdev)
838 return component_add(&pdev->dev, &hdmi4_component_ops);
841 static int hdmi4_remove(struct platform_device *pdev)
843 component_del(&pdev->dev, &hdmi4_component_ops);
847 static int hdmi_runtime_suspend(struct device *dev)
849 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
851 dispc_runtime_put(hdmi->dss->dispc);
856 static int hdmi_runtime_resume(struct device *dev)
858 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
861 r = dispc_runtime_get(hdmi->dss->dispc);
868 static const struct dev_pm_ops hdmi_pm_ops = {
869 .runtime_suspend = hdmi_runtime_suspend,
870 .runtime_resume = hdmi_runtime_resume,
873 static const struct of_device_id hdmi_of_match[] = {
874 { .compatible = "ti,omap4-hdmi", },
878 struct platform_driver omapdss_hdmi4hw_driver = {
879 .probe = hdmi4_probe,
880 .remove = hdmi4_remove,
882 .name = "omapdss_hdmi",
884 .of_match_table = hdmi_of_match,
885 .suppress_bind_attrs = true,