1 /*
2  * Support for Intel Camera Imaging ISP subsystem.
3  * Copyright (c) 2015, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14 
15 #ifndef __IA_CSS_STREAM_PUBLIC_H
16 #define __IA_CSS_STREAM_PUBLIC_H
17 
18 /* @file
19  * This file contains support for configuring and controlling streams
20  */
21 
22 #include <type_support.h>
23 #include "ia_css_types.h"
24 #include "ia_css_pipe_public.h"
25 #include "ia_css_metadata.h"
26 #include "ia_css_tpg.h"
27 #include "ia_css_prbs.h"
28 #include "ia_css_input_port.h"
29 
30 /* Input modes, these enumerate all supported input modes.
31  *  Note that not all ISP modes support all input modes.
32  */
33 enum ia_css_input_mode {
34 	IA_CSS_INPUT_MODE_SENSOR, /** data from sensor */
35 	IA_CSS_INPUT_MODE_FIFO,   /** data from input-fifo */
36 	IA_CSS_INPUT_MODE_TPG,    /** data from test-pattern generator */
37 	IA_CSS_INPUT_MODE_PRBS,   /** data from pseudo-random bit stream */
38 	IA_CSS_INPUT_MODE_MEMORY, /** data from a frame in memory */
39 	IA_CSS_INPUT_MODE_BUFFERED_SENSOR /** data is sent through mipi buffer */
40 };
41 
42 /* Structure of the MIPI buffer configuration
43  */
44 struct ia_css_mipi_buffer_config {
45 	unsigned int size_mem_words; /** The frame size in the system memory
46 					  words (32B) */
47 	bool contiguous;	     /** Allocated memory physically
48 					  contiguously or not. \deprecated{Will be false always.}*/
49 	unsigned int nof_mipi_buffers; /** The number of MIPI buffers required for this
50 					stream */
51 };
52 
53 enum {
54 	IA_CSS_STREAM_ISYS_STREAM_0 = 0,
55 	IA_CSS_STREAM_DEFAULT_ISYS_STREAM_IDX = IA_CSS_STREAM_ISYS_STREAM_0,
56 	IA_CSS_STREAM_ISYS_STREAM_1,
57 	IA_CSS_STREAM_MAX_ISYS_STREAM_PER_CH
58 };
59 
60 /* This is input data configuration for one MIPI data type. We can have
61  *  multiple of this in one virtual channel.
62  */
63 struct ia_css_stream_isys_stream_config {
64 	struct ia_css_resolution  input_res; /** Resolution of input data */
65 	enum atomisp_input_format format; /** Format of input stream. This data
66 					       format will be mapped to MIPI data
67 					       type internally. */
68 	int linked_isys_stream_id; /** default value is -1, other value means
69 							current isys_stream shares the same buffer with
70 							indicated isys_stream*/
71 	bool valid; /** indicate whether other fields have valid value */
72 };
73 
74 struct ia_css_stream_input_config {
75 	struct ia_css_resolution  input_res; /** Resolution of input data */
76 	struct ia_css_resolution  effective_res; /** Resolution of input data.
77 							Used for CSS 2400/1 System and deprecated for other
78 							systems (replaced by input_effective_res in
79 							ia_css_pipe_config) */
80 	enum atomisp_input_format format; /** Format of input stream. This data
81 					       format will be mapped to MIPI data
82 					       type internally. */
83 	enum ia_css_bayer_order bayer_order; /** Bayer order for RAW streams */
84 };
85 
86 /* Input stream description. This describes how input will flow into the
87  *  CSS. This is used to program the CSS hardware.
88  */
89 struct ia_css_stream_config {
90 	enum ia_css_input_mode    mode; /** Input mode */
91 	union {
92 		struct ia_css_input_port  port; /** Port, for sensor only. */
93 		struct ia_css_tpg_config  tpg;  /** TPG configuration */
94 		struct ia_css_prbs_config prbs; /** PRBS configuration */
95 	} source; /** Source of input data */
96 	unsigned int	      channel_id; /** Channel on which input data
97 						   will arrive. Use this field
98 						   to specify virtual channel id.
99 						   Valid values are: 0, 1, 2, 3 */
100 	struct ia_css_stream_isys_stream_config
101 		isys_config[IA_CSS_STREAM_MAX_ISYS_STREAM_PER_CH];
102 	struct ia_css_stream_input_config input_config;
103 
104 	/* Currently, Android and Windows platforms interpret the binning_factor parameter
105 	 * differently. In Android, the binning factor is expressed in the form
106 	 * 2^N * 2^N, whereas in Windows platform, the binning factor is N*N
107 	 * To use the Windows method of specification, the caller has to define
108 	 * macro USE_WINDOWS_BINNING_FACTOR. This is for backward compatibility only
109 	 * and will be deprecated. In the future,all platforms will use the N*N method
110 	 */
111 	/* ISP2401 */
112 	unsigned int sensor_binning_factor; /** Binning factor used by sensor
113 						 to produce image data. This is
114 						 used for shading correction. */
115 	unsigned int pixels_per_clock; /** Number of pixels per clock, which can be
116 					    1, 2 or 4. */
117 	bool online; /** offline will activate RAW copy on SP, use this for
118 			  continuous capture. */
119 	/* ISYS2401 usage: ISP receives data directly from sensor, no copy. */
120 	unsigned int init_num_cont_raw_buf; /** initial number of raw buffers to
121 					     allocate */
122 	unsigned int target_num_cont_raw_buf; /** total number of raw buffers to
123 					     allocate */
124 	bool pack_raw_pixels; /** Pack pixels in the raw buffers */
125 	bool continuous; /** Use SP copy feature to continuously capture frames
126 			      to system memory and run pipes in offline mode */
127 	bool disable_cont_viewfinder; /** disable continuous viewfinder for ZSL use case */
128 	s32 flash_gpio_pin; /** pin on which the flash is connected, -1 for no flash */
129 	int left_padding; /** The number of input-formatter left-paddings, -1 for default from binary.*/
130 	struct ia_css_mipi_buffer_config
131 		mipi_buffer_config; /** mipi buffer configuration */
132 	struct ia_css_metadata_config
133 		metadata_config;     /** Metadata configuration. */
134 	bool ia_css_enable_raw_buffer_locking; /** Enable Raw Buffer Locking for HALv3 Support */
135 	bool lock_all;
136 	/** Lock all RAW buffers (true) or lock only buffers processed by
137 	     video or preview pipe (false).
138 	     This setting needs to be enabled to allow raw buffer locking
139 	     without continuous viewfinder. */
140 };
141 
142 struct ia_css_stream;
143 
144 /* Stream info, this struct describes properties of a stream after it has been
145  *  created.
146  */
147 struct ia_css_stream_info {
148 	struct ia_css_metadata_info metadata_info;
149 	/** Info about the metadata layout, this contains the stride. */
150 };
151 
152 /* @brief Load default stream configuration
153  * @param[in,out]	stream_config The stream configuration.
154  * @return	None
155  *
156  * This function will reset the stream configuration to the default state:
157 @code
158 	memset(stream_config, 0, sizeof(*stream_config));
159 	stream_config->online = true;
160 	stream_config->left_padding = -1;
161 @endcode
162  */
163 void ia_css_stream_config_defaults(struct ia_css_stream_config *stream_config);
164 
165 /*
166  * create the internal structures and fill in the configuration data and pipes
167  */
168 
169 /* @brief Creates a stream
170 * @param[in]	stream_config The stream configuration.
171 * @param[in]	num_pipes The number of pipes to incorporate in the stream.
172 * @param[in]	pipes The pipes.
173 * @param[out]	stream The stream.
174 * @return	0 or the error code.
175 *
176 * This function will create a stream with a given configuration and given pipes.
177 */
178 int
179 ia_css_stream_create(const struct ia_css_stream_config *stream_config,
180 		     int num_pipes,
181 		     struct ia_css_pipe *pipes[],
182 		     struct ia_css_stream **stream);
183 
184 /* @brief Destroys a stream
185  * @param[in]	stream The stream.
186  * @return	0 or the error code.
187  *
188  * This function will destroy a given stream.
189  */
190 int
191 ia_css_stream_destroy(struct ia_css_stream *stream);
192 
193 /* @brief Provides information about a stream
194  * @param[in]	stream The stream.
195  * @param[out]	stream_info The information about the stream.
196  * @return	0 or the error code.
197  *
198  * This function will destroy a given stream.
199  */
200 int
201 ia_css_stream_get_info(const struct ia_css_stream *stream,
202 		       struct ia_css_stream_info *stream_info);
203 
204 /* @brief load (rebuild) a stream that was unloaded.
205  * @param[in]	stream The stream
206  * @return		0 or the error code
207  *
208  * Rebuild a stream, including allocating structs, setting configuration and
209  * building the required pipes.
210  */
211 int
212 ia_css_stream_load(struct ia_css_stream *stream);
213 
214 /* @brief Starts the stream.
215  * @param[in]	stream The stream.
216  * @return 0 or the error code.
217  *
218  * The dynamic data in
219  * the buffers are not used and need to be queued with a separate call
220  * to ia_css_pipe_enqueue_buffer.
221  * NOTE: this function will only send start event to corresponding
222  * thread and will not start SP any more.
223  */
224 int
225 ia_css_stream_start(struct ia_css_stream *stream);
226 
227 /* @brief Stop the stream.
228  * @param[in]	stream The stream.
229  * @return	0 or the error code.
230  *
231  * NOTE: this function will send stop event to pipes belong to this
232  * stream but will not terminate threads.
233  */
234 int
235 ia_css_stream_stop(struct ia_css_stream *stream);
236 
237 /* @brief Check if a stream has stopped
238  * @param[in]	stream The stream.
239  * @return	boolean flag
240  *
241  * This function will check if the stream has stopped and return the correspondent boolean flag.
242  */
243 bool
244 ia_css_stream_has_stopped(struct ia_css_stream *stream);
245 
246 /* @brief	destroy a stream according to the stream seed previosly saved in the seed array.
247  * @param[in]	stream The stream.
248  * @return	0 (no other errors are generated now)
249  *
250  * Destroy the stream and all the pipes related to it.
251  */
252 int
253 ia_css_stream_unload(struct ia_css_stream *stream);
254 
255 /* @brief Returns stream format
256  * @param[in]	stream The stream.
257  * @return	format of the string
258  *
259  * This function will return the stream format.
260  */
261 enum atomisp_input_format
262 ia_css_stream_get_format(const struct ia_css_stream *stream);
263 
264 /* @brief Check if the stream is configured for 2 pixels per clock
265  * @param[in]	stream The stream.
266  * @return	boolean flag
267  *
268  * This function will check if the stream is configured for 2 pixels per clock and
269  * return the correspondent boolean flag.
270  */
271 bool
272 ia_css_stream_get_two_pixels_per_clock(const struct ia_css_stream *stream);
273 
274 /* @brief Sets the output frame stride (at the last pipe)
275  * @param[in]	stream The stream
276  * @param[in]	output_padded_width - the output buffer stride.
277  * @return	ia_css_err
278  *
279  * This function will Set the output frame stride (at the last pipe)
280  */
281 int
282 ia_css_stream_set_output_padded_width(struct ia_css_stream *stream,
283 				      unsigned int output_padded_width);
284 
285 /* @brief Return max number of continuous RAW frames.
286  * @param[in]	stream The stream.
287  * @param[out]	buffer_depth The maximum number of continuous RAW frames.
288  * @return	0 or -EINVAL
289  *
290  * This function will return the maximum number of continuous RAW frames
291  * the system can support.
292  */
293 int
294 ia_css_stream_get_max_buffer_depth(struct ia_css_stream *stream,
295 				   int *buffer_depth);
296 
297 /* @brief Set nr of continuous RAW frames to use.
298  *
299  * @param[in]	stream The stream.
300  * @param[in]	buffer_depth	Number of frames to set.
301  * @return	0 or error code upon error.
302  *
303  * Set the number of continuous frames to use during continuous modes.
304  */
305 int
306 ia_css_stream_set_buffer_depth(struct ia_css_stream *stream, int buffer_depth);
307 
308 /* @brief Get number of continuous RAW frames to use.
309  * @param[in]	stream The stream.
310  * @param[out]	buffer_depth The number of frames to use
311  * @return	0 or -EINVAL
312  *
313  * Get the currently set number of continuous frames
314  * to use during continuous modes.
315  */
316 int
317 ia_css_stream_get_buffer_depth(struct ia_css_stream *stream, int *buffer_depth);
318 
319 /* ===== CAPTURE ===== */
320 
321 /* @brief Configure the continuous capture
322  *
323  * @param[in]	stream		The stream.
324  * @param[in]	num_captures	The number of RAW frames to be processed to
325  *				YUV. Setting this to -1 will make continuous
326  *				capture run until it is stopped.
327  *				This number will also be used to allocate RAW
328  *				buffers. To allow the viewfinder to also
329  *				keep operating, 2 extra buffers will always be
330  *				allocated.
331  *				If the offset is negative and the skip setting
332  *				is greater than 0, additional buffers may be
333  *				needed.
334  * @param[in]	skip		Skip N frames in between captures. This can be
335  *				used to select a slower capture frame rate than
336  *				the sensor output frame rate.
337  * @param[in]	offset		Start the RAW-to-YUV processing at RAW buffer
338  *				with this offset. This allows the user to
339  *				process RAW frames that were captured in the
340  *				past or future.
341  * @return			0 or error code upon error.
342  *
343  *  For example, to capture the current frame plus the 2 previous
344  *  frames and 2 subsequent frames, you would call
345  *  ia_css_stream_capture(5, 0, -2).
346  */
347 int
348 ia_css_stream_capture(struct ia_css_stream *stream,
349 		      int num_captures,
350 		      unsigned int skip,
351 		      int offset);
352 
353 /* @brief Specify which raw frame to tag based on exp_id found in frame info
354  *
355  * @param[in]	stream The stream.
356  * @param[in]	exp_id	The exposure id of the raw frame to tag.
357  *
358  * @return			0 or error code upon error.
359  *
360  * This function allows the user to tag a raw frame based on the exposure id
361  * found in the viewfinder frames' frame info.
362  */
363 int
364 ia_css_stream_capture_frame(struct ia_css_stream *stream,
365 			    unsigned int exp_id);
366 
367 /* ===== VIDEO ===== */
368 
369 /* @brief Send streaming data into the css input FIFO
370  *
371  * @param[in]	stream	The stream.
372  * @param[in]	data	Pointer to the pixels to be send.
373  * @param[in]	width	Width of the input frame.
374  * @param[in]	height	Height of the input frame.
375  * @return	None
376  *
377  * Send streaming data into the css input FIFO. This is for testing purposes
378  * only. This uses the channel ID and input format as set by the user with
379  * the regular functions for this.
380  * This function blocks until the entire frame has been written into the
381  * input FIFO.
382  *
383  * Note:
384  * For higher flexibility the ia_css_stream_send_input_frame is replaced by
385  * three separate functions:
386  * 1) ia_css_stream_start_input_frame
387  * 2) ia_css_stream_send_input_line
388  * 3) ia_css_stream_end_input_frame
389  * In this way it is possible to stream multiple frames on different
390  * channel ID's on a line basis. It will be possible to simulate
391  * line-interleaved Stereo 3D muxed on 1 mipi port.
392  * These 3 functions are for testing purpose only and can be used in
393  * conjunction with ia_css_stream_send_input_frame
394  */
395 void
396 ia_css_stream_send_input_frame(const struct ia_css_stream *stream,
397 			       const unsigned short *data,
398 			       unsigned int width,
399 			       unsigned int height);
400 
401 /* @brief Start an input frame on the CSS input FIFO.
402  *
403  * @param[in]	stream The stream.
404  * @return	None
405  *
406  * Starts the streaming to mipi frame by sending SoF for channel channel_id.
407  * It will use the input_format and two_pixels_per_clock as provided by
408  * the user.
409  * For the "correct" use-case, input_format and two_pixels_per_clock must match
410  * with the values as set by the user with the regular functions.
411  * To simulate an error, the user can provide "incorrect" values for
412  * input_format and/or two_pixels_per_clock.
413  */
414 void
415 ia_css_stream_start_input_frame(const struct ia_css_stream *stream);
416 
417 /* @brief Send a line of input data into the CSS input FIFO.
418  *
419  * @param[in]	stream		The stream.
420  * @param[in]	data	Array of the first line of image data.
421  * @param	width	The width (in pixels) of the first line.
422  * @param[in]	data2	Array of the second line of image data.
423  * @param	width2	The width (in pixels) of the second line.
424  * @return	None
425  *
426  * Sends 1 frame line. Start with SoL followed by width bytes of data, followed
427  * by width2 bytes of data2 and followed by and EoL
428  * It will use the input_format and two_pixels_per_clock settings as provided
429  * with the ia_css_stream_start_input_frame function call.
430  *
431  * This function blocks until the entire line has been written into the
432  * input FIFO.
433  */
434 void
435 ia_css_stream_send_input_line(const struct ia_css_stream *stream,
436 			      const unsigned short *data,
437 			      unsigned int width,
438 			      const unsigned short *data2,
439 			      unsigned int width2);
440 
441 /* @brief Send a line of input embedded data into the CSS input FIFO.
442  *
443  * @param[in]	stream     Pointer of the stream.
444  * @param[in]	format     Format of the embedded data.
445  * @param[in]	data       Pointer of the embedded data line.
446  * @param[in]	width      The width (in pixels) of the line.
447  * @return		None
448  *
449  * Sends one embedded data line to input fifo. Start with SoL followed by
450  * width bytes of data, and followed by and EoL.
451  * It will use the two_pixels_per_clock settings as provided with the
452  * ia_css_stream_start_input_frame function call.
453  *
454  * This function blocks until the entire line has been written into the
455  * input FIFO.
456  */
457 void
458 ia_css_stream_send_input_embedded_line(const struct ia_css_stream *stream,
459 				       enum atomisp_input_format format,
460 				       const unsigned short *data,
461 				       unsigned int width);
462 
463 /* @brief End an input frame on the CSS input FIFO.
464  *
465  * @param[in]	stream	The stream.
466  * @return	None
467  *
468  * Send the end-of-frame signal into the CSS input FIFO.
469  */
470 void
471 ia_css_stream_end_input_frame(const struct ia_css_stream *stream);
472 
473 /* @brief send a request flash command to SP
474  *
475  * @param[in]	stream The stream.
476  * @return	None
477  *
478  * Driver needs to call this function to send a flash request command
479  * to SP, SP will be responsible for switching on/off the flash at proper
480  * time. Due to the SP multi-threading environment, this request may have
481  * one-frame delay, the driver needs to check the flashed flag in frame info
482  * to determine which frame is being flashed.
483  */
484 void
485 ia_css_stream_request_flash(struct ia_css_stream *stream);
486 
487 /* @brief Configure a stream with filter coefficients.
488  *	   @deprecated {Replaced by
489  *				   ia_css_pipe_set_isp_config_on_pipe()}
490  *
491  * @param[in]	stream The stream.
492  * @param[in]	config	The set of filter coefficients.
493  * @param[in]   pipe Pipe to be updated when set isp config, NULL means to
494  *		   update all pipes in the stream.
495  * @return		0 or error code upon error.
496  *
497  * This function configures the filter coefficients for an image
498  * stream. For image pipes that do not execute any ISP filters, this
499  * function will have no effect.
500  * It is safe to call this function while the image stream is running,
501  * in fact this is the expected behavior most of the time. Proper
502  * resource locking and double buffering is in place to allow for this.
503  */
504 int
505 ia_css_stream_set_isp_config_on_pipe(struct ia_css_stream *stream,
506 				     const struct ia_css_isp_config *config,
507 				     struct ia_css_pipe *pipe);
508 
509 /* @brief Configure a stream with filter coefficients.
510  *	   @deprecated {Replaced by
511  *				   ia_css_pipe_set_isp_config()}
512  * @param[in]	stream	The stream.
513  * @param[in]	config	The set of filter coefficients.
514  * @return		0 or error code upon error.
515  *
516  * This function configures the filter coefficients for an image
517  * stream. For image pipes that do not execute any ISP filters, this
518  * function will have no effect. All pipes of a stream will be updated.
519  * See ::ia_css_stream_set_isp_config_on_pipe() for the per-pipe alternative.
520  * It is safe to call this function while the image stream is running,
521  * in fact this is the expected behaviour most of the time. Proper
522  * resource locking and double buffering is in place to allow for this.
523  */
524 int
525 ia_css_stream_set_isp_config(
526     struct ia_css_stream *stream,
527     const struct ia_css_isp_config *config);
528 
529 /* @brief Get selected configuration settings
530  * @param[in]	stream	The stream.
531  * @param[out]	config	Configuration settings.
532  * @return		None
533  */
534 void
535 ia_css_stream_get_isp_config(const struct ia_css_stream *stream,
536 			     struct ia_css_isp_config *config);
537 
538 /* @brief allocate continuous raw frames for continuous capture
539  * @param[in]	stream The stream.
540  * @return 0 or error code.
541  *
542  *  because this allocation takes a long time (around 120ms per frame),
543  *  we separate the allocation part and update part to let driver call
544  *  this function without locking. This function is the allocation part
545  *  and next one is update part
546  */
547 int
548 ia_css_alloc_continuous_frame_remain(struct ia_css_stream *stream);
549 
550 /* @brief allocate continuous raw frames for continuous capture
551  * @param[in]	stream The stream.
552  * @return	0 or error code.
553  *
554  *  because this allocation takes a long time (around 120ms per frame),
555  *  we separate the allocation part and update part to let driver call
556  *  this function without locking. This function is the update part
557  */
558 int
559 ia_css_update_continuous_frames(struct ia_css_stream *stream);
560 
561 /* @brief ia_css_unlock_raw_frame . unlock a raw frame (HALv3 Support)
562  * @param[in]	stream The stream.
563  * @param[in]   exp_id exposure id that uniquely identifies the locked Raw Frame Buffer
564  * @return      ia_css_err 0 or error code
565  *
566  * As part of HALv3 Feature requirement, SP locks raw buffer until the Application
567  * releases its reference to a raw buffer (which are managed by SP), this function allows
568  * application to explicitly unlock that buffer in SP.
569  */
570 int
571 ia_css_unlock_raw_frame(struct ia_css_stream *stream, uint32_t exp_id);
572 
573 /* @brief ia_css_en_dz_capt_pipe . Enable/Disable digital zoom for capture pipe
574  * @param[in]   stream The stream.
575  * @param[in]   enable - true, disable - false
576  * @return      None
577  *
578  * Enables or disables digital zoom for capture pipe in provided stream, if capture pipe
579  * exists. This function sets enable_zoom flag in CAPTURE_PP stage of the capture pipe.
580  * In process_zoom_and_motion(), decision to enable or disable zoom for every stage depends
581  * on this flag.
582  */
583 void
584 ia_css_en_dz_capt_pipe(struct ia_css_stream *stream, bool enable);
585 #endif /* __IA_CSS_STREAM_PUBLIC_H */
586