1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Support for Medifield PNW Camera Imaging ISP subsystem.
4  *
5  * Copyright (c) 2010 Intel Corporation. All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License version
9  * 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  *
17  */
18 #ifndef __ATOMISP_SUBDEV_H__
19 #define __ATOMISP_SUBDEV_H__
20 
21 #include <media/v4l2-ctrls.h>
22 #include <media/v4l2-device.h>
23 #include <media/v4l2-subdev.h>
24 #include <media/videobuf2-v4l2.h>
25 #include "atomisp_common.h"
26 #include "atomisp_compat.h"
27 #include "atomisp_v4l2.h"
28 
29 #include "ia_css.h"
30 
31 /* EXP_ID's ranger is 1 ~ 250 */
32 #define ATOMISP_MAX_EXP_ID     (250)
33 
34 #define ATOMISP_SUBDEV_PAD_SINK			0
35 #define ATOMISP_SUBDEV_PAD_SOURCE		1
36 #define ATOMISP_SUBDEV_PADS_NUM			2
37 
38 struct atomisp_in_fmt_conv {
39 	u32     code;
40 	u8 bpp; /* bits per pixel */
41 	u8 depth; /* uncompressed */
42 	enum atomisp_input_format atomisp_in_fmt;
43 	enum ia_css_bayer_order bayer_order;
44 };
45 
46 struct atomisp_sub_device;
47 
48 struct atomisp_video_pipe {
49 	struct video_device vdev;
50 	enum v4l2_buf_type type;
51 	struct media_pad pad;
52 	struct media_pipeline pipe;
53 	struct vb2_queue vb_queue;
54 	/* Lock for vb_queue, when also taking isp->mutex this must be taken first! */
55 	struct mutex vb_queue_mutex;
56 	/* List of video-buffers handed over to the CSS  */
57 	struct list_head buffers_in_css;
58 	/* List of video-buffers handed over to the driver, but not yet to the CSS */
59 	struct list_head activeq;
60 	/*
61 	 * the buffers waiting for per-frame parameters, this is only valid
62 	 * in per-frame setting mode.
63 	 */
64 	struct list_head buffers_waiting_for_param;
65 	/* the link list to store per_frame parameters */
66 	struct list_head per_frame_params;
67 
68 	/* Filled through atomisp_get_css_frame_info() on queue setup */
69 	struct ia_css_frame_info frame_info;
70 
71 	/* Set from streamoff to disallow queuing further buffers in CSS */
72 	bool stopping;
73 
74 	/*
75 	 * irq_lock is used to protect video buffer state change operations and
76 	 * also to make activeq and capq operations atomic.
77 	 */
78 	spinlock_t irq_lock;
79 	unsigned int users;
80 
81 	struct atomisp_device *isp;
82 	struct v4l2_pix_format pix;
83 	u32 sh_fmt;
84 
85 	struct atomisp_sub_device *asd;
86 
87 	/*
88 	 * This frame_config_id is got from CSS when dequueues buffers from CSS,
89 	 * it is used to indicate which parameter it has applied.
90 	 */
91 	unsigned int frame_config_id[VIDEO_MAX_FRAME];
92 	/*
93 	 * This config id is set when camera HAL enqueues buffer, it has a
94 	 * non-zero value to indicate which parameter it needs to applu
95 	 */
96 	unsigned int frame_request_config_id[VIDEO_MAX_FRAME];
97 	struct atomisp_css_params_with_list *frame_params[VIDEO_MAX_FRAME];
98 };
99 
100 #define vq_to_pipe(queue) \
101 	container_of(queue, struct atomisp_video_pipe, vb_queue)
102 
103 #define vb_to_pipe(vb) vq_to_pipe((vb)->vb2_queue)
104 
105 struct atomisp_pad_format {
106 	struct v4l2_mbus_framefmt fmt;
107 	struct v4l2_rect crop;
108 	struct v4l2_rect compose;
109 };
110 
111 /*
112  * This structure is used to cache the CSS parameters, it aligns to
113  * struct ia_css_isp_config but without un-supported and deprecated parts.
114  */
115 struct atomisp_css_params {
116 	struct ia_css_wb_config   wb_config;
117 	struct ia_css_cc_config   cc_config;
118 	struct ia_css_tnr_config  tnr_config;
119 	struct ia_css_ecd_config  ecd_config;
120 	struct ia_css_ynr_config  ynr_config;
121 	struct ia_css_fc_config   fc_config;
122 	struct ia_css_formats_config formats_config;
123 	struct ia_css_cnr_config  cnr_config;
124 	struct ia_css_macc_config macc_config;
125 	struct ia_css_ctc_config  ctc_config;
126 	struct ia_css_aa_config   aa_config;
127 	struct ia_css_aa_config   baa_config;
128 	struct ia_css_ce_config   ce_config;
129 	struct ia_css_ob_config   ob_config;
130 	struct ia_css_dp_config   dp_config;
131 	struct ia_css_de_config   de_config;
132 	struct ia_css_gc_config   gc_config;
133 	struct ia_css_nr_config   nr_config;
134 	struct ia_css_ee_config   ee_config;
135 	struct ia_css_anr_config  anr_config;
136 	struct ia_css_3a_config   s3a_config;
137 	struct ia_css_xnr_config  xnr_config;
138 	struct ia_css_dz_config   dz_config;
139 	struct ia_css_cc_config yuv2rgb_cc_config;
140 	struct ia_css_cc_config rgb2yuv_cc_config;
141 	struct ia_css_macc_table  macc_table;
142 	struct ia_css_gamma_table gamma_table;
143 	struct ia_css_ctc_table   ctc_table;
144 
145 	struct ia_css_xnr_table   xnr_table;
146 	struct ia_css_rgb_gamma_table r_gamma_table;
147 	struct ia_css_rgb_gamma_table g_gamma_table;
148 	struct ia_css_rgb_gamma_table b_gamma_table;
149 
150 	struct ia_css_vector      motion_vector;
151 	struct ia_css_anr_thres   anr_thres;
152 
153 	struct ia_css_dvs_6axis_config *dvs_6axis;
154 	struct ia_css_dvs2_coefficients *dvs2_coeff;
155 	struct ia_css_shading_table *shading_table;
156 	struct ia_css_morph_table   *morph_table;
157 
158 	/*
159 	 * Used to store the user pointer address of the frame. driver needs to
160 	 * translate to ia_css_frame * and then set to CSS.
161 	 */
162 	void		*output_frame;
163 	u32	isp_config_id;
164 
165 	/* Indicates which parameters need to be updated. */
166 	struct atomisp_parameters update_flag;
167 };
168 
169 struct atomisp_subdev_params {
170 	int yuv_ds_en;
171 	unsigned int color_effect;
172 	bool gdc_cac_en;
173 	bool macc_en;
174 	bool bad_pixel_en;
175 	bool video_dis_en;
176 	bool sc_en;
177 	bool fpn_en;
178 	bool xnr_en;
179 	bool low_light;
180 	int false_color;
181 	unsigned int histogram_elenum;
182 
183 	/* Current grid info */
184 	struct ia_css_grid_info curr_grid_info;
185 	enum ia_css_pipe_id s3a_enabled_pipe;
186 
187 	int s3a_output_bytes;
188 
189 	bool dis_proj_data_valid;
190 
191 	struct ia_css_dz_config   dz_config;  /** Digital Zoom */
192 	struct ia_css_capture_config   capture_config;
193 
194 	struct ia_css_isp_config config;
195 
196 	/* current configurations */
197 	struct atomisp_css_params css_param;
198 
199 	/*
200 	 * Intermediate buffers used to communicate data between
201 	 * CSS and user space.
202 	 */
203 	struct ia_css_3a_statistics *s3a_user_stat;
204 
205 	void *metadata_user[ATOMISP_METADATA_TYPE_NUM];
206 	u32 metadata_width_size;
207 
208 	struct ia_css_dvs2_statistics *dvs_stat;
209 	struct ia_css_dvs_6axis_config *dvs_6axis;
210 	u32 exp_id;
211 	int  dvs_hor_coef_bytes;
212 	int  dvs_ver_coef_bytes;
213 	int  dvs_ver_proj_bytes;
214 	int  dvs_hor_proj_bytes;
215 
216 	/* Flag to check if driver needs to update params to css */
217 	bool css_update_params_needed;
218 };
219 
220 struct atomisp_css_params_with_list {
221 	/* parameters for CSS */
222 	struct atomisp_css_params params;
223 	struct list_head list;
224 };
225 
226 struct atomisp_sub_device {
227 	struct v4l2_subdev subdev;
228 	struct media_pad pads[ATOMISP_SUBDEV_PADS_NUM];
229 	struct atomisp_pad_format fmt[ATOMISP_SUBDEV_PADS_NUM];
230 	/* Padding for currently set sink-pad fmt */
231 	u32 sink_pad_padding_w;
232 	u32 sink_pad_padding_h;
233 
234 	unsigned int output;
235 	struct atomisp_video_pipe video_out;
236 	struct atomisp_device *isp;
237 	struct v4l2_ctrl_handler ctrl_handler;
238 	struct v4l2_ctrl *run_mode;
239 	struct v4l2_ctrl *vfpp;
240 	struct v4l2_ctrl *continuous_raw_buffer_size;
241 	struct v4l2_ctrl *continuous_viewfinder;
242 	struct v4l2_ctrl *enable_raw_buffer_lock;
243 
244 	/* ISP2401 */
245 	struct v4l2_ctrl *ion_dev_fd;
246 
247 	struct v4l2_ctrl *disable_dz;
248 
249 	struct atomisp_subdev_params params;
250 
251 	struct atomisp_stream_env stream_env[ATOMISP_INPUT_STREAM_NUM];
252 
253 	struct v4l2_pix_format dvs_envelop;
254 	unsigned int s3a_bufs_in_css[IA_CSS_PIPE_ID_NUM];
255 	unsigned int dis_bufs_in_css;
256 
257 	unsigned int metadata_bufs_in_css
258 	[ATOMISP_INPUT_STREAM_NUM][IA_CSS_PIPE_ID_NUM];
259 	/* The list of free and available metadata buffers for CSS */
260 	struct list_head metadata[ATOMISP_METADATA_TYPE_NUM];
261 	/* The list of metadata buffers which have been en-queued to CSS */
262 	struct list_head metadata_in_css[ATOMISP_METADATA_TYPE_NUM];
263 	/* The list of metadata buffers which are ready for userspace to get */
264 	struct list_head metadata_ready[ATOMISP_METADATA_TYPE_NUM];
265 
266 	/* The list of free and available s3a stat buffers for CSS */
267 	struct list_head s3a_stats;
268 	/* The list of s3a stat buffers which have been en-queued to CSS */
269 	struct list_head s3a_stats_in_css;
270 	/* The list of s3a stat buffers which are ready for userspace to get */
271 	struct list_head s3a_stats_ready;
272 
273 	struct list_head dis_stats;
274 	struct list_head dis_stats_in_css;
275 	spinlock_t dis_stats_lock;
276 
277 	/* This field specifies which camera (v4l2 input) is selected. */
278 	int input_curr;
279 
280 	atomic_t sof_count;
281 	atomic_t sequence;      /* Sequence value that is assigned to buffer. */
282 	atomic_t sequence_temp;
283 
284 	/*
285 	 * Writers of streaming must hold both isp->mutex and isp->lock.
286 	 * Readers of streaming need to hold only one of the two locks.
287 	 */
288 	bool streaming;
289 	bool stream_prepared; /* whether css stream is created */
290 	bool recreate_streams_on_resume;
291 
292 	unsigned int latest_preview_exp_id; /* CSS ZSL/SDV raw buffer id */
293 
294 	bool copy_mode; /* CSI2+ use copy mode */
295 
296 	int raw_buffer_bitmap[ATOMISP_MAX_EXP_ID / 32 +
297 						 1]; /* Record each Raw Buffer lock status */
298 	int raw_buffer_locked_count;
299 	spinlock_t raw_buffer_bitmap_lock;
300 
301 	/* ISP2401 */
302 	bool re_trigger_capture;
303 
304 	struct atomisp_resolution sensor_array_res;
305 	bool high_speed_mode; /* Indicate whether now is a high speed mode */
306 
307 	unsigned int preview_exp_id;
308 	unsigned int postview_exp_id;
309 };
310 
311 extern const struct atomisp_in_fmt_conv atomisp_in_fmt_conv[];
312 
313 u32 atomisp_subdev_uncompressed_code(u32 code);
314 bool atomisp_subdev_is_compressed(u32 code);
315 const struct atomisp_in_fmt_conv *atomisp_find_in_fmt_conv(u32 code);
316 
317 /* ISP2400 */
318 const struct atomisp_in_fmt_conv *atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
319     enum atomisp_input_format atomisp_in_fmt);
320 
321 /* ISP2401 */
322 const struct atomisp_in_fmt_conv
323 *atomisp_find_in_fmt_conv_by_atomisp_in_fmt(enum atomisp_input_format
324 	atomisp_in_fmt);
325 
326 const struct atomisp_in_fmt_conv *atomisp_find_in_fmt_conv_compressed(u32 code);
327 bool atomisp_subdev_format_conversion(struct atomisp_sub_device *asd);
328 
329 /* Get pointer to appropriate format */
330 struct v4l2_mbus_framefmt
331 *atomisp_subdev_get_ffmt(struct v4l2_subdev *sd,
332 			 struct v4l2_subdev_state *sd_state, uint32_t which,
333 			 uint32_t pad);
334 struct v4l2_rect *atomisp_subdev_get_rect(struct v4l2_subdev *sd,
335 	struct v4l2_subdev_state *sd_state,
336 	u32 which, uint32_t pad,
337 	uint32_t target);
338 int atomisp_subdev_set_selection(struct v4l2_subdev *sd,
339 				 struct v4l2_subdev_state *sd_state,
340 				 u32 which, uint32_t pad, uint32_t target,
341 				 u32 flags, struct v4l2_rect *r);
342 /* Actually set the format */
343 void atomisp_subdev_set_ffmt(struct v4l2_subdev *sd,
344 			     struct v4l2_subdev_state *sd_state,
345 			     uint32_t which,
346 			     u32 pad, struct v4l2_mbus_framefmt *ffmt);
347 
348 void atomisp_subdev_cleanup_pending_events(struct atomisp_sub_device *asd);
349 
350 void atomisp_subdev_unregister_entities(struct atomisp_sub_device *asd);
351 int atomisp_subdev_register_subdev(struct atomisp_sub_device *asd,
352 				   struct v4l2_device *vdev);
353 int atomisp_subdev_init(struct atomisp_device *isp);
354 void atomisp_subdev_cleanup(struct atomisp_device *isp);
355 
356 #endif /* __ATOMISP_SUBDEV_H__ */
357