1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <fdtdec.h>
9 #include <log.h>
10 #include <video.h>
11 #include <asm/global_data.h>
12 #include <asm/sdl.h>
13 #include <asm/state.h>
14 #include <asm/u-boot-sandbox.h>
15 #include <dm/device-internal.h>
16 #include <dm/test.h>
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
20 enum {
21 	/* Default LCD size we support */
22 	LCD_MAX_WIDTH		= 1366,
23 	LCD_MAX_HEIGHT		= 768,
24 };
25 
sandbox_sdl_probe(struct udevice * dev)26 static int sandbox_sdl_probe(struct udevice *dev)
27 {
28 	struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
29 	struct sandbox_sdl_plat *plat = dev_get_plat(dev);
30 	struct video_priv *uc_priv = dev_get_uclass_priv(dev);
31 	struct sandbox_state *state = state_get_current();
32 	int ret;
33 
34 	ret = sandbox_sdl_init_display(plat->xres, plat->yres, plat->bpix,
35 				       state->double_lcd);
36 	if (ret) {
37 		puts("LCD init failed\n");
38 		return ret;
39 	}
40 	uc_priv->xsize = plat->xres;
41 	uc_priv->ysize = plat->yres;
42 	uc_priv->bpix = plat->bpix;
43 	uc_priv->rot = plat->rot;
44 	uc_priv->vidconsole_drv_name = plat->vidconsole_drv_name;
45 	uc_priv->font_size = plat->font_size;
46 	if (IS_ENABLED(CONFIG_VIDEO_COPY))
47 		uc_plat->copy_base = uc_plat->base + uc_plat->size / 2;
48 
49 	return 0;
50 }
51 
set_bpp(struct udevice * dev,enum video_log2_bpp l2bpp)52 static void set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp)
53 {
54 	struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
55 	struct sandbox_sdl_plat *plat = dev_get_plat(dev);
56 
57 	plat->bpix = l2bpp;
58 
59 	uc_plat->size = plat->xres * plat->yres * VNBYTES(plat->bpix);
60 
61 	/*
62 	 * Set up to the maximum size we'll ever need. This is a strange case.
63 	 * The video memory is allocated by video_post_bind() called from
64 	 * board_init_r(). If a test changes the reoslution so it needs more
65 	 * memory later (with sandbox_sdl_set_bpp()), it is too late to make
66 	 * the frame buffer larger.
67 	 *
68 	 * So use a maximum size here.
69 	 */
70 	uc_plat->size = max(uc_plat->size, 1920U * 1080 * VNBYTES(VIDEO_BPP32));
71 
72 	/* Allow space for two buffers, the lower one being the copy buffer */
73 	log_debug("Frame buffer size %x\n", uc_plat->size);
74 
75 	/*
76 	 * If a copy framebuffer is used, double the size and use the last half
77 	 * as the copy, with the first half as the normal frame buffer.
78 	 */
79 	if (IS_ENABLED(CONFIG_VIDEO_COPY))
80 		uc_plat->size *= 2;
81 }
82 
sandbox_sdl_set_bpp(struct udevice * dev,enum video_log2_bpp l2bpp)83 int sandbox_sdl_set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp)
84 {
85 	struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
86 	int ret;
87 
88 	if (device_active(dev))
89 		return -EINVAL;
90 	sandbox_sdl_remove_display();
91 
92 	uc_plat->hide_logo = true;
93 	set_bpp(dev, l2bpp);
94 
95 	ret = device_probe(dev);
96 	if (ret)
97 		return ret;
98 
99 	return 0;
100 }
101 
sandbox_sdl_remove(struct udevice * dev)102 static int sandbox_sdl_remove(struct udevice *dev)
103 {
104 	/*
105 	 * Removing the display it a bit annoying when running unit tests, since
106 	 * they remove all devices. It is nice to be able to see what the test
107 	 * wrote onto the display. So this comment is just here to show how to
108 	 * do it, if we want to make it optional one day.
109 	 *
110 	 * sandbox_sdl_remove_display();
111 	 */
112 	return 0;
113 }
114 
sandbox_sdl_bind(struct udevice * dev)115 static int sandbox_sdl_bind(struct udevice *dev)
116 {
117 	struct sandbox_sdl_plat *plat = dev_get_plat(dev);
118 	enum video_log2_bpp l2bpp;
119 	int ret = 0;
120 
121 	plat->xres = dev_read_u32_default(dev, "xres", LCD_MAX_WIDTH);
122 	plat->yres = dev_read_u32_default(dev, "yres", LCD_MAX_HEIGHT);
123 	l2bpp = dev_read_u32_default(dev, "log2-depth", VIDEO_BPP16);
124 	plat->rot = dev_read_u32_default(dev, "rotate", 0);
125 
126 	set_bpp(dev, l2bpp);
127 
128 	return ret;
129 }
130 
131 static const struct udevice_id sandbox_sdl_ids[] = {
132 	{ .compatible = "sandbox,lcd-sdl" },
133 	{ }
134 };
135 
136 U_BOOT_DRIVER(sandbox_lcd_sdl) = {
137 	.name	= "sandbox_lcd_sdl",
138 	.id	= UCLASS_VIDEO,
139 	.of_match = sandbox_sdl_ids,
140 	.bind	= sandbox_sdl_bind,
141 	.probe	= sandbox_sdl_probe,
142 	.remove	= sandbox_sdl_remove,
143 	.plat_auto	= sizeof(struct sandbox_sdl_plat),
144 };
145