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>
9 #include "sysfs_local.h"
11 static void sdw_slave_release(struct device *dev)
13 struct sdw_slave *slave = dev_to_sdw_dev(dev);
15 mutex_destroy(&slave->sdw_dev_lock);
19 struct device_type sdw_slave_type = {
21 .release = sdw_slave_release,
22 .uevent = sdw_slave_uevent,
25 int sdw_slave_add(struct sdw_bus *bus,
26 struct sdw_slave_id *id, struct fwnode_handle *fwnode)
28 struct sdw_slave *slave;
32 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
36 /* Initialize data structure */
37 memcpy(&slave->id, id, sizeof(*id));
38 slave->dev.parent = bus->dev;
39 slave->dev.fwnode = fwnode;
41 if (id->unique_id == SDW_IGNORED_UNIQUE_ID) {
42 /* name shall be sdw:link:mfg:part:class */
43 dev_set_name(&slave->dev, "sdw:%01x:%04x:%04x:%02x",
44 bus->link_id, id->mfg_id, id->part_id,
47 /* name shall be sdw:link:mfg:part:class:unique */
48 dev_set_name(&slave->dev, "sdw:%01x:%04x:%04x:%02x:%01x",
49 bus->link_id, id->mfg_id, id->part_id,
50 id->class_id, id->unique_id);
53 slave->dev.bus = &sdw_bus_type;
54 slave->dev.of_node = of_node_get(to_of_node(fwnode));
55 slave->dev.type = &sdw_slave_type;
56 slave->dev.groups = sdw_slave_status_attr_groups;
58 slave->status = SDW_SLAVE_UNATTACHED;
59 init_completion(&slave->enumeration_complete);
60 init_completion(&slave->initialization_complete);
62 slave->probed = false;
63 slave->first_interrupt_done = false;
64 mutex_init(&slave->sdw_dev_lock);
66 for (i = 0; i < SDW_MAX_PORTS; i++)
67 init_completion(&slave->port_ready[i]);
69 mutex_lock(&bus->bus_lock);
70 list_add_tail(&slave->node, &bus->slaves);
71 mutex_unlock(&bus->bus_lock);
73 ret = device_register(&slave->dev);
75 dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
78 * On err, don't free but drop ref as this will be freed
79 * when release method is invoked.
81 mutex_lock(&bus->bus_lock);
82 list_del(&slave->node);
83 mutex_unlock(&bus->bus_lock);
84 put_device(&slave->dev);
88 sdw_slave_debugfs_init(slave);
92 EXPORT_SYMBOL(sdw_slave_add);
94 #if IS_ENABLED(CONFIG_ACPI)
96 static bool find_slave(struct sdw_bus *bus,
97 struct acpi_device *adev,
98 struct sdw_slave_id *id)
101 unsigned int link_id;
104 status = acpi_evaluate_integer(adev->handle,
105 METHOD_NAME__ADR, NULL, &addr);
107 if (ACPI_FAILURE(status)) {
108 dev_err(bus->dev, "_ADR resolution failed: %x\n",
113 if (bus->ops->override_adr)
114 addr = bus->ops->override_adr(bus, addr);
119 /* Extract link id from ADR, Bit 51 to 48 (included) */
120 link_id = SDW_DISCO_LINK_ID(addr);
122 /* Check for link_id match */
123 if (link_id != bus->link_id)
126 sdw_extract_slave_id(bus, addr, id);
132 * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
133 * @bus: SDW bus instance
135 * Scans Master ACPI node for SDW child Slave devices and registers it.
137 int sdw_acpi_find_slaves(struct sdw_bus *bus)
139 struct acpi_device *adev, *parent;
140 struct acpi_device *adev2, *parent2;
142 parent = ACPI_COMPANION(bus->dev);
144 dev_err(bus->dev, "Can't find parent for acpi bind\n");
148 list_for_each_entry(adev, &parent->children, node) {
149 struct sdw_slave_id id;
150 struct sdw_slave_id id2;
151 bool ignore_unique_id = true;
153 if (!find_slave(bus, adev, &id))
156 /* brute-force O(N^2) search for duplicates */
158 list_for_each_entry(adev2, &parent2->children, node) {
163 if (!find_slave(bus, adev2, &id2))
166 if (id.sdw_version != id2.sdw_version ||
167 id.mfg_id != id2.mfg_id ||
168 id.part_id != id2.part_id ||
169 id.class_id != id2.class_id)
172 if (id.unique_id != id2.unique_id) {
174 "Valid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n",
175 id.unique_id, id2.unique_id, id.mfg_id, id.part_id);
176 ignore_unique_id = false;
179 "Invalid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n",
180 id.unique_id, id2.unique_id, id.mfg_id, id.part_id);
185 if (ignore_unique_id)
186 id.unique_id = SDW_IGNORED_UNIQUE_ID;
189 * don't error check for sdw_slave_add as we want to continue
192 sdw_slave_add(bus, &id, acpi_fwnode_handle(adev));
201 * sdw_of_find_slaves() - Find Slave devices in master device tree node
202 * @bus: SDW bus instance
204 * Scans Master DT node for SDW child Slave devices and registers it.
206 int sdw_of_find_slaves(struct sdw_bus *bus)
208 struct device *dev = bus->dev;
209 struct device_node *node;
211 for_each_child_of_node(bus->dev->of_node, node) {
212 int link_id, ret, len;
213 unsigned int sdw_version;
214 const char *compat = NULL;
215 struct sdw_slave_id id;
218 compat = of_get_property(node, "compatible", NULL);
222 ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
223 &id.mfg_id, &id.part_id, &id.class_id);
226 dev_err(dev, "Invalid compatible string found %s\n",
231 addr = of_get_property(node, "reg", &len);
232 if (!addr || (len < 2 * sizeof(u32))) {
233 dev_err(dev, "Invalid Link and Instance ID\n");
237 link_id = be32_to_cpup(addr++);
238 id.unique_id = be32_to_cpup(addr);
239 id.sdw_version = sdw_version;
241 /* Check for link_id match */
242 if (link_id != bus->link_id)
245 sdw_slave_add(bus, &id, of_fwnode_handle(node));