video console: allow font size configuration at runtime
[platform/kernel/u-boot.git] / drivers / video / console_rotate.c
index 227141d..65358a1 100644 (file)
@@ -1,9 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (c) 2015 Google, Inc
  * (C) Copyright 2015
  * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
- *
- * SPDX-License-Identifier:    GPL-2.0+
+ * (C) Copyright 2023 Dzmitry Sankouski <dsankouski@gmail.com>
  */
 
 #include <common.h>
 #include <video.h>
 #include <video_console.h>
 #include <video_font.h>                /* Get font data, width and height */
+#include "vidconsole_internal.h"
 
 static int console_set_row_1(struct udevice *dev, uint row, int clr)
 {
        struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
+       struct console_simple_priv *priv = dev_get_priv(dev);
+       struct video_fontdata *fontdata = priv->fontdata;
        int pbytes = VNBYTES(vid_priv->bpix);
-       void *line;
+       void *start, *dst, *line;
        int i, j;
+       int ret;
 
-       line = vid_priv->fb + vid_priv->line_length -
-               (row + 1) * VIDEO_FONT_HEIGHT * pbytes;
+       start = vid_priv->fb + vid_priv->line_length -
+               (row + 1) * fontdata->height * pbytes;
+       line = start;
        for (j = 0; j < vid_priv->ysize; j++) {
-               switch (vid_priv->bpix) {
-#ifdef CONFIG_VIDEO_BPP8
-               case VIDEO_BPP8: {
-                       uint8_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
-                               *dst++ = clr;
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP16
-               case VIDEO_BPP16: {
-                       uint16_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
-                               *dst++ = clr;
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP32
-               case VIDEO_BPP32: {
-                       uint32_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
-                               *dst++ = clr;
-                       break;
-               }
-#endif
-               default:
-                       return -ENOSYS;
-               }
+               dst = line;
+               for (i = 0; i < fontdata->height; i++)
+                       fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
                line += vid_priv->line_length;
        }
+       ret = vidconsole_sync_copy(dev, start, line);
+       if (ret)
+               return ret;
 
        return 0;
 }
 
 static int console_move_rows_1(struct udevice *dev, uint rowdst, uint rowsrc,
-                              uint count)
+                                  uint count)
 {
        struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
+       struct console_simple_priv *priv = dev_get_priv(dev);
+       struct video_fontdata *fontdata = priv->fontdata;
+       int pbytes = VNBYTES(vid_priv->bpix);
        void *dst;
        void *src;
-       int pbytes = VNBYTES(vid_priv->bpix);
-       int j;
+       int j, ret;
 
        dst = vid_priv->fb + vid_priv->line_length -
-               (rowdst + count) * VIDEO_FONT_HEIGHT * pbytes;
+               (rowdst + count) * fontdata->height * pbytes;
        src = vid_priv->fb + vid_priv->line_length -
-               (rowsrc + count) * VIDEO_FONT_HEIGHT * pbytes;
+               (rowsrc + count) * fontdata->height * pbytes;
 
        for (j = 0; j < vid_priv->ysize; j++) {
-               memmove(dst, src, VIDEO_FONT_HEIGHT * pbytes * count);
+               ret = vidconsole_memmove(dev, dst, src,
+                                       fontdata->height * pbytes * count);
+               if (ret)
+                       return ret;
                src += vid_priv->line_length;
                dst += vid_priv->line_length;
        }
@@ -87,103 +72,54 @@ static int console_putc_xy_1(struct udevice *dev, uint x_frac, uint y, char ch)
        struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
        struct udevice *vid = dev->parent;
        struct video_priv *vid_priv = dev_get_uclass_priv(vid);
+       struct console_simple_priv *priv = dev_get_priv(dev);
+       struct video_fontdata *fontdata = priv->fontdata;
        int pbytes = VNBYTES(vid_priv->bpix);
-       int i, col;
-       int mask = 0x80;
-       void *line;
-       uchar *pfont = video_fontdata + ch * VIDEO_FONT_HEIGHT;
+       int x, linenum, ret;
+       void *start, *line;
+       uchar *pfont = fontdata->video_fontdata +
+                       (u8)ch * fontdata->char_pixel_bytes;
 
-       line = vid_priv->fb + (VID_TO_PIXEL(x_frac) + 1) *
-                       vid_priv->line_length - (y + 1) * pbytes;
        if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
                return -EAGAIN;
+       linenum = VID_TO_PIXEL(x_frac) + 1;
+       x = y + 1;
+       start = vid_priv->fb + linenum * vid_priv->line_length - x * pbytes;
+       line = start;
 
-       for (col = 0; col < VIDEO_FONT_HEIGHT; col++) {
-               switch (vid_priv->bpix) {
-#ifdef CONFIG_VIDEO_BPP8
-               case VIDEO_BPP8: {
-                       uint8_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
-                               *dst-- = (pfont[i] & mask) ? vid_priv->colour_fg
-                                       : vid_priv->colour_bg;
-                       }
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP16
-               case VIDEO_BPP16: {
-                       uint16_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
-                               *dst-- = (pfont[i] & mask) ? vid_priv->colour_fg
-                                       : vid_priv->colour_bg;
-                       }
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP32
-               case VIDEO_BPP32: {
-                       uint32_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
-                               *dst-- = (pfont[i] & mask) ? vid_priv->colour_fg
-                                       : vid_priv->colour_bg;
-                       }
-                       break;
-               }
-#endif
-               default:
-                       return -ENOSYS;
-               }
-               line += vid_priv->line_length;
-               mask >>= 1;
-       }
+       ret = fill_char_horizontally(pfont, &line, vid_priv, fontdata, FLIPPED_DIRECTION);
+       if (ret)
+               return ret;
 
-       return VID_TO_POS(VIDEO_FONT_WIDTH);
+       /* We draw backwards from 'start, so account for the first line */
+       ret = vidconsole_sync_copy(dev, start - vid_priv->line_length, line);
+       if (ret)
+               return ret;
+
+       return VID_TO_POS(fontdata->width);
 }
 
 
 static int console_set_row_2(struct udevice *dev, uint row, int clr)
 {
        struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
-       void *line;
-       int pixels = VIDEO_FONT_HEIGHT * vid_priv->xsize;
-       int i;
-
-       line = vid_priv->fb + vid_priv->ysize * vid_priv->line_length -
-               (row + 1) * VIDEO_FONT_HEIGHT * vid_priv->line_length;
-       switch (vid_priv->bpix) {
-#ifdef CONFIG_VIDEO_BPP8
-       case VIDEO_BPP8: {
-               uint8_t *dst = line;
-
-               for (i = 0; i < pixels; i++)
-                       *dst++ = clr;
-               break;
-       }
-#endif
-#ifdef CONFIG_VIDEO_BPP16
-       case VIDEO_BPP16: {
-               uint16_t *dst = line;
-
-               for (i = 0; i < pixels; i++)
-                       *dst++ = clr;
-               break;
-       }
-#endif
-#ifdef CONFIG_VIDEO_BPP32
-       case VIDEO_BPP32: {
-               uint32_t *dst = line;
-
-               for (i = 0; i < pixels; i++)
-                       *dst++ = clr;
-               break;
-       }
-#endif
-       default:
-               return -ENOSYS;
-       }
+       struct console_simple_priv *priv = dev_get_priv(dev);
+       struct video_fontdata *fontdata = priv->fontdata;
+       void *start, *line, *dst, *end;
+       int pixels = fontdata->height * vid_priv->xsize;
+       int i, ret;
+       int pbytes = VNBYTES(vid_priv->bpix);
+
+       start = vid_priv->fb + vid_priv->ysize * vid_priv->line_length -
+               (row + 1) * fontdata->height * vid_priv->line_length;
+       line = start;
+       dst = line;
+       for (i = 0; i < pixels; i++)
+               fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
+       end = dst;
+       ret = vidconsole_sync_copy(dev, start, end);
+       if (ret)
+               return ret;
 
        return 0;
 }
