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