<div dir="ltr">Hi, your statement:<div><br></div><div>"However, analog display usually have fairly loose timings requirements,</div>the only discrete parameters being the total number of lines and pixel<br>clock frequency."<div><br></div><div>Please do not make it as a rule. You said yourself: "usually". Arcade CRT have more loose timings, but professional broadcast TV's such as Sony PVM, Sony BVM, JVC. These cost tens of thousand dollars back in the day. Now they are affordable for gamers. I just solved issue in Retroarch, CRT Switchres library here: <a href="https://github.com/antonioginer/switchres/issues/96">https://github.com/antonioginer/switchres/issues/96</a></div><div><br></div><div>This model is quite common among retrogamers and on Reddit.</div><div><br></div><div>Some developers do not test it properly.</div><div><br></div><div>This model requires exact number of lines.</div><div><br></div><div>For Switchres we came up with these ranges:</div><div><pre class="gmail-notranslate" style="box-sizing:border-box;font-family:ui-monospace,SFMono-Regular,"SF Mono",Menlo,Consolas,"Liberation Mono",monospace;font-size:11.9px;margin-top:0px;margin-bottom:16px;padding:16px;overflow:auto;line-height:1.45;border-radius:6px;color:rgb(36,41,47)"><code style="box-sizing:border-box;font-family:ui-monospace,SFMono-Regular,"SF Mono",Menlo,Consolas,"Liberation Mono",monospace;padding:0px;margin:0px;background:transparent;border-radius:6px;word-break:normal;border:0px;display:inline;overflow:visible;line-height:inherit">        crt_range0 15625-15750, 49.50-65.00, 2.000, 4.700, 8.000, 0.064, 0.192, 1.024, 1, 1, 192, 288, 0, 0
        crt_range1 15625.00-15625.00, 50.00-50.00, 1.500, 4.700, 5.800, 0.064, 0.160, 1.056, 1, 1, 0, 0, 448, 576
        crt_range2 15734.26-15734.26, 59.94-59.94, 1.500, 4.700, 4.700, 0.191, 0.191, 0.953, 1, 1, 0, 0, 448, 480
</code></pre>crt_range0 is default, more loose definition for MAME emulators. crt_range1 is PAL and crt_range2 is NTSC.</div><div><br></div><div>Yes, this model does support both NTSC and PAL.</div><div><br></div><div>Does your driver or library support that?</div><div><br></div><div>For example old driver in Windows 7 with NVIDIA 2007 driver on Geforce 7600 can support both NTSC and PAL and these are being switched automatically by the resolution you choose. So in desktop properties, you change to 640x480 and it will switch TV chipset to NTSC 480i. Then you change to 720x576 and it will switch TV chipset to PAL 576i.</div><div><br></div><div>It would be preferred if advanced users could set up these numbers from a commandline during a runtime, so it would depend on the app being used.</div><div><br></div><div>Lukas</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Nov 7, 2022 at 3:17 PM Maxime Ripard <maxime@cerno.tech> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Multiple drivers (meson, vc4, sun4i) define analog TV 525-lines and<br>
625-lines modes in their drivers.<br>
<br>
Since those modes are fairly standard, and that we'll need to use them<br>
in more places in the future, it makes sense to move their definition<br>
into the core framework.<br>
<br>
However, analog display usually have fairly loose timings requirements,<br>
the only discrete parameters being the total number of lines and pixel<br>
clock frequency. Thus, we created a function that will create a display<br>
mode from the standard, the pixel frequency and the active area.<br>
<br>
Signed-off-by: Maxime Ripard <maxime@cerno.tech><br>
<br>
---<br>
Changes in v6:<br>
- Fix typo<br>
<br>
Changes in v4:<br>
- Reworded the line length check comment<br>
- Switch to HZ_PER_KHZ in tests<br>
- Use previous timing to fill our mode<br>
- Move the number of lines check earlier<br>
---<br>
 drivers/gpu/drm/drm_modes.c            | 474 +++++++++++++++++++++++++++++++++<br>
 drivers/gpu/drm/tests/Makefile         |   1 +<br>
 drivers/gpu/drm/tests/drm_modes_test.c | 144 ++++++++++<br>
 include/drm/drm_modes.h                |  17 ++<br>
 4 files changed, 636 insertions(+)<br>
<br>
diff --git a/drivers/gpu/drm/drm_modes.c b/drivers/gpu/drm/drm_modes.c<br>
index 5d4ac79381c4..71c050c3ee6b 100644<br>
--- a/drivers/gpu/drm/drm_modes.c<br>
+++ b/drivers/gpu/drm/drm_modes.c<br>
@@ -116,6 +116,480 @@ void drm_mode_probed_add(struct drm_connector *connector,<br>
 }<br>
 EXPORT_SYMBOL(drm_mode_probed_add);<br>
