1bcac5902SPierre-Louis Bossart // SPDX-License-Identifier: GPL-2.0-only
2bcac5902SPierre-Louis Bossart // Copyright(c) 2015-2020 Intel Corporation.
3bcac5902SPierre-Louis Bossart
4bcac5902SPierre-Louis Bossart #include <linux/device.h>
5bcac5902SPierre-Louis Bossart #include <linux/mod_devicetable.h>
6bcac5902SPierre-Louis Bossart #include <linux/slab.h>
7bcac5902SPierre-Louis Bossart #include <linux/sysfs.h>
8bcac5902SPierre-Louis Bossart #include <linux/soundwire/sdw.h>
9bcac5902SPierre-Louis Bossart #include <linux/soundwire/sdw_type.h>
10bcac5902SPierre-Louis Bossart #include "bus.h"
11bcac5902SPierre-Louis Bossart #include "sysfs_local.h"
12bcac5902SPierre-Louis Bossart
13bcac5902SPierre-Louis Bossart /*
14bcac5902SPierre-Louis Bossart * Slave sysfs
15bcac5902SPierre-Louis Bossart */
16bcac5902SPierre-Louis Bossart
17bcac5902SPierre-Louis Bossart /*
18bcac5902SPierre-Louis Bossart * The sysfs for Slave reflects the MIPI description as given
190173f525SPierre-Louis Bossart * in the MIPI DisCo spec.
200173f525SPierre-Louis Bossart * status and device_number come directly from the MIPI SoundWire
210173f525SPierre-Louis Bossart * 1.x specification.
22bcac5902SPierre-Louis Bossart *
23bcac5902SPierre-Louis Bossart * Base file is device
240173f525SPierre-Louis Bossart * |---- status
250173f525SPierre-Louis Bossart * |---- device_number
26bcac5902SPierre-Louis Bossart * |---- modalias
27bcac5902SPierre-Louis Bossart * |---- dev-properties
28bcac5902SPierre-Louis Bossart * |---- mipi_revision
29bcac5902SPierre-Louis Bossart * |---- wake_capable
30bcac5902SPierre-Louis Bossart * |---- test_mode_capable
31bcac5902SPierre-Louis Bossart * |---- clk_stop_mode1
32bcac5902SPierre-Louis Bossart * |---- simple_clk_stop_capable
33bcac5902SPierre-Louis Bossart * |---- clk_stop_timeout
34bcac5902SPierre-Louis Bossart * |---- ch_prep_timeout
35bcac5902SPierre-Louis Bossart * |---- reset_behave
36bcac5902SPierre-Louis Bossart * |---- high_PHY_capable
37bcac5902SPierre-Louis Bossart * |---- paging_support
38bcac5902SPierre-Louis Bossart * |---- bank_delay_support
39bcac5902SPierre-Louis Bossart * |---- p15_behave
40bcac5902SPierre-Louis Bossart * |---- master_count
41bcac5902SPierre-Louis Bossart * |---- source_ports
42bcac5902SPierre-Louis Bossart * |---- sink_ports
43bcac5902SPierre-Louis Bossart * |---- dp0
44bcac5902SPierre-Louis Bossart * |---- max_word
45bcac5902SPierre-Louis Bossart * |---- min_word
46bcac5902SPierre-Louis Bossart * |---- words
47bcac5902SPierre-Louis Bossart * |---- BRA_flow_controlled
48bcac5902SPierre-Louis Bossart * |---- simple_ch_prep_sm
49bcac5902SPierre-Louis Bossart * |---- imp_def_interrupts
50bcac5902SPierre-Louis Bossart * |---- dpN_<sink/src>
51bcac5902SPierre-Louis Bossart * |---- max_word
52bcac5902SPierre-Louis Bossart * |---- min_word
53bcac5902SPierre-Louis Bossart * |---- words
54bcac5902SPierre-Louis Bossart * |---- type
55bcac5902SPierre-Louis Bossart * |---- max_grouping
56bcac5902SPierre-Louis Bossart * |---- simple_ch_prep_sm
57bcac5902SPierre-Louis Bossart * |---- ch_prep_timeout
58bcac5902SPierre-Louis Bossart * |---- imp_def_interrupts
59bcac5902SPierre-Louis Bossart * |---- min_ch
60bcac5902SPierre-Louis Bossart * |---- max_ch
61bcac5902SPierre-Louis Bossart * |---- channels
62bcac5902SPierre-Louis Bossart * |---- ch_combinations
63bcac5902SPierre-Louis Bossart * |---- max_async_buffer
64bcac5902SPierre-Louis Bossart * |---- block_pack_mode
65bcac5902SPierre-Louis Bossart * |---- port_encoding
66bcac5902SPierre-Louis Bossart *
67bcac5902SPierre-Louis Bossart */
68bcac5902SPierre-Louis Bossart
69bcac5902SPierre-Louis Bossart #define sdw_slave_attr(field, format_string) \
70bcac5902SPierre-Louis Bossart static ssize_t field##_show(struct device *dev, \
71bcac5902SPierre-Louis Bossart struct device_attribute *attr, \
72bcac5902SPierre-Louis Bossart char *buf) \
73bcac5902SPierre-Louis Bossart { \
74bcac5902SPierre-Louis Bossart struct sdw_slave *slave = dev_to_sdw_dev(dev); \
75bcac5902SPierre-Louis Bossart return sprintf(buf, format_string, slave->prop.field); \
76bcac5902SPierre-Louis Bossart } \
77bcac5902SPierre-Louis Bossart static DEVICE_ATTR_RO(field)
78bcac5902SPierre-Louis Bossart
79bcac5902SPierre-Louis Bossart sdw_slave_attr(mipi_revision, "0x%x\n");
80bcac5902SPierre-Louis Bossart sdw_slave_attr(wake_capable, "%d\n");
81bcac5902SPierre-Louis Bossart sdw_slave_attr(test_mode_capable, "%d\n");
82bcac5902SPierre-Louis Bossart sdw_slave_attr(clk_stop_mode1, "%d\n");
83bcac5902SPierre-Louis Bossart sdw_slave_attr(simple_clk_stop_capable, "%d\n");
84bcac5902SPierre-Louis Bossart sdw_slave_attr(clk_stop_timeout, "%d\n");
85bcac5902SPierre-Louis Bossart sdw_slave_attr(ch_prep_timeout, "%d\n");
86bcac5902SPierre-Louis Bossart sdw_slave_attr(reset_behave, "%d\n");
87bcac5902SPierre-Louis Bossart sdw_slave_attr(high_PHY_capable, "%d\n");
88bcac5902SPierre-Louis Bossart sdw_slave_attr(paging_support, "%d\n");
89bcac5902SPierre-Louis Bossart sdw_slave_attr(bank_delay_support, "%d\n");
90bcac5902SPierre-Louis Bossart sdw_slave_attr(p15_behave, "%d\n");
91bcac5902SPierre-Louis Bossart sdw_slave_attr(master_count, "%d\n");
92bcac5902SPierre-Louis Bossart sdw_slave_attr(source_ports, "0x%x\n");
93bcac5902SPierre-Louis Bossart sdw_slave_attr(sink_ports, "0x%x\n");
94bcac5902SPierre-Louis Bossart
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)95bcac5902SPierre-Louis Bossart static ssize_t modalias_show(struct device *dev,
96bcac5902SPierre-Louis Bossart struct device_attribute *attr, char *buf)
97bcac5902SPierre-Louis Bossart {
98bcac5902SPierre-Louis Bossart struct sdw_slave *slave = dev_to_sdw_dev(dev);
99bcac5902SPierre-Louis Bossart
100bcac5902SPierre-Louis Bossart return sdw_slave_modalias(slave, buf, 256);
101bcac5902SPierre-Louis Bossart }
102bcac5902SPierre-Louis Bossart static DEVICE_ATTR_RO(modalias);
103bcac5902SPierre-Louis Bossart
104bcac5902SPierre-Louis Bossart static struct attribute *slave_attrs[] = {
105bcac5902SPierre-Louis Bossart &dev_attr_modalias.attr,
106bcac5902SPierre-Louis Bossart NULL,
107bcac5902SPierre-Louis Bossart };
108bcac5902SPierre-Louis Bossart ATTRIBUTE_GROUPS(slave);
109bcac5902SPierre-Louis Bossart
110bcac5902SPierre-Louis Bossart static struct attribute *slave_dev_attrs[] = {
111bcac5902SPierre-Louis Bossart &dev_attr_mipi_revision.attr,
112bcac5902SPierre-Louis Bossart &dev_attr_wake_capable.attr,
113bcac5902SPierre-Louis Bossart &dev_attr_test_mode_capable.attr,
114bcac5902SPierre-Louis Bossart &dev_attr_clk_stop_mode1.attr,
115bcac5902SPierre-Louis Bossart &dev_attr_simple_clk_stop_capable.attr,
116bcac5902SPierre-Louis Bossart &dev_attr_clk_stop_timeout.attr,
117bcac5902SPierre-Louis Bossart &dev_attr_ch_prep_timeout.attr,
118bcac5902SPierre-Louis Bossart &dev_attr_reset_behave.attr,
119bcac5902SPierre-Louis Bossart &dev_attr_high_PHY_capable.attr,
120bcac5902SPierre-Louis Bossart &dev_attr_paging_support.attr,
121bcac5902SPierre-Louis Bossart &dev_attr_bank_delay_support.attr,
122bcac5902SPierre-Louis Bossart &dev_attr_p15_behave.attr,
123bcac5902SPierre-Louis Bossart &dev_attr_master_count.attr,
124bcac5902SPierre-Louis Bossart &dev_attr_source_ports.attr,
125bcac5902SPierre-Louis Bossart &dev_attr_sink_ports.attr,
126bcac5902SPierre-Louis Bossart NULL,
127bcac5902SPierre-Louis Bossart };
128bcac5902SPierre-Louis Bossart
129bcac5902SPierre-Louis Bossart /*
130bcac5902SPierre-Louis Bossart * we don't use ATTRIBUTES_GROUP here since we want to add a subdirectory
131bcac5902SPierre-Louis Bossart * for device-level properties
132bcac5902SPierre-Louis Bossart */
133*565e3afaSRikard Falkeborn static const struct attribute_group sdw_slave_dev_attr_group = {
134bcac5902SPierre-Louis Bossart .attrs = slave_dev_attrs,
135bcac5902SPierre-Louis Bossart .name = "dev-properties",
136bcac5902SPierre-Louis Bossart };
137bcac5902SPierre-Louis Bossart
138bcac5902SPierre-Louis Bossart /*
139bcac5902SPierre-Louis Bossart * DP0 sysfs
140bcac5902SPierre-Louis Bossart */
141bcac5902SPierre-Louis Bossart
142bcac5902SPierre-Louis Bossart #define sdw_dp0_attr(field, format_string) \
143bcac5902SPierre-Louis Bossart static ssize_t field##_show(struct device *dev, \
144bcac5902SPierre-Louis Bossart struct device_attribute *attr, \
145bcac5902SPierre-Louis Bossart char *buf) \
146bcac5902SPierre-Louis Bossart { \
147bcac5902SPierre-Louis Bossart struct sdw_slave *slave = dev_to_sdw_dev(dev); \
148bcac5902SPierre-Louis Bossart return sprintf(buf, format_string, slave->prop.dp0_prop->field);\
149bcac5902SPierre-Louis Bossart } \
150bcac5902SPierre-Louis Bossart static DEVICE_ATTR_RO(field)
151bcac5902SPierre-Louis Bossart
152bcac5902SPierre-Louis Bossart sdw_dp0_attr(max_word, "%d\n");
153bcac5902SPierre-Louis Bossart sdw_dp0_attr(min_word, "%d\n");
154bcac5902SPierre-Louis Bossart sdw_dp0_attr(BRA_flow_controlled, "%d\n");
155bcac5902SPierre-Louis Bossart sdw_dp0_attr(simple_ch_prep_sm, "%d\n");
156bcac5902SPierre-Louis Bossart sdw_dp0_attr(imp_def_interrupts, "0x%x\n");
157bcac5902SPierre-Louis Bossart
words_show(struct device * dev,struct device_attribute * attr,char * buf)158bcac5902SPierre-Louis Bossart static ssize_t words_show(struct device *dev,
159bcac5902SPierre-Louis Bossart struct device_attribute *attr, char *buf)
160bcac5902SPierre-Louis Bossart {
161bcac5902SPierre-Louis Bossart struct sdw_slave *slave = dev_to_sdw_dev(dev);
162bcac5902SPierre-Louis Bossart ssize_t size = 0;
163bcac5902SPierre-Louis Bossart int i;
164bcac5902SPierre-Louis Bossart
165bcac5902SPierre-Louis Bossart for (i = 0; i < slave->prop.dp0_prop->num_words; i++)
166bcac5902SPierre-Louis Bossart size += sprintf(buf + size, "%d ",
167bcac5902SPierre-Louis Bossart slave->prop.dp0_prop->words[i]);
168bcac5902SPierre-Louis Bossart size += sprintf(buf + size, "\n");
169bcac5902SPierre-Louis Bossart
170bcac5902SPierre-Louis Bossart return size;
171bcac5902SPierre-Louis Bossart }
172bcac5902SPierre-Louis Bossart static DEVICE_ATTR_RO(words);
173bcac5902SPierre-Louis Bossart
174bcac5902SPierre-Louis Bossart static struct attribute *dp0_attrs[] = {
175bcac5902SPierre-Louis Bossart &dev_attr_max_word.attr,
176bcac5902SPierre-Louis Bossart &dev_attr_min_word.attr,
177bcac5902SPierre-Louis Bossart &dev_attr_words.attr,
178bcac5902SPierre-Louis Bossart &dev_attr_BRA_flow_controlled.attr,
179bcac5902SPierre-Louis Bossart &dev_attr_simple_ch_prep_sm.attr,
180bcac5902SPierre-Louis Bossart &dev_attr_imp_def_interrupts.attr,
181bcac5902SPierre-Louis Bossart NULL,
182bcac5902SPierre-Louis Bossart };
183bcac5902SPierre-Louis Bossart
184bcac5902SPierre-Louis Bossart /*
185bcac5902SPierre-Louis Bossart * we don't use ATTRIBUTES_GROUP here since we want to add a subdirectory
186bcac5902SPierre-Louis Bossart * for dp0-level properties
187bcac5902SPierre-Louis Bossart */
188bcac5902SPierre-Louis Bossart static const struct attribute_group dp0_group = {
189bcac5902SPierre-Louis Bossart .attrs = dp0_attrs,
190bcac5902SPierre-Louis Bossart .name = "dp0",
191bcac5902SPierre-Louis Bossart };
192bcac5902SPierre-Louis Bossart
sdw_slave_sysfs_init(struct sdw_slave * slave)193bcac5902SPierre-Louis Bossart int sdw_slave_sysfs_init(struct sdw_slave *slave)
194bcac5902SPierre-Louis Bossart {
195bcac5902SPierre-Louis Bossart int ret;
196bcac5902SPierre-Louis Bossart
197bcac5902SPierre-Louis Bossart ret = devm_device_add_groups(&slave->dev, slave_groups);
198bcac5902SPierre-Louis Bossart if (ret < 0)
199bcac5902SPierre-Louis Bossart return ret;
200bcac5902SPierre-Louis Bossart
201bcac5902SPierre-Louis Bossart ret = devm_device_add_group(&slave->dev, &sdw_slave_dev_attr_group);
202bcac5902SPierre-Louis Bossart if (ret < 0)
203bcac5902SPierre-Louis Bossart return ret;
204bcac5902SPierre-Louis Bossart
205bcac5902SPierre-Louis Bossart if (slave->prop.dp0_prop) {
206bcac5902SPierre-Louis Bossart ret = devm_device_add_group(&slave->dev, &dp0_group);
207bcac5902SPierre-Louis Bossart if (ret < 0)
208bcac5902SPierre-Louis Bossart return ret;
209bcac5902SPierre-Louis Bossart }
210bcac5902SPierre-Louis Bossart
211bcac5902SPierre-Louis Bossart if (slave->prop.source_ports || slave->prop.sink_ports) {
212bcac5902SPierre-Louis Bossart ret = sdw_slave_sysfs_dpn_init(slave);
213bcac5902SPierre-Louis Bossart if (ret < 0)
214bcac5902SPierre-Louis Bossart return ret;
215bcac5902SPierre-Louis Bossart }
216bcac5902SPierre-Louis Bossart
217bcac5902SPierre-Louis Bossart return 0;
218bcac5902SPierre-Louis Bossart }
2190173f525SPierre-Louis Bossart
2200173f525SPierre-Louis Bossart /*
2210173f525SPierre-Louis Bossart * the status is shown in capital letters for UNATTACHED and RESERVED
2220173f525SPierre-Louis Bossart * on purpose, to highligh users to the fact that these status values
2230173f525SPierre-Louis Bossart * are not expected.
2240173f525SPierre-Louis Bossart */
2250173f525SPierre-Louis Bossart static const char *const slave_status[] = {
2260173f525SPierre-Louis Bossart [SDW_SLAVE_UNATTACHED] = "UNATTACHED",
2270173f525SPierre-Louis Bossart [SDW_SLAVE_ATTACHED] = "Attached",
2280173f525SPierre-Louis Bossart [SDW_SLAVE_ALERT] = "Alert",
2290173f525SPierre-Louis Bossart [SDW_SLAVE_RESERVED] = "RESERVED",
2300173f525SPierre-Louis Bossart };
2310173f525SPierre-Louis Bossart
status_show(struct device * dev,struct device_attribute * attr,char * buf)2320173f525SPierre-Louis Bossart static ssize_t status_show(struct device *dev,
2330173f525SPierre-Louis Bossart struct device_attribute *attr, char *buf)
2340173f525SPierre-Louis Bossart {
2350173f525SPierre-Louis Bossart struct sdw_slave *slave = dev_to_sdw_dev(dev);
2360173f525SPierre-Louis Bossart
2370173f525SPierre-Louis Bossart return sprintf(buf, "%s\n", slave_status[slave->status]);
2380173f525SPierre-Louis Bossart }
2390173f525SPierre-Louis Bossart static DEVICE_ATTR_RO(status);
2400173f525SPierre-Louis Bossart
device_number_show(struct device * dev,struct device_attribute * attr,char * buf)2410173f525SPierre-Louis Bossart static ssize_t device_number_show(struct device *dev,
2420173f525SPierre-Louis Bossart struct device_attribute *attr, char *buf)
2430173f525SPierre-Louis Bossart {
2440173f525SPierre-Louis Bossart struct sdw_slave *slave = dev_to_sdw_dev(dev);
2450173f525SPierre-Louis Bossart
2460173f525SPierre-Louis Bossart if (slave->status == SDW_SLAVE_UNATTACHED)
2470173f525SPierre-Louis Bossart return sprintf(buf, "%s", "N/A");
2480173f525SPierre-Louis Bossart else
2490173f525SPierre-Louis Bossart return sprintf(buf, "%d", slave->dev_num);
2500173f525SPierre-Louis Bossart }
2510173f525SPierre-Louis Bossart static DEVICE_ATTR_RO(device_number);
2520173f525SPierre-Louis Bossart
2530173f525SPierre-Louis Bossart static struct attribute *slave_status_attrs[] = {
2540173f525SPierre-Louis Bossart &dev_attr_status.attr,
2550173f525SPierre-Louis Bossart &dev_attr_device_number.attr,
2560173f525SPierre-Louis Bossart NULL,
2570173f525SPierre-Louis Bossart };
2580173f525SPierre-Louis Bossart
2590173f525SPierre-Louis Bossart /*
2600173f525SPierre-Louis Bossart * we don't use ATTRIBUTES_GROUP here since the group is used in a
2610173f525SPierre-Louis Bossart * separate file and can't be handled as a static.
2620173f525SPierre-Louis Bossart */
2630173f525SPierre-Louis Bossart static const struct attribute_group sdw_slave_status_attr_group = {
2640173f525SPierre-Louis Bossart .attrs = slave_status_attrs,
2650173f525SPierre-Louis Bossart };
2660173f525SPierre-Louis Bossart
2670173f525SPierre-Louis Bossart const struct attribute_group *sdw_slave_status_attr_groups[] = {
2680173f525SPierre-Louis Bossart &sdw_slave_status_attr_group,
2690173f525SPierre-Louis Bossart NULL
2700173f525SPierre-Louis Bossart };
271