blob: 2afe66fab1a23db1c1f8784faed7a0eac12e365b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass7d95f2a2014-02-27 13:26:19 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass7d95f2a2014-02-27 13:26:19 -07004 */
5
6#include <common.h>
Simon Glass3ade5bc2016-01-18 19:52:25 -07007#include <dm.h>
Simon Glass7d95f2a2014-02-27 13:26:19 -07008#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass3ade5bc2016-01-18 19:52:25 -070010#include <video.h>
Simon Glass401d1c42020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glass7d95f2a2014-02-27 13:26:19 -070012#include <asm/sdl.h>
Simon Glass6be88c72020-02-03 07:36:13 -070013#include <asm/state.h>
Simon Glass7d95f2a2014-02-27 13:26:19 -070014#include <asm/u-boot-sandbox.h>
Simon Glass8657ad42021-11-19 13:23:50 -070015#include <dm/device-internal.h>
Simon Glass3ade5bc2016-01-18 19:52:25 -070016#include <dm/test.h>
Simon Glass7d95f2a2014-02-27 13:26:19 -070017
18DECLARE_GLOBAL_DATA_PTR;
19
20enum {
Simon Glass3ade5bc2016-01-18 19:52:25 -070021 /* Default LCD size we support */
Simon Glass7d95f2a2014-02-27 13:26:19 -070022 LCD_MAX_WIDTH = 1366,
23 LCD_MAX_HEIGHT = 768,
Simon Glass7d95f2a2014-02-27 13:26:19 -070024};
25
Simon Glass3ade5bc2016-01-18 19:52:25 -070026static int sandbox_sdl_probe(struct udevice *dev)
Simon Glass7d95f2a2014-02-27 13:26:19 -070027{
Simon Glass8a8d24b2020-12-03 16:55:23 -070028 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glassc69cda22020-12-03 16:55:20 -070029 struct sandbox_sdl_plat *plat = dev_get_plat(dev);
Simon Glass3ade5bc2016-01-18 19:52:25 -070030 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6be88c72020-02-03 07:36:13 -070031 struct sandbox_state *state = state_get_current();
Simon Glass3ade5bc2016-01-18 19:52:25 -070032 int ret;
Simon Glass7d95f2a2014-02-27 13:26:19 -070033
Simon Glass6be88c72020-02-03 07:36:13 -070034 ret = sandbox_sdl_init_display(plat->xres, plat->yres, plat->bpix,
35 state->double_lcd);
Simon Glass3ade5bc2016-01-18 19:52:25 -070036 if (ret) {
Simon Glass7d95f2a2014-02-27 13:26:19 -070037 puts("LCD init failed\n");
Simon Glass3ade5bc2016-01-18 19:52:25 -070038 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;
Simon Glass8de536c2016-01-14 18:10:49 -070044 uc_priv->vidconsole_drv_name = plat->vidconsole_drv_name;
45 uc_priv->font_size = plat->font_size;
Simon Glassf578ca72020-07-02 21:12:29 -060046 if (IS_ENABLED(CONFIG_VIDEO_COPY))
Simon Glass84051742021-11-19 13:23:49 -070047 uc_plat->copy_base = uc_plat->base + uc_plat->size / 2;
Simon Glass3ade5bc2016-01-18 19:52:25 -070048
49 return 0;
Simon Glass7d95f2a2014-02-27 13:26:19 -070050}
51
Simon Glass0fe5e942021-11-19 13:23:45 -070052static void set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp)
Simon Glass7d95f2a2014-02-27 13:26:19 -070053{
Simon Glass8a8d24b2020-12-03 16:55:23 -070054 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glassc69cda22020-12-03 16:55:20 -070055 struct sandbox_sdl_plat *plat = dev_get_plat(dev);
Simon Glass7d95f2a2014-02-27 13:26:19 -070056
Simon Glass0fe5e942021-11-19 13:23:45 -070057 plat->bpix = l2bpp;
58
Simon Glass301af232021-11-19 13:23:48 -070059 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));
Simon Glassf578ca72020-07-02 21:12:29 -060071
72 /* Allow space for two buffers, the lower one being the copy buffer */
73 log_debug("Frame buffer size %x\n", uc_plat->size);
Simon Glass301af232021-11-19 13:23:48 -070074
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 */
Simon Glassf578ca72020-07-02 21:12:29 -060079 if (IS_ENABLED(CONFIG_VIDEO_COPY))
80 uc_plat->size *= 2;
Simon Glass0fe5e942021-11-19 13:23:45 -070081}
82
Simon Glass8657ad42021-11-19 13:23:50 -070083int sandbox_sdl_set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp)
84{
85 int ret;
86
87 if (device_active(dev))
88 return -EINVAL;
89 sandbox_sdl_remove_display();
90
91 set_bpp(dev, l2bpp);
92
93 ret = device_probe(dev);
94 if (ret)
95 return ret;
96
97 return 0;
98}
99
Simon Glass250e7352021-11-19 13:23:46 -0700100static int sandbox_sdl_remove(struct udevice *dev)
101{
102 /*
103 * Removing the display it a bit annoying when running unit tests, since
104 * they remove all devices. It is nice to be able to see what the test
105 * wrote onto the display. So this comment is just here to show how to
106 * do it, if we want to make it optional one day.
107 *
108 * sandbox_sdl_remove_display();
109 */
Simon Glass250e7352021-11-19 13:23:46 -0700110 return 0;
111}
112
Simon Glass0fe5e942021-11-19 13:23:45 -0700113static int sandbox_sdl_bind(struct udevice *dev)
114{
115 struct sandbox_sdl_plat *plat = dev_get_plat(dev);
116 enum video_log2_bpp l2bpp;
117 int ret = 0;
118
119 plat->xres = dev_read_u32_default(dev, "xres", LCD_MAX_WIDTH);
120 plat->yres = dev_read_u32_default(dev, "yres", LCD_MAX_HEIGHT);
121 l2bpp = dev_read_u32_default(dev, "log2-depth", VIDEO_BPP16);
122 plat->rot = dev_read_u32_default(dev, "rotate", 0);
123
124 set_bpp(dev, l2bpp);
Simon Glass7d95f2a2014-02-27 13:26:19 -0700125
126 return ret;
127}
Simon Glass3ade5bc2016-01-18 19:52:25 -0700128
129static const struct udevice_id sandbox_sdl_ids[] = {
130 { .compatible = "sandbox,lcd-sdl" },
131 { }
132};
133
Walter Lozanoe3e24702020-06-25 01:10:04 -0300134U_BOOT_DRIVER(sandbox_lcd_sdl) = {
135 .name = "sandbox_lcd_sdl",
Simon Glass3ade5bc2016-01-18 19:52:25 -0700136 .id = UCLASS_VIDEO,
137 .of_match = sandbox_sdl_ids,
138 .bind = sandbox_sdl_bind,
139 .probe = sandbox_sdl_probe,
Simon Glass250e7352021-11-19 13:23:46 -0700140 .remove = sandbox_sdl_remove,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700141 .plat_auto = sizeof(struct sandbox_sdl_plat),
Simon Glass3ade5bc2016-01-18 19:52:25 -0700142};