1 /*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Author:
24 * Antti Koskipaa <antti.koskipaa@linux.intel.com>
25 *
26 */
27
28 #include "igt.h"
29 #include <limits.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <time.h>
37
38 struct context {
39 int max;
40 };
41
42
43 #define TOLERANCE 5 /* percent */
44 #define BACKLIGHT_PATH "/sys/class/backlight/intel_backlight"
45
46 #define FADESTEPS 10
47 #define FADESPEED 100 /* milliseconds between steps */
48
49 IGT_TEST_DESCRIPTION("Basic backlight sysfs test");
50 static int8_t *pm_data = NULL;
51
backlight_read(int * result,const char * fname)52 static int backlight_read(int *result, const char *fname)
53 {
54 int fd;
55 char full[PATH_MAX];
56 char dst[64];
57 int r, e;
58
59 igt_assert(snprintf(full, PATH_MAX, "%s/%s", BACKLIGHT_PATH, fname) < PATH_MAX);
60
61 fd = open(full, O_RDONLY);
62 if (fd == -1)
63 return -errno;
64
65 r = read(fd, dst, sizeof(dst));
66 e = errno;
67 close(fd);
68
69 if (r < 0)
70 return -e;
71
72 errno = 0;
73 *result = strtol(dst, NULL, 10);
74 return errno;
75 }
76
backlight_write(int value,const char * fname)77 static int backlight_write(int value, const char *fname)
78 {
79 int fd;
80 char full[PATH_MAX];
81 char src[64];
82 int len;
83
84 igt_assert(snprintf(full, PATH_MAX, "%s/%s", BACKLIGHT_PATH, fname) < PATH_MAX);
85 fd = open(full, O_WRONLY);
86 if (fd == -1)
87 return -errno;
88
89 len = snprintf(src, sizeof(src), "%i", value);
90 len = write(fd, src, len);
91 close(fd);
92
93 if (len < 0)
94 return len;
95
96 return 0;
97 }
98
test_and_verify(struct context * context,int val)99 static void test_and_verify(struct context *context, int val)
100 {
101 const int tolerance = val * TOLERANCE / 100;
102 int result;
103
104 igt_assert_eq(backlight_write(val, "brightness"), 0);
105 igt_assert_eq(backlight_read(&result, "brightness"), 0);
106 /* Check that the exact value sticks */
107 igt_assert_eq(result, val);
108
109 igt_assert_eq(backlight_read(&result, "actual_brightness"), 0);
110 /* Some rounding may happen depending on hw */
111 igt_assert_f(result >= max(0, val - tolerance) &&
112 result <= min(context->max, val + tolerance),
113 "actual_brightness [%d] did not match expected brightness [%d +- %d]\n",
114 result, val, tolerance);
115 }
116
test_brightness(struct context * context)117 static void test_brightness(struct context *context)
118 {
119 test_and_verify(context, 0);
120 test_and_verify(context, context->max);
121 test_and_verify(context, context->max / 2);
122 }
123
test_bad_brightness(struct context * context)124 static void test_bad_brightness(struct context *context)
125 {
126 int val;
127 /* First write some sane value */
128 backlight_write(context->max / 2, "brightness");
129 /* Writing invalid values should fail and not change the value */
130 igt_assert_lt(backlight_write(-1, "brightness"), 0);
131 backlight_read(&val, "brightness");
132 igt_assert_eq(val, context->max / 2);
133 igt_assert_lt(backlight_write(context->max + 1, "brightness"), 0);
134 backlight_read(&val, "brightness");
135 igt_assert_eq(val, context->max / 2);
136 igt_assert_lt(backlight_write(INT_MAX, "brightness"), 0);
137 backlight_read(&val, "brightness");
138 igt_assert_eq(val, context->max / 2);
139 }
140
test_fade(struct context * context)141 static void test_fade(struct context *context)
142 {
143 int i;
144 static const struct timespec ts = { .tv_sec = 0, .tv_nsec = FADESPEED*1000000 };
145
146 /* Fade out, then in */
147 for (i = context->max; i > 0; i -= context->max / FADESTEPS) {
148 test_and_verify(context, i);
149 nanosleep(&ts, NULL);
150 }
151 for (i = 0; i <= context->max; i += context->max / FADESTEPS) {
152 test_and_verify(context, i);
153 nanosleep(&ts, NULL);
154 }
155 }
156
157 static void
test_fade_with_dpms(struct context * context,igt_output_t * output)158 test_fade_with_dpms(struct context *context, igt_output_t *output)
159 {
160 igt_require(igt_setup_runtime_pm());
161
162 kmstest_set_connector_dpms(output->display->drm_fd,
163 output->config.connector,
164 DRM_MODE_DPMS_OFF);
165 igt_require(igt_wait_for_pm_status(IGT_RUNTIME_PM_STATUS_SUSPENDED));
166
167 kmstest_set_connector_dpms(output->display->drm_fd,
168 output->config.connector,
169 DRM_MODE_DPMS_ON);
170 igt_assert(igt_wait_for_pm_status(IGT_RUNTIME_PM_STATUS_ACTIVE));
171
172 test_fade(context);
173 }
174
175 static void
test_fade_with_suspend(struct context * context,igt_output_t * output)176 test_fade_with_suspend(struct context *context, igt_output_t *output)
177 {
178 igt_system_suspend_autoresume(SUSPEND_STATE_MEM, SUSPEND_TEST_NONE);
179
180 test_fade(context);
181 }
182
183 igt_main
184 {
185 struct context context = {0};
186 int old;
187 igt_display_t display;
188 igt_output_t *output;
189 struct igt_fb fb;
190
191 igt_skip_on_simulation();
192
193 igt_fixture {
194 enum pipe pipe;
195 bool found = false;
196 char full_name[32] = {};
197 char *name;
198 drmModeModeInfo *mode;
199 igt_plane_t *primary;
200
201 /* Get the max value and skip the whole test if sysfs interface not available */
202 igt_skip_on(backlight_read(&old, "brightness"));
203 igt_assert(backlight_read(&context.max, "max_brightness") > -1);
204
205 /*
206 * Backlight tests requires the output to be enabled,
207 * try to enable all.
208 */
209 kmstest_set_vt_graphics_mode();
210 igt_display_require(&display, drm_open_driver(DRIVER_INTEL));
211
212 /* should be ../../cardX-$output */
213 igt_assert_lt(12, readlink(BACKLIGHT_PATH "/device", full_name, sizeof(full_name) - 1));
214 name = basename(full_name);
215
216 for_each_pipe_with_valid_output(&display, pipe, output) {
217 if (strcmp(name + 6, output->name))
218 continue;
219 found = true;
220 break;
221 }
222
223 igt_require_f(found,
224 "Could not map backlight for \"%s\" to connected output\n",
225 name);
226
227 igt_output_set_pipe(output, pipe);
228 mode = igt_output_get_mode(output);
229
230 igt_create_pattern_fb(display.drm_fd,
231 mode->hdisplay, mode->vdisplay,
232 DRM_FORMAT_XRGB8888,
233 LOCAL_DRM_FORMAT_MOD_NONE, &fb);
234 primary = igt_output_get_plane_type(output, DRM_PLANE_TYPE_PRIMARY);
235 igt_plane_set_fb(primary, &fb);
236
237 igt_display_commit2(&display, display.is_atomic ? COMMIT_ATOMIC : COMMIT_LEGACY);
238 pm_data = igt_pm_enable_sata_link_power_management();
239 }
240
241 igt_subtest("basic-brightness")
242 test_brightness(&context);
243 igt_subtest("bad-brightness")
244 test_bad_brightness(&context);
245 igt_subtest("fade")
246 test_fade(&context);
247 igt_subtest("fade_with_dpms")
248 test_fade_with_dpms(&context, output);
249 igt_subtest("fade_with_suspend")
250 test_fade_with_suspend(&context, output);
251
252 igt_fixture {
253 /* Restore old brightness */
254 backlight_write(old, "brightness");
255
256 igt_display_fini(&display);
257 igt_remove_fb(display.drm_fd, &fb);
258 igt_pm_restore_sata_link_power_management(pm_data);
259 free(pm_data);
260 close(display.drm_fd);
261 }
262 }
263