1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Support for Intel Camera Imaging ISP subsystem.
4 * Copyright (c) 2015, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 * more details.
14 */
15
16#ifndef __IA_CSS_FRAME_PUBLIC_H
17#define __IA_CSS_FRAME_PUBLIC_H
18
19/* @file
20 * This file contains structs to describe various frame-formats supported by the ISP.
21 */
22
23#include <media/videobuf2-v4l2.h>
24#include <type_support.h>
25#include "ia_css_err.h"
26#include "ia_css_types.h"
27#include "ia_css_frame_format.h"
28#include "ia_css_buffer.h"
29
30/* For RAW input, the bayer order needs to be specified separately. There
31 *  are 4 possible orders. The name is constructed by taking the first two
32 *  colors on the first line and the first two colors from the second line.
33 */
34enum ia_css_bayer_order {
35	IA_CSS_BAYER_ORDER_GRBG, /** GRGRGRGRGR .. BGBGBGBGBG */
36	IA_CSS_BAYER_ORDER_RGGB, /** RGRGRGRGRG .. GBGBGBGBGB */
37	IA_CSS_BAYER_ORDER_BGGR, /** BGBGBGBGBG .. GRGRGRGRGR */
38	IA_CSS_BAYER_ORDER_GBRG, /** GBGBGBGBGB .. RGRGRGRGRG */
39};
40
41#define IA_CSS_BAYER_ORDER_NUM (IA_CSS_BAYER_ORDER_GBRG + 1)
42
43/* Frame plane structure. This describes one plane in an image
44 *  frame buffer.
45 */
46struct ia_css_frame_plane {
47	unsigned int height; /** height of a plane in lines */
48	unsigned int width;  /** width of a line, in DMA elements, note that
49				  for RGB565 the three subpixels are stored in
50				  one element. For all other formats this is
51				  the number of subpixels per line. */
52	unsigned int stride; /** stride of a line in bytes */
53	unsigned int offset; /** offset in bytes to start of frame data.
54				  offset is wrt data field in ia_css_frame */
55};
56
57/* Binary "plane". This is used to story binary streams such as jpeg
58 *  images. This is not actually a real plane.
59 */
60struct ia_css_frame_binary_plane {
61	unsigned int		  size; /** number of bytes in the stream */
62	struct ia_css_frame_plane data; /** plane */
63};
64
65/* Container for planar YUV frames. This contains 3 planes.
66 */
67struct ia_css_frame_yuv_planes {
68	struct ia_css_frame_plane y; /** Y plane */
69	struct ia_css_frame_plane u; /** U plane */
70	struct ia_css_frame_plane v; /** V plane */
71};
72
73/* Container for semi-planar YUV frames.
74  */
75struct ia_css_frame_nv_planes {
76	struct ia_css_frame_plane y;  /** Y plane */
77	struct ia_css_frame_plane uv; /** UV plane */
78};
79
80/* Container for planar RGB frames. Each color has its own plane.
81 */
82struct ia_css_frame_rgb_planes {
83	struct ia_css_frame_plane r; /** Red plane */
84	struct ia_css_frame_plane g; /** Green plane */
85	struct ia_css_frame_plane b; /** Blue plane */
86};
87
88/* Container for 6-plane frames. These frames are used internally
89 *  in the advanced ISP only.
90 */
91struct ia_css_frame_plane6_planes {
92	struct ia_css_frame_plane r;	  /** Red plane */
93	struct ia_css_frame_plane r_at_b; /** Red at blue plane */
94	struct ia_css_frame_plane gr;	  /** Red-green plane */
95	struct ia_css_frame_plane gb;	  /** Blue-green plane */
96	struct ia_css_frame_plane b;	  /** Blue plane */
97	struct ia_css_frame_plane b_at_r; /** Blue at red plane */
98};
99
100/* Crop info struct - stores the lines to be cropped in isp */
101struct ia_css_crop_info {
102	/* the final start column and start line
103	 * sum of lines to be cropped + bayer offset
104	 */
105	unsigned int start_column;
106	unsigned int start_line;
107};
108
109/* Frame info struct. This describes the contents of an image frame buffer.
110  */
111struct ia_css_frame_info {
112	struct ia_css_resolution res; /** Frame resolution (valid data) */
113	unsigned int padded_width; /** stride of line in memory (in pixels) */
114	enum ia_css_frame_format format; /** format of the frame data */
115	unsigned int raw_bit_depth; /** number of valid bits per pixel,
116					 only valid for RAW bayer frames */
117	enum ia_css_bayer_order raw_bayer_order; /** bayer order, only valid
118						      for RAW bayer frames */
119	/* the params below are computed based on bayer_order
120	 * we can remove the raw_bayer_order if it is redundant
121	 * keeping it for now as bxt and fpn code seem to use it
122	 */
123	struct ia_css_crop_info crop_info;
124};
125
126#define IA_CSS_BINARY_DEFAULT_FRAME_INFO { \
127	.format			= IA_CSS_FRAME_FORMAT_NUM,  \
128	.raw_bayer_order	= IA_CSS_BAYER_ORDER_NUM, \
129}
130
131/**
132 *  Specifies the DVS loop delay in "frame periods"
133 */
134enum ia_css_frame_delay {
135	IA_CSS_FRAME_DELAY_0, /** Frame delay = 0 */
136	IA_CSS_FRAME_DELAY_1, /** Frame delay = 1 */
137	IA_CSS_FRAME_DELAY_2  /** Frame delay = 2 */
138};
139
140enum ia_css_frame_flash_state {
141	IA_CSS_FRAME_FLASH_STATE_NONE,
142	IA_CSS_FRAME_FLASH_STATE_PARTIAL,
143	IA_CSS_FRAME_FLASH_STATE_FULL
144};
145
146/* Frame structure. This structure describes an image buffer or frame.
147 *  This is the main structure used for all input and output images.
148 */
149struct ia_css_frame {
150	/*
151	 * The videobuf2 core will allocate buffers including room for private
152	 * data (the rest of struct ia_css_frame). The vb2_v4l2_buffer must
153	 * be the first member for this to work!
154	 * Note the atomisp code also uses ia_css_frame-s which are not used
155	 * as v4l2-buffers in some places. In this case the vb2 member will
156	 * be unused.
157	 */
158	struct vb2_v4l2_buffer vb;
159	/* List-head for linking into the activeq or buffers_waiting_for_param list */
160	struct list_head queue;
161	struct ia_css_frame_info frame_info; /** info struct describing the frame */
162	ia_css_ptr   data;	       /** pointer to start of image data */
163	unsigned int data_bytes;       /** size of image data in bytes */
164	/* LA: move this to ia_css_buffer */
165	/*
166	 * -1 if data address is static during life time of pipeline
167	 * >=0 if data address can change per pipeline/frame iteration
168	 *     index to dynamic data: ia_css_frame_in, ia_css_frame_out
169	 *                            ia_css_frame_out_vf
170	 *     index to host-sp queue id: queue_0, queue_1 etc.
171	 */
172	int dynamic_queue_id;
173	/*
174	 * if it is dynamic frame, buf_type indicates which buffer type it
175	 * should use for event generation. we have this because in vf_pp
176	 * binary, we use output port, but we expect VF_OUTPUT_DONE event
177	 */
178	enum ia_css_buffer_type buf_type;
179	enum ia_css_frame_flash_state flash_state;
180	unsigned int exp_id;
181	/** exposure id, see ia_css_event_public.h for more detail */
182	u32 isp_config_id; /** Unique ID to track which config was actually applied to a particular frame */
183	bool valid; /** First video output frame is not valid */
184	union {
185		unsigned int	_initialisation_dummy;
186		struct ia_css_frame_plane raw;
187		struct ia_css_frame_plane rgb;
188		struct ia_css_frame_rgb_planes planar_rgb;
189		struct ia_css_frame_plane yuyv;
190		struct ia_css_frame_yuv_planes yuv;
191		struct ia_css_frame_nv_planes nv;
192		struct ia_css_frame_plane6_planes plane6;
193		struct ia_css_frame_binary_plane binary;
194	} planes; /** frame planes, select the right one based on
195		       info.format */
196};
197
198#define vb_to_frame(vb2) \
199	container_of(to_vb2_v4l2_buffer(vb2), struct ia_css_frame, vb)
200
201#define DEFAULT_FRAME { \
202	.frame_info		= IA_CSS_BINARY_DEFAULT_FRAME_INFO, \
203	.dynamic_queue_id	= SH_CSS_INVALID_QUEUE_ID, \
204	.buf_type		= IA_CSS_BUFFER_TYPE_INVALID, \
205	.flash_state		= IA_CSS_FRAME_FLASH_STATE_NONE, \
206}
207
208/* @brief Allocate a CSS frame structure
209 *
210 * @param	frame		The allocated frame.
211 * @param	width		The width (in pixels) of the frame.
212 * @param	height		The height (in lines) of the frame.
213 * @param	format		The frame format.
214 * @param	stride		The padded stride, in pixels.
215 * @param	raw_bit_depth	The raw bit depth, in bits.
216 * @return			The error code.
217 *
218 * Allocate a CSS frame structure. The memory for the frame data will be
219 * allocated in the CSS address space.
220 */
221int
222ia_css_frame_allocate(struct ia_css_frame **frame,
223		      unsigned int width,
224		      unsigned int height,
225		      enum ia_css_frame_format format,
226		      unsigned int stride,
227		      unsigned int raw_bit_depth);
228
229/* @brief Initialize a CSS frame structure using a frame info structure.
230 *
231 * @param	frame	The allocated frame.
232 * @param[in]	info	The frame info structure.
233 * @return		The error code.
234 *
235 * Initialize a frame using the resolution and format from a frame info struct.
236 */
237int ia_css_frame_init_from_info(struct ia_css_frame *frame,
238				const struct ia_css_frame_info *info);
239
240/* @brief Allocate a CSS frame structure using a frame info structure.
241 *
242 * @param	frame	The allocated frame.
243 * @param[in]	info	The frame info structure.
244 * @return		The error code.
245 *
246 * Allocate a frame using the resolution and format from a frame info struct.
247 * This is a convenience function, implemented on top of
248 * ia_css_frame_allocate().
249 */
250int
251ia_css_frame_allocate_from_info(struct ia_css_frame **frame,
252				const struct ia_css_frame_info *info);
253/* @brief Free a CSS frame structure.
254 *
255 * @param[in]	frame	Pointer to the frame.
256 * @return	None
257 *
258 * Free a CSS frame structure. This will free both the frame structure
259 * and the pixel data pointer contained within the frame structure.
260 */
261void
262ia_css_frame_free(struct ia_css_frame *frame);
263
264static inline const struct ia_css_frame_info *
265ia_css_frame_get_info(const struct ia_css_frame *frame)
266{
267	return frame ? &frame->frame_info : NULL;
268}
269
270#endif /* __IA_CSS_FRAME_PUBLIC_H */
271