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_power_on_core(struct omap_hdmi *hdmi)
115 if (hdmi->core.core_pwr_cnt++)
118 r = regulator_enable(hdmi->vdda_reg);
122 r = hdmi_runtime_get(hdmi);
124 goto err_runtime_get;
126 hdmi4_core_powerdown_disable(&hdmi->core);
128 /* Make selection of HDMI in DSS */
129 dss_select_hdmi_venc_clk_source(hdmi->dss, DSS_HDMI_M_PCLK);
131 hdmi->core_enabled = true;
136 regulator_disable(hdmi->vdda_reg);
138 hdmi->core.core_pwr_cnt--;
143 static void hdmi_power_off_core(struct omap_hdmi *hdmi)
145 if (--hdmi->core.core_pwr_cnt)
148 hdmi->core_enabled = false;
150 hdmi_runtime_put(hdmi);
151 regulator_disable(hdmi->vdda_reg);
154 static int hdmi_power_on_full(struct omap_hdmi *hdmi)
157 const struct videomode *vm;
158 struct hdmi_wp_data *wp = &hdmi->wp;
159 struct dss_pll_clock_info hdmi_cinfo = { 0 };
162 r = hdmi_power_on_core(hdmi);
166 /* disable and clear irqs */
167 hdmi_wp_clear_irqenable(wp, ~HDMI_IRQ_CORE);
168 hdmi_wp_set_irqstatus(wp, ~HDMI_IRQ_CORE);
172 DSSDBG("hdmi_power_on hactive= %d vactive = %d\n", vm->hactive,
176 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
179 /* DSS_HDMI_TCLK is bitclk / 10 */
182 dss_pll_calc_b(&hdmi->pll.pll, clk_get_rate(hdmi->pll.pll.clkin),
185 r = dss_pll_enable(&hdmi->pll.pll);
187 DSSERR("Failed to enable PLL\n");
191 r = dss_pll_set_config(&hdmi->pll.pll, &hdmi_cinfo);
193 DSSERR("Failed to configure PLL\n");
197 r = hdmi_phy_configure(&hdmi->phy, hdmi_cinfo.clkdco,
198 hdmi_cinfo.clkout[0]);
200 DSSDBG("Failed to configure PHY\n");
204 r = hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
208 hdmi4_configure(&hdmi->core, &hdmi->wp, &hdmi->cfg);
210 r = dss_mgr_enable(&hdmi->output);
214 r = hdmi_wp_video_start(&hdmi->wp);
218 hdmi_wp_set_irqenable(wp,
219 HDMI_IRQ_LINK_CONNECT | HDMI_IRQ_LINK_DISCONNECT);
224 dss_mgr_disable(&hdmi->output);
226 hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_OFF);
230 dss_pll_disable(&hdmi->pll.pll);
232 hdmi_power_off_core(hdmi);
236 static void hdmi_power_off_full(struct omap_hdmi *hdmi)
238 hdmi_wp_clear_irqenable(&hdmi->wp, ~HDMI_IRQ_CORE);
240 hdmi_wp_video_stop(&hdmi->wp);
242 dss_mgr_disable(&hdmi->output);
244 hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_OFF);
246 dss_pll_disable(&hdmi->pll.pll);
248 hdmi_power_off_core(hdmi);
251 static void hdmi_display_set_timings(struct omap_dss_device *dssdev,
252 const struct videomode *vm)
254 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
256 mutex_lock(&hdmi->lock);
260 dispc_set_tv_pclk(hdmi->dss->dispc, vm->pixelclock);
262 mutex_unlock(&hdmi->lock);
265 static int hdmi_dump_regs(struct seq_file *s, void *p)
267 struct omap_hdmi *hdmi = s->private;
269 mutex_lock(&hdmi->lock);
271 if (hdmi_runtime_get(hdmi)) {
272 mutex_unlock(&hdmi->lock);
276 hdmi_wp_dump(&hdmi->wp, s);
277 hdmi_pll_dump(&hdmi->pll, s);
278 hdmi_phy_dump(&hdmi->phy, s);
279 hdmi4_core_dump(&hdmi->core, s);
281 hdmi_runtime_put(hdmi);
282 mutex_unlock(&hdmi->lock);
286 static int read_edid(struct omap_hdmi *hdmi, u8 *buf, int len)
290 mutex_lock(&hdmi->lock);
292 r = hdmi_runtime_get(hdmi);
295 r = hdmi4_read_edid(&hdmi->core, buf, len);
297 hdmi_runtime_put(hdmi);
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_hdmi *hdmi = dssdev_to_hdmi(dssdev);
321 DSSDBG("ENTER hdmi_display_enable\n");
323 mutex_lock(&hdmi->lock);
325 if (!dssdev->dispc_channel_connected) {
326 DSSERR("failed to enable display: no output/manager\n");
331 r = hdmi_power_on_full(hdmi);
333 DSSERR("failed to power on device\n");
337 if (hdmi->audio_configured) {
338 r = hdmi4_audio_config(&hdmi->core, &hdmi->wp,
340 hdmi->cfg.vm.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)
364 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
367 DSSDBG("Enter hdmi_display_disable\n");
369 mutex_lock(&hdmi->lock);
371 spin_lock_irqsave(&hdmi->audio_playing_lock, flags);
372 hdmi_stop_audio_stream(hdmi);
373 hdmi->display_enabled = false;
374 spin_unlock_irqrestore(&hdmi->audio_playing_lock, flags);
376 hdmi_power_off_full(hdmi);
378 mutex_unlock(&hdmi->lock);
381 int hdmi4_core_enable(struct hdmi_core_data *core)
383 struct omap_hdmi *hdmi = container_of(core, struct omap_hdmi, core);
386 DSSDBG("ENTER omapdss_hdmi4_core_enable\n");
388 mutex_lock(&hdmi->lock);
390 r = hdmi_power_on_core(hdmi);
392 DSSERR("failed to power on device\n");
396 mutex_unlock(&hdmi->lock);
400 mutex_unlock(&hdmi->lock);
404 void hdmi4_core_disable(struct hdmi_core_data *core)
406 struct omap_hdmi *hdmi = container_of(core, struct omap_hdmi, core);
408 DSSDBG("Enter omapdss_hdmi4_core_disable\n");
410 mutex_lock(&hdmi->lock);
412 hdmi_power_off_core(hdmi);
414 mutex_unlock(&hdmi->lock);
417 static int hdmi_connect(struct omap_dss_device *src,
418 struct omap_dss_device *dst)
422 r = omapdss_device_connect(dst->dss, dst, dst->next);
426 dst->dispc_channel_connected = true;
430 static void hdmi_disconnect(struct omap_dss_device *src,
431 struct omap_dss_device *dst)
433 dst->dispc_channel_connected = false;
435 omapdss_device_disconnect(dst, dst->next);
438 static int hdmi_read_edid(struct omap_dss_device *dssdev,
441 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
445 need_enable = hdmi->core_enabled == false;
448 r = hdmi4_core_enable(&hdmi->core);
453 r = read_edid(hdmi, edid, len);
455 hdmi4_cec_set_phys_addr(&hdmi->core,
456 cec_get_edid_phys_addr(edid, r, NULL));
458 hdmi4_cec_set_phys_addr(&hdmi->core, CEC_PHYS_ADDR_INVALID);
460 hdmi4_core_disable(&hdmi->core);
465 static void hdmi_lost_hotplug(struct omap_dss_device *dssdev)
467 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
469 hdmi4_cec_set_phys_addr(&hdmi->core, CEC_PHYS_ADDR_INVALID);
472 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
473 const struct hdmi_avi_infoframe *avi)
475 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
477 hdmi->cfg.infoframe = *avi;
481 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
484 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
486 hdmi->cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
490 static const struct omap_dss_device_ops hdmi_ops = {
491 .connect = hdmi_connect,
492 .disconnect = hdmi_disconnect,
494 .enable = hdmi_display_enable,
495 .disable = hdmi_display_disable,
497 .set_timings = hdmi_display_set_timings,
499 .read_edid = hdmi_read_edid,
502 .lost_hotplug = hdmi_lost_hotplug,
503 .set_infoframe = hdmi_set_infoframe,
504 .set_hdmi_mode = hdmi_set_hdmi_mode,
508 /* -----------------------------------------------------------------------------
512 static int hdmi_audio_startup(struct device *dev,
513 void (*abort_cb)(struct device *dev))
515 struct omap_hdmi *hd = dev_get_drvdata(dev);
517 mutex_lock(&hd->lock);
519 WARN_ON(hd->audio_abort_cb != NULL);
521 hd->audio_abort_cb = abort_cb;
523 mutex_unlock(&hd->lock);
528 static int hdmi_audio_shutdown(struct device *dev)
530 struct omap_hdmi *hd = dev_get_drvdata(dev);
532 mutex_lock(&hd->lock);
533 hd->audio_abort_cb = NULL;
534 hd->audio_configured = false;
535 hd->audio_playing = false;
536 mutex_unlock(&hd->lock);
541 static int hdmi_audio_start(struct device *dev)
543 struct omap_hdmi *hd = dev_get_drvdata(dev);
546 spin_lock_irqsave(&hd->audio_playing_lock, flags);
548 if (hd->display_enabled) {
549 if (!hdmi_mode_has_audio(&hd->cfg))
550 DSSERR("%s: Video mode does not support audio\n",
552 hdmi_start_audio_stream(hd);
554 hd->audio_playing = true;
556 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
560 static void hdmi_audio_stop(struct device *dev)
562 struct omap_hdmi *hd = dev_get_drvdata(dev);
565 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
567 spin_lock_irqsave(&hd->audio_playing_lock, flags);
569 if (hd->display_enabled)
570 hdmi_stop_audio_stream(hd);
571 hd->audio_playing = false;
573 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
576 static int hdmi_audio_config(struct device *dev,
577 struct omap_dss_audio *dss_audio)
579 struct omap_hdmi *hd = dev_get_drvdata(dev);
582 mutex_lock(&hd->lock);
584 if (hd->display_enabled) {
585 ret = hdmi4_audio_config(&hd->core, &hd->wp, dss_audio,
586 hd->cfg.vm.pixelclock);
591 hd->audio_configured = true;
592 hd->audio_config = *dss_audio;
594 mutex_unlock(&hd->lock);
599 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
600 .audio_startup = hdmi_audio_startup,
601 .audio_shutdown = hdmi_audio_shutdown,
602 .audio_start = hdmi_audio_start,
603 .audio_stop = hdmi_audio_stop,
604 .audio_config = hdmi_audio_config,
607 static int hdmi_audio_register(struct omap_hdmi *hdmi)
609 struct omap_hdmi_audio_pdata pdata = {
610 .dev = &hdmi->pdev->dev,
612 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi->wp),
613 .ops = &hdmi_audio_ops,
616 hdmi->audio_pdev = platform_device_register_data(
617 &hdmi->pdev->dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
618 &pdata, sizeof(pdata));
620 if (IS_ERR(hdmi->audio_pdev))
621 return PTR_ERR(hdmi->audio_pdev);
626 /* -----------------------------------------------------------------------------
627 * Component Bind & Unbind
630 static int hdmi4_bind(struct device *dev, struct device *master, void *data)
632 struct dss_device *dss = dss_get_device(master);
633 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
638 r = hdmi_pll_init(dss, hdmi->pdev, &hdmi->pll, &hdmi->wp);
642 r = hdmi4_cec_init(hdmi->pdev, &hdmi->core, &hdmi->wp);
646 r = hdmi_audio_register(hdmi);
648 DSSERR("Registering HDMI audio failed\n");
652 hdmi->debugfs = dss_debugfs_create_file(dss, "hdmi", hdmi_dump_regs,
658 hdmi4_cec_uninit(&hdmi->core);
660 hdmi_pll_uninit(&hdmi->pll);
664 static void hdmi4_unbind(struct device *dev, struct device *master, void *data)
666 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
668 dss_debugfs_remove_file(hdmi->debugfs);
670 if (hdmi->audio_pdev)
671 platform_device_unregister(hdmi->audio_pdev);
673 hdmi4_cec_uninit(&hdmi->core);
674 hdmi_pll_uninit(&hdmi->pll);
677 static const struct component_ops hdmi4_component_ops = {
679 .unbind = hdmi4_unbind,
682 /* -----------------------------------------------------------------------------
683 * Probe & Remove, Suspend & Resume
686 static int hdmi4_init_output(struct omap_hdmi *hdmi)
688 struct omap_dss_device *out = &hdmi->output;
691 out->dev = &hdmi->pdev->dev;
692 out->id = OMAP_DSS_OUTPUT_HDMI;
693 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
694 out->name = "hdmi.0";
695 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
696 out->ops = &hdmi_ops;
697 out->owner = THIS_MODULE;
698 out->of_ports = BIT(0);
699 out->ops_flags = OMAP_DSS_DEVICE_OP_EDID;
701 out->next = omapdss_of_find_connected_device(out->dev->of_node, 0);
702 if (IS_ERR(out->next)) {
703 if (PTR_ERR(out->next) != -EPROBE_DEFER)
704 dev_err(out->dev, "failed to find video sink\n");
705 return PTR_ERR(out->next);
708 r = omapdss_output_validate(out);
710 omapdss_device_put(out->next);
715 omapdss_device_register(out);
720 static void hdmi4_uninit_output(struct omap_hdmi *hdmi)
722 struct omap_dss_device *out = &hdmi->output;
725 omapdss_device_put(out->next);
726 omapdss_device_unregister(out);
729 static int hdmi4_probe_of(struct omap_hdmi *hdmi)
731 struct platform_device *pdev = hdmi->pdev;
732 struct device_node *node = pdev->dev.of_node;
733 struct device_node *ep;
736 ep = of_graph_get_endpoint_by_regs(node, 0, 0);
740 r = hdmi_parse_lanes_of(pdev, ep, &hdmi->phy);
745 static int hdmi4_probe(struct platform_device *pdev)
747 struct omap_hdmi *hdmi;
751 hdmi = kzalloc(sizeof(*hdmi), GFP_KERNEL);
757 dev_set_drvdata(&pdev->dev, hdmi);
759 mutex_init(&hdmi->lock);
760 spin_lock_init(&hdmi->audio_playing_lock);
762 r = hdmi4_probe_of(hdmi);
766 r = hdmi_wp_init(pdev, &hdmi->wp, 4);
770 r = hdmi_phy_init(pdev, &hdmi->phy, 4);
774 r = hdmi4_core_init(pdev, &hdmi->core);
778 irq = platform_get_irq(pdev, 0);
780 DSSERR("platform_get_irq failed\n");
785 r = devm_request_threaded_irq(&pdev->dev, irq,
786 NULL, hdmi_irq_handler,
787 IRQF_ONESHOT, "OMAP HDMI", hdmi);
789 DSSERR("HDMI IRQ request failed\n");
793 hdmi->vdda_reg = devm_regulator_get(&pdev->dev, "vdda");
794 if (IS_ERR(hdmi->vdda_reg)) {
795 r = PTR_ERR(hdmi->vdda_reg);
796 if (r != -EPROBE_DEFER)
797 DSSERR("can't get VDDA regulator\n");
801 pm_runtime_enable(&pdev->dev);
803 r = hdmi4_init_output(hdmi);
807 r = component_add(&pdev->dev, &hdmi4_component_ops);
809 goto err_uninit_output;
814 hdmi4_uninit_output(hdmi);
816 pm_runtime_disable(&pdev->dev);
822 static int hdmi4_remove(struct platform_device *pdev)
824 struct omap_hdmi *hdmi = platform_get_drvdata(pdev);
826 component_del(&pdev->dev, &hdmi4_component_ops);
828 hdmi4_uninit_output(hdmi);
830 pm_runtime_disable(&pdev->dev);
836 static int hdmi_runtime_suspend(struct device *dev)
838 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
840 dispc_runtime_put(hdmi->dss->dispc);
845 static int hdmi_runtime_resume(struct device *dev)
847 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
850 r = dispc_runtime_get(hdmi->dss->dispc);
857 static const struct dev_pm_ops hdmi_pm_ops = {
858 .runtime_suspend = hdmi_runtime_suspend,
859 .runtime_resume = hdmi_runtime_resume,
862 static const struct of_device_id hdmi_of_match[] = {
863 { .compatible = "ti,omap4-hdmi", },
867 struct platform_driver omapdss_hdmi4hw_driver = {
868 .probe = hdmi4_probe,
869 .remove = hdmi4_remove,
871 .name = "omapdss_hdmi",
873 .of_match_table = hdmi_of_match,
874 .suppress_bind_attrs = true,