Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/*
 * Support for Intel Camera Imaging ISP subsystem.
 * Copyright (c) 2015, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#include "ia_css_mipi.h"
#include "sh_css_mipi.h"
#include <type_support.h>
#include "system_global.h"
#include "ia_css_err.h"
#include "ia_css_pipe.h"
#include "ia_css_stream_format.h"
#include "sh_css_stream_format.h"
#include "ia_css_stream_public.h"
#include "ia_css_frame_public.h"
#include "ia_css_input_port.h"
#include "ia_css_debug.h"
#include "sh_css_struct.h"
#include "sh_css_defs.h"
#include "sh_css_sp.h" /* sh_css_update_host2sp_mipi_frame sh_css_update_host2sp_num_mipi_frames ... */
#include "sw_event_global.h" /* IA_CSS_PSYS_SW_EVENT_MIPI_BUFFERS_READY */

#if defined(USE_INPUT_SYSTEM_VERSION_2) || defined(USE_INPUT_SYSTEM_VERSION_2401)
static uint32_t ref_count_mipi_allocation[N_CSI_PORTS]; /* Initialized in mipi_init */
#endif

enum ia_css_err
ia_css_mipi_frame_specify(const unsigned int size_mem_words,
				const bool contiguous)
{
	enum ia_css_err err = IA_CSS_SUCCESS;

	my_css.size_mem_words = size_mem_words;
	(void)contiguous;

	return err;
}

#ifdef ISP2401
#if defined(USE_INPUT_SYSTEM_VERSION_2) || defined(USE_INPUT_SYSTEM_VERSION_2401)
/*
 * Check if a source port or TPG/PRBS ID is valid
 */
static bool ia_css_mipi_is_source_port_valid(struct ia_css_pipe *pipe,
						unsigned int *pport)
{
	bool ret = true;
	unsigned int port = 0;
	unsigned int max_ports = 0;

	switch (pipe->stream->config.mode) {
	case IA_CSS_INPUT_MODE_BUFFERED_SENSOR:
		port = (unsigned int) pipe->stream->config.source.port.port;
		max_ports = N_CSI_PORTS;
		break;
	case IA_CSS_INPUT_MODE_TPG:
		port = (unsigned int) pipe->stream->config.source.tpg.id;
		max_ports = N_CSS_TPG_IDS;
		break;
	case IA_CSS_INPUT_MODE_PRBS:
		port = (unsigned int) pipe->stream->config.source.prbs.id;
		max_ports = N_CSS_PRBS_IDS;
		break;
	default:
		assert(false);
		ret = false;
		break;
	}

	if (ret) {
		assert(port < max_ports);

		if (port >= max_ports)
			ret = false;
	}

	*pport = port;

	return ret;
}
#endif

#endif
/* Assumptions:
 *	- A line is multiple of 4 bytes = 1 word.
 *	- Each frame has SOF and EOF (each 1 word).
 *	- Each line has format header and optionally SOL and EOL (each 1 word).
 *	- Odd and even lines of YUV420 format are different in bites per pixel size.
 *	- Custom size of embedded data.
 *  -- Interleaved frames are not taken into account.
 *  -- Lines are multiples of 8B, and not necessary of (custom 3B, or 7B
 *  etc.).
 * Result is given in DDR mem words, 32B or 256 bits
 */
