projects/linux·blob master · refs

286 lines · 6.9 KiB

1// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2// Copyright(c) 2015-17 Intel Corporation.
3
4#include <linux/acpi.h>
5#include <linux/of.h>
6#include <linux/soundwire/sdw.h>
7#include <linux/soundwire/sdw_type.h>
8#include <sound/sdca.h>
9#include "bus.h"
10#include "sysfs_local.h"
11
12static void sdw_slave_release(struct device *dev)
13{
14 struct sdw_slave *slave = dev_to_sdw_dev(dev);
15
16 of_node_put(slave->dev.of_node);
17 mutex_destroy(&slave->sdw_dev_lock);
18 kfree(slave);
19}
20
21const struct device_type sdw_slave_type = {
22 .name = "sdw_slave",
23 .release = sdw_slave_release,
24 .uevent = sdw_slave_uevent,
25};
26EXPORT_SYMBOL_GPL(sdw_slave_type);
27
28int sdw_slave_add(struct sdw_bus *bus,
29 struct sdw_slave_id *id, struct fwnode_handle *fwnode)
30{
31 struct sdw_slave *slave;
32 int ret;
33 int i;
34
35 slave = kzalloc_obj(*slave);
36 if (!slave)
37 return -ENOMEM;
38
39 /* Initialize data structure */
40 memcpy(&slave->id, id, sizeof(*id));
41 slave->dev.parent = bus->dev;
42 slave->dev.fwnode = fwnode;
43
44 if (id->unique_id == SDW_IGNORED_UNIQUE_ID) {
45 /* name shall be sdw:ctrl:link:mfg:part:class */
46 dev_set_name(&slave->dev, "sdw:%01x:%01x:%04x:%04x:%02x",
47 bus->controller_id, bus->link_id, id->mfg_id, id->part_id,
48 id->class_id);
49 } else {
50 /* name shall be sdw:ctrl:link:mfg:part:class:unique */
51 dev_set_name(&slave->dev, "sdw:%01x:%01x:%04x:%04x:%02x:%01x",
52 bus->controller_id, bus->link_id, id->mfg_id, id->part_id,
53 id->class_id, id->unique_id);
54 }
55
56 slave->dev.bus = &sdw_bus_type;
57 slave->dev.of_node = of_node_get(to_of_node(fwnode));
58 slave->dev.type = &sdw_slave_type;
59 slave->dev.groups = sdw_slave_status_attr_groups;
60 slave->bus = bus;
61 slave->status = SDW_SLAVE_UNATTACHED;
62 init_completion(&slave->enumeration_complete);
63 init_completion(&slave->initialization_complete);
64 slave->dev_num = 0;
65 slave->probed = false;
66 slave->first_interrupt_done = false;
67 mutex_init(&slave->sdw_dev_lock);
68
69 for (i = 0; i < SDW_MAX_PORTS; i++)
70 init_completion(&slave->port_ready[i]);
71
72 mutex_lock(&bus->bus_lock);
73 list_add_tail(&slave->node, &bus->slaves);
74 mutex_unlock(&bus->bus_lock);
75
76 /*
77 * The Soundwire driver probe may optionally register SDCA
78 * sub-devices, one per Function. This means the information
79 * on the SDCA revision and the number/type of Functions need
80 * to be extracted from platform firmware before the SoundWire
81 * driver probe, and as a consequence before the SoundWire
82 * device_register() below.
83 */
84 sdca_lookup_interface_revision(slave);
85 sdca_lookup_functions(slave);
86
87 ret = device_register(&slave->dev);
88 if (ret) {
89 dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
90
91 /*
92 * On err, don't free but drop ref as this will be freed
93 * when release method is invoked.
94 */
95 mutex_lock(&bus->bus_lock);
96 list_del(&slave->node);
97 mutex_unlock(&bus->bus_lock);
98 put_device(&slave->dev);
99
100 return ret;
101 }
102 sdw_slave_debugfs_init(slave);
103
104 return ret;
105}
106EXPORT_SYMBOL(sdw_slave_add);
107
108#if IS_ENABLED(CONFIG_ACPI)
109
110static bool find_slave(struct sdw_bus *bus,
111 struct acpi_device *adev,
112 struct sdw_slave_id *id)
113{
114 unsigned int link_id;
115 u64 addr;
116 int ret;
117
118 if (acpi_bus_get_status(adev) || !acpi_dev_ready_for_enumeration(adev))
119 return false;
120
121 ret = acpi_get_local_u64_address(adev->handle, &addr);
122 if (ret < 0)
123 return false;
124
125 if (bus->ops->override_adr)
126 addr = bus->ops->override_adr(bus, addr);
127
128 if (!addr)
129 return false;
130
131 /* Extract link id from ADR, Bit 51 to 48 (included) */
132 link_id = SDW_DISCO_LINK_ID(addr);
133
134 /* Check for link_id match */
135 if (link_id != bus->link_id)
136 return false;
137
138 sdw_extract_slave_id(bus, addr, id);
139
140 return true;
141}
142
143struct sdw_acpi_child_walk_data {
144 struct sdw_bus *bus;
145 struct acpi_device *adev;
146 struct sdw_slave_id id;
147 bool ignore_unique_id;
148};
149
150static int sdw_acpi_check_duplicate(struct acpi_device *adev, void *data)
151{
152 struct sdw_acpi_child_walk_data *cwd = data;
153 struct sdw_bus *bus = cwd->bus;
154 struct sdw_slave_id id;
155
156 if (adev == cwd->adev)
157 return 0;
158
159 if (!find_slave(bus, adev, &id))
160 return 0;
161
162 if (cwd->id.sdw_version != id.sdw_version || cwd->id.mfg_id != id.mfg_id ||
163 cwd->id.part_id != id.part_id || cwd->id.class_id != id.class_id)
164 return 0;
165
166 if (cwd->id.unique_id != id.unique_id) {
167 dev_dbg(bus->dev,
168 "Valid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n",
169 cwd->id.unique_id, id.unique_id, cwd->id.mfg_id,
170 cwd->id.part_id);
171 cwd->ignore_unique_id = false;
172 return 0;
173 }
174
175 dev_err(bus->dev,
176 "Invalid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n",
177 cwd->id.unique_id, id.unique_id, cwd->id.mfg_id, cwd->id.part_id);
178 return -ENODEV;
179}
180
181static int sdw_acpi_find_one(struct acpi_device *adev, void *data)
182{
183 struct sdw_bus *bus = data;
184 struct sdw_acpi_child_walk_data cwd = {
185 .bus = bus,
186 .adev = adev,
187 .ignore_unique_id = true,
188 };
189 int ret;
190
191 if (!find_slave(bus, adev, &cwd.id))
192 return 0;
193
194 /* Brute-force O(N^2) search for duplicates. */
195 ret = acpi_dev_for_each_child(ACPI_COMPANION(bus->dev),
196 sdw_acpi_check_duplicate, &cwd);
197 if (ret)
198 return ret;
199
200 if (cwd.ignore_unique_id)
201 cwd.id.unique_id = SDW_IGNORED_UNIQUE_ID;
202
203 /* Ignore errors and continue. */
204 sdw_slave_add(bus, &cwd.id, acpi_fwnode_handle(adev));
205 return 0;
206}
207
208/*
209 * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
210 * @bus: SDW bus instance
211 *
212 * Scans Master ACPI node for SDW child Slave devices and registers it.
213 */
214int sdw_acpi_find_slaves(struct sdw_bus *bus)
215{
216 struct acpi_device *parent;
217
218 parent = ACPI_COMPANION(bus->dev);
219 if (!parent) {
220 dev_err(bus->dev, "Can't find parent for acpi bind\n");
221 return -ENODEV;
222 }
223
224 return acpi_dev_for_each_child(parent, sdw_acpi_find_one, bus);
225}
226
227#endif
228
229/*
230 * sdw_of_find_slaves() - Find Slave devices in master device tree node
231 * @bus: SDW bus instance
232 *
233 * Scans Master DT node for SDW child Slave devices and registers it.
234 */
235int sdw_of_find_slaves(struct sdw_bus *bus)
236{
237 struct device *dev = bus->dev;
238 struct device_node *node;
239
240 for_each_child_of_node(bus->dev->of_node, node) {
241 int link_id, ret, len;
242 unsigned int sdw_version;
243 const char *compat = NULL;
244 struct sdw_slave_id id;
245 const __be32 *addr;
246
247 ret = of_property_read_string(node, "compatible", &compat);
248 if (ret)
249 continue;
250
251 ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
252 &id.mfg_id, &id.part_id, &id.class_id);
253
254 if (ret != 4) {
255 dev_err(dev, "Invalid compatible string found %s\n",
256 compat);
257 continue;
258 }
259
260 addr = of_get_property(node, "reg", &len);
261 if (!addr || (len < 2 * sizeof(u32))) {
262 dev_err(dev, "Invalid Link and Instance ID\n");
263 continue;
264 }
265
266 link_id = be32_to_cpup(addr++);
267 id.unique_id = be32_to_cpup(addr);
268 id.sdw_version = sdw_version;
269
270 /* Check for link_id match */
271 if (link_id != bus->link_id)
272 continue;
273
274 sdw_slave_add(bus, &id, of_fwnode_handle(node));
275 }
276
277 return 0;
278}
279
280struct device *of_sdw_find_device_by_node(struct device_node *np)
281{
282 return bus_find_device_by_of_node(&sdw_bus_type, np);
283}
284EXPORT_SYMBOL_GPL(of_sdw_find_device_by_node);
285
286MODULE_IMPORT_NS("SND_SOC_SDCA");