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
backlight_8_to_16(unsigned int backlight_8bit)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 */
mod_power_set_backlight_control_type(struct core_power * core_power,unsigned int inst,enum backlight_control_type backlight_control_type)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 */
is_vesa_abc(struct core_power * core_power,unsigned int inst)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 */
backlight_millipercent_to_millinit_legacy(struct core_power * core_power,unsigned int millipercent,unsigned int inst)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 */
backlight_millipercent_to_millinit(struct core_power * core_power,unsigned int millipercent,unsigned int inst)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 */
backlight_millinit_to_millipercent_legacy(struct core_power * core_power,unsigned int millinit,unsigned int inst)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 */
backlight_millinit_to_millipercent(struct core_power * core_power,unsigned int millinit,unsigned int inst)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
backlight_pwm_to_millipercent_legacy(struct core_power * core_power,unsigned int pwm,unsigned int inst)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 */
backlight_pwm_to_millipercent(struct core_power * core_power,unsigned int pwm,unsigned int inst)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
backlight_pwm_to_millinit_legacy(struct core_power * core_power,unsigned int pwm,unsigned int inst)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 */
backlight_pwm_to_millinit(struct core_power * core_power,unsigned int pwm,unsigned int inst)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
backlight_millipercent_to_pwm_legacy(struct core_power * core_power,unsigned int millipercent,unsigned int inst)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 */
backlight_millipercent_to_pwm(struct core_power * core_power,unsigned int millipercent,unsigned int inst)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 */
backlight_millinit_to_pwm_legacy(struct core_power * core_power,unsigned int millinit,unsigned int inst)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 */
backlight_millinit_to_pwm(struct core_power * core_power,unsigned int millinit,unsigned int inst)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
validate_ext_backlight_caps(struct dm_acpi_atif_backlight_caps * ext_backlight_caps)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. */
initialize_backlight_caps(struct core_power * core_power,unsigned int inst)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_obj(struct dm_acpi_atif_backlight_caps));
709
710 if (ext_backlight_caps == NULL)
711 return;
712
713 /* Retrieve ACPI extended brightness caps */
714 if (dm_query_extended_brightness_caps
715 (dc->ctx, acpi_display_type, ext_backlight_caps)) {
716 custom_curve_present = validate_ext_backlight_caps(ext_backlight_caps);
717 }
718
719 if (core_power->bl_prop[inst].use_custom_backlight_caps &&
720 fill_custom_backlight_caps(
721 core_power->bl_prop[inst].custom_backlight_caps_config_no,
722 ext_backlight_caps)) {
723 custom_curve_present = validate_ext_backlight_caps(ext_backlight_caps);
724 }
725
726 if (custom_curve_present) {
727 unsigned int index = 1;
728 unsigned int num_of_data_points = ext_backlight_caps->num_data_points;
729
730 core_power->bl_prop[inst].ac_backlight_percent =
731 ext_backlight_caps->ac_level_percentage;
732 core_power->bl_prop[inst].dc_backlight_percent =
733 ext_backlight_caps->dc_level_percentage;
734 core_power->bl_prop[inst].backlight_lut[0] =
735 backlight_8_to_16(
736 ext_backlight_caps->min_input_signal);
737 core_power->bl_prop[inst].backlight_lut[num_levels - 1] =
738 backlight_8_to_16(
739 ext_backlight_caps->max_input_signal);
740
741 /* Filling translation table from data points -
742 * between every two provided data points we
743 * lineary interpolate missing values
744 */
745 for (i = 0; i < num_of_data_points; i++) {
746 unsigned int luminance =
747 ext_backlight_caps->data_points[i].luminance;
748 unsigned int signal_level =
749 backlight_8_to_16(
750 ext_backlight_caps->data_points[i].signal_level);
751
752 /* Since luminance is a percentage, scale it by num_levels*/
753 luminance = (luminance * num_levels) / 101;
754
755 /* Lineary interpolate missing values */
756 if (index < luminance) {
757 unsigned int base_value =
758 core_power->bl_prop[inst].backlight_lut[index-1];
759 unsigned int delta_signal =
760 signal_level - base_value;
761 unsigned int delta_luma =
762 luminance - index + 1;
763 unsigned int step = delta_signal;
764
765 for (; index < luminance; index++) {
766 core_power->bl_prop[inst].backlight_lut[index] =
767 base_value + (step / delta_luma);
768 step += delta_signal;
769 }
770 }
771
772 /* Now [index == luminance],
773 * so we can add data point to the translation table
774 */
775 core_power->bl_prop[inst].backlight_lut[index++] = signal_level;
776 }
777
778 /* Complete the final segment of interpolation -
779 * between last datapoint and maximum value
780 */
781 if (index < num_levels - 1) {
782 unsigned int base_value =
783 core_power->bl_prop[inst].backlight_lut[index-1];
784 unsigned int delta_signal =
785 core_power->bl_prop[inst].backlight_lut[num_levels - 1] -
786 base_value;
787 unsigned int delta_luma = num_levels - index;
788 unsigned int step = delta_signal;
789
790 for (; index < num_levels - 1; index++) {
791 core_power->bl_prop[inst].backlight_lut[index] =
792 base_value + (step / delta_luma);
793 step += delta_signal;
794 }
795 }
796 /* Build backlight translation table based on default curve */
797 } else {
798 /* Defines default backlight curve F(x) = A(x*x) + Bx + C.
799 *
800 * Backlight curve should always satisfy:
801 * F(0) = min, F(100) = max,
802 * So polynom coefficients are:
803 * A is 0.0255 - B/100 - min/10000 - (255-max)/10000 =
804 * (max - min)/10000 - B/100
805 * B is adjustable factor to modify the curve.
806 * Bigger B results in less concave curve.
807 * B range is [0..(max-min)/100]
808 * C is backlight minimum
809 */
810 unsigned int backlight_curve_coeff_a_factor =
811 num_levels * num_levels;
812 unsigned int backlight_curve_coeff_b = num_levels;
813 unsigned int delta =
814 core_power->bl_prop[inst].backlight_lut[num_levels - 1] -
815 core_power->bl_prop[inst].backlight_lut[0];
816 unsigned int coeffC = core_power->bl_prop[inst].backlight_lut[0];
817 unsigned int coeffB =
818 (backlight_curve_coeff_b < delta ?
819 backlight_curve_coeff_b : delta);
820 unsigned long long coeffA = delta - coeffB; /* coeffB is B*100 */
821
822 for (i = 1; i < num_levels - 1; i++) {
823 uint64_t lut_val = div_u64(coeffA * i * i, backlight_curve_coeff_a_factor) +
824 div_u64((uint64_t)coeffB * i, backlight_curve_coeff_b) + coeffC;
825
826 ASSERT(lut_val <= 0xFFFFFFFF);
827 core_power->bl_prop[inst].backlight_lut[i] = (unsigned int)lut_val;
828 }
829 }
830
831 if (ext_backlight_caps != NULL)
832 kfree(ext_backlight_caps);
833
834 /* Successfully initialized */
835 core_power->bl_prop[inst].backlight_caps_valid = true;
836 }
837
varibright_set_level(struct core_power * core_power)838 static void varibright_set_level(struct core_power *core_power)
839 {
840 if (!core_power->varibright_prop.varibright_active ||
841 !core_power->varibright_prop.varibright_user_enable)
842 core_power->varibright_prop.varibright_hw_level = 0;
843 else
844 core_power->varibright_prop.varibright_hw_level =
845 core_power->varibright_prop.varibright_level;
846 }
847
mod_power_hw_init_backlight(struct mod_power * mod_power)848 bool mod_power_hw_init_backlight(struct mod_power *mod_power)
849 {
850 struct core_power *core_power = NULL;
851 struct dc *dc = NULL;
852 struct dmcu *dmcu = NULL;
853 struct dmcu_iram_parameters params;
854 unsigned int i;
855
856 if (mod_power == NULL)
857 return false;
858
859 core_power = MOD_POWER_TO_CORE(mod_power);
860 dc = core_power->dc;
861
862 for (i = 0; i < core_power->edp_num; i++) {
863 params.set = core_power->varibright_prop.varibright_config_setting;
864 params.backlight_ramping_override = core_power->bl_prop[i].backlight_ramping_override;
865 params.backlight_ramping_reduction = core_power->bl_prop[i].backlight_ramping_reduction;
866 params.backlight_ramping_start = core_power->bl_prop[i].backlight_ramping_start;
867 params.backlight_lut_array = core_power->bl_prop[i].backlight_lut;
868 params.backlight_lut_array_size = core_power->bl_prop[i].num_backlight_levels;
869 params.min_abm_backlight = core_power->bl_prop[i].min_abm_backlight;
870
871 dmcu = dc->res_pool->dmcu;
872
873 // In the case where abm is implemented on dmcub,
874 // dmcu object will be null.
875 // ABM 2.4 and up are implemented on dmcub.
876 if (dmcu) {
877 //DMCU does not support multiple eDP
878 return dmcu_load_iram(dmcu, params);
879 } else if (dc->ctx->dmub_srv) {
880 if (!dmub_init_abm_config(dc->res_pool, params, i))
881 return false;
882 } else
883 return false;
884 }
885 return true;
886 }
887
mod_power_update_backlight_on_mode_change(struct core_power * core_power,struct dc_link * link,unsigned int panel_inst,uint8_t aux_inst,bool is_hdr)888 void mod_power_update_backlight_on_mode_change(
889 struct core_power *core_power,
890 struct dc_link *link,
891 unsigned int panel_inst,
892 uint8_t aux_inst,
893 bool is_hdr)
894 {
895 struct set_backlight_level_params backlight_level_params = { 0 };
896
897 /* Cache the panel's backlight control type once at mode-change/init
898 * time. It is a stable per-panel property (decided in the OS shim
899 * from panel type + DPCD caps), so the brightness translation
900 * helpers can read it without it being passed on every call.
901 */
902 mod_power_set_backlight_control_type(core_power, panel_inst,
903 link->backlight_control_type);
904
905 if ((link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 1 ||
906 link->dpcd_sink_ext_caps.bits.sdr_aux_backlight_control == 1) &&
907 link->backlight_control_type == BACKLIGHT_CONTROL_AMD_AUX)
908 dc_link_set_backlight_level_nits(link, core_power->bl_state[panel_inst].isHDR,
909 core_power->bl_state[panel_inst].backlight_millinit, 0);
910
911 backlight_level_params.frame_ramp = 0;
912
913 fill_backlight_level_params(core_power, &backlight_level_params, panel_inst, aux_inst,
914 core_power->bl_state[panel_inst].backlight_pwm, link->backlight_control_type,
915 core_power->bl_state[panel_inst].backlight_millinit, 0, is_hdr);
916
917 dc_link_set_backlight_level(link, &backlight_level_params);
918 }
919
set_backlight_millinits_aux(struct core_power * core_power,struct dc_stream_state * stream,unsigned int backlight_millinits,unsigned int transition_time_millisec,unsigned int inst)920 static bool set_backlight_millinits_aux(struct core_power *core_power,
921 struct dc_stream_state *stream,
922 unsigned int backlight_millinits,
923 unsigned int transition_time_millisec,
924 unsigned int inst)
925 {
926 struct dc_link *link = NULL;
927
928 if (core_power == NULL)
929 return false;
930
931 if (stream == NULL)
932 return true;
933
934 link = dc_stream_get_link(stream);
935
936 // only use internal backlight control if dmub capabilities are not present
937 if (link->backlight_control_type == BACKLIGHT_CONTROL_VESA_AUX &&
938 link->dc->caps.dmub_caps.aux_backlight_support)
939 return true;
940
941 return dc_link_set_backlight_level_nits(link, core_power->bl_state[inst].isHDR,
942 backlight_millinits, transition_time_millisec);
943 }
944
set_backlight(struct core_power * core_power,struct dc_stream_state * stream,struct set_backlight_level_params * backlight_level_params,unsigned int inst)945 static bool set_backlight(struct core_power *core_power,
946 struct dc_stream_state *stream,
947 struct set_backlight_level_params *backlight_level_params,
948 unsigned int inst)
949 {
950 bool retv = false;
951 unsigned int frame_ramp = 0;
952 unsigned int vsync_rate_hz;
953 union dmcu_abm_set_bl_params params;
954 const struct dc_link *link = NULL;
955 unsigned int backlight_pwm_u16_16 = backlight_level_params->backlight_pwm_u16_16;
956 unsigned int transition_time_millisec = backlight_level_params->transition_time_in_ms;
957
958 if (core_power == NULL)
959 return false;
960
961 core_power->bl_state[inst].backlight_pwm = backlight_pwm_u16_16;
962
963 if (stream == NULL)
964 return true;
965
966 if (stream->link->connector_signal != SIGNAL_TYPE_EDP)
967 return false;
968
969 if (transition_time_millisec != 0) {
970 unsigned int v_total =
971 (stream->adjust.v_total_max == 0) ? stream->timing.v_total : stream->adjust.v_total_max;
972
973 vsync_rate_hz = (unsigned int)div_u64(div_u64((stream->
974 timing.pix_clk_100hz * 100),
975 v_total),
976 stream->timing.h_total);
977
978 if (core_power->bl_state[inst].smooth_brightness_enabled)
979 frame_ramp = ((vsync_rate_hz *
980 transition_time_millisec) + 500) / 1000;
981 }
982
983 core_power->bl_state[inst].frame_ramp = frame_ramp;
984 params.u32All = 0;
985 params.bits.gradual_change = (frame_ramp > 0);
986 params.bits.frame_ramp = frame_ramp;
987 link = dc_stream_get_link(stream);
988
989 mod_power_set_psr_event(&core_power->mod_public, stream, true, psr_event_hw_programming, true);
990 mod_power_set_replay_event(&core_power->mod_public, stream, true, replay_event_hw_programming, true);
991
992 backlight_level_params->frame_ramp = params.u32All;
993 retv = dc_link_set_backlight_level(link, backlight_level_params);
994
995 mod_power_set_psr_event(&core_power->mod_public, stream, false, psr_event_hw_programming, false);
996 mod_power_set_replay_event(&core_power->mod_public, stream, false, replay_event_hw_programming, false);
997
998 return retv;
999 }
1000
fill_backlight_level_params(struct core_power * core_power,struct set_backlight_level_params * backlight_level_params,int panel_inst,uint8_t aux_inst,unsigned int backlight_pwm,enum backlight_control_type backlight_control_type,unsigned int backlight_millinit,unsigned int transition_time_millisec,bool is_hdr)1001 void fill_backlight_level_params(struct core_power *core_power,
1002 struct set_backlight_level_params *backlight_level_params,
1003 int panel_inst, uint8_t aux_inst, unsigned int backlight_pwm,
1004 enum backlight_control_type backlight_control_type,
1005 unsigned int backlight_millinit, unsigned int transition_time_millisec,
1006 bool is_hdr)
1007 {
1008 struct pwr_backlight_properties *bl_prop = &core_power->bl_prop[panel_inst];
1009
1010 backlight_level_params->aux_inst = aux_inst;
1011 backlight_level_params->backlight_pwm_u16_16 = backlight_pwm;
1012 backlight_level_params->control_type = backlight_control_type;
1013 backlight_level_params->backlight_millinits = backlight_millinit;
1014 backlight_level_params->transition_time_in_ms = transition_time_millisec;
1015 backlight_level_params->min_luminance = bl_prop->min_brightness_millinits;
1016 backlight_level_params->max_luminance = bl_prop->max_brightness_millinits;
1017 backlight_level_params->min_backlight_pwm = bl_prop->min_backlight_pwm;
1018 backlight_level_params->max_backlight_pwm = bl_prop->max_backlight_pwm;
1019
1020 if (backlight_control_type == BACKLIGHT_CONTROL_AMD_AUX && !is_hdr)
1021 backlight_level_params->control_type = BACKLIGHT_CONTROL_PWM;
1022 }
1023
mod_power_set_backlight_nits(struct mod_power * mod_power,struct dc_stream_state * stream,unsigned int backlight_millinit,unsigned int transition_time_millisec,bool skip_aux,bool is_hdr)1024 bool mod_power_set_backlight_nits(struct mod_power *mod_power,
1025 struct dc_stream_state *stream,
1026 unsigned int backlight_millinit,
1027 unsigned int transition_time_millisec,
1028 bool skip_aux,
1029 bool is_hdr)
1030 {
1031 struct core_power *core_power = NULL;
1032 unsigned int backlight_pwm;
1033 unsigned int panel_inst = 0;
1034 struct set_backlight_level_params backlight_level_params = { 0 };
1035 const struct dc_link *link = NULL;
1036 uint8_t aux_inst = 0;
1037
1038 if (mod_power == NULL)
1039 return false;
1040
1041 core_power = MOD_POWER_TO_CORE(mod_power);
1042 link = dc_stream_get_link(stream);
1043
1044 aux_inst = link->dc->link_srv->get_ddc_aux_inst(link);
1045
1046 if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &panel_inst))
1047 return false;
1048
1049 if (!skip_aux) {
1050 if (!set_backlight_millinits_aux(core_power, stream,
1051 backlight_millinit, transition_time_millisec, panel_inst))
1052 return false;
1053 }
1054 // always send both AUX (above) and PWM (below)
1055 core_power->bl_state[panel_inst].backlight_millinit = backlight_millinit;
1056
1057 core_power->bl_state[panel_inst].backlight_millipercent =
1058 backlight_millinit_to_millipercent(
1059 core_power, backlight_millinit, panel_inst);
1060
1061 backlight_pwm = backlight_millinit_to_pwm(
1062 core_power, backlight_millinit, panel_inst);
1063
1064 fill_backlight_level_params(core_power, &backlight_level_params, panel_inst, aux_inst, backlight_pwm,
1065 link->backlight_control_type, backlight_millinit, transition_time_millisec, is_hdr);
1066
1067 return set_backlight(core_power, stream,
1068 &backlight_level_params, panel_inst);
1069 }
1070
mod_power_backlight_percent_to_nits(struct mod_power * mod_power,struct dc_stream_state * stream,unsigned int backlight_millipercent,unsigned int * backlight_millinit)1071 bool mod_power_backlight_percent_to_nits(struct mod_power *mod_power,
1072 struct dc_stream_state *stream,
1073 unsigned int backlight_millipercent,
1074 unsigned int *backlight_millinit)
1075 {
1076 struct core_power *core_power = NULL;
1077 unsigned int inst = 0;
1078
1079 if (mod_power == NULL)
1080 return false;
1081
1082 core_power = MOD_POWER_TO_CORE(mod_power);
1083
1084 if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
1085 return false;
1086
1087 *backlight_millinit = backlight_millipercent_to_millinit(
1088 core_power, backlight_millipercent, inst);
1089 return true;
1090 }
1091
mod_power_backlight_nits_to_percent(struct mod_power * mod_power,struct dc_stream_state * stream,unsigned int backlight_millinit,unsigned int * backlight_millipercent)1092 bool mod_power_backlight_nits_to_percent(struct mod_power *mod_power,
1093 struct dc_stream_state *stream,
1094 unsigned int backlight_millinit,
1095 unsigned int *backlight_millipercent)
1096 {
1097 struct core_power *core_power = NULL;
1098 unsigned int inst = 0;
1099
1100 if (mod_power == NULL)
1101 return false;
1102
1103 core_power = MOD_POWER_TO_CORE(mod_power);
1104
1105 if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
1106 return false;
1107
1108 *backlight_millipercent = backlight_millinit_to_millipercent(
1109 core_power, backlight_millinit, inst);
1110 return true;
1111 }
1112
mod_power_set_backlight_percent(struct mod_power * mod_power,struct dc_stream_state * stream,unsigned int backlight_millipercent,unsigned int transition_time_millisec,bool is_hdr)1113 bool mod_power_set_backlight_percent(struct mod_power *mod_power,
1114 struct dc_stream_state *stream,
1115 unsigned int backlight_millipercent,
1116 unsigned int transition_time_millisec,
1117 bool is_hdr)
1118 {
1119 struct core_power *core_power = NULL;
1120 struct set_backlight_level_params backlight_level_params = { 0 };
1121 const struct dc_link *link = NULL;
1122 unsigned int backlight_pwm;
1123 unsigned int panel_inst = 0;
1124 uint8_t aux_inst = 0;
1125
1126 if (mod_power == NULL)
1127 return false;
1128
1129 core_power = MOD_POWER_TO_CORE(mod_power);
1130 link = dc_stream_get_link(stream);
1131 aux_inst = link->dc->link_srv->get_ddc_aux_inst(link);
1132
1133 if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &panel_inst))
1134 return false;
1135 core_power->bl_state[panel_inst].backlight_millipercent = backlight_millipercent;
1136
1137 core_power->bl_state[panel_inst].backlight_millinit =
1138 backlight_millipercent_to_millinit(
1139 core_power, backlight_millipercent, panel_inst);
1140
1141 backlight_pwm = backlight_millipercent_to_pwm(
1142 core_power, backlight_millipercent, panel_inst);
1143
1144 fill_backlight_level_params(core_power, &backlight_level_params, panel_inst,
1145 aux_inst, backlight_pwm, link->backlight_control_type,
1146 core_power->bl_state[panel_inst].backlight_millinit, transition_time_millisec, is_hdr);
1147
1148 return set_backlight(core_power, stream,
1149 &backlight_level_params, panel_inst);
1150 }
1151
mod_power_update_backlight(struct mod_power * mod_power,struct dc_stream_state * stream,unsigned int backlight_millipercent)1152 void mod_power_update_backlight(struct mod_power *mod_power,
1153 struct dc_stream_state *stream,
1154 unsigned int backlight_millipercent)
1155 {
1156 struct core_power *core_power = NULL;
1157 unsigned int inst = 0;
1158
1159 if (mod_power == NULL)
1160 return;
1161
1162 core_power = MOD_POWER_TO_CORE(mod_power);
1163
1164 if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
1165 return;
1166 core_power->bl_state[inst].backlight_millipercent = backlight_millipercent;
1167
1168 core_power->bl_state[inst].backlight_millinit =
1169 backlight_millipercent_to_millinit(
1170 core_power, backlight_millipercent, inst);
1171
1172 core_power->bl_state[inst].backlight_pwm = backlight_millipercent_to_pwm(
1173 core_power, backlight_millipercent, inst);
1174 }
1175
mod_power_update_backlight_nits(struct mod_power * mod_power,struct dc_stream_state * stream,unsigned int backlight_millinit)1176 void mod_power_update_backlight_nits(struct mod_power *mod_power,
1177 struct dc_stream_state *stream,
1178 unsigned int backlight_millinit)
1179 {
1180 struct core_power *core_power = NULL;
1181 unsigned int inst = 0;
1182
1183 if (mod_power == NULL)
1184 return;
1185
1186 core_power = MOD_POWER_TO_CORE(mod_power);
1187
1188 if (!dc_get_edp_link_panel_inst(core_power->dc, stream->link, &inst))
1189 return;
1190
1191 core_power->bl_state[inst].backlight_millinit = backlight_millinit;
1192
1193 core_power->bl_state[inst].backlight_millipercent = backlight_millinit_to_millipercent(
1194 core_power, backlight_millinit, inst);
1195 core_power->bl_state[inst].backlight_pwm = backlight_millinit_to_pwm(
1196 core_power, backlight_millinit, inst);
1197 }
1198
mod_power_get_backlight_pwm(struct mod_power * mod_power,unsigned int * backlight_pwm,unsigned int inst)1199 bool mod_power_get_backlight_pwm(struct mod_power *mod_power,
1200 unsigned int *backlight_pwm,
1201 unsigned int inst)
1202 {
1203 struct core_power *core_power = NULL;
1204
1205 if (mod_power == NULL)
1206 return false;
1207
1208 core_power = MOD_POWER_TO_CORE(mod_power);
1209
1210 *backlight_pwm = core_power->bl_state[inst].backlight_pwm;
1211
1212 return true;
1213 }
1214
mod_power_get_backlight_nits(struct mod_power * mod_power,unsigned int * backlight_millinit,unsigned int inst)1215 bool mod_power_get_backlight_nits(struct mod_power *mod_power,
1216 unsigned int *backlight_millinit,
1217 unsigned int inst)
1218 {
1219 struct core_power *core_power = NULL;
1220
1221 if (mod_power == NULL)
1222 return false;
1223
1224 core_power = MOD_POWER_TO_CORE(mod_power);
1225
1226 *backlight_millinit = core_power->bl_state[inst].backlight_millinit;
1227
1228 return true;
1229 }
1230
mod_power_get_backlight_percent(struct mod_power * mod_power,unsigned int * backlight_millipercent,unsigned int inst)1231 bool mod_power_get_backlight_percent(struct mod_power *mod_power,
1232 unsigned int *backlight_millipercent,
1233 unsigned int inst)
1234 {
1235 struct core_power *core_power = NULL;
1236
1237 if (mod_power == NULL)
1238 return false;
1239
1240 core_power = MOD_POWER_TO_CORE(mod_power);
1241
1242 *backlight_millipercent = core_power->bl_state[inst].backlight_millipercent;
1243
1244 return true;
1245 }
1246
mod_power_get_hw_target_backlight_pwm_nits(struct mod_power * mod_power,const struct dc_link * link,unsigned int * backlight_millinit,unsigned int inst)1247 bool mod_power_get_hw_target_backlight_pwm_nits(struct mod_power *mod_power,
1248 const struct dc_link *link,
1249 unsigned int *backlight_millinit,
1250 unsigned int inst)
1251 {
1252 struct core_power *core_power = NULL;
1253 unsigned int backlight_u16_16 = 0;
1254
1255 if (mod_power == NULL)
1256 return false;
1257
1258 core_power = MOD_POWER_TO_CORE(mod_power);
1259
1260 if (mod_power_get_hw_target_backlight_pwm(mod_power, link,
1261 &backlight_u16_16)) {
1262 *backlight_millinit =
1263 backlight_pwm_to_millinit(core_power,
1264 backlight_u16_16, inst);
1265 return true;
1266 }
1267 return false;
1268 }
1269
mod_power_get_hw_target_backlight_pwm_percent(struct mod_power * mod_power,const struct dc_link * link,unsigned int * backlight_millipercent,unsigned int inst)1270 bool mod_power_get_hw_target_backlight_pwm_percent(struct mod_power *mod_power,
1271 const struct dc_link *link,
1272 unsigned int *backlight_millipercent,
1273 unsigned int inst)
1274 {
1275 struct core_power *core_power = NULL;
1276 unsigned int backlight_u16_16 = 0;
1277
1278 if (mod_power == NULL)
1279 return false;
1280
1281 core_power = MOD_POWER_TO_CORE(mod_power);
1282
1283 if (mod_power_get_hw_target_backlight_pwm(mod_power, link,
1284 &backlight_u16_16)) {
1285 *backlight_millipercent =
1286 backlight_pwm_to_millipercent(core_power,
1287 backlight_u16_16, inst);
1288 return true;
1289 }
1290 return false;
1291 }
1292
mod_power_get_hw_target_backlight_pwm(struct mod_power * mod_power,const struct dc_link * link,unsigned int * backlight_u16_16)1293 bool mod_power_get_hw_target_backlight_pwm(struct mod_power *mod_power,
1294 const struct dc_link *link,
1295 unsigned int *backlight_u16_16)
1296 {
1297 if (mod_power == NULL)
1298 return false;
1299
1300 *backlight_u16_16 = dc_link_get_target_backlight_pwm(link);
1301
1302 return true;
1303 }
1304
mod_power_get_hw_backlight_pwm_nits(struct mod_power * mod_power,const struct dc_link * link,unsigned int * backlight_millinit,unsigned int inst)1305 bool mod_power_get_hw_backlight_pwm_nits(struct mod_power *mod_power,
1306 const struct dc_link *link,
1307 unsigned int *backlight_millinit,
1308 unsigned int inst)
1309 {
1310 struct core_power *core_power = NULL;
1311 unsigned int backlight_u16_16 = 0;
1312
1313 if (mod_power == NULL)
1314 return false;
1315
1316 core_power = MOD_POWER_TO_CORE(mod_power);
1317
1318 if (mod_power_get_hw_backlight_pwm(mod_power, link, &backlight_u16_16)) {
1319 *backlight_millinit =
1320 backlight_pwm_to_millinit(core_power,
1321 backlight_u16_16, inst);
1322 return true;
1323 }
1324 return false;
1325 }
1326
mod_power_get_hw_backlight_aux_nits(struct mod_power * mod_power,struct dc_stream_state ** streams,int num_streams,unsigned int * backlight_millinit_avg,unsigned int * backlight_millinit_peak)1327 bool mod_power_get_hw_backlight_aux_nits(struct mod_power *mod_power,
1328 struct dc_stream_state **streams, int num_streams,
1329 unsigned int *backlight_millinit_avg,
1330 unsigned int *backlight_millinit_peak)
1331 {
1332 struct core_power *core_power = NULL;
1333 struct dc_link *link = NULL;
1334 int stream_index;
1335
1336 if (mod_power == NULL)
1337 return false;
1338
1339 core_power = MOD_POWER_TO_CORE(mod_power);
1340
1341 if (core_power == NULL)
1342 return false;
1343
1344 if (num_streams < 1)
1345 return true;
1346
1347 for (stream_index = 0; stream_index < num_streams; stream_index++)
1348 if (streams[stream_index]->link->connector_signal == SIGNAL_TYPE_EDP ||
1349 streams[stream_index]->link->connector_signal == SIGNAL_TYPE_DISPLAY_PORT)
1350 break;
1351
1352 if (stream_index == num_streams)
1353 return false;
1354
1355 link = dc_stream_get_link(streams[stream_index]);
1356 if (link->dpcd_sink_ext_caps.bits.hdr_aux_backlight_control == 0)
1357 return false;
1358
1359 return dc_link_get_backlight_level_nits(link, backlight_millinit_avg,
1360 backlight_millinit_peak);
1361 }
1362
mod_power_get_hw_backlight_pwm_percent(struct mod_power * mod_power,const struct dc_link * link,unsigned int * backlight_millipercent,unsigned int inst)1363 bool mod_power_get_hw_backlight_pwm_percent(struct mod_power *mod_power,
1364 const struct dc_link *link,
1365 unsigned int *backlight_millipercent,
1366 unsigned int inst)
1367 {
1368 struct core_power *core_power = NULL;
1369 unsigned int backlight_u16_16 = 0;
1370
1371 if (mod_power == NULL)
1372 return false;
1373
1374 core_power = MOD_POWER_TO_CORE(mod_power);
1375
1376 if (mod_power_get_hw_backlight_pwm(mod_power, link, &backlight_u16_16)) {
1377 *backlight_millipercent =
1378 backlight_pwm_to_millipercent(core_power,
1379 backlight_u16_16, inst);
1380 return true;
1381 }
1382 return false;
1383 }
1384
mod_power_get_hw_backlight_pwm(struct mod_power * mod_power,const struct dc_link * link,unsigned int * backlight_u16_16)1385 bool mod_power_get_hw_backlight_pwm(struct mod_power *mod_power,
1386 const struct dc_link *link,
1387 unsigned int *backlight_u16_16)
1388 {
1389 if (mod_power == NULL)
1390 return false;
1391
1392 *backlight_u16_16 = dc_link_get_backlight_level(link);
1393
1394 return true;
1395 }
1396
mod_power_get_panel_backlight_boundaries(struct mod_power * mod_power,unsigned int * out_min_backlight,unsigned int * out_max_backlight,unsigned int * out_ac_backlight_percent,unsigned int * out_dc_backlight_percent,unsigned int inst)1397 bool mod_power_get_panel_backlight_boundaries(
1398 struct mod_power *mod_power,
1399 unsigned int *out_min_backlight,
1400 unsigned int *out_max_backlight,
1401 unsigned int *out_ac_backlight_percent,
1402 unsigned int *out_dc_backlight_percent,
1403 unsigned int inst)
1404 {
1405 struct core_power *core_power = NULL;
1406
1407 if (mod_power == NULL)
1408 return false;
1409
1410 core_power = MOD_POWER_TO_CORE(mod_power);
1411
1412 /* If cache was successfully updated,
1413 * copy the values to output structure and return success
1414 */
1415 if (core_power->bl_prop[inst].backlight_caps_valid) {
1416 *out_min_backlight = core_power->bl_prop[inst].backlight_lut[0];
1417 *out_max_backlight =
1418 core_power->bl_prop[inst].backlight_lut[
1419 core_power->bl_prop[inst].num_backlight_levels - 1];
1420 *out_ac_backlight_percent =
1421 core_power->bl_prop[inst].ac_backlight_percent;
1422 *out_dc_backlight_percent =
1423 core_power->bl_prop[inst].dc_backlight_percent;
1424
1425 return true;
1426 }
1427
1428 return false;
1429 }
1430
mod_power_set_smooth_brightness(struct mod_power * mod_power,bool enable_brightness,unsigned int inst)1431 bool mod_power_set_smooth_brightness(struct mod_power *mod_power,
1432 bool enable_brightness,
1433 unsigned int inst)
1434 {
1435 struct core_power *core_power = NULL;
1436
1437 if (mod_power == NULL)
1438 return false;
1439
1440 core_power = MOD_POWER_TO_CORE(mod_power);
1441
1442 core_power->bl_state[inst].smooth_brightness_enabled = enable_brightness;
1443
1444 return true;
1445 }
1446
mod_power_varibright_feature_enable(struct mod_power * mod_power,bool enable,struct dc_stream_update * stream_update)1447 bool mod_power_varibright_feature_enable(struct mod_power *mod_power, bool enable,
1448 struct dc_stream_update *stream_update)
1449 {
1450 struct core_power *core_power = NULL;
1451
1452 if (mod_power == NULL)
1453 return false;
1454
1455 core_power = MOD_POWER_TO_CORE(mod_power);
1456 core_power->varibright_prop.varibright_user_enable = enable;
1457
1458 /* find abm hw level to program, and save in stream update */
1459 varibright_set_level(core_power);
1460 *stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1461
1462 DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1463 WPP_BIT_FLAG_Backlight_ABM,
1464 ">ABM feature enable: enable=%u su->varibright_level=%u varibright_hw_level=%u",
1465 (unsigned int) enable,
1466 *stream_update->abm_level,
1467 core_power->varibright_prop.varibright_hw_level);
1468 return true;
1469 }
1470
mod_power_varibright_activate(struct mod_power * mod_power,bool activate,struct dc_stream_update * stream_update)1471 bool mod_power_varibright_activate(struct mod_power *mod_power,
1472 bool activate,
1473 struct dc_stream_update *stream_update)
1474 {
1475 struct core_power *core_power = NULL;
1476
1477 if (mod_power == NULL)
1478 return false;
1479
1480 core_power = MOD_POWER_TO_CORE(mod_power);
1481 core_power->varibright_prop.varibright_active = activate;
1482
1483 /* find abm hw level to program, and save in stream update */
1484 varibright_set_level(core_power);
1485 *stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1486
1487 DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1488 WPP_BIT_FLAG_Backlight_ABM,
1489 ">ABM activate: activate=%u su->varibright_level=%u",
1490 (unsigned int) activate,
1491 *stream_update->abm_level);
1492 return true;
1493 }
mod_power_varibright_set_level(struct mod_power * mod_power,unsigned int level,struct dc_stream_update * stream_update)1494 bool mod_power_varibright_set_level(struct mod_power *mod_power, unsigned int level,
1495 struct dc_stream_update *stream_update)
1496 {
1497 struct core_power *core_power = NULL;
1498
1499 if (mod_power == NULL)
1500 return false;
1501
1502 core_power = MOD_POWER_TO_CORE(mod_power);
1503 core_power->varibright_prop.varibright_level = level;
1504 core_power->varibright_prop.varibright_hw_level = level;
1505
1506 /* find abm hw level to program, and save in stream update */
1507 varibright_set_level(core_power);
1508 *stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1509
1510 DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1511 WPP_BIT_FLAG_Backlight_ABM,
1512 ">ABM set level: level=%u -> (varibright_level=%u varibright_hw_level=%u) -> su->varibright_level=%u",
1513 level,
1514 core_power->varibright_prop.varibright_level,
1515 core_power->varibright_prop.varibright_hw_level,
1516 *stream_update->abm_level);
1517 return true;
1518 }
1519
mod_power_varibright_set_hw_level(struct mod_power * mod_power,unsigned int level,struct dc_stream_update * stream_update)1520 bool mod_power_varibright_set_hw_level(struct mod_power *mod_power, unsigned int level,
1521 struct dc_stream_update *stream_update)
1522 {
1523 struct core_power *core_power = NULL;
1524
1525 if (mod_power == NULL)
1526 return false;
1527
1528 core_power = MOD_POWER_TO_CORE(mod_power);
1529
1530 if (level == 0 || level == ABM_LEVEL_IMMEDIATE_DISABLE)
1531 core_power->varibright_prop.varibright_active = 0;
1532 else
1533 core_power->varibright_prop.varibright_active = 1;
1534 core_power->varibright_prop.varibright_hw_level = level;
1535 *stream_update->abm_level = core_power->varibright_prop.varibright_hw_level;
1536
1537 DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1538 WPP_BIT_FLAG_Backlight_ABM,
1539 ">ABM set level: level=%u -> (varibright_level=%u varibright_hw_level=%u) -> su->varibright_level=%u",
1540 level,
1541 core_power->varibright_prop.varibright_level,
1542 core_power->varibright_prop.varibright_hw_level,
1543 *stream_update->abm_level);
1544 return true;
1545 }
1546
mod_power_get_varibright_level(struct mod_power * mod_power,unsigned int * varibright_level)1547 bool mod_power_get_varibright_level(struct mod_power *mod_power,
1548 unsigned int *varibright_level)
1549 {
1550 struct core_power *core_power = NULL;
1551
1552 if (mod_power == NULL)
1553 return false;
1554
1555 core_power = MOD_POWER_TO_CORE(mod_power);
1556
1557 *varibright_level = core_power->varibright_prop.varibright_level;
1558
1559 DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1560 WPP_BIT_FLAG_Backlight_ABM,
1561 ">get varibright level: cp->varibright_level=%u",
1562 *varibright_level);
1563 return true;
1564
1565 }
1566
mod_power_get_varibright_hw_level(struct mod_power * mod_power,unsigned int * varibright_level)1567 bool mod_power_get_varibright_hw_level(struct mod_power *mod_power,
1568 unsigned int *varibright_level)
1569 {
1570 struct core_power *core_power = NULL;
1571
1572 if (mod_power == NULL)
1573 return false;
1574
1575 core_power = MOD_POWER_TO_CORE(mod_power);
1576
1577 *varibright_level = core_power->varibright_prop.varibright_hw_level;
1578 DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1579 WPP_BIT_FLAG_Backlight_ABM,
1580 ">get varibright HW level: hw_level=%u",
1581 *varibright_level);
1582 return true;
1583 }
1584
mod_power_get_varibright_default_level(struct mod_power * mod_power,unsigned int * varibright_level)1585 bool mod_power_get_varibright_default_level(struct mod_power *mod_power,
1586 unsigned int *varibright_level)
1587 {
1588 struct core_power *core_power = NULL;
1589
1590 if (mod_power == NULL)
1591 return false;
1592
1593 core_power = MOD_POWER_TO_CORE(mod_power);
1594
1595 *varibright_level = core_power->varibright_prop.def_varibright_level;
1596 DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1597 WPP_BIT_FLAG_Backlight_ABM,
1598 ">get varibright default level: def_varibright_level=%u",
1599 *varibright_level);
1600 return true;
1601 }
1602
mod_power_get_varibright_enable(struct mod_power * mod_power,bool * varibright_enable)1603 bool mod_power_get_varibright_enable(struct mod_power *mod_power,
1604 bool *varibright_enable)
1605 {
1606 struct core_power *core_power = NULL;
1607
1608 if (mod_power == NULL)
1609 return false;
1610
1611 core_power = MOD_POWER_TO_CORE(mod_power);
1612
1613 *varibright_enable = core_power->varibright_prop.varibright_user_enable;
1614 DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1615 WPP_BIT_FLAG_Backlight_ABM,
1616 ">get varibright enable state: varibright_user_enable=%u",
1617 (unsigned int) (*varibright_enable));
1618 return true;
1619 }
1620
mod_power_is_abm_active(struct mod_power * mod_power,const struct dc_link * link,unsigned int inst)1621 bool mod_power_is_abm_active(struct mod_power *mod_power,
1622 const struct dc_link *link,
1623 unsigned int inst)
1624 {
1625 unsigned int user_backlight = 0;
1626 unsigned int current_backlight = 0;
1627 bool is_active = false;
1628
1629 if (mod_power == NULL)
1630 return false;
1631
1632 mod_power_get_backlight_pwm(mod_power, &user_backlight, inst);
1633 mod_power_get_hw_backlight_pwm(mod_power, link, ¤t_backlight);
1634
1635 if (user_backlight != current_backlight)
1636 is_active = true;
1637 else
1638 is_active = false;
1639 DC_TRACE_LEVEL_MESSAGEP(DAL_TRACE_LEVEL_INFORMATION,
1640 WPP_BIT_FLAG_Backlight_ABM,
1641 ">get ABM active state: is_active=%u (user_backlight_pwm=%u, current_backlight_pwm=%u)",
1642 (unsigned int)is_active,
1643 user_backlight,
1644 current_backlight);
1645 return is_active;
1646 }
1647
mod_power_is_abm_supported(struct mod_power * mod_power,unsigned int inst)1648 bool mod_power_is_abm_supported(struct mod_power *mod_power,
1649 unsigned int inst)
1650 {
1651 struct core_power *core_power = NULL;
1652 struct dc *dc = NULL;
1653
1654 if (mod_power == NULL)
1655 return false;
1656
1657 core_power = MOD_POWER_TO_CORE(mod_power);
1658 dc = core_power->dc;
1659
1660 // It's only implemented on dmcub.
1661 if (dc->ctx->dmub_srv) {
1662 if (!dmub_is_abm_supported(dc->res_pool, inst))
1663 return false;
1664 } else
1665 return false;
1666
1667 return true;
1668 }
1669
mod_power_abm_set_event(struct mod_power * mod_power,unsigned int full_screen,unsigned int trans_info,unsigned int hdr_mode,unsigned int scaling_enable,unsigned int scaling_strength_map,unsigned int inst)1670 bool mod_power_abm_set_event(struct mod_power *mod_power,
1671 unsigned int full_screen, unsigned int trans_info,
1672 unsigned int hdr_mode, unsigned int scaling_enable,
1673 unsigned int scaling_strength_map, unsigned int inst)
1674 {
1675 struct core_power *core_power = NULL;
1676 struct dc *dc = NULL;
1677
1678 if (mod_power == NULL)
1679 return false;
1680
1681 core_power = MOD_POWER_TO_CORE(mod_power);
1682 dc = core_power->dc;
1683
1684 // It's only implemented on dmcub.
1685 if (dc->ctx->dmub_srv) {
1686 if (!dmub_set_abm_event(dc->res_pool, full_screen, trans_info,
1687 hdr_mode, scaling_enable, scaling_strength_map, inst))
1688 return false;
1689 } else
1690 return false;
1691
1692 return true;
1693 }
1694
mod_power_abm_set_strength(struct mod_power * mod_power,unsigned int strength,unsigned int inst)1695 bool mod_power_abm_set_strength(struct mod_power *mod_power,
1696 unsigned int strength,
1697 unsigned int inst)
1698 {
1699 struct core_power *core_power = NULL;
1700 struct dc *dc = NULL;
1701
1702 if (mod_power == NULL)
1703 return false;
1704
1705 core_power = MOD_POWER_TO_CORE(mod_power);
1706 dc = core_power->dc;
1707
1708 // It's only implemented on dmcub.
1709 if (dc->ctx->dmub_srv) {
1710 if (!dmub_set_abm_strength(dc->res_pool, strength, inst))
1711 return false;
1712 } else
1713 return false;
1714
1715 return true;
1716 }
1717
fill_backlight_transform_table(struct dmcu_iram_parameters params,struct iram_table_v_2 * table)1718 static void fill_backlight_transform_table(struct dmcu_iram_parameters params,
1719 struct iram_table_v_2 *table)
1720 {
1721 unsigned int i;
1722 unsigned int num_entries = NUM_BL_CURVE_SEGS;
1723 unsigned int lut_index;
1724
1725 table->backlight_thresholds[0] = 0;
1726 ASSERT(params.backlight_lut_array[0] <= 0xFFFF);
1727 table->backlight_offsets[0] = (uint16_t)params.backlight_lut_array[0];
1728 table->backlight_thresholds[num_entries-1] = 0xFFFF;
1729 ASSERT(params.backlight_lut_array[params.backlight_lut_array_size - 1] <= 0xFFFF);
1730 table->backlight_offsets[num_entries-1] =
1731 (uint16_t)params.backlight_lut_array[params.backlight_lut_array_size - 1];
1732
1733 /* Setup all brightness levels between 0% and 100% exclusive
1734 * Fills brightness-to-backlight transform table. Backlight custom curve
1735 * describes transform from brightness to backlight. It will be defined
1736 * as set of thresholds and set of offsets, together, implying
1737 * extrapolation of custom curve into 16 uniformly spanned linear
1738 * segments. Each threshold/offset represented by 16 bit entry in
1739 * format U4.10.
1740 */
1741 for (i = 1; i+1 < num_entries; i++) {
1742 lut_index = (params.backlight_lut_array_size - 1) * i / (num_entries - 1);
1743
1744 ASSERT(lut_index < params.backlight_lut_array_size);
1745
1746 unsigned int threshold_val = DIV_ROUNDUP((i * 65536), num_entries);
1747 unsigned int offset_val = params.backlight_lut_array[lut_index];
1748
1749 ASSERT(threshold_val <= 0xFFFF);
1750 ASSERT(offset_val <= 0xFFFF);
1751
1752 table->backlight_thresholds[i] = cpu_to_be16((uint16_t)threshold_val);
1753 table->backlight_offsets[i] = cpu_to_be16((uint16_t)offset_val);
1754 }
1755 }
1756
fill_backlight_transform_table_v_2_2(struct dmcu_iram_parameters params,struct iram_table_v_2_2 * table,bool big_endian)1757 static void fill_backlight_transform_table_v_2_2(struct dmcu_iram_parameters params,
1758 struct iram_table_v_2_2 *table, bool big_endian)
1759 {
1760 unsigned int i;
1761 unsigned int num_entries = NUM_BL_CURVE_SEGS;
1762 unsigned int lut_index;
1763
1764 table->backlight_thresholds[0] = 0;
1765 ASSERT(params.backlight_lut_array[0] <= 0xFFFF);
1766 table->backlight_offsets[0] = (uint16_t)params.backlight_lut_array[0];
1767 table->backlight_thresholds[num_entries-1] = 0xFFFF;
1768 ASSERT(params.backlight_lut_array[params.backlight_lut_array_size - 1] <= 0xFFFF);
1769 table->backlight_offsets[num_entries-1] =
1770 (uint16_t)params.backlight_lut_array[params.backlight_lut_array_size - 1];
1771
1772 /* Setup all brightness levels between 0% and 100% exclusive
1773 * Fills brightness-to-backlight transform table. Backlight custom curve
1774 * describes transform from brightness to backlight. It will be defined
1775 * as set of thresholds and set of offsets, together, implying
1776 * extrapolation of custom curve into 16 uniformly spanned linear
1777 * segments. Each threshold/offset represented by 16 bit entry in
1778 * format U4.10.
1779 */
1780 for (i = 1; i+1 < num_entries; i++) {
1781 lut_index = DIV_ROUNDUP((i * params.backlight_lut_array_size), num_entries);
1782 ASSERT(lut_index < params.backlight_lut_array_size);
1783
1784 unsigned int threshold_val = DIV_ROUNDUP((i * 65536), num_entries);
1785 unsigned int offset_val = params.backlight_lut_array[lut_index];
1786
1787 ASSERT(threshold_val <= 0xFFFF);
1788 ASSERT(offset_val <= 0xFFFF);
1789
1790 table->backlight_thresholds[i] = (big_endian) ?
1791 cpu_to_be16((uint16_t)threshold_val) : cpu_to_le16((uint16_t)threshold_val);
1792 table->backlight_offsets[i] = (big_endian) ?
1793 cpu_to_be16((uint16_t)offset_val) : cpu_to_le16((uint16_t)offset_val);
1794 }
1795 }
1796
fill_iram_v_2(struct iram_table_v_2 * ram_table,struct dmcu_iram_parameters params)1797 static void fill_iram_v_2(struct iram_table_v_2 *ram_table, struct dmcu_iram_parameters params)
1798 {
1799 unsigned int set = params.set;
1800
1801 ram_table->min_abm_backlight =
1802 cpu_to_be16(params.min_abm_backlight);
1803 ram_table->deviation_gain = 0xb3;
1804
1805 ram_table->blRampReduction =
1806 cpu_to_be16(params.backlight_ramping_reduction);
1807 ram_table->blRampStart =
1808 cpu_to_be16(params.backlight_ramping_start);
1809
1810 ram_table->min_reduction[0][0] = min_reduction_table[abm_config[set][0]];
1811 ram_table->min_reduction[1][0] = min_reduction_table[abm_config[set][0]];
1812 ram_table->min_reduction[2][0] = min_reduction_table[abm_config[set][0]];
1813 ram_table->min_reduction[3][0] = min_reduction_table[abm_config[set][0]];
1814 ram_table->min_reduction[4][0] = min_reduction_table[abm_config[set][0]];
1815 ram_table->max_reduction[0][0] = max_reduction_table[abm_config[set][0]];
1816 ram_table->max_reduction[1][0] = max_reduction_table[abm_config[set][0]];
1817 ram_table->max_reduction[2][0] = max_reduction_table[abm_config[set][0]];
1818 ram_table->max_reduction[3][0] = max_reduction_table[abm_config[set][0]];
1819 ram_table->max_reduction[4][0] = max_reduction_table[abm_config[set][0]];
1820
1821 ram_table->min_reduction[0][1] = min_reduction_table[abm_config[set][1]];
1822 ram_table->min_reduction[1][1] = min_reduction_table[abm_config[set][1]];
1823 ram_table->min_reduction[2][1] = min_reduction_table[abm_config[set][1]];
1824 ram_table->min_reduction[3][1] = min_reduction_table[abm_config[set][1]];
1825 ram_table->min_reduction[4][1] = min_reduction_table[abm_config[set][1]];
1826 ram_table->max_reduction[0][1] = max_reduction_table[abm_config[set][1]];
1827 ram_table->max_reduction[1][1] = max_reduction_table[abm_config[set][1]];
1828 ram_table->max_reduction[2][1] = max_reduction_table[abm_config[set][1]];
1829 ram_table->max_reduction[3][1] = max_reduction_table[abm_config[set][1]];
1830 ram_table->max_reduction[4][1] = max_reduction_table[abm_config[set][1]];
1831
1832 ram_table->min_reduction[0][2] = min_reduction_table[abm_config[set][2]];
1833 ram_table->min_reduction[1][2] = min_reduction_table[abm_config[set][2]];
1834 ram_table->min_reduction[2][2] = min_reduction_table[abm_config[set][2]];
1835 ram_table->min_reduction[3][2] = min_reduction_table[abm_config[set][2]];
1836 ram_table->min_reduction[4][2] = min_reduction_table[abm_config[set][2]];
1837 ram_table->max_reduction[0][2] = max_reduction_table[abm_config[set][2]];
1838 ram_table->max_reduction[1][2] = max_reduction_table[abm_config[set][2]];
1839 ram_table->max_reduction[2][2] = max_reduction_table[abm_config[set][2]];
1840 ram_table->max_reduction[3][2] = max_reduction_table[abm_config[set][2]];
1841 ram_table->max_reduction[4][2] = max_reduction_table[abm_config[set][2]];
1842
1843 ram_table->min_reduction[0][3] = min_reduction_table[abm_config[set][3]];
1844 ram_table->min_reduction[1][3] = min_reduction_table[abm_config[set][3]];
1845 ram_table->min_reduction[2][3] = min_reduction_table[abm_config[set][3]];
1846 ram_table->min_reduction[3][3] = min_reduction_table[abm_config[set][3]];
1847 ram_table->min_reduction[4][3] = min_reduction_table[abm_config[set][3]];
1848 ram_table->max_reduction[0][3] = max_reduction_table[abm_config[set][3]];
1849 ram_table->max_reduction[1][3] = max_reduction_table[abm_config[set][3]];
1850 ram_table->max_reduction[2][3] = max_reduction_table[abm_config[set][3]];
1851 ram_table->max_reduction[3][3] = max_reduction_table[abm_config[set][3]];
1852 ram_table->max_reduction[4][3] = max_reduction_table[abm_config[set][3]];
1853
1854 ram_table->bright_pos_gain[0][0] = 0x20;
1855 ram_table->bright_pos_gain[0][1] = 0x20;
1856 ram_table->bright_pos_gain[0][2] = 0x20;
1857 ram_table->bright_pos_gain[0][3] = 0x20;
1858 ram_table->bright_pos_gain[1][0] = 0x20;
1859 ram_table->bright_pos_gain[1][1] = 0x20;
1860 ram_table->bright_pos_gain[1][2] = 0x20;
1861 ram_table->bright_pos_gain[1][3] = 0x20;
1862 ram_table->bright_pos_gain[2][0] = 0x20;
1863 ram_table->bright_pos_gain[2][1] = 0x20;
1864 ram_table->bright_pos_gain[2][2] = 0x20;
1865 ram_table->bright_pos_gain[2][3] = 0x20;
1866 ram_table->bright_pos_gain[3][0] = 0x20;
1867 ram_table->bright_pos_gain[3][1] = 0x20;
1868 ram_table->bright_pos_gain[3][2] = 0x20;
1869 ram_table->bright_pos_gain[3][3] = 0x20;
1870 ram_table->bright_pos_gain[4][0] = 0x20;
1871 ram_table->bright_pos_gain[4][1] = 0x20;
1872 ram_table->bright_pos_gain[4][2] = 0x20;
1873 ram_table->bright_pos_gain[4][3] = 0x20;
1874 ram_table->bright_neg_gain[0][0] = 0x00;
1875 ram_table->bright_neg_gain[0][1] = 0x00;
1876 ram_table->bright_neg_gain[0][2] = 0x00;
1877 ram_table->bright_neg_gain[0][3] = 0x00;
1878 ram_table->bright_neg_gain[1][0] = 0x00;
1879 ram_table->bright_neg_gain[1][1] = 0x00;
1880 ram_table->bright_neg_gain[1][2] = 0x00;
1881 ram_table->bright_neg_gain[1][3] = 0x00;
1882 ram_table->bright_neg_gain[2][0] = 0x00;
1883 ram_table->bright_neg_gain[2][1] = 0x00;
1884 ram_table->bright_neg_gain[2][2] = 0x00;
1885 ram_table->bright_neg_gain[2][3] = 0x00;
1886 ram_table->bright_neg_gain[3][0] = 0x00;
1887 ram_table->bright_neg_gain[3][1] = 0x00;
1888 ram_table->bright_neg_gain[3][2] = 0x00;
1889 ram_table->bright_neg_gain[3][3] = 0x00;
1890 ram_table->bright_neg_gain[4][0] = 0x00;
1891 ram_table->bright_neg_gain[4][1] = 0x00;
1892 ram_table->bright_neg_gain[4][2] = 0x00;
1893 ram_table->bright_neg_gain[4][3] = 0x00;
1894 ram_table->dark_pos_gain[0][0] = 0x00;
1895 ram_table->dark_pos_gain[0][1] = 0x00;
1896 ram_table->dark_pos_gain[0][2] = 0x00;
1897 ram_table->dark_pos_gain[0][3] = 0x00;
1898 ram_table->dark_pos_gain[1][0] = 0x00;
1899 ram_table->dark_pos_gain[1][1] = 0x00;
1900 ram_table->dark_pos_gain[1][2] = 0x00;
1901 ram_table->dark_pos_gain[1][3] = 0x00;
1902 ram_table->dark_pos_gain[2][0] = 0x00;
1903 ram_table->dark_pos_gain[2][1] = 0x00;
1904 ram_table->dark_pos_gain[2][2] = 0x00;
1905 ram_table->dark_pos_gain[2][3] = 0x00;
1906 ram_table->dark_pos_gain[3][0] = 0x00;
1907 ram_table->dark_pos_gain[3][1] = 0x00;
1908 ram_table->dark_pos_gain[3][2] = 0x00;
1909 ram_table->dark_pos_gain[3][3] = 0x00;
1910 ram_table->dark_pos_gain[4][0] = 0x00;
1911 ram_table->dark_pos_gain[4][1] = 0x00;
1912 ram_table->dark_pos_gain[4][2] = 0x00;
1913 ram_table->dark_pos_gain[4][3] = 0x00;
1914 ram_table->dark_neg_gain[0][0] = 0x00;
1915 ram_table->dark_neg_gain[0][1] = 0x00;
1916 ram_table->dark_neg_gain[0][2] = 0x00;
1917 ram_table->dark_neg_gain[0][3] = 0x00;
1918 ram_table->dark_neg_gain[1][0] = 0x00;
1919 ram_table->dark_neg_gain[1][1] = 0x00;
1920 ram_table->dark_neg_gain[1][2] = 0x00;
1921 ram_table->dark_neg_gain[1][3] = 0x00;
1922 ram_table->dark_neg_gain[2][0] = 0x00;
1923 ram_table->dark_neg_gain[2][1] = 0x00;
1924 ram_table->dark_neg_gain[2][2] = 0x00;
1925 ram_table->dark_neg_gain[2][3] = 0x00;
1926 ram_table->dark_neg_gain[3][0] = 0x00;
1927 ram_table->dark_neg_gain[3][1] = 0x00;
1928 ram_table->dark_neg_gain[3][2] = 0x00;
1929 ram_table->dark_neg_gain[3][3] = 0x00;
1930 ram_table->dark_neg_gain[4][0] = 0x00;
1931 ram_table->dark_neg_gain[4][1] = 0x00;
1932 ram_table->dark_neg_gain[4][2] = 0x00;
1933 ram_table->dark_neg_gain[4][3] = 0x00;
1934
1935 ram_table->iir_curve[0] = 0x65;
1936 ram_table->iir_curve[1] = 0x65;
1937 ram_table->iir_curve[2] = 0x65;
1938 ram_table->iir_curve[3] = 0x65;
1939 ram_table->iir_curve[4] = 0x65;
1940
1941 //Gamma 2.4
1942 ram_table->crgb_thresh[0] = cpu_to_be16(0x13b6);
1943 ram_table->crgb_thresh[1] = cpu_to_be16(0x1648);
1944 ram_table->crgb_thresh[2] = cpu_to_be16(0x18e3);
1945 ram_table->crgb_thresh[3] = cpu_to_be16(0x1b41);
1946 ram_table->crgb_thresh[4] = cpu_to_be16(0x1d46);
1947 ram_table->crgb_thresh[5] = cpu_to_be16(0x1f21);
1948 ram_table->crgb_thresh[6] = cpu_to_be16(0x2167);
1949 ram_table->crgb_thresh[7] = cpu_to_be16(0x2384);
1950 ram_table->crgb_offset[0] = cpu_to_be16(0x2999);
1951 ram_table->crgb_offset[1] = cpu_to_be16(0x3999);
1952 ram_table->crgb_offset[2] = cpu_to_be16(0x4666);
1953 ram_table->crgb_offset[3] = cpu_to_be16(0x5999);
1954 ram_table->crgb_offset[4] = cpu_to_be16(0x6333);
1955 ram_table->crgb_offset[5] = cpu_to_be16(0x7800);
1956 ram_table->crgb_offset[6] = cpu_to_be16(0x8c00);
1957 ram_table->crgb_offset[7] = cpu_to_be16(0xa000);
1958 ram_table->crgb_slope[0] = cpu_to_be16(0x3147);
1959 ram_table->crgb_slope[1] = cpu_to_be16(0x2978);
1960 ram_table->crgb_slope[2] = cpu_to_be16(0x23a2);
1961 ram_table->crgb_slope[3] = cpu_to_be16(0x1f55);
1962 ram_table->crgb_slope[4] = cpu_to_be16(0x1c63);
1963 ram_table->crgb_slope[5] = cpu_to_be16(0x1a0f);
1964 ram_table->crgb_slope[6] = cpu_to_be16(0x178d);
1965 ram_table->crgb_slope[7] = cpu_to_be16(0x15ab);
1966
1967 fill_backlight_transform_table(
1968 params, ram_table);
1969 }
1970
fill_iram_v_2_2(struct iram_table_v_2_2 * ram_table,struct dmcu_iram_parameters params)1971 static void fill_iram_v_2_2(struct iram_table_v_2_2 *ram_table, struct dmcu_iram_parameters params)
1972 {
1973 unsigned int set = params.set;
1974
1975 ram_table->flags = 0x0;
1976
1977 ram_table->min_abm_backlight =
1978 cpu_to_be16(params.min_abm_backlight);
1979
1980 ram_table->deviation_gain[0] = 0xb3;
1981 ram_table->deviation_gain[1] = 0xa8;
1982 ram_table->deviation_gain[2] = 0x98;
1983 ram_table->deviation_gain[3] = 0x68;
1984
1985 ram_table->min_reduction[0][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1986 ram_table->min_reduction[1][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1987 ram_table->min_reduction[2][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1988 ram_table->min_reduction[3][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1989 ram_table->min_reduction[4][0] = min_reduction_table_v_2_2[abm_config[set][0]];
1990 ram_table->max_reduction[0][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1991 ram_table->max_reduction[1][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1992 ram_table->max_reduction[2][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1993 ram_table->max_reduction[3][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1994 ram_table->max_reduction[4][0] = max_reduction_table_v_2_2[abm_config[set][0]];
1995
1996 ram_table->min_reduction[0][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1997 ram_table->min_reduction[1][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1998 ram_table->min_reduction[2][1] = min_reduction_table_v_2_2[abm_config[set][1]];
1999 ram_table->min_reduction[3][1] = min_reduction_table_v_2_2[abm_config[set][1]];
2000 ram_table->min_reduction[4][1] = min_reduction_table_v_2_2[abm_config[set][1]];
2001 ram_table->max_reduction[0][1] = max_reduction_table_v_2_2[abm_config[set][1]];
2002 ram_table->max_reduction[1][1] = max_reduction_table_v_2_2[abm_config[set][1]];
2003 ram_table->max_reduction[2][1] = max_reduction_table_v_2_2[abm_config[set][1]];
2004 ram_table->max_reduction[3][1] = max_reduction_table_v_2_2[abm_config[set][1]];
2005 ram_table->max_reduction[4][1] = max_reduction_table_v_2_2[abm_config[set][1]];
2006
2007 ram_table->min_reduction[0][2] = min_reduction_table_v_2_2[abm_config[set][2]];
2008 ram_table->min_reduction[1][2] = min_reduction_table_v_2_2[abm_config[set][2]];
2009 ram_table->min_reduction[2][2] = min_reduction_table_v_2_2[abm_config[set][2]];
2010 ram_table->min_reduction[3][2] = min_reduction_table_v_2_2[abm_config[set][2]];
2011 ram_table->min_reduction[4][2] = min_reduction_table_v_2_2[abm_config[set][2]];
2012 ram_table->max_reduction[0][2] = max_reduction_table_v_2_2[abm_config[set][2]];
2013 ram_table->max_reduction[1][2] = max_reduction_table_v_2_2[abm_config[set][2]];
2014 ram_table->max_reduction[2][2] = max_reduction_table_v_2_2[abm_config[set][2]];
2015 ram_table->max_reduction[3][2] = max_reduction_table_v_2_2[abm_config[set][2]];
2016 ram_table->max_reduction[4][2] = max_reduction_table_v_2_2[abm_config[set][2]];
2017
2018 ram_table->min_reduction[0][3] = min_reduction_table_v_2_2[abm_config[set][3]];
2019 ram_table->min_reduction[1][3] = min_reduction_table_v_2_2[abm_config[set][3]];
2020 ram_table->min_reduction[2][3] = min_reduction_table_v_2_2[abm_config[set][3]];
2021 ram_table->min_reduction[3][3] = min_reduction_table_v_2_2[abm_config[set][3]];
2022 ram_table->min_reduction[4][3] = min_reduction_table_v_2_2[abm_config[set][3]];
2023 ram_table->max_reduction[0][3] = max_reduction_table_v_2_2[abm_config[set][3]];
2024 ram_table->max_reduction[1][3] = max_reduction_table_v_2_2[abm_config[set][3]];
2025 ram_table->max_reduction[2][3] = max_reduction_table_v_2_2[abm_config[set][3]];
2026 ram_table->max_reduction[3][3] = max_reduction_table_v_2_2[abm_config[set][3]];
2027 ram_table->max_reduction[4][3] = max_reduction_table_v_2_2[abm_config[set][3]];
2028
2029 ram_table->bright_pos_gain[0][0] = 0x20;
2030 ram_table->bright_pos_gain[0][1] = 0x20;
2031 ram_table->bright_pos_gain[0][2] = 0x20;
2032 ram_table->bright_pos_gain[0][3] = 0x20;
2033 ram_table->bright_pos_gain[1][0] = 0x20;
2034 ram_table->bright_pos_gain[1][1] = 0x20;
2035 ram_table->bright_pos_gain[1][2] = 0x20;
2036 ram_table->bright_pos_gain[1][3] = 0x20;
2037 ram_table->bright_pos_gain[2][0] = 0x20;
2038 ram_table->bright_pos_gain[2][1] = 0x20;
2039 ram_table->bright_pos_gain[2][2] = 0x20;
2040 ram_table->bright_pos_gain[2][3] = 0x20;
2041 ram_table->bright_pos_gain[3][0] = 0x20;
2042 ram_table->bright_pos_gain[3][1] = 0x20;
2043 ram_table->bright_pos_gain[3][2] = 0x20;
2044 ram_table->bright_pos_gain[3][3] = 0x20;
2045 ram_table->bright_pos_gain[4][0] = 0x20;
2046 ram_table->bright_pos_gain[4][1] = 0x20;
2047 ram_table->bright_pos_gain[4][2] = 0x20;
2048 ram_table->bright_pos_gain[4][3] = 0x20;
2049
2050 ram_table->dark_pos_gain[0][0] = 0x00;
2051 ram_table->dark_pos_gain[0][1] = 0x00;
2052 ram_table->dark_pos_gain[0][2] = 0x00;
2053 ram_table->dark_pos_gain[0][3] = 0x00;
2054 ram_table->dark_pos_gain[1][0] = 0x00;
2055 ram_table->dark_pos_gain[1][1] = 0x00;
2056 ram_table->dark_pos_gain[1][2] = 0x00;
2057 ram_table->dark_pos_gain[1][3] = 0x00;
2058 ram_table->dark_pos_gain[2][0] = 0x00;
2059 ram_table->dark_pos_gain[2][1] = 0x00;
2060 ram_table->dark_pos_gain[2][2] = 0x00;
2061 ram_table->dark_pos_gain[2][3] = 0x00;
2062 ram_table->dark_pos_gain[3][0] = 0x00;
2063 ram_table->dark_pos_gain[3][1] = 0x00;
2064 ram_table->dark_pos_gain[3][2] = 0x00;
2065 ram_table->dark_pos_gain[3][3] = 0x00;
2066 ram_table->dark_pos_gain[4][0] = 0x00;
2067 ram_table->dark_pos_gain[4][1] = 0x00;
2068 ram_table->dark_pos_gain[4][2] = 0x00;
2069 ram_table->dark_pos_gain[4][3] = 0x00;
2070
2071 ram_table->hybrid_factor[0] = 0xff;
2072 ram_table->hybrid_factor[1] = 0xff;
2073 ram_table->hybrid_factor[2] = 0xff;
2074 ram_table->hybrid_factor[3] = 0xc0;
2075
2076 ram_table->contrast_factor[0] = 0x99;
2077 ram_table->contrast_factor[1] = 0x99;
2078 ram_table->contrast_factor[2] = 0x90;
2079 ram_table->contrast_factor[3] = 0x80;
2080
2081 ram_table->iir_curve[0] = 0x65;
2082 ram_table->iir_curve[1] = 0x65;
2083 ram_table->iir_curve[2] = 0x65;
2084 ram_table->iir_curve[3] = 0x65;
2085 ram_table->iir_curve[4] = 0x65;
2086
2087 //Gamma 2.2
2088 ram_table->crgb_thresh[0] = cpu_to_be16(0x127c);
2089 ram_table->crgb_thresh[1] = cpu_to_be16(0x151b);
2090 ram_table->crgb_thresh[2] = cpu_to_be16(0x17d5);
2091 ram_table->crgb_thresh[3] = cpu_to_be16(0x1a56);
2092 ram_table->crgb_thresh[4] = cpu_to_be16(0x1c83);
2093 ram_table->crgb_thresh[5] = cpu_to_be16(0x1e72);
2094 ram_table->crgb_thresh[6] = cpu_to_be16(0x20f0);
2095 ram_table->crgb_thresh[7] = cpu_to_be16(0x232b);
2096 ram_table->crgb_offset[0] = cpu_to_be16(0x2999);
2097 ram_table->crgb_offset[1] = cpu_to_be16(0x3999);
2098 ram_table->crgb_offset[2] = cpu_to_be16(0x4666);
2099 ram_table->crgb_offset[3] = cpu_to_be16(0x5999);
2100 ram_table->crgb_offset[4] = cpu_to_be16(0x6333);
2101 ram_table->crgb_offset[5] = cpu_to_be16(0x7800);
2102 ram_table->crgb_offset[6] = cpu_to_be16(0x8c00);
2103 ram_table->crgb_offset[7] = cpu_to_be16(0xa000);
2104 ram_table->crgb_slope[0] = cpu_to_be16(0x3609);
2105 ram_table->crgb_slope[1] = cpu_to_be16(0x2dfa);
2106 ram_table->crgb_slope[2] = cpu_to_be16(0x27ea);
2107 ram_table->crgb_slope[3] = cpu_to_be16(0x235d);
2108 ram_table->crgb_slope[4] = cpu_to_be16(0x2042);
2109 ram_table->crgb_slope[5] = cpu_to_be16(0x1dc3);
2110 ram_table->crgb_slope[6] = cpu_to_be16(0x1b1a);
2111 ram_table->crgb_slope[7] = cpu_to_be16(0x1910);
2112
2113 fill_backlight_transform_table_v_2_2(
2114 params, ram_table, true);
2115 }
2116
fill_iram_v_2_3(struct iram_table_v_2_2 * ram_table,struct dmcu_iram_parameters params,bool big_endian)2117 static void fill_iram_v_2_3(struct iram_table_v_2_2 *ram_table, struct dmcu_iram_parameters params, bool big_endian)
2118 {
2119 unsigned int i, j;
2120 unsigned int set = params.set;
2121
2122 ram_table->flags = 0x0;
2123 ram_table->min_abm_backlight = (uint16_t)((big_endian) ?
2124 cpu_to_be16(params.min_abm_backlight) :
2125 cpu_to_le16(params.min_abm_backlight));
2126
2127 for (i = 0; i < NUM_AGGR_LEVEL; i++) {
2128 ram_table->hybrid_factor[i] = (uint8_t)abm_settings[set][i].brightness_gain;
2129 ram_table->contrast_factor[i] = abm_settings[set][i].contrast_factor;
2130 ram_table->deviation_gain[i] = abm_settings[set][i].deviation_gain;
2131 ram_table->min_knee[i] = abm_settings[set][i].min_knee;
2132 ram_table->max_knee[i] = abm_settings[set][i].max_knee;
2133
2134 for (j = 0; j < NUM_AMBI_LEVEL; j++) {
2135 ram_table->min_reduction[j][i] = abm_settings[set][i].min_reduction;
2136 ram_table->max_reduction[j][i] = abm_settings[set][i].max_reduction;
2137 ram_table->bright_pos_gain[j][i] = abm_settings[set][i].bright_pos_gain;
2138 ram_table->dark_pos_gain[j][i] = abm_settings[set][i].dark_pos_gain;
2139 }
2140 }
2141
2142 ram_table->iir_curve[0] = 0x65;
2143 ram_table->iir_curve[1] = 0x65;
2144 ram_table->iir_curve[2] = 0x65;
2145 ram_table->iir_curve[3] = 0x65;
2146 ram_table->iir_curve[4] = 0x65;
2147
2148 //Gamma 2.2
2149 ram_table->crgb_thresh[0] = bswap16_based_on_endian(big_endian, 0x127c);
2150 ram_table->crgb_thresh[1] = bswap16_based_on_endian(big_endian, 0x151b);
2151 ram_table->crgb_thresh[2] = bswap16_based_on_endian(big_endian, 0x17d5);
2152 ram_table->crgb_thresh[3] = bswap16_based_on_endian(big_endian, 0x1a56);
2153 ram_table->crgb_thresh[4] = bswap16_based_on_endian(big_endian, 0x1c83);
2154 ram_table->crgb_thresh[5] = bswap16_based_on_endian(big_endian, 0x1e72);
2155 ram_table->crgb_thresh[6] = bswap16_based_on_endian(big_endian, 0x20f0);
2156 ram_table->crgb_thresh[7] = bswap16_based_on_endian(big_endian, 0x232b);
2157 ram_table->crgb_offset[0] = bswap16_based_on_endian(big_endian, 0x2999);
2158 ram_table->crgb_offset[1] = bswap16_based_on_endian(big_endian, 0x3999);
2159 ram_table->crgb_offset[2] = bswap16_based_on_endian(big_endian, 0x4666);
2160 ram_table->crgb_offset[3] = bswap16_based_on_endian(big_endian, 0x5999);
2161 ram_table->crgb_offset[4] = bswap16_based_on_endian(big_endian, 0x6333);
2162 ram_table->crgb_offset[5] = bswap16_based_on_endian(big_endian, 0x7800);
2163 ram_table->crgb_offset[6] = bswap16_based_on_endian(big_endian, 0x8c00);
2164 ram_table->crgb_offset[7] = bswap16_based_on_endian(big_endian, 0xa000);
2165 ram_table->crgb_slope[0] = bswap16_based_on_endian(big_endian, 0x3609);
2166 ram_table->crgb_slope[1] = bswap16_based_on_endian(big_endian, 0x2dfa);
2167 ram_table->crgb_slope[2] = bswap16_based_on_endian(big_endian, 0x27ea);
2168 ram_table->crgb_slope[3] = bswap16_based_on_endian(big_endian, 0x235d);
2169 ram_table->crgb_slope[4] = bswap16_based_on_endian(big_endian, 0x2042);
2170 ram_table->crgb_slope[5] = bswap16_based_on_endian(big_endian, 0x1dc3);
2171 ram_table->crgb_slope[6] = bswap16_based_on_endian(big_endian, 0x1b1a);
2172 ram_table->crgb_slope[7] = bswap16_based_on_endian(big_endian, 0x1910);
2173
2174 fill_backlight_transform_table_v_2_2(
2175 params, ram_table, big_endian);
2176 }
2177
dmub_init_abm_config(struct resource_pool * res_pool,struct dmcu_iram_parameters params,unsigned int inst)2178 bool dmub_init_abm_config(struct resource_pool *res_pool,
2179 struct dmcu_iram_parameters params,
2180 unsigned int inst)
2181 {
2182 struct iram_table_v_2_2 ram_table;
2183 struct abm_config_table config;
2184 unsigned int set = params.set;
2185 bool result = false;
2186 uint32_t i, j = 0;
2187
2188 if (res_pool->abm == NULL && res_pool->multiple_abms[inst] == NULL)
2189 return false;
2190
2191 memset(&ram_table, 0, sizeof(ram_table));
2192 memset(&config, 0, sizeof(config));
2193
2194 fill_iram_v_2_3(&ram_table, params, false);
2195
2196 // We must copy to structure that is aligned to 32-bit
2197 for (i = 0; i < NUM_POWER_FN_SEGS; i++) {
2198 config.crgb_thresh[i] = ram_table.crgb_thresh[i];
2199 config.crgb_offset[i] = ram_table.crgb_offset[i];
2200 config.crgb_slope[i] = ram_table.crgb_slope[i];
2201 }
2202
2203 for (i = 0; i < NUM_BL_CURVE_SEGS; i++) {
2204 config.backlight_thresholds[i] = ram_table.backlight_thresholds[i];
2205 config.backlight_offsets[i] = ram_table.backlight_offsets[i];
2206 }
2207
2208 for (i = 0; i < NUM_AMBI_LEVEL; i++)
2209 config.iir_curve[i] = ram_table.iir_curve[i];
2210
2211 for (i = 0; i < NUM_AMBI_LEVEL; i++) {
2212 for (j = 0; j < NUM_AGGR_LEVEL; j++) {
2213 config.min_reduction[i][j] = ram_table.min_reduction[i][j];
2214 config.max_reduction[i][j] = ram_table.max_reduction[i][j];
2215 config.bright_pos_gain[i][j] = ram_table.bright_pos_gain[i][j];
2216 config.dark_pos_gain[i][j] = ram_table.dark_pos_gain[i][j];
2217 }
2218 }
2219
2220 for (i = 0; i < NUM_AGGR_LEVEL; i++) {
2221 config.hybrid_factor[i] = ram_table.hybrid_factor[i];
2222 config.contrast_factor[i] = ram_table.contrast_factor[i];
2223 config.deviation_gain[i] = ram_table.deviation_gain[i];
2224 config.min_knee[i] = ram_table.min_knee[i];
2225 config.max_knee[i] = ram_table.max_knee[i];
2226 }
2227
2228 if (params.backlight_ramping_override) {
2229
2230 ASSERT(params.backlight_ramping_reduction <= 0xFFFF);
2231 ASSERT(params.backlight_ramping_start <= 0xFFFF);
2232 for (i = 0; i < NUM_AGGR_LEVEL; i++) {
2233 config.blRampReduction[i] = (uint16_t)params.backlight_ramping_reduction;
2234 config.blRampStart[i] = (uint16_t)params.backlight_ramping_start;
2235 }
2236 } else {
2237 for (i = 0; i < NUM_AGGR_LEVEL; i++) {
2238 config.blRampReduction[i] = abm_settings[set][i].blRampReduction;
2239 config.blRampStart[i] = abm_settings[set][i].blRampStart;
2240 }
2241 }
2242
2243 config.min_abm_backlight = ram_table.min_abm_backlight;
2244
2245 if (res_pool->multiple_abms[inst]) {
2246 result = res_pool->multiple_abms[inst]->funcs->init_abm_config(
2247 res_pool->multiple_abms[inst], (char *)(&config), sizeof(struct abm_config_table), inst);
2248 } else
2249 result = res_pool->abm->funcs->init_abm_config(
2250 res_pool->abm, (char *)(&config), sizeof(struct abm_config_table), 0);
2251
2252 return result;
2253 }
2254
dmub_is_abm_supported(struct resource_pool * res_pool,unsigned int inst)2255 bool dmub_is_abm_supported(struct resource_pool *res_pool, unsigned int inst)
2256 {
2257
2258 if (res_pool->abm == NULL && res_pool->multiple_abms[inst] == NULL)
2259 return false;
2260
2261 return true;
2262 }
2263
dmub_set_abm_event(struct resource_pool * res_pool,unsigned int full_screen,unsigned int trans_info,unsigned int hdr_mode,unsigned int scaling_enable,unsigned int scaling_strength_map,unsigned int inst)2264 bool dmub_set_abm_event(struct resource_pool *res_pool,
2265 unsigned int full_screen, unsigned int trans_info,
2266 unsigned int hdr_mode, unsigned int scaling_enable, unsigned int scaling_strength_map,
2267 unsigned int inst)
2268 {
2269 bool result = false;
2270
2271 if (res_pool->abm == NULL && res_pool->multiple_abms[inst] == NULL)
2272 return false;
2273
2274 if (res_pool->multiple_abms[inst]) {
2275 if (res_pool->multiple_abms[inst]->funcs->set_abm_event)
2276 result = res_pool->multiple_abms[inst]->funcs->set_abm_event(
2277 res_pool->multiple_abms[inst], full_screen, trans_info,
2278 hdr_mode, scaling_enable, scaling_strength_map, inst);
2279 } else {
2280 if (res_pool->abm->funcs->set_abm_event)
2281 result = res_pool->abm->funcs->set_abm_event(
2282 res_pool->abm, full_screen, trans_info,
2283 hdr_mode, scaling_enable, scaling_strength_map, inst);
2284 }
2285
2286 return result;
2287 }
2288
dmub_set_abm_strength(struct resource_pool * res_pool,unsigned int strength,unsigned int inst)2289 bool dmub_set_abm_strength(struct resource_pool *res_pool,
2290 unsigned int strength,
2291 unsigned int inst)
2292 {
2293 bool result = false;
2294
2295 if (res_pool->abm == NULL && res_pool->multiple_abms[inst] == NULL)
2296 return false;
2297
2298 if (res_pool->multiple_abms[inst]) {
2299 if (res_pool->multiple_abms[inst]->funcs->set_abm_level)
2300 result = res_pool->multiple_abms[inst]->funcs->set_abm_level(
2301 res_pool->multiple_abms[inst], strength);
2302 } else {
2303 if (res_pool->abm->funcs->set_abm_level)
2304 result = res_pool->abm->funcs->set_abm_level(
2305 res_pool->abm, strength);
2306 }
2307
2308 return result;
2309 }
2310
dmcu_load_iram(struct dmcu * dmcu,struct dmcu_iram_parameters params)2311 bool dmcu_load_iram(struct dmcu *dmcu,
2312 struct dmcu_iram_parameters params)
2313 {
2314 unsigned char ram_table[IRAM_SIZE];
2315 bool result = false;
2316
2317 if (dmcu == NULL)
2318 return false;
2319
2320 if (dmcu && !dmcu->funcs->is_dmcu_initialized(dmcu))
2321 return true;
2322
2323 memset(&ram_table, 0, sizeof(ram_table));
2324
2325 if (dmcu->dmcu_version.abm_version == 0x24) {
2326 fill_iram_v_2_3((struct iram_table_v_2_2 *)ram_table, params, true);
2327 result = dmcu->funcs->load_iram(dmcu, 0, (char *)(&ram_table),
2328 IRAM_RESERVE_AREA_START_V2_2);
2329 } else if (dmcu->dmcu_version.abm_version == 0x23) {
2330 fill_iram_v_2_3((struct iram_table_v_2_2 *)ram_table, params, true);
2331
2332 result = dmcu->funcs->load_iram(
2333 dmcu, 0, (char *)(&ram_table), IRAM_RESERVE_AREA_START_V2_2);
2334 } else if (dmcu->dmcu_version.abm_version == 0x22) {
2335 fill_iram_v_2_2((struct iram_table_v_2_2 *)ram_table, params);
2336
2337 result = dmcu->funcs->load_iram(
2338 dmcu, 0, (char *)(&ram_table), IRAM_RESERVE_AREA_START_V2_2);
2339 } else {
2340 fill_iram_v_2((struct iram_table_v_2 *)ram_table, params);
2341
2342 result = dmcu->funcs->load_iram(
2343 dmcu, 0, (char *)(&ram_table), IRAM_RESERVE_AREA_START_V2);
2344
2345 if (result)
2346 result = dmcu->funcs->load_iram(
2347 dmcu, IRAM_RESERVE_AREA_END_V2 + 1,
2348 (char *)(&ram_table) + IRAM_RESERVE_AREA_END_V2 + 1,
2349 sizeof(ram_table) - IRAM_RESERVE_AREA_END_V2 - 1);
2350 }
2351
2352 return result;
2353 }
2354
fill_custom_backlight_caps(unsigned int config_no,struct dm_acpi_atif_backlight_caps * caps)2355 bool fill_custom_backlight_caps(unsigned int config_no, struct dm_acpi_atif_backlight_caps *caps)
2356 {
2357 unsigned int data_points_size;
2358 uint64_t caps_size;
2359
2360 if (config_no >= ARRAY_SIZE(custom_backlight_profiles))
2361 return false;
2362
2363 data_points_size = custom_backlight_profiles[config_no].num_data_points
2364 * sizeof(custom_backlight_profiles[config_no].data_points[0]);
2365
2366 caps_size = sizeof(struct dm_acpi_atif_backlight_caps) - sizeof(caps->data_points) + data_points_size;
2367 ASSERT(caps_size <= 0xFFFF);
2368 caps->size = (uint16_t)caps_size;
2369 caps->flags = 0;
2370 caps->error_code = 0;
2371 caps->ac_level_percentage = custom_backlight_profiles[config_no].ac_level_percentage;
2372 caps->dc_level_percentage = custom_backlight_profiles[config_no].dc_level_percentage;
2373 caps->min_input_signal = custom_backlight_profiles[config_no].min_input_signal;
2374 caps->max_input_signal = custom_backlight_profiles[config_no].max_input_signal;
2375 caps->num_data_points = (uint8_t)custom_backlight_profiles[config_no].num_data_points;
2376 memcpy(caps->data_points, custom_backlight_profiles[config_no].data_points, data_points_size);
2377 return true;
2378 }
2379