Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 2 | /* |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 3 | * (C) Copyright 2001-2015 |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 4 | * DENX Software Engineering -- wd@denx.de |
| 5 | * Compulab Ltd - http://compulab.co.il/ |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 6 | * Bernecker & Rainer Industrieelektronik GmbH - http://www.br-automation.com |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <command.h> |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 11 | #include <lcd.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | b03e051 | 2019-11-14 12:57:24 -0700 | [diff] [blame] | 13 | #include <serial.h> |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 14 | #include <video_font.h> /* Get font data, width and height */ |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 15 | #if defined(CONFIG_LCD_LOGO) |
| 16 | #include <bmp_logo.h> |
| 17 | #endif |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 18 | |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 19 | static struct console_t cons; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 20 | |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 21 | void lcd_set_col(short col) |
| 22 | { |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 23 | cons.curr_col = col; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void lcd_set_row(short row) |
| 27 | { |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 28 | cons.curr_row = row; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void lcd_position_cursor(unsigned col, unsigned row) |
| 32 | { |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 33 | cons.curr_col = min_t(short, col, cons.cols - 1); |
| 34 | cons.curr_row = min_t(short, row, cons.rows - 1); |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | int lcd_get_screen_rows(void) |
| 38 | { |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 39 | return cons.rows; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | int lcd_get_screen_columns(void) |
| 43 | { |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 44 | return cons.cols; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 47 | static void lcd_putc_xy0(struct console_t *pcons, ushort x, ushort y, char c) |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 48 | { |
Hannes Petermaier | a202c5b | 2015-03-27 08:01:36 +0100 | [diff] [blame] | 49 | int fg_color = lcd_getfgcolor(); |
| 50 | int bg_color = lcd_getbgcolor(); |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 51 | int i, row; |
| 52 | fbptr_t *dst = (fbptr_t *)pcons->fbbase + |
| 53 | y * pcons->lcdsizex + |
| 54 | x; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 55 | |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 56 | for (row = 0; row < VIDEO_FONT_HEIGHT; row++) { |
| 57 | uchar bits = video_fontdata[c * VIDEO_FONT_HEIGHT + row]; |
| 58 | for (i = 0; i < VIDEO_FONT_WIDTH; ++i) { |
| 59 | *dst++ = (bits & 0x80) ? fg_color : bg_color; |
Hannes Petermaier | 97562c1 | 2015-03-27 08:01:35 +0100 | [diff] [blame] | 60 | bits <<= 1; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 61 | } |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 62 | dst += (pcons->lcdsizex - VIDEO_FONT_WIDTH); |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 66 | static inline void console_setrow0(struct console_t *pcons, u32 row, int clr) |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 67 | { |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 68 | int i; |
| 69 | fbptr_t *dst = (fbptr_t *)pcons->fbbase + |
| 70 | row * VIDEO_FONT_HEIGHT * |
| 71 | pcons->lcdsizex; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 72 | |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 73 | for (i = 0; i < (VIDEO_FONT_HEIGHT * pcons->lcdsizex); i++) |
| 74 | *dst++ = clr; |
| 75 | } |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 76 | |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 77 | static inline void console_moverow0(struct console_t *pcons, |
| 78 | u32 rowdst, u32 rowsrc) |
| 79 | { |
| 80 | int i; |
| 81 | fbptr_t *dst = (fbptr_t *)pcons->fbbase + |
| 82 | rowdst * VIDEO_FONT_HEIGHT * |
| 83 | pcons->lcdsizex; |
| 84 | |
| 85 | fbptr_t *src = (fbptr_t *)pcons->fbbase + |
| 86 | rowsrc * VIDEO_FONT_HEIGHT * |
| 87 | pcons->lcdsizex; |
| 88 | |
| 89 | for (i = 0; i < (VIDEO_FONT_HEIGHT * pcons->lcdsizex); i++) |
| 90 | *dst++ = *src++; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static inline void console_back(void) |
| 94 | { |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 95 | if (--cons.curr_col < 0) { |
| 96 | cons.curr_col = cons.cols - 1; |
| 97 | if (--cons.curr_row < 0) |
| 98 | cons.curr_row = 0; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 101 | cons.fp_putc_xy(&cons, |
| 102 | cons.curr_col * VIDEO_FONT_WIDTH, |
| 103 | cons.curr_row * VIDEO_FONT_HEIGHT, ' '); |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static inline void console_newline(void) |
| 107 | { |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 108 | const int rows = CONFIG_CONSOLE_SCROLL_LINES; |
| 109 | int bg_color = lcd_getbgcolor(); |
| 110 | int i; |
| 111 | |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 112 | cons.curr_col = 0; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 113 | |
| 114 | /* Check if we need to scroll the terminal */ |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 115 | if (++cons.curr_row >= cons.rows) { |
| 116 | for (i = 0; i < cons.rows-rows; i++) |
| 117 | cons.fp_console_moverow(&cons, i, i+rows); |
| 118 | for (i = 0; i < rows; i++) |
| 119 | cons.fp_console_setrow(&cons, cons.rows-i-1, bg_color); |
| 120 | cons.curr_row -= rows; |
| 121 | } |
| 122 | lcd_sync(); |
| 123 | } |
| 124 | |
| 125 | void console_calc_rowcol(struct console_t *pcons, u32 sizex, u32 sizey) |
| 126 | { |
| 127 | pcons->cols = sizex / VIDEO_FONT_WIDTH; |
| 128 | #if defined(CONFIG_LCD_LOGO) && !defined(CONFIG_LCD_INFO_BELOW_LOGO) |
| 129 | pcons->rows = (pcons->lcdsizey - BMP_LOGO_HEIGHT); |
| 130 | pcons->rows /= VIDEO_FONT_HEIGHT; |
| 131 | #else |
| 132 | pcons->rows = sizey / VIDEO_FONT_HEIGHT; |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | void __weak lcd_init_console_rot(struct console_t *pcons) |
| 137 | { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | void lcd_init_console(void *address, int vl_cols, int vl_rows, int vl_rot) |
| 142 | { |
| 143 | memset(&cons, 0, sizeof(cons)); |
| 144 | cons.fbbase = address; |
| 145 | |
| 146 | cons.lcdsizex = vl_cols; |
| 147 | cons.lcdsizey = vl_rows; |
| 148 | cons.lcdrot = vl_rot; |
| 149 | |
| 150 | cons.fp_putc_xy = &lcd_putc_xy0; |
| 151 | cons.fp_console_moverow = &console_moverow0; |
| 152 | cons.fp_console_setrow = &console_setrow0; |
| 153 | console_calc_rowcol(&cons, cons.lcdsizex, cons.lcdsizey); |
| 154 | |
| 155 | lcd_init_console_rot(&cons); |
| 156 | |
| 157 | debug("lcd_console: have %d/%d col/rws on scr %dx%d (%d deg rotated)\n", |
| 158 | cons.cols, cons.rows, cons.lcdsizex, cons.lcdsizey, vl_rot); |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void lcd_putc(const char c) |
| 162 | { |
| 163 | if (!lcd_is_enabled) { |
| 164 | serial_putc(c); |
| 165 | |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | switch (c) { |
| 170 | case '\r': |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 171 | cons.curr_col = 0; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 172 | return; |
| 173 | case '\n': |
| 174 | console_newline(); |
| 175 | |
| 176 | return; |
| 177 | case '\t': /* Tab (8 chars alignment) */ |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 178 | cons.curr_col += 8; |
| 179 | cons.curr_col &= ~7; |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 180 | |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 181 | if (cons.curr_col >= cons.cols) |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 182 | console_newline(); |
| 183 | |
| 184 | return; |
| 185 | case '\b': |
| 186 | console_back(); |
| 187 | |
| 188 | return; |
| 189 | default: |
Hannes Petermaier | 604c7d4 | 2015-03-27 08:01:38 +0100 | [diff] [blame] | 190 | cons.fp_putc_xy(&cons, |
| 191 | cons.curr_col * VIDEO_FONT_WIDTH, |
| 192 | cons.curr_row * VIDEO_FONT_HEIGHT, c); |
Hannes Petermaier | 7471142 | 2015-03-27 08:01:37 +0100 | [diff] [blame] | 193 | if (++cons.curr_col >= cons.cols) |
Nikita Kiryanov | 904672e | 2014-12-08 17:14:45 +0200 | [diff] [blame] | 194 | console_newline(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void lcd_puts(const char *s) |
| 199 | { |
| 200 | if (!lcd_is_enabled) { |
| 201 | serial_puts(s); |
| 202 | |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | while (*s) |
| 207 | lcd_putc(*s++); |
| 208 | |
| 209 | lcd_sync(); |
| 210 | } |
| 211 | |
| 212 | void lcd_printf(const char *fmt, ...) |
| 213 | { |
| 214 | va_list args; |
| 215 | char buf[CONFIG_SYS_PBSIZE]; |
| 216 | |
| 217 | va_start(args, fmt); |
| 218 | vsprintf(buf, fmt, args); |
| 219 | va_end(args); |
| 220 | |
| 221 | lcd_puts(buf); |
| 222 | } |
Hannes Petermaier | d38d0c6 | 2015-02-03 13:22:24 +0100 | [diff] [blame] | 223 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 224 | static int do_lcd_setcursor(struct cmd_tbl *cmdtp, int flag, int argc, |
Hannes Petermaier | d38d0c6 | 2015-02-03 13:22:24 +0100 | [diff] [blame] | 225 | char *const argv[]) |
| 226 | { |
| 227 | unsigned int col, row; |
| 228 | |
| 229 | if (argc != 3) |
| 230 | return CMD_RET_USAGE; |
| 231 | |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 232 | col = dectoul(argv[1], NULL); |
| 233 | row = dectoul(argv[2], NULL); |
Hannes Petermaier | d38d0c6 | 2015-02-03 13:22:24 +0100 | [diff] [blame] | 234 | lcd_position_cursor(col, row); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 239 | static int do_lcd_puts(struct cmd_tbl *cmdtp, int flag, int argc, |
Hannes Petermaier | 1b7caf1 | 2015-02-03 13:22:25 +0100 | [diff] [blame] | 240 | char *const argv[]) |
| 241 | { |
| 242 | if (argc != 2) |
| 243 | return CMD_RET_USAGE; |
| 244 | |
| 245 | lcd_puts(argv[1]); |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
Hannes Petermaier | d38d0c6 | 2015-02-03 13:22:24 +0100 | [diff] [blame] | 250 | U_BOOT_CMD( |
| 251 | setcurs, 3, 1, do_lcd_setcursor, |
| 252 | "set cursor position within screen", |
| 253 | " <col> <row> in character" |
| 254 | ); |
Hannes Petermaier | 1b7caf1 | 2015-02-03 13:22:25 +0100 | [diff] [blame] | 255 | |
| 256 | U_BOOT_CMD( |
| 257 | lcdputs, 2, 1, do_lcd_puts, |
| 258 | "print string on lcd-framebuffer", |
| 259 | " <string>" |
| 260 | ); |