/* SPDX-License-Identifier: GPL-2.0-only */1/*2* v4l2-dv-timings - Internal header with dv-timings helper functions3*4* Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.5*/67#ifndef __V4L2_DV_TIMINGS_H8#define __V4L2_DV_TIMINGS_H910#include <linux/debugfs.h>11#include <linux/videodev2.h>1213/**14* v4l2_calc_timeperframe - helper function to calculate timeperframe based15* v4l2_dv_timings fields.16* @t: Timings for the video mode.17*18* Calculates the expected timeperframe using the pixel clock value and19* horizontal/vertical measures. This means that v4l2_dv_timings structure20* must be correctly and fully filled.21*/22struct v4l2_fract v4l2_calc_timeperframe(const struct v4l2_dv_timings *t);2324/*25* v4l2_dv_timings_presets: list of all dv_timings presets.26*/27extern const struct v4l2_dv_timings v4l2_dv_timings_presets[];2829/**30* typedef v4l2_check_dv_timings_fnc - timings check callback31*32* @t: the v4l2_dv_timings struct.33* @handle: a handle from the driver.34*35* Returns true if the given timings are valid.36*/37typedef bool v4l2_check_dv_timings_fnc(const struct v4l2_dv_timings *t, void *handle);3839/**40* v4l2_valid_dv_timings() - are these timings valid?41*42* @t: the v4l2_dv_timings struct.43* @cap: the v4l2_dv_timings_cap capabilities.44* @fnc: callback to check if this timing is OK. May be NULL.45* @fnc_handle: a handle that is passed on to @fnc.46*47* Returns true if the given dv_timings struct is supported by the48* hardware capabilities and the callback function (if non-NULL), returns49* false otherwise.50*/51bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,52const struct v4l2_dv_timings_cap *cap,53v4l2_check_dv_timings_fnc fnc,54void *fnc_handle);5556/**57* v4l2_enum_dv_timings_cap() - Helper function to enumerate possible DV58* timings based on capabilities59*60* @t: the v4l2_enum_dv_timings struct.61* @cap: the v4l2_dv_timings_cap capabilities.62* @fnc: callback to check if this timing is OK. May be NULL.63* @fnc_handle: a handle that is passed on to @fnc.64*65* This enumerates dv_timings using the full list of possible CEA-861 and DMT66* timings, filtering out any timings that are not supported based on the67* hardware capabilities and the callback function (if non-NULL).68*69* If a valid timing for the given index is found, it will fill in @t and70* return 0, otherwise it returns -EINVAL.71*/72int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,73const struct v4l2_dv_timings_cap *cap,74v4l2_check_dv_timings_fnc fnc,75void *fnc_handle);7677/**78* v4l2_find_dv_timings_cap() - Find the closest timings struct79*80* @t: the v4l2_enum_dv_timings struct.81* @cap: the v4l2_dv_timings_cap capabilities.82* @pclock_delta: maximum delta between t->pixelclock and the timing struct83* under consideration.84* @fnc: callback to check if a given timings struct is OK. May be NULL.85* @fnc_handle: a handle that is passed on to @fnc.86*87* This function tries to map the given timings to an entry in the88* full list of possible CEA-861 and DMT timings, filtering out any timings89* that are not supported based on the hardware capabilities and the callback90* function (if non-NULL).91*92* On success it will fill in @t with the found timings and it returns true.93* On failure it will return false.94*/95bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,96const struct v4l2_dv_timings_cap *cap,97unsigned pclock_delta,98v4l2_check_dv_timings_fnc fnc,99void *fnc_handle);100101/**102* v4l2_find_dv_timings_cea861_vic() - find timings based on CEA-861 VIC103* @t: the timings data.104* @vic: CEA-861 VIC code105*106* On success it will fill in @t with the found timings and it returns true.107* On failure it will return false.108*/109bool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings *t, u8 vic);110111/**112* v4l2_match_dv_timings() - do two timings match?113*114* @measured: the measured timings data.115* @standard: the timings according to the standard.116* @pclock_delta: maximum delta in Hz between standard->pixelclock and117* the measured timings.118* @match_reduced_fps: if true, then fail if V4L2_DV_FL_REDUCED_FPS does not119* match.120*121* Returns true if the two timings match, returns false otherwise.122*/123bool v4l2_match_dv_timings(const struct v4l2_dv_timings *measured,124const struct v4l2_dv_timings *standard,125unsigned pclock_delta, bool match_reduced_fps);126127/**128* v4l2_print_dv_timings() - log the contents of a dv_timings struct129* @dev_prefix:device prefix for each log line.130* @prefix: additional prefix for each log line, may be NULL.131* @t: the timings data.132* @detailed: if true, give a detailed log.133*/134void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,135const struct v4l2_dv_timings *t, bool detailed);136137/**138* v4l2_detect_cvt - detect if the given timings follow the CVT standard139*140* @frame_height: the total height of the frame (including blanking) in lines.141* @hfreq: the horizontal frequency in Hz.142* @vsync: the height of the vertical sync in lines.143* @active_width: active width of image (does not include blanking). This144* information is needed only in case of version 2 of reduced blanking.145* In other cases, this parameter does not have any effect on timings.146* @polarities: the horizontal and vertical polarities (same as struct147* v4l2_bt_timings polarities).148* @interlaced: if this flag is true, it indicates interlaced format149* @cap: the v4l2_dv_timings_cap capabilities.150* @fmt: the resulting timings.151*152* This function will attempt to detect if the given values correspond to a153* valid CVT format. If so, then it will return true, and fmt will be filled154* in with the found CVT timings.155*/156bool v4l2_detect_cvt(unsigned int frame_height, unsigned int hfreq,157unsigned int vsync, unsigned int active_width,158u32 polarities, bool interlaced,159const struct v4l2_dv_timings_cap *cap,160struct v4l2_dv_timings *fmt);161162/**163* v4l2_detect_gtf - detect if the given timings follow the GTF standard164*165* @frame_height: the total height of the frame (including blanking) in lines.166* @hfreq: the horizontal frequency in Hz.167* @vsync: the height of the vertical sync in lines.168* @polarities: the horizontal and vertical polarities (same as struct169* v4l2_bt_timings polarities).170* @interlaced: if this flag is true, it indicates interlaced format171* @aspect: preferred aspect ratio. GTF has no method of determining the172* aspect ratio in order to derive the image width from the173* image height, so it has to be passed explicitly. Usually174* the native screen aspect ratio is used for this. If it175* is not filled in correctly, then 16:9 will be assumed.176* @cap: the v4l2_dv_timings_cap capabilities.177* @fmt: the resulting timings.178*179* This function will attempt to detect if the given values correspond to a180* valid GTF format. If so, then it will return true, and fmt will be filled181* in with the found GTF timings.182*/183bool v4l2_detect_gtf(unsigned int frame_height, unsigned int hfreq,184unsigned int vsync, u32 polarities, bool interlaced,185struct v4l2_fract aspect,186const struct v4l2_dv_timings_cap *cap,187struct v4l2_dv_timings *fmt);188189/**190* v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes191* 0x15 and 0x16 from the EDID.192*193* @hor_landscape: byte 0x15 from the EDID.194* @vert_portrait: byte 0x16 from the EDID.195*196* Determines the aspect ratio from the EDID.197* See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:198* "Horizontal and Vertical Screen Size or Aspect Ratio"199*/200struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait);201202/**203* v4l2_dv_timings_aspect_ratio - calculate the aspect ratio based on the204* v4l2_dv_timings information.205*206* @t: the timings data.207*/208struct v4l2_fract v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings *t);209210/**211* can_reduce_fps - check if conditions for reduced fps are true.212* @bt: v4l2 timing structure213*214* For different timings reduced fps is allowed if the following conditions215* are met:216*217* - For CVT timings: if reduced blanking v2 (vsync == 8) is true.218* - For CEA861 timings: if %V4L2_DV_FL_CAN_REDUCE_FPS flag is true.219*/220static inline bool can_reduce_fps(struct v4l2_bt_timings *bt)221{222if ((bt->standards & V4L2_DV_BT_STD_CVT) && (bt->vsync == 8))223return true;224225if ((bt->standards & V4L2_DV_BT_STD_CEA861) &&226(bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS))227return true;228229return false;230}231232/**233* struct v4l2_hdmi_colorimetry - describes the HDMI colorimetry information234* @colorspace: enum v4l2_colorspace, the colorspace235* @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding236* @quantization: enum v4l2_quantization, colorspace quantization237* @xfer_func: enum v4l2_xfer_func, colorspace transfer function238*/239struct v4l2_hdmi_colorimetry {240enum v4l2_colorspace colorspace;241enum v4l2_ycbcr_encoding ycbcr_enc;242enum v4l2_quantization quantization;243enum v4l2_xfer_func xfer_func;244};245246struct hdmi_avi_infoframe;247struct hdmi_vendor_infoframe;248249struct v4l2_hdmi_colorimetry250v4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe *avi,251const struct hdmi_vendor_infoframe *hdmi,252unsigned int height);253254unsigned int v4l2_num_edid_blocks(const u8 *edid, unsigned int max_blocks);255u16 v4l2_get_edid_phys_addr(const u8 *edid, unsigned int size,256unsigned int *offset);257void v4l2_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);258u16 v4l2_phys_addr_for_input(u16 phys_addr, u8 input);259int v4l2_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);260261/* Add support for exporting InfoFrames to debugfs */262263/*264* HDMI InfoFrames start with a 3 byte header, then a checksum,265* followed by the actual IF payload.266*267* The payload length is limited to 30 bytes according to the HDMI spec,268* but since the length is encoded in 5 bits, it can be 31 bytes theoretically.269* So set the max length as 31 + 3 (header) + 1 (checksum) = 35.270*/271#define V4L2_DEBUGFS_IF_MAX_LEN (35)272273#define V4L2_DEBUGFS_IF_AVI BIT(0)274#define V4L2_DEBUGFS_IF_AUDIO BIT(1)275#define V4L2_DEBUGFS_IF_SPD BIT(2)276#define V4L2_DEBUGFS_IF_HDMI BIT(3)277278typedef ssize_t (*v4l2_debugfs_if_read_t)(u32 type, void *priv,279struct file *filp, char __user *ubuf,280size_t count, loff_t *ppos);281282struct v4l2_debugfs_if {283struct dentry *if_dir;284void *priv;285286v4l2_debugfs_if_read_t if_read;287};288289#ifdef CONFIG_DEBUG_FS290struct v4l2_debugfs_if *v4l2_debugfs_if_alloc(struct dentry *root, u32 if_types,291void *priv,292v4l2_debugfs_if_read_t if_read);293void v4l2_debugfs_if_free(struct v4l2_debugfs_if *infoframes);294#else295static inline296struct v4l2_debugfs_if *v4l2_debugfs_if_alloc(struct dentry *root, u32 if_types,297void *priv,298v4l2_debugfs_if_read_t if_read)299{300return NULL;301}302303static inline void v4l2_debugfs_if_free(struct v4l2_debugfs_if *infoframes)304{305}306#endif307308#endif309310311