IGT: Add a luminance test
PATCH 1/1 IGT: Add a luminance test
From: Mario Limonciello (AMD) <[email protected]>
This will make sure that luminance can be changed from DRM master and that it stays in sync with sysfs.
Assisted-by Claude Sonnet
Signed-off-by Mario Limonciello (AMD) <[email protected]>
--- tests/kms_luminance.c | 393 ++++++++++++++++++++++++++++++++++++++++++ tests/meson.build | 1 + 2 files changed, 394 insertions(+) create mode 100644 tests/kms_luminance.c
tests/kms_luminance.c new file +393 -0
@@ -0,0 +1,393 @@ +/* +* Copyright © 2026 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 (including the next +* paragraph) 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 AUTHORS OR COPYRIGHT HOLDERS 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. +*/ + +/** +* TEST: kms luminance +* Category: Display +* Description: Test LUMINANCE connector property and backlight synchronization +* Driver requirement: any +* Mega feature: General Display Features +*/ + +#include "igt.h" +#include <dirent.h> +#include <errno.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdlib.h> + +#define BACKLIGHT_PATH "/sys/class/backlight" + +/** +* SUBTEST: luminance-basic +* Description: Verify LUMINANCE property exists and has correct range +*/ + +/** +* SUBTEST: luminance-dpms-coordination +* Description: Verify LUMINANCE is forced to 0 when DPMS is off +*/ + +/** +* SUBTEST: luminance-sysfs-to-drm +* Description: Test sysfs brightness changes synchronize to DRM LUMINANCE property +*/ + +/** +* SUBTEST: luminance-drm-to-sysfs +* Description: Test DRM LUMINANCE property changes synchronize to sysfs brightness +*/ + +typedef struct { +int drm_fd; +igt_display_t display; +igt_output_t *output; +char backlight_path[256 ]; +int max_brightness; +}data_t ; + +static bool find_backlight_for_connector (igt_output_t *output,char *path,size_t path_len) +{ +char link_path[512 ]; +char backlight_name[256 ]; +ssize_t len; +DIR *dir; +struct dirent *entry; +char *slash; + +/* Try to find backlight linked to this connector via sysfs */ +snprintf (link_path,sizeof (link_path), +"/sys/class/drm/card%d-%s/backlight" , + output->display->drm_fd, +igt_output_name (output)); + + len =readlink (link_path, backlight_name,sizeof (backlight_name) -1 ); +if (len >0 ) { + backlight_name[len] ='\0' ; +/* Extract just the backlight device name */ + slash =strrchr (backlight_name,'/' ); +if (slash) +snprintf (path, path_len,"%s/%s" ,BACKLIGHT_PATH , slash +1 ); +else +snprintf (path, path_len,"%s/%s" ,BACKLIGHT_PATH , backlight_name); +return true; + } + +/* Fallback: look for any backlight device (common case: single panel) */ + dir =opendir (BACKLIGHT_PATH ); + +if (!dir) +return false; + +while ((entry =readdir (dir)) !=NULL ) { +if (entry->d_name[0 ] =='.' ) +continue ; + +snprintf (path, path_len,"%s/%s" ,BACKLIGHT_PATH , entry->d_name); +closedir (dir); +return true; + } + +closedir (dir); +return false; +} + +static int read_sysfs_int (const char *path) +{ +FILE *f; +int value =-1 ; + + f =fopen (path,"r" ); +if (!f) +return -1 ; + +if (fscanf (f,"%d" , &value) !=1 ) + value =-1 ; + +fclose (f); +return value; +} + +static bool write_sysfs_int (const char *path,int value) +{ +FILE *f; + + f =fopen (path,"w" ); +if (!f) +return false; + +fprintf (f,"%d\n" , value); +fclose (f); +return true; +} + +static void require_luminance_support (data_t *data) +{ +uint64_t value; + +igt_require (igt_has_drm_cap (data->drm_fd,DRM_CLIENT_CAP_ATOMIC )); +igt_require (drmSetClientCap (data->drm_fd,DRM_CLIENT_CAP_ATOMIC ,1 ) ==0 ); + +/* Check if LUMINANCE capability is supported */ +igt_require (drmSetClientCap (data->drm_fd,8 /* DRM_CLIENT_CAP_LUMINANCE */ ,1 ) ==0 ); + +/* Find eDP output with LUMINANCE property */ +for_each_connected_output (&data->display, data->output) { +if (data->output->config.connector->connector_type == DRM_MODE_CONNECTOR_eDP) { +if (kmstest_get_property (data->drm_fd, + data->output->config.connector->connector_id, +DRM_MODE_OBJECT_CONNECTOR , +"LUMINANCE" , +NULL , &value,NULL )) { +/* Found eDP with LUMINANCE */ +igt_require (find_backlight_for_connector (data->output, + data->backlight_path, +sizeof (data->backlight_path))); +return ; + } + } + } + +igt_skip ("No eDP connector with LUMINANCE property found\n" ); +} + +static void test_luminance_basic (data_t *data) +{ +uint32_t prop_id; +uint64_t value, range_min, range_max; +bool is_atomic; +drmModePropertyPtr prop; + +igt_info ("Testing LUMINANCE property on %s\n" ,igt_output_name (data->output)); + +igt_assert (kmstest_get_property (data->drm_fd, + data->output->config.connector->connector_id, +DRM_MODE_OBJECT_CONNECTOR , +"LUMINANCE" , + &prop_id, &value,NULL )); + +/* Verify property is atomic */ + prop =drmModeGetProperty (data->drm_fd, prop_id); +igt_assert (prop); + is_atomic = !!(prop->flags &DRM_MODE_PROP_ATOMIC ); +igt_assert_f (is_atomic,"LUMINANCE property must be atomic\n" ); + +/* Verify range is [0, 65535] */ +igt_assert_f (prop->count_values ==2 ,"LUMINANCE must be a range property\n" ); + range_min = prop->values[0 ]; + range_max = prop->values[1 ]; +igt_assert_f (range_min ==0 ,"LUMINANCE min must be 0, got %lu\n" , range_min); +igt_assert_f (range_max ==65535 ,"LUMINANCE max must be 65535, got %lu\n" , range_max); + +igt_info ("LUMINANCE property: id=%u, value=%lu, range=[%lu, %lu]\n" , + prop_id, value, range_min, range_max); + +drmModeFreeProperty (prop); +} + +static void test_luminance_sysfs_to_drm (data_t *data) +{ +char brightness_path[512 ]; +char max_brightness_path[512 ]; +int original_brightness, new_brightness, max_brightness; +uint64_t original_luminance, new_luminance, expected_luminance; + +snprintf (brightness_path,sizeof (brightness_path),"%s/brightness" , data->backlight_path); +snprintf (max_brightness_path,sizeof (max_brightness_path),"%s/max_brightness" , data->backlight_path); + + max_brightness =read_sysfs_int (max_brightness_path); +igt_assert_f (max_brightness >0 ,"Failed to read max_brightness\n" ); + +/* Read original values */ + original_brightness =read_sysfs_int (brightness_path); +igt_assert_f (original_brightness >=0 ,"Failed to read brightness\n" ); + +kmstest_get_property (data->drm_fd, + data->output->config.connector->connector_id, +DRM_MODE_OBJECT_CONNECTOR , +"LUMINANCE" , +NULL , &original_luminance,NULL ); + +igt_info ("Initial: sysfs=%d/%d, LUMINANCE=%lu\n" , + original_brightness, max_brightness, original_luminance); + +/* Test multiple values: min, 25%, 50%, 75%, max */ +int test_values[] = {0 , max_brightness /4 , max_brightness /2 , + max_brightness *3 /4 , max_brightness}; + +for (int i =0 ; i <5 ; i++) { + new_brightness = test_values[i]; +igt_assert (write_sysfs_int (brightness_path, new_brightness)); + +/* Give kernel time to propagate notification */ +usleep (300000 );/* 300ms */ + +/* Read back DRM property */ +kmstest_get_property (data->drm_fd, + data->output->config.connector->connector_id, +DRM_MODE_OBJECT_CONNECTOR , +"LUMINANCE" , +NULL , &new_luminance,NULL ); + +/* Calculate expected LUMINANCE value using kernel's formula: +* set = (v * U16_MAX) / max +*/ + expected_luminance = ((uint64_t )new_brightness *65535 ) / max_brightness; + +igt_info ("Test %d: sysfs=%d, LUMINANCE=%lu (expected %lu)\n" , + i, new_brightness, new_luminance, expected_luminance); + +/* Allow for small rounding differences - at most 2 to catch formula bugs */ +igt_assert_f (abs ((int )(new_luminance - expected_luminance)) <=2 , +"LUMINANCE not synchronized: expected %lu, got %lu (diff: %d)\n" , + expected_luminance, new_luminance,abs ((int )(new_luminance - expected_luminance))); + } + +/* Restore original brightness */ +write_sysfs_int (brightness_path, original_brightness); +usleep (100000 ); +} + +static void test_luminance_drm_to_sysfs (data_t *data) +{ +char brightness_path[512 ]; +char max_brightness_path[512 ]; +int original_brightness, new_brightness, max_brightness, expected_brightness; +uint64_t original_luminance, new_luminance, luminance_max; +drmModeAtomicReqPtr req; +drmModePropertyPtr prop; +uint32_t prop_id; +int ret; + +snprintf (brightness_path,sizeof (brightness_path),"%s/brightness" , data->backlight_path); +snprintf (max_brightness_path,sizeof (max_brightness_path),"%s/max_brightness" , data->backlight_path); + + max_brightness =read_sysfs_int (max_brightness_path); +igt_assert_f (max_brightness >0 ,"Failed to read max_brightness\n" ); + +/* Read original values */ + original_brightness =read_sysfs_int (brightness_path); +igt_assert_f (original_brightness >=0 ,"Failed to read brightness\n" ); + +kmstest_get_property (data->drm_fd, + data->output->config.connector->connector_id, +DRM_MODE_OBJECT_CONNECTOR , +"LUMINANCE" , + &prop_id, &original_luminance,NULL ); + +/* Get LUMINANCE property range */ + prop =drmModeGetProperty (data->drm_fd, prop_id); +igt_assert (prop); +igt_assert (prop->count_values ==2 ); + luminance_max = prop->values[1 ]; +drmModeFreeProperty (prop); + +igt_info ("Initial: sysfs=%d/%d, LUMINANCE=%lu/%lu\n" , + original_brightness, max_brightness, original_luminance, luminance_max); + +/* Test multiple values: min, 25%, 50%, 75%, max of the LUMINANCE range */ +uint64_t test_luminance[] = {0 , luminance_max /4 , luminance_max /2 , + luminance_max *3 /4 , luminance_max}; + +for (int i =0 ; i <5 ; i++) { + new_luminance = test_luminance[i]; + + req =drmModeAtomicAlloc (); +igt_assert (req); + + ret =drmModeAtomicAddProperty (req, data->output->config.connector->connector_id, + prop_id, new_luminance); +igt_assert (ret >=0 ); + + ret =drmModeAtomicCommit (data->drm_fd, req,DRM_MODE_ATOMIC_ALLOW_MODESET ,NULL ); +igt_assert_f (ret ==0 ,"Atomic commit failed: %s\n" ,strerror (errno)); + +drmModeAtomicFree (req); + +/* Give kernel time to update sysfs */ +usleep (300000 );/* 300ms */ + +/* Read back sysfs brightness */ + new_brightness =read_sysfs_int (brightness_path); +igt_assert_f (new_brightness >=0 ,"Failed to read brightness after DRM change\n" ); + +/* Calculate expected sysfs value using kernel's formula: +* brightness = (luminance * max_brightness) / luminance_max +*/ + expected_brightness = (new_luminance * max_brightness) / luminance_max; + +igt_info ("Test %d: LUMINANCE=%lu, sysfs=%d (expected %d)\n" , + i, new_luminance, new_brightness, expected_brightness); + +/* Allow for small rounding differences - at most 2 to catch formula bugs */ +igt_assert_f (abs (new_brightness - expected_brightness) <=2 , +"sysfs not synchronized: expected %d, got %d (diff: %d)\n" , + expected_brightness, new_brightness,abs (new_brightness - expected_brightness)); + } + +/* Restore original value */ + req =drmModeAtomicAlloc (); +igt_assert (req); +drmModeAtomicAddProperty (req, data->output->config.connector->connector_id, + prop_id, original_luminance); +drmModeAtomicCommit (data->drm_fd, req,DRM_MODE_ATOMIC_ALLOW_MODESET ,NULL ); +drmModeAtomicFree (req); +usleep (100000 ); +} + +int igt_main () +{ +data_t data = {}; + + igt_fixture() { + data.drm_fd =drm_open_driver_master (DRIVER_ANY ); +igt_require (data.drm_fd >=0 ); + +kmstest_set_vt_graphics_mode (); + +igt_display_require (&data.display, data.drm_fd); +require_luminance_support (&data); + +igt_info ("Using backlight: %s\n" , data.backlight_path); + } + +igt_describe ("Verify LUMINANCE property exists and has correct range" ); + igt_subtest("luminance-basic") +test_luminance_basic (&data); + +igt_describe ("Test sysfs brightness changes synchronize to DRM LUMINANCE property" ); + igt_subtest("luminance-sysfs-to-drm") +test_luminance_sysfs_to_drm (&data); + +igt_describe ("Test DRM LUMINANCE property changes synchronize to sysfs brightness" ); + igt_subtest("luminance-drm-to-sysfs") +test_luminance_drm_to_sysfs (&data); + + igt_fixture() { +igt_display_fini (&data.display); +drm_close_driver (data.drm_fd); + } +}
tests/meson.build +1 -0
@@ -46,6 +46,7 @@ test_progs = [ 'kms_hdr', 'kms_invalid_mode', 'kms_lease', + 'kms_luminance', 'kms_multipipe_modeset', 'kms_panel_fitting', 'kms_pipe_crc_basic',
Signature · 1 line
2.53.0