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