blob: bea563a6c5b7ebf25334df75aab8194842b8432b [file] [log] [blame]
Simon Glass84c7fb32016-01-18 19:52:17 -07001/*
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
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <common.h>
12#include <dm.h>
13#include <video.h>
14#include <video_console.h>
15#include <video_font.h> /* Get font data, width and height */
16
17/* By default we scroll by a single line */
18#ifndef CONFIG_CONSOLE_SCROLL_LINES
19#define CONFIG_CONSOLE_SCROLL_LINES 1
20#endif
21
22int vidconsole_putc_xy(struct udevice *dev, uint x, uint y, char ch)
23{
24 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
25
26 if (!ops->putc_xy)
27 return -ENOSYS;
28 return ops->putc_xy(dev, x, y, ch);
29}
30
31int vidconsole_move_rows(struct udevice *dev, uint rowdst, uint rowsrc,
32 uint count)
33{
34 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
35
36 if (!ops->move_rows)
37 return -ENOSYS;
38 return ops->move_rows(dev, rowdst, rowsrc, count);
39}
40
41int vidconsole_set_row(struct udevice *dev, uint row, int clr)
42{
43 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
44
45 if (!ops->set_row)
46 return -ENOSYS;
47 return ops->set_row(dev, row, clr);
48}
49
Simon Glassafee7432016-01-14 18:10:40 -070050static int vidconsole_entry_start(struct udevice *dev)
51{
52 struct vidconsole_ops *ops = vidconsole_get_ops(dev);
53
54 if (!ops->entry_start)
55 return -ENOSYS;
56 return ops->entry_start(dev);
57}
58
Simon Glass84c7fb32016-01-18 19:52:17 -070059/* Move backwards one space */
60static void vidconsole_back(struct udevice *dev)
61{
62 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
63
Simon Glass52c10c52016-01-14 18:10:37 -070064 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassa74451d2016-01-14 18:10:39 -070065 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glass52c10c52016-01-14 18:10:37 -070066 priv->xcur_frac = (priv->cols - 1) *
67 VID_TO_POS(priv->x_charsize);
68 priv->ycur -= priv->y_charsize;
69 if (priv->ycur < 0)
70 priv->ycur = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -070071 }
Simon Glass84c7fb32016-01-18 19:52:17 -070072}
73
74/* Move to a newline, scrolling the display if necessary */
75static void vidconsole_newline(struct udevice *dev)
76{
77 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
78 struct udevice *vid_dev = dev->parent;
79 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
80 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
81 int i;
82
Simon Glassa74451d2016-01-14 18:10:39 -070083 priv->xcur_frac = priv->xstart_frac;
Simon Glass52c10c52016-01-14 18:10:37 -070084 priv->ycur += priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -070085
86 /* Check if we need to scroll the terminal */
Simon Glass52c10c52016-01-14 18:10:37 -070087 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass84c7fb32016-01-18 19:52:17 -070088 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
89 for (i = 0; i < rows; i++)
90 vidconsole_set_row(dev, priv->rows - i - 1,
91 vid_priv->colour_bg);
Simon Glass52c10c52016-01-14 18:10:37 -070092 priv->ycur -= rows * priv->y_charsize;
Simon Glass84c7fb32016-01-18 19:52:17 -070093 }
Simon Glassafee7432016-01-14 18:10:40 -070094 priv->last_ch = 0;
95
Simon Glass84c7fb32016-01-18 19:52:17 -070096 video_sync(dev->parent);
97}
98
99int vidconsole_put_char(struct udevice *dev, char ch)
100{
101 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
102 int ret;
103
104 switch (ch) {
Simon Glass37b80202016-01-14 18:10:38 -0700105 case '\a':
106 /* beep */
107 break;
Simon Glass84c7fb32016-01-18 19:52:17 -0700108 case '\r':
Simon Glassa74451d2016-01-14 18:10:39 -0700109 priv->xcur_frac = priv->xstart_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700110 break;
111 case '\n':
112 vidconsole_newline(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700113 vidconsole_entry_start(dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700114 break;
115 case '\t': /* Tab (8 chars alignment) */
Simon Glass52c10c52016-01-14 18:10:37 -0700116 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
117 + 1) * priv->tab_width_frac;
Simon Glass84c7fb32016-01-18 19:52:17 -0700118
Simon Glass52c10c52016-01-14 18:10:37 -0700119 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass84c7fb32016-01-18 19:52:17 -0700120 vidconsole_newline(dev);
121 break;
122 case '\b':
123 vidconsole_back(dev);
Simon Glassafee7432016-01-14 18:10:40 -0700124 priv->last_ch = 0;
Simon Glass84c7fb32016-01-18 19:52:17 -0700125 break;
126 default:
127 /*
128 * Failure of this function normally indicates an unsupported
129 * colour depth. Check this and return an error to help with
130 * diagnosis.
131 */
Simon Glass52c10c52016-01-14 18:10:37 -0700132 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
133 if (ret == -EAGAIN) {
134 vidconsole_newline(dev);
135 ret = vidconsole_putc_xy(dev, priv->xcur_frac,
136 priv->ycur, ch);
137 }
138 if (ret < 0)
Simon Glass84c7fb32016-01-18 19:52:17 -0700139 return ret;
Simon Glass52c10c52016-01-14 18:10:37 -0700140 priv->xcur_frac += ret;
Simon Glassafee7432016-01-14 18:10:40 -0700141 priv->last_ch = ch;
Simon Glass52c10c52016-01-14 18:10:37 -0700142 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass84c7fb32016-01-18 19:52:17 -0700143 vidconsole_newline(dev);
144 break;
145 }
146
147 return 0;
148}
149
150static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
151{
152 struct udevice *dev = sdev->priv;
153
154 vidconsole_put_char(dev, ch);
155}
156
157static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
158{
159 struct udevice *dev = sdev->priv;
160
161 while (*s)
162 vidconsole_put_char(dev, *s++);
163}
164
165/* Set up the number of rows and colours (rotated drivers override this) */
166static int vidconsole_pre_probe(struct udevice *dev)
167{
168 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
169 struct udevice *vid = dev->parent;
170 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
171
Simon Glass52c10c52016-01-14 18:10:37 -0700172 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass84c7fb32016-01-18 19:52:17 -0700173
174 return 0;
175}
176
177/* Register the device with stdio */
178static int vidconsole_post_probe(struct udevice *dev)
179{
180 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
181 struct stdio_dev *sdev = &priv->sdev;
182 int ret;
183
Simon Glass52c10c52016-01-14 18:10:37 -0700184 if (!priv->tab_width_frac)
185 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
186
Simon Glass798ff502016-01-21 19:44:51 -0700187 if (dev->seq) {
188 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
189 dev->seq);
190 } else {
191 strcpy(sdev->name, "vidconsole");
192 }
Simon Glass52c10c52016-01-14 18:10:37 -0700193
Simon Glass84c7fb32016-01-18 19:52:17 -0700194 sdev->flags = DEV_FLAGS_OUTPUT;
195 sdev->putc = vidconsole_putc;
196 sdev->puts = vidconsole_puts;
197 sdev->priv = dev;
198 ret = stdio_register(sdev);
199 if (ret)
200 return ret;
201
202 return 0;
203}
204
205UCLASS_DRIVER(vidconsole) = {
206 .id = UCLASS_VIDEO_CONSOLE,
207 .name = "vidconsole0",
208 .pre_probe = vidconsole_pre_probe,
209 .post_probe = vidconsole_post_probe,
210 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
211};
212
213void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
214{
215 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glass52c10c52016-01-14 18:10:37 -0700216 struct udevice *vid_dev = dev->parent;
217 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Simon Glass84c7fb32016-01-18 19:52:17 -0700218
Simon Glass52c10c52016-01-14 18:10:37 -0700219 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
220 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
Simon Glass84c7fb32016-01-18 19:52:17 -0700221}
222
223static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
224 char *const argv[])
225{
226 unsigned int col, row;
227 struct udevice *dev;
228
229 if (argc != 3)
230 return CMD_RET_USAGE;
231
232 uclass_first_device(UCLASS_VIDEO_CONSOLE, &dev);
233 if (!dev)
234 return CMD_RET_FAILURE;
235 col = simple_strtoul(argv[1], NULL, 10);
236 row = simple_strtoul(argv[2], NULL, 10);
237 vidconsole_position_cursor(dev, col, row);
238
239 return 0;
240}
241
242static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
243 char *const argv[])
244{
245 struct udevice *dev;
246 const char *s;
247
248 if (argc != 2)
249 return CMD_RET_USAGE;
250
251 uclass_first_device(UCLASS_VIDEO_CONSOLE, &dev);
252 if (!dev)
253 return CMD_RET_FAILURE;
254 for (s = argv[1]; *s; s++)
255 vidconsole_put_char(dev, *s);
256
257 return 0;
258}
259
260U_BOOT_CMD(
261 setcurs, 3, 1, do_video_setcursor,
262 "set cursor position within screen",
263 " <col> <row> in character"
264);
265
266U_BOOT_CMD(
267 lcdputs, 2, 1, do_video_puts,
268 "print string on video framebuffer",
269 " <string>"
270);