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