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 */
43enum 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 */
66struct 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
83struct 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
93enum mtk_jpeg_hw_state {
94	MTK_JPEG_HW_IDLE = 0,
95	MTK_JPEG_HW_BUSY = 1,
96};
97
98struct 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
104enum mtk_jpegenc_hw_id {
105	MTK_JPEGENC_HW0,
106	MTK_JPEGENC_HW1,
107	MTK_JPEGENC_HW_MAX,
108};
109
110enum 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 */
122struct 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 */
132struct 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_state:		record hw state
148 * @hw_lock:		spinlock protecting the hw device resource
149 */
150struct mtk_jpegenc_comp_dev {
151	struct device *dev;
152	struct platform_device *plat_dev;
153	void __iomem *reg_base;
154	struct mtk_jpeg_dev *master_dev;
155	struct mtk_jpegenc_clk venc_clk;
156	int jpegenc_irq;
157	struct delayed_work job_timeout_work;
158	struct mtk_jpeg_hw_param hw_param;
159	enum mtk_jpeg_hw_state hw_state;
160	/* spinlock protecting the hw device resource */
161	spinlock_t hw_lock;
162};
163
164/**
165 * struct mtk_jpegdec_comp_dev - JPEG COREX abstraction
166 * @dev:			JPEG device
167 * @plat_dev:			platform device data
168 * @reg_base:			JPEG registers mapping
169 * @master_dev:			mtk_jpeg_dev device
170 * @jdec_clk:			mtk_jpegdec_clk
171 * @jpegdec_irq:		jpeg decode irq num
172 * @job_timeout_work:		decode timeout workqueue
173 * @hw_param:			jpeg decode hw parameters
174 * @hw_state:			record hw state
175 * @hw_lock:			spinlock protecting hw
176 */
177struct mtk_jpegdec_comp_dev {
178	struct device *dev;
179	struct platform_device *plat_dev;
180	void __iomem *reg_base;
181	struct mtk_jpeg_dev *master_dev;
182	struct mtk_jpegdec_clk jdec_clk;
183	int jpegdec_irq;
184	struct delayed_work job_timeout_work;
185	struct mtk_jpeg_hw_param hw_param;
186	enum mtk_jpeg_hw_state hw_state;
187	/* spinlock protecting the hw device resource */
188	spinlock_t hw_lock;
189};
190
191/**
192 * struct mtk_jpeg_dev - JPEG IP abstraction
193 * @lock:		the mutex protecting this structure
194 * @hw_lock:		spinlock protecting the hw device resource
195 * @workqueue:		decode work queue
196 * @dev:		JPEG device
197 * @v4l2_dev:		v4l2 device for mem2mem mode
198 * @m2m_dev:		v4l2 mem2mem device data
199 * @alloc_ctx:		videobuf2 memory allocator's context
200 * @vdev:		video device node for jpeg mem2mem mode
201 * @reg_base:		JPEG registers mapping
202 * @job_timeout_work:	IRQ timeout structure
203 * @variant:		driver variant to be used
204 * @reg_encbase:	jpg encode register base addr
205 * @enc_hw_dev:	jpg encode hardware device
206 * @hw_wq:		jpg wait queue
207 * @hw_rdy:		jpg hw ready flag
208 * @reg_decbase:	jpg decode register base addr
209 * @dec_hw_dev:	jpg decode hardware device
210 * @hw_index:		jpg hw index
211 */
212struct mtk_jpeg_dev {
213	struct mutex		lock;
214	spinlock_t		hw_lock;
215	struct workqueue_struct	*workqueue;
216	struct device		*dev;
217	struct v4l2_device	v4l2_dev;
218	struct v4l2_m2m_dev	*m2m_dev;
219	void			*alloc_ctx;
220	struct video_device	*vdev;
221	void __iomem		*reg_base;
222	struct delayed_work job_timeout_work;
223	const struct mtk_jpeg_variant *variant;
224
225	void __iomem *reg_encbase[MTK_JPEGENC_HW_MAX];
226	struct mtk_jpegenc_comp_dev *enc_hw_dev[MTK_JPEGENC_HW_MAX];
227	wait_queue_head_t hw_wq;
228	atomic_t hw_rdy;
229
230	void __iomem *reg_decbase[MTK_JPEGDEC_HW_MAX];
231	struct mtk_jpegdec_comp_dev *dec_hw_dev[MTK_JPEGDEC_HW_MAX];
232	atomic_t hw_index;
233};
234
235/**
236 * struct mtk_jpeg_fmt - driver's internal color format data
237 * @fourcc:	the fourcc code, 0 if not applicable
238 * @hw_format:	hardware format value
239 * @h_sample:	horizontal sample count of plane in 4 * 4 pixel image
240 * @v_sample:	vertical sample count of plane in 4 * 4 pixel image
241 * @colplanes:	number of color planes (1 for packed formats)
242 * @h_align:	horizontal alignment order (align to 2^h_align)
243 * @v_align:	vertical alignment order (align to 2^v_align)
244 * @flags:	flags describing format applicability
245 */
246struct mtk_jpeg_fmt {
247	u32	fourcc;
248	u32	hw_format;
249	int	h_sample[VIDEO_MAX_PLANES];
250	int	v_sample[VIDEO_MAX_PLANES];
251	int	colplanes;
252	int	h_align;
253	int	v_align;
254	u32	flags;
255};
256
257/**
258 * struct mtk_jpeg_q_data - parameters of one queue
259 * @fmt:	  driver-specific format of this queue
260 * @pix_mp:	  multiplanar format
261 * @enc_crop_rect:	jpeg encoder crop information
262 */
263struct mtk_jpeg_q_data {
264	struct mtk_jpeg_fmt	*fmt;
265	struct v4l2_pix_format_mplane pix_mp;
266	struct v4l2_rect enc_crop_rect;
267};
268
269/**
270 * struct mtk_jpeg_ctx - the device context data
271 * @jpeg:			JPEG IP device for this context
272 * @out_q:			source (output) queue information
273 * @cap_q:			destination queue information
274 * @fh:				V4L2 file handle
275 * @state:			state of the context
276 * @enable_exif:		enable exif mode of jpeg encoder
277 * @enc_quality:		jpeg encoder quality
278 * @restart_interval:		jpeg encoder restart interval
279 * @ctrl_hdl:			controls handler
280 * @jpeg_work:			jpeg encoder workqueue
281 * @total_frame_num:		encoded frame number
282 * @dst_done_queue:		encoded frame buffer queue
283 * @done_queue_lock:		encoded frame operation spinlock
284 * @last_done_frame_num:	the last encoded frame number
285 */
286struct mtk_jpeg_ctx {
287	struct mtk_jpeg_dev		*jpeg;
288	struct mtk_jpeg_q_data		out_q;
289	struct mtk_jpeg_q_data		cap_q;
290	struct v4l2_fh			fh;
291	enum mtk_jpeg_ctx_state		state;
292	bool enable_exif;
293	u8 enc_quality;
294	u8 restart_interval;
295	struct v4l2_ctrl_handler ctrl_hdl;
296
297	struct work_struct jpeg_work;
298	u32 total_frame_num;
299	struct list_head dst_done_queue;
300	/* spinlock protecting the encode done buffer */
301	spinlock_t done_queue_lock;
302	u32 last_done_frame_num;
303};
304
305#endif /* _MTK_JPEG_CORE_H */
306