Simon Glass | 84c7fb3 | 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 | afee743 | 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 | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 59 | /* Move backwards one space */ |
Simon Glass | 33bd3b6 | 2016-01-14 18:10:41 -0700 | [diff] [blame] | 60 | static int vidconsole_back(struct udevice *dev) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 61 | { |
| 62 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 33bd3b6 | 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 | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 71 | |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 72 | priv->xcur_frac -= VID_TO_POS(priv->x_charsize); |
Simon Glass | a74451d | 2016-01-14 18:10:39 -0700 | [diff] [blame] | 73 | if (priv->xcur_frac < priv->xstart_frac) { |
Simon Glass | 52c10c5 | 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 | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 79 | } |
Simon Glass | 2f63264 | 2017-06-12 06:21:36 -0600 | [diff] [blame] | 80 | video_sync(dev->parent); |
Simon Glass | 33bd3b6 | 2016-01-14 18:10:41 -0700 | [diff] [blame] | 81 | |
| 82 | return 0; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /* Move to a newline, scrolling the display if necessary */ |
| 86 | static void vidconsole_newline(struct udevice *dev) |
| 87 | { |
| 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; |
| 92 | int i; |
| 93 | |
Simon Glass | a74451d | 2016-01-14 18:10:39 -0700 | [diff] [blame] | 94 | priv->xcur_frac = priv->xstart_frac; |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 95 | priv->ycur += priv->y_charsize; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 96 | |
| 97 | /* Check if we need to scroll the terminal */ |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 98 | if ((priv->ycur + priv->y_charsize) / priv->y_charsize > priv->rows) { |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 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); |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 103 | priv->ycur -= rows * priv->y_charsize; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 104 | } |
Simon Glass | afee743 | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 105 | priv->last_ch = 0; |
| 106 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 107 | video_sync(dev->parent); |
| 108 | } |
| 109 | |
| 110 | int vidconsole_put_char(struct udevice *dev, char ch) |
| 111 | { |
| 112 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 113 | int ret; |
| 114 | |
| 115 | switch (ch) { |
Simon Glass | 37b8020 | 2016-01-14 18:10:38 -0700 | [diff] [blame] | 116 | case '\a': |
| 117 | /* beep */ |
| 118 | break; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 119 | case '\r': |
Simon Glass | a74451d | 2016-01-14 18:10:39 -0700 | [diff] [blame] | 120 | priv->xcur_frac = priv->xstart_frac; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 121 | break; |
| 122 | case '\n': |
| 123 | vidconsole_newline(dev); |
Simon Glass | afee743 | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 124 | vidconsole_entry_start(dev); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 125 | break; |
| 126 | case '\t': /* Tab (8 chars alignment) */ |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 127 | priv->xcur_frac = ((priv->xcur_frac / priv->tab_width_frac) |
| 128 | + 1) * priv->tab_width_frac; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 129 | |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 130 | if (priv->xcur_frac >= priv->xsize_frac) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 131 | vidconsole_newline(dev); |
| 132 | break; |
| 133 | case '\b': |
| 134 | vidconsole_back(dev); |
Simon Glass | afee743 | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 135 | priv->last_ch = 0; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 136 | break; |
| 137 | default: |
| 138 | /* |
| 139 | * Failure of this function normally indicates an unsupported |
| 140 | * colour depth. Check this and return an error to help with |
| 141 | * diagnosis. |
| 142 | */ |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 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, |
| 147 | priv->ycur, ch); |
| 148 | } |
| 149 | if (ret < 0) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 150 | return ret; |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 151 | priv->xcur_frac += ret; |
Simon Glass | afee743 | 2016-01-14 18:10:40 -0700 | [diff] [blame] | 152 | priv->last_ch = ch; |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 153 | if (priv->xcur_frac >= priv->xsize_frac) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 154 | vidconsole_newline(dev); |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | static void vidconsole_putc(struct stdio_dev *sdev, const char ch) |
| 162 | { |
| 163 | struct udevice *dev = sdev->priv; |
| 164 | |
| 165 | vidconsole_put_char(dev, ch); |
| 166 | } |
| 167 | |
| 168 | static void vidconsole_puts(struct stdio_dev *sdev, const char *s) |
| 169 | { |
| 170 | struct udevice *dev = sdev->priv; |
| 171 | |
| 172 | while (*s) |
| 173 | vidconsole_put_char(dev, *s++); |
Simon Glass | 4f3873d | 2016-01-30 16:37:41 -0700 | [diff] [blame] | 174 | video_sync(dev->parent); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Set up the number of rows and colours (rotated drivers override this) */ |
| 178 | static int vidconsole_pre_probe(struct udevice *dev) |
| 179 | { |
| 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); |
| 183 | |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 184 | priv->xsize_frac = VID_TO_POS(vid_priv->xsize); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* Register the device with stdio */ |
| 190 | static int vidconsole_post_probe(struct udevice *dev) |
| 191 | { |
| 192 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
| 193 | struct stdio_dev *sdev = &priv->sdev; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 194 | |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 195 | if (!priv->tab_width_frac) |
| 196 | priv->tab_width_frac = VID_TO_POS(priv->x_charsize) * 8; |
| 197 | |
Simon Glass | 798ff50 | 2016-01-21 19:44:51 -0700 | [diff] [blame] | 198 | if (dev->seq) { |
| 199 | snprintf(sdev->name, sizeof(sdev->name), "vidconsole%d", |
| 200 | dev->seq); |
| 201 | } else { |
| 202 | strcpy(sdev->name, "vidconsole"); |
| 203 | } |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 204 | |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 205 | sdev->flags = DEV_FLAGS_OUTPUT; |
| 206 | sdev->putc = vidconsole_putc; |
| 207 | sdev->puts = vidconsole_puts; |
| 208 | sdev->priv = dev; |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 209 | |
Masahiro Yamada | bf528cd | 2016-09-06 22:17:33 +0900 | [diff] [blame] | 210 | return stdio_register(sdev); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 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), |
| 219 | }; |
| 220 | |
| 221 | void vidconsole_position_cursor(struct udevice *dev, unsigned col, unsigned row) |
| 222 | { |
| 223 | struct vidconsole_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 224 | struct udevice *vid_dev = dev->parent; |
| 225 | struct video_priv *vid_priv = dev_get_uclass_priv(vid_dev); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 226 | |
Simon Glass | 52c10c5 | 2016-01-14 18:10:37 -0700 | [diff] [blame] | 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); |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static int do_video_setcursor(cmd_tbl_t *cmdtp, int flag, int argc, |
| 232 | char *const argv[]) |
| 233 | { |
| 234 | unsigned int col, row; |
| 235 | struct udevice *dev; |
| 236 | |
| 237 | if (argc != 3) |
| 238 | return CMD_RET_USAGE; |
| 239 | |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 240 | if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev)) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 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); |
| 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int do_video_puts(cmd_tbl_t *cmdtp, int flag, int argc, |
| 250 | char *const argv[]) |
| 251 | { |
| 252 | struct udevice *dev; |
| 253 | const char *s; |
| 254 | |
| 255 | if (argc != 2) |
| 256 | return CMD_RET_USAGE; |
| 257 | |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 258 | if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev)) |
Simon Glass | 84c7fb3 | 2016-01-18 19:52:17 -0700 | [diff] [blame] | 259 | return CMD_RET_FAILURE; |
| 260 | for (s = argv[1]; *s; s++) |
| 261 | vidconsole_put_char(dev, *s); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | U_BOOT_CMD( |
| 267 | setcurs, 3, 1, do_video_setcursor, |
| 268 | "set cursor position within screen", |
| 269 | " <col> <row> in character" |
| 270 | ); |
| 271 | |
| 272 | U_BOOT_CMD( |
| 273 | lcdputs, 2, 1, do_video_puts, |
| 274 | "print string on video framebuffer", |
| 275 | " <string>" |
| 276 | ); |