1 /*
2  * Video uclass to support displays (see also vidconsole for text)
3  *
4  * Copyright (c) 2015 Google, Inc
5  */
6 
7 #ifndef _VIDEO_H_
8 #define _VIDEO_H_
9 
10 #include <stdio_dev.h>
11 
12 struct udevice;
13 
14 /**
15  * struct video_uc_plat - uclass platform data for a video device
16  *
17  * This holds information that the uclass needs to know about each device. It
18  * is accessed using dev_get_uclass_plat(dev). See 'Theory of operation' at
19  * the top of video-uclass.c for details on how this information is set.
20  *
21  * @align: Frame-buffer alignment, indicating the memory boundary the frame
22  *	buffer should start on. If 0, 1MB is assumed
23  * @size: Frame-buffer size, in bytes
24  * @base: Base address of frame buffer, 0 if not yet known. If CONFIG_VIDEO_COPY
25  *	is enabled, this is the software copy, so writes to this will not be
26  *	visible until vidconsole_sync_copy() is called. If CONFIG_VIDEO_COPY is
27  *	disabled, this is the hardware framebuffer.
28  * @copy_base: Base address of a hardware copy of the frame buffer. If
29  *	CONFIG_VIDEO_COPY is disabled, this is not used.
30  * @copy_size: Size of copy framebuffer, used if @size is 0
31  * @hide_logo: Hide the logo (used for testing)
32  */
33 struct video_uc_plat {
34 	uint align;
35 	uint size;
36 	ulong base;
37 	ulong copy_base;
38 	ulong copy_size;
39 	bool hide_logo;
40 };
41 
42 enum video_polarity {
43 	VIDEO_ACTIVE_HIGH,	/* Pins are active high */
44 	VIDEO_ACTIVE_LOW,	/* Pins are active low */
45 };
46 
47 /*
48  * Bits per pixel selector. Each value n is such that the bits-per-pixel is
49  * 2 ^ n
50  */
51 enum video_log2_bpp {
52 	VIDEO_BPP1	= 0,
53 	VIDEO_BPP2,
54 	VIDEO_BPP4,
55 	VIDEO_BPP8,
56 	VIDEO_BPP16,
57 	VIDEO_BPP32,
58 };
59 
60 /* Convert enum video_log2_bpp to bytes and bits */
61 #define VNBYTES(bpix)	((1 << (bpix)) / 8)
62 #define VNBITS(bpix)	(1 << (bpix))
63 
64 enum video_format {
65 	VIDEO_UNKNOWN,
66 	VIDEO_RGBA8888,
67 	VIDEO_X8B8G8R8,
68 	VIDEO_X8R8G8B8,
69 	VIDEO_X2R10G10B10,
70 };
71 
72 /**
73  * struct video_priv - Device information used by the video uclass
74  *
75  * @xsize:	Number of pixel columns (e.g. 1366)
76  * @ysize:	Number of pixels rows (e.g.. 768)
77  * @rot:	Display rotation (0=none, 1=90 degrees clockwise, etc.). THis
78  *		does not affect @xsize and @ysize
79  * @bpix:	Encoded bits per pixel (enum video_log2_bpp)
80  * @format:	Pixel format (enum video_format)
81  * @vidconsole_drv_name:	Driver to use for the text console, NULL to
82  *		select automatically
83  * @font_size:	Font size in pixels (0 to use a default value)
84  * @fb:		Frame buffer
85  * @fb_size:	Frame buffer size
86  * @copy_fb:	Copy of the frame buffer to keep up to date; see struct
87  *		video_uc_plat
88  * @damage:	A bounding box of framebuffer regions updated since last sync
89  * @damage.xstart:	X start position in pixels from the left
90  * @damage.ystart:	Y start position in pixels from the top
91  * @damage.xend:	X end position in pixels from the left
92  * @damage.xend:	Y end position in pixels from the top
93  * @line_length:	Length of each frame buffer line, in bytes. This can be
94  *		set by the driver, but if not, the uclass will set it after
95  *		probing
96  * @colour_fg:	Foreground colour (pixel value)
97  * @colour_bg:	Background colour (pixel value)
98  * @flush_dcache:	true to enable flushing of the data cache after
99  *		the LCD is updated
100  * @fg_col_idx:	Foreground color code (bit 3 = bold, bit 0-2 = color)
101  * @bg_col_idx:	Background color code (bit 3 = bold, bit 0-2 = color)
102  * @last_sync:	Monotonic time of last video sync
103  * @white_on_black: Use a black background
104  */
105 struct video_priv {
106 	/* Things set up by the driver: */
107 	ushort xsize;
108 	ushort ysize;
109 	ushort rot;
110 	enum video_log2_bpp bpix;
111 	enum video_format format;
112 	const char *vidconsole_drv_name;
113 	int font_size;
114 
115 	/*
116 	 * Things that are private to the uclass: don't use these in the
117 	 * driver
118 	 */
119 	void *fb;
120 	int fb_size;
121 	void *copy_fb;
122 	struct {
123 		int xstart;
124 		int ystart;
125 		int xend;
126 		int yend;
127 	} damage;
128 	int line_length;
129 	u32 colour_fg;
130 	u32 colour_bg;
131 	bool flush_dcache;
132 	u8 fg_col_idx;
133 	u8 bg_col_idx;
134 	ulong last_sync;
135 	bool white_on_black;
136 };
137 
138 /**
139  * struct video_ops - structure for keeping video operations
140  * @video_sync: Synchronize FB with device. Some device like SPI based LCD
141  *		displays needs synchronization when data in an FB is available.
142  *		For these devices implement video_sync hook to call a sync
143  *		function. vid is pointer to video device udevice. Function
144  *		should return 0 on success video_sync and error code otherwise
145  */
146 struct video_ops {
147 	int (*video_sync)(struct udevice *vid);
148 };
149 
150 #define video_get_ops(dev)        ((struct video_ops *)(dev)->driver->ops)
151 
152 /**
153  * struct video_handoff - video information passed from SPL
154  *
155  * This is used when video is set up by SPL, to provide the details to U-Boot
156  * proper.
157  *
158  * @fb: Base address of frame buffer, 0 if not yet known
159  * @size: Frame-buffer size, in bytes
160  * @xsize:	Number of pixel columns (e.g. 1366)
161  * @ysize:	Number of pixels rows (e.g.. 768)
162  * @line_length:	Length of each frame buffer line, in bytes. This can be
163  *		set by the driver, but if not, the uclass will set it after
164  *		probing
165  * @bpix:	Encoded bits per pixel (enum video_log2_bpp)
166  * @format:	Video format (enum video_format)
167  */
168 struct video_handoff {
169 	u64 fb;
170 	u32 size;
171 	u16 xsize;
172 	u16 ysize;
173 	u32 line_length;
174 	u8 bpix;
175 	u8 format;
176 };
177 
178 /** enum colour_idx - the 16 colors supported by consoles */
179 enum colour_idx {
180 	VID_BLACK = 0,
181 	VID_RED,
182 	VID_GREEN,
183 	VID_BROWN,
184 	VID_BLUE,
185 	VID_MAGENTA,
186 	VID_CYAN,
187 	VID_LIGHT_GRAY,
188 	VID_GRAY,
189 	VID_LIGHT_RED,
190 	VID_LIGHT_GREEN,
191 	VID_YELLOW,
192 	VID_LIGHT_BLUE,
193 	VID_LIGHT_MAGENTA,
194 	VID_LIGHT_CYAN,
195 	VID_WHITE,
196 	VID_DARK_GREY,
197 
198 	VID_COLOUR_COUNT
199 };
200 
201 /**
202  * video_index_to_colour() - convert a color code to a pixel's internal
203  * representation
204  *
205  * The caller has to guarantee that the color index is less than
206  * VID_COLOR_COUNT.
207  *
208  * @priv	private data of the video device (UCLASS_VIDEO)
209  * @idx		color index (e.g. VID_YELLOW)
210  * Return:	color value
211  */
212 u32 video_index_to_colour(struct video_priv *priv, enum colour_idx idx);
213 
214 /**
215  * video_reserve() - Reserve frame-buffer memory for video devices
216  *
217  * Note: This function is for internal use.
218  *
219  * This uses the uclass plat's @size and @align members to figure out
220  * a size and position for each frame buffer as part of the pre-relocation
221  * process of determining the post-relocation memory layout.
222  *
223  * gd->video_top is set to the initial value of *@addrp and gd->video_bottom
224  * is set to the final value.
225  *
226  * @addrp:	On entry, the top of available memory. On exit, the new top,
227  *		after allocating the required memory.
228  * Return: 0
229  */
230 int video_reserve(ulong *addrp);
231 
232 /**
233  * video_clear() - Clear a device's frame buffer to background colour.
234  *
235  * @dev:	Device to clear
236  * Return: 0 on success
237  */
238 int video_clear(struct udevice *dev);
239 
240 /**
241  * video_fill() - Fill a device's frame buffer to a colour.
242  *
243  * @dev:	Device to fill
244  * @colour:	Colour to use, in the frame buffer's format
245  * Return: 0 on success
246  */
247 int video_fill(struct udevice *dev, u32 colour);
248 
249 /**
250  * video_fill_part() - Erase a region
251  *
252  * Erase a rectangle on the display within the given bounds
253  *
254  * @dev:	Device to update
255  * @xstart:	X start position in pixels from the left
256  * @ystart:	Y start position in pixels from the top
257  * @xend:	X end position in pixels from the left
258  * @yend:	Y end position  in pixels from the top
259  * @colour:	Value to write
260  * Return: 0 if OK, -ENOSYS if the display depth is not supported
261  */
262 int video_fill_part(struct udevice *dev, int xstart, int ystart, int xend,
263 		    int yend, u32 colour);
264 
265 /**
266  * video_draw_box() - Draw a box
267  *
268  * Draw a rectangle on the display within the given bounds
269  *
270  * @dev:	Device to update
271  * @x0:		X start position in pixels from the left
272  * @y0:		Y start position in pixels from the top
273  * @x1:		X end position in pixels from the left
274  * @y1:		Y end position in pixels from the top
275  * @width:	width in pixels
276  * @colour:	Value to write
277  * Return: 0 if OK, -ENOSYS if the display depth is not supported
278  */
279 int video_draw_box(struct udevice *dev, int x0, int y0, int x1, int y1,
280 		   int width, u32 colour);
281 
282 /**
283  * video_sync() - Sync a device's frame buffer with its hardware
284  *
285  * @vid:	Device to sync
286  * @force:	True to force a sync even if there was one recently (this is
287  *		very expensive on sandbox)
288  *
289  * @return: 0 on success, error code otherwise
290  *
291  * Some frame buffers are cached or have a secondary frame buffer. This
292  * function syncs the damaged parts of them up so that the current contents
293  * of the U-Boot frame buffer are displayed to the user. It clears the damage
294  * buffer.
295  */
296 int video_sync(struct udevice *vid, bool force);
297 
298 /**
299  * video_sync_all() - Sync all devices' frame buffers with their hardware
300  *
301  * This calls video_sync() on all active video devices.
302  */
303 void video_sync_all(void);
304 
305 /**
306  * video_bmp_get_info() - Get information about a bitmap image
307  *
308  * @bmp_image: Pointer to BMP image to check
309  * @widthp: Returns width in pixels
310  * @heightp: Returns height in pixels
311  * @bpixp: Returns log2 of bits per pixel
312  */
313 void video_bmp_get_info(void *bmp_image, ulong *widthp, ulong *heightp,
314 			uint *bpixp);
315 
316 /**
317  * video_bmp_display() - Display a BMP file
318  *
319  * @dev:	Device to display the bitmap on
320  * @bmp_image:	Address of bitmap image to display
321  * @x:		X position in pixels from the left
322  * @y:		Y position in pixels from the top
323  * @align:	true to adjust the coordinates to centre the image. If false
324  *		the coordinates are used as is. If true:
325  *
326  *		- if a coordinate is 0x7fff then the image will be centred in
327  *		  that direction
328  *		- if a coordinate is -ve then it will be offset to the
329  *		  left/top of the centre by that many pixels
330  *		- if a coordinate is positive it will be used unchanged.
331  * Return: 0 if OK, -ve on error
332  */
333 int video_bmp_display(struct udevice *dev, ulong bmp_image, int x, int y,
334 		      bool align);
335 
336 /**
337  * video_get_xsize() - Get the width of the display in pixels
338  *
339  * @dev:	Device to check
340  * Return: device frame buffer width in pixels
341  */
342 int video_get_xsize(struct udevice *dev);
343 
344 /**
345  * video_get_ysize() - Get the height of the display in pixels
346  *
347  * @dev:	Device to check
348  * Return: device frame buffer height in pixels
349  */
350 int video_get_ysize(struct udevice *dev);
351 
352 /**
353  * Set whether we need to flush the dcache when changing the image. This
354  * defaults to off.
355  *
356  * @param flush		non-zero to flush cache after update, 0 to skip
357  */
358 void video_set_flush_dcache(struct udevice *dev, bool flush);
359 
360 /**
361  * Set default colors and attributes
362  *
363  * @dev:	video device
364  * @invert	true to invert colours
365  */
366 void video_set_default_colors(struct udevice *dev, bool invert);
367 
368 /**
369  * video_set_white_on_black() - Change the setting for white-on-black
370  *
371  * This does nothing if the setting is already the same.
372  *
373  * @dev: video device
374  * @white_on_black: true to use white-on-black, false for black-on-white
375  */
376 void video_set_white_on_black(struct udevice *dev, bool white_on_black);
377 
378 /**
379  * video_default_font_height() - Get the default font height
380  *
381  * @dev:	video device
382  * Returns: Default font height in pixels, which depends on which console driver
383  * is in use
384  */
385 int video_default_font_height(struct udevice *dev);
386 
387 #ifdef CONFIG_VIDEO_DAMAGE
388 /**
389  * video_damage() - Notify the video subsystem about screen updates.
390  *
391  * @vid:	Device to sync
392  * @x:	        Upper left X coordinate of the damaged rectangle
393  * @y:	        Upper left Y coordinate of the damaged rectangle
394  * @width:	Width of the damaged rectangle
395  * @height:	Height of the damaged rectangle
396  *
397  * Some frame buffers are cached or have a secondary frame buffer. This
398  * function notifies the video subsystem about rectangles that were updated
399  * within the frame buffer. They may only get written to the screen on the
400  * next call to video_sync().
401  */
402 void video_damage(struct udevice *vid, int x, int y, int width, int height);
403 #else
video_damage(struct udevice * vid,int x,int y,int width,int height)404 static inline void video_damage(struct udevice *vid, int x, int y, int width,
405 				int height)
406 {
407 	return;
408 }
409 #endif /* CONFIG_VIDEO_DAMAGE */
410 
411 /**
412  * video_is_active() - Test if one video device it active
413  *
414  * Return: true if at least one video device is active, else false.
415  */
416 bool video_is_active(void);
417 
418 /**
419  * video_get_u_boot_logo() - Get a pointer to the U-Boot logo
420  *
421  * Returns: Pointer to logo
422  */
423 void *video_get_u_boot_logo(void);
424 
425 /*
426  * bmp_display() - Display BMP (bitmap) data located in memory
427  *
428  * @addr: address of the bmp data
429  * @x: Position of bitmap from the left side, in pixels
430  * @y: Position of bitmap from the top, in pixels
431  */
432 int bmp_display(ulong addr, int x, int y);
433 
434 /*
435  * bmp_info() - Show information about bmp file
436  *
437  * @addr: address of bmp file
438  * Returns: 0 if OK, else 1 if bmp image not found
439  */
440 int bmp_info(ulong addr);
441 
442 /*
443  * video_reserve_from_bloblist()- Reserve frame-buffer memory for video devices
444  * using blobs.
445  *
446  * @ho: video information passed from SPL
447  * Returns: 0 (always)
448  */
449 int video_reserve_from_bloblist(struct video_handoff *ho);
450 
451 /**
452  * video_get_fb() - Get the first framebuffer address
453  *
454  * This function does not probe video devices, so can only be used after a video
455  * device has been activated.
456  *
457  * Return: address of the framebuffer of the first video device found, or 0 if
458  * there is no device
459  */
460 ulong video_get_fb(void);
461 
462 #endif
463