1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* linux/drivers/media/platform/s5p-jpeg/jpeg-core.h
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5  *		http://www.samsung.com
6  *
7  * Author: Andrzej Pietrasiewicz <andrzejtp2010@gmail.com>
8  */
9 
10 #ifndef JPEG_CORE_H_
11 #define JPEG_CORE_H_
12 
13 #include <linux/interrupt.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-fh.h>
16 #include <media/v4l2-ctrls.h>
17 
18 #define S5P_JPEG_M2M_NAME		"s5p-jpeg"
19 
20 #define JPEG_MAX_CLOCKS			4
21 
22 /* JPEG compression quality setting */
23 #define S5P_JPEG_COMPR_QUAL_BEST	0
24 #define S5P_JPEG_COMPR_QUAL_WORST	3
25 
26 /* JPEG RGB to YCbCr conversion matrix coefficients */
27 #define S5P_JPEG_COEF11			0x4d
28 #define S5P_JPEG_COEF12			0x97
29 #define S5P_JPEG_COEF13			0x1e
30 #define S5P_JPEG_COEF21			0x2c
31 #define S5P_JPEG_COEF22			0x57
32 #define S5P_JPEG_COEF23			0x83
33 #define S5P_JPEG_COEF31			0x83
34 #define S5P_JPEG_COEF32			0x6e
35 #define S5P_JPEG_COEF33			0x13
36 
37 #define EXYNOS3250_IRQ_TIMEOUT		0x10000000
38 
39 /* a selection of JPEG markers */
40 #define JPEG_MARKER_TEM				0x01
41 #define JPEG_MARKER_SOF0				0xc0
42 #define JPEG_MARKER_DHT				0xc4
43 #define JPEG_MARKER_RST				0xd0
44 #define JPEG_MARKER_SOI				0xd8
45 #define JPEG_MARKER_EOI				0xd9
46 #define	JPEG_MARKER_SOS				0xda
47 #define JPEG_MARKER_DQT				0xdb
48 #define JPEG_MARKER_DHP				0xde
49 
50 /* Flags that indicate a format can be used for capture/output */
51 #define SJPEG_FMT_FLAG_ENC_CAPTURE	(1 << 0)
52 #define SJPEG_FMT_FLAG_ENC_OUTPUT	(1 << 1)
53 #define SJPEG_FMT_FLAG_DEC_CAPTURE	(1 << 2)
54 #define SJPEG_FMT_FLAG_DEC_OUTPUT	(1 << 3)
55 #define SJPEG_FMT_FLAG_S5P		(1 << 4)
56 #define SJPEG_FMT_FLAG_EXYNOS3250	(1 << 5)
57 #define SJPEG_FMT_FLAG_EXYNOS4		(1 << 6)
58 #define SJPEG_FMT_RGB			(1 << 7)
59 #define SJPEG_FMT_NON_RGB		(1 << 8)
60 
61 #define S5P_JPEG_ENCODE		0
62 #define S5P_JPEG_DECODE		1
63 #define S5P_JPEG_DISABLE	-1
64 
65 #define FMT_TYPE_OUTPUT		0
66 #define FMT_TYPE_CAPTURE	1
67 
68 #define SJPEG_SUBSAMPLING_444	0x11
69 #define SJPEG_SUBSAMPLING_422	0x21
70 #define SJPEG_SUBSAMPLING_420	0x22
71 
72 #define S5P_JPEG_MAX_MARKER	4
73 
74 /* Version numbers */
75 enum sjpeg_version {
76 	SJPEG_S5P,
77 	SJPEG_EXYNOS3250,
78 	SJPEG_EXYNOS4,
79 	SJPEG_EXYNOS5420,
80 	SJPEG_EXYNOS5433,
81 };
82 
83 enum exynos4_jpeg_result {
84 	OK_ENC_OR_DEC,
85 	ERR_PROT,
86 	ERR_DEC_INVALID_FORMAT,
87 	ERR_MULTI_SCAN,
88 	ERR_FRAME,
89 	ERR_UNKNOWN,
90 };
91 
92 enum  exynos4_jpeg_img_quality_level {
93 	QUALITY_LEVEL_1 = 0,	/* high */
94 	QUALITY_LEVEL_2,
95 	QUALITY_LEVEL_3,
96 	QUALITY_LEVEL_4,	/* low */
97 };
98 
99 enum s5p_jpeg_ctx_state {
100 	JPEGCTX_RUNNING = 0,
101 	JPEGCTX_RESOLUTION_CHANGE,
102 };
103 
104 /**
105  * struct s5p_jpeg - JPEG IP abstraction
106  * @lock:		the mutex protecting this structure
107  * @slock:		spinlock protecting the device contexts
108  * @v4l2_dev:		v4l2 device for mem2mem mode
109  * @vfd_encoder:	video device node for encoder mem2mem mode
110  * @vfd_decoder:	video device node for decoder mem2mem mode
111  * @m2m_dev:		v4l2 mem2mem device data
112  * @regs:		JPEG IP registers mapping
113  * @irq:		JPEG IP irq
114  * @irq_ret:		JPEG IP irq result value
115  * @clocks:		JPEG IP clock(s)
116  * @dev:		JPEG IP struct device
117  * @variant:		driver variant to be used
118  * @irq_status:		interrupt flags set during single encode/decode
119  *			operation
120  */
121 struct s5p_jpeg {
122 	struct mutex		lock;
123 	spinlock_t		slock;
124 
125 	struct v4l2_device	v4l2_dev;
126 	struct video_device	*vfd_encoder;
127 	struct video_device	*vfd_decoder;
128 	struct v4l2_m2m_dev	*m2m_dev;
129 
130 	void __iomem		*regs;
131 	unsigned int		irq;
132 	enum exynos4_jpeg_result irq_ret;
133 	struct clk		*clocks[JPEG_MAX_CLOCKS];
134 	struct device		*dev;
135 	struct s5p_jpeg_variant *variant;
136 	u32			irq_status;
137 };
138 
139 struct s5p_jpeg_variant {
140 	unsigned int		version;
141 	unsigned int		fmt_ver_flag;
142 	unsigned int		hw3250_compat:1;
143 	unsigned int		htbl_reinit:1;
144 	unsigned int		hw_ex4_compat:1;
145 	struct v4l2_m2m_ops	*m2m_ops;
146 	irqreturn_t		(*jpeg_irq)(int irq, void *priv);
147 	const char		*clk_names[JPEG_MAX_CLOCKS];
148 	int			num_clocks;
149 };
150 
151 /**
152  * struct s5p_jpeg_fmt - driver's internal color format data
153  * @fourcc:	the fourcc code, 0 if not applicable
154  * @depth:	number of bits per pixel
155  * @colplanes:	number of color planes (1 for packed formats)
156  * @memplanes:	number of memory planes (1 for packed formats)
157  * @h_align:	horizontal alignment order (align to 2^h_align)
158  * @v_align:	vertical alignment order (align to 2^v_align)
159  * @subsampling:subsampling of a raw format or a JPEG
160  * @flags:	flags describing format applicability
161  */
162 struct s5p_jpeg_fmt {
163 	u32	fourcc;
164 	int	depth;
165 	int	colplanes;
166 	int	memplanes;
167 	int	h_align;
168 	int	v_align;
169 	int	subsampling;
170 	u32	flags;
171 };
172 
173 /**
174  * struct s5p_jpeg_marker - collection of markers from jpeg header
175  * @marker:	markers' positions relative to the buffer beginning
176  * @len:	markers' payload lengths (without length field)
177  * @n:		number of markers in collection
178  */
179 struct s5p_jpeg_marker {
180 	u32	marker[S5P_JPEG_MAX_MARKER];
181 	u32	len[S5P_JPEG_MAX_MARKER];
182 	u32	n;
183 };
184 
185 /**
186  * struct s5p_jpeg_q_data - parameters of one queue
187  * @fmt:	driver-specific format of this queue
188  * @w:		image width
189  * @h:		image height
190  * @sos:	JPEG_MARKER_SOS's position relative to the buffer beginning
191  * @dht:	JPEG_MARKER_DHT' positions relative to the buffer beginning
192  * @dqt:	JPEG_MARKER_DQT' positions relative to the buffer beginning
193  * @sof:	JPEG_MARKER_SOF0's position relative to the buffer beginning
194  * @sof_len:	JPEG_MARKER_SOF0's payload length (without length field itself)
195  * @size:	image buffer size in bytes
196  */
197 struct s5p_jpeg_q_data {
198 	struct s5p_jpeg_fmt	*fmt;
199 	u32			w;
200 	u32			h;
201 	u32			sos;
202 	struct s5p_jpeg_marker	dht;
203 	struct s5p_jpeg_marker	dqt;
204 	u32			sof;
205 	u32			sof_len;
206 	u32			size;
207 };
208 
209 /**
210  * struct s5p_jpeg_ctx - the device context data
211  * @jpeg:		JPEG IP device for this context
212  * @mode:		compression (encode) operation or decompression (decode)
213  * @compr_quality:	destination image quality in compression (encode) mode
214  * @restart_interval:	JPEG restart interval for JPEG encoding
215  * @subsampling:	subsampling of a raw format or a JPEG
216  * @out_q:		source (output) queue information
217  * @cap_q:		destination (capture) queue queue information
218  * @scale_factor:	scale factor for JPEG decoding
219  * @crop_rect:		a rectangle representing crop area of the output buffer
220  * @fh:			V4L2 file handle
221  * @hdr_parsed:		set if header has been parsed during decompression
222  * @crop_altered:	set if crop rectangle has been altered by the user space
223  * @ctrl_handler:	controls handler
224  * @state:		state of the context
225  */
226 struct s5p_jpeg_ctx {
227 	struct s5p_jpeg		*jpeg;
228 	unsigned int		mode;
229 	unsigned short		compr_quality;
230 	unsigned short		restart_interval;
231 	unsigned short		subsampling;
232 	struct s5p_jpeg_q_data	out_q;
233 	struct s5p_jpeg_q_data	cap_q;
234 	unsigned int		scale_factor;
235 	struct v4l2_rect	crop_rect;
236 	struct v4l2_fh		fh;
237 	bool			hdr_parsed;
238 	bool			crop_altered;
239 	struct v4l2_ctrl_handler ctrl_handler;
240 	enum s5p_jpeg_ctx_state	state;
241 };
242 
243 /**
244  * struct s5p_jpeg_buffer - description of memory containing input JPEG data
245  * @size:	buffer size
246  * @curr:	current position in the buffer
247  * @data:	pointer to the data
248  */
249 struct s5p_jpeg_buffer {
250 	unsigned long size;
251 	unsigned long curr;
252 	unsigned long data;
253 };
254 
255 /**
256  * struct s5p_jpeg_addr - JPEG converter physical address set for DMA
257  * @y:   luminance plane physical address
258  * @cb:  Cb plane physical address
259  * @cr:  Cr plane physical address
260  */
261 struct s5p_jpeg_addr {
262 	u32     y;
263 	u32     cb;
264 	u32     cr;
265 };
266 
267 #endif /* JPEG_CORE_H */
268