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