From 4562236b3bc0a28aeb6ee93b2d8a849a4c4e1c7c Mon Sep 17 00:00:00 2001 From: Harry Wentland Date: Tue, 12 Sep 2017 15:58:20 -0400 Subject: drm/amd/dc: Add dc display driver (v2) Supported DCE versions: 8.0, 10.0, 11.0, 11.2 v2: rebase against 4.11 Signed-off-by: Harry Wentland Acked-by: Alex Deucher Signed-off-by: Alex Deucher --- drivers/gpu/drm/amd/display/modules/color/color.c | 2094 ++++++++++++++++++++ .../gpu/drm/amd/display/modules/freesync/Makefile | 10 + .../drm/amd/display/modules/freesync/freesync.c | 1158 +++++++++++ .../gpu/drm/amd/display/modules/inc/mod_color.h | 179 ++ .../gpu/drm/amd/display/modules/inc/mod_freesync.h | 149 ++ .../gpu/drm/amd/display/modules/inc/mod_power.h | 112 ++ drivers/gpu/drm/amd/display/modules/power/power.c | 784 ++++++++ 7 files changed, 4486 insertions(+) create mode 100644 drivers/gpu/drm/amd/display/modules/color/color.c create mode 100644 drivers/gpu/drm/amd/display/modules/freesync/Makefile create mode 100644 drivers/gpu/drm/amd/display/modules/freesync/freesync.c create mode 100644 drivers/gpu/drm/amd/display/modules/inc/mod_color.h create mode 100644 drivers/gpu/drm/amd/display/modules/inc/mod_freesync.h create mode 100644 drivers/gpu/drm/amd/display/modules/inc/mod_power.h create mode 100644 drivers/gpu/drm/amd/display/modules/power/power.c (limited to 'drivers/gpu/drm/amd/display/modules') diff --git a/drivers/gpu/drm/amd/display/modules/color/color.c b/drivers/gpu/drm/amd/display/modules/color/color.c new file mode 100644 index 000000000000..cf030b18f6a9 --- /dev/null +++ b/drivers/gpu/drm/amd/display/modules/color/color.c @@ -0,0 +1,2094 @@ +/* + * Copyright 2016 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: AMD + * + */ + +#include "dm_services.h" +#include "dc.h" +#include "mod_color.h" +#include "core_types.h" +#include "fixed31_32.h" +#include "core_dc.h" + +#define MOD_COLOR_MAX_CONCURRENT_SINKS 32 +#define DIVIDER 10000 +/* S2D13 value in [-3.00...0.9999] */ +#define S2D13_MIN (-3 * DIVIDER) +#define S2D13_MAX (3 * DIVIDER) +#define S0D13_MIN (-1 * DIVIDER) +#define S0D13_MAX (1 * DIVIDER) + +struct sink_caps { + const struct dc_sink *sink; +}; + +struct gamut_calculation_matrix { + struct fixed31_32 MTransposed[9]; + struct fixed31_32 XYZtoRGB_Custom[9]; + struct fixed31_32 XYZtoRGB_Ref[9]; + struct fixed31_32 RGBtoXYZ_Final[9]; + + struct fixed31_32 MResult[9]; + struct fixed31_32 fXYZofWhiteRef[9]; + struct fixed31_32 fXYZofRGBRef[9]; +}; + +struct gamut_src_dst_matrix { + struct fixed31_32 rgbCoeffDst[9]; + struct fixed31_32 whiteCoeffDst[3]; + struct fixed31_32 rgbCoeffSrc[9]; + struct fixed31_32 whiteCoeffSrc[3]; +}; + +struct color_state { + bool user_enable_color_temperature; + int custom_color_temperature; + struct color_space_coordinates source_gamut; + struct color_space_coordinates destination_gamut; + struct color_range contrast; + struct color_range saturation; + struct color_range brightness; + struct color_range hue; + enum dc_quantization_range preferred_quantization_range; +}; + +struct core_color { + struct mod_color public; + struct dc *dc; + int num_sinks; + struct sink_caps *caps; + struct color_state *state; +}; + +#define MOD_COLOR_TO_CORE(mod_color)\ + container_of(mod_color, struct core_color, public) + +#define COLOR_REGISTRY_NAME "color_v1" + +/*Matrix Calculation Functions*/ +/** + ***************************************************************************** + * Function: transposeMatrix + * + * @brief + * rotate the matrix 90 degrees clockwise + * rows become a columns and columns to rows + * @param [ in ] M - source matrix + * @param [ in ] Rows - num of Rows of the original matrix + * @param [ in ] Cols - num of Cols of the original matrix + * @param [ out] MTransposed - result matrix + * @return void + * + ***************************************************************************** + */ +static void transpose_matrix(const struct fixed31_32 *M, unsigned int Rows, + unsigned int Cols, struct fixed31_32 *MTransposed) +{ + unsigned int i, j; + + for (i = 0; i < Rows; i++) { + for (j = 0; j < Cols; j++) + MTransposed[(j*Rows)+i] = M[(i*Cols)+j]; + } +} + +/** + ***************************************************************************** + * Function: multiplyMatrices + * + * @brief + * multiplies produce of two matrices: M = M1[ulRows1 x ulCols1] * + * M2[ulCols1 x ulCols2]. + * + * @param [ in ] M1 - first Matrix. + * @param [ in ] M2 - second Matrix. + * @param [ in ] Rows1 - num of Rows of the first Matrix + * @param [ in ] Cols1 - num of Cols of the first Matrix/Num of Rows + * of the second Matrix + * @param [ in ] Cols2 - num of Cols of the second Matrix + * @param [out ] mResult - resulting matrix. + * @return void + * + ***************************************************************************** + */ +static void multiply_matrices(struct fixed31_32 *mResult, + const struct fixed31_32 *M1, + const struct fixed31_32 *M2, unsigned int Rows1, + unsigned int Cols1, unsigned int Cols2) +{ + unsigned int i, j, k; + + for (i = 0; i < Rows1; i++) { + for (j = 0; j < Cols2; j++) { + mResult[(i * Cols2) + j] = dal_fixed31_32_zero; + for (k = 0; k < Cols1; k++) + mResult[(i * Cols2) + j] = + dal_fixed31_32_add + (mResult[(i * Cols2) + j], + dal_fixed31_32_mul(M1[(i * Cols1) + k], + M2[(k * Cols2) + j])); + } + } +} + +/** + ***************************************************************************** + * Function: cFind3X3Det + * + * @brief + * finds determinant of given 3x3 matrix + * + * @param [ in ] m - matrix + * @return determinate whioch could not be zero + * + ***************************************************************************** + */ +static struct fixed31_32 find_3X3_det(const struct fixed31_32 *m) +{ + struct fixed31_32 det, A1, A2, A3; + + A1 = dal_fixed31_32_mul(m[0], + dal_fixed31_32_sub(dal_fixed31_32_mul(m[4], m[8]), + dal_fixed31_32_mul(m[5], m[7]))); + A2 = dal_fixed31_32_mul(m[1], + dal_fixed31_32_sub(dal_fixed31_32_mul(m[3], m[8]), + dal_fixed31_32_mul(m[5], m[6]))); + A3 = dal_fixed31_32_mul(m[2], + dal_fixed31_32_sub(dal_fixed31_32_mul(m[3], m[7]), + dal_fixed31_32_mul(m[4], m[6]))); + det = dal_fixed31_32_add(dal_fixed31_32_sub(A1, A2), A3); + return det; +} + + +/** + ***************************************************************************** + * Function: computeInverseMatrix_3x3 + * + * @brief + * builds inverse matrix + * + * @param [ in ] m - matrix + * @param [ out ] im - result matrix + * @return true if success + * + ***************************************************************************** + */ +static bool compute_inverse_matrix_3x3(const struct fixed31_32 *m, + struct fixed31_32 *im) +{ + struct fixed31_32 determinant = find_3X3_det(m); + + if (dal_fixed31_32_eq(determinant, dal_fixed31_32_zero) == false) { + im[0] = dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_mul(m[4], m[8]), + dal_fixed31_32_mul(m[5], m[7])), determinant); + im[1] = dal_fixed31_32_neg(dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_mul(m[1], m[8]), + dal_fixed31_32_mul(m[2], m[7])), determinant)); + im[2] = dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_mul(m[1], m[5]), + dal_fixed31_32_mul(m[2], m[4])), determinant); + im[3] = dal_fixed31_32_neg(dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_mul(m[3], m[8]), + dal_fixed31_32_mul(m[5], m[6])), determinant)); + im[4] = dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_mul(m[0], m[8]), + dal_fixed31_32_mul(m[2], m[6])), determinant); + im[5] = dal_fixed31_32_neg(dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_mul(m[0], m[5]), + dal_fixed31_32_mul(m[2], m[3])), determinant)); + im[6] = dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_mul(m[3], m[7]), + dal_fixed31_32_mul(m[4], m[6])), determinant); + im[7] = dal_fixed31_32_neg(dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_mul(m[0], m[7]), + dal_fixed31_32_mul(m[1], m[6])), determinant)); + im[8] = dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_mul(m[0], m[4]), + dal_fixed31_32_mul(m[1], m[3])), determinant); + return true; + } + return false; +} + +/** + ***************************************************************************** + * Function: calculateXYZtoRGB_M3x3 + * + * @brief + * Calculates transformation matrix from XYZ coordinates to RBG + * + * @param [ in ] XYZofRGB - primaries XYZ + * @param [ in ] XYZofWhite - white point. + * @param [ out ] XYZtoRGB - RGB primires + * @return true if success + * + ***************************************************************************** + */ +static bool calculate_XYZ_to_RGB_3x3(const struct fixed31_32 *XYZofRGB, + const struct fixed31_32 *XYZofWhite, + struct fixed31_32 *XYZtoRGB) +{ + + struct fixed31_32 MInversed[9]; + struct fixed31_32 SVector[3]; + + /*1. Find Inverse matrix 3x3 of MTransposed*/ + if (!compute_inverse_matrix_3x3(XYZofRGB, MInversed)) + return false; + + /*2. Calculate vector: |Sr Sg Sb| = [MInversed] * |Wx Wy Wz|*/ + multiply_matrices(SVector, MInversed, XYZofWhite, 3, 3, 1); + + /*3. Calculate matrix XYZtoRGB 3x3*/ + XYZtoRGB[0] = dal_fixed31_32_mul(XYZofRGB[0], SVector[0]); + XYZtoRGB[1] = dal_fixed31_32_mul(XYZofRGB[1], SVector[1]); + XYZtoRGB[2] = dal_fixed31_32_mul(XYZofRGB[2], SVector[2]); + + XYZtoRGB[3] = dal_fixed31_32_mul(XYZofRGB[3], SVector[0]); + XYZtoRGB[4] = dal_fixed31_32_mul(XYZofRGB[4], SVector[1]); + XYZtoRGB[5] = dal_fixed31_32_mul(XYZofRGB[5], SVector[2]); + + XYZtoRGB[6] = dal_fixed31_32_mul(XYZofRGB[6], SVector[0]); + XYZtoRGB[7] = dal_fixed31_32_mul(XYZofRGB[7], SVector[1]); + XYZtoRGB[8] = dal_fixed31_32_mul(XYZofRGB[8], SVector[2]); + + return true; +} + +static bool gamut_to_color_matrix( + const struct fixed31_32 *pXYZofRGB,/*destination gamut*/ + const struct fixed31_32 *pXYZofWhite,/*destination of white point*/ + const struct fixed31_32 *pRefXYZofRGB,/*source gamut*/ + const struct fixed31_32 *pRefXYZofWhite,/*source of white point*/ + bool invert, + struct fixed31_32 *tempMatrix3X3) +{ + int i = 0; + struct gamut_calculation_matrix *matrix = + dm_alloc(sizeof(struct gamut_calculation_matrix)); + + struct fixed31_32 *pXYZtoRGB_Temp; + struct fixed31_32 *pXYZtoRGB_Final; + + matrix->fXYZofWhiteRef[0] = pRefXYZofWhite[0]; + matrix->fXYZofWhiteRef[1] = pRefXYZofWhite[1]; + matrix->fXYZofWhiteRef[2] = pRefXYZofWhite[2]; + + + matrix->fXYZofRGBRef[0] = pRefXYZofRGB[0]; + matrix->fXYZofRGBRef[1] = pRefXYZofRGB[1]; + matrix->fXYZofRGBRef[2] = pRefXYZofRGB[2]; + + matrix->fXYZofRGBRef[3] = pRefXYZofRGB[3]; + matrix->fXYZofRGBRef[4] = pRefXYZofRGB[4]; + matrix->fXYZofRGBRef[5] = pRefXYZofRGB[5]; + + matrix->fXYZofRGBRef[6] = pRefXYZofRGB[6]; + matrix->fXYZofRGBRef[7] = pRefXYZofRGB[7]; + matrix->fXYZofRGBRef[8] = pRefXYZofRGB[8]; + + /*default values - unity matrix*/ + while (i < 9) { + if (i == 0 || i == 4 || i == 8) + tempMatrix3X3[i] = dal_fixed31_32_one; + else + tempMatrix3X3[i] = dal_fixed31_32_zero; + i++; + } + + /*1. Decide about the order of calculation. + * bInvert == FALSE --> RGBtoXYZ_Ref * XYZtoRGB_Custom + * bInvert == TRUE --> RGBtoXYZ_Custom * XYZtoRGB_Ref */ + if (invert) { + pXYZtoRGB_Temp = matrix->XYZtoRGB_Custom; + pXYZtoRGB_Final = matrix->XYZtoRGB_Ref; + } else { + pXYZtoRGB_Temp = matrix->XYZtoRGB_Ref; + pXYZtoRGB_Final = matrix->XYZtoRGB_Custom; + } + + /*2. Calculate XYZtoRGB_Ref*/ + transpose_matrix(matrix->fXYZofRGBRef, 3, 3, matrix->MTransposed); + + if (!calculate_XYZ_to_RGB_3x3( + matrix->MTransposed, + matrix->fXYZofWhiteRef, + matrix->XYZtoRGB_Ref)) + goto function_fail; + + /*3. Calculate XYZtoRGB_Custom*/ + transpose_matrix(pXYZofRGB, 3, 3, matrix->MTransposed); + + if (!calculate_XYZ_to_RGB_3x3( + matrix->MTransposed, + pXYZofWhite, + matrix->XYZtoRGB_Custom)) + goto function_fail; + + /*4. Calculate RGBtoXYZ - + * inverse matrix 3x3 of XYZtoRGB_Ref or XYZtoRGB_Custom*/ + if (!compute_inverse_matrix_3x3(pXYZtoRGB_Temp, matrix->RGBtoXYZ_Final)) + goto function_fail; + + /*5. Calculate M(3x3) = RGBtoXYZ * XYZtoRGB*/ + multiply_matrices(matrix->MResult, matrix->RGBtoXYZ_Final, + pXYZtoRGB_Final, 3, 3, 3); + + for (i = 0; i < 9; i++) + tempMatrix3X3[i] = matrix->MResult[i]; + + dm_free(matrix); + + return true; + +function_fail: + dm_free(matrix); + return false; +} + +static bool build_gamut_remap_matrix + (struct color_space_coordinates gamut_description, + struct fixed31_32 *rgb_matrix, + struct fixed31_32 *white_point_matrix) +{ + struct fixed31_32 fixed_blueX = dal_fixed31_32_from_fraction + (gamut_description.blueX, DIVIDER); + struct fixed31_32 fixed_blueY = dal_fixed31_32_from_fraction + (gamut_description.blueY, DIVIDER); + struct fixed31_32 fixed_greenX = dal_fixed31_32_from_fraction + (gamut_description.greenX, DIVIDER); + struct fixed31_32 fixed_greenY = dal_fixed31_32_from_fraction + (gamut_description.greenY, DIVIDER); + struct fixed31_32 fixed_redX = dal_fixed31_32_from_fraction + (gamut_description.redX, DIVIDER); + struct fixed31_32 fixed_redY = dal_fixed31_32_from_fraction + (gamut_description.redY, DIVIDER); + struct fixed31_32 fixed_whiteX = dal_fixed31_32_from_fraction + (gamut_description.whiteX, DIVIDER); + struct fixed31_32 fixed_whiteY = dal_fixed31_32_from_fraction + (gamut_description.whiteY, DIVIDER); + + rgb_matrix[0] = dal_fixed31_32_div(fixed_redX, fixed_redY); + rgb_matrix[1] = dal_fixed31_32_one; + rgb_matrix[2] = dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_sub(dal_fixed31_32_one, fixed_redX), + fixed_redY), fixed_redY); + + rgb_matrix[3] = dal_fixed31_32_div(fixed_greenX, fixed_greenY); + rgb_matrix[4] = dal_fixed31_32_one; + rgb_matrix[5] = dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_sub(dal_fixed31_32_one, fixed_greenX), + fixed_greenY), fixed_greenY); + + rgb_matrix[6] = dal_fixed31_32_div(fixed_blueX, fixed_blueY); + rgb_matrix[7] = dal_fixed31_32_one; + rgb_matrix[8] = dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_sub(dal_fixed31_32_one, fixed_blueX), + fixed_blueY), fixed_blueY); + + white_point_matrix[0] = dal_fixed31_32_div(fixed_whiteX, fixed_whiteY); + white_point_matrix[1] = dal_fixed31_32_one; + white_point_matrix[2] = dal_fixed31_32_div(dal_fixed31_32_sub + (dal_fixed31_32_sub(dal_fixed31_32_one, fixed_whiteX), + fixed_whiteY), fixed_whiteY); + + return true; +} + +static bool check_dc_support(const struct dc *dc) +{ + if (dc->stream_funcs.set_gamut_remap == NULL) + return false; + + return true; +} + +static uint16_t fixed_point_to_int_frac( + struct fixed31_32 arg, + uint8_t integer_bits, + uint8_t fractional_bits) +{ + int32_t numerator; + int32_t divisor = 1 << fractional_bits; + + uint16_t result; + + uint16_t d = (uint16_t)dal_fixed31_32_floor( + dal_fixed31_32_abs( + arg)); + + if (d <= (uint16_t)(1 << integer_bits) - (1 / (uint16_t)divisor)) + numerator = (uint16_t)dal_fixed31_32_floor( + dal_fixed31_32_mul_int( + arg, + divisor)); + else { + numerator = dal_fixed31_32_floor( + dal_fixed31_32_sub( + dal_fixed31_32_from_int( + 1LL << integer_bits), + dal_fixed31_32_recip( + dal_fixed31_32_from_int( + divisor)))); + } + + if (numerator >= 0) + result = (uint16_t)numerator; + else + result = (uint16_t)( + (1 << (integer_bits + fractional_bits + 1)) + numerator); + + if ((result != 0) && dal_fixed31_32_lt( + arg, dal_fixed31_32_zero)) + result |= 1 << (integer_bits + fractional_bits); + + return result; +} + +/** +* convert_float_matrix +* This converts a double into HW register spec defined format S2D13. +* @param : +* @return None +*/ + +static void convert_float_matrix_legacy( + uint16_t *matrix, + struct fixed31_32 *flt, + uint32_t buffer_size) +{ + const struct fixed31_32 min_2_13 = + dal_fixed31_32_from_fraction(S2D13_MIN, DIVIDER); + const struct fixed31_32 max_2_13 = + dal_fixed31_32_from_fraction(S2D13_MAX, DIVIDER); + uint32_t i; + + for (i = 0; i < buffer_size; ++i) { + uint32_t reg_value = + fixed_point_to_int_frac( + dal_fixed31_32_clamp( + flt[i], + min_2_13, + max_2_13), + 2, + 13); + + matrix[i] = (uint16_t)reg_value; + } +} + +static void convert_float_matrix( + uint16_t *matrix, + struct fixed31_32 *flt, + uint32_t buffer_size) +{ + const struct fixed31_32 min_0_13 = + dal_fixed31_32_from_fraction(S0D13_MIN, DIVIDER); + const struct fixed31_32 max_0_13 = + dal_fixed31_32_from_fraction(S0D13_MAX, DIVIDER); + const struct fixed31_32 min_2_13 = + dal_fixed31_32_from_fraction(S2D13_MIN, DIVIDER); + const struct fixed31_32 max_2_13 = + dal_fixed31_32_from_fraction(S2D13_MAX, DIVIDER); + uint32_t i; + uint16_t temp_matrix[12]; + + for (i = 0; i < buffer_size; ++i) { + if (i == 3 || i == 7 || i == 11) { + uint32_t reg_value = + fixed_point_to_int_frac( + dal_fixed31_32_clamp( + flt[i], + min_0_13, + max_0_13), + 2, + 13); + + temp_matrix[i] = (uint16_t)reg_value; + } else { + uint32_t reg_value = + fixed_point_to_int_frac( + dal_fixed31_32_clamp( + flt[i], + min_2_13, + max_2_13), + 2, + 13); + + temp_matrix[i] = (uint16_t)reg_value; + } + } + + matrix[4] = temp_matrix[0]; + matrix[5] = temp_matrix[1]; + matrix[6] = temp_matrix[2]; + matrix[7] = temp_matrix[3]; + + matrix[8] = temp_matrix[4]; + matrix[9] = temp_matrix[5]; + matrix[10] = temp_matrix[6]; + matrix[11] = temp_matrix[7]; + + matrix[0] = temp_matrix[8]; + matrix[1] = temp_matrix[9]; + matrix[2] = temp_matrix[10]; + matrix[3] = temp_matrix[11]; +} + +static int get_hw_value_from_sw_value(int swVal, int swMin, + int swMax, int hwMin, int hwMax) +{ + int dSW = swMax - swMin; /*software adjustment range size*/ + int dHW = hwMax - hwMin; /*hardware adjustment range size*/ + int hwVal; /*HW adjustment value*/ + + /* error case, I preserve the behavior from the predecessor + *getHwStepFromSwHwMinMaxValue (removed in Feb 2013) + *which was the FP version that only computed SCLF (i.e. dHW/dSW). + *it would return 0 in this case so + *hwVal = hwMin from the formula given in @brief + */ + if (dSW == 0) + return hwMin; + + /*it's quite often that ranges match, + *e.g. for overlay colors currently (Feb 2013) + *only brightness has a different + *HW range, and in this case no multiplication or division is needed, + *and if minimums match, no calculation at all + */ + if (dSW != dHW) { + hwVal = (swVal - swMin)*dHW/dSW + hwMin; + } else { + hwVal = swVal; + if (swMin != hwMin) + hwVal += (hwMin - swMin); + } + + return hwVal; +} + +static void initialize_fix_point_color_values( + struct core_color *core_color, + unsigned int sink_index, + struct fixed31_32 *grph_cont, + struct fixed31_32 *grph_sat, + struct fixed31_32 *grph_bright, + struct fixed31_32 *sin_grph_hue, + struct fixed31_32 *cos_grph_hue) +{ + /* Hue adjustment could be negative. -45 ~ +45 */ + struct fixed31_32 hue = + dal_fixed31_32_mul( + dal_fixed31_32_from_fraction + (get_hw_value_from_sw_value + (core_color->state[sink_index].hue.current, + core_color->state[sink_index].hue.min, + core_color->state[sink_index].hue.max, + -30, 30), 180), + dal_fixed31_32_pi); + + *sin_grph_hue = dal_fixed31_32_sin(hue); + *cos_grph_hue = dal_fixed31_32_cos(hue); + + *grph_cont = + dal_fixed31_32_from_fraction(get_hw_value_from_sw_value + (core_color->state[sink_index].contrast.current, + core_color->state[sink_index].contrast.min, + core_color->state[sink_index].contrast.max, + 50, 150), 100); + *grph_sat = + dal_fixed31_32_from_fraction(get_hw_value_from_sw_value + (core_color->state[sink_index].saturation.current, + core_color->state[sink_index].saturation.min, + core_color->state[sink_index].saturation.max, + 0, 200), 100); + *grph_bright = + dal_fixed31_32_from_fraction(get_hw_value_from_sw_value + (core_color->state[sink_index].brightness.current, + core_color->state[sink_index].brightness.min, + core_color->state[sink_index].brightness.max, + -25, 25), 100); +} + + +/* Given a specific dc_sink* this function finds its equivalent + * on the dc_sink array and returns the corresponding index + */ +static unsigned int sink_index_from_sink(struct core_color *core_color, + const struct dc_sink *sink) +{ + unsigned int index = 0; + + for (index = 0; index < core_color->num_sinks; index++) + if (core_color->caps[index].sink == sink) + return index; + + /* Could not find sink requested */ + ASSERT(false); + return index; +} + +static void calculate_rgb_matrix_legacy(struct core_color *core_color, + unsigned int sink_index, + struct fixed31_32 *rgb_matrix) +{ + const struct fixed31_32 k1 = + dal_fixed31_32_from_fraction(701000, 1000000); + const struct fixed31_32 k2 = + dal_fixed31_32_from_fraction(236568, 1000000); + const struct fixed31_32 k3 = + dal_fixed31_32_from_fraction(-587000, 1000000); + const struct fixed31_32 k4 = + dal_fixed31_32_from_fraction(464432, 1000000); + const struct fixed31_32 k5 = + dal_fixed31_32_from_fraction(-114000, 1000000); + const struct fixed31_32 k6 = + dal_fixed31_32_from_fraction(-701000, 1000000); + const struct fixed31_32 k7 = + dal_fixed31_32_from_fraction(-299000, 1000000); + const struct fixed31_32 k8 = + dal_fixed31_32_from_fraction(-292569, 1000000); + const struct fixed31_32 k9 = + dal_fixed31_32_from_fraction(413000, 1000000); + const struct fixed31_32 k10 = + dal_fixed31_32_from_fraction(-92482, 1000000); + const struct fixed31_32 k11 = + dal_fixed31_32_from_fraction(-114000, 1000000); + const struct fixed31_32 k12 = + dal_fixed31_32_from_fraction(385051, 1000000); + const struct fixed31_32 k13 = + dal_fixed31_32_from_fraction(-299000, 1000000); + const struct fixed31_32 k14 = + dal_fixed31_32_from_fraction(886000, 1000000); + const struct fixed31_32 k15 = + dal_fixed31_32_from_fraction(-587000, 1000000); + const struct fixed31_32 k16 = + dal_fixed31_32_from_fraction(-741914, 1000000); + const struct fixed31_32 k17 = + dal_fixed31_32_from_fraction(886000, 1000000); + const struct fixed31_32 k18 = + dal_fixed31_32_from_fraction(-144086, 1000000); + + const struct fixed31_32 luma_r = + dal_fixed31_32_from_fraction(299, 1000); + const struct fixed31_32 luma_g = + dal_fixed31_32_from_fraction(587, 1000); + const struct fixed31_32 luma_b = + dal_fixed31_32_from_fraction(114, 1000); + + struct fixed31_32 grph_cont; + struct fixed31_32 grph_sat; + struct fixed31_32 grph_bright; + struct fixed31_32 sin_grph_hue; + struct fixed31_32 cos_grph_hue; + + initialize_fix_point_color_values( + core_color, sink_index, &grph_cont, &grph_sat, + &grph_bright, &sin_grph_hue, &cos_grph_hue); + + /* COEF_1_1 = GrphCont * (LumaR + GrphSat * (Cos(GrphHue) * K1 +*/ + /* Sin(GrphHue) * K2))*/ + /* (Cos(GrphHue) * K1 + Sin(GrphHue) * K2)*/ + rgb_matrix[0] = + dal_fixed31_32_add( + dal_fixed31_32_mul(cos_grph_hue, k1), + dal_fixed31_32_mul(sin_grph_hue, k2)); + /* GrphSat * (Cos(GrphHue) * K1 + Sin(GrphHue) * K2 */ + rgb_matrix[0] = dal_fixed31_32_mul(grph_sat, rgb_matrix[0]); + /* (LumaR + GrphSat * (Cos(GrphHue) * K1 + Sin(GrphHue) * K2))*/ + rgb_matrix[0] = dal_fixed31_32_add(luma_r, rgb_matrix[0]); + /* GrphCont * (LumaR + GrphSat * (Cos(GrphHue) * K1 + Sin(GrphHue)**/ + /* K2))*/ + rgb_matrix[0] = dal_fixed31_32_mul(grph_cont, rgb_matrix[0]); + + /* COEF_1_2 = GrphCont * (LumaG + GrphSat * (Cos(GrphHue) * K3 +*/ + /* Sin(GrphHue) * K4))*/ + /* (Cos(GrphHue) * K3 + Sin(GrphHue) * K4)*/ + rgb_matrix[1] = + dal_fixed31_32_add( + dal_fixed31_32_mul(cos_grph_hue, k3), + dal_fixed31_32_mul(sin_grph_hue, k4)); + /* GrphSat * (Cos(GrphHue) * K3 + Sin(GrphHue) * K4)*/ + rgb_matrix[1] = dal_fixed31_32_mul(grph_sat, rgb_matrix[1]); + /* (LumaG + GrphSat * (Cos(GrphHue) * K3 + Sin(GrphHue) * K4))*/ + rgb_matrix[1] = dal_fixed31_32_add(luma_g, rgb_matrix[1]); + /* GrphCont * (LumaG + GrphSat * (Cos(GrphHue) * K3 + Sin(GrphHue)**/ + /* K4))*/ + rgb_matrix[1] = dal_fixed31_32_mul(grph_cont, rgb_matrix[1]); + + /* COEF_1_3 = GrphCont * (LumaB + GrphSat * (Cos(GrphHue) * K5 +*/ + /* Sin(GrphHue) * K6))*/ + /* (Cos(GrphHue) * K5 + Sin(GrphHue) * K6)*/ + rgb_matrix[2] = + dal_fixed31_32_add( + dal_fixed31_32_mul(cos_grph_hue, k5), + dal_fixed31_32_mul(sin_grph_hue, k6)); + /* GrphSat * (Cos(GrphHue) * K5 + Sin(GrphHue) * K6)*/ + rgb_matrix[2] = dal_fixed31_32_mul(grph_sat, rgb_matrix[2]); + /* LumaB + GrphSat * (Cos(GrphHue) * K5 + Sin(GrphHue) * K6)*/ + rgb_matrix[2] = dal_fixed31_32_add(luma_b, rgb_matrix[2]); + /* GrphCont * (LumaB + GrphSat * (Cos(GrphHue) * K5 + Sin(GrphHue)**/ + /* K6))*/ + rgb_matrix[2] = dal_fixed31_32_mul(grph_cont, rgb_matrix[2]); + + /* COEF_1_4 = GrphBright*/ + rgb_matrix[3] = grph_bright; + + /* COEF_2_1 = GrphCont * (LumaR + GrphSat * (Cos(GrphHue) * K7 +*/ + /* Sin(GrphHue) * K8))*/ + /* (Cos(GrphHue) * K7 + Sin(GrphHue) * K8)*/ + rgb_matrix[4] = + dal_fixed31_32_add( + dal_fixed31_32_mul(cos_grph_hue, k7), + dal_fixed31_32_mul(sin_grph_hue, k8)); + /* GrphSat * (Cos(GrphHue) * K7 + Sin(GrphHue) * K8)*/ + rgb_matrix[4] = dal_fixed31_32_mul(grph_sat, rgb_matrix[4]); + /* (LumaR + GrphSat * (Cos(GrphHue) * K7 + Sin(GrphHue) * K8))*/ + rgb_matrix[4] = dal_fixed31_32_add(luma_r, rgb_matrix[4]); + /* GrphCont * (LumaR + GrphSat * (Cos(GrphHue) * K7 + Sin(GrphHue)**/ + /* K8))*/ + rgb_matrix[4] = dal_fixed31_32_mul(grph_cont, rgb_matrix[4]); + + /* COEF_2_2 = GrphCont * (LumaG + GrphSat * (Cos(GrphHue) * K9 +*/ + /* Sin(GrphHue) * K10))*/ + /* (Cos(GrphHue) * K9 + Sin(GrphHue) * K10))*/ + rgb_matrix[5] = + dal_fixed31_32_add( + dal_fixed31_32_mul(cos_grph_hue, k9), + dal_fixed31_32_mul(sin_grph_hue, k10)); + /* GrphSat * (Cos(GrphHue) * K9 + Sin(GrphHue) * K10))*/ + rgb_matrix[5] = dal_fixed31_32_mul(grph_sat, rgb_matrix[5]); + /* (LumaG + GrphSat * (Cos(GrphHue) * K9 + Sin(GrphHue) * K10))*/ + rgb_matrix[5] = dal_fixed31_32_add(luma_g, rgb_matrix[5]); + /* GrphCont * (LumaG + GrphSat * (Cos(GrphHue) * K9 + Sin(GrphHue)**/ + /* K10))*/ + rgb_matrix[5] = dal_fixed31_32_mul(grph_cont, rgb_matrix[5]); + + /* COEF_2_3 = GrphCont * (LumaB + GrphSat * (Cos(GrphHue) * K11 +*/ + /* Sin(GrphHue) * K12))*/ + /* (Cos(GrphHue) * K11 + Sin(GrphHue) * K12))*/ + rgb_matrix[6] = + dal_fixed31_32_add( + dal_fixed31_32_mul(cos_grph_hue, k11), + dal_fixed31_32_mul(sin_grph_hue, k12)); + /* GrphSat * (Cos(GrphHue) * K11 + Sin(GrphHue) * K12))*/ + rgb_matrix[6] = dal_fixed31_32_mul(grph_sat, rgb_matrix[6]); + /* (LumaB + GrphSat * (Cos(GrphHue) * K11 + Sin(GrphHue) * K12))*/ + rgb_matrix[6] = dal_fixed31_32_add(luma_b, rgb_matrix[6]); + /* GrphCont * (LumaB + GrphSat * (Cos(GrphHue) * K11 + Sin(GrphHue)**/ + /* K12))*/ + rgb_matrix[6] = dal_fixed31_32_mul(grph_cont, rgb_matrix[6]); + + /* COEF_2_4 = GrphBright*/ + rgb_matrix[7] = grph_bright; + + /* COEF_3_1 = GrphCont * (LumaR + GrphSat * (Cos(GrphHue) * K13 +*/ + /* Sin(GrphHue) * K14))*/ + /* (Cos(GrphHue) * K13 + Sin(GrphHue) * K14)) */ + rgb_matrix[8] = + dal_fixed31_32_add( + dal_fixed31_32_mul(cos_grph_hue, k13), + dal_fixed31_32_mul(sin_grph_hue, k14)); + /* GrphSat * (Cos(GrphHue) * K13 + Sin(GrphHue) * K14)) */ + rgb_matrix[8] = dal_fixed31_32_mul(grph_sat, rgb_matrix[8]); + /* (LumaR + GrphSat * (Cos(GrphHue) * K13 + Sin(GrphHue) * K14)) */ + rgb_matrix[8] = dal_fixed31_32_add(luma_r, rgb_matrix[8]); + /* GrphCont * (LumaR + GrphSat * (Cos(GrphHue) * K13 + Sin(GrphHue)**/ + /* K14)) */ + rgb_matrix[8] = dal_fixed31_32_mul(grph_cont, rgb_matrix[8]); + + /* COEF_3_2 = GrphCont * (LumaG + GrphSat * (Cos(GrphHue) * K15 +*/ + /* Sin(GrphHue) * K16)) */ + /* GrphSat * (Cos(GrphHue) * K15 + Sin(GrphHue) * K16) */ + rgb_matrix[9] = + dal_fixed31_32_add( + dal_fixed31_32_mul(cos_grph_hue, k15), + dal_fixed31_32_mul(sin_grph_hue, k16)); + /* (LumaG + GrphSat * (Cos(GrphHue) * K15 + Sin(GrphHue) * K16)) */ + rgb_matrix[9] = dal_fixed31_32_mul(grph_sat, rgb_matrix[9]); + /* (LumaG + GrphSat * (Cos(GrphHue) * K15 + Sin(GrphHue) * K16)) */ + rgb_matrix[9] = dal_fixed31_32_add(luma_g, rgb_matrix[9]); + /* GrphCont * (LumaG + GrphSat * (Cos(GrphHue) * K15 + Sin(GrphHue)**/ + /* K16)) */ + rgb_matrix[9] = dal_fixed31_32_mul(grph_cont, rgb_matrix[9]); + + /* COEF_3_3 = GrphCont * (LumaB + GrphSat * (Cos(GrphHue) * K17 +*/ + /* Sin(GrphHue) * K18)) */ + /* (Cos(GrphHue) * K17 + Sin(GrphHue) * K18)) */ + rgb_matrix[10] = + dal_fixed31_32_add( + dal_fixed31_32_mul(cos_grph_hue, k17), + dal_fixed31_32_mul(sin_grph_hue, k18)); + /* GrphSat * (Cos(GrphHue) * K17 + Sin(GrphHue) * K18)) */ + rgb_matrix[10] = dal_fixed31_32_mul(grph_sat, rgb_matrix[10]); + /* (LumaB + GrphSat * (Cos(GrphHue) * K17 + Sin(GrphHue) * K18)) */ + rgb_matrix[10] = dal_fixed31_32_add(luma_b, rgb_matrix[10]); + /* GrphCont * (LumaB + GrphSat * (Cos(GrphHue) * K17 + Sin(GrphHue)**/ + /* K18)) */ + rgb_matrix[10] = dal_fixed31_32_mul(grph_cont, rgb_matrix[10]); + + /* COEF_3_4 = GrphBright */ + rgb_matrix[11] = grph_bright; +} + +static void calculate_rgb_limited_range_matrix(struct core_color *core_color, + unsigned int sink_index, struct fixed31_32 *rgb_matrix) +{ + struct fixed31_32 ideal[12]; + + static const int32_t matrix_[] = { + 85546875, 0, 0, 6250000, + 0, 85546875, 0, 6250000, + 0, 0, 85546875, 6250000 + }; + + uint32_t i = 0; + + do { + ideal[i] = dal_fixed31_32_from_fraction( + matrix_[i], + 100000000); + ++i; + } while (i != ARRAY_SIZE(matrix_)); + + + struct fixed31_32 grph_cont; + struct fixed31_32 grph_sat; + struct fixed31_32 grph_bright; + struct fixed31_32 sin_grph_hue; + struct fixed31_32 cos_grph_hue; + + initialize_fix_point_color_values( + core_color, sink_index, &grph_cont, &grph_sat, + &grph_bright, &sin_grph_hue, &cos_grph_hue); + + const struct fixed31_32 multiplier = + dal_fixed31_32_mul(grph_cont, grph_sat); + + rgb_matrix[8] = dal_fixed31_32_mul(ideal[0], grph_cont); + + rgb_matrix[9] = dal_fixed31_32_mul(ideal[1], grph_cont); + + rgb_matrix[10] = dal_fixed31_32_mul(ideal[2], grph_cont); + + rgb_matrix[11] = dal_fixed31_32_add( + ideal[3], + dal_fixed31_32_mul( + grph_bright, + dal_fixed31_32_from_fraction(86, 100))); + + rgb_matrix[0] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_add( + dal_fixed31_32_mul( + ideal[8], + sin_grph_hue), + dal_fixed31_32_mul( + ideal[4], + cos_grph_hue))); + + rgb_matrix[1] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_add( + dal_fixed31_32_mul( + ideal[9], + sin_grph_hue), + dal_fixed31_32_mul( + ideal[5], + cos_grph_hue))); + + rgb_matrix[2] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_add( + dal_fixed31_32_mul( + ideal[10], + sin_grph_hue), + dal_fixed31_32_mul( + ideal[6], + cos_grph_hue))); + + rgb_matrix[3] = ideal[7]; + + rgb_matrix[4] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_sub( + dal_fixed31_32_mul( + ideal[8], + cos_grph_hue), + dal_fixed31_32_mul( + ideal[4], + sin_grph_hue))); + + rgb_matrix[5] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_sub( + dal_fixed31_32_mul( + ideal[9], + cos_grph_hue), + dal_fixed31_32_mul( + ideal[5], + sin_grph_hue))); + + rgb_matrix[6] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_sub( + dal_fixed31_32_mul( + ideal[10], + cos_grph_hue), + dal_fixed31_32_mul( + ideal[6], + sin_grph_hue))); + + rgb_matrix[7] = ideal[11]; +} + +static void calculate_yuv_matrix(struct core_color *core_color, + unsigned int sink_index, + enum dc_color_space color_space, + struct fixed31_32 *yuv_matrix) +{ + struct fixed31_32 ideal[12]; + uint32_t i = 0; + + if ((color_space == COLOR_SPACE_YPBPR601) || + (color_space == COLOR_SPACE_YCBCR601) || + (color_space == COLOR_SPACE_YCBCR601_LIMITED)) { + static const int32_t matrix_[] = { + 25578516, 50216016, 9752344, 6250000, + -14764391, -28985609, 43750000, 50000000, + 43750000, -36635164, -7114836, 50000000 + }; + do { + ideal[i] = dal_fixed31_32_from_fraction( + matrix_[i], + 100000000); + ++i; + } while (i != ARRAY_SIZE(matrix_)); + } else { + static const int32_t matrix_[] = { + 18187266, 61183125, 6176484, 6250000, + -10025059, -33724941, 43750000, 50000000, + 43750000, -39738379, -4011621, 50000000 + }; + do { + ideal[i] = dal_fixed31_32_from_fraction( + matrix_[i], + 100000000); + ++i; + } while (i != ARRAY_SIZE(matrix_)); + } + + struct fixed31_32 grph_cont; + struct fixed31_32 grph_sat; + struct fixed31_32 grph_bright; + struct fixed31_32 sin_grph_hue; + struct fixed31_32 cos_grph_hue; + + initialize_fix_point_color_values( + core_color, sink_index, &grph_cont, &grph_sat, + &grph_bright, &sin_grph_hue, &cos_grph_hue); + + const struct fixed31_32 multiplier = + dal_fixed31_32_mul(grph_cont, grph_sat); + + yuv_matrix[0] = dal_fixed31_32_mul(ideal[0], grph_cont); + + yuv_matrix[1] = dal_fixed31_32_mul(ideal[1], grph_cont); + + yuv_matrix[2] = dal_fixed31_32_mul(ideal[2], grph_cont); + + yuv_matrix[4] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_add( + dal_fixed31_32_mul( + ideal[4], + cos_grph_hue), + dal_fixed31_32_mul( + ideal[8], + sin_grph_hue))); + + yuv_matrix[5] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_add( + dal_fixed31_32_mul( + ideal[5], + cos_grph_hue), + dal_fixed31_32_mul( + ideal[9], + sin_grph_hue))); + + yuv_matrix[6] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_add( + dal_fixed31_32_mul( + ideal[6], + cos_grph_hue), + dal_fixed31_32_mul( + ideal[10], + sin_grph_hue))); + + yuv_matrix[7] = ideal[7]; + + yuv_matrix[8] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_sub( + dal_fixed31_32_mul( + ideal[8], + cos_grph_hue), + dal_fixed31_32_mul( + ideal[4], + sin_grph_hue))); + + yuv_matrix[9] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_sub( + dal_fixed31_32_mul( + ideal[9], + cos_grph_hue), + dal_fixed31_32_mul( + ideal[5], + sin_grph_hue))); + + yuv_matrix[10] = dal_fixed31_32_mul( + multiplier, + dal_fixed31_32_sub( + dal_fixed31_32_mul( + ideal[10], + cos_grph_hue), + dal_fixed31_32_mul( + ideal[6], + sin_grph_hue))); + + yuv_matrix[11] = ideal[11]; + + if ((color_space == COLOR_SPACE_YCBCR601_LIMITED) || + (color_space == COLOR_SPACE_YCBCR709_LIMITED)) { + yuv_matrix[3] = dal_fixed31_32_add(ideal[3], grph_bright); + } else { + yuv_matrix[3] = dal_fixed31_32_add( + ideal[3], + dal_fixed31_32_mul( + grph_bright, + dal_fixed31_32_from_fraction(86, 100))); + } +} + +static void calculate_csc_matrix(struct core_color *core_color, + unsigned int sink_index, + enum dc_color_space color_space, + uint16_t *csc_matrix) +{ + struct fixed31_32 fixed_csc_matrix[12]; + switch (color_space) { + case COLOR_SPACE_SRGB: + calculate_rgb_matrix_legacy + (core_color, sink_index, fixed_csc_matrix); + convert_float_matrix_legacy + (csc_matrix, fixed_csc_matrix, 12); + break; + case COLOR_SPACE_SRGB_LIMITED: + calculate_rgb_limited_range_matrix(core_color, sink_index, + fixed_csc_matrix); + convert_float_matrix(csc_matrix, fixed_csc_matrix, 12); + break; + case COLOR_SPACE_YCBCR601: + case COLOR_SPACE_YCBCR709: + case COLOR_SPACE_YCBCR601_LIMITED: + case COLOR_SPACE_YCBCR709_LIMITED: + case COLOR_SPACE_YPBPR601: + case COLOR_SPACE_YPBPR709: + calculate_yuv_matrix(core_color, sink_index, color_space, + fixed_csc_matrix); + convert_float_matrix(csc_matrix, fixed_csc_matrix, 12); + break; + default: + calculate_rgb_matrix_legacy + (core_color, sink_index, fixed_csc_matrix); + convert_float_matrix_legacy + (csc_matrix, fixed_csc_matrix, 12); + break; + } +} + +struct mod_color *mod_color_create(struct dc *dc) +{ + int i = 0; + struct core_color *core_color = + dm_alloc(sizeof(struct core_color)); + struct core_dc *core_dc = DC_TO_CORE(dc); + struct persistent_data_flag flag; + + if (core_color == NULL) + goto fail_alloc_context; + + core_color->caps = dm_alloc(sizeof(struct sink_caps) * + MOD_COLOR_MAX_CONCURRENT_SINKS); + + if (core_color->caps == NULL) + goto fail_alloc_caps; + + for (i = 0; i < MOD_COLOR_MAX_CONCURRENT_SINKS; i++) + core_color->caps[i].sink = NULL; + + core_color->state = dm_alloc(sizeof(struct color_state) * + MOD_COLOR_MAX_CONCURRENT_SINKS); + + /*hardcoded to sRGB with 6500 color temperature*/ + for (i = 0; i < MOD_COLOR_MAX_CONCURRENT_SINKS; i++) { + core_color->state[i].source_gamut.blueX = 1500; + core_color->state[i].source_gamut.blueY = 600; + core_color->state[i].source_gamut.greenX = 3000; + core_color->state[i].source_gamut.greenY = 6000; + core_color->state[i].source_gamut.redX = 6400; + core_color->state[i].source_gamut.redY = 3300; + core_color->state[i].source_gamut.whiteX = 3127; + core_color->state[i].source_gamut.whiteY = 3290; + + core_color->state[i].destination_gamut.blueX = 1500; + core_color->state[i].destination_gamut.blueY = 600; + core_color->state[i].destination_gamut.greenX = 3000; + core_color->state[i].destination_gamut.greenY = 6000; + core_color->state[i].destination_gamut.redX = 6400; + core_color->state[i].destination_gamut.redY = 3300; + core_color->state[i].destination_gamut.whiteX = 3127; + core_color->state[i].destination_gamut.whiteY = 3290; + + core_color->state[i].custom_color_temperature = 6500; + + core_color->state[i].contrast.current = 100; + core_color->state[i].contrast.min = 0; + core_color->state[i].contrast.max = 200; + + core_color->state[i].saturation.current = 100; + core_color->state[i].saturation.min = 0; + core_color->state[i].saturation.max = 200; + + core_color->state[i].brightness.current = 0; + core_color->state[i].brightness.min = -100; + core_color->state[i].brightness.max = 100; + + core_color->state[i].hue.current = 0; + core_color->state[i].hue.min = -30; + core_color->state[i].hue.max = 30; + } + + if (core_color->state == NULL) + goto fail_alloc_state; + + core_color->num_sinks = 0; + + if (dc == NULL) + goto fail_construct; + + core_color->dc = dc; + + if (!check_dc_support(dc)) + goto fail_construct; + + /* Create initial module folder in registry for color adjustment */ + flag.save_per_edid = true; + flag.save_per_link = false; + + dm_write_persistent_data(core_dc->ctx, NULL, COLOR_REGISTRY_NAME, NULL, + NULL, 0, &flag); + + return &core_color->public; + +fail_construct: + dm_free(core_color->state); + +fail_alloc_state: + dm_free(core_color->caps); + +fail_alloc_caps: + dm_free(core_color); + +fail_alloc_context: + return NULL; +} + +void mod_color_destroy(struct mod_color *mod_color) +{ + if (mod_color != NULL) { + int i; + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + + dm_free(core_color->state); + + for (i = 0; i < core_color->num_sinks; i++) + dc_sink_release(core_color->caps[i].sink); + + dm_free(core_color->caps); + + dm_free(core_color); + } +} + +bool mod_color_add_sink(struct mod_color *mod_color, const struct dc_sink *sink) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + struct core_dc *core_dc = DC_TO_CORE(core_color->dc); + bool persistent_color_temp_enable; + int persistent_custom_color_temp = 0; + struct color_space_coordinates persistent_source_gamut; + struct color_space_coordinates persistent_destination_gamut; + int persistent_brightness; + int persistent_contrast; + int persistent_hue; + int persistent_saturation; + enum dc_quantization_range persistent_quantization_range; + struct persistent_data_flag flag; + + if (core_color->num_sinks < MOD_COLOR_MAX_CONCURRENT_SINKS) { + dc_sink_retain(sink); + core_color->caps[core_color->num_sinks].sink = sink; + core_color->state[core_color->num_sinks]. + user_enable_color_temperature = true; + + /* get persistent data from registry */ + flag.save_per_edid = true; + flag.save_per_link = false; + + + if (dm_read_persistent_data(core_dc->ctx, sink, + COLOR_REGISTRY_NAME, + "enablecolortempadj", + &persistent_color_temp_enable, + sizeof(bool), &flag)) + core_color->state[core_color->num_sinks]. + user_enable_color_temperature = + persistent_color_temp_enable; + else + core_color->state[core_color->num_sinks]. + user_enable_color_temperature = true; + + if (dm_read_persistent_data(core_dc->ctx, sink, + COLOR_REGISTRY_NAME, + "customcolortemp", + &persistent_custom_color_temp, + sizeof(int), &flag)) + core_color->state[core_color->num_sinks]. + custom_color_temperature + = persistent_custom_color_temp; + else + core_color->state[core_color->num_sinks]. + custom_color_temperature = 6500; + + if (dm_read_persistent_data(core_dc->ctx, sink, + COLOR_REGISTRY_NAME, + "sourcegamut", + &persistent_source_gamut, + sizeof(struct color_space_coordinates), + &flag)) { + memcpy(&core_color->state[core_color->num_sinks]. + source_gamut, &persistent_source_gamut, + sizeof(struct color_space_coordinates)); + } else { + core_color->state[core_color->num_sinks]. + source_gamut.blueX = 1500; + core_color->state[core_color->num_sinks]. + source_gamut.blueY = 600; + core_color->state[core_color->num_sinks]. + source_gamut.greenX = 3000; + core_color->state[core_color->num_sinks]. + source_gamut.greenY = 6000; + core_color->state[core_color->num_sinks]. + source_gamut.redX = 6400; + core_color->state[core_color->num_sinks]. + source_gamut.redY = 3300; + core_color->state[core_color->num_sinks]. + source_gamut.whiteX = 3127; + core_color->state[core_color->num_sinks]. + source_gamut.whiteY = 3290; + } + + if (dm_read_persistent_data(core_dc->ctx, sink, COLOR_REGISTRY_NAME, + "destgamut", + &persistent_destination_gamut, + sizeof(struct color_space_coordinates), + &flag)) { + memcpy(&core_color->state[core_color->num_sinks]. + destination_gamut, + &persistent_destination_gamut, + sizeof(struct color_space_coordinates)); + } else { + core_color->state[core_color->num_sinks]. + destination_gamut.blueX = 1500; + core_color->state[core_color->num_sinks]. + destination_gamut.blueY = 600; + core_color->state[core_color->num_sinks]. + destination_gamut.greenX = 3000; + core_color->state[core_color->num_sinks]. + destination_gamut.greenY = 6000; + core_color->state[core_color->num_sinks]. + destination_gamut.redX = 6400; + core_color->state[core_color->num_sinks]. + destination_gamut.redY = 3300; + core_color->state[core_color->num_sinks]. + destination_gamut.whiteX = 3127; + core_color->state[core_color->num_sinks]. + destination_gamut.whiteY = 3290; + } + + if (dm_read_persistent_data(core_dc->ctx, sink, COLOR_REGISTRY_NAME, + "brightness", + &persistent_brightness, + sizeof(int), &flag)) + core_color->state[core_color->num_sinks]. + brightness.current = persistent_brightness; + else + core_color->state[core_color->num_sinks]. + brightness.current = 0; + + if (dm_read_persistent_data(core_dc->ctx, sink, COLOR_REGISTRY_NAME, + "contrast", + &persistent_contrast, + sizeof(int), &flag)) + core_color->state[core_color->num_sinks]. + contrast.current = persistent_contrast; + else + core_color->state[core_color->num_sinks]. + contrast.current = 100; + + if (dm_read_persistent_data(core_dc->ctx, sink, COLOR_REGISTRY_NAME, + "hue", + &persistent_hue, + sizeof(int), &flag)) + core_color->state[core_color->num_sinks]. + hue.current = persistent_hue; + else + core_color->state[core_color->num_sinks]. + hue.current = 0; + + if (dm_read_persistent_data(core_dc->ctx, sink, COLOR_REGISTRY_NAME, + "saturation", + &persistent_saturation, + sizeof(int), &flag)) + core_color->state[core_color->num_sinks]. + saturation.current = persistent_saturation; + else + core_color->state[core_color->num_sinks]. + saturation.current = 100; + + if (dm_read_persistent_data(core_dc->ctx, sink, + COLOR_REGISTRY_NAME, + "preferred_quantization_range", + &persistent_quantization_range, + sizeof(int), &flag)) + core_color->state[core_color->num_sinks]. + preferred_quantization_range = + persistent_quantization_range; + else + core_color->state[core_color->num_sinks]. + preferred_quantization_range = QUANTIZATION_RANGE_FULL; + + core_color->num_sinks++; + return true; + } + return false; +} + +bool mod_color_remove_sink(struct mod_color *mod_color, + const struct dc_sink *sink) +{ + int i = 0, j = 0; + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + + for (i = 0; i < core_color->num_sinks; i++) { + if (core_color->caps[i].sink == sink) { + /* To remove this sink, shift everything after down */ + for (j = i; j < core_color->num_sinks - 1; j++) { + core_color->caps[j].sink = + core_color->caps[j + 1].sink; + + memcpy(&core_color->state[j], + &core_color->state[j + 1], + sizeof(struct color_state)); + } + + core_color->num_sinks--; + + dc_sink_release(sink); + + return true; + } + } + + return false; +} + +bool mod_color_update_gamut_to_stream(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + struct core_dc *core_dc = DC_TO_CORE(core_color->dc); + struct persistent_data_flag flag; + struct gamut_src_dst_matrix *matrix = + dm_alloc(sizeof(struct gamut_src_dst_matrix)); + + unsigned int stream_index, sink_index, j; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + + /* Write persistent data in registry*/ + flag.save_per_edid = true; + flag.save_per_link = false; + + dm_write_persistent_data(core_dc->ctx, + streams[stream_index]->sink, + COLOR_REGISTRY_NAME, + "sourcegamut", + &core_color->state[sink_index]. + source_gamut, + sizeof(struct color_space_coordinates), + &flag); + + dm_write_persistent_data(core_dc->ctx, + streams[stream_index]->sink, + COLOR_REGISTRY_NAME, + "destgamut", + &core_color->state[sink_index]. + destination_gamut, + sizeof(struct color_space_coordinates), + &flag); + + if (!build_gamut_remap_matrix + (core_color->state[sink_index].source_gamut, + matrix->rgbCoeffSrc, + matrix->whiteCoeffSrc)) + goto function_fail; + + if (!build_gamut_remap_matrix + (core_color->state[sink_index]. + destination_gamut, + matrix->rgbCoeffDst, matrix->whiteCoeffDst)) + goto function_fail; + + struct fixed31_32 gamut_result[12]; + struct fixed31_32 temp_matrix[9]; + + if (!gamut_to_color_matrix( + matrix->rgbCoeffDst, + matrix->whiteCoeffDst, + matrix->rgbCoeffSrc, + matrix->whiteCoeffSrc, + true, + temp_matrix)) + goto function_fail; + + gamut_result[0] = temp_matrix[0]; + gamut_result[1] = temp_matrix[1]; + gamut_result[2] = temp_matrix[2]; + gamut_result[3] = matrix->whiteCoeffSrc[0]; + gamut_result[4] = temp_matrix[3]; + gamut_result[5] = temp_matrix[4]; + gamut_result[6] = temp_matrix[5]; + gamut_result[7] = matrix->whiteCoeffSrc[1]; + gamut_result[8] = temp_matrix[6]; + gamut_result[9] = temp_matrix[7]; + gamut_result[10] = temp_matrix[8]; + gamut_result[11] = matrix->whiteCoeffSrc[2]; + + struct core_stream *core_stream = + DC_STREAM_TO_CORE + (streams[stream_index]); + + core_stream->public.gamut_remap_matrix.enable_remap = true; + + for (j = 0; j < 12; j++) + core_stream->public. + gamut_remap_matrix.matrix[j] = + gamut_result[j]; + } + + dm_free(matrix); + core_color->dc->stream_funcs.set_gamut_remap + (core_color->dc, streams, num_streams); + + return true; + +function_fail: + dm_free(matrix); + return false; +} + +bool mod_color_adjust_source_gamut(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + struct gamut_space_coordinates *input_gamut_coordinates, + struct white_point_coodinates *input_white_point_coordinates) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + + unsigned int stream_index, sink_index; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + + core_color->state[sink_index].source_gamut.blueX = + input_gamut_coordinates->blueX; + core_color->state[sink_index].source_gamut.blueY = + input_gamut_coordinates->blueY; + core_color->state[sink_index].source_gamut.greenX = + input_gamut_coordinates->greenX; + core_color->state[sink_index].source_gamut.greenY = + input_gamut_coordinates->greenY; + core_color->state[sink_index].source_gamut.redX = + input_gamut_coordinates->redX; + core_color->state[sink_index].source_gamut.redY = + input_gamut_coordinates->redY; + core_color->state[sink_index].source_gamut.whiteX = + input_white_point_coordinates->whiteX; + core_color->state[sink_index].source_gamut.whiteY = + input_white_point_coordinates->whiteY; + } + + if (!mod_color_update_gamut_to_stream(mod_color, streams, num_streams)) + return false; + + return true; +} + +bool mod_color_adjust_destination_gamut(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + struct gamut_space_coordinates *input_gamut_coordinates, + struct white_point_coodinates *input_white_point_coordinates) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + + unsigned int stream_index, sink_index; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + + core_color->state[sink_index].destination_gamut.blueX = + input_gamut_coordinates->blueX; + core_color->state[sink_index].destination_gamut.blueY = + input_gamut_coordinates->blueY; + core_color->state[sink_index].destination_gamut.greenX = + input_gamut_coordinates->greenX; + core_color->state[sink_index].destination_gamut.greenY = + input_gamut_coordinates->greenY; + core_color->state[sink_index].destination_gamut.redX = + input_gamut_coordinates->redX; + core_color->state[sink_index].destination_gamut.redY = + input_gamut_coordinates->redY; + core_color->state[sink_index].destination_gamut.whiteX = + input_white_point_coordinates->whiteX; + core_color->state[sink_index].destination_gamut.whiteY = + input_white_point_coordinates->whiteY; + } + + if (!mod_color_update_gamut_to_stream(mod_color, streams, num_streams)) + return false; + + return true; +} + +bool mod_color_set_white_point(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + struct white_point_coodinates *white_point) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + + unsigned int stream_index, sink_index; + + for (stream_index = 0; stream_index < num_streams; + stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + core_color->state[sink_index].source_gamut.whiteX = + white_point->whiteX; + core_color->state[sink_index].source_gamut.whiteY = + white_point->whiteY; + } + + if (!mod_color_update_gamut_to_stream(mod_color, streams, num_streams)) + return false; + + return true; +} + +bool mod_color_set_user_enable(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + bool user_enable) +{ + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + struct core_dc *core_dc = DC_TO_CORE(core_color->dc); + struct persistent_data_flag flag; + unsigned int stream_index, sink_index; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + core_color->state[sink_index].user_enable_color_temperature + = user_enable; + + /* Write persistent data in registry*/ + flag.save_per_edid = true; + flag.save_per_link = false; + + dm_write_persistent_data(core_dc->ctx, + streams[stream_index]->sink, + COLOR_REGISTRY_NAME, + "enablecolortempadj", + &user_enable, + sizeof(bool), + &flag); + } + return true; +} + +bool mod_color_get_user_enable(struct mod_color *mod_color, + const struct dc_sink *sink, + bool *user_enable) +{ + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + + unsigned int sink_index = sink_index_from_sink(core_color, sink); + + *user_enable = core_color->state[sink_index]. + user_enable_color_temperature; + + return true; +} + +bool mod_color_get_custom_color_temperature(struct mod_color *mod_color, + const struct dc_sink *sink, + int *color_temperature) +{ + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + + unsigned int sink_index = sink_index_from_sink(core_color, sink); + + *color_temperature = core_color->state[sink_index]. + custom_color_temperature; + + return true; +} + +bool mod_color_set_custom_color_temperature(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int color_temperature) +{ + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + struct core_dc *core_dc = DC_TO_CORE(core_color->dc); + struct persistent_data_flag flag; + unsigned int stream_index, sink_index; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + core_color->state[sink_index].custom_color_temperature + = color_temperature; + + /* Write persistent data in registry*/ + flag.save_per_edid = true; + flag.save_per_link = false; + + dm_write_persistent_data(core_dc->ctx, + streams[stream_index]->sink, + COLOR_REGISTRY_NAME, + "customcolortemp", + &color_temperature, + sizeof(int), + &flag); + } + return true; +} + +bool mod_color_get_color_saturation(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_range *color_saturation) +{ + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + + unsigned int sink_index = sink_index_from_sink(core_color, sink); + + *color_saturation = core_color->state[sink_index].saturation; + + return true; +} + +bool mod_color_get_color_contrast(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_range *color_contrast) +{ + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + + unsigned int sink_index = sink_index_from_sink(core_color, sink); + + *color_contrast = core_color->state[sink_index].contrast; + + return true; +} + +bool mod_color_get_color_brightness(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_range *color_brightness) +{ + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + + unsigned int sink_index = sink_index_from_sink(core_color, sink); + + *color_brightness = core_color->state[sink_index].brightness; + + return true; +} + +bool mod_color_get_color_hue(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_range *color_hue) +{ + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + + unsigned int sink_index = sink_index_from_sink(core_color, sink); + + *color_hue = core_color->state[sink_index].hue; + + return true; +} + +bool mod_color_get_source_gamut(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_space_coordinates *source_gamut) +{ + struct core_color *core_color = + MOD_COLOR_TO_CORE(mod_color); + + unsigned int sink_index = sink_index_from_sink(core_color, sink); + + *source_gamut = core_color->state[sink_index].source_gamut; + + return true; +} + +bool mod_color_notify_mode_change(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + + struct gamut_src_dst_matrix *matrix = + dm_alloc(sizeof(struct gamut_src_dst_matrix)); + + unsigned int stream_index, sink_index, j; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + + if (!build_gamut_remap_matrix + (core_color->state[sink_index].source_gamut, + matrix->rgbCoeffSrc, + matrix->whiteCoeffSrc)) + goto function_fail; + + if (!build_gamut_remap_matrix + (core_color->state[sink_index]. + destination_gamut, + matrix->rgbCoeffDst, matrix->whiteCoeffDst)) + goto function_fail; + + struct fixed31_32 gamut_result[12]; + struct fixed31_32 temp_matrix[9]; + + if (!gamut_to_color_matrix( + matrix->rgbCoeffDst, + matrix->whiteCoeffDst, + matrix->rgbCoeffSrc, + matrix->whiteCoeffSrc, + true, + temp_matrix)) + goto function_fail; + + gamut_result[0] = temp_matrix[0]; + gamut_result[1] = temp_matrix[1]; + gamut_result[2] = temp_matrix[2]; + gamut_result[3] = matrix->whiteCoeffSrc[0]; + gamut_result[4] = temp_matrix[3]; + gamut_result[5] = temp_matrix[4]; + gamut_result[6] = temp_matrix[5]; + gamut_result[7] = matrix->whiteCoeffSrc[1]; + gamut_result[8] = temp_matrix[6]; + gamut_result[9] = temp_matrix[7]; + gamut_result[10] = temp_matrix[8]; + gamut_result[11] = matrix->whiteCoeffSrc[2]; + + + struct core_stream *core_stream = + DC_STREAM_TO_CORE + (streams[stream_index]); + + core_stream->public.gamut_remap_matrix.enable_remap = true; + + for (j = 0; j < 12; j++) + core_stream->public. + gamut_remap_matrix.matrix[j] = + gamut_result[j]; + + calculate_csc_matrix(core_color, sink_index, + core_stream->public.output_color_space, + core_stream->public.csc_color_matrix.matrix); + + core_stream->public.csc_color_matrix.enable_adjustment = true; + } + + dm_free(matrix); + + return true; + +function_fail: + dm_free(matrix); + return false; +} + +bool mod_color_set_brightness(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int brightness_value) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + struct core_dc *core_dc = DC_TO_CORE(core_color->dc); + struct persistent_data_flag flag; + unsigned int stream_index, sink_index; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + + struct core_stream *core_stream = + DC_STREAM_TO_CORE + (streams[stream_index]); + + core_color->state[sink_index].brightness.current = + brightness_value; + + calculate_csc_matrix(core_color, sink_index, + core_stream->public.output_color_space, + core_stream->public.csc_color_matrix.matrix); + + core_stream->public.csc_color_matrix.enable_adjustment = true; + + /* Write persistent data in registry*/ + flag.save_per_edid = true; + flag.save_per_link = false; + dm_write_persistent_data(core_dc->ctx, + streams[stream_index]->sink, + COLOR_REGISTRY_NAME, + "brightness", + &brightness_value, + sizeof(int), + &flag); + } + + core_color->dc->stream_funcs.set_gamut_remap + (core_color->dc, streams, num_streams); + + return true; +} + +bool mod_color_set_contrast(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int contrast_value) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + struct core_dc *core_dc = DC_TO_CORE(core_color->dc); + struct persistent_data_flag flag; + unsigned int stream_index, sink_index; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + + struct core_stream *core_stream = + DC_STREAM_TO_CORE + (streams[stream_index]); + + core_color->state[sink_index].contrast.current = + contrast_value; + + calculate_csc_matrix(core_color, sink_index, + core_stream->public.output_color_space, + core_stream->public.csc_color_matrix.matrix); + + core_stream->public.csc_color_matrix.enable_adjustment = true; + + /* Write persistent data in registry*/ + flag.save_per_edid = true; + flag.save_per_link = false; + dm_write_persistent_data(core_dc->ctx, + streams[stream_index]->sink, + COLOR_REGISTRY_NAME, + "contrast", + &contrast_value, + sizeof(int), + &flag); + } + + core_color->dc->stream_funcs.set_gamut_remap + (core_color->dc, streams, num_streams); + + return true; +} + +bool mod_color_set_hue(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int hue_value) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + struct core_dc *core_dc = DC_TO_CORE(core_color->dc); + struct persistent_data_flag flag; + unsigned int stream_index, sink_index; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + + struct core_stream *core_stream = + DC_STREAM_TO_CORE + (streams[stream_index]); + + core_color->state[sink_index].hue.current = hue_value; + + calculate_csc_matrix(core_color, sink_index, + core_stream->public.output_color_space, + core_stream->public.csc_color_matrix.matrix); + + core_stream->public.csc_color_matrix.enable_adjustment = true; + + /* Write persistent data in registry*/ + flag.save_per_edid = true; + flag.save_per_link = false; + dm_write_persistent_data(core_dc->ctx, + streams[stream_index]->sink, + COLOR_REGISTRY_NAME, + "hue", + &hue_value, + sizeof(int), + &flag); + } + + core_color->dc->stream_funcs.set_gamut_remap + (core_color->dc, streams, num_streams); + + return true; +} + +bool mod_color_set_saturation(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int saturation_value) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + struct core_dc *core_dc = DC_TO_CORE(core_color->dc); + struct persistent_data_flag flag; + unsigned int stream_index, sink_index; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + sink_index = sink_index_from_sink(core_color, + streams[stream_index]->sink); + + struct core_stream *core_stream = + DC_STREAM_TO_CORE + (streams[stream_index]); + + core_color->state[sink_index].saturation.current = + saturation_value; + + calculate_csc_matrix(core_color, sink_index, + core_stream->public.output_color_space, + core_stream->public.csc_color_matrix.matrix); + + core_stream->public.csc_color_matrix.enable_adjustment = true; + + /* Write persistent data in registry*/ + flag.save_per_edid = true; + flag.save_per_link = false; + dm_write_persistent_data(core_dc->ctx, + streams[stream_index]->sink, + COLOR_REGISTRY_NAME, + "saturation", + &saturation_value, + sizeof(int), + &flag); + } + + core_color->dc->stream_funcs.set_gamut_remap + (core_color->dc, streams, num_streams); + + return true; +} + +bool mod_color_set_preferred_quantization_range(struct mod_color *mod_color, + const struct dc_sink *sink, + enum dc_quantization_range quantization_range) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + struct core_dc *core_dc = DC_TO_CORE(core_color->dc); + struct persistent_data_flag flag; + unsigned int sink_index; + + sink_index = sink_index_from_sink(core_color, sink); + if (core_color->state[sink_index]. + preferred_quantization_range != quantization_range) { + core_color->state[sink_index].preferred_quantization_range = + quantization_range; + flag.save_per_edid = true; + flag.save_per_link = false; + dm_write_persistent_data(core_dc->ctx, + sink, + COLOR_REGISTRY_NAME, + "quantization_range", + &quantization_range, + sizeof(int), + &flag); + } + + return true; +} + +bool mod_color_get_preferred_quantization_range(struct mod_color *mod_color, + const struct dc_sink *sink, + enum dc_quantization_range *quantization_range) +{ + struct core_color *core_color = MOD_COLOR_TO_CORE(mod_color); + unsigned int sink_index; + + sink_index = sink_index_from_sink(core_color, sink); + *quantization_range = core_color->state[sink_index]. + preferred_quantization_range; + return true; +} diff --git a/drivers/gpu/drm/amd/display/modules/freesync/Makefile b/drivers/gpu/drm/amd/display/modules/freesync/Makefile new file mode 100644 index 000000000000..db8e0ff6d7a9 --- /dev/null +++ b/drivers/gpu/drm/amd/display/modules/freesync/Makefile @@ -0,0 +1,10 @@ +# +# Makefile for the 'freesync' sub-module of DAL. +# + +FREESYNC = freesync.o + +AMD_DAL_FREESYNC = $(addprefix $(AMDDALPATH)/modules/freesync/,$(FREESYNC)) +#$(info ************ DAL-FREE SYNC_MAKEFILE ************) + +AMD_DISPLAY_FILES += $(AMD_DAL_FREESYNC) diff --git a/drivers/gpu/drm/amd/display/modules/freesync/freesync.c b/drivers/gpu/drm/amd/display/modules/freesync/freesync.c new file mode 100644 index 000000000000..eb912baa0169 --- /dev/null +++ b/drivers/gpu/drm/amd/display/modules/freesync/freesync.c @@ -0,0 +1,1158 @@ +/* + * Copyright 2016 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: AMD + * + */ + +#include "dm_services.h" +#include "dc.h" +#include "mod_freesync.h" +#include "core_types.h" +#include "core_dc.h" + +#define MOD_FREESYNC_MAX_CONCURRENT_STREAMS 32 + +/* Refresh rate ramp at a fixed rate of 65 Hz/second */ +#define STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME ((1000 / 60) * 65) +/* Number of elements in the render times cache array */ +#define RENDER_TIMES_MAX_COUNT 20 +/* Threshold to exit BTR (to avoid frequent enter-exits at the lower limit) */ +#define BTR_EXIT_MARGIN 2000 + +#define FREESYNC_REGISTRY_NAME "freesync_v1" + +struct gradual_static_ramp { + bool ramp_is_active; + bool ramp_direction_is_up; + unsigned int ramp_current_frame_duration_in_ns; +}; + +struct time_cache { + /* video (48Hz feature) related */ + unsigned int update_duration_in_ns; + + /* BTR/fixed refresh related */ + unsigned int prev_time_stamp_in_us; + + unsigned int min_render_time_in_us; + unsigned int max_render_time_in_us; + + unsigned int render_times_index; + unsigned int render_times[RENDER_TIMES_MAX_COUNT]; +}; + +struct below_the_range { + bool btr_active; + bool program_btr; + + unsigned int mid_point_in_us; + + unsigned int inserted_frame_duration_in_us; + unsigned int frames_to_insert; + unsigned int frame_counter; +}; + +struct fixed_refresh { + bool fixed_refresh_active; + bool program_fixed_refresh; +}; + +struct freesync_state { + bool fullscreen; + bool static_screen; + bool video; + + unsigned int nominal_refresh_rate_in_micro_hz; + bool windowed_fullscreen; + + struct time_cache time; + + struct gradual_static_ramp static_ramp; + struct below_the_range btr; + struct fixed_refresh fixed_refresh; +}; + +struct freesync_entity { + const struct dc_stream *stream; + struct mod_freesync_caps *caps; + struct freesync_state state; + struct mod_freesync_user_enable user_enable; +}; + +struct core_freesync { + struct mod_freesync public; + struct dc *dc; + struct freesync_entity *map; + int num_entities; +}; + +#define MOD_FREESYNC_TO_CORE(mod_freesync)\ + container_of(mod_freesync, struct core_freesync, public) + +static bool check_dc_support(const struct dc *dc) +{ + if (dc->stream_funcs.adjust_vmin_vmax == NULL) + return false; + + return true; +} + +struct mod_freesync *mod_freesync_create(struct dc *dc) +{ + struct core_freesync *core_freesync = + dm_alloc(sizeof(struct core_freesync)); + + struct core_dc *core_dc = DC_TO_CORE(dc); + + struct persistent_data_flag flag; + + int i = 0; + + if (core_freesync == NULL) + goto fail_alloc_context; + + core_freesync->map = dm_alloc(sizeof(struct freesync_entity) * + MOD_FREESYNC_MAX_CONCURRENT_STREAMS); + + if (core_freesync->map == NULL) + goto fail_alloc_map; + + for (i = 0; i < MOD_FREESYNC_MAX_CONCURRENT_STREAMS; i++) + core_freesync->map[i].stream = NULL; + + core_freesync->num_entities = 0; + + if (dc == NULL) + goto fail_construct; + + core_freesync->dc = dc; + + if (!check_dc_support(dc)) + goto fail_construct; + + /* Create initial module folder in registry for freesync enable data */ + flag.save_per_edid = true; + flag.save_per_link = false; + dm_write_persistent_data(core_dc->ctx, NULL, FREESYNC_REGISTRY_NAME, NULL, NULL, + 0, &flag); + + return &core_freesync->public; + +fail_construct: + dm_free(core_freesync->map); + +fail_alloc_map: + dm_free(core_freesync); + +fail_alloc_context: + return NULL; +} + +void mod_freesync_destroy(struct mod_freesync *mod_freesync) +{ + if (mod_freesync != NULL) { + int i; + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + + for (i = 0; i < core_freesync->num_entities; i++) + if (core_freesync->map[i].stream) + dc_stream_release(core_freesync->map[i].stream); + + dm_free(core_freesync->map); + + dm_free(core_freesync); + } +} + +/* Given a specific dc_stream* this function finds its equivalent + * on the core_freesync->map and returns the corresponding index + */ +static unsigned int map_index_from_stream(struct core_freesync *core_freesync, + const struct dc_stream *stream) +{ + unsigned int index = 0; + + for (index = 0; index < core_freesync->num_entities; index++) { + if (core_freesync->map[index].stream == stream) { + return index; + } + } + /* Could not find stream requested */ + ASSERT(false); + return index; +} + +bool mod_freesync_add_stream(struct mod_freesync *mod_freesync, + const struct dc_stream *stream, struct mod_freesync_caps *caps) +{ + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + struct core_stream *core_stream = + DC_STREAM_TO_CORE(stream); + struct core_dc *core_dc = DC_TO_CORE(core_freesync->dc); + + int persistent_freesync_enable = 0; + struct persistent_data_flag flag; + + flag.save_per_edid = true; + flag.save_per_link = false; + + if (core_freesync->num_entities < MOD_FREESYNC_MAX_CONCURRENT_STREAMS) { + + dc_stream_retain(stream); + + core_freesync->map[core_freesync->num_entities].stream = stream; + core_freesync->map[core_freesync->num_entities].caps = caps; + + core_freesync->map[core_freesync->num_entities].state. + fullscreen = false; + core_freesync->map[core_freesync->num_entities].state. + static_screen = false; + core_freesync->map[core_freesync->num_entities].state. + video = false; + core_freesync->map[core_freesync->num_entities].state.time. + update_duration_in_ns = 0; + core_freesync->map[core_freesync->num_entities].state. + static_ramp.ramp_is_active = false; + + /* get persistent data from registry */ + if (dm_read_persistent_data(core_dc->ctx, stream->sink, + FREESYNC_REGISTRY_NAME, + "userenable", &persistent_freesync_enable, + sizeof(int), &flag)) { + core_freesync->map[core_freesync->num_entities].user_enable. + enable_for_gaming = + (persistent_freesync_enable & 1) ? true : false; + core_freesync->map[core_freesync->num_entities].user_enable. + enable_for_static = + (persistent_freesync_enable & 2) ? true : false; + core_freesync->map[core_freesync->num_entities].user_enable. + enable_for_video = + (persistent_freesync_enable & 4) ? true : false; + } else { + core_freesync->map[core_freesync->num_entities].user_enable. + enable_for_gaming = false; + core_freesync->map[core_freesync->num_entities].user_enable. + enable_for_static = false; + core_freesync->map[core_freesync->num_entities].user_enable. + enable_for_video = false; + } + + if (caps->supported) + core_stream->public.ignore_msa_timing_param = 1; + + core_freesync->num_entities++; + return true; + } + return false; +} + +bool mod_freesync_remove_stream(struct mod_freesync *mod_freesync, + const struct dc_stream *stream) +{ + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + + int i = 0; + unsigned int index = map_index_from_stream(core_freesync, stream); + dc_stream_release(core_freesync->map[index].stream); + core_freesync->map[index].stream = NULL; + /* To remove this entity, shift everything after down */ + for (i = index; i < core_freesync->num_entities - 1; i++) + core_freesync->map[i] = core_freesync->map[i + 1]; + core_freesync->num_entities--; + return true; +} + +static void update_stream_freesync_context(struct core_freesync *core_freesync, + const struct dc_stream *stream) +{ + unsigned int index; + struct freesync_context *ctx; + struct core_stream *core_stream; + + core_stream = DC_STREAM_TO_CORE(stream); + ctx = &core_stream->public.freesync_ctx; + + index = map_index_from_stream(core_freesync, stream); + + ctx->supported = core_freesync->map[index].caps->supported; + ctx->enabled = (core_freesync->map[index].user_enable.enable_for_gaming || + core_freesync->map[index].user_enable.enable_for_video || + core_freesync->map[index].user_enable.enable_for_static); + ctx->active = (core_freesync->map[index].state.fullscreen || + core_freesync->map[index].state.video || + core_freesync->map[index].state.static_ramp.ramp_is_active); + ctx->min_refresh_in_micro_hz = + core_freesync->map[index].caps->min_refresh_in_micro_hz; + ctx->nominal_refresh_in_micro_hz = core_freesync-> + map[index].state.nominal_refresh_rate_in_micro_hz; + +} + +static void update_stream(struct core_freesync *core_freesync, + const struct dc_stream *stream) +{ + struct core_stream *core_stream = DC_STREAM_TO_CORE(stream); + + unsigned int index = map_index_from_stream(core_freesync, stream); + if (core_freesync->map[index].caps->supported) { + core_stream->public.ignore_msa_timing_param = 1; + update_stream_freesync_context(core_freesync, stream); + } +} + +static void calc_vmin_vmax(struct core_freesync *core_freesync, + const struct dc_stream *stream, int *vmin, int *vmax) +{ + unsigned int min_frame_duration_in_ns = 0, max_frame_duration_in_ns = 0; + unsigned int index = map_index_from_stream(core_freesync, stream); + + min_frame_duration_in_ns = ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + core_freesync->map[index].state. + nominal_refresh_rate_in_micro_hz))); + max_frame_duration_in_ns = ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + core_freesync->map[index].caps->min_refresh_in_micro_hz))); + + *vmax = div64_u64(div64_u64(((unsigned long long)( + max_frame_duration_in_ns) * stream->timing.pix_clk_khz), + stream->timing.h_total), 1000000); + *vmin = div64_u64(div64_u64(((unsigned long long)( + min_frame_duration_in_ns) * stream->timing.pix_clk_khz), + stream->timing.h_total), 1000000); +} + +static void calc_v_total_from_duration(const struct dc_stream *stream, + unsigned int duration_in_ns, int *v_total_nominal) +{ + *v_total_nominal = div64_u64(div64_u64(((unsigned long long)( + duration_in_ns) * stream->timing.pix_clk_khz), + stream->timing.h_total), 1000000); +} + +static void calc_v_total_for_static_ramp(struct core_freesync *core_freesync, + const struct dc_stream *stream, + unsigned int index, int *v_total) +{ + unsigned int frame_duration = 0; + + struct gradual_static_ramp *static_ramp_variables = + &core_freesync->map[index].state.static_ramp; + + /* Calc ratio between new and current frame duration with 3 digit */ + unsigned int frame_duration_ratio = div64_u64(1000000, + (1000 + div64_u64(((unsigned long long)( + STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) * + static_ramp_variables->ramp_current_frame_duration_in_ns), + 1000000000))); + + /* Calculate delta between new and current frame duration in ns */ + unsigned int frame_duration_delta = div64_u64(((unsigned long long)( + static_ramp_variables->ramp_current_frame_duration_in_ns) * + (1000 - frame_duration_ratio)), 1000); + + /* Adjust frame duration delta based on ratio between current and + * standard frame duration (frame duration at 60 Hz refresh rate). + */ + unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)( + frame_duration_delta) * static_ramp_variables-> + ramp_current_frame_duration_in_ns), 16666666); + + /* Going to a higher refresh rate (lower frame duration) */ + if (static_ramp_variables->ramp_direction_is_up) { + /* reduce frame duration */ + static_ramp_variables->ramp_current_frame_duration_in_ns -= + ramp_rate_interpolated; + + /* min frame duration */ + frame_duration = ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + core_freesync->map[index].state. + nominal_refresh_rate_in_micro_hz))); + + /* adjust for frame duration below min */ + if (static_ramp_variables->ramp_current_frame_duration_in_ns <= + frame_duration) { + + static_ramp_variables->ramp_is_active = false; + static_ramp_variables-> + ramp_current_frame_duration_in_ns = + frame_duration; + } + /* Going to a lower refresh rate (larger frame duration) */ + } else { + /* increase frame duration */ + static_ramp_variables->ramp_current_frame_duration_in_ns += + ramp_rate_interpolated; + + /* max frame duration */ + frame_duration = ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + core_freesync->map[index].caps->min_refresh_in_micro_hz))); + + /* adjust for frame duration above max */ + if (static_ramp_variables->ramp_current_frame_duration_in_ns >= + frame_duration) { + + static_ramp_variables->ramp_is_active = false; + static_ramp_variables-> + ramp_current_frame_duration_in_ns = + frame_duration; + } + } + + calc_v_total_from_duration(stream, static_ramp_variables-> + ramp_current_frame_duration_in_ns, v_total); +} + +static void reset_freesync_state_variables(struct freesync_state* state) +{ + state->static_ramp.ramp_is_active = false; + if (state->nominal_refresh_rate_in_micro_hz) + state->static_ramp.ramp_current_frame_duration_in_ns = + ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + state->nominal_refresh_rate_in_micro_hz))); + + state->btr.btr_active = false; + state->btr.frame_counter = 0; + state->btr.frames_to_insert = 0; + state->btr.inserted_frame_duration_in_us = 0; + state->btr.program_btr = false; + + state->fixed_refresh.fixed_refresh_active = false; + state->fixed_refresh.program_fixed_refresh = false; +} +/* + * Sets freesync mode on a stream depending on current freesync state. + */ +static bool set_freesync_on_streams(struct core_freesync *core_freesync, + const struct dc_stream **streams, int num_streams) +{ + int v_total_nominal = 0, v_total_min = 0, v_total_max = 0; + unsigned int stream_idx, map_index = 0; + struct freesync_state *state; + + if (num_streams == 0 || streams == NULL || num_streams > 1) + return false; + + for (stream_idx = 0; stream_idx < num_streams; stream_idx++) { + + map_index = map_index_from_stream(core_freesync, + streams[stream_idx]); + + state = &core_freesync->map[map_index].state; + + if (core_freesync->map[map_index].caps->supported) { + + /* Fullscreen has the topmost priority. If the + * fullscreen bit is set, we are in a fullscreen + * application where it should not matter if it is + * static screen. We should not check the static_screen + * or video bit. + * + * Special cases of fullscreen include btr and fixed + * refresh. We program btr on every flip and involves + * programming full range right before the last inserted frame. + * However, we do not want to program the full freesync range + * when fixed refresh is active, because we only program + * that logic once and this will override it. + */ + if (core_freesync->map[map_index].user_enable. + enable_for_gaming == true && + state->fullscreen == true && + state->fixed_refresh.fixed_refresh_active == false) { + /* Enable freesync */ + + calc_vmin_vmax(core_freesync, + streams[stream_idx], + &v_total_min, &v_total_max); + + /* Update the freesync context for the stream */ + update_stream_freesync_context(core_freesync, + streams[stream_idx]); + + core_freesync->dc->stream_funcs. + adjust_vmin_vmax(core_freesync->dc, streams, + num_streams, v_total_min, + v_total_max); + + return true; + + } else if (core_freesync->map[map_index].user_enable. + enable_for_video && state->video == true) { + /* Enable 48Hz feature */ + + calc_v_total_from_duration(streams[stream_idx], + state->time.update_duration_in_ns, + &v_total_nominal); + + /* Program only if v_total_nominal is in range*/ + if (v_total_nominal >= + streams[stream_idx]->timing.v_total) { + + /* Update the freesync context for + * the stream + */ + update_stream_freesync_context( + core_freesync, + streams[stream_idx]); + + core_freesync->dc->stream_funcs. + adjust_vmin_vmax( + core_freesync->dc, streams, + num_streams, v_total_nominal, + v_total_nominal); + } + return true; + + } else { + /* Disable freesync */ + v_total_nominal = streams[stream_idx]-> + timing.v_total; + + /* Update the freesync context for + * the stream + */ + update_stream_freesync_context( + core_freesync, + streams[stream_idx]); + + core_freesync->dc->stream_funcs. + adjust_vmin_vmax( + core_freesync->dc, streams, + num_streams, v_total_nominal, + v_total_nominal); + + /* Reset the cached variables */ + reset_freesync_state_variables(state); + + return true; + } + } else { + /* Disable freesync */ + v_total_nominal = streams[stream_idx]-> + timing.v_total; + /* + * we have to reset drr always even sink does + * not support freesync because a former stream has + * be programmed + */ + core_freesync->dc->stream_funcs. + adjust_vmin_vmax( + core_freesync->dc, streams, + num_streams, v_total_nominal, + v_total_nominal); + /* Reset the cached variables */ + reset_freesync_state_variables(state); + } + + } + + return false; +} + +static void set_static_ramp_variables(struct core_freesync *core_freesync, + unsigned int index, bool enable_static_screen) +{ + unsigned int frame_duration = 0; + + struct gradual_static_ramp *static_ramp_variables = + &core_freesync->map[index].state.static_ramp; + + /* If ramp is not active, set initial frame duration depending on + * whether we are enabling/disabling static screen mode. If the ramp is + * already active, ramp should continue in the opposite direction + * starting with the current frame duration + */ + if (!static_ramp_variables->ramp_is_active) { + + static_ramp_variables->ramp_is_active = true; + + if (enable_static_screen == true) { + /* Going to lower refresh rate, so start from max + * refresh rate (min frame duration) + */ + frame_duration = ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + core_freesync->map[index].state. + nominal_refresh_rate_in_micro_hz))); + } else { + /* Going to higher refresh rate, so start from min + * refresh rate (max frame duration) + */ + frame_duration = ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + core_freesync->map[index].caps->min_refresh_in_micro_hz))); + } + + static_ramp_variables-> + ramp_current_frame_duration_in_ns = frame_duration; + } + + /* If we are ENABLING static screen, refresh rate should go DOWN. + * If we are DISABLING static screen, refresh rate should go UP. + */ + static_ramp_variables->ramp_direction_is_up = !enable_static_screen; +} + +void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams) +{ + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + + unsigned int index, v_total = 0; + struct freesync_state *state; + + if (core_freesync->num_entities == 0) + return; + + index = map_index_from_stream(core_freesync, + streams[0]); + + if (core_freesync->map[index].caps->supported == false) + return; + + state = &core_freesync->map[index].state; + + /* Below the Range Logic */ + + /* Only execute if in fullscreen mode */ + if (state->fullscreen == true && + core_freesync->map[index].user_enable.enable_for_gaming) { + + if (state->btr.btr_active) + if (state->btr.frame_counter > 0) + + state->btr.frame_counter--; + + if (state->btr.frame_counter == 1) { + + /* Restore FreeSync */ + set_freesync_on_streams(core_freesync, streams, + num_streams); + } + } + + /* If in fullscreen freesync mode or in video, do not program + * static screen ramp values + */ + if (state->fullscreen == true || state->video == true) { + + state->static_ramp.ramp_is_active = false; + + return; + } + + /* Gradual Static Screen Ramping Logic */ + + /* Execute if ramp is active and user enabled freesync static screen*/ + if (state->static_ramp.ramp_is_active && + core_freesync->map[index].user_enable.enable_for_static) { + + calc_v_total_for_static_ramp(core_freesync, streams[0], + index, &v_total); + + /* Update the freesync context for the stream */ + update_stream_freesync_context(core_freesync, streams[0]); + + /* Program static screen ramp values */ + core_freesync->dc->stream_funcs.adjust_vmin_vmax( + core_freesync->dc, streams, + num_streams, v_total, + v_total); + } +} + +void mod_freesync_update_state(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams, + struct mod_freesync_params *freesync_params) +{ + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + bool freesync_program_required = false; + unsigned int stream_index; + struct freesync_state *state; + + if (core_freesync->num_entities == 0) + return; + + for(stream_index = 0; stream_index < num_streams; stream_index++) { + + unsigned int map_index = map_index_from_stream(core_freesync, + streams[stream_index]); + + state = &core_freesync->map[map_index].state; + + switch (freesync_params->state){ + case FREESYNC_STATE_FULLSCREEN: + state->fullscreen = freesync_params->enable; + freesync_program_required = true; + state->windowed_fullscreen = + freesync_params->windowed_fullscreen; + break; + case FREESYNC_STATE_STATIC_SCREEN: + /* Static screen ramp is only enabled for embedded + * panels. Also change core variables only if there + * is a change. + */ + if (dc_is_embedded_signal( + streams[stream_index]->sink->sink_signal) && + state->static_screen != + freesync_params->enable) { + + /* Change the state flag */ + state->static_screen = freesync_params->enable; + + /* Change static screen ramp variables */ + set_static_ramp_variables(core_freesync, + map_index, + freesync_params->enable); + } + /* We program the ramp starting next VUpdate */ + break; + case FREESYNC_STATE_VIDEO: + /* Change core variables only if there is a change*/ + if(freesync_params->update_duration_in_ns != + state->time.update_duration_in_ns) { + + state->video = freesync_params->enable; + state->time.update_duration_in_ns = + freesync_params->update_duration_in_ns; + + freesync_program_required = true; + } + break; + } + } + + if (freesync_program_required) + /* Program freesync according to current state*/ + set_freesync_on_streams(core_freesync, streams, num_streams); +} + + +bool mod_freesync_get_state(struct mod_freesync *mod_freesync, + const struct dc_stream *stream, + struct mod_freesync_params *freesync_params) +{ + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + + unsigned int index = map_index_from_stream(core_freesync, stream); + + if (core_freesync->map[index].state.fullscreen) { + freesync_params->state = FREESYNC_STATE_FULLSCREEN; + freesync_params->enable = true; + } else if (core_freesync->map[index].state.static_screen) { + freesync_params->state = FREESYNC_STATE_STATIC_SCREEN; + freesync_params->enable = true; + } else if (core_freesync->map[index].state.video) { + freesync_params->state = FREESYNC_STATE_VIDEO; + freesync_params->enable = true; + } else { + freesync_params->state = FREESYNC_STATE_NONE; + freesync_params->enable = false; + } + + freesync_params->update_duration_in_ns = + core_freesync->map[index].state.time.update_duration_in_ns; + + return true; +} + +bool mod_freesync_set_user_enable(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams, + struct mod_freesync_user_enable *user_enable) +{ + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + struct core_dc *core_dc = DC_TO_CORE(core_freesync->dc); + + unsigned int stream_index, map_index; + int persistent_data = 0; + struct persistent_data_flag flag; + + flag.save_per_edid = true; + flag.save_per_link = false; + + for(stream_index = 0; stream_index < num_streams; + stream_index++){ + + map_index = map_index_from_stream(core_freesync, + streams[stream_index]); + + core_freesync->map[map_index].user_enable = *user_enable; + + /* Write persistent data in registry*/ + if (core_freesync->map[map_index].user_enable. + enable_for_gaming) + persistent_data = persistent_data | 1; + if (core_freesync->map[map_index].user_enable. + enable_for_static) + persistent_data = persistent_data | 2; + if (core_freesync->map[map_index].user_enable. + enable_for_video) + persistent_data = persistent_data | 4; + + dm_write_persistent_data(core_dc->ctx, + streams[stream_index]->sink, + FREESYNC_REGISTRY_NAME, + "userenable", + &persistent_data, + sizeof(int), + &flag); + } + + set_freesync_on_streams(core_freesync, streams, num_streams); + + return true; +} + +bool mod_freesync_get_user_enable(struct mod_freesync *mod_freesync, + const struct dc_stream *stream, + struct mod_freesync_user_enable *user_enable) +{ + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + + unsigned int index = map_index_from_stream(core_freesync, stream); + + *user_enable = core_freesync->map[index].user_enable; + + return true; +} + +void mod_freesync_notify_mode_change(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams) +{ + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + + unsigned int stream_index, map_index; + unsigned min_frame_duration_in_ns, max_frame_duration_in_ns; + struct freesync_state *state; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + + map_index = map_index_from_stream(core_freesync, + streams[stream_index]); + + state = &core_freesync->map[map_index].state; + + if (core_freesync->map[map_index].caps->supported) { + /* Update the field rate for new timing */ + state->nominal_refresh_rate_in_micro_hz = 1000000 * + div64_u64(div64_u64((streams[stream_index]-> + timing.pix_clk_khz * 1000), + streams[stream_index]->timing.v_total), + streams[stream_index]->timing.h_total); + + /* Update the stream */ + update_stream(core_freesync, streams[stream_index]); + + /* Determine whether BTR can be supported */ + min_frame_duration_in_ns = ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + state->nominal_refresh_rate_in_micro_hz))); + + max_frame_duration_in_ns = ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + core_freesync->map[map_index].caps->min_refresh_in_micro_hz))); + + if (max_frame_duration_in_ns >= + 2 * min_frame_duration_in_ns) + core_freesync->map[map_index].caps->btr_supported = true; + else + core_freesync->map[map_index].caps->btr_supported = false; + + /* Cache the time variables */ + state->time.max_render_time_in_us = + max_frame_duration_in_ns / 1000; + state->time.min_render_time_in_us = + min_frame_duration_in_ns / 1000; + state->btr.mid_point_in_us = + (max_frame_duration_in_ns + + min_frame_duration_in_ns) / 2000; + + } + } + + /* Program freesync according to current state*/ + set_freesync_on_streams(core_freesync, streams, num_streams); +} + +/* Add the timestamps to the cache and determine whether BTR programming + * is required, depending on the times calculated + */ +static void update_timestamps(struct core_freesync *core_freesync, + const struct dc_stream *stream, unsigned int map_index, + unsigned int last_render_time_in_us) +{ + struct freesync_state *state = &core_freesync->map[map_index].state; + + state->time.render_times[state->time.render_times_index] = + last_render_time_in_us; + state->time.render_times_index++; + + if (state->time.render_times_index >= RENDER_TIMES_MAX_COUNT) + state->time.render_times_index = 0; + + if (last_render_time_in_us + BTR_EXIT_MARGIN < + state->time.max_render_time_in_us) { + + /* Exit Below the Range */ + if (state->btr.btr_active) { + + state->btr.program_btr = true; + state->btr.btr_active = false; + state->btr.frame_counter = 0; + + /* Exit Fixed Refresh mode */ + } else if (state->fixed_refresh.fixed_refresh_active) { + + state->fixed_refresh.program_fixed_refresh = true; + state->fixed_refresh.fixed_refresh_active = false; + + } + + } else if (last_render_time_in_us > state->time.max_render_time_in_us) { + + /* Enter Below the Range */ + if (!state->btr.btr_active && + core_freesync->map[map_index].caps->btr_supported) { + + state->btr.program_btr = true; + state->btr.btr_active = true; + + /* Enter Fixed Refresh mode */ + } else if (!state->fixed_refresh.fixed_refresh_active && + !core_freesync->map[map_index].caps->btr_supported) { + + state->fixed_refresh.program_fixed_refresh = true; + state->fixed_refresh.fixed_refresh_active = true; + + } + } + + /* When Below the Range is active, must react on every frame */ + if (state->btr.btr_active) + state->btr.program_btr = true; +} + +static void apply_below_the_range(struct core_freesync *core_freesync, + const struct dc_stream *stream, unsigned int map_index, + unsigned int last_render_time_in_us) +{ + unsigned int inserted_frame_duration_in_us = 0; + unsigned int mid_point_frames_ceil = 0; + unsigned int mid_point_frames_floor = 0; + unsigned int frame_time_in_us = 0; + unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF; + unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF; + unsigned int frames_to_insert = 0; + unsigned int inserted_frame_v_total = 0; + unsigned int vmin = 0, vmax = 0; + unsigned int min_frame_duration_in_ns = 0; + struct freesync_state *state = &core_freesync->map[map_index].state; + + if (!state->btr.program_btr) + return; + + state->btr.program_btr = false; + + min_frame_duration_in_ns = ((unsigned int) (div64_u64( + (1000000000ULL * 1000000), + state->nominal_refresh_rate_in_micro_hz))); + + /* Program BTR */ + + /* BTR set to "not active" so disengage */ + if (!state->btr.btr_active) + + /* Restore FreeSync */ + set_freesync_on_streams(core_freesync, &stream, 1); + + /* BTR set to "active" so engage */ + else { + + /* Calculate number of midPoint frames that could fit within + * the render time interval- take ceil of this value + */ + mid_point_frames_ceil = (last_render_time_in_us + + state->btr.mid_point_in_us- 1) / + state->btr.mid_point_in_us; + + if (mid_point_frames_ceil > 0) { + + frame_time_in_us = last_render_time_in_us / + mid_point_frames_ceil; + delta_from_mid_point_in_us_1 = (state->btr.mid_point_in_us > + frame_time_in_us) ? + (state->btr.mid_point_in_us - frame_time_in_us): + (frame_time_in_us - state->btr.mid_point_in_us); + } + + /* Calculate number of midPoint frames that could fit within + * the render time interval- take floor of this value + */ + mid_point_frames_floor = last_render_time_in_us / + state->btr.mid_point_in_us; + + if (mid_point_frames_floor > 0) { + + frame_time_in_us = last_render_time_in_us / + mid_point_frames_floor; + delta_from_mid_point_in_us_2 = (state->btr.mid_point_in_us > + frame_time_in_us) ? + (state->btr.mid_point_in_us - frame_time_in_us): + (frame_time_in_us - state->btr.mid_point_in_us); + } + + /* Choose number of frames to insert based on how close it + * can get to the mid point of the variable range. + */ + if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2) + frames_to_insert = mid_point_frames_ceil; + else + frames_to_insert = mid_point_frames_floor; + + /* Either we've calculated the number of frames to insert, + * or we need to insert min duration frames + */ + if (frames_to_insert > 0) + inserted_frame_duration_in_us = last_render_time_in_us / + frames_to_insert; + + if (inserted_frame_duration_in_us < + state->time.min_render_time_in_us) + + inserted_frame_duration_in_us = + state->time.min_render_time_in_us; + + /* We need the v_total_min from capability */ + calc_vmin_vmax(core_freesync, stream, &vmin, &vmax); + + inserted_frame_v_total = vmin; + if (min_frame_duration_in_ns / 1000) + inserted_frame_v_total = inserted_frame_duration_in_us * + vmin / (min_frame_duration_in_ns / 1000); + + /* Set length of inserted frames as v_total_max*/ + vmax = inserted_frame_v_total; + + /* Program V_TOTAL */ + core_freesync->dc->stream_funcs.adjust_vmin_vmax( + core_freesync->dc, &stream, + 1, vmin, + vmax); + + /* Cache the calculated variables */ + state->btr.inserted_frame_duration_in_us = + inserted_frame_duration_in_us; + state->btr.frames_to_insert = frames_to_insert; + state->btr.frame_counter = frames_to_insert; + + } +} + +static void apply_fixed_refresh(struct core_freesync *core_freesync, + const struct dc_stream *stream, unsigned int map_index) +{ + unsigned int vmin = 0, vmax = 0; + struct freesync_state *state = &core_freesync->map[map_index].state; + + if (!state->fixed_refresh.program_fixed_refresh) + return; + + state->fixed_refresh.program_fixed_refresh = false; + + /* Program Fixed Refresh */ + + /* Fixed Refresh set to "not active" so disengage */ + if (!state->fixed_refresh.fixed_refresh_active) { + set_freesync_on_streams(core_freesync, &stream, 1); + + /* Fixed Refresh set to "active" so engage (fix to max) */ + } else { + + calc_vmin_vmax(core_freesync, stream, &vmin, &vmax); + + vmax = vmin; + + core_freesync->dc->stream_funcs.adjust_vmin_vmax( + core_freesync->dc, &stream, + 1, vmin, + vmax); + } +} + +void mod_freesync_pre_update_plane_addresses(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams, + unsigned int curr_time_stamp_in_us) +{ + unsigned int stream_index, map_index, last_render_time_in_us = 0; + struct core_freesync *core_freesync = + MOD_FREESYNC_TO_CORE(mod_freesync); + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + + map_index = map_index_from_stream(core_freesync, + streams[stream_index]); + + if (core_freesync->map[map_index].caps->supported) { + + last_render_time_in_us = curr_time_stamp_in_us - + core_freesync->map[map_index].state.time. + prev_time_stamp_in_us; + + /* Add the timestamps to the cache and determine + * whether BTR program is required + */ + update_timestamps(core_freesync, streams[stream_index], + map_index, last_render_time_in_us); + + if (core_freesync->map[map_index].state.fullscreen && + core_freesync->map[map_index].user_enable. + enable_for_gaming) { + + if (core_freesync->map[map_index].caps->btr_supported) { + + apply_below_the_range(core_freesync, + streams[stream_index], map_index, + last_render_time_in_us); + } else { + apply_fixed_refresh(core_freesync, + streams[stream_index], map_index); + } + } + + core_freesync->map[map_index].state.time. + prev_time_stamp_in_us = curr_time_stamp_in_us; + } + + } +} diff --git a/drivers/gpu/drm/amd/display/modules/inc/mod_color.h b/drivers/gpu/drm/amd/display/modules/inc/mod_color.h new file mode 100644 index 000000000000..e54fe2cb8611 --- /dev/null +++ b/drivers/gpu/drm/amd/display/modules/inc/mod_color.h @@ -0,0 +1,179 @@ +/* + * Copyright 2016 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: AMD + * + */ + + +#ifndef MOD_COLOR_H_ +#define MOD_COLOR_H_ + +#include "dm_services.h" + +struct mod_color { + int dummy; +}; + +struct color_space_coordinates { + unsigned int redX; + unsigned int redY; + unsigned int greenX; + unsigned int greenY; + unsigned int blueX; + unsigned int blueY; + unsigned int whiteX; + unsigned int whiteY; +}; + +struct gamut_space_coordinates { + unsigned int redX; + unsigned int redY; + unsigned int greenX; + unsigned int greenY; + unsigned int blueX; + unsigned int blueY; +}; + +struct gamut_space_entry { + unsigned int index; + unsigned int redX; + unsigned int redY; + unsigned int greenX; + unsigned int greenY; + unsigned int blueX; + unsigned int blueY; + + int a0; + int a1; + int a2; + int a3; + int gamma; +}; + +struct white_point_coodinates { + unsigned int whiteX; + unsigned int whiteY; +}; + +struct white_point_coodinates_entry { + unsigned int index; + unsigned int whiteX; + unsigned int whiteY; +}; + +struct color_range { + int current; + int min; + int max; +}; + +struct mod_color *mod_color_create(struct dc *dc); + +void mod_color_destroy(struct mod_color *mod_color); + +bool mod_color_add_sink(struct mod_color *mod_color, + const struct dc_sink *sink); + +bool mod_color_remove_sink(struct mod_color *mod_color, + const struct dc_sink *sink); + +bool mod_color_update_gamut_to_stream(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams); + +bool mod_color_set_white_point(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + struct white_point_coodinates *white_point); + +bool mod_color_adjust_source_gamut(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + struct gamut_space_coordinates *input_gamut_coordinates, + struct white_point_coodinates *input_white_point_coordinates); + +bool mod_color_adjust_destination_gamut(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + struct gamut_space_coordinates *input_gamut_coordinates, + struct white_point_coodinates *input_white_point_coordinates); + +bool mod_color_get_user_enable(struct mod_color *mod_color, + const struct dc_sink *sink, + bool *user_enable); + +bool mod_color_set_user_enable(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + bool user_enable); + +bool mod_color_get_custom_color_temperature(struct mod_color *mod_color, + const struct dc_sink *sink, + int *color_temperature); + +bool mod_color_set_custom_color_temperature(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int color_temperature); + +bool mod_color_get_color_saturation(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_range *color_saturation); + +bool mod_color_get_color_contrast(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_range *color_contrast); + +bool mod_color_get_color_brightness(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_range *color_brightness); + +bool mod_color_get_color_hue(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_range *color_hue); + +bool mod_color_get_source_gamut(struct mod_color *mod_color, + const struct dc_sink *sink, + struct color_space_coordinates *source_gamut); + +bool mod_color_notify_mode_change(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams); + +bool mod_color_set_brightness(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int brightness_value); + +bool mod_color_set_contrast(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int contrast_value); + +bool mod_color_set_hue(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int hue_value); + +bool mod_color_set_saturation(struct mod_color *mod_color, + const struct dc_stream **streams, int num_streams, + int saturation_value); + +bool mod_color_set_preferred_quantization_range(struct mod_color *mod_color, + const struct dc_sink *sink, + enum dc_quantization_range quantization_range); + +bool mod_color_get_preferred_quantization_range(struct mod_color *mod_color, + const struct dc_sink *sink, + enum dc_quantization_range *quantization_range); + +#endif /* MOD_COLOR_H_ */ diff --git a/drivers/gpu/drm/amd/display/modules/inc/mod_freesync.h b/drivers/gpu/drm/amd/display/modules/inc/mod_freesync.h new file mode 100644 index 000000000000..7abfe34dc2d9 --- /dev/null +++ b/drivers/gpu/drm/amd/display/modules/inc/mod_freesync.h @@ -0,0 +1,149 @@ +/* + * Copyright 2016 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: AMD + * + */ + + + + +/* + * Copyright 2016 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: AMD + * + */ + +#ifndef MOD_FREESYNC_H_ +#define MOD_FREESYNC_H_ + +#include "dm_services.h" + +struct mod_freesync *mod_freesync_create(struct dc *dc); +void mod_freesync_destroy(struct mod_freesync *mod_freesync); + +struct mod_freesync { + int dummy; +}; + +enum mod_freesync_state { + FREESYNC_STATE_NONE, + FREESYNC_STATE_FULLSCREEN, + FREESYNC_STATE_STATIC_SCREEN, + FREESYNC_STATE_VIDEO +}; + +enum mod_freesync_user_enable_mask { + FREESYNC_USER_ENABLE_STATIC = 0x1, + FREESYNC_USER_ENABLE_VIDEO = 0x2, + FREESYNC_USER_ENABLE_GAMING = 0x4 +}; + +struct mod_freesync_user_enable { + bool enable_for_static; + bool enable_for_video; + bool enable_for_gaming; +}; + +struct mod_freesync_caps { + bool supported; + unsigned int min_refresh_in_micro_hz; + unsigned int max_refresh_in_micro_hz; + + bool btr_supported; +}; + +struct mod_freesync_params { + enum mod_freesync_state state; + bool enable; + unsigned int update_duration_in_ns; + bool windowed_fullscreen; +}; + +/* + * Add stream to be tracked by module + */ +bool mod_freesync_add_stream(struct mod_freesync *mod_freesync, + const struct dc_stream *stream, struct mod_freesync_caps *caps); + +/* + * Remove stream to be tracked by module + */ +bool mod_freesync_remove_stream(struct mod_freesync *mod_freesync, + const struct dc_stream *stream); + +/* + * Build additional parameters for dc_stream when creating stream for + * sink to support freesync + */ +void mod_freesync_update_stream(struct mod_freesync *mod_freesync, + struct dc_stream *stream); + +/* + * Update the freesync state flags for each display and program + * freesync accordingly + */ +void mod_freesync_update_state(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams, + struct mod_freesync_params *freesync_params); + +bool mod_freesync_get_state(struct mod_freesync *mod_freesync, + const struct dc_stream *stream, + struct mod_freesync_params *freesync_params); + +bool mod_freesync_set_user_enable(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams, + struct mod_freesync_user_enable *user_enable); + +bool mod_freesync_get_user_enable(struct mod_freesync *mod_freesync, + const struct dc_stream *stream, + struct mod_freesync_user_enable *user_enable); + +void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams); + +void mod_freesync_notify_mode_change(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams); + +void mod_freesync_pre_update_plane_addresses(struct mod_freesync *mod_freesync, + const struct dc_stream **streams, int num_streams, + unsigned int curr_time_stamp); + +#endif diff --git a/drivers/gpu/drm/amd/display/modules/inc/mod_power.h b/drivers/gpu/drm/amd/display/modules/inc/mod_power.h new file mode 100644 index 000000000000..a204e8d6cd23 --- /dev/null +++ b/drivers/gpu/drm/amd/display/modules/inc/mod_power.h @@ -0,0 +1,112 @@ +/* + * Copyright 2016 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: AMD + * + */ + +#ifndef MODULES_INC_MOD_POWER_H_ +#define MODULES_INC_MOD_POWER_H_ + +#include "dm_services.h" + +struct mod_power { + int dummy; +}; + +/* VariBright related commands */ +enum varibright_command { + VariBright_Cmd__SetVBLevel = 0, + VariBright_Cmd__UserEnable, + VariBright_Cmd__PreDisplayConfigChange, + VariBright_Cmd__PostDisplayConfigChange, + VariBright_Cmd__SuspendABM, + VariBright_Cmd__ResumeABM, + + VariBright_Cmd__Unknown, +}; + +/* VariBright settings structure */ +struct varibright_info { + enum varibright_command cmd; + + unsigned int level; + bool enable; + bool activate; +}; + +enum dmcu_block_psr_reason { + /* This is a bitfield mask */ + dmcu_block_psr_reason_invalid = 0x0, + dmcu_block_psr_reason_vsync_int = 0x1, + dmcu_block_psr_reason_shared_primary = 0x2, + dmcu_block_psr_reason_unsupported_link_rate = 0x4 +}; + +struct mod_power *mod_power_create(struct dc *dc); + +void mod_power_destroy(struct mod_power *mod_power); + +bool mod_power_add_sink(struct mod_power *mod_power, + const struct dc_sink *sink); + +bool mod_power_remove_sink(struct mod_power *mod_power, + const struct dc_sink *sink); + +bool mod_power_set_backlight(struct mod_power *mod_power, + const struct dc_stream **streams, int num_streams, + unsigned int backlight_8bit); + +bool mod_power_get_backlight(struct mod_power *mod_power, + const struct dc_sink *sink, + unsigned int *backlight_8bit); + +void mod_power_initialize_backlight_caps + (struct mod_power *mod_power); + +unsigned int mod_power_backlight_level_percentage_to_signal + (struct mod_power *mod_power, unsigned int percentage); + +unsigned int mod_power_backlight_level_signal_to_percentage + (struct mod_power *mod_power, unsigned int signalLevel8bit); + +bool mod_power_get_panel_backlight_boundaries + (struct mod_power *mod_power, + unsigned int *min_backlight, + unsigned int *max_backlight, + unsigned int *output_ac_level_percentage, + unsigned int *output_dc_level_percentage); + +bool mod_power_set_smooth_brightness(struct mod_power *mod_power, + const struct dc_sink *sink, bool enable_brightness); + +bool mod_power_notify_mode_change(struct mod_power *mod_power, + const struct dc_stream *stream); + +bool mod_power_varibright_control(struct mod_power *mod_power, + struct varibright_info *input_varibright_info); + +bool mod_power_block_psr(bool block_enable, enum dmcu_block_psr_reason reason); + +bool mod_power_set_psr_enable(struct mod_power *mod_power, + bool psr_enable); + +#endif /* MODULES_INC_MOD_POWER_H_ */ diff --git a/drivers/gpu/drm/amd/display/modules/power/power.c b/drivers/gpu/drm/amd/display/modules/power/power.c new file mode 100644 index 000000000000..ea07e847da0a --- /dev/null +++ b/drivers/gpu/drm/amd/display/modules/power/power.c @@ -0,0 +1,784 @@ +/* + * Copyright 2016 Advanced Micro Devices, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR + * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Authors: AMD + * + */ + +#include "mod_power.h" +#include "dm_services.h" +#include "dc.h" +#include "core_types.h" +#include "core_dc.h" + +#define MOD_POWER_MAX_CONCURRENT_SINKS 32 +#define SMOOTH_BRIGHTNESS_ADJUSTMENT_TIME_IN_MS 500 + +struct sink_caps { + const struct dc_sink *sink; +}; + +struct backlight_state { + unsigned int backlight; + unsigned int frame_ramp; + bool smooth_brightness_enabled; +}; + +struct core_power { + struct mod_power public; + struct dc *dc; + int num_sinks; + struct sink_caps *caps; + struct backlight_state *state; +}; + +union dmcu_abm_set_bl_params { + struct { + unsigned int gradual_change : 1; /* [0:0] */ + unsigned int reserved : 15; /* [15:1] */ + unsigned int frame_ramp : 16; /* [31:16] */ + } bits; + unsigned int u32All; +}; + +/* Backlight cached properties */ +static unsigned int backlight_8bit_lut_array[101]; +static unsigned int ac_level_percentage; +static unsigned int dc_level_percentage; +static bool backlight_caps_valid; +/* we use lazy initialization of backlight capabilities cache */ +static bool backlight_caps_initialized; +/* AC/DC levels initialized later in separate context */ +static bool backlight_def_levels_valid; + +/* ABM cached properties */ +static unsigned int abm_level; +static bool abm_user_enable; +static bool abm_active; + +/*PSR cached properties*/ +static unsigned int block_psr; + +/* Defines default backlight curve F(x) = A(x*x) + Bx + C. + * + * Backlight curve should always satisfy F(0) = min, F(100) = max, + * so polynom coefficients are: + * A is 0.0255 - B/100 - min/10000 - (255-max)/10000 = (max - min)/10000 - B/100 + * B is adjustable factor to modify the curve. + * Bigger B results in less concave curve. B range is [0..(max-min)/100] + * C is backlight minimum + */ +static const unsigned int backlight_curve_coeff_a_factor = 10000; +static const unsigned int backlight_curve_coeff_b = 100; +static const unsigned int backlight_curve_coeff_b_factor = 100; + +/* Minimum and maximum backlight input signal levels */ +static const unsigned int default_min_backlight = 12; +static const unsigned int default_max_backlight = 255; + +/* Other backlight constants */ +static const unsigned int absolute_backlight_max = 255; + +#define MOD_POWER_TO_CORE(mod_power)\ + container_of(mod_power, struct core_power, public) + +static bool check_dc_support(const struct dc *dc) +{ + if (dc->stream_funcs.set_backlight == NULL) + return false; + + return true; +} + +/* Given a specific dc_sink* this function finds its equivalent + * on the dc_sink array and returns the corresponding index + */ +static unsigned int sink_index_from_sink(struct core_power *core_power, + const struct dc_sink *sink) +{ + unsigned int index = 0; + + for (index = 0; index < core_power->num_sinks; index++) + if (core_power->caps[index].sink == sink) + return index; + + /* Could not find sink requested */ + ASSERT(false); + return index; +} + +static unsigned int convertBL8to17(unsigned int backlight_8bit) +{ + unsigned int temp_ulong = backlight_8bit * 0x10101; + unsigned char temp_uchar = + (unsigned char)(((temp_ulong & 0x80) >> 7) & 1); + + temp_ulong = (temp_ulong >> 8) + temp_uchar; + + return temp_ulong; +} + +static uint16_t convertBL8to16(unsigned int backlight_8bit) +{ + return (uint16_t)((backlight_8bit * 0x10101) >> 8); +} + +/*This is used when OS wants to retrieve the current BL. + * We return the 8bit value to OS. + */ +static unsigned int convertBL17to8(unsigned int backlight_17bit) +{ + if (backlight_17bit & 0x10000) + return default_max_backlight; + else + return (backlight_17bit >> 8); +} + +struct mod_power *mod_power_create(struct dc *dc) +{ + struct core_power *core_power = + dm_alloc(sizeof(struct core_power)); + + struct core_dc *core_dc = DC_TO_CORE(dc); + + int i = 0; + + if (core_power == NULL) + goto fail_alloc_context; + + core_power->caps = dm_alloc(sizeof(struct sink_caps) * + MOD_POWER_MAX_CONCURRENT_SINKS); + + if (core_power->caps == NULL) + goto fail_alloc_caps; + + for (i = 0; i < MOD_POWER_MAX_CONCURRENT_SINKS; i++) + core_power->caps[i].sink = NULL; + + core_power->state = dm_alloc(sizeof(struct backlight_state) * + MOD_POWER_MAX_CONCURRENT_SINKS); + + if (core_power->state == NULL) + goto fail_alloc_state; + + core_power->num_sinks = 0; + backlight_caps_valid = false; + + if (dc == NULL) + goto fail_construct; + + core_power->dc = dc; + + if (!check_dc_support(dc)) + goto fail_construct; + + abm_user_enable = false; + abm_active = false; + + return &core_power->public; + +fail_construct: + dm_free(core_power->state); + +fail_alloc_state: + dm_free(core_power->caps); + +fail_alloc_caps: + dm_free(core_power); + +fail_alloc_context: + return NULL; +} + + +void mod_power_destroy(struct mod_power *mod_power) +{ + if (mod_power != NULL) { + int i; + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + + dm_free(core_power->state); + + for (i = 0; i < core_power->num_sinks; i++) + dc_sink_release(core_power->caps[i].sink); + + dm_free(core_power->caps); + + dm_free(core_power); + } +} + +bool mod_power_add_sink(struct mod_power *mod_power, + const struct dc_sink *sink) +{ + if (sink->sink_signal == SIGNAL_TYPE_VIRTUAL) + return false; + + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + struct core_dc *core_dc = DC_TO_CORE(core_power->dc); + + if (core_power->num_sinks < MOD_POWER_MAX_CONCURRENT_SINKS) { + dc_sink_retain(sink); + core_power->caps[core_power->num_sinks].sink = sink; + core_power->state[core_power->num_sinks]. + smooth_brightness_enabled = false; + core_power->state[core_power->num_sinks]. + backlight = 100; + core_power->num_sinks++; + return true; + } + + return false; +} + +bool mod_power_remove_sink(struct mod_power *mod_power, + const struct dc_sink *sink) +{ + int i = 0, j = 0; + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + + for (i = 0; i < core_power->num_sinks; i++) { + if (core_power->caps[i].sink == sink) { + /* To remove this sink, shift everything after down */ + for (j = i; j < core_power->num_sinks - 1; j++) { + core_power->caps[j].sink = + core_power->caps[j + 1].sink; + + memcpy(&core_power->state[j], + &core_power->state[j + 1], + sizeof(struct backlight_state)); + } + core_power->num_sinks--; + dc_sink_release(sink); + return true; + } + } + return false; +} + +bool mod_power_set_backlight(struct mod_power *mod_power, + const struct dc_stream **streams, int num_streams, + unsigned int backlight_8bit) +{ + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + + unsigned int frame_ramp = 0; + + unsigned int stream_index, sink_index, vsync_rate_hz; + + union dmcu_abm_set_bl_params params; + + for (stream_index = 0; stream_index < num_streams; stream_index++) { + if (streams[stream_index]->sink->sink_signal == SIGNAL_TYPE_VIRTUAL) { + core_power->state[sink_index].backlight = 0; + core_power->state[sink_index].frame_ramp = 0; + core_power->state[sink_index].smooth_brightness_enabled = false; + continue; + } + + sink_index = sink_index_from_sink(core_power, + streams[stream_index]->sink); + + vsync_rate_hz = div64_u64(div64_u64((streams[stream_index]-> + timing.pix_clk_khz * 1000), + streams[stream_index]->timing.v_total), + streams[stream_index]->timing.h_total); + + core_power->state[sink_index].backlight = backlight_8bit; + + if (core_power->state[sink_index].smooth_brightness_enabled) + frame_ramp = ((vsync_rate_hz * + SMOOTH_BRIGHTNESS_ADJUSTMENT_TIME_IN_MS) + 500) + / 1000; + else + frame_ramp = 0; + + core_power->state[sink_index].frame_ramp = frame_ramp; + } + + params.u32All = 0; + params.bits.gradual_change = (frame_ramp > 0); + params.bits.frame_ramp = frame_ramp; + + core_power->dc->stream_funcs.set_backlight + (core_power->dc, backlight_8bit, params.u32All, streams[0]); + + return true; +} + +bool mod_power_get_backlight(struct mod_power *mod_power, + const struct dc_sink *sink, + unsigned int *backlight_8bit) +{ + if (sink->sink_signal == SIGNAL_TYPE_VIRTUAL) + return false; + + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + + unsigned int sink_index = sink_index_from_sink(core_power, sink); + + *backlight_8bit = core_power->state[sink_index].backlight; + + return true; +} + +/* hard coded to default backlight curve. */ +void mod_power_initialize_backlight_caps(struct mod_power + *mod_power) +{ + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + struct core_dc *core_dc = DC_TO_CORE(core_power->dc); + unsigned int i; + + backlight_caps_initialized = true; + + struct dm_acpi_atif_backlight_caps *pExtCaps = NULL; + bool customCurvePresent = false; + bool customMinMaxPresent = false; + bool customDefLevelsPresent = false; + + /* Allocate memory for ATIF output + * (do not want to use 256 bytes on the stack) + */ + pExtCaps = (struct dm_acpi_atif_backlight_caps *) + (dm_alloc(sizeof(struct dm_acpi_atif_backlight_caps))); + if (pExtCaps == NULL) + return; + + /* Retrieve ACPI extended brightness caps */ + if (dm_query_extended_brightness_caps + (core_dc->ctx, AcpiDisplayType_LCD1, pExtCaps)) { + ac_level_percentage = pExtCaps->acLevelPercentage; + dc_level_percentage = pExtCaps->dcLevelPercentage; + customMinMaxPresent = true; + customDefLevelsPresent = true; + customCurvePresent = (pExtCaps->numOfDataPoints > 0); + + ASSERT(pExtCaps->numOfDataPoints <= 99); + } else { + dm_free(pExtCaps); + return; + } + + if (customMinMaxPresent) + backlight_8bit_lut_array[0] = pExtCaps->minInputSignal; + else + backlight_8bit_lut_array[0] = default_min_backlight; + + if (customMinMaxPresent) + backlight_8bit_lut_array[100] = pExtCaps->maxInputSignal; + else + backlight_8bit_lut_array[100] = default_max_backlight; + + ASSERT(backlight_8bit_lut_array[100] <= absolute_backlight_max); + ASSERT(backlight_8bit_lut_array[0] <= + backlight_8bit_lut_array[100]); + + /* Just to make sure we use valid values */ + if (backlight_8bit_lut_array[100] > absolute_backlight_max) + backlight_8bit_lut_array[100] = absolute_backlight_max; + if (backlight_8bit_lut_array[0] > backlight_8bit_lut_array[100]) { + unsigned int swap; + + swap = backlight_8bit_lut_array[0]; + backlight_8bit_lut_array[0] = backlight_8bit_lut_array[100]; + backlight_8bit_lut_array[100] = swap; + } + + /* Build backlight translation table for custom curve */ + if (customCurvePresent) { + unsigned int index = 1; + unsigned int numOfDataPoints = + (pExtCaps->numOfDataPoints <= 99 ? + pExtCaps->numOfDataPoints : 99); + + /* Filling translation table from data points - + * between every two provided data points we + * lineary interpolate missing values + */ + for (i = 0; i < numOfDataPoints; i++) { + /* Clamp signal level between min and max + * (since min and max might come other + * soruce like registry) + */ + unsigned int luminance = + pExtCaps->dataPoints[i].luminance; + unsigned int signalLevel = + pExtCaps->dataPoints[i].signalLevel; + + if (signalLevel < backlight_8bit_lut_array[0]) + signalLevel = backlight_8bit_lut_array[0]; + if (signalLevel > backlight_8bit_lut_array[100]) + signalLevel = backlight_8bit_lut_array[100]; + + /* Lineary interpolate missing values */ + if (index < luminance) { + unsigned int baseValue = + backlight_8bit_lut_array[index-1]; + unsigned int deltaSignal = + signalLevel - baseValue; + unsigned int deltaLuma = + luminance - index + 1; + unsigned int step = deltaSignal; + + for (; index < luminance; index++) { + backlight_8bit_lut_array[index] = + baseValue + (step / deltaLuma); + step += deltaSignal; + } + } + + /* Now [index == luminance], + * so we can add data point to the translation table + */ + backlight_8bit_lut_array[index++] = signalLevel; + } + + /* Complete the final segment of interpolation - + * between last datapoint and maximum value + */ + if (index < 100) { + unsigned int baseValue = + backlight_8bit_lut_array[index-1]; + unsigned int deltaSignal = + backlight_8bit_lut_array[100] - + baseValue; + unsigned int deltaLuma = 100 - index + 1; + unsigned int step = deltaSignal; + + for (; index < 100; index++) { + backlight_8bit_lut_array[index] = + baseValue + (step / deltaLuma); + step += deltaSignal; + } + } + /* Build backlight translation table based on default curve */ + } else { + unsigned int delta = + backlight_8bit_lut_array[100] - + backlight_8bit_lut_array[0]; + unsigned int coeffC = backlight_8bit_lut_array[0]; + unsigned int coeffB = + (backlight_curve_coeff_b < delta ? + backlight_curve_coeff_b : delta); + unsigned int coeffA = delta - coeffB; /* coeffB is B*100 */ + + for (i = 1; i < 100; i++) { + backlight_8bit_lut_array[i] = + (coeffA * i * i) / + backlight_curve_coeff_a_factor + + (coeffB * i) / + backlight_curve_coeff_b_factor + + coeffC; + } + } + + if (pExtCaps != NULL) + dm_free(pExtCaps); + + /* Successfully initialized */ + backlight_caps_valid = true; + backlight_def_levels_valid = customDefLevelsPresent; +} + +unsigned int mod_power_backlight_level_percentage_to_signal( + struct mod_power *mod_power, unsigned int percentage) +{ + /* Do lazy initialization of backlight capabilities*/ + if (!backlight_caps_initialized) + mod_power_initialize_backlight_caps(mod_power); + + /* Since the translation table is indexed by percentage, + * we simply return backlight value at given percent + */ + if (backlight_caps_valid && percentage <= 100) + return backlight_8bit_lut_array[percentage]; + + return -1; +} + +unsigned int mod_power_backlight_level_signal_to_percentage( + struct mod_power *mod_power, + unsigned int signalLevel8bit) +{ + unsigned int invalid_backlight = (unsigned int)(-1); + /* Do lazy initialization of backlight capabilities */ + if (!backlight_caps_initialized) + mod_power_initialize_backlight_caps(mod_power); + + /* If customer curve cannot convert to differentiated value near min + * it is important to report 0 for min signal to pass setting "Dimmed" + * setting in HCK brightness2 tests. + */ + if (signalLevel8bit <= backlight_8bit_lut_array[0]) + return 0; + + /* Since the translation table is indexed by percentage + * we need to do a binary search over the array + * Another option would be to guess entry based on linear distribution + * and then do linear search in correct direction + */ + if (backlight_caps_valid && signalLevel8bit <= + absolute_backlight_max) { + unsigned int min = 0; + unsigned int max = 100; + unsigned int mid = invalid_backlight; + + while (max >= min) { + mid = (min + max) / 2; /* floor of half range */ + + if (backlight_8bit_lut_array[mid] < signalLevel8bit) + min = mid + 1; + else if (backlight_8bit_lut_array[mid] > + signalLevel8bit) + max = mid - 1; + else + break; + + if (max == 0 || max == 1) + return invalid_backlight; + } + return mid; + } + + return invalid_backlight; +} + + +bool mod_power_get_panel_backlight_boundaries( + struct mod_power *mod_power, + unsigned int *min_backlight, + unsigned int *max_backlight, + unsigned int *output_ac_level_percentage, + unsigned int *output_dc_level_percentage) +{ + /* Do lazy initialization of backlight capabilities */ + if (!backlight_caps_initialized) + mod_power_initialize_backlight_caps(mod_power); + + /* If cache was successfully updated, + * copy the values to output structure and return success + */ + if (backlight_caps_valid) { + *min_backlight = backlight_8bit_lut_array[0]; + *max_backlight = backlight_8bit_lut_array[100]; + + *output_ac_level_percentage = ac_level_percentage; + *output_dc_level_percentage = dc_level_percentage; + + return true; + } + + return false; +} + +bool mod_power_set_smooth_brightness(struct mod_power *mod_power, + const struct dc_sink *sink, bool enable_brightness) +{ + if (sink->sink_signal == SIGNAL_TYPE_VIRTUAL) + return false; + + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + unsigned int sink_index = sink_index_from_sink(core_power, sink); + + core_power->state[sink_index].smooth_brightness_enabled + = enable_brightness; + return true; +} + +bool mod_power_notify_mode_change(struct mod_power *mod_power, + const struct dc_stream *stream) +{ + if (stream->sink->sink_signal == SIGNAL_TYPE_VIRTUAL) + return false; + + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + + unsigned int sink_index = sink_index_from_sink(core_power, + stream->sink); + unsigned int frame_ramp = core_power->state[sink_index].frame_ramp; + union dmcu_abm_set_bl_params params; + + params.u32All = 0; + params.bits.gradual_change = (frame_ramp > 0); + params.bits.frame_ramp = frame_ramp; + + core_power->dc->stream_funcs.set_backlight + (core_power->dc, + core_power->state[sink_index].backlight, + params.u32All, stream); + + core_power->dc->stream_funcs.setup_psr + (core_power->dc, stream); + + return true; +} + + +static bool mod_power_abm_feature_enable(struct mod_power + *mod_power, bool enable) +{ + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + if (abm_user_enable == enable) + return true; + + abm_user_enable = enable; + + if (enable) { + if (abm_level != 0 && abm_active) + core_power->dc->stream_funcs.set_abm_level + (core_power->dc, abm_level); + } else { + if (abm_level != 0 && abm_active) { + abm_level = 0; + core_power->dc->stream_funcs.set_abm_level + (core_power->dc, abm_level); + } + } + + return true; +} + +static bool mod_power_abm_activate(struct mod_power + *mod_power, bool activate) +{ + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + if (abm_active == activate) + return true; + + abm_active = activate; + + if (activate) { + if (abm_level != 0 && abm_user_enable) + core_power->dc->stream_funcs.set_abm_level + (core_power->dc, abm_level); + } else { + if (abm_level != 0 && abm_user_enable) { + abm_level = 0; + core_power->dc->stream_funcs.set_abm_level + (core_power->dc, abm_level); + } + } + + return true; +} + +static bool mod_power_abm_set_level(struct mod_power *mod_power, + unsigned int level) +{ + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + if (abm_level == level) + return true; + + if (abm_active && abm_user_enable && level == 0) + core_power->dc->stream_funcs.set_abm_level + (core_power->dc, 0); + else if (abm_active && abm_user_enable && level != 0) + core_power->dc->stream_funcs.set_abm_level + (core_power->dc, level); + + abm_level = level; + + return true; +} + +bool mod_power_varibright_control(struct mod_power *mod_power, + struct varibright_info *input_varibright_info) +{ + switch (input_varibright_info->cmd) { + case VariBright_Cmd__SetVBLevel: + { + /* Set VariBright user level. */ + mod_power_abm_set_level(mod_power, + input_varibright_info->level); + } + break; + + case VariBright_Cmd__UserEnable: + { + /* Set VariBright user enable state. */ + mod_power_abm_feature_enable(mod_power, + input_varibright_info->enable); + } + break; + + case VariBright_Cmd__PostDisplayConfigChange: + { + /* Set VariBright user level. */ + mod_power_abm_set_level(mod_power, + input_varibright_info->level); + + /* Set VariBright user enable state. */ + mod_power_abm_feature_enable(mod_power, + input_varibright_info->enable); + + /* Set VariBright activate based on power state. */ + mod_power_abm_activate(mod_power, + input_varibright_info->activate); + } + break; + + default: + { + return false; + } + break; + } + + return true; +} + +bool mod_power_block_psr(bool block_enable, enum dmcu_block_psr_reason reason) +{ + if (block_enable) + block_psr |= reason; + else + block_psr &= ~reason; + + return true; +} + + +bool mod_power_set_psr_enable(struct mod_power *mod_power, + bool psr_enable) +{ + struct core_power *core_power = + MOD_POWER_TO_CORE(mod_power); + + if (block_psr == 0) + return core_power->dc->stream_funcs.set_psr_enable + (core_power->dc, psr_enable); + + return false; +} + + -- cgit v1.2.3-59-g8ed1b