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