1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Russell King
4  *  Rewritten from the dovefb driver, and Armada510 manuals.
5  */
6 #include <linux/ctype.h>
7 #include <linux/debugfs.h>
8 #include <linux/module.h>
9 #include <linux/seq_file.h>
10 #include <drm/drmP.h>
11 #include "armada_crtc.h"
12 #include "armada_drm.h"
13 
14 static int armada_debugfs_gem_linear_show(struct seq_file *m, void *data)
15 {
16 	struct drm_info_node *node = m->private;
17 	struct drm_device *dev = node->minor->dev;
18 	struct armada_private *priv = dev->dev_private;
19 	struct drm_printer p = drm_seq_file_printer(m);
20 
21 	mutex_lock(&priv->linear_lock);
22 	drm_mm_print(&priv->linear, &p);
23 	mutex_unlock(&priv->linear_lock);
24 
25 	return 0;
26 }
27 
28 static int armada_debugfs_reg_show(struct seq_file *m, void *data)
29 {
30 	struct drm_device *dev = m->private;
31 	struct armada_private *priv = dev->dev_private;
32 	int n, i;
33 
34 	if (priv) {
35 		for (n = 0; n < ARRAY_SIZE(priv->dcrtc); n++) {
36 			struct armada_crtc *dcrtc = priv->dcrtc[n];
37 			if (!dcrtc)
38 				continue;
39 
40 			for (i = 0x84; i <= 0x1c4; i += 4) {
41 				uint32_t v = readl_relaxed(dcrtc->base + i);
42 				seq_printf(m, "%u: 0x%04x: 0x%08x\n", n, i, v);
43 			}
44 		}
45 	}
46 
47 	return 0;
48 }
49 
50 static int armada_debugfs_reg_r_open(struct inode *inode, struct file *file)
51 {
52 	return single_open(file, armada_debugfs_reg_show, inode->i_private);
53 }
54 
55 static const struct file_operations fops_reg_r = {
56 	.owner = THIS_MODULE,
57 	.open = armada_debugfs_reg_r_open,
58 	.read = seq_read,
59 	.llseek = seq_lseek,
60 	.release = single_release,
61 };
62 
63 static int armada_debugfs_write(struct file *file, const char __user *ptr,
64 	size_t len, loff_t *off)
65 {
66 	struct drm_device *dev = file->private_data;
67 	struct armada_private *priv = dev->dev_private;
68 	struct armada_crtc *dcrtc = priv->dcrtc[0];
69 	char buf[32], *p;
70 	uint32_t reg, val;
71 	int ret;
72 
73 	if (*off != 0)
74 		return 0;
75 
76 	if (len > sizeof(buf) - 1)
77 		len = sizeof(buf) - 1;
78 
79 	ret = strncpy_from_user(buf, ptr, len);
80 	if (ret < 0)
81 		return ret;
82 	buf[len] = '\0';
83 
84 	reg = simple_strtoul(buf, &p, 16);
85 	if (!isspace(*p))
86 		return -EINVAL;
87 	val = simple_strtoul(p + 1, NULL, 16);
88 
89 	if (reg >= 0x84 && reg <= 0x1c4)
90 		writel(val, dcrtc->base + reg);
91 
92 	return len;
93 }
94 
95 static const struct file_operations fops_reg_w = {
96 	.owner = THIS_MODULE,
97 	.open = simple_open,
98 	.write = armada_debugfs_write,
99 	.llseek = noop_llseek,
100 };
101 
102 static struct drm_info_list armada_debugfs_list[] = {
103 	{ "gem_linear", armada_debugfs_gem_linear_show, 0 },
104 };
105 #define ARMADA_DEBUGFS_ENTRIES ARRAY_SIZE(armada_debugfs_list)
106 
107 int armada_drm_debugfs_init(struct drm_minor *minor)
108 {
109 	struct dentry *de;
110 	int ret;
111 
112 	ret = drm_debugfs_create_files(armada_debugfs_list,
113 				       ARMADA_DEBUGFS_ENTRIES,
114 				       minor->debugfs_root, minor);
115 	if (ret)
116 		return ret;
117 
118 	de = debugfs_create_file("reg", S_IFREG | S_IRUSR,
119 				 minor->debugfs_root, minor->dev, &fops_reg_r);
120 	if (!de)
121 		return -ENOMEM;
122 
123 	de = debugfs_create_file("reg_wr", S_IFREG | S_IWUSR,
124 				 minor->debugfs_root, minor->dev, &fops_reg_w);
125 	if (!de)
126 		return -ENOMEM;
127 
128 	return 0;
129 }
130