cxl: Fix typo in debug print
[linux/fpc-iii.git] / drivers / video / fbdev / omap2 / dss / hdmi4.c
blob916d47978f4118f95e76b337e2fb1df8d0298f1c
1 /*
2 * HDMI interface DSS driver for TI's OMAP4 family of SoCs.
3 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com/
4 * Authors: Yong Zhi
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
14 * more details.
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>
25 #include <linux/io.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 <video/omapdss.h>
36 #include <sound/omap-hdmi-audio.h>
38 #include "hdmi4_core.h"
39 #include "dss.h"
40 #include "dss_features.h"
41 #include "hdmi.h"
43 static struct omap_hdmi hdmi;
45 static int hdmi_runtime_get(void)
47 int r;
49 DSSDBG("hdmi_runtime_get\n");
51 r = pm_runtime_get_sync(&hdmi.pdev->dev);
52 WARN_ON(r < 0);
53 if (r < 0)
54 return r;
56 return 0;
59 static void hdmi_runtime_put(void)
61 int r;
63 DSSDBG("hdmi_runtime_put\n");
65 r = pm_runtime_put_sync(&hdmi.pdev->dev);
66 WARN_ON(r < 0 && r != -ENOSYS);
69 static irqreturn_t hdmi_irq_handler(int irq, void *data)
71 struct hdmi_wp_data *wp = data;
72 u32 irqstatus;
74 irqstatus = hdmi_wp_get_irqstatus(wp);
75 hdmi_wp_set_irqstatus(wp, irqstatus);
77 if ((irqstatus & HDMI_IRQ_LINK_CONNECT) &&
78 irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
80 * If we get both connect and disconnect interrupts at the same
81 * time, turn off the PHY, clear interrupts, and restart, which
82 * raises connect interrupt if a cable is connected, or nothing
83 * if cable is not connected.
85 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_OFF);
87 hdmi_wp_set_irqstatus(wp, HDMI_IRQ_LINK_CONNECT |
88 HDMI_IRQ_LINK_DISCONNECT);
90 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
91 } else if (irqstatus & HDMI_IRQ_LINK_CONNECT) {
92 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_TXON);
93 } else if (irqstatus & HDMI_IRQ_LINK_DISCONNECT) {
94 hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
97 return IRQ_HANDLED;
100 static int hdmi_init_regulator(void)
102 int r;
103 struct regulator *reg;
105 if (hdmi.vdda_reg != NULL)
106 return 0;
108 reg = devm_regulator_get(&hdmi.pdev->dev, "vdda");
110 if (IS_ERR(reg)) {
111 if (PTR_ERR(reg) != -EPROBE_DEFER)
112 DSSERR("can't get VDDA regulator\n");
113 return PTR_ERR(reg);
116 if (regulator_can_change_voltage(reg)) {
117 r = regulator_set_voltage(reg, 1800000, 1800000);
118 if (r) {
119 devm_regulator_put(reg);
120 DSSWARN("can't set the regulator voltage\n");
121 return r;
125 hdmi.vdda_reg = reg;
127 return 0;
130 static int hdmi_power_on_core(struct omap_dss_device *dssdev)
132 int r;
134 r = regulator_enable(hdmi.vdda_reg);
135 if (r)
136 return r;
138 r = hdmi_runtime_get();
139 if (r)
140 goto err_runtime_get;
142 /* Make selection of HDMI in DSS */
143 dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK);
145 hdmi.core_enabled = true;
147 return 0;
149 err_runtime_get:
150 regulator_disable(hdmi.vdda_reg);
152 return r;
155 static void hdmi_power_off_core(struct omap_dss_device *dssdev)
157 hdmi.core_enabled = false;
159 hdmi_runtime_put();
160 regulator_disable(hdmi.vdda_reg);
163 static int hdmi_power_on_full(struct omap_dss_device *dssdev)
165 int r;
166 struct omap_video_timings *p;
167 struct omap_overlay_manager *mgr = hdmi.output.manager;
168 struct hdmi_wp_data *wp = &hdmi.wp;
169 struct dss_pll_clock_info hdmi_cinfo = { 0 };
171 r = hdmi_power_on_core(dssdev);
172 if (r)
173 return r;
175 /* disable and clear irqs */
176 hdmi_wp_clear_irqenable(wp, 0xffffffff);
177 hdmi_wp_set_irqstatus(wp, 0xffffffff);
179 p = &hdmi.cfg.timings;
181 DSSDBG("hdmi_power_on x_res= %d y_res = %d\n", p->x_res, p->y_res);
183 hdmi_pll_compute(&hdmi.pll, p->pixelclock, &hdmi_cinfo);
185 r = dss_pll_enable(&hdmi.pll.pll);
186 if (r) {
187 DSSERR("Failed to enable PLL\n");
188 goto err_pll_enable;
191 r = dss_pll_set_config(&hdmi.pll.pll, &hdmi_cinfo);
192 if (r) {
193 DSSERR("Failed to configure PLL\n");
194 goto err_pll_cfg;
197 r = hdmi_phy_configure(&hdmi.phy, hdmi_cinfo.clkdco,
198 hdmi_cinfo.clkout[0]);
199 if (r) {
200 DSSDBG("Failed to configure PHY\n");
201 goto err_phy_cfg;
204 r = hdmi_wp_set_phy_pwr(wp, HDMI_PHYPWRCMD_LDOON);
205 if (r)
206 goto err_phy_pwr;
208 hdmi4_configure(&hdmi.core, &hdmi.wp, &hdmi.cfg);
210 /* bypass TV gamma table */
211 dispc_enable_gamma_table(0);
213 /* tv size */
214 dss_mgr_set_timings(mgr, p);
216 r = hdmi_wp_video_start(&hdmi.wp);
217 if (r)
218 goto err_vid_enable;
220 r = dss_mgr_enable(mgr);
221 if (r)
222 goto err_mgr_enable;
224 hdmi_wp_set_irqenable(wp,
225 HDMI_IRQ_LINK_CONNECT | HDMI_IRQ_LINK_DISCONNECT);
227 return 0;
229 err_mgr_enable:
230 hdmi_wp_video_stop(&hdmi.wp);
231 err_vid_enable:
232 err_phy_cfg:
233 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
234 err_phy_pwr:
235 err_pll_cfg:
236 dss_pll_disable(&hdmi.pll.pll);
237 err_pll_enable:
238 hdmi_power_off_core(dssdev);
239 return -EIO;
242 static void hdmi_power_off_full(struct omap_dss_device *dssdev)
244 struct omap_overlay_manager *mgr = hdmi.output.manager;
246 hdmi_wp_clear_irqenable(&hdmi.wp, 0xffffffff);
248 dss_mgr_disable(mgr);
250 hdmi_wp_video_stop(&hdmi.wp);
252 hdmi_wp_set_phy_pwr(&hdmi.wp, HDMI_PHYPWRCMD_OFF);
254 dss_pll_disable(&hdmi.pll.pll);
256 hdmi_power_off_core(dssdev);
259 static int hdmi_display_check_timing(struct omap_dss_device *dssdev,
260 struct omap_video_timings *timings)
262 struct omap_dss_device *out = &hdmi.output;
264 if (!dispc_mgr_timings_ok(out->dispc_channel, timings))
265 return -EINVAL;
267 return 0;
270 static void hdmi_display_set_timing(struct omap_dss_device *dssdev,
271 struct omap_video_timings *timings)
273 mutex_lock(&hdmi.lock);
275 hdmi.cfg.timings = *timings;
277 dispc_set_tv_pclk(timings->pixelclock);
279 mutex_unlock(&hdmi.lock);
282 static void hdmi_display_get_timings(struct omap_dss_device *dssdev,
283 struct omap_video_timings *timings)
285 *timings = hdmi.cfg.timings;
288 static void hdmi_dump_regs(struct seq_file *s)
290 mutex_lock(&hdmi.lock);
292 if (hdmi_runtime_get()) {
293 mutex_unlock(&hdmi.lock);
294 return;
297 hdmi_wp_dump(&hdmi.wp, s);
298 hdmi_pll_dump(&hdmi.pll, s);
299 hdmi_phy_dump(&hdmi.phy, s);
300 hdmi4_core_dump(&hdmi.core, s);
302 hdmi_runtime_put();
303 mutex_unlock(&hdmi.lock);
306 static int read_edid(u8 *buf, int len)
308 int r;
310 mutex_lock(&hdmi.lock);
312 r = hdmi_runtime_get();
313 BUG_ON(r);
315 r = hdmi4_read_edid(&hdmi.core, buf, len);
317 hdmi_runtime_put();
318 mutex_unlock(&hdmi.lock);
320 return r;
323 static int hdmi_display_enable(struct omap_dss_device *dssdev)
325 struct omap_dss_device *out = &hdmi.output;
326 int r = 0;
328 DSSDBG("ENTER hdmi_display_enable\n");
330 mutex_lock(&hdmi.lock);
332 if (out == NULL || out->manager == NULL) {
333 DSSERR("failed to enable display: no output/manager\n");
334 r = -ENODEV;
335 goto err0;
338 r = hdmi_power_on_full(dssdev);
339 if (r) {
340 DSSERR("failed to power on device\n");
341 goto err0;
344 hdmi.display_enabled = true;
346 mutex_unlock(&hdmi.lock);
347 return 0;
349 err0:
350 mutex_unlock(&hdmi.lock);
351 return r;
354 static void hdmi_display_disable(struct omap_dss_device *dssdev)
356 DSSDBG("Enter hdmi_display_disable\n");
358 mutex_lock(&hdmi.lock);
360 if (hdmi.audio_pdev && hdmi.audio_abort_cb)
361 hdmi.audio_abort_cb(&hdmi.audio_pdev->dev);
363 hdmi_power_off_full(dssdev);
365 hdmi.display_enabled = false;
367 mutex_unlock(&hdmi.lock);
370 static int hdmi_core_enable(struct omap_dss_device *dssdev)
372 int r = 0;
374 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
376 mutex_lock(&hdmi.lock);
378 r = hdmi_power_on_core(dssdev);
379 if (r) {
380 DSSERR("failed to power on device\n");
381 goto err0;
384 mutex_unlock(&hdmi.lock);
385 return 0;
387 err0:
388 mutex_unlock(&hdmi.lock);
389 return r;
392 static void hdmi_core_disable(struct omap_dss_device *dssdev)
394 DSSDBG("Enter omapdss_hdmi_core_disable\n");
396 mutex_lock(&hdmi.lock);
398 hdmi_power_off_core(dssdev);
400 mutex_unlock(&hdmi.lock);
403 static int hdmi_connect(struct omap_dss_device *dssdev,
404 struct omap_dss_device *dst)
406 struct omap_overlay_manager *mgr;
407 int r;
409 r = hdmi_init_regulator();
410 if (r)
411 return r;
413 mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
414 if (!mgr)
415 return -ENODEV;
417 r = dss_mgr_connect(mgr, dssdev);
418 if (r)
419 return r;
421 r = omapdss_output_set_device(dssdev, dst);
422 if (r) {
423 DSSERR("failed to connect output to new device: %s\n",
424 dst->name);
425 dss_mgr_disconnect(mgr, dssdev);
426 return r;
429 return 0;
432 static void hdmi_disconnect(struct omap_dss_device *dssdev,
433 struct omap_dss_device *dst)
435 WARN_ON(dst != dssdev->dst);
437 if (dst != dssdev->dst)
438 return;
440 omapdss_output_unset_device(dssdev);
442 if (dssdev->manager)
443 dss_mgr_disconnect(dssdev->manager, dssdev);
446 static int hdmi_read_edid(struct omap_dss_device *dssdev,
447 u8 *edid, int len)
449 bool need_enable;
450 int r;
452 need_enable = hdmi.core_enabled == false;
454 if (need_enable) {
455 r = hdmi_core_enable(dssdev);
456 if (r)
457 return r;
460 r = read_edid(edid, len);
462 if (need_enable)
463 hdmi_core_disable(dssdev);
465 return r;
468 static int hdmi_set_infoframe(struct omap_dss_device *dssdev,
469 const struct hdmi_avi_infoframe *avi)
471 hdmi.cfg.infoframe = *avi;
472 return 0;
475 static int hdmi_set_hdmi_mode(struct omap_dss_device *dssdev,
476 bool hdmi_mode)
478 hdmi.cfg.hdmi_dvi_mode = hdmi_mode ? HDMI_HDMI : HDMI_DVI;
479 return 0;
482 static const struct omapdss_hdmi_ops hdmi_ops = {
483 .connect = hdmi_connect,
484 .disconnect = hdmi_disconnect,
486 .enable = hdmi_display_enable,
487 .disable = hdmi_display_disable,
489 .check_timings = hdmi_display_check_timing,
490 .set_timings = hdmi_display_set_timing,
491 .get_timings = hdmi_display_get_timings,
493 .read_edid = hdmi_read_edid,
494 .set_infoframe = hdmi_set_infoframe,
495 .set_hdmi_mode = hdmi_set_hdmi_mode,
498 static void hdmi_init_output(struct platform_device *pdev)
500 struct omap_dss_device *out = &hdmi.output;
502 out->dev = &pdev->dev;
503 out->id = OMAP_DSS_OUTPUT_HDMI;
504 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
505 out->name = "hdmi.0";
506 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
507 out->ops.hdmi = &hdmi_ops;
508 out->owner = THIS_MODULE;
510 omapdss_register_output(out);
513 static void hdmi_uninit_output(struct platform_device *pdev)
515 struct omap_dss_device *out = &hdmi.output;
517 omapdss_unregister_output(out);
520 static int hdmi_probe_of(struct platform_device *pdev)
522 struct device_node *node = pdev->dev.of_node;
523 struct device_node *ep;
524 int r;
526 ep = omapdss_of_get_first_endpoint(node);
527 if (!ep)
528 return 0;
530 r = hdmi_parse_lanes_of(pdev, ep, &hdmi.phy);
531 if (r)
532 goto err;
534 of_node_put(ep);
535 return 0;
537 err:
538 of_node_put(ep);
539 return r;
542 /* Audio callbacks */
543 static int hdmi_audio_startup(struct device *dev,
544 void (*abort_cb)(struct device *dev))
546 struct omap_hdmi *hd = dev_get_drvdata(dev);
547 int ret = 0;
549 mutex_lock(&hd->lock);
551 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
552 ret = -EPERM;
553 goto out;
556 hd->audio_abort_cb = abort_cb;
558 out:
559 mutex_unlock(&hd->lock);
561 return ret;
564 static int hdmi_audio_shutdown(struct device *dev)
566 struct omap_hdmi *hd = dev_get_drvdata(dev);
568 mutex_lock(&hd->lock);
569 hd->audio_abort_cb = NULL;
570 mutex_unlock(&hd->lock);
572 return 0;
575 static int hdmi_audio_start(struct device *dev)
577 struct omap_hdmi *hd = dev_get_drvdata(dev);
579 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
580 WARN_ON(!hd->display_enabled);
582 hdmi_wp_audio_enable(&hd->wp, true);
583 hdmi4_audio_start(&hd->core, &hd->wp);
585 return 0;
588 static void hdmi_audio_stop(struct device *dev)
590 struct omap_hdmi *hd = dev_get_drvdata(dev);
592 WARN_ON(!hdmi_mode_has_audio(&hd->cfg));
593 WARN_ON(!hd->display_enabled);
595 hdmi4_audio_stop(&hd->core, &hd->wp);
596 hdmi_wp_audio_enable(&hd->wp, false);
599 static int hdmi_audio_config(struct device *dev,
600 struct omap_dss_audio *dss_audio)
602 struct omap_hdmi *hd = dev_get_drvdata(dev);
603 int ret;
605 mutex_lock(&hd->lock);
607 if (!hdmi_mode_has_audio(&hd->cfg) || !hd->display_enabled) {
608 ret = -EPERM;
609 goto out;
612 ret = hdmi4_audio_config(&hd->core, &hd->wp, dss_audio,
613 hd->cfg.timings.pixelclock);
615 out:
616 mutex_unlock(&hd->lock);
618 return ret;
621 static const struct omap_hdmi_audio_ops hdmi_audio_ops = {
622 .audio_startup = hdmi_audio_startup,
623 .audio_shutdown = hdmi_audio_shutdown,
624 .audio_start = hdmi_audio_start,
625 .audio_stop = hdmi_audio_stop,
626 .audio_config = hdmi_audio_config,
629 static int hdmi_audio_register(struct device *dev)
631 struct omap_hdmi_audio_pdata pdata = {
632 .dev = dev,
633 .dss_version = omapdss_get_version(),
634 .audio_dma_addr = hdmi_wp_get_audio_dma_addr(&hdmi.wp),
635 .ops = &hdmi_audio_ops,
638 hdmi.audio_pdev = platform_device_register_data(
639 dev, "omap-hdmi-audio", PLATFORM_DEVID_AUTO,
640 &pdata, sizeof(pdata));
642 if (IS_ERR(hdmi.audio_pdev))
643 return PTR_ERR(hdmi.audio_pdev);
645 return 0;
648 /* HDMI HW IP initialisation */
649 static int omapdss_hdmihw_probe(struct platform_device *pdev)
651 int r;
652 int irq;
654 hdmi.pdev = pdev;
655 dev_set_drvdata(&pdev->dev, &hdmi);
657 mutex_init(&hdmi.lock);
659 if (pdev->dev.of_node) {
660 r = hdmi_probe_of(pdev);
661 if (r)
662 return r;
665 r = hdmi_wp_init(pdev, &hdmi.wp);
666 if (r)
667 return r;
669 r = hdmi_pll_init(pdev, &hdmi.pll, &hdmi.wp);
670 if (r)
671 return r;
673 r = hdmi_phy_init(pdev, &hdmi.phy);
674 if (r)
675 goto err;
677 r = hdmi4_core_init(pdev, &hdmi.core);
678 if (r)
679 goto err;
681 irq = platform_get_irq(pdev, 0);
682 if (irq < 0) {
683 DSSERR("platform_get_irq failed\n");
684 r = -ENODEV;
685 goto err;
688 r = devm_request_threaded_irq(&pdev->dev, irq,
689 NULL, hdmi_irq_handler,
690 IRQF_ONESHOT, "OMAP HDMI", &hdmi.wp);
691 if (r) {
692 DSSERR("HDMI IRQ request failed\n");
693 goto err;
696 pm_runtime_enable(&pdev->dev);
698 hdmi_init_output(pdev);
700 r = hdmi_audio_register(&pdev->dev);
701 if (r) {
702 DSSERR("Registering HDMI audio failed\n");
703 hdmi_uninit_output(pdev);
704 pm_runtime_disable(&pdev->dev);
705 return r;
708 dss_debugfs_create_file("hdmi", hdmi_dump_regs);
710 return 0;
711 err:
712 hdmi_pll_uninit(&hdmi.pll);
713 return r;
716 static int __exit omapdss_hdmihw_remove(struct platform_device *pdev)
718 if (hdmi.audio_pdev)
719 platform_device_unregister(hdmi.audio_pdev);
721 hdmi_uninit_output(pdev);
723 hdmi_pll_uninit(&hdmi.pll);
725 pm_runtime_disable(&pdev->dev);
727 return 0;
730 static int hdmi_runtime_suspend(struct device *dev)
732 dispc_runtime_put();
734 return 0;
737 static int hdmi_runtime_resume(struct device *dev)
739 int r;
741 r = dispc_runtime_get();
742 if (r < 0)
743 return r;
745 return 0;
748 static const struct dev_pm_ops hdmi_pm_ops = {
749 .runtime_suspend = hdmi_runtime_suspend,
750 .runtime_resume = hdmi_runtime_resume,
753 static const struct of_device_id hdmi_of_match[] = {
754 { .compatible = "ti,omap4-hdmi", },
758 static struct platform_driver omapdss_hdmihw_driver = {
759 .probe = omapdss_hdmihw_probe,
760 .remove = __exit_p(omapdss_hdmihw_remove),
761 .driver = {
762 .name = "omapdss_hdmi",
763 .pm = &hdmi_pm_ops,
764 .of_match_table = hdmi_of_match,
765 .suppress_bind_attrs = true,
769 int __init hdmi4_init_platform_driver(void)
771 return platform_driver_register(&omapdss_hdmihw_driver);
774 void __exit hdmi4_uninit_platform_driver(void)
776 platform_driver_unregister(&omapdss_hdmihw_driver);