2 * HDMI driver for OMAP5
4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/
9 * Archit Taneja <archit@ti.com>
10 * Tomi Valkeinen <tomi.valkeinen@ti.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 * You should have received a copy of the GNU General Public License along with
22 * this program. If not, see <http://www.gnu.org/licenses/>.
25 #define DSS_SUBSYS_NAME "HDMI"
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/err.h>
31 #include <linux/interrupt.h>
32 #include <linux/mutex.h>
33 #include <linux/delay.h>
34 #include <linux/string.h>
35 #include <linux/platform_device.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/clk.h>
38 #include <linux/gpio.h>
39 #include <linux/regulator/consumer.h>
40 #include <linux/component.h>
42 #include <linux/of_graph.h>
43 #include <sound/omap-hdmi-audio.h>
46 #include "hdmi5_core.h"
49 static int hdmi_runtime_get(struct omap_hdmi *hdmi)
53 DSSDBG("hdmi_runtime_get\n");
55 r = pm_runtime_get_sync(&hdmi->pdev->dev);
63 static void hdmi_runtime_put(struct omap_hdmi *hdmi)
67 DSSDBG("hdmi_runtime_put\n");
69 r = pm_runtime_put_sync(&hdmi->pdev->dev);
70 WARN_ON(r < 0 && r != -ENOSYS);
73 static irqreturn_t hdmi_irq_handler(int irq, void *data)
75 struct omap_hdmi *hdmi = data;
76 struct hdmi_wp_data *wp = &hdmi->wp;
79 irqstatus = hdmi_wp_get_irqstatus(wp);
80 hdmi_wp_set_irqstatus(wp, irqstatus);
82 if ((irqstatus & HDMI_IRQ_LINK_CONNECT) &&
83 irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
86 * If we get both connect and disconnect interrupts at the same
87 * time, turn off the PHY, clear interrupts, and restart, which
88 * raises connect interrupt if a cable is connected, or nothing
89 * if cable is not connected.
92 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_OFF);
95 * We always get bogus CONNECT & DISCONNECT interrupts when
96 * setting the PHY to LDOON. To ignore those, we force the RXDET
97 * line to 0 until the PHY power state has been changed.
99 v = hdmi_read_reg(hdmi->phy.base, HDMI_TXPHY_PAD_CFG_CTRL);
100 v = FLD_MOD(v, 1, 15, 15); /* FORCE_RXDET_HIGH */
101 v = FLD_MOD(v, 0, 14, 7); /* RXDET_LINE */
102 hdmi_write_reg(hdmi->phy.base, HDMI_TXPHY_PAD_CFG_CTRL, v);
104 hdmi_wp_set_irqstatus(wp, HDMI_IRQ_LINK_CONNECT |
105 HDMI_IRQ_LINK_DISCONNECT);
107 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
109 REG_FLD_MOD(hdmi->phy.base, HDMI_TXPHY_PAD_CFG_CTRL, 0, 15, 15);
111 } else if (irqstatus & HDMI_IRQ_LINK_CONNECT) {
112 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_TXON);
113 } else if (irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
114 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
120 static int hdmi_init_regulator(struct omap_hdmi *hdmi)
122 struct regulator *reg;
124 if (hdmi->vdda_reg != NULL)
127 reg = devm_regulator_get(&hdmi->pdev->dev, "vdda");
129 DSSERR("can't get VDDA regulator\n");
133 hdmi->vdda_reg = reg;
138 static int hdmi_power_on_core(struct omap_hdmi *hdmi)
142 r = regulator_enable(hdmi->vdda_reg);
146 r = hdmi_runtime_get(hdmi);
148 goto err_runtime_get;
150 /* Make selection of HDMI in DSS */
151 dss_select_hdmi_venc_clk_source(hdmi->dss, DSS_HDMI_M_PCLK);
153 hdmi->core_enabled = true;
158 regulator_disable(hdmi->vdda_reg);
163 static void hdmi_power_off_core(struct omap_hdmi *hdmi)
165 hdmi->core_enabled = false;
167 hdmi_runtime_put(hdmi);
168 regulator_disable(hdmi->vdda_reg);
171 static int hdmi_power_on_full(struct omap_hdmi *hdmi)
174 struct videomode *vm;
175 struct dss_pll_clock_info hdmi_cinfo = { 0 };
178 r = hdmi_power_on_core(hdmi);
184 DSSDBG("hdmi_power_on hactive= %d vactive = %d\n", vm->hactive,
188 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
191 /* DSS_HDMI_TCLK is bitclk / 10 */
194 dss_pll_calc_b(&hdmi->pll.pll, clk_get_rate(hdmi->pll.pll.clkin),
197 /* disable and clear irqs */
198 hdmi_wp_clear_irqenable(&hdmi->wp, 0xffffffff);
199 hdmi_wp_set_irqstatus(&hdmi->wp,
200 hdmi_wp_get_irqstatus(&hdmi->wp));
202 r = dss_pll_enable(&hdmi->pll.pll);
204 DSSERR("Failed to enable PLL\n");
208 r = dss_pll_set_config(&hdmi->pll.pll, &hdmi_cinfo);
210 DSSERR("Failed to configure PLL\n");
214 r = hdmi_phy_configure(&hdmi->phy, hdmi_cinfo.clkdco,
215 hdmi_cinfo.clkout[0]);
217 DSSDBG("Failed to start PHY\n");
221 r = hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_LDOON);
225 hdmi5_configure(&hdmi->core, &hdmi->wp, &hdmi->cfg);
228 dss_mgr_set_timings(&hdmi->output, vm);
230 r = dss_mgr_enable(&hdmi->output);
234 r = hdmi_wp_video_start(&hdmi->wp);
238 hdmi_wp_set_irqenable(&hdmi->wp,
239 HDMI_IRQ_LINK_CONNECT | HDMI_IRQ_LINK_DISCONNECT);
244 dss_mgr_disable(&hdmi->output);
246 hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_OFF);
250 dss_pll_disable(&hdmi->pll.pll);
252 hdmi_power_off_core(hdmi);
256 static void hdmi_power_off_full(struct omap_hdmi *hdmi)
258 hdmi_wp_clear_irqenable(&hdmi->wp, 0xffffffff);
260 hdmi_wp_video_stop(&hdmi->wp);
262 dss_mgr_disable(&hdmi->output);
264 hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_OFF);
266 dss_pll_disable(&hdmi->pll.pll);
268 hdmi_power_off_core(hdmi);
271 static int hdmi_display_check_timing(struct omap_dss_device *dssdev,
272 struct videomode *vm)
274 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
276 if (!dispc_mgr_timings_ok(hdmi->dss->dispc, dssdev->dispc_channel, vm))
282 static void hdmi_display_set_timing(struct omap_dss_device *dssdev,
283 struct videomode *vm)
285 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
287 mutex_lock(&hdmi->lock);
291 dispc_set_tv_pclk(hdmi->dss->dispc, vm->pixelclock);
293 mutex_unlock(&hdmi->lock);
296 static void hdmi_display_get_timings(struct omap_dss_device *dssdev,
297 struct videomode *vm)
299 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
304 static int hdmi_dump_regs(struct seq_file *s, void *p)
306 struct omap_hdmi *hdmi = s->private;
308 mutex_lock(&hdmi->lock);
310 if (hdmi_runtime_get(hdmi)) {
311 mutex_unlock(&hdmi->lock);
315 hdmi_wp_dump(&hdmi->wp, s);
316 hdmi_pll_dump(&hdmi->pll, s);
317 hdmi_phy_dump(&hdmi->phy, s);
318 hdmi5_core_dump(&hdmi->core, s);
320 hdmi_runtime_put(hdmi);
321 mutex_unlock(&hdmi->lock);
325 static int read_edid(struct omap_hdmi *hdmi, u8 *buf, int len)
330 mutex_lock(&hdmi->lock);
332 r = hdmi_runtime_get(hdmi);
335 idlemode = REG_GET(hdmi->wp.base, HDMI_WP_SYSCONFIG, 3, 2);
337 REG_FLD_MOD(hdmi->wp.base, HDMI_WP_SYSCONFIG, 1, 3, 2);
339 r = hdmi5_read_edid(&hdmi->core, buf, len);
341 REG_FLD_MOD(hdmi->wp.base, HDMI_WP_SYSCONFIG, idlemode, 3, 2);
343 hdmi_runtime_put(hdmi);
344 mutex_unlock(&hdmi->lock);
349 static void hdmi_start_audio_stream(struct omap_hdmi *hd)
351 REG_FLD_MOD(hd->wp.base, HDMI_WP_SYSCONFIG, 1, 3, 2);
352 hdmi_wp_audio_enable(&hd->wp, true);
353 hdmi_wp_audio_core_req_enable(&hd->wp, true);
356 static void hdmi_stop_audio_stream(struct omap_hdmi *hd)
358 hdmi_wp_audio_core_req_enable(&hd->wp, false);
359 hdmi_wp_audio_enable(&hd->wp, false);
360 REG_FLD_MOD(hd->wp.base, HDMI_WP_SYSCONFIG, hd->wp_idlemode, 3, 2);
363 static int hdmi_display_enable(struct omap_dss_device *dssdev)
365 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
369 DSSDBG("ENTER hdmi_display_enable\n");
371 mutex_lock(&hdmi->lock);
373 if (!dssdev->dispc_channel_connected) {
374 DSSERR("failed to enable display: no output/manager\n");
379 r = hdmi_power_on_full(hdmi);
381 DSSERR("failed to power on device\n");
385 if (hdmi->audio_configured) {
386 r = hdmi5_audio_config(&hdmi->core, &hdmi->wp,
388 hdmi->cfg.vm.pixelclock);
390 DSSERR("Error restoring audio configuration: %d", r);
391 hdmi->audio_abort_cb(&hdmi->pdev->dev);
392 hdmi->audio_configured = false;
396 spin_lock_irqsave(&hdmi->audio_playing_lock, flags);
397 if (hdmi->audio_configured && hdmi->audio_playing)
398 hdmi_start_audio_stream(hdmi);
399 hdmi->display_enabled = true;
400 spin_unlock_irqrestore(&hdmi->audio_playing_lock, flags);
402 mutex_unlock(&hdmi->lock);
406 mutex_unlock(&hdmi->lock);
410 static void hdmi_display_disable(struct omap_dss_device *dssdev)
412 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
415 DSSDBG("Enter hdmi_display_disable\n");
417 mutex_lock(&hdmi->lock);
419 spin_lock_irqsave(&hdmi->audio_playing_lock, flags);
420 hdmi_stop_audio_stream(hdmi);
421 hdmi->display_enabled = false;
422 spin_unlock_irqrestore(&hdmi->audio_playing_lock, flags);
424 hdmi_power_off_full(hdmi);
426 mutex_unlock(&hdmi->lock);
429 static int hdmi_core_enable(struct omap_hdmi *hdmi)
433 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
435 mutex_lock(&hdmi->lock);
437 r = hdmi_power_on_core(hdmi);
439 DSSERR("failed to power on device\n");
443 mutex_unlock(&hdmi->lock);
447 mutex_unlock(&hdmi->lock);
451 static void hdmi_core_disable(struct omap_hdmi *hdmi)
453 DSSDBG("Enter omapdss_hdmi_core_disable\n");
455 mutex_lock(&hdmi->lock);
457 hdmi_power_off_core(hdmi);
459 mutex_unlock(&hdmi->lock);
462 static int hdmi_connect(struct omap_dss_device *dssdev,
463 struct omap_dss_device *dst)
465 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
468 r = hdmi_init_regulator(hdmi);
472 r = dss_mgr_connect(&hdmi->output, dssdev);
476 r = omapdss_output_set_device(dssdev, dst);
478 DSSERR("failed to connect output to new device: %s\n",
480 dss_mgr_disconnect(&hdmi->output, dssdev);
487 static void hdmi_disconnect(struct omap_dss_device *dssdev,
488 struct omap_dss_device *dst)
490 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
492 WARN_ON(dst != dssdev->dst);
494 if (dst != dssdev->dst)
497 omapdss_output_unset_device(dssdev);
499 dss_mgr_disconnect(&hdmi->output, dssdev);
502 static int hdmi_read_edid(struct omap_dss_device *dssdev,
505 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
509 need_enable = hdmi->core_enabled == false;
512 r = hdmi_core_enable(hdmi);
517 r = read_edid(hdmi, edid, len);
520 hdmi_core_disable(hdmi);
525 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
526 const struct hdmi_avi_infoframe *avi)
528 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
530 hdmi->cfg.infoframe = *avi;
534 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
537 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
539 hdmi->cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
543 static const struct omapdss_hdmi_ops hdmi_ops = {
544 .connect = hdmi_connect,
545 .disconnect = hdmi_disconnect,
547 .enable = hdmi_display_enable,
548 .disable = hdmi_display_disable,
550 .check_timings = hdmi_display_check_timing,
551 .set_timings = hdmi_display_set_timing,
552 .get_timings = hdmi_display_get_timings,
554 .read_edid = hdmi_read_edid,
555 .set_infoframe = hdmi_set_infoframe,
556 .set_hdmi_mode = hdmi_set_hdmi_mode,
559 static void hdmi_init_output(struct omap_hdmi *hdmi)
561 struct omap_dss_device *out = &hdmi->output;
563 out->dev = &hdmi->pdev->dev;
564 out->id = OMAP_DSS_OUTPUT_HDMI;
565 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
566 out->name = "hdmi.0";
567 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
568 out->ops.hdmi = &hdmi_ops;
569 out->owner = THIS_MODULE;
571 omapdss_register_output(out);
574 static void hdmi_uninit_output(struct omap_hdmi *hdmi)
576 struct omap_dss_device *out = &hdmi->output;
578 omapdss_unregister_output(out);
581 static int hdmi_probe_of(struct omap_hdmi *hdmi)
583 struct platform_device *pdev = hdmi->pdev;
584 struct device_node *node = pdev->dev.of_node;
585 struct device_node *ep;
588 ep = of_graph_get_endpoint_by_regs(node, 0, 0);
592 r = hdmi_parse_lanes_of(pdev, ep, &hdmi->phy);
604 /* Audio callbacks */
605 static int hdmi_audio_startup(struct device *dev,
606 void (*abort_cb)(struct device *dev))
608 struct omap_hdmi *hd = dev_get_drvdata(dev);
610 mutex_lock(&hd->lock);
612 WARN_ON(hd->audio_abort_cb != NULL);
614 hd->audio_abort_cb = abort_cb;
616 mutex_unlock(&hd->lock);
621 static int hdmi_audio_shutdown(struct device *dev)
623 struct omap_hdmi *hd = dev_get_drvdata(dev);
625 mutex_lock(&hd->lock);
626 hd->audio_abort_cb = NULL;
627 hd->audio_configured = false;
628 hd->audio_playing = false;
629 mutex_unlock(&hd->lock);
634 static int hdmi_audio_start(struct device *dev)
636 struct omap_hdmi *hd = dev_get_drvdata(dev);
639 spin_lock_irqsave(&hd->audio_playing_lock, flags);
641 if (hd->display_enabled) {
642 if (!hdmi_mode_has_audio(&hd->cfg))
643 DSSERR("%s: Video mode does not support audio\n",
645 hdmi_start_audio_stream(hd);
647 hd->audio_playing = true;
649 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
653 static void hdmi_audio_stop(struct device *dev)
655 struct omap_hdmi *hd = dev_get_drvdata(dev);
658 if (!hdmi_mode_has_audio(&hd->cfg))
659 DSSERR("%s: Video mode does not support audio\n", __func__);
661 spin_lock_irqsave(&hd->audio_playing_lock, flags);
663 if (hd->display_enabled)
664 hdmi_stop_audio_stream(hd);
665 hd->audio_playing = false;
667 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
670 static int hdmi_audio_config(struct device *dev,
671 struct omap_dss_audio *dss_audio)
673 struct omap_hdmi *hd = dev_get_drvdata(dev);
676 mutex_lock(&hd->lock);
678 if (hd->display_enabled) {
679 ret = hdmi5_audio_config(&hd->core, &hd->wp, dss_audio,
680 hd->cfg.vm.pixelclock);
685 hd->audio_configured = true;
686 hd->audio_config = *dss_audio;
688 mutex_unlock(&hd->lock);
693 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
694 .audio_startup = hdmi_audio_startup,
695 .audio_shutdown = hdmi_audio_shutdown,
696 .audio_start = hdmi_audio_start,
697 .audio_stop = hdmi_audio_stop,
698 .audio_config = hdmi_audio_config,
701 static int hdmi_audio_register(struct omap_hdmi *hdmi)
703 struct omap_hdmi_audio_pdata pdata = {
704 .dev = &hdmi->pdev->dev,
706 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi->wp),
707 .ops = &hdmi_audio_ops,
710 hdmi->audio_pdev = platform_device_register_data(
711 &hdmi->pdev->dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
712 &pdata, sizeof(pdata));
714 if (IS_ERR(hdmi->audio_pdev))
715 return PTR_ERR(hdmi->audio_pdev);
717 hdmi_runtime_get(hdmi);
719 REG_GET(hdmi->wp.base, HDMI_WP_SYSCONFIG, 3, 2);
720 hdmi_runtime_put(hdmi);
725 /* HDMI HW IP initialisation */
726 static int hdmi5_bind(struct device *dev, struct device *master, void *data)
728 struct platform_device *pdev = to_platform_device(dev);
729 struct dss_device *dss = dss_get_device(master);
730 struct omap_hdmi *hdmi;
734 hdmi = kzalloc(sizeof(*hdmi), GFP_KERNEL);
740 dev_set_drvdata(&pdev->dev, hdmi);
742 mutex_init(&hdmi->lock);
743 spin_lock_init(&hdmi->audio_playing_lock);
745 r = hdmi_probe_of(hdmi);
749 r = hdmi_wp_init(pdev, &hdmi->wp, 5);
753 r = hdmi_pll_init(dss, pdev, &hdmi->pll, &hdmi->wp);
757 r = hdmi_phy_init(pdev, &hdmi->phy, 5);
761 r = hdmi5_core_init(pdev, &hdmi->core);
765 irq = platform_get_irq(pdev, 0);
767 DSSERR("platform_get_irq failed\n");
772 r = devm_request_threaded_irq(&pdev->dev, irq,
773 NULL, hdmi_irq_handler,
774 IRQF_ONESHOT, "OMAP HDMI", hdmi);
776 DSSERR("HDMI IRQ request failed\n");
780 pm_runtime_enable(&pdev->dev);
782 hdmi_init_output(hdmi);
784 r = hdmi_audio_register(hdmi);
786 DSSERR("Registering HDMI audio failed %d\n", r);
787 hdmi_uninit_output(hdmi);
788 pm_runtime_disable(&pdev->dev);
792 hdmi->debugfs = dss_debugfs_create_file(dss, "hdmi", hdmi_dump_regs,
798 hdmi_pll_uninit(&hdmi->pll);
804 static void hdmi5_unbind(struct device *dev, struct device *master, void *data)
806 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
808 dss_debugfs_remove_file(hdmi->debugfs);
810 if (hdmi->audio_pdev)
811 platform_device_unregister(hdmi->audio_pdev);
813 hdmi_uninit_output(hdmi);
815 hdmi_pll_uninit(&hdmi->pll);
817 pm_runtime_disable(dev);
822 static const struct component_ops hdmi5_component_ops = {
824 .unbind = hdmi5_unbind,
827 static int hdmi5_probe(struct platform_device *pdev)
829 return component_add(&pdev->dev, &hdmi5_component_ops);
832 static int hdmi5_remove(struct platform_device *pdev)
834 component_del(&pdev->dev, &hdmi5_component_ops);
838 static int hdmi_runtime_suspend(struct device *dev)
840 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
842 dispc_runtime_put(hdmi->dss->dispc);
847 static int hdmi_runtime_resume(struct device *dev)
849 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
852 r = dispc_runtime_get(hdmi->dss->dispc);
859 static const struct dev_pm_ops hdmi_pm_ops = {
860 .runtime_suspend = hdmi_runtime_suspend,
861 .runtime_resume = hdmi_runtime_resume,
864 static const struct of_device_id hdmi_of_match[] = {
865 { .compatible = "ti,omap5-hdmi", },
866 { .compatible = "ti,dra7-hdmi", },
870 struct platform_driver omapdss_hdmi5hw_driver = {
871 .probe = hdmi5_probe,
872 .remove = hdmi5_remove,
874 .name = "omapdss_hdmi5",
876 .of_match_table = hdmi_of_match,
877 .suppress_bind_attrs = true,