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_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 drm_display_mode *mode)
253 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
255 mutex_lock(&hdmi->lock);
257 drm_display_mode_to_videomode(mode, &hdmi->cfg.vm);
259 dispc_set_tv_pclk(hdmi->dss->dispc, mode->clock * 1000);
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 void 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 r = hdmi_power_on_full(hdmi);
335 DSSERR("failed to power on device\n");
339 if (hdmi->audio_configured) {
340 r = hdmi5_audio_config(&hdmi->core, &hdmi->wp,
342 hdmi->cfg.vm.pixelclock);
344 DSSERR("Error restoring audio configuration: %d", r);
345 hdmi->audio_abort_cb(&hdmi->pdev->dev);
346 hdmi->audio_configured = false;
350 spin_lock_irqsave(&hdmi->audio_playing_lock, flags);
351 if (hdmi->audio_configured && hdmi->audio_playing)
352 hdmi_start_audio_stream(hdmi);
353 hdmi->display_enabled = true;
354 spin_unlock_irqrestore(&hdmi->audio_playing_lock, flags);
357 mutex_unlock(&hdmi->lock);
360 static void hdmi_display_disable(struct omap_dss_device *dssdev)
362 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
365 DSSDBG("Enter hdmi_display_disable\n");
367 mutex_lock(&hdmi->lock);
369 spin_lock_irqsave(&hdmi->audio_playing_lock, flags);
370 hdmi_stop_audio_stream(hdmi);
371 hdmi->display_enabled = false;
372 spin_unlock_irqrestore(&hdmi->audio_playing_lock, flags);
374 hdmi_power_off_full(hdmi);
376 mutex_unlock(&hdmi->lock);
379 static int hdmi_core_enable(struct omap_hdmi *hdmi)
383 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
385 mutex_lock(&hdmi->lock);
387 r = hdmi_power_on_core(hdmi);
389 DSSERR("failed to power on device\n");
393 mutex_unlock(&hdmi->lock);
397 mutex_unlock(&hdmi->lock);
401 static void hdmi_core_disable(struct omap_hdmi *hdmi)
403 DSSDBG("Enter omapdss_hdmi_core_disable\n");
405 mutex_lock(&hdmi->lock);
407 hdmi_power_off_core(hdmi);
409 mutex_unlock(&hdmi->lock);
412 static int hdmi_connect(struct omap_dss_device *src,
413 struct omap_dss_device *dst)
415 return omapdss_device_connect(dst->dss, dst, dst->next);
418 static void hdmi_disconnect(struct omap_dss_device *src,
419 struct omap_dss_device *dst)
421 omapdss_device_disconnect(dst, dst->next);
424 static int hdmi_read_edid(struct omap_dss_device *dssdev,
427 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
431 need_enable = hdmi->core_enabled == false;
434 r = hdmi_core_enable(hdmi);
439 r = read_edid(hdmi, edid, len);
442 hdmi_core_disable(hdmi);
447 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
448 const struct hdmi_avi_infoframe *avi)
450 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
452 hdmi->cfg.infoframe = *avi;
456 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
459 struct omap_hdmi *hdmi = dssdev_to_hdmi(dssdev);
461 hdmi->cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
465 static const struct omap_dss_device_ops hdmi_ops = {
466 .connect = hdmi_connect,
467 .disconnect = hdmi_disconnect,
469 .enable = hdmi_display_enable,
470 .disable = hdmi_display_disable,
472 .set_timings = hdmi_display_set_timings,
474 .read_edid = hdmi_read_edid,
477 .set_infoframe = hdmi_set_infoframe,
478 .set_hdmi_mode = hdmi_set_hdmi_mode,
482 /* -----------------------------------------------------------------------------
486 static int hdmi_audio_startup(struct device *dev,
487 void (*abort_cb)(struct device *dev))
489 struct omap_hdmi *hd = dev_get_drvdata(dev);
491 mutex_lock(&hd->lock);
493 WARN_ON(hd->audio_abort_cb != NULL);
495 hd->audio_abort_cb = abort_cb;
497 mutex_unlock(&hd->lock);
502 static int hdmi_audio_shutdown(struct device *dev)
504 struct omap_hdmi *hd = dev_get_drvdata(dev);
506 mutex_lock(&hd->lock);
507 hd->audio_abort_cb = NULL;
508 hd->audio_configured = false;
509 hd->audio_playing = false;
510 mutex_unlock(&hd->lock);
515 static int hdmi_audio_start(struct device *dev)
517 struct omap_hdmi *hd = dev_get_drvdata(dev);
520 spin_lock_irqsave(&hd->audio_playing_lock, flags);
522 if (hd->display_enabled) {
523 if (!hdmi_mode_has_audio(&hd->cfg))
524 DSSERR("%s: Video mode does not support audio\n",
526 hdmi_start_audio_stream(hd);
528 hd->audio_playing = true;
530 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
534 static void hdmi_audio_stop(struct device *dev)
536 struct omap_hdmi *hd = dev_get_drvdata(dev);
539 if (!hdmi_mode_has_audio(&hd->cfg))
540 DSSERR("%s: Video mode does not support audio\n", __func__);
542 spin_lock_irqsave(&hd->audio_playing_lock, flags);
544 if (hd->display_enabled)
545 hdmi_stop_audio_stream(hd);
546 hd->audio_playing = false;
548 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
551 static int hdmi_audio_config(struct device *dev,
552 struct omap_dss_audio *dss_audio)
554 struct omap_hdmi *hd = dev_get_drvdata(dev);
557 mutex_lock(&hd->lock);
559 if (hd->display_enabled) {
560 ret = hdmi5_audio_config(&hd->core, &hd->wp, dss_audio,
561 hd->cfg.vm.pixelclock);
566 hd->audio_configured = true;
567 hd->audio_config = *dss_audio;
569 mutex_unlock(&hd->lock);
574 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
575 .audio_startup = hdmi_audio_startup,
576 .audio_shutdown = hdmi_audio_shutdown,
577 .audio_start = hdmi_audio_start,
578 .audio_stop = hdmi_audio_stop,
579 .audio_config = hdmi_audio_config,
582 static int hdmi_audio_register(struct omap_hdmi *hdmi)
584 struct omap_hdmi_audio_pdata pdata = {
585 .dev = &hdmi->pdev->dev,
587 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi->wp),
588 .ops = &hdmi_audio_ops,
591 hdmi->audio_pdev = platform_device_register_data(
592 &hdmi->pdev->dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
593 &pdata, sizeof(pdata));
595 if (IS_ERR(hdmi->audio_pdev))
596 return PTR_ERR(hdmi->audio_pdev);
598 hdmi_runtime_get(hdmi);
600 REG_GET(hdmi->wp.base, HDMI_WP_SYSCONFIG, 3, 2);
601 hdmi_runtime_put(hdmi);
606 /* -----------------------------------------------------------------------------
607 * Component Bind & Unbind
610 static int hdmi5_bind(struct device *dev, struct device *master, void *data)
612 struct dss_device *dss = dss_get_device(master);
613 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
618 r = hdmi_pll_init(dss, hdmi->pdev, &hdmi->pll, &hdmi->wp);
622 r = hdmi_audio_register(hdmi);
624 DSSERR("Registering HDMI audio failed %d\n", r);
628 hdmi->debugfs = dss_debugfs_create_file(dss, "hdmi", hdmi_dump_regs,
634 hdmi_pll_uninit(&hdmi->pll);
638 static void hdmi5_unbind(struct device *dev, struct device *master, void *data)
640 struct omap_hdmi *hdmi = dev_get_drvdata(dev);
642 dss_debugfs_remove_file(hdmi->debugfs);
644 if (hdmi->audio_pdev)
645 platform_device_unregister(hdmi->audio_pdev);
647 hdmi_pll_uninit(&hdmi->pll);
650 static const struct component_ops hdmi5_component_ops = {
652 .unbind = hdmi5_unbind,
655 /* -----------------------------------------------------------------------------
656 * Probe & Remove, Suspend & Resume
659 static int hdmi5_init_output(struct omap_hdmi *hdmi)
661 struct omap_dss_device *out = &hdmi->output;
664 out->dev = &hdmi->pdev->dev;
665 out->id = OMAP_DSS_OUTPUT_HDMI;
666 out->type = OMAP_DISPLAY_TYPE_HDMI;
667 out->name = "hdmi.0";
668 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
669 out->ops = &hdmi_ops;
670 out->owner = THIS_MODULE;
671 out->of_ports = BIT(0);
672 out->ops_flags = OMAP_DSS_DEVICE_OP_EDID;
674 r = omapdss_device_init_output(out);
678 omapdss_device_register(out);
683 static void hdmi5_uninit_output(struct omap_hdmi *hdmi)
685 struct omap_dss_device *out = &hdmi->output;
687 omapdss_device_unregister(out);
688 omapdss_device_cleanup_output(out);
691 static int hdmi5_probe_of(struct omap_hdmi *hdmi)
693 struct platform_device *pdev = hdmi->pdev;
694 struct device_node *node = pdev->dev.of_node;
695 struct device_node *ep;
698 ep = of_graph_get_endpoint_by_regs(node, 0, 0);
702 r = hdmi_parse_lanes_of(pdev, ep, &hdmi->phy);
707 static int hdmi5_probe(struct platform_device *pdev)
709 struct omap_hdmi *hdmi;
713 hdmi = kzalloc(sizeof(*hdmi), GFP_KERNEL);
719 dev_set_drvdata(&pdev->dev, hdmi);
721 mutex_init(&hdmi->lock);
722 spin_lock_init(&hdmi->audio_playing_lock);
724 r = hdmi5_probe_of(hdmi);
728 r = hdmi_wp_init(pdev, &hdmi->wp, 5);
732 r = hdmi_phy_init(pdev, &hdmi->phy, 5);
736 r = hdmi5_core_init(pdev, &hdmi->core);
740 irq = platform_get_irq(pdev, 0);
742 DSSERR("platform_get_irq failed\n");
747 r = devm_request_threaded_irq(&pdev->dev, irq,
748 NULL, hdmi_irq_handler,
749 IRQF_ONESHOT, "OMAP HDMI", hdmi);
751 DSSERR("HDMI IRQ request failed\n");
755 hdmi->vdda_reg = devm_regulator_get(&pdev->dev, "vdda");
756 if (IS_ERR(hdmi->vdda_reg)) {
757 r = PTR_ERR(hdmi->vdda_reg);
758 if (r != -EPROBE_DEFER)
759 DSSERR("can't get VDDA regulator\n");
763 pm_runtime_enable(&pdev->dev);
765 r = hdmi5_init_output(hdmi);
769 r = component_add(&pdev->dev, &hdmi5_component_ops);
771 goto err_uninit_output;
776 hdmi5_uninit_output(hdmi);
778 pm_runtime_disable(&pdev->dev);
784 static int hdmi5_remove(struct platform_device *pdev)
786 struct omap_hdmi *hdmi = platform_get_drvdata(pdev);
788 component_del(&pdev->dev, &hdmi5_component_ops);
790 hdmi5_uninit_output(hdmi);
792 pm_runtime_disable(&pdev->dev);
798 static const struct of_device_id hdmi_of_match[] = {
799 { .compatible = "ti,omap5-hdmi", },
800 { .compatible = "ti,dra7-hdmi", },
804 struct platform_driver omapdss_hdmi5hw_driver = {
805 .probe = hdmi5_probe,
806 .remove = hdmi5_remove,
808 .name = "omapdss_hdmi5",
809 .of_match_table = hdmi_of_match,
810 .suppress_bind_attrs = true,