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