blob: 884db01f067eab6f64bef98b8f4d545c75a3bc4a [file] [log] [blame]
Simon Glass83510762016-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
50/* Move backwards one space */
51static void vidconsole_back(struct udevice *dev)
52{
53 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
54
Simon Glassf2661782016-01-14 18:10:37 -070055 priv->xcur_frac -= VID_TO_POS(priv->x_charsize);
Simon Glassc5b77d02016-01-14 18:10:39 -070056 if (priv->xcur_frac < priv->xstart_frac) {
Simon Glassf2661782016-01-14 18:10:37 -070057 priv->xcur_frac = (priv->cols - 1) *
58 VID_TO_POS(priv->x_charsize);
59 priv->ycur -= priv->y_charsize;
60 if (priv->ycur < 0)
61 priv->ycur = 0;
Simon Glass83510762016-01-18 19:52:17 -070062 }
Simon Glass83510762016-01-18 19:52:17 -070063}
64
65/* Move to a newline, scrolling the display if necessary */
66static void vidconsole_newline(struct udevice *dev)
67{
68 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
69 struct udevice *vid_dev = dev->parent;
70 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
71 const int rows = CONFIG_CONSOLE_SCROLL_LINES;
72 int i;
73
Simon Glassc5b77d02016-01-14 18:10:39 -070074 priv->xcur_frac = priv->xstart_frac;
Simon Glassf2661782016-01-14 18:10:37 -070075 priv->ycur += priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -070076
77 /* Check if we need to scroll the terminal */
Simon Glassf2661782016-01-14 18:10:37 -070078 if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) {
Simon Glass83510762016-01-18 19:52:17 -070079 vidconsole_move_rows(dev, 0, rows, priv->rows - rows);
80 for (i = 0; i < rows; i++)
81 vidconsole_set_row(dev, priv->rows - i - 1,
82 vid_priv->colour_bg);
Simon Glassf2661782016-01-14 18:10:37 -070083 priv->ycur -= rows * priv->y_charsize;
Simon Glass83510762016-01-18 19:52:17 -070084 }
85 video_sync(dev->parent);
86}
87
88int vidconsole_put_char(struct udevice *dev, char ch)
89{
90 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
91 int ret;
92
93 switch (ch) {
Simon Glass5508f102016-01-14 18:10:38 -070094 case '\a':
95 /* beep */
96 break;
Simon Glass83510762016-01-18 19:52:17 -070097 case '\r':
Simon Glassc5b77d02016-01-14 18:10:39 -070098 priv->xcur_frac = priv->xstart_frac;
Simon Glass83510762016-01-18 19:52:17 -070099 break;
100 case '\n':
101 vidconsole_newline(dev);
102 break;
103 case '\t': /* Tab (8 chars alignment) */
Simon Glassf2661782016-01-14 18:10:37 -0700104 priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac)
105 + 1) * priv->tab_width_frac;
Simon Glass83510762016-01-18 19:52:17 -0700106
Simon Glassf2661782016-01-14 18:10:37 -0700107 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass83510762016-01-18 19:52:17 -0700108 vidconsole_newline(dev);
109 break;
110 case '\b':
111 vidconsole_back(dev);
112 break;
113 default:
114 /*
115 * Failure of this function normally indicates an unsupported
116 * colour depth. Check this and return an error to help with
117 * diagnosis.
118 */
Simon Glassf2661782016-01-14 18:10:37 -0700119 ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch);
120 if (ret == -EAGAIN) {
121 vidconsole_newline(dev);
122 ret = vidconsole_putc_xy(dev, priv->xcur_frac,
123 priv->ycur, ch);
124 }
125 if (ret < 0)
Simon Glass83510762016-01-18 19:52:17 -0700126 return ret;
Simon Glassf2661782016-01-14 18:10:37 -0700127 priv->xcur_frac += ret;
128 if (priv->xcur_frac >= priv->xsize_frac)
Simon Glass83510762016-01-18 19:52:17 -0700129 vidconsole_newline(dev);
130 break;
131 }
132
133 return 0;
134}
135
136static void vidconsole_putc(struct stdio_dev *sdev, const char ch)
137{
138 struct udevice *dev = sdev->priv;
139
140 vidconsole_put_char(dev, ch);
141}
142
143static void vidconsole_puts(struct stdio_dev *sdev, const char *s)
144{
145 struct udevice *dev = sdev->priv;
146
147 while (*s)
148 vidconsole_put_char(dev, *s++);
149}
150
151/* Set up the number of rows and colours (rotated drivers override this) */
152static int vidconsole_pre_probe(struct udevice *dev)
153{
154 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
155 struct udevice *vid = dev->parent;
156 struct video_priv *vid_priv = dev_get_uclass_priv(vid);
157
Simon Glassf2661782016-01-14 18:10:37 -0700158 priv->xsize_frac = VID_TO_POS(vid_priv->xsize);
Simon Glass83510762016-01-18 19:52:17 -0700159
160 return 0;
161}
162
163/* Register the device with stdio */
164static int vidconsole_post_probe(struct udevice *dev)
165{
166 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
167 struct stdio_dev *sdev = &priv->sdev;
168 int ret;
169
Simon Glassf2661782016-01-14 18:10:37 -0700170 if (!priv->tab_width_frac)
171 priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8;
172
Simon Glassf1a12472016-01-21 19:44:51 -0700173 if (dev->seq) {
174 snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d",
175 dev->seq);
176 } else {
177 strcpy(sdev->name, "vidconsole");
178 }
Simon Glassf2661782016-01-14 18:10:37 -0700179
Simon Glass83510762016-01-18 19:52:17 -0700180 sdev->flags = DEV_FLAGS_OUTPUT;
181 sdev->putc = vidconsole_putc;
182 sdev->puts = vidconsole_puts;
183 sdev->priv = dev;
184 ret = stdio_register(sdev);
185 if (ret)
186 return ret;
187
188 return 0;
189}
190
191UCLASS_DRIVER(vidconsole) = {
192 .id = UCLASS_VIDEO_CONSOLE,
193 .name = "vidconsole0",
194 .pre_probe = vidconsole_pre_probe,
195 .post_probe = vidconsole_post_probe,
196 .per_device_auto_alloc_size = sizeof(struct vidconsole_priv),
197};
198
199void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row)
200{
201 struct vidconsole_priv *priv = dev_get_uclass_priv(dev);
Simon Glassf2661782016-01-14 18:10:37 -0700202 struct udevice *vid_dev = dev->parent;
203 struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev);
Simon Glass83510762016-01-18 19:52:17 -0700204
Simon Glassf2661782016-01-14 18:10:37 -0700205 priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1));
206 priv->ycur = min_t(short, row, vid_priv->ysize - 1);
Simon Glass83510762016-01-18 19:52:17 -0700207}
208
209static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc,
210 char *const argv[])
211{
212 unsigned int col, row;
213 struct udevice *dev;
214
215 if (argc != 3)
216 return CMD_RET_USAGE;
217
218 uclass_first_device(UCLASS_VIDEO_CONSOLE, &dev);
219 if (!dev)
220 return CMD_RET_FAILURE;
221 col = simple_strtoul(argv[1], NULL, 10);
222 row = simple_strtoul(argv[2], NULL, 10);
223 vidconsole_position_cursor(dev, col, row);
224
225 return 0;
226}
227
228static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc,
229 char *const argv[])
230{
231 struct udevice *dev;
232 const char *s;
233
234 if (argc != 2)
235 return CMD_RET_USAGE;
236
237 uclass_first_device(UCLASS_VIDEO_CONSOLE, &dev);
238 if (!dev)
239 return CMD_RET_FAILURE;
240 for (s = argv[1]; *s; s++)
241 vidconsole_put_char(dev, *s);
242
243 return 0;
244}
245
246U_BOOT_CMD(
247 setcurs, 3, 1, do_video_setcursor,
248 "set cursor position within screen",
249 " <col> <row> in character"
250);
251
252U_BOOT_CMD(
253 lcdputs, 2, 1, do_video_puts,
254 "print string on video framebuffer",
255 " <string>"
256);