enum ia_css_err
ia_css_mipi_frame_calculate_size(const unsigned int width,
				const unsigned int height,
				const enum ia_css_stream_format format,
				const bool hasSOLandEOL,
				const unsigned int embedded_data_size_words,
				unsigned int *size_mem_words)
{
	enum ia_css_err err = IA_CSS_SUCCESS;

	unsigned int bits_per_pixel = 0;
	unsigned int even_line_bytes = 0;
	unsigned int odd_line_bytes = 0;
	unsigned int words_per_odd_line = 0;
	unsigned int words_for_first_line = 0;
	unsigned int words_per_even_line = 0;
	unsigned int mem_words_per_even_line = 0;
	unsigned int mem_words_per_odd_line = 0;
	unsigned int mem_words_for_first_line = 0;
	unsigned int mem_words_for_EOF = 0;
	unsigned int mem_words = 0;
	unsigned int width_padded = width;

#if defined(USE_INPUT_SYSTEM_VERSION_2401)
	/* The changes will be reverted as soon as RAW
	 * Buffers are deployed by the 2401 Input System
	 * in the non-continuous use scenario.
	 */
	width_padded += (2 * ISP_VEC_NELEMS);
#endif

	IA_CSS_ENTER("padded_width=%d, height=%d, format=%d, hasSOLandEOL=%d, embedded_data_size_words=%d\n",
		     width_padded, height, format, hasSOLandEOL, embedded_data_size_words);

	switch (format) {
	case IA_CSS_STREAM_FORMAT_RAW_6:		/* 4p, 3B, 24bits */
		bits_per_pixel = 6;	break;
	case IA_CSS_STREAM_FORMAT_RAW_7:		/* 8p, 7B, 56bits */
		bits_per_pixel = 7;		break;
	case IA_CSS_STREAM_FORMAT_RAW_8:		/* 1p, 1B, 8bits */
	case IA_CSS_STREAM_FORMAT_BINARY_8:		/*  8bits, TODO: check. */
	case IA_CSS_STREAM_FORMAT_YUV420_8:		/* odd 2p, 2B, 16bits, even 2p, 4B, 32bits */
		bits_per_pixel = 8;		break;
	case IA_CSS_STREAM_FORMAT_YUV420_10:		/* odd 4p, 5B, 40bits, even 4p, 10B, 80bits */
	case IA_CSS_STREAM_FORMAT_RAW_10:		/* 4p, 5B, 40bits */
#if !defined(HAS_NO_PACKED_RAW_PIXELS)
		/* The changes will be reverted as soon as RAW
		 * Buffers are deployed by the 2401 Input System
		 * in the non-continuous use scenario.
		 */
		bits_per_pixel = 10;
#else
		bits_per_pixel = 16;
#endif
		break;
	case IA_CSS_STREAM_FORMAT_YUV420_8_LEGACY:	/* 2p, 3B, 24bits */
	case IA_CSS_STREAM_FORMAT_RAW_12:		/* 2p, 3B, 24bits */
		bits_per_pixel = 12;	break;
	case IA_CSS_STREAM_FORMAT_RAW_14:		/* 4p, 7B, 56bits */
		bits_per_pixel = 14;	break;
	case IA_CSS_STREAM_FORMAT_RGB_444:		/* 1p, 2B, 16bits */
	case IA_CSS_STREAM_FORMAT_RGB_555:		/* 1p, 2B, 16bits */
	case IA_CSS_STREAM_FORMAT_RGB_565:		/* 1p, 2B, 16bits */
	case IA_CSS_STREAM_FORMAT_YUV422_8:		/* 2p, 4B, 32bits */
		bits_per_pixel = 16;	break;
	case IA_CSS_STREAM_FORMAT_RGB_666:		/* 4p, 9B, 72bits */
		bits_per_pixel = 18;	break;
	case IA_CSS_STREAM_FORMAT_YUV422_10:		/* 2p, 5B, 40bits */
		bits_per_pixel = 20;	break;
	case IA_CSS_STREAM_FORMAT_RGB_888:		/* 1p, 3B, 24bits */
		bits_per_pixel = 24;	break;

	case IA_CSS_STREAM_FORMAT_YUV420_16:		/* Not supported */
	case IA_CSS_STREAM_FORMAT_YUV422_16:		/* Not supported */
	case IA_CSS_STREAM_FORMAT_RAW_16:		/* TODO: not specified in MIPI SPEC, check */
	default:
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	}

	odd_line_bytes = (width_padded * bits_per_pixel + 7) >> 3; /* ceil ( bits per line / 8) */

	/* Even lines for YUV420 formats are double in bits_per_pixel. */
	if (format == IA_CSS_STREAM_FORMAT_YUV420_8
			|| format == IA_CSS_STREAM_FORMAT_YUV420_10
			|| format == IA_CSS_STREAM_FORMAT_YUV420_16) {
		even_line_bytes = (width_padded * 2 * bits_per_pixel + 7) >> 3; /* ceil ( bits per line / 8) */
	} else {
		even_line_bytes = odd_line_bytes;
	}

   /*  a frame represented in memory:  ()- optional; data - payload words.
	*  addr		0	1	2	3	4	5	6	7:
	*  first	SOF	(SOL)	PACK_H	data	data	data	data	data
	*		data	data	data	data	data	data	data	data
	*		...
	*		data	data	0	0	0	0	0	0
	*  second	(EOL)	(SOL)	PACK_H	data	data	data	data	data
	*		data	data	data	data	data	data	data	data
	*		...
	*		data	data	0	0	0	0	0	0
	*  ...
	*  last		(EOL)	EOF	0	0	0	0	0	0
	*
	*  Embedded lines are regular lines stored before the first and after
	*  payload lines.
	*/

	words_per_odd_line = (odd_line_bytes + 3) >> 2;
		/* ceil(odd_line_bytes/4); word = 4 bytes */
	words_per_even_line  = (even_line_bytes  + 3) >> 2;
	words_for_first_line = words_per_odd_line + 2 + (hasSOLandEOL ? 1 : 0);
		/* + SOF +packet header + optionally (SOL), but (EOL) is not in the first line */
	words_per_odd_line	+= (1 + (hasSOLandEOL ? 2 : 0));
		/* each non-first line has format header, and optionally (SOL) and (EOL). */
	words_per_even_line += (1 + (hasSOLandEOL ? 2 : 0));

	mem_words_per_odd_line	 = (words_per_odd_line + 7) >> 3;
		/* ceil(words_per_odd_line/8); mem_word = 32 bytes, 8 words */
	mem_words_for_first_line = (words_for_first_line + 7) >> 3;
	mem_words_per_even_line  = (words_per_even_line + 7) >> 3;
	mem_words_for_EOF        = 1; /* last line consisit of the optional (EOL) and EOF */

	mem_words = ((embedded_data_size_words + 7) >> 3) +
		mem_words_for_first_line +
				(((height + 1) >> 1) - 1) * mem_words_per_odd_line +
				/* ceil (height/2) - 1 (first line is calculated separatelly) */
				  (height      >> 1) * mem_words_per_even_line + /* floor(height/2) */
				mem_words_for_EOF;

	*size_mem_words = mem_words; /* ceil(words/8); mem word is 32B = 8words. */
	/* Check if the above is still needed. */

	IA_CSS_LEAVE_ERR(err);
	return err;
}

