2 * HDMI driver for OMAP5
4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.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_power_on_core(struct omap_hdmi *hdmi)
124 r = regulator_enable(hdmi->vdda_reg);
128 r = hdmi_runtime_get(hdmi);
130 goto err_runtime_get;
132 /* Make selection of HDMI in DSS */
133 dss_select_hdmi_venc_clk_source(hdmi->dss, DSS_HDMI_M_PCLK);
135 hdmi->core_enabled = true;
140 regulator_disable(hdmi->vdda_reg);
145 static void hdmi_power_off_core(struct omap_hdmi *hdmi)
147 hdmi->core_enabled = false;
149 hdmi_runtime_put(hdmi);
150 regulator_disable(hdmi->vdda_reg);
153 static int hdmi_power_on_full(struct omap_hdmi *hdmi)
156 const struct videomode *vm;
157 struct dss_pll_clock_info hdmi_cinfo = { 0 };
160 r = hdmi_power_on_core(hdmi);
166 DSSDBG("hdmi_power_on hactive= %d vactive = %d\n", vm->hactive,
170 if (vm->flags & DISPLAY_FLAGS_DOUBLECLK)
173 /* DSS_HDMI_TCLK is bitclk / 10 */
176 dss_pll_calc_b(&hdmi->pll.pll, clk_get_rate(hdmi->pll.pll.clkin),
179 /* disable and clear irqs */
180 hdmi_wp_clear_irqenable(&hdmi->wp, 0xffffffff);
181 hdmi_wp_set_irqstatus(&hdmi->wp,
182 hdmi_wp_get_irqstatus(&hdmi->wp));
184 r = dss_pll_enable(&hdmi->pll.pll);
186 DSSERR("Failed to enable PLL\n");
190 r = dss_pll_set_config(&hdmi->pll.pll, &hdmi_cinfo);
192 DSSERR("Failed to configure PLL\n");
196 r = hdmi_phy_configure(&hdmi->phy, hdmi_cinfo.clkdco,
197 hdmi_cinfo.clkout[0]);
199 DSSDBG("Failed to start PHY\n");
203 r = hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_LDOON);
207 hdmi5_configure(&hdmi->core, &hdmi->wp, &hdmi->cfg);
209 r = dss_mgr_enable(&hdmi->output);
213 r = hdmi_wp_video_start(&hdmi->wp);
217 hdmi_wp_set_irqenable(&hdmi->wp,
218 HDMI_IRQ_LINK_CONNECT | HDMI_IRQ_LINK_DISCONNECT);
223 dss_mgr_disable(&hdmi->output);
225 hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_OFF);
229 dss_pll_disable(&hdmi->pll.pll);
231 hdmi_power_off_core(hdmi);
235 static void hdmi_power_off_full(struct omap_hdmi *hdmi)
237 hdmi_wp_clear_irqenable(&hdmi->wp, 0xffffffff);
239 hdmi_wp_video_stop(&hdmi->wp);
241 dss_mgr_disable(&hdmi->output);
243 hdmi_wp_set_phy_pwr(&hdmi->wp, HDMI_PHYPWRCMD_OFF);
245 dss_pll_disable(&hdmi->pll.pll);
247 hdmi_power_off_core(hdmi);
250 static void hdmi_display_set_timings(struct omap_dss_device *dssdev,
251 const struct videomode *vm)
253 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
255 mutex_lock(&hdmi->lock);
259 dispc_set_tv_pclk(hdmi->dss->dispc, vm->pixelclock);
261 mutex_unlock(&hdmi->lock);
264 static int hdmi_dump_regs(struct seq_file *s, void *p)
266 struct omap_hdmi *hdmi = s->private;
268 mutex_lock(&hdmi->lock);
270 if (hdmi_runtime_get(hdmi)) {
271 mutex_unlock(&hdmi->lock);
275 hdmi_wp_dump(&hdmi->wp, s);
276 hdmi_pll_dump(&hdmi->pll, s);
277 hdmi_phy_dump(&hdmi->phy, s);
278 hdmi5_core_dump(&hdmi->core, s);
280 hdmi_runtime_put(hdmi);
281 mutex_unlock(&hdmi->lock);
285 static int read_edid(struct omap_hdmi *hdmi, u8 *buf, int len)
290 mutex_lock(&hdmi->lock);
292 r = hdmi_runtime_get(hdmi);
295 idlemode = REG_GET(hdmi->wp.base, HDMI_WP_SYSCONFIG, 3, 2);
297 REG_FLD_MOD(hdmi->wp.base, HDMI_WP_SYSCONFIG, 1, 3, 2);
299 r = hdmi5_read_edid(&hdmi->core, buf, len);
301 REG_FLD_MOD(hdmi->wp.base, HDMI_WP_SYSCONFIG, idlemode, 3, 2);
303 hdmi_runtime_put(hdmi);
304 mutex_unlock(&hdmi->lock);
309 static void hdmi_start_audio_stream(struct omap_hdmi *hd)
311 REG_FLD_MOD(hd->wp.base, HDMI_WP_SYSCONFIG, 1, 3, 2);
312 hdmi_wp_audio_enable(&hd->wp, true);
313 hdmi_wp_audio_core_req_enable(&hd->wp, true);
316 static void hdmi_stop_audio_stream(struct omap_hdmi *hd)
318 hdmi_wp_audio_core_req_enable(&hd->wp, false);
319 hdmi_wp_audio_enable(&hd->wp, false);
320 REG_FLD_MOD(hd->wp.base, HDMI_WP_SYSCONFIG, hd->wp_idlemode, 3, 2);
323 static int hdmi_display_enable(struct omap_dss_device *dssdev)
325 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
329 DSSDBG("ENTER hdmi_display_enable\n");
331 mutex_lock(&hdmi->lock);
333 if (!dssdev->dispc_channel_connected) {
334 DSSERR("failed to enable display: no output/manager\n");
339 r = hdmi_power_on_full(hdmi);
341 DSSERR("failed to power on device\n");
345 if (hdmi->audio_configured) {
346 r = hdmi5_audio_config(&hdmi->core, &hdmi->wp,
348 hdmi->cfg.vm.pixelclock);
350 DSSERR("Error restoring audio configuration: %d", r);
351 hdmi->audio_abort_cb(&hdmi->pdev->dev);
352 hdmi->audio_configured = false;
356 spin_lock_irqsave(&hdmi->audio_playing_lock, flags);
357 if (hdmi->audio_configured && hdmi->audio_playing)
358 hdmi_start_audio_stream(hdmi);
359 hdmi->display_enabled = true;
360 spin_unlock_irqrestore(&hdmi->audio_playing_lock, flags);
362 mutex_unlock(&hdmi->lock);
366 mutex_unlock(&hdmi->lock);
370 static void hdmi_display_disable(struct omap_dss_device *dssdev)
372 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
375 DSSDBG("Enter hdmi_display_disable\n");
377 mutex_lock(&hdmi->lock);
379 spin_lock_irqsave(&hdmi->audio_playing_lock, flags);
380 hdmi_stop_audio_stream(hdmi);
381 hdmi->display_enabled = false;
382 spin_unlock_irqrestore(&hdmi->audio_playing_lock, flags);
384 hdmi_power_off_full(hdmi);
386 mutex_unlock(&hdmi->lock);
389 static int hdmi_core_enable(struct omap_hdmi *hdmi)
393 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
395 mutex_lock(&hdmi->lock);
397 r = hdmi_power_on_core(hdmi);
399 DSSERR("failed to power on device\n");
403 mutex_unlock(&hdmi->lock);
407 mutex_unlock(&hdmi->lock);
411 static void hdmi_core_disable(struct omap_hdmi *hdmi)
413 DSSDBG("Enter omapdss_hdmi_core_disable\n");
415 mutex_lock(&hdmi->lock);
417 hdmi_power_off_core(hdmi);
419 mutex_unlock(&hdmi->lock);
422 static int hdmi_connect(struct omap_dss_device *src,
423 struct omap_dss_device *dst)
427 r = omapdss_device_connect(dst->dss, dst, dst->next);
431 dst->dispc_channel_connected = true;
435 static void hdmi_disconnect(struct omap_dss_device *src,
436 struct omap_dss_device *dst)
438 dst->dispc_channel_connected = false;
440 omapdss_device_disconnect(dst, dst->next);
443 static int hdmi_read_edid(struct omap_dss_device *dssdev,
446 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
450 need_enable = hdmi->core_enabled == false;
453 r = hdmi_core_enable(hdmi);
458 r = read_edid(hdmi, edid, len);
461 hdmi_core_disable(hdmi);
466 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
467 const struct hdmi_avi_infoframe *avi)
469 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
471 hdmi->cfg.infoframe = *avi;
475 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
478 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
480 hdmi->cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
484 static const struct omap_dss_device_ops hdmi_ops = {
485 .connect = hdmi_connect,
486 .disconnect = hdmi_disconnect,
488 .enable = hdmi_display_enable,
489 .disable = hdmi_display_disable,
491 .set_timings = hdmi_display_set_timings,
493 .read_edid = hdmi_read_edid,
496 .set_infoframe = hdmi_set_infoframe,
497 .set_hdmi_mode = hdmi_set_hdmi_mode,
501 /* -----------------------------------------------------------------------------
505 static int hdmi_audio_startup(struct device *dev,
506 void (*abort_cb)(struct device *dev))
508 struct omap_hdmi *hd = dev_get_drvdata(dev);
510 mutex_lock(&hd->lock);
512 WARN_ON(hd->audio_abort_cb != NULL);
514 hd->audio_abort_cb = abort_cb;
516 mutex_unlock(&hd->lock);
521 static int hdmi_audio_shutdown(struct device *dev)
523 struct omap_hdmi *hd = dev_get_drvdata(dev);
525 mutex_lock(&hd->lock);
526 hd->audio_abort_cb = NULL;
527 hd->audio_configured = false;
528 hd->audio_playing = false;
529 mutex_unlock(&hd->lock);
534 static int hdmi_audio_start(struct device *dev)
536 struct omap_hdmi *hd = dev_get_drvdata(dev);
539 spin_lock_irqsave(&hd->audio_playing_lock, flags);
541 if (hd->display_enabled) {
542 if (!hdmi_mode_has_audio(&hd->cfg))
543 DSSERR("%s: Video mode does not support audio\n",
545 hdmi_start_audio_stream(hd);
547 hd->audio_playing = true;
549 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
553 static void hdmi_audio_stop(struct device *dev)
555 struct omap_hdmi *hd = dev_get_drvdata(dev);
558 if (!hdmi_mode_has_audio(&hd->cfg))
559 DSSERR("%s: Video mode does not support audio\n", __func__);
561 spin_lock_irqsave(&hd->audio_playing_lock, flags);
563 if (hd->display_enabled)
564 hdmi_stop_audio_stream(hd);
565 hd->audio_playing = false;
567 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
570 static int hdmi_audio_config(struct device *dev,
571 struct omap_dss_audio *dss_audio)
573 struct omap_hdmi *hd = dev_get_drvdata(dev);
576 mutex_lock(&hd->lock);
578 if (hd->display_enabled) {
579 ret = hdmi5_audio_config(&hd->core, &hd->wp, dss_audio,
580 hd->cfg.vm.pixelclock);
585 hd->audio_configured = true;
586 hd->audio_config = *dss_audio;
588 mutex_unlock(&hd->lock);
593 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
594 .audio_startup = hdmi_audio_startup,
595 .audio_shutdown = hdmi_audio_shutdown,
596 .audio_start = hdmi_audio_start,
597 .audio_stop = hdmi_audio_stop,
598 .audio_config = hdmi_audio_config,
601 static int hdmi_audio_register(struct omap_hdmi *hdmi)
603 struct omap_hdmi_audio_pdata pdata = {
604 .dev = &hdmi->pdev->dev,
606 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi->wp),
607 .ops = &hdmi_audio_ops,
610 hdmi->audio_pdev = platform_device_register_data(
611 &hdmi->pdev->dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
612 &pdata, sizeof(pdata));
614 if (IS_ERR(hdmi->audio_pdev))
615 return PTR_ERR(hdmi->audio_pdev);
617 hdmi_runtime_get(hdmi);
619 REG_GET(hdmi->wp.base, HDMI_WP_SYSCONFIG, 3, 2);
620 hdmi_runtime_put(hdmi);
625 /* -----------------------------------------------------------------------------
626 * Component Bind & Unbind
629 static int hdmi5_bind(struct device *dev, struct device *master, void *data)
631 struct dss_device *dss = dss_get_device(master);
632 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
637 r = hdmi_pll_init(dss, hdmi->pdev, &hdmi->pll, &hdmi->wp);
641 r = hdmi_audio_register(hdmi);
643 DSSERR("Registering HDMI audio failed %d\n", r);
647 hdmi->debugfs = dss_debugfs_create_file(dss, "hdmi", hdmi_dump_regs,
653 hdmi_pll_uninit(&hdmi->pll);
657 static void hdmi5_unbind(struct device *dev, struct device *master, void *data)
659 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
661 dss_debugfs_remove_file(hdmi->debugfs);
663 if (hdmi->audio_pdev)
664 platform_device_unregister(hdmi->audio_pdev);
666 hdmi_pll_uninit(&hdmi->pll);
669 static const struct component_ops hdmi5_component_ops = {
671 .unbind = hdmi5_unbind,
674 /* -----------------------------------------------------------------------------
675 * Probe & Remove, Suspend & Resume
678 static int hdmi5_init_output(struct omap_hdmi *hdmi)
680 struct omap_dss_device *out = &hdmi->output;
683 out->dev = &hdmi->pdev->dev;
684 out->id = OMAP_DSS_OUTPUT_HDMI;
685 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
686 out->name = "hdmi.0";
687 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
688 out->ops = &hdmi_ops;
689 out->owner = THIS_MODULE;
690 out->of_ports = BIT(0);
691 out->ops_flags = OMAP_DSS_DEVICE_OP_EDID;
693 out->next = omapdss_of_find_connected_device(out->dev->of_node, 0);
694 if (IS_ERR(out->next)) {
695 if (PTR_ERR(out->next) != -EPROBE_DEFER)
696 dev_err(out->dev, "failed to find video sink\n");
697 return PTR_ERR(out->next);
700 r = omapdss_output_validate(out);
702 omapdss_device_put(out->next);
707 omapdss_device_register(out);
712 static void hdmi5_uninit_output(struct omap_hdmi *hdmi)
714 struct omap_dss_device *out = &hdmi->output;
717 omapdss_device_put(out->next);
718 omapdss_device_unregister(out);
721 static int hdmi5_probe_of(struct omap_hdmi *hdmi)
723 struct platform_device *pdev = hdmi->pdev;
724 struct device_node *node = pdev->dev.of_node;
725 struct device_node *ep;
728 ep = of_graph_get_endpoint_by_regs(node, 0, 0);
732 r = hdmi_parse_lanes_of(pdev, ep, &hdmi->phy);
737 static int hdmi5_probe(struct platform_device *pdev)
739 struct omap_hdmi *hdmi;
743 hdmi = kzalloc(sizeof(*hdmi), GFP_KERNEL);
749 dev_set_drvdata(&pdev->dev, hdmi);
751 mutex_init(&hdmi->lock);
752 spin_lock_init(&hdmi->audio_playing_lock);
754 r = hdmi5_probe_of(hdmi);
758 r = hdmi_wp_init(pdev, &hdmi->wp, 5);
762 r = hdmi_phy_init(pdev, &hdmi->phy, 5);
766 r = hdmi5_core_init(pdev, &hdmi->core);
770 irq = platform_get_irq(pdev, 0);
772 DSSERR("platform_get_irq failed\n");
777 r = devm_request_threaded_irq(&pdev->dev, irq,
778 NULL, hdmi_irq_handler,
779 IRQF_ONESHOT, "OMAP HDMI", hdmi);
781 DSSERR("HDMI IRQ request failed\n");
785 hdmi->vdda_reg = devm_regulator_get(&pdev->dev, "vdda");
786 if (IS_ERR(hdmi->vdda_reg)) {
787 r = PTR_ERR(hdmi->vdda_reg);
788 if (r != -EPROBE_DEFER)
789 DSSERR("can't get VDDA regulator\n");
793 pm_runtime_enable(&pdev->dev);
795 r = hdmi5_init_output(hdmi);
799 r = component_add(&pdev->dev, &hdmi5_component_ops);
801 goto err_uninit_output;
806 hdmi5_uninit_output(hdmi);
808 pm_runtime_disable(&pdev->dev);
814 static int hdmi5_remove(struct platform_device *pdev)
816 struct omap_hdmi *hdmi = platform_get_drvdata(pdev);
818 component_del(&pdev->dev, &hdmi5_component_ops);
820 hdmi5_uninit_output(hdmi);
822 pm_runtime_disable(&pdev->dev);
828 static int hdmi_runtime_suspend(struct device *dev)
830 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
832 dispc_runtime_put(hdmi->dss->dispc);
837 static int hdmi_runtime_resume(struct device *dev)
839 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
842 r = dispc_runtime_get(hdmi->dss->dispc);
849 static const struct dev_pm_ops hdmi_pm_ops = {
850 .runtime_suspend = hdmi_runtime_suspend,
851 .runtime_resume = hdmi_runtime_resume,
854 static const struct of_device_id hdmi_of_match[] = {
855 { .compatible = "ti,omap5-hdmi", },
856 { .compatible = "ti,dra7-hdmi", },
860 struct platform_driver omapdss_hdmi5hw_driver = {
861 .probe = hdmi5_probe,
862 .remove = hdmi5_remove,
864 .name = "omapdss_hdmi5",
866 .of_match_table = hdmi_of_match,
867 .suppress_bind_attrs = true,