From patchwork Tue Mar 7 16:48:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jose Abreu X-Patchwork-Id: 39837 X-Patchwork-Delegate: hverkuil@xs4all.nl Received: from mail.tu-berlin.de ([130.149.7.33]) by www.linuxtv.org with esmtp (Exim 4.84_2) (envelope-from ) id 1clIRc-00076z-IR; Tue, 07 Mar 2017 16:58:40 +0000 X-tubIT-Incoming-IP: 209.132.180.67 Received: from vger.kernel.org ([209.132.180.67]) by mail.tu-berlin.de (exim-4.84_2/mailfrontend-7) with esmtp id 1clIRa-000806-1F; Tue, 07 Mar 2017 17:58:40 +0100 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932700AbdCGQ6P (ORCPT + 1 other); Tue, 7 Mar 2017 11:58:15 -0500 Received: from smtprelay.synopsys.com ([198.182.47.9]:47229 "EHLO smtprelay.synopsys.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755402AbdCGQ6G (ORCPT ); Tue, 7 Mar 2017 11:58:06 -0500 Received: from mailhost.synopsys.com (mailhost1.synopsys.com [10.12.238.239]) by smtprelay.synopsys.com (Postfix) with ESMTP id E574F24E1EE0; Tue, 7 Mar 2017 08:48:34 -0800 (PST) Received: from mailhost.synopsys.com (localhost [127.0.0.1]) by mailhost.synopsys.com (Postfix) with ESMTP id CBFDE345; Tue, 7 Mar 2017 08:48:34 -0800 (PST) Received: from joabreu-VirtualBox.internal.synopsys.com (joabreu-e7440.internal.synopsys.com [10.107.19.78]) by mailhost.synopsys.com (Postfix) with ESMTP id 24A4F318; Tue, 7 Mar 2017 08:48:32 -0800 (PST) From: Jose Abreu To: linux-media@vger.kernel.org Cc: Jose Abreu , Carlos Palminha , Mauro Carvalho Chehab , Hans Verkuil , Charles-Antoine Couret , linux-kernel@vger.kernel.org Subject: [PATCH] [media] v4l2-dv-timings: Introduce v4l2_calc_fps() Date: Tue, 7 Mar 2017 16:48:27 +0000 Message-Id: <94397052765d1f6d84dc7edac65f906b09890871.1488905139.git.joabreu@synopsys.com> X-Mailer: git-send-email 1.9.1 Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-PMX-Version: 6.0.0.2142326, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2017.3.7.165117 X-PMX-Spam: Gauge=IIIIIIII, Probability=8%, Report=' MULTIPLE_RCPTS 0.1, HTML_00_01 0.05, HTML_00_10 0.05, BODYTEXTP_SIZE_3000_LESS 0, BODY_SIZE_2000_2999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, LEGITIMATE_SIGNS 0, MULTIPLE_REAL_RCPTS 0, NO_URI_HTTPS 0, __ANY_URI 0, __CC_NAME 0, __CC_NAME_DIFF_FROM_ACC 0, __CC_REAL_NAMES 0, __CP_MEDIA_BODY 0, __FROM_DOMAIN_IN_ANY_CC2 0, __FROM_DOMAIN_IN_RCPT 0, __HAS_CC_HDR 0, __HAS_FROM 0, __HAS_LIST_ID 0, __HAS_MSGID 0, __HAS_X_MAILER 0, __HAS_X_MAILING_LIST 0, __MIME_TEXT_ONLY 0, __MIME_TEXT_P 0, __MIME_TEXT_P1 0, __MULTIPLE_RCPTS_CC_X2 0, __NO_HTML_TAG_RAW 0, __SANE_MSGID 0, __TO_MALFORMED_2 0, __TO_NO_NAME 0, __URI_NO_WWW 0, __URI_NS ' HDMI Receivers receive video modes which, according to CEA specification, can have different frames per second (fps) values. This patch introduces a helper function in the media core which can calculate the expected video mode fps given the pixel clock value and the horizontal/vertical values. HDMI video receiver drivers are expected to use this helper so that they can correctly fill the v4l2_streamparm structure which is requested by vidioc_g_parm callback. We could also use a lookup table for this but it wouldn't correctly handle 60Hz vs 59.94Hz situations as this all depends on the pixel clock value. Signed-off-by: Jose Abreu Cc: Carlos Palminha Cc: Mauro Carvalho Chehab Cc: Hans Verkuil Cc: Charles-Antoine Couret Cc: linux-media@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/media/v4l2-core/v4l2-dv-timings.c | 29 +++++++++++++++++++++++++++++ include/media/v4l2-dv-timings.h | 8 ++++++++ 2 files changed, 37 insertions(+) diff --git a/drivers/media/v4l2-core/v4l2-dv-timings.c b/drivers/media/v4l2-core/v4l2-dv-timings.c index 5c8c49d..19946c6 100644 --- a/drivers/media/v4l2-core/v4l2-dv-timings.c +++ b/drivers/media/v4l2-core/v4l2-dv-timings.c @@ -814,3 +814,32 @@ struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait) return aspect; } EXPORT_SYMBOL_GPL(v4l2_calc_aspect_ratio); + +struct v4l2_fract v4l2_calc_fps(const struct v4l2_dv_timings *t) +{ + const struct v4l2_bt_timings *bt = &t->bt; + struct v4l2_fract fps_fract = { 1, 1 }; + unsigned long n, d; + unsigned long mask = GENMASK(BITS_PER_LONG - 1, 0); + u32 htot, vtot, fps; + u64 pclk; + + if (t->type != V4L2_DV_BT_656_1120) + return fps_fract; + + htot = V4L2_DV_BT_FRAME_WIDTH(bt); + vtot = V4L2_DV_BT_FRAME_HEIGHT(bt); + pclk = bt->pixelclock; + if (bt->interlaced) + htot /= 2; + + fps = (htot * vtot) > 0 ? div_u64((100 * pclk), (htot * vtot)) : 0; + + rational_best_approximation(fps, 100, mask, mask, &n, &d); + + fps_fract.numerator = d; + fps_fract.denominator = n; + return fps_fract; +} +EXPORT_SYMBOL_GPL(v4l2_calc_fps); + diff --git a/include/media/v4l2-dv-timings.h b/include/media/v4l2-dv-timings.h index 61a1889..d23b168 100644 --- a/include/media/v4l2-dv-timings.h +++ b/include/media/v4l2-dv-timings.h @@ -196,6 +196,14 @@ bool v4l2_detect_gtf(unsigned frame_height, unsigned hfreq, unsigned vsync, struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait); /** + * v4l2_calc_fps - calculate the frames per seconds based on the + * v4l2_dv_timings information. + * + * @t: the timings data. + */ +struct v4l2_fract v4l2_calc_fps(const struct v4l2_dv_timings *t); + +/** * v4l2_dv_timings_aspect_ratio - calculate the aspect ratio based on the * v4l2_dv_timings information. *