#if !defined(HAS_NO_INPUT_SYSTEM) && defined(USE_INPUT_SYSTEM_VERSION_2)
enum ia_css_err
ia_css_mipi_frame_enable_check_on_size(const enum ia_css_csi2_port port,
				const unsigned int	size_mem_words)
{
	uint32_t idx;

	enum ia_css_err err = IA_CSS_ERR_RESOURCE_NOT_AVAILABLE;

	OP___assert(port < N_CSI_PORTS);
	OP___assert(size_mem_words != 0);

	for (idx = 0; idx < IA_CSS_MIPI_SIZE_CHECK_MAX_NOF_ENTRIES_PER_PORT &&
		my_css.mipi_sizes_for_check[port][idx] != 0;
		idx++) { /* do nothing */
	}
	if (idx < IA_CSS_MIPI_SIZE_CHECK_MAX_NOF_ENTRIES_PER_PORT) {
		my_css.mipi_sizes_for_check[port][idx] = size_mem_words;
		err = IA_CSS_SUCCESS;
	}

	return err;
}
#endif

void
mipi_init(void)
{
#if defined(USE_INPUT_SYSTEM_VERSION_2) || defined(USE_INPUT_SYSTEM_VERSION_2401)
	unsigned int i;

	for (i = 0; i < N_CSI_PORTS; i++)
		ref_count_mipi_allocation[i] = 0;
#endif
}

