blob: 69dfa9302735b0f23bf3c4065516135ccb89d7f0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb9ddbf42014-02-27 13:26:19 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glassb9ddbf42014-02-27 13:26:19 -07004 */
5
Simon Glass38a2ae22016-01-18 19:52:25 -07006#include <dm.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -07007#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass38a2ae22016-01-18 19:52:25 -07009#include <video.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -070011#include <asm/sdl.h>
Simon Glassf91de0b2020-02-03 07:36:13 -070012#include <asm/state.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -070013#include <asm/u-boot-sandbox.h>
Simon Glassc3b5adf2021-11-19 13:23:50 -070014#include <dm/device-internal.h>
Simon Glass38a2ae22016-01-18 19:52:25 -070015#include <dm/test.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -070016
17DECLARE_GLOBAL_DATA_PTR;
18
19enum {
Simon Glass38a2ae22016-01-18 19:52:25 -070020 /* Default LCD size we support */
Simon Glassb9ddbf42014-02-27 13:26:19 -070021 LCD_MAX_WIDTH = 1366,
22 LCD_MAX_HEIGHT = 768,
Simon Glassb9ddbf42014-02-27 13:26:19 -070023};
24
Simon Glass38a2ae22016-01-18 19:52:25 -070025static int sandbox_sdl_probe(struct udevice *dev)
Simon Glassb9ddbf42014-02-27 13:26:19 -070026{
Simon Glassb75b15b2020-12-03 16:55:23 -070027 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glassfa20e932020-12-03 16:55:20 -070028 struct sandbox_sdl_plat *plat = dev_get_plat(dev);
Simon Glass38a2ae22016-01-18 19:52:25 -070029 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassf91de0b2020-02-03 07:36:13 -070030 struct sandbox_state *state = state_get_current();
Simon Glass38a2ae22016-01-18 19:52:25 -070031 int ret;
Simon Glassb9ddbf42014-02-27 13:26:19 -070032
Simon Glassf91de0b2020-02-03 07:36:13 -070033 ret = sandbox_sdl_init_display(plat->xres, plat->yres, plat->bpix,
34 state->double_lcd);
Simon Glass38a2ae22016-01-18 19:52:25 -070035 if (ret) {
Simon Glassb9ddbf42014-02-27 13:26:19 -070036 puts("LCD init failed\n");
Simon Glass38a2ae22016-01-18 19:52:25 -070037 return ret;
38 }
39 uc_priv->xsize = plat->xres;
40 uc_priv->ysize = plat->yres;
41 uc_priv->bpix = plat->bpix;
42 uc_priv->rot = plat->rot;
Simon Glass69f617f2016-01-14 18:10:49 -070043 uc_priv->vidconsole_drv_name = plat->vidconsole_drv_name;
44 uc_priv->font_size = plat->font_size;
Simon Glass2a0f8e32020-07-02 21:12:29 -060045 if (IS_ENABLED(CONFIG_VIDEO_COPY))
Simon Glassa45738a2021-11-19 13:23:49 -070046 uc_plat->copy_base = uc_plat->base + uc_plat->size / 2;
Simon Glass38a2ae22016-01-18 19:52:25 -070047
48 return 0;
Simon Glassb9ddbf42014-02-27 13:26:19 -070049}
50
Simon Glassc3cfd762021-11-19 13:23:45 -070051static void set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp)
Simon Glassb9ddbf42014-02-27 13:26:19 -070052{
Simon Glassb75b15b2020-12-03 16:55:23 -070053 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glassfa20e932020-12-03 16:55:20 -070054 struct sandbox_sdl_plat *plat = dev_get_plat(dev);
Simon Glassb9ddbf42014-02-27 13:26:19 -070055
Simon Glassc3cfd762021-11-19 13:23:45 -070056 plat->bpix = l2bpp;
57
Simon Glass0c5b59e2021-11-19 13:23:48 -070058 uc_plat->size = plat->xres * plat->yres * VNBYTES(plat->bpix);
59
60 /*
61 * Set up to the maximum size we'll ever need. This is a strange case.
62 * The video memory is allocated by video_post_bind() called from
63 * board_init_r(). If a test changes the reoslution so it needs more
64 * memory later (with sandbox_sdl_set_bpp()), it is too late to make
65 * the frame buffer larger.
66 *
67 * So use a maximum size here.
68 */
69 uc_plat->size = max(uc_plat->size, 1920U * 1080 * VNBYTES(VIDEO_BPP32));
Simon Glass2a0f8e32020-07-02 21:12:29 -060070
71 /* Allow space for two buffers, the lower one being the copy buffer */
72 log_debug("Frame buffer size %x\n", uc_plat->size);
Simon Glass0c5b59e2021-11-19 13:23:48 -070073
74 /*
75 * If a copy framebuffer is used, double the size and use the last half
76 * as the copy, with the first half as the normal frame buffer.
77 */
Simon Glass2a0f8e32020-07-02 21:12:29 -060078 if (IS_ENABLED(CONFIG_VIDEO_COPY))
79 uc_plat->size *= 2;
Simon Glassc3cfd762021-11-19 13:23:45 -070080}
81
Simon Glassc3b5adf2021-11-19 13:23:50 -070082int sandbox_sdl_set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp)
83{
Simon Glass87a3cd72021-11-19 13:24:03 -070084 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glassc3b5adf2021-11-19 13:23:50 -070085 int ret;
86
87 if (device_active(dev))
88 return -EINVAL;
89 sandbox_sdl_remove_display();
90
Simon Glass87a3cd72021-11-19 13:24:03 -070091 uc_plat->hide_logo = true;
Simon Glassc3b5adf2021-11-19 13:23:50 -070092 set_bpp(dev, l2bpp);
93
94 ret = device_probe(dev);
95 if (ret)
96 return ret;
97
98 return 0;
99}
100
Simon Glasscce74802021-11-19 13:23:46 -0700101static int sandbox_sdl_remove(struct udevice *dev)
102{
103 /*
104 * Removing the display it a bit annoying when running unit tests, since
105 * they remove all devices. It is nice to be able to see what the test
106 * wrote onto the display. So this comment is just here to show how to
107 * do it, if we want to make it optional one day.
108 *
109 * sandbox_sdl_remove_display();
110 */
Simon Glasscce74802021-11-19 13:23:46 -0700111 return 0;
112}
113
Simon Glassc3cfd762021-11-19 13:23:45 -0700114static int sandbox_sdl_bind(struct udevice *dev)
115{
116 struct sandbox_sdl_plat *plat = dev_get_plat(dev);
117 enum video_log2_bpp l2bpp;
118 int ret = 0;
119
120 plat->xres = dev_read_u32_default(dev, "xres", LCD_MAX_WIDTH);
121 plat->yres = dev_read_u32_default(dev, "yres", LCD_MAX_HEIGHT);
122 l2bpp = dev_read_u32_default(dev, "log2-depth", VIDEO_BPP16);
123 plat->rot = dev_read_u32_default(dev, "rotate", 0);
124
125 set_bpp(dev, l2bpp);
Simon Glassb9ddbf42014-02-27 13:26:19 -0700126
127 return ret;
128}
Simon Glass38a2ae22016-01-18 19:52:25 -0700129
130static const struct udevice_id sandbox_sdl_ids[] = {
131 { .compatible = "sandbox,lcd-sdl" },
132 { }
133};
134
Walter Lozano2901ac62020-06-25 01:10:04 -0300135U_BOOT_DRIVER(sandbox_lcd_sdl) = {
136 .name = "sandbox_lcd_sdl",
Simon Glass38a2ae22016-01-18 19:52:25 -0700137 .id = UCLASS_VIDEO,
138 .of_match = sandbox_sdl_ids,
139 .bind = sandbox_sdl_bind,
140 .probe = sandbox_sdl_probe,
Simon Glasscce74802021-11-19 13:23:46 -0700141 .remove = sandbox_sdl_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -0700142 .plat_auto = sizeof(struct sandbox_sdl_plat),
Simon Glass38a2ae22016-01-18 19:52:25 -0700143};