1ab15d248SHans Verkuil /* SPDX-License-Identifier: GPL-2.0-only */
2b18787edSHans Verkuil /*
3b18787edSHans Verkuil * v4l2-dv-timings - Internal header with dv-timings helper functions
4b18787edSHans Verkuil *
5b18787edSHans Verkuil * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6b18787edSHans Verkuil */
7b18787edSHans Verkuil
8b18787edSHans Verkuil #ifndef __V4L2_DV_TIMINGS_H
9b18787edSHans Verkuil #define __V4L2_DV_TIMINGS_H
10b18787edSHans Verkuil
11b18787edSHans Verkuil #include <linux/videodev2.h>
12b18787edSHans Verkuil
1387f9ed85SJose Abreu /**
1487f9ed85SJose Abreu * v4l2_calc_timeperframe - helper function to calculate timeperframe based
1587f9ed85SJose Abreu * v4l2_dv_timings fields.
1687f9ed85SJose Abreu * @t: Timings for the video mode.
1787f9ed85SJose Abreu *
1887f9ed85SJose Abreu * Calculates the expected timeperframe using the pixel clock value and
1987f9ed85SJose Abreu * horizontal/vertical measures. This means that v4l2_dv_timings structure
2087f9ed85SJose Abreu * must be correctly and fully filled.
2187f9ed85SJose Abreu */
2287f9ed85SJose Abreu struct v4l2_fract v4l2_calc_timeperframe(const struct v4l2_dv_timings *t);
2387f9ed85SJose Abreu
24fb91aecbSMauro Carvalho Chehab /*
25506bb54bSMauro Carvalho Chehab * v4l2_dv_timings_presets: list of all dv_timings presets.
26d1c65ad6SHans Verkuil */
27d1c65ad6SHans Verkuil extern const struct v4l2_dv_timings v4l2_dv_timings_presets[];
28d1c65ad6SHans Verkuil
29f06606e5SMauro Carvalho Chehab /**
30f06606e5SMauro Carvalho Chehab * typedef v4l2_check_dv_timings_fnc - timings check callback
31506bb54bSMauro Carvalho Chehab *
32b8f0fff4SHans Verkuil * @t: the v4l2_dv_timings struct.
33b8f0fff4SHans Verkuil * @handle: a handle from the driver.
34b8f0fff4SHans Verkuil *
35b8f0fff4SHans Verkuil * Returns true if the given timings are valid.
36b8f0fff4SHans Verkuil */
37b8f0fff4SHans Verkuil typedef bool v4l2_check_dv_timings_fnc(const struct v4l2_dv_timings *t, void *handle);
38b8f0fff4SHans Verkuil
39506bb54bSMauro Carvalho Chehab /**
40506bb54bSMauro Carvalho Chehab * v4l2_valid_dv_timings() - are these timings valid?
41506bb54bSMauro Carvalho Chehab *
42b18787edSHans Verkuil * @t: the v4l2_dv_timings struct.
43b18787edSHans Verkuil * @cap: the v4l2_dv_timings_cap capabilities.
44b8f0fff4SHans Verkuil * @fnc: callback to check if this timing is OK. May be NULL.
45b8f0fff4SHans Verkuil * @fnc_handle: a handle that is passed on to @fnc.
46b18787edSHans Verkuil *
47b18787edSHans Verkuil * Returns true if the given dv_timings struct is supported by the
48b8f0fff4SHans Verkuil * hardware capabilities and the callback function (if non-NULL), returns
49b8f0fff4SHans Verkuil * false otherwise.
50b18787edSHans Verkuil */
5170b65494SHans Verkuil bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
52b8f0fff4SHans Verkuil const struct v4l2_dv_timings_cap *cap,
53b8f0fff4SHans Verkuil v4l2_check_dv_timings_fnc fnc,
54b8f0fff4SHans Verkuil void *fnc_handle);
55b18787edSHans Verkuil
56506bb54bSMauro Carvalho Chehab /**
57506bb54bSMauro Carvalho Chehab * v4l2_enum_dv_timings_cap() - Helper function to enumerate possible DV
58506bb54bSMauro Carvalho Chehab * timings based on capabilities
59506bb54bSMauro Carvalho Chehab *
60b18787edSHans Verkuil * @t: the v4l2_enum_dv_timings struct.
61b18787edSHans Verkuil * @cap: the v4l2_dv_timings_cap capabilities.
62b8f0fff4SHans Verkuil * @fnc: callback to check if this timing is OK. May be NULL.
63b8f0fff4SHans Verkuil * @fnc_handle: a handle that is passed on to @fnc.
64b18787edSHans Verkuil *
65b18787edSHans Verkuil * This enumerates dv_timings using the full list of possible CEA-861 and DMT
66b18787edSHans Verkuil * timings, filtering out any timings that are not supported based on the
67b8f0fff4SHans Verkuil * hardware capabilities and the callback function (if non-NULL).
68b18787edSHans Verkuil *
69b18787edSHans Verkuil * If a valid timing for the given index is found, it will fill in @t and
70b18787edSHans Verkuil * return 0, otherwise it returns -EINVAL.
71b18787edSHans Verkuil */
72b18787edSHans Verkuil int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
73b8f0fff4SHans Verkuil const struct v4l2_dv_timings_cap *cap,
74b8f0fff4SHans Verkuil v4l2_check_dv_timings_fnc fnc,
75b8f0fff4SHans Verkuil void *fnc_handle);
76b18787edSHans Verkuil
77506bb54bSMauro Carvalho Chehab /**
78506bb54bSMauro Carvalho Chehab * v4l2_find_dv_timings_cap() - Find the closest timings struct
79506bb54bSMauro Carvalho Chehab *
80b18787edSHans Verkuil * @t: the v4l2_enum_dv_timings struct.
81b18787edSHans Verkuil * @cap: the v4l2_dv_timings_cap capabilities.
82b18787edSHans Verkuil * @pclock_delta: maximum delta between t->pixelclock and the timing struct
83b18787edSHans Verkuil * under consideration.
84b8f0fff4SHans Verkuil * @fnc: callback to check if a given timings struct is OK. May be NULL.
85b8f0fff4SHans Verkuil * @fnc_handle: a handle that is passed on to @fnc.
86b18787edSHans Verkuil *
87b18787edSHans Verkuil * This function tries to map the given timings to an entry in the
88b18787edSHans Verkuil * full list of possible CEA-861 and DMT timings, filtering out any timings
89b8f0fff4SHans Verkuil * that are not supported based on the hardware capabilities and the callback
90b8f0fff4SHans Verkuil * function (if non-NULL).
91b18787edSHans Verkuil *
92b18787edSHans Verkuil * On success it will fill in @t with the found timings and it returns true.
93b18787edSHans Verkuil * On failure it will return false.
94b18787edSHans Verkuil */
95b18787edSHans Verkuil bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
96b18787edSHans Verkuil const struct v4l2_dv_timings_cap *cap,
97b8f0fff4SHans Verkuil unsigned pclock_delta,
98b8f0fff4SHans Verkuil v4l2_check_dv_timings_fnc fnc,
99b8f0fff4SHans Verkuil void *fnc_handle);
100b18787edSHans Verkuil
101506bb54bSMauro Carvalho Chehab /**
10265243076SHans Verkuil * v4l2_find_dv_timings_cea861_vic() - find timings based on CEA-861 VIC
10365243076SHans Verkuil * @t: the timings data.
10465243076SHans Verkuil * @vic: CEA-861 VIC code
10565243076SHans Verkuil *
10665243076SHans Verkuil * On success it will fill in @t with the found timings and it returns true.
10765243076SHans Verkuil * On failure it will return false.
10865243076SHans Verkuil */
10965243076SHans Verkuil bool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings *t, u8 vic);
11065243076SHans Verkuil
11165243076SHans Verkuil /**
112506bb54bSMauro Carvalho Chehab * v4l2_match_dv_timings() - do two timings match?
113506bb54bSMauro Carvalho Chehab *
11425764158SHans Verkuil * @measured: the measured timings data.
11525764158SHans Verkuil * @standard: the timings according to the standard.
11625764158SHans Verkuil * @pclock_delta: maximum delta in Hz between standard->pixelclock and
11725764158SHans Verkuil * the measured timings.
11885f9e06cSHans Verkuil * @match_reduced_fps: if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
11985f9e06cSHans Verkuil * match.
12025764158SHans Verkuil *
12125764158SHans Verkuil * Returns true if the two timings match, returns false otherwise.
12225764158SHans Verkuil */
123ef1ed8f5SHans Verkuil bool v4l2_match_dv_timings(const struct v4l2_dv_timings *measured,
12425764158SHans Verkuil const struct v4l2_dv_timings *standard,
12585f9e06cSHans Verkuil unsigned pclock_delta, bool match_reduced_fps);
12625764158SHans Verkuil
127506bb54bSMauro Carvalho Chehab /**
128506bb54bSMauro Carvalho Chehab * v4l2_print_dv_timings() - log the contents of a dv_timings struct
1290216dc2fSHans Verkuil * @dev_prefix:device prefix for each log line.
1300216dc2fSHans Verkuil * @prefix: additional prefix for each log line, may be NULL.
1310216dc2fSHans Verkuil * @t: the timings data.
1320216dc2fSHans Verkuil * @detailed: if true, give a detailed log.
1330216dc2fSHans Verkuil */
1340216dc2fSHans Verkuil void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
1350216dc2fSHans Verkuil const struct v4l2_dv_timings *t, bool detailed);
1360216dc2fSHans Verkuil
137506bb54bSMauro Carvalho Chehab /**
138506bb54bSMauro Carvalho Chehab * v4l2_detect_cvt - detect if the given timings follow the CVT standard
139506bb54bSMauro Carvalho Chehab *
140fd4b0d75SGeliang Tang * @frame_height: the total height of the frame (including blanking) in lines.
141fd4b0d75SGeliang Tang * @hfreq: the horizontal frequency in Hz.
142fd4b0d75SGeliang Tang * @vsync: the height of the vertical sync in lines.
143fd4b0d75SGeliang Tang * @active_width: active width of image (does not include blanking). This
1445fea1bb7SPrashant Laddha * information is needed only in case of version 2 of reduced blanking.
1455fea1bb7SPrashant Laddha * In other cases, this parameter does not have any effect on timings.
146fd4b0d75SGeliang Tang * @polarities: the horizontal and vertical polarities (same as struct
14725764158SHans Verkuil * v4l2_bt_timings polarities).
148fd4b0d75SGeliang Tang * @interlaced: if this flag is true, it indicates interlaced format
149fd4b0d75SGeliang Tang * @fmt: the resulting timings.
15025764158SHans Verkuil *
15125764158SHans Verkuil * This function will attempt to detect if the given values correspond to a
15225764158SHans Verkuil * valid CVT format. If so, then it will return true, and fmt will be filled
15325764158SHans Verkuil * in with the found CVT timings.
15425764158SHans Verkuil */
15525764158SHans Verkuil bool v4l2_detect_cvt(unsigned frame_height, unsigned hfreq, unsigned vsync,
1565fea1bb7SPrashant Laddha unsigned active_width, u32 polarities, bool interlaced,
1575fea1bb7SPrashant Laddha struct v4l2_dv_timings *fmt);
15825764158SHans Verkuil
159506bb54bSMauro Carvalho Chehab /**
160506bb54bSMauro Carvalho Chehab * v4l2_detect_gtf - detect if the given timings follow the GTF standard
161506bb54bSMauro Carvalho Chehab *
162fd4b0d75SGeliang Tang * @frame_height: the total height of the frame (including blanking) in lines.
163fd4b0d75SGeliang Tang * @hfreq: the horizontal frequency in Hz.
164fd4b0d75SGeliang Tang * @vsync: the height of the vertical sync in lines.
165fd4b0d75SGeliang Tang * @polarities: the horizontal and vertical polarities (same as struct
16625764158SHans Verkuil * v4l2_bt_timings polarities).
167fd4b0d75SGeliang Tang * @interlaced: if this flag is true, it indicates interlaced format
168fd4b0d75SGeliang Tang * @aspect: preferred aspect ratio. GTF has no method of determining the
16925764158SHans Verkuil * aspect ratio in order to derive the image width from the
17025764158SHans Verkuil * image height, so it has to be passed explicitly. Usually
17125764158SHans Verkuil * the native screen aspect ratio is used for this. If it
17225764158SHans Verkuil * is not filled in correctly, then 16:9 will be assumed.
173fd4b0d75SGeliang Tang * @fmt: the resulting timings.
17425764158SHans Verkuil *
17525764158SHans Verkuil * This function will attempt to detect if the given values correspond to a
17625764158SHans Verkuil * valid GTF format. If so, then it will return true, and fmt will be filled
17725764158SHans Verkuil * in with the found GTF timings.
17825764158SHans Verkuil */
17925764158SHans Verkuil bool v4l2_detect_gtf(unsigned frame_height, unsigned hfreq, unsigned vsync,
180061ddda6SPrashant Laddha u32 polarities, bool interlaced, struct v4l2_fract aspect,
18125764158SHans Verkuil struct v4l2_dv_timings *fmt);
18225764158SHans Verkuil
183506bb54bSMauro Carvalho Chehab /**
184506bb54bSMauro Carvalho Chehab * v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
18525764158SHans Verkuil * 0x15 and 0x16 from the EDID.
186506bb54bSMauro Carvalho Chehab *
187fd4b0d75SGeliang Tang * @hor_landscape: byte 0x15 from the EDID.
188fd4b0d75SGeliang Tang * @vert_portrait: byte 0x16 from the EDID.
18925764158SHans Verkuil *
19025764158SHans Verkuil * Determines the aspect ratio from the EDID.
19125764158SHans Verkuil * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
19225764158SHans Verkuil * "Horizontal and Vertical Screen Size or Aspect Ratio"
19325764158SHans Verkuil */
19425764158SHans Verkuil struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait);
19525764158SHans Verkuil
19665243076SHans Verkuil /**
19765243076SHans Verkuil * v4l2_dv_timings_aspect_ratio - calculate the aspect ratio based on the
19865243076SHans Verkuil * v4l2_dv_timings information.
19965243076SHans Verkuil *
20065243076SHans Verkuil * @t: the timings data.
20165243076SHans Verkuil */
20265243076SHans Verkuil struct v4l2_fract v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings *t);
20365243076SHans Verkuil
2044ffbf143SMauro Carvalho Chehab /**
2054ffbf143SMauro Carvalho Chehab * can_reduce_fps - check if conditions for reduced fps are true.
2064ffbf143SMauro Carvalho Chehab * @bt: v4l2 timing structure
2074ffbf143SMauro Carvalho Chehab *
2084ffbf143SMauro Carvalho Chehab * For different timings reduced fps is allowed if the following conditions
2094ffbf143SMauro Carvalho Chehab * are met:
2104ffbf143SMauro Carvalho Chehab *
2114ffbf143SMauro Carvalho Chehab * - For CVT timings: if reduced blanking v2 (vsync == 8) is true.
2124ffbf143SMauro Carvalho Chehab * - For CEA861 timings: if %V4L2_DV_FL_CAN_REDUCE_FPS flag is true.
2138d7322f4SPrashant Laddha */
can_reduce_fps(struct v4l2_bt_timings * bt)2148d7322f4SPrashant Laddha static inline bool can_reduce_fps(struct v4l2_bt_timings *bt)
2158d7322f4SPrashant Laddha {
2168d7322f4SPrashant Laddha if ((bt->standards & V4L2_DV_BT_STD_CVT) && (bt->vsync == 8))
2178d7322f4SPrashant Laddha return true;
2188d7322f4SPrashant Laddha
2198d7322f4SPrashant Laddha if ((bt->standards & V4L2_DV_BT_STD_CEA861) &&
2208d7322f4SPrashant Laddha (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS))
2218d7322f4SPrashant Laddha return true;
2228d7322f4SPrashant Laddha
2238d7322f4SPrashant Laddha return false;
2248d7322f4SPrashant Laddha }
2258d7322f4SPrashant Laddha
226357a856aSHans Verkuil /**
227*b0649455SMauro Carvalho Chehab * struct v4l2_hdmi_colorimetry - describes the HDMI colorimetry information
228357a856aSHans Verkuil * @colorspace: enum v4l2_colorspace, the colorspace
229357a856aSHans Verkuil * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
230357a856aSHans Verkuil * @quantization: enum v4l2_quantization, colorspace quantization
231357a856aSHans Verkuil * @xfer_func: enum v4l2_xfer_func, colorspace transfer function
232357a856aSHans Verkuil */
233357a856aSHans Verkuil struct v4l2_hdmi_colorimetry {
234357a856aSHans Verkuil enum v4l2_colorspace colorspace;
235357a856aSHans Verkuil enum v4l2_ycbcr_encoding ycbcr_enc;
236357a856aSHans Verkuil enum v4l2_quantization quantization;
237357a856aSHans Verkuil enum v4l2_xfer_func xfer_func;
238357a856aSHans Verkuil };
239357a856aSHans Verkuil
240357a856aSHans Verkuil struct hdmi_avi_infoframe;
241357a856aSHans Verkuil struct hdmi_vendor_infoframe;
242357a856aSHans Verkuil
243357a856aSHans Verkuil struct v4l2_hdmi_colorimetry
244357a856aSHans Verkuil v4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe *avi,
245357a856aSHans Verkuil const struct hdmi_vendor_infoframe *hdmi,
246357a856aSHans Verkuil unsigned int height);
2478d7322f4SPrashant Laddha
2489cfd2753SHans Verkuil u16 v4l2_get_edid_phys_addr(const u8 *edid, unsigned int size,
2499cfd2753SHans Verkuil unsigned int *offset);
2509cfd2753SHans Verkuil void v4l2_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
2519cfd2753SHans Verkuil u16 v4l2_phys_addr_for_input(u16 phys_addr, u8 input);
2529cfd2753SHans Verkuil int v4l2_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
2539cfd2753SHans Verkuil
254b18787edSHans Verkuil #endif
255