enum ia_css_err
calculate_mipi_buff_size(
		struct ia_css_stream_config *stream_cfg,
		unsigned int *size_mem_words)
{
#if !defined(USE_INPUT_SYSTEM_VERSION_2401)
	enum ia_css_err err = IA_CSS_ERR_INTERNAL_ERROR;
	(void)stream_cfg;
	(void)size_mem_words;
#else
	unsigned int width;
	unsigned int height;
	enum ia_css_stream_format format;
	bool pack_raw_pixels;

	unsigned int width_padded;
	unsigned int bits_per_pixel = 0;

	unsigned int even_line_bytes = 0;
	unsigned int odd_line_bytes = 0;

	unsigned int words_per_odd_line = 0;
	unsigned int words_per_even_line = 0;

	unsigned int mem_words_per_even_line = 0;
	unsigned int mem_words_per_odd_line = 0;

	unsigned int mem_words_per_buff_line = 0;
	unsigned int mem_words_per_buff = 0;
	enum ia_css_err err = IA_CSS_SUCCESS;

	/**
#ifndef ISP2401
	 * zhengjie.lu@intel.com
	 *
#endif
	 * NOTE
	 * - In the struct "ia_css_stream_config", there
	 *   are two members: "input_config" and "isys_config".
	 *   Both of them provide the same information, e.g.
	 *   input_res and format.
	 *
	 *   Question here is that: which one shall be used?
	 */
	width = stream_cfg->input_config.input_res.width;
	height = stream_cfg->input_config.input_res.height;
	format = stream_cfg->input_config.format;
	pack_raw_pixels = stream_cfg->pack_raw_pixels;
	/** end of NOTE */

	/**
#ifndef ISP2401
	 * zhengjie.lu@intel.com
	 *
#endif
	 * NOTE
	 * - The following code is derived from the
	 *   existing code "ia_css_mipi_frame_calculate_size()".
	 *
	 *   Question here is: why adding "2 * ISP_VEC_NELEMS"
	 *   to "width_padded", but not making "width_padded"
	 *   aligned with "2 * ISP_VEC_NELEMS"?
	 */
	/* The changes will be reverted as soon as RAW
	 * Buffers are deployed by the 2401 Input System
	 * in the non-continuous use scenario.
	 */
	width_padded = width + (2 * ISP_VEC_NELEMS);
	/** end of NOTE */

	IA_CSS_ENTER("padded_width=%d, height=%d, format=%d\n",
		     width_padded, height, format);

	bits_per_pixel = sh_css_stream_format_2_bits_per_subpixel(format);
	bits_per_pixel =
		(format == IA_CSS_STREAM_FORMAT_RAW_10 && pack_raw_pixels) ? bits_per_pixel : 16;
	if (bits_per_pixel == 0)
		return IA_CSS_ERR_INTERNAL_ERROR;

	odd_line_bytes = (width_padded * bits_per_pixel + 7) >> 3; /* ceil ( bits per line / 8) */

	/* Even lines for YUV420 formats are double in bits_per_pixel. */
	if (format == IA_CSS_STREAM_FORMAT_YUV420_8
		|| format == IA_CSS_STREAM_FORMAT_YUV420_10) {
		even_line_bytes = (width_padded * 2 * bits_per_pixel + 7) >> 3; /* ceil ( bits per line / 8) */
	} else {
		even_line_bytes = odd_line_bytes;
	}

	words_per_odd_line	 = (odd_line_bytes   + 3) >> 2;
		/* ceil(odd_line_bytes/4); word = 4 bytes */
	words_per_even_line  = (even_line_bytes  + 3) >> 2;

	mem_words_per_odd_line	 = (words_per_odd_line + 7) >> 3;
		/* ceil(words_per_odd_line/8); mem_word = 32 bytes, 8 words */
	mem_words_per_even_line  = (words_per_even_line + 7) >> 3;

	mem_words_per_buff_line =
		(mem_words_per_odd_line > mem_words_per_even_line) ? mem_words_per_odd_line : mem_words_per_even_line;
	mem_words_per_buff = mem_words_per_buff_line * height;

	*size_mem_words = mem_words_per_buff;

	IA_CSS_LEAVE_ERR(err);
#endif
	return err;
}

enum ia_css_err
allocate_mipi_frames(struct ia_css_pipe *pipe, struct ia_css_stream_info *info)
{
#if defined(USE_INPUT_SYSTEM_VERSION_2) || defined(USE_INPUT_SYSTEM_VERSION_2401)
	enum ia_css_err err = IA_CSS_ERR_INTERNAL_ERROR;
#ifndef ISP2401
	unsigned int port;
#else
	unsigned int port = 0;
#endif
	struct ia_css_frame_info mipi_intermediate_info;

	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
		"allocate_mipi_frames(%p) enter:\n", pipe);

	assert(pipe != NULL);
	assert(pipe->stream != NULL);
	if ((pipe == NULL) || (pipe->stream == NULL)) {
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
			"allocate_mipi_frames(%p) exit: pipe or stream is null.\n",
			pipe);
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	}

#ifdef USE_INPUT_SYSTEM_VERSION_2401
	if (pipe->stream->config.online) {
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
			"allocate_mipi_frames(%p) exit: no buffers needed for 2401 pipe mode.\n",
			pipe);
		return IA_CSS_SUCCESS;
	}

