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