dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / soundwire / slave.c
blob4bacdb187eab87665272714310dea7f5ddfc2668
1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2 // Copyright(c) 2015-17 Intel Corporation.
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 "bus.h"
10 static void sdw_slave_release(struct device *dev)
12 struct sdw_slave *slave = dev_to_sdw_dev(dev);
14 kfree(slave);
17 static int sdw_slave_add(struct sdw_bus *bus,
18 struct sdw_slave_id *id, struct fwnode_handle *fwnode)
20 struct sdw_slave *slave;
21 int ret;
23 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
24 if (!slave)
25 return -ENOMEM;
27 /* Initialize data structure */
28 memcpy(&slave->id, id, sizeof(*id));
29 slave->dev.parent = bus->dev;
30 slave->dev.fwnode = fwnode;
32 if (id->unique_id == SDW_IGNORED_UNIQUE_ID) {
33 /* name shall be sdw:link:mfg:part:class */
34 dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x",
35 bus->link_id, id->mfg_id, id->part_id,
36 id->class_id);
37 } else {
38 /* name shall be sdw:link:mfg:part:class:unique */
39 dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x:%x",
40 bus->link_id, id->mfg_id, id->part_id,
41 id->class_id, id->unique_id);
44 slave->dev.release = sdw_slave_release;
45 slave->dev.bus = &sdw_bus_type;
46 slave->dev.of_node = of_node_get(to_of_node(fwnode));
47 slave->bus = bus;
48 slave->status = SDW_SLAVE_UNATTACHED;
49 init_completion(&slave->enumeration_complete);
50 init_completion(&slave->initialization_complete);
51 slave->dev_num = 0;
52 init_completion(&slave->probe_complete);
53 slave->probed = false;
55 mutex_lock(&bus->bus_lock);
56 list_add_tail(&slave->node, &bus->slaves);
57 mutex_unlock(&bus->bus_lock);
59 ret = device_register(&slave->dev);
60 if (ret) {
61 dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
64 * On err, don't free but drop ref as this will be freed
65 * when release method is invoked.
67 mutex_lock(&bus->bus_lock);
68 list_del(&slave->node);
69 mutex_unlock(&bus->bus_lock);
70 put_device(&slave->dev);
72 return ret;
74 sdw_slave_debugfs_init(slave);
76 return ret;
79 #if IS_ENABLED(CONFIG_ACPI)
81 static bool find_slave(struct sdw_bus *bus,
82 struct acpi_device *adev,
83 struct sdw_slave_id *id)
85 unsigned long long addr;
86 unsigned int link_id;
87 acpi_status status;
89 status = acpi_evaluate_integer(adev->handle,
90 METHOD_NAME__ADR, NULL, &addr);
92 if (ACPI_FAILURE(status)) {
93 dev_err(bus->dev, "_ADR resolution failed: %x\n",
94 status);
95 return false;
98 /* Extract link id from ADR, Bit 51 to 48 (included) */
99 link_id = (addr >> 48) & GENMASK(3, 0);
101 /* Check for link_id match */
102 if (link_id != bus->link_id)
103 return false;
105 sdw_extract_slave_id(bus, addr, id);
107 return true;
111 * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
112 * @bus: SDW bus instance
114 * Scans Master ACPI node for SDW child Slave devices and registers it.
116 int sdw_acpi_find_slaves(struct sdw_bus *bus)
118 struct acpi_device *adev, *parent;
119 struct acpi_device *adev2, *parent2;
121 parent = ACPI_COMPANION(bus->dev);
122 if (!parent) {
123 dev_err(bus->dev, "Can't find parent for acpi bind\n");
124 return -ENODEV;
127 list_for_each_entry(adev, &parent->children, node) {
128 struct sdw_slave_id id;
129 struct sdw_slave_id id2;
130 bool ignore_unique_id = true;
132 if (!find_slave(bus, adev, &id))
133 continue;
135 /* brute-force O(N^2) search for duplicates */
136 parent2 = parent;
137 list_for_each_entry(adev2, &parent2->children, node) {
139 if (adev == adev2)
140 continue;
142 if (!find_slave(bus, adev2, &id2))
143 continue;
145 if (id.sdw_version != id2.sdw_version ||
146 id.mfg_id != id2.mfg_id ||
147 id.part_id != id2.part_id ||
148 id.class_id != id2.class_id)
149 continue;
151 if (id.unique_id != id2.unique_id) {
152 dev_dbg(bus->dev,
153 "Valid unique IDs %x %x for Slave mfg %x part %d\n",
154 id.unique_id, id2.unique_id,
155 id.mfg_id, id.part_id);
156 ignore_unique_id = false;
157 } else {
158 dev_err(bus->dev,
159 "Invalid 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 return -ENODEV;
166 if (ignore_unique_id)
167 id.unique_id = SDW_IGNORED_UNIQUE_ID;
170 * don't error check for sdw_slave_add as we want to continue
171 * adding Slaves
173 sdw_slave_add(bus, &id, acpi_fwnode_handle(adev));
176 return 0;
179 #endif
182 * sdw_of_find_slaves() - Find Slave devices in master device tree node
183 * @bus: SDW bus instance
185 * Scans Master DT node for SDW child Slave devices and registers it.
187 int sdw_of_find_slaves(struct sdw_bus *bus)
189 struct device *dev = bus->dev;
190 struct device_node *node;
192 for_each_child_of_node(bus->dev->of_node, node) {
193 int link_id, ret, len;
194 unsigned int sdw_version;
195 const char *compat = NULL;
196 struct sdw_slave_id id;
197 const __be32 *addr;
199 compat = of_get_property(node, "compatible", NULL);
200 if (!compat)
201 continue;
203 ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
204 &id.mfg_id, &id.part_id, &id.class_id);
206 if (ret != 4) {
207 dev_err(dev, "Invalid compatible string found %s\n",
208 compat);
209 continue;
212 addr = of_get_property(node, "reg", &len);
213 if (!addr || (len < 2 * sizeof(u32))) {
214 dev_err(dev, "Invalid Link and Instance ID\n");
215 continue;
218 link_id = be32_to_cpup(addr++);
219 id.unique_id = be32_to_cpup(addr);
220 id.sdw_version = sdw_version;
222 /* Check for link_id match */
223 if (link_id != bus->link_id)
224 continue;
226 sdw_slave_add(bus, &id, of_fwnode_handle(node));
229 return 0;