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