xref: /OK3568_Linux_fs/kernel/drivers/media/platform/sti/delta/delta.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) STMicroelectronics SA 2015
4*4882a593Smuzhiyun  * Author: Hugues Fruchet <hugues.fruchet@st.com> for STMicroelectronics.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #ifndef DELTA_H
8*4882a593Smuzhiyun #define DELTA_H
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/rpmsg.h>
11*4882a593Smuzhiyun #include <media/v4l2-device.h>
12*4882a593Smuzhiyun #include <media/v4l2-mem2mem.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "delta-cfg.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /*
17*4882a593Smuzhiyun  * enum delta_state - state of decoding instance
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *@DELTA_STATE_WF_FORMAT:
20*4882a593Smuzhiyun  *	Wait for compressed format to be set by V4L2 client in order
21*4882a593Smuzhiyun  *	to know what is the relevant decoder to open.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  *@DELTA_STATE_WF_STREAMINFO:
24*4882a593Smuzhiyun  *	Wait for stream information to be available (bitstream
25*4882a593Smuzhiyun  *	header parsing is done).
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  *@DELTA_STATE_READY:
28*4882a593Smuzhiyun  *	Decoding instance is ready to decode compressed access unit.
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  *@DELTA_STATE_WF_EOS:
31*4882a593Smuzhiyun  *	Decoding instance is waiting for EOS (End Of Stream) completion.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  *@DELTA_STATE_EOS:
34*4882a593Smuzhiyun  *	EOS (End Of Stream) is completed (signaled to user). Decoding instance
35*4882a593Smuzhiyun  *	should then be closed.
36*4882a593Smuzhiyun  */
37*4882a593Smuzhiyun enum delta_state {
38*4882a593Smuzhiyun 	DELTA_STATE_WF_FORMAT,
39*4882a593Smuzhiyun 	DELTA_STATE_WF_STREAMINFO,
40*4882a593Smuzhiyun 	DELTA_STATE_READY,
41*4882a593Smuzhiyun 	DELTA_STATE_WF_EOS,
42*4882a593Smuzhiyun 	DELTA_STATE_EOS
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun  * struct delta_streaminfo - information about stream to decode
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * @flags:		validity of fields (crop, pixelaspect, other)
49*4882a593Smuzhiyun  * @width:		width of video stream
50*4882a593Smuzhiyun  * @height:		height ""
51*4882a593Smuzhiyun  * @streamformat:	fourcc compressed format of video (MJPEG, MPEG2, ...)
52*4882a593Smuzhiyun  * @dpb:		number of frames needed to decode a single frame
53*4882a593Smuzhiyun  *			(h264 dpb, up to 16)
54*4882a593Smuzhiyun  * @crop:		cropping window inside decoded frame (1920x1080@0,0
55*4882a593Smuzhiyun  *			inside 1920x1088 frame for ex.)
56*4882a593Smuzhiyun  * @pixelaspect:	pixel aspect ratio of video (4/3, 5/4)
57*4882a593Smuzhiyun  * @field:		interlaced or not
58*4882a593Smuzhiyun  * @profile:		profile string
59*4882a593Smuzhiyun  * @level:		level string
60*4882a593Smuzhiyun  * @other:		other string information from codec
61*4882a593Smuzhiyun  * @colorspace:		colorspace identifier
62*4882a593Smuzhiyun  * @xfer_func:		transfer function identifier
63*4882a593Smuzhiyun  * @ycbcr_enc:		Y'CbCr encoding identifier
64*4882a593Smuzhiyun  * @quantization:	quantization identifier
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun struct delta_streaminfo {
67*4882a593Smuzhiyun 	u32 flags;
68*4882a593Smuzhiyun 	u32 streamformat;
69*4882a593Smuzhiyun 	u32 width;
70*4882a593Smuzhiyun 	u32 height;
71*4882a593Smuzhiyun 	u32 dpb;
72*4882a593Smuzhiyun 	struct v4l2_rect crop;
73*4882a593Smuzhiyun 	struct v4l2_fract pixelaspect;
74*4882a593Smuzhiyun 	enum v4l2_field field;
75*4882a593Smuzhiyun 	u8 profile[32];
76*4882a593Smuzhiyun 	u8 level[32];
77*4882a593Smuzhiyun 	u8 other[32];
78*4882a593Smuzhiyun 	enum v4l2_colorspace colorspace;
79*4882a593Smuzhiyun 	enum v4l2_xfer_func xfer_func;
80*4882a593Smuzhiyun 	enum v4l2_ycbcr_encoding ycbcr_enc;
81*4882a593Smuzhiyun 	enum v4l2_quantization quantization;
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun #define DELTA_STREAMINFO_FLAG_CROP		0x0001
85*4882a593Smuzhiyun #define DELTA_STREAMINFO_FLAG_PIXELASPECT	0x0002
86*4882a593Smuzhiyun #define DELTA_STREAMINFO_FLAG_OTHER		0x0004
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun /*
89*4882a593Smuzhiyun  * struct delta_au - access unit structure.
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * @vbuf:	video buffer information for V4L2
92*4882a593Smuzhiyun  * @list:	V4L2 m2m list that the frame belongs to
93*4882a593Smuzhiyun  * @prepared:	if set vaddr/paddr are resolved
94*4882a593Smuzhiyun  * @vaddr:	virtual address (kernel can read/write)
95*4882a593Smuzhiyun  * @paddr:	physical address (for hardware)
96*4882a593Smuzhiyun  * @flags:	access unit type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
97*4882a593Smuzhiyun  * @dts:	decoding timestamp of this access unit
98*4882a593Smuzhiyun  */
99*4882a593Smuzhiyun struct delta_au {
100*4882a593Smuzhiyun 	struct vb2_v4l2_buffer vbuf;	/* keep first */
101*4882a593Smuzhiyun 	struct list_head list;	/* keep second */
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	bool prepared;
104*4882a593Smuzhiyun 	u32 size;
105*4882a593Smuzhiyun 	void *vaddr;
106*4882a593Smuzhiyun 	dma_addr_t paddr;
107*4882a593Smuzhiyun 	u32 flags;
108*4882a593Smuzhiyun 	u64 dts;
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun  * struct delta_frameinfo - information about decoded frame
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * @flags:		validity of fields (crop, pixelaspect)
115*4882a593Smuzhiyun  * @pixelformat:	fourcc code for uncompressed video format
116*4882a593Smuzhiyun  * @width:		width of frame
117*4882a593Smuzhiyun  * @height:		height of frame
118*4882a593Smuzhiyun  * @aligned_width:	width of frame (with encoder or decoder alignment
119*4882a593Smuzhiyun  *			constraint)
120*4882a593Smuzhiyun  * @aligned_height:	height of frame (with encoder or decoder alignment
121*4882a593Smuzhiyun  *			constraint)
122*4882a593Smuzhiyun  * @size:		maximum size in bytes required for data
123*4882a593Smuzhiyun  * @crop:		cropping window inside frame (1920x1080@0,0
124*4882a593Smuzhiyun  *			inside 1920x1088 frame for ex.)
125*4882a593Smuzhiyun  * @pixelaspect:	pixel aspect ratio of video (4/3, 5/4)
126*4882a593Smuzhiyun  * @field:		interlaced mode
127*4882a593Smuzhiyun  * @colorspace:		colorspace identifier
128*4882a593Smuzhiyun  * @xfer_func:		transfer function identifier
129*4882a593Smuzhiyun  * @ycbcr_enc:		Y'CbCr encoding identifier
130*4882a593Smuzhiyun  * @quantization:	quantization identifier
131*4882a593Smuzhiyun  */
132*4882a593Smuzhiyun struct delta_frameinfo {
133*4882a593Smuzhiyun 	u32 flags;
134*4882a593Smuzhiyun 	u32 pixelformat;
135*4882a593Smuzhiyun 	u32 width;
136*4882a593Smuzhiyun 	u32 height;
137*4882a593Smuzhiyun 	u32 aligned_width;
138*4882a593Smuzhiyun 	u32 aligned_height;
139*4882a593Smuzhiyun 	u32 size;
140*4882a593Smuzhiyun 	struct v4l2_rect crop;
141*4882a593Smuzhiyun 	struct v4l2_fract pixelaspect;
142*4882a593Smuzhiyun 	enum v4l2_field field;
143*4882a593Smuzhiyun 	enum v4l2_colorspace colorspace;
144*4882a593Smuzhiyun 	enum v4l2_xfer_func xfer_func;
145*4882a593Smuzhiyun 	enum v4l2_ycbcr_encoding ycbcr_enc;
146*4882a593Smuzhiyun 	enum v4l2_quantization quantization;
147*4882a593Smuzhiyun };
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun #define DELTA_FRAMEINFO_FLAG_CROP		0x0001
150*4882a593Smuzhiyun #define DELTA_FRAMEINFO_FLAG_PIXELASPECT	0x0002
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun /*
153*4882a593Smuzhiyun  * struct delta_frame - frame structure.
154*4882a593Smuzhiyun  *
155*4882a593Smuzhiyun  * @vbuf:	video buffer information for V4L2
156*4882a593Smuzhiyun  * @list:	V4L2 m2m list that the frame belongs to
157*4882a593Smuzhiyun  * @info:	frame information (width, height, format, alignment...)
158*4882a593Smuzhiyun  * @prepared:	if set pix/vaddr/paddr are resolved
159*4882a593Smuzhiyun  * @index:	frame index, aligned on V4L2 wow
160*4882a593Smuzhiyun  * @vaddr:	virtual address (kernel can read/write)
161*4882a593Smuzhiyun  * @paddr:	physical address (for hardware)
162*4882a593Smuzhiyun  * @state:	frame state for frame lifecycle tracking
163*4882a593Smuzhiyun  *		(DELTA_FRAME_FREE/DEC/OUT/REC/...)
164*4882a593Smuzhiyun  * @flags:	frame type (V4L2_BUF_FLAG_KEYFRAME/PFRAME/BFRAME)
165*4882a593Smuzhiyun  * @dts:	decoding timestamp of this frame
166*4882a593Smuzhiyun  * @field:	field order for interlaced frame
167*4882a593Smuzhiyun  */
168*4882a593Smuzhiyun struct delta_frame {
169*4882a593Smuzhiyun 	struct vb2_v4l2_buffer vbuf;	/* keep first */
170*4882a593Smuzhiyun 	struct list_head list;	/* keep second */
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	struct delta_frameinfo info;
173*4882a593Smuzhiyun 	bool prepared;
174*4882a593Smuzhiyun 	u32 index;
175*4882a593Smuzhiyun 	void *vaddr;
176*4882a593Smuzhiyun 	dma_addr_t paddr;
177*4882a593Smuzhiyun 	u32 state;
178*4882a593Smuzhiyun 	u32 flags;
179*4882a593Smuzhiyun 	u64 dts;
180*4882a593Smuzhiyun 	enum v4l2_field field;
181*4882a593Smuzhiyun };
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /* frame state for frame lifecycle tracking */
184*4882a593Smuzhiyun #define DELTA_FRAME_FREE	0x00 /* is free and can be used for decoding */
185*4882a593Smuzhiyun #define DELTA_FRAME_REF		0x01 /* is a reference frame */
186*4882a593Smuzhiyun #define DELTA_FRAME_BSY		0x02 /* is owned by decoder and busy */
187*4882a593Smuzhiyun #define DELTA_FRAME_DEC		0x04 /* contains decoded content */
188*4882a593Smuzhiyun #define DELTA_FRAME_OUT		0x08 /* has been given to user */
189*4882a593Smuzhiyun #define DELTA_FRAME_RDY		0x10 /* is ready but still held by decoder */
190*4882a593Smuzhiyun #define DELTA_FRAME_M2M		0x20 /* is owned by mem2mem framework */
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /*
193*4882a593Smuzhiyun  * struct delta_dts - decoding timestamp.
194*4882a593Smuzhiyun  *
195*4882a593Smuzhiyun  * @list:	list to chain timestamps
196*4882a593Smuzhiyun  * @val:	timestamp in microseconds
197*4882a593Smuzhiyun  */
198*4882a593Smuzhiyun struct delta_dts {
199*4882a593Smuzhiyun 	struct list_head list;
200*4882a593Smuzhiyun 	u64 val;
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun struct delta_buf {
204*4882a593Smuzhiyun 	u32 size;
205*4882a593Smuzhiyun 	void *vaddr;
206*4882a593Smuzhiyun 	dma_addr_t paddr;
207*4882a593Smuzhiyun 	const char *name;
208*4882a593Smuzhiyun 	unsigned long attrs;
209*4882a593Smuzhiyun };
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun struct delta_ipc_ctx {
212*4882a593Smuzhiyun 	int cb_err;
213*4882a593Smuzhiyun 	u32 copro_hdl;
214*4882a593Smuzhiyun 	struct completion done;
215*4882a593Smuzhiyun 	struct delta_buf ipc_buf_struct;
216*4882a593Smuzhiyun 	struct delta_buf *ipc_buf;
217*4882a593Smuzhiyun };
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun struct delta_ipc_param {
220*4882a593Smuzhiyun 	u32 size;
221*4882a593Smuzhiyun 	void *data;
222*4882a593Smuzhiyun };
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun struct delta_ctx;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun  * struct delta_dec - decoder structure.
228*4882a593Smuzhiyun  *
229*4882a593Smuzhiyun  * @name:		name of this decoder
230*4882a593Smuzhiyun  * @streamformat:	input stream format that this decoder support
231*4882a593Smuzhiyun  * @pixelformat:	pixel format of decoded frame that this decoder support
232*4882a593Smuzhiyun  * @max_width:		(optional) maximum width that can decode this decoder
233*4882a593Smuzhiyun  *			if not set, maximum width is DELTA_MAX_WIDTH
234*4882a593Smuzhiyun  * @max_height:		(optional) maximum height that can decode this decoder
235*4882a593Smuzhiyun  *			if not set, maximum height is DELTA_MAX_HEIGHT
236*4882a593Smuzhiyun  * @pm:			(optional) if set, decoder will manage power on its own
237*4882a593Smuzhiyun  * @open:		open this decoder
238*4882a593Smuzhiyun  * @close:		close this decoder
239*4882a593Smuzhiyun  * @setup_frame:	setup frame to be used by decoder, see below
240*4882a593Smuzhiyun  * @get_streaminfo:	get stream related infos, see below
241*4882a593Smuzhiyun  * @get_frameinfo:	get decoded frame related infos, see below
242*4882a593Smuzhiyun  * @set_frameinfo:	(optional) set decoded frame related infos, see below
243*4882a593Smuzhiyun  * @setup_frame:	setup frame to be used by decoder, see below
244*4882a593Smuzhiyun  * @decode:		decode a single access unit, see below
245*4882a593Smuzhiyun  * @get_frame:		get the next decoded frame available, see below
246*4882a593Smuzhiyun  * @recycle:		recycle the given frame, see below
247*4882a593Smuzhiyun  * @flush:		(optional) flush decoder, see below
248*4882a593Smuzhiyun  * @drain:		(optional) drain decoder, see below
249*4882a593Smuzhiyun  */
250*4882a593Smuzhiyun struct delta_dec {
251*4882a593Smuzhiyun 	const char *name;
252*4882a593Smuzhiyun 	u32 streamformat;
253*4882a593Smuzhiyun 	u32 pixelformat;
254*4882a593Smuzhiyun 	u32 max_width;
255*4882a593Smuzhiyun 	u32 max_height;
256*4882a593Smuzhiyun 	bool pm;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	/*
259*4882a593Smuzhiyun 	 * decoder ops
260*4882a593Smuzhiyun 	 */
261*4882a593Smuzhiyun 	int (*open)(struct delta_ctx *ctx);
262*4882a593Smuzhiyun 	int (*close)(struct delta_ctx *ctx);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	/*
265*4882a593Smuzhiyun 	 * setup_frame() - setup frame to be used by decoder
266*4882a593Smuzhiyun 	 * @ctx:	(in) instance
267*4882a593Smuzhiyun 	 * @frame:	(in) frame to use
268*4882a593Smuzhiyun 	 *  @frame.index	(in) identifier of frame
269*4882a593Smuzhiyun 	 *  @frame.vaddr	(in) virtual address (kernel can read/write)
270*4882a593Smuzhiyun 	 *  @frame.paddr	(in) physical address (for hardware)
271*4882a593Smuzhiyun 	 *
272*4882a593Smuzhiyun 	 * Frame is to be allocated by caller, then given
273*4882a593Smuzhiyun 	 * to decoder through this call.
274*4882a593Smuzhiyun 	 * Several frames must be given to decoder (dpb),
275*4882a593Smuzhiyun 	 * each frame is identified using its index.
276*4882a593Smuzhiyun 	 */
277*4882a593Smuzhiyun 	int (*setup_frame)(struct delta_ctx *ctx, struct delta_frame *frame);
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	/*
280*4882a593Smuzhiyun 	 * get_streaminfo() - get stream related infos
281*4882a593Smuzhiyun 	 * @ctx:	(in) instance
282*4882a593Smuzhiyun 	 * @streaminfo:	(out) width, height, dpb,...
283*4882a593Smuzhiyun 	 *
284*4882a593Smuzhiyun 	 * Precondition: stream header must have been successfully
285*4882a593Smuzhiyun 	 * parsed to have this call successful & @streaminfo valid.
286*4882a593Smuzhiyun 	 * Header parsing must be done using decode(), giving
287*4882a593Smuzhiyun 	 * explicitly header access unit or first access unit of bitstream.
288*4882a593Smuzhiyun 	 * If no valid header is found, get_streaminfo will return -ENODATA,
289*4882a593Smuzhiyun 	 * in this case the next bitstream access unit must be decoded till
290*4882a593Smuzhiyun 	 * get_streaminfo becomes successful.
291*4882a593Smuzhiyun 	 */
292*4882a593Smuzhiyun 	int (*get_streaminfo)(struct delta_ctx *ctx,
293*4882a593Smuzhiyun 			      struct delta_streaminfo *streaminfo);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	/*
296*4882a593Smuzhiyun 	 * get_frameinfo() - get decoded frame related infos
297*4882a593Smuzhiyun 	 * @ctx:	(in) instance
298*4882a593Smuzhiyun 	 * @frameinfo:	(out) width, height, alignment, crop, ...
299*4882a593Smuzhiyun 	 *
300*4882a593Smuzhiyun 	 * Precondition: get_streaminfo() must be successful
301*4882a593Smuzhiyun 	 */
302*4882a593Smuzhiyun 	int (*get_frameinfo)(struct delta_ctx *ctx,
303*4882a593Smuzhiyun 			     struct delta_frameinfo *frameinfo);
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	/*
306*4882a593Smuzhiyun 	 * set_frameinfo() - set decoded frame related infos
307*4882a593Smuzhiyun 	 * @ctx:	(in) instance
308*4882a593Smuzhiyun 	 * @frameinfo:	(out) width, height, alignment, crop, ...
309*4882a593Smuzhiyun 	 *
310*4882a593Smuzhiyun 	 * Optional.
311*4882a593Smuzhiyun 	 * Typically used to negotiate with decoder the output
312*4882a593Smuzhiyun 	 * frame if decoder can do post-processing.
313*4882a593Smuzhiyun 	 */
314*4882a593Smuzhiyun 	int (*set_frameinfo)(struct delta_ctx *ctx,
315*4882a593Smuzhiyun 			     struct delta_frameinfo *frameinfo);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	/*
318*4882a593Smuzhiyun 	 * decode() - decode a single access unit
319*4882a593Smuzhiyun 	 * @ctx:	(in) instance
320*4882a593Smuzhiyun 	 * @au:		(in/out) access unit
321*4882a593Smuzhiyun 	 *  @au.size	(in) size of au to decode
322*4882a593Smuzhiyun 	 *  @au.vaddr	(in) virtual address (kernel can read/write)
323*4882a593Smuzhiyun 	 *  @au.paddr	(in) physical address (for hardware)
324*4882a593Smuzhiyun 	 *  @au.flags	(out) au type (V4L2_BUF_FLAG_KEYFRAME/
325*4882a593Smuzhiyun 	 *			PFRAME/BFRAME)
326*4882a593Smuzhiyun 	 *
327*4882a593Smuzhiyun 	 * Decode the access unit given. Decode is synchronous;
328*4882a593Smuzhiyun 	 * access unit memory is no more needed after this call.
329*4882a593Smuzhiyun 	 * After this call, none, one or several frames could
330*4882a593Smuzhiyun 	 * have been decoded, which can be retrieved using
331*4882a593Smuzhiyun 	 * get_frame().
332*4882a593Smuzhiyun 	 */
333*4882a593Smuzhiyun 	int (*decode)(struct delta_ctx *ctx, struct delta_au *au);
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun 	/*
336*4882a593Smuzhiyun 	 * get_frame() - get the next decoded frame available
337*4882a593Smuzhiyun 	 * @ctx:	(in) instance
338*4882a593Smuzhiyun 	 * @frame:	(out) frame with decoded data:
339*4882a593Smuzhiyun 	 *  @frame.index	(out) identifier of frame
340*4882a593Smuzhiyun 	 *  @frame.field	(out) field order for interlaced frame
341*4882a593Smuzhiyun 	 *  @frame.state	(out) frame state for frame lifecycle tracking
342*4882a593Smuzhiyun 	 *  @frame.flags	(out) frame type (V4L2_BUF_FLAG_KEYFRAME/
343*4882a593Smuzhiyun 	 *			PFRAME/BFRAME)
344*4882a593Smuzhiyun 	 *
345*4882a593Smuzhiyun 	 * Get the next available decoded frame.
346*4882a593Smuzhiyun 	 * If no frame is available, -ENODATA is returned.
347*4882a593Smuzhiyun 	 * If a frame is available, frame structure is filled with
348*4882a593Smuzhiyun 	 * relevant data, frame.index identifying this exact frame.
349*4882a593Smuzhiyun 	 * When this frame is no more needed by upper layers,
350*4882a593Smuzhiyun 	 * recycle() must be called giving this frame identifier.
351*4882a593Smuzhiyun 	 */
352*4882a593Smuzhiyun 	int (*get_frame)(struct delta_ctx *ctx, struct delta_frame **frame);
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/*
355*4882a593Smuzhiyun 	 * recycle() - recycle the given frame
356*4882a593Smuzhiyun 	 * @ctx:	(in) instance
357*4882a593Smuzhiyun 	 * @frame:	(in) frame to recycle:
358*4882a593Smuzhiyun 	 *  @frame.index	(in) identifier of frame
359*4882a593Smuzhiyun 	 *
360*4882a593Smuzhiyun 	 * recycle() is to be called by user when the decoded frame
361*4882a593Smuzhiyun 	 * is no more needed (composition/display done).
362*4882a593Smuzhiyun 	 * This frame will then be reused by decoder to proceed
363*4882a593Smuzhiyun 	 * with next frame decoding.
364*4882a593Smuzhiyun 	 * If not enough frames have been provided through setup_frame(),
365*4882a593Smuzhiyun 	 * or recycle() is not called fast enough, the decoder can run out
366*4882a593Smuzhiyun 	 * of available frames to proceed with decoding (starvation).
367*4882a593Smuzhiyun 	 * This case is guarded by wq_recycle wait queue which ensures that
368*4882a593Smuzhiyun 	 * decoder is called only if at least one frame is available.
369*4882a593Smuzhiyun 	 */
370*4882a593Smuzhiyun 	int (*recycle)(struct delta_ctx *ctx, struct delta_frame *frame);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	/*
373*4882a593Smuzhiyun 	 * flush() - flush decoder
374*4882a593Smuzhiyun 	 * @ctx:	(in) instance
375*4882a593Smuzhiyun 	 *
376*4882a593Smuzhiyun 	 * Optional.
377*4882a593Smuzhiyun 	 * Reset decoder context and discard all internal buffers.
378*4882a593Smuzhiyun 	 * This allows implementation of seek, which leads to discontinuity
379*4882a593Smuzhiyun 	 * of input bitstream that decoder must know to restart its internal
380*4882a593Smuzhiyun 	 * decoding logic.
381*4882a593Smuzhiyun 	 */
382*4882a593Smuzhiyun 	int (*flush)(struct delta_ctx *ctx);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 	/*
385*4882a593Smuzhiyun 	 * drain() - drain decoder
386*4882a593Smuzhiyun 	 * @ctx:	(in) instance
387*4882a593Smuzhiyun 	 *
388*4882a593Smuzhiyun 	 * Optional.
389*4882a593Smuzhiyun 	 * Mark decoder pending frames (decoded but not yet output) as ready
390*4882a593Smuzhiyun 	 * so that they can be output to client at EOS (End Of Stream).
391*4882a593Smuzhiyun 	 * get_frame() is to be called in a loop right after drain() to
392*4882a593Smuzhiyun 	 * get all those pending frames.
393*4882a593Smuzhiyun 	 */
394*4882a593Smuzhiyun 	int (*drain)(struct delta_ctx *ctx);
395*4882a593Smuzhiyun };
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun struct delta_dev;
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun /*
400*4882a593Smuzhiyun  * struct delta_ctx - instance structure.
401*4882a593Smuzhiyun  *
402*4882a593Smuzhiyun  * @flags:		validity of fields (streaminfo)
403*4882a593Smuzhiyun  * @fh:			V4L2 file handle
404*4882a593Smuzhiyun  * @dev:		device context
405*4882a593Smuzhiyun  * @dec:		selected decoder context for this instance
406*4882a593Smuzhiyun  * @ipc_ctx:		context of IPC communication with firmware
407*4882a593Smuzhiyun  * @state:		instance state
408*4882a593Smuzhiyun  * @frame_num:		frame number
409*4882a593Smuzhiyun  * @au_num:		access unit number
410*4882a593Smuzhiyun  * @max_au_size:	max size of an access unit
411*4882a593Smuzhiyun  * @streaminfo:		stream information (width, height, dpb, interlacing...)
412*4882a593Smuzhiyun  * @frameinfo:		frame information (width, height, format, alignment...)
413*4882a593Smuzhiyun  * @nb_of_frames:	number of frames available for decoding
414*4882a593Smuzhiyun  * @frames:		array of decoding frames to keep track of frame
415*4882a593Smuzhiyun  *			state and manage frame recycling
416*4882a593Smuzhiyun  * @decoded_frames:	nb of decoded frames from opening
417*4882a593Smuzhiyun  * @output_frames:	nb of output frames from opening
418*4882a593Smuzhiyun  * @dropped_frames:	nb of frames dropped (ie access unit not parsed
419*4882a593Smuzhiyun  *			or frame decoded but not output)
420*4882a593Smuzhiyun  * @stream_errors:	nb of stream errors (corrupted, not supported, ...)
421*4882a593Smuzhiyun  * @decode_errors:	nb of decode errors (firmware error)
422*4882a593Smuzhiyun  * @sys_errors:		nb of system errors (memory, ipc, ...)
423*4882a593Smuzhiyun  * @dts:		FIFO of decoding timestamp.
424*4882a593Smuzhiyun  *			output frames are timestamped with incoming access
425*4882a593Smuzhiyun  *			unit timestamps using this fifo.
426*4882a593Smuzhiyun  * @name:		string naming this instance (debug purpose)
427*4882a593Smuzhiyun  * @run_work:		decoding work
428*4882a593Smuzhiyun  * @lock:		lock for decoding work serialization
429*4882a593Smuzhiyun  * @aborting:		true if current job aborted
430*4882a593Smuzhiyun  * @priv:		private decoder context for this instance, allocated
431*4882a593Smuzhiyun  *			by decoder @open time.
432*4882a593Smuzhiyun  */
433*4882a593Smuzhiyun struct delta_ctx {
434*4882a593Smuzhiyun 	u32 flags;
435*4882a593Smuzhiyun 	struct v4l2_fh fh;
436*4882a593Smuzhiyun 	struct delta_dev *dev;
437*4882a593Smuzhiyun 	const struct delta_dec *dec;
438*4882a593Smuzhiyun 	struct delta_ipc_ctx ipc_ctx;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	enum delta_state state;
441*4882a593Smuzhiyun 	u32 frame_num;
442*4882a593Smuzhiyun 	u32 au_num;
443*4882a593Smuzhiyun 	size_t max_au_size;
444*4882a593Smuzhiyun 	struct delta_streaminfo streaminfo;
445*4882a593Smuzhiyun 	struct delta_frameinfo frameinfo;
446*4882a593Smuzhiyun 	u32 nb_of_frames;
447*4882a593Smuzhiyun 	struct delta_frame *frames[DELTA_MAX_FRAMES];
448*4882a593Smuzhiyun 	u32 decoded_frames;
449*4882a593Smuzhiyun 	u32 output_frames;
450*4882a593Smuzhiyun 	u32 dropped_frames;
451*4882a593Smuzhiyun 	u32 stream_errors;
452*4882a593Smuzhiyun 	u32 decode_errors;
453*4882a593Smuzhiyun 	u32 sys_errors;
454*4882a593Smuzhiyun 	struct list_head dts;
455*4882a593Smuzhiyun 	char name[100];
456*4882a593Smuzhiyun 	struct work_struct run_work;
457*4882a593Smuzhiyun 	struct mutex lock;
458*4882a593Smuzhiyun 	bool aborting;
459*4882a593Smuzhiyun 	void *priv;
460*4882a593Smuzhiyun };
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun #define DELTA_FLAG_STREAMINFO 0x0001
463*4882a593Smuzhiyun #define DELTA_FLAG_FRAMEINFO 0x0002
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun #define DELTA_MAX_FORMATS  DELTA_MAX_DECODERS
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun /*
468*4882a593Smuzhiyun  * struct delta_dev - device struct, 1 per probe (so single one for
469*4882a593Smuzhiyun  * all platform life)
470*4882a593Smuzhiyun  *
471*4882a593Smuzhiyun  * @v4l2_dev:		v4l2 device
472*4882a593Smuzhiyun  * @vdev:		v4l2 video device
473*4882a593Smuzhiyun  * @pdev:		platform device
474*4882a593Smuzhiyun  * @dev:		device
475*4882a593Smuzhiyun  * @m2m_dev:		memory-to-memory V4L2 device
476*4882a593Smuzhiyun  * @lock:		device lock, for crit section & V4L2 ops serialization.
477*4882a593Smuzhiyun  * @clk_delta:		delta main clock
478*4882a593Smuzhiyun  * @clk_st231:		st231 coprocessor main clock
479*4882a593Smuzhiyun  * @clk_flash_promip:	flash promip clock
480*4882a593Smuzhiyun  * @decoders:		list of registered decoders
481*4882a593Smuzhiyun  * @nb_of_decoders:	nb of registered decoders
482*4882a593Smuzhiyun  * @pixelformats:	supported uncompressed video formats
483*4882a593Smuzhiyun  * @nb_of_pixelformats:	number of supported umcompressed video formats
484*4882a593Smuzhiyun  * @streamformats:	supported compressed video formats
485*4882a593Smuzhiyun  * @nb_of_streamformats:number of supported compressed video formats
486*4882a593Smuzhiyun  * @instance_id:	rolling counter identifying an instance (debug purpose)
487*4882a593Smuzhiyun  * @work_queue:		decoding job work queue
488*4882a593Smuzhiyun  * @rpmsg_driver:	rpmsg IPC driver
489*4882a593Smuzhiyun  * @rpmsg_device:	rpmsg IPC device
490*4882a593Smuzhiyun  */
491*4882a593Smuzhiyun struct delta_dev {
492*4882a593Smuzhiyun 	struct v4l2_device v4l2_dev;
493*4882a593Smuzhiyun 	struct video_device *vdev;
494*4882a593Smuzhiyun 	struct platform_device *pdev;
495*4882a593Smuzhiyun 	struct device *dev;
496*4882a593Smuzhiyun 	struct v4l2_m2m_dev *m2m_dev;
497*4882a593Smuzhiyun 	struct mutex lock;
498*4882a593Smuzhiyun 	struct clk *clk_delta;
499*4882a593Smuzhiyun 	struct clk *clk_st231;
500*4882a593Smuzhiyun 	struct clk *clk_flash_promip;
501*4882a593Smuzhiyun 	const struct delta_dec *decoders[DELTA_MAX_DECODERS];
502*4882a593Smuzhiyun 	u32 nb_of_decoders;
503*4882a593Smuzhiyun 	u32 pixelformats[DELTA_MAX_FORMATS];
504*4882a593Smuzhiyun 	u32 nb_of_pixelformats;
505*4882a593Smuzhiyun 	u32 streamformats[DELTA_MAX_FORMATS];
506*4882a593Smuzhiyun 	u32 nb_of_streamformats;
507*4882a593Smuzhiyun 	u8 instance_id;
508*4882a593Smuzhiyun 	struct workqueue_struct *work_queue;
509*4882a593Smuzhiyun 	struct rpmsg_driver rpmsg_driver;
510*4882a593Smuzhiyun 	struct rpmsg_device *rpmsg_device;
511*4882a593Smuzhiyun };
512*4882a593Smuzhiyun 
frame_type_str(u32 flags)513*4882a593Smuzhiyun static inline char *frame_type_str(u32 flags)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	if (flags & V4L2_BUF_FLAG_KEYFRAME)
516*4882a593Smuzhiyun 		return "I";
517*4882a593Smuzhiyun 	if (flags & V4L2_BUF_FLAG_PFRAME)
518*4882a593Smuzhiyun 		return "P";
519*4882a593Smuzhiyun 	if (flags & V4L2_BUF_FLAG_BFRAME)
520*4882a593Smuzhiyun 		return "B";
521*4882a593Smuzhiyun 	if (flags & V4L2_BUF_FLAG_LAST)
522*4882a593Smuzhiyun 		return "EOS";
523*4882a593Smuzhiyun 	return "?";
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun 
frame_field_str(enum v4l2_field field)526*4882a593Smuzhiyun static inline char *frame_field_str(enum v4l2_field field)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun 	if (field == V4L2_FIELD_NONE)
529*4882a593Smuzhiyun 		return "-";
530*4882a593Smuzhiyun 	if (field == V4L2_FIELD_TOP)
531*4882a593Smuzhiyun 		return "T";
532*4882a593Smuzhiyun 	if (field == V4L2_FIELD_BOTTOM)
533*4882a593Smuzhiyun 		return "B";
534*4882a593Smuzhiyun 	if (field == V4L2_FIELD_INTERLACED)
535*4882a593Smuzhiyun 		return "I";
536*4882a593Smuzhiyun 	if (field == V4L2_FIELD_INTERLACED_TB)
537*4882a593Smuzhiyun 		return "TB";
538*4882a593Smuzhiyun 	if (field == V4L2_FIELD_INTERLACED_BT)
539*4882a593Smuzhiyun 		return "BT";
540*4882a593Smuzhiyun 	return "?";
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
frame_state_str(u32 state,char * str,unsigned int len)543*4882a593Smuzhiyun static inline char *frame_state_str(u32 state, char *str, unsigned int len)
544*4882a593Smuzhiyun {
545*4882a593Smuzhiyun 	snprintf(str, len, "%s %s %s %s %s %s",
546*4882a593Smuzhiyun 		 (state & DELTA_FRAME_REF)  ? "ref" : "   ",
547*4882a593Smuzhiyun 		 (state & DELTA_FRAME_BSY)  ? "bsy" : "   ",
548*4882a593Smuzhiyun 		 (state & DELTA_FRAME_DEC)  ? "dec" : "   ",
549*4882a593Smuzhiyun 		 (state & DELTA_FRAME_OUT)  ? "out" : "   ",
550*4882a593Smuzhiyun 		 (state & DELTA_FRAME_M2M)  ? "m2m" : "   ",
551*4882a593Smuzhiyun 		 (state & DELTA_FRAME_RDY)  ? "rdy" : "   ");
552*4882a593Smuzhiyun 	return str;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun int delta_get_frameinfo_default(struct delta_ctx *ctx,
556*4882a593Smuzhiyun 				struct delta_frameinfo *frameinfo);
557*4882a593Smuzhiyun int delta_recycle_default(struct delta_ctx *pctx,
558*4882a593Smuzhiyun 			  struct delta_frame *frame);
559*4882a593Smuzhiyun 
560*4882a593Smuzhiyun int delta_get_free_frame(struct delta_ctx *ctx,
561*4882a593Smuzhiyun 			 struct delta_frame **pframe);
562*4882a593Smuzhiyun 
563*4882a593Smuzhiyun int delta_get_sync(struct delta_ctx *ctx);
564*4882a593Smuzhiyun void delta_put_autosuspend(struct delta_ctx *ctx);
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun #endif /* DELTA_H */
567