xref: /linux/drivers/gpu/drm/amd/display/modules/power/power_abm.c (revision 5b69770e945caa1bef6c4848bb279ec09d3e9d4f)
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 			dc_link_set_backlight_level_nits(link, core_power->bl_state[panel_inst].isHDR,
722 				core_power->bl_state[panel_inst].backlight_millinit, 0);
723 
724 		backlight_level_params.frame_ramp = 0;
725 
726 		fill_backlight_level_params(core_power, &backlight_level_params, panel_inst, aux_inst,
727 			core_power->bl_state[panel_inst].backlight_pwm, link->backlight_control_type,
728 			core_power->bl_state[panel_inst].backlight_millinit, 0, is_hdr);
729 
730 		dc_link_set_backlight_level(link, &backlight_level_params);
731 }
732 
733 static bool set_backlight_millinits_aux(struct core_power *core_power,
734 		struct dc_stream_state *stream,
735 		unsigned int backlight_millinits,
736 		unsigned int transition_time_millisec,
737 		unsigned int inst)
738 {
739 	struct dc_link *link = NULL;
740 
741 	if (core_power == NULL)
742 		return false;
743 
744 	if (stream == NULL)
745 		return true;
746 
747 	link = dc_stream_get_link(stream);
748 
749 	return dc_link_set_backlight_level_nits(link, core_power->bl_state[inst].isHDR,
750 			backlight_millinits, transition_time_millisec);
751 }
752 
753 static bool set_backlight(struct core_power *core_power,
754 		struct dc_stream_state *stream,
755 		struct set_backlight_level_params *backlight_level_params,
756 		unsigned int inst)
757 {
758 	bool retv = false;
759 	unsigned int frame_ramp = 0;
760 	unsigned int vsync_rate_hz;
761 	union dmcu_abm_set_bl_params params;
762 	const struct dc_link *link = NULL;
763 	unsigned int backlight_pwm_u16_16 = backlight_level_params->backlight_pwm_u16_16;
764 	unsigned int transition_time_millisec = backlight_level_params->transition_time_in_ms;
765 
766 	if (core_power == NULL)
767 		return false;
768 
769 	core_power->bl_state[inst].backlight_pwm = backlight_pwm_u16_16;
770 
771 	if (stream == NULL)
772 		return true;
773 
774 	if (stream->link->connector_signal != SIGNAL_TYPE_EDP)
775 		return false;
776 
777 	if (transition_time_millisec != 0) {
778 		unsigned int v_total =
779 			(stream->adjust.v_total_max == 0) ? stream->timing.v_total : stream->adjust.v_total_max;
780 
781 		vsync_rate_hz = (unsigned int)div_u64(div_u64((stream->
782 			timing.pix_clk_100hz * 100),
783 			v_total),
784 			stream->timing.h_total);
785 
786 		if (core_power->bl_state[inst].smooth_brightness_enabled)
787 			frame_ramp = ((vsync_rate_hz *
788 				transition_time_millisec) + 500) / 1000;
789 	}
790 
791 	core_power->bl_state[inst].frame_ramp = frame_ramp;
792 	params.u32All = 0;
793 	params.bits.gradual_change = (frame_ramp > 0);
794 	params.bits.frame_ramp = frame_ramp;
795 	link = dc_stream_get_link(stream);
796 
797 	mod_power_set_psr_event(&core_power->mod_public, stream, true, psr_event_hw_programming, true);
798 	mod_power_set_replay_event(&core_power->mod_public, stream, true, replay_event_hw_programming, true);
799 
800 	backlight_level_params->frame_ramp = params.u32All;
801 	retv = dc_link_set_backlight_level(link, backlight_level_params);
802 
803 	mod_power_set_psr_event(&core_power->mod_public, stream, false, psr_event_hw_programming, false);
804 	mod_power_set_replay_event(&core_power->mod_public, stream, false, replay_event_hw_programming, false);
805 
806 	return retv;
807 }
808 
809 void fill_backlight_level_params(struct core_power *core_power,
810 	struct set_backlight_level_params *backlight_level_params,
811 	int panel_inst, uint8_t aux_inst, unsigned int backlight_pwm,
812 	enum backlight_control_type backlight_control_type,
813 	unsigned int backlight_millinit, unsigned int transition_time_millisec,
814 	bool is_hdr)
815 {
816 	struct pwr_backlight_properties *bl_prop = &core_power->bl_prop[panel_inst];
817 
818 	backlight_level_params->aux_inst = aux_inst;
819 	backlight_level_params->backlight_pwm_u16_16 = backlight_pwm;
820 	backlight_level_params->control_type = backlight_control_type;
821 	backlight_level_params->backlight_millinits = backlight_millinit;
822 	backlight_level_params->transition_time_in_ms = transition_time_millisec;
823 	backlight_level_params->min_luminance = bl_prop->min_brightness_millinits;
824 	backlight_level_params->max_luminance = bl_prop->max_brightness_millinits;
825 	backlight_level_params->min_backlight_pwm = bl_prop->min_backlight_pwm;
826 	backlight_level_params->max_backlight_pwm = bl_prop->max_backlight_pwm;
827 
828 	if (backlight_control_type == BACKLIGHT_CONTROL_AMD_AUX && !is_hdr)
829 		backlight_level_params->control_type = BACKLIGHT_CONTROL_PWM;
830 }
831 
832 bool mod_power_set_backlight_nits(struct mod_power *mod_power,
833 		struct dc_stream_state *stream,
834 		unsigned int backlight_millinit,
835 		unsigned int transition_time_millisec,
836 		bool skip_aux,
837 		bool is_hdr)
838 {
839 	struct core_power *core_power = NULL;
840 	unsigned int backlight_pwm;
841 	unsigned int panel_inst = 0;
842 	struct set_backlight_level_params backlight_level_params = { 0 };
843 	const struct dc_link *link = NULL;
844 	uint8_t aux_inst = 0;
845 
846 	if (mod_power == NULL)
847 		return false;
848 
849 	core_power = MOD_POWER_TO_CORE(mod_power);
850 	link = dc_stream_get_link(stream);
851 
852 	aux_inst = link->dc->link_srv->get_ddc_aux_inst(link);
853 
854 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &panel_inst))
855 		return false;
856 
857 	if (!skip_aux) {
858 		if (!set_backlight_millinits_aux(core_power, stream,
859 						backlight_millinit, transition_time_millisec, panel_inst))
860 			return false;
861 	}
862 // always send both AUX (above) and PWM (below)
863 	core_power->bl_state[panel_inst].backlight_millinit = backlight_millinit;
864 
865 	core_power->bl_state[panel_inst].backlight_millipercent =
866 		backlight_millinit_to_millipercent(
867 				core_power, backlight_millinit, panel_inst);
868 
869 	backlight_pwm = backlight_millinit_to_pwm(
870 				core_power, backlight_millinit, panel_inst);
871 
872 	fill_backlight_level_params(core_power, &backlight_level_params, panel_inst, aux_inst, backlight_pwm,
873 		link->backlight_control_type, backlight_millinit, transition_time_millisec, is_hdr);
874 
875 	return set_backlight(core_power, stream,
876 			&backlight_level_params, panel_inst);
877 }
878 
879 bool mod_power_backlight_percent_to_nits(struct mod_power *mod_power,
880 		struct dc_stream_state *stream,
881 		unsigned int backlight_millipercent,
882 		unsigned int *backlight_millinit)
883 {
884 	struct core_power *core_power = NULL;
885 	unsigned int inst = 0;
886 
887 	if (mod_power == NULL)
888 		return false;
889 
890 	core_power = MOD_POWER_TO_CORE(mod_power);
891 
892 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
893 		return false;
894 
895 	*backlight_millinit = backlight_millipercent_to_millinit(
896 			core_power, backlight_millipercent, inst);
897 	return true;
898 }
899 
900 bool mod_power_backlight_nits_to_percent(struct mod_power *mod_power,
901 		struct dc_stream_state *stream,
902 		unsigned int backlight_millinit,
903 		unsigned int *backlight_millipercent)
904 {
905 	struct core_power *core_power = NULL;
906 	unsigned int inst = 0;
907 
908 	if (mod_power == NULL)
909 		return false;
910 
911 	core_power = MOD_POWER_TO_CORE(mod_power);
912 
913 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
914 		return false;
915 
916 	*backlight_millipercent = backlight_millinit_to_millipercent(
917 			core_power, backlight_millinit, inst);
918 	return true;
919 }
920 
921 bool mod_power_set_backlight_percent(struct mod_power *mod_power,
922 		struct dc_stream_state *stream,
923 		unsigned int backlight_millipercent,
924 		unsigned int transition_time_millisec,
925 		bool is_hdr)
926 {
927 	struct core_power *core_power = NULL;
928 	struct set_backlight_level_params backlight_level_params = { 0 };
929 	const struct dc_link *link = NULL;
930 	unsigned int backlight_pwm;
931 	unsigned int panel_inst = 0;
932 	uint8_t aux_inst = 0;
933 
934 	if (mod_power == NULL)
935 		return false;
936 
937 	core_power = MOD_POWER_TO_CORE(mod_power);
938 	link = dc_stream_get_link(stream);
939 	aux_inst = link->dc->link_srv->get_ddc_aux_inst(link);
940 
941 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &panel_inst))
942 		return false;
943 	core_power->bl_state[panel_inst].backlight_millipercent = backlight_millipercent;
944 
945 	core_power->bl_state[panel_inst].backlight_millinit =
946 		backlight_millipercent_to_millinit(
947 				core_power, backlight_millipercent, panel_inst);
948 
949 	backlight_pwm = backlight_millipercent_to_pwm(
950 				core_power, backlight_millipercent, panel_inst);
951 
952 	fill_backlight_level_params(core_power, &backlight_level_params, panel_inst,
953 		aux_inst, backlight_pwm, link->backlight_control_type,
954 		core_power->bl_state[panel_inst].backlight_millinit, transition_time_millisec, is_hdr);
955 
956 	return set_backlight(core_power, stream,
957 			&backlight_level_params, panel_inst);
958 }
959 
960 void mod_power_update_backlight(struct mod_power *mod_power,
961 		struct dc_stream_state *stream,
962 		unsigned int backlight_millipercent)
963 {
964 	struct core_power *core_power = NULL;
965 	unsigned int inst = 0;
966 
967 	if (mod_power == NULL)
968 		return;
969 
970 	core_power = MOD_POWER_TO_CORE(mod_power);
971 
972 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
973 		return;
974 	core_power->bl_state[inst].backlight_millipercent = backlight_millipercent;
975 
976 	core_power->bl_state[inst].backlight_millinit =
977 		backlight_millipercent_to_millinit(
978 			core_power, backlight_millipercent, inst);
979 
980 	core_power->bl_state[inst].backlight_pwm = backlight_millipercent_to_pwm(
981 		core_power, backlight_millipercent, inst);
982 }
983 
984 void mod_power_update_backlight_nits(struct mod_power *mod_power,
985 		struct dc_stream_state *stream,
986 		unsigned int backlight_millinit)
987 {
988 	struct core_power *core_power = NULL;
989 	unsigned int inst = 0;
990 
991 	if (mod_power == NULL)
992 		return;
993 
994 	core_power = MOD_POWER_TO_CORE(mod_power);
995 
996 	if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
997 		return;
998 
999 	core_power->bl_state[inst].backlight_millinit = backlight_millinit;
1000 
1001 	core_power->bl_state[inst].backlight_millipercent = backlight_millinit_to_millipercent(
1002 		core_power, backlight_millinit, inst);
1003 	core_power->bl_state[inst].backlight_pwm = backlight_millinit_to_pwm(
1004 		core_power, backlight_millinit, inst);
1005 }
1006 
1007 bool mod_power_get_backlight_pwm(struct mod_power *mod_power,
1008 		unsigned int *backlight_pwm,
1009 		unsigned int inst)
1010 {
1011 	struct core_power *core_power = NULL;
1012 
1013 	if (mod_power == NULL)
1014 		return false;
1015 
1016 	core_power = MOD_POWER_TO_CORE(mod_power);
1017 
1018 	*backlight_pwm = core_power->bl_state[inst].backlight_pwm;
1019 
1020 	return true;
1021 }
1022 
1023 bool mod_power_get_backlight_nits(struct mod_power *mod_power,
1024 		unsigned int *backlight_millinit,
1025 		unsigned int inst)
1026 {
1027 	struct core_power *core_power = NULL;
1028 
1029 	if (mod_power == NULL)
1030 		return false;
1031 
1032 	core_power = MOD_POWER_TO_CORE(mod_power);
1033 
1034 	*backlight_millinit = core_power->bl_state[inst].backlight_millinit;
1035 
1036 	return true;
1037 }
1038 
1039 bool mod_power_get_backlight_percent(struct mod_power *mod_power,
1040 		unsigned int *backlight_millipercent,
1041 		unsigned int inst)
1042 {
1043 	struct core_power *core_power = NULL;
1044 
1045 	if (mod_power == NULL)
1046 		return false;
1047 
1048 	core_power = MOD_POWER_TO_CORE(mod_power);
1049 
1050 	*backlight_millipercent = core_power->bl_state[inst].backlight_millipercent;
1051 
1052 	return true;
1053 }
1054 
1055 bool mod_power_get_hw_target_backlight_pwm_nits(struct mod_power *mod_power,
1056 		const struct dc_link *link,
1057 		unsigned int *backlight_millinit,
1058 		unsigned int inst)
1059 {
1060 	struct core_power *core_power = NULL;
1061 	unsigned int backlight_u16_16 = 0;
1062 
1063 	if (mod_power == NULL)
1064 		return false;
1065 
1066 	core_power = MOD_POWER_TO_CORE(mod_power);
1067 
1068 	if (mod_power_get_hw_target_backlight_pwm(mod_power, link,
1069 							&backlight_u16_16)) {
1070 		*backlight_millinit =
1071 			backlight_pwm_to_millinit(core_power,
1072 					backlight_u16_16, inst);
1073 		return true;
1074 	}
1075 	return false;
1076 }
1077 
1078 bool mod_power_get_hw_target_backlight_pwm_percent(struct mod_power *mod_power,
1079 		const struct dc_link *link,
1080 		unsigned int *backlight_millipercent,
1081 		unsigned int inst)
1082 {
1083 	struct core_power *core_power = NULL;
1084 	unsigned int backlight_u16_16 = 0;
1085 
1086 	if (mod_power == NULL)
1087 		return false;
1088 
1089 	core_power = MOD_POWER_TO_CORE(mod_power);
1090 
1091 	if (mod_power_get_hw_target_backlight_pwm(mod_power, link,
1092 							&backlight_u16_16)) {
1093 		*backlight_millipercent =
1094 			backlight_pwm_to_millipercent(core_power,
1095 					backlight_u16_16, inst);
1096 		return true;
1097 	}
1098 	return false;
1099 }
1100 
1101 bool mod_power_get_hw_target_backlight_pwm(struct mod_power *mod_power,
1102 		const struct dc_link *link,
1103 		unsigned int *backlight_u16_16)
1104 {
1105 	if (mod_power == NULL)
1106 		return false;
1107 
1108 	*backlight_u16_16 = dc_link_get_target_backlight_pwm(link);
1109 
1110 	return true;
1111 }
1112 
1113 bool mod_power_get_hw_backlight_pwm_nits(struct mod_power *mod_power,
1114 		const struct dc_link *link,
1115 		unsigned int *backlight_millinit,
1116 		unsigned int inst)
1117 {
1118 	struct core_power *core_power = NULL;
1119 	unsigned int backlight_u16_16 = 0;
1120 
1121 	if (mod_power == NULL)
1122 		return false;
1123 
1124 	core_power = MOD_POWER_TO_CORE(mod_power);
1125 
1126 	if (mod_power_get_hw_backlight_pwm(mod_power, link, &backlight_u16_16)) {
1127 		*backlight_millinit =
1128 			backlight_pwm_to_millinit(core_power,
1129 					backlight_u16_16, inst);
1130 		return true;
1131 	}
1132 	return false;
1133 }
1134 
1135 bool mod_power_get_hw_backlight_aux_nits(struct mod_power *mod_power,
1136 		struct dc_stream_state **streams, int num_streams,
1137 		unsigned int *backlight_millinit_avg,
1138 		unsigned int *backlight_millinit_peak)
1139 {
1140 	struct core_power *core_power = NULL;
1141 	struct dc_link *link = NULL;
1142 	int stream_index;
1143 
1144 	if (mod_power == NULL)
1145 		return false;
1146 
1147 	core_power = MOD_POWER_TO_CORE(mod_power);
1148 
1149 	if (core_power == NULL)
1150 		return false;
1151 
1152 	if (num_streams < 1)
1153 		return true;
1154 
1155 	for (stream_index = 0; stream_index < num_streams; stream_index++)
1156 		if (streams[stream_index]->link->connector_signal == SIGNAL_TYPE_EDP ||
1157 				streams[stream_index]->link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT)
1158 			break;
1159 
1160 	if (stream_index == num_streams)
1161 		return false;
1162 
1163 	link = dc_stream_get_link(streams[stream_index]);
1164 	if (link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 0)
1165 		return false;
1166 
1167 	return dc_link_get_backlight_level_nits(link, backlight_millinit_avg,
1168 			backlight_millinit_peak);
1169 }
1170 
1171 bool mod_power_get_hw_backlight_pwm_percent(struct mod_power *mod_power,
1172 		const struct dc_link *link,
1173 		unsigned int *backlight_millipercent,
1174 		unsigned int inst)
1175 {
1176 	struct core_power *core_power = NULL;
1177 	unsigned int backlight_u16_16 = 0;
1178 
1179 	if (mod_power == NULL)
1180 		return false;
1181 
1182 	core_power = MOD_POWER_TO_CORE(mod_power);
1183 
1184 	if (mod_power_get_hw_backlight_pwm(mod_power, link, &backlight_u16_16)) {
1185 		*backlight_millipercent =
1186 			backlight_pwm_to_millipercent(core_power,
1187 					backlight_u16_16, inst);
1188 		return true;
1189 	}
1190 	return false;
1191 }
1192 
1193 bool mod_power_get_hw_backlight_pwm(struct mod_power *mod_power,
1194 		const struct dc_link *link,
1195 		unsigned int *backlight_u16_16)
1196 {
1197 	if (mod_power == NULL)
1198 		return false;
1199 
1200 	*backlight_u16_16 = dc_link_get_backlight_level(link);
1201 
1202 	return true;
1203 }
1204 
1205 bool mod_power_get_panel_backlight_boundaries(
1206 				struct mod_power *mod_power,
1207 				unsigned int *out_min_backlight,
1208 				unsigned int *out_max_backlight,
1209 				unsigned int *out_ac_backlight_percent,
1210 				unsigned int *out_dc_backlight_percent,
1211 				unsigned int inst)
1212 {
1213 	struct core_power *core_power = NULL;
1214 
1215 	if (mod_power == NULL)
1216 		return false;
1217 
1218 	core_power = MOD_POWER_TO_CORE(mod_power);
1219 
1220 	/* If cache was successfully updated,
1221 	 * copy the values to output structure and return success
1222 	 */
1223 	if (core_power->bl_prop[inst].backlight_caps_valid) {
1224 		*out_min_backlight = core_power->bl_prop[inst].backlight_lut[0];
1225 		*out_max_backlight =
1226 			core_power->bl_prop[inst].backlight_lut[
1227 				core_power->bl_prop[inst].num_backlight_levels - 1];
1228 		*out_ac_backlight_percent =
1229 			core_power->bl_prop[inst].ac_backlight_percent;
1230 		*out_dc_backlight_percent =
1231 			core_power->bl_prop[inst].dc_backlight_percent;
1232 
1233 		return true;
1234 	}
1235 
1236 	return false;
1237 }
1238 
1239 bool mod_power_set_smooth_brightness(struct mod_power *mod_power,
1240 		bool enable_brightness,
1241 		unsigned int inst)
1242 {
1243 	struct core_power *core_power = NULL;
1244 
1245 	if (mod_power == NULL)
1246 		return false;
1247 
1248 	core_power = MOD_POWER_TO_CORE(mod_power);
1249 
1250 	core_power->bl_state[inst].smooth_brightness_enabled = enable_brightness;
1251 
1252 	return true;
1253 }
1254 
1255 bool mod_power_varibright_feature_enable(struct mod_power *mod_power, bool enable,
1256 		struct dc_stream_update *stream_update)
1257 {
1258 	struct core_power *core_power = NULL;
1259 
1260 	if (mod_power == NULL)
1261 		return false;
1262 
1263 	core_power = MOD_POWER_TO_CORE(mod_power);
1264 	core_power->varibright_prop.varibright_user_enable = enable;
1265 
1266 	/* find abm hw level to program, and save in stream update */
1267 	varibright_set_level(core_power);
1268 	*stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1269 
1270 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1271 						WPP_BIT_FLAG_Backlight_ABM,
1272 						">ABM feature enable: enable=%u su->varibright_level=%u varibright_hw_level=%u",
1273 						(unsigned int) enable,
1274 						*stream_update->abm_level,
1275 						core_power->varibright_prop.varibright_hw_level);
1276 	return true;
1277 }
1278 
1279 bool mod_power_varibright_activate(struct mod_power *mod_power,
1280 		bool activate,
1281 		struct dc_stream_update *stream_update)
1282 {
1283 	struct core_power *core_power = NULL;
1284 
1285 	if (mod_power == NULL)
1286 		return false;
1287 
1288 	core_power = MOD_POWER_TO_CORE(mod_power);
1289 	core_power->varibright_prop.varibright_active = activate;
1290 
1291 	/* find abm hw level to program, and save in stream update */
1292 	varibright_set_level(core_power);
1293 	*stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1294 
1295 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1296 						WPP_BIT_FLAG_Backlight_ABM,
1297 						">ABM activate: activate=%u su->varibright_level=%u",
1298 						(unsigned int) activate,
1299 						*stream_update->abm_level);
1300 	return true;
1301 }
1302 bool mod_power_varibright_set_level(struct mod_power *mod_power, unsigned int level,
1303 		struct dc_stream_update *stream_update)
1304 {
1305 	struct core_power *core_power = NULL;
1306 
1307 	if (mod_power == NULL)
1308 		return false;
1309 
1310 	core_power = MOD_POWER_TO_CORE(mod_power);
1311 	core_power->varibright_prop.varibright_level = level;
1312 	core_power->varibright_prop.varibright_hw_level = level;
1313 
1314 	/* find abm hw level to program, and save in stream update */
1315 	varibright_set_level(core_power);
1316 	*stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1317 
1318 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1319 						WPP_BIT_FLAG_Backlight_ABM,
1320 						">ABM set level: level=%u -> (varibright_level=%u varibright_hw_level=%u) -> su->varibright_level=%u",
1321 						level,
1322 						core_power->varibright_prop.varibright_level,
1323 						core_power->varibright_prop.varibright_hw_level,
1324 						*stream_update->abm_level);
1325 	return true;
1326 }
1327 
1328 bool mod_power_varibright_set_hw_level(struct mod_power *mod_power, unsigned int level,
1329 		struct dc_stream_update *stream_update)
1330 {
1331 	struct core_power *core_power = NULL;
1332 
1333 	if (mod_power == NULL)
1334 		return false;
1335 
1336 	core_power = MOD_POWER_TO_CORE(mod_power);
1337 
1338 	if (level == 0 || level == ABM_LEVEL_IMMEDIATE_DISABLE)
1339 		core_power->varibright_prop.varibright_active = 0;
1340 	else
1341 		core_power->varibright_prop.varibright_active = 1;
1342 	core_power->varibright_prop.varibright_hw_level = level;
1343 	*stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1344 
1345 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1346 						WPP_BIT_FLAG_Backlight_ABM,
1347 						">ABM set level: level=%u -> (varibright_level=%u varibright_hw_level=%u) -> su->varibright_level=%u",
1348 						level,
1349 						core_power->varibright_prop.varibright_level,
1350 						core_power->varibright_prop.varibright_hw_level,
1351 						*stream_update->abm_level);
1352 	return true;
1353 }
1354 
1355 bool mod_power_get_varibright_level(struct mod_power *mod_power,
1356 		unsigned int *varibright_level)
1357 {
1358 	struct core_power *core_power = NULL;
1359 
1360 	if (mod_power == NULL)
1361 		return false;
1362 
1363 	core_power = MOD_POWER_TO_CORE(mod_power);
1364 
1365 	*varibright_level = core_power->varibright_prop.varibright_level;
1366 
1367 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1368 						WPP_BIT_FLAG_Backlight_ABM,
1369 						">get varibright level: cp->varibright_level=%u",
1370 						*varibright_level);
1371 	return true;
1372 
1373 }
1374 
1375 bool mod_power_get_varibright_hw_level(struct mod_power *mod_power,
1376 		unsigned int *varibright_level)
1377 {
1378 	struct core_power *core_power = NULL;
1379 
1380 	if (mod_power == NULL)
1381 		return false;
1382 
1383 	core_power = MOD_POWER_TO_CORE(mod_power);
1384 
1385 	*varibright_level = core_power->varibright_prop.varibright_hw_level;
1386 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1387 						WPP_BIT_FLAG_Backlight_ABM,
1388 						">get varibright HW level: hw_level=%u",
1389 						*varibright_level);
1390 	return true;
1391 }
1392 
1393 bool mod_power_get_varibright_default_level(struct mod_power *mod_power,
1394 		unsigned int *varibright_level)
1395 {
1396 	struct core_power *core_power = NULL;
1397 
1398 	if (mod_power == NULL)
1399 		return false;
1400 
1401 	core_power = MOD_POWER_TO_CORE(mod_power);
1402 
1403 	*varibright_level = core_power->varibright_prop.def_varibright_level;
1404 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1405 						WPP_BIT_FLAG_Backlight_ABM,
1406 						">get varibright default level: def_varibright_level=%u",
1407 						*varibright_level);
1408 	return true;
1409 }
1410 
1411 bool mod_power_get_varibright_enable(struct mod_power *mod_power,
1412 		bool *varibright_enable)
1413 {
1414 	struct core_power *core_power = NULL;
1415 
1416 	if (mod_power == NULL)
1417 		return false;
1418 
1419 	core_power = MOD_POWER_TO_CORE(mod_power);
1420 
1421 	*varibright_enable = core_power->varibright_prop.varibright_user_enable;
1422 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1423 				WPP_BIT_FLAG_Backlight_ABM,
1424 				">get varibright enable state: varibright_user_enable=%u",
1425 				(unsigned int) (*varibright_enable));
1426 	return true;
1427 }
1428 
1429 bool mod_power_is_abm_active(struct mod_power *mod_power,
1430 		const struct dc_link *link,
1431 		unsigned int inst)
1432 {
1433 	unsigned int user_backlight = 0;
1434 	unsigned int current_backlight = 0;
1435 	bool is_active = false;
1436 
1437 	if (mod_power == NULL)
1438 		return false;
1439 
1440 	mod_power_get_backlight_pwm(mod_power, &user_backlight, inst);
1441 	mod_power_get_hw_backlight_pwm(mod_power, link,	&current_backlight);
1442 
1443 	if (user_backlight != current_backlight)
1444 		is_active = true;
1445 	else
1446 		is_active = false;
1447 	DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1448 						WPP_BIT_FLAG_Backlight_ABM,
1449 						">get ABM active state: is_active=%u (user_backlight_pwm=%u, current_backlight_pwm=%u)",
1450 						(unsigned int)is_active,
1451 						user_backlight,
1452 						current_backlight);
1453 	return is_active;
1454 }
1455 
1456 static void fill_backlight_transform_table(struct dmcu_iram_parameters params,
1457 		struct iram_table_v_2 *table)
1458 {
1459 	unsigned int i;
1460 	unsigned int num_entries = NUM_BL_CURVE_SEGS;
1461 	unsigned int lut_index;
1462 
1463 	table->backlight_thresholds[0] = 0;
1464 	ASSERT(params.backlight_lut_array[0] <= 0xFFFF);
1465 	table->backlight_offsets[0] = (uint16_t)params.backlight_lut_array[0];
1466 	table->backlight_thresholds[num_entries-1] = 0xFFFF;
1467 	ASSERT(params.backlight_lut_array[params.backlight_lut_array_size - 1] <= 0xFFFF);
1468 	table->backlight_offsets[num_entries-1] =
1469 		(uint16_t)params.backlight_lut_array[params.backlight_lut_array_size - 1];
1470 
1471 	/* Setup all brightness levels between 0% and 100% exclusive
1472 	 * Fills brightness-to-backlight transform table. Backlight custom curve
1473 	 * describes transform from brightness to backlight. It will be defined
1474 	 * as set of thresholds and set of offsets, together, implying
1475 	 * extrapolation of custom curve into 16 uniformly spanned linear
1476 	 * segments.  Each threshold/offset represented by 16 bit entry in
1477 	 * format U4.10.
1478 	 */
1479 	for (i = 1; i+1 < num_entries; i++) {
1480 		lut_index = (params.backlight_lut_array_size - 1) * i / (num_entries - 1);
1481 
1482 		ASSERT(lut_index < params.backlight_lut_array_size);
1483 
1484 		unsigned int threshold_val = DIV_ROUNDUP((i * 65536), num_entries);
1485 		unsigned int offset_val = params.backlight_lut_array[lut_index];
1486 
1487 		ASSERT(threshold_val <= 0xFFFF);
1488 		ASSERT(offset_val <= 0xFFFF);
1489 
1490 		table->backlight_thresholds[i] = cpu_to_be16((uint16_t)threshold_val);
1491 		table->backlight_offsets[i]    = cpu_to_be16((uint16_t)offset_val);
1492 	}
1493 }
1494 
1495 static void fill_backlight_transform_table_v_2_2(struct dmcu_iram_parameters params,
1496 		struct iram_table_v_2_2 *table, bool big_endian)
1497 {
1498 	unsigned int i;
1499 	unsigned int num_entries = NUM_BL_CURVE_SEGS;
1500 	unsigned int lut_index;
1501 
1502 	table->backlight_thresholds[0] = 0;
1503 	ASSERT(params.backlight_lut_array[0] <= 0xFFFF);
1504 	table->backlight_offsets[0] = (uint16_t)params.backlight_lut_array[0];
1505 	table->backlight_thresholds[num_entries-1] = 0xFFFF;
1506 	ASSERT(params.backlight_lut_array[params.backlight_lut_array_size - 1] <= 0xFFFF);
1507 	table->backlight_offsets[num_entries-1] =
1508 		(uint16_t)params.backlight_lut_array[params.backlight_lut_array_size - 1];
1509 
1510 	/* Setup all brightness levels between 0% and 100% exclusive
1511 	 * Fills brightness-to-backlight transform table. Backlight custom curve
1512 	 * describes transform from brightness to backlight. It will be defined
1513 	 * as set of thresholds and set of offsets, together, implying
1514 	 * extrapolation of custom curve into 16 uniformly spanned linear
1515 	 * segments.  Each threshold/offset represented by 16 bit entry in
1516 	 * format U4.10.
1517 	 */
1518 	for (i = 1; i+1 < num_entries; i++) {
1519 		lut_index = DIV_ROUNDUP((i * params.backlight_lut_array_size), num_entries);
1520 		ASSERT(lut_index < params.backlight_lut_array_size);
1521 
1522 		unsigned int threshold_val = DIV_ROUNDUP((i * 65536), num_entries);
1523 		unsigned int offset_val = params.backlight_lut_array[lut_index];
1524 
1525 		ASSERT(threshold_val <= 0xFFFF);
1526 		ASSERT(offset_val <= 0xFFFF);
1527 
1528 		table->backlight_thresholds[i] = (big_endian) ?
1529 			cpu_to_be16((uint16_t)threshold_val) : cpu_to_le16((uint16_t)threshold_val);
1530 		table->backlight_offsets[i] = (big_endian) ?
1531 			cpu_to_be16((uint16_t)offset_val) : cpu_to_le16((uint16_t)offset_val);
1532 	}
1533 }
1534 
1535 static void fill_iram_v_2(struct iram_table_v_2 *ram_table, struct dmcu_iram_parameters params)
1536 {
1537 	unsigned int set = params.set;
1538 
1539 	ram_table->min_abm_backlight =
1540 			cpu_to_be16(params.min_abm_backlight);
1541 	ram_table->deviation_gain = 0xb3;
1542 
1543 	ram_table->blRampReduction =
1544 		cpu_to_be16(params.backlight_ramping_reduction);
1545 	ram_table->blRampStart =
1546 		cpu_to_be16(params.backlight_ramping_start);
1547 
1548 	ram_table->min_reduction[0][0] = min_reduction_table[abm_config[set][0]];
1549 	ram_table->min_reduction[1][0] = min_reduction_table[abm_config[set][0]];
1550 	ram_table->min_reduction[2][0] = min_reduction_table[abm_config[set][0]];
1551 	ram_table->min_reduction[3][0] = min_reduction_table[abm_config[set][0]];
1552 	ram_table->min_reduction[4][0] = min_reduction_table[abm_config[set][0]];
1553 	ram_table->max_reduction[0][0] = max_reduction_table[abm_config[set][0]];
1554 	ram_table->max_reduction[1][0] = max_reduction_table[abm_config[set][0]];
1555 	ram_table->max_reduction[2][0] = max_reduction_table[abm_config[set][0]];
1556 	ram_table->max_reduction[3][0] = max_reduction_table[abm_config[set][0]];
1557 	ram_table->max_reduction[4][0] = max_reduction_table[abm_config[set][0]];
1558 
1559 	ram_table->min_reduction[0][1] = min_reduction_table[abm_config[set][1]];
1560 	ram_table->min_reduction[1][1] = min_reduction_table[abm_config[set][1]];
1561 	ram_table->min_reduction[2][1] = min_reduction_table[abm_config[set][1]];
1562 	ram_table->min_reduction[3][1] = min_reduction_table[abm_config[set][1]];
1563 	ram_table->min_reduction[4][1] = min_reduction_table[abm_config[set][1]];
1564 	ram_table->max_reduction[0][1] = max_reduction_table[abm_config[set][1]];
1565 	ram_table->max_reduction[1][1] = max_reduction_table[abm_config[set][1]];
1566 	ram_table->max_reduction[2][1] = max_reduction_table[abm_config[set][1]];
1567 	ram_table->max_reduction[3][1] = max_reduction_table[abm_config[set][1]];
1568 	ram_table->max_reduction[4][1] = max_reduction_table[abm_config[set][1]];
1569 
1570 	ram_table->min_reduction[0][2] = min_reduction_table[abm_config[set][2]];
1571 	ram_table->min_reduction[1][2] = min_reduction_table[abm_config[set][2]];
1572 	ram_table->min_reduction[2][2] = min_reduction_table[abm_config[set][2]];
1573 	ram_table->min_reduction[3][2] = min_reduction_table[abm_config[set][2]];
1574 	ram_table->min_reduction[4][2] = min_reduction_table[abm_config[set][2]];
1575 	ram_table->max_reduction[0][2] = max_reduction_table[abm_config[set][2]];
1576 	ram_table->max_reduction[1][2] = max_reduction_table[abm_config[set][2]];
1577 	ram_table->max_reduction[2][2] = max_reduction_table[abm_config[set][2]];
1578 	ram_table->max_reduction[3][2] = max_reduction_table[abm_config[set][2]];
1579 	ram_table->max_reduction[4][2] = max_reduction_table[abm_config[set][2]];
1580 
1581 	ram_table->min_reduction[0][3] = min_reduction_table[abm_config[set][3]];
1582 	ram_table->min_reduction[1][3] = min_reduction_table[abm_config[set][3]];
1583 	ram_table->min_reduction[2][3] = min_reduction_table[abm_config[set][3]];
1584 	ram_table->min_reduction[3][3] = min_reduction_table[abm_config[set][3]];
1585 	ram_table->min_reduction[4][3] = min_reduction_table[abm_config[set][3]];
1586 	ram_table->max_reduction[0][3] = max_reduction_table[abm_config[set][3]];
1587 	ram_table->max_reduction[1][3] = max_reduction_table[abm_config[set][3]];
1588 	ram_table->max_reduction[2][3] = max_reduction_table[abm_config[set][3]];
1589 	ram_table->max_reduction[3][3] = max_reduction_table[abm_config[set][3]];
1590 	ram_table->max_reduction[4][3] = max_reduction_table[abm_config[set][3]];
1591 
1592 	ram_table->bright_pos_gain[0][0] = 0x20;
1593 	ram_table->bright_pos_gain[0][1] = 0x20;
1594 	ram_table->bright_pos_gain[0][2] = 0x20;
1595 	ram_table->bright_pos_gain[0][3] = 0x20;
1596 	ram_table->bright_pos_gain[1][0] = 0x20;
1597 	ram_table->bright_pos_gain[1][1] = 0x20;
1598 	ram_table->bright_pos_gain[1][2] = 0x20;
1599 	ram_table->bright_pos_gain[1][3] = 0x20;
1600 	ram_table->bright_pos_gain[2][0] = 0x20;
1601 	ram_table->bright_pos_gain[2][1] = 0x20;
1602 	ram_table->bright_pos_gain[2][2] = 0x20;
1603 	ram_table->bright_pos_gain[2][3] = 0x20;
1604 	ram_table->bright_pos_gain[3][0] = 0x20;
1605 	ram_table->bright_pos_gain[3][1] = 0x20;
1606 	ram_table->bright_pos_gain[3][2] = 0x20;
1607 	ram_table->bright_pos_gain[3][3] = 0x20;
1608 	ram_table->bright_pos_gain[4][0] = 0x20;
1609 	ram_table->bright_pos_gain[4][1] = 0x20;
1610 	ram_table->bright_pos_gain[4][2] = 0x20;
1611 	ram_table->bright_pos_gain[4][3] = 0x20;
1612 	ram_table->bright_neg_gain[0][0] = 0x00;
1613 	ram_table->bright_neg_gain[0][1] = 0x00;
1614 	ram_table->bright_neg_gain[0][2] = 0x00;
1615 	ram_table->bright_neg_gain[0][3] = 0x00;
1616 	ram_table->bright_neg_gain[1][0] = 0x00;
1617 	ram_table->bright_neg_gain[1][1] = 0x00;
1618 	ram_table->bright_neg_gain[1][2] = 0x00;
1619 	ram_table->bright_neg_gain[1][3] = 0x00;
1620 	ram_table->bright_neg_gain[2][0] = 0x00;
1621 	ram_table->bright_neg_gain[2][1] = 0x00;
1622 	ram_table->bright_neg_gain[2][2] = 0x00;
1623 	ram_table->bright_neg_gain[2][3] = 0x00;
1624 	ram_table->bright_neg_gain[3][0] = 0x00;
1625 	ram_table->bright_neg_gain[3][1] = 0x00;
1626 	ram_table->bright_neg_gain[3][2] = 0x00;
1627 	ram_table->bright_neg_gain[3][3] = 0x00;
1628 	ram_table->bright_neg_gain[4][0] = 0x00;
1629 	ram_table->bright_neg_gain[4][1] = 0x00;
1630 	ram_table->bright_neg_gain[4][2] = 0x00;
1631 	ram_table->bright_neg_gain[4][3] = 0x00;
1632 	ram_table->dark_pos_gain[0][0] = 0x00;
1633 	ram_table->dark_pos_gain[0][1] = 0x00;
1634 	ram_table->dark_pos_gain[0][2] = 0x00;
1635 	ram_table->dark_pos_gain[0][3] = 0x00;
1636 	ram_table->dark_pos_gain[1][0] = 0x00;
1637 	ram_table->dark_pos_gain[1][1] = 0x00;
1638 	ram_table->dark_pos_gain[1][2] = 0x00;
1639 	ram_table->dark_pos_gain[1][3] = 0x00;
1640 	ram_table->dark_pos_gain[2][0] = 0x00;
1641 	ram_table->dark_pos_gain[2][1] = 0x00;
1642 	ram_table->dark_pos_gain[2][2] = 0x00;
1643 	ram_table->dark_pos_gain[2][3] = 0x00;
1644 	ram_table->dark_pos_gain[3][0] = 0x00;
1645 	ram_table->dark_pos_gain[3][1] = 0x00;
1646 	ram_table->dark_pos_gain[3][2] = 0x00;
1647 	ram_table->dark_pos_gain[3][3] = 0x00;
1648 	ram_table->dark_pos_gain[4][0] = 0x00;
1649 	ram_table->dark_pos_gain[4][1] = 0x00;
1650 	ram_table->dark_pos_gain[4][2] = 0x00;
1651 	ram_table->dark_pos_gain[4][3] = 0x00;
1652 	ram_table->dark_neg_gain[0][0] = 0x00;
1653 	ram_table->dark_neg_gain[0][1] = 0x00;
1654 	ram_table->dark_neg_gain[0][2] = 0x00;
1655 	ram_table->dark_neg_gain[0][3] = 0x00;
1656 	ram_table->dark_neg_gain[1][0] = 0x00;
1657 	ram_table->dark_neg_gain[1][1] = 0x00;
1658 	ram_table->dark_neg_gain[1][2] = 0x00;
1659 	ram_table->dark_neg_gain[1][3] = 0x00;
1660 	ram_table->dark_neg_gain[2][0] = 0x00;
1661 	ram_table->dark_neg_gain[2][1] = 0x00;
1662 	ram_table->dark_neg_gain[2][2] = 0x00;
1663 	ram_table->dark_neg_gain[2][3] = 0x00;
1664 	ram_table->dark_neg_gain[3][0] = 0x00;
1665 	ram_table->dark_neg_gain[3][1] = 0x00;
1666 	ram_table->dark_neg_gain[3][2] = 0x00;
1667 	ram_table->dark_neg_gain[3][3] = 0x00;
1668 	ram_table->dark_neg_gain[4][0] = 0x00;
1669 	ram_table->dark_neg_gain[4][1] = 0x00;
1670 	ram_table->dark_neg_gain[4][2] = 0x00;
1671 	ram_table->dark_neg_gain[4][3] = 0x00;
1672 
1673 	ram_table->iir_curve[0] = 0x65;
1674 	ram_table->iir_curve[1] = 0x65;
1675 	ram_table->iir_curve[2] = 0x65;
1676 	ram_table->iir_curve[3] = 0x65;
1677 	ram_table->iir_curve[4] = 0x65;
1678 
1679 	//Gamma 2.4
1680 	ram_table->crgb_thresh[0] = cpu_to_be16(0x13b6);
1681 	ram_table->crgb_thresh[1] = cpu_to_be16(0x1648);
1682 	ram_table->crgb_thresh[2] = cpu_to_be16(0x18e3);
1683 	ram_table->crgb_thresh[3] = cpu_to_be16(0x1b41);
1684 	ram_table->crgb_thresh[4] = cpu_to_be16(0x1d46);
1685 	ram_table->crgb_thresh[5] = cpu_to_be16(0x1f21);
1686 	ram_table->crgb_thresh[6] = cpu_to_be16(0x2167);
1687 	ram_table->crgb_thresh[7] = cpu_to_be16(0x2384);
1688 	ram_table->crgb_offset[0] = cpu_to_be16(0x2999);
1689 	ram_table->crgb_offset[1] = cpu_to_be16(0x3999);
1690 	ram_table->crgb_offset[2] = cpu_to_be16(0x4666);
1691 	ram_table->crgb_offset[3] = cpu_to_be16(0x5999);
1692 	ram_table->crgb_offset[4] = cpu_to_be16(0x6333);
1693 	ram_table->crgb_offset[5] = cpu_to_be16(0x7800);
1694 	ram_table->crgb_offset[6] = cpu_to_be16(0x8c00);
1695 	ram_table->crgb_offset[7] = cpu_to_be16(0xa000);
1696 	ram_table->crgb_slope[0]  = cpu_to_be16(0x3147);
1697 	ram_table->crgb_slope[1]  = cpu_to_be16(0x2978);
1698 	ram_table->crgb_slope[2]  = cpu_to_be16(0x23a2);
1699 	ram_table->crgb_slope[3]  = cpu_to_be16(0x1f55);
1700 	ram_table->crgb_slope[4]  = cpu_to_be16(0x1c63);
1701 	ram_table->crgb_slope[5]  = cpu_to_be16(0x1a0f);
1702 	ram_table->crgb_slope[6]  = cpu_to_be16(0x178d);
1703 	ram_table->crgb_slope[7]  = cpu_to_be16(0x15ab);
1704 
1705 	fill_backlight_transform_table(
1706 			params, ram_table);
1707 }
1708 
1709 static void fill_iram_v_2_2(struct iram_table_v_2_2 *ram_table, struct dmcu_iram_parameters params)
1710 {
1711 	unsigned int set = params.set;
1712 
1713 	ram_table->flags = 0x0;
1714 
1715 	ram_table->min_abm_backlight =
1716 			cpu_to_be16(params.min_abm_backlight);
1717 
1718 	ram_table->deviation_gain[0] = 0xb3;
1719 	ram_table->deviation_gain[1] = 0xa8;
1720 	ram_table->deviation_gain[2] = 0x98;
1721 	ram_table->deviation_gain[3] = 0x68;
1722 
1723 	ram_table->min_reduction[0][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1724 	ram_table->min_reduction[1][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1725 	ram_table->min_reduction[2][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1726 	ram_table->min_reduction[3][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1727 	ram_table->min_reduction[4][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1728 	ram_table->max_reduction[0][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1729 	ram_table->max_reduction[1][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1730 	ram_table->max_reduction[2][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1731 	ram_table->max_reduction[3][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1732 	ram_table->max_reduction[4][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1733 
1734 	ram_table->min_reduction[0][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1735 	ram_table->min_reduction[1][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1736 	ram_table->min_reduction[2][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1737 	ram_table->min_reduction[3][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1738 	ram_table->min_reduction[4][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1739 	ram_table->max_reduction[0][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1740 	ram_table->max_reduction[1][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1741 	ram_table->max_reduction[2][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1742 	ram_table->max_reduction[3][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1743 	ram_table->max_reduction[4][1] = max_reduction_table_v_2_2[abm_config[set][1]];
1744 
1745 	ram_table->min_reduction[0][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1746 	ram_table->min_reduction[1][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1747 	ram_table->min_reduction[2][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1748 	ram_table->min_reduction[3][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1749 	ram_table->min_reduction[4][2] = min_reduction_table_v_2_2[abm_config[set][2]];
1750 	ram_table->max_reduction[0][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1751 	ram_table->max_reduction[1][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1752 	ram_table->max_reduction[2][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1753 	ram_table->max_reduction[3][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1754 	ram_table->max_reduction[4][2] = max_reduction_table_v_2_2[abm_config[set][2]];
1755 
1756 	ram_table->min_reduction[0][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1757 	ram_table->min_reduction[1][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1758 	ram_table->min_reduction[2][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1759 	ram_table->min_reduction[3][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1760 	ram_table->min_reduction[4][3] = min_reduction_table_v_2_2[abm_config[set][3]];
1761 	ram_table->max_reduction[0][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1762 	ram_table->max_reduction[1][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1763 	ram_table->max_reduction[2][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1764 	ram_table->max_reduction[3][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1765 	ram_table->max_reduction[4][3] = max_reduction_table_v_2_2[abm_config[set][3]];
1766 
1767 	ram_table->bright_pos_gain[0][0] = 0x20;
1768 	ram_table->bright_pos_gain[0][1] = 0x20;
1769 	ram_table->bright_pos_gain[0][2] = 0x20;
1770 	ram_table->bright_pos_gain[0][3] = 0x20;
1771 	ram_table->bright_pos_gain[1][0] = 0x20;
1772 	ram_table->bright_pos_gain[1][1] = 0x20;
1773 	ram_table->bright_pos_gain[1][2] = 0x20;
1774 	ram_table->bright_pos_gain[1][3] = 0x20;
1775 	ram_table->bright_pos_gain[2][0] = 0x20;
1776 	ram_table->bright_pos_gain[2][1] = 0x20;
1777 	ram_table->bright_pos_gain[2][2] = 0x20;
1778 	ram_table->bright_pos_gain[2][3] = 0x20;
1779 	ram_table->bright_pos_gain[3][0] = 0x20;
1780 	ram_table->bright_pos_gain[3][1] = 0x20;
1781 	ram_table->bright_pos_gain[3][2] = 0x20;
1782 	ram_table->bright_pos_gain[3][3] = 0x20;
1783 	ram_table->bright_pos_gain[4][0] = 0x20;
1784 	ram_table->bright_pos_gain[4][1] = 0x20;
1785 	ram_table->bright_pos_gain[4][2] = 0x20;
1786 	ram_table->bright_pos_gain[4][3] = 0x20;
1787 
1788 	ram_table->dark_pos_gain[0][0] = 0x00;
1789 	ram_table->dark_pos_gain[0][1] = 0x00;
1790 	ram_table->dark_pos_gain[0][2] = 0x00;
1791 	ram_table->dark_pos_gain[0][3] = 0x00;
1792 	ram_table->dark_pos_gain[1][0] = 0x00;
1793 	ram_table->dark_pos_gain[1][1] = 0x00;
1794 	ram_table->dark_pos_gain[1][2] = 0x00;
1795 	ram_table->dark_pos_gain[1][3] = 0x00;
1796 	ram_table->dark_pos_gain[2][0] = 0x00;
1797 	ram_table->dark_pos_gain[2][1] = 0x00;
1798 	ram_table->dark_pos_gain[2][2] = 0x00;
1799 	ram_table->dark_pos_gain[2][3] = 0x00;
1800 	ram_table->dark_pos_gain[3][0] = 0x00;
1801 	ram_table->dark_pos_gain[3][1] = 0x00;
1802 	ram_table->dark_pos_gain[3][2] = 0x00;
1803 	ram_table->dark_pos_gain[3][3] = 0x00;
1804 	ram_table->dark_pos_gain[4][0] = 0x00;
1805 	ram_table->dark_pos_gain[4][1] = 0x00;
1806 	ram_table->dark_pos_gain[4][2] = 0x00;
1807 	ram_table->dark_pos_gain[4][3] = 0x00;
1808 
1809 	ram_table->hybrid_factor[0] = 0xff;
1810 	ram_table->hybrid_factor[1] = 0xff;
1811 	ram_table->hybrid_factor[2] = 0xff;
1812 	ram_table->hybrid_factor[3] = 0xc0;
1813 
1814 	ram_table->contrast_factor[0] = 0x99;
1815 	ram_table->contrast_factor[1] = 0x99;
1816 	ram_table->contrast_factor[2] = 0x90;
1817 	ram_table->contrast_factor[3] = 0x80;
1818 
1819 	ram_table->iir_curve[0] = 0x65;
1820 	ram_table->iir_curve[1] = 0x65;
1821 	ram_table->iir_curve[2] = 0x65;
1822 	ram_table->iir_curve[3] = 0x65;
1823 	ram_table->iir_curve[4] = 0x65;
1824 
1825 	//Gamma 2.2
1826 	ram_table->crgb_thresh[0] = cpu_to_be16(0x127c);
1827 	ram_table->crgb_thresh[1] = cpu_to_be16(0x151b);
1828 	ram_table->crgb_thresh[2] = cpu_to_be16(0x17d5);
1829 	ram_table->crgb_thresh[3] = cpu_to_be16(0x1a56);
1830 	ram_table->crgb_thresh[4] = cpu_to_be16(0x1c83);
1831 	ram_table->crgb_thresh[5] = cpu_to_be16(0x1e72);
1832 	ram_table->crgb_thresh[6] = cpu_to_be16(0x20f0);
1833 	ram_table->crgb_thresh[7] = cpu_to_be16(0x232b);
1834 	ram_table->crgb_offset[0] = cpu_to_be16(0x2999);
1835 	ram_table->crgb_offset[1] = cpu_to_be16(0x3999);
1836 	ram_table->crgb_offset[2] = cpu_to_be16(0x4666);
1837 	ram_table->crgb_offset[3] = cpu_to_be16(0x5999);
1838 	ram_table->crgb_offset[4] = cpu_to_be16(0x6333);
1839 	ram_table->crgb_offset[5] = cpu_to_be16(0x7800);
1840 	ram_table->crgb_offset[6] = cpu_to_be16(0x8c00);
1841 	ram_table->crgb_offset[7] = cpu_to_be16(0xa000);
1842 	ram_table->crgb_slope[0]  = cpu_to_be16(0x3609);
1843 	ram_table->crgb_slope[1]  = cpu_to_be16(0x2dfa);
1844 	ram_table->crgb_slope[2]  = cpu_to_be16(0x27ea);
1845 	ram_table->crgb_slope[3]  = cpu_to_be16(0x235d);
1846 	ram_table->crgb_slope[4]  = cpu_to_be16(0x2042);
1847 	ram_table->crgb_slope[5]  = cpu_to_be16(0x1dc3);
1848 	ram_table->crgb_slope[6]  = cpu_to_be16(0x1b1a);
1849 	ram_table->crgb_slope[7]  = cpu_to_be16(0x1910);
1850 
1851 	fill_backlight_transform_table_v_2_2(
1852 			params, ram_table, true);
1853 }
1854 
1855 static void fill_iram_v_2_3(struct iram_table_v_2_2 *ram_table, struct dmcu_iram_parameters params, bool big_endian)
1856 {
1857 	unsigned int i, j;
1858 	unsigned int set = params.set;
1859 
1860 	ram_table->flags = 0x0;
1861 	ram_table->min_abm_backlight = (uint16_t)((big_endian) ?
1862 		cpu_to_be16(params.min_abm_backlight) :
1863 		cpu_to_le16(params.min_abm_backlight));
1864 
1865 	for (i = 0; i < NUM_AGGR_LEVEL; i++) {
1866 		ram_table->hybrid_factor[i] = (uint8_t)abm_settings[set][i].brightness_gain;
1867 		ram_table->contrast_factor[i] = abm_settings[set][i].contrast_factor;
1868 		ram_table->deviation_gain[i] = abm_settings[set][i].deviation_gain;
1869 		ram_table->min_knee[i] = abm_settings[set][i].min_knee;
1870 		ram_table->max_knee[i] = abm_settings[set][i].max_knee;
1871 
1872 		for (j = 0; j < NUM_AMBI_LEVEL; j++) {
1873 			ram_table->min_reduction[j][i] = abm_settings[set][i].min_reduction;
1874 			ram_table->max_reduction[j][i] = abm_settings[set][i].max_reduction;
1875 			ram_table->bright_pos_gain[j][i] = abm_settings[set][i].bright_pos_gain;
1876 			ram_table->dark_pos_gain[j][i] = abm_settings[set][i].dark_pos_gain;
1877 		}
1878 	}
1879 
1880 	ram_table->iir_curve[0] = 0x65;
1881 	ram_table->iir_curve[1] = 0x65;
1882 	ram_table->iir_curve[2] = 0x65;
1883 	ram_table->iir_curve[3] = 0x65;
1884 	ram_table->iir_curve[4] = 0x65;
1885 
1886 	//Gamma 2.2
1887 	ram_table->crgb_thresh[0] = bswap16_based_on_endian(big_endian, 0x127c);
1888 	ram_table->crgb_thresh[1] = bswap16_based_on_endian(big_endian, 0x151b);
1889 	ram_table->crgb_thresh[2] = bswap16_based_on_endian(big_endian, 0x17d5);
1890 	ram_table->crgb_thresh[3] = bswap16_based_on_endian(big_endian, 0x1a56);
1891 	ram_table->crgb_thresh[4] = bswap16_based_on_endian(big_endian, 0x1c83);
1892 	ram_table->crgb_thresh[5] = bswap16_based_on_endian(big_endian, 0x1e72);
1893 	ram_table->crgb_thresh[6] = bswap16_based_on_endian(big_endian, 0x20f0);
1894 	ram_table->crgb_thresh[7] = bswap16_based_on_endian(big_endian, 0x232b);
1895 	ram_table->crgb_offset[0] = bswap16_based_on_endian(big_endian, 0x2999);
1896 	ram_table->crgb_offset[1] = bswap16_based_on_endian(big_endian, 0x3999);
1897 	ram_table->crgb_offset[2] = bswap16_based_on_endian(big_endian, 0x4666);
1898 	ram_table->crgb_offset[3] = bswap16_based_on_endian(big_endian, 0x5999);
1899 	ram_table->crgb_offset[4] = bswap16_based_on_endian(big_endian, 0x6333);
1900 	ram_table->crgb_offset[5] = bswap16_based_on_endian(big_endian, 0x7800);
1901 	ram_table->crgb_offset[6] = bswap16_based_on_endian(big_endian, 0x8c00);
1902 	ram_table->crgb_offset[7] = bswap16_based_on_endian(big_endian, 0xa000);
1903 	ram_table->crgb_slope[0]  = bswap16_based_on_endian(big_endian, 0x3609);
1904 	ram_table->crgb_slope[1]  = bswap16_based_on_endian(big_endian, 0x2dfa);
1905 	ram_table->crgb_slope[2]  = bswap16_based_on_endian(big_endian, 0x27ea);
1906 	ram_table->crgb_slope[3]  = bswap16_based_on_endian(big_endian, 0x235d);
1907 	ram_table->crgb_slope[4]  = bswap16_based_on_endian(big_endian, 0x2042);
1908 	ram_table->crgb_slope[5]  = bswap16_based_on_endian(big_endian, 0x1dc3);
1909 	ram_table->crgb_slope[6]  = bswap16_based_on_endian(big_endian, 0x1b1a);
1910 	ram_table->crgb_slope[7]  = bswap16_based_on_endian(big_endian, 0x1910);
1911 
1912 	fill_backlight_transform_table_v_2_2(
1913 			params, ram_table, big_endian);
1914 }
1915 
1916 bool dmub_init_abm_config(struct resource_pool *res_pool,
1917 	struct dmcu_iram_parameters params,
1918 	unsigned int inst)
1919 {
1920 	struct iram_table_v_2_2 ram_table;
1921 	struct abm_config_table config;
1922 	unsigned int set = params.set;
1923 	bool result = false;
1924 	uint32_t i, j = 0;
1925 
1926 	if (res_pool->abm == NULL && res_pool->multiple_abms[inst] == NULL)
1927 		return false;
1928 
1929 	memset(&ram_table, 0, sizeof(ram_table));
1930 	memset(&config, 0, sizeof(config));
1931 
1932 	fill_iram_v_2_3(&ram_table, params, false);
1933 
1934 	// We must copy to structure that is aligned to 32-bit
1935 	for (i = 0; i < NUM_POWER_FN_SEGS; i++) {
1936 		config.crgb_thresh[i] = ram_table.crgb_thresh[i];
1937 		config.crgb_offset[i] = ram_table.crgb_offset[i];
1938 		config.crgb_slope[i] = ram_table.crgb_slope[i];
1939 	}
1940 
1941 	for (i = 0; i < NUM_BL_CURVE_SEGS; i++) {
1942 		config.backlight_thresholds[i] = ram_table.backlight_thresholds[i];
1943 		config.backlight_offsets[i] = ram_table.backlight_offsets[i];
1944 	}
1945 
1946 	for (i = 0; i < NUM_AMBI_LEVEL; i++)
1947 		config.iir_curve[i] = ram_table.iir_curve[i];
1948 
1949 	for (i = 0; i < NUM_AMBI_LEVEL; i++) {
1950 		for (j = 0; j < NUM_AGGR_LEVEL; j++) {
1951 			config.min_reduction[i][j] = ram_table.min_reduction[i][j];
1952 			config.max_reduction[i][j] = ram_table.max_reduction[i][j];
1953 			config.bright_pos_gain[i][j] = ram_table.bright_pos_gain[i][j];
1954 			config.dark_pos_gain[i][j] = ram_table.dark_pos_gain[i][j];
1955 		}
1956 	}
1957 
1958 	for (i = 0; i < NUM_AGGR_LEVEL; i++) {
1959 		config.hybrid_factor[i] = ram_table.hybrid_factor[i];
1960 		config.contrast_factor[i] = ram_table.contrast_factor[i];
1961 		config.deviation_gain[i] = ram_table.deviation_gain[i];
1962 		config.min_knee[i] = ram_table.min_knee[i];
1963 		config.max_knee[i] = ram_table.max_knee[i];
1964 	}
1965 
1966 	if (params.backlight_ramping_override) {
1967 
1968 		ASSERT(params.backlight_ramping_reduction <= 0xFFFF);
1969 		ASSERT(params.backlight_ramping_start <= 0xFFFF);
1970 		for (i = 0; i < NUM_AGGR_LEVEL; i++) {
1971 			config.blRampReduction[i] = (uint16_t)params.backlight_ramping_reduction;
1972 			config.blRampStart[i]     = (uint16_t)params.backlight_ramping_start;
1973 		}
1974 	} else {
1975 		for (i = 0; i < NUM_AGGR_LEVEL; i++) {
1976 			config.blRampReduction[i] = abm_settings[set][i].blRampReduction;
1977 			config.blRampStart[i] = abm_settings[set][i].blRampStart;
1978 		}
1979 	}
1980 
1981 	config.min_abm_backlight = ram_table.min_abm_backlight;
1982 
1983 	if (res_pool->multiple_abms[inst]) {
1984 		result = res_pool->multiple_abms[inst]->funcs->init_abm_config(
1985 			res_pool->multiple_abms[inst], (char *)(&config), sizeof(struct abm_config_table), inst);
1986 	} else
1987 		result = res_pool->abm->funcs->init_abm_config(
1988 			res_pool->abm, (char *)(&config), sizeof(struct abm_config_table), 0);
1989 
1990 	return result;
1991 }
1992 
1993 bool dmcu_load_iram(struct dmcu *dmcu,
1994 	struct dmcu_iram_parameters params)
1995 {
1996 	unsigned char ram_table[IRAM_SIZE];
1997 	bool result = false;
1998 
1999 	if (dmcu == NULL)
2000 		return false;
2001 
2002 	if (dmcu && !dmcu->funcs->is_dmcu_initialized(dmcu))
2003 		return true;
2004 
2005 	memset(&ram_table, 0, sizeof(ram_table));
2006 
2007 	if (dmcu->dmcu_version.abm_version == 0x24) {
2008 		fill_iram_v_2_3((struct iram_table_v_2_2 *)ram_table, params, true);
2009 		result = dmcu->funcs->load_iram(dmcu, 0, (char *)(&ram_table),
2010 						IRAM_RESERVE_AREA_START_V2_2);
2011 	} else if (dmcu->dmcu_version.abm_version == 0x23) {
2012 		fill_iram_v_2_3((struct iram_table_v_2_2 *)ram_table, params, true);
2013 
2014 		result = dmcu->funcs->load_iram(
2015 				dmcu, 0, (char *)(&ram_table), IRAM_RESERVE_AREA_START_V2_2);
2016 	} else if (dmcu->dmcu_version.abm_version == 0x22) {
2017 		fill_iram_v_2_2((struct iram_table_v_2_2 *)ram_table, params);
2018 
2019 		result = dmcu->funcs->load_iram(
2020 				dmcu, 0, (char *)(&ram_table), IRAM_RESERVE_AREA_START_V2_2);
2021 	} else {
2022 		fill_iram_v_2((struct iram_table_v_2 *)ram_table, params);
2023 
2024 		result = dmcu->funcs->load_iram(
2025 				dmcu, 0, (char *)(&ram_table), IRAM_RESERVE_AREA_START_V2);
2026 
2027 		if (result)
2028 			result = dmcu->funcs->load_iram(
2029 					dmcu, IRAM_RESERVE_AREA_END_V2 + 1,
2030 					(char *)(&ram_table) + IRAM_RESERVE_AREA_END_V2 + 1,
2031 					sizeof(ram_table) - IRAM_RESERVE_AREA_END_V2 - 1);
2032 	}
2033 
2034 	return result;
2035 }
2036 
2037 bool fill_custom_backlight_caps(unsigned int config_no, struct dm_acpi_atif_backlight_caps *caps)
2038 {
2039 	unsigned int data_points_size;
2040 	uint64_t caps_size;
2041 
2042 	if (config_no >= ARRAY_SIZE(custom_backlight_profiles))
2043 		return false;
2044 
2045 	data_points_size = custom_backlight_profiles[config_no].num_data_points
2046 			* sizeof(custom_backlight_profiles[config_no].data_points[0]);
2047 
2048 	caps_size = sizeof(struct dm_acpi_atif_backlight_caps) - sizeof(caps->data_points) + data_points_size;
2049 	ASSERT(caps_size <= 0xFFFF);
2050 	caps->size = (uint16_t)caps_size;
2051 	caps->flags = 0;
2052 	caps->error_code = 0;
2053 	caps->ac_level_percentage = custom_backlight_profiles[config_no].ac_level_percentage;
2054 	caps->dc_level_percentage = custom_backlight_profiles[config_no].dc_level_percentage;
2055 	caps->min_input_signal = custom_backlight_profiles[config_no].min_input_signal;
2056 	caps->max_input_signal = custom_backlight_profiles[config_no].max_input_signal;
2057 	caps->num_data_points = (uint8_t)custom_backlight_profiles[config_no].num_data_points;
2058 	memcpy(caps->data_points, custom_backlight_profiles[config_no].data_points, data_points_size);
2059 	return true;
2060 }
2061