1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
4 #include <linux/acpi.h>
6 #include <linux/soundwire/sdw.h>
7 #include <linux/soundwire/sdw_type.h>
10 static void sdw_slave_release(struct device *dev)
12 struct sdw_slave *slave = dev_to_sdw_dev(dev);
17 struct device_type sdw_slave_type = {
19 .release = sdw_slave_release,
20 .uevent = sdw_slave_uevent,
23 static int sdw_slave_add(struct sdw_bus *bus,
24 struct sdw_slave_id *id, struct fwnode_handle *fwnode)
26 struct sdw_slave *slave;
29 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
33 /* Initialize data structure */
34 memcpy(&slave->id, id, sizeof(*id));
35 slave->dev.parent = bus->dev;
36 slave->dev.fwnode = fwnode;
38 if (id->unique_id == SDW_IGNORED_UNIQUE_ID) {
39 /* name shall be sdw:link:mfg:part:class */
40 dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x",
41 bus->link_id, id->mfg_id, id->part_id,
44 /* name shall be sdw:link:mfg:part:class:unique */
45 dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x:%x",
46 bus->link_id, id->mfg_id, id->part_id,
47 id->class_id, id->unique_id);
50 slave->dev.bus = &sdw_bus_type;
51 slave->dev.of_node = of_node_get(to_of_node(fwnode));
52 slave->dev.type = &sdw_slave_type;
54 slave->status = SDW_SLAVE_UNATTACHED;
55 init_completion(&slave->enumeration_complete);
56 init_completion(&slave->initialization_complete);
58 init_completion(&slave->probe_complete);
59 slave->probed = false;
61 mutex_lock(&bus->bus_lock);
62 list_add_tail(&slave->node, &bus->slaves);
63 mutex_unlock(&bus->bus_lock);
65 ret = device_register(&slave->dev);
67 dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
70 * On err, don't free but drop ref as this will be freed
71 * when release method is invoked.
73 mutex_lock(&bus->bus_lock);
74 list_del(&slave->node);
75 mutex_unlock(&bus->bus_lock);
76 put_device(&slave->dev);
80 sdw_slave_debugfs_init(slave);
85 #if IS_ENABLED(CONFIG_ACPI)
87 static bool find_slave(struct sdw_bus *bus,
88 struct acpi_device *adev,
89 struct sdw_slave_id *id)
91 unsigned long long addr;
95 status = acpi_evaluate_integer(adev->handle,
96 METHOD_NAME__ADR, NULL, &addr);
98 if (ACPI_FAILURE(status)) {
99 dev_err(bus->dev, "_ADR resolution failed: %x\n",
104 /* Extract link id from ADR, Bit 51 to 48 (included) */
105 link_id = (addr >> 48) & GENMASK(3, 0);
107 /* Check for link_id match */
108 if (link_id != bus->link_id)
111 sdw_extract_slave_id(bus, addr, id);
117 * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
118 * @bus: SDW bus instance
120 * Scans Master ACPI node for SDW child Slave devices and registers it.
122 int sdw_acpi_find_slaves(struct sdw_bus *bus)
124 struct acpi_device *adev, *parent;
125 struct acpi_device *adev2, *parent2;
127 parent = ACPI_COMPANION(bus->dev);
129 dev_err(bus->dev, "Can't find parent for acpi bind\n");
133 list_for_each_entry(adev, &parent->children, node) {
134 struct sdw_slave_id id;
135 struct sdw_slave_id id2;
136 bool ignore_unique_id = true;
138 if (!find_slave(bus, adev, &id))
141 /* brute-force O(N^2) search for duplicates */
143 list_for_each_entry(adev2, &parent2->children, node) {
148 if (!find_slave(bus, adev2, &id2))
151 if (id.sdw_version != id2.sdw_version ||
152 id.mfg_id != id2.mfg_id ||
153 id.part_id != id2.part_id ||
154 id.class_id != id2.class_id)
157 if (id.unique_id != id2.unique_id) {
159 "Valid unique IDs %x %x for Slave mfg %x part %d\n",
160 id.unique_id, id2.unique_id,
161 id.mfg_id, id.part_id);
162 ignore_unique_id = false;
165 "Invalid unique IDs %x %x for Slave mfg %x part %d\n",
166 id.unique_id, id2.unique_id,
167 id.mfg_id, id.part_id);
172 if (ignore_unique_id)
173 id.unique_id = SDW_IGNORED_UNIQUE_ID;
176 * don't error check for sdw_slave_add as we want to continue
179 sdw_slave_add(bus, &id, acpi_fwnode_handle(adev));
188 * sdw_of_find_slaves() - Find Slave devices in master device tree node
189 * @bus: SDW bus instance
191 * Scans Master DT node for SDW child Slave devices and registers it.
193 int sdw_of_find_slaves(struct sdw_bus *bus)
195 struct device *dev = bus->dev;
196 struct device_node *node;
198 for_each_child_of_node(bus->dev->of_node, node) {
199 int link_id, ret, len;
200 unsigned int sdw_version;
201 const char *compat = NULL;
202 struct sdw_slave_id id;
205 compat = of_get_property(node, "compatible", NULL);
209 ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
210 &id.mfg_id, &id.part_id, &id.class_id);
213 dev_err(dev, "Invalid compatible string found %s\n",
218 addr = of_get_property(node, "reg", &len);
219 if (!addr || (len < 2 * sizeof(u32))) {
220 dev_err(dev, "Invalid Link and Instance ID\n");
224 link_id = be32_to_cpup(addr++);
225 id.unique_id = be32_to_cpup(addr);
226 id.sdw_version = sdw_version;
228 /* Check for link_id match */
229 if (link_id != bus->link_id)
232 sdw_slave_add(bus, &id, of_fwnode_handle(node));