xref: /linux/drivers/gpu/drm/amd/display/modules/power/power_abm.c (revision 5c9deba5578db5a3ea05be8a3e4a59ecb08ee76f)
1 // SPDX-License-Identifier: MIT
2 //
3 // Copyright 2026 Advanced Micro Devices, Inc.
4 
5 #include "dm_services.h"
6 #include "dc.h"
7 #include "mod_power.h"
8 #include "core_types.h"
9 #include "dmcu.h"
10 #include "abm.h"
11 #include "power_helpers.h"
12 #include "dce/dmub_psr.h"
13 #include "dal_asic_id.h"
14 #include "link_service.h"
15 #include <linux/math.h>
16 
17 #define DC_TRACE_LEVEL_MESSAGE(...) /* do nothing */
18 #define DC_TRACE_LEVEL_MESSAGEP(...) /* do nothing */
19 
20 #define DIV_ROUNDUP(a, b) (((a)+((b)/2))/(b))
21 #define bswap16_based_on_endian(big_endian, value) \
22 	((big_endian) ? cpu_to_be16(value) : cpu_to_le16(value))
23 
24 /* Possible Min Reduction config from least aggressive to most aggressive
25  *  0    1     2     3     4     5     6     7     8     9     10    11   12
26  * 100  98.0 94.1  94.1  85.1  80.3  75.3  69.4  60.0  57.6  50.2  49.8  40.0 %
27  */
28 static const unsigned char min_reduction_table[13] = {
29 0xff, 0xfa, 0xf0, 0xf0, 0xd9, 0xcd, 0xc0, 0xb1, 0x99, 0x93, 0x80, 0x82, 0x66};
30 
31 /* Possible Max Reduction configs from least aggressive to most aggressive
32  *  0    1     2     3     4     5     6     7     8     9     10    11   12
33  * 96.1 89.8 85.1  80.3  69.4  64.7  64.7  50.2  39.6  30.2  30.2  30.2  19.6 %
34  */
35 static const unsigned char max_reduction_table[13] = {
36 0xf5, 0xe5, 0xd9, 0xcd, 0xb1, 0xa5, 0xa5, 0x80, 0x65, 0x4d, 0x4d, 0x4d, 0x32};
37 
38 /* Possible ABM 2.2 Min Reduction configs from least aggressive to most aggressive
39  *  0    1     2     3     4     5     6     7     8     9     10    11   12
40  * 100  100   100   100   100   100   100   100  100  92.2  83.1  75.3  75.3 %
41  */
42 static const unsigned char min_reduction_table_v_2_2[13] = {
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xeb, 0xd4, 0xc0, 0xc0};
44 
45 /* Possible ABM 2.2 Max Reduction configs from least aggressive to most aggressive
46  *  0    1     2     3     4     5     6     7     8     9     10    11   12
47  * 96.1 89.8 74.9  69.4  64.7  52.2  48.6  39.6  30.2  25.1  19.6  12.5  12.5 %
48  */
49 static const unsigned char max_reduction_table_v_2_2[13] = {
50 0xf5, 0xe5, 0xbf, 0xb1, 0xa5, 0x85, 0x7c, 0x65, 0x4d, 0x40, 0x32, 0x20, 0x20};
51 
52 /* Predefined ABM configuration sets. We may have different configuration sets
53  * in order to satisfy different power/quality requirements.
54  */
55 static const unsigned char abm_config[abm_defines_max_config][abm_defines_max_level] = {
56 /*  ABM Level 1,    ABM Level 2,    ABM Level 3,    ABM Level 4 */
57 {       2,              5,              7,              8       },	/* Default - Medium aggressiveness */
58 {       2,              5,              8,              11      },	/* Alt #1  - Increased aggressiveness */
59 {       0,              2,              4,              8       },	/* Alt #2  - Minimal aggressiveness */
60 {       3,              6,              10,             12      },	/* Alt #3  - Super aggressiveness */
61 };
62 
63 struct abm_parameters {
64 	unsigned char min_reduction;
65 	unsigned char max_reduction;
66 	unsigned char bright_pos_gain;
67 	unsigned char dark_pos_gain;
68 	unsigned char brightness_gain;
69 	unsigned char contrast_factor;
70 	unsigned char deviation_gain;
71 	unsigned char min_knee;
72 	unsigned char max_knee;
73 	unsigned short blRampReduction;
74 	unsigned short blRampStart;
75 };
76 
77 static const struct abm_parameters abm_settings_config0[abm_defines_max_level] = {
78 //  min_red  max_red  bright_pos  dark_pos  bright_gain  contrast  dev   min_knee  max_knee  blRed    blStart
79 	{0xff,   0xbf,    0x20,       0x00,     0xff,        0x99,     0xb3, 0x40,     0xe0,     0xf777,  0xcccc},
80 	{0xde,   0x85,    0x20,       0x00,     0xe0,        0x90,     0xa8, 0x40,     0xc8,     0xf777,  0xcccc},
81 	{0xb0,   0x50,    0x20,       0x00,     0xc0,        0x88,     0x78, 0x70,     0xa0,     0xeeee,  0x9999},
82 	{0x82,   0x40,    0x20,       0x00,     0x00,        0xb8,     0xb3, 0x70,     0x70,     0xe333,  0xb333},
83 };
84 
85 static const struct abm_parameters abm_settings_config1[abm_defines_max_level] = {
86 //  min_red  max_red  bright_pos  dark_pos  bright_gain  contrast  dev   min_knee  max_knee  blRed  blStart
87 	{0xf0,   0xd9,    0x20,       0x00,     0x00,        0xff,     0xb3, 0x70,     0x70,     0xcccc,  0xcccc},
88 	{0xcd,   0xa5,    0x20,       0x00,     0x00,        0xff,     0xb3, 0x70,     0x70,     0xcccc,  0xcccc},
89 	{0x99,   0x65,    0x20,       0x00,     0x00,        0xff,     0xb3, 0x70,     0x70,     0xcccc,  0xcccc},
90 	{0x82,   0x4d,    0x20,       0x00,     0x00,        0xff,     0xb3, 0x70,     0x70,     0xcccc,  0xcccc},
91 };
92 
93 static const struct abm_parameters abm_settings_config2[abm_defines_max_level] = {
94 //  min_red  max_red  bright_pos  dark_pos  bright_gain  contrast  dev   min_knee  max_knee  blRed    blStart
95 	{0xf0,   0xbf,    0x20,       0x00,     0x88,        0x99,     0xb3, 0x40,     0xe0,    0x0000,  0xcccc},
96 	{0xd8,   0x85,    0x20,       0x00,     0x70,        0x90,     0xa8, 0x40,     0xc8,    0x0700,  0xb333},
97 	{0xb8,   0x58,    0x20,       0x00,     0x64,        0x88,     0x78, 0x70,     0xa0,    0x7000,  0x9999},
98 	{0x82,   0x40,    0x20,       0x00,     0x00,        0xb8,     0xb3, 0x70,     0x70,    0xc333,  0xb333},
99 };
100 
101 static const struct abm_parameters * const abm_settings[] = {
102 	abm_settings_config0,
103 	abm_settings_config1,
104 	abm_settings_config2,
105 };
106 
107 static const struct dm_bl_data_point custom_backlight_curve0[] = {
108 		{2, 14}, {4, 16}, {6, 18}, {8, 21}, {10, 23}, {12, 26}, {14, 29}, {16, 32}, {18, 35},
109 		{20, 38}, {22, 41}, {24, 44}, {26, 48}, {28, 52}, {30, 55}, {32, 59}, {34, 62},
110 		{36, 67}, {38, 71}, {40, 75}, {42, 80}, {44, 84}, {46, 88}, {48, 93}, {50, 98},
111 		{52, 103}, {54, 108}, {56, 113}, {58, 118}, {60, 123}, {62, 129}, {64, 135}, {66, 140},
112 		{68, 146}, {70, 152}, {72, 158}, {74, 164}, {76, 171}, {78, 177}, {80, 183}, {82, 190},
113 		{84, 197}, {86, 204}, {88, 211}, {90, 218}, {92, 225}, {94, 232}, {96, 240}, {98, 247}};
114 
115 struct custom_backlight_profile {
116 	uint8_t  ac_level_percentage;
117 	uint8_t  dc_level_percentage;
118 	uint8_t  min_input_signal;
119 	uint8_t  max_input_signal;
120 	uint8_t  num_data_points;
121 	const struct dm_bl_data_point *data_points;
122 };
123 
124 static const struct custom_backlight_profile custom_backlight_profiles[] = {
125 		{100, 32, 12, 255, ARRAY_SIZE(custom_backlight_curve0), custom_backlight_curve0},
126 };
127 
128 #define NUM_AMBI_LEVEL    5
129 #define NUM_AGGR_LEVEL    4
130 #define NUM_POWER_FN_SEGS 8
131 #define NUM_BL_CURVE_SEGS 16
132 #define IRAM_SIZE 256
133 
134 #define IRAM_RESERVE_AREA_START_V2 0xF0  // reserve 0xF0~0xF6 are write by DMCU only
135 #define IRAM_RESERVE_AREA_END_V2 0xF6  // reserve 0xF0~0xF6 are write by DMCU only
136 
137 #define IRAM_RESERVE_AREA_START_V2_2 0xF0  // reserve 0xF0~0xFF are write by DMCU only
138 #define IRAM_RESERVE_AREA_END_V2_2 0xFF  // reserve 0xF0~0xFF are write by DMCU only
139 
140 #pragma pack(push, 1)
141 /* NOTE: iRAM is 256B in size */
142 struct iram_table_v_2 {
143 	/* flags                      */
144 	uint16_t min_abm_backlight;					/* 0x00 U16  */
145 
146 	/* parameters for ABM2.0 algorithm */
147 	uint8_t min_reduction[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];		/* 0x02 U0.8 */
148 	uint8_t max_reduction[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];		/* 0x16 U0.8 */
149 	uint8_t bright_pos_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];	/* 0x2a U2.6 */
150 	uint8_t bright_neg_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];	/* 0x3e U2.6 */
151 	uint8_t dark_pos_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];		/* 0x52 U2.6 */
152 	uint8_t dark_neg_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];		/* 0x66 U2.6 */
153 	uint8_t iir_curve[NUM_AMBI_LEVEL];				/* 0x7a U0.8 */
154 	uint8_t deviation_gain;						/* 0x7f U0.8 */
155 
156 	/* parameters for crgb conversion */
157 	uint16_t crgb_thresh[NUM_POWER_FN_SEGS];			/* 0x80 U3.13 */
158 	uint16_t crgb_offset[NUM_POWER_FN_SEGS];			/* 0x90 U1.15 */
159 	uint16_t crgb_slope[NUM_POWER_FN_SEGS];				/* 0xa0 U4.12 */
160 
161 	/* parameters for custom curve */
162 	/* thresholds for brightness --> backlight */
163 	uint16_t backlight_thresholds[NUM_BL_CURVE_SEGS];		/* 0xb0 U16.0 */
164 	/* offsets for brightness --> backlight */
165 	uint16_t backlight_offsets[NUM_BL_CURVE_SEGS];			/* 0xd0 U16.0 */
166 
167 	/* For reading PSR State directly from IRAM */
168 	uint8_t psr_state;						/* 0xf0       */
169 	uint8_t dmcu_mcp_interface_version;				/* 0xf1       */
170 	uint8_t dmcu_abm_feature_version;				/* 0xf2       */
171 	uint8_t dmcu_psr_feature_version;				/* 0xf3       */
172 	uint16_t dmcu_version;						/* 0xf4       */
173 	uint8_t dmcu_state;						/* 0xf6       */
174 
175 	uint16_t blRampReduction;					/* 0xf7       */
176 	uint16_t blRampStart;						/* 0xf9       */
177 	uint8_t dummy5;							/* 0xfb       */
178 	uint8_t dummy6;							/* 0xfc       */
179 	uint8_t dummy7;							/* 0xfd       */
180 	uint8_t dummy8;							/* 0xfe       */
181 	uint8_t dummy9;							/* 0xff       */
182 };
183 
184 struct iram_table_v_2_2 {
185 	/* flags                      */
186 	uint16_t flags;							/* 0x00 U16  */
187 
188 	/* parameters for ABM2.2 algorithm */
189 	uint8_t min_reduction[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];		/* 0x02 U0.8 */
190 	uint8_t max_reduction[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];		/* 0x16 U0.8 */
191 	uint8_t bright_pos_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];	/* 0x2a U2.6 */
192 	uint8_t dark_pos_gain[NUM_AMBI_LEVEL][NUM_AGGR_LEVEL];		/* 0x3e U2.6 */
193 	uint8_t hybrid_factor[NUM_AGGR_LEVEL];				/* 0x52 U0.8 */
194 	uint8_t contrast_factor[NUM_AGGR_LEVEL];			/* 0x56 U0.8 */
195 	uint8_t deviation_gain[NUM_AGGR_LEVEL];				/* 0x5a U0.8 */
196 	uint8_t iir_curve[NUM_AMBI_LEVEL];				/* 0x5e U0.8 */
197 	uint8_t min_knee[NUM_AGGR_LEVEL];				/* 0x63 U0.8 */
198 	uint8_t max_knee[NUM_AGGR_LEVEL];				/* 0x67 U0.8 */
199 	uint16_t min_abm_backlight;					/* 0x6b U16  */
200 	uint8_t pad[19];						/* 0x6d U0.8 */
201 
202 	/* parameters for crgb conversion */
203 	uint16_t crgb_thresh[NUM_POWER_FN_SEGS];			/* 0x80 U3.13 */
204 	uint16_t crgb_offset[NUM_POWER_FN_SEGS];			/* 0x90 U1.15 */
205 	uint16_t crgb_slope[NUM_POWER_FN_SEGS];				/* 0xa0 U4.12 */
206 
207 	/* parameters for custom curve */
208 	/* thresholds for brightness --> backlight */
209 	uint16_t backlight_thresholds[NUM_BL_CURVE_SEGS];		/* 0xb0 U16.0 */
210 	/* offsets for brightness --> backlight */
211 	uint16_t backlight_offsets[NUM_BL_CURVE_SEGS];			/* 0xd0 U16.0 */
212 
213 	/* For reading PSR State directly from IRAM */
214 	uint8_t psr_state;						/* 0xf0       */
215 	uint8_t dmcu_mcp_interface_version;				/* 0xf1       */
216 	uint8_t dmcu_abm_feature_version;				/* 0xf2       */
217 	uint8_t dmcu_psr_feature_version;				/* 0xf3       */
218 	uint16_t dmcu_version;						/* 0xf4       */
219 	uint8_t dmcu_state;						/* 0xf6       */
220 
221 	uint8_t dummy1;							/* 0xf7       */
222 	uint8_t dummy2;							/* 0xf8       */
223 	uint8_t dummy3;							/* 0xf9       */
224 	uint8_t dummy4;							/* 0xfa       */
225 	uint8_t dummy5;							/* 0xfb       */
226 	uint8_t dummy6;							/* 0xfc       */
227 	uint8_t dummy7;							/* 0xfd       */
228 	uint8_t dummy8;							/* 0xfe       */
229 	uint8_t dummy9;							/* 0xff       */
230 };
231 #pragma pack(pop)
232 
233 #define MOD_POWER_MAX_CONCURRENT_STREAMS 32
234 #define SMOOTH_BRIGHTNESS_ADJUSTMENT_TIME_IN_MS 500
235 
236 /* If system or panel does not report some sort of brightness percent to nits
237  * mapping, we will use following default values so backlight control using
238  * nits based interfaces will still work, but might not describe panel
239  * correctly. In this case percentage based backlight control should ideally
240  * be used.
241  * Min = 5 nits
242  * Max = 300 nits
243  */
244 
245 #define MOD_POWER_TO_CORE(mod_power)\
246 		container_of(mod_power, struct core_power, mod_public)
247 
248 static uint16_t backlight_8_to_16(unsigned int backlight_8bit)
249 {
250 	return (uint16_t)(backlight_8bit * 0x101);
251 }
252 
253 unsigned int backlight_millipercent_to_millinit(
254 		struct core_power *core_power, unsigned int millipercent, unsigned int inst)
255 {
256 	unsigned int millinit = 0;
257 	unsigned long long numerator = 0;
258 
259 	if (core_power == NULL)
260 		return 0;
261 
262 	numerator = ((unsigned long long)millipercent) *
263 				core_power->bl_prop[inst].nits_range;
264 	millinit = ((unsigned int)div_u64(numerator, 100000)) +
265 			core_power->bl_prop[inst].min_brightness_millinits;
266 
267 	return millinit;
268 }
269 
270 static unsigned int backlight_millinit_to_millipercent(
271 		struct core_power *core_power, unsigned int millinit, unsigned int inst)
272 {
273 	unsigned int millipercent = 0;
274 	unsigned long long numerator = 0;
275 
276 	if (core_power == NULL)
277 		return 0;
278 
279 	if (millinit <= core_power->bl_prop[inst].min_brightness_millinits)
280 		return 0;
281 
282 	if (millinit >= core_power->bl_prop[inst].max_brightness_millinits)
283 		return (100 * 1000);
284 
285 	numerator = (((unsigned long long)millinit) -
286 			core_power->bl_prop[inst].min_brightness_millinits) * 100000;
287 	millipercent = ((unsigned int)div_u64(numerator,
288 				core_power->bl_prop[inst].nits_range));
289 
290 	return millipercent;
291 }
292 
293 static unsigned int backlight_pwm_to_millipercent(
294 		struct core_power *core_power, unsigned int pwm, unsigned int inst)
295 {
296 	unsigned int millipercent = 0;
297 	unsigned int max_index = 0;
298 
299 	if (core_power == NULL)
300 		return 0;
301 
302 	if (!core_power->bl_prop[inst].backlight_caps_valid)
303 		return 0;
304 
305 	/* Doesn't really make sense to have one single backlight level
306 	 * possible...
307 	 */
308 	if (core_power->bl_prop[inst].num_backlight_levels < 2)
309 		return 0;
310 
311 	max_index = core_power->bl_prop[inst].num_backlight_levels - 1;
312 
313 	if (pwm <= core_power->bl_prop[inst].backlight_lut[0])
314 		return 0;
315 
316 	if (pwm > core_power->bl_prop[inst].backlight_lut[max_index])
317 		return (100 * 1000);
318 
319 	/* We need to do a binary search over the array for where the pwm level
320 	 * is in the lut. Based on the index we can determine percentage.
321 	 */
322 	unsigned int min = 0;
323 	unsigned int max = max_index;
324 	unsigned int mid = 0;
325 
326 	while (max >= min) {
327 		mid = (min + max) / 2; /* floor of half range */
328 
329 		if (core_power->bl_prop[inst].backlight_lut[mid] < pwm)
330 			min = mid + 1;
331 		else if (core_power->bl_prop[inst].backlight_lut[mid] > pwm)
332 			max = mid - 1;
333 		else
334 			break;
335 	}
336 
337 	/* In this case, exact match is not found. Check if mid/min/max
338 	 * value is actually closer.
339 	 */
340 	if (max < min) {
341 		unsigned int min_delta;
342 		unsigned int mid_delta;
343 		unsigned int max_delta;
344 
345 		min_delta = (core_power->bl_prop[inst].backlight_lut[min] > pwm) ?
346 				core_power->bl_prop[inst].backlight_lut[min] - pwm :
347 				pwm - core_power->bl_prop[inst].backlight_lut[min];
348 
349 		mid_delta = (core_power->bl_prop[inst].backlight_lut[mid] > pwm) ?
350 				core_power->bl_prop[inst].backlight_lut[mid] - pwm :
351 				pwm - core_power->bl_prop[inst].backlight_lut[mid];
352 
353 		max_delta = (core_power->bl_prop[inst].backlight_lut[max] > pwm) ?
354 				core_power->bl_prop[inst].backlight_lut[max] - pwm :
355 				pwm - core_power->bl_prop[inst].backlight_lut[max];
356 
357 		if ((min_delta < mid_delta) && (min_delta < max_delta))
358 			mid = min;
359 
360 		if ((max_delta < mid_delta) && (max_delta < min_delta))
361 			mid = max;
362 	}
363 
364 	/* No interpolation, just take closest index */
365 	millipercent = 1000 * 100 * mid / max_index;
366 
367 	return millipercent;
368 }
369 
370 static unsigned int backlight_pwm_to_millinit(
371 		struct core_power *core_power, unsigned int pwm, unsigned int inst)
372 {
373 	unsigned int millinit = 0;
374 
375 	if (core_power == NULL)
376 		return 0;
377 
378 	if (pwm <= core_power->bl_prop[inst].min_backlight_pwm)
379 		return core_power->bl_prop[inst].min_brightness_millinits;
380 
381 	if (pwm >= core_power->bl_prop[inst].max_backlight_pwm)
382 		return core_power->bl_prop[inst].max_brightness_millinits;
383 
384 	millinit = ((unsigned int)div_u64(((unsigned long long)pwm -
385 				core_power->bl_prop[inst].min_backlight_pwm) *
386 				core_power->bl_prop[inst].nits_range,
387 				core_power->bl_prop[inst].backlight_range));
388 
389 	millinit += core_power->bl_prop[inst].min_brightness_millinits;
390 
391 	if (millinit > core_power->bl_prop[inst].max_brightness_millinits)
392 		millinit = core_power->bl_prop[inst].max_brightness_millinits;
393 
394 	return millinit;
395 }
396 
397 unsigned int backlight_millipercent_to_pwm(
398 		struct core_power *core_power, unsigned int millipercent, unsigned int inst)
399 {
400 	unsigned int pwm = (unsigned int)-1;
401 	unsigned int index = 0;
402 
403 	if (core_power == NULL)
404 		return 0;
405 
406 	// Bypass the brightness mapping LUT
407 	if (core_power->bl_prop->use_linear_backlight_curve) {
408 		pwm = core_power->bl_prop[inst].min_backlight_pwm +
409 			(unsigned int) div_u64((unsigned long long) millipercent *
410 			core_power->bl_prop[inst].backlight_range,
411 			100000);
412 
413 		if (pwm > core_power->bl_prop[inst].max_backlight_pwm)
414 			pwm = core_power->bl_prop[inst].max_backlight_pwm;
415 
416 		return pwm;
417 	}
418 
419 	if (millipercent >= (100 * 1000))
420 		return core_power->bl_prop[inst].backlight_lut[core_power->bl_prop[inst].num_backlight_levels - 1];
421 
422 	/* This will give the floor index. */
423 	index = ((core_power->bl_prop[inst].num_backlight_levels - 1) *
424 						millipercent) / 100000;
425 	/* Null check otherwise eDP doesn't lightup when connected to DP1 */
426 	if (core_power->bl_prop[inst].backlight_lut == NULL)
427 		return pwm;
428 
429 	pwm = core_power->bl_prop[inst].backlight_lut[index];
430 
431 	return pwm;
432 }
433 
434 static unsigned int backlight_millinit_to_pwm(
435 		struct core_power *core_power, unsigned int millinit, unsigned int inst)
436 {
437 	unsigned int pwm = 0;
438 
439 	if (core_power == NULL)
440 		return 0;
441 
442 	/* For nits based brightness, the signal will be a value
443 	 * between the minimum and maximum value.
444 	 */
445 	if (millinit >= core_power->bl_prop[inst].max_brightness_millinits)
446 		return core_power->bl_prop[inst].max_backlight_pwm;
447 	else if (millinit <= core_power->bl_prop[inst].min_brightness_millinits)
448 		return core_power->bl_prop[inst].min_backlight_pwm;
449 
450 	pwm = ((unsigned int)div_u64(((unsigned long long)millinit -
451 			core_power->bl_prop[inst].min_brightness_millinits) *
452 			core_power->bl_prop[inst].backlight_range,
453 			core_power->bl_prop[inst].nits_range));
454 
455 	pwm += core_power->bl_prop[inst].min_backlight_pwm;
456 
457 	if (pwm > core_power->bl_prop[inst].max_backlight_pwm)
458 		pwm = core_power->bl_prop[inst].max_backlight_pwm;
459 
460 	return pwm;
461 }
462 
463 static bool validate_ext_backlight_caps(
464 		struct dm_acpi_atif_backlight_caps *ext_backlight_caps)
465 {
466 	unsigned int i;
467 	unsigned int num_of_data_points = 0;
468 	unsigned int last_signal_level = 0;
469 	unsigned int last_luminance = 0;
470 
471 	num_of_data_points = ext_backlight_caps->num_data_points;
472 
473 	/* Validation rules:
474 	 * 1. BIOS should carry customized data points and
475 	 * the number of data points should not be larger than 99.
476 	 * 2. The max_input_signal should be larger than min_input_signal.
477 	 * 3. For each data point:
478 	 *	a. luminance should be in ascending order and
479 	 *	should not be 0 or 100 since the corresponding signal_level
480 	 *	are assigned by min_input_signal and max_input_signal.
481 	 *	b. signal_level should be in ascending order and
482 	 *	be within the range of min/max_input_signal.
483 	 */
484 	if (num_of_data_points > BL_DATA_POINTS)
485 		return false;
486 
487 	if (ext_backlight_caps->min_input_signal >= ext_backlight_caps->max_input_signal)
488 		return false;
489 
490 	last_signal_level = ext_backlight_caps->min_input_signal;
491 	for (i = 0; i < num_of_data_points; i++) {
492 		unsigned int luminance = ext_backlight_caps->data_points[i].luminance;
493 		unsigned int signal_level = ext_backlight_caps->data_points[i].signal_level;
494 
495 		if ((luminance <= last_luminance) || (luminance > BL_DATA_POINTS))
496 			return false;
497 
498 		if ((signal_level <= last_signal_level) || (signal_level >= ext_backlight_caps->max_input_signal))
499 			return false;
500 
501 		last_signal_level = signal_level;
502 		last_luminance = luminance;
503 	}
504 
505 	return true;
506 }
507 
508 /* hard coded to default backlight curve. */
509 void initialize_backlight_caps(struct core_power *core_power, unsigned int inst)
510 {
511 	unsigned int i;
512 	struct dm_acpi_atif_backlight_caps *ext_backlight_caps = NULL;
513 	bool custom_curve_present = false;
514 	unsigned int num_levels = 0;
515 	struct dc *dc = NULL;
516 	enum dm_acpi_display_type acpi_display_type =
517 		(inst == 0) ? AcpiDisplayType_LCD1 : AcpiDisplayType_LCD2;
518 
519 	if (core_power == NULL)
520 		return;
521 	dc = core_power->dc;
522 
523 	num_levels = core_power->bl_prop[inst].num_backlight_levels;
524 
525 	/* Allocate memory for ATIF output
526 	 * (do not want to use 256 bytes on the stack)
527 	 */
528 	ext_backlight_caps = (struct dm_acpi_atif_backlight_caps *)
529 		(kzalloc(sizeof(struct dm_acpi_atif_backlight_caps),
530 				GFP_KERNEL));
531 
532 	if (ext_backlight_caps == NULL)
533 		return;
534 
535 	/* Retrieve ACPI extended brightness caps */
536 	if (dm_query_extended_brightness_caps
537 		(dc->ctx, acpi_display_type, ext_backlight_caps)) {
538 		custom_curve_present = validate_ext_backlight_caps(ext_backlight_caps);
539 	}
540 
541 	if (core_power->bl_prop[inst].use_custom_backlight_caps &&
542 			fill_custom_backlight_caps(
543 					core_power->bl_prop[inst].custom_backlight_caps_config_no,
544 					ext_backlight_caps)) {
545 		custom_curve_present = validate_ext_backlight_caps(ext_backlight_caps);
546 	}
547 
548 	if (custom_curve_present) {
549 		unsigned int index = 1;
550 		unsigned int num_of_data_points = ext_backlight_caps->num_data_points;
551 
552 		core_power->bl_prop[inst].ac_backlight_percent =
553 			ext_backlight_caps->ac_level_percentage;
554 		core_power->bl_prop[inst].dc_backlight_percent =
555 			ext_backlight_caps->dc_level_percentage;
556 		core_power->bl_prop[inst].backlight_lut[0] =
557 			backlight_8_to_16(
558 				ext_backlight_caps->min_input_signal);
559 		core_power->bl_prop[inst].backlight_lut[num_levels - 1] =
560 			backlight_8_to_16(
561 				ext_backlight_caps->max_input_signal);
562 
563 		/* Filling translation table from data points -
564 		 * between every two provided data points we
565 		 * lineary interpolate missing values
566 		 */
567 		for (i = 0; i < num_of_data_points; i++) {
568 			unsigned int luminance =
569 				ext_backlight_caps->data_points[i].luminance;
570 			unsigned int signal_level =
571 				backlight_8_to_16(
572 					ext_backlight_caps->data_points[i].signal_level);
573 
574 			/* Since luminance is a percentage, scale it by num_levels*/
575 			luminance = (luminance * num_levels) / 101;
576 
577 			/* Lineary interpolate missing values */
578 			if (index < luminance) {
579 				unsigned int base_value =
580 					core_power->bl_prop[inst].backlight_lut[index-1];
581 				unsigned int delta_signal =
582 					signal_level - base_value;
583 				unsigned int delta_luma =
584 					luminance - index + 1;
585 				unsigned int step  = delta_signal;
586 
587 				for (; index < luminance; index++) {
588 					core_power->bl_prop[inst].backlight_lut[index] =
589 						base_value + (step / delta_luma);
590 					step += delta_signal;
591 				}
592 			}
593 
594 			/* Now [index == luminance],
595 			 * so we can add data point to the translation table
596 			 */
597 			core_power->bl_prop[inst].backlight_lut[index++] = signal_level;
598 		}
599 
600 		/* Complete the final segment of interpolation -
601 		 * between last datapoint and maximum value
602 		 */
603 		if (index < num_levels - 1) {
604 			unsigned int base_value =
605 				core_power->bl_prop[inst].backlight_lut[index-1];
606 			unsigned int delta_signal =
607 				core_power->bl_prop[inst].backlight_lut[num_levels - 1] -
608 								base_value;
609 			unsigned int delta_luma = num_levels - index;
610 			unsigned int step = delta_signal;
611 
612 			for (; index < num_levels - 1; index++) {
613 				core_power->bl_prop[inst].backlight_lut[index] =
614 						base_value + (step / delta_luma);
615 				step += delta_signal;
616 			}
617 		}
618 	/* Build backlight translation table based on default curve */
619 	} else {
620 		/* Defines default backlight curve F(x) = A(x*x) + Bx + C.
621 		 *
622 		 * Backlight curve should always  satisfy:
623 		 * F(0) = min, F(100) = max,
624 		 * So polynom coefficients are:
625 		 * A is 0.0255 - B/100 - min/10000 - (255-max)/10000 =
626 		 * (max - min)/10000 - B/100
627 		 * B is adjustable factor to modify the curve.
628 		 * Bigger B results in less concave curve.
629 		 * B range is [0..(max-min)/100]
630 		 * C is backlight minimum
631 		 */
632 		unsigned int backlight_curve_coeff_a_factor =
633 				num_levels * num_levels;
634 		unsigned int backlight_curve_coeff_b = num_levels;
635 		unsigned int delta =
636 			core_power->bl_prop[inst].backlight_lut[num_levels - 1] -
637 				core_power->bl_prop[inst].backlight_lut[0];
638 		unsigned int coeffC = core_power->bl_prop[inst].backlight_lut[0];
639 		unsigned int coeffB =
640 				(backlight_curve_coeff_b < delta ?
641 					backlight_curve_coeff_b : delta);
642 		unsigned long long coeffA = delta - coeffB; /* coeffB is B*100 */
643 
644 		for (i = 1; i < num_levels - 1; i++) {
645 			uint64_t lut_val = div_u64(coeffA * i * i, backlight_curve_coeff_a_factor) +
646 				div_u64((uint64_t)coeffB * i, backlight_curve_coeff_b) + coeffC;
647 
648 			ASSERT(lut_val <= 0xFFFFFFFF);
649 			core_power->bl_prop[inst].backlight_lut[i] = (unsigned int)lut_val;
650 		}
651 	}
652 
653 	if (ext_backlight_caps != NULL)
654 		kfree(ext_backlight_caps);
655 
656 	/* Successfully initialized */
657 	core_power->bl_prop[inst].backlight_caps_valid = true;
658 }
659 
660 static void varibright_set_level(struct core_power *core_power)
661 {
662 	if (!core_power->varibright_prop.varibright_active ||
663 		!core_power->varibright_prop.varibright_user_enable)
664 		core_power->varibright_prop.varibright_hw_level = 0;
665 	else
666 		core_power->varibright_prop.varibright_hw_level =
667 			core_power->varibright_prop.varibright_level;
668 }
669 
670 bool mod_power_hw_init_backlight(struct mod_power *mod_power)
671 {
672 	struct core_power *core_power = NULL;
673 	struct dc *dc = NULL;
674 	struct dmcu *dmcu = NULL;
675 	struct dmcu_iram_parameters params;
676 	unsigned int i;
677 
678 	if (mod_power == NULL)
679 		return false;
680 
681 	core_power = MOD_POWER_TO_CORE(mod_power);
682 	dc = core_power->dc;
683 
684 	for (i = 0; i < core_power->edp_num; i++) {
685 		params.set = core_power->varibright_prop.varibright_config_setting;
686 		params.backlight_ramping_override = core_power->bl_prop[i].backlight_ramping_override;
687 		params.backlight_ramping_reduction = core_power->bl_prop[i].backlight_ramping_reduction;
688 		params.backlight_ramping_start = core_power->bl_prop[i].backlight_ramping_start;
689 		params.backlight_lut_array = core_power->bl_prop[i].backlight_lut;
690 		params.backlight_lut_array_size = core_power->bl_prop[i].num_backlight_levels;
691 		params.min_abm_backlight = core_power->bl_prop[i].min_abm_backlight;
692 
693 		dmcu = dc->res_pool->dmcu;
694 
695 		// In the case where abm is implemented on dmcub,
696 		// dmcu object will be null.
697 		// ABM 2.4 and up are implemented on dmcub.
698 		if (dmcu) {
699 			//DMCU does not support multiple eDP
700 			return dmcu_load_iram(dmcu, params);
701 		} else if (dc->ctx->dmub_srv) {
702 			if (!dmub_init_abm_config(dc->res_pool, params, i))
703 				return false;
704 		} else
705 			return false;
706 	}
707 	return true;
708 }
709 
710 void mod_power_update_backlight_on_mode_change(
711     struct core_power *core_power,
712     struct dc_link *link,
713     unsigned int panel_inst,
714     uint8_t aux_inst,
715     bool is_hdr)
716 {
717     struct set_backlight_level_params backlight_level_params = { 0 };
718 
719 		if ((link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1 ||
720 			link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1) &&
721 			link->backlight_control_type == BACKLIGHT_CONTROL_AMD_AUX)
722 			dc_link_set_backlight_level_nits(link, core_power->bl_state[panel_inst].isHDR,
723 				core_power->bl_state[panel_inst].backlight_millinit, 0);
724 
725 		backlight_level_params.frame_ramp = 0;
726 
727 		fill_backlight_level_params(core_power, &backlight_level_params, panel_inst, aux_inst,
728 			core_power->bl_state[panel_inst].backlight_pwm, link->backlight_control_type,
729 			core_power->bl_state[panel_inst].backlight_millinit, 0, is_hdr);
730 
731 		dc_link_set_backlight_level(link, &backlight_level_params);
732 }
733 
734 static bool set_backlight_millinits_aux(struct core_power *core_power,
735 		struct dc_stream_state *stream,
736 		unsigned int backlight_millinits,
737 		unsigned int transition_time_millisec,
738 		unsigned int inst)
739 {
740 	struct dc_link *link = NULL;
741 
742 	if (core_power == NULL)
743 		return false;
744 
745 	if (stream == NULL)
746 		return true;
747 
748 	link = dc_stream_get_link(stream);
749 
750 	// only use internal backlight control if dmub capabilities are not present
751 	if (link->backlight_control_type == BACKLIGHT_CONTROL_VESA_AUX &&
752 		link->dc->caps.dmub_caps.aux_backlight_support)
753 		return true;
754 
755 	return dc_link_set_backlight_level_nits(link, core_power->bl_state[inst].isHDR,
756 			backlight_millinits, transition_time_millisec);
757 }
758 
759 static bool set_backlight(struct core_power *core_power,
760 		struct dc_stream_state *stream,
761 		struct set_backlight_level_params *backlight_level_params,
762 		unsigned int inst)
763 {
764 	bool retv = false;
765 	unsigned int frame_ramp = 0;
766 	unsigned int vsync_rate_hz;
767 	union dmcu_abm_set_bl_params params;
768 	const struct dc_link *link = NULL;
769 	unsigned int backlight_pwm_u16_16 = backlight_level_params->backlight_pwm_u16_16;
770 	unsigned int transition_time_millisec = backlight_level_params->transition_time_in_ms;
771 
772 	if (core_power == NULL)
773 		return false;
774 
775 	core_power->bl_state[inst].backlight_pwm = backlight_pwm_u16_16;
776 
777 	if (stream == NULL)
778 		return true;
779 
780 	if (stream->link->connector_signal != SIGNAL_TYPE_EDP)
781 		return false;
782 
783 	if (transition_time_millisec != 0) {
784 		unsigned int v_total =
785 			(stream->adjust.v_total_max == 0) ? stream->timing.v_total : stream->adjust.v_total_max;
786 
787 		vsync_rate_hz = (unsigned int)div_u64(div_u64((stream->
788 			timing.pix_clk_100hz * 100),
789 			v_total),
790 			stream->timing.h_total);
791 
792 		if (core_power->bl_state[inst].smooth_brightness_enabled)
793 			frame_ramp = ((vsync_rate_hz *
794 				transition_time_millisec) + 500) / 1000;
795 	}
796 
797 	core_power->bl_state[inst].frame_ramp = frame_ramp;
798 	params.u32All = 0;
799 	params.bits.gradual_change = (frame_ramp > 0);
800 	params.bits.frame_ramp = frame_ramp;
801 	link = dc_stream_get_link(stream);
802 
803 	mod_power_set_psr_event(&core_power->mod_public, stream, true, psr_event_hw_programming, true);
804 	mod_power_set_replay_event(&core_power->mod_public, stream, true, replay_event_hw_programming, true);
805 
806 	backlight_level_params->frame_ramp = params.u32All;
807 	retv = dc_link_set_backlight_level(link, backlight_level_params);
808 
809 	mod_power_set_psr_event(&core_power->mod_public, stream, false, psr_event_hw_programming, false);
810 	mod_power_set_replay_event(&core_power->mod_public, stream, false, replay_event_hw_programming, false);
811 
812 	return retv;
813 }
814 
815 void fill_backlight_level_params(struct core_power *core_power,
816 	struct set_backlight_level_params *backlight_level_params,
817 	int panel_inst, uint8_t aux_inst, unsigned int backlight_pwm,
818 	enum backlight_control_type backlight_control_type,
819 	unsigned int backlight_millinit, unsigned int transition_time_millisec,
820 	bool is_hdr)
821 {
822 	struct pwr_backlight_properties *bl_prop = &core_power->bl_prop[panel_inst];
823 
824 	backlight_level_params->aux_inst = aux_inst;
825 	backlight_level_params->backlight_pwm_u16_16 = backlight_pwm;
826 	backlight_level_params->control_type = backlight_control_type;
827 	backlight_level_params->backlight_millinits = backlight_millinit;
828 	backlight_level_params->transition_time_in_ms = transition_time_millisec;
829 	backlight_level_params->min_luminance = bl_prop->min_brightness_millinits;
830 	backlight_level_params->max_luminance = bl_prop->max_brightness_millinits;
831 	backlight_level_params->min_backlight_pwm = bl_prop->min_backlight_pwm;
832 	backlight_level_params->max_backlight_pwm = bl_prop->max_backlight_pwm;
833 
834 	if (backlight_control_type == BACKLIGHT_CONTROL_AMD_AUX && !is_hdr)
835 		backlight_level_params->control_type = BACKLIGHT_CONTROL_PWM;
836 }
837 
838 bool mod_power_set_backlight_nits(struct mod_power *mod_power,
839 		struct dc_stream_state *stream,
840 		unsigned int backlight_millinit,
841 		unsigned int transition_time_millisec,
842 		bool skip_aux,
843 		bool is_hdr)
844 {
845 	struct core_power *core_power = NULL;
846 	unsigned int backlight_pwm;
847 	unsigned int panel_inst = 0;
848 	struct set_backlight_level_params backlight_level_params = { 0 };
849 	const struct dc_link *link = NULL;
850 	uint8_t aux_inst = 0;
851 
852 	if (mod_power == NULL)
853 		return false;
854 
855 	core_power = MOD_POWER_TO_CORE(mod_power);
856 	link = dc_stream_get_link(stream);
857 
858 	aux_inst = link->dc->link_srv->get_ddc_aux_inst(link);
859 
860 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &panel_inst))
861 		return false;
862 
863 	if (!skip_aux) {
864 		if (!set_backlight_millinits_aux(core_power, stream,
865 						backlight_millinit, transition_time_millisec, panel_inst))
866 			return false;
867 	}
868 // always send both AUX (above) and PWM (below)
869 	core_power->bl_state[panel_inst].backlight_millinit = backlight_millinit;
870 
871 	core_power->bl_state[panel_inst].backlight_millipercent =
872 		backlight_millinit_to_millipercent(
873 				core_power, backlight_millinit, panel_inst);
874 
875 	backlight_pwm = backlight_millinit_to_pwm(
876 				core_power, backlight_millinit, panel_inst);
877 
878 	fill_backlight_level_params(core_power, &backlight_level_params, panel_inst, aux_inst, backlight_pwm,
879 		link->backlight_control_type, backlight_millinit, transition_time_millisec, is_hdr);
880 
881 	return set_backlight(core_power, stream,
882 			&backlight_level_params, panel_inst);
883 }
884 
885 bool mod_power_backlight_percent_to_nits(struct mod_power *mod_power,
886 		struct dc_stream_state *stream,
887 		unsigned int backlight_millipercent,
888 		unsigned int *backlight_millinit)
889 {
890 	struct core_power *core_power = NULL;
891 	unsigned int inst = 0;
892 
893 	if (mod_power == NULL)
894 		return false;
895 
896 	core_power = MOD_POWER_TO_CORE(mod_power);
897 
898 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
899 		return false;
900 
901 	*backlight_millinit = backlight_millipercent_to_millinit(
902 			core_power, backlight_millipercent, inst);
903 	return true;
904 }
905 
906 bool mod_power_backlight_nits_to_percent(struct mod_power *mod_power,
907 		struct dc_stream_state *stream,
908 		unsigned int backlight_millinit,
909 		unsigned int *backlight_millipercent)
910 {
911 	struct core_power *core_power = NULL;
912 	unsigned int inst = 0;
913 
914 	if (mod_power == NULL)
915 		return false;
916 
917 	core_power = MOD_POWER_TO_CORE(mod_power);
918 
919 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
920 		return false;
921 
922 	*backlight_millipercent = backlight_millinit_to_millipercent(
923 			core_power, backlight_millinit, inst);
924 	return true;
925 }
926 
927 bool mod_power_set_backlight_percent(struct mod_power *mod_power,
928 		struct dc_stream_state *stream,
929 		unsigned int backlight_millipercent,
930 		unsigned int transition_time_millisec,
931 		bool is_hdr)
932 {
933 	struct core_power *core_power = NULL;
934 	struct set_backlight_level_params backlight_level_params = { 0 };
935 	const struct dc_link *link = NULL;
936 	unsigned int backlight_pwm;
937 	unsigned int panel_inst = 0;
938 	uint8_t aux_inst = 0;
939 
940 	if (mod_power == NULL)
941 		return false;
942 
943 	core_power = MOD_POWER_TO_CORE(mod_power);
944 	link = dc_stream_get_link(stream);
945 	aux_inst = link->dc->link_srv->get_ddc_aux_inst(link);
946 
947 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &panel_inst))
948 		return false;
949 	core_power->bl_state[panel_inst].backlight_millipercent = backlight_millipercent;
950 
951 	core_power->bl_state[panel_inst].backlight_millinit =
952 		backlight_millipercent_to_millinit(
953 				core_power, backlight_millipercent, panel_inst);
954 
955 	backlight_pwm = backlight_millipercent_to_pwm(
956 				core_power, backlight_millipercent, panel_inst);
957 
958 	fill_backlight_level_params(core_power, &backlight_level_params, panel_inst,
959 		aux_inst, backlight_pwm, link->backlight_control_type,
960 		core_power->bl_state[panel_inst].backlight_millinit, transition_time_millisec, is_hdr);
961 
962 	return set_backlight(core_power, stream,
963 			&backlight_level_params, panel_inst);
964 }
965 
966 void mod_power_update_backlight(struct mod_power *mod_power,
967 		struct dc_stream_state *stream,
968 		unsigned int backlight_millipercent)
969 {
970 	struct core_power *core_power = NULL;
971 	unsigned int inst = 0;
972 
973 	if (mod_power == NULL)
974 		return;
975 
976 	core_power = MOD_POWER_TO_CORE(mod_power);
977 
978 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
979 		return;
980 	core_power->bl_state[inst].backlight_millipercent = backlight_millipercent;
981 
982 	core_power->bl_state[inst].backlight_millinit =
983 		backlight_millipercent_to_millinit(
984 			core_power, backlight_millipercent, inst);
985 
986 	core_power->bl_state[inst].backlight_pwm = backlight_millipercent_to_pwm(
987 		core_power, backlight_millipercent, inst);
988 }
989 
990 void mod_power_update_backlight_nits(struct mod_power *mod_power,
991 		struct dc_stream_state *stream,
992 		unsigned int backlight_millinit)
993 {
994 	struct core_power *core_power = NULL;
995 	unsigned int inst = 0;
996 
997 	if (mod_power == NULL)
998 		return;
999 
1000 	core_power = MOD_POWER_TO_CORE(mod_power);
1001 
1002 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
1003 		return;
1004 
1005 	core_power->bl_state[inst].backlight_millinit = backlight_millinit;
1006 
1007 	core_power->bl_state[inst].backlight_millipercent = backlight_millinit_to_millipercent(
1008 		core_power, backlight_millinit, inst);
1009 	core_power->bl_state[inst].backlight_pwm = backlight_millinit_to_pwm(
1010 		core_power, backlight_millinit, inst);
1011 }
1012 
1013 bool mod_power_get_backlight_pwm(struct mod_power *mod_power,
1014 		unsigned int *backlight_pwm,
1015 		unsigned int inst)
1016 {
1017 	struct core_power *core_power = NULL;
1018 
1019 	if (mod_power == NULL)
1020 		return false;
1021 
1022 	core_power = MOD_POWER_TO_CORE(mod_power);
1023 
1024 	*backlight_pwm = core_power->bl_state[inst].backlight_pwm;
1025 
1026 	return true;
1027 }
1028 
1029 bool mod_power_get_backlight_nits(struct mod_power *mod_power,
1030 		unsigned int *backlight_millinit,
1031 		unsigned int inst)
1032 {
1033 	struct core_power *core_power = NULL;
1034 
1035 	if (mod_power == NULL)
1036 		return false;
1037 
1038 	core_power = MOD_POWER_TO_CORE(mod_power);
1039 
1040 	*backlight_millinit = core_power->bl_state[inst].backlight_millinit;
1041 
1042 	return true;
1043 }
1044 
1045 bool mod_power_get_backlight_percent(struct mod_power *mod_power,
1046 		unsigned int *backlight_millipercent,
1047 		unsigned int inst)
1048 {
1049 	struct core_power *core_power = NULL;
1050 
1051 	if (mod_power == NULL)
1052 		return false;
1053 
1054 	core_power = MOD_POWER_TO_CORE(mod_power);
1055 
1056 	*backlight_millipercent = core_power->bl_state[inst].backlight_millipercent;
1057 
1058 	return true;
1059 }
1060 
1061 bool mod_power_get_hw_target_backlight_pwm_nits(struct mod_power *mod_power,
1062 		const struct dc_link *link,
1063 		unsigned int *backlight_millinit,
1064 		unsigned int inst)
1065 {
1066 	struct core_power *core_power = NULL;
1067 	unsigned int backlight_u16_16 = 0;
1068 
1069 	if (mod_power == NULL)
1070 		return false;
1071 
1072 	core_power = MOD_POWER_TO_CORE(mod_power);
1073 
1074 	if (mod_power_get_hw_target_backlight_pwm(mod_power, link,
1075 							&backlight_u16_16)) {
1076 		*backlight_millinit =
1077 			backlight_pwm_to_millinit(core_power,
1078 					backlight_u16_16, inst);
1079 		return true;
1080 	}
1081 	return false;
1082 }
1083 
1084 bool mod_power_get_hw_target_backlight_pwm_percent(struct mod_power *mod_power,
1085 		const struct dc_link *link,
1086 		unsigned int *backlight_millipercent,
1087 		unsigned int inst)
1088 {
1089 	struct core_power *core_power = NULL;
1090 	unsigned int backlight_u16_16 = 0;
1091 
1092 	if (mod_power == NULL)
1093 		return false;
1094 
1095 	core_power = MOD_POWER_TO_CORE(mod_power);
1096 
1097 	if (mod_power_get_hw_target_backlight_pwm(mod_power, link,
1098 							&backlight_u16_16)) {
1099 		*backlight_millipercent =
1100 			backlight_pwm_to_millipercent(core_power,
1101 					backlight_u16_16, inst);
1102 		return true;
1103 	}
1104 	return false;
1105 }
1106 
1107 bool mod_power_get_hw_target_backlight_pwm(struct mod_power *mod_power,
1108 		const struct dc_link *link,
1109 		unsigned int *backlight_u16_16)
1110 {
1111 	if (mod_power == NULL)
1112 		return false;
1113 
1114 	*backlight_u16_16 = dc_link_get_target_backlight_pwm(link);
1115 
1116 	return true;
1117 }
1118 
1119 bool mod_power_get_hw_backlight_pwm_nits(struct mod_power *mod_power,
1120 		const struct dc_link *link,
1121 		unsigned int *backlight_millinit,
1122 		unsigned int inst)
1123 {
1124 	struct core_power *core_power = NULL;
1125 	unsigned int backlight_u16_16 = 0;
1126 
1127 	if (mod_power == NULL)
1128 		return false;
1129 
1130 	core_power = MOD_POWER_TO_CORE(mod_power);
1131 
1132 	if (mod_power_get_hw_backlight_pwm(mod_power, link, &backlight_u16_16)) {
1133 		*backlight_millinit =
1134 			backlight_pwm_to_millinit(core_power,
1135 					backlight_u16_16, inst);
1136 		return true;
1137 	}
1138 	return false;
1139 }
1140 
1141 bool mod_power_get_hw_backlight_aux_nits(struct mod_power *mod_power,
1142 		struct dc_stream_state **streams, int num_streams,
1143 		unsigned int *backlight_millinit_avg,
1144 		unsigned int *backlight_millinit_peak)
1145 {
1146 	struct core_power *core_power = NULL;
1147 	struct dc_link *link = NULL;
1148 	int stream_index;
1149 
1150 	if (mod_power == NULL)
1151 		return false;
1152 
1153 	core_power = MOD_POWER_TO_CORE(mod_power);
1154 
1155 	if (core_power == NULL)
1156 		return false;
1157 
1158 	if (num_streams < 1)
1159 		return true;
1160 
1161 	for (stream_index = 0; stream_index < num_streams; stream_index++)
1162 		if (streams[stream_index]->link->connector_signal == SIGNAL_TYPE_EDP ||
1163 				streams[stream_index]->link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT)
1164 			break;
1165 
1166 	if (stream_index == num_streams)
1167 		return false;
1168 
1169 	link = dc_stream_get_link(streams[stream_index]);
1170 	if (link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 0)
1171 		return false;
1172 
1173 	return dc_link_get_backlight_level_nits(link, backlight_millinit_avg,
1174 			backlight_millinit_peak);
1175 }
1176 
1177 bool mod_power_get_hw_backlight_pwm_percent(struct mod_power *mod_power,
1178 		const struct dc_link *link,
1179 		unsigned int *backlight_millipercent,
1180 		unsigned int inst)
1181 {
1182 	struct core_power *core_power = NULL;
1183 	unsigned int backlight_u16_16 = 0;
1184 
1185 	if (mod_power == NULL)
1186 		return false;
1187 
1188 	core_power = MOD_POWER_TO_CORE(mod_power);
1189 
1190 	if (mod_power_get_hw_backlight_pwm(mod_power, link, &backlight_u16_16)) {
1191 		*backlight_millipercent =
1192 			backlight_pwm_to_millipercent(core_power,
1193 					backlight_u16_16, inst);
1194 		return true;
1195 	}
1196 	return false;
1197 }
1198 
1199 bool mod_power_get_hw_backlight_pwm(struct mod_power *mod_power,
1200 		const struct dc_link *link,
1201 		unsigned int *backlight_u16_16)
1202 {
1203 	if (mod_power == NULL)
1204 		return false;
1205 
1206 	*backlight_u16_16 = dc_link_get_backlight_level(link);
1207 
1208 	return true;
1209 }
1210 
1211 bool mod_power_get_panel_backlight_boundaries(
1212 				struct mod_power *mod_power,
1213 				unsigned int *out_min_backlight,
1214 				unsigned int *out_max_backlight,
1215 				unsigned int *out_ac_backlight_percent,
1216 				unsigned int *out_dc_backlight_percent,
1217 				unsigned int inst)
1218 {
1219 	struct core_power *core_power = NULL;
1220 
1221 	if (mod_power == NULL)
1222 		return false;
1223 
1224 	core_power = MOD_POWER_TO_CORE(mod_power);
1225 
1226 	/* If cache was successfully updated,
1227 	 * copy the values to output structure and return success
1228 	 */
1229 	if (core_power->bl_prop[inst].backlight_caps_valid) {
1230 		*out_min_backlight = core_power->bl_prop[inst].backlight_lut[0];
1231 		*out_max_backlight =
1232 			core_power->bl_prop[inst].backlight_lut[
1233 				core_power->bl_prop[inst].num_backlight_levels - 1];
1234 		*out_ac_backlight_percent =
1235 			core_power->bl_prop[inst].ac_backlight_percent;
1236 		*out_dc_backlight_percent =
1237 			core_power->bl_prop[inst].dc_backlight_percent;
1238 
1239 		return true;
1240 	}
1241 
1242 	return false;
1243 }
1244 
1245 bool mod_power_set_smooth_brightness(struct mod_power *mod_power,
1246 		bool enable_brightness,
1247 		unsigned int inst)
1248 {
1249 	struct core_power *core_power = NULL;
1250 
1251 	if (mod_power == NULL)
1252 		return false;
1253 
1254 	core_power = MOD_POWER_TO_CORE(mod_power);
1255 
1256 	core_power->bl_state[inst].smooth_brightness_enabled = enable_brightness;
1257 
1258 	return true;
1259 }
1260 
1261 bool mod_power_varibright_feature_enable(struct mod_power *mod_power, bool enable,
1262 		struct dc_stream_update *stream_update)
1263 {
1264 	struct core_power *core_power = NULL;
1265 
1266 	if (mod_power == NULL)
1267 		return false;
1268 
1269 	core_power = MOD_POWER_TO_CORE(mod_power);
1270 	core_power->varibright_prop.varibright_user_enable = enable;
1271 
1272 	/* find abm hw level to program, and save in stream update */
1273 	varibright_set_level(core_power);
1274 	*stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1275 
1276 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1277 						WPP_BIT_FLAG_Backlight_ABM,
1278 						">ABM feature enable: enable=%u su->varibright_level=%u varibright_hw_level=%u",
1279 						(unsigned int) enable,
1280 						*stream_update->abm_level,
1281 						core_power->varibright_prop.varibright_hw_level);
1282 	return true;
1283 }
1284 
1285 bool mod_power_varibright_activate(struct mod_power *mod_power,
1286 		bool activate,
1287 		struct dc_stream_update *stream_update)
1288 {
1289 	struct core_power *core_power = NULL;
1290 
1291 	if (mod_power == NULL)
1292 		return false;
1293 
1294 	core_power = MOD_POWER_TO_CORE(mod_power);
1295 	core_power->varibright_prop.varibright_active = activate;
1296 
1297 	/* find abm hw level to program, and save in stream update */
1298 	varibright_set_level(core_power);
1299 	*stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1300 
1301 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1302 						WPP_BIT_FLAG_Backlight_ABM,
1303 						">ABM activate: activate=%u su->varibright_level=%u",
1304 						(unsigned int) activate,
1305 						*stream_update->abm_level);
1306 	return true;
1307 }
1308 bool mod_power_varibright_set_level(struct mod_power *mod_power, unsigned int level,
1309 		struct dc_stream_update *stream_update)
1310 {
1311 	struct core_power *core_power = NULL;
1312 
1313 	if (mod_power == NULL)
1314 		return false;
1315 
1316 	core_power = MOD_POWER_TO_CORE(mod_power);
1317 	core_power->varibright_prop.varibright_level = level;
1318 	core_power->varibright_prop.varibright_hw_level = level;
1319 
1320 	/* find abm hw level to program, and save in stream update */
1321 	varibright_set_level(core_power);
1322 	*stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1323 
1324 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1325 						WPP_BIT_FLAG_Backlight_ABM,
1326 						">ABM set level: level=%u -> (varibright_level=%u varibright_hw_level=%u) -> su->varibright_level=%u",
1327 						level,
1328 						core_power->varibright_prop.varibright_level,
1329 						core_power->varibright_prop.varibright_hw_level,
1330 						*stream_update->abm_level);
1331 	return true;
1332 }
1333 
1334 bool mod_power_varibright_set_hw_level(struct mod_power *mod_power, unsigned int level,
1335 		struct dc_stream_update *stream_update)
1336 {
1337 	struct core_power *core_power = NULL;
1338 
1339 	if (mod_power == NULL)
1340 		return false;
1341 
1342 	core_power = MOD_POWER_TO_CORE(mod_power);
1343 
1344 	if (level == 0 || level == ABM_LEVEL_IMMEDIATE_DISABLE)
1345 		core_power->varibright_prop.varibright_active = 0;
1346 	else
1347 		core_power->varibright_prop.varibright_active = 1;
1348 	core_power->varibright_prop.varibright_hw_level = level;
1349 	*stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1350 
1351 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1352 						WPP_BIT_FLAG_Backlight_ABM,
1353 						">ABM set level: level=%u -> (varibright_level=%u varibright_hw_level=%u) -> su->varibright_level=%u",
1354 						level,
1355 						core_power->varibright_prop.varibright_level,
1356 						core_power->varibright_prop.varibright_hw_level,
1357 						*stream_update->abm_level);
1358 	return true;
1359 }
1360 
1361 bool mod_power_get_varibright_level(struct mod_power *mod_power,
1362 		unsigned int *varibright_level)
1363 {
1364 	struct core_power *core_power = NULL;
1365 
1366 	if (mod_power == NULL)
1367 		return false;
1368 
1369 	core_power = MOD_POWER_TO_CORE(mod_power);
1370 
1371 	*varibright_level = core_power->varibright_prop.varibright_level;
1372 
1373 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1374 						WPP_BIT_FLAG_Backlight_ABM,
1375 						">get varibright level: cp->varibright_level=%u",
1376 						*varibright_level);
1377 	return true;
1378 
1379 }
1380 
1381 bool mod_power_get_varibright_hw_level(struct mod_power *mod_power,
1382 		unsigned int *varibright_level)
1383 {
1384 	struct core_power *core_power = NULL;
1385 
1386 	if (mod_power == NULL)
1387 		return false;
1388 
1389 	core_power = MOD_POWER_TO_CORE(mod_power);
1390 
1391 	*varibright_level = core_power->varibright_prop.varibright_hw_level;
1392 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1393 						WPP_BIT_FLAG_Backlight_ABM,
1394 						">get varibright HW level: hw_level=%u",
1395 						*varibright_level);
1396 	return true;
1397 }
1398 
1399 bool mod_power_get_varibright_default_level(struct mod_power *mod_power,
1400 		unsigned int *varibright_level)
1401 {
1402 	struct core_power *core_power = NULL;
1403 
1404 	if (mod_power == NULL)
1405 		return false;
1406 
1407 	core_power = MOD_POWER_TO_CORE(mod_power);
1408 
1409 	*varibright_level = core_power->varibright_prop.def_varibright_level;
1410 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1411 						WPP_BIT_FLAG_Backlight_ABM,
1412 						">get varibright default level: def_varibright_level=%u",
1413 						*varibright_level);
1414 	return true;
1415 }
1416 
1417 bool mod_power_get_varibright_enable(struct mod_power *mod_power,
1418 		bool *varibright_enable)
1419 {
1420 	struct core_power *core_power = NULL;
1421 
1422 	if (mod_power == NULL)
1423 		return false;
1424 
1425 	core_power = MOD_POWER_TO_CORE(mod_power);
1426 
1427 	*varibright_enable = core_power->varibright_prop.varibright_user_enable;
1428 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1429 				WPP_BIT_FLAG_Backlight_ABM,
1430 				">get varibright enable state: varibright_user_enable=%u",
1431 				(unsigned int) (*varibright_enable));
1432 	return true;
1433 }
1434 
1435 bool mod_power_is_abm_active(struct mod_power *mod_power,
1436 		const struct dc_link *link,
1437 		unsigned int inst)
1438 {
1439 	unsigned int user_backlight = 0;
1440 	unsigned int current_backlight = 0;
1441 	bool is_active = false;
1442 
1443 	if (mod_power == NULL)
1444 		return false;
1445 
1446 	mod_power_get_backlight_pwm(mod_power, &user_backlight, inst);
1447 	mod_power_get_hw_backlight_pwm(mod_power, link,	&current_backlight);
1448 
1449 	if (user_backlight != current_backlight)
1450 		is_active = true;
1451 	else
1452 		is_active = false;
1453 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1454 						WPP_BIT_FLAG_Backlight_ABM,
1455 						">get ABM active state: is_active=%u (user_backlight_pwm=%u, current_backlight_pwm=%u)",
1456 						(unsigned int)is_active,
1457 						user_backlight,
1458 						current_backlight);
1459 	return is_active;
1460 }
1461 
1462 bool mod_power_is_abm_supported(struct mod_power *mod_power,
1463 		unsigned int inst)
1464 {
1465 	struct core_power *core_power = NULL;
1466 	struct dc *dc = NULL;
1467 
1468 	if (mod_power == NULL)
1469 		return false;
1470 
1471 	core_power = MOD_POWER_TO_CORE(mod_power);
1472 	dc = core_power->dc;
1473 
1474 	// It's only implemented on dmcub.
1475 	if (dc->ctx->dmub_srv) {
1476 		if (!dmub_is_abm_supported(dc->res_pool, inst))
1477 			return false;
1478 	} else
1479 		return false;
1480 
1481 	return true;
1482 }
1483 
1484 bool mod_power_abm_set_event(struct mod_power *mod_power,
1485 		unsigned int full_screen, unsigned int trans_info,
1486 		unsigned int hdr_mode, unsigned int scaling_enable,
1487 		unsigned int scaling_strength_map, unsigned int inst)
1488 {
1489 	struct core_power *core_power = NULL;
1490 	struct dc *dc = NULL;
1491 
1492 	if (mod_power == NULL)
1493 		return false;
1494 
1495 	core_power = MOD_POWER_TO_CORE(mod_power);
1496 	dc = core_power->dc;
1497 
1498 	// It's only implemented on dmcub.
1499 	if (dc->ctx->dmub_srv) {
1500 		if (!dmub_set_abm_event(dc->res_pool, full_screen, trans_info,
1501 				hdr_mode, scaling_enable, scaling_strength_map, inst))
1502 			return false;
1503 	} else
1504 		return false;
1505 
1506 	return true;
1507 }
1508 
1509 bool mod_power_abm_set_strength(struct mod_power *mod_power,
1510 		unsigned int strength,
1511 		unsigned int inst)
1512 {
1513 	struct core_power *core_power = NULL;
1514 	struct dc *dc = NULL;
1515 
1516 	if (mod_power == NULL)
1517 		return false;
1518 
1519 	core_power = MOD_POWER_TO_CORE(mod_power);
1520 	dc = core_power->dc;
1521 
1522 	// It's only implemented on dmcub.
1523 	if (dc->ctx->dmub_srv) {
1524 		if (!dmub_set_abm_strength(dc->res_pool, strength, inst))
1525 			return false;
1526 	} else
1527 		return false;
1528 
1529 	return true;
1530 }
1531 
1532 static void fill_backlight_transform_table(struct dmcu_iram_parameters params,
1533 		struct iram_table_v_2 *table)
1534 {
1535 	unsigned int i;
1536 	unsigned int num_entries = NUM_BL_CURVE_SEGS;
1537 	unsigned int lut_index;
1538 
1539 	table->backlight_thresholds[0] = 0;
1540 	ASSERT(params.backlight_lut_array[0] <= 0xFFFF);
1541 	table->backlight_offsets[0] = (uint16_t)params.backlight_lut_array[0];
1542 	table->backlight_thresholds[num_entries-1] = 0xFFFF;
1543 	ASSERT(params.backlight_lut_array[params.backlight_lut_array_size - 1] <= 0xFFFF);
1544 	table->backlight_offsets[num_entries-1] =
1545 		(uint16_t)params.backlight_lut_array[params.backlight_lut_array_size - 1];
1546 
1547 	/* Setup all brightness levels between 0% and 100% exclusive
1548 	 * Fills brightness-to-backlight transform table. Backlight custom curve
1549 	 * describes transform from brightness to backlight. It will be defined
1550 	 * as set of thresholds and set of offsets, together, implying
1551 	 * extrapolation of custom curve into 16 uniformly spanned linear
1552 	 * segments.  Each threshold/offset represented by 16 bit entry in
1553 	 * format U4.10.
1554 	 */
1555 	for (i = 1; i+1 < num_entries; i++) {
1556 		lut_index = (params.backlight_lut_array_size - 1) * i / (num_entries - 1);
1557 
1558 		ASSERT(lut_index < params.backlight_lut_array_size);
1559 
1560 		unsigned int threshold_val = DIV_ROUNDUP((i * 65536), num_entries);
1561 		unsigned int offset_val = params.backlight_lut_array[lut_index];
1562 
1563 		ASSERT(threshold_val <= 0xFFFF);
1564 		ASSERT(offset_val <= 0xFFFF);
1565 
1566 		table->backlight_thresholds[i] = cpu_to_be16((uint16_t)threshold_val);
1567 		table->backlight_offsets[i]    = cpu_to_be16((uint16_t)offset_val);
1568 	}
1569 }
1570 
1571 static void fill_backlight_transform_table_v_2_2(struct dmcu_iram_parameters params,
1572 		struct iram_table_v_2_2 *table, bool big_endian)
1573 {
1574 	unsigned int i;
1575 	unsigned int num_entries = NUM_BL_CURVE_SEGS;
1576 	unsigned int lut_index;
1577 
1578 	table->backlight_thresholds[0] = 0;
1579 	ASSERT(params.backlight_lut_array[0] <= 0xFFFF);
1580 	table->backlight_offsets[0] = (uint16_t)params.backlight_lut_array[0];
1581 	table->backlight_thresholds[num_entries-1] = 0xFFFF;
1582 	ASSERT(params.backlight_lut_array[params.backlight_lut_array_size - 1] <= 0xFFFF);
1583 	table->backlight_offsets[num_entries-1] =
1584 		(uint16_t)params.backlight_lut_array[params.backlight_lut_array_size - 1];
1585 
1586 	/* Setup all brightness levels between 0% and 100% exclusive
1587 	 * Fills brightness-to-backlight transform table. Backlight custom curve
1588 	 * describes transform from brightness to backlight. It will be defined
1589 	 * as set of thresholds and set of offsets, together, implying
1590 	 * extrapolation of custom curve into 16 uniformly spanned linear
1591 	 * segments.  Each threshold/offset represented by 16 bit entry in
1592 	 * format U4.10.
1593 	 */
1594 	for (i = 1; i+1 < num_entries; i++) {
1595 		lut_index = DIV_ROUNDUP((i * params.backlight_lut_array_size), num_entries);
1596 		ASSERT(lut_index < params.backlight_lut_array_size);
1597 
1598 		unsigned int threshold_val = DIV_ROUNDUP((i * 65536), num_entries);
1599 		unsigned int offset_val = params.backlight_lut_array[lut_index];
1600 
1601 		ASSERT(threshold_val <= 0xFFFF);
1602 		ASSERT(offset_val <= 0xFFFF);
1603 
1604 		table->backlight_thresholds[i] = (big_endian) ?
1605 			cpu_to_be16((uint16_t)threshold_val) : cpu_to_le16((uint16_t)threshold_val);
1606 		table->backlight_offsets[i] = (big_endian) ?
1607 			cpu_to_be16((uint16_t)offset_val) : cpu_to_le16((uint16_t)offset_val);
1608 	}
1609 }
1610 
1611 static void fill_iram_v_2(struct iram_table_v_2 *ram_table, struct dmcu_iram_parameters params)
1612 {
1613 	unsigned int set = params.set;
1614 
1615 	ram_table->min_abm_backlight =
1616 			cpu_to_be16(params.min_abm_backlight);
1617 	ram_table->deviation_gain = 0xb3;
1618 
1619 	ram_table->blRampReduction =
1620 		cpu_to_be16(params.backlight_ramping_reduction);
1621 	ram_table->blRampStart =
1622 		cpu_to_be16(params.backlight_ramping_start);
1623 
1624 	ram_table->min_reduction[0][0] = min_reduction_table[abm_config[set][0]];
1625 	ram_table->min_reduction[1][0] = min_reduction_table[abm_config[set][0]];
1626 	ram_table->min_reduction[2][0] = min_reduction_table[abm_config[set][0]];
1627 	ram_table->min_reduction[3][0] = min_reduction_table[abm_config[set][0]];
1628 	ram_table->min_reduction[4][0] = min_reduction_table[abm_config[set][0]];
1629 	ram_table->max_reduction[0][0] = max_reduction_table[abm_config[set][0]];
1630 	ram_table->max_reduction[1][0] = max_reduction_table[abm_config[set][0]];
1631 	ram_table->max_reduction[2][0] = max_reduction_table[abm_config[set][0]];
1632 	ram_table->max_reduction[3][0] = max_reduction_table[abm_config[set][0]];
1633 	ram_table->max_reduction[4][0] = max_reduction_table[abm_config[set][0]];
1634 
1635 	ram_table->min_reduction[0][1] = min_reduction_table[abm_config[set][1]];
1636 	ram_table->min_reduction[1][1] = min_reduction_table[abm_config[set][1]];
1637 	ram_table->min_reduction[2][1] = min_reduction_table[abm_config[set][1]];
1638 	ram_table->min_reduction[3][1] = min_reduction_table[abm_config[set][1]];
1639 	ram_table->min_reduction[4][1] = min_reduction_table[abm_config[set][1]];
1640 	ram_table->max_reduction[0][1] = max_reduction_table[abm_config[set][1]];
1641 	ram_table->max_reduction[1][1] = max_reduction_table[abm_config[set][1]];
1642 	ram_table->max_reduction[2][1] = max_reduction_table[abm_config[set][1]];
1643 	ram_table->max_reduction[3][1] = max_reduction_table[abm_config[set][1]];
1644 	ram_table->max_reduction[4][1] = max_reduction_table[abm_config[set][1]];
1645 
1646 	ram_table->min_reduction[0][2] = min_reduction_table[abm_config[set][2]];
1647 	ram_table->min_reduction[1][2] = min_reduction_table[abm_config[set][2]];
1648 	ram_table->min_reduction[2][2] = min_reduction_table[abm_config[set][2]];
1649 	ram_table->min_reduction[3][2] = min_reduction_table[abm_config[set][2]];
1650 	ram_table->min_reduction[4][2] = min_reduction_table[abm_config[set][2]];
1651 	ram_table->max_reduction[0][2] = max_reduction_table[abm_config[set][2]];
1652 	ram_table->max_reduction[1][2] = max_reduction_table[abm_config[set][2]];
1653 	ram_table->max_reduction[2][2] = max_reduction_table[abm_config[set][2]];
1654 	ram_table->max_reduction[3][2] = max_reduction_table[abm_config[set][2]];
1655 	ram_table->max_reduction[4][2] = max_reduction_table[abm_config[set][2]];
1656 
1657 	ram_table->min_reduction[0][3] = min_reduction_table[abm_config[set][3]];
1658 	ram_table->min_reduction[1][3] = min_reduction_table[abm_config[set][3]];
1659 	ram_table->min_reduction[2][3] = min_reduction_table[abm_config[set][3]];
1660 	ram_table->min_reduction[3][3] = min_reduction_table[abm_config[set][3]];
1661 	ram_table->min_reduction[4][3] = min_reduction_table[abm_config[set][3]];
1662 	ram_table->max_reduction[0][3] = max_reduction_table[abm_config[set][3]];
1663 	ram_table->max_reduction[1][3] = max_reduction_table[abm_config[set][3]];
1664 	ram_table->max_reduction[2][3] = max_reduction_table[abm_config[set][3]];
1665 	ram_table->max_reduction[3][3] = max_reduction_table[abm_config[set][3]];
1666 	ram_table->max_reduction[4][3] = max_reduction_table[abm_config[set][3]];
1667 
1668 	ram_table->bright_pos_gain[0][0] = 0x20;
1669 	ram_table->bright_pos_gain[0][1] = 0x20;
1670 	ram_table->bright_pos_gain[0][2] = 0x20;
1671 	ram_table->bright_pos_gain[0][3] = 0x20;
1672 	ram_table->bright_pos_gain[1][0] = 0x20;
1673 	ram_table->bright_pos_gain[1][1] = 0x20;
1674 	ram_table->bright_pos_gain[1][2] = 0x20;
1675 	ram_table->bright_pos_gain[1][3] = 0x20;
1676 	ram_table->bright_pos_gain[2][0] = 0x20;
1677 	ram_table->bright_pos_gain[2][1] = 0x20;
1678 	ram_table->bright_pos_gain[2][2] = 0x20;
1679 	ram_table->bright_pos_gain[2][3] = 0x20;
1680 	ram_table->bright_pos_gain[3][0] = 0x20;
1681 	ram_table->bright_pos_gain[3][1] = 0x20;
1682 	ram_table->bright_pos_gain[3][2] = 0x20;
1683 	ram_table->bright_pos_gain[3][3] = 0x20;
1684 	ram_table->bright_pos_gain[4][0] = 0x20;
1685 	ram_table->bright_pos_gain[4][1] = 0x20;
1686 	ram_table->bright_pos_gain[4][2] = 0x20;
1687 	ram_table->bright_pos_gain[4][3] = 0x20;
1688 	ram_table->bright_neg_gain[0][0] = 0x00;
1689 	ram_table->bright_neg_gain[0][1] = 0x00;
1690 	ram_table->bright_neg_gain[0][2] = 0x00;
1691 	ram_table->bright_neg_gain[0][3] = 0x00;
1692 	ram_table->bright_neg_gain[1][0] = 0x00;
1693 	ram_table->bright_neg_gain[1][1] = 0x00;
1694 	ram_table->bright_neg_gain[1][2] = 0x00;
1695 	ram_table->bright_neg_gain[1][3] = 0x00;
1696 	ram_table->bright_neg_gain[2][0] = 0x00;
1697 	ram_table->bright_neg_gain[2][1] = 0x00;
1698 	ram_table->bright_neg_gain[2][2] = 0x00;
1699 	ram_table->bright_neg_gain[2][3] = 0x00;
1700 	ram_table->bright_neg_gain[3][0] = 0x00;
1701 	ram_table->bright_neg_gain[3][1] = 0x00;
1702 	ram_table->bright_neg_gain[3][2] = 0x00;
1703 	ram_table->bright_neg_gain[3][3] = 0x00;
1704 	ram_table->bright_neg_gain[4][0] = 0x00;
1705 	ram_table->bright_neg_gain[4][1] = 0x00;
1706 	ram_table->bright_neg_gain[4][2] = 0x00;
1707 	ram_table->bright_neg_gain[4][3] = 0x00;
1708 	ram_table->dark_pos_gain[0][0] = 0x00;
1709 	ram_table->dark_pos_gain[0][1] = 0x00;
1710 	ram_table->dark_pos_gain[0][2] = 0x00;
1711 	ram_table->dark_pos_gain[0][3] = 0x00;
1712 	ram_table->dark_pos_gain[1][0] = 0x00;
1713 	ram_table->dark_pos_gain[1][1] = 0x00;
1714 	ram_table->dark_pos_gain[1][2] = 0x00;
1715 	ram_table->dark_pos_gain[1][3] = 0x00;
1716 	ram_table->dark_pos_gain[2][0] = 0x00;
1717 	ram_table->dark_pos_gain[2][1] = 0x00;
1718 	ram_table->dark_pos_gain[2][2] = 0x00;
1719 	ram_table->dark_pos_gain[2][3] = 0x00;
1720 	ram_table->dark_pos_gain[3][0] = 0x00;
1721 	ram_table->dark_pos_gain[3][1] = 0x00;
1722 	ram_table->dark_pos_gain[3][2] = 0x00;
1723 	ram_table->dark_pos_gain[3][3] = 0x00;
1724 	ram_table->dark_pos_gain[4][0] = 0x00;
1725 	ram_table->dark_pos_gain[4][1] = 0x00;
1726 	ram_table->dark_pos_gain[4][2] = 0x00;
1727 	ram_table->dark_pos_gain[4][3] = 0x00;
1728 	ram_table->dark_neg_gain[0][0] = 0x00;
1729 	ram_table->dark_neg_gain[0][1] = 0x00;
1730 	ram_table->dark_neg_gain[0][2] = 0x00;
1731 	ram_table->dark_neg_gain[0][3] = 0x00;
1732 	ram_table->dark_neg_gain[1][0] = 0x00;
1733 	ram_table->dark_neg_gain[1][1] = 0x00;
1734 	ram_table->dark_neg_gain[1][2] = 0x00;
1735 	ram_table->dark_neg_gain[1][3] = 0x00;
1736 	ram_table->dark_neg_gain[2][0] = 0x00;
1737 	ram_table->dark_neg_gain[2][1] = 0x00;
1738 	ram_table->dark_neg_gain[2][2] = 0x00;
1739 	ram_table->dark_neg_gain[2][3] = 0x00;
1740 	ram_table->dark_neg_gain[3][0] = 0x00;
1741 	ram_table->dark_neg_gain[3][1] = 0x00;
1742 	ram_table->dark_neg_gain[3][2] = 0x00;
1743 	ram_table->dark_neg_gain[3][3] = 0x00;
1744 	ram_table->dark_neg_gain[4][0] = 0x00;
1745 	ram_table->dark_neg_gain[4][1] = 0x00;
1746 	ram_table->dark_neg_gain[4][2] = 0x00;
1747 	ram_table->dark_neg_gain[4][3] = 0x00;
1748 
1749 	ram_table->iir_curve[0] = 0x65;
1750 	ram_table->iir_curve[1] = 0x65;
1751 	ram_table->iir_curve[2] = 0x65;
1752 	ram_table->iir_curve[3] = 0x65;
1753 	ram_table->iir_curve[4] = 0x65;
1754 
1755 	//Gamma 2.4
1756 	ram_table->crgb_thresh[0] = cpu_to_be16(0x13b6);
1757 	ram_table->crgb_thresh[1] = cpu_to_be16(0x1648);
1758 	ram_table->crgb_thresh[2] = cpu_to_be16(0x18e3);
1759 	ram_table->crgb_thresh[3] = cpu_to_be16(0x1b41);
1760 	ram_table->crgb_thresh[4] = cpu_to_be16(0x1d46);
1761 	ram_table->crgb_thresh[5] = cpu_to_be16(0x1f21);
1762 	ram_table->crgb_thresh[6] = cpu_to_be16(0x2167);
1763 	ram_table->crgb_thresh[7] = cpu_to_be16(0x2384);
1764 	ram_table->crgb_offset[0] = cpu_to_be16(0x2999);
1765 	ram_table->crgb_offset[1] = cpu_to_be16(0x3999);
1766 	ram_table->crgb_offset[2] = cpu_to_be16(0x4666);
1767 	ram_table->crgb_offset[3] = cpu_to_be16(0x5999);
1768 	ram_table->crgb_offset[4] = cpu_to_be16(0x6333);
1769 	ram_table->crgb_offset[5] = cpu_to_be16(0x7800);
1770 	ram_table->crgb_offset[6] = cpu_to_be16(0x8c00);
1771 	ram_table->crgb_offset[7] = cpu_to_be16(0xa000);
1772 	ram_table->crgb_slope[0]  = cpu_to_be16(0x3147);
1773 	ram_table->crgb_slope[1]  = cpu_to_be16(0x2978);
1774 	ram_table->crgb_slope[2]  = cpu_to_be16(0x23a2);
1775 	ram_table->crgb_slope[3]  = cpu_to_be16(0x1f55);
1776 	ram_table->crgb_slope[4]  = cpu_to_be16(0x1c63);
1777 	ram_table->crgb_slope[5]  = cpu_to_be16(0x1a0f);
1778 	ram_table->crgb_slope[6]  = cpu_to_be16(0x178d);
1779 	ram_table->crgb_slope[7]  = cpu_to_be16(0x15ab);
1780 
1781 	fill_backlight_transform_table(
1782 			params, ram_table);
1783 }
1784 
1785 static void fill_iram_v_2_2(struct iram_table_v_2_2 *ram_table, struct dmcu_iram_parameters params)
1786 {
1787 	unsigned int set = params.set;
1788 
1789 	ram_table->flags = 0x0;
1790 
1791 	ram_table->min_abm_backlight =
1792 			cpu_to_be16(params.min_abm_backlight);
1793 
1794 	ram_table->deviation_gain[0] = 0xb3;
1795 	ram_table->deviation_gain[1] = 0xa8;
1796 	ram_table->deviation_gain[2] = 0x98;
1797 	ram_table->deviation_gain[3] = 0x68;
1798 
1799 	ram_table->min_reduction[0][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1800 	ram_table->min_reduction[1][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1801 	ram_table->min_reduction[2][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1802 	ram_table->min_reduction[3][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1803 	ram_table->min_reduction[4][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1804 	ram_table->max_reduction[0][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1805 	ram_table->max_reduction[1][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1806 	ram_table->max_reduction[2][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1807 	ram_table->max_reduction[3][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1808 	ram_table->max_reduction[4][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1809 
1810 	ram_table->min_reduction[0][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1811 	ram_table->min_reduction[1][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1812 	ram_table->min_reduction[2][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1813 	ram_table->min_reduction[3][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1814 	ram_table->min_reduction[4][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1815 	ram_table->max_reduction[0][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1816 	ram_table->max_reduction[1][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1817 	ram_table->max_reduction[2][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1818 	ram_table->max_reduction[3][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1819 	ram_table->max_reduction[4][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1820 
1821 	ram_table->min_reduction[0][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1822 	ram_table->min_reduction[1][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1823 	ram_table->min_reduction[2][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1824 	ram_table->min_reduction[3][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1825 	ram_table->min_reduction[4][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1826 	ram_table->max_reduction[0][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1827 	ram_table->max_reduction[1][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1828 	ram_table->max_reduction[2][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1829 	ram_table->max_reduction[3][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1830 	ram_table->max_reduction[4][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1831 
1832 	ram_table->min_reduction[0][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1833 	ram_table->min_reduction[1][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1834 	ram_table->min_reduction[2][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1835 	ram_table->min_reduction[3][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1836 	ram_table->min_reduction[4][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1837 	ram_table->max_reduction[0][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1838 	ram_table->max_reduction[1][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1839 	ram_table->max_reduction[2][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1840 	ram_table->max_reduction[3][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1841 	ram_table->max_reduction[4][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1842 
1843 	ram_table->bright_pos_gain[0][0] = 0x20;
1844 	ram_table->bright_pos_gain[0][1] = 0x20;
1845 	ram_table->bright_pos_gain[0][2] = 0x20;
1846 	ram_table->bright_pos_gain[0][3] = 0x20;
1847 	ram_table->bright_pos_gain[1][0] = 0x20;
1848 	ram_table->bright_pos_gain[1][1] = 0x20;
1849 	ram_table->bright_pos_gain[1][2] = 0x20;
1850 	ram_table->bright_pos_gain[1][3] = 0x20;
1851 	ram_table->bright_pos_gain[2][0] = 0x20;
1852 	ram_table->bright_pos_gain[2][1] = 0x20;
1853 	ram_table->bright_pos_gain[2][2] = 0x20;
1854 	ram_table->bright_pos_gain[2][3] = 0x20;
1855 	ram_table->bright_pos_gain[3][0] = 0x20;
1856 	ram_table->bright_pos_gain[3][1] = 0x20;
1857 	ram_table->bright_pos_gain[3][2] = 0x20;
1858 	ram_table->bright_pos_gain[3][3] = 0x20;
1859 	ram_table->bright_pos_gain[4][0] = 0x20;
1860 	ram_table->bright_pos_gain[4][1] = 0x20;
1861 	ram_table->bright_pos_gain[4][2] = 0x20;
1862 	ram_table->bright_pos_gain[4][3] = 0x20;
1863 
1864 	ram_table->dark_pos_gain[0][0] = 0x00;
1865 	ram_table->dark_pos_gain[0][1] = 0x00;
1866 	ram_table->dark_pos_gain[0][2] = 0x00;
1867 	ram_table->dark_pos_gain[0][3] = 0x00;
1868 	ram_table->dark_pos_gain[1][0] = 0x00;
1869 	ram_table->dark_pos_gain[1][1] = 0x00;
1870 	ram_table->dark_pos_gain[1][2] = 0x00;
1871 	ram_table->dark_pos_gain[1][3] = 0x00;
1872 	ram_table->dark_pos_gain[2][0] = 0x00;
1873 	ram_table->dark_pos_gain[2][1] = 0x00;
1874 	ram_table->dark_pos_gain[2][2] = 0x00;
1875 	ram_table->dark_pos_gain[2][3] = 0x00;
1876 	ram_table->dark_pos_gain[3][0] = 0x00;
1877 	ram_table->dark_pos_gain[3][1] = 0x00;
1878 	ram_table->dark_pos_gain[3][2] = 0x00;
1879 	ram_table->dark_pos_gain[3][3] = 0x00;
1880 	ram_table->dark_pos_gain[4][0] = 0x00;
1881 	ram_table->dark_pos_gain[4][1] = 0x00;
1882 	ram_table->dark_pos_gain[4][2] = 0x00;
1883 	ram_table->dark_pos_gain[4][3] = 0x00;
1884 
1885 	ram_table->hybrid_factor[0] = 0xff;
1886 	ram_table->hybrid_factor[1] = 0xff;
1887 	ram_table->hybrid_factor[2] = 0xff;
1888 	ram_table->hybrid_factor[3] = 0xc0;
1889 
1890 	ram_table->contrast_factor[0] = 0x99;
1891 	ram_table->contrast_factor[1] = 0x99;
1892 	ram_table->contrast_factor[2] = 0x90;
1893 	ram_table->contrast_factor[3] = 0x80;
1894 
1895 	ram_table->iir_curve[0] = 0x65;
1896 	ram_table->iir_curve[1] = 0x65;
1897 	ram_table->iir_curve[2] = 0x65;
1898 	ram_table->iir_curve[3] = 0x65;
1899 	ram_table->iir_curve[4] = 0x65;
1900 
1901 	//Gamma 2.2
1902 	ram_table->crgb_thresh[0] = cpu_to_be16(0x127c);
1903 	ram_table->crgb_thresh[1] = cpu_to_be16(0x151b);
1904 	ram_table->crgb_thresh[2] = cpu_to_be16(0x17d5);
1905 	ram_table->crgb_thresh[3] = cpu_to_be16(0x1a56);
1906 	ram_table->crgb_thresh[4] = cpu_to_be16(0x1c83);
1907 	ram_table->crgb_thresh[5] = cpu_to_be16(0x1e72);
1908 	ram_table->crgb_thresh[6] = cpu_to_be16(0x20f0);
1909 	ram_table->crgb_thresh[7] = cpu_to_be16(0x232b);
1910 	ram_table->crgb_offset[0] = cpu_to_be16(0x2999);
1911 	ram_table->crgb_offset[1] = cpu_to_be16(0x3999);
1912 	ram_table->crgb_offset[2] = cpu_to_be16(0x4666);
1913 	ram_table->crgb_offset[3] = cpu_to_be16(0x5999);
1914 	ram_table->crgb_offset[4] = cpu_to_be16(0x6333);
1915 	ram_table->crgb_offset[5] = cpu_to_be16(0x7800);
1916 	ram_table->crgb_offset[6] = cpu_to_be16(0x8c00);
1917 	ram_table->crgb_offset[7] = cpu_to_be16(0xa000);
1918 	ram_table->crgb_slope[0]  = cpu_to_be16(0x3609);
1919 	ram_table->crgb_slope[1]  = cpu_to_be16(0x2dfa);
1920 	ram_table->crgb_slope[2]  = cpu_to_be16(0x27ea);
1921 	ram_table->crgb_slope[3]  = cpu_to_be16(0x235d);
1922 	ram_table->crgb_slope[4]  = cpu_to_be16(0x2042);
1923 	ram_table->crgb_slope[5]  = cpu_to_be16(0x1dc3);
1924 	ram_table->crgb_slope[6]  = cpu_to_be16(0x1b1a);
1925 	ram_table->crgb_slope[7]  = cpu_to_be16(0x1910);
1926 
1927 	fill_backlight_transform_table_v_2_2(
1928 			params, ram_table, true);
1929 }
1930 
1931 static void fill_iram_v_2_3(struct iram_table_v_2_2 *ram_table, struct dmcu_iram_parameters params, bool big_endian)
1932 {
1933 	unsigned int i, j;
1934 	unsigned int set = params.set;
1935 
1936 	ram_table->flags = 0x0;
1937 	ram_table->min_abm_backlight = (uint16_t)((big_endian) ?
1938 		cpu_to_be16(params.min_abm_backlight) :
1939 		cpu_to_le16(params.min_abm_backlight));
1940 
1941 	for (i = 0; i < NUM_AGGR_LEVEL; i++) {
1942 		ram_table->hybrid_factor[i] = (uint8_t)abm_settings[set][i].brightness_gain;
1943 		ram_table->contrast_factor[i] = abm_settings[set][i].contrast_factor;
1944 		ram_table->deviation_gain[i] = abm_settings[set][i].deviation_gain;
1945 		ram_table->min_knee[i] = abm_settings[set][i].min_knee;
1946 		ram_table->max_knee[i] = abm_settings[set][i].max_knee;
1947 
1948 		for (j = 0; j < NUM_AMBI_LEVEL; j++) {
1949 			ram_table->min_reduction[j][i] = abm_settings[set][i].min_reduction;
1950 			ram_table->max_reduction[j][i] = abm_settings[set][i].max_reduction;
1951 			ram_table->bright_pos_gain[j][i] = abm_settings[set][i].bright_pos_gain;
1952 			ram_table->dark_pos_gain[j][i] = abm_settings[set][i].dark_pos_gain;
1953 		}
1954 	}
1955 
1956 	ram_table->iir_curve[0] = 0x65;
1957 	ram_table->iir_curve[1] = 0x65;
1958 	ram_table->iir_curve[2] = 0x65;
1959 	ram_table->iir_curve[3] = 0x65;
1960 	ram_table->iir_curve[4] = 0x65;
1961 
1962 	//Gamma 2.2
1963 	ram_table->crgb_thresh[0] = bswap16_based_on_endian(big_endian, 0x127c);
1964 	ram_table->crgb_thresh[1] = bswap16_based_on_endian(big_endian, 0x151b);
1965 	ram_table->crgb_thresh[2] = bswap16_based_on_endian(big_endian, 0x17d5);
1966 	ram_table->crgb_thresh[3] = bswap16_based_on_endian(big_endian, 0x1a56);
1967 	ram_table->crgb_thresh[4] = bswap16_based_on_endian(big_endian, 0x1c83);
1968 	ram_table->crgb_thresh[5] = bswap16_based_on_endian(big_endian, 0x1e72);
1969 	ram_table->crgb_thresh[6] = bswap16_based_on_endian(big_endian, 0x20f0);
1970 	ram_table->crgb_thresh[7] = bswap16_based_on_endian(big_endian, 0x232b);
1971 	ram_table->crgb_offset[0] = bswap16_based_on_endian(big_endian, 0x2999);
1972 	ram_table->crgb_offset[1] = bswap16_based_on_endian(big_endian, 0x3999);
1973 	ram_table->crgb_offset[2] = bswap16_based_on_endian(big_endian, 0x4666);
1974 	ram_table->crgb_offset[3] = bswap16_based_on_endian(big_endian, 0x5999);
1975 	ram_table->crgb_offset[4] = bswap16_based_on_endian(big_endian, 0x6333);
1976 	ram_table->crgb_offset[5] = bswap16_based_on_endian(big_endian, 0x7800);
1977 	ram_table->crgb_offset[6] = bswap16_based_on_endian(big_endian, 0x8c00);
1978 	ram_table->crgb_offset[7] = bswap16_based_on_endian(big_endian, 0xa000);
1979 	ram_table->crgb_slope[0]  = bswap16_based_on_endian(big_endian, 0x3609);
1980 	ram_table->crgb_slope[1]  = bswap16_based_on_endian(big_endian, 0x2dfa);
1981 	ram_table->crgb_slope[2]  = bswap16_based_on_endian(big_endian, 0x27ea);
1982 	ram_table->crgb_slope[3]  = bswap16_based_on_endian(big_endian, 0x235d);
1983 	ram_table->crgb_slope[4]  = bswap16_based_on_endian(big_endian, 0x2042);
1984 	ram_table->crgb_slope[5]  = bswap16_based_on_endian(big_endian, 0x1dc3);
1985 	ram_table->crgb_slope[6]  = bswap16_based_on_endian(big_endian, 0x1b1a);
1986 	ram_table->crgb_slope[7]  = bswap16_based_on_endian(big_endian, 0x1910);
1987 
1988 	fill_backlight_transform_table_v_2_2(
1989 			params, ram_table, big_endian);
1990 }
1991 
1992 bool dmub_init_abm_config(struct resource_pool *res_pool,
1993 	struct dmcu_iram_parameters params,
1994 	unsigned int inst)
1995 {
1996 	struct iram_table_v_2_2 ram_table;
1997 	struct abm_config_table config;
1998 	unsigned int set = params.set;
1999 	bool result = false;
2000 	uint32_t i, j = 0;
2001 
2002 	if (res_pool->abm == NULL && res_pool->multiple_abms[inst] == NULL)
2003 		return false;
2004 
2005 	memset(&ram_table, 0, sizeof(ram_table));
2006 	memset(&config, 0, sizeof(config));
2007 
2008 	fill_iram_v_2_3(&ram_table, params, false);
2009 
2010 	// We must copy to structure that is aligned to 32-bit
2011 	for (i = 0; i < NUM_POWER_FN_SEGS; i++) {
2012 		config.crgb_thresh[i] = ram_table.crgb_thresh[i];
2013 		config.crgb_offset[i] = ram_table.crgb_offset[i];
2014 		config.crgb_slope[i] = ram_table.crgb_slope[i];
2015 	}
2016 
2017 	for (i = 0; i < NUM_BL_CURVE_SEGS; i++) {
2018 		config.backlight_thresholds[i] = ram_table.backlight_thresholds[i];
2019 		config.backlight_offsets[i] = ram_table.backlight_offsets[i];
2020 	}
2021 
2022 	for (i = 0; i < NUM_AMBI_LEVEL; i++)
2023 		config.iir_curve[i] = ram_table.iir_curve[i];
2024 
2025 	for (i = 0; i < NUM_AMBI_LEVEL; i++) {
2026 		for (j = 0; j < NUM_AGGR_LEVEL; j++) {
2027 			config.min_reduction[i][j] = ram_table.min_reduction[i][j];
2028 			config.max_reduction[i][j] = ram_table.max_reduction[i][j];
2029 			config.bright_pos_gain[i][j] = ram_table.bright_pos_gain[i][j];
2030 			config.dark_pos_gain[i][j] = ram_table.dark_pos_gain[i][j];
2031 		}
2032 	}
2033 
2034 	for (i = 0; i < NUM_AGGR_LEVEL; i++) {
2035 		config.hybrid_factor[i] = ram_table.hybrid_factor[i];
2036 		config.contrast_factor[i] = ram_table.contrast_factor[i];
2037 		config.deviation_gain[i] = ram_table.deviation_gain[i];
2038 		config.min_knee[i] = ram_table.min_knee[i];
2039 		config.max_knee[i] = ram_table.max_knee[i];
2040 	}
2041 
2042 	if (params.backlight_ramping_override) {
2043 
2044 		ASSERT(params.backlight_ramping_reduction <= 0xFFFF);
2045 		ASSERT(params.backlight_ramping_start <= 0xFFFF);
2046 		for (i = 0; i < NUM_AGGR_LEVEL; i++) {
2047 			config.blRampReduction[i] = (uint16_t)params.backlight_ramping_reduction;
2048 			config.blRampStart[i]     = (uint16_t)params.backlight_ramping_start;
2049 		}
2050 	} else {
2051 		for (i = 0; i < NUM_AGGR_LEVEL; i++) {
2052 			config.blRampReduction[i] = abm_settings[set][i].blRampReduction;
2053 			config.blRampStart[i] = abm_settings[set][i].blRampStart;
2054 		}
2055 	}
2056 
2057 	config.min_abm_backlight = ram_table.min_abm_backlight;
2058 
2059 	if (res_pool->multiple_abms[inst]) {
2060 		result = res_pool->multiple_abms[inst]->funcs->init_abm_config(
2061 			res_pool->multiple_abms[inst], (char *)(&config), sizeof(struct abm_config_table), inst);
2062 	} else
2063 		result = res_pool->abm->funcs->init_abm_config(
2064 			res_pool->abm, (char *)(&config), sizeof(struct abm_config_table), 0);
2065 
2066 	return result;
2067 }
2068 
2069 bool dmub_is_abm_supported(struct resource_pool *res_pool, unsigned int inst)
2070 {
2071 
2072 	if (res_pool->abm == NULL && res_pool->multiple_abms[inst] == NULL)
2073 		return false;
2074 
2075 	return true;
2076 }
2077 
2078 bool dmub_set_abm_event(struct resource_pool *res_pool,
2079 		unsigned int full_screen, unsigned int trans_info,
2080 		unsigned int hdr_mode, unsigned int scaling_enable, unsigned int scaling_strength_map,
2081 		unsigned int inst)
2082 {
2083 	bool result = false;
2084 
2085 	if (res_pool->abm == NULL && res_pool->multiple_abms[inst] == NULL)
2086 		return false;
2087 
2088 	if (res_pool->multiple_abms[inst]) {
2089 		if (res_pool->multiple_abms[inst]->funcs->set_abm_event)
2090 			result = res_pool->multiple_abms[inst]->funcs->set_abm_event(
2091 				res_pool->multiple_abms[inst], full_screen, trans_info,
2092 					hdr_mode, scaling_enable, scaling_strength_map, inst);
2093 	} else {
2094 		if (res_pool->abm->funcs->set_abm_event)
2095 			result = res_pool->abm->funcs->set_abm_event(
2096 					res_pool->abm, full_screen, trans_info,
2097 					hdr_mode, scaling_enable, scaling_strength_map, inst);
2098 	}
2099 
2100 	return result;
2101 }
2102 
2103 bool dmub_set_abm_strength(struct resource_pool *res_pool,
2104 	unsigned int strength,
2105 	unsigned int inst)
2106 {
2107 	bool result = false;
2108 
2109 	if (res_pool->abm == NULL && res_pool->multiple_abms[inst] == NULL)
2110 		return false;
2111 
2112 	if (res_pool->multiple_abms[inst]) {
2113 		if (res_pool->multiple_abms[inst]->funcs->set_abm_level)
2114 			result = res_pool->multiple_abms[inst]->funcs->set_abm_level(
2115 				res_pool->multiple_abms[inst], strength);
2116 	} else {
2117 		if (res_pool->abm->funcs->set_abm_level)
2118 			result = res_pool->abm->funcs->set_abm_level(
2119 					res_pool->abm, strength);
2120 	}
2121 
2122 	return result;
2123 }
2124 
2125 bool dmcu_load_iram(struct dmcu *dmcu,
2126 	struct dmcu_iram_parameters params)
2127 {
2128 	unsigned char ram_table[IRAM_SIZE];
2129 	bool result = false;
2130 
2131 	if (dmcu == NULL)
2132 		return false;
2133 
2134 	if (dmcu && !dmcu->funcs->is_dmcu_initialized(dmcu))
2135 		return true;
2136 
2137 	memset(&ram_table, 0, sizeof(ram_table));
2138 
2139 	if (dmcu->dmcu_version.abm_version == 0x24) {
2140 		fill_iram_v_2_3((struct iram_table_v_2_2 *)ram_table, params, true);
2141 		result = dmcu->funcs->load_iram(dmcu, 0, (char *)(&ram_table),
2142 						IRAM_RESERVE_AREA_START_V2_2);
2143 	} else if (dmcu->dmcu_version.abm_version == 0x23) {
2144 		fill_iram_v_2_3((struct iram_table_v_2_2 *)ram_table, params, true);
2145 
2146 		result = dmcu->funcs->load_iram(
2147 				dmcu, 0, (char *)(&ram_table), IRAM_RESERVE_AREA_START_V2_2);
2148 	} else if (dmcu->dmcu_version.abm_version == 0x22) {
2149 		fill_iram_v_2_2((struct iram_table_v_2_2 *)ram_table, params);
2150 
2151 		result = dmcu->funcs->load_iram(
2152 				dmcu, 0, (char *)(&ram_table), IRAM_RESERVE_AREA_START_V2_2);
2153 	} else {
2154 		fill_iram_v_2((struct iram_table_v_2 *)ram_table, params);
2155 
2156 		result = dmcu->funcs->load_iram(
2157 				dmcu, 0, (char *)(&ram_table), IRAM_RESERVE_AREA_START_V2);
2158 
2159 		if (result)
2160 			result = dmcu->funcs->load_iram(
2161 					dmcu, IRAM_RESERVE_AREA_END_V2 + 1,
2162 					(char *)(&ram_table) + IRAM_RESERVE_AREA_END_V2 + 1,
2163 					sizeof(ram_table) - IRAM_RESERVE_AREA_END_V2 - 1);
2164 	}
2165 
2166 	return result;
2167 }
2168 
2169 bool fill_custom_backlight_caps(unsigned int config_no, struct dm_acpi_atif_backlight_caps *caps)
2170 {
2171 	unsigned int data_points_size;
2172 	uint64_t caps_size;
2173 
2174 	if (config_no >= ARRAY_SIZE(custom_backlight_profiles))
2175 		return false;
2176 
2177 	data_points_size = custom_backlight_profiles[config_no].num_data_points
2178 			* sizeof(custom_backlight_profiles[config_no].data_points[0]);
2179 
2180 	caps_size = sizeof(struct dm_acpi_atif_backlight_caps) - sizeof(caps->data_points) + data_points_size;
2181 	ASSERT(caps_size <= 0xFFFF);
2182 	caps->size = (uint16_t)caps_size;
2183 	caps->flags = 0;
2184 	caps->error_code = 0;
2185 	caps->ac_level_percentage = custom_backlight_profiles[config_no].ac_level_percentage;
2186 	caps->dc_level_percentage = custom_backlight_profiles[config_no].dc_level_percentage;
2187 	caps->min_input_signal = custom_backlight_profiles[config_no].min_input_signal;
2188 	caps->max_input_signal = custom_backlight_profiles[config_no].max_input_signal;
2189 	caps->num_data_points = (uint8_t)custom_backlight_profiles[config_no].num_data_points;
2190 	memcpy(caps->data_points, custom_backlight_profiles[config_no].data_points, data_points_size);
2191 	return true;
2192 }
2193