#endif
#ifndef ISP2401
	if (pipe->stream->config.mode != IA_CSS_INPUT_MODE_BUFFERED_SENSOR) {
#else
	if (!(pipe->stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR ||
		pipe->stream->config.mode == IA_CSS_INPUT_MODE_TPG ||
		pipe->stream->config.mode == IA_CSS_INPUT_MODE_PRBS)) {
#endif
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
			"allocate_mipi_frames(%p) exit: no buffers needed for pipe mode.\n",
			pipe);
		return IA_CSS_SUCCESS; /* AM TODO: Check  */
	}

#ifndef ISP2401
	port = (unsigned int) pipe->stream->config.source.port.port;
	assert(port < N_CSI_PORTS);
	if (port >= N_CSI_PORTS) {
#else
	if (!ia_css_mipi_is_source_port_valid(pipe, &port)) {
#endif
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
			"allocate_mipi_frames(%p) exit: error: port is not correct (port=%d).\n",
			pipe, port);
		return IA_CSS_ERR_INTERNAL_ERROR;
	}

#ifdef USE_INPUT_SYSTEM_VERSION_2401
	err = calculate_mipi_buff_size(
			&(pipe->stream->config),
			&(my_css.mipi_frame_size[port]));
#endif

#if defined(USE_INPUT_SYSTEM_VERSION_2)
	if (ref_count_mipi_allocation[port] != 0) {
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
			"allocate_mipi_frames(%p) exit: already allocated for this port (port=%d).\n",
			pipe, port);
		return IA_CSS_SUCCESS;
	}
#else
	/* 2401 system allows multiple streams to use same physical port. This is not
	 * true for 2400 system. Currently 2401 uses MIPI buffers as a temporary solution.
	 * TODO AM: Once that is changed (removed) this code should be removed as well.
	 * In that case only 2400 related code should remain.
	 */
	if (ref_count_mipi_allocation[port] != 0) {
		ref_count_mipi_allocation[port]++;
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
			"allocate_mipi_frames(%p) leave: nothing to do, already allocated for this port (port=%d).\n",
			pipe, port);
		return IA_CSS_SUCCESS;
	}
#endif

	ref_count_mipi_allocation[port]++;

	/* TODO: Cleaning needed. */
	/* This code needs to modified to allocate the MIPI frames in the correct normal way
	  with an allocate from info, by justin */
	mipi_intermediate_info = pipe->pipe_settings.video.video_binary.internal_frame_info;
	mipi_intermediate_info.res.width = 0;
	mipi_intermediate_info.res.height = 0;
	/* To indicate it is not (yet) valid format. */
	mipi_intermediate_info.format = IA_CSS_FRAME_FORMAT_NUM;
	mipi_intermediate_info.padded_width = 0;
	mipi_intermediate_info.raw_bit_depth = 0;

	/* AM TODO: mipi frames number should come from stream struct. */
	my_css.num_mipi_frames[port] = NUM_MIPI_FRAMES_PER_STREAM;

	/* Incremental allocation (per stream), not for all streams at once. */
	{ /* limit the scope of i,j */
		unsigned i, j;
		for (i = 0; i < my_css.num_mipi_frames[port]; i++) {
			/* free previous frame */
			if (my_css.mipi_frames[port][i]) {
				ia_css_frame_free(my_css.mipi_frames[port][i]);
				my_css.mipi_frames[port][i] = NULL;
			}
			/* check if new frame is needed */
			if (i < my_css.num_mipi_frames[port]) {
				/* allocate new frame */
				err = ia_css_frame_allocate_with_buffer_size(
					&my_css.mipi_frames[port][i],
					my_css.mipi_frame_size[port] * HIVE_ISP_DDR_WORD_BYTES,
					false);
				if (err != IA_CSS_SUCCESS) {
					for (j = 0; j < i; j++) {
						if (my_css.mipi_frames[port][j]) {
							ia_css_frame_free(my_css.mipi_frames[port][j]);
							my_css.mipi_frames[port][j] = NULL;
						}
					}
					ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
						"allocate_mipi_frames(%p, %d) exit: error: allocation failed.\n",
						pipe, port);
					return err;
				}
			}
			if (info->metadata_info.size > 0) {
				/* free previous metadata buffer */
				if (my_css.mipi_metadata[port][i] != NULL) {
					ia_css_metadata_free(my_css.mipi_metadata[port][i]);
					my_css.mipi_metadata[port][i] = NULL;
				}
				/* check if need to allocate a new metadata buffer */
				if (i < my_css.num_mipi_frames[port]) {
					/* allocate new metadata buffer */
					my_css.mipi_metadata[port][i] = ia_css_metadata_allocate(&info->metadata_info);
					if (my_css.mipi_metadata[port][i] == NULL) {
						ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
							"allocate_mipi_metadata(%p, %d) failed.\n",
							pipe, port);
						return err;
					}
				}
			}
		}
	}
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
		"allocate_mipi_frames(%p) exit:\n", pipe);

	return err;
