fix a kmap leak in virtio_console
[linux/fpc-iii.git] / drivers / video / omap2 / dss / hdmi4.c
blob4a74538f9ea5ea87b8a0f49149700662c103c01a
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>
37 #include "hdmi4_core.h"
38 #include "dss.h"
39 #include "dss_features.h"
41 static struct {
42 struct mutex lock;
43 struct platform_device *pdev;
45 struct hdmi_wp_data wp;
46 struct hdmi_pll_data pll;
47 struct hdmi_phy_data phy;
48 struct hdmi_core_data core;
50 struct hdmi_config cfg;
52 struct clk *sys_clk;
53 struct regulator *vdda_hdmi_dac_reg;
55 bool core_enabled;
57 struct omap_dss_device output;
58 } hdmi;
60 static int hdmi_runtime_get(void)
62 int r;
64 DSSDBG("hdmi_runtime_get\n");
66 r = pm_runtime_get_sync(&hdmi.pdev->dev);
67 WARN_ON(r < 0);
68 if (r < 0)
69 return r;
71 return 0;
74 static void hdmi_runtime_put(void)
76 int r;
78 DSSDBG("hdmi_runtime_put\n");
80 r = pm_runtime_put_sync(&hdmi.pdev->dev);
81 WARN_ON(r < 0 && r != -ENOSYS);
84 static int hdmi_init_regulator(void)
86 struct regulator *reg;
88 if (hdmi.vdda_hdmi_dac_reg != NULL)
89 return 0;
91 reg = devm_regulator_get(&hdmi.pdev->dev, "vdda_hdmi_dac");
93 /* DT HACK: try VDAC to make omapdss work for o4 sdp/panda */
94 if (IS_ERR(reg))
95 reg = devm_regulator_get(&hdmi.pdev->dev, "VDAC");
97 if (IS_ERR(reg)) {
98 if (PTR_ERR(reg) != -EPROBE_DEFER)
99 DSSERR("can't get VDDA_HDMI_DAC regulator\n");
100 return PTR_ERR(reg);
103 hdmi.vdda_hdmi_dac_reg = reg;
105 return 0;
108 static int hdmi_power_on_core(struct omap_dss_device *dssdev)
110 int r;
112 r = regulator_enable(hdmi.vdda_hdmi_dac_reg);
113 if (r)
114 return r;
116 r = hdmi_runtime_get();
117 if (r)
118 goto err_runtime_get;
120 /* Make selection of HDMI in DSS */
121 dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK);
123 hdmi.core_enabled = true;
125 return 0;
127 err_runtime_get:
128 regulator_disable(hdmi.vdda_hdmi_dac_reg);
130 return r;
133 static void hdmi_power_off_core(struct omap_dss_device *dssdev)
135 hdmi.core_enabled = false;
137 hdmi_runtime_put();
138 regulator_disable(hdmi.vdda_hdmi_dac_reg);
141 static int hdmi_power_on_full(struct omap_dss_device *dssdev)
143 int r;
144 struct omap_video_timings *p;
145 struct omap_overlay_manager *mgr = hdmi.output.manager;
146 unsigned long phy;
148 r = hdmi_power_on_core(dssdev);
149 if (r)
150 return r;
152 p = &hdmi.cfg.timings;
154 DSSDBG("hdmi_power_on x_res= %d y_res = %d\n", p->x_res, p->y_res);
156 phy = p->pixel_clock;
158 hdmi_pll_compute(&hdmi.pll, clk_get_rate(hdmi.sys_clk), phy);
160 /* config the PLL and PHY hdmi_set_pll_pwrfirst */
161 r = hdmi_pll_enable(&hdmi.pll, &hdmi.wp);
162 if (r) {
163 DSSDBG("Failed to lock PLL\n");
164 goto err_pll_enable;
167 r = hdmi_phy_enable(&hdmi.phy, &hdmi.wp, &hdmi.cfg);
168 if (r) {
169 DSSDBG("Failed to start PHY\n");
170 goto err_phy_enable;
173 hdmi4_configure(&hdmi.core, &hdmi.wp, &hdmi.cfg);
175 /* bypass TV gamma table */
176 dispc_enable_gamma_table(0);
178 /* tv size */
179 dss_mgr_set_timings(mgr, p);
181 r = hdmi_wp_video_start(&hdmi.wp);
182 if (r)
183 goto err_vid_enable;
185 r = dss_mgr_enable(mgr);
186 if (r)
187 goto err_mgr_enable;
189 return 0;
191 err_mgr_enable:
192 hdmi_wp_video_stop(&hdmi.wp);
193 err_vid_enable:
194 hdmi_phy_disable(&hdmi.phy, &hdmi.wp);
195 err_phy_enable:
196 hdmi_pll_disable(&hdmi.pll, &hdmi.wp);
197 err_pll_enable:
198 hdmi_power_off_core(dssdev);
199 return -EIO;
202 static void hdmi_power_off_full(struct omap_dss_device *dssdev)
204 struct omap_overlay_manager *mgr = hdmi.output.manager;
206 dss_mgr_disable(mgr);
208 hdmi_wp_video_stop(&hdmi.wp);
209 hdmi_phy_disable(&hdmi.phy, &hdmi.wp);
210 hdmi_pll_disable(&hdmi.pll, &hdmi.wp);
212 hdmi_power_off_core(dssdev);
215 static int hdmi_display_check_timing(struct omap_dss_device *dssdev,
216 struct omap_video_timings *timings)
218 struct omap_dss_device *out = &hdmi.output;
220 if (!dispc_mgr_timings_ok(out->dispc_channel, timings))
221 return -EINVAL;
223 return 0;
226 static void hdmi_display_set_timing(struct omap_dss_device *dssdev,
227 struct omap_video_timings *timings)
229 struct hdmi_cm cm;
230 const struct hdmi_config *t;
232 mutex_lock(&hdmi.lock);
234 cm = hdmi_get_code(timings);
235 hdmi.cfg.cm = cm;
237 t = hdmi_get_timings(cm.mode, cm.code);
238 if (t != NULL) {
239 hdmi.cfg = *t;
241 dispc_set_tv_pclk(t->timings.pixel_clock * 1000);
242 } else {
243 hdmi.cfg.timings = *timings;
244 hdmi.cfg.cm.code = 0;
245 hdmi.cfg.cm.mode = HDMI_DVI;
247 dispc_set_tv_pclk(timings->pixel_clock * 1000);
250 DSSDBG("using mode: %s, code %d\n", hdmi.cfg.cm.mode == HDMI_DVI ?
251 "DVI" : "HDMI", hdmi.cfg.cm.code);
253 mutex_unlock(&hdmi.lock);
256 static void hdmi_display_get_timings(struct omap_dss_device *dssdev,
257 struct omap_video_timings *timings)
259 const struct hdmi_config *cfg;
260 struct hdmi_cm cm = hdmi.cfg.cm;
262 cfg = hdmi_get_timings(cm.mode, cm.code);
263 if (cfg == NULL)
264 cfg = hdmi_default_timing();
266 memcpy(timings, &cfg->timings, sizeof(cfg->timings));
269 static void hdmi_dump_regs(struct seq_file *s)
271 mutex_lock(&hdmi.lock);
273 if (hdmi_runtime_get()) {
274 mutex_unlock(&hdmi.lock);
275 return;
278 hdmi_wp_dump(&hdmi.wp, s);
279 hdmi_pll_dump(&hdmi.pll, s);
280 hdmi_phy_dump(&hdmi.phy, s);
281 hdmi4_core_dump(&hdmi.core, s);
283 hdmi_runtime_put();
284 mutex_unlock(&hdmi.lock);
287 static int read_edid(u8 *buf, int len)
289 int r;
291 mutex_lock(&hdmi.lock);
293 r = hdmi_runtime_get();
294 BUG_ON(r);
296 r = hdmi4_read_edid(&hdmi.core, buf, len);
298 hdmi_runtime_put();
299 mutex_unlock(&hdmi.lock);
301 return r;
304 static int hdmi_display_enable(struct omap_dss_device *dssdev)
306 struct omap_dss_device *out = &hdmi.output;
307 int r = 0;
309 DSSDBG("ENTER hdmi_display_enable\n");
311 mutex_lock(&hdmi.lock);
313 if (out == NULL || out->manager == NULL) {
314 DSSERR("failed to enable display: no output/manager\n");
315 r = -ENODEV;
316 goto err0;
319 r = hdmi_power_on_full(dssdev);
320 if (r) {
321 DSSERR("failed to power on device\n");
322 goto err0;
325 mutex_unlock(&hdmi.lock);
326 return 0;
328 err0:
329 mutex_unlock(&hdmi.lock);
330 return r;
333 static void hdmi_display_disable(struct omap_dss_device *dssdev)
335 DSSDBG("Enter hdmi_display_disable\n");
337 mutex_lock(&hdmi.lock);
339 hdmi_power_off_full(dssdev);
341 mutex_unlock(&hdmi.lock);
344 static int hdmi_core_enable(struct omap_dss_device *dssdev)
346 int r = 0;
348 DSSDBG("ENTER omapdss_hdmi_core_enable\n");
350 mutex_lock(&hdmi.lock);
352 r = hdmi_power_on_core(dssdev);
353 if (r) {
354 DSSERR("failed to power on device\n");
355 goto err0;
358 mutex_unlock(&hdmi.lock);
359 return 0;
361 err0:
362 mutex_unlock(&hdmi.lock);
363 return r;
366 static void hdmi_core_disable(struct omap_dss_device *dssdev)
368 DSSDBG("Enter omapdss_hdmi_core_disable\n");
370 mutex_lock(&hdmi.lock);
372 hdmi_power_off_core(dssdev);
374 mutex_unlock(&hdmi.lock);
377 static int hdmi_get_clocks(struct platform_device *pdev)
379 struct clk *clk;
381 clk = devm_clk_get(&pdev->dev, "sys_clk");
382 if (IS_ERR(clk)) {
383 DSSERR("can't get sys_clk\n");
384 return PTR_ERR(clk);
387 hdmi.sys_clk = clk;
389 return 0;
392 static int hdmi_connect(struct omap_dss_device *dssdev,
393 struct omap_dss_device *dst)
395 struct omap_overlay_manager *mgr;
396 int r;
398 r = hdmi_init_regulator();
399 if (r)
400 return r;
402 mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
403 if (!mgr)
404 return -ENODEV;
406 r = dss_mgr_connect(mgr, dssdev);
407 if (r)
408 return r;
410 r = omapdss_output_set_device(dssdev, dst);
411 if (r) {
412 DSSERR("failed to connect output to new device: %s\n",
413 dst->name);
414 dss_mgr_disconnect(mgr, dssdev);
415 return r;
418 return 0;
421 static void hdmi_disconnect(struct omap_dss_device *dssdev,
422 struct omap_dss_device *dst)
424 WARN_ON(dst != dssdev->dst);
426 if (dst != dssdev->dst)
427 return;
429 omapdss_output_unset_device(dssdev);
431 if (dssdev->manager)
432 dss_mgr_disconnect(dssdev->manager, dssdev);
435 static int hdmi_read_edid(struct omap_dss_device *dssdev,
436 u8 *edid, int len)
438 bool need_enable;
439 int r;
441 need_enable = hdmi.core_enabled == false;
443 if (need_enable) {
444 r = hdmi_core_enable(dssdev);
445 if (r)
446 return r;
449 r = read_edid(edid, len);
451 if (need_enable)
452 hdmi_core_disable(dssdev);
454 return r;
457 #if defined(CONFIG_OMAP4_DSS_HDMI_AUDIO)
458 static int hdmi_audio_enable(struct omap_dss_device *dssdev)
460 int r;
462 mutex_lock(&hdmi.lock);
464 if (!hdmi_mode_has_audio(hdmi.cfg.cm.mode)) {
465 r = -EPERM;
466 goto err;
469 r = hdmi_wp_audio_enable(&hdmi.wp, true);
470 if (r)
471 goto err;
473 mutex_unlock(&hdmi.lock);
474 return 0;
476 err:
477 mutex_unlock(&hdmi.lock);
478 return r;
481 static void hdmi_audio_disable(struct omap_dss_device *dssdev)
483 hdmi_wp_audio_enable(&hdmi.wp, false);
486 static int hdmi_audio_start(struct omap_dss_device *dssdev)
488 return hdmi4_audio_start(&hdmi.core, &hdmi.wp);
491 static void hdmi_audio_stop(struct omap_dss_device *dssdev)
493 hdmi4_audio_stop(&hdmi.core, &hdmi.wp);
496 static bool hdmi_audio_supported(struct omap_dss_device *dssdev)
498 bool r;
500 mutex_lock(&hdmi.lock);
502 r = hdmi_mode_has_audio(hdmi.cfg.cm.mode);
504 mutex_unlock(&hdmi.lock);
505 return r;
508 static int hdmi_audio_config(struct omap_dss_device *dssdev,
509 struct omap_dss_audio *audio)
511 int r;
512 u32 pclk = hdmi.cfg.timings.pixel_clock;
514 mutex_lock(&hdmi.lock);
516 if (!hdmi_mode_has_audio(hdmi.cfg.cm.mode)) {
517 r = -EPERM;
518 goto err;
521 r = hdmi4_audio_config(&hdmi.core, &hdmi.wp, audio, pclk);
522 if (r)
523 goto err;
525 mutex_unlock(&hdmi.lock);
526 return 0;
528 err:
529 mutex_unlock(&hdmi.lock);
530 return r;
532 #else
533 static int hdmi_audio_enable(struct omap_dss_device *dssdev)
535 return -EPERM;
538 static void hdmi_audio_disable(struct omap_dss_device *dssdev)
542 static int hdmi_audio_start(struct omap_dss_device *dssdev)
544 return -EPERM;
547 static void hdmi_audio_stop(struct omap_dss_device *dssdev)
551 static bool hdmi_audio_supported(struct omap_dss_device *dssdev)
553 return false;
556 static int hdmi_audio_config(struct omap_dss_device *dssdev,
557 struct omap_dss_audio *audio)
559 return -EPERM;
561 #endif
563 static const struct omapdss_hdmi_ops hdmi_ops = {
564 .connect = hdmi_connect,
565 .disconnect = hdmi_disconnect,
567 .enable = hdmi_display_enable,
568 .disable = hdmi_display_disable,
570 .check_timings = hdmi_display_check_timing,
571 .set_timings = hdmi_display_set_timing,
572 .get_timings = hdmi_display_get_timings,
574 .read_edid = hdmi_read_edid,
576 .audio_enable = hdmi_audio_enable,
577 .audio_disable = hdmi_audio_disable,
578 .audio_start = hdmi_audio_start,
579 .audio_stop = hdmi_audio_stop,
580 .audio_supported = hdmi_audio_supported,
581 .audio_config = hdmi_audio_config,
584 static void hdmi_init_output(struct platform_device *pdev)
586 struct omap_dss_device *out = &hdmi.output;
588 out->dev = &pdev->dev;
589 out->id = OMAP_DSS_OUTPUT_HDMI;
590 out->output_type = OMAP_DISPLAY_TYPE_HDMI;
591 out->name = "hdmi.0";
592 out->dispc_channel = OMAP_DSS_CHANNEL_DIGIT;
593 out->ops.hdmi = &hdmi_ops;
594 out->owner = THIS_MODULE;
596 omapdss_register_output(out);
599 static void __exit hdmi_uninit_output(struct platform_device *pdev)
601 struct omap_dss_device *out = &hdmi.output;
603 omapdss_unregister_output(out);
606 /* HDMI HW IP initialisation */
607 static int omapdss_hdmihw_probe(struct platform_device *pdev)
609 int r;
611 hdmi.pdev = pdev;
613 mutex_init(&hdmi.lock);
615 r = hdmi_wp_init(pdev, &hdmi.wp);
616 if (r)
617 return r;
619 r = hdmi_pll_init(pdev, &hdmi.pll);
620 if (r)
621 return r;
623 r = hdmi_phy_init(pdev, &hdmi.phy);
624 if (r)
625 return r;
627 r = hdmi4_core_init(pdev, &hdmi.core);
628 if (r)
629 return r;
631 r = hdmi_get_clocks(pdev);
632 if (r) {
633 DSSERR("can't get clocks\n");
634 return r;
637 pm_runtime_enable(&pdev->dev);
639 hdmi_init_output(pdev);
641 dss_debugfs_create_file("hdmi", hdmi_dump_regs);
643 return 0;
646 static int __exit omapdss_hdmihw_remove(struct platform_device *pdev)
648 hdmi_uninit_output(pdev);
650 pm_runtime_disable(&pdev->dev);
652 return 0;
655 static int hdmi_runtime_suspend(struct device *dev)
657 clk_disable_unprepare(hdmi.sys_clk);
659 dispc_runtime_put();
661 return 0;
664 static int hdmi_runtime_resume(struct device *dev)
666 int r;
668 r = dispc_runtime_get();
669 if (r < 0)
670 return r;
672 clk_prepare_enable(hdmi.sys_clk);
674 return 0;
677 static const struct dev_pm_ops hdmi_pm_ops = {
678 .runtime_suspend = hdmi_runtime_suspend,
679 .runtime_resume = hdmi_runtime_resume,
682 static struct platform_driver omapdss_hdmihw_driver = {
683 .probe = omapdss_hdmihw_probe,
684 .remove = __exit_p(omapdss_hdmihw_remove),
685 .driver = {
686 .name = "omapdss_hdmi",
687 .owner = THIS_MODULE,
688 .pm = &hdmi_pm_ops,
692 int __init hdmi4_init_platform_driver(void)
694 return platform_driver_register(&omapdss_hdmihw_driver);
697 void __exit hdmi4_uninit_platform_driver(void)
699 platform_driver_unregister(&omapdss_hdmihw_driver);