1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Simplefb device tree support
4 *
5 * (C) Copyright 2015
6 * Stephen Warren <swarren@wwwdotorg.org>
7 */
8
9#include <common.h>
10#include <dm.h>
11#include <fdt_support.h>
12#include <asm/global_data.h>
13#include <linux/libfdt.h>
14#include <video.h>
15#include <spl.h>
16#include <bloblist.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20static int fdt_simplefb_configure_node(void *blob, int off)
21{
22	int xsize, ysize;
23	int bpix; /* log2 of bits per pixel */
24	const char *name;
25	ulong fb_base;
26	struct video_uc_plat *plat;
27	struct video_priv *uc_priv;
28	struct udevice *dev;
29	int ret;
30
31	if (IS_ENABLED(CONFIG_SPL_VIDEO_HANDOFF) && spl_phase() > PHASE_SPL) {
32		struct video_handoff *ho;
33
34		ho = bloblist_find(BLOBLISTT_U_BOOT_VIDEO, sizeof(*ho));
35		if (!ho)
36			return log_msg_ret("Missing video bloblist", -ENOENT);
37
38		xsize = ho->xsize;
39		ysize = ho->ysize;
40		bpix = ho->bpix;
41		fb_base = ho->fb;
42	} else {
43		ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
44		if (ret)
45			return ret;
46		uc_priv = dev_get_uclass_priv(dev);
47		plat = dev_get_uclass_plat(dev);
48		xsize = uc_priv->xsize;
49		ysize = uc_priv->ysize;
50		bpix = uc_priv->bpix;
51		fb_base = plat->base;
52	}
53
54	switch (bpix) {
55	case 4: /* VIDEO_BPP16 */
56		name = "r5g6b5";
57		break;
58	case 5: /* VIDEO_BPP32 */
59		name = "a8r8g8b8";
60		break;
61	default:
62		return -EINVAL;
63	}
64
65	return fdt_setup_simplefb_node(blob, off, fb_base, xsize, ysize,
66				       xsize * (1 << bpix) / 8, name);
67}
68
69int fdt_simplefb_add_node(void *blob)
70{
71	static const char compat[] = "simple-framebuffer";
72	static const char disabled[] = "disabled";
73	int off, ret;
74
75	off = fdt_add_subnode(blob, 0, "framebuffer");
76	if (off < 0)
77		return -1;
78
79	ret = fdt_setprop(blob, off, "status", disabled, sizeof(disabled));
80	if (ret < 0)
81		return -1;
82
83	ret = fdt_setprop(blob, off, "compatible", compat, sizeof(compat));
84	if (ret < 0)
85		return -1;
86
87	return fdt_simplefb_configure_node(blob, off);
88}
89
90/**
91 * fdt_simplefb_enable_existing_node() - enable simple-framebuffer DT node
92 *
93 * @blob:	device-tree
94 * Return:	0 on success, non-zero otherwise
95 */
96static int fdt_simplefb_enable_existing_node(void *blob)
97{
98	int off;
99
100	off = fdt_node_offset_by_compatible(blob, -1, "simple-framebuffer");
101	if (off < 0)
102		return -1;
103
104	return fdt_simplefb_configure_node(blob, off);
105}
106
107#if IS_ENABLED(CONFIG_VIDEO)
108int fdt_simplefb_enable_and_mem_rsv(void *blob)
109{
110	int ret;
111
112	/* nothing to do when video is not active */
113	if (!video_is_active())
114		return 0;
115
116	ret = fdt_simplefb_enable_existing_node(blob);
117	if (ret)
118		return ret;
119
120	return fdt_add_fb_mem_rsv(blob);
121}
122#endif
123