#else
	(void)pipe;
	(void)info;
	return IA_CSS_SUCCESS;
#endif
}

enum ia_css_err
free_mipi_frames(struct ia_css_pipe *pipe)
{
#if defined(USE_INPUT_SYSTEM_VERSION_2) || defined(USE_INPUT_SYSTEM_VERSION_2401)
	enum ia_css_err err = IA_CSS_ERR_INTERNAL_ERROR;
#ifndef ISP2401
	unsigned int port;
#else
	unsigned int port = 0;
#endif
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
		"free_mipi_frames(%p) enter:\n", pipe);

	/* assert(pipe != NULL); TEMP: TODO: Should be assert only. */
	if (pipe != NULL) {
		assert(pipe->stream != NULL);
		if ((pipe == NULL) || (pipe->stream == NULL)) {
			ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
				"free_mipi_frames(%p) exit: error: pipe or stream is null.\n",
				pipe);
			return IA_CSS_ERR_INVALID_ARGUMENTS;
		}

#ifndef ISP2401
		if (pipe->stream->config.mode != IA_CSS_INPUT_MODE_BUFFERED_SENSOR) {
#else
		if (!(pipe->stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR ||
			pipe->stream->config.mode == IA_CSS_INPUT_MODE_TPG ||
			pipe->stream->config.mode == IA_CSS_INPUT_MODE_PRBS)) {
#endif
			ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
				"free_mipi_frames(%p) exit: error: wrong mode.\n",
				pipe);
			return err;
		}

#ifndef ISP2401
		port = (unsigned int) pipe->stream->config.source.port.port;
		assert(port < N_CSI_PORTS);
		if (port >= N_CSI_PORTS) {
#else
		if (!ia_css_mipi_is_source_port_valid(pipe, &port)) {
#endif
			ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
#ifndef ISP2401
				"free_mipi_frames(%p, %d) exit: error: pipe port is not correct.\n",
#else
				"free_mipi_frames(%p) exit: error: pipe port is not correct (port=%d).\n",
#endif
				pipe, port);
			return err;
		}
#ifdef ISP2401

#endif
		if (ref_count_mipi_allocation[port] > 0) {
#if defined(USE_INPUT_SYSTEM_VERSION_2)
			assert(ref_count_mipi_allocation[port] == 1);
			if (ref_count_mipi_allocation[port] != 1) {
				ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
					"free_mipi_frames(%p) exit: error: wrong ref_count (ref_count=%d).\n",
					pipe, ref_count_mipi_allocation[port]);
				return err;
			}
#endif

			ref_count_mipi_allocation[port]--;

			if (ref_count_mipi_allocation[port] == 0) {
				/* no streams are using this buffer, so free it */
				unsigned int i;
				for (i = 0; i < my_css.num_mipi_frames[port]; i++) {
					if (my_css.mipi_frames[port][i] != NULL) {
						ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
							"free_mipi_frames(port=%d, num=%d).\n", port, i);
						ia_css_frame_free(my_css.mipi_frames[port][i]);
						my_css.mipi_frames[port][i] = NULL;
					}
					if (my_css.mipi_metadata[port][i] != NULL) {
						ia_css_metadata_free(my_css.mipi_metadata[port][i]);
						my_css.mipi_metadata[port][i] = NULL;
					}
				}

				ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
					"free_mipi_frames(%p) exit (deallocated).\n", pipe);
			}
#if defined(USE_INPUT_SYSTEM_VERSION_2401)
			else {
				/* 2401 system allows multiple streams to use same physical port. This is not
				 * true for 2400 system. Currently 2401 uses MIPI buffers as a temporary solution.
				 * TODO AM: Once that is changed (removed) this code should be removed as well.
				 * In that case only 2400 related code should remain.
				 */
				ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
					"free_mipi_frames(%p) leave: nothing to do, other streams still use this port (port=%d).\n",
					pipe, port);
			}
