xref: /aosp_15_r20/external/igt-gpu-tools/tools/dpcd_reg.c (revision d83cc019efdc2edc6c4b16e9034a3ceb8d35d77c)
1 /*
2  * Copyright © 2018 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 FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * DPCD register read/write tool
24  * This tool wraps around DRM_DP_AUX_DEV module to provide DPCD register read
25  * and write, so CONFIG_DRM_DP_AUX_DEV needs to be set.
26  */
27 
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <getopt.h>
34 #include <stdint.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <stdbool.h>
38 
39 #define MAX_DP_OFFSET	0xfffff
40 #define DRM_AUX_MINORS	256
41 
42 const char aux_dev[] = "/dev/drm_dp_aux";
43 
44 struct dpcd_block {
45 	/* DPCD dump start address. */
46 	uint32_t offset;
47 	/* DPCD number of bytes to read. If unset, defaults to 1. */
48 	size_t count;
49 };
50 
51 struct dpcd_data {
52 	int devid;
53 	int file_op;
54 	struct dpcd_block rw;
55 	enum command {
56 		DUMP,
57 		READ,
58 		WRITE,
59 	} cmd;
60 	uint8_t val;
61 };
62 
63 static const struct dpcd_block dump_list[] = {
64 	/* DP_DPCD_REV */
65 	{ .offset = 0, .count = 15 },
66 	/* DP_PSR_SUPPORT to DP_PSR_CAPS*/
67 	{ .offset =  0x70, .count = 2 },
68 	/* DP_DOWNSTREAM_PORT_0 */
69 	{ .offset =  0x80, .count = 16 },
70 	/* DP_LINK_BW_SET to DP_EDP_CONFIGURATION_SET */
71 	{ .offset = 0x100, .count = 11 },
72 	/* DP_SINK_COUNT to DP_ADJUST_REQUEST_LANE2_3 */
73 	{ .offset = 0x200, .count = 8 },
74 	/* DP_SET_POWER */
75 	{ .offset = 0x600 },
76 	/* DP_EDP_DPCD_REV */
77 	{ .offset =  0x700 },
78 	/* DP_EDP_GENERAL_CAP_1 to DP_EDP_GENERAL_CAP_3 */
79 	{ .offset = 0x701, .count = 4 },
80 	/* DP_EDP_DISPLAY_CONTROL_REGISTER to DP_EDP_BACKLIGHT_FREQ_CAP_MAX_LSB */
81 	{ .offset = 0x720, .count = 16},
82 	/* DP_EDP_DBC_MINIMUM_BRIGHTNESS_SET to DP_EDP_DBC_MAXIMUM_BRIGHTNESS_SET */
83 	{ .offset =  0x732, .count = 2 },
84 	/* DP_PSR_STATUS to DP_PSR_STATUS */
85 	{ .offset = 0x2008, .count = 1 },
86 };
87 
print_usage(void)88 static void print_usage(void)
89 {
90 	printf("Usage: dpcd_reg [OPTION ...] COMMAND\n\n");
91 	printf("COMMAND is one of:\n");
92 	printf("  read:		Read [count] bytes dpcd reg at an offset\n");
93 	printf("  write:	Write a dpcd reg at an offset\n\n");
94 	printf("Options for the above COMMANDS are\n");
95 	printf(" --device=DEVID		Aux device id, as listed in /dev/drm_dp_aux_dev[n]. Defaults to 0. Upper limit - 256\n");
96 	printf(" --offset=REG_ADDR	DPCD register offset in hex. Defaults to 0x0. Upper limit - 0xfffff\n");
97 	printf(" --count=BYTES		For reads, specify number of bytes to be read from the offset. Defaults to 1\n");
98 	printf(" --value		For writes, specify a hex value to be written. Upper limit - 0xff\n\n");
99 
100 	printf(" --help: print the usage\n");
101 }
102 
strtol_err_util(char * endptr,long * val)103 static inline bool strtol_err_util(char *endptr, long *val)
104 {
105 	if (*endptr != '\0' || *val < 0 ||
106 	    (*val == LONG_MAX && errno == ERANGE))
107 		return true;
108 
109 	return false;
110 }
111 
parse_opts(struct dpcd_data * dpcd,int argc,char ** argv)112 static int parse_opts(struct dpcd_data *dpcd, int argc, char **argv)
113 {
114 	int ret, vflag = 0;
115 	long temp;
116 	char *endptr;
117 
118 	struct option longopts[] = {
119 		{ "count",	required_argument,	NULL,		'c' },
120 		{ "device",	required_argument,	NULL,		'd' },
121 		{ "help",	no_argument,		NULL,		'h' },
122 		{ "offset",	required_argument,	NULL,		'o' },
123 		{ "value",	required_argument,	NULL,		'v' },
124 		{ 0 }
125 	};
126 
127 	while ((ret = getopt_long(argc, argv, "-:c:d:ho:v:", longopts, NULL)) != -1) {
128 		switch (ret) {
129 		case 'c':
130 			temp = strtol(optarg, &endptr, 10);
131 			if (strtol_err_util(endptr, &temp)) {
132 				fprintf(stderr,
133 					"--count argument is invalid/negative/out-of-range\n");
134 				print_usage();
135 				return EXIT_FAILURE;
136 			}
137 			dpcd->rw.count = temp;
138 			break;
139 		case 'd':
140 			temp = strtol(optarg, &endptr, 10);
141 			if (strtol_err_util(endptr, &temp) ||
142 					    temp > DRM_AUX_MINORS) {
143 				fprintf(stderr,
144 					"--device argument is invalid/negative/out-of-range\n");
145 				print_usage();
146 				return ERANGE;
147 			}
148 			dpcd->devid = temp;
149 			break;
150 		case 'h':
151 			printf("DPCD register read and write tool\n\n");
152 			printf("This tool requires CONFIG_DRM_DP_AUX_CHARDEV\n"
153 				"to be set in the kernel config.\n\n");
154 			print_usage();
155 			exit(EXIT_SUCCESS);
156 		case 'o':
157 			temp = strtol(optarg, &endptr, 16);
158 			if (strtol_err_util(endptr, &temp) ||
159 					    temp > MAX_DP_OFFSET) {
160 				fprintf(stderr,
161 					"--offset argument is invalid/negative/out-of-range\n");
162 				print_usage();
163 				return ERANGE;
164 			}
165 			dpcd->rw.offset = temp;
166 			break;
167 		case 'v':
168 			vflag = 'v';
169 			temp = strtol(optarg, &endptr, 16);
170 			if (strtol_err_util(endptr, &temp) || temp > 0xff) {
171 				fprintf(stderr,
172 					"--value argument is invalid/negative/out-of-range\n");
173 				print_usage();
174 				return ERANGE;
175 			}
176 			dpcd->val = temp;
177 			break;
178 		/* Command parsing */
179 		case 1:
180 			if (strcmp(optarg, "read") == 0) {
181 				dpcd->cmd = READ;
182 			} else if (strcmp(optarg, "write") == 0) {
183 				dpcd->cmd = WRITE;
184 				dpcd->file_op = O_WRONLY;
185 			} else if (strcmp(optarg, "dump") != 0) {
186 				fprintf(stderr, "Unrecognized command\n");
187 				print_usage();
188 				return EXIT_FAILURE;
189 			}
190 			break;
191 		case ':':
192 			fprintf(stderr, "Option -%c requires an argument\n",
193 				 optopt);
194 			print_usage();
195 			return EXIT_FAILURE;
196 		default:
197 			fprintf(stderr, "Invalid option\n");
198 			print_usage();
199 			return EXIT_FAILURE;
200 		}
201 	}
202 
203 	if ((dpcd->rw.count + dpcd->rw.offset) > (MAX_DP_OFFSET + 1)) {
204 		fprintf(stderr, "Out of bounds. Count + Offset <= 0x100000\n");
205 		return ERANGE;
206 	}
207 
208 	if ((dpcd->cmd == WRITE) && (vflag != 'v')) {
209 		fprintf(stderr, "Write value is missing\n");
210 		print_usage();
211 		return EXIT_FAILURE;
212 	}
213 
214 	return EXIT_SUCCESS;
215 }
216 
dpcd_read(int fd,uint32_t offset,size_t count)217 static int dpcd_read(int fd, uint32_t offset, size_t count)
218 {
219 	int ret = EXIT_SUCCESS, pret, i;
220 	uint8_t *buf = calloc(count, sizeof(uint8_t));
221 
222 	if (!buf) {
223 		fprintf(stderr, "Can't allocate read buffer\n");
224 		return ENOMEM;
225 	}
226 
227 	pret = pread(fd, buf, count, offset);
228 	if (pret < 0) {
229 		fprintf(stderr, "Failed to read - %s\n", strerror(errno));
230 		ret = errno;
231 		goto out;
232 	}
233 
234 	if (pret < count) {
235 		fprintf(stderr,
236 			"Read %u byte(s), expected %zu bytes, starting at offset %x\n\n", pret, count, offset);
237 		ret = EXIT_FAILURE;
238 	}
239 
240 	printf("0x%04x: ", offset);
241 	for (i = 0; i < pret; i++)
242 		printf(" %02x", *(buf + i));
243 	printf("\n");
244 
245 out:
246 	free(buf);
247 	return ret;
248 }
249 
dpcd_write(int fd,uint32_t offset,uint8_t val)250 static int dpcd_write(int fd, uint32_t offset, uint8_t val)
251 {
252 	int ret = EXIT_SUCCESS, pret;
253 
254 	pret = pwrite(fd, (const void *)&val, sizeof(uint8_t), offset);
255 	if (pret < 0) {
256 		fprintf(stderr, "Failed to write - %s\n", strerror(errno));
257 		ret = errno;
258 	} else if (pret == 0) {
259 		fprintf(stderr, "Zero bytes were written\n");
260 		ret = EXIT_FAILURE;
261 	}
262 
263 	return ret;
264 }
265 
dpcd_dump(int fd)266 static int dpcd_dump(int fd)
267 {
268 	size_t count;
269 	int ret, i;
270 
271 	for (i = 0; i < sizeof(dump_list) / sizeof(dump_list[0]); i++) {
272 		count = dump_list[i].count ? dump_list[i].count: 1;
273 		ret = dpcd_read(fd, dump_list[i].offset, count);
274 		if (ret != EXIT_SUCCESS) {
275 			fprintf(stderr, "Dump failed while reading %04x\n",
276 				 dump_list[i].offset);
277 			return ret;
278 		}
279 	}
280 	return ret;
281 }
282 
main(int argc,char ** argv)283 int main(int argc, char **argv)
284 {
285 	char dev_name[20];
286 	int ret, fd;
287 
288 	struct dpcd_data dpcd = {
289 		.devid = 0,
290 		.file_op = O_RDONLY,
291 		.rw.offset = 0x0,
292 		.rw.count = 1,
293 		.cmd = DUMP,
294 	};
295 
296 	ret = parse_opts(&dpcd, argc, argv);
297 	if (ret != EXIT_SUCCESS)
298 		return ret;
299 
300 	snprintf(dev_name, strlen(aux_dev) + 4, "%s%d", aux_dev, dpcd.devid);
301 
302 	fd = open(dev_name, dpcd.file_op);
303 	if (fd < 0) {
304 		fprintf(stderr,
305 			"Failed to open %s aux device - error: %s\n", dev_name,
306 			strerror(errno));
307 		return errno;
308 	}
309 
310 	switch (dpcd.cmd) {
311 	case READ:
312 		ret = dpcd_read(fd, dpcd.rw.offset, dpcd.rw.count);
313 		break;
314 	case WRITE:
315 		ret = dpcd_write(fd, dpcd.rw.offset, dpcd.val);
316 		break;
317 	case DUMP:
318 	default:
319 		ret = dpcd_dump(fd);
320 		break;
321 	}
322 
323 	close(fd);
324 
325 	return ret;
326 }
327