#include <drm/drm_print.h>
 #include <drm/drm_rect.h>
 
+#include "drm_format_internal.h"
+
 /**
  * drm_format_conv_state_init - Initialize format-conversion state
  * @state: The state to initialize
                                     xfrm_line);
 }
 
+static __always_inline void drm_fb_xfrm_line_32to32(void *dbuf, const void *sbuf,
+                                                   unsigned int pixels,
+                                                   u32 (*xfrm_pixel)(u32))
+{
+       __le32 *dbuf32 = dbuf;
+       const __le32 *sbuf32 = sbuf;
+       const __le32 *send32 = sbuf32 + pixels;
+
+       while (sbuf32 < send32)
+               *dbuf32++ = cpu_to_le32(xfrm_pixel(le32_to_cpup(sbuf32++)));
+}
+
 /**
  * drm_fb_memcpy - Copy clip buffer
  * @dst: Array of destination buffers
 
 static void drm_fb_xrgb8888_to_argb8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
 {
-       __le32 *dbuf32 = dbuf;
-       const __le32 *sbuf32 = sbuf;
-       unsigned int x;
-       u32 pix;
-
-       for (x = 0; x < pixels; x++) {
-               pix = le32_to_cpu(sbuf32[x]);
-               pix |= GENMASK(31, 24); /* fill alpha bits */
-               dbuf32[x] = cpu_to_le32(pix);
-       }
+       drm_fb_xfrm_line_32to32(dbuf, sbuf, pixels, drm_pixel_xrgb8888_to_argb8888);
 }
 
 /**
 
 static void drm_fb_xrgb8888_to_abgr8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
 {
-       __le32 *dbuf32 = dbuf;
-       const __le32 *sbuf32 = sbuf;
-       unsigned int x;
-       u32 pix;
-
-       for (x = 0; x < pixels; x++) {
-               pix = le32_to_cpu(sbuf32[x]);
-               pix = ((pix & 0x00ff0000) >> 16) <<  0 |
-                     ((pix & 0x0000ff00) >>  8) <<  8 |
-                     ((pix & 0x000000ff) >>  0) << 16 |
-                     GENMASK(31, 24); /* fill alpha bits */
-               *dbuf32++ = cpu_to_le32(pix);
-       }
+       drm_fb_xfrm_line_32to32(dbuf, sbuf, pixels, drm_pixel_xrgb8888_to_abgr8888);
 }
 
 static void drm_fb_xrgb8888_to_abgr8888(struct iosys_map *dst, const unsigned int *dst_pitch,
 
 static void drm_fb_xrgb8888_to_xbgr8888_line(void *dbuf, const void *sbuf, unsigned int pixels)
 {
-       __le32 *dbuf32 = dbuf;
-       const __le32 *sbuf32 = sbuf;
-       unsigned int x;
-       u32 pix;
-
-       for (x = 0; x < pixels; x++) {
-               pix = le32_to_cpu(sbuf32[x]);
-               pix = ((pix & 0x00ff0000) >> 16) <<  0 |
-                     ((pix & 0x0000ff00) >>  8) <<  8 |
-                     ((pix & 0x000000ff) >>  0) << 16 |
-                     ((pix & 0xff000000) >> 24) << 24;
-               *dbuf32++ = cpu_to_le32(pix);
-       }
+       drm_fb_xfrm_line_32to32(dbuf, sbuf, pixels, drm_pixel_xrgb8888_to_xbgr8888);
 }
 
 static void drm_fb_xrgb8888_to_xbgr8888(struct iosys_map *dst, const unsigned int *dst_pitch,
 
 static void drm_fb_xrgb8888_to_xrgb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
 {
-       __le32 *dbuf32 = dbuf;
-       const __le32 *sbuf32 = sbuf;
-       unsigned int x;
-       u32 val32;
-       u32 pix;
-
-       for (x = 0; x < pixels; x++) {
-               pix = le32_to_cpu(sbuf32[x]);
-               val32 = ((pix & 0x000000FF) << 2) |
-                       ((pix & 0x0000FF00) << 4) |
-                       ((pix & 0x00FF0000) << 6);
-               pix = val32 | ((val32 >> 8) & 0x00300C03);
-               *dbuf32++ = cpu_to_le32(pix);
-       }
+       drm_fb_xfrm_line_32to32(dbuf, sbuf, pixels, drm_pixel_xrgb8888_to_xrgb2101010);
 }
 
 /**
 
 static void drm_fb_xrgb8888_to_argb2101010_line(void *dbuf, const void *sbuf, unsigned int pixels)
 {
-       __le32 *dbuf32 = dbuf;
-       const __le32 *sbuf32 = sbuf;
-       unsigned int x;
-       u32 val32;
-       u32 pix;
-
-       for (x = 0; x < pixels; x++) {
-               pix = le32_to_cpu(sbuf32[x]);
-               val32 = ((pix & 0x000000ff) << 2) |
-                       ((pix & 0x0000ff00) << 4) |
-                       ((pix & 0x00ff0000) << 6);
-               pix = GENMASK(31, 30) | /* set alpha bits */
-                     val32 | ((val32 >> 8) & 0x00300c03);
-               *dbuf32++ = cpu_to_le32(pix);
-       }
+       drm_fb_xfrm_line_32to32(dbuf, sbuf, pixels, drm_pixel_xrgb8888_to_argb2101010);
 }
 
 /**