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