xref: /OK3568_Linux_fs/kernel/drivers/media/platform/rockchip/isp/capture.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Rockchip isp1 driver
3  *
4  * Copyright (C) 2017 Rockchip Electronics Co., Ltd.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  */
34 
35 #ifndef _RKISP_PATH_VIDEO_H
36 #define _RKISP_PATH_VIDEO_H
37 
38 #include <linux/interrupt.h>
39 
40 #include "common.h"
41 #include "capture_v1x.h"
42 #include "capture_v2x.h"
43 #include "capture_v3x.h"
44 #include "isp_ispp.h"
45 
46 #define SP_VDEV_NAME DRIVER_NAME	"_selfpath"
47 #define MP_VDEV_NAME DRIVER_NAME	"_mainpath"
48 #define FBC_VDEV_NAME DRIVER_NAME	"_fbcpath"
49 #define BP_VDEV_NAME DRIVER_NAME	"_bypasspath"
50 #define MPDS_VDEV_NAME DRIVER_NAME	"_mainpath_4x4sampling"
51 #define BPDS_VDEV_NAME DRIVER_NAME	"_bypasspath_4x4sampling"
52 #define LUMA_VDEV_NAME DRIVER_NAME	"_lumapath"
53 #define VIR_VDEV_NAME DRIVER_NAME	"_iqtool"
54 
55 #define DMATX0_VDEV_NAME DRIVER_NAME	"_rawwr0"
56 #define DMATX1_VDEV_NAME DRIVER_NAME	"_rawwr1"
57 #define DMATX2_VDEV_NAME DRIVER_NAME	"_rawwr2"
58 #define DMATX3_VDEV_NAME DRIVER_NAME	"_rawwr3"
59 
60 struct rkisp_stream;
61 
62 enum {
63 	ROCKIT_DVBM_END,
64 	ROCKIT_DVBM_START,
65 };
66 
67 enum {
68 	RDBK_L,
69 	RDBK_M,
70 	RDBK_S,
71 	RDBK_MAX,
72 };
73 
74 enum {
75 	RKISP_STREAM_MP,
76 	RKISP_STREAM_SP,
77 	RKISP_STREAM_DMATX0,
78 	RKISP_STREAM_DMATX1,
79 	RKISP_STREAM_DMATX2,
80 	RKISP_STREAM_DMATX3,
81 	RKISP_STREAM_FBC,
82 	RKISP_STREAM_BP,
83 	RKISP_STREAM_MPDS,
84 	RKISP_STREAM_BPDS,
85 	RKISP_STREAM_LUMA,
86 	RKISP_STREAM_VIR,
87 	RKISP_MAX_STREAM,
88 };
89 
90 /*
91  * @fourcc: pixel format
92  * @mbus_code: pixel format over bus
93  * @fmt_type: helper filed for pixel format
94  * @bpp: bits per pixel
95  * @bayer_pat: bayer patten type
96  * @cplanes: number of colour planes
97  * @mplanes: number of stored memory planes
98  * @uv_swap: if cb cr swaped, for yuv
99  * @write_format: defines how YCbCr self picture data is written to memory
100  * @input_format: defines sp input format
101  * @output_format: defines sp output format
102  */
103 struct capture_fmt {
104 	u32 fourcc;
105 	u32 mbus_code;
106 	u8 fmt_type;
107 	u8 cplanes;
108 	u8 mplanes;
109 	u8 uv_swap;
110 	u32 write_format;
111 	u32 output_format;
112 	u8 bpp[VIDEO_MAX_PLANES];
113 };
114 
115 enum rkisp_sp_inp {
116 	RKISP_SP_INP_ISP,
117 	RKISP_SP_INP_DMA_SP,
118 	RKISP_SP_INP_MAX
119 };
120 
121 enum rkisp_field {
122 	RKISP_FIELD_ODD,
123 	RKISP_FIELD_EVEN,
124 	RKISP_FIELD_INVAL,
125 };
126 
127 struct rkisp_stream_sp {
128 	int y_stride;
129 	int vir_offs;
130 	enum rkisp_sp_inp input_sel;
131 	enum rkisp_field field;
132 	enum rkisp_field field_rec;
133 };
134 
135 struct rkisp_stream_mp {
136 	bool raw_enable;
137 };
138 
139 struct rkisp_stream_dmatx {
140 	u8 pre_stop;
141 	u8 is_config;
142 };
143 
144 struct rkisp_stream_dmarx {
145 	int y_stride;
146 };
147 
148 /* Different config between selfpath and mainpath */
149 struct stream_config {
150 	const struct capture_fmt *fmts;
151 	int fmt_size;
152 	int max_rsz_width;
153 	int max_rsz_height;
154 	int min_rsz_width;
155 	int min_rsz_height;
156 	const int frame_end_id;
157 	/* registers */
158 	struct {
159 		u32 ctrl;
160 		u32 ctrl_shd;
161 		u32 scale_hy;
162 		u32 scale_hcr;
163 		u32 scale_hcb;
164 		u32 scale_vy;
165 		u32 scale_vc;
166 		u32 scale_lut;
167 		u32 scale_lut_addr;
168 		u32 scale_hy_shd;
169 		u32 scale_hcr_shd;
170 		u32 scale_hcb_shd;
171 		u32 scale_vy_shd;
172 		u32 scale_vc_shd;
173 		u32 phase_hy;
174 		u32 phase_hc;
175 		u32 phase_vy;
176 		u32 phase_vc;
177 		u32 phase_hy_shd;
178 		u32 phase_hc_shd;
179 		u32 phase_vy_shd;
180 		u32 phase_vc_shd;
181 	} rsz;
182 	struct {
183 		u32 ctrl;
184 		u32 yuvmode_mask;
185 		u32 rawmode_mask;
186 		u32 h_offset;
187 		u32 v_offset;
188 		u32 h_size;
189 		u32 v_size;
190 	} dual_crop;
191 	struct {
192 		u32 y_size_init;
193 		u32 cb_size_init;
194 		u32 cr_size_init;
195 		u32 y_base_ad_init;
196 		u32 cb_base_ad_init;
197 		u32 cr_base_ad_init;
198 		u32 y_offs_cnt_init;
199 		u32 cb_offs_cnt_init;
200 		u32 cr_offs_cnt_init;
201 		u32 y_base_ad_shd;
202 		u32 length;
203 		u32 ctrl;
204 		u32 y_pic_size;
205 	} mi;
206 	struct {
207 		u32 ctrl;
208 		u32 pic_size;
209 		u32 pic_offs;
210 	} dma;
211 };
212 
213 /* Different reg ops between selfpath and mainpath */
214 struct streams_ops {
215 	int (*config_mi)(struct rkisp_stream *stream);
216 	void (*stop_mi)(struct rkisp_stream *stream);
217 	void (*enable_mi)(struct rkisp_stream *stream);
218 	void (*disable_mi)(struct rkisp_stream *stream);
219 	void (*set_data_path)(struct rkisp_stream *stream);
220 	bool (*is_stream_stopped)(struct rkisp_stream *stream);
221 	void (*update_mi)(struct rkisp_stream *stream);
222 	int (*frame_end)(struct rkisp_stream *stream, u32 state);
223 	int (*frame_start)(struct rkisp_stream *stream, u32 mis);
224 	int (*set_wrap)(struct rkisp_stream *stream, int line);
225 };
226 
227 struct rockit_isp_ops {
228 	int (*rkisp_stream_start)(struct rkisp_stream *stream);
229 	void (*rkisp_stream_stop)(struct rkisp_stream *stream);
230 	int (*rkisp_set_fmt)(struct rkisp_stream *stream,
231 			   struct v4l2_pix_format_mplane *pixm,
232 			   bool try);
233 };
234 
235 /*
236  * struct rkisp_stream - ISP capture video device
237  *
238  * @id: stream video identify
239  * @interlaced: selfpath interlaced flag
240  * @out_isp_fmt: output isp format
241  * @out_fmt: output buffer size
242  * @dcrop: coordinates of dual-crop
243  *
244  * @vbq_lock: lock to protect buf_queue
245  * @buf_queue: queued buffer list
246  *
247  * rkisp use shadowsock registers, so it need two buffer at a time
248  * @curr_buf: the buffer used for current frame
249  * @next_buf: the buffer used for next frame
250  * @linked: stream link to isp
251  * @done: wait frame end event queue
252  * @burst: burst length for Y and CB/CR
253  * @sequence: damtx video frame sequence
254  */
255 struct rkisp_stream {
256 	unsigned int id;
257 	unsigned interlaced:1;
258 	struct rkisp_device *ispdev;
259 	struct rkisp_vdev_node vnode;
260 	struct capture_fmt out_isp_fmt;
261 	struct v4l2_pix_format_mplane out_fmt;
262 	struct v4l2_rect dcrop;
263 	struct streams_ops *ops;
264 	struct stream_config *config;
265 	spinlock_t vbq_lock;
266 	struct list_head buf_queue;
267 	struct rkisp_buffer *curr_buf;
268 	struct rkisp_buffer *next_buf;
269 	struct rkisp_dummy_buffer dummy_buf;
270 	struct mutex apilock;
271 	struct tasklet_struct buf_done_tasklet;
272 	struct list_head buf_done_list;
273 	bool streaming;
274 	bool stopping;
275 	bool frame_end;
276 	bool linked;
277 	bool start_stream;
278 	bool is_mf_upd;
279 	bool is_flip;
280 	bool is_pause;
281 	bool is_crop_upd;
282 	bool is_using_resmem;
283 	bool frame_early;
284 	wait_queue_head_t done;
285 	unsigned int burst;
286 	atomic_t sequence;
287 	struct frame_debug_info dbg;
288 	int conn_id;
289 	u32 memory;
290 	u32 skip_frame;
291 	union {
292 		struct rkisp_stream_sp sp;
293 		struct rkisp_stream_mp mp;
294 		struct rkisp_stream_dmarx dmarx;
295 		struct rkisp_stream_dmatx dmatx;
296 	} u;
297 };
298 
299 struct rkisp_vir_cpy {
300 	struct work_struct work;
301 	struct completion cmpl;
302 	struct list_head queue;
303 	struct rkisp_stream *stream;
304 };
305 
306 struct rkisp_capture_device {
307 	struct rkisp_device *ispdev;
308 	struct rkisp_stream stream[RKISP_MAX_STREAM];
309 	struct rkisp_buffer *rdbk_buf[RDBK_MAX];
310 	struct rkisp_vir_cpy vir_cpy;
311 	struct tasklet_struct rd_tasklet;
312 	atomic_t refcnt;
313 	u32 wait_line;
314 	u32 wrap_width;
315 	u32 wrap_line;
316 	bool is_done_early;
317 	bool is_mirror;
318 
319 	struct work_struct fast_work;
320 };
321 
322 extern struct stream_config rkisp_mp_stream_config;
323 extern struct stream_config rkisp_sp_stream_config;
324 extern struct rockit_isp_ops rockit_isp_ops;
325 
326 void rkisp_stream_buf_done_early(struct rkisp_device *dev);
327 void rkisp_stream_buf_done(struct rkisp_stream *stream,
328 			   struct rkisp_buffer *buf);
329 void rkisp_unregister_stream_vdev(struct rkisp_stream *stream);
330 int rkisp_register_stream_vdev(struct rkisp_stream *stream);
331 void rkisp_unregister_stream_vdevs(struct rkisp_device *dev);
332 int rkisp_register_stream_vdevs(struct rkisp_device *dev);
333 void rkisp_mi_isr(u32 mis_val, struct rkisp_device *dev);
334 void rkisp_set_stream_def_fmt(struct rkisp_device *dev, u32 id,
335 			      u32 width, u32 height, u32 pixelformat);
336 int rkisp_stream_frame_start(struct rkisp_device *dev, u32 isp_mis);
337 int rkisp_fcc_xysubs(u32 fcc, u32 *xsubs, u32 *ysubs);
338 int rkisp_mbus_code_xysubs(u32 code, u32 *xsubs, u32 *ysubs);
339 int rkisp_fh_open(struct file *filp);
340 int rkisp_fop_release(struct file *file);
341 
342 int rkisp_get_tb_stream_info(struct rkisp_stream *stream,
343 			     struct rkisp_tb_stream_info *info);
344 int rkisp_free_tb_stream_buf(struct rkisp_stream *stream);
345 #endif /* _RKISP_PATH_VIDEO_H */
346