@@ -192,16 +128,19 @@ static int console_move_rows_2(struct udevice *dev, uint rowdst, uint rowsrc,
                               uint count)
 {
        struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
+       struct console_simple_priv *priv = dev_get_priv(dev);
+       struct video_fontdata *fontdata = priv->fontdata;
        void *dst;
        void *src;
        void *end;
 
        end = vid_priv->fb + vid_priv->ysize * vid_priv->line_length;
-       dst = end - (rowdst + count) * VIDEO_FONT_HEIGHT *
+       dst = end - (rowdst + count) * fontdata->height *
                vid_priv->line_length;
-       src = end - (rowsrc + count) * VIDEO_FONT_HEIGHT *
+       src = end - (rowsrc + count) * fontdata->height *
                vid_priv->line_length;
-       memmove(dst, src, VIDEO_FONT_HEIGHT * vid_priv->line_length * count);
+       vidconsole_memmove(dev, dst, src,
+                          fontdata->height * vid_priv->line_length * count);
 
        return 0;
 }
@@ -211,108 +150,53 @@ static int console_putc_xy_2(struct udevice *dev, uint x_frac, uint y, char ch)
        struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
        struct udevice *vid = dev->parent;
        struct video_priv *vid_priv = dev_get_uclass_priv(vid);
