aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHans Verkuil <hans.verkuil@cisco.com>2013-07-29 07:40:56 -0400
committerMauro Carvalho Chehab <m.chehab@samsung.com>2013-08-18 07:16:54 -0400
commit2576415846bcbad3c0a6885fc44f950837106364 (patch)
tree99772fbb135740430c638fc3eb6aa91f0a6f751a
parentb18787ed1ce32eb0c2ce2323220abd4ed93c4b97 (diff)
[media] v4l2: move dv-timings related code to v4l2-dv-timings.c
v4l2-common.c contained a bunch of dv-timings related functions. Move that to the new v4l2-dv-timings.c which is a more appropriate place for them. There aren't many drivers that do HDTV, so it is a good idea to separate common code related to that into a module of its own. Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com> Acked-by: Lad, Prabhakar <prabhakar.csengg@gmail.com> Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>
-rw-r--r--drivers/media/i2c/ad9389b.c1
-rw-r--r--drivers/media/i2c/adv7604.c1
-rw-r--r--drivers/media/i2c/ths8200.c1
-rw-r--r--drivers/media/usb/hdpvr/hdpvr-video.c1
-rw-r--r--drivers/media/v4l2-core/v4l2-common.c357
-rw-r--r--drivers/media/v4l2-core/v4l2-dv-timings.c358
-rw-r--r--include/media/v4l2-common.h13
-rw-r--r--include/media/v4l2-dv-timings.h59
8 files changed, 420 insertions, 371 deletions
diff --git a/drivers/media/i2c/ad9389b.c b/drivers/media/i2c/ad9389b.c
index ba4364dfae66..2fa8d7286cea 100644
--- a/drivers/media/i2c/ad9389b.c
+++ b/drivers/media/i2c/ad9389b.c
@@ -33,6 +33,7 @@
33#include <linux/v4l2-dv-timings.h> 33#include <linux/v4l2-dv-timings.h>
34#include <media/v4l2-device.h> 34#include <media/v4l2-device.h>
35#include <media/v4l2-common.h> 35#include <media/v4l2-common.h>
36#include <media/v4l2-dv-timings.h>
36#include <media/v4l2-ctrls.h> 37#include <media/v4l2-ctrls.h>
37#include <media/ad9389b.h> 38#include <media/ad9389b.h>
38 39
diff --git a/drivers/media/i2c/adv7604.c b/drivers/media/i2c/adv7604.c
index 1d675b58fd71..181a6c359335 100644
--- a/drivers/media/i2c/adv7604.c
+++ b/drivers/media/i2c/adv7604.c
@@ -38,6 +38,7 @@
38#include <linux/v4l2-dv-timings.h> 38#include <linux/v4l2-dv-timings.h>
39#include <media/v4l2-device.h> 39#include <media/v4l2-device.h>
40#include <media/v4l2-ctrls.h> 40#include <media/v4l2-ctrls.h>
41#include <media/v4l2-dv-timings.h>
41#include <media/adv7604.h> 42#include <media/adv7604.h>
42 43
43static int debug; 44static int debug;
diff --git a/drivers/media/i2c/ths8200.c b/drivers/media/i2c/ths8200.c
index 8a29810d155a..aef7c0e7cd67 100644
--- a/drivers/media/i2c/ths8200.c
+++ b/drivers/media/i2c/ths8200.c
@@ -21,6 +21,7 @@
21#include <linux/module.h> 21#include <linux/module.h>
22#include <linux/v4l2-dv-timings.h> 22#include <linux/v4l2-dv-timings.h>
23 23
24#include <media/v4l2-dv-timings.h>
24#include <media/v4l2-async.h> 25#include <media/v4l2-async.h>
25#include <media/v4l2-device.h> 26#include <media/v4l2-device.h>
26 27
diff --git a/drivers/media/usb/hdpvr/hdpvr-video.c b/drivers/media/usb/hdpvr/hdpvr-video.c
index 4f8567aa99d8..9c67b6e127e3 100644
--- a/drivers/media/usb/hdpvr/hdpvr-video.c
+++ b/drivers/media/usb/hdpvr/hdpvr-video.c
@@ -24,6 +24,7 @@
24#include <linux/v4l2-dv-timings.h> 24#include <linux/v4l2-dv-timings.h>
25#include <media/v4l2-dev.h> 25#include <media/v4l2-dev.h>
26#include <media/v4l2-common.h> 26#include <media/v4l2-common.h>
27#include <media/v4l2-dv-timings.h>
27#include <media/v4l2-ioctl.h> 28#include <media/v4l2-ioctl.h>
28#include <media/v4l2-event.h> 29#include <media/v4l2-event.h>
29#include "hdpvr.h" 30#include "hdpvr.h"
diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
index a95e5e23403f..037d7a55aa8c 100644
--- a/drivers/media/v4l2-core/v4l2-common.c
+++ b/drivers/media/v4l2-core/v4l2-common.c
@@ -495,363 +495,6 @@ void v4l_bound_align_image(u32 *w, unsigned int wmin, unsigned int wmax,
495} 495}
496EXPORT_SYMBOL_GPL(v4l_bound_align_image); 496EXPORT_SYMBOL_GPL(v4l_bound_align_image);
497 497
498/**
499 * v4l_match_dv_timings - check if two timings match
500 * @t1 - compare this v4l2_dv_timings struct...
501 * @t2 - with this struct.
502 * @pclock_delta - the allowed pixelclock deviation.
503 *
504 * Compare t1 with t2 with a given margin of error for the pixelclock.
505 */
506bool v4l_match_dv_timings(const struct v4l2_dv_timings *t1,
507 const struct v4l2_dv_timings *t2,
508 unsigned pclock_delta)
509{
510 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
511 return false;
512 if (t1->bt.width == t2->bt.width &&
513 t1->bt.height == t2->bt.height &&
514 t1->bt.interlaced == t2->bt.interlaced &&
515 t1->bt.polarities == t2->bt.polarities &&
516 t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
517 t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
518 t1->bt.hfrontporch == t2->bt.hfrontporch &&
519 t1->bt.vfrontporch == t2->bt.vfrontporch &&
520 t1->bt.vsync == t2->bt.vsync &&
521 t1->bt.vbackporch == t2->bt.vbackporch &&
522 (!t1->bt.interlaced ||
523 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
524 t1->bt.il_vsync == t2->bt.il_vsync &&
525 t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
526 return true;
527 return false;
528}
529EXPORT_SYMBOL_GPL(v4l_match_dv_timings);
530
531/*
532 * CVT defines
533 * Based on Coordinated Video Timings Standard
534 * version 1.1 September 10, 2003
535 */
536
537#define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
538
539/* Normal blanking */
540#define CVT_MIN_V_BPORCH 7 /* lines */
541#define CVT_MIN_V_PORCH_RND 3 /* lines */
542#define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
543
544/* Normal blanking for CVT uses GTF to calculate horizontal blanking */
545#define CVT_CELL_GRAN 8 /* character cell granularity */
546#define CVT_M 600 /* blanking formula gradient */
547#define CVT_C 40 /* blanking formula offset */
548#define CVT_K 128 /* blanking formula scaling factor */
549#define CVT_J 20 /* blanking formula scaling factor */
550#define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
551#define CVT_M_PRIME (CVT_K * CVT_M / 256)
552
553/* Reduced Blanking */
554#define CVT_RB_MIN_V_BPORCH 7 /* lines */
555#define CVT_RB_V_FPORCH 3 /* lines */
556#define CVT_RB_MIN_V_BLANK 460 /* us */
557#define CVT_RB_H_SYNC 32 /* pixels */
558#define CVT_RB_H_BPORCH 80 /* pixels */
559#define CVT_RB_H_BLANK 160 /* pixels */
560
561/** v4l2_detect_cvt - detect if the given timings follow the CVT standard
562 * @frame_height - the total height of the frame (including blanking) in lines.
563 * @hfreq - the horizontal frequency in Hz.
564 * @vsync - the height of the vertical sync in lines.
565 * @polarities - the horizontal and vertical polarities (same as struct
566 * v4l2_bt_timings polarities).
567 * @fmt - the resulting timings.
568 *
569 * This function will attempt to detect if the given values correspond to a
570 * valid CVT format. If so, then it will return true, and fmt will be filled
571 * in with the found CVT timings.
572 */
573bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
574 u32 polarities, struct v4l2_dv_timings *fmt)
575{
576 int v_fp, v_bp, h_fp, h_bp, hsync;
577 int frame_width, image_height, image_width;
578 bool reduced_blanking;
579 unsigned pix_clk;
580
581 if (vsync < 4 || vsync > 7)
582 return false;
583
584 if (polarities == V4L2_DV_VSYNC_POS_POL)
585 reduced_blanking = false;
586 else if (polarities == V4L2_DV_HSYNC_POS_POL)
587 reduced_blanking = true;
588 else
589 return false;
590
591 /* Vertical */
592 if (reduced_blanking) {
593 v_fp = CVT_RB_V_FPORCH;
594 v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 999999) / 1000000;
595 v_bp -= vsync + v_fp;
596
597 if (v_bp < CVT_RB_MIN_V_BPORCH)
598 v_bp = CVT_RB_MIN_V_BPORCH;
599 } else {
600 v_fp = CVT_MIN_V_PORCH_RND;
601 v_bp = (CVT_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
602
603 if (v_bp < CVT_MIN_V_BPORCH)
604 v_bp = CVT_MIN_V_BPORCH;
605 }
606 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
607
608 /* Aspect ratio based on vsync */
609 switch (vsync) {
610 case 4:
611 image_width = (image_height * 4) / 3;
612 break;
613 case 5:
614 image_width = (image_height * 16) / 9;
615 break;
616 case 6:
617 image_width = (image_height * 16) / 10;
618 break;
619 case 7:
620 /* special case */
621 if (image_height == 1024)
622 image_width = (image_height * 5) / 4;
623 else if (image_height == 768)
624 image_width = (image_height * 15) / 9;
625 else
626 return false;
627 break;
628 default:
629 return false;
630 }
631
632 image_width = image_width & ~7;
633
634 /* Horizontal */
635 if (reduced_blanking) {
636 pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq;
637 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
638
639 h_bp = CVT_RB_H_BPORCH;
640 hsync = CVT_RB_H_SYNC;
641 h_fp = CVT_RB_H_BLANK - h_bp - hsync;
642
643 frame_width = image_width + CVT_RB_H_BLANK;
644 } else {
645 int h_blank;
646 unsigned ideal_duty_cycle = CVT_C_PRIME - (CVT_M_PRIME * 1000) / hfreq;
647
648 h_blank = (image_width * ideal_duty_cycle + (100 - ideal_duty_cycle) / 2) /
649 (100 - ideal_duty_cycle);
650 h_blank = h_blank - h_blank % (2 * CVT_CELL_GRAN);
651
652 if (h_blank * 100 / image_width < 20) {
653 h_blank = image_width / 5;
654 h_blank = (h_blank + 0x7) & ~0x7;
655 }
656
657 pix_clk = (image_width + h_blank) * hfreq;
658 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
659
660 h_bp = h_blank / 2;
661 frame_width = image_width + h_blank;
662
663 hsync = (frame_width * 8 + 50) / 100;
664 hsync = hsync - hsync % CVT_CELL_GRAN;
665 h_fp = h_blank - hsync - h_bp;
666 }
667
668 fmt->bt.polarities = polarities;
669 fmt->bt.width = image_width;
670 fmt->bt.height = image_height;
671 fmt->bt.hfrontporch = h_fp;
672 fmt->bt.vfrontporch = v_fp;
673 fmt->bt.hsync = hsync;
674 fmt->bt.vsync = vsync;
675 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
676 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
677 fmt->bt.pixelclock = pix_clk;
678 fmt->bt.standards = V4L2_DV_BT_STD_CVT;
679 if (reduced_blanking)
680 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
681 return true;
682}
683EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
684
685/*
686 * GTF defines
687 * Based on Generalized Timing Formula Standard
688 * Version 1.1 September 2, 1999
689 */
690
691#define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
692
693#define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
694#define GTF_V_FP 1 /* vertical front porch (lines) */
695#define GTF_CELL_GRAN 8 /* character cell granularity */
696
697/* Default */
698#define GTF_D_M 600 /* blanking formula gradient */
699#define GTF_D_C 40 /* blanking formula offset */
700#define GTF_D_K 128 /* blanking formula scaling factor */
701#define GTF_D_J 20 /* blanking formula scaling factor */
702#define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
703#define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
704
705/* Secondary */
706#define GTF_S_M 3600 /* blanking formula gradient */
707#define GTF_S_C 40 /* blanking formula offset */
708#define GTF_S_K 128 /* blanking formula scaling factor */
709#define GTF_S_J 35 /* blanking formula scaling factor */
710#define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
711#define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
712
713/** v4l2_detect_gtf - detect if the given timings follow the GTF standard
714 * @frame_height - the total height of the frame (including blanking) in lines.
715 * @hfreq - the horizontal frequency in Hz.
716 * @vsync - the height of the vertical sync in lines.
717 * @polarities - the horizontal and vertical polarities (same as struct
718 * v4l2_bt_timings polarities).
719 * @aspect - preferred aspect ratio. GTF has no method of determining the
720 * aspect ratio in order to derive the image width from the
721 * image height, so it has to be passed explicitly. Usually
722 * the native screen aspect ratio is used for this. If it
723 * is not filled in correctly, then 16:9 will be assumed.
724 * @fmt - the resulting timings.
725 *
726 * This function will attempt to detect if the given values correspond to a
727 * valid GTF format. If so, then it will return true, and fmt will be filled
728 * in with the found GTF timings.
729 */
730bool v4l2_detect_gtf(unsigned frame_height,
731 unsigned hfreq,
732 unsigned vsync,
733 u32 polarities,
734 struct v4l2_fract aspect,
735 struct v4l2_dv_timings *fmt)
736{
737 int pix_clk;
738 int v_fp, v_bp, h_fp, hsync;
739 int frame_width, image_height, image_width;
740 bool default_gtf;
741 int h_blank;
742
743 if (vsync != 3)
744 return false;
745
746 if (polarities == V4L2_DV_VSYNC_POS_POL)
747 default_gtf = true;
748 else if (polarities == V4L2_DV_HSYNC_POS_POL)
749 default_gtf = false;
750 else
751 return false;
752
753 /* Vertical */
754 v_fp = GTF_V_FP;
755 v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
756 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
757
758 if (aspect.numerator == 0 || aspect.denominator == 0) {
759 aspect.numerator = 16;
760 aspect.denominator = 9;
761 }
762 image_width = ((image_height * aspect.numerator) / aspect.denominator);
763
764 /* Horizontal */
765 if (default_gtf)
766 h_blank = ((image_width * GTF_D_C_PRIME * hfreq) -
767 (image_width * GTF_D_M_PRIME * 1000) +
768 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) /
769 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000);
770 else
771 h_blank = ((image_width * GTF_S_C_PRIME * hfreq) -
772 (image_width * GTF_S_M_PRIME * 1000) +
773 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) /
774 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000);
775
776 h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN);
777 frame_width = image_width + h_blank;
778
779 pix_clk = (image_width + h_blank) * hfreq;
780 pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
781
782 hsync = (frame_width * 8 + 50) / 100;
783 hsync = hsync - hsync % GTF_CELL_GRAN;
784
785 h_fp = h_blank / 2 - hsync;
786
787 fmt->bt.polarities = polarities;
788 fmt->bt.width = image_width;
789 fmt->bt.height = image_height;
790 fmt->bt.hfrontporch = h_fp;
791 fmt->bt.vfrontporch = v_fp;
792 fmt->bt.hsync = hsync;
793 fmt->bt.vsync = vsync;
794 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
795 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
796 fmt->bt.pixelclock = pix_clk;
797 fmt->bt.standards = V4L2_DV_BT_STD_GTF;
798 if (!default_gtf)
799 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
800 return true;
801}
802EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
803
804/** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
805 * 0x15 and 0x16 from the EDID.
806 * @hor_landscape - byte 0x15 from the EDID.
807 * @vert_portrait - byte 0x16 from the EDID.
808 *
809 * Determines the aspect ratio from the EDID.
810 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
811 * "Horizontal and Vertical Screen Size or Aspect Ratio"
812 */
813struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
814{
815 struct v4l2_fract aspect = { 16, 9 };
816 u32 tmp;
817 u8 ratio;
818
819 /* Nothing filled in, fallback to 16:9 */
820 if (!hor_landscape && !vert_portrait)
821 return aspect;
822 /* Both filled in, so they are interpreted as the screen size in cm */
823 if (hor_landscape && vert_portrait) {
824 aspect.numerator = hor_landscape;
825 aspect.denominator = vert_portrait;
826 return aspect;
827 }
828 /* Only one is filled in, so interpret them as a ratio:
829 (val + 99) / 100 */
830 ratio = hor_landscape | vert_portrait;
831 /* Change some rounded values into the exact aspect ratio */
832 if (ratio == 79) {
833 aspect.numerator = 16;
834 aspect.denominator = 9;
835 } else if (ratio == 34) {
836 aspect.numerator = 4;
837 aspect.numerator = 3;
838 } else if (ratio == 68) {
839 aspect.numerator = 15;
840 aspect.numerator = 9;
841 } else {
842 aspect.numerator = hor_landscape + 99;
843 aspect.denominator = 100;
844 }
845 if (hor_landscape)
846 return aspect;
847 /* The aspect ratio is for portrait, so swap numerator and denominator */
848 tmp = aspect.denominator;
849 aspect.denominator = aspect.numerator;
850 aspect.numerator = tmp;
851 return aspect;
852}
853EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
854
855const struct v4l2_frmsize_discrete *v4l2_find_nearest_format( 498const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
856 const struct v4l2_discrete_probe *probe, 499 const struct v4l2_discrete_probe *probe,
857 s32 width, s32 height) 500 s32 width, s32 height)
diff --git a/drivers/media/v4l2-core/v4l2-dv-timings.c b/drivers/media/v4l2-core/v4l2-dv-timings.c
index 58279467a7a5..f20b316f7d00 100644
--- a/drivers/media/v4l2-core/v4l2-dv-timings.c
+++ b/drivers/media/v4l2-core/v4l2-dv-timings.c
@@ -24,7 +24,6 @@
24#include <linux/errno.h> 24#include <linux/errno.h>
25#include <linux/videodev2.h> 25#include <linux/videodev2.h>
26#include <linux/v4l2-dv-timings.h> 26#include <linux/v4l2-dv-timings.h>
27#include <media/v4l2-common.h>
28#include <media/v4l2-dv-timings.h> 27#include <media/v4l2-dv-timings.h>
29 28
30static const struct v4l2_dv_timings timings[] = { 29static const struct v4l2_dv_timings timings[] = {
@@ -190,3 +189,360 @@ bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
190 return false; 189 return false;
191} 190}
192EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap); 191EXPORT_SYMBOL_GPL(v4l2_find_dv_timings_cap);
192
193/**
194 * v4l_match_dv_timings - check if two timings match
195 * @t1 - compare this v4l2_dv_timings struct...
196 * @t2 - with this struct.
197 * @pclock_delta - the allowed pixelclock deviation.
198 *
199 * Compare t1 with t2 with a given margin of error for the pixelclock.
200 */
201bool v4l_match_dv_timings(const struct v4l2_dv_timings *t1,
202 const struct v4l2_dv_timings *t2,
203 unsigned pclock_delta)
204{
205 if (t1->type != t2->type || t1->type != V4L2_DV_BT_656_1120)
206 return false;
207 if (t1->bt.width == t2->bt.width &&
208 t1->bt.height == t2->bt.height &&
209 t1->bt.interlaced == t2->bt.interlaced &&
210 t1->bt.polarities == t2->bt.polarities &&
211 t1->bt.pixelclock >= t2->bt.pixelclock - pclock_delta &&
212 t1->bt.pixelclock <= t2->bt.pixelclock + pclock_delta &&
213 t1->bt.hfrontporch == t2->bt.hfrontporch &&
214 t1->bt.vfrontporch == t2->bt.vfrontporch &&
215 t1->bt.vsync == t2->bt.vsync &&
216 t1->bt.vbackporch == t2->bt.vbackporch &&
217 (!t1->bt.interlaced ||
218 (t1->bt.il_vfrontporch == t2->bt.il_vfrontporch &&
219 t1->bt.il_vsync == t2->bt.il_vsync &&
220 t1->bt.il_vbackporch == t2->bt.il_vbackporch)))
221 return true;
222 return false;
223}
224EXPORT_SYMBOL_GPL(v4l_match_dv_timings);
225
226/*
227 * CVT defines
228 * Based on Coordinated Video Timings Standard
229 * version 1.1 September 10, 2003
230 */
231
232#define CVT_PXL_CLK_GRAN 250000 /* pixel clock granularity */
233
234/* Normal blanking */
235#define CVT_MIN_V_BPORCH 7 /* lines */
236#define CVT_MIN_V_PORCH_RND 3 /* lines */
237#define CVT_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
238
239/* Normal blanking for CVT uses GTF to calculate horizontal blanking */
240#define CVT_CELL_GRAN 8 /* character cell granularity */
241#define CVT_M 600 /* blanking formula gradient */
242#define CVT_C 40 /* blanking formula offset */
243#define CVT_K 128 /* blanking formula scaling factor */
244#define CVT_J 20 /* blanking formula scaling factor */
245#define CVT_C_PRIME (((CVT_C - CVT_J) * CVT_K / 256) + CVT_J)
246#define CVT_M_PRIME (CVT_K * CVT_M / 256)
247
248/* Reduced Blanking */
249#define CVT_RB_MIN_V_BPORCH 7 /* lines */
250#define CVT_RB_V_FPORCH 3 /* lines */
251#define CVT_RB_MIN_V_BLANK 460 /* us */
252#define CVT_RB_H_SYNC 32 /* pixels */
253#define CVT_RB_H_BPORCH 80 /* pixels */
254#define CVT_RB_H_BLANK 160 /* pixels */
255
256/** v4l2_detect_cvt - detect if the given timings follow the CVT standard
257 * @frame_height - the total height of the frame (including blanking) in lines.
258 * @hfreq - the horizontal frequency in Hz.
259 * @vsync - the height of the vertical sync in lines.
260 * @polarities - the horizontal and vertical polarities (same as struct
261 * v4l2_bt_timings polarities).
262 * @fmt - the resulting timings.
263 *
264 * This function will attempt to detect if the given values correspond to a
265 * valid CVT format. If so, then it will return true, and fmt will be filled
266 * in with the found CVT timings.
267 */
268bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
269 u32 polarities, struct v4l2_dv_timings *fmt)
270{
271 int v_fp, v_bp, h_fp, h_bp, hsync;
272 int frame_width, image_height, image_width;
273 bool reduced_blanking;
274 unsigned pix_clk;
275
276 if (vsync < 4 || vsync > 7)
277 return false;
278
279 if (polarities == V4L2_DV_VSYNC_POS_POL)
280 reduced_blanking = false;
281 else if (polarities == V4L2_DV_HSYNC_POS_POL)
282 reduced_blanking = true;
283 else
284 return false;
285
286 /* Vertical */
287 if (reduced_blanking) {
288 v_fp = CVT_RB_V_FPORCH;
289 v_bp = (CVT_RB_MIN_V_BLANK * hfreq + 999999) / 1000000;
290 v_bp -= vsync + v_fp;
291
292 if (v_bp < CVT_RB_MIN_V_BPORCH)
293 v_bp = CVT_RB_MIN_V_BPORCH;
294 } else {
295 v_fp = CVT_MIN_V_PORCH_RND;
296 v_bp = (CVT_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
297
298 if (v_bp < CVT_MIN_V_BPORCH)
299 v_bp = CVT_MIN_V_BPORCH;
300 }
301 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
302
303 /* Aspect ratio based on vsync */
304 switch (vsync) {
305 case 4:
306 image_width = (image_height * 4) / 3;
307 break;
308 case 5:
309 image_width = (image_height * 16) / 9;
310 break;
311 case 6:
312 image_width = (image_height * 16) / 10;
313 break;
314 case 7:
315 /* special case */
316 if (image_height == 1024)
317 image_width = (image_height * 5) / 4;
318 else if (image_height == 768)
319 image_width = (image_height * 15) / 9;
320 else
321 return false;
322 break;
323 default:
324 return false;
325 }
326
327 image_width = image_width & ~7;
328
329 /* Horizontal */
330 if (reduced_blanking) {
331 pix_clk = (image_width + CVT_RB_H_BLANK) * hfreq;
332 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
333
334 h_bp = CVT_RB_H_BPORCH;
335 hsync = CVT_RB_H_SYNC;
336 h_fp = CVT_RB_H_BLANK - h_bp - hsync;
337
338 frame_width = image_width + CVT_RB_H_BLANK;
339 } else {
340 int h_blank;
341 unsigned ideal_duty_cycle = CVT_C_PRIME - (CVT_M_PRIME * 1000) / hfreq;
342
343 h_blank = (image_width * ideal_duty_cycle + (100 - ideal_duty_cycle) / 2) /
344 (100 - ideal_duty_cycle);
345 h_blank = h_blank - h_blank % (2 * CVT_CELL_GRAN);
346
347 if (h_blank * 100 / image_width < 20) {
348 h_blank = image_width / 5;
349 h_blank = (h_blank + 0x7) & ~0x7;
350 }
351
352 pix_clk = (image_width + h_blank) * hfreq;
353 pix_clk = (pix_clk / CVT_PXL_CLK_GRAN) * CVT_PXL_CLK_GRAN;
354
355 h_bp = h_blank / 2;
356 frame_width = image_width + h_blank;
357
358 hsync = (frame_width * 8 + 50) / 100;
359 hsync = hsync - hsync % CVT_CELL_GRAN;
360 h_fp = h_blank - hsync - h_bp;
361 }
362
363 fmt->bt.polarities = polarities;
364 fmt->bt.width = image_width;
365 fmt->bt.height = image_height;
366 fmt->bt.hfrontporch = h_fp;
367 fmt->bt.vfrontporch = v_fp;
368 fmt->bt.hsync = hsync;
369 fmt->bt.vsync = vsync;
370 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
371 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
372 fmt->bt.pixelclock = pix_clk;
373 fmt->bt.standards = V4L2_DV_BT_STD_CVT;
374 if (reduced_blanking)
375 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
376 return true;
377}
378EXPORT_SYMBOL_GPL(v4l2_detect_cvt);
379
380/*
381 * GTF defines
382 * Based on Generalized Timing Formula Standard
383 * Version 1.1 September 2, 1999
384 */
385
386#define GTF_PXL_CLK_GRAN 250000 /* pixel clock granularity */
387
388#define GTF_MIN_VSYNC_BP 550 /* min time of vsync + back porch (us) */
389#define GTF_V_FP 1 /* vertical front porch (lines) */
390#define GTF_CELL_GRAN 8 /* character cell granularity */
391
392/* Default */
393#define GTF_D_M 600 /* blanking formula gradient */
394#define GTF_D_C 40 /* blanking formula offset */
395#define GTF_D_K 128 /* blanking formula scaling factor */
396#define GTF_D_J 20 /* blanking formula scaling factor */
397#define GTF_D_C_PRIME ((((GTF_D_C - GTF_D_J) * GTF_D_K) / 256) + GTF_D_J)
398#define GTF_D_M_PRIME ((GTF_D_K * GTF_D_M) / 256)
399
400/* Secondary */
401#define GTF_S_M 3600 /* blanking formula gradient */
402#define GTF_S_C 40 /* blanking formula offset */
403#define GTF_S_K 128 /* blanking formula scaling factor */
404#define GTF_S_J 35 /* blanking formula scaling factor */
405#define GTF_S_C_PRIME ((((GTF_S_C - GTF_S_J) * GTF_S_K) / 256) + GTF_S_J)
406#define GTF_S_M_PRIME ((GTF_S_K * GTF_S_M) / 256)
407
408/** v4l2_detect_gtf - detect if the given timings follow the GTF standard
409 * @frame_height - the total height of the frame (including blanking) in lines.
410 * @hfreq - the horizontal frequency in Hz.
411 * @vsync - the height of the vertical sync in lines.
412 * @polarities - the horizontal and vertical polarities (same as struct
413 * v4l2_bt_timings polarities).
414 * @aspect - preferred aspect ratio. GTF has no method of determining the
415 * aspect ratio in order to derive the image width from the
416 * image height, so it has to be passed explicitly. Usually
417 * the native screen aspect ratio is used for this. If it
418 * is not filled in correctly, then 16:9 will be assumed.
419 * @fmt - the resulting timings.
420 *
421 * This function will attempt to detect if the given values correspond to a
422 * valid GTF format. If so, then it will return true, and fmt will be filled
423 * in with the found GTF timings.
424 */
425bool v4l2_detect_gtf(unsigned frame_height,
426 unsigned hfreq,
427 unsigned vsync,
428 u32 polarities,
429 struct v4l2_fract aspect,
430 struct v4l2_dv_timings *fmt)
431{
432 int pix_clk;
433 int v_fp, v_bp, h_fp, hsync;
434 int frame_width, image_height, image_width;
435 bool default_gtf;
436 int h_blank;
437
438 if (vsync != 3)
439 return false;
440
441 if (polarities == V4L2_DV_VSYNC_POS_POL)
442 default_gtf = true;
443 else if (polarities == V4L2_DV_HSYNC_POS_POL)
444 default_gtf = false;
445 else
446 return false;
447
448 /* Vertical */
449 v_fp = GTF_V_FP;
450 v_bp = (GTF_MIN_VSYNC_BP * hfreq + 999999) / 1000000 - vsync;
451 image_height = (frame_height - v_fp - vsync - v_bp + 1) & ~0x1;
452
453 if (aspect.numerator == 0 || aspect.denominator == 0) {
454 aspect.numerator = 16;
455 aspect.denominator = 9;
456 }
457 image_width = ((image_height * aspect.numerator) / aspect.denominator);
458
459 /* Horizontal */
460 if (default_gtf)
461 h_blank = ((image_width * GTF_D_C_PRIME * hfreq) -
462 (image_width * GTF_D_M_PRIME * 1000) +
463 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000) / 2) /
464 (hfreq * (100 - GTF_D_C_PRIME) + GTF_D_M_PRIME * 1000);
465 else
466 h_blank = ((image_width * GTF_S_C_PRIME * hfreq) -
467 (image_width * GTF_S_M_PRIME * 1000) +
468 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000) / 2) /
469 (hfreq * (100 - GTF_S_C_PRIME) + GTF_S_M_PRIME * 1000);
470
471 h_blank = h_blank - h_blank % (2 * GTF_CELL_GRAN);
472 frame_width = image_width + h_blank;
473
474 pix_clk = (image_width + h_blank) * hfreq;
475 pix_clk = pix_clk / GTF_PXL_CLK_GRAN * GTF_PXL_CLK_GRAN;
476
477 hsync = (frame_width * 8 + 50) / 100;
478 hsync = hsync - hsync % GTF_CELL_GRAN;
479
480 h_fp = h_blank / 2 - hsync;
481
482 fmt->bt.polarities = polarities;
483 fmt->bt.width = image_width;
484 fmt->bt.height = image_height;
485 fmt->bt.hfrontporch = h_fp;
486 fmt->bt.vfrontporch = v_fp;
487 fmt->bt.hsync = hsync;
488 fmt->bt.vsync = vsync;
489 fmt->bt.hbackporch = frame_width - image_width - h_fp - hsync;
490 fmt->bt.vbackporch = frame_height - image_height - v_fp - vsync;
491 fmt->bt.pixelclock = pix_clk;
492 fmt->bt.standards = V4L2_DV_BT_STD_GTF;
493 if (!default_gtf)
494 fmt->bt.flags |= V4L2_DV_FL_REDUCED_BLANKING;
495 return true;
496}
497EXPORT_SYMBOL_GPL(v4l2_detect_gtf);
498
499/** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
500 * 0x15 and 0x16 from the EDID.
501 * @hor_landscape - byte 0x15 from the EDID.
502 * @vert_portrait - byte 0x16 from the EDID.
503 *
504 * Determines the aspect ratio from the EDID.
505 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
506 * "Horizontal and Vertical Screen Size or Aspect Ratio"
507 */
508struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait)
509{
510 struct v4l2_fract aspect = { 16, 9 };
511 u32 tmp;
512 u8 ratio;
513
514 /* Nothing filled in, fallback to 16:9 */
515 if (!hor_landscape && !vert_portrait)
516 return aspect;
517 /* Both filled in, so they are interpreted as the screen size in cm */
518 if (hor_landscape && vert_portrait) {
519 aspect.numerator = hor_landscape;
520 aspect.denominator = vert_portrait;
521 return aspect;
522 }
523 /* Only one is filled in, so interpret them as a ratio:
524 (val + 99) / 100 */
525 ratio = hor_landscape | vert_portrait;
526 /* Change some rounded values into the exact aspect ratio */
527 if (ratio == 79) {
528 aspect.numerator = 16;
529 aspect.denominator = 9;
530 } else if (ratio == 34) {
531 aspect.numerator = 4;
532 aspect.numerator = 3;
533 } else if (ratio == 68) {
534 aspect.numerator = 15;
535 aspect.numerator = 9;
536 } else {
537 aspect.numerator = hor_landscape + 99;
538 aspect.denominator = 100;
539 }
540 if (hor_landscape)
541 return aspect;
542 /* The aspect ratio is for portrait, so swap numerator and denominator */
543 tmp = aspect.denominator;
544 aspect.denominator = aspect.numerator;
545 aspect.numerator = tmp;
546 return aspect;
547}
548EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio);
diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
index 015ff82da73c..0e1d01056f16 100644
--- a/include/media/v4l2-common.h
+++ b/include/media/v4l2-common.h
@@ -201,19 +201,6 @@ const struct v4l2_frmsize_discrete *v4l2_find_nearest_format(
201 const struct v4l2_discrete_probe *probe, 201 const struct v4l2_discrete_probe *probe,
202 s32 width, s32 height); 202 s32 width, s32 height);
203 203
204bool v4l_match_dv_timings(const struct v4l2_dv_timings *t1,
205 const struct v4l2_dv_timings *t2,
206 unsigned pclock_delta);
207
208bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
209 u32 polarities, struct v4l2_dv_timings *fmt);
210
211bool v4l2_detect_gtf(unsigned frame_height, unsigned hfreq, unsigned vsync,
212 u32 polarities, struct v4l2_fract aspect,
213 struct v4l2_dv_timings *fmt);
214
215struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait);
216
217void v4l2_get_timestamp(struct timeval *tv); 204void v4l2_get_timestamp(struct timeval *tv);
218 205
219#endif /* V4L2_COMMON_H_ */ 206#endif /* V4L2_COMMON_H_ */
diff --git a/include/media/v4l2-dv-timings.h b/include/media/v4l2-dv-timings.h
index 41075fa02a96..4c7bb5491658 100644
--- a/include/media/v4l2-dv-timings.h
+++ b/include/media/v4l2-dv-timings.h
@@ -64,4 +64,63 @@ bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
64 const struct v4l2_dv_timings_cap *cap, 64 const struct v4l2_dv_timings_cap *cap,
65 unsigned pclock_delta); 65 unsigned pclock_delta);
66 66
67/** v4l_match_dv_timings() - do two timings match?
68 * @measured: the measured timings data.
69 * @standard: the timings according to the standard.
70 * @pclock_delta: maximum delta in Hz between standard->pixelclock and
71 * the measured timings.
72 *
73 * Returns true if the two timings match, returns false otherwise.
74 */
75bool v4l_match_dv_timings(const struct v4l2_dv_timings *measured,
76 const struct v4l2_dv_timings *standard,
77 unsigned pclock_delta);
78
79/** v4l2_detect_cvt - detect if the given timings follow the CVT standard
80 * @frame_height - the total height of the frame (including blanking) in lines.
81 * @hfreq - the horizontal frequency in Hz.
82 * @vsync - the height of the vertical sync in lines.
83 * @polarities - the horizontal and vertical polarities (same as struct
84 * v4l2_bt_timings polarities).
85 * @fmt - the resulting timings.
86 *
87 * This function will attempt to detect if the given values correspond to a
88 * valid CVT format. If so, then it will return true, and fmt will be filled
89 * in with the found CVT timings.
90 */
91bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
92 u32 polarities, struct v4l2_dv_timings *fmt);
93
94/** v4l2_detect_gtf - detect if the given timings follow the GTF standard
95 * @frame_height - the total height of the frame (including blanking) in lines.
96 * @hfreq - the horizontal frequency in Hz.
97 * @vsync - the height of the vertical sync in lines.
98 * @polarities - the horizontal and vertical polarities (same as struct
99 * v4l2_bt_timings polarities).
100 * @aspect - preferred aspect ratio. GTF has no method of determining the
101 * aspect ratio in order to derive the image width from the
102 * image height, so it has to be passed explicitly. Usually
103 * the native screen aspect ratio is used for this. If it
104 * is not filled in correctly, then 16:9 will be assumed.
105 * @fmt - the resulting timings.
106 *
107 * This function will attempt to detect if the given values correspond to a
108 * valid GTF format. If so, then it will return true, and fmt will be filled
109 * in with the found GTF timings.
110 */
111bool v4l2_detect_gtf(unsigned frame_height, unsigned hfreq, unsigned vsync,
112 u32 polarities, struct v4l2_fract aspect,
113 struct v4l2_dv_timings *fmt);
114
115/** v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
116 * 0x15 and 0x16 from the EDID.
117 * @hor_landscape - byte 0x15 from the EDID.
118 * @vert_portrait - byte 0x16 from the EDID.
119 *
120 * Determines the aspect ratio from the EDID.
121 * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
122 * "Horizontal and Vertical Screen Size or Aspect Ratio"
123 */
124struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait);
125
67#endif 126#endif