2 * HDMI interface DSS driver for TI's OMAP4 family of SoCs.
3 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com/
5 * Mythri pk <mythripk@ti.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published by
9 * the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #define DSS_SUBSYS_NAME "HDMI"
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/err.h>
26 #include <linux/interrupt.h>
27 #include <linux/mutex.h>
28 #include <linux/delay.h>
29 #include <linux/string.h>
30 #include <linux/platform_device.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/clk.h>
33 #include <linux/gpio.h>
34 #include <linux/regulator/consumer.h>
35 #include <linux/component.h>
36 #include <video/omapdss.h>
37 #include <sound/omap-hdmi-audio.h>
39 #include "hdmi4_core.h"
41 #include "dss_features.h"
44 static struct omap_hdmi hdmi;
46 static int hdmi_runtime_get(void)
50 DSSDBG("hdmi_runtime_get\n");
52 r = pm_runtime_get_sync(&hdmi.pdev->dev);
60 static void hdmi_runtime_put(void)
64 DSSDBG("hdmi_runtime_put\n");
66 r = pm_runtime_put_sync(&hdmi.pdev->dev);
67 WARN_ON(r < 0 && r != -ENOSYS);
70 static irqreturn_t hdmi_irq_handler(int irq, void *data)
72 struct hdmi_wp_data *wp = data;
75 irqstatus = hdmi_wp_get_irqstatus(wp);
76 hdmi_wp_set_irqstatus(wp, irqstatus);
78 if ((irqstatus & HDMI_IRQ_LINK_CONNECT) &&
79 irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
81 * If we get both connect and disconnect interrupts at the same
82 * time, turn off the PHY, clear interrupts, and restart, which
83 * raises connect interrupt if a cable is connected, or nothing
84 * if cable is not connected.
86 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_OFF);
88 hdmi_wp_set_irqstatus(wp, HDMI_IRQ_LINK_CONNECT |
89 HDMI_IRQ_LINK_DISCONNECT);
91 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
92 } else if (irqstatus & HDMI_IRQ_LINK_CONNECT) {
93 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_TXON);
94 } else if (irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
95 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
101 static int hdmi_init_regulator(void)
104 struct regulator *reg;
106 if (hdmi.vdda_reg != NULL)
109 reg = devm_regulator_get(&hdmi.pdev->dev, "vdda");
112 if (PTR_ERR(reg) != -EPROBE_DEFER)
113 DSSERR("can't get VDDA regulator\n");
117 if (regulator_can_change_voltage(reg)) {
118 r = regulator_set_voltage(reg, 1800000, 1800000);
120 devm_regulator_put(reg);
121 DSSWARN("can't set the regulator voltage\n");
131 static int hdmi_power_on_core(struct omap_dss_device *dssdev)
135 r = regulator_enable(hdmi.vdda_reg);
139 r = hdmi_runtime_get();
141 goto err_runtime_get;
143 /* Make selection of HDMI in DSS */
144 dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK);
146 hdmi.core_enabled = true;
151 regulator_disable(hdmi.vdda_reg);
156 static void hdmi_power_off_core(struct omap_dss_device *dssdev)
158 hdmi.core_enabled = false;
161 regulator_disable(hdmi.vdda_reg);
164 static int hdmi_power_on_full(struct omap_dss_device *dssdev)
167 struct omap_video_timings *p;
168 struct omap_overlay_manager *mgr = hdmi.output.manager;
169 struct hdmi_wp_data *wp = &hdmi.wp;
170 struct dss_pll_clock_info hdmi_cinfo = { 0 };
173 r = hdmi_power_on_core(dssdev);
177 /* disable and clear irqs */
178 hdmi_wp_clear_irqenable(wp, 0xffffffff);
179 hdmi_wp_set_irqstatus(wp, 0xffffffff);
181 p = &hdmi.cfg.timings;
183 DSSDBG("hdmi_power_on x_res= %d y_res = %d\n", p->x_res, p->y_res);
189 hdmi_pll_compute(&hdmi.pll, pc, &hdmi_cinfo);
191 r = dss_pll_enable(&hdmi.pll.pll);
193 DSSERR("Failed to enable PLL\n");
197 r = dss_pll_set_config(&hdmi.pll.pll, &hdmi_cinfo);
199 DSSERR("Failed to configure PLL\n");
203 r = hdmi_phy_configure(&hdmi.phy, hdmi_cinfo.clkdco,
204 hdmi_cinfo.clkout[0]);
206 DSSDBG("Failed to configure PHY\n");
210 r = hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
214 hdmi4_configure(&hdmi.core, &hdmi.wp, &hdmi.cfg);
216 /* bypass TV gamma table */
217 dispc_enable_gamma_table(0);
220 dss_mgr_set_timings(mgr, p);
222 r = dss_mgr_enable(mgr);
226 r = hdmi_wp_video_start(&hdmi.wp);
230 hdmi_wp_set_irqenable(wp,
231 HDMI_IRQ_LINK_CONNECT | HDMI_IRQ_LINK_DISCONNECT);
236 dss_mgr_disable(mgr);
238 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
242 dss_pll_disable(&hdmi.pll.pll);
244 hdmi_power_off_core(dssdev);
248 static void hdmi_power_off_full(struct omap_dss_device *dssdev)
250 struct omap_overlay_manager *mgr = hdmi.output.manager;
252 hdmi_wp_clear_irqenable(&hdmi.wp, 0xffffffff);
254 hdmi_wp_video_stop(&hdmi.wp);
256 dss_mgr_disable(mgr);
258 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
260 dss_pll_disable(&hdmi.pll.pll);
262 hdmi_power_off_core(dssdev);
265 static int hdmi_display_check_timing(struct omap_dss_device *dssdev,
266 struct omap_video_timings *timings)
268 struct omap_dss_device *out = &hdmi.output;
270 if (!dispc_mgr_timings_ok(out->dispc_channel, timings))
276 static void hdmi_display_set_timing(struct omap_dss_device *dssdev,
277 struct omap_video_timings *timings)
279 mutex_lock(&hdmi.lock);
281 hdmi.cfg.timings = *timings;
283 dispc_set_tv_pclk(timings->pixelclock);
285 mutex_unlock(&hdmi.lock);
288 static void hdmi_display_get_timings(struct omap_dss_device *dssdev,
289 struct omap_video_timings *timings)
291 *timings = hdmi.cfg.timings;
294 static void hdmi_dump_regs(struct seq_file *s)
296 mutex_lock(&hdmi.lock);
298 if (hdmi_runtime_get()) {
299 mutex_unlock(&hdmi.lock);
303 hdmi_wp_dump(&hdmi.wp, s);
304 hdmi_pll_dump(&hdmi.pll, s);
305 hdmi_phy_dump(&hdmi.phy, s);
306 hdmi4_core_dump(&hdmi.core, s);
309 mutex_unlock(&hdmi.lock);
312 static int read_edid(u8 *buf, int len)
316 mutex_lock(&hdmi.lock);
318 r = hdmi_runtime_get();
321 r = hdmi4_read_edid(&hdmi.core, buf, len);
324 mutex_unlock(&hdmi.lock);
329 static void hdmi_start_audio_stream(struct omap_hdmi *hd)
331 hdmi_wp_audio_enable(&hd->wp, true);
332 hdmi4_audio_start(&hd->core, &hd->wp);
335 static void hdmi_stop_audio_stream(struct omap_hdmi *hd)
337 hdmi4_audio_stop(&hd->core, &hd->wp);
338 hdmi_wp_audio_enable(&hd->wp, false);
341 static int hdmi_display_enable(struct omap_dss_device *dssdev)
343 struct omap_dss_device *out = &hdmi.output;
347 DSSDBG("ENTER hdmi_display_enable\n");
349 mutex_lock(&hdmi.lock);
351 if (!out->dispc_channel_connected) {
352 DSSERR("failed to enable display: no output/manager\n");
357 r = hdmi_power_on_full(dssdev);
359 DSSERR("failed to power on device\n");
363 if (hdmi.audio_configured) {
364 r = hdmi4_audio_config(&hdmi.core, &hdmi.wp, &hdmi.audio_config,
365 hdmi.cfg.timings.pixelclock);
367 DSSERR("Error restoring audio configuration: %d", r);
368 hdmi.audio_abort_cb(&hdmi.pdev->dev);
369 hdmi.audio_configured = false;
373 spin_lock_irqsave(&hdmi.audio_playing_lock, flags);
374 if (hdmi.audio_configured && hdmi.audio_playing)
375 hdmi_start_audio_stream(&hdmi);
376 hdmi.display_enabled = true;
377 spin_unlock_irqrestore(&hdmi.audio_playing_lock, flags);
379 mutex_unlock(&hdmi.lock);
383 mutex_unlock(&hdmi.lock);
387 static void hdmi_display_disable(struct omap_dss_device *dssdev)
391 DSSDBG("Enter hdmi_display_disable\n");
393 mutex_lock(&hdmi.lock);
395 spin_lock_irqsave(&hdmi.audio_playing_lock, flags);
396 hdmi_stop_audio_stream(&hdmi);
397 hdmi.display_enabled = false;
398 spin_unlock_irqrestore(&hdmi.audio_playing_lock, flags);
400 hdmi_power_off_full(dssdev);
402 mutex_unlock(&hdmi.lock);
405 static int hdmi_core_enable(struct omap_dss_device *dssdev)
409 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
411 mutex_lock(&hdmi.lock);
413 r = hdmi_power_on_core(dssdev);
415 DSSERR("failed to power on device\n");
419 mutex_unlock(&hdmi.lock);
423 mutex_unlock(&hdmi.lock);
427 static void hdmi_core_disable(struct omap_dss_device *dssdev)
429 DSSDBG("Enter omapdss_hdmi_core_disable\n");
431 mutex_lock(&hdmi.lock);
433 hdmi_power_off_core(dssdev);
435 mutex_unlock(&hdmi.lock);
438 static int hdmi_connect(struct omap_dss_device *dssdev,
439 struct omap_dss_device *dst)
441 struct omap_overlay_manager *mgr;
444 r = hdmi_init_regulator();
448 mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
452 r = dss_mgr_connect(mgr, dssdev);
456 r = omapdss_output_set_device(dssdev, dst);
458 DSSERR("failed to connect output to new device: %s\n",
460 dss_mgr_disconnect(mgr, dssdev);
467 static void hdmi_disconnect(struct omap_dss_device *dssdev,
468 struct omap_dss_device *dst)
470 WARN_ON(dst != dssdev->dst);
472 if (dst != dssdev->dst)
475 omapdss_output_unset_device(dssdev);
478 dss_mgr_disconnect(dssdev->manager, dssdev);
481 static int hdmi_read_edid(struct omap_dss_device *dssdev,
487 need_enable = hdmi.core_enabled == false;
490 r = hdmi_core_enable(dssdev);
495 r = read_edid(edid, len);
498 hdmi_core_disable(dssdev);
503 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
504 const struct hdmi_avi_infoframe *avi)
506 hdmi.cfg.infoframe = *avi;
510 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
513 hdmi.cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
517 static const struct omapdss_hdmi_ops hdmi_ops = {
518 .connect = hdmi_connect,
519 .disconnect = hdmi_disconnect,
521 .enable = hdmi_display_enable,
522 .disable = hdmi_display_disable,
524 .check_timings = hdmi_display_check_timing,
525 .set_timings = hdmi_display_set_timing,
526 .get_timings = hdmi_display_get_timings,
528 .read_edid = hdmi_read_edid,
529 .set_infoframe = hdmi_set_infoframe,
530 .set_hdmi_mode = hdmi_set_hdmi_mode,
533 static void hdmi_init_output(struct platform_device *pdev)
535 struct omap_dss_device *out = &hdmi.output;
537 out->dev = &pdev->dev;
538 out->id = OMAP_DSS_OUTPUT_HDMI;
539 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
540 out->name = "hdmi.0";
541 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
542 out->ops.hdmi = &hdmi_ops;
543 out->owner = THIS_MODULE;
545 omapdss_register_output(out);
548 static void hdmi_uninit_output(struct platform_device *pdev)
550 struct omap_dss_device *out = &hdmi.output;
552 omapdss_unregister_output(out);
555 static int hdmi_probe_of(struct platform_device *pdev)
557 struct device_node *node = pdev->dev.of_node;
558 struct device_node *ep;
561 ep = omapdss_of_get_first_endpoint(node);
565 r = hdmi_parse_lanes_of(pdev, ep, &hdmi.phy);
577 /* Audio callbacks */
578 static int hdmi_audio_startup(struct device *dev,
579 void (*abort_cb)(struct device *dev))
581 struct omap_hdmi *hd = dev_get_drvdata(dev);
584 mutex_lock(&hd->lock);
586 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
591 hd->audio_abort_cb = abort_cb;
594 mutex_unlock(&hd->lock);
599 static int hdmi_audio_shutdown(struct device *dev)
601 struct omap_hdmi *hd = dev_get_drvdata(dev);
603 mutex_lock(&hd->lock);
604 hd->audio_abort_cb = NULL;
605 hd->audio_configured = false;
606 hd->audio_playing = false;
607 mutex_unlock(&hd->lock);
612 static int hdmi_audio_start(struct device *dev)
614 struct omap_hdmi *hd = dev_get_drvdata(dev);
617 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
619 spin_lock_irqsave(&hd->audio_playing_lock, flags);
621 if (hd->display_enabled)
622 hdmi_start_audio_stream(hd);
623 hd->audio_playing = true;
625 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
629 static void hdmi_audio_stop(struct device *dev)
631 struct omap_hdmi *hd = dev_get_drvdata(dev);
634 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
636 spin_lock_irqsave(&hd->audio_playing_lock, flags);
638 if (hd->display_enabled)
639 hdmi_stop_audio_stream(hd);
640 hd->audio_playing = false;
642 spin_unlock_irqrestore(&hd->audio_playing_lock, flags);
645 static int hdmi_audio_config(struct device *dev,
646 struct omap_dss_audio *dss_audio)
648 struct omap_hdmi *hd = dev_get_drvdata(dev);
651 mutex_lock(&hd->lock);
653 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
658 ret = hdmi4_audio_config(&hd->core, &hd->wp, dss_audio,
659 hd->cfg.timings.pixelclock);
661 hd->audio_configured = true;
662 hd->audio_config = *dss_audio;
665 mutex_unlock(&hd->lock);
670 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
671 .audio_startup = hdmi_audio_startup,
672 .audio_shutdown = hdmi_audio_shutdown,
673 .audio_start = hdmi_audio_start,
674 .audio_stop = hdmi_audio_stop,
675 .audio_config = hdmi_audio_config,
678 static int hdmi_audio_register(struct device *dev)
680 struct omap_hdmi_audio_pdata pdata = {
682 .dss_version = omapdss_get_version(),
683 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi.wp),
684 .ops = &hdmi_audio_ops,
687 hdmi.audio_pdev = platform_device_register_data(
688 dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
689 &pdata, sizeof(pdata));
691 if (IS_ERR(hdmi.audio_pdev))
692 return PTR_ERR(hdmi.audio_pdev);
697 /* HDMI HW IP initialisation */
698 static int hdmi4_bind(struct device *dev, struct device *master, void *data)
700 struct platform_device *pdev = to_platform_device(dev);
705 dev_set_drvdata(&pdev->dev, &hdmi);
707 mutex_init(&hdmi.lock);
708 spin_lock_init(&hdmi.audio_playing_lock);
710 if (pdev->dev.of_node) {
711 r = hdmi_probe_of(pdev);
716 r = hdmi_wp_init(pdev, &hdmi.wp);
720 r = hdmi_pll_init(pdev, &hdmi.pll, &hdmi.wp);
724 r = hdmi_phy_init(pdev, &hdmi.phy);
728 r = hdmi4_core_init(pdev, &hdmi.core);
732 irq = platform_get_irq(pdev, 0);
734 DSSERR("platform_get_irq failed\n");
739 r = devm_request_threaded_irq(&pdev->dev, irq,
740 NULL, hdmi_irq_handler,
741 IRQF_ONESHOT, "OMAP HDMI", &hdmi.wp);
743 DSSERR("HDMI IRQ request failed\n");
747 pm_runtime_enable(&pdev->dev);
749 hdmi_init_output(pdev);
751 r = hdmi_audio_register(&pdev->dev);
753 DSSERR("Registering HDMI audio failed\n");
754 hdmi_uninit_output(pdev);
755 pm_runtime_disable(&pdev->dev);
759 dss_debugfs_create_file("hdmi", hdmi_dump_regs);
763 hdmi_pll_uninit(&hdmi.pll);
767 static void hdmi4_unbind(struct device *dev, struct device *master, void *data)
769 struct platform_device *pdev = to_platform_device(dev);
772 platform_device_unregister(hdmi.audio_pdev);
774 hdmi_uninit_output(pdev);
776 hdmi_pll_uninit(&hdmi.pll);
778 pm_runtime_disable(&pdev->dev);
781 static const struct component_ops hdmi4_component_ops = {
783 .unbind = hdmi4_unbind,
786 static int hdmi4_probe(struct platform_device *pdev)
788 return component_add(&pdev->dev, &hdmi4_component_ops);
791 static int hdmi4_remove(struct platform_device *pdev)
793 component_del(&pdev->dev, &hdmi4_component_ops);
797 static int hdmi_runtime_suspend(struct device *dev)
804 static int hdmi_runtime_resume(struct device *dev)
808 r = dispc_runtime_get();
815 static const struct dev_pm_ops hdmi_pm_ops = {
816 .runtime_suspend = hdmi_runtime_suspend,
817 .runtime_resume = hdmi_runtime_resume,
820 static const struct of_device_id hdmi_of_match[] = {
821 { .compatible = "ti,omap4-hdmi", },
825 static struct platform_driver omapdss_hdmihw_driver = {
826 .probe = hdmi4_probe,
827 .remove = hdmi4_remove,
829 .name = "omapdss_hdmi",
831 .of_match_table = hdmi_of_match,
832 .suppress_bind_attrs = true,
836 int __init hdmi4_init_platform_driver(void)
838 return platform_driver_register(&omapdss_hdmihw_driver);
841 void hdmi4_uninit_platform_driver(void)
843 platform_driver_unregister(&omapdss_hdmihw_driver);