]> Pileus Git - ~andy/linux/blob - drivers/video/omap2/dss/hdmi4.c
Merge tag 'efi-urgent' into x86/urgent
[~andy/linux] / drivers / video / omap2 / dss / hdmi4.c
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>
6  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #define DSS_SUBSYS_NAME "HDMI"
21
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
37 #include "hdmi4_core.h"
38 #include "dss.h"
39 #include "dss_features.h"
40
41 static struct {
42         struct mutex lock;
43         struct platform_device *pdev;
44
45         struct hdmi_wp_data     wp;
46         struct hdmi_pll_data    pll;
47         struct hdmi_phy_data    phy;
48         struct hdmi_core_data   core;
49
50         struct hdmi_config cfg;
51
52         struct clk *sys_clk;
53         struct regulator *vdda_hdmi_dac_reg;
54
55         bool core_enabled;
56
57         struct omap_dss_device output;
58 } hdmi;
59
60 static int hdmi_runtime_get(void)
61 {
62         int r;
63
64         DSSDBG("hdmi_runtime_get\n");
65
66         r = pm_runtime_get_sync(&hdmi.pdev->dev);
67         WARN_ON(r < 0);
68         if (r < 0)
69                 return r;
70
71         return 0;
72 }
73
74 static void hdmi_runtime_put(void)
75 {
76         int r;
77
78         DSSDBG("hdmi_runtime_put\n");
79
80         r = pm_runtime_put_sync(&hdmi.pdev->dev);
81         WARN_ON(r < 0 && r != -ENOSYS);
82 }
83
84 static int hdmi_init_regulator(void)
85 {
86         struct regulator *reg;
87
88         if (hdmi.vdda_hdmi_dac_reg != NULL)
89                 return 0;
90
91         reg = devm_regulator_get(&hdmi.pdev->dev, "vdda_hdmi_dac");
92
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");
96
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);
101         }
102
103         hdmi.vdda_hdmi_dac_reg = reg;
104
105         return 0;
106 }
107
108 static int hdmi_power_on_core(struct omap_dss_device *dssdev)
109 {
110         int r;
111
112         r = regulator_enable(hdmi.vdda_hdmi_dac_reg);
113         if (r)
114                 return r;
115
116         r = hdmi_runtime_get();
117         if (r)
118                 goto err_runtime_get;
119
120         /* Make selection of HDMI in DSS */
121         dss_select_hdmi_venc_clk_source(DSS_HDMI_M_PCLK);
122
123         hdmi.core_enabled = true;
124
125         return 0;
126
127 err_runtime_get:
128         regulator_disable(hdmi.vdda_hdmi_dac_reg);
129
130         return r;
131 }
132
133 static void hdmi_power_off_core(struct omap_dss_device *dssdev)
134 {
135         hdmi.core_enabled = false;
136
137         hdmi_runtime_put();
138         regulator_disable(hdmi.vdda_hdmi_dac_reg);
139 }
140
141 static int hdmi_power_on_full(struct omap_dss_device *dssdev)
142 {
143         int r;
144         struct omap_video_timings *p;
145         struct omap_overlay_manager *mgr = hdmi.output.manager;
146         unsigned long phy;
147
148         r = hdmi_power_on_core(dssdev);
149         if (r)
150                 return r;
151
152         p = &hdmi.cfg.timings;
153
154         DSSDBG("hdmi_power_on x_res= %d y_res = %d\n", p->x_res, p->y_res);
155
156         phy = p->pixel_clock;
157
158         hdmi_pll_compute(&hdmi.pll, clk_get_rate(hdmi.sys_clk), phy);
159
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;
165         }
166
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;
171         }
172
173         hdmi4_configure(&hdmi.core, &hdmi.wp, &hdmi.cfg);
174
175         /* bypass TV gamma table */
176         dispc_enable_gamma_table(0);
177
178         /* tv size */
179         dss_mgr_set_timings(mgr, p);
180
181         r = hdmi_wp_video_start(&hdmi.wp);
182         if (r)
183                 goto err_vid_enable;
184
185         r = dss_mgr_enable(mgr);
186         if (r)
187                 goto err_mgr_enable;
188
189         return 0;
190
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;
200 }
201
202 static void hdmi_power_off_full(struct omap_dss_device *dssdev)
203 {
204         struct omap_overlay_manager *mgr = hdmi.output.manager;
205
206         dss_mgr_disable(mgr);
207
208         hdmi_wp_video_stop(&hdmi.wp);
209         hdmi_phy_disable(&hdmi.phy, &hdmi.wp);
210         hdmi_pll_disable(&hdmi.pll, &hdmi.wp);
211
212         hdmi_power_off_core(dssdev);
213 }
214
215 static int hdmi_display_check_timing(struct omap_dss_device *dssdev,
216                                         struct omap_video_timings *timings)
217 {
218         struct omap_dss_device *out = &hdmi.output;
219
220         if (!dispc_mgr_timings_ok(out->dispc_channel, timings))
221                 return -EINVAL;
222
223         return 0;
224 }
225
226 static void hdmi_display_set_timing(struct omap_dss_device *dssdev,
227                 struct omap_video_timings *timings)
228 {
229         struct hdmi_cm cm;
230         const struct hdmi_config *t;
231
232         mutex_lock(&hdmi.lock);
233
234         cm = hdmi_get_code(timings);
235         hdmi.cfg.cm = cm;
236
237         t = hdmi_get_timings(cm.mode, cm.code);
238         if (t != NULL) {
239                 hdmi.cfg = *t;
240
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;
246
247                 dispc_set_tv_pclk(timings->pixel_clock * 1000);
248         }
249
250         DSSDBG("using mode: %s, code %d\n", hdmi.cfg.cm.mode == HDMI_DVI ?
251                         "DVI" : "HDMI", hdmi.cfg.cm.code);
252
253         mutex_unlock(&hdmi.lock);
254 }
255
256 static void hdmi_display_get_timings(struct omap_dss_device *dssdev,
257                 struct omap_video_timings *timings)
258 {
259         const struct hdmi_config *cfg;
260         struct hdmi_cm cm = hdmi.cfg.cm;
261
262         cfg = hdmi_get_timings(cm.mode, cm.code);
263         if (cfg == NULL)
264                 cfg = hdmi_default_timing();
265
266         memcpy(timings, &cfg->timings, sizeof(cfg->timings));
267 }
268
269 static void hdmi_dump_regs(struct seq_file *s)
270 {
271         mutex_lock(&hdmi.lock);
272
273         if (hdmi_runtime_get()) {
274                 mutex_unlock(&hdmi.lock);
275                 return;
276         }
277
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);
282
283         hdmi_runtime_put();
284         mutex_unlock(&hdmi.lock);
285 }
286
287 static int read_edid(u8 *buf, int len)
288 {
289         int r;
290
291         mutex_lock(&hdmi.lock);
292
293         r = hdmi_runtime_get();
294         BUG_ON(r);
295
296         r = hdmi4_read_edid(&hdmi.core,  buf, len);
297
298         hdmi_runtime_put();
299         mutex_unlock(&hdmi.lock);
300
301         return r;
302 }
303
304 static int hdmi_display_enable(struct omap_dss_device *dssdev)
305 {
306         struct omap_dss_device *out = &hdmi.output;
307         int r = 0;
308
309         DSSDBG("ENTER hdmi_display_enable\n");
310
311         mutex_lock(&hdmi.lock);
312
313         if (out == NULL || out->manager == NULL) {
314                 DSSERR("failed to enable display: no output/manager\n");
315                 r = -ENODEV;
316                 goto err0;
317         }
318
319         r = hdmi_power_on_full(dssdev);
320         if (r) {
321                 DSSERR("failed to power on device\n");
322                 goto err0;
323         }
324
325         mutex_unlock(&hdmi.lock);
326         return 0;
327
328 err0:
329         mutex_unlock(&hdmi.lock);
330         return r;
331 }
332
333 static void hdmi_display_disable(struct omap_dss_device *dssdev)
334 {
335         DSSDBG("Enter hdmi_display_disable\n");
336
337         mutex_lock(&hdmi.lock);
338
339         hdmi_power_off_full(dssdev);
340
341         mutex_unlock(&hdmi.lock);
342 }
343
344 static int hdmi_core_enable(struct omap_dss_device *dssdev)
345 {
346         int r = 0;
347
348         DSSDBG("ENTER omapdss_hdmi_core_enable\n");
349
350         mutex_lock(&hdmi.lock);
351
352         r = hdmi_power_on_core(dssdev);
353         if (r) {
354                 DSSERR("failed to power on device\n");
355                 goto err0;
356         }
357
358         mutex_unlock(&hdmi.lock);
359         return 0;
360
361 err0:
362         mutex_unlock(&hdmi.lock);
363         return r;
364 }
365
366 static void hdmi_core_disable(struct omap_dss_device *dssdev)
367 {
368         DSSDBG("Enter omapdss_hdmi_core_disable\n");
369
370         mutex_lock(&hdmi.lock);
371
372         hdmi_power_off_core(dssdev);
373
374         mutex_unlock(&hdmi.lock);
375 }
376
377 static int hdmi_get_clocks(struct platform_device *pdev)
378 {
379         struct clk *clk;
380
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);
385         }
386
387         hdmi.sys_clk = clk;
388
389         return 0;
390 }
391
392 static int hdmi_connect(struct omap_dss_device *dssdev,
393                 struct omap_dss_device *dst)
394 {
395         struct omap_overlay_manager *mgr;
396         int r;
397
398         r = hdmi_init_regulator();
399         if (r)
400                 return r;
401
402         mgr = omap_dss_get_overlay_manager(dssdev->dispc_channel);
403         if (!mgr)
404                 return -ENODEV;
405
406         r = dss_mgr_connect(mgr, dssdev);
407         if (r)
408                 return r;
409
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;
416         }
417
418         return 0;
419 }
420
421 static void hdmi_disconnect(struct omap_dss_device *dssdev,
422                 struct omap_dss_device *dst)
423 {
424         WARN_ON(dst != dssdev->dst);
425
426         if (dst != dssdev->dst)
427                 return;
428
429         omapdss_output_unset_device(dssdev);
430
431         if (dssdev->manager)
432                 dss_mgr_disconnect(dssdev->manager, dssdev);
433 }
434
435 static int hdmi_read_edid(struct omap_dss_device *dssdev,
436                 u8 *edid, int len)
437 {
438         bool need_enable;
439         int r;
440
441         need_enable = hdmi.core_enabled == false;
442
443         if (need_enable) {
444                 r = hdmi_core_enable(dssdev);
445                 if (r)
446                         return r;
447         }
448
449         r = read_edid(edid, len);
450
451         if (need_enable)
452                 hdmi_core_disable(dssdev);
453
454         return r;
455 }
456
457 #if defined(CONFIG_OMAP4_DSS_HDMI_AUDIO)
458 static int hdmi_audio_enable(struct omap_dss_device *dssdev)
459 {
460         int r;
461
462         mutex_lock(&hdmi.lock);
463
464         if (!hdmi_mode_has_audio(hdmi.cfg.cm.mode)) {
465                 r = -EPERM;
466                 goto err;
467         }
468
469         r = hdmi_wp_audio_enable(&hdmi.wp, true);
470         if (r)
471                 goto err;
472
473         mutex_unlock(&hdmi.lock);
474         return 0;
475
476 err:
477         mutex_unlock(&hdmi.lock);
478         return r;
479 }
480
481 static void hdmi_audio_disable(struct omap_dss_device *dssdev)
482 {
483         hdmi_wp_audio_enable(&hdmi.wp, false);
484 }
485
486 static int hdmi_audio_start(struct omap_dss_device *dssdev)
487 {
488         return hdmi4_audio_start(&hdmi.core, &hdmi.wp);
489 }
490
491 static void hdmi_audio_stop(struct omap_dss_device *dssdev)
492 {
493         hdmi4_audio_stop(&hdmi.core, &hdmi.wp);
494 }
495
496 static bool hdmi_audio_supported(struct omap_dss_device *dssdev)
497 {
498         bool r;
499
500         mutex_lock(&hdmi.lock);
501
502         r = hdmi_mode_has_audio(hdmi.cfg.cm.mode);
503
504         mutex_unlock(&hdmi.lock);
505         return r;
506 }
507
508 static int hdmi_audio_config(struct omap_dss_device *dssdev,
509                 struct omap_dss_audio *audio)
510 {
511         int r;
512         u32 pclk = hdmi.cfg.timings.pixel_clock;
513
514         mutex_lock(&hdmi.lock);
515
516         if (!hdmi_mode_has_audio(hdmi.cfg.cm.mode)) {
517                 r = -EPERM;
518                 goto err;
519         }
520
521         r = hdmi4_audio_config(&hdmi.core, &hdmi.wp, audio, pclk);
522         if (r)
523                 goto err;
524
525         mutex_unlock(&hdmi.lock);
526         return 0;
527
528 err:
529         mutex_unlock(&hdmi.lock);
530         return r;
531 }
532 #else
533 static int hdmi_audio_enable(struct omap_dss_device *dssdev)
534 {
535         return -EPERM;
536 }
537
538 static void hdmi_audio_disable(struct omap_dss_device *dssdev)
539 {
540 }
541
542 static int hdmi_audio_start(struct omap_dss_device *dssdev)
543 {
544         return -EPERM;
545 }
546
547 static void hdmi_audio_stop(struct omap_dss_device *dssdev)
548 {
549 }
550
551 static bool hdmi_audio_supported(struct omap_dss_device *dssdev)
552 {
553         return false;
554 }
555
556 static int hdmi_audio_config(struct omap_dss_device *dssdev,
557                 struct omap_dss_audio *audio)
558 {
559         return -EPERM;
560 }
561 #endif
562
563 static const struct omapdss_hdmi_ops hdmi_ops = {
564         .connect                = hdmi_connect,
565         .disconnect             = hdmi_disconnect,
566
567         .enable                 = hdmi_display_enable,
568         .disable                = hdmi_display_disable,
569
570         .check_timings          = hdmi_display_check_timing,
571         .set_timings            = hdmi_display_set_timing,
572         .get_timings            = hdmi_display_get_timings,
573
574         .read_edid              = hdmi_read_edid,
575
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,
582 };
583
584 static void hdmi_init_output(struct platform_device *pdev)
585 {
586         struct omap_dss_device *out = &hdmi.output;
587
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;
595
596         omapdss_register_output(out);
597 }
598
599 static void __exit hdmi_uninit_output(struct platform_device *pdev)
600 {
601         struct omap_dss_device *out = &hdmi.output;
602
603         omapdss_unregister_output(out);
604 }
605
606 /* HDMI HW IP initialisation */
607 static int omapdss_hdmihw_probe(struct platform_device *pdev)
608 {
609         int r;
610
611         hdmi.pdev = pdev;
612
613         mutex_init(&hdmi.lock);
614
615         r = hdmi_wp_init(pdev, &hdmi.wp);
616         if (r)
617                 return r;
618
619         r = hdmi_pll_init(pdev, &hdmi.pll);
620         if (r)
621                 return r;
622
623         r = hdmi_phy_init(pdev, &hdmi.phy);
624         if (r)
625                 return r;
626
627         r = hdmi4_core_init(pdev, &hdmi.core);
628         if (r)
629                 return r;
630
631         r = hdmi_get_clocks(pdev);
632         if (r) {
633                 DSSERR("can't get clocks\n");
634                 return r;
635         }
636
637         pm_runtime_enable(&pdev->dev);
638
639         hdmi_init_output(pdev);
640
641         dss_debugfs_create_file("hdmi", hdmi_dump_regs);
642
643         return 0;
644 }
645
646 static int __exit omapdss_hdmihw_remove(struct platform_device *pdev)
647 {
648         hdmi_uninit_output(pdev);
649
650         pm_runtime_disable(&pdev->dev);
651
652         return 0;
653 }
654
655 static int hdmi_runtime_suspend(struct device *dev)
656 {
657         clk_disable_unprepare(hdmi.sys_clk);
658
659         dispc_runtime_put();
660
661         return 0;
662 }
663
664 static int hdmi_runtime_resume(struct device *dev)
665 {
666         int r;
667
668         r = dispc_runtime_get();
669         if (r < 0)
670                 return r;
671
672         clk_prepare_enable(hdmi.sys_clk);
673
674         return 0;
675 }
676
677 static const struct dev_pm_ops hdmi_pm_ops = {
678         .runtime_suspend = hdmi_runtime_suspend,
679         .runtime_resume = hdmi_runtime_resume,
680 };
681
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,
689         },
690 };
691
692 int __init hdmi4_init_platform_driver(void)
693 {
694         return platform_driver_register(&omapdss_hdmihw_driver);
695 }
696
697 void __exit hdmi4_uninit_platform_driver(void)
698 {
699         platform_driver_unregister(&omapdss_hdmihw_driver);
700 }