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