1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2015-2020 Intel Corporation.
3 
4 #include <linux/device.h>
5 #include <linux/mod_devicetable.h>
6 #include <linux/slab.h>
7 #include <linux/sysfs.h>
8 #include <linux/soundwire/sdw.h>
9 #include <linux/soundwire/sdw_type.h>
10 #include "bus.h"
11 #include "sysfs_local.h"
12 
13 /*
14  * Slave sysfs
15  */
16 
17 /*
18  * The sysfs for Slave reflects the MIPI description as given
19  * in the MIPI DisCo spec.
20  * status and device_number come directly from the MIPI SoundWire
21  * 1.x specification.
22  *
23  * Base file is device
24  *	|---- status
25  *	|---- device_number
26  *	|---- modalias
27  *	|---- dev-properties
28  *		|---- mipi_revision
29  *		|---- wake_capable
30  *		|---- test_mode_capable
31  *		|---- clk_stop_mode1
32  *		|---- simple_clk_stop_capable
33  *		|---- clk_stop_timeout
34  *		|---- ch_prep_timeout
35  *		|---- reset_behave
36  *		|---- high_PHY_capable
37  *		|---- paging_support
38  *		|---- bank_delay_support
39  *		|---- p15_behave
40  *		|---- master_count
41  *		|---- source_ports
42  *		|---- sink_ports
43  *	|---- dp0
44  *		|---- max_word
45  *		|---- min_word
46  *		|---- words
47  *		|---- BRA_flow_controlled
48  *		|---- simple_ch_prep_sm
49  *		|---- imp_def_interrupts
50  *	|---- dpN_<sink/src>
51  *		|---- max_word
52  *		|---- min_word
53  *		|---- words
54  *		|---- type
55  *		|---- max_grouping
56  *		|---- simple_ch_prep_sm
57  *		|---- ch_prep_timeout
58  *		|---- imp_def_interrupts
59  *		|---- min_ch
60  *		|---- max_ch
61  *		|---- channels
62  *		|---- ch_combinations
63  *		|---- max_async_buffer
64  *		|---- block_pack_mode
65  *		|---- port_encoding
66  *
67  */
68 
69 #define sdw_slave_attr(field, format_string)			\
70 static ssize_t field##_show(struct device *dev,			\
71 			    struct device_attribute *attr,	\
72 			    char *buf)				\
73 {								\
74 	struct sdw_slave *slave = dev_to_sdw_dev(dev);		\
75 	return sprintf(buf, format_string, slave->prop.field);	\
76 }								\
77 static DEVICE_ATTR_RO(field)
78 
79 sdw_slave_attr(mipi_revision, "0x%x\n");
80 sdw_slave_attr(wake_capable, "%d\n");
81 sdw_slave_attr(test_mode_capable, "%d\n");
82 sdw_slave_attr(clk_stop_mode1, "%d\n");
83 sdw_slave_attr(simple_clk_stop_capable, "%d\n");
84 sdw_slave_attr(clk_stop_timeout, "%d\n");
85 sdw_slave_attr(ch_prep_timeout, "%d\n");
86 sdw_slave_attr(reset_behave, "%d\n");
87 sdw_slave_attr(high_PHY_capable, "%d\n");
88 sdw_slave_attr(paging_support, "%d\n");
89 sdw_slave_attr(bank_delay_support, "%d\n");
90 sdw_slave_attr(p15_behave, "%d\n");
91 sdw_slave_attr(master_count, "%d\n");
92 sdw_slave_attr(source_ports, "0x%x\n");
93 sdw_slave_attr(sink_ports, "0x%x\n");
94 
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)95 static ssize_t modalias_show(struct device *dev,
96 			     struct device_attribute *attr, char *buf)
97 {
98 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
99 
100 	return sdw_slave_modalias(slave, buf, 256);
101 }
102 static DEVICE_ATTR_RO(modalias);
103 
104 static struct attribute *slave_attrs[] = {
105 	&dev_attr_modalias.attr,
106 	NULL,
107 };
108 
109 static const struct attribute_group slave_attr_group = {
110 	.attrs = slave_attrs,
111 };
112 
113 static struct attribute *slave_dev_attrs[] = {
114 	&dev_attr_mipi_revision.attr,
115 	&dev_attr_wake_capable.attr,
116 	&dev_attr_test_mode_capable.attr,
117 	&dev_attr_clk_stop_mode1.attr,
118 	&dev_attr_simple_clk_stop_capable.attr,
119 	&dev_attr_clk_stop_timeout.attr,
120 	&dev_attr_ch_prep_timeout.attr,
121 	&dev_attr_reset_behave.attr,
122 	&dev_attr_high_PHY_capable.attr,
123 	&dev_attr_paging_support.attr,
124 	&dev_attr_bank_delay_support.attr,
125 	&dev_attr_p15_behave.attr,
126 	&dev_attr_master_count.attr,
127 	&dev_attr_source_ports.attr,
128 	&dev_attr_sink_ports.attr,
129 	NULL,
130 };
131 
132 static const struct attribute_group sdw_slave_dev_attr_group = {
133 	.attrs	= slave_dev_attrs,
134 	.name = "dev-properties",
135 };
136 
137 /*
138  * DP0 sysfs
139  */
140 
141 #define sdw_dp0_attr(field, format_string)				\
142 static ssize_t field##_show(struct device *dev,				\
143 			    struct device_attribute *attr,		\
144 			    char *buf)					\
145 {									\
146 	struct sdw_slave *slave = dev_to_sdw_dev(dev);			\
147 	return sprintf(buf, format_string, slave->prop.dp0_prop->field);\
148 }									\
149 static DEVICE_ATTR_RO(field)
150 
151 sdw_dp0_attr(max_word, "%d\n");
152 sdw_dp0_attr(min_word, "%d\n");
153 sdw_dp0_attr(BRA_flow_controlled, "%d\n");
154 sdw_dp0_attr(simple_ch_prep_sm, "%d\n");
155 sdw_dp0_attr(imp_def_interrupts, "0x%x\n");
156 
words_show(struct device * dev,struct device_attribute * attr,char * buf)157 static ssize_t words_show(struct device *dev,
158 			  struct device_attribute *attr, char *buf)
159 {
160 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
161 	ssize_t size = 0;
162 	int i;
163 
164 	for (i = 0; i < slave->prop.dp0_prop->num_words; i++)
165 		size += sprintf(buf + size, "%d ",
166 				slave->prop.dp0_prop->words[i]);
167 	size += sprintf(buf + size, "\n");
168 
169 	return size;
170 }
171 static DEVICE_ATTR_RO(words);
172 
173 static struct attribute *dp0_attrs[] = {
174 	&dev_attr_max_word.attr,
175 	&dev_attr_min_word.attr,
176 	&dev_attr_words.attr,
177 	&dev_attr_BRA_flow_controlled.attr,
178 	&dev_attr_simple_ch_prep_sm.attr,
179 	&dev_attr_imp_def_interrupts.attr,
180 	NULL,
181 };
182 
dp0_attr_visible(struct kobject * kobj,struct attribute * attr,int n)183 static umode_t dp0_attr_visible(struct kobject *kobj, struct attribute *attr,
184 			      int n)
185 {
186 	struct sdw_slave *slave = dev_to_sdw_dev(kobj_to_dev(kobj));
187 
188 	if (slave->prop.dp0_prop)
189 		return attr->mode;
190 	return 0;
191 }
192 
dp0_group_visible(struct kobject * kobj)193 static bool dp0_group_visible(struct kobject *kobj)
194 {
195 	struct sdw_slave *slave = dev_to_sdw_dev(kobj_to_dev(kobj));
196 
197 	if (slave->prop.dp0_prop)
198 		return true;
199 	return false;
200 }
201 DEFINE_SYSFS_GROUP_VISIBLE(dp0);
202 
203 static const struct attribute_group dp0_group = {
204 	.attrs = dp0_attrs,
205 	.is_visible = SYSFS_GROUP_VISIBLE(dp0),
206 	.name = "dp0",
207 };
208 
209 const struct attribute_group *sdw_attr_groups[] = {
210 	&slave_attr_group,
211 	&sdw_slave_dev_attr_group,
212 	&dp0_group,
213 	NULL,
214 };
215 
216 /*
217  * the status is shown in capital letters for UNATTACHED and RESERVED
218  * on purpose, to highligh users to the fact that these status values
219  * are not expected.
220  */
221 static const char *const slave_status[] = {
222 	[SDW_SLAVE_UNATTACHED] =  "UNATTACHED",
223 	[SDW_SLAVE_ATTACHED] = "Attached",
224 	[SDW_SLAVE_ALERT] = "Alert",
225 	[SDW_SLAVE_RESERVED] = "RESERVED",
226 };
227 
status_show(struct device * dev,struct device_attribute * attr,char * buf)228 static ssize_t status_show(struct device *dev,
229 			   struct device_attribute *attr, char *buf)
230 {
231 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
232 
233 	return sprintf(buf, "%s\n", slave_status[slave->status]);
234 }
235 static DEVICE_ATTR_RO(status);
236 
device_number_show(struct device * dev,struct device_attribute * attr,char * buf)237 static ssize_t device_number_show(struct device *dev,
238 				  struct device_attribute *attr, char *buf)
239 {
240 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
241 
242 	if (slave->status == SDW_SLAVE_UNATTACHED)
243 		return sprintf(buf, "%s", "N/A");
244 	else
245 		return sprintf(buf, "%d", slave->dev_num);
246 }
247 static DEVICE_ATTR_RO(device_number);
248 
249 static struct attribute *slave_status_attrs[] = {
250 	&dev_attr_status.attr,
251 	&dev_attr_device_number.attr,
252 	NULL,
253 };
254 
255 /*
256  * we don't use ATTRIBUTES_GROUP here since the group is used in a
257  * separate file and can't be handled as a static.
258  */
259 static const struct attribute_group sdw_slave_status_attr_group = {
260 	.attrs	= slave_status_attrs,
261 };
262 
263 const struct attribute_group *sdw_slave_status_attr_groups[] = {
264 	&sdw_slave_status_attr_group,
265 	NULL
266 };
267