-       int i, row;
-       void *line;
+       struct console_simple_priv *priv = dev_get_priv(dev);
+       struct video_fontdata *fontdata = priv->fontdata;
+       int pbytes = VNBYTES(vid_priv->bpix);
+       int linenum, x, ret;
+       void *start, *line;
+       uchar *pfont = fontdata->video_fontdata +
+                       (u8)ch * fontdata->char_pixel_bytes;
 
        if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
                return -EAGAIN;
+       linenum = vid_priv->ysize - y - 1;
+       x = vid_priv->xsize - VID_TO_PIXEL(x_frac) - 1;
+       start = vid_priv->fb + linenum * vid_priv->line_length + x * pbytes;
+       line = start;
 
-       line = vid_priv->fb + (vid_priv->ysize - y - 1) *
-                       vid_priv->line_length +
-                       (vid_priv->xsize - VID_TO_PIXEL(x_frac) -
-                       VIDEO_FONT_WIDTH - 1) * VNBYTES(vid_priv->bpix);
-
-       for (row = 0; row < VIDEO_FONT_HEIGHT; row++) {
-               uchar bits = video_fontdata[ch * VIDEO_FONT_HEIGHT + row];
-
-               switch (vid_priv->bpix) {
-#ifdef CONFIG_VIDEO_BPP8
-               case VIDEO_BPP8: {
-                       uint8_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_WIDTH; i++) {
-                               *dst-- = (bits & 0x80) ? vid_priv->colour_fg
-                                       : vid_priv->colour_bg;
-                               bits <<= 1;
-                       }
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP16
-               case VIDEO_BPP16: {
-                       uint16_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_WIDTH; i++) {
-                               *dst-- = (bits & 0x80) ? vid_priv->colour_fg
-                                       : vid_priv->colour_bg;
-                               bits <<= 1;
-                       }
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP32
-               case VIDEO_BPP32: {
-                       uint32_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_WIDTH; i++) {
-                               *dst-- = (bits & 0x80) ? vid_priv->colour_fg
-                                       : vid_priv->colour_bg;
-                               bits <<= 1;
-                       }
-                       break;
-               }
-#endif
-               default:
-                       return -ENOSYS;
-               }
-               line -= vid_priv->line_length;
-       }
+       ret = fill_char_vertically(pfont, &line, vid_priv, fontdata, FLIPPED_DIRECTION);
+       if (ret)
+               return ret;
 
