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