xref: /linux/drivers/gpu/drm/amd/display/dc/sspl/dc_spl.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: MIT
2 //
3 // Copyright 2024 Advanced Micro Devices, Inc.
4 
5 #include "dc_spl.h"
6 #include "dc_spl_scl_easf_filters.h"
7 #include "dc_spl_isharp_filters.h"
8 #include "spl_debug.h"
9 
10 /*
11  * Custom float is provided by the parent repo's custom_float.h. It is included
12  * here (after dc_spl.h -> dc_spl_types.h, which fully defines struct
13  * dscl_prog_data) so the opp.h -> transform.h -> dc_spl_types.h chain that
14  * custom_float.h drags in does not re-enter dc_spl_types.h before
15  * dscl_prog_data is complete.
16  */
17 #define spl_div64_s64 div64_s64
18 #define spl_swap      swap
19 #include "custom_float.h"
20 #define spl_custom_float_format custom_float_format
21 #define spl_convert_to_custom_float_format convert_to_custom_float_format
22 
23 #define IDENTITY_RATIO(ratio) (spl_fixpt_u3d19(ratio) == (1 << 19))
24 #define MIN_VIEWPORT_SIZE 12
25 
26 static bool spl_is_yuv420(enum spl_pixel_format format)
27 {
28 	if ((format >= SPL_PIXEL_FORMAT_420BPP8) &&
29 		(format <= SPL_PIXEL_FORMAT_420BPP10))
30 		return true;
31 
32 	return false;
33 }
34 
35 static bool spl_is_yuv422(enum spl_pixel_format format)
36 {
37 	if ((format >= SPL_PIXEL_FORMAT_422BPP8) &&
38 		(format <= SPL_PIXEL_FORMAT_422BPP12))
39 		return true;
40 
41 	return false;
42 }
43 
44 static bool spl_is_rgb8(enum spl_pixel_format format)
45 {
46 	if (format == SPL_PIXEL_FORMAT_ARGB8888)
47 		return true;
48 
49 	return false;
50 }
51 
52 static bool spl_is_video_format(enum spl_pixel_format format)
53 {
54 	if (format >= SPL_PIXEL_FORMAT_VIDEO_BEGIN
55 		&& format <= SPL_PIXEL_FORMAT_VIDEO_END)
56 		return true;
57 	else
58 		return false;
59 }
60 
61 static bool spl_is_subsampled_format(enum spl_pixel_format format)
62 {
63 	if (format >= SPL_PIXEL_FORMAT_SUBSAMPLED_BEGIN
64 		&& format <= SPL_PIXEL_FORMAT_SUBSAMPLED_END)
65 		return true;
66 	else
67 		return false;
68 }
69 
70 static struct spl_rect intersect_rec(const struct spl_rect *r0, const struct spl_rect *r1)
71 {
72 	struct spl_rect rec;
73 	int r0_x_end = r0->x + r0->width;
74 	int r1_x_end = r1->x + r1->width;
75 	int r0_y_end = r0->y + r0->height;
76 	int r1_y_end = r1->y + r1->height;
77 
78 	rec.x = r0->x > r1->x ? r0->x : r1->x;
79 	rec.width = r0_x_end > r1_x_end ? r1_x_end - rec.x : r0_x_end - rec.x;
80 	rec.y = r0->y > r1->y ? r0->y : r1->y;
81 	rec.height = r0_y_end > r1_y_end ? r1_y_end - rec.y : r0_y_end - rec.y;
82 
83 	/* in case that there is no intersection */
84 	if (rec.width < 0 || rec.height < 0)
85 		memset(&rec, 0, sizeof(rec));
86 
87 	return rec;
88 }
89 
90 static struct spl_rect shift_rec(const struct spl_rect *rec_in, int x, int y)
91 {
92 	struct spl_rect rec_out = *rec_in;
93 
94 	rec_out.x += x;
95 	rec_out.y += y;
96 
97 	return rec_out;
98 }
99 
100 static void spl_opp_adjust_rect(struct spl_rect *rec, const struct spl_opp_adjust *adjust)
101 {
102 	if ((rec->x + adjust->x) >= 0)
103 		rec->x += adjust->x;
104 
105 	if ((rec->y + adjust->y) >= 0)
106 		rec->y += adjust->y;
107 
108 	if ((rec->width + adjust->width) >= 1)
109 		rec->width += adjust->width;
110 
111 	if ((rec->height + adjust->height) >= 1)
112 		rec->height += adjust->height;
113 }
114 
115 static struct spl_rect calculate_plane_rec_in_timing_active(
116 		struct spl_in *spl_in,
117 		const struct spl_rect *rec_in)
118 {
119 	/*
120 	 * The following diagram shows an example where we map a 1920x1200
121 	 * desktop to a 2560x1440 timing with a plane rect in the middle
122 	 * of the screen. To map a plane rect from Stream Source to Timing
123 	 * Active space, we first multiply stream scaling ratios (i.e 2304/1920
124 	 * horizontal and 1440/1200 vertical) to the plane's x and y, then
125 	 * we add stream destination offsets (i.e 128 horizontal, 0 vertical).
126 	 * This will give us a plane rect's position in Timing Active. However
127 	 * we have to remove the fractional. The rule is that we find left/right
128 	 * and top/bottom positions and round the value to the adjacent integer.
129 	 *
130 	 * Stream Source Space
131 	 * ------------
132 	 *        __________________________________________________
133 	 *       |Stream Source (1920 x 1200) ^                     |
134 	 *       |                            y                     |
135 	 *       |         <------- w --------|>                    |
136 	 *       |          __________________V                     |
137 	 *       |<-- x -->|Plane//////////////| ^                  |
138 	 *       |         |(pre scale)////////| |                  |
139 	 *       |         |///////////////////| |                  |
140 	 *       |         |///////////////////| h                  |
141 	 *       |         |///////////////////| |                  |
142 	 *       |         |///////////////////| |                  |
143 	 *       |         |///////////////////| V                  |
144 	 *       |                                                  |
145 	 *       |                                                  |
146 	 *       |__________________________________________________|
147 	 *
148 	 *
149 	 * Timing Active Space
150 	 * ---------------------------------
151 	 *
152 	 *       Timing Active (2560 x 1440)
153 	 *        __________________________________________________
154 	 *       |*****|  Stteam Destination (2304 x 1440)    |*****|
155 	 *       |*****|                                      |*****|
156 	 *       |<128>|                                      |*****|
157 	 *       |*****|     __________________               |*****|
158 	 *       |*****|    |Plane/////////////|              |*****|
159 	 *       |*****|    |(post scale)//////|              |*****|
160 	 *       |*****|    |//////////////////|              |*****|
161 	 *       |*****|    |//////////////////|              |*****|
162 	 *       |*****|    |//////////////////|              |*****|
163 	 *       |*****|    |//////////////////|              |*****|
164 	 *       |*****|                                      |*****|
165 	 *       |*****|                                      |*****|
166 	 *       |*****|                                      |*****|
167 	 *       |*****|______________________________________|*****|
168 	 *
169 	 * So the resulting formulas are shown below:
170 	 *
171 	 * recout_x = 128 + round(plane_x * 2304 / 1920)
172 	 * recout_w = 128 + round((plane_x + plane_w) * 2304 / 1920) - recout_x
173 	 * recout_y = 0 + round(plane_y * 1440 / 1200)
174 	 * recout_h = 0 + round((plane_y + plane_h) * 1440 / 1200) - recout_y
175 	 *
176 	 * NOTE: fixed point division is not error free. To reduce errors
177 	 * introduced by fixed point division, we divide only after
178 	 * multiplication is complete.
179 	 */
180 	const struct spl_rect *stream_src = &spl_in->basic_out.src_rect;
181 	const struct spl_rect *stream_dst = &spl_in->basic_out.dst_rect;
182 	struct spl_rect rec_out = {0};
183 	struct spl_fixed31_32 temp;
184 
185 
186 	temp = spl_fixpt_from_fraction(
187 			rec_in->x * (long long)stream_dst->width,
188 			stream_src->width);
189 	rec_out.x = stream_dst->x + spl_fixpt_round(temp);
190 
191 	temp = spl_fixpt_from_fraction(
192 			(rec_in->x + rec_in->width) * (long long)stream_dst->width,
193 			stream_src->width);
194 	rec_out.width = stream_dst->x + spl_fixpt_round(temp) - rec_out.x;
195 
196 	temp = spl_fixpt_from_fraction(
197 			rec_in->y * (long long)stream_dst->height,
198 			stream_src->height);
199 	rec_out.y = stream_dst->y + spl_fixpt_round(temp);
200 
201 	temp = spl_fixpt_from_fraction(
202 			(rec_in->y + rec_in->height) * (long long)stream_dst->height,
203 			stream_src->height);
204 	rec_out.height = stream_dst->y + spl_fixpt_round(temp) - rec_out.y;
205 
206 	return rec_out;
207 }
208 
209 static struct spl_rect calculate_mpc_slice_in_timing_active(
210 		struct spl_in *spl_in,
211 		struct spl_rect *plane_clip_rec)
212 {
213 	bool use_recout_width_aligned =
214 		spl_in->basic_in.num_h_slices_recout_width_align.use_recout_width_aligned;
215 	int mpc_slice_count =
216 		spl_in->basic_in.num_h_slices_recout_width_align.num_slices_recout_width.mpc_num_h_slices;
217 	int recout_width_align =
218 		spl_in->basic_in.num_h_slices_recout_width_align.num_slices_recout_width.mpc_recout_width_align;
219 	int mpc_slice_idx = spl_in->basic_in.mpc_h_slice_index;
220 	int epimo = mpc_slice_count - plane_clip_rec->width % mpc_slice_count - 1;
221 	struct spl_rect mpc_rec;
222 
223 	if (spl_in->basic_in.custom_width != 0) {
224 		mpc_rec.width = spl_in->basic_in.custom_width;
225 		mpc_rec.x = spl_in->basic_in.custom_x;
226 		mpc_rec.height = plane_clip_rec->height;
227 		mpc_rec.y = plane_clip_rec->y;
228 	} else if (use_recout_width_aligned) {
229 		mpc_rec.width = recout_width_align;
230 		if ((mpc_rec.width * (mpc_slice_idx + 1)) > plane_clip_rec->width) {
231 			mpc_rec.width = plane_clip_rec->width % recout_width_align;
232 			mpc_rec.x = plane_clip_rec->x + recout_width_align * mpc_slice_idx;
233 		} else
234 			mpc_rec.x = plane_clip_rec->x + mpc_rec.width * mpc_slice_idx;
235 		mpc_rec.height = plane_clip_rec->height;
236 		mpc_rec.y = plane_clip_rec->y;
237 
238 	} else {
239 		mpc_rec.width = plane_clip_rec->width / mpc_slice_count;
240 		mpc_rec.x = plane_clip_rec->x + mpc_rec.width * mpc_slice_idx;
241 		mpc_rec.height = plane_clip_rec->height;
242 		mpc_rec.y = plane_clip_rec->y;
243 	}
244 	SPL_ASSERT(mpc_slice_count == 1 ||
245 			spl_in->basic_out.view_format != SPL_VIEW_3D_SIDE_BY_SIDE ||
246 			mpc_rec.width % 2 == 0);
247 
248 	/* extra pixels in the division remainder need to go to pipes after
249 	 * the extra pixel index minus one(epimo) defined here as:
250 	 */
251 	if ((use_recout_width_aligned == false) &&
252 		mpc_slice_idx > epimo && spl_in->basic_in.custom_width == 0) {
253 		mpc_rec.x += mpc_slice_idx - epimo - 1;
254 		mpc_rec.width += 1;
255 	}
256 
257 	if (spl_in->basic_out.view_format == SPL_VIEW_3D_TOP_AND_BOTTOM) {
258 		SPL_ASSERT(mpc_rec.height % 2 == 0);
259 		mpc_rec.height /= 2;
260 	}
261 	return mpc_rec;
262 }
263 
264 static struct spl_rect calculate_odm_slice_in_timing_active(struct spl_in *spl_in)
265 {
266 	int odm_slice_count = spl_in->basic_out.odm_combine_factor;
267 	int odm_slice_idx = spl_in->odm_slice_index;
268 	bool is_last_odm_slice = (odm_slice_idx + 1) == odm_slice_count;
269 	int h_active = spl_in->basic_out.output_size.width;
270 	int v_active = spl_in->basic_out.output_size.height;
271 	int odm_slice_width;
272 	struct spl_rect odm_rec;
273 
274 	if (spl_in->basic_out.odm_combine_factor > 0) {
275 		odm_slice_width = h_active / odm_slice_count;
276 		/*
277 		 * deprecated, caller must pass in odm slice rect i.e OPP input
278 		 * rect in timing active for the new interface.
279 		 */
280 		if (spl_in->basic_out.use_two_pixels_per_container && (odm_slice_width % 2))
281 			odm_slice_width++;
282 
283 		odm_rec.x = odm_slice_width * odm_slice_idx;
284 		odm_rec.width = is_last_odm_slice ?
285 			/* last slice width is the reminder of h_active */
286 			h_active - odm_slice_width * (odm_slice_count - 1) :
287 			/* odm slice width is the floor of h_active / count */
288 			odm_slice_width;
289 		odm_rec.y = 0;
290 		odm_rec.height = v_active;
291 
292 		return odm_rec;
293 	}
294 
295 	return spl_in->basic_out.odm_slice_rect;
296 }
297 
298 static void spl_calculate_recout(struct spl_in *spl_in, struct spl_scratch *spl_scratch, struct spl_out *spl_out)
299 {
300 	/*
301 	 * A plane clip represents the desired plane size and position in Stream
302 	 * Source Space. Stream Source is the destination where all planes are
303 	 * blended (i.e. positioned, scaled and overlaid). It is a canvas where
304 	 * all planes associated with the current stream are drawn together.
305 	 * After Stream Source is completed, we will further scale and
306 	 * reposition the entire canvas of the stream source to Stream
307 	 * Destination in Timing Active Space. This could be due to display
308 	 * overscan adjustment where we will need to rescale and reposition all
309 	 * the planes so they can fit into a TV with overscan or downscale
310 	 * upscale features such as GPU scaling or VSR.
311 	 *
312 	 * This two step blending is a virtual procedure in software. In
313 	 * hardware there is no such thing as Stream Source. all planes are
314 	 * blended once in Timing Active Space. Software virtualizes a Stream
315 	 * Source space to decouple the math complicity so scaling param
316 	 * calculation focuses on one step at a time.
317 	 *
318 	 * In the following two diagrams, user applied 10% overscan adjustment
319 	 * so the Stream Source needs to be scaled down a little before mapping
320 	 * to Timing Active Space. As a result the Plane Clip is also scaled
321 	 * down by the same ratio, Plane Clip position (i.e. x and y) with
322 	 * respect to Stream Source is also scaled down. To map it in Timing
323 	 * Active Space additional x and y offsets from Stream Destination are
324 	 * added to Plane Clip as well.
325 	 *
326 	 * Stream Source Space
327 	 * ------------
328 	 *        __________________________________________________
329 	 *       |Stream Source (3840 x 2160) ^                     |
330 	 *       |                            y                     |
331 	 *       |                            |                     |
332 	 *       |          __________________V                     |
333 	 *       |<-- x -->|Plane Clip/////////|                    |
334 	 *       |         |(pre scale)////////|                    |
335 	 *       |         |///////////////////|                    |
336 	 *       |         |///////////////////|                    |
337 	 *       |         |///////////////////|                    |
338 	 *       |         |///////////////////|                    |
339 	 *       |         |///////////////////|                    |
340 	 *       |                                                  |
341 	 *       |                                                  |
342 	 *       |__________________________________________________|
343 	 *
344 	 *
345 	 * Timing Active Space (3840 x 2160)
346 	 * ---------------------------------
347 	 *
348 	 *       Timing Active
349 	 *        __________________________________________________
350 	 *       | y_____________________________________________   |
351 	 *       |x |Stream Destination (3456 x 1944)            |  |
352 	 *       |  |                                            |  |
353 	 *       |  |        __________________                  |  |
354 	 *       |  |       |Plane Clip////////|                 |  |
355 	 *       |  |       |(post scale)//////|                 |  |
356 	 *       |  |       |//////////////////|                 |  |
357 	 *       |  |       |//////////////////|                 |  |
358 	 *       |  |       |//////////////////|                 |  |
359 	 *       |  |       |//////////////////|                 |  |
360 	 *       |  |                                            |  |
361 	 *       |  |                                            |  |
362 	 *       |  |____________________________________________|  |
363 	 *       |__________________________________________________|
364 	 *
365 	 *
366 	 * In Timing Active Space a plane clip could be further sliced into
367 	 * pieces called MPC slices. Each Pipe Context is responsible for
368 	 * processing only one MPC slice so the plane processing workload can be
369 	 * distributed to multiple DPP Pipes. MPC slices could be blended
370 	 * together to a single ODM slice. Each ODM slice is responsible for
371 	 * processing a portion of Timing Active divided horizontally so the
372 	 * output pixel processing workload can be distributed to multiple OPP
373 	 * pipes. All ODM slices are mapped together in ODM block so all MPC
374 	 * slices belong to different ODM slices could be pieced together to
375 	 * form a single image in Timing Active. MPC slices must belong to
376 	 * single ODM slice. If an MPC slice goes across ODM slice boundary, it
377 	 * needs to be divided into two MPC slices one for each ODM slice.
378 	 *
379 	 * In the following diagram the output pixel processing workload is
380 	 * divided horizontally into two ODM slices one for each OPP blend tree.
381 	 * OPP0 blend tree is responsible for processing left half of Timing
382 	 * Active, while OPP2 blend tree is responsible for processing right
383 	 * half.
384 	 *
385 	 * The plane has two MPC slices. However since the right MPC slice goes
386 	 * across ODM boundary, two DPP pipes are needed one for each OPP blend
387 	 * tree. (i.e. DPP1 for OPP0 blend tree and DPP2 for OPP2 blend tree).
388 	 *
389 	 * Assuming that we have a Pipe Context associated with OPP0 and DPP1
390 	 * working on processing the plane in the diagram. We want to know the
391 	 * width and height of the shaded rectangle and its relative position
392 	 * with respect to the ODM slice0. This is called the recout of the pipe
393 	 * context.
394 	 *
395 	 * Planes can be at arbitrary size and position and there could be an
396 	 * arbitrary number of MPC and ODM slices. The algorithm needs to take
397 	 * all scenarios into account.
398 	 *
399 	 * Timing Active Space (3840 x 2160)
400 	 * ---------------------------------
401 	 *
402 	 *       Timing Active
403 	 *        __________________________________________________
404 	 *       |OPP0(ODM slice0)^        |OPP2(ODM slice1)        |
405 	 *       |                y        |                        |
406 	 *       |                |  <- w ->                        |
407 	 *       |           _____V________|____                    |
408 	 *       |          |DPP0 ^  |DPP1 |DPP2|                   |
409 	 *       |<------ x |-----|->|/////|    |                   |
410 	 *       |          |     |  |/////|    |                   |
411 	 *       |          |     h  |/////|    |                   |
412 	 *       |          |     |  |/////|    |                   |
413 	 *       |          |_____V__|/////|____|                   |
414 	 *       |                         |                        |
415 	 *       |                         |                        |
416 	 *       |                         |                        |
417 	 *       |_________________________|________________________|
418 	 *
419 	 *
420 	 */
421 	struct spl_rect plane_clip;
422 	struct spl_rect mpc_slice_of_plane_clip;
423 	struct spl_rect odm_slice;
424 	struct spl_rect overlapping_area;
425 
426 	plane_clip = calculate_plane_rec_in_timing_active(spl_in,
427 			&spl_in->basic_in.clip_rect);
428 	/* guard plane clip from drawing beyond stream dst here */
429 	plane_clip = intersect_rec(&plane_clip,
430 				&spl_in->basic_out.dst_rect);
431 	mpc_slice_of_plane_clip = calculate_mpc_slice_in_timing_active(
432 			spl_in, &plane_clip);
433 	odm_slice = calculate_odm_slice_in_timing_active(spl_in);
434 	overlapping_area = intersect_rec(&mpc_slice_of_plane_clip, &odm_slice);
435 
436 	if (overlapping_area.height > 0 &&
437 			overlapping_area.width > 0) {
438 		/* shift the overlapping area so it is with respect to current
439 		 * ODM slice's position
440 		 */
441 		spl_scratch->scl_data.recout = shift_rec(
442 				&overlapping_area,
443 				-odm_slice.x, -odm_slice.y);
444 		spl_scratch->scl_data.recout.height -=
445 			spl_in->debug.visual_confirm_base_offset;
446 		spl_scratch->scl_data.recout.height -=
447 			spl_in->debug.visual_confirm_dpp_offset;
448 	} else
449 		/* if there is no overlap, zero recout */
450 		memset(&spl_scratch->scl_data.recout, 0,
451 				sizeof(struct spl_rect));
452 }
453 
454 /* Calculate scaling ratios */
455 static void spl_calculate_scaling_ratios(struct spl_in *spl_in,
456 		struct spl_scratch *spl_scratch,
457 		struct spl_out *spl_out)
458 {
459 	const int in_w = spl_in->basic_out.src_rect.width;
460 	const int in_h = spl_in->basic_out.src_rect.height;
461 	const int out_w = spl_in->basic_out.dst_rect.width;
462 	const int out_h = spl_in->basic_out.dst_rect.height;
463 	struct spl_rect surf_src = spl_in->basic_in.src_rect;
464 
465 	/*Swap surf_src height and width since scaling ratios are in recout rotation*/
466 	if (spl_in->basic_in.rotation == SPL_ROTATION_ANGLE_90 ||
467 		spl_in->basic_in.rotation == SPL_ROTATION_ANGLE_270)
468 		spl_swap(surf_src.height, surf_src.width);
469 
470 	spl_scratch->scl_data.ratios.horz = spl_fixpt_from_fraction(
471 					surf_src.width,
472 					spl_in->basic_in.dst_rect.width);
473 	spl_scratch->scl_data.ratios.vert = spl_fixpt_from_fraction(
474 					surf_src.height,
475 					spl_in->basic_in.dst_rect.height);
476 
477 	if (spl_in->basic_out.view_format == SPL_VIEW_3D_SIDE_BY_SIDE)
478 		spl_scratch->scl_data.ratios.horz.value *= 2;
479 	else if (spl_in->basic_out.view_format == SPL_VIEW_3D_TOP_AND_BOTTOM)
480 		spl_scratch->scl_data.ratios.vert.value *= 2;
481 
482 	spl_scratch->scl_data.ratios.vert.value = spl_div64_s64(
483 		spl_scratch->scl_data.ratios.vert.value * in_h, out_h);
484 	spl_scratch->scl_data.ratios.horz.value = spl_div64_s64(
485 		spl_scratch->scl_data.ratios.horz.value * in_w, out_w);
486 
487 	spl_scratch->scl_data.ratios.horz_c = spl_scratch->scl_data.ratios.horz;
488 	spl_scratch->scl_data.ratios.vert_c = spl_scratch->scl_data.ratios.vert;
489 
490 	if (spl_is_yuv420(spl_in->basic_in.format)) {
491 		spl_scratch->scl_data.ratios.horz_c.value /= 2;
492 		spl_scratch->scl_data.ratios.vert_c.value /= 2;
493 	} else if (spl_is_yuv422(spl_in->basic_in.format)) {
494 		if (spl_in->basic_in.rotation == SPL_ROTATION_ANGLE_90 ||
495 			spl_in->basic_in.rotation == SPL_ROTATION_ANGLE_270)
496 			spl_scratch->scl_data.ratios.vert_c.value /= 2;
497 		else
498 			spl_scratch->scl_data.ratios.horz_c.value /= 2;
499 	}
500 	spl_scratch->scl_data.ratios.horz = spl_fixpt_truncate(
501 			spl_scratch->scl_data.ratios.horz, 19);
502 	spl_scratch->scl_data.ratios.vert = spl_fixpt_truncate(
503 			spl_scratch->scl_data.ratios.vert, 19);
504 	spl_scratch->scl_data.ratios.horz_c = spl_fixpt_truncate(
505 			spl_scratch->scl_data.ratios.horz_c, 19);
506 	spl_scratch->scl_data.ratios.vert_c = spl_fixpt_truncate(
507 			spl_scratch->scl_data.ratios.vert_c, 19);
508 
509 	/*
510 	 * Coefficient table and some registers are different based on ratio
511 	 * that is output/input.  Currently we calculate input/output
512 	 * Store 1/ratio in recip_ratio for those lookups
513 	 */
514 	spl_scratch->scl_data.recip_ratios.horz = spl_fixpt_recip(
515 			spl_scratch->scl_data.ratios.horz);
516 	spl_scratch->scl_data.recip_ratios.vert = spl_fixpt_recip(
517 			spl_scratch->scl_data.ratios.vert);
518 	spl_scratch->scl_data.recip_ratios.horz_c = spl_fixpt_recip(
519 			spl_scratch->scl_data.ratios.horz_c);
520 	spl_scratch->scl_data.recip_ratios.vert_c = spl_fixpt_recip(
521 			spl_scratch->scl_data.ratios.vert_c);
522 }
523 
524 /* Calculate Viewport size */
525 static void spl_calculate_viewport_size(struct spl_in *spl_in, struct spl_scratch *spl_scratch)
526 {
527 	spl_scratch->scl_data.viewport.width = spl_fixpt_ceil(spl_fixpt_mul_int(spl_scratch->scl_data.ratios.horz,
528 							spl_scratch->scl_data.recout.width));
529 	spl_scratch->scl_data.viewport.height = spl_fixpt_ceil(spl_fixpt_mul_int(spl_scratch->scl_data.ratios.vert,
530 							spl_scratch->scl_data.recout.height));
531 	spl_scratch->scl_data.viewport_c.width = spl_fixpt_ceil(spl_fixpt_mul_int(spl_scratch->scl_data.ratios.horz_c,
532 						spl_scratch->scl_data.recout.width));
533 	spl_scratch->scl_data.viewport_c.height = spl_fixpt_ceil(spl_fixpt_mul_int(spl_scratch->scl_data.ratios.vert_c,
534 						spl_scratch->scl_data.recout.height));
535 	if (spl_in->basic_in.rotation == SPL_ROTATION_ANGLE_90 ||
536 			spl_in->basic_in.rotation == SPL_ROTATION_ANGLE_270) {
537 		spl_swap(spl_scratch->scl_data.viewport.width, spl_scratch->scl_data.viewport.height);
538 		spl_swap(spl_scratch->scl_data.viewport_c.width, spl_scratch->scl_data.viewport_c.height);
539 	}
540 }
541 
542 static void spl_get_vp_scan_direction(enum spl_rotation_angle rotation,
543 			   bool horizontal_mirror,
544 			   bool *orthogonal_rotation,
545 			   bool *flip_vert_scan_dir,
546 			   bool *flip_horz_scan_dir)
547 {
548 	*orthogonal_rotation = false;
549 	*flip_vert_scan_dir = false;
550 	*flip_horz_scan_dir = false;
551 	if (rotation == SPL_ROTATION_ANGLE_180) {
552 		*flip_vert_scan_dir = true;
553 		*flip_horz_scan_dir = true;
554 	} else if (rotation == SPL_ROTATION_ANGLE_90) {
555 		*orthogonal_rotation = true;
556 		*flip_horz_scan_dir = true;
557 	} else if (rotation == SPL_ROTATION_ANGLE_270) {
558 		*orthogonal_rotation = true;
559 		*flip_vert_scan_dir = true;
560 	}
561 
562 	if (horizontal_mirror)
563 		*flip_horz_scan_dir = !*flip_horz_scan_dir;
564 }
565 
566 /*
567  * We completely calculate vp offset, size and inits here based entirely on scaling
568  * ratios and recout for pixel perfect pipe combine.
569  */
570 static void spl_calculate_init_and_vp(bool flip_scan_dir,
571 				int recout_offset_within_recout_full,
572 				int recout_size,
573 				int src_size,
574 				int taps,
575 				struct spl_fixed31_32 ratio,
576 				struct spl_fixed31_32 init_adj,
577 				struct spl_fixed31_32 *init,
578 				int *vp_offset,
579 				int *vp_size)
580 {
581 	struct spl_fixed31_32 temp;
582 	int int_part;
583 
584 	/*
585 	 * First of the taps starts sampling pixel number <init_int_part> corresponding to recout
586 	 * pixel 1. Next recout pixel samples int part of <init + scaling ratio> and so on.
587 	 * All following calculations are based on this logic.
588 	 *
589 	 * Init calculated according to formula:
590 	 * init = (scaling_ratio + number_of_taps + 1) / 2
591 	 * init_bot = init + scaling_ratio
592 	 * to get pixel perfect combine add the fraction from calculating vp offset
593 	 */
594 	temp = spl_fixpt_mul_int(ratio, recout_offset_within_recout_full);
595 	*vp_offset = spl_fixpt_floor(temp);
596 	temp.value &= 0xffffffff;
597 	*init = spl_fixpt_add(spl_fixpt_div_int(spl_fixpt_add_int(ratio, taps + 1), 2), temp);
598 	*init = spl_fixpt_add(*init, init_adj);
599 	*init = spl_fixpt_truncate(*init, 19);
600 
601 	/*
602 	 * If viewport has non 0 offset and there are more taps than covered by init then
603 	 * we should decrease the offset and increase init so we are never sampling
604 	 * outside of viewport.
605 	 */
606 	int_part = spl_fixpt_floor(*init);
607 	if (int_part < taps) {
608 		int_part = taps - int_part;
609 		if (int_part > *vp_offset)
610 			int_part = *vp_offset;
611 		*vp_offset -= int_part;
612 		*init = spl_fixpt_add_int(*init, int_part);
613 	}
614 	/*
615 	 * If taps are sampling outside of viewport at end of recout and there are more pixels
616 	 * available in the surface we should increase the viewport size, regardless set vp to
617 	 * only what is used.
618 	 */
619 	temp = spl_fixpt_add(*init, spl_fixpt_mul_int(ratio, recout_size - 1));
620 	*vp_size = spl_fixpt_floor(temp);
621 	if (*vp_size + *vp_offset > src_size)
622 		*vp_size = src_size - *vp_offset;
623 
624 	/* We did all the math assuming we are scanning same direction as display does,
625 	 * however mirror/rotation changes how vp scans vs how it is offset. If scan direction
626 	 * is flipped we simply need to calculate offset from the other side of plane.
627 	 * Note that outside of viewport all scaling hardware works in recout space.
628 	 */
629 	if (flip_scan_dir)
630 		*vp_offset = src_size - *vp_offset - *vp_size;
631 }
632 
633 /*Calculate inits and viewport */
634 static void spl_calculate_inits_and_viewports(struct spl_in *spl_in,
635 		struct spl_scratch *spl_scratch)
636 {
637 	struct spl_rect src = spl_in->basic_in.src_rect;
638 	struct spl_rect recout_dst_in_active_timing;
639 	struct spl_rect recout_clip_in_active_timing;
640 	struct spl_rect recout_clip_in_recout_dst;
641 	struct spl_rect overlap_in_active_timing;
642 	struct spl_rect odm_slice = calculate_odm_slice_in_timing_active(spl_in);
643 	int vp_hc_div = spl_is_subsampled_format(spl_in->basic_in.format) ? 2 : 1;
644 	int vp_vc_div = spl_is_yuv420(spl_in->basic_in.format) ? 2 : 1;
645 	bool orthogonal_rotation, flip_vert_scan_dir, flip_horz_scan_dir;
646 	struct spl_fixed31_32 init_adj_h = spl_fixpt_zero;
647 	struct spl_fixed31_32 init_adj_v = spl_fixpt_zero;
648 
649 	recout_clip_in_active_timing = shift_rec(
650 			&spl_scratch->scl_data.recout, odm_slice.x, odm_slice.y);
651 	recout_dst_in_active_timing = calculate_plane_rec_in_timing_active(
652 			spl_in, &spl_in->basic_in.dst_rect);
653 	overlap_in_active_timing = intersect_rec(&recout_clip_in_active_timing,
654 			&recout_dst_in_active_timing);
655 	if (overlap_in_active_timing.width > 0 &&
656 			overlap_in_active_timing.height > 0)
657 		recout_clip_in_recout_dst = shift_rec(&overlap_in_active_timing,
658 				-recout_dst_in_active_timing.x,
659 				-recout_dst_in_active_timing.y);
660 	else
661 		memset(&recout_clip_in_recout_dst, 0, sizeof(struct spl_rect));
662 	/*
663 	 * Work in recout rotation since that requires less transformations
664 	 */
665 	spl_get_vp_scan_direction(
666 			spl_in->basic_in.rotation,
667 			spl_in->basic_in.horizontal_mirror,
668 			&orthogonal_rotation,
669 			&flip_vert_scan_dir,
670 			&flip_horz_scan_dir);
671 
672 	if (spl_is_subsampled_format(spl_in->basic_in.format)) {
673 		/* this gives the direction of the cositing (negative will move
674 		 * left, right otherwise)
675 		 */
676 		int h_sign = flip_horz_scan_dir ? -1 : 1;
677 		int v_sign = flip_vert_scan_dir ? -1 : 1;
678 
679 		switch (spl_in->basic_in.cositing) {
680 		case CHROMA_COSITING_TOPLEFT:
681 			init_adj_h = spl_fixpt_from_fraction(h_sign, 4);
682 			init_adj_v = spl_fixpt_from_fraction(v_sign, 4);
683 			break;
684 		case CHROMA_COSITING_LEFT:
685 			init_adj_h = spl_fixpt_from_fraction(h_sign, 4);
686 			init_adj_v = spl_fixpt_zero;
687 			break;
688 		case CHROMA_COSITING_NONE:
689 		default:
690 			init_adj_h = spl_fixpt_zero;
691 			init_adj_v = spl_fixpt_zero;
692 			break;
693 		}
694 	}
695 
696 	if (orthogonal_rotation) {
697 		spl_swap(src.width, src.height);
698 		spl_swap(flip_vert_scan_dir, flip_horz_scan_dir);
699 		spl_swap(vp_hc_div, vp_vc_div);
700 		spl_swap(init_adj_h, init_adj_v);
701 	}
702 
703 	spl_calculate_init_and_vp(
704 			flip_horz_scan_dir,
705 			recout_clip_in_recout_dst.x,
706 			spl_scratch->scl_data.recout.width,
707 			src.width,
708 			spl_scratch->scl_data.taps.h_taps,
709 			spl_scratch->scl_data.ratios.horz,
710 			spl_fixpt_zero,
711 			&spl_scratch->scl_data.inits.h,
712 			&spl_scratch->scl_data.viewport.x,
713 			&spl_scratch->scl_data.viewport.width);
714 	spl_calculate_init_and_vp(
715 			flip_horz_scan_dir,
716 			recout_clip_in_recout_dst.x,
717 			spl_scratch->scl_data.recout.width,
718 			src.width / vp_hc_div,
719 			spl_scratch->scl_data.taps.h_taps_c,
720 			spl_scratch->scl_data.ratios.horz_c,
721 			init_adj_h,
722 			&spl_scratch->scl_data.inits.h_c,
723 			&spl_scratch->scl_data.viewport_c.x,
724 			&spl_scratch->scl_data.viewport_c.width);
725 	spl_calculate_init_and_vp(
726 			flip_vert_scan_dir,
727 			recout_clip_in_recout_dst.y,
728 			spl_scratch->scl_data.recout.height,
729 			src.height,
730 			spl_scratch->scl_data.taps.v_taps,
731 			spl_scratch->scl_data.ratios.vert,
732 			spl_fixpt_zero,
733 			&spl_scratch->scl_data.inits.v,
734 			&spl_scratch->scl_data.viewport.y,
735 			&spl_scratch->scl_data.viewport.height);
736 	spl_calculate_init_and_vp(
737 			flip_vert_scan_dir,
738 			recout_clip_in_recout_dst.y,
739 			spl_scratch->scl_data.recout.height,
740 			src.height / vp_vc_div,
741 			spl_scratch->scl_data.taps.v_taps_c,
742 			spl_scratch->scl_data.ratios.vert_c,
743 			init_adj_v,
744 			&spl_scratch->scl_data.inits.v_c,
745 			&spl_scratch->scl_data.viewport_c.y,
746 			&spl_scratch->scl_data.viewport_c.height);
747 	if (orthogonal_rotation) {
748 		spl_swap(spl_scratch->scl_data.viewport.x, spl_scratch->scl_data.viewport.y);
749 		spl_swap(spl_scratch->scl_data.viewport.width, spl_scratch->scl_data.viewport.height);
750 		spl_swap(spl_scratch->scl_data.viewport_c.x, spl_scratch->scl_data.viewport_c.y);
751 		spl_swap(spl_scratch->scl_data.viewport_c.width, spl_scratch->scl_data.viewport_c.height);
752 		spl_swap(vp_hc_div, vp_vc_div);
753 	}
754 	spl_scratch->scl_data.viewport.x += src.x;
755 	spl_scratch->scl_data.viewport.y += src.y;
756 	SPL_ASSERT(src.x % vp_hc_div == 0 && src.y % vp_vc_div == 0);
757 	spl_scratch->scl_data.viewport_c.x += src.x / vp_hc_div;
758 	spl_scratch->scl_data.viewport_c.y += src.y / vp_vc_div;
759 }
760 
761 static void spl_handle_3d_recout(struct spl_in *spl_in, struct spl_rect *recout)
762 {
763 	/*
764 	 * Handle side by side and top bottom 3d recout offsets after vp calculation
765 	 * since 3d is special and needs to calculate vp as if there is no recout offset
766 	 * This may break with rotation, good thing we aren't mixing hw rotation and 3d
767 	 */
768 	if (spl_in->basic_in.mpc_h_slice_index) {
769 		SPL_ASSERT(spl_in->basic_in.rotation == SPL_ROTATION_ANGLE_0 ||
770 			(spl_in->basic_out.view_format != SPL_VIEW_3D_TOP_AND_BOTTOM &&
771 					spl_in->basic_out.view_format != SPL_VIEW_3D_SIDE_BY_SIDE));
772 		if (spl_in->basic_out.view_format == SPL_VIEW_3D_TOP_AND_BOTTOM)
773 			recout->y += recout->height;
774 		else if (spl_in->basic_out.view_format == SPL_VIEW_3D_SIDE_BY_SIDE)
775 			recout->x += recout->width;
776 	}
777 }
778 
779 static void spl_clamp_viewport(struct spl_rect *viewport, int min_viewport_size)
780 {
781 	if (min_viewport_size == 0)
782 		min_viewport_size = MIN_VIEWPORT_SIZE;
783 	/* Clamp minimum viewport size */
784 	if (viewport->height < min_viewport_size)
785 		viewport->height = min_viewport_size;
786 	if (viewport->width < min_viewport_size)
787 		viewport->width = min_viewport_size;
788 }
789 
790 static enum scl_mode spl_get_dscl_mode(const struct spl_in *spl_in,
791 				const struct spl_scaler_data *data,
792 				bool enable_isharp, bool enable_easf)
793 {
794 	(void)enable_easf;
795 	const long long one = spl_fixpt_one.value;
796 	enum spl_pixel_format pixel_format = spl_in->basic_in.format;
797 
798 	/* Bypass if ratio is 1:1 with no ISHARP or force scale on */
799 	if (data->ratios.horz.value == one
800 			&& data->ratios.vert.value == one
801 			&& data->ratios.horz_c.value == one
802 			&& data->ratios.vert_c.value == one
803 			&& !spl_in->basic_out.always_scale
804 			&& !enable_isharp)
805 		return SCL_MODE_SCALING_444_BYPASS;
806 
807 	if (!spl_is_subsampled_format(pixel_format)) {
808 		if (spl_is_video_format(pixel_format))
809 			return SCL_MODE_SCALING_444_YCBCR_ENABLE;
810 		else
811 			return SCL_MODE_SCALING_444_RGB_ENABLE;
812 	}
813 
814 	/*
815 	 * Bypass YUV if Y is 1:1 with no ISHARP
816 	 * Do not bypass UV at 1:1 for cositing to be applied
817 	 */
818 	if (!enable_isharp) {
819 		if (data->ratios.horz.value == one && data->ratios.vert.value == one && !spl_in->basic_out.always_scale)
820 			return SCL_MODE_SCALING_420_LUMA_BYPASS;
821 	}
822 
823 	return SCL_MODE_SCALING_420_YCBCR_ENABLE;
824 }
825 
826 static void spl_choose_lls_policy(enum spl_pixel_format format,
827 	enum linear_light_scaling *lls_pref)
828 {
829 	if (spl_is_subsampled_format(format))
830 		*lls_pref = LLS_PREF_NO;
831 	else /* RGB or YUV444 */
832 		*lls_pref = LLS_PREF_YES;
833 }
834 
835 /* Enable EASF ?*/
836 static bool enable_easf(struct spl_in *spl_in, struct spl_scratch *spl_scratch)
837 {
838 	int vratio = 0;
839 	int hratio = 0;
840 	bool skip_easf = false;
841 
842 	if (spl_in->disable_easf)
843 		skip_easf = true;
844 
845 	vratio = spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert);
846 	hratio = spl_fixpt_ceil(spl_scratch->scl_data.ratios.horz);
847 
848 	/*
849 	 * No EASF support for downscaling > 2:1
850 	 * EASF support for upscaling or downscaling up to 2:1
851 	 */
852 	if ((vratio > 2) || (hratio > 2))
853 		skip_easf = true;
854 
855 	/*
856 	 * If lls_pref is LLS_PREF_DONT_CARE, then use pixel format
857 	 *  to determine whether to use LINEAR or NONLINEAR scaling
858 	 */
859 	if (spl_in->lls_pref == LLS_PREF_DONT_CARE)
860 		spl_choose_lls_policy(spl_in->basic_in.format,
861 			&spl_in->lls_pref);
862 
863 	/* Check for linear scaling or EASF preferred */
864 	if (spl_in->lls_pref != LLS_PREF_YES && !spl_in->prefer_easf)
865 		skip_easf = true;
866 
867 	return skip_easf;
868 }
869 
870 /* Check if video is in fullscreen mode */
871 static bool spl_is_video_fullscreen(struct spl_in *spl_in)
872 {
873 	if (spl_is_video_format(spl_in->basic_in.format) && spl_in->is_fullscreen)
874 		return true;
875 	return false;
876 }
877 
878 static bool spl_get_isharp_en(struct spl_in *spl_in,
879 	struct spl_scratch *spl_scratch)
880 {
881 	bool enable_isharp = false;
882 	int vratio = 0;
883 	int hratio = 0;
884 	struct spl_taps taps = spl_scratch->scl_data.taps;
885 	bool fullscreen = spl_is_video_fullscreen(spl_in);
886 
887 	/* Return if adaptive sharpness is disabled */
888 	if (spl_in->adaptive_sharpness.enable == false)
889 		return enable_isharp;
890 
891 	vratio = spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert);
892 	hratio = spl_fixpt_ceil(spl_scratch->scl_data.ratios.horz);
893 
894 	/* No iSHARP support for downscaling */
895 	if (vratio > 1 || hratio > 1)
896 		return enable_isharp;
897 
898 	// Scaling is up to 1:1 (no scaling) or upscaling
899 
900 	/*
901 	 * Apply sharpness to RGB and YUV (NV12/P010)
902 	 *  surfaces based on policy setting
903 	 */
904 	if (!spl_is_video_format(spl_in->basic_in.format) &&
905 		(spl_in->sharpen_policy == SHARPEN_YUV))
906 		return enable_isharp;
907 	else if ((spl_is_video_format(spl_in->basic_in.format) && !fullscreen) &&
908 		(spl_in->sharpen_policy == SHARPEN_RGB_FULLSCREEN_YUV))
909 		return enable_isharp;
910 	else if (!spl_in->is_fullscreen &&
911 			spl_in->sharpen_policy == SHARPEN_FULLSCREEN_ALL)
912 		return enable_isharp;
913 
914 	/*
915 	 * Apply sharpness if supports horizontal taps 4,6 AND
916 	 *  vertical taps 3, 4, 6
917 	 */
918 	if ((taps.h_taps == 4 || taps.h_taps == 6) &&
919 		(taps.v_taps == 3 || taps.v_taps == 4 || taps.v_taps == 6))
920 		enable_isharp = true;
921 
922 	return enable_isharp;
923 }
924 
925 /* Calculate number of tap with adaptive scaling off */
926 static void spl_get_taps_non_adaptive_scaler(
927 		struct spl_scratch *spl_scratch,
928 		const struct spl_taps *in_taps,
929 		bool is_horz_subsampled,
930 		bool is_vert_subsampled)
931 {
932 	bool check_max_downscale = false;
933 
934 	if (in_taps->h_taps == 0) {
935 		if (spl_fixpt_ceil(spl_scratch->scl_data.ratios.horz) > 1)
936 			spl_scratch->scl_data.taps.h_taps = spl_min(2 * spl_fixpt_ceil(
937 				spl_scratch->scl_data.ratios.horz), 8);
938 		else
939 			spl_scratch->scl_data.taps.h_taps = 4;
940 	} else
941 		spl_scratch->scl_data.taps.h_taps = in_taps->h_taps;
942 
943 	if (in_taps->v_taps == 0) {
944 		if (spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert) > 1)
945 			spl_scratch->scl_data.taps.v_taps = spl_min(2 * spl_fixpt_ceil(
946 				spl_scratch->scl_data.ratios.vert), 8);
947 		else
948 			spl_scratch->scl_data.taps.v_taps = 4;
949 	} else
950 		spl_scratch->scl_data.taps.v_taps = in_taps->v_taps;
951 
952 	if (in_taps->v_taps_c == 0) {
953 		if (spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert_c) > 1)
954 			spl_scratch->scl_data.taps.v_taps_c = spl_min(2 * spl_fixpt_ceil(
955 				spl_scratch->scl_data.ratios.vert_c), 8);
956 		else
957 			spl_scratch->scl_data.taps.v_taps_c = 4;
958 	} else
959 		spl_scratch->scl_data.taps.v_taps_c = in_taps->v_taps_c;
960 
961 	if (in_taps->h_taps_c == 0) {
962 		if (spl_fixpt_ceil(spl_scratch->scl_data.ratios.horz_c) > 1)
963 			spl_scratch->scl_data.taps.h_taps_c = spl_min(2 * spl_fixpt_ceil(
964 				spl_scratch->scl_data.ratios.horz_c), 8);
965 		else
966 			spl_scratch->scl_data.taps.h_taps_c = 4;
967 	} else if ((in_taps->h_taps_c % 2) != 0 && in_taps->h_taps_c != 1)
968 		/* Only 1 and even h_taps_c are supported by hw */
969 		spl_scratch->scl_data.taps.h_taps_c = in_taps->h_taps_c - 1;
970 	else
971 		spl_scratch->scl_data.taps.h_taps_c = in_taps->h_taps_c;
972 
973 
974 	/*
975 	 * Max downscale supported is 6.0x.  Add ASSERT to catch if go beyond that
976 	 */
977 	check_max_downscale = spl_fixpt_le(spl_scratch->scl_data.ratios.horz,
978 		spl_fixpt_from_fraction(6, 1));
979 	SPL_ASSERT(check_max_downscale);
980 	check_max_downscale = spl_fixpt_le(spl_scratch->scl_data.ratios.vert,
981 		spl_fixpt_from_fraction(6, 1));
982 	SPL_ASSERT(check_max_downscale);
983 	check_max_downscale = spl_fixpt_le(spl_scratch->scl_data.ratios.horz_c,
984 		spl_fixpt_from_fraction(6, 1));
985 	SPL_ASSERT(check_max_downscale);
986 	check_max_downscale = spl_fixpt_le(spl_scratch->scl_data.ratios.vert_c,
987 		spl_fixpt_from_fraction(6, 1));
988 	SPL_ASSERT(check_max_downscale);
989 
990 
991 	if (IDENTITY_RATIO(spl_scratch->scl_data.ratios.horz))
992 		spl_scratch->scl_data.taps.h_taps = 1;
993 	if (IDENTITY_RATIO(spl_scratch->scl_data.ratios.vert))
994 		spl_scratch->scl_data.taps.v_taps = 1;
995 	if (IDENTITY_RATIO(spl_scratch->scl_data.ratios.horz_c) && !is_horz_subsampled)
996 		spl_scratch->scl_data.taps.h_taps_c = 1;
997 	if (IDENTITY_RATIO(spl_scratch->scl_data.ratios.vert_c) && !is_vert_subsampled)
998 		spl_scratch->scl_data.taps.v_taps_c = 1;
999 }
1000 
1001 /* Calculate optimal number of taps */
1002 static bool spl_get_optimal_number_of_taps(
1003 	  int max_downscale_src_width, struct spl_in *spl_in, struct spl_scratch *spl_scratch,
1004 	  const struct spl_taps *in_taps, bool *enable_easf_v, bool *enable_easf_h,
1005 	  bool *enable_isharp)
1006 {
1007 	int num_part_y, num_part_c;
1008 	unsigned int max_taps_y, max_taps_c;
1009 	unsigned int min_taps_y, min_taps_c;
1010 	enum lb_memory_config lb_config;
1011 	bool skip_easf          = false;
1012 	bool is_horz_subsampled = spl_is_subsampled_format(spl_in->basic_in.format);
1013 	bool is_vert_subsampled = spl_is_yuv420(spl_in->basic_in.format);
1014 
1015 	if (spl_scratch->scl_data.viewport.width > spl_scratch->scl_data.h_active &&
1016 		max_downscale_src_width != 0 &&
1017 		spl_scratch->scl_data.viewport.width > max_downscale_src_width) {
1018 		spl_get_taps_non_adaptive_scaler(spl_scratch, in_taps, is_horz_subsampled, is_vert_subsampled);
1019 		*enable_easf_v = false;
1020 		*enable_easf_h = false;
1021 		*enable_isharp = false;
1022 		return false;
1023 	}
1024 
1025 	/* Disable adaptive scaler and sharpener when integer scaling is enabled */
1026 	if (spl_in->scaling_quality.integer_scaling) {
1027 		spl_get_taps_non_adaptive_scaler(spl_scratch, in_taps, is_horz_subsampled, is_vert_subsampled);
1028 		*enable_easf_v = false;
1029 		*enable_easf_h = false;
1030 		*enable_isharp = false;
1031 		return true;
1032 	}
1033 
1034 	/* Check if we are using EASF or not */
1035 	skip_easf = enable_easf(spl_in, spl_scratch);
1036 
1037 	/*
1038 	 * Set default taps if none are provided
1039 	 * From programming guide: taps = min{ ceil(2*H_RATIO,1), 8} for downscaling
1040 	 * taps = 4 for upscaling
1041 	 */
1042 	if (skip_easf) {
1043 		spl_get_taps_non_adaptive_scaler(spl_scratch, in_taps, is_horz_subsampled, is_vert_subsampled);
1044 	}
1045 	else {
1046 		if (spl_is_subsampled_format(spl_in->basic_in.format)) {
1047 			spl_scratch->scl_data.taps.h_taps = 6;
1048 			spl_scratch->scl_data.taps.v_taps = 6;
1049 			spl_scratch->scl_data.taps.h_taps_c = 4;
1050 			spl_scratch->scl_data.taps.v_taps_c = 4;
1051 		} else { /* RGB / YUV444 */
1052 			spl_scratch->scl_data.taps.h_taps = 6;
1053 			spl_scratch->scl_data.taps.v_taps = 6;
1054 			spl_scratch->scl_data.taps.h_taps_c = 6;
1055 			spl_scratch->scl_data.taps.v_taps_c = 6;
1056 		}
1057 
1058 		/* Override mode: keep EASF enabled but use input taps if valid */
1059 		if (spl_in->override_easf) {
1060 			spl_scratch->scl_data.taps.h_taps = (in_taps->h_taps != 0) ? in_taps->h_taps : spl_scratch->scl_data.taps.h_taps;
1061 			spl_scratch->scl_data.taps.v_taps = (in_taps->v_taps != 0) ? in_taps->v_taps : spl_scratch->scl_data.taps.v_taps;
1062 			spl_scratch->scl_data.taps.h_taps_c = (in_taps->h_taps_c != 0) ? in_taps->h_taps_c : spl_scratch->scl_data.taps.h_taps_c;
1063 			spl_scratch->scl_data.taps.v_taps_c = (in_taps->v_taps_c != 0) ? in_taps->v_taps_c : spl_scratch->scl_data.taps.v_taps_c;
1064 
1065 			if ((spl_scratch->scl_data.taps.h_taps > 6) || (spl_scratch->scl_data.taps.v_taps > 6))
1066 				skip_easf = true;
1067 			if ((spl_scratch->scl_data.taps.h_taps > 1) && (spl_scratch->scl_data.taps.h_taps % 2))
1068 				spl_scratch->scl_data.taps.h_taps--;
1069 			if ((spl_scratch->scl_data.taps.h_taps_c > 1) && (spl_scratch->scl_data.taps.h_taps_c % 2))
1070 				spl_scratch->scl_data.taps.h_taps_c--;
1071 		}
1072 	}
1073 
1074 	/*Ensure we can support the requested number of vtaps*/
1075 	min_taps_y = spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert);
1076 	min_taps_c = spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert_c);
1077 
1078 	/* Use LB_MEMORY_CONFIG_3 for 4:2:0 */
1079 	if (spl_is_yuv420(spl_in->basic_in.format))
1080 		lb_config = LB_MEMORY_CONFIG_3;
1081 	else
1082 		lb_config = LB_MEMORY_CONFIG_0;
1083 	// Determine max vtap support by calculating how much line buffer can fit
1084 	spl_in->callbacks.spl_calc_lb_num_partitions(spl_in->basic_out.alpha_en, &spl_scratch->scl_data,
1085 			lb_config, &num_part_y, &num_part_c);
1086 	/* MAX_V_TAPS = MIN (NUM_LINES - MAX(CEILING(V_RATIO,1)-2, 0), 8) */
1087 	if (spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert) > 2)
1088 		if ((spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert) - 2) > num_part_y)
1089 			max_taps_y = 0;
1090 		else
1091 			max_taps_y = num_part_y - (spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert) - 2);
1092 	else
1093 		max_taps_y = num_part_y;
1094 
1095 	if (spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert_c) > 2)
1096 		if ((spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert_c) - 2) > num_part_c)
1097 			max_taps_c = 0;
1098 		else
1099 			max_taps_c = num_part_c - (spl_fixpt_ceil(spl_scratch->scl_data.ratios.vert_c) - 2);
1100 	else
1101 		max_taps_c = num_part_c;
1102 
1103 	if (max_taps_y < min_taps_y)
1104 		return false;
1105 	else if (max_taps_c < min_taps_c)
1106 		return false;
1107 
1108 	if (spl_scratch->scl_data.taps.v_taps > max_taps_y)
1109 		spl_scratch->scl_data.taps.v_taps = max_taps_y;
1110 
1111 	if (spl_scratch->scl_data.taps.v_taps_c > max_taps_c)
1112 		spl_scratch->scl_data.taps.v_taps_c = max_taps_c;
1113 
1114 	if (!skip_easf) {
1115 		/*
1116 		 * RGB ( L + NL ) and Linear HDR support 6x6, 6x4, 6x3, 4x4, 4x3
1117 		 * NL YUV420 only supports 6x6, 6x4 for Y and 4x4 for UV
1118 		 *
1119 		 * If LB does not support 3, 4, or 6 taps, then disable EASF_V
1120 		 *  and only enable EASF_H.  So for RGB, support 6x2, 4x2
1121 		 *  and for NL YUV420, support 6x2 for Y and 4x2 for UV
1122 		 *
1123 		 * All other cases, have to disable EASF_V and EASF_H
1124 		 *
1125 		 * If optimal no of taps is 5, then set it to 4
1126 		 * If optimal no of taps is 7 or 8, then fine since max tap is 6
1127 		 *
1128 		 */
1129 		if (spl_scratch->scl_data.taps.v_taps == 5)
1130 			spl_scratch->scl_data.taps.v_taps = 4;
1131 
1132 		if (spl_scratch->scl_data.taps.v_taps_c == 5)
1133 			spl_scratch->scl_data.taps.v_taps_c = 4;
1134 
1135 		if (spl_scratch->scl_data.taps.h_taps == 5)
1136 			spl_scratch->scl_data.taps.h_taps = 4;
1137 
1138 		if (spl_scratch->scl_data.taps.h_taps_c == 5)
1139 			spl_scratch->scl_data.taps.h_taps_c = 4;
1140 
1141 		if (spl_is_video_format(spl_in->basic_in.format)) {
1142 			if (spl_scratch->scl_data.taps.h_taps <= 4) {
1143 				*enable_easf_v = false;
1144 				*enable_easf_h = false;
1145 			} else if (spl_scratch->scl_data.taps.v_taps <= 3) {
1146 				*enable_easf_v = false;
1147 				*enable_easf_h = true;
1148 			} else {
1149 				*enable_easf_v = true;
1150 				*enable_easf_h = true;
1151 			}
1152 			SPL_ASSERT((spl_scratch->scl_data.taps.v_taps > 1) &&
1153 				(spl_scratch->scl_data.taps.v_taps_c > 1));
1154 		} else { /* RGB */
1155 			if (spl_scratch->scl_data.taps.h_taps <= 3) {
1156 				*enable_easf_v = false;
1157 				*enable_easf_h = false;
1158 			} else if (spl_scratch->scl_data.taps.v_taps < 3) {
1159 				*enable_easf_v = false;
1160 				*enable_easf_h = true;
1161 			} else {
1162 				*enable_easf_v = true;
1163 				*enable_easf_h = true;
1164 			}
1165 			SPL_ASSERT(spl_scratch->scl_data.taps.v_taps > 1);
1166 		}
1167 	} else {
1168 		*enable_easf_v = false;
1169 		*enable_easf_h = false;
1170 	} // end of if prefer_easf
1171 
1172 	/* Sharpener requires scaler to be enabled, including for 1:1
1173 	 * Check if ISHARP can be enabled
1174 	 * If ISHARP is not enabled, set taps to 1 if ratio is 1:1
1175 	 *  except for chroma taps.  Keep previous taps so it can
1176 	 *  handle cositing
1177 	 */
1178 
1179 	*enable_isharp = spl_get_isharp_en(spl_in, spl_scratch);
1180 	if (!*enable_isharp && !spl_in->basic_out.always_scale)	{
1181 		if ((IDENTITY_RATIO(spl_scratch->scl_data.ratios.horz)) &&
1182 			(IDENTITY_RATIO(spl_scratch->scl_data.ratios.vert))) {
1183 			spl_scratch->scl_data.taps.h_taps = 1;
1184 			spl_scratch->scl_data.taps.v_taps = 1;
1185 			if (IDENTITY_RATIO(spl_scratch->scl_data.ratios.horz_c) && !is_horz_subsampled)
1186 				spl_scratch->scl_data.taps.h_taps_c = 1;
1187 
1188 			if (IDENTITY_RATIO(spl_scratch->scl_data.ratios.vert_c) && !is_vert_subsampled)
1189 				spl_scratch->scl_data.taps.v_taps_c = 1;
1190 
1191 			*enable_easf_v = false;
1192 			*enable_easf_h = false;
1193 		} else {
1194 			if ((!*enable_easf_h) &&
1195 				(IDENTITY_RATIO(spl_scratch->scl_data.ratios.horz)))
1196 				spl_scratch->scl_data.taps.h_taps = 1;
1197 
1198 			if ((!*enable_easf_v) &&
1199 				(IDENTITY_RATIO(spl_scratch->scl_data.ratios.vert)))
1200 				spl_scratch->scl_data.taps.v_taps = 1;
1201 
1202 			if ((!*enable_easf_h) && !is_horz_subsampled &&
1203 				(IDENTITY_RATIO(spl_scratch->scl_data.ratios.horz_c)))
1204 				spl_scratch->scl_data.taps.h_taps_c = 1;
1205 
1206 			if ((!*enable_easf_v) && !is_vert_subsampled &&
1207 				(IDENTITY_RATIO(spl_scratch->scl_data.ratios.vert_c)))
1208 				spl_scratch->scl_data.taps.v_taps_c = 1;
1209 
1210 		}
1211 	}
1212 	return true;
1213 }
1214 
1215 static void spl_set_black_color_data(enum spl_pixel_format format,
1216 			struct scl_black_color *scl_black_color)
1217 {
1218 	bool ycbcr = spl_is_video_format(format);
1219 	if (ycbcr)	{
1220 		scl_black_color->offset_rgb_y = BLACK_OFFSET_RGB_Y;
1221 		scl_black_color->offset_rgb_cbcr = BLACK_OFFSET_CBCR;
1222 	}	else {
1223 		scl_black_color->offset_rgb_y = 0x0;
1224 		scl_black_color->offset_rgb_cbcr = 0x0;
1225 	}
1226 }
1227 
1228 static void spl_set_manual_ratio_init_data(struct dscl_prog_data *dscl_prog_data,
1229 		const struct spl_scaler_data *scl_data)
1230 {
1231 	struct spl_fixed31_32 bot;
1232 
1233 	dscl_prog_data->ratios.h_scale_ratio = spl_fixpt_u3d19(
1234 			scl_data->ratios.horz) << 5;
1235 	dscl_prog_data->ratios.v_scale_ratio = spl_fixpt_u3d19(
1236 			scl_data->ratios.vert) << 5;
1237 	dscl_prog_data->ratios.h_scale_ratio_c = spl_fixpt_u3d19(
1238 			scl_data->ratios.horz_c) << 5;
1239 	dscl_prog_data->ratios.v_scale_ratio_c = spl_fixpt_u3d19(
1240 			scl_data->ratios.vert_c) << 5;
1241 	/*
1242 	 * 0.24 format for fraction, first five bits zeroed
1243 	 */
1244 	dscl_prog_data->init.h_filter_init_frac =
1245 			spl_fixpt_u0d19(scl_data->inits.h) << 5;
1246 	dscl_prog_data->init.h_filter_init_int =
1247 			spl_fixpt_floor(scl_data->inits.h);
1248 	dscl_prog_data->init.h_filter_init_frac_c =
1249 			spl_fixpt_u0d19(scl_data->inits.h_c) << 5;
1250 	dscl_prog_data->init.h_filter_init_int_c =
1251 			spl_fixpt_floor(scl_data->inits.h_c);
1252 	dscl_prog_data->init.v_filter_init_frac =
1253 			spl_fixpt_u0d19(scl_data->inits.v) << 5;
1254 	dscl_prog_data->init.v_filter_init_int =
1255 			spl_fixpt_floor(scl_data->inits.v);
1256 	dscl_prog_data->init.v_filter_init_frac_c =
1257 			spl_fixpt_u0d19(scl_data->inits.v_c) << 5;
1258 	dscl_prog_data->init.v_filter_init_int_c =
1259 			spl_fixpt_floor(scl_data->inits.v_c);
1260 
1261 	bot = spl_fixpt_add(scl_data->inits.v, scl_data->ratios.vert);
1262 	dscl_prog_data->init.v_filter_init_bot_frac = spl_fixpt_u0d19(bot) << 5;
1263 	dscl_prog_data->init.v_filter_init_bot_int = spl_fixpt_floor(bot);
1264 	bot = spl_fixpt_add(scl_data->inits.v_c, scl_data->ratios.vert_c);
1265 	dscl_prog_data->init.v_filter_init_bot_frac_c = spl_fixpt_u0d19(bot) << 5;
1266 	dscl_prog_data->init.v_filter_init_bot_int_c = spl_fixpt_floor(bot);
1267 }
1268 
1269 static void spl_set_taps_data(struct dscl_prog_data *dscl_prog_data,
1270 		const struct spl_scaler_data *scl_data)
1271 {
1272 	dscl_prog_data->taps.v_taps = scl_data->taps.v_taps - 1;
1273 	dscl_prog_data->taps.h_taps = scl_data->taps.h_taps - 1;
1274 	dscl_prog_data->taps.v_taps_c = scl_data->taps.v_taps_c - 1;
1275 	dscl_prog_data->taps.h_taps_c = scl_data->taps.h_taps_c - 1;
1276 }
1277 
1278 /* Populate dscl prog data structure from scaler data calculated by SPL */
1279 static void spl_set_dscl_prog_data(struct spl_in *spl_in, struct spl_scratch *spl_scratch,
1280 	struct spl_out *spl_out, bool enable_easf_v, bool enable_easf_h, bool enable_isharp)
1281 {
1282 	struct dscl_prog_data *dscl_prog_data = spl_out->dscl_prog_data;
1283 
1284 	const struct spl_scaler_data *data = &spl_scratch->scl_data;
1285 
1286 	struct scl_black_color *scl_black_color = &dscl_prog_data->scl_black_color;
1287 
1288 	bool enable_easf = enable_easf_v || enable_easf_h;
1289 
1290 	// Set values for recout
1291 	dscl_prog_data->recout = spl_scratch->scl_data.recout;
1292 	// Set values for MPC Size
1293 	dscl_prog_data->mpc_size.width = spl_scratch->scl_data.h_active;
1294 	dscl_prog_data->mpc_size.height = spl_scratch->scl_data.v_active;
1295 
1296 	// SCL_MODE - Set SCL_MODE data
1297 	dscl_prog_data->dscl_mode = spl_get_dscl_mode(spl_in, data, enable_isharp,
1298 		enable_easf);
1299 
1300 	// SCL_BLACK_COLOR
1301 	spl_set_black_color_data(spl_in->basic_in.format, scl_black_color);
1302 
1303 	/* Manually calculate scale ratio and init values */
1304 	spl_set_manual_ratio_init_data(dscl_prog_data, data);
1305 
1306 	// Set HTaps/VTaps
1307 	spl_set_taps_data(dscl_prog_data, data);
1308 	// Set viewport
1309 	dscl_prog_data->viewport = spl_scratch->scl_data.viewport;
1310 	// Set viewport_c
1311 	dscl_prog_data->viewport_c = spl_scratch->scl_data.viewport_c;
1312 	// Set filters data
1313 	SPL_NAMESPACE(spl_set_filters_data(dscl_prog_data, data, enable_easf_v, enable_easf_h));
1314 }
1315 
1316 /* Calculate C0-C3 coefficients based on HDR_mult */
1317 static void spl_calculate_c0_c3_hdr(struct dscl_prog_data *dscl_prog_data, uint32_t sdr_white_level_nits)
1318 {
1319 	struct spl_fixed31_32 hdr_mult, c0_mult, c1_mult, c2_mult;
1320 	struct spl_fixed31_32 c0_calc, c1_calc, c2_calc;
1321 	struct spl_custom_float_format fmt;
1322 	uint32_t hdr_multx100_int;
1323 
1324 	if ((sdr_white_level_nits >= 80) && (sdr_white_level_nits <= 480))
1325 		hdr_multx100_int = sdr_white_level_nits * 100 / 80;
1326 	else
1327 		hdr_multx100_int = 100; /* default for 80 nits otherwise */
1328 
1329 	hdr_mult = spl_fixpt_from_fraction((long long)hdr_multx100_int, 100LL);
1330 	c0_mult = spl_fixpt_from_fraction(2126LL, 10000LL);
1331 	c1_mult = spl_fixpt_from_fraction(7152LL, 10000LL);
1332 	c2_mult = spl_fixpt_from_fraction(722LL, 10000LL);
1333 
1334 	c0_calc = spl_fixpt_mul(hdr_mult, spl_fixpt_mul(c0_mult,
1335 		spl_fixpt_from_fraction(16384LL, 125LL)));
1336 	c1_calc = spl_fixpt_mul(hdr_mult, spl_fixpt_mul(c1_mult,
1337 		spl_fixpt_from_fraction(16384LL, 125LL)));
1338 	c2_calc = spl_fixpt_mul(hdr_mult, spl_fixpt_mul(c2_mult,
1339 		spl_fixpt_from_fraction(16384LL, 125LL)));
1340 
1341 	fmt.exponenta_bits = 5;
1342 	fmt.mantissa_bits = 10;
1343 	fmt.sign = true;
1344 
1345 	// fp1.5.10, C0 coefficient (LN_rec709:  HDR_MULT * 0.212600 * 2^14/125)
1346 	spl_convert_to_custom_float_format(c0_calc, &fmt,
1347 		&dscl_prog_data->easf_matrix_c0);
1348 	// fp1.5.10, C1 coefficient (LN_rec709:  HDR_MULT * 0.715200 * 2^14/125)
1349 	spl_convert_to_custom_float_format(c1_calc, &fmt,
1350 		&dscl_prog_data->easf_matrix_c1);
1351 	// fp1.5.10, C2 coefficient (LN_rec709:  HDR_MULT * 0.072200 * 2^14/125)
1352 	spl_convert_to_custom_float_format(c2_calc, &fmt,
1353 		&dscl_prog_data->easf_matrix_c2);
1354 	dscl_prog_data->easf_matrix_c3 = 0x0; // fp1.5.10, C3 coefficient
1355 }
1356 
1357 /* Set EASF data */
1358 static void spl_set_easf_data(struct spl_scratch *spl_scratch, struct spl_out *spl_out, bool enable_easf_v,
1359 	bool enable_easf_h, enum linear_light_scaling lls_pref,
1360 	enum spl_pixel_format format, enum system_setup setup,
1361 	uint32_t sdr_white_level_nits)
1362 {
1363 	struct dscl_prog_data *dscl_prog_data = spl_out->dscl_prog_data;
1364 	if (enable_easf_v) {
1365 		dscl_prog_data->easf_v_en = true;
1366 		dscl_prog_data->easf_v_ring = 0;
1367 		dscl_prog_data->easf_v_sharp_factor = 1;
1368 		dscl_prog_data->easf_v_bf1_en = 1;	// 1-bit, BF1 calculation enable, 0=disable, 1=enable
1369 		dscl_prog_data->easf_v_bf2_mode = 0xF;	// 4-bit, BF2 calculation mode
1370 		/* 2-bit, BF3 chroma mode correction calculation mode */
1371 		dscl_prog_data->easf_v_bf3_mode = SPL_NAMESPACE(spl_get_v_bf3_mode(
1372 			spl_scratch->scl_data.recip_ratios.vert));
1373 		/* FP1.5.10 [ minCoef ]*/
1374 		dscl_prog_data->easf_v_ringest_3tap_dntilt_uptilt =
1375 			SPL_NAMESPACE(spl_get_3tap_dntilt_uptilt_offset(spl_scratch->scl_data.taps.v_taps,
1376 				spl_scratch->scl_data.recip_ratios.vert));
1377 		/* FP1.5.10 [ upTiltMaxVal ]*/
1378 		dscl_prog_data->easf_v_ringest_3tap_uptilt_max =
1379 			SPL_NAMESPACE(spl_get_3tap_uptilt_maxval(spl_scratch->scl_data.taps.v_taps,
1380 				spl_scratch->scl_data.recip_ratios.vert));
1381 		/* FP1.5.10 [ dnTiltSlope ]*/
1382 		dscl_prog_data->easf_v_ringest_3tap_dntilt_slope =
1383 			SPL_NAMESPACE(spl_get_3tap_dntilt_slope(spl_scratch->scl_data.taps.v_taps,
1384 				spl_scratch->scl_data.recip_ratios.vert));
1385 		/* FP1.5.10 [ upTilt1Slope ]*/
1386 		dscl_prog_data->easf_v_ringest_3tap_uptilt1_slope =
1387 			SPL_NAMESPACE(spl_get_3tap_uptilt1_slope(spl_scratch->scl_data.taps.v_taps,
1388 				spl_scratch->scl_data.recip_ratios.vert));
1389 		/* FP1.5.10 [ upTilt2Slope ]*/
1390 		dscl_prog_data->easf_v_ringest_3tap_uptilt2_slope =
1391 			SPL_NAMESPACE(spl_get_3tap_uptilt2_slope(spl_scratch->scl_data.taps.v_taps,
1392 				spl_scratch->scl_data.recip_ratios.vert));
1393 		/* FP1.5.10 [ upTilt2Offset ]*/
1394 		dscl_prog_data->easf_v_ringest_3tap_uptilt2_offset =
1395 			SPL_NAMESPACE(spl_get_3tap_uptilt2_offset(spl_scratch->scl_data.taps.v_taps,
1396 				spl_scratch->scl_data.recip_ratios.vert));
1397 		/* FP1.5.10; (2.0) Ring reducer gain for 4 or 6-tap mode [H_REDUCER_GAIN4] */
1398 		dscl_prog_data->easf_v_ringest_eventap_reduceg1 =
1399 			SPL_NAMESPACE(spl_get_reducer_gain4(spl_scratch->scl_data.taps.v_taps,
1400 				spl_scratch->scl_data.recip_ratios.vert));
1401 		/* FP1.5.10; (2.5) Ring reducer gain for 6-tap mode [V_REDUCER_GAIN6] */
1402 		dscl_prog_data->easf_v_ringest_eventap_reduceg2 =
1403 			SPL_NAMESPACE(spl_get_reducer_gain6(spl_scratch->scl_data.taps.v_taps,
1404 				spl_scratch->scl_data.recip_ratios.vert));
1405 		/* FP1.5.10; (-0.135742) Ring gain for 6-tap set to -139/1024 */
1406 		dscl_prog_data->easf_v_ringest_eventap_gain1 =
1407 			SPL_NAMESPACE(spl_get_gainRing4(spl_scratch->scl_data.taps.v_taps,
1408 				spl_scratch->scl_data.recip_ratios.vert));
1409 		/* FP1.5.10; (-0.024414) Ring gain for 6-tap set to -25/1024 */
1410 		dscl_prog_data->easf_v_ringest_eventap_gain2 =
1411 			SPL_NAMESPACE(spl_get_gainRing6(spl_scratch->scl_data.taps.v_taps,
1412 				spl_scratch->scl_data.recip_ratios.vert));
1413 		dscl_prog_data->easf_v_bf_maxa = 63; //Vertical Max BF value A in U0.6 format.Selected if V_FCNTL == 0
1414 		dscl_prog_data->easf_v_bf_maxb = 63; //Vertical Max BF value A in U0.6 format.Selected if V_FCNTL == 1
1415 		dscl_prog_data->easf_v_bf_mina = 0;	//Vertical Min BF value A in U0.6 format.Selected if V_FCNTL == 0
1416 		dscl_prog_data->easf_v_bf_minb = 0;	//Vertical Min BF value A in U0.6 format.Selected if V_FCNTL == 1
1417 		if (lls_pref == LLS_PREF_YES)	{
1418 			dscl_prog_data->easf_v_bf2_flat1_gain = 4;	// U1.3, BF2 Flat1 Gain control
1419 			dscl_prog_data->easf_v_bf2_flat2_gain = 8;	// U4.0, BF2 Flat2 Gain control
1420 			dscl_prog_data->easf_v_bf2_roc_gain = 4;	// U2.2, Rate Of Change control
1421 
1422 			dscl_prog_data->easf_v_bf1_pwl_in_seg0 = 0x600;	// S0.10, BF1 PWL Segment 0 = -512
1423 			dscl_prog_data->easf_v_bf1_pwl_base_seg0 = 0;	// U0.6, BF1 Base PWL Segment 0
1424 			dscl_prog_data->easf_v_bf1_pwl_slope_seg0 = 3;	// S7.3, BF1 Slope PWL Segment 0
1425 			dscl_prog_data->easf_v_bf1_pwl_in_seg1 = 0x7EC;	// S0.10, BF1 PWL Segment 1 = -20
1426 			dscl_prog_data->easf_v_bf1_pwl_base_seg1 = 12;	// U0.6, BF1 Base PWL Segment 1
1427 			dscl_prog_data->easf_v_bf1_pwl_slope_seg1 = 326;	// S7.3, BF1 Slope PWL Segment 1
1428 			dscl_prog_data->easf_v_bf1_pwl_in_seg2 = 0;	// S0.10, BF1 PWL Segment 2
1429 			dscl_prog_data->easf_v_bf1_pwl_base_seg2 = 63;	// U0.6, BF1 Base PWL Segment 2
1430 			dscl_prog_data->easf_v_bf1_pwl_slope_seg2 = 0;	// S7.3, BF1 Slope PWL Segment 2
1431 			dscl_prog_data->easf_v_bf1_pwl_in_seg3 = 16;	// S0.10, BF1 PWL Segment 3
1432 			dscl_prog_data->easf_v_bf1_pwl_base_seg3 = 63;	// U0.6, BF1 Base PWL Segment 3
1433 			dscl_prog_data->easf_v_bf1_pwl_slope_seg3 = 0x7C8;	// S7.3, BF1 Slope PWL Segment 3 = -56
1434 			dscl_prog_data->easf_v_bf1_pwl_in_seg4 = 32;	// S0.10, BF1 PWL Segment 4
1435 			dscl_prog_data->easf_v_bf1_pwl_base_seg4 = 56;	// U0.6, BF1 Base PWL Segment 4
1436 			dscl_prog_data->easf_v_bf1_pwl_slope_seg4 = 0x7D0;	// S7.3, BF1 Slope PWL Segment 4 = -48
1437 			dscl_prog_data->easf_v_bf1_pwl_in_seg5 = 48;	// S0.10, BF1 PWL Segment 5
1438 			dscl_prog_data->easf_v_bf1_pwl_base_seg5 = 50;	// U0.6, BF1 Base PWL Segment 5
1439 			dscl_prog_data->easf_v_bf1_pwl_slope_seg5 = 0x710;	// S7.3, BF1 Slope PWL Segment 5 = -240
1440 			dscl_prog_data->easf_v_bf1_pwl_in_seg6 = 64;	// S0.10, BF1 PWL Segment 6
1441 			dscl_prog_data->easf_v_bf1_pwl_base_seg6 = 20;	// U0.6, BF1 Base PWL Segment 6
1442 			dscl_prog_data->easf_v_bf1_pwl_slope_seg6 = 0x760;	// S7.3, BF1 Slope PWL Segment 6 = -160
1443 			dscl_prog_data->easf_v_bf1_pwl_in_seg7 = 80;	// S0.10, BF1 PWL Segment 7
1444 			dscl_prog_data->easf_v_bf1_pwl_base_seg7 = 0;	// U0.6, BF1 Base PWL Segment 7
1445 
1446 			dscl_prog_data->easf_v_bf3_pwl_in_set0 = 0x000;	// FP0.6.6, BF3 Input value PWL Segment 0
1447 			dscl_prog_data->easf_v_bf3_pwl_base_set0 = 63;	// S0.6, BF3 Base PWL Segment 0
1448 			dscl_prog_data->easf_v_bf3_pwl_slope_set0 = 0x12C5;	// FP1.6.6, BF3 Slope PWL Segment 0
1449 			dscl_prog_data->easf_v_bf3_pwl_in_set1 =
1450 				0x0B37; // FP0.6.6, BF3 Input value PWL Segment 1 (0.0078125 * 125^3)
1451 			dscl_prog_data->easf_v_bf3_pwl_base_set1 = 62;	// S0.6, BF3 Base PWL Segment 1
1452 			dscl_prog_data->easf_v_bf3_pwl_slope_set1 =
1453 				0x13B8;	// FP1.6.6, BF3 Slope PWL Segment 1
1454 			dscl_prog_data->easf_v_bf3_pwl_in_set2 =
1455 				0x0BB7;	// FP0.6.6, BF3 Input value PWL Segment 2 (0.03125 * 125^3)
1456 			dscl_prog_data->easf_v_bf3_pwl_base_set2 = 20;	// S0.6, BF3 Base PWL Segment 2
1457 			dscl_prog_data->easf_v_bf3_pwl_slope_set2 =
1458 				0x1356;	// FP1.6.6, BF3 Slope PWL Segment 2
1459 			dscl_prog_data->easf_v_bf3_pwl_in_set3 =
1460 				0x0BF7;	// FP0.6.6, BF3 Input value PWL Segment 3 (0.0625 * 125^3)
1461 			dscl_prog_data->easf_v_bf3_pwl_base_set3 = 0;	// S0.6, BF3 Base PWL Segment 3
1462 			dscl_prog_data->easf_v_bf3_pwl_slope_set3 =
1463 				0x136B;	// FP1.6.6, BF3 Slope PWL Segment 3
1464 			dscl_prog_data->easf_v_bf3_pwl_in_set4 =
1465 				0x0C37;	// FP0.6.6, BF3 Input value PWL Segment 4 (0.125 * 125^3)
1466 			dscl_prog_data->easf_v_bf3_pwl_base_set4 = 0x4E;	// S0.6, BF3 Base PWL Segment 4 = -50
1467 			dscl_prog_data->easf_v_bf3_pwl_slope_set4 =
1468 				0x1200;	// FP1.6.6, BF3 Slope PWL Segment 4
1469 			dscl_prog_data->easf_v_bf3_pwl_in_set5 =
1470 				0x0CF7;	// FP0.6.6, BF3 Input value PWL Segment 5 (1.0 * 125^3)
1471 			dscl_prog_data->easf_v_bf3_pwl_base_set5 = 0x41;	// S0.6, BF3 Base PWL Segment 5 = -63
1472 		}	else	{
1473 			dscl_prog_data->easf_v_bf2_flat1_gain = 13;	// U1.3, BF2 Flat1 Gain control
1474 			dscl_prog_data->easf_v_bf2_flat2_gain = 15;	// U4.0, BF2 Flat2 Gain control
1475 			dscl_prog_data->easf_v_bf2_roc_gain = 14;	// U2.2, Rate Of Change control
1476 
1477 			dscl_prog_data->easf_v_bf1_pwl_in_seg0 = 0x440;	// S0.10, BF1 PWL Segment 0 = -960
1478 			dscl_prog_data->easf_v_bf1_pwl_base_seg0 = 0;	// U0.6, BF1 Base PWL Segment 0
1479 			dscl_prog_data->easf_v_bf1_pwl_slope_seg0 = 2;	// S7.3, BF1 Slope PWL Segment 0
1480 			dscl_prog_data->easf_v_bf1_pwl_in_seg1 = 0x7C4;	// S0.10, BF1 PWL Segment 1 = -60
1481 			dscl_prog_data->easf_v_bf1_pwl_base_seg1 = 12;	// U0.6, BF1 Base PWL Segment 1
1482 			dscl_prog_data->easf_v_bf1_pwl_slope_seg1 = 109;	// S7.3, BF1 Slope PWL Segment 1
1483 			dscl_prog_data->easf_v_bf1_pwl_in_seg2 = 0;	// S0.10, BF1 PWL Segment 2
1484 			dscl_prog_data->easf_v_bf1_pwl_base_seg2 = 63;	// U0.6, BF1 Base PWL Segment 2
1485 			dscl_prog_data->easf_v_bf1_pwl_slope_seg2 = 0;	// S7.3, BF1 Slope PWL Segment 2
1486 			dscl_prog_data->easf_v_bf1_pwl_in_seg3 = 48;	// S0.10, BF1 PWL Segment 3
1487 			dscl_prog_data->easf_v_bf1_pwl_base_seg3 = 63;	// U0.6, BF1 Base PWL Segment 3
1488 			dscl_prog_data->easf_v_bf1_pwl_slope_seg3 = 0x7ED;	// S7.3, BF1 Slope PWL Segment 3 = -19
1489 			dscl_prog_data->easf_v_bf1_pwl_in_seg4 = 96;	// S0.10, BF1 PWL Segment 4
1490 			dscl_prog_data->easf_v_bf1_pwl_base_seg4 = 56;	// U0.6, BF1 Base PWL Segment 4
1491 			dscl_prog_data->easf_v_bf1_pwl_slope_seg4 = 0x7F0;	// S7.3, BF1 Slope PWL Segment 4 = -16
1492 			dscl_prog_data->easf_v_bf1_pwl_in_seg5 = 144;	// S0.10, BF1 PWL Segment 5
1493 			dscl_prog_data->easf_v_bf1_pwl_base_seg5 = 50;	// U0.6, BF1 Base PWL Segment 5
1494 			dscl_prog_data->easf_v_bf1_pwl_slope_seg5 = 0x7B0;	// S7.3, BF1 Slope PWL Segment 5 = -80
1495 			dscl_prog_data->easf_v_bf1_pwl_in_seg6 = 192;	// S0.10, BF1 PWL Segment 6
1496 			dscl_prog_data->easf_v_bf1_pwl_base_seg6 = 20;	// U0.6, BF1 Base PWL Segment 6
1497 			dscl_prog_data->easf_v_bf1_pwl_slope_seg6 = 0x7CB;	// S7.3, BF1 Slope PWL Segment 6 = -53
1498 			dscl_prog_data->easf_v_bf1_pwl_in_seg7 = 240;	// S0.10, BF1 PWL Segment 7
1499 			dscl_prog_data->easf_v_bf1_pwl_base_seg7 = 0;	// U0.6, BF1 Base PWL Segment 7
1500 
1501 			dscl_prog_data->easf_v_bf3_pwl_in_set0 = 0x000;	// FP0.6.6, BF3 Input value PWL Segment 0
1502 			dscl_prog_data->easf_v_bf3_pwl_base_set0 = 63;	// S0.6, BF3 Base PWL Segment 0
1503 			dscl_prog_data->easf_v_bf3_pwl_slope_set0 = 0x0000;	// FP1.6.6, BF3 Slope PWL Segment 0
1504 			dscl_prog_data->easf_v_bf3_pwl_in_set1 =
1505 				0x06C0; // FP0.6.6, BF3 Input value PWL Segment 1 (0.0625)
1506 			dscl_prog_data->easf_v_bf3_pwl_base_set1 = 63;	// S0.6, BF3 Base PWL Segment 1
1507 			dscl_prog_data->easf_v_bf3_pwl_slope_set1 = 0x1896;	// FP1.6.6, BF3 Slope PWL Segment 1
1508 			dscl_prog_data->easf_v_bf3_pwl_in_set2 =
1509 				0x0700;	// FP0.6.6, BF3 Input value PWL Segment 2 (0.125)
1510 			dscl_prog_data->easf_v_bf3_pwl_base_set2 = 20;	// S0.6, BF3 Base PWL Segment 2
1511 			dscl_prog_data->easf_v_bf3_pwl_slope_set2 = 0x1810;	// FP1.6.6, BF3 Slope PWL Segment 2
1512 			dscl_prog_data->easf_v_bf3_pwl_in_set3 =
1513 				0x0740;	// FP0.6.6, BF3 Input value PWL Segment 3 (0.25)
1514 			dscl_prog_data->easf_v_bf3_pwl_base_set3 = 0;	// S0.6, BF3 Base PWL Segment 3
1515 			dscl_prog_data->easf_v_bf3_pwl_slope_set3 =
1516 				0x1878;	// FP1.6.6, BF3 Slope PWL Segment 3
1517 			dscl_prog_data->easf_v_bf3_pwl_in_set4 =
1518 				0x0761;	// FP0.6.6, BF3 Input value PWL Segment 4 (0.375)
1519 			dscl_prog_data->easf_v_bf3_pwl_base_set4 = 0x44;	// S0.6, BF3 Base PWL Segment 4 = -60
1520 			dscl_prog_data->easf_v_bf3_pwl_slope_set4 = 0x1760;	// FP1.6.6, BF3 Slope PWL Segment 4
1521 			dscl_prog_data->easf_v_bf3_pwl_in_set5 =
1522 				0x0780;	// FP0.6.6, BF3 Input value PWL Segment 5 (0.5)
1523 			dscl_prog_data->easf_v_bf3_pwl_base_set5 = 0x41;	// S0.6, BF3 Base PWL Segment 5 = -63
1524 		}
1525 	} else
1526 		dscl_prog_data->easf_v_en = false;
1527 
1528 	if (enable_easf_h) {
1529 		dscl_prog_data->easf_h_en = true;
1530 		dscl_prog_data->easf_h_ring = 0;
1531 		dscl_prog_data->easf_h_sharp_factor = 1;
1532 		dscl_prog_data->easf_h_bf1_en =
1533 			1;	// 1-bit, BF1 calculation enable, 0=disable, 1=enable
1534 		dscl_prog_data->easf_h_bf2_mode =
1535 			0xF;	// 4-bit, BF2 calculation mode
1536 		/* 2-bit, BF3 chroma mode correction calculation mode */
1537 		dscl_prog_data->easf_h_bf3_mode = SPL_NAMESPACE(spl_get_h_bf3_mode(
1538 			spl_scratch->scl_data.recip_ratios.horz));
1539 		/* FP1.5.10; (2.0) Ring reducer gain for 4 or 6-tap mode [H_REDUCER_GAIN4] */
1540 		dscl_prog_data->easf_h_ringest_eventap_reduceg1 =
1541 			SPL_NAMESPACE(spl_get_reducer_gain4(spl_scratch->scl_data.taps.h_taps,
1542 				spl_scratch->scl_data.recip_ratios.horz));
1543 		/* FP1.5.10; (2.5) Ring reducer gain for 6-tap mode [V_REDUCER_GAIN6] */
1544 		dscl_prog_data->easf_h_ringest_eventap_reduceg2 =
1545 			SPL_NAMESPACE(spl_get_reducer_gain6(spl_scratch->scl_data.taps.h_taps,
1546 				spl_scratch->scl_data.recip_ratios.horz));
1547 		/* FP1.5.10; (-0.135742) Ring gain for 6-tap set to -139/1024 */
1548 		dscl_prog_data->easf_h_ringest_eventap_gain1 =
1549 			SPL_NAMESPACE(spl_get_gainRing4(spl_scratch->scl_data.taps.h_taps,
1550 				spl_scratch->scl_data.recip_ratios.horz));
1551 		/* FP1.5.10; (-0.024414) Ring gain for 6-tap set to -25/1024 */
1552 		dscl_prog_data->easf_h_ringest_eventap_gain2 =
1553 			SPL_NAMESPACE(spl_get_gainRing6(spl_scratch->scl_data.taps.h_taps,
1554 				spl_scratch->scl_data.recip_ratios.horz));
1555 		dscl_prog_data->easf_h_bf_maxa = 63; //Horz Max BF value A in U0.6 format.Selected if H_FCNTL==0
1556 		dscl_prog_data->easf_h_bf_maxb = 63; //Horz Max BF value B in U0.6 format.Selected if H_FCNTL==1
1557 		dscl_prog_data->easf_h_bf_mina = 0;	//Horz Min BF value B in U0.6 format.Selected if H_FCNTL==0
1558 		dscl_prog_data->easf_h_bf_minb = 0;	//Horz Min BF value B in U0.6 format.Selected if H_FCNTL==1
1559 		if (lls_pref == LLS_PREF_YES)	{
1560 			dscl_prog_data->easf_h_bf2_flat1_gain = 4;	// U1.3, BF2 Flat1 Gain control
1561 			dscl_prog_data->easf_h_bf2_flat2_gain = 8;	// U4.0, BF2 Flat2 Gain control
1562 			dscl_prog_data->easf_h_bf2_roc_gain = 4;	// U2.2, Rate Of Change control
1563 
1564 			dscl_prog_data->easf_h_bf1_pwl_in_seg0 = 0x600;	// S0.10, BF1 PWL Segment 0 = -512
1565 			dscl_prog_data->easf_h_bf1_pwl_base_seg0 = 0;	// U0.6, BF1 Base PWL Segment 0
1566 			dscl_prog_data->easf_h_bf1_pwl_slope_seg0 = 3;	// S7.3, BF1 Slope PWL Segment 0
1567 			dscl_prog_data->easf_h_bf1_pwl_in_seg1 = 0x7EC;	// S0.10, BF1 PWL Segment 1 = -20
1568 			dscl_prog_data->easf_h_bf1_pwl_base_seg1 = 12;	// U0.6, BF1 Base PWL Segment 1
1569 			dscl_prog_data->easf_h_bf1_pwl_slope_seg1 = 326;	// S7.3, BF1 Slope PWL Segment 1
1570 			dscl_prog_data->easf_h_bf1_pwl_in_seg2 = 0;	// S0.10, BF1 PWL Segment 2
1571 			dscl_prog_data->easf_h_bf1_pwl_base_seg2 = 63;	// U0.6, BF1 Base PWL Segment 2
1572 			dscl_prog_data->easf_h_bf1_pwl_slope_seg2 = 0;	// S7.3, BF1 Slope PWL Segment 2
1573 			dscl_prog_data->easf_h_bf1_pwl_in_seg3 = 16;	// S0.10, BF1 PWL Segment 3
1574 			dscl_prog_data->easf_h_bf1_pwl_base_seg3 = 63;	// U0.6, BF1 Base PWL Segment 3
1575 			dscl_prog_data->easf_h_bf1_pwl_slope_seg3 = 0x7C8;	// S7.3, BF1 Slope PWL Segment 3 = -56
1576 			dscl_prog_data->easf_h_bf1_pwl_in_seg4 = 32;	// S0.10, BF1 PWL Segment 4
1577 			dscl_prog_data->easf_h_bf1_pwl_base_seg4 = 56;	// U0.6, BF1 Base PWL Segment 4
1578 			dscl_prog_data->easf_h_bf1_pwl_slope_seg4 = 0x7D0;	// S7.3, BF1 Slope PWL Segment 4 = -48
1579 			dscl_prog_data->easf_h_bf1_pwl_in_seg5 = 48;	// S0.10, BF1 PWL Segment 5
1580 			dscl_prog_data->easf_h_bf1_pwl_base_seg5 = 50;	// U0.6, BF1 Base PWL Segment 5
1581 			dscl_prog_data->easf_h_bf1_pwl_slope_seg5 = 0x710;	// S7.3, BF1 Slope PWL Segment 5 = -240
1582 			dscl_prog_data->easf_h_bf1_pwl_in_seg6 = 64;	// S0.10, BF1 PWL Segment 6
1583 			dscl_prog_data->easf_h_bf1_pwl_base_seg6 = 20;	// U0.6, BF1 Base PWL Segment 6
1584 			dscl_prog_data->easf_h_bf1_pwl_slope_seg6 = 0x760;	// S7.3, BF1 Slope PWL Segment 6 = -160
1585 			dscl_prog_data->easf_h_bf1_pwl_in_seg7 = 80;	// S0.10, BF1 PWL Segment 7
1586 			dscl_prog_data->easf_h_bf1_pwl_base_seg7 = 0;	// U0.6, BF1 Base PWL Segment 7
1587 
1588 			dscl_prog_data->easf_h_bf3_pwl_in_set0 = 0x000;	// FP0.6.6, BF3 Input value PWL Segment 0
1589 			dscl_prog_data->easf_h_bf3_pwl_base_set0 = 63;	// S0.6, BF3 Base PWL Segment 0
1590 			dscl_prog_data->easf_h_bf3_pwl_slope_set0 = 0x12C5;	// FP1.6.6, BF3 Slope PWL Segment 0
1591 			dscl_prog_data->easf_h_bf3_pwl_in_set1 =
1592 				0x0B37;	// FP0.6.6, BF3 Input value PWL Segment 1 (0.0078125 * 125^3)
1593 			dscl_prog_data->easf_h_bf3_pwl_base_set1 = 62;	// S0.6, BF3 Base PWL Segment 1
1594 			dscl_prog_data->easf_h_bf3_pwl_slope_set1 =	0x13B8;	// FP1.6.6, BF3 Slope PWL Segment 1
1595 			dscl_prog_data->easf_h_bf3_pwl_in_set2 =
1596 				0x0BB7;	// FP0.6.6, BF3 Input value PWL Segment 2 (0.03125 * 125^3)
1597 			dscl_prog_data->easf_h_bf3_pwl_base_set2 = 20;	// S0.6, BF3 Base PWL Segment 2
1598 			dscl_prog_data->easf_h_bf3_pwl_slope_set2 =	0x1356;	// FP1.6.6, BF3 Slope PWL Segment 2
1599 			dscl_prog_data->easf_h_bf3_pwl_in_set3 =
1600 				0x0BF7;	// FP0.6.6, BF3 Input value PWL Segment 3 (0.0625 * 125^3)
1601 			dscl_prog_data->easf_h_bf3_pwl_base_set3 = 0;	// S0.6, BF3 Base PWL Segment 3
1602 			dscl_prog_data->easf_h_bf3_pwl_slope_set3 =	0x136B;	// FP1.6.6, BF3 Slope PWL Segment 3
1603 			dscl_prog_data->easf_h_bf3_pwl_in_set4 =
1604 				0x0C37;	// FP0.6.6, BF3 Input value PWL Segment 4 (0.125 * 125^3)
1605 			dscl_prog_data->easf_h_bf3_pwl_base_set4 = 0x4E;	// S0.6, BF3 Base PWL Segment 4 = -50
1606 			dscl_prog_data->easf_h_bf3_pwl_slope_set4 = 0x1200;	// FP1.6.6, BF3 Slope PWL Segment 4
1607 			dscl_prog_data->easf_h_bf3_pwl_in_set5 =
1608 				0x0CF7;	// FP0.6.6, BF3 Input value PWL Segment 5 (1.0 * 125^3)
1609 			dscl_prog_data->easf_h_bf3_pwl_base_set5 = 0x41;	// S0.6, BF3 Base PWL Segment 5 = -63
1610 		} else {
1611 			dscl_prog_data->easf_h_bf2_flat1_gain = 13;	// U1.3, BF2 Flat1 Gain control
1612 			dscl_prog_data->easf_h_bf2_flat2_gain = 15;	// U4.0, BF2 Flat2 Gain control
1613 			dscl_prog_data->easf_h_bf2_roc_gain = 14;	// U2.2, Rate Of Change control
1614 
1615 			dscl_prog_data->easf_h_bf1_pwl_in_seg0 = 0x440;	// S0.10, BF1 PWL Segment 0 = -960
1616 			dscl_prog_data->easf_h_bf1_pwl_base_seg0 = 0;	// U0.6, BF1 Base PWL Segment 0
1617 			dscl_prog_data->easf_h_bf1_pwl_slope_seg0 = 2;	// S7.3, BF1 Slope PWL Segment 0
1618 			dscl_prog_data->easf_h_bf1_pwl_in_seg1 = 0x7C4;	// S0.10, BF1 PWL Segment 1 = -60
1619 			dscl_prog_data->easf_h_bf1_pwl_base_seg1 = 12;	// U0.6, BF1 Base PWL Segment 1
1620 			dscl_prog_data->easf_h_bf1_pwl_slope_seg1 = 109;	// S7.3, BF1 Slope PWL Segment 1
1621 			dscl_prog_data->easf_h_bf1_pwl_in_seg2 = 0;	// S0.10, BF1 PWL Segment 2
1622 			dscl_prog_data->easf_h_bf1_pwl_base_seg2 = 63;	// U0.6, BF1 Base PWL Segment 2
1623 			dscl_prog_data->easf_h_bf1_pwl_slope_seg2 = 0;	// S7.3, BF1 Slope PWL Segment 2
1624 			dscl_prog_data->easf_h_bf1_pwl_in_seg3 = 48;	// S0.10, BF1 PWL Segment 3
1625 			dscl_prog_data->easf_h_bf1_pwl_base_seg3 = 63;	// U0.6, BF1 Base PWL Segment 3
1626 			dscl_prog_data->easf_h_bf1_pwl_slope_seg3 = 0x7ED;	// S7.3, BF1 Slope PWL Segment 3 = -19
1627 			dscl_prog_data->easf_h_bf1_pwl_in_seg4 = 96;	// S0.10, BF1 PWL Segment 4
1628 			dscl_prog_data->easf_h_bf1_pwl_base_seg4 = 56;	// U0.6, BF1 Base PWL Segment 4
1629 			dscl_prog_data->easf_h_bf1_pwl_slope_seg4 = 0x7F0;	// S7.3, BF1 Slope PWL Segment 4 = -16
1630 			dscl_prog_data->easf_h_bf1_pwl_in_seg5 = 144;	// S0.10, BF1 PWL Segment 5
1631 			dscl_prog_data->easf_h_bf1_pwl_base_seg5 = 50;	// U0.6, BF1 Base PWL Segment 5
1632 			dscl_prog_data->easf_h_bf1_pwl_slope_seg5 = 0x7B0;	// S7.3, BF1 Slope PWL Segment 5 = -80
1633 			dscl_prog_data->easf_h_bf1_pwl_in_seg6 = 192;	// S0.10, BF1 PWL Segment 6
1634 			dscl_prog_data->easf_h_bf1_pwl_base_seg6 = 20;	// U0.6, BF1 Base PWL Segment 6
1635 			dscl_prog_data->easf_h_bf1_pwl_slope_seg6 = 0x7CB;	// S7.3, BF1 Slope PWL Segment 6 = -53
1636 			dscl_prog_data->easf_h_bf1_pwl_in_seg7 = 240;	// S0.10, BF1 PWL Segment 7
1637 			dscl_prog_data->easf_h_bf1_pwl_base_seg7 = 0;	// U0.6, BF1 Base PWL Segment 7
1638 
1639 			dscl_prog_data->easf_h_bf3_pwl_in_set0 = 0x000;	// FP0.6.6, BF3 Input value PWL Segment 0
1640 			dscl_prog_data->easf_h_bf3_pwl_base_set0 = 63;	// S0.6, BF3 Base PWL Segment 0
1641 			dscl_prog_data->easf_h_bf3_pwl_slope_set0 = 0x0000;	// FP1.6.6, BF3 Slope PWL Segment 0
1642 			dscl_prog_data->easf_h_bf3_pwl_in_set1 =
1643 				0x06C0;	// FP0.6.6, BF3 Input value PWL Segment 1 (0.0625)
1644 			dscl_prog_data->easf_h_bf3_pwl_base_set1 = 63;	// S0.6, BF3 Base PWL Segment 1
1645 			dscl_prog_data->easf_h_bf3_pwl_slope_set1 = 0x1896;	// FP1.6.6, BF3 Slope PWL Segment 1
1646 			dscl_prog_data->easf_h_bf3_pwl_in_set2 =
1647 				0x0700;	// FP0.6.6, BF3 Input value PWL Segment 2 (0.125)
1648 			dscl_prog_data->easf_h_bf3_pwl_base_set2 = 20;	// S0.6, BF3 Base PWL Segment 2
1649 			dscl_prog_data->easf_h_bf3_pwl_slope_set2 = 0x1810;	// FP1.6.6, BF3 Slope PWL Segment 2
1650 			dscl_prog_data->easf_h_bf3_pwl_in_set3 =
1651 				0x0740;	// FP0.6.6, BF3 Input value PWL Segment 3 (0.25)
1652 			dscl_prog_data->easf_h_bf3_pwl_base_set3 = 0;	// S0.6, BF3 Base PWL Segment 3
1653 			dscl_prog_data->easf_h_bf3_pwl_slope_set3 = 0x1878;	// FP1.6.6, BF3 Slope PWL Segment 3
1654 			dscl_prog_data->easf_h_bf3_pwl_in_set4 =
1655 				0x0761;	// FP0.6.6, BF3 Input value PWL Segment 4 (0.375)
1656 			dscl_prog_data->easf_h_bf3_pwl_base_set4 = 0x44;	// S0.6, BF3 Base PWL Segment 4 = -60
1657 			dscl_prog_data->easf_h_bf3_pwl_slope_set4 = 0x1760;	// FP1.6.6, BF3 Slope PWL Segment 4
1658 			dscl_prog_data->easf_h_bf3_pwl_in_set5 =
1659 				0x0780;	// FP0.6.6, BF3 Input value PWL Segment 5 (0.5)
1660 			dscl_prog_data->easf_h_bf3_pwl_base_set5 = 0x41;	// S0.6, BF3 Base PWL Segment 5 = -63
1661 		} // if (lls_pref == LLS_PREF_YES)
1662 	} else
1663 		dscl_prog_data->easf_h_en = false;
1664 
1665 	if (lls_pref == LLS_PREF_YES)	{
1666 		dscl_prog_data->easf_ltonl_en = 1;	// Linear input
1667 		if ((setup == HDR_L) && (spl_is_rgb8(format))) {
1668 			/* Calculate C0-C3 coefficients based on HDR multiplier */
1669 			spl_calculate_c0_c3_hdr(dscl_prog_data, sdr_white_level_nits);
1670 		} else { // HDR_L ( DWM ) and SDR_L
1671 			dscl_prog_data->easf_matrix_c0 =
1672 				0x4EF7;	// fp1.5.10, C0 coefficient (LN_rec709:  0.2126 * (2^14)/125 = 27.86590720)
1673 			dscl_prog_data->easf_matrix_c1 =
1674 				0x55DC;	// fp1.5.10, C1 coefficient (LN_rec709:  0.7152 * (2^14)/125 = 93.74269440)
1675 			dscl_prog_data->easf_matrix_c2 =
1676 				0x48BB;	// fp1.5.10, C2 coefficient (LN_rec709:  0.0722 * (2^14)/125 = 9.46339840)
1677 			dscl_prog_data->easf_matrix_c3 =
1678 				0x0;	// fp1.5.10, C3 coefficient
1679 		}
1680 	}	else	{
1681 		dscl_prog_data->easf_ltonl_en = 0;	// Non-Linear input
1682 		dscl_prog_data->easf_matrix_c0 =
1683 			0x3434;	// fp1.5.10, C0 coefficient (LN_BT2020:  0.262695312500000)
1684 		dscl_prog_data->easf_matrix_c1 =
1685 			0x396D;	// fp1.5.10, C1 coefficient (LN_BT2020:  0.678222656250000)
1686 		dscl_prog_data->easf_matrix_c2 =
1687 			0x2B97;	// fp1.5.10, C2 coefficient (LN_BT2020:  0.059295654296875)
1688 		dscl_prog_data->easf_matrix_c3 =
1689 			0x0;	// fp1.5.10, C3 coefficient
1690 	}
1691 
1692 	if (spl_is_subsampled_format(format)) { /* TODO: 0 = RGB, 1 = YUV */
1693 		dscl_prog_data->easf_matrix_mode = 1;
1694 		/*
1695 		 * 2-bit, BF3 chroma mode correction calculation mode
1696 		 * Needs to be disabled for YUV420 mode
1697 		 * Override lookup value
1698 		 */
1699 		dscl_prog_data->easf_v_bf3_mode = 0;
1700 		dscl_prog_data->easf_h_bf3_mode = 0;
1701 	} else
1702 		dscl_prog_data->easf_matrix_mode = 0;
1703 
1704 }
1705 
1706 /*Set isharp noise detection */
1707 static void spl_set_isharp_noise_det_mode(struct dscl_prog_data *dscl_prog_data,
1708 	const struct spl_scaler_data *data)
1709 {
1710 	// ISHARP_NOISEDET_MODE
1711 	// 0: 3x5 as VxH
1712 	// 1: 4x5 as VxH
1713 	// 2:
1714 	// 3: 5x5 as VxH
1715 	if (data->taps.v_taps == 6)
1716 		dscl_prog_data->isharp_noise_det.mode = 3;
1717 	else if (data->taps.v_taps == 4)
1718 		dscl_prog_data->isharp_noise_det.mode = 1;
1719 	else if (data->taps.v_taps == 3)
1720 		dscl_prog_data->isharp_noise_det.mode = 0;
1721 };
1722 /* Set Sharpener data */
1723 static void spl_set_isharp_data(struct dscl_prog_data *dscl_prog_data,
1724 		struct adaptive_sharpness adp_sharpness, bool enable_isharp,
1725 		enum linear_light_scaling lls_pref, enum spl_pixel_format format,
1726 		const struct spl_scaler_data *data, struct spl_fixed31_32 ratio,
1727 		enum system_setup setup, enum scale_to_sharpness_policy scale_to_sharpness_policy)
1728 {
1729 	(void)format;
1730 	/* Turn off sharpener if not required */
1731 	if (!enable_isharp) {
1732 		dscl_prog_data->isharp_en = 0;
1733 		return;
1734 	}
1735 
1736 	SPL_NAMESPACE(spl_build_isharp_1dlut_from_reference_curve(ratio, setup, adp_sharpness,
1737 		scale_to_sharpness_policy));
1738 	memcpy(dscl_prog_data->isharp_delta, SPL_NAMESPACE(spl_get_pregen_filter_isharp_1D_lut(setup)),
1739 		sizeof(uint32_t) * ISHARP_LUT_TABLE_SIZE);
1740 	dscl_prog_data->sharpness_level = adp_sharpness.sharpness_level;
1741 
1742 	dscl_prog_data->isharp_en = 1;	// ISHARP_EN
1743 	// Set ISHARP_NOISEDET_MODE if htaps = 6-tap
1744 	if (data->taps.h_taps == 6) {
1745 		dscl_prog_data->isharp_noise_det.enable = 1;	/* ISHARP_NOISEDET_EN */
1746 		spl_set_isharp_noise_det_mode(dscl_prog_data, data);	/* ISHARP_NOISEDET_MODE */
1747 	} else
1748 		dscl_prog_data->isharp_noise_det.enable = 0;	// ISHARP_NOISEDET_EN
1749 	// Program noise detection threshold
1750 	dscl_prog_data->isharp_noise_det.uthreshold = 24;	// ISHARP_NOISEDET_UTHRE
1751 	dscl_prog_data->isharp_noise_det.dthreshold = 4;	// ISHARP_NOISEDET_DTHRE
1752 	// Program noise detection gain
1753 	dscl_prog_data->isharp_noise_det.pwl_start_in = 3;	// ISHARP_NOISEDET_PWL_START_IN
1754 	dscl_prog_data->isharp_noise_det.pwl_end_in = 13;	// ISHARP_NOISEDET_PWL_END_IN
1755 	dscl_prog_data->isharp_noise_det.pwl_slope = 1623;	// ISHARP_NOISEDET_PWL_SLOPE
1756 
1757 	if (lls_pref == LLS_PREF_NO) /* ISHARP_FMT_MODE */
1758 		dscl_prog_data->isharp_fmt.mode = 1;
1759 	else
1760 		dscl_prog_data->isharp_fmt.mode = 0;
1761 
1762 	dscl_prog_data->isharp_fmt.norm = 0x3C00;	// ISHARP_FMT_NORM
1763 	dscl_prog_data->isharp_lba.mode = 0;	// ISHARP_LBA_MODE
1764 
1765 	if (setup == SDR_L) {
1766 		// ISHARP_LBA_PWL_SEG0: ISHARP Local Brightness Adjustment PWL Segment 0
1767 		dscl_prog_data->isharp_lba.in_seg[0] = 0;	// ISHARP LBA PWL for Seg 0. INPUT value in U0.10 format
1768 		dscl_prog_data->isharp_lba.base_seg[0] = 0;	// ISHARP LBA PWL for Seg 0. BASE value in U0.6 format
1769 		dscl_prog_data->isharp_lba.slope_seg[0] = 62;	// ISHARP LBA for Seg 0. SLOPE value in S5.3 format
1770 		// ISHARP_LBA_PWL_SEG1: ISHARP LBA PWL Segment 1
1771 		dscl_prog_data->isharp_lba.in_seg[1] = 130;	// ISHARP LBA PWL for Seg 1. INPUT value in U0.10 format
1772 		dscl_prog_data->isharp_lba.base_seg[1] = 63; // ISHARP LBA PWL for Seg 1. BASE value in U0.6 format
1773 		dscl_prog_data->isharp_lba.slope_seg[1] = 0; // ISHARP LBA for Seg 1. SLOPE value in S5.3 format
1774 		// ISHARP_LBA_PWL_SEG2: ISHARP LBA PWL Segment 2
1775 		dscl_prog_data->isharp_lba.in_seg[2] = 450; // ISHARP LBA PWL for Seg 2. INPUT value in U0.10 format
1776 		dscl_prog_data->isharp_lba.base_seg[2] = 63; // ISHARP LBA PWL for Seg 2. BASE value in U0.6 format
1777 		dscl_prog_data->isharp_lba.slope_seg[2] = 0x18D; // ISHARP LBA for Seg 2. SLOPE value in S5.3 format = -115
1778 		// ISHARP_LBA_PWL_SEG3: ISHARP LBA PWL Segment 3
1779 		dscl_prog_data->isharp_lba.in_seg[3] = 520; // ISHARP LBA PWL for Seg 3.INPUT value in U0.10 format
1780 		dscl_prog_data->isharp_lba.base_seg[3] = 0; // ISHARP LBA PWL for Seg 3. BASE value in U0.6 format
1781 		dscl_prog_data->isharp_lba.slope_seg[3] = 0; // ISHARP LBA for Seg 3. SLOPE value in S5.3 format
1782 		// ISHARP_LBA_PWL_SEG4: ISHARP LBA PWL Segment 4
1783 		dscl_prog_data->isharp_lba.in_seg[4] = 520; // ISHARP LBA PWL for Seg 4.INPUT value in U0.10 format
1784 		dscl_prog_data->isharp_lba.base_seg[4] = 0; // ISHARP LBA PWL for Seg 4. BASE value in U0.6 format
1785 		dscl_prog_data->isharp_lba.slope_seg[4] = 0; // ISHARP LBA for Seg 4. SLOPE value in S5.3 format
1786 		// ISHARP_LBA_PWL_SEG5: ISHARP LBA PWL Segment 5
1787 		dscl_prog_data->isharp_lba.in_seg[5] = 520; // ISHARP LBA PWL for Seg 5.INPUT value in U0.10 format
1788 		dscl_prog_data->isharp_lba.base_seg[5] = 0;	// ISHARP LBA PWL for Seg 5. BASE value in U0.6 format
1789 	} else if (setup == HDR_L) {
1790 		// ISHARP_LBA_PWL_SEG0: ISHARP Local Brightness Adjustment PWL Segment 0
1791 		dscl_prog_data->isharp_lba.in_seg[0] = 0;	// ISHARP LBA PWL for Seg 0. INPUT value in U0.10 format
1792 		dscl_prog_data->isharp_lba.base_seg[0] = 0;	// ISHARP LBA PWL for Seg 0. BASE value in U0.6 format
1793 		dscl_prog_data->isharp_lba.slope_seg[0] = 32;	// ISHARP LBA for Seg 0. SLOPE value in S5.3 format
1794 		// ISHARP_LBA_PWL_SEG1: ISHARP LBA PWL Segment 1
1795 		dscl_prog_data->isharp_lba.in_seg[1] = 254;	// ISHARP LBA PWL for Seg 1. INPUT value in U0.10 format
1796 		dscl_prog_data->isharp_lba.base_seg[1] = 63; // ISHARP LBA PWL for Seg 1. BASE value in U0.6 format
1797 		dscl_prog_data->isharp_lba.slope_seg[1] = 0; // ISHARP LBA for Seg 1. SLOPE value in S5.3 format
1798 		// ISHARP_LBA_PWL_SEG2: ISHARP LBA PWL Segment 2
1799 		dscl_prog_data->isharp_lba.in_seg[2] = 559; // ISHARP LBA PWL for Seg 2. INPUT value in U0.10 format
1800 		dscl_prog_data->isharp_lba.base_seg[2] = 63; // ISHARP LBA PWL for Seg 2. BASE value in U0.6 format
1801 		dscl_prog_data->isharp_lba.slope_seg[2] = 0x10C; // ISHARP LBA for Seg 2. SLOPE value in S5.3 format = -244
1802 		// ISHARP_LBA_PWL_SEG3: ISHARP LBA PWL Segment 3
1803 		dscl_prog_data->isharp_lba.in_seg[3] = 592; // ISHARP LBA PWL for Seg 3.INPUT value in U0.10 format
1804 		dscl_prog_data->isharp_lba.base_seg[3] = 0; // ISHARP LBA PWL for Seg 3. BASE value in U0.6 format
1805 		dscl_prog_data->isharp_lba.slope_seg[3] = 0; // ISHARP LBA for Seg 3. SLOPE value in S5.3 format
1806 		// ISHARP_LBA_PWL_SEG4: ISHARP LBA PWL Segment 4
1807 		dscl_prog_data->isharp_lba.in_seg[4] = 1023; // ISHARP LBA PWL for Seg 4.INPUT value in U0.10 format
1808 		dscl_prog_data->isharp_lba.base_seg[4] = 0; // ISHARP LBA PWL for Seg 4. BASE value in U0.6 format
1809 		dscl_prog_data->isharp_lba.slope_seg[4] = 0; // ISHARP LBA for Seg 4. SLOPE value in S5.3 format
1810 		// ISHARP_LBA_PWL_SEG5: ISHARP LBA PWL Segment 5
1811 		dscl_prog_data->isharp_lba.in_seg[5] = 1023; // ISHARP LBA PWL for Seg 5.INPUT value in U0.10 format
1812 		dscl_prog_data->isharp_lba.base_seg[5] = 0;	// ISHARP LBA PWL for Seg 5. BASE value in U0.6 format
1813 	} else {
1814 		// ISHARP_LBA_PWL_SEG0: ISHARP Local Brightness Adjustment PWL Segment 0
1815 		dscl_prog_data->isharp_lba.in_seg[0] = 0;	// ISHARP LBA PWL for Seg 0. INPUT value in U0.10 format
1816 		dscl_prog_data->isharp_lba.base_seg[0] = 0;	// ISHARP LBA PWL for Seg 0. BASE value in U0.6 format
1817 		dscl_prog_data->isharp_lba.slope_seg[0] = 40;	// ISHARP LBA for Seg 0. SLOPE value in S5.3 format
1818 		// ISHARP_LBA_PWL_SEG1: ISHARP LBA PWL Segment 1
1819 		dscl_prog_data->isharp_lba.in_seg[1] = 204;	// ISHARP LBA PWL for Seg 1. INPUT value in U0.10 format
1820 		dscl_prog_data->isharp_lba.base_seg[1] = 63; // ISHARP LBA PWL for Seg 1. BASE value in U0.6 format
1821 		dscl_prog_data->isharp_lba.slope_seg[1] = 0; // ISHARP LBA for Seg 1. SLOPE value in S5.3 format
1822 		// ISHARP_LBA_PWL_SEG2: ISHARP LBA PWL Segment 2
1823 		dscl_prog_data->isharp_lba.in_seg[2] = 818; // ISHARP LBA PWL for Seg 2. INPUT value in U0.10 format
1824 		dscl_prog_data->isharp_lba.base_seg[2] = 63; // ISHARP LBA PWL for Seg 2. BASE value in U0.6 format
1825 		dscl_prog_data->isharp_lba.slope_seg[2] = 0x1D9; // ISHARP LBA for Seg 2. SLOPE value in S5.3 format = -39
1826 		// ISHARP_LBA_PWL_SEG3: ISHARP LBA PWL Segment 3
1827 		dscl_prog_data->isharp_lba.in_seg[3] = 1023; // ISHARP LBA PWL for Seg 3.INPUT value in U0.10 format
1828 		dscl_prog_data->isharp_lba.base_seg[3] = 0; // ISHARP LBA PWL for Seg 3. BASE value in U0.6 format
1829 		dscl_prog_data->isharp_lba.slope_seg[3] = 0; // ISHARP LBA for Seg 3. SLOPE value in S5.3 format
1830 		// ISHARP_LBA_PWL_SEG4: ISHARP LBA PWL Segment 4
1831 		dscl_prog_data->isharp_lba.in_seg[4] = 1023; // ISHARP LBA PWL for Seg 4.INPUT value in U0.10 format
1832 		dscl_prog_data->isharp_lba.base_seg[4] = 0; // ISHARP LBA PWL for Seg 4. BASE value in U0.6 format
1833 		dscl_prog_data->isharp_lba.slope_seg[4] = 0; // ISHARP LBA for Seg 4. SLOPE value in S5.3 format
1834 		// ISHARP_LBA_PWL_SEG5: ISHARP LBA PWL Segment 5
1835 		dscl_prog_data->isharp_lba.in_seg[5] = 1023; // ISHARP LBA PWL for Seg 5.INPUT value in U0.10 format
1836 		dscl_prog_data->isharp_lba.base_seg[5] = 0;	// ISHARP LBA PWL for Seg 5. BASE value in U0.6 format
1837 	}
1838 
1839 	// Program the nldelta soft clip values
1840 	if (lls_pref == LLS_PREF_YES) {
1841 		dscl_prog_data->isharp_nldelta_sclip.enable_p = 0;	/* ISHARP_NLDELTA_SCLIP_EN_P */
1842 		dscl_prog_data->isharp_nldelta_sclip.pivot_p = 0;	/* ISHARP_NLDELTA_SCLIP_PIVOT_P */
1843 		dscl_prog_data->isharp_nldelta_sclip.slope_p = 0;	/* ISHARP_NLDELTA_SCLIP_SLOPE_P */
1844 		dscl_prog_data->isharp_nldelta_sclip.enable_n = 1;	/* ISHARP_NLDELTA_SCLIP_EN_N */
1845 		dscl_prog_data->isharp_nldelta_sclip.pivot_n = 71;	/* ISHARP_NLDELTA_SCLIP_PIVOT_N */
1846 		dscl_prog_data->isharp_nldelta_sclip.slope_n = 16;	/* ISHARP_NLDELTA_SCLIP_SLOPE_N */
1847 	} else {
1848 		dscl_prog_data->isharp_nldelta_sclip.enable_p = 1;	/* ISHARP_NLDELTA_SCLIP_EN_P */
1849 		dscl_prog_data->isharp_nldelta_sclip.pivot_p = 70;	/* ISHARP_NLDELTA_SCLIP_PIVOT_P */
1850 		dscl_prog_data->isharp_nldelta_sclip.slope_p = 24;	/* ISHARP_NLDELTA_SCLIP_SLOPE_P */
1851 		dscl_prog_data->isharp_nldelta_sclip.enable_n = 1;	/* ISHARP_NLDELTA_SCLIP_EN_N */
1852 		dscl_prog_data->isharp_nldelta_sclip.pivot_n = 70;	/* ISHARP_NLDELTA_SCLIP_PIVOT_N */
1853 		dscl_prog_data->isharp_nldelta_sclip.slope_n = 24;	/* ISHARP_NLDELTA_SCLIP_SLOPE_N */
1854 	}
1855 
1856 	// Set the values as per lookup table
1857 	SPL_NAMESPACE(spl_set_blur_scale_data(dscl_prog_data, data));
1858 }
1859 
1860 static void determine_upsp_values(struct spl_in *spl_in, struct dscl_prog_data *dscl_prog_data)
1861 {
1862 	dscl_prog_data->upsp_mode = spl_in->upsp_mode;
1863 
1864 	if (dscl_prog_data->upsp_mode == UPSP_BYPASS) { //Set all UPSP register fields to 0 if bypass
1865 		dscl_prog_data->upsp_v_num_taps = UPSP_2_TAPS;
1866 		dscl_prog_data->upsp_h_num_taps = UPSP_2_TAPS;
1867 		dscl_prog_data->upsp_boundary_mode = UPSP_BOUNDARY_EDGE;
1868 		dscl_prog_data->upsp_v_init_int = 0x0;
1869 		dscl_prog_data->upsp_v_init_frac = 0x0;
1870 		dscl_prog_data->upsp_v_coef_tap0_p0 = 0x0;
1871 		dscl_prog_data->upsp_v_coef_tap1_p0 = 0x0;
1872 		dscl_prog_data->upsp_v_coef_tap2_p0 = 0x0;
1873 		dscl_prog_data->upsp_v_coef_tap3_p0 = 0x0;
1874 		dscl_prog_data->upsp_v_coef_tap0_p1 = 0x0;
1875 		dscl_prog_data->upsp_v_coef_tap1_p1 = 0x0;
1876 		dscl_prog_data->upsp_v_coef_tap2_p1 = 0x0;
1877 		dscl_prog_data->upsp_v_coef_tap3_p1 = 0x0;
1878 		dscl_prog_data->upsp_h_init_int = 0x0;
1879 		dscl_prog_data->upsp_h_init_frac = 0x0;
1880 		dscl_prog_data->upsp_h_coef_tap0_p0 = 0x0;
1881 		dscl_prog_data->upsp_h_coef_tap1_p0 = 0x0;
1882 		dscl_prog_data->upsp_h_coef_tap2_p0 = 0x0;
1883 		dscl_prog_data->upsp_h_coef_tap3_p0 = 0x0;
1884 		dscl_prog_data->upsp_h_coef_tap0_p1 = 0x0;
1885 		dscl_prog_data->upsp_h_coef_tap1_p1 = 0x0;
1886 		dscl_prog_data->upsp_h_coef_tap2_p1 = 0x0;
1887 		dscl_prog_data->upsp_h_coef_tap3_p1 = 0x0;
1888 		dscl_prog_data->upsp_clamp_max = 0x0;
1889 		dscl_prog_data->upsp_clamp_min = 0x0;
1890 	} else {
1891 		dscl_prog_data->upsp_v_num_taps = UPSP_4_TAPS;
1892 		dscl_prog_data->upsp_h_num_taps = UPSP_4_TAPS;
1893 		dscl_prog_data->upsp_boundary_mode = UPSP_BOUNDARY_EDGE;
1894 		dscl_prog_data->upsp_clamp_max = 0xFFF;//4095
1895 		dscl_prog_data->upsp_clamp_min = 0x0;
1896 
1897 		if (spl_in->basic_in.cositing == CHROMA_COSITING_TOPLEFT) { //Vertical Subsampling: Co-sited
1898 			if (dscl_prog_data->upsp_v_num_taps == UPSP_4_TAPS) {
1899 				dscl_prog_data->upsp_v_init_int = 0x3;
1900 				dscl_prog_data->upsp_v_init_frac = 0x0;
1901 				dscl_prog_data->upsp_v_coef_tap0_p0 = 0x00;
1902 				dscl_prog_data->upsp_v_coef_tap1_p0 = 0x40;
1903 				dscl_prog_data->upsp_v_coef_tap2_p0 = 0x00;
1904 				dscl_prog_data->upsp_v_coef_tap3_p0 = 0x00;
1905 				dscl_prog_data->upsp_v_coef_tap0_p1 = 0xFC;
1906 				dscl_prog_data->upsp_v_coef_tap1_p1 = 0x24;
1907 				dscl_prog_data->upsp_v_coef_tap2_p1 = 0x24;
1908 				dscl_prog_data->upsp_v_coef_tap3_p1 = 0xFC;
1909 			} else { //2 taps
1910 				dscl_prog_data->upsp_v_init_int = 0x2;
1911 				dscl_prog_data->upsp_v_init_frac = 0x0;
1912 				dscl_prog_data->upsp_v_coef_tap0_p0 = 0x40;
1913 				dscl_prog_data->upsp_v_coef_tap1_p0 = 0x00;
1914 				dscl_prog_data->upsp_v_coef_tap2_p0 = 0x00;
1915 				dscl_prog_data->upsp_v_coef_tap3_p0 = 0x00;
1916 				dscl_prog_data->upsp_v_coef_tap0_p1 = 0x20;
1917 				dscl_prog_data->upsp_v_coef_tap1_p1 = 0x20;
1918 				dscl_prog_data->upsp_v_coef_tap2_p1 = 0x00;
1919 				dscl_prog_data->upsp_v_coef_tap3_p1 = 0x00;
1920 			}
1921 		} else { //Vertical Subsampling: Interstitial
1922 			if (dscl_prog_data->upsp_v_num_taps == UPSP_4_TAPS) {
1923 				dscl_prog_data->upsp_v_init_int = 0x2;
1924 				dscl_prog_data->upsp_v_init_frac = 0x1;
1925 				dscl_prog_data->upsp_v_coef_tap0_p0 = 0xFB;
1926 				dscl_prog_data->upsp_v_coef_tap1_p0 = 0x2F;
1927 				dscl_prog_data->upsp_v_coef_tap2_p0 = 0x19;
1928 				dscl_prog_data->upsp_v_coef_tap3_p0 = 0xFD;
1929 				dscl_prog_data->upsp_v_coef_tap0_p1 = 0xFD;
1930 				dscl_prog_data->upsp_v_coef_tap1_p1 = 0x19;
1931 				dscl_prog_data->upsp_v_coef_tap2_p1 = 0x2F;
1932 				dscl_prog_data->upsp_v_coef_tap3_p1 = 0xFB;
1933 			} else { //2 taps
1934 				dscl_prog_data->upsp_v_init_int = 0x1;
1935 				dscl_prog_data->upsp_v_init_frac = 0x1;
1936 				dscl_prog_data->upsp_v_coef_tap0_p0 = 0x28;
1937 				dscl_prog_data->upsp_v_coef_tap1_p0 = 0x18;
1938 				dscl_prog_data->upsp_v_coef_tap2_p0 = 0x00;
1939 				dscl_prog_data->upsp_v_coef_tap3_p0 = 0x00;
1940 				dscl_prog_data->upsp_v_coef_tap0_p1 = 0x18;
1941 				dscl_prog_data->upsp_v_coef_tap1_p1 = 0x28;
1942 				dscl_prog_data->upsp_v_coef_tap2_p1 = 0x00;
1943 				dscl_prog_data->upsp_v_coef_tap3_p1 = 0x00;
1944 			}
1945 		}
1946 		if (spl_in->basic_in.cositing == CHROMA_COSITING_LEFT || spl_in->basic_in.cositing == CHROMA_COSITING_TOPLEFT) { //Horizontal Subsampling: Co-sited
1947 			if (dscl_prog_data->upsp_h_num_taps == UPSP_4_TAPS) {
1948 				dscl_prog_data->upsp_h_init_int = 0x3;
1949 				dscl_prog_data->upsp_h_init_frac = 0x0;
1950 				dscl_prog_data->upsp_h_coef_tap0_p0 = 0x00;
1951 				dscl_prog_data->upsp_h_coef_tap1_p0 = 0x40;
1952 				dscl_prog_data->upsp_h_coef_tap2_p0 = 0x00;
1953 				dscl_prog_data->upsp_h_coef_tap3_p0 = 0x00;
1954 				dscl_prog_data->upsp_h_coef_tap0_p1 = 0xFC;
1955 				dscl_prog_data->upsp_h_coef_tap1_p1 = 0x24;
1956 				dscl_prog_data->upsp_h_coef_tap2_p1 = 0x24;
1957 				dscl_prog_data->upsp_h_coef_tap3_p1 = 0xFC;
1958 			} else { //2 taps
1959 				dscl_prog_data->upsp_h_init_int = 0x2;
1960 				dscl_prog_data->upsp_h_init_frac = 0x0;
1961 				dscl_prog_data->upsp_h_coef_tap0_p0 = 0x40;
1962 				dscl_prog_data->upsp_h_coef_tap1_p0 = 0x00;
1963 				dscl_prog_data->upsp_h_coef_tap2_p0 = 0x00;
1964 				dscl_prog_data->upsp_h_coef_tap3_p0 = 0x00;
1965 				dscl_prog_data->upsp_h_coef_tap0_p1 = 0x20;
1966 				dscl_prog_data->upsp_h_coef_tap1_p1 = 0x20;
1967 				dscl_prog_data->upsp_h_coef_tap2_p1 = 0x00;
1968 				dscl_prog_data->upsp_h_coef_tap3_p1 = 0x00;
1969 			}
1970 		} else { //Horizontal Subsampling: Interstitial
1971 			if (dscl_prog_data->upsp_h_num_taps == UPSP_4_TAPS) {
1972 				dscl_prog_data->upsp_h_init_int = 0x2;
1973 				dscl_prog_data->upsp_h_init_frac = 0x1;
1974 				dscl_prog_data->upsp_h_coef_tap0_p0 = 0xFB;
1975 				dscl_prog_data->upsp_h_coef_tap1_p0 = 0x2F;
1976 				dscl_prog_data->upsp_h_coef_tap2_p0 = 0x19;
1977 				dscl_prog_data->upsp_h_coef_tap3_p0 = 0xFD;
1978 				dscl_prog_data->upsp_h_coef_tap0_p1 = 0xFD;
1979 				dscl_prog_data->upsp_h_coef_tap1_p1 = 0x19;
1980 				dscl_prog_data->upsp_h_coef_tap2_p1 = 0x2F;
1981 				dscl_prog_data->upsp_h_coef_tap3_p1 = 0xFB;
1982 			} else { //2 taps
1983 				dscl_prog_data->upsp_h_init_int = 0x1;
1984 				dscl_prog_data->upsp_h_init_frac = 0x1;
1985 				dscl_prog_data->upsp_h_coef_tap0_p0 = 0x28;
1986 				dscl_prog_data->upsp_h_coef_tap1_p0 = 0x18;
1987 				dscl_prog_data->upsp_h_coef_tap2_p0 = 0x00;
1988 				dscl_prog_data->upsp_h_coef_tap3_p0 = 0x00;
1989 				dscl_prog_data->upsp_h_coef_tap0_p1 = 0x18;
1990 				dscl_prog_data->upsp_h_coef_tap1_p1 = 0x28;
1991 				dscl_prog_data->upsp_h_coef_tap2_p1 = 0x00;
1992 				dscl_prog_data->upsp_h_coef_tap3_p1 = 0x00;
1993 			}
1994 		}
1995 	}
1996 }
1997 
1998 /* Calculate recout, scaling ratio, and viewport, then get optimal number of taps */
1999 static bool spl_calculate_number_of_taps(struct spl_in *spl_in, struct spl_scratch *spl_scratch, struct spl_out *spl_out,
2000 	bool *enable_easf_v, bool *enable_easf_h, bool *enable_isharp)
2001 {
2002 	bool res = false;
2003 
2004 	memset(spl_scratch, 0, sizeof(struct spl_scratch));
2005 	spl_scratch->scl_data.h_active = spl_in->h_active;
2006 	spl_scratch->scl_data.v_active = spl_in->v_active;
2007 
2008 	// All SPL calls
2009 	/* recout calculation */
2010 	/* depends on h_active */
2011 	spl_calculate_recout(spl_in, spl_scratch, spl_out);
2012 	/* depends on pixel format */
2013 	spl_calculate_scaling_ratios(spl_in, spl_scratch, spl_out);
2014 	/* Adjust recout for opp if needed */
2015 	spl_opp_adjust_rect(&spl_scratch->scl_data.recout, &spl_in->basic_in.opp_recout_adjust);
2016 	/* depends on scaling ratios and recout, does not calculate offset yet */
2017 	spl_calculate_viewport_size(spl_in, spl_scratch);
2018 
2019 	res = spl_get_optimal_number_of_taps(
2020 			  spl_in->basic_out.max_downscale_src_width, spl_in,
2021 			  spl_scratch, &spl_in->scaling_quality, enable_easf_v,
2022 			  enable_easf_h, enable_isharp);
2023 	return res;
2024 }
2025 
2026 /* Calculate scaler parameters */
2027 bool SPL_NAMESPACE(spl_calculate_scaler_params(struct spl_in *spl_in, struct spl_out *spl_out))
2028 {
2029 	bool res = false;
2030 	bool enable_easf_v = false;
2031 	bool enable_easf_h = false;
2032 	int vratio = 0;
2033 	int hratio = 0;
2034 	struct spl_scratch spl_scratch;
2035 	struct spl_fixed31_32 isharp_scale_ratio;
2036 	enum system_setup setup;
2037 	bool enable_isharp = false;
2038 	const struct spl_scaler_data *data = &spl_scratch.scl_data;
2039 
2040 	determine_upsp_values(spl_in, spl_out->dscl_prog_data);
2041 
2042 	res = spl_calculate_number_of_taps(spl_in, &spl_scratch, spl_out,
2043 		&enable_easf_v, &enable_easf_h, &enable_isharp);
2044 
2045 	/*
2046 	 * Depends on recout, scaling ratios, h_active and taps
2047 	 * May need to re-check lb size after this in some obscure scenario
2048 	 */
2049 	if (res)
2050 		spl_calculate_inits_and_viewports(spl_in, &spl_scratch);
2051 	// Handle 3d recout
2052 	spl_handle_3d_recout(spl_in, &spl_scratch.scl_data.recout);
2053 	// Clamp
2054 	spl_clamp_viewport(&spl_scratch.scl_data.viewport, spl_in->min_viewport_size);
2055 
2056 	// Save all calculated parameters in dscl_prog_data structure to program hw registers
2057 	spl_set_dscl_prog_data(spl_in, &spl_scratch, spl_out, enable_easf_v, enable_easf_h, enable_isharp);
2058 
2059 	if (!res)
2060 		return res;
2061 
2062 	if (spl_in->lls_pref == LLS_PREF_YES) {
2063 		if (spl_in->is_hdr_on)
2064 			setup = HDR_L;
2065 		else
2066 			setup = SDR_L;
2067 	} else {
2068 		if (spl_in->is_hdr_on)
2069 			setup = HDR_NL;
2070 		else
2071 			setup = SDR_NL;
2072 	}
2073 
2074 	// Set EASF
2075 	spl_set_easf_data(&spl_scratch, spl_out, enable_easf_v, enable_easf_h, spl_in->lls_pref,
2076 		spl_in->basic_in.format, setup, spl_in->sdr_white_level_nits);
2077 
2078 	// Set iSHARP
2079 	vratio = spl_fixpt_ceil(spl_scratch.scl_data.ratios.vert);
2080 	hratio = spl_fixpt_ceil(spl_scratch.scl_data.ratios.horz);
2081 	if (vratio <= hratio)
2082 		isharp_scale_ratio = spl_scratch.scl_data.recip_ratios.vert;
2083 	else
2084 		isharp_scale_ratio = spl_scratch.scl_data.recip_ratios.horz;
2085 
2086 	spl_set_isharp_data(spl_out->dscl_prog_data, spl_in->adaptive_sharpness, enable_isharp,
2087 		spl_in->lls_pref, spl_in->basic_in.format, data, isharp_scale_ratio, setup,
2088 		spl_in->debug.scale_to_sharpness_policy);
2089 
2090 	return res;
2091 }
2092 
2093 /* External interface to get number of taps only */
2094 bool SPL_NAMESPACE(spl_get_number_of_taps(struct spl_in *spl_in, struct spl_out *spl_out))
2095 {
2096 	bool res = false;
2097 	bool enable_easf_v = false;
2098 	bool enable_easf_h = false;
2099 	bool enable_isharp = false;
2100 	struct spl_scratch spl_scratch;
2101 	struct dscl_prog_data *dscl_prog_data = spl_out->dscl_prog_data;
2102 	const struct spl_scaler_data *data = &spl_scratch.scl_data;
2103 
2104 	res = spl_calculate_number_of_taps(spl_in, &spl_scratch, spl_out,
2105 		&enable_easf_v, &enable_easf_h, &enable_isharp);
2106 	spl_set_taps_data(dscl_prog_data, data);
2107 	return res;
2108 }
2109