summaryrefslogtreecommitdiffstats
path: root/commands/of_display_timings.c
blob: 4e5ec223b7d0b9e5e005d2b82c910ef15b18cfea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-License-Identifier: GPL-2.0-only
// SPDX-FileCopyrightText: © 2014 Teresa Gámez <t.gamez@phytec.de>, PHYTEC Messtechnik GmbH

/* of_display_timings.c - list and select display_timings */

#include <common.h>
#include <filetype.h>
#include <libfile.h>
#include <of.h>
#include <command.h>
#include <malloc.h>
#include <complete.h>
#include <asm/byteorder.h>
#include <getopt.h>
#include <linux/err.h>
#include <string.h>

static int of_display_timing(struct device_node *root, void *timingpath)
{
	int ret = 0;
	struct device_node *display;

	display = of_find_node_by_path_from(root, timingpath);
	if (!display) {
		pr_err("Path to display-timings is not vaild.\n");
		ret = -EINVAL;
		goto out;
	}

	ret = of_set_property_to_child_phandle(display, "native-mode");
	if (ret)
		pr_err("Could not set display\n");
out:
	return ret;
}

static int do_of_display_timings(int argc, char *argv[])
{
	int opt;
	int list = 0;
	int selected = 0;
	struct device_node *root = NULL;
	struct device_node *display = NULL;
	struct device_node *timings = NULL;
	char *timingpath = NULL;
	char *dtbfile = NULL;

	while ((opt = getopt(argc, argv, "sS:lf:")) > 0) {
		switch (opt) {
		case 'l':
			list = 1;
			break;
		case 'f':
			dtbfile = optarg;
			break;
		case 's':
			selected = 1;
			break;
		case 'S':
			timingpath = xzalloc(strlen(optarg) + 1);
			strcpy(timingpath, optarg);
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	/* Check if external dtb given */
	if (dtbfile) {
		void *fdt;
		size_t size;

		fdt = read_file(dtbfile, &size);
		if (!fdt) {
			pr_err("unable to read %s: %s\n", dtbfile,
				strerror(errno));
			return -errno;
		}

		if (file_detect_type(fdt, size) != filetype_oftree) {
			pr_err("%s is not a oftree file.\n", dtbfile);
			free(fdt);
			return -EINVAL;
		}

		root = of_unflatten_dtb(fdt, size);

		free(fdt);

		if (IS_ERR(root))
			return PTR_ERR(root);

	} else {
		root = of_get_root_node();
	}

	if (list) {
		int found = 0;
		const char *node = "display-timings";

		for_each_node_by_name_from(display, root, node) {
			for_each_child_of_node(display, timings) {
				printf("%s\n", timings->full_name);
				found = 1;
			}
		}

		if (!found)
			printf("No display-timings found.\n");
	}

	if (selected) {
		int found = 0;
		const char *node = "display-timings";

		for_each_node_by_name_from(display, root, node) {
			timings = of_parse_phandle_from(display, root,
							"native-mode", 0);
			if (!timings)
				continue;

			printf("%s\n", timings->full_name);
			found = 1;
		}

		if (!found)
			printf("No selected display-timings found.\n");
	}

	if (timingpath)
		of_register_fixup(of_display_timing, timingpath);

	return 0;
}

BAREBOX_CMD_HELP_START(of_display_timings)
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT("-l",  "list path of all available display-timings")
BAREBOX_CMD_HELP_OPT("-s",  "list path of all selected display-timings")
BAREBOX_CMD_HELP_OPT("-S path",  "select display-timings and register oftree fixup")
BAREBOX_CMD_HELP_OPT("-f dtb",  "work on dtb. Has no effect on -s option")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(of_display_timings)
	.cmd	= do_of_display_timings,
	BAREBOX_CMD_DESC("print and select display-timings")
	BAREBOX_CMD_OPTS("[-ls] [-S path] [-f dtb]")
	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
	BAREBOX_CMD_COMPLETE(devicetree_file_complete)
	BAREBOX_CMD_HELP(cmd_of_display_timings_help)
BAREBOX_CMD_END