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