From patchwork Thu May 28 11:44:54 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Antti_Koskip=C3=A4=C3=A4?= X-Patchwork-Id: 6497481 Return-Path: X-Original-To: patchwork-intel-gfx@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id A21F99F1C1 for ; Thu, 28 May 2015 11:45:00 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 989F1205FF for ; Thu, 28 May 2015 11:44:59 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 78C5620557 for ; Thu, 28 May 2015 11:44:58 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id AA8E76EA53; Thu, 28 May 2015 04:44:57 -0700 (PDT) X-Original-To: intel-gfx@lists.freedesktop.org Delivered-To: intel-gfx@lists.freedesktop.org Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by gabe.freedesktop.org (Postfix) with ESMTP id 6DA186EA53 for ; Thu, 28 May 2015 04:44:56 -0700 (PDT) Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga103.jf.intel.com with ESMTP; 28 May 2015 04:44:56 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.13,513,1427785200"; d="scan'208";a="733056800" Received: from sahapukki.fi.intel.com ([10.237.72.154]) by fmsmga002.fm.intel.com with ESMTP; 28 May 2015 04:44:55 -0700 From: Antti Koskipaa To: intel-gfx@lists.freedesktop.org Date: Thu, 28 May 2015 14:44:54 +0300 Message-Id: <1432813494-7972-1-git-send-email-antti.koskipaa@linux.intel.com> X-Mailer: git-send-email 1.8.3.2 MIME-Version: 1.0 Subject: [Intel-gfx] [PATCH i-g-t v2] tests/pm_backlight: Add backlight test X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is a basic sanity test of the backlight sysfs interface. v2: - Add jani's suggestion for immediate readback - Remove unused parameter from test_and_verify() - Add fade test Issue: VIZ-3377 Signed-off-by: Antti Koskipaa Reviewed-by: Jani Nikula --- tests/.gitignore | 1 + tests/Makefile.sources | 1 + tests/pm_backlight.c | 171 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 tests/pm_backlight.c diff --git a/tests/.gitignore b/tests/.gitignore index a3f3143..f816ded 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -150,6 +150,7 @@ kms_vblank kms_crtc_background_color kms_plane_scaling kms_panel_fitting +pm_backlight pm_lpsp pm_rc6_residency pm_rpm diff --git a/tests/Makefile.sources b/tests/Makefile.sources index 994c31b..d2a44e8 100644 --- a/tests/Makefile.sources +++ b/tests/Makefile.sources @@ -80,6 +80,7 @@ TESTS_progs_M = \ kms_crtc_background_color \ kms_plane_scaling \ kms_panel_fitting \ + pm_backlight \ pm_lpsp \ pm_rpm \ pm_rps \ diff --git a/tests/pm_backlight.c b/tests/pm_backlight.c new file mode 100644 index 0000000..d02336d --- /dev/null +++ b/tests/pm_backlight.c @@ -0,0 +1,171 @@ +/* + * Copyright © 2015 Intel Corporation + * + * 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. + * + * Author: + * Antti Koskipaa + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "igt_core.h" + +#define TOLERANCE 5 /* percent */ +#define BACKLIGHT_PATH "/sys/class/backlight/intel_backlight" + +#define FADESTEPS 10 +#define FADESPEED 100 /* milliseconds between steps */ + +IGT_TEST_DESCRIPTION("Basic backlight sysfs test"); + +static int backlight_read(int *result, const char *fname) +{ + int fd; + char full[PATH_MAX]; + char dst[64]; + int r, e; + + igt_assert(snprintf(full, PATH_MAX, "%s/%s", BACKLIGHT_PATH, fname) < PATH_MAX); + + fd = open(full, O_RDONLY); + if (fd == -1) + return -errno; + + r = read(fd, dst, sizeof(dst)); + e = errno; + close(fd); + + if (r < 0) + return -e; + + errno = 0; + *result = strtol(dst, NULL, 10); + return errno; +} + +static int backlight_write(int value, const char *fname) +{ + int fd; + char full[PATH_MAX]; + char src[64]; + int len; + + igt_assert(snprintf(full, PATH_MAX, "%s/%s", BACKLIGHT_PATH, fname) < PATH_MAX); + fd = open(full, O_WRONLY); + if (fd == -1) + return -errno; + + len = snprintf(src, sizeof(src), "%i", value); + len = write(fd, src, len); + close(fd); + + if (len < 0) + return len; + + return 0; +} + +static void test_and_verify(int val) +{ + int result; + + igt_assert(backlight_write(val, "brightness") == 0); + igt_assert(backlight_read(&result, "brightness") == 0); + /* Check that the exact value sticks */ + igt_assert(result == val); + + igt_assert(backlight_read(&result, "actual_brightness") == 0); + /* Some rounding may happen depending on hw. Just check that it's close enough. */ + igt_assert(result <= val + val * TOLERANCE / 100 && result >= val - val * TOLERANCE / 100); +} + +static void test_brightness(int max) +{ + test_and_verify(0); + test_and_verify(max); + test_and_verify(max / 2); +} + +static void test_bad_brightness(int max) +{ + int val; + /* First write some sane value */ + backlight_write(max / 2, "brightness"); + /* Writing invalid values should fail and not change the value */ + igt_assert(backlight_write(-1, "brightness") < 0); + backlight_read(&val, "brightness"); + igt_assert(val == max / 2); + igt_assert(backlight_write(max + 1, "brightness") < 0); + backlight_read(&val, "brightness"); + igt_assert(val == max / 2); + igt_assert(backlight_write(INT_MAX, "brightness") < 0); + backlight_read(&val, "brightness"); + igt_assert(val == max / 2); +} + +static void test_fade(int max) +{ + int i; + static const struct timespec ts = { .tv_sec = 0, .tv_nsec = FADESPEED*1000000 }; + + /* Fade out, then in */ + for (i = max; i > 0; i -= max / FADESTEPS) { + test_and_verify(i); + nanosleep(&ts, NULL); + } + for (i = 0; i <= max; i += max / FADESTEPS) { + test_and_verify(i); + nanosleep(&ts, NULL); + } +} + +igt_main +{ + int max, old; + + igt_skip_on_simulation(); + + igt_fixture { + /* Get the max value and skip the whole test if sysfs interface not available */ + igt_skip_on(backlight_read(&old, "brightness")); + igt_assert(backlight_read(&max, "max_brightness") > -1); + } + + igt_subtest("brightness") + test_brightness(max); + igt_subtest("bad-brightness") + test_bad_brightness(max); + igt_subtest("fade") + test_fade(max); + + igt_fixture { + /* Restore old brightness */ + backlight_write(old, "brightness"); + } +}