162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * v4l2-dv-timings - Internal header with dv-timings helper functions
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#ifndef __V4L2_DV_TIMINGS_H
962306a36Sopenharmony_ci#define __V4L2_DV_TIMINGS_H
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci#include <linux/videodev2.h>
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci/**
1462306a36Sopenharmony_ci * v4l2_calc_timeperframe - helper function to calculate timeperframe based
1562306a36Sopenharmony_ci *	v4l2_dv_timings fields.
1662306a36Sopenharmony_ci * @t: Timings for the video mode.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * Calculates the expected timeperframe using the pixel clock value and
1962306a36Sopenharmony_ci * horizontal/vertical measures. This means that v4l2_dv_timings structure
2062306a36Sopenharmony_ci * must be correctly and fully filled.
2162306a36Sopenharmony_ci */
2262306a36Sopenharmony_cistruct v4l2_fract v4l2_calc_timeperframe(const struct v4l2_dv_timings *t);
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci/*
2562306a36Sopenharmony_ci * v4l2_dv_timings_presets: list of all dv_timings presets.
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ciextern const struct v4l2_dv_timings v4l2_dv_timings_presets[];
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/**
3062306a36Sopenharmony_ci * typedef v4l2_check_dv_timings_fnc - timings check callback
3162306a36Sopenharmony_ci *
3262306a36Sopenharmony_ci * @t: the v4l2_dv_timings struct.
3362306a36Sopenharmony_ci * @handle: a handle from the driver.
3462306a36Sopenharmony_ci *
3562306a36Sopenharmony_ci * Returns true if the given timings are valid.
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_citypedef bool v4l2_check_dv_timings_fnc(const struct v4l2_dv_timings *t, void *handle);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci/**
4062306a36Sopenharmony_ci * v4l2_valid_dv_timings() - are these timings valid?
4162306a36Sopenharmony_ci *
4262306a36Sopenharmony_ci * @t:	  the v4l2_dv_timings struct.
4362306a36Sopenharmony_ci * @cap: the v4l2_dv_timings_cap capabilities.
4462306a36Sopenharmony_ci * @fnc: callback to check if this timing is OK. May be NULL.
4562306a36Sopenharmony_ci * @fnc_handle: a handle that is passed on to @fnc.
4662306a36Sopenharmony_ci *
4762306a36Sopenharmony_ci * Returns true if the given dv_timings struct is supported by the
4862306a36Sopenharmony_ci * hardware capabilities and the callback function (if non-NULL), returns
4962306a36Sopenharmony_ci * false otherwise.
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_cibool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
5262306a36Sopenharmony_ci			   const struct v4l2_dv_timings_cap *cap,
5362306a36Sopenharmony_ci			   v4l2_check_dv_timings_fnc fnc,
5462306a36Sopenharmony_ci			   void *fnc_handle);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/**
5762306a36Sopenharmony_ci * v4l2_enum_dv_timings_cap() - Helper function to enumerate possible DV
5862306a36Sopenharmony_ci *	 timings based on capabilities
5962306a36Sopenharmony_ci *
6062306a36Sopenharmony_ci * @t:	  the v4l2_enum_dv_timings struct.
6162306a36Sopenharmony_ci * @cap: the v4l2_dv_timings_cap capabilities.
6262306a36Sopenharmony_ci * @fnc: callback to check if this timing is OK. May be NULL.
6362306a36Sopenharmony_ci * @fnc_handle: a handle that is passed on to @fnc.
6462306a36Sopenharmony_ci *
6562306a36Sopenharmony_ci * This enumerates dv_timings using the full list of possible CEA-861 and DMT
6662306a36Sopenharmony_ci * timings, filtering out any timings that are not supported based on the
6762306a36Sopenharmony_ci * hardware capabilities and the callback function (if non-NULL).
6862306a36Sopenharmony_ci *
6962306a36Sopenharmony_ci * If a valid timing for the given index is found, it will fill in @t and
7062306a36Sopenharmony_ci * return 0, otherwise it returns -EINVAL.
7162306a36Sopenharmony_ci */
7262306a36Sopenharmony_ciint v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
7362306a36Sopenharmony_ci			     const struct v4l2_dv_timings_cap *cap,
7462306a36Sopenharmony_ci			     v4l2_check_dv_timings_fnc fnc,
7562306a36Sopenharmony_ci			     void *fnc_handle);
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/**
7862306a36Sopenharmony_ci * v4l2_find_dv_timings_cap() - Find the closest timings struct
7962306a36Sopenharmony_ci *
8062306a36Sopenharmony_ci * @t:	  the v4l2_enum_dv_timings struct.
8162306a36Sopenharmony_ci * @cap: the v4l2_dv_timings_cap capabilities.
8262306a36Sopenharmony_ci * @pclock_delta: maximum delta between t->pixelclock and the timing struct
8362306a36Sopenharmony_ci *		under consideration.
8462306a36Sopenharmony_ci * @fnc: callback to check if a given timings struct is OK. May be NULL.
8562306a36Sopenharmony_ci * @fnc_handle: a handle that is passed on to @fnc.
8662306a36Sopenharmony_ci *
8762306a36Sopenharmony_ci * This function tries to map the given timings to an entry in the
8862306a36Sopenharmony_ci * full list of possible CEA-861 and DMT timings, filtering out any timings
8962306a36Sopenharmony_ci * that are not supported based on the hardware capabilities and the callback
9062306a36Sopenharmony_ci * function (if non-NULL).
9162306a36Sopenharmony_ci *
9262306a36Sopenharmony_ci * On success it will fill in @t with the found timings and it returns true.
9362306a36Sopenharmony_ci * On failure it will return false.
9462306a36Sopenharmony_ci */
9562306a36Sopenharmony_cibool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
9662306a36Sopenharmony_ci			      const struct v4l2_dv_timings_cap *cap,
9762306a36Sopenharmony_ci			      unsigned pclock_delta,
9862306a36Sopenharmony_ci			      v4l2_check_dv_timings_fnc fnc,
9962306a36Sopenharmony_ci			      void *fnc_handle);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci/**
10262306a36Sopenharmony_ci * v4l2_find_dv_timings_cea861_vic() - find timings based on CEA-861 VIC
10362306a36Sopenharmony_ci * @t:		the timings data.
10462306a36Sopenharmony_ci * @vic:	CEA-861 VIC code
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * On success it will fill in @t with the found timings and it returns true.
10762306a36Sopenharmony_ci * On failure it will return false.
10862306a36Sopenharmony_ci */
10962306a36Sopenharmony_cibool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings *t, u8 vic);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/**
11262306a36Sopenharmony_ci * v4l2_match_dv_timings() - do two timings match?
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci * @measured:	  the measured timings data.
11562306a36Sopenharmony_ci * @standard:	  the timings according to the standard.
11662306a36Sopenharmony_ci * @pclock_delta: maximum delta in Hz between standard->pixelclock and
11762306a36Sopenharmony_ci *		the measured timings.
11862306a36Sopenharmony_ci * @match_reduced_fps: if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
11962306a36Sopenharmony_ci * match.
12062306a36Sopenharmony_ci *
12162306a36Sopenharmony_ci * Returns true if the two timings match, returns false otherwise.
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_cibool v4l2_match_dv_timings(const struct v4l2_dv_timings *measured,
12462306a36Sopenharmony_ci			   const struct v4l2_dv_timings *standard,
12562306a36Sopenharmony_ci			   unsigned pclock_delta, bool match_reduced_fps);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci/**
12862306a36Sopenharmony_ci * v4l2_print_dv_timings() - log the contents of a dv_timings struct
12962306a36Sopenharmony_ci * @dev_prefix:device prefix for each log line.
13062306a36Sopenharmony_ci * @prefix:	additional prefix for each log line, may be NULL.
13162306a36Sopenharmony_ci * @t:		the timings data.
13262306a36Sopenharmony_ci * @detailed:	if true, give a detailed log.
13362306a36Sopenharmony_ci */
13462306a36Sopenharmony_civoid v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
13562306a36Sopenharmony_ci			   const struct v4l2_dv_timings *t, bool detailed);
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci/**
13862306a36Sopenharmony_ci * v4l2_detect_cvt - detect if the given timings follow the CVT standard
13962306a36Sopenharmony_ci *
14062306a36Sopenharmony_ci * @frame_height: the total height of the frame (including blanking) in lines.
14162306a36Sopenharmony_ci * @hfreq: the horizontal frequency in Hz.
14262306a36Sopenharmony_ci * @vsync: the height of the vertical sync in lines.
14362306a36Sopenharmony_ci * @active_width: active width of image (does not include blanking). This
14462306a36Sopenharmony_ci * information is needed only in case of version 2 of reduced blanking.
14562306a36Sopenharmony_ci * In other cases, this parameter does not have any effect on timings.
14662306a36Sopenharmony_ci * @polarities: the horizontal and vertical polarities (same as struct
14762306a36Sopenharmony_ci *		v4l2_bt_timings polarities).
14862306a36Sopenharmony_ci * @interlaced: if this flag is true, it indicates interlaced format
14962306a36Sopenharmony_ci * @fmt: the resulting timings.
15062306a36Sopenharmony_ci *
15162306a36Sopenharmony_ci * This function will attempt to detect if the given values correspond to a
15262306a36Sopenharmony_ci * valid CVT format. If so, then it will return true, and fmt will be filled
15362306a36Sopenharmony_ci * in with the found CVT timings.
15462306a36Sopenharmony_ci */
15562306a36Sopenharmony_cibool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
15662306a36Sopenharmony_ci		unsigned active_width, u32 polarities, bool interlaced,
15762306a36Sopenharmony_ci		struct v4l2_dv_timings *fmt);
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ci/**
16062306a36Sopenharmony_ci * v4l2_detect_gtf - detect if the given timings follow the GTF standard
16162306a36Sopenharmony_ci *
16262306a36Sopenharmony_ci * @frame_height: the total height of the frame (including blanking) in lines.
16362306a36Sopenharmony_ci * @hfreq: the horizontal frequency in Hz.
16462306a36Sopenharmony_ci * @vsync: the height of the vertical sync in lines.
16562306a36Sopenharmony_ci * @polarities: the horizontal and vertical polarities (same as struct
16662306a36Sopenharmony_ci *		v4l2_bt_timings polarities).
16762306a36Sopenharmony_ci * @interlaced: if this flag is true, it indicates interlaced format
16862306a36Sopenharmony_ci * @aspect: preferred aspect ratio. GTF has no method of determining the
16962306a36Sopenharmony_ci *		aspect ratio in order to derive the image width from the
17062306a36Sopenharmony_ci *		image height, so it has to be passed explicitly. Usually
17162306a36Sopenharmony_ci *		the native screen aspect ratio is used for this. If it
17262306a36Sopenharmony_ci *		is not filled in correctly, then 16:9 will be assumed.
17362306a36Sopenharmony_ci * @fmt: the resulting timings.
17462306a36Sopenharmony_ci *
17562306a36Sopenharmony_ci * This function will attempt to detect if the given values correspond to a
17662306a36Sopenharmony_ci * valid GTF format. If so, then it will return true, and fmt will be filled
17762306a36Sopenharmony_ci * in with the found GTF timings.
17862306a36Sopenharmony_ci */
17962306a36Sopenharmony_cibool v4l2_detect_gtf(unsigned frame_height, unsigned hfreq, unsigned vsync,
18062306a36Sopenharmony_ci		u32 polarities, bool interlaced, struct v4l2_fract aspect,
18162306a36Sopenharmony_ci		struct v4l2_dv_timings *fmt);
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci/**
18462306a36Sopenharmony_ci * v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
18562306a36Sopenharmony_ci *	0x15 and 0x16 from the EDID.
18662306a36Sopenharmony_ci *
18762306a36Sopenharmony_ci * @hor_landscape: byte 0x15 from the EDID.
18862306a36Sopenharmony_ci * @vert_portrait: byte 0x16 from the EDID.
18962306a36Sopenharmony_ci *
19062306a36Sopenharmony_ci * Determines the aspect ratio from the EDID.
19162306a36Sopenharmony_ci * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
19262306a36Sopenharmony_ci * "Horizontal and Vertical Screen Size or Aspect Ratio"
19362306a36Sopenharmony_ci */
19462306a36Sopenharmony_cistruct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait);
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci/**
19762306a36Sopenharmony_ci * v4l2_dv_timings_aspect_ratio - calculate the aspect ratio based on the
19862306a36Sopenharmony_ci *	v4l2_dv_timings information.
19962306a36Sopenharmony_ci *
20062306a36Sopenharmony_ci * @t: the timings data.
20162306a36Sopenharmony_ci */
20262306a36Sopenharmony_cistruct v4l2_fract v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings *t);
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci/**
20562306a36Sopenharmony_ci * can_reduce_fps - check if conditions for reduced fps are true.
20662306a36Sopenharmony_ci * @bt: v4l2 timing structure
20762306a36Sopenharmony_ci *
20862306a36Sopenharmony_ci * For different timings reduced fps is allowed if the following conditions
20962306a36Sopenharmony_ci * are met:
21062306a36Sopenharmony_ci *
21162306a36Sopenharmony_ci *   - For CVT timings: if reduced blanking v2 (vsync == 8) is true.
21262306a36Sopenharmony_ci *   - For CEA861 timings: if %V4L2_DV_FL_CAN_REDUCE_FPS flag is true.
21362306a36Sopenharmony_ci */
21462306a36Sopenharmony_cistatic inline  bool can_reduce_fps(struct v4l2_bt_timings *bt)
21562306a36Sopenharmony_ci{
21662306a36Sopenharmony_ci	if ((bt->standards & V4L2_DV_BT_STD_CVT) && (bt->vsync == 8))
21762306a36Sopenharmony_ci		return true;
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	if ((bt->standards & V4L2_DV_BT_STD_CEA861) &&
22062306a36Sopenharmony_ci	    (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS))
22162306a36Sopenharmony_ci		return true;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	return false;
22462306a36Sopenharmony_ci}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci/**
22762306a36Sopenharmony_ci * struct v4l2_hdmi_colorimetry - describes the HDMI colorimetry information
22862306a36Sopenharmony_ci * @colorspace:		enum v4l2_colorspace, the colorspace
22962306a36Sopenharmony_ci * @ycbcr_enc:		enum v4l2_ycbcr_encoding, Y'CbCr encoding
23062306a36Sopenharmony_ci * @quantization:	enum v4l2_quantization, colorspace quantization
23162306a36Sopenharmony_ci * @xfer_func:		enum v4l2_xfer_func, colorspace transfer function
23262306a36Sopenharmony_ci */
23362306a36Sopenharmony_cistruct v4l2_hdmi_colorimetry {
23462306a36Sopenharmony_ci	enum v4l2_colorspace colorspace;
23562306a36Sopenharmony_ci	enum v4l2_ycbcr_encoding ycbcr_enc;
23662306a36Sopenharmony_ci	enum v4l2_quantization quantization;
23762306a36Sopenharmony_ci	enum v4l2_xfer_func xfer_func;
23862306a36Sopenharmony_ci};
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_cistruct hdmi_avi_infoframe;
24162306a36Sopenharmony_cistruct hdmi_vendor_infoframe;
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_cistruct v4l2_hdmi_colorimetry
24462306a36Sopenharmony_civ4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe *avi,
24562306a36Sopenharmony_ci			 const struct hdmi_vendor_infoframe *hdmi,
24662306a36Sopenharmony_ci			 unsigned int height);
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ciu16 v4l2_get_edid_phys_addr(const u8 *edid, unsigned int size,
24962306a36Sopenharmony_ci			    unsigned int *offset);
25062306a36Sopenharmony_civoid v4l2_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
25162306a36Sopenharmony_ciu16 v4l2_phys_addr_for_input(u16 phys_addr, u8 input);
25262306a36Sopenharmony_ciint v4l2_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci#endif
255