xref: /openbmc/linux/drivers/soundwire/debugfs.c (revision 86e281fc)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2017-2019 Intel Corporation.
3 
4 #include <linux/device.h>
5 #include <linux/debugfs.h>
6 #include <linux/mod_devicetable.h>
7 #include <linux/pm_runtime.h>
8 #include <linux/slab.h>
9 #include <linux/soundwire/sdw.h>
10 #include <linux/soundwire/sdw_registers.h>
11 #include "bus.h"
12 
13 static struct dentry *sdw_debugfs_root;
14 
15 void sdw_bus_debugfs_init(struct sdw_bus *bus)
16 {
17 	char name[16];
18 
19 	if (!sdw_debugfs_root)
20 		return;
21 
22 	/* create the debugfs master-N */
23 	snprintf(name, sizeof(name), "master-%d-%d", bus->id, bus->link_id);
24 	bus->debugfs = debugfs_create_dir(name, sdw_debugfs_root);
25 }
26 
27 void sdw_bus_debugfs_exit(struct sdw_bus *bus)
28 {
29 	debugfs_remove_recursive(bus->debugfs);
30 }
31 
32 #define RD_BUF (3 * PAGE_SIZE)
33 
34 static ssize_t sdw_sprintf(struct sdw_slave *slave,
35 			   char *buf, size_t pos, unsigned int reg)
36 {
37 	int value;
38 
39 	value = sdw_read_no_pm(slave, reg);
40 
41 	if (value < 0)
42 		return scnprintf(buf + pos, RD_BUF - pos, "%3x\tXX\n", reg);
43 	else
44 		return scnprintf(buf + pos, RD_BUF - pos,
45 				"%3x\t%2x\n", reg, value);
46 }
47 
48 static int sdw_slave_reg_show(struct seq_file *s_file, void *data)
49 {
50 	struct sdw_slave *slave = s_file->private;
51 	char *buf;
52 	ssize_t ret;
53 	int i, j;
54 
55 	buf = kzalloc(RD_BUF, GFP_KERNEL);
56 	if (!buf)
57 		return -ENOMEM;
58 
59 	ret = pm_runtime_get_sync(&slave->dev);
60 	if (ret < 0 && ret != -EACCES) {
61 		pm_runtime_put_noidle(&slave->dev);
62 		kfree(buf);
63 		return ret;
64 	}
65 
66 	ret = scnprintf(buf, RD_BUF, "Register  Value\n");
67 
68 	/* DP0 non-banked registers */
69 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nDP0\n");
70 	for (i = SDW_DP0_INT; i <= SDW_DP0_PREPARECTRL; i++)
71 		ret += sdw_sprintf(slave, buf, ret, i);
72 
73 	/* DP0 Bank 0 registers */
74 	ret += scnprintf(buf + ret, RD_BUF - ret, "Bank0\n");
75 	ret += sdw_sprintf(slave, buf, ret, SDW_DP0_CHANNELEN);
76 	for (i = SDW_DP0_SAMPLECTRL1; i <= SDW_DP0_LANECTRL; i++)
77 		ret += sdw_sprintf(slave, buf, ret, i);
78 
79 	/* DP0 Bank 1 registers */
80 	ret += scnprintf(buf + ret, RD_BUF - ret, "Bank1\n");
81 	ret += sdw_sprintf(slave, buf, ret,
82 			SDW_DP0_CHANNELEN + SDW_BANK1_OFFSET);
83 	for (i = SDW_DP0_SAMPLECTRL1 + SDW_BANK1_OFFSET;
84 			i <= SDW_DP0_LANECTRL + SDW_BANK1_OFFSET; i++)
85 		ret += sdw_sprintf(slave, buf, ret, i);
86 
87 	/* SCP registers */
88 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nSCP\n");
89 	for (i = SDW_SCP_INT1; i <= SDW_SCP_BUS_CLOCK_BASE; i++)
90 		ret += sdw_sprintf(slave, buf, ret, i);
91 	for (i = SDW_SCP_DEVID_0; i <= SDW_SCP_DEVID_5; i++)
92 		ret += sdw_sprintf(slave, buf, ret, i);
93 	for (i = SDW_SCP_FRAMECTRL_B0; i <= SDW_SCP_BUSCLOCK_SCALE_B0; i++)
94 		ret += sdw_sprintf(slave, buf, ret, i);
95 	for (i = SDW_SCP_FRAMECTRL_B1; i <= SDW_SCP_BUSCLOCK_SCALE_B1; i++)
96 		ret += sdw_sprintf(slave, buf, ret, i);
97 	for (i = SDW_SCP_PHY_OUT_CTRL_0; i <= SDW_SCP_PHY_OUT_CTRL_7; i++)
98 		ret += sdw_sprintf(slave, buf, ret, i);
99 
100 
101 	/*
102 	 * SCP Bank 0/1 registers are read-only and cannot be
103 	 * retrieved from the Slave. The Master typically keeps track
104 	 * of the current frame size so the information can be found
105 	 * in other places
106 	 */
107 
108 	/* DP1..14 registers */
109 	for (i = 1; SDW_VALID_PORT_RANGE(i); i++) {
110 
111 		/* DPi registers */
112 		ret += scnprintf(buf + ret, RD_BUF - ret, "\nDP%d\n", i);
113 		for (j = SDW_DPN_INT(i); j <= SDW_DPN_PREPARECTRL(i); j++)
114 			ret += sdw_sprintf(slave, buf, ret, j);
115 
116 		/* DPi Bank0 registers */
117 		ret += scnprintf(buf + ret, RD_BUF - ret, "Bank0\n");
118 		for (j = SDW_DPN_CHANNELEN_B0(i);
119 		     j <= SDW_DPN_LANECTRL_B0(i); j++)
120 			ret += sdw_sprintf(slave, buf, ret, j);
121 
122 		/* DPi Bank1 registers */
123 		ret += scnprintf(buf + ret, RD_BUF - ret, "Bank1\n");
124 		for (j = SDW_DPN_CHANNELEN_B1(i);
125 		     j <= SDW_DPN_LANECTRL_B1(i); j++)
126 			ret += sdw_sprintf(slave, buf, ret, j);
127 	}
128 
129 	seq_printf(s_file, "%s", buf);
130 
131 	pm_runtime_mark_last_busy(&slave->dev);
132 	pm_runtime_put(&slave->dev);
133 
134 	kfree(buf);
135 
136 	return 0;
137 }
138 DEFINE_SHOW_ATTRIBUTE(sdw_slave_reg);
139 
140 void sdw_slave_debugfs_init(struct sdw_slave *slave)
141 {
142 	struct dentry *master;
143 	struct dentry *d;
144 	char name[32];
145 
146 	master = slave->bus->debugfs;
147 
148 	/* create the debugfs slave-name */
149 	snprintf(name, sizeof(name), "%s", dev_name(&slave->dev));
150 	d = debugfs_create_dir(name, master);
151 
152 	debugfs_create_file("registers", 0400, d, slave, &sdw_slave_reg_fops);
153 
154 	slave->debugfs = d;
155 }
156 
157 void sdw_slave_debugfs_exit(struct sdw_slave *slave)
158 {
159 	debugfs_remove_recursive(slave->debugfs);
160 }
161 
162 void sdw_debugfs_init(void)
163 {
164 	sdw_debugfs_root = debugfs_create_dir("soundwire", NULL);
165 }
166 
167 void sdw_debugfs_exit(void)
168 {
169 	debugfs_remove_recursive(sdw_debugfs_root);
170 }
171