1 // SPDX-License-Identifier: GPL-2.0-only 2 // Copyright(c) 2017-2019 Intel Corporation. 3 4 #include <linux/cleanup.h> 5 #include <linux/device.h> 6 #include <linux/debugfs.h> 7 #include <linux/firmware.h> 8 #include <linux/mod_devicetable.h> 9 #include <linux/pm_runtime.h> 10 #include <linux/slab.h> 11 #include <linux/soundwire/sdw.h> 12 #include <linux/soundwire/sdw_registers.h> 13 #include "bus.h" 14 15 static struct dentry *sdw_debugfs_root; 16 17 void sdw_bus_debugfs_init(struct sdw_bus *bus) 18 { 19 char name[16]; 20 21 if (!sdw_debugfs_root) 22 return; 23 24 /* create the debugfs master-N */ 25 snprintf(name, sizeof(name), "master-%d-%d", bus->controller_id, bus->link_id); 26 bus->debugfs = debugfs_create_dir(name, sdw_debugfs_root); 27 } 28 29 void sdw_bus_debugfs_exit(struct sdw_bus *bus) 30 { 31 debugfs_remove_recursive(bus->debugfs); 32 } 33 34 #define RD_BUF (3 * PAGE_SIZE) 35 36 static ssize_t sdw_sprintf(struct sdw_slave *slave, 37 char *buf, size_t pos, unsigned int reg) 38 { 39 int value; 40 41 value = sdw_read_no_pm(slave, reg); 42 43 if (value < 0) 44 return scnprintf(buf + pos, RD_BUF - pos, "%3x\tXX\n", reg); 45 else 46 return scnprintf(buf + pos, RD_BUF - pos, 47 "%3x\t%2x\n", reg, value); 48 } 49 50 static int sdw_slave_reg_show(struct seq_file *s_file, void *data) 51 { 52 struct sdw_slave *slave = s_file->private; 53 ssize_t ret; 54 int i, j; 55 56 char *buf __free(kfree) = kzalloc(RD_BUF, GFP_KERNEL); 57 if (!buf) 58 return -ENOMEM; 59 60 ret = pm_runtime_get_sync(&slave->dev); 61 if (ret < 0 && ret != -EACCES) { 62 pm_runtime_put_noidle(&slave->dev); 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 return 0; 135 } 136 DEFINE_SHOW_ATTRIBUTE(sdw_slave_reg); 137 138 #define MAX_CMD_BYTES 256 139 140 static int cmd; 141 static u32 start_addr; 142 static size_t num_bytes; 143 static u8 read_buffer[MAX_CMD_BYTES]; 144 static char *firmware_file; 145 146 static int set_command(void *data, u64 value) 147 { 148 struct sdw_slave *slave = data; 149 150 if (value > 1) 151 return -EINVAL; 152 153 /* Userspace changed the hardware state behind the kernel's back */ 154 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 155 156 dev_dbg(&slave->dev, "command: %s\n", value ? "read" : "write"); 157 cmd = value; 158 159 return 0; 160 } 161 DEFINE_DEBUGFS_ATTRIBUTE(set_command_fops, NULL, 162 set_command, "%llu\n"); 163 164 static int set_start_address(void *data, u64 value) 165 { 166 struct sdw_slave *slave = data; 167 168 /* Userspace changed the hardware state behind the kernel's back */ 169 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 170 171 dev_dbg(&slave->dev, "start address %#llx\n", value); 172 173 start_addr = value; 174 175 return 0; 176 } 177 DEFINE_DEBUGFS_ATTRIBUTE(set_start_address_fops, NULL, 178 set_start_address, "%llu\n"); 179 180 static int set_num_bytes(void *data, u64 value) 181 { 182 struct sdw_slave *slave = data; 183 184 if (value == 0 || value > MAX_CMD_BYTES) 185 return -EINVAL; 186 187 /* Userspace changed the hardware state behind the kernel's back */ 188 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 189 190 dev_dbg(&slave->dev, "number of bytes %lld\n", value); 191 192 num_bytes = value; 193 194 return 0; 195 } 196 DEFINE_DEBUGFS_ATTRIBUTE(set_num_bytes_fops, NULL, 197 set_num_bytes, "%llu\n"); 198 199 static int cmd_go(void *data, u64 value) 200 { 201 struct sdw_slave *slave = data; 202 int ret; 203 204 if (value != 1) 205 return -EINVAL; 206 207 /* one last check */ 208 if (start_addr > SDW_REG_MAX || 209 num_bytes == 0 || num_bytes > MAX_CMD_BYTES) 210 return -EINVAL; 211 212 ret = pm_runtime_get_sync(&slave->dev); 213 if (ret < 0 && ret != -EACCES) { 214 pm_runtime_put_noidle(&slave->dev); 215 return ret; 216 } 217 218 /* Userspace changed the hardware state behind the kernel's back */ 219 add_taint(TAINT_USER, LOCKDEP_STILL_OK); 220 221 dev_dbg(&slave->dev, "starting command\n"); 222 223 if (cmd == 0) { 224 const struct firmware *fw; 225 226 ret = request_firmware(&fw, firmware_file, &slave->dev); 227 if (ret < 0) { 228 dev_err(&slave->dev, "firmware %s not found\n", firmware_file); 229 goto out; 230 } 231 232 if (fw->size != num_bytes) { 233 dev_err(&slave->dev, 234 "firmware %s: unexpected size %zd, desired %zd\n", 235 firmware_file, fw->size, num_bytes); 236 release_firmware(fw); 237 goto out; 238 } 239 240 ret = sdw_nwrite_no_pm(slave, start_addr, num_bytes, fw->data); 241 release_firmware(fw); 242 } else { 243 ret = sdw_nread_no_pm(slave, start_addr, num_bytes, read_buffer); 244 } 245 246 dev_dbg(&slave->dev, "command completed %d\n", ret); 247 248 out: 249 pm_runtime_mark_last_busy(&slave->dev); 250 pm_runtime_put(&slave->dev); 251 252 return ret; 253 } 254 DEFINE_DEBUGFS_ATTRIBUTE(cmd_go_fops, NULL, 255 cmd_go, "%llu\n"); 256 257 #define MAX_LINE_LEN 128 258 259 static int read_buffer_show(struct seq_file *s_file, void *data) 260 { 261 char buf[MAX_LINE_LEN]; 262 int i; 263 264 if (num_bytes == 0 || num_bytes > MAX_CMD_BYTES) 265 return -EINVAL; 266 267 for (i = 0; i < num_bytes; i++) { 268 scnprintf(buf, MAX_LINE_LEN, "address %#x val 0x%02x\n", 269 start_addr + i, read_buffer[i]); 270 seq_printf(s_file, "%s", buf); 271 } 272 273 return 0; 274 } 275 DEFINE_SHOW_ATTRIBUTE(read_buffer); 276 277 void sdw_slave_debugfs_init(struct sdw_slave *slave) 278 { 279 struct dentry *master; 280 struct dentry *d; 281 char name[32]; 282 283 master = slave->bus->debugfs; 284 285 /* create the debugfs slave-name */ 286 snprintf(name, sizeof(name), "%s", dev_name(&slave->dev)); 287 d = debugfs_create_dir(name, master); 288 289 debugfs_create_file("registers", 0400, d, slave, &sdw_slave_reg_fops); 290 291 /* interface to send arbitrary commands */ 292 debugfs_create_file("command", 0200, d, slave, &set_command_fops); 293 debugfs_create_file("start_address", 0200, d, slave, &set_start_address_fops); 294 debugfs_create_file("num_bytes", 0200, d, slave, &set_num_bytes_fops); 295 debugfs_create_file("go", 0200, d, slave, &cmd_go_fops); 296 297 debugfs_create_file("read_buffer", 0400, d, slave, &read_buffer_fops); 298 firmware_file = NULL; 299 debugfs_create_str("firmware_file", 0200, d, &firmware_file); 300 301 slave->debugfs = d; 302 } 303 304 void sdw_slave_debugfs_exit(struct sdw_slave *slave) 305 { 306 debugfs_remove_recursive(slave->debugfs); 307 } 308 309 void sdw_debugfs_init(void) 310 { 311 sdw_debugfs_root = debugfs_create_dir("soundwire", NULL); 312 } 313 314 void sdw_debugfs_exit(void) 315 { 316 debugfs_remove_recursive(sdw_debugfs_root); 317 } 318