Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | int 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 | |
| 31 | int 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 | |
| 41 | int 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 Glass | 58c733a | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 50 | static 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 Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 59 | /* Move backwards one space */ |
Simon Glass | 7b9f7e4 | 2016-01-14 18:10:41 -0700 | [diff] [blame] | 60 | static int vidconsole_back(struct udevice *dev) |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 61 | { |
| 62 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 7b9f7e4 | 2016-01-14 18:10:41 -0700 | [diff] [blame] | 63 | struct vidconsole_ops *ops = vidconsole_get_ops(dev); |
| 64 | int ret; |
| 65 | |
| 66 | if (ops->backspace) { |
| 67 | ret = ops->backspace(dev); |
| 68 | if (ret != -ENOSYS) |
| 69 | return ret; |
| 70 | } |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 71 | |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 72 | priv->xcur_frac -= VID_TO_POS(priv->x_charsize); |
Simon Glass | c5b77d0 | 2016-01-14 18:10:39 -0700 | [diff] [blame] | 73 | if (priv->xcur_frac < priv->xstart_frac) { |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 74 | priv->xcur_frac = (priv->cols - 1) * |
| 75 | VID_TO_POS(priv->x_charsize); |
| 76 | priv->ycur -= priv->y_charsize; |
| 77 | if (priv->ycur < 0) |
| 78 | priv->ycur = 0; |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 79 | } |
Simon Glass | 7b9f7e4 | 2016-01-14 18:10:41 -0700 | [diff] [blame] | 80 | |
| 81 | return 0; |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* Move to a newline, scrolling the display if necessary */ |
| 85 | static void vidconsole_newline(struct udevice *dev) |
| 86 | { |
| 87 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 88 | struct udevice *vid_dev = dev->parent; |
| 89 | struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev); |
| 90 | const int rows = CONFIG_CONSOLE_SCROLL_LINES; |
| 91 | int i; |
| 92 | |
Simon Glass | c5b77d0 | 2016-01-14 18:10:39 -0700 | [diff] [blame] | 93 | priv->xcur_frac = priv->xstart_frac; |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 94 | priv->ycur += priv->y_charsize; |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 95 | |
| 96 | /* Check if we need to scroll the terminal */ |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 97 | if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) { |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 98 | vidconsole_move_rows(dev, 0, rows, priv->rows - rows); |
| 99 | for (i = 0; i < rows; i++) |
| 100 | vidconsole_set_row(dev, priv->rows - i - 1, |
| 101 | vid_priv->colour_bg); |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 102 | priv->ycur -= rows * priv->y_charsize; |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 103 | } |
Simon Glass | 58c733a | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 104 | priv->last_ch = 0; |
| 105 | |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 106 | video_sync(dev->parent); |
| 107 | } |
| 108 | |
| 109 | int vidconsole_put_char(struct udevice *dev, char ch) |
| 110 | { |
| 111 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 112 | int ret; |
| 113 | |
| 114 | switch (ch) { |
Simon Glass | 5508f10 | 2016-01-14 18:10:38 -0700 | [diff] [blame] | 115 | case '\a': |
| 116 | /* beep */ |
| 117 | break; |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 118 | case '\r': |
Simon Glass | c5b77d0 | 2016-01-14 18:10:39 -0700 | [diff] [blame] | 119 | priv->xcur_frac = priv->xstart_frac; |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 120 | break; |
| 121 | case '\n': |
| 122 | vidconsole_newline(dev); |
Simon Glass | 58c733a | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 123 | vidconsole_entry_start(dev); |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 124 | break; |
| 125 | case '\t': /* Tab (8 chars alignment) */ |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 126 | priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac) |
| 127 | + 1) * priv->tab_width_frac; |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 128 | |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 129 | if (priv->xcur_frac >= priv->xsize_frac) |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 130 | vidconsole_newline(dev); |
| 131 | break; |
| 132 | case '\b': |
| 133 | vidconsole_back(dev); |
Simon Glass | 58c733a | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 134 | priv->last_ch = 0; |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 135 | break; |
| 136 | default: |
| 137 | /* |
| 138 | * Failure of this function normally indicates an unsupported |
| 139 | * colour depth. Check this and return an error to help with |
| 140 | * diagnosis. |
| 141 | */ |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 142 | ret = vidconsole_putc_xy(dev, priv->xcur_frac, priv->ycur, ch); |
| 143 | if (ret == -EAGAIN) { |
| 144 | vidconsole_newline(dev); |
| 145 | ret = vidconsole_putc_xy(dev, priv->xcur_frac, |
| 146 | priv->ycur, ch); |
| 147 | } |
| 148 | if (ret < 0) |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 149 | return ret; |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 150 | priv->xcur_frac += ret; |
Simon Glass | 58c733a | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 151 | priv->last_ch = ch; |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 152 | if (priv->xcur_frac >= priv->xsize_frac) |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 153 | vidconsole_newline(dev); |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static void vidconsole_putc(struct stdio_dev *sdev, const char ch) |
| 161 | { |
| 162 | struct udevice *dev = sdev->priv; |
| 163 | |
| 164 | vidconsole_put_char(dev, ch); |
| 165 | } |
| 166 | |
| 167 | static void vidconsole_puts(struct stdio_dev *sdev, const char *s) |
| 168 | { |
| 169 | struct udevice *dev = sdev->priv; |
| 170 | |
| 171 | while (*s) |
| 172 | vidconsole_put_char(dev, *s++); |
| 173 | } |
| 174 | |
| 175 | /* Set up the number of rows and colours (rotated drivers override this) */ |
| 176 | static int vidconsole_pre_probe(struct udevice *dev) |
| 177 | { |
| 178 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 179 | struct udevice *vid = dev->parent; |
| 180 | struct video_priv *vid_priv = dev_get_uclass_priv(vid); |
| 181 | |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 182 | priv->xsize_frac = VID_TO_POS(vid_priv->xsize); |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* Register the device with stdio */ |
| 188 | static int vidconsole_post_probe(struct udevice *dev) |
| 189 | { |
| 190 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 191 | struct stdio_dev *sdev = &priv->sdev; |
| 192 | int ret; |
| 193 | |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 194 | if (!priv->tab_width_frac) |
| 195 | priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8; |
| 196 | |
Simon Glass | f1a1247 | 2016-01-21 19:44:51 -0700 | [diff] [blame] | 197 | if (dev->seq) { |
| 198 | snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d", |
| 199 | dev->seq); |
| 200 | } else { |
| 201 | strcpy(sdev->name, "vidconsole"); |
| 202 | } |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 203 | |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 204 | sdev->flags = DEV_FLAGS_OUTPUT; |
| 205 | sdev->putc = vidconsole_putc; |
| 206 | sdev->puts = vidconsole_puts; |
| 207 | sdev->priv = dev; |
| 208 | ret = stdio_register(sdev); |
| 209 | if (ret) |
| 210 | return ret; |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | UCLASS_DRIVER(vidconsole) = { |
| 216 | .id = UCLASS_VIDEO_CONSOLE, |
| 217 | .name = "vidconsole0", |
| 218 | .pre_probe = vidconsole_pre_probe, |
| 219 | .post_probe = vidconsole_post_probe, |
| 220 | .per_device_auto_alloc_size = sizeof(struct vidconsole_priv), |
| 221 | }; |
| 222 | |
| 223 | void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row) |
| 224 | { |
| 225 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 226 | struct udevice *vid_dev = dev->parent; |
| 227 | struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev); |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 228 | |
Simon Glass | f266178 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 229 | priv->xcur_frac = VID_TO_POS(min_t(short, col, vid_priv->xsize - 1)); |
| 230 | priv->ycur = min_t(short, row, vid_priv->ysize - 1); |
Simon Glass | 8351076 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc, |
| 234 | char *const argv[]) |
| 235 | { |
| 236 | unsigned int col, row; |
| 237 | struct udevice *dev; |
| 238 | |
| 239 | if (argc != 3) |
| 240 | return CMD_RET_USAGE; |
| 241 | |
| 242 | uclass_first_device(UCLASS_VIDEO_CONSOLE, &dev); |
| 243 | if (!dev) |
| 244 | return CMD_RET_FAILURE; |
| 245 | col = simple_strtoul(argv[1], NULL, 10); |
| 246 | row = simple_strtoul(argv[2], NULL, 10); |
| 247 | vidconsole_position_cursor(dev, col, row); |
| 248 | |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc, |
| 253 | char *const argv[]) |
| 254 | { |
| 255 | struct udevice *dev; |
| 256 | const char *s; |
| 257 | |
| 258 | if (argc != 2) |
| 259 | return CMD_RET_USAGE; |
| 260 | |
| 261 | uclass_first_device(UCLASS_VIDEO_CONSOLE, &dev); |
| 262 | if (!dev) |
| 263 | return CMD_RET_FAILURE; |
| 264 | for (s = argv[1]; *s; s++) |
| 265 | vidconsole_put_char(dev, *s); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | U_BOOT_CMD( |
| 271 | setcurs, 3, 1, do_video_setcursor, |
| 272 | "set cursor position within screen", |
| 273 | " <col> <row> in character" |
| 274 | ); |
| 275 | |
| 276 | U_BOOT_CMD( |
| 277 | lcdputs, 2, 1, do_video_puts, |
| 278 | "print string on video framebuffer", |
| 279 | " <string>" |
| 280 | ); |