<br>
+enum drm_mode_analog {<br>
+       DRM_MODE_ANALOG_NTSC, /* 525 lines, 60Hz */<br>
+       DRM_MODE_ANALOG_PAL, /* 625 lines, 50Hz */<br>
+};<br>
+<br>
+/*<br>
+ * The timings come from:<br>
+ * - <a href="https://web.archive.org/web/20220406232708/http://www.kolumbus.fi/pami1/video/pal_ntsc.html" rel="noreferrer" target="_blank">https://web.archive.org/web/20220406232708/http://www.kolumbus.fi/pami1/video/pal_ntsc.html</a><br>
+ * - <a href="https://web.archive.org/web/20220406124914/http://martin.hinner.info/vga/pal.html" rel="noreferrer" target="_blank">https://web.archive.org/web/20220406124914/http://martin.hinner.info/vga/pal.html</a><br>
+ * - <a href="https://web.archive.org/web/20220609202433/http://www.batsocks.co.uk/readme/video_timing.htm" rel="noreferrer" target="_blank">https://web.archive.org/web/20220609202433/http://www.batsocks.co.uk/readme/video_timing.htm</a><br>
+ */<br>
+#define NTSC_LINE_DURATION_NS          63556U<br>
+#define NTSC_LINES_NUMBER              525<br>
+<br>
+#define NTSC_HBLK_DURATION_TYP_NS      10900U<br>
+#define NTSC_HBLK_DURATION_MIN_NS      (NTSC_HBLK_DURATION_TYP_NS - 200)<br>
+#define NTSC_HBLK_DURATION_MAX_NS      (NTSC_HBLK_DURATION_TYP_NS + 200)<br>
+<br>
+#define NTSC_HACT_DURATION_TYP_NS      (NTSC_LINE_DURATION_NS - NTSC_HBLK_DURATION_TYP_NS)<br>
+#define NTSC_HACT_DURATION_MIN_NS      (NTSC_LINE_DURATION_NS - NTSC_HBLK_DURATION_MAX_NS)<br>
+#define NTSC_HACT_DURATION_MAX_NS      (NTSC_LINE_DURATION_NS - NTSC_HBLK_DURATION_MIN_NS)<br>
+<br>
+#define NTSC_HFP_DURATION_TYP_NS       1500<br>
+#define NTSC_HFP_DURATION_MIN_NS       1270<br>
+#define NTSC_HFP_DURATION_MAX_NS       2220<br>
+<br>
+#define NTSC_HSLEN_DURATION_TYP_NS     4700<br>
+#define NTSC_HSLEN_DURATION_MIN_NS     (NTSC_HSLEN_DURATION_TYP_NS - 100)<br>
+#define NTSC_HSLEN_DURATION_MAX_NS     (NTSC_HSLEN_DURATION_TYP_NS + 100)<br>
+<br>
+#define NTSC_HBP_DURATION_TYP_NS       4700<br>
+<br>
+/*<br>
+ * I couldn't find the actual tolerance for the back porch, so let's<br>
+ * just reuse the sync length ones.<br>
+ */<br>
+#define NTSC_HBP_DURATION_MIN_NS       (NTSC_HBP_DURATION_TYP_NS - 100)<br>
+#define NTSC_HBP_DURATION_MAX_NS       (NTSC_HBP_DURATION_TYP_NS + 100)<br>
+<br>
+#define PAL_LINE_DURATION_NS           64000U<br>
+#define PAL_LINES_NUMBER               625<br>
+<br>
+#define PAL_HACT_DURATION_TYP_NS       51950U<br>
+#define PAL_HACT_DURATION_MIN_NS       (PAL_HACT_DURATION_TYP_NS - 100)<br>
+#define PAL_HACT_DURATION_MAX_NS       (PAL_HACT_DURATION_TYP_NS + 400)<br>
+<br>
+#define PAL_HBLK_DURATION_TYP_NS       (PAL_LINE_DURATION_NS - PAL_HACT_DURATION_TYP_NS)<br>
+#define PAL_HBLK_DURATION_MIN_NS       (PAL_LINE_DURATION_NS - PAL_HACT_DURATION_MAX_NS)<br>
+#define PAL_HBLK_DURATION_MAX_NS       (PAL_LINE_DURATION_NS - PAL_HACT_DURATION_MIN_NS)<br>
+<br>
+#define PAL_HFP_DURATION_TYP_NS                1650<br>
+#define PAL_HFP_DURATION_MIN_NS                (PAL_HFP_DURATION_TYP_NS - 100)<br>
+#define PAL_HFP_DURATION_MAX_NS                (PAL_HFP_DURATION_TYP_NS + 400)<br>
+<br>
+#define PAL_HSLEN_DURATION_TYP_NS      4700<br>
+#define PAL_HSLEN_DURATION_MIN_NS      (PAL_HSLEN_DURATION_TYP_NS - 200)<br>
+#define PAL_HSLEN_DURATION_MAX_NS      (PAL_HSLEN_DURATION_TYP_NS + 200)<br>
+<br>
+#define PAL_HBP_DURATION_TYP_NS                5700<br>
+#define PAL_HBP_DURATION_MIN_NS                (PAL_HBP_DURATION_TYP_NS - 200)<br>
+#define PAL_HBP_DURATION_MAX_NS                (PAL_HBP_DURATION_TYP_NS + 200)<br>
+<br>
+struct analog_param_field {<br>
+       unsigned int even, odd;<br>
+};<br>
+<br>
+#define PARAM_FIELD(_odd, _even)               \<br>
+       { .even = _even, .odd = _odd }<br>
+<br>
+struct analog_param_range {<br>
+       unsigned int    min, typ, max;<br>
+};<br>
+<br>
+#define PARAM_RANGE(_min, _typ, _max)          \<br>
+       { .min = _min, .typ = _typ, .max = _max }<br>
+<br>
+struct analog_parameters {<br>
+       unsigned int                    num_lines;<br>
+       unsigned int                    line_duration_ns;<br>
+<br>
+       struct analog_param_range       hact_ns;<br>
+       struct analog_param_range       hfp_ns;<br>
+       struct analog_param_range       hslen_ns;<br>
+       struct analog_param_range       hbp_ns;<br>
+       struct analog_param_range       hblk_ns;<br>
+<br>
+       unsigned int                    bt601_hfp;<br>
+<br>
+       struct analog_param_field       vfp_lines;<br>
+       struct analog_param_field       vslen_lines;<br>
+       struct analog_param_field       vbp_lines;<br>
+};<br>
+<br>
+#define TV_MODE_PARAMETER(_mode, _lines, _line_dur, _hact, _hfp, _hslen, _hbp, _hblk, _bt601_hfp, _vfp, _vslen, _vbp) \<br>
+       [_mode] = {                                                     \<br>
+               .num_lines = _lines,                                    \<br>
+               .line_duration_ns = _line_dur,                          \<br>
+               .hact_ns = _hact,                                       \<br>
+               .hfp_ns = _hfp,                                         \<br>
+               .hslen_ns = _hslen,                                     \<br>
+               .hbp_ns = _hbp,                                         \<br>
+               .hblk_ns = _hblk,                                       \<br>
+               .bt601_hfp = _bt601_hfp,                                \<br>
+               .vfp_lines = _vfp,                                      \<br>
+               .vslen_lines = _vslen,                                  \<br>
+               .vbp_lines = _vbp,                                      \<br>
+       }<br>
+<br>
+const static struct analog_parameters tv_modes_parameters[] = {<br>
+       TV_MODE_PARAMETER(DRM_MODE_ANALOG_NTSC,<br>
+                         NTSC_LINES_NUMBER,<br>
+                         NTSC_LINE_DURATION_NS,<br>
+                         PARAM_RANGE(NTSC_HACT_DURATION_MIN_NS,<br>
+                                     NTSC_HACT_DURATION_TYP_NS,<br>
+                                     NTSC_HACT_DURATION_MAX_NS),<br>
+                         PARAM_RANGE(NTSC_HFP_DURATION_MIN_NS,<br>
+                                     NTSC_HFP_DURATION_TYP_NS,<br>
+                                     NTSC_HFP_DURATION_MAX_NS),<br>
+                         PARAM_RANGE(NTSC_HSLEN_DURATION_MIN_NS,<br>
+                                     NTSC_HSLEN_DURATION_TYP_NS,<br>
+                                     NTSC_HSLEN_DURATION_MAX_NS),<br>
+                         PARAM_RANGE(NTSC_HBP_DURATION_MIN_NS,<br>
+                                     NTSC_HBP_DURATION_TYP_NS,<br>
+                                     NTSC_HBP_DURATION_MAX_NS),<br>
+                         PARAM_RANGE(NTSC_HBLK_DURATION_MIN_NS,<br>
+                                     NTSC_HBLK_DURATION_TYP_NS,<br>
+                                     NTSC_HBLK_DURATION_MAX_NS),<br>
+                         16,<br>
+                         PARAM_FIELD(3, 3),<br>
+                         PARAM_FIELD(3, 3),<br>
+                         PARAM_FIELD(16, 17)),<br>
+       TV_MODE_PARAMETER(DRM_MODE_ANALOG_PAL,<br>
+                         PAL_LINES_NUMBER,<br>
+                         PAL_LINE_DURATION_NS,<br>
+                         PARAM_RANGE(PAL_HACT_DURATION_MIN_NS,<br>
+                                     PAL_HACT_DURATION_TYP_NS,<br>
+                                     PAL_HACT_DURATION_MAX_NS),<br>
+                         PARAM_RANGE(PAL_HFP_DURATION_MIN_NS,<br>
+                                     PAL_HFP_DURATION_TYP_NS,<br>
+                                     PAL_HFP_DURATION_MAX_NS),<br>
+                         PARAM_RANGE(PAL_HSLEN_DURATION_MIN_NS,<br>
+                                     PAL_HSLEN_DURATION_TYP_NS,<br>
+                                     PAL_HSLEN_DURATION_MAX_NS),<br>
+                         PARAM_RANGE(PAL_HBP_DURATION_MIN_NS,<br>
+                                     PAL_HBP_DURATION_TYP_NS,<br>
+                                     PAL_HBP_DURATION_MAX_NS),<br>
+                         PARAM_RANGE(PAL_HBLK_DURATION_MIN_NS,<br>
+                                     PAL_HBLK_DURATION_TYP_NS,<br>
+                                     PAL_HBLK_DURATION_MAX_NS),<br>
+                         12,<br>
+<br>
+                         /*<br>
+                          * The front porch is actually 6 short sync<br>
+                          * pulses for the even field, and 5 for the<br>
+                          * odd field. Each sync takes half a life so<br>
+                          * the odd field front porch is shorter by<br>
+                          * half a line.<br>
+                          *<br>
+                          * In progressive, we're supposed to use 6<br>
+                          * pulses, so we're fine there<br>
+                          */<br>
+                         PARAM_FIELD(3, 2),<br>
+<br>
+                         /*<br>
+                          * The vsync length is 5 long sync pulses,<br>
+                          * each field taking half a line. We're<br>
+                          * shorter for both fields by half a line.<br>
+                          *<br>
+                          * In progressive, we're supposed to use 5<br>
+                          * pulses, so we're off by half<br>
+                          * a line.<br>
+                          *<br>
+                          * In interlace, we're now off by half a line<br>
+                          * for the even field and one line for the odd<br>
+                          * field.<br>
+                          */<br>
+                         PARAM_FIELD(3, 3),<br>
+<br>
+                         /*<br>
+                          * The back porch starts with post-equalizing<br>
+                          * pulses, consisting in 5 short sync pulses<br>
+                          * for the even field, 4 for the odd field. In<br>
+                          * progressive, it's 5 short syncs.<br>
+                          *<br>
+                          * In progressive, we thus have 2.5 lines,<br>
+                          * plus the 0.5 line we were missing<br>
+                          * previously, so we should use 3 lines.<br>
+                          *<br>
+                          * In interlace, the even field is in the<br>
+                          * exact same case than progressive. For the<br>
+                          * odd field, we should be using 2 lines but<br>
+                          * we're one line short, so we'll make up for<br>
+                          * it here by using 3.<br>
+                          *<br>
+                          * The entire blanking area is supposed to<br>
+                          * take 25 lines, so we also need to account<br>
+                          * for the rest of the blanking area that<br>
+                          * can't be in either the front porch or sync<br>
+                          * period.<br>
+                          */<br>
+                         PARAM_FIELD(19, 20)),<br>
+};<br>
+<br>
+static int fill_analog_mode(struct drm_device *dev,<br>
+                           struct drm_display_mode *mode,<br>
+                           const struct analog_parameters *params,<br>
+                           unsigned long pixel_clock_hz,<br>
+                           unsigned int hactive,<br>
+                           unsigned int vactive,<br>
+                           bool interlace)<br>
+{<br>
+       unsigned long pixel_duration_ns = NSEC_PER_SEC / pixel_clock_hz;<br>
+       unsigned int htotal, vtotal;<br>
+       unsigned int max_hact, hact_duration_ns;<br>
+       unsigned int hblk, hblk_duration_ns;<br>
+       unsigned int hfp, hfp_duration_ns;<br>
+       unsigned int hslen, hslen_duration_ns;<br>
+       unsigned int hbp, hbp_duration_ns;<br>
+       unsigned int porches, porches_duration_ns;<br>
+       unsigned int vfp, vfp_min;<br>
+       unsigned int vbp, vbp_min;<br>
+       unsigned int vslen;<br>
+       bool bt601 = false;<br>
+       int porches_rem;<br>
+       u64 result;<br>
+<br>
+       drm_dbg_kms(dev,<br>
+                   "Generating a %ux%u%c, %u-line mode with a %lu kHz clock\n",<br>
+                   hactive, vactive,<br>
+                   interlace ? 'i' : 'p',<br>
+                   params->num_lines,<br>
+                   pixel_clock_hz / 1000);<br>
+<br>
+       max_hact = params->hact_ns.max / pixel_duration_ns;<br>
+       if (pixel_clock_hz == 13500000 && hactive > max_hact && hactive <= 720) {<br>
+               drm_dbg_kms(dev, "Trying to generate a BT.601 mode. Disabling checks.\n");<br>
+               bt601 = true;<br>
+       }<br>
+<br>
+       /*<br>
+        * Our pixel duration is going to be round down by the division,<br>
+        * so rounding up is probably going to introduce even more<br>
+        * deviation.<br>
+        */<br>
+       result = (u64)params->line_duration_ns * pixel_clock_hz;<br>
+       do_div(result, NSEC_PER_SEC);<br>
+       htotal = result;<br>
+<br>
+       drm_dbg_kms(dev, "Total Horizontal Number of Pixels: %u\n", htotal);<br>
+<br>
+       hact_duration_ns = hactive * pixel_duration_ns;<br>
+       if (!bt601 &&<br>
+           (hact_duration_ns < params->hact_ns.min ||<br>
+            hact_duration_ns > params->hact_ns.max)) {<br>
+               DRM_ERROR("Invalid horizontal active area duration: %uns (min: %u, max %u)\n",<br>
+                         hact_duration_ns, params->hact_ns.min, params->hact_ns.max);<br>
+               return -EINVAL;<br>
+       }<br>
+<br>
+       hblk = htotal - hactive;<br>
+       drm_dbg_kms(dev, "Horizontal Blanking Period: %u\n", hblk);<br>
+<br>
+       hblk_duration_ns = hblk * pixel_duration_ns;<br>
+       if (!bt601 &&<br>
+           (hblk_duration_ns < params->hblk_ns.min ||<br>
+            hblk_duration_ns > params->hblk_ns.max)) {<br>
+               DRM_ERROR("Invalid horizontal blanking duration: %uns (min: %u, max %u)\n",<br>
+                         hblk_duration_ns, params->hblk_ns.min, params->hblk_ns.max);<br>
+               return -EINVAL;<br>
+       }<br>
+<br>
+       hslen = DIV_ROUND_UP(params->hslen_ns.typ, pixel_duration_ns);<br>
+       drm_dbg_kms(dev, "Horizontal Sync Period: %u\n", hslen);<br>
+<br>
+       hslen_duration_ns = hslen * pixel_duration_ns;<br>
+       if (!bt601 &&<br>
+           (hslen_duration_ns < params->hslen_ns.min ||<br>
+            hslen_duration_ns > params->hslen_ns.max)) {<br>
+               DRM_ERROR("Invalid horizontal sync duration: %uns (min: %u, max %u)\n",<br>
+                         hslen_duration_ns, params->hslen_ns.min, params->hslen_ns.max);<br>
+               return -EINVAL;<br>
+       }<br>
+<br>
+       porches = hblk - hslen;<br>
+       drm_dbg_kms(dev, "Remaining horizontal pixels for both porches: %u\n", porches);<br>
+<br>
+       porches_duration_ns = porches * pixel_duration_ns;<br>
+       if (!bt601 &&<br>
+           (porches_duration_ns > (params->hfp_ns.max + params->hbp_ns.max) ||<br>
+            porches_duration_ns < (params->hfp_ns.min + params->hbp_ns.min))) {<br>
+               DRM_ERROR("Invalid horizontal porches duration: %uns\n", porches_duration_ns);<br>
+               return -EINVAL;<br>
+       }<br>
+<br>
+       if (bt601) {<br>
+               hfp = params->bt601_hfp;<br>
+       } else {<br>
+               unsigned int hfp_min = DIV_ROUND_UP(params->hfp_ns.min,<br>
+                                                   pixel_duration_ns);<br>
+               unsigned int hbp_min = DIV_ROUND_UP(params->hbp_ns.min,<br>
+                                                   pixel_duration_ns);<br>
+                int porches_rem = porches - hfp_min - hbp_min;<br>
+<br>
+               hfp = hfp_min + DIV_ROUND_UP(porches_rem, 2);<br>
+       }<br>
+<br>
+       drm_dbg_kms(dev, "Horizontal Front Porch: %u\n", hfp);<br>
+<br>
+       hfp_duration_ns = hfp * pixel_duration_ns;<br>
+       if (!bt601 &&<br>
+           (hfp_duration_ns < params->hfp_ns.min ||<br>
+            hfp_duration_ns > params->hfp_ns.max)) {<br>
+               DRM_ERROR("Invalid horizontal front porch duration: %uns (min: %u, max %u)\n",<br>
+                         hfp_duration_ns, params->hfp_ns.min, params->hfp_ns.max);<br>
+               return -EINVAL;<br>
+       }<br>
+<br>
+       hbp = porches - hfp;<br>
+       drm_dbg_kms(dev, "Horizontal Back Porch: %u\n", hbp);<br>
+<br>
+       hbp_duration_ns = hbp * pixel_duration_ns;<br>
+       if (!bt601 &&<br>
+           (hbp_duration_ns < params->hbp_ns.min ||<br>
+            hbp_duration_ns > params->hbp_ns.max)) {<br>
+               DRM_ERROR("Invalid horizontal back porch duration: %uns (min: %u, max %u)\n",<br>
+                         hbp_duration_ns, params->hbp_ns.min, params->hbp_ns.max);<br>
+               return -EINVAL;<br>
+       }<br>
+<br>
+       if (htotal != (hactive + hfp + hslen + hbp))<br>
+               return -EINVAL;<br>
+<br>
+       mode->clock = pixel_clock_hz / 1000;<br>
+       mode->hdisplay = hactive;<br>
+       mode->hsync_start = mode->hdisplay + hfp;<br>
+       mode->hsync_end = mode->hsync_start + hslen;<br>
+       mode->htotal = mode->hsync_end + hbp;<br>
+<br>
+       if (interlace) {<br>
+               vfp_min = params->vfp_lines.even + params->vfp_lines.odd;<br>
+               vbp_min = params->vbp_lines.even + params->vbp_lines.odd;<br>
+               vslen = params->vslen_lines.even + params->vslen_lines.odd;<br>
+       } else {<br>
+               /*<br>
+                * By convention, NTSC (aka 525/60) systems start with<br>
+                * the even field, but PAL (aka 625/50) systems start<br>
+                * with the odd one.<br>
+                *<br>
+                * PAL systems also have asymetric timings between the<br>
+                * even and odd field, while NTSC is symetric.<br>
+                *<br>
+                * Moreover, if we want to create a progressive mode for<br>
+                * PAL, we need to use the odd field timings.<br>
+                *<br>
+                * Since odd == even for NTSC, we can just use the odd<br>
+                * one all the time to simplify the code a bit.<br>
+                */<br>
+               vfp_min = params->vfp_lines.odd;<br>
+               vbp_min = params->vbp_lines.odd;<br>
+               vslen = params->vslen_lines.odd;<br>
+       }<br>
+<br>
+       drm_dbg_kms(dev, "Vertical Sync Period: %u\n", vslen);<br>
+<br>
+       porches = params->num_lines - vactive - vslen;<br>
+       drm_dbg_kms(dev, "Remaining vertical pixels for both porches: %u\n", porches);<br>
+<br>
+       porches_rem = porches - vfp_min - vbp_min;<br>
+       vfp = vfp_min + (porches_rem / 2);<br>
+       drm_dbg_kms(dev, "Vertical Front Porch: %u\n", vfp);<br>
+<br>
+       vbp = porches - vfp;<br>
+       drm_dbg_kms(dev, "Vertical Back Porch: %u\n", vbp);<br>
+<br>
+       vtotal = vactive + vfp + vslen + vbp;<br>
+       if (params->num_lines != vtotal) {<br>
+               DRM_ERROR("Invalid vertical total: %upx (expected %upx)\n",<br>
+                         vtotal, params->num_lines);<br>
+               return -EINVAL;<br>
+       }<br>
+<br>
+       mode->vdisplay = vactive;<br>
+       mode->vsync_start = mode->vdisplay + vfp;<br>
+       mode->vsync_end = mode->vsync_start + vslen;<br>
+       mode->vtotal = mode->vsync_end + vbp;<br>
+<br>
+       if (mode->vtotal != params->num_lines)<br>
+               return -EINVAL;<br>
+<br>
+       mode->type = DRM_MODE_TYPE_DRIVER;<br>
+       mode->flags = DRM_MODE_FLAG_NVSYNC | DRM_MODE_FLAG_NHSYNC;<br>
+       if (interlace)<br>
+               mode->flags |= DRM_MODE_FLAG_INTERLACE;<br>
+<br>
+       drm_mode_set_name(mode);<br>
+<br>
+       drm_dbg_kms(dev, "Generated mode " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));<br>
+<br>
+       return 0;<br>
+}<br>
+<br>
+/**<br>
+ * drm_analog_tv_mode - create a display mode for an analog TV<br>
+ * @dev: drm device<br>
+ * @tv_mode: TV Mode standard to create a mode for. See DRM_MODE_TV_MODE_*.<br>
+ * @pixel_clock_hz: Pixel Clock Frequency, in Hertz<br>
+ * @hdisplay: hdisplay size<br>
+ * @vdisplay: vdisplay size<br>
+ * @interlace: whether to compute an interlaced mode<br>
+ *<br>
+ * This function creates a struct drm_display_mode instance suited for<br>
+ * an analog TV output, for one of the usual analog TV mode.<br>
+ *<br>
+ * Note that @hdisplay is larger than the usual constraints for the PAL<br>
+ * and NTSC timings, and we'll choose to ignore most timings constraints<br>
+ * to reach those resolutions.<br>
+ *<br>
+ * Returns:<br>
+ *<br>
+ * A pointer to the mode, allocated with drm_mode_create(). Returns NULL<br>
+ * on error.<br>
+ */<br>
+struct drm_display_mode *drm_analog_tv_mode(struct drm_device *dev,<br>
+                                           enum drm_connector_tv_mode tv_mode,<br>
+                                           unsigned long pixel_clock_hz,<br>
+                                           unsigned int hdisplay,<br>
+                                           unsigned int vdisplay,<br>
+                                           bool interlace)<br>
+{<br>
+       struct drm_display_mode *mode;<br>
+       enum drm_mode_analog analog;<br>
+       int ret;<br>
+<br>
+       switch (tv_mode) {<br>
+       case DRM_MODE_TV_MODE_NTSC:<br>
+               fallthrough;<br>
+       case DRM_MODE_TV_MODE_NTSC_443:<br>
+               fallthrough;<br>
+       case DRM_MODE_TV_MODE_NTSC_J:<br>
+               fallthrough;<br>
+       case DRM_MODE_TV_MODE_PAL_M:<br>
+               analog = DRM_MODE_ANALOG_NTSC;<br>
+               break;<br>
+<br>
+       case DRM_MODE_TV_MODE_PAL:<br>
+               fallthrough;<br>
+       case DRM_MODE_TV_MODE_PAL_N:<br>
+               fallthrough;<br>
+       case DRM_MODE_TV_MODE_SECAM:<br>
+               analog = DRM_MODE_ANALOG_PAL;<br>
+               break;<br>
+<br>
+       default:<br>
+               return NULL;<br>
+       }<br>
+<br>
+       mode = drm_mode_create(dev);<br>
+       if (!mode)<br>
+               return NULL;<br>
+<br>
+       ret = fill_analog_mode(dev, mode,<br>
+                              &tv_modes_parameters[analog],<br>
+                              pixel_clock_hz, hdisplay, vdisplay, interlace);<br>
+       if (ret)<br>
+               goto err_free_mode;<br>
+<br>
+       return mode;<br>
+<br>
+err_free_mode:<br>
+       drm_mode_destroy(dev, mode);<br>
+       return NULL;<br>
+}<br>
+EXPORT_SYMBOL(drm_analog_tv_mode);<br>
+<br>
 /**<br>
  * drm_cvt_mode -create a modeline based on the CVT algorithm<br>
  * @dev: drm device<br>
diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile<br>
index b29ef1085cad..b22ac96fdd65 100644<br>
--- a/drivers/gpu/drm/tests/Makefile<br>
+++ b/drivers/gpu/drm/tests/Makefile<br>
@@ -10,5 +10,6 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \<br>
        drm_framebuffer_test.o \<br>
        drm_kunit_helpers.o \<br>
        drm_mm_test.o \<br>
+       drm_modes_test.o \<br>
        drm_plane_helper_test.o \<br>
        drm_rect_test.o<br>
diff --git a/drivers/gpu/drm/tests/drm_modes_test.c b/drivers/gpu/drm/tests/drm_modes_test.c<br>
new file mode 100644<br>
index 000000000000..550e3b95453e<br>
--- /dev/null<br>
+++ b/drivers/gpu/drm/tests/drm_modes_test.c<br>
@@ -0,0 +1,144 @@<br>
+// SPDX-License-Identifier: GPL-2.0<br>
+/*<br>
+ * Kunit test for drm_modes functions<br>
+ */<br>
+<br>
+#include <drm/drm_modes.h><br>
+<br>
+#include <kunit/test.h><br>
+<br>
+#include <linux/units.h><br>
+<br>
+#include "drm_kunit_helpers.h"<br>
+<br>
+struct drm_modes_test_priv {<br>
+       struct drm_device *drm;<br>
+};<br>
+<br>
+static int drm_modes_test_init(struct kunit *test)<br>
+{<br>
+       struct drm_modes_test_priv *priv;<br>
+<br>
+       priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);<br>
+       KUNIT_ASSERT_NOT_NULL(test, priv);<br>
+<br>
+       priv->drm = drm_kunit_device_init(test, "drm-modes-test");<br>
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv->drm);<br>
+<br>
+       test->priv = priv;<br>
+<br>
+       return 0;<br>
+}<br>
+<br>
+static void drm_modes_analog_tv_ntsc_480i(struct kunit *test)<br>
+{<br>
+       struct drm_modes_test_priv *priv = test->priv;<br>
+       struct drm_display_mode *mode;<br>
+<br>
+       mode = drm_analog_tv_mode(priv->drm,<br>
+                                 DRM_MODE_TV_MODE_NTSC,<br>
+                                 13500 * HZ_PER_KHZ, 720, 480,<br>
+                                 true);<br>
+       KUNIT_ASSERT_NOT_NULL(test, mode);<br>
+<br>
+       KUNIT_EXPECT_EQ(test, drm_mode_vrefresh(mode), 60);<br>
+       KUNIT_EXPECT_EQ(test, mode->hdisplay, 720);<br>
+<br>
+       /* BT.601 defines hsync_start at 736 for 480i */<br>
+       KUNIT_EXPECT_EQ(test, mode->hsync_start, 736);<br>
+<br>
+       /*<br>
+        * The NTSC standard expects a line to take 63.556us. With a<br>
+        * pixel clock of 13.5 MHz, a pixel takes around 74ns, so we<br>
+        * need to have 63556ns / 74ns = 858.<br>
+        *<br>
+        * This is also mandated by BT.601.<br>
+        */<br>
+       KUNIT_EXPECT_EQ(test, mode->htotal, 858);<br>
+<br>
+       KUNIT_EXPECT_EQ(test, mode->vdisplay, 480);<br>
+       KUNIT_EXPECT_EQ(test, mode->vtotal, 525);<br>
+}<br>
+<br>
+static void drm_modes_analog_tv_ntsc_480i_inlined(struct kunit *test)<br>
+{<br>
+       struct drm_modes_test_priv *priv = test->priv;<br>
+       struct drm_display_mode *expected, *mode;<br>
+<br>
+       expected = drm_analog_tv_mode(priv->drm,<br>
+                                     DRM_MODE_TV_MODE_NTSC,<br>
+                                     13500 * HZ_PER_KHZ, 720, 480,<br>
+                                     true);<br>
+       KUNIT_ASSERT_NOT_NULL(test, expected);<br>
+<br>
+       mode = drm_mode_analog_ntsc_480i(priv->drm);<br>
+       KUNIT_ASSERT_NOT_NULL(test, mode);<br>
+<br>
+       KUNIT_EXPECT_TRUE(test, drm_mode_equal(expected, mode));<br>
+}<br>
+<br>
+static void drm_modes_analog_tv_pal_576i(struct kunit *test)<br>
+{<br>
+       struct drm_modes_test_priv *priv = test->priv;<br>
+       struct drm_display_mode *mode;<br>
+<br>
+       mode = drm_analog_tv_mode(priv->drm,<br>
+                                 DRM_MODE_TV_MODE_PAL,<br>
+                                 13500 * HZ_PER_KHZ, 720, 576,<br>
+                                 true);<br>
+       KUNIT_ASSERT_NOT_NULL(test, mode);<br>
+<br>
+       KUNIT_EXPECT_EQ(test, drm_mode_vrefresh(mode), 50);<br>
+       KUNIT_EXPECT_EQ(test, mode->hdisplay, 720);<br>
+<br>
+       /* BT.601 defines hsync_start at 732 for 576i */<br>
+       KUNIT_EXPECT_EQ(test, mode->hsync_start, 732);<br>
+<br>
+       /*<br>
+        * The PAL standard expects a line to take 64us. With a pixel<br>
+        * clock of 13.5 MHz, a pixel takes around 74ns, so we need to<br>
+        * have 64000ns / 74ns = 864.<br>
+        *<br>
+        * This is also mandated by BT.601.<br>
+        */<br>
+       KUNIT_EXPECT_EQ(test, mode->htotal, 864);<br>
+<br>
+       KUNIT_EXPECT_EQ(test, mode->vdisplay, 576);<br>
+       KUNIT_EXPECT_EQ(test, mode->vtotal, 625);<br>
+}<br>
+<br>
+static void drm_modes_analog_tv_pal_576i_inlined(struct kunit *test)<br>
+{<br>
+       struct drm_modes_test_priv *priv = test->priv;<br>
+       struct drm_display_mode *expected, *mode;<br>
+<br>
+       expected = drm_analog_tv_mode(priv->drm,<br>
+                                     DRM_MODE_TV_MODE_PAL,<br>
+                                     13500 * HZ_PER_KHZ, 720, 576,<br>
+                                     true);<br>
+       KUNIT_ASSERT_NOT_NULL(test, expected);<br>
+<br>
+       mode = drm_mode_analog_pal_576i(priv->drm);<br>
+       KUNIT_ASSERT_NOT_NULL(test, mode);<br>
+<br>
+       KUNIT_EXPECT_TRUE(test, drm_mode_equal(expected, mode));<br>
+}<br>
+<br>
+static struct kunit_case drm_modes_analog_tv_tests[] = {<br>
+       KUNIT_CASE(drm_modes_analog_tv_ntsc_480i),<br>
+       KUNIT_CASE(drm_modes_analog_tv_ntsc_480i_inlined),<br>
+       KUNIT_CASE(drm_modes_analog_tv_pal_576i),<br>
+       KUNIT_CASE(drm_modes_analog_tv_pal_576i_inlined),<br>
+       { }<br>
+};<br>
+<br>
+static struct kunit_suite drm_modes_analog_tv_test_suite = {<br>
+       .name = "drm_modes_analog_tv",<br>
+       .init = drm_modes_test_init,<br>
+       .test_cases = drm_modes_analog_tv_tests,<br>
+};<br>
+<br>
+kunit_test_suites(<br>
+       &drm_modes_analog_tv_test_suite<br>
+);<br>
+MODULE_LICENSE("GPL v2");<br>
diff --git a/include/drm/drm_modes.h b/include/drm/drm_modes.h<br>
index b0c680e6f670..c613f0abe9dc 100644<br>
--- a/include/drm/drm_modes.h<br>
+++ b/include/drm/drm_modes.h<br>
@@ -468,6 +468,23 @@ bool drm_mode_is_420_also(const struct drm_display_info *display,<br>
 bool drm_mode_is_420(const struct drm_display_info *display,<br>
                     const struct drm_display_mode *mode);<br>
<br>
+struct drm_display_mode *drm_analog_tv_mode(struct drm_device *dev,<br>
+                                           enum drm_connector_tv_mode mode,<br>
+                                           unsigned long pixel_clock_hz,<br>
+                                           unsigned int hdisplay,<br>
+                                           unsigned int vdisplay,<br>
+                                           bool interlace);<br>
+<br>
+static inline struct drm_display_mode *drm_mode_analog_ntsc_480i(struct drm_device *dev)<br>
+{<br>
+       return drm_analog_tv_mode(dev, DRM_MODE_TV_MODE_NTSC, 13500000, 720, 480, true);<br>
+}<br>
+<br>
+static inline struct drm_display_mode *drm_mode_analog_pal_576i(struct drm_device *dev)<br>
+{<br>
+       return drm_analog_tv_mode(dev, DRM_MODE_TV_MODE_PAL, 13500000, 720, 576, true);<br>
+}<br>
+<br>
 struct drm_display_mode *drm_cvt_mode(struct drm_device *dev,<br>
                                      int hdisplay, int vdisplay, int vrefresh,<br>
                                      bool reduced, bool interlaced,<br>
<br>
-- <br>
b4 0.11.0-dev-99e3a<br>
</blockquote></div>