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