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/interrupt.h>
13 #include <media/v4l2-ctrls.h>
14 #include <media/v4l2-device.h>
15 #include <media/v4l2-fh.h>
16 
17 #define MTK_JPEG_NAME		"mtk-jpeg"
18 
19 #define MTK_JPEG_COMP_MAX		3
20 
21 #define MTK_JPEG_FMT_FLAG_OUTPUT	BIT(0)
22 #define MTK_JPEG_FMT_FLAG_CAPTURE	BIT(1)
23 
24 #define MTK_JPEG_MIN_WIDTH	32U
25 #define MTK_JPEG_MIN_HEIGHT	32U
26 #define MTK_JPEG_MAX_WIDTH	65535U
27 #define MTK_JPEG_MAX_HEIGHT	65535U
28 
29 #define MTK_JPEG_DEFAULT_SIZEIMAGE	(1 * 1024 * 1024)
30 
31 #define MTK_JPEG_HW_TIMEOUT_MSEC 1000
32 
33 #define MTK_JPEG_MAX_EXIF_SIZE	(64 * 1024)
34 
35 /**
36  * enum mtk_jpeg_ctx_state - states of the context state machine
37  * @MTK_JPEG_INIT:		current state is initialized
38  * @MTK_JPEG_RUNNING:		current state is running
39  * @MTK_JPEG_SOURCE_CHANGE:	current state is source resolution change
40  */
41 enum mtk_jpeg_ctx_state {
42 	MTK_JPEG_INIT = 0,
43 	MTK_JPEG_RUNNING,
44 	MTK_JPEG_SOURCE_CHANGE,
45 };
46 
47 /**
48  * struct mtk_jpeg_variant - mtk jpeg driver variant
49  * @clks:			clock names
50  * @num_clks:			numbers of clock
51  * @formats:			jpeg driver's internal color format
52  * @num_formats:		number of formats
53  * @qops:			the callback of jpeg vb2_ops
54  * @irq_handler:		jpeg irq handler callback
55  * @hw_reset:			jpeg hardware reset callback
56  * @m2m_ops:			the callback of jpeg v4l2_m2m_ops
57  * @dev_name:			jpeg device name
58  * @ioctl_ops:			the callback of jpeg v4l2_ioctl_ops
59  * @out_q_default_fourcc:	output queue default fourcc
60  * @cap_q_default_fourcc:	capture queue default fourcc
61  */
62 struct mtk_jpeg_variant {
63 	struct clk_bulk_data *clks;
64 	int num_clks;
65 	struct mtk_jpeg_fmt *formats;
66 	int num_formats;
67 	const struct vb2_ops *qops;
68 	irqreturn_t (*irq_handler)(int irq, void *priv);
69 	void (*hw_reset)(void __iomem *base);
70 	const struct v4l2_m2m_ops *m2m_ops;
71 	const char *dev_name;
72 	const struct v4l2_ioctl_ops *ioctl_ops;
73 	u32 out_q_default_fourcc;
74 	u32 cap_q_default_fourcc;
75 };
76 
77 /**
78  * struct mtk_jpeg_dev - JPEG IP abstraction
79  * @lock:		the mutex protecting this structure
80  * @hw_lock:		spinlock protecting the hw device resource
81  * @workqueue:		decode work queue
82  * @dev:		JPEG device
83  * @v4l2_dev:		v4l2 device for mem2mem mode
84  * @m2m_dev:		v4l2 mem2mem device data
85  * @alloc_ctx:		videobuf2 memory allocator's context
86  * @vdev:		video device node for jpeg mem2mem mode
87  * @reg_base:		JPEG registers mapping
88  * @job_timeout_work:	IRQ timeout structure
89  * @variant:		driver variant to be used
90  */
91 struct mtk_jpeg_dev {
92 	struct mutex		lock;
93 	spinlock_t		hw_lock;
94 	struct workqueue_struct	*workqueue;
95 	struct device		*dev;
96 	struct v4l2_device	v4l2_dev;
97 	struct v4l2_m2m_dev	*m2m_dev;
98 	void			*alloc_ctx;
99 	struct video_device	*vdev;
100 	void __iomem		*reg_base;
101 	struct delayed_work job_timeout_work;
102 	const struct mtk_jpeg_variant *variant;
103 };
104 
105 /**
106  * struct mtk_jpeg_fmt - driver's internal color format data
107  * @fourcc:	the fourcc code, 0 if not applicable
108  * @hw_format:	hardware format value
109  * @h_sample:	horizontal sample count of plane in 4 * 4 pixel image
110  * @v_sample:	vertical sample count of plane in 4 * 4 pixel image
111  * @colplanes:	number of color planes (1 for packed formats)
112  * @h_align:	horizontal alignment order (align to 2^h_align)
113  * @v_align:	vertical alignment order (align to 2^v_align)
114  * @flags:	flags describing format applicability
115  */
116 struct mtk_jpeg_fmt {
117 	u32	fourcc;
118 	u32	hw_format;
119 	int	h_sample[VIDEO_MAX_PLANES];
120 	int	v_sample[VIDEO_MAX_PLANES];
121 	int	colplanes;
122 	int	h_align;
123 	int	v_align;
124 	u32	flags;
125 };
126 
127 /**
128  * struct mtk_jpeg_q_data - parameters of one queue
129  * @fmt:	  driver-specific format of this queue
130  * @pix_mp:	  multiplanar format
131  * @enc_crop_rect:	jpeg encoder crop information
132  */
133 struct mtk_jpeg_q_data {
134 	struct mtk_jpeg_fmt	*fmt;
135 	struct v4l2_pix_format_mplane pix_mp;
136 	struct v4l2_rect enc_crop_rect;
137 };
138 
139 /**
140  * struct mtk_jpeg_ctx - the device context data
141  * @jpeg:		JPEG IP device for this context
142  * @out_q:		source (output) queue information
143  * @cap_q:		destination (capture) queue queue information
144  * @fh:			V4L2 file handle
145  * @state:		state of the context
146  * @enable_exif:	enable exif mode of jpeg encoder
147  * @enc_quality:	jpeg encoder quality
148  * @restart_interval:	jpeg encoder restart interval
149  * @ctrl_hdl:		controls handler
150  */
151 struct mtk_jpeg_ctx {
152 	struct mtk_jpeg_dev		*jpeg;
153 	struct mtk_jpeg_q_data		out_q;
154 	struct mtk_jpeg_q_data		cap_q;
155 	struct v4l2_fh			fh;
156 	enum mtk_jpeg_ctx_state		state;
157 	bool enable_exif;
158 	u8 enc_quality;
159 	u8 restart_interval;
160 	struct v4l2_ctrl_handler ctrl_hdl;
161 };
162 
163 #endif /* _MTK_JPEG_CORE_H */
164