#endif
		}
	} else { /* pipe ==NULL */
		/* AM TEMP: free-ing all mipi buffers just like a legacy code. */
		for (port = CSI_PORT0_ID; port < N_CSI_PORTS; port++) {
			unsigned int i;
			for (i = 0; i < my_css.num_mipi_frames[port]; i++) {
				if (my_css.mipi_frames[port][i] != NULL) {
					ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE_PRIVATE,
						"free_mipi_frames(port=%d, num=%d).\n", port, i);
					ia_css_frame_free(my_css.mipi_frames[port][i]);
					my_css.mipi_frames[port][i] = NULL;
				}
				if (my_css.mipi_metadata[port][i] != NULL) {
					ia_css_metadata_free(my_css.mipi_metadata[port][i]);
					my_css.mipi_metadata[port][i] = NULL;
				}
			}
			ref_count_mipi_allocation[port] = 0;
		}
	}
#else
	(void)pipe;
#endif
	return IA_CSS_SUCCESS;
}

enum ia_css_err
send_mipi_frames(struct ia_css_pipe *pipe)
{
#if defined(USE_INPUT_SYSTEM_VERSION_2) || defined(USE_INPUT_SYSTEM_VERSION_2401)
	enum ia_css_err err = IA_CSS_ERR_INTERNAL_ERROR;
	unsigned int i;
#ifndef ISP2401
	unsigned int port;
#else
	unsigned int port = 0;
#endif

	IA_CSS_ENTER_PRIVATE("pipe=%p", pipe);

	assert(pipe != NULL);
	assert(pipe->stream != NULL);
	if (pipe == NULL || pipe->stream == NULL) {
		IA_CSS_ERROR("pipe or stream is null");
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	}

	/* multi stream video needs mipi buffers */
	/* nothing to be done in other cases. */
#ifndef ISP2401
	if (pipe->stream->config.mode != IA_CSS_INPUT_MODE_BUFFERED_SENSOR) {
#else
	if (!(pipe->stream->config.mode == IA_CSS_INPUT_MODE_BUFFERED_SENSOR ||
		pipe->stream->config.mode == IA_CSS_INPUT_MODE_TPG ||
		pipe->stream->config.mode == IA_CSS_INPUT_MODE_PRBS)) {
#endif
		IA_CSS_LOG("nothing to be done for this mode");
		return IA_CSS_SUCCESS;
		/* TODO: AM: maybe this should be returning an error. */
	}

#ifndef ISP2401
	port = (unsigned int) pipe->stream->config.source.port.port;
	assert(port < N_CSI_PORTS);
	if (port >= N_CSI_PORTS) {
		IA_CSS_ERROR("invalid port specified (%d)", port);
#else
	if (!ia_css_mipi_is_source_port_valid(pipe, &port)) {
		IA_CSS_ERROR("send_mipi_frames(%p) exit: invalid port specified (port=%d).\n", pipe, port);
#endif
		return err;
	}

	/* Hand-over the SP-internal mipi buffers */
	for (i = 0; i < my_css.num_mipi_frames[port]; i++) {
		/* Need to include the ofset for port. */
		sh_css_update_host2sp_mipi_frame(port * NUM_MIPI_FRAMES_PER_STREAM + i,
			my_css.mipi_frames[port][i]);
		sh_css_update_host2sp_mipi_metadata(port * NUM_MIPI_FRAMES_PER_STREAM + i,
			my_css.mipi_metadata[port][i]);
	}
	sh_css_update_host2sp_num_mipi_frames(my_css.num_mipi_frames[port]);

	/**********************************
	 * Send an event to inform the SP
	 * that all MIPI frames are passed.
	 **********************************/
	if (!sh_css_sp_is_running()) {
		/* SP is not running. The queues are not valid */
		IA_CSS_ERROR("sp is not running");
		return err;
	}

	ia_css_bufq_enqueue_psys_event(
			IA_CSS_PSYS_SW_EVENT_MIPI_BUFFERS_READY,
			(uint8_t)port,
			(uint8_t)my_css.num_mipi_frames[port],
			0 /* not used */);
	IA_CSS_LEAVE_ERR_PRIVATE(IA_CSS_SUCCESS);
#else
	(void)pipe;
#endif
	return IA_CSS_SUCCESS;
}