1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2017-2019 Intel Corporation.
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>
15 static struct dentry
*sdw_debugfs_root
;
17 void sdw_bus_debugfs_init(struct sdw_bus
*bus
)
21 if (!sdw_debugfs_root
)
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
);
29 void sdw_bus_debugfs_exit(struct sdw_bus
*bus
)
31 debugfs_remove_recursive(bus
->debugfs
);
34 #define RD_BUF (3 * PAGE_SIZE)
36 static ssize_t
sdw_sprintf(struct sdw_slave
*slave
,
37 char *buf
, size_t pos
, unsigned int reg
)
41 value
= sdw_read_no_pm(slave
, reg
);
44 return scnprintf(buf
+ pos
, RD_BUF
- pos
, "%3x\tXX\n", reg
);
46 return scnprintf(buf
+ pos
, RD_BUF
- pos
,
47 "%3x\t%2x\n", reg
, value
);
50 static int sdw_slave_reg_show(struct seq_file
*s_file
, void *data
)
52 struct sdw_slave
*slave
= s_file
->private;
56 char *buf
__free(kfree
) = kzalloc(RD_BUF
, GFP_KERNEL
);
60 ret
= pm_runtime_get_sync(&slave
->dev
);
61 if (ret
< 0 && ret
!= -EACCES
) {
62 pm_runtime_put_noidle(&slave
->dev
);
66 ret
= scnprintf(buf
, RD_BUF
, "Register Value\n");
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
);
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
);
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
);
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
);
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
108 /* DP1..14 registers */
109 for (i
= 1; SDW_VALID_PORT_RANGE(i
); i
++) {
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
);
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
);
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
);
129 seq_printf(s_file
, "%s", buf
);
131 pm_runtime_mark_last_busy(&slave
->dev
);
132 pm_runtime_put(&slave
->dev
);
136 DEFINE_SHOW_ATTRIBUTE(sdw_slave_reg
);
138 #define MAX_CMD_BYTES 256
141 static u32 start_addr
;
142 static size_t num_bytes
;
143 static u8 read_buffer
[MAX_CMD_BYTES
];
144 static char *firmware_file
;
146 static int set_command(void *data
, u64 value
)
148 struct sdw_slave
*slave
= data
;
153 /* Userspace changed the hardware state behind the kernel's back */
154 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
156 dev_dbg(&slave
->dev
, "command: %s\n", value
? "read" : "write");
161 DEFINE_DEBUGFS_ATTRIBUTE(set_command_fops
, NULL
,
162 set_command
, "%llu\n");
164 static int set_start_address(void *data
, u64 value
)
166 struct sdw_slave
*slave
= data
;
168 /* Userspace changed the hardware state behind the kernel's back */
169 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
171 dev_dbg(&slave
->dev
, "start address %#llx\n", value
);
177 DEFINE_DEBUGFS_ATTRIBUTE(set_start_address_fops
, NULL
,
178 set_start_address
, "%llu\n");
180 static int set_num_bytes(void *data
, u64 value
)
182 struct sdw_slave
*slave
= data
;
184 if (value
== 0 || value
> MAX_CMD_BYTES
)
187 /* Userspace changed the hardware state behind the kernel's back */
188 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
190 dev_dbg(&slave
->dev
, "number of bytes %lld\n", value
);
196 DEFINE_DEBUGFS_ATTRIBUTE(set_num_bytes_fops
, NULL
,
197 set_num_bytes
, "%llu\n");
199 static int cmd_go(void *data
, u64 value
)
201 struct sdw_slave
*slave
= data
;
208 if (start_addr
> SDW_REG_MAX
||
209 num_bytes
== 0 || num_bytes
> MAX_CMD_BYTES
)
212 ret
= pm_runtime_get_sync(&slave
->dev
);
213 if (ret
< 0 && ret
!= -EACCES
) {
214 pm_runtime_put_noidle(&slave
->dev
);
218 /* Userspace changed the hardware state behind the kernel's back */
219 add_taint(TAINT_USER
, LOCKDEP_STILL_OK
);
221 dev_dbg(&slave
->dev
, "starting command\n");
224 const struct firmware
*fw
;
226 ret
= request_firmware(&fw
, firmware_file
, &slave
->dev
);
228 dev_err(&slave
->dev
, "firmware %s not found\n", firmware_file
);
232 if (fw
->size
!= num_bytes
) {
234 "firmware %s: unexpected size %zd, desired %zd\n",
235 firmware_file
, fw
->size
, num_bytes
);
236 release_firmware(fw
);
240 ret
= sdw_nwrite_no_pm(slave
, start_addr
, num_bytes
, fw
->data
);
241 release_firmware(fw
);
243 ret
= sdw_nread_no_pm(slave
, start_addr
, num_bytes
, read_buffer
);
246 dev_dbg(&slave
->dev
, "command completed %d\n", ret
);
249 pm_runtime_mark_last_busy(&slave
->dev
);
250 pm_runtime_put(&slave
->dev
);
254 DEFINE_DEBUGFS_ATTRIBUTE(cmd_go_fops
, NULL
,
257 #define MAX_LINE_LEN 128
259 static int read_buffer_show(struct seq_file
*s_file
, void *data
)
261 char buf
[MAX_LINE_LEN
];
264 if (num_bytes
== 0 || num_bytes
> MAX_CMD_BYTES
)
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
);
275 DEFINE_SHOW_ATTRIBUTE(read_buffer
);
277 void sdw_slave_debugfs_init(struct sdw_slave
*slave
)
279 struct dentry
*master
;
283 master
= slave
->bus
->debugfs
;
285 /* create the debugfs slave-name */
286 snprintf(name
, sizeof(name
), "%s", dev_name(&slave
->dev
));
287 d
= debugfs_create_dir(name
, master
);
289 debugfs_create_file("registers", 0400, d
, slave
, &sdw_slave_reg_fops
);
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
);
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
);
304 void sdw_slave_debugfs_exit(struct sdw_slave
*slave
)
306 debugfs_remove_recursive(slave
->debugfs
);
309 void sdw_debugfs_init(void)
311 sdw_debugfs_root
= debugfs_create_dir("soundwire", NULL
);
314 void sdw_debugfs_exit(void)
316 debugfs_remove_recursive(sdw_debugfs_root
);