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 
sdw_bus_debugfs_init(struct sdw_bus * bus)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 
sdw_bus_debugfs_exit(struct sdw_bus * bus)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 
sdw_sprintf(struct sdw_slave * slave,char * buf,size_t pos,unsigned int reg)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 
sdw_slave_reg_show(struct seq_file * s_file,void * data)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_resume_and_get(&slave->dev);
60 	if (ret < 0 && ret != -EACCES) {
61 		kfree(buf);
62 		return ret;
63 	}
64 
65 	ret = scnprintf(buf, RD_BUF, "Register  Value\n");
66 
67 	/* DP0 non-banked registers */
68 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nDP0\n");
69 	for (i = SDW_DP0_INT; i <= SDW_DP0_PREPARECTRL; i++)
70 		ret += sdw_sprintf(slave, buf, ret, i);
71 
72 	/* DP0 Bank 0 registers */
73 	ret += scnprintf(buf + ret, RD_BUF - ret, "Bank0\n");
74 	ret += sdw_sprintf(slave, buf, ret, SDW_DP0_CHANNELEN);
75 	for (i = SDW_DP0_SAMPLECTRL1; i <= SDW_DP0_LANECTRL; i++)
76 		ret += sdw_sprintf(slave, buf, ret, i);
77 
78 	/* DP0 Bank 1 registers */
79 	ret += scnprintf(buf + ret, RD_BUF - ret, "Bank1\n");
80 	ret += sdw_sprintf(slave, buf, ret,
81 			SDW_DP0_CHANNELEN + SDW_BANK1_OFFSET);
82 	for (i = SDW_DP0_SAMPLECTRL1 + SDW_BANK1_OFFSET;
83 			i <= SDW_DP0_LANECTRL + SDW_BANK1_OFFSET; i++)
84 		ret += sdw_sprintf(slave, buf, ret, i);
85 
86 	/* SCP registers */
87 	ret += scnprintf(buf + ret, RD_BUF - ret, "\nSCP\n");
88 	for (i = SDW_SCP_INT1; i <= SDW_SCP_BANKDELAY; i++)
89 		ret += sdw_sprintf(slave, buf, ret, i);
90 	for (i = SDW_SCP_DEVID_0; i <= SDW_SCP_DEVID_5; i++)
91 		ret += sdw_sprintf(slave, buf, ret, i);
92 
93 	/*
94 	 * SCP Bank 0/1 registers are read-only and cannot be
95 	 * retrieved from the Slave. The Master typically keeps track
96 	 * of the current frame size so the information can be found
97 	 * in other places
98 	 */
99 
100 	/* DP1..14 registers */
101 	for (i = 1; SDW_VALID_PORT_RANGE(i); i++) {
102 
103 		/* DPi registers */
104 		ret += scnprintf(buf + ret, RD_BUF - ret, "\nDP%d\n", i);
105 		for (j = SDW_DPN_INT(i); j <= SDW_DPN_PREPARECTRL(i); j++)
106 			ret += sdw_sprintf(slave, buf, ret, j);
107 
108 		/* DPi Bank0 registers */
109 		ret += scnprintf(buf + ret, RD_BUF - ret, "Bank0\n");
110 		for (j = SDW_DPN_CHANNELEN_B0(i);
111 		     j <= SDW_DPN_LANECTRL_B0(i); j++)
112 			ret += sdw_sprintf(slave, buf, ret, j);
113 
114 		/* DPi Bank1 registers */
115 		ret += scnprintf(buf + ret, RD_BUF - ret, "Bank1\n");
116 		for (j = SDW_DPN_CHANNELEN_B1(i);
117 		     j <= SDW_DPN_LANECTRL_B1(i); j++)
118 			ret += sdw_sprintf(slave, buf, ret, j);
119 	}
120 
121 	seq_printf(s_file, "%s", buf);
122 
123 	pm_runtime_mark_last_busy(&slave->dev);
124 	pm_runtime_put(&slave->dev);
125 
126 	kfree(buf);
127 
128 	return 0;
129 }
130 DEFINE_SHOW_ATTRIBUTE(sdw_slave_reg);
131 
sdw_slave_debugfs_init(struct sdw_slave * slave)132 void sdw_slave_debugfs_init(struct sdw_slave *slave)
133 {
134 	struct dentry *master;
135 	struct dentry *d;
136 	char name[32];
137 
138 	master = slave->bus->debugfs;
139 
140 	/* create the debugfs slave-name */
141 	snprintf(name, sizeof(name), "%s", dev_name(&slave->dev));
142 	d = debugfs_create_dir(name, master);
143 
144 	debugfs_create_file("registers", 0400, d, slave, &sdw_slave_reg_fops);
145 
146 	slave->debugfs = d;
147 }
148 
sdw_slave_debugfs_exit(struct sdw_slave * slave)149 void sdw_slave_debugfs_exit(struct sdw_slave *slave)
150 {
151 	debugfs_remove_recursive(slave->debugfs);
152 }
153 
sdw_debugfs_init(void)154 void sdw_debugfs_init(void)
155 {
156 	sdw_debugfs_root = debugfs_create_dir("soundwire", NULL);
157 }
158 
sdw_debugfs_exit(void)159 void sdw_debugfs_exit(void)
160 {
161 	debugfs_remove_recursive(sdw_debugfs_root);
162 }
163