xref: /OK3568_Linux_fs/kernel/drivers/media/platform/rockchip/ispp/stream.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2019 Fuzhou Rockchip Electronics Co., Ltd. */
3 
4 #ifndef _RKISPP_STREAM_H
5 #define _RKISPP_STREAM_H
6 
7 #include "common.h"
8 #include "params.h"
9 
10 #define RKISPP_FEC_BUF_MAX 7
11 
12 struct rkispp_stream;
13 
14 /*
15  * STREAM_II: input image data
16  * STREAM_MB: module bypass output, no scale
17  * STREAM_S0: scale0 output
18  * STREAM_S1: scale1 output
19  * STREAM_S2: scale2 output
20  * STREAM_VIR: virtual output for debug
21  */
22 enum rkispp_stream_id {
23 	STREAM_II = 0,
24 	STREAM_MB,
25 	STREAM_VIR,
26 	STREAM_S0,
27 	STREAM_S1,
28 	STREAM_S2,
29 	STREAM_MAX
30 };
31 
32 /*
33  * fourcc: pixel format
34  * cplanes: number of colour planes
35  * mplanes: number of stored memory planes
36  * wr_fmt: defines format for reg
37  * bpp: bits per pixel
38  */
39 struct capture_fmt {
40 	u32 fourcc;
41 	u8 cplanes;
42 	u8 mplanes;
43 	u8 wr_fmt;
44 	u8 bpp[VIDEO_MAX_PLANES];
45 };
46 
47 /* Different config for stream */
48 struct stream_config {
49 	const struct capture_fmt *fmts;
50 	unsigned int fmt_size;
51 	u32 frame_end_id;
52 	/* registers */
53 	struct {
54 		u32 ctrl;
55 		u32 factor;
56 		u32 cur_y_base;
57 		u32 cur_uv_base;
58 		u32 cur_vir_stride;
59 		u32 cur_y_base_shd;
60 		u32 cur_uv_base_shd;
61 	} reg;
62 };
63 
64 /* Different reg ops for stream */
65 struct streams_ops {
66 	int (*config)(struct rkispp_stream *stream);
67 	void (*update)(struct rkispp_stream *stream);
68 	void (*stop)(struct rkispp_stream *stream);
69 	int (*start)(struct rkispp_stream *stream);
70 	int (*is_stopped)(struct rkispp_stream *stream);
71 	int (*limit_check)(struct rkispp_stream *stream,
72 			   struct v4l2_pix_format_mplane *try_fmt);
73 };
74 
75 /* stream input/out flag */
76 enum stream_type {
77 	STREAM_INPUT,
78 	STREAM_OUTPUT,
79 };
80 
81 /* internal using buf */
82 
83 struct in_tnr_buf {
84 	struct rkispp_dummy_buffer iir;
85 	struct rkispp_dummy_buffer gain_kg;
86 	struct rkispp_dummy_buffer wr[RKISPP_BUF_MAX][GROUP_BUF_MAX];
87 };
88 
89 struct in_nr_buf {
90 	struct rkispp_dummy_buffer tmp_yuv;
91 	struct rkispp_dummy_buffer wr[RKISPP_FEC_BUF_MAX];
92 };
93 
94 struct tnr_module {
95 	struct in_tnr_buf buf;
96 	struct list_head list_rd;
97 	struct list_head list_wr;
98 	struct list_head list_rpt;
99 	spinlock_t buf_lock;
100 	struct rkisp_ispp_buf *cur_rd;
101 	struct rkisp_ispp_buf *nxt_rd;
102 	struct rkisp_ispp_buf *cur_wr;
103 	struct rkisp_ispp_reg *reg_buf;
104 	struct frame_debug_info dbg;
105 	u32 uv_offset;
106 	bool is_end;
107 	bool is_3to1;
108 	bool is_buf_init;
109 	bool is_trigger;
110 };
111 
112 struct nr_module {
113 	struct in_nr_buf buf;
114 	struct list_head list_rd;
115 	struct list_head list_wr;
116 	struct list_head list_rpt;
117 	spinlock_t buf_lock;
118 	struct rkisp_ispp_buf *cur_rd;
119 	struct rkispp_dummy_buffer *cur_wr;
120 	struct rkisp_ispp_reg *reg_buf;
121 	struct frame_debug_info dbg;
122 	u32 uv_offset;
123 	bool is_end;
124 	bool is_buf_init;
125 };
126 
127 struct fec_module {
128 	struct list_head list_rd;
129 	struct list_head list_wr;
130 	struct rkisp_ispp_buf *cur_rd;
131 	struct rkispp_dummy_buffer *dummy_cur_rd;
132 	struct rkisp_ispp_reg *reg_buf;
133 	struct frame_debug_info dbg;
134 	spinlock_t buf_lock;
135 	u32 uv_offset;
136 	bool is_end;
137 };
138 
139 /* struct rkispp_stream - ISPP stream video device
140  * id: stream video identify
141  * buf_queue: queued buffer list
142  * curr_buf: the buffer used for current frame
143  * next_buf: the buffer used for next frame
144  * done: wait frame end event queue
145  * vbq_lock: lock to protect buf_queue
146  * out_cap_fmt: the output of ispp
147  * out_fmt: the output of v4l2 pix format
148  * last_module: last function module
149  * streaming: stream start flag
150  * stopping: stream stop flag
151  * linked: link enable flag
152  */
153 struct rkispp_stream {
154 	enum rkispp_stream_id id;
155 	struct rkispp_device *isppdev;
156 	struct rkispp_vdev_node vnode;
157 
158 	struct list_head buf_queue;
159 	struct rkispp_buffer *curr_buf;
160 	wait_queue_head_t done;
161 	spinlock_t vbq_lock;
162 
163 	enum stream_type type;
164 	struct streams_ops *ops;
165 	struct stream_config *config;
166 	struct capture_fmt out_cap_fmt;
167 	struct v4l2_pix_format_mplane out_fmt;
168 	struct frame_debug_info dbg;
169 
170 	u8 last_module;
171 	u8 conn_id;
172 	bool streaming;
173 	bool stopping;
174 	bool linked;
175 	bool is_upd;
176 	bool is_cfg;
177 	bool is_end;
178 	bool is_reg_withstream;
179 };
180 
181 enum {
182 	MONITOR_OFF = 0,
183 	MONITOR_TNR = BIT(0),
184 	MONITOR_NR = BIT(1),
185 	MONITOR_FEC = BIT(2),
186 };
187 
188 struct module_monitor {
189 	struct rkispp_device *dev;
190 	struct work_struct work;
191 	struct completion cmpl;
192 	u16 time;
193 	u8 module;
194 	bool is_err;
195 };
196 
197 struct rkispp_monitor {
198 	struct module_monitor tnr;
199 	struct module_monitor nr;
200 	struct module_monitor fec;
201 	struct completion cmpl;
202 	spinlock_t lock;
203 	u8 monitoring_module;
204 	u8 restart_module;
205 	u8 retry;
206 	bool is_restart;
207 	bool is_en;
208 };
209 
210 
211 struct rkispp_stream_ops {
212 	int (*config_modules)(struct rkispp_device *dev);
213 	void (*destroy_buf)(struct rkispp_stream *stream);
214 	void (*fec_work_event)(struct rkispp_device *dev, void *buf_rd,
215 			       bool is_isr, bool is_quick);
216 	int (*start_isp)(struct rkispp_device *dev);
217 	void (*check_to_force_update)(struct rkispp_device *dev, u32 mis_val);
218 	void (*update_mi)(struct rkispp_stream *stream);
219 	enum hrtimer_restart (*rkispp_frame_done_early)(struct hrtimer *timer);
220 	void (*rkispp_module_work_event)(struct rkispp_device *dev,
221 					 void *buf_rd, void *buf_wr,
222 					 u32 module, bool is_isr);
223 };
224 
225 struct rkispp_vir_cpy {
226 	struct work_struct work;
227 	struct completion cmpl;
228 	struct list_head queue;
229 	struct rkispp_stream *stream;
230 };
231 
232 /* rkispp stream device */
233 struct rkispp_stream_vdev {
234 	struct rkispp_stream stream[STREAM_MAX];
235 	struct tnr_module tnr;
236 	struct nr_module nr;
237 	struct fec_module fec;
238 	struct frame_debug_info dbg;
239 	struct rkispp_monitor monitor;
240 	struct rkispp_stream_ops *stream_ops;
241 	struct rkispp_vir_cpy vir_cpy;
242 	struct rkisp_ispp_buf input[VIDEO_MAX_FRAME];
243 	struct hrtimer fec_qst;
244 	struct hrtimer frame_qst;
245 	atomic_t refcnt;
246 	u32 module_ens;
247 	u32 irq_ends;
248 	u32 wait_line;
249 	bool is_done_early;
250 };
251 
252 int rkispp_get_tnrbuf_fd(struct rkispp_device *dev, struct rkispp_buf_idxfd *idxfd);
253 int rkispp_get_nrbuf_fd(struct rkispp_device *dev, struct rkispp_buf_idxfd *idxfd);
254 void rkispp_set_trigger_mode(struct rkispp_device *dev,
255 			     struct rkispp_trigger_mode *mode);
256 void rkispp_isr(u32 mis_val, struct rkispp_device *dev);
257 void rkispp_unregister_stream_vdevs(struct rkispp_device *dev);
258 int rkispp_register_stream_vdevs(struct rkispp_device *dev);
259 void *get_pool_buf(struct rkispp_device *dev, struct rkisp_ispp_buf *dbufs);
260 void *dbuf_to_dummy(struct dma_buf *dbuf, struct rkispp_dummy_buffer *pool, int num);
261 void *get_list_buf(struct list_head *list, bool is_isp_ispp);
262 void get_stream_buf(struct rkispp_stream *stream);
263 void secure_config_mb(struct rkispp_stream *stream);
264 
265 #if IS_ENABLED(CONFIG_VIDEO_ROCKCHIP_ISPP_VERSION_V10)
266 void rkispp_stream_init_ops_v10(struct rkispp_stream_vdev *stream_vdev);
267 void rkispp_params_init_ops_v10(struct rkispp_params_vdev *params_vdev);
268 #else
rkispp_stream_init_ops_v10(struct rkispp_stream_vdev * stream_vdev)269 static inline void rkispp_stream_init_ops_v10(struct rkispp_stream_vdev *stream_vdev) {}
rkispp_params_init_ops_v10(struct rkispp_params_vdev * params_vdev)270 static inline void rkispp_params_init_ops_v10(struct rkispp_params_vdev *params_vdev) {}
271 #endif
272 
273 #if IS_ENABLED(CONFIG_VIDEO_ROCKCHIP_ISPP_VERSION_V20)
274 void rkispp_stream_init_ops_v20(struct rkispp_stream_vdev *stream_vdev);
275 void rkispp_params_init_ops_v20(struct rkispp_params_vdev *params_vdev);
276 #else
rkispp_stream_init_ops_v20(struct rkispp_stream_vdev * stream_vdev)277 static inline void rkispp_stream_init_ops_v20(struct rkispp_stream_vdev *stream_vdev) {}
rkispp_params_init_ops_v20(struct rkispp_params_vdev * params_vdev)278 static inline void rkispp_params_init_ops_v20(struct rkispp_params_vdev *params_vdev) {}
279 #endif
280 int rkispp_frame_end(struct rkispp_stream *stream, u32 state);
281 void rkispp_start_3a_run(struct rkispp_device *dev);
282 #endif
283