v4l2-dv-timings.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * v4l2-dv-timings - Internal header with dv-timings helper functions
  4. *
  5. * Copyright 2013 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  6. */
  7. #ifndef __V4L2_DV_TIMINGS_H
  8. #define __V4L2_DV_TIMINGS_H
  9. #include <linux/debugfs.h>
  10. #include <linux/videodev2.h>
  11. /**
  12. * v4l2_calc_timeperframe - helper function to calculate timeperframe based
  13. * v4l2_dv_timings fields.
  14. * @t: Timings for the video mode.
  15. *
  16. * Calculates the expected timeperframe using the pixel clock value and
  17. * horizontal/vertical measures. This means that v4l2_dv_timings structure
  18. * must be correctly and fully filled.
  19. */
  20. struct v4l2_fract v4l2_calc_timeperframe(const struct v4l2_dv_timings *t);
  21. /*
  22. * v4l2_dv_timings_presets: list of all dv_timings presets.
  23. */
  24. extern const struct v4l2_dv_timings v4l2_dv_timings_presets[];
  25. /**
  26. * typedef v4l2_check_dv_timings_fnc - timings check callback
  27. *
  28. * @t: the v4l2_dv_timings struct.
  29. * @handle: a handle from the driver.
  30. *
  31. * Returns true if the given timings are valid.
  32. */
  33. typedef bool v4l2_check_dv_timings_fnc(const struct v4l2_dv_timings *t, void *handle);
  34. /**
  35. * v4l2_valid_dv_timings() - are these timings valid?
  36. *
  37. * @t: the v4l2_dv_timings struct.
  38. * @cap: the v4l2_dv_timings_cap capabilities.
  39. * @fnc: callback to check if this timing is OK. May be NULL.
  40. * @fnc_handle: a handle that is passed on to @fnc.
  41. *
  42. * Returns true if the given dv_timings struct is supported by the
  43. * hardware capabilities and the callback function (if non-NULL), returns
  44. * false otherwise.
  45. */
  46. bool v4l2_valid_dv_timings(const struct v4l2_dv_timings *t,
  47. const struct v4l2_dv_timings_cap *cap,
  48. v4l2_check_dv_timings_fnc fnc,
  49. void *fnc_handle);
  50. /**
  51. * v4l2_enum_dv_timings_cap() - Helper function to enumerate possible DV
  52. * timings based on capabilities
  53. *
  54. * @t: the v4l2_enum_dv_timings struct.
  55. * @cap: the v4l2_dv_timings_cap capabilities.
  56. * @fnc: callback to check if this timing is OK. May be NULL.
  57. * @fnc_handle: a handle that is passed on to @fnc.
  58. *
  59. * This enumerates dv_timings using the full list of possible CEA-861 and DMT
  60. * timings, filtering out any timings that are not supported based on the
  61. * hardware capabilities and the callback function (if non-NULL).
  62. *
  63. * If a valid timing for the given index is found, it will fill in @t and
  64. * return 0, otherwise it returns -EINVAL.
  65. */
  66. int v4l2_enum_dv_timings_cap(struct v4l2_enum_dv_timings *t,
  67. const struct v4l2_dv_timings_cap *cap,
  68. v4l2_check_dv_timings_fnc fnc,
  69. void *fnc_handle);
  70. /**
  71. * v4l2_find_dv_timings_cap() - Find the closest timings struct
  72. *
  73. * @t: the v4l2_enum_dv_timings struct.
  74. * @cap: the v4l2_dv_timings_cap capabilities.
  75. * @pclock_delta: maximum delta between t->pixelclock and the timing struct
  76. * under consideration.
  77. * @fnc: callback to check if a given timings struct is OK. May be NULL.
  78. * @fnc_handle: a handle that is passed on to @fnc.
  79. *
  80. * This function tries to map the given timings to an entry in the
  81. * full list of possible CEA-861 and DMT timings, filtering out any timings
  82. * that are not supported based on the hardware capabilities and the callback
  83. * function (if non-NULL).
  84. *
  85. * On success it will fill in @t with the found timings and it returns true.
  86. * On failure it will return false.
  87. */
  88. bool v4l2_find_dv_timings_cap(struct v4l2_dv_timings *t,
  89. const struct v4l2_dv_timings_cap *cap,
  90. unsigned pclock_delta,
  91. v4l2_check_dv_timings_fnc fnc,
  92. void *fnc_handle);
  93. /**
  94. * v4l2_find_dv_timings_cea861_vic() - find timings based on CEA-861 VIC
  95. * @t: the timings data.
  96. * @vic: CEA-861 VIC code
  97. *
  98. * On success it will fill in @t with the found timings and it returns true.
  99. * On failure it will return false.
  100. */
  101. bool v4l2_find_dv_timings_cea861_vic(struct v4l2_dv_timings *t, u8 vic);
  102. /**
  103. * v4l2_match_dv_timings() - do two timings match?
  104. *
  105. * @measured: the measured timings data.
  106. * @standard: the timings according to the standard.
  107. * @pclock_delta: maximum delta in Hz between standard->pixelclock and
  108. * the measured timings.
  109. * @match_reduced_fps: if true, then fail if V4L2_DV_FL_REDUCED_FPS does not
  110. * match.
  111. *
  112. * Returns true if the two timings match, returns false otherwise.
  113. */
  114. bool v4l2_match_dv_timings(const struct v4l2_dv_timings *measured,
  115. const struct v4l2_dv_timings *standard,
  116. unsigned pclock_delta, bool match_reduced_fps);
  117. /**
  118. * v4l2_print_dv_timings() - log the contents of a dv_timings struct
  119. * @dev_prefix:device prefix for each log line.
  120. * @prefix: additional prefix for each log line, may be NULL.
  121. * @t: the timings data.
  122. * @detailed: if true, give a detailed log.
  123. */
  124. void v4l2_print_dv_timings(const char *dev_prefix, const char *prefix,
  125. const struct v4l2_dv_timings *t, bool detailed);
  126. /**
  127. * v4l2_detect_cvt - detect if the given timings follow the CVT standard
  128. *
  129. * @frame_height: the total height of the frame (including blanking) in lines.
  130. * @hfreq: the horizontal frequency in Hz.
  131. * @vsync: the height of the vertical sync in lines.
  132. * @active_width: active width of image (does not include blanking). This
  133. * information is needed only in case of version 2 of reduced blanking.
  134. * In other cases, this parameter does not have any effect on timings.
  135. * @polarities: the horizontal and vertical polarities (same as struct
  136. * v4l2_bt_timings polarities).
  137. * @interlaced: if this flag is true, it indicates interlaced format
  138. * @cap: the v4l2_dv_timings_cap capabilities.
  139. * @fmt: the resulting timings.
  140. *
  141. * This function will attempt to detect if the given values correspond to a
  142. * valid CVT format. If so, then it will return true, and fmt will be filled
  143. * in with the found CVT timings.
  144. */
  145. bool v4l2_detect_cvt(unsigned int frame_height, unsigned int hfreq,
  146. unsigned int vsync, unsigned int active_width,
  147. u32 polarities, bool interlaced,
  148. const struct v4l2_dv_timings_cap *cap,
  149. struct v4l2_dv_timings *fmt);
  150. /**
  151. * v4l2_detect_gtf - detect if the given timings follow the GTF standard
  152. *
  153. * @frame_height: the total height of the frame (including blanking) in lines.
  154. * @hfreq: the horizontal frequency in Hz.
  155. * @vsync: the height of the vertical sync in lines.
  156. * @polarities: the horizontal and vertical polarities (same as struct
  157. * v4l2_bt_timings polarities).
  158. * @interlaced: if this flag is true, it indicates interlaced format
  159. * @aspect: preferred aspect ratio. GTF has no method of determining the
  160. * aspect ratio in order to derive the image width from the
  161. * image height, so it has to be passed explicitly. Usually
  162. * the native screen aspect ratio is used for this. If it
  163. * is not filled in correctly, then 16:9 will be assumed.
  164. * @cap: the v4l2_dv_timings_cap capabilities.
  165. * @fmt: the resulting timings.
  166. *
  167. * This function will attempt to detect if the given values correspond to a
  168. * valid GTF format. If so, then it will return true, and fmt will be filled
  169. * in with the found GTF timings.
  170. */
  171. bool v4l2_detect_gtf(unsigned int frame_height, unsigned int hfreq,
  172. unsigned int vsync, u32 polarities, bool interlaced,
  173. struct v4l2_fract aspect,
  174. const struct v4l2_dv_timings_cap *cap,
  175. struct v4l2_dv_timings *fmt);
  176. /**
  177. * v4l2_calc_aspect_ratio - calculate the aspect ratio based on bytes
  178. * 0x15 and 0x16 from the EDID.
  179. *
  180. * @hor_landscape: byte 0x15 from the EDID.
  181. * @vert_portrait: byte 0x16 from the EDID.
  182. *
  183. * Determines the aspect ratio from the EDID.
  184. * See VESA Enhanced EDID standard, release A, rev 2, section 3.6.2:
  185. * "Horizontal and Vertical Screen Size or Aspect Ratio"
  186. */
  187. struct v4l2_fract v4l2_calc_aspect_ratio(u8 hor_landscape, u8 vert_portrait);
  188. /**
  189. * v4l2_dv_timings_aspect_ratio - calculate the aspect ratio based on the
  190. * v4l2_dv_timings information.
  191. *
  192. * @t: the timings data.
  193. */
  194. struct v4l2_fract v4l2_dv_timings_aspect_ratio(const struct v4l2_dv_timings *t);
  195. /**
  196. * can_reduce_fps - check if conditions for reduced fps are true.
  197. * @bt: v4l2 timing structure
  198. *
  199. * For different timings reduced fps is allowed if the following conditions
  200. * are met:
  201. *
  202. * - For CVT timings: if reduced blanking v2 (vsync == 8) is true.
  203. * - For CEA861 timings: if %V4L2_DV_FL_CAN_REDUCE_FPS flag is true.
  204. */
  205. static inline bool can_reduce_fps(struct v4l2_bt_timings *bt)
  206. {
  207. if ((bt->standards & V4L2_DV_BT_STD_CVT) && (bt->vsync == 8))
  208. return true;
  209. if ((bt->standards & V4L2_DV_BT_STD_CEA861) &&
  210. (bt->flags & V4L2_DV_FL_CAN_REDUCE_FPS))
  211. return true;
  212. return false;
  213. }
  214. /**
  215. * struct v4l2_hdmi_colorimetry - describes the HDMI colorimetry information
  216. * @colorspace: enum v4l2_colorspace, the colorspace
  217. * @ycbcr_enc: enum v4l2_ycbcr_encoding, Y'CbCr encoding
  218. * @quantization: enum v4l2_quantization, colorspace quantization
  219. * @xfer_func: enum v4l2_xfer_func, colorspace transfer function
  220. */
  221. struct v4l2_hdmi_colorimetry {
  222. enum v4l2_colorspace colorspace;
  223. enum v4l2_ycbcr_encoding ycbcr_enc;
  224. enum v4l2_quantization quantization;
  225. enum v4l2_xfer_func xfer_func;
  226. };
  227. struct hdmi_avi_infoframe;
  228. struct hdmi_vendor_infoframe;
  229. struct v4l2_hdmi_colorimetry
  230. v4l2_hdmi_rx_colorimetry(const struct hdmi_avi_infoframe *avi,
  231. const struct hdmi_vendor_infoframe *hdmi,
  232. unsigned int height);
  233. unsigned int v4l2_num_edid_blocks(const u8 *edid, unsigned int max_blocks);
  234. u16 v4l2_get_edid_phys_addr(const u8 *edid, unsigned int size,
  235. unsigned int *offset);
  236. void v4l2_set_edid_phys_addr(u8 *edid, unsigned int size, u16 phys_addr);
  237. u16 v4l2_phys_addr_for_input(u16 phys_addr, u8 input);
  238. int v4l2_phys_addr_validate(u16 phys_addr, u16 *parent, u16 *port);
  239. /* Add support for exporting InfoFrames to debugfs */
  240. /*
  241. * HDMI InfoFrames start with a 3 byte header, then a checksum,
  242. * followed by the actual IF payload.
  243. *
  244. * The payload length is limited to 30 bytes according to the HDMI spec,
  245. * but since the length is encoded in 5 bits, it can be 31 bytes theoretically.
  246. * So set the max length as 31 + 3 (header) + 1 (checksum) = 35.
  247. */
  248. #define V4L2_DEBUGFS_IF_MAX_LEN (35)
  249. #define V4L2_DEBUGFS_IF_AVI BIT(0)
  250. #define V4L2_DEBUGFS_IF_AUDIO BIT(1)
  251. #define V4L2_DEBUGFS_IF_SPD BIT(2)
  252. #define V4L2_DEBUGFS_IF_HDMI BIT(3)
  253. #define V4L2_DEBUGFS_IF_DRM BIT(4)
  254. typedef ssize_t (*v4l2_debugfs_if_read_t)(u32 type, void *priv,
  255. struct file *filp, char __user *ubuf,
  256. size_t count, loff_t *ppos);
  257. struct v4l2_debugfs_if {
  258. struct dentry *if_dir;
  259. void *priv;
  260. v4l2_debugfs_if_read_t if_read;
  261. };
  262. #ifdef CONFIG_DEBUG_FS
  263. struct v4l2_debugfs_if *v4l2_debugfs_if_alloc(struct dentry *root, u32 if_types,
  264. void *priv,
  265. v4l2_debugfs_if_read_t if_read);
  266. void v4l2_debugfs_if_free(struct v4l2_debugfs_if *infoframes);
  267. #else
  268. static inline
  269. struct v4l2_debugfs_if *v4l2_debugfs_if_alloc(struct dentry *root, u32 if_types,
  270. void *priv,
  271. v4l2_debugfs_if_read_t if_read)
  272. {
  273. return NULL;
  274. }
  275. static inline void v4l2_debugfs_if_free(struct v4l2_debugfs_if *infoframes)
  276. {
  277. }
  278. #endif
  279. #endif