2 * Copyright (c) 2015 Google, Inc
3 * (C) Copyright 2001-2015
4 * DENX Software Engineering -- wd@denx.de
5 * Compulab Ltd - http://compulab.co.il/
6 * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <video_console.h>
15 #include <video_font.h> /* Get font data, width and height */
17 /* By default we scroll by a single line */
18 #ifndef CONFIG_CONSOLE_SCROLL_LINES
19 #define CONFIG_CONSOLE_SCROLL_LINES 1
22 int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
24 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
28 return ops->putc_xy(dev, x, y, ch);
31 int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
34 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
38 return ops->move_rows(dev, rowdst, rowsrc, count);
41 int vidconsole_set_row(struct udevice *dev, uint row, int clr)
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
47 return ops->set_row(dev, row, clr);
50 static int vidconsole_entry_start(struct udevice *dev)
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
54 if (!ops->entry_start)
56 return ops->entry_start(dev);
59 /* Move backwards one space */
60 static int vidconsole_back(struct udevice *dev)
62 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
63 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
67 ret = ops->backspace(dev);
72 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
73 if (priv->xcur_frac < priv->xstart_frac) {
74 priv->xcur_frac = (priv->cols - 1) *
75 VID_TO_POS(priv->x_charsize);
76 priv->ycur -= priv->y_charsize;
80 video_sync(dev->parent);
85 /* Move to a newline, scrolling the display if necessary */
86 static void vidconsole_newline(struct udevice *dev)
88 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
89 struct udevice *vid_dev = dev->parent;
90 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
91 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
94 priv->xcur_frac = priv->xstart_frac;
95 priv->ycur += priv->y_charsize;
97 /* Check if we need to scroll the terminal */
98 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
99 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
100 for (i = 0; i < rows; i++)
101 vidconsole_set_row(dev, priv->rows - i - 1,
102 vid_priv->colour_bg);
103 priv->ycur -= rows * priv->y_charsize;
107 video_sync(dev->parent);
110 int vidconsole_put_char(struct udevice *dev, char ch)
112 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
120 priv->xcur_frac = priv->xstart_frac;
123 vidconsole_newline(dev);
124 vidconsole_entry_start(dev);
126 case '\t': /* Tab (8 chars alignment) */
127 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
128 + 1) * priv->tab_width_frac;
130 if (priv->xcur_frac >= priv->xsize_frac)
131 vidconsole_newline(dev);
134 vidconsole_back(dev);
139 * Failure of this function normally indicates an unsupported
140 * colour depth. Check this and return an error to help with
143 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
144 if (ret == -EAGAIN) {
145 vidconsole_newline(dev);
146 ret = vidconsole_putc_xy(dev, priv->xcur_frac,
151 priv->xcur_frac += ret;
153 if (priv->xcur_frac >= priv->xsize_frac)
154 vidconsole_newline(dev);
161 static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
163 struct udevice *dev = sdev->priv;
165 vidconsole_put_char(dev, ch);
168 static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
170 struct udevice *dev = sdev->priv;
173 vidconsole_put_char(dev, *s++);
174 video_sync(dev->parent);
177 /* Set up the number of rows and colours (rotated drivers override this) */
178 static int vidconsole_pre_probe(struct udevice *dev)
180 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
181 struct udevice *vid = dev->parent;
182 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
184 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
189 /* Register the device with stdio */
190 static int vidconsole_post_probe(struct udevice *dev)
192 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
193 struct stdio_dev *sdev = &priv->sdev;
195 if (!priv->tab_width_frac)
196 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
199 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
202 strcpy(sdev->name, "vidconsole");
205 sdev->flags = DEV_FLAGS_OUTPUT;
206 sdev->putc = vidconsole_putc;
207 sdev->puts = vidconsole_puts;
210 return stdio_register(sdev);
213 UCLASS_DRIVER(vidconsole) = {
214 .id = UCLASS_VIDEO_CONSOLE,
215 .name = "vidconsole0",
216 .pre_probe = vidconsole_pre_probe,
217 .post_probe = vidconsole_post_probe,
218 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
221 void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
223 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
224 struct udevice *vid_dev = dev->parent;
225 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
227 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
228 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
231 static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
234 unsigned int col, row;
238 return CMD_RET_USAGE;
240 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
241 return CMD_RET_FAILURE;
242 col = simple_strtoul(argv[1], NULL, 10);
243 row = simple_strtoul(argv[2], NULL, 10);
244 vidconsole_position_cursor(dev, col, row);
249 static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
256 return CMD_RET_USAGE;
258 if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev))
259 return CMD_RET_FAILURE;
260 for (s = argv[1]; *s; s++)
261 vidconsole_put_char(dev, *s);
267 setcurs, 3, 1, do_video_setcursor,
268 "set cursor position within screen",
269 " <col> <row> in character"
273 lcdputs, 2, 1, do_video_puts,
274 "print string on video framebuffer",