-       return VID_TO_POS(VIDEO_FONT_WIDTH);
+       /* Add 4 bytes to allow for the first pixel writen */
+       ret = vidconsole_sync_copy(dev, start + 4, line);
+       if (ret)
+               return ret;
+
+       return VID_TO_POS(fontdata->width);
 }
 
 static int console_set_row_3(struct udevice *dev, uint row, int clr)
 {
        struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
+       struct console_simple_priv *priv = dev_get_priv(dev);
+       struct video_fontdata *fontdata = priv->fontdata;
        int pbytes = VNBYTES(vid_priv->bpix);
-       void *line;
-       int i, j;
+       void *start, *dst, *line;
+       int i, j, ret;
 
-       line = vid_priv->fb + row * VIDEO_FONT_HEIGHT * pbytes;
+       start = vid_priv->fb + row * fontdata->height * pbytes;
+       line = start;
        for (j = 0; j < vid_priv->ysize; j++) {
-               switch (vid_priv->bpix) {
-#ifdef CONFIG_VIDEO_BPP8
-               case VIDEO_BPP8: {
-                       uint8_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
-                               *dst++ = clr;
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP16
-               case VIDEO_BPP16: {
-                       uint16_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
-                               *dst++ = clr;
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP32
-               case VIDEO_BPP32: {
-                       uint32_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++)
-                               *dst++ = clr;
-                       break;
-               }
-#endif
-               default:
-                       return -ENOSYS;
-               }
+               dst = line;
+               for (i = 0; i < fontdata->height; i++)
+                       fill_pixel_and_goto_next(&dst, clr, pbytes, pbytes);
                line += vid_priv->line_length;
        }
+       ret = vidconsole_sync_copy(dev, start, line);
+       if (ret)
+               return ret;
 
        return 0;
 }
@@ -321,16 +205,21 @@ static int console_move_rows_3(struct udevice *dev, uint rowdst, uint rowsrc,
                               uint count)
 {
        struct video_priv *vid_priv = dev_get_uclass_priv(dev->parent);
+       struct console_simple_priv *priv = dev_get_priv(dev);
+       struct video_fontdata *fontdata = priv->fontdata;
+       int pbytes = VNBYTES(vid_priv->bpix);
        void *dst;
        void *src;
-       int pbytes = VNBYTES(vid_priv->bpix);
-       int j;
+       int j, ret;
 
-       dst = vid_priv->fb + rowdst * VIDEO_FONT_HEIGHT * pbytes;
-       src = vid_priv->fb + rowsrc * VIDEO_FONT_HEIGHT * pbytes;
+       dst = vid_priv->fb + rowdst * fontdata->height * pbytes;
+       src = vid_priv->fb + rowsrc * fontdata->height * pbytes;
 
        for (j = 0; j < vid_priv->ysize; j++) {
-               memmove(dst, src, VIDEO_FONT_HEIGHT * pbytes * count);
+               ret = vidconsole_memmove(dev, dst, src,
+                                       fontdata->height * pbytes * count);
+               if (ret)
+                       return ret;
                src += vid_priv->line_length;
                dst += vid_priv->line_length;
        }
@@ -343,127 +232,79 @@ static int console_putc_xy_3(struct udevice *dev, uint x_frac, uint y, char ch)
        struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
        struct udevice *vid = dev->parent;
        struct video_priv *vid_priv = dev_get_uclass_priv(vid);
+       struct console_simple_priv *priv = dev_get_priv(dev);
+       struct video_fontdata *fontdata = priv->fontdata;
        int pbytes = VNBYTES(vid_priv->bpix);
-       int i, col;
-       int mask = 0x80;
-       void *line = vid_priv->fb +
-               (vid_priv->ysize - VID_TO_PIXEL(x_frac) - 1) *
-               vid_priv->line_length + y * pbytes;
-       uchar *pfont = video_fontdata + ch * VIDEO_FONT_HEIGHT;
+       int linenum, x, ret;
+       void *start, *line;
+       uchar *pfont = fontdata->video_fontdata +
+                       (u8)ch * fontdata->char_pixel_bytes;
 
        if (x_frac + VID_TO_POS(vc_priv->x_charsize) > vc_priv->xsize_frac)
                return -EAGAIN;
-
-       for (col = 0; col < VIDEO_FONT_HEIGHT; col++) {
-               switch (vid_priv->bpix) {
-#ifdef CONFIG_VIDEO_BPP8
-               case VIDEO_BPP8: {
-                       uint8_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
-                               *dst++ = (pfont[i] & mask) ? vid_priv->colour_fg
-                                       : vid_priv->colour_bg;
-                       }
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP16
-               case VIDEO_BPP16: {
-                       uint16_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
-                               *dst++ = (pfont[i] & mask) ? vid_priv->colour_fg
-                                       : vid_priv->colour_bg;
-                       }
-                       break;
-               }
-#endif
-#ifdef CONFIG_VIDEO_BPP32
-               case VIDEO_BPP32: {
-                       uint32_t *dst = line;
-
-                       for (i = 0; i < VIDEO_FONT_HEIGHT; i++) {
-                               *dst++ = (pfont[i] & mask) ? vid_priv->colour_fg
-                                       : vid_priv->colour_bg;
-                       }
-                       break;
-               }
-#endif
-               default:
-                       return -ENOSYS;
-               }
-               line -= vid_priv->line_length;
-               mask >>= 1;
-       }
-
-       return VID_TO_POS(VIDEO_FONT_WIDTH);
-}
-
-
-static int console_probe_2(struct udevice *dev)
-{
-       struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
-       struct udevice *vid_dev = dev->parent;
-       struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
-
-       vc_priv->x_charsize = VIDEO_FONT_WIDTH;
-       vc_priv->y_charsize = VIDEO_FONT_HEIGHT;
-       vc_priv->cols = vid_priv->xsize / VIDEO_FONT_WIDTH;
-       vc_priv->rows = vid_priv->ysize / VIDEO_FONT_HEIGHT;
-
-       return 0;
-}
-
-static int console_probe_1_3(struct udevice *dev)
-{
-       struct vidconsole_priv *vc_priv = dev_get_uclass_priv(dev);
-       struct udevice *vid_dev = dev->parent;
-       struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
-
-       vc_priv->x_charsize = VIDEO_FONT_WIDTH;
-       vc_priv->y_charsize = VIDEO_FONT_HEIGHT;
-       vc_priv->cols = vid_priv->ysize / VIDEO_FONT_WIDTH;
-       vc_priv->rows = vid_priv->xsize / VIDEO_FONT_HEIGHT;
-       vc_priv->xsize_frac = VID_TO_POS(vid_priv->ysize);
-
-       return 0;
+       x = y;
+       linenum = vid_priv->ysize - VID_TO_PIXEL(x_frac) - 1;
+       start = vid_priv->fb + linenum * vid_priv->line_length + y * pbytes;
+       line = start;
+
+       ret = fill_char_horizontally(pfont, &line, vid_priv, fontdata, NORMAL_DIRECTION);
+       if (ret)
+               return ret;
+       /* Add a line to allow for the first pixels writen */
+       ret = vidconsole_sync_copy(dev, start + vid_priv->line_length, line);
+       if (ret)
+               return ret;
+
+       return VID_TO_POS(fontdata->width);
 }
 
 struct vidconsole_ops console_ops_1 = {
        .putc_xy        = console_putc_xy_1,
        .move_rows      = console_move_rows_1,
        .set_row        = console_set_row_1,
+       .get_font_size  = console_simple_get_font_size,
+       .get_font       = console_simple_get_font,
+       .select_font    = console_simple_select_font,
 };
 
 struct vidconsole_ops console_ops_2 = {
        .putc_xy        = console_putc_xy_2,
        .move_rows      = console_move_rows_2,
        .set_row        = console_set_row_2,
+       .get_font_size  = console_simple_get_font_size,
+       .get_font       = console_simple_get_font,
+       .select_font    = console_simple_select_font,
 };
 
 struct vidconsole_ops console_ops_3 = {
        .putc_xy        = console_putc_xy_3,
        .move_rows      = console_move_rows_3,
        .set_row        = console_set_row_3,
+       .get_font_size  = console_simple_get_font_size,
+       .get_font       = console_simple_get_font,
+       .select_font    = console_simple_select_font,
 };
 
 U_BOOT_DRIVER(vidconsole_1) = {
        .name   = "vidconsole1",
        .id     = UCLASS_VIDEO_CONSOLE,
        .ops    = &console_ops_1,
-       .probe  = console_probe_1_3,
+       .probe  = console_probe,
+       .priv_auto      = sizeof(struct console_simple_priv),
 };
 
 U_BOOT_DRIVER(vidconsole_2) = {
        .name   = "vidconsole2",
        .id     = UCLASS_VIDEO_CONSOLE,
        .ops    = &console_ops_2,
-       .probe  = console_probe_2,
+       .probe  = console_probe,
+       .priv_auto      = sizeof(struct console_simple_priv),
 };
 
 U_BOOT_DRIVER(vidconsole_3) = {
        .name   = "vidconsole3",
        .id     = UCLASS_VIDEO_CONSOLE,
        .ops    = &console_ops_3,
-       .probe  = console_probe_1_3,
+       .probe  = console_probe,
+       .priv_auto      = sizeof(struct console_simple_priv),
 };