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 enum atomisp_subdev_input_entity {
34 	ATOMISP_SUBDEV_INPUT_NONE,
35 	ATOMISP_SUBDEV_INPUT_MEMORY,
36 	ATOMISP_SUBDEV_INPUT_CSI2,
37 	/*
38 	 * The following enum for CSI2 port must go together in one row.
39 	 * Otherwise it breaks the code logic.
40 	 */
41 	ATOMISP_SUBDEV_INPUT_CSI2_PORT1,
42 	ATOMISP_SUBDEV_INPUT_CSI2_PORT2,
43 	ATOMISP_SUBDEV_INPUT_CSI2_PORT3,
44 };
45 
46 #define ATOMISP_SUBDEV_PAD_SINK			0
47 /* capture output for still frames */
48 #define ATOMISP_SUBDEV_PAD_SOURCE_CAPTURE	1
49 /* viewfinder output for downscaled capture output */
50 #define ATOMISP_SUBDEV_PAD_SOURCE_VF		2
51 /* preview output for display */
52 #define ATOMISP_SUBDEV_PAD_SOURCE_PREVIEW	3
53 /* main output for video pipeline */
54 #define ATOMISP_SUBDEV_PAD_SOURCE_VIDEO	4
55 #define ATOMISP_SUBDEV_PADS_NUM			5
56 
57 struct atomisp_in_fmt_conv {
58 	u32     code;
59 	u8 bpp; /* bits per pixel */
60 	u8 depth; /* uncompressed */
61 	enum atomisp_input_format atomisp_in_fmt;
62 	enum ia_css_bayer_order bayer_order;
63 };
64 
65 struct atomisp_sub_device;
66 
67 struct atomisp_video_pipe {
68 	struct video_device vdev;
69 	enum v4l2_buf_type type;
70 	struct media_pad pad;
71 	struct vb2_queue vb_queue;
72 	/* Lock for vb_queue, when also taking isp->mutex this must be taken first! */
73 	struct mutex vb_queue_mutex;
74 	/* List of video-buffers handed over to the CSS  */
75 	struct list_head buffers_in_css;
76 	/* List of video-buffers handed over to the driver, but not yet to the CSS */
77 	struct list_head activeq;
78 	/*
79 	 * the buffers waiting for per-frame parameters, this is only valid
80 	 * in per-frame setting mode.
81 	 */
82 	struct list_head buffers_waiting_for_param;
83 	/* the link list to store per_frame parameters */
84 	struct list_head per_frame_params;
85 
86 	/* Filled through atomisp_get_css_frame_info() on queue setup */
87 	struct ia_css_frame_info frame_info;
88 
89 	/* Store here the initial run mode */
90 	unsigned int default_run_mode;
91 	/* Set from streamoff to disallow queuing further buffers in CSS */
92 	bool stopping;
93 
94 	/*
95 	 * irq_lock is used to protect video buffer state change operations and
96 	 * also to make activeq and capq operations atomic.
97 	 */
98 	spinlock_t irq_lock;
99 	unsigned int users;
100 
101 	struct atomisp_device *isp;
102 	struct v4l2_pix_format pix;
103 	u32 sh_fmt;
104 
105 	struct atomisp_sub_device *asd;
106 
107 	/*
108 	 * This frame_config_id is got from CSS when dequueues buffers from CSS,
109 	 * it is used to indicate which parameter it has applied.
110 	 */
111 	unsigned int frame_config_id[VIDEO_MAX_FRAME];
112 	/*
113 	 * This config id is set when camera HAL enqueues buffer, it has a
114 	 * non-zero value to indicate which parameter it needs to applu
115 	 */
116 	unsigned int frame_request_config_id[VIDEO_MAX_FRAME];
117 	struct atomisp_css_params_with_list *frame_params[VIDEO_MAX_FRAME];
118 };
119 
120 #define vq_to_pipe(queue) \
121 	container_of(queue, struct atomisp_video_pipe, vb_queue)
122 
123 #define vb_to_pipe(vb) vq_to_pipe((vb)->vb2_queue)
124 
125 struct atomisp_pad_format {
126 	struct v4l2_mbus_framefmt fmt;
127 	struct v4l2_rect crop;
128 	struct v4l2_rect compose;
129 };
130 
131 /* Internal states for flash process */
132 enum atomisp_flash_state {
133 	ATOMISP_FLASH_IDLE,
134 	ATOMISP_FLASH_REQUESTED,
135 	ATOMISP_FLASH_ONGOING,
136 	ATOMISP_FLASH_DONE
137 };
138 
139 /*
140  * This structure is used to cache the CSS parameters, it aligns to
141  * struct ia_css_isp_config but without un-supported and deprecated parts.
142  */
143 struct atomisp_css_params {
144 	struct ia_css_wb_config   wb_config;
145 	struct ia_css_cc_config   cc_config;
146 	struct ia_css_tnr_config  tnr_config;
147 	struct ia_css_ecd_config  ecd_config;
148 	struct ia_css_ynr_config  ynr_config;
149 	struct ia_css_fc_config   fc_config;
150 	struct ia_css_formats_config formats_config;
151 	struct ia_css_cnr_config  cnr_config;
152 	struct ia_css_macc_config macc_config;
153 	struct ia_css_ctc_config  ctc_config;
154 	struct ia_css_aa_config   aa_config;
155 	struct ia_css_aa_config   baa_config;
156 	struct ia_css_ce_config   ce_config;
157 	struct ia_css_ob_config   ob_config;
158 	struct ia_css_dp_config   dp_config;
159 	struct ia_css_de_config   de_config;
160 	struct ia_css_gc_config   gc_config;
161 	struct ia_css_nr_config   nr_config;
162 	struct ia_css_ee_config   ee_config;
163 	struct ia_css_anr_config  anr_config;
164 	struct ia_css_3a_config   s3a_config;
165 	struct ia_css_xnr_config  xnr_config;
166 	struct ia_css_dz_config   dz_config;
167 	struct ia_css_cc_config yuv2rgb_cc_config;
168 	struct ia_css_cc_config rgb2yuv_cc_config;
169 	struct ia_css_macc_table  macc_table;
170 	struct ia_css_gamma_table gamma_table;
171 	struct ia_css_ctc_table   ctc_table;
172 
173 	struct ia_css_xnr_table   xnr_table;
174 	struct ia_css_rgb_gamma_table r_gamma_table;
175 	struct ia_css_rgb_gamma_table g_gamma_table;
176 	struct ia_css_rgb_gamma_table b_gamma_table;
177 
178 	struct ia_css_vector      motion_vector;
179 	struct ia_css_anr_thres   anr_thres;
180 
181 	struct ia_css_dvs_6axis_config *dvs_6axis;
182 	struct ia_css_dvs2_coefficients *dvs2_coeff;
183 	struct ia_css_shading_table *shading_table;
184 	struct ia_css_morph_table   *morph_table;
185 
186 	/*
187 	 * Used to store the user pointer address of the frame. driver needs to
188 	 * translate to ia_css_frame * and then set to CSS.
189 	 */
190 	void		*output_frame;
191 	u32	isp_config_id;
192 
193 	/* Indicates which parameters need to be updated. */
194 	struct atomisp_parameters update_flag;
195 };
196 
197 struct atomisp_subdev_params {
198 	/* FIXME: Determines whether raw capture buffer are being passed to
199 	 * user space. Unimplemented for now. */
200 	int online_process;
201 	int yuv_ds_en;
202 	unsigned int color_effect;
203 	bool gdc_cac_en;
204 	bool macc_en;
205 	bool bad_pixel_en;
206 	bool video_dis_en;
207 	bool sc_en;
208 	bool fpn_en;
209 	bool xnr_en;
210 	bool low_light;
211 	int false_color;
212 	unsigned int histogram_elenum;
213 
214 	/* Current grid info */
215 	struct ia_css_grid_info curr_grid_info;
216 	enum ia_css_pipe_id s3a_enabled_pipe;
217 
218 	int s3a_output_bytes;
219 
220 	bool dis_proj_data_valid;
221 
222 	struct ia_css_dz_config   dz_config;  /** Digital Zoom */
223 	struct ia_css_capture_config   capture_config;
224 
225 	struct ia_css_isp_config config;
226 
227 	/* current configurations */
228 	struct atomisp_css_params css_param;
229 
230 	/*
231 	 * Intermediate buffers used to communicate data between
232 	 * CSS and user space.
233 	 */
234 	struct ia_css_3a_statistics *s3a_user_stat;
235 
236 	void *metadata_user[ATOMISP_METADATA_TYPE_NUM];
237 	u32 metadata_width_size;
238 
239 	struct ia_css_dvs2_statistics *dvs_stat;
240 	struct ia_css_dvs_6axis_config *dvs_6axis;
241 	u32 exp_id;
242 	int  dvs_hor_coef_bytes;
243 	int  dvs_ver_coef_bytes;
244 	int  dvs_ver_proj_bytes;
245 	int  dvs_hor_proj_bytes;
246 
247 	/* Flash */
248 	int num_flash_frames;
249 	enum atomisp_flash_state flash_state;
250 	enum atomisp_frame_status last_frame_status;
251 
252 	/* continuous capture */
253 	struct atomisp_cont_capture_conf offline_parm;
254 	/* Flag to check if driver needs to update params to css */
255 	bool css_update_params_needed;
256 };
257 
258 struct atomisp_css_params_with_list {
259 	/* parameters for CSS */
260 	struct atomisp_css_params params;
261 	struct list_head list;
262 };
263 
264 struct atomisp_sub_device {
265 	struct v4l2_subdev subdev;
266 	struct media_pad pads[ATOMISP_SUBDEV_PADS_NUM];
267 	struct atomisp_pad_format fmt[ATOMISP_SUBDEV_PADS_NUM];
268 	u16 capture_pad; /* main capture pad; defines much of isp config */
269 
270 	enum atomisp_subdev_input_entity input;
271 	unsigned int output;
272 	struct atomisp_video_pipe video_out_capture; /* capture output */
273 	struct atomisp_video_pipe video_out_vf;      /* viewfinder output */
274 	struct atomisp_video_pipe video_out_preview; /* preview output */
275 	/* video pipe main output */
276 	struct atomisp_video_pipe video_out_video_capture;
277 	/* struct isp_subdev_params params; */
278 	struct atomisp_device *isp;
279 	struct v4l2_ctrl_handler ctrl_handler;
280 	struct v4l2_ctrl *fmt_auto;
281 	struct v4l2_ctrl *run_mode;
282 	struct v4l2_ctrl *depth_mode;
283 	struct v4l2_ctrl *vfpp;
284 	struct v4l2_ctrl *continuous_mode;
285 	struct v4l2_ctrl *continuous_raw_buffer_size;
286 	struct v4l2_ctrl *continuous_viewfinder;
287 	struct v4l2_ctrl *enable_raw_buffer_lock;
288 
289 	/* ISP2401 */
290 	struct v4l2_ctrl *ion_dev_fd;
291 
292 	struct v4l2_ctrl *disable_dz;
293 
294 	struct atomisp_subdev_params params;
295 
296 	struct atomisp_stream_env stream_env[ATOMISP_INPUT_STREAM_NUM];
297 
298 	struct v4l2_pix_format dvs_envelop;
299 	unsigned int s3a_bufs_in_css[IA_CSS_PIPE_ID_NUM];
300 	unsigned int dis_bufs_in_css;
301 
302 	unsigned int metadata_bufs_in_css
303 	[ATOMISP_INPUT_STREAM_NUM][IA_CSS_PIPE_ID_NUM];
304 	/* The list of free and available metadata buffers for CSS */
305 	struct list_head metadata[ATOMISP_METADATA_TYPE_NUM];
306 	/* The list of metadata buffers which have been en-queued to CSS */
307 	struct list_head metadata_in_css[ATOMISP_METADATA_TYPE_NUM];
308 	/* The list of metadata buffers which are ready for userspace to get */
309 	struct list_head metadata_ready[ATOMISP_METADATA_TYPE_NUM];
310 
311 	/* The list of free and available s3a stat buffers for CSS */
312 	struct list_head s3a_stats;
313 	/* The list of s3a stat buffers which have been en-queued to CSS */
314 	struct list_head s3a_stats_in_css;
315 	/* The list of s3a stat buffers which are ready for userspace to get */
316 	struct list_head s3a_stats_ready;
317 
318 	struct list_head dis_stats;
319 	struct list_head dis_stats_in_css;
320 	spinlock_t dis_stats_lock;
321 
322 	struct ia_css_frame *vf_frame; /* TODO: needed? */
323 	struct ia_css_frame *raw_output_frame;
324 	enum atomisp_frame_status frame_status[VIDEO_MAX_FRAME];
325 
326 	/* This field specifies which camera (v4l2 input) is selected. */
327 	int input_curr;
328 
329 	atomic_t sof_count;
330 	atomic_t sequence;      /* Sequence value that is assigned to buffer. */
331 	atomic_t sequence_temp;
332 
333 	/*
334 	 * Writers of streaming must hold both isp->mutex and isp->lock.
335 	 * Readers of streaming need to hold only one of the two locks.
336 	 */
337 	unsigned int streaming;
338 	bool stream_prepared; /* whether css stream is created */
339 
340 	/* subdev index: will be used to show which subdev is holding the
341 	 * resource, like which camera is used by which subdev
342 	 */
343 	unsigned int index;
344 
345 	/* delayed memory allocation for css */
346 	struct completion init_done;
347 	struct workqueue_struct *delayed_init_workq;
348 	unsigned int delayed_init;
349 	struct work_struct delayed_init_work;
350 
351 	unsigned int latest_preview_exp_id; /* CSS ZSL/SDV raw buffer id */
352 
353 	unsigned int mipi_frame_size;
354 
355 	bool copy_mode; /* CSI2+ use copy mode */
356 	bool yuvpp_mode;	/* CSI2+ yuvpp pipe */
357 
358 	int raw_buffer_bitmap[ATOMISP_MAX_EXP_ID / 32 +
359 						 1]; /* Record each Raw Buffer lock status */
360 	int raw_buffer_locked_count;
361 	spinlock_t raw_buffer_bitmap_lock;
362 
363 	/* ISP2401 */
364 	bool re_trigger_capture;
365 
366 	struct atomisp_resolution sensor_array_res;
367 	bool high_speed_mode; /* Indicate whether now is a high speed mode */
368 	int pending_capture_request; /* Indicates the number of pending capture requests. */
369 
370 	unsigned int preview_exp_id;
371 	unsigned int postview_exp_id;
372 };
373 
374 extern const struct atomisp_in_fmt_conv atomisp_in_fmt_conv[];
375 
376 u32 atomisp_subdev_uncompressed_code(u32 code);
377 bool atomisp_subdev_is_compressed(u32 code);
378 const struct atomisp_in_fmt_conv *atomisp_find_in_fmt_conv(u32 code);
379 
380 /* ISP2400 */
381 const struct atomisp_in_fmt_conv *atomisp_find_in_fmt_conv_by_atomisp_in_fmt(
382     enum atomisp_input_format atomisp_in_fmt);
383 
384 /* ISP2401 */
385 const struct atomisp_in_fmt_conv
386 *atomisp_find_in_fmt_conv_by_atomisp_in_fmt(enum atomisp_input_format
387 	atomisp_in_fmt);
388 
389 const struct atomisp_in_fmt_conv *atomisp_find_in_fmt_conv_compressed(u32 code);
390 bool atomisp_subdev_format_conversion(struct atomisp_sub_device *asd,
391 				      unsigned int source_pad);
392 uint16_t atomisp_subdev_source_pad(struct video_device *vdev);
393 
394 /* Get pointer to appropriate format */
395 struct v4l2_mbus_framefmt
396 *atomisp_subdev_get_ffmt(struct v4l2_subdev *sd,
397 			 struct v4l2_subdev_state *sd_state, uint32_t which,
398 			 uint32_t pad);
399 struct v4l2_rect *atomisp_subdev_get_rect(struct v4l2_subdev *sd,
400 	struct v4l2_subdev_state *sd_state,
401 	u32 which, uint32_t pad,
402 	uint32_t target);
403 int atomisp_subdev_set_selection(struct v4l2_subdev *sd,
404 				 struct v4l2_subdev_state *sd_state,
405 				 u32 which, uint32_t pad, uint32_t target,
406 				 u32 flags, struct v4l2_rect *r);
407 /* Actually set the format */
408 void atomisp_subdev_set_ffmt(struct v4l2_subdev *sd,
409 			     struct v4l2_subdev_state *sd_state,
410 			     uint32_t which,
411 			     u32 pad, struct v4l2_mbus_framefmt *ffmt);
412 
413 int atomisp_update_run_mode(struct atomisp_sub_device *asd);
414 
415 void atomisp_subdev_cleanup_pending_events(struct atomisp_sub_device *asd);
416 
417 void atomisp_subdev_unregister_entities(struct atomisp_sub_device *asd);
418 int atomisp_subdev_register_subdev(struct atomisp_sub_device *asd,
419 				   struct v4l2_device *vdev);
420 int atomisp_subdev_register_video_nodes(struct atomisp_sub_device *asd,
421 					struct v4l2_device *vdev);
422 int atomisp_subdev_init(struct atomisp_device *isp);
423 void atomisp_subdev_cleanup(struct atomisp_device *isp);
424 int atomisp_create_pads_links(struct atomisp_device *isp);
425 
426 #endif /* __ATOMISP_SUBDEV_H__ */
427