1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (c) 2016 MediaTek Inc.
4  * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
5  *         Rick Chang <rick.chang@mediatek.com>
6  *         Xia Jiang <xia.jiang@mediatek.com>
7  */
8 
9 #ifndef _MTK_JPEG_CORE_H
10 #define _MTK_JPEG_CORE_H
11 
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
14 #include <media/v4l2-ctrls.h>
15 #include <media/v4l2-device.h>
16 #include <media/v4l2-fh.h>
17 #include <media/videobuf2-v4l2.h>
18 
19 #include "mtk_jpeg_dec_hw.h"
20 
21 #define MTK_JPEG_NAME		"mtk-jpeg"
22 
23 #define MTK_JPEG_FMT_FLAG_OUTPUT	BIT(0)
24 #define MTK_JPEG_FMT_FLAG_CAPTURE	BIT(1)
25 
26 #define MTK_JPEG_MIN_WIDTH	32U
27 #define MTK_JPEG_MIN_HEIGHT	32U
28 #define MTK_JPEG_MAX_WIDTH	65535U
29 #define MTK_JPEG_MAX_HEIGHT	65535U
30 
31 #define MTK_JPEG_DEFAULT_SIZEIMAGE	(1 * 1024 * 1024)
32 
33 #define MTK_JPEG_HW_TIMEOUT_MSEC 1000
34 
35 #define MTK_JPEG_MAX_EXIF_SIZE	(64 * 1024)
36 
37 /**
38  * enum mtk_jpeg_ctx_state - states of the context state machine
39  * @MTK_JPEG_INIT:		current state is initialized
40  * @MTK_JPEG_RUNNING:		current state is running
41  * @MTK_JPEG_SOURCE_CHANGE:	current state is source resolution change
42  */
43 enum mtk_jpeg_ctx_state {
44 	MTK_JPEG_INIT = 0,
45 	MTK_JPEG_RUNNING,
46 	MTK_JPEG_SOURCE_CHANGE,
47 };
48 
49 /**
50  * struct mtk_jpeg_variant - mtk jpeg driver variant
51  * @clks:			clock names
52  * @num_clks:			numbers of clock
53  * @formats:			jpeg driver's internal color format
54  * @num_formats:		number of formats
55  * @qops:			the callback of jpeg vb2_ops
56  * @irq_handler:		jpeg irq handler callback
57  * @hw_reset:			jpeg hardware reset callback
58  * @m2m_ops:			the callback of jpeg v4l2_m2m_ops
59  * @dev_name:			jpeg device name
60  * @ioctl_ops:			the callback of jpeg v4l2_ioctl_ops
61  * @out_q_default_fourcc:	output queue default fourcc
62  * @cap_q_default_fourcc:	capture queue default fourcc
63  * @multi_core:		mark jpeg hw is multi_core or not
64  * @jpeg_worker:		jpeg dec or enc worker
65  */
66 struct mtk_jpeg_variant {
67 	struct clk_bulk_data *clks;
68 	int num_clks;
69 	struct mtk_jpeg_fmt *formats;
70 	int num_formats;
71 	const struct vb2_ops *qops;
72 	irqreturn_t (*irq_handler)(int irq, void *priv);
73 	void (*hw_reset)(void __iomem *base);
74 	const struct v4l2_m2m_ops *m2m_ops;
75 	const char *dev_name;
76 	const struct v4l2_ioctl_ops *ioctl_ops;
77 	u32 out_q_default_fourcc;
78 	u32 cap_q_default_fourcc;
79 	bool multi_core;
80 	void (*jpeg_worker)(struct work_struct *work);
81 };
82 
83 struct mtk_jpeg_src_buf {
84 	u32 frame_num;
85 	struct vb2_v4l2_buffer b;
86 	struct list_head list;
87 	u32 bs_size;
88 	struct mtk_jpeg_dec_param dec_param;
89 
90 	struct mtk_jpeg_ctx *curr_ctx;
91 };
92 
93 enum mtk_jpeg_hw_state {
94 	MTK_JPEG_HW_IDLE = 0,
95 	MTK_JPEG_HW_BUSY = 1,
96 };
97 
98 struct mtk_jpeg_hw_param {
99 	struct vb2_v4l2_buffer *src_buffer;
100 	struct vb2_v4l2_buffer *dst_buffer;
101 	struct mtk_jpeg_ctx *curr_ctx;
102 };
103 
104 enum mtk_jpegenc_hw_id {
105 	MTK_JPEGENC_HW0,
106 	MTK_JPEGENC_HW1,
107 	MTK_JPEGENC_HW_MAX,
108 };
109 
110 enum mtk_jpegdec_hw_id {
111 	MTK_JPEGDEC_HW0,
112 	MTK_JPEGDEC_HW1,
113 	MTK_JPEGDEC_HW2,
114 	MTK_JPEGDEC_HW_MAX,
115 };
116 
117 /**
118  * struct mtk_jpegenc_clk - Structure used to store vcodec clock information
119  * @clks:		JPEG encode clock
120  * @clk_num:		JPEG encode clock numbers
121  */
122 struct mtk_jpegenc_clk {
123 	struct clk_bulk_data *clks;
124 	int clk_num;
125 };
126 
127 /**
128  * struct mtk_jpegdec_clk - Structure used to store vcodec clock information
129  * @clks:		JPEG decode clock
130  * @clk_num:		JPEG decode clock numbers
131  */
132 struct mtk_jpegdec_clk {
133 	struct clk_bulk_data *clks;
134 	int clk_num;
135 };
136 
137 /**
138  * struct mtk_jpegenc_comp_dev - JPEG COREX abstraction
139  * @dev:		JPEG device
140  * @plat_dev:		platform device data
141  * @reg_base:		JPEG registers mapping
142  * @master_dev:		mtk_jpeg_dev device
143  * @venc_clk:		jpeg encode clock
144  * @jpegenc_irq:	jpeg encode irq num
145  * @job_timeout_work:	encode timeout workqueue
146  * @hw_param:		jpeg encode hw parameters
147  * @hw_rdy:		record hw ready
148  * @hw_state:		record hw state
149  * @hw_lock:		spinlock protecting the hw device resource
150  */
151 struct mtk_jpegenc_comp_dev {
152 	struct device *dev;
153 	struct platform_device *plat_dev;
154 	void __iomem *reg_base;
155 	struct mtk_jpeg_dev *master_dev;
156 	struct mtk_jpegenc_clk venc_clk;
157 	int jpegenc_irq;
158 	struct delayed_work job_timeout_work;
159 	struct mtk_jpeg_hw_param hw_param;
160 	enum mtk_jpeg_hw_state hw_state;
161 	/* spinlock protecting the hw device resource */
162 	spinlock_t hw_lock;
163 };
164 
165 /**
166  * struct mtk_jpegdec_comp_dev - JPEG COREX abstraction
167  * @dev:			JPEG device
168  * @plat_dev:			platform device data
169  * @reg_base:			JPEG registers mapping
170  * @master_dev:			mtk_jpeg_dev device
171  * @jdec_clk:			mtk_jpegdec_clk
172  * @jpegdec_irq:		jpeg decode irq num
173  * @job_timeout_work:		decode timeout workqueue
174  * @hw_param:			jpeg decode hw parameters
175  * @hw_state:			record hw state
176  * @hw_lock:			spinlock protecting hw
177  */
178 struct mtk_jpegdec_comp_dev {
179 	struct device *dev;
180 	struct platform_device *plat_dev;
181 	void __iomem *reg_base;
182 	struct mtk_jpeg_dev *master_dev;
183 	struct mtk_jpegdec_clk jdec_clk;
184 	int jpegdec_irq;
185 	struct delayed_work job_timeout_work;
186 	struct mtk_jpeg_hw_param hw_param;
187 	enum mtk_jpeg_hw_state hw_state;
188 	/* spinlock protecting the hw device resource */
189 	spinlock_t hw_lock;
190 };
191 
192 /**
193  * struct mtk_jpeg_dev - JPEG IP abstraction
194  * @lock:		the mutex protecting this structure
195  * @hw_lock:		spinlock protecting the hw device resource
196  * @workqueue:		decode work queue
197  * @dev:		JPEG device
198  * @v4l2_dev:		v4l2 device for mem2mem mode
199  * @m2m_dev:		v4l2 mem2mem device data
200  * @alloc_ctx:		videobuf2 memory allocator's context
201  * @vdev:		video device node for jpeg mem2mem mode
202  * @reg_base:		JPEG registers mapping
203  * @job_timeout_work:	IRQ timeout structure
204  * @variant:		driver variant to be used
205  * @reg_encbase:	jpg encode register base addr
206  * @enc_hw_dev:	jpg encode hardware device
207  * @hw_wq:		jpg wait queue
208  * @hw_rdy:		jpg hw ready flag
209  * @reg_decbase:	jpg decode register base addr
210  * @dec_hw_dev:	jpg decode hardware device
211  * @hw_index:		jpg hw index
212  */
213 struct mtk_jpeg_dev {
214 	struct mutex		lock;
215 	spinlock_t		hw_lock;
216 	struct workqueue_struct	*workqueue;
217 	struct device		*dev;
218 	struct v4l2_device	v4l2_dev;
219 	struct v4l2_m2m_dev	*m2m_dev;
220 	void			*alloc_ctx;
221 	struct video_device	*vdev;
222 	void __iomem		*reg_base;
223 	struct delayed_work job_timeout_work;
224 	const struct mtk_jpeg_variant *variant;
225 
226 	void __iomem *reg_encbase[MTK_JPEGENC_HW_MAX];
227 	struct mtk_jpegenc_comp_dev *enc_hw_dev[MTK_JPEGENC_HW_MAX];
228 	wait_queue_head_t hw_wq;
229 	atomic_t hw_rdy;
230 
231 	void __iomem *reg_decbase[MTK_JPEGDEC_HW_MAX];
232 	struct mtk_jpegdec_comp_dev *dec_hw_dev[MTK_JPEGDEC_HW_MAX];
233 	atomic_t hw_index;
234 };
235 
236 /**
237  * struct mtk_jpeg_fmt - driver's internal color format data
238  * @fourcc:	the fourcc code, 0 if not applicable
239  * @hw_format:	hardware format value
240  * @h_sample:	horizontal sample count of plane in 4 * 4 pixel image
241  * @v_sample:	vertical sample count of plane in 4 * 4 pixel image
242  * @colplanes:	number of color planes (1 for packed formats)
243  * @h_align:	horizontal alignment order (align to 2^h_align)
244  * @v_align:	vertical alignment order (align to 2^v_align)
245  * @flags:	flags describing format applicability
246  */
247 struct mtk_jpeg_fmt {
248 	u32	fourcc;
249 	u32	hw_format;
250 	int	h_sample[VIDEO_MAX_PLANES];
251 	int	v_sample[VIDEO_MAX_PLANES];
252 	int	colplanes;
253 	int	h_align;
254 	int	v_align;
255 	u32	flags;
256 };
257 
258 /**
259  * struct mtk_jpeg_q_data - parameters of one queue
260  * @fmt:	  driver-specific format of this queue
261  * @pix_mp:	  multiplanar format
262  * @enc_crop_rect:	jpeg encoder crop information
263  */
264 struct mtk_jpeg_q_data {
265 	struct mtk_jpeg_fmt	*fmt;
266 	struct v4l2_pix_format_mplane pix_mp;
267 	struct v4l2_rect enc_crop_rect;
268 };
269 
270 /**
271  * struct mtk_jpeg_ctx - the device context data
272  * @jpeg:			JPEG IP device for this context
273  * @out_q:			source (output) queue information
274  * @cap_q:			destination queue information
275  * @fh:				V4L2 file handle
276  * @state:			state of the context
277  * @enable_exif:		enable exif mode of jpeg encoder
278  * @enc_quality:		jpeg encoder quality
279  * @restart_interval:		jpeg encoder restart interval
280  * @ctrl_hdl:			controls handler
281  * @jpeg_work:			jpeg encoder workqueue
282  * @total_frame_num:		encoded frame number
283  * @dst_done_queue:		encoded frame buffer queue
284  * @done_queue_lock:		encoded frame operation spinlock
285  * @last_done_frame_num:	the last encoded frame number
286  */
287 struct mtk_jpeg_ctx {
288 	struct mtk_jpeg_dev		*jpeg;
289 	struct mtk_jpeg_q_data		out_q;
290 	struct mtk_jpeg_q_data		cap_q;
291 	struct v4l2_fh			fh;
292 	enum mtk_jpeg_ctx_state		state;
293 	bool enable_exif;
294 	u8 enc_quality;
295 	u8 restart_interval;
296 	struct v4l2_ctrl_handler ctrl_hdl;
297 
298 	struct work_struct jpeg_work;
299 	u32 total_frame_num;
300 	struct list_head dst_done_queue;
301 	/* spinlock protecting the encode done buffer */
302 	spinlock_t done_queue_lock;
303 	u32 last_done_frame_num;
304 };
305 
306 #endif /* _MTK_JPEG_CORE_H */
307