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