1 /* SPDX-License-Identifier: GPL-2.0-only */
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>
9 #ifndef _MTK_JPEG_CORE_H
10 #define _MTK_JPEG_CORE_H
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>
19 #include "mtk_jpeg_dec_hw.h"
21 #define MTK_JPEG_NAME "mtk-jpeg"
23 #define MTK_JPEG_FMT_FLAG_OUTPUT BIT(0)
24 #define MTK_JPEG_FMT_FLAG_CAPTURE BIT(1)
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
31 #define MTK_JPEG_DEFAULT_SIZEIMAGE (1 * 1024 * 1024)
33 #define MTK_JPEG_HW_TIMEOUT_MSEC 1000
35 #define MTK_JPEG_MAX_EXIF_SIZE (64 * 1024)
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
43 enum mtk_jpeg_ctx_state {
46 MTK_JPEG_SOURCE_CHANGE,
50 * struct mtk_jpeg_variant - mtk jpeg driver variant
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
66 struct mtk_jpeg_variant {
67 struct clk_bulk_data *clks;
69 struct mtk_jpeg_fmt *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;
76 const struct v4l2_ioctl_ops *ioctl_ops;
77 u32 out_q_default_fourcc;
78 u32 cap_q_default_fourcc;
80 void (*jpeg_worker)(struct work_struct *work);
83 struct mtk_jpeg_src_buf {
85 struct vb2_v4l2_buffer b;
86 struct list_head list;
88 struct mtk_jpeg_dec_param dec_param;
90 struct mtk_jpeg_ctx *curr_ctx;
93 enum mtk_jpeg_hw_state {
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;
104 enum mtk_jpegenc_hw_id {
110 enum mtk_jpegdec_hw_id {
118 * struct mtk_jpegenc_clk - Structure used to store vcodec clock information
119 * @clks: JPEG encode clock
120 * @clk_num: JPEG encode clock numbers
122 struct mtk_jpegenc_clk {
123 struct clk_bulk_data *clks;
128 * struct mtk_jpegdec_clk - Structure used to store vcodec clock information
129 * @clks: JPEG decode clock
130 * @clk_num: JPEG decode clock numbers
132 struct mtk_jpegdec_clk {
133 struct clk_bulk_data *clks;
138 * struct mtk_jpegenc_comp_dev - JPEG COREX abstraction
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
151 struct mtk_jpegenc_comp_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;
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 */
166 * struct mtk_jpegdec_comp_dev - JPEG COREX abstraction
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
178 struct mtk_jpegdec_comp_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;
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 */
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
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
213 struct mtk_jpeg_dev {
216 struct workqueue_struct *workqueue;
218 struct v4l2_device v4l2_dev;
219 struct v4l2_m2m_dev *m2m_dev;
221 struct video_device *vdev;
222 void __iomem *reg_base;
223 struct delayed_work job_timeout_work;
224 const struct mtk_jpeg_variant *variant;
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;
231 void __iomem *reg_decbase[MTK_JPEGDEC_HW_MAX];
232 struct mtk_jpegdec_comp_dev *dec_hw_dev[MTK_JPEGDEC_HW_MAX];
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
247 struct mtk_jpeg_fmt {
250 int h_sample[VIDEO_MAX_PLANES];
251 int v_sample[VIDEO_MAX_PLANES];
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
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;
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
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;
292 enum mtk_jpeg_ctx_state state;
296 struct v4l2_ctrl_handler ctrl_hdl;
298 struct work_struct jpeg_work;
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;
306 #endif /* _MTK_JPEG_CORE_H */