blob: 9081c7da62e4b52f1e8009b9c54d338176192618 [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
6#include <common.h>
Simon Glass38a2ae22016-01-18 19:52:25 -07007#include <dm.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -07008#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass38a2ae22016-01-18 19:52:25 -070010#include <video.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -070012#include <asm/sdl.h>
Simon Glassf91de0b2020-02-03 07:36:13 -070013#include <asm/state.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -070014#include <asm/u-boot-sandbox.h>
Simon Glassc3b5adf2021-11-19 13:23:50 -070015#include <dm/device-internal.h>
Simon Glass38a2ae22016-01-18 19:52:25 -070016#include <dm/test.h>
Simon Glassb9ddbf42014-02-27 13:26:19 -070017
18DECLARE_GLOBAL_DATA_PTR;
19
20enum {
Simon Glass38a2ae22016-01-18 19:52:25 -070021 /* Default LCD size we support */
Simon Glassb9ddbf42014-02-27 13:26:19 -070022 LCD_MAX_WIDTH = 1366,
23 LCD_MAX_HEIGHT = 768,
Simon Glassb9ddbf42014-02-27 13:26:19 -070024};
25
Simon Glass38a2ae22016-01-18 19:52:25 -070026static int sandbox_sdl_probe(struct udevice *dev)
Simon Glassb9ddbf42014-02-27 13:26:19 -070027{
Simon Glassb75b15b2020-12-03 16:55:23 -070028 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glassfa20e932020-12-03 16:55:20 -070029 struct sandbox_sdl_plat *plat = dev_get_plat(dev);
Simon Glass38a2ae22016-01-18 19:52:25 -070030 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassf91de0b2020-02-03 07:36:13 -070031 struct sandbox_state *state = state_get_current();
Simon Glass38a2ae22016-01-18 19:52:25 -070032 int ret;
Simon Glassb9ddbf42014-02-27 13:26:19 -070033
Simon Glassf91de0b2020-02-03 07:36:13 -070034 ret = sandbox_sdl_init_display(plat->xres, plat->yres, plat->bpix,
35 state->double_lcd);
Simon Glass38a2ae22016-01-18 19:52:25 -070036 if (ret) {
Simon Glassb9ddbf42014-02-27 13:26:19 -070037 puts("LCD init failed\n");
Simon Glass38a2ae22016-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 Glass69f617f2016-01-14 18:10:49 -070044 uc_priv->vidconsole_drv_name = plat->vidconsole_drv_name;
45 uc_priv->font_size = plat->font_size;
Simon Glass2a0f8e32020-07-02 21:12:29 -060046 if (IS_ENABLED(CONFIG_VIDEO_COPY))
Simon Glassa45738a2021-11-19 13:23:49 -070047 uc_plat->copy_base = uc_plat->base + uc_plat->size / 2;
Simon Glass38a2ae22016-01-18 19:52:25 -070048
49 return 0;
Simon Glassb9ddbf42014-02-27 13:26:19 -070050}
51
Simon Glassc3cfd762021-11-19 13:23:45 -070052static void set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp)
Simon Glassb9ddbf42014-02-27 13:26:19 -070053{
Simon Glassb75b15b2020-12-03 16:55:23 -070054 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glassfa20e932020-12-03 16:55:20 -070055 struct sandbox_sdl_plat *plat = dev_get_plat(dev);
Simon Glassb9ddbf42014-02-27 13:26:19 -070056
Simon Glassc3cfd762021-11-19 13:23:45 -070057 plat->bpix = l2bpp;
58
Simon Glass0c5b59e2021-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 Glass2a0f8e32020-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 Glass0c5b59e2021-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 Glass2a0f8e32020-07-02 21:12:29 -060079 if (IS_ENABLED(CONFIG_VIDEO_COPY))
80 uc_plat->size *= 2;
Simon Glassc3cfd762021-11-19 13:23:45 -070081}
82
Simon Glassc3b5adf2021-11-19 13:23:50 -070083int sandbox_sdl_set_bpp(struct udevice *dev, enum video_log2_bpp l2bpp)
84{
Simon Glass87a3cd72021-11-19 13:24:03 -070085 struct video_uc_plat *uc_plat = dev_get_uclass_plat(dev);
Simon Glassc3b5adf2021-11-19 13:23:50 -070086 int ret;
87
88 if (device_active(dev))
89 return -EINVAL;
90 sandbox_sdl_remove_display();
91
Simon Glass87a3cd72021-11-19 13:24:03 -070092 uc_plat->hide_logo = true;
Simon Glassc3b5adf2021-11-19 13:23:50 -070093 set_bpp(dev, l2bpp);
94
95 ret = device_probe(dev);
96 if (ret)
97 return ret;
98
99 return 0;
100}
101
Simon Glasscce74802021-11-19 13:23:46 -0700102static 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 */
Simon Glasscce74802021-11-19 13:23:46 -0700112 return 0;
113}
114
Simon Glassc3cfd762021-11-19 13:23:45 -0700115static 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);
Simon Glassb9ddbf42014-02-27 13:26:19 -0700127
128 return ret;
129}
Simon Glass38a2ae22016-01-18 19:52:25 -0700130
131static const struct udevice_id sandbox_sdl_ids[] = {
132 { .compatible = "sandbox,lcd-sdl" },
133 { }
134};
135
Walter Lozano2901ac62020-06-25 01:10:04 -0300136U_BOOT_DRIVER(sandbox_lcd_sdl) = {
137 .name = "sandbox_lcd_sdl",
Simon Glass38a2ae22016-01-18 19:52:25 -0700138 .id = UCLASS_VIDEO,
139 .of_match = sandbox_sdl_ids,
140 .bind = sandbox_sdl_bind,
141 .probe = sandbox_sdl_probe,
Simon Glasscce74802021-11-19 13:23:46 -0700142 .remove = sandbox_sdl_remove,
Simon Glass71fa5b42020-12-03 16:55:18 -0700143 .plat_auto = sizeof(struct sandbox_sdl_plat),
Simon Glass38a2ae22016-01-18 19:52:25 -0700144};