xref: /openbmc/linux/drivers/gpu/drm/msm/dp/dp_debug.c (revision 6726d552)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
4  */
5 
6 #define pr_fmt(fmt)"[drm-dp] %s: " fmt, __func__
7 
8 #include <linux/debugfs.h>
9 #include <drm/drm_connector.h>
10 #include <drm/drm_file.h>
11 
12 #include "dp_parser.h"
13 #include "dp_catalog.h"
14 #include "dp_aux.h"
15 #include "dp_ctrl.h"
16 #include "dp_debug.h"
17 #include "dp_display.h"
18 
19 #define DEBUG_NAME "msm_dp"
20 
21 struct dp_debug_private {
22 	struct dentry *root;
23 
24 	struct dp_usbpd *usbpd;
25 	struct dp_link *link;
26 	struct dp_panel *panel;
27 	struct drm_connector *connector;
28 	struct device *dev;
29 	struct drm_device *drm_dev;
30 
31 	struct dp_debug dp_debug;
32 };
33 
34 static int dp_debug_show(struct seq_file *seq, void *p)
35 {
36 	struct dp_debug_private *debug = seq->private;
37 	u64 lclk = 0;
38 	u32 link_params_rate;
39 	const struct drm_display_mode *drm_mode;
40 
41 	if (!debug)
42 		return -ENODEV;
43 
44 	drm_mode = &debug->panel->dp_mode.drm_mode;
45 
46 	seq_printf(seq, "\tname = %s\n", DEBUG_NAME);
47 	seq_printf(seq, "\tdrm_dp_link\n\t\trate = %u\n",
48 			debug->panel->link_info.rate);
49 	seq_printf(seq, "\t\tnum_lanes = %u\n",
50 			debug->panel->link_info.num_lanes);
51 	seq_printf(seq, "\t\tcapabilities = %lu\n",
52 			debug->panel->link_info.capabilities);
53 	seq_printf(seq, "\tdp_panel_info:\n\t\tactive = %dx%d\n",
54 			drm_mode->hdisplay,
55 			drm_mode->vdisplay);
56 	seq_printf(seq, "\t\tback_porch = %dx%d\n",
57 			drm_mode->htotal - drm_mode->hsync_end,
58 			drm_mode->vtotal - drm_mode->vsync_end);
59 	seq_printf(seq, "\t\tfront_porch = %dx%d\n",
60 			drm_mode->hsync_start - drm_mode->hdisplay,
61 			drm_mode->vsync_start - drm_mode->vdisplay);
62 	seq_printf(seq, "\t\tsync_width = %dx%d\n",
63 			drm_mode->hsync_end - drm_mode->hsync_start,
64 			drm_mode->vsync_end - drm_mode->vsync_start);
65 	seq_printf(seq, "\t\tactive_low = %dx%d\n",
66 			debug->panel->dp_mode.h_active_low,
67 			debug->panel->dp_mode.v_active_low);
68 	seq_printf(seq, "\t\th_skew = %d\n",
69 			drm_mode->hskew);
70 	seq_printf(seq, "\t\trefresh rate = %d\n",
71 			drm_mode_vrefresh(drm_mode));
72 	seq_printf(seq, "\t\tpixel clock khz = %d\n",
73 			drm_mode->clock);
74 	seq_printf(seq, "\t\tbpp = %d\n",
75 			debug->panel->dp_mode.bpp);
76 
77 	/* Link Information */
78 	seq_printf(seq, "\tdp_link:\n\t\ttest_requested = %d\n",
79 			debug->link->sink_request);
80 	seq_printf(seq, "\t\tnum_lanes = %d\n",
81 			debug->link->link_params.num_lanes);
82 	link_params_rate = debug->link->link_params.rate;
83 	seq_printf(seq, "\t\tbw_code = %d\n",
84 			drm_dp_link_rate_to_bw_code(link_params_rate));
85 	lclk = debug->link->link_params.rate * 1000;
86 	seq_printf(seq, "\t\tlclk = %lld\n", lclk);
87 	seq_printf(seq, "\t\tv_level = %d\n",
88 			debug->link->phy_params.v_level);
89 	seq_printf(seq, "\t\tp_level = %d\n",
90 			debug->link->phy_params.p_level);
91 
92 	return 0;
93 }
94 DEFINE_SHOW_ATTRIBUTE(dp_debug);
95 
96 static int dp_test_data_show(struct seq_file *m, void *data)
97 {
98 	const struct dp_debug_private *debug = m->private;
99 	const struct drm_connector *connector = debug->connector;
100 	u32 bpc;
101 
102 	if (connector->status == connector_status_connected) {
103 		bpc = debug->link->test_video.test_bit_depth;
104 		seq_printf(m, "hdisplay: %d\n",
105 				debug->link->test_video.test_h_width);
106 		seq_printf(m, "vdisplay: %d\n",
107 				debug->link->test_video.test_v_height);
108 		seq_printf(m, "bpc: %u\n",
109 				dp_link_bit_depth_to_bpc(bpc));
110 	} else {
111 		seq_puts(m, "0");
112 	}
113 
114 	return 0;
115 }
116 DEFINE_SHOW_ATTRIBUTE(dp_test_data);
117 
118 static int dp_test_type_show(struct seq_file *m, void *data)
119 {
120 	const struct dp_debug_private *debug = m->private;
121 	const struct drm_connector *connector = debug->connector;
122 
123 	if (connector->status == connector_status_connected)
124 		seq_printf(m, "%02x", DP_TEST_LINK_VIDEO_PATTERN);
125 	else
126 		seq_puts(m, "0");
127 
128 	return 0;
129 }
130 DEFINE_SHOW_ATTRIBUTE(dp_test_type);
131 
132 static ssize_t dp_test_active_write(struct file *file,
133 		const char __user *ubuf,
134 		size_t len, loff_t *offp)
135 {
136 	char *input_buffer;
137 	int status = 0;
138 	const struct dp_debug_private *debug;
139 	const struct drm_connector *connector;
140 	int val = 0;
141 
142 	debug = ((struct seq_file *)file->private_data)->private;
143 	connector = debug->connector;
144 
145 	if (len == 0)
146 		return 0;
147 
148 	input_buffer = memdup_user_nul(ubuf, len);
149 	if (IS_ERR(input_buffer))
150 		return PTR_ERR(input_buffer);
151 
152 	DRM_DEBUG_DRIVER("Copied %d bytes from user\n", (unsigned int)len);
153 
154 	if (connector->status == connector_status_connected) {
155 		status = kstrtoint(input_buffer, 10, &val);
156 		if (status < 0) {
157 			kfree(input_buffer);
158 			return status;
159 		}
160 		DRM_DEBUG_DRIVER("Got %d for test active\n", val);
161 		/* To prevent erroneous activation of the compliance
162 		 * testing code, only accept an actual value of 1 here
163 		 */
164 		if (val == 1)
165 			debug->panel->video_test = true;
166 		else
167 			debug->panel->video_test = false;
168 	}
169 	kfree(input_buffer);
170 
171 	*offp += len;
172 	return len;
173 }
174 
175 static int dp_test_active_show(struct seq_file *m, void *data)
176 {
177 	struct dp_debug_private *debug = m->private;
178 	struct drm_connector *connector = debug->connector;
179 
180 	if (connector->status == connector_status_connected) {
181 		if (debug->panel->video_test)
182 			seq_puts(m, "1");
183 		else
184 			seq_puts(m, "0");
185 	} else {
186 		seq_puts(m, "0");
187 	}
188 
189 	return 0;
190 }
191 
192 static int dp_test_active_open(struct inode *inode,
193 		struct file *file)
194 {
195 	return single_open(file, dp_test_active_show,
196 			inode->i_private);
197 }
198 
199 static const struct file_operations test_active_fops = {
200 	.owner = THIS_MODULE,
201 	.open = dp_test_active_open,
202 	.read = seq_read,
203 	.llseek = seq_lseek,
204 	.release = single_release,
205 	.write = dp_test_active_write
206 };
207 
208 static void dp_debug_init(struct dp_debug *dp_debug, struct drm_minor *minor)
209 {
210 	char path[64];
211 	struct dp_debug_private *debug = container_of(dp_debug,
212 			struct dp_debug_private, dp_debug);
213 
214 	snprintf(path, sizeof(path), "msm_dp-%s", debug->connector->name);
215 
216 	debug->root = debugfs_create_dir(path, minor->debugfs_root);
217 
218 	debugfs_create_file("dp_debug", 0444, debug->root,
219 			debug, &dp_debug_fops);
220 
221 	debugfs_create_file("msm_dp_test_active", 0444,
222 			debug->root,
223 			debug, &test_active_fops);
224 
225 	debugfs_create_file("msm_dp_test_data", 0444,
226 			debug->root,
227 			debug, &dp_test_data_fops);
228 
229 	debugfs_create_file("msm_dp_test_type", 0444,
230 			debug->root,
231 			debug, &dp_test_type_fops);
232 }
233 
234 struct dp_debug *dp_debug_get(struct device *dev, struct dp_panel *panel,
235 		struct dp_usbpd *usbpd, struct dp_link *link,
236 		struct drm_connector *connector, struct drm_minor *minor)
237 {
238 	struct dp_debug_private *debug;
239 	struct dp_debug *dp_debug;
240 	int rc;
241 
242 	if (!dev || !panel || !usbpd || !link) {
243 		DRM_ERROR("invalid input\n");
244 		rc = -EINVAL;
245 		goto error;
246 	}
247 
248 	debug = devm_kzalloc(dev, sizeof(*debug), GFP_KERNEL);
249 	if (!debug) {
250 		rc = -ENOMEM;
251 		goto error;
252 	}
253 
254 	debug->dp_debug.debug_en = false;
255 	debug->usbpd = usbpd;
256 	debug->link = link;
257 	debug->panel = panel;
258 	debug->dev = dev;
259 	debug->drm_dev = minor->dev;
260 	debug->connector = connector;
261 
262 	dp_debug = &debug->dp_debug;
263 	dp_debug->vdisplay = 0;
264 	dp_debug->hdisplay = 0;
265 	dp_debug->vrefresh = 0;
266 
267 	dp_debug_init(dp_debug, minor);
268 
269 	return dp_debug;
270  error:
271 	return ERR_PTR(rc);
272 }
273 
274 static int dp_debug_deinit(struct dp_debug *dp_debug)
275 {
276 	struct dp_debug_private *debug;
277 
278 	if (!dp_debug)
279 		return -EINVAL;
280 
281 	debug = container_of(dp_debug, struct dp_debug_private, dp_debug);
282 
283 	debugfs_remove_recursive(debug->root);
284 
285 	return 0;
286 }
287 
288 void dp_debug_put(struct dp_debug *dp_debug)
289 {
290 	struct dp_debug_private *debug;
291 
292 	if (!dp_debug)
293 		return;
294 
295 	debug = container_of(dp_debug, struct dp_debug_private, dp_debug);
296 
297 	dp_debug_deinit(dp_debug);
298 
299 	devm_kfree(debug->dev, debug);
300 }
301