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