xref: /linux/drivers/soundwire/slave.c (revision 711673f8dd19cfb907913cb762d4c6c1b9d2a332)
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 
sdw_slave_release(struct device * dev)12 static 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 
21 const struct device_type sdw_slave_type = {
22 	.name =		"sdw_slave",
23 	.release =	sdw_slave_release,
24 	.uevent =	sdw_slave_uevent,
25 };
26 EXPORT_SYMBOL_GPL(sdw_slave_type);
27 
sdw_slave_add(struct sdw_bus * bus,struct sdw_slave_id * id,struct fwnode_handle * fwnode)28 int 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(sizeof(*slave), GFP_KERNEL);
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 }
106 EXPORT_SYMBOL(sdw_slave_add);
107 
108 #if IS_ENABLED(CONFIG_ACPI)
109 
find_slave(struct sdw_bus * bus,struct acpi_device * adev,struct sdw_slave_id * id)110 static 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 	ret = acpi_get_local_u64_address(adev->handle, &addr);
119 	if (ret < 0)
120 		return false;
121 
122 	if (bus->ops->override_adr)
123 		addr = bus->ops->override_adr(bus, addr);
124 
125 	if (!addr)
126 		return false;
127 
128 	/* Extract link id from ADR, Bit 51 to 48 (included) */
129 	link_id = SDW_DISCO_LINK_ID(addr);
130 
131 	/* Check for link_id match */
132 	if (link_id != bus->link_id)
133 		return false;
134 
135 	sdw_extract_slave_id(bus, addr, id);
136 
137 	return true;
138 }
139 
140 struct sdw_acpi_child_walk_data {
141 	struct sdw_bus *bus;
142 	struct acpi_device *adev;
143 	struct sdw_slave_id id;
144 	bool ignore_unique_id;
145 };
146 
sdw_acpi_check_duplicate(struct acpi_device * adev,void * data)147 static int sdw_acpi_check_duplicate(struct acpi_device *adev, void *data)
148 {
149 	struct sdw_acpi_child_walk_data *cwd = data;
150 	struct sdw_bus *bus = cwd->bus;
151 	struct sdw_slave_id id;
152 
153 	if (adev == cwd->adev)
154 		return 0;
155 
156 	if (!find_slave(bus, adev, &id))
157 		return 0;
158 
159 	if (cwd->id.sdw_version != id.sdw_version || cwd->id.mfg_id != id.mfg_id ||
160 	    cwd->id.part_id != id.part_id || cwd->id.class_id != id.class_id)
161 		return 0;
162 
163 	if (cwd->id.unique_id != id.unique_id) {
164 		dev_dbg(bus->dev,
165 			"Valid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n",
166 			cwd->id.unique_id, id.unique_id, cwd->id.mfg_id,
167 			cwd->id.part_id);
168 		cwd->ignore_unique_id = false;
169 		return 0;
170 	}
171 
172 	dev_err(bus->dev,
173 		"Invalid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n",
174 		cwd->id.unique_id, id.unique_id, cwd->id.mfg_id, cwd->id.part_id);
175 	return -ENODEV;
176 }
177 
sdw_acpi_find_one(struct acpi_device * adev,void * data)178 static int sdw_acpi_find_one(struct acpi_device *adev, void *data)
179 {
180 	struct sdw_bus *bus = data;
181 	struct sdw_acpi_child_walk_data cwd = {
182 		.bus = bus,
183 		.adev = adev,
184 		.ignore_unique_id = true,
185 	};
186 	int ret;
187 
188 	if (!find_slave(bus, adev, &cwd.id))
189 		return 0;
190 
191 	/* Brute-force O(N^2) search for duplicates. */
192 	ret = acpi_dev_for_each_child(ACPI_COMPANION(bus->dev),
193 				      sdw_acpi_check_duplicate, &cwd);
194 	if (ret)
195 		return ret;
196 
197 	if (cwd.ignore_unique_id)
198 		cwd.id.unique_id = SDW_IGNORED_UNIQUE_ID;
199 
200 	/* Ignore errors and continue. */
201 	sdw_slave_add(bus, &cwd.id, acpi_fwnode_handle(adev));
202 	return 0;
203 }
204 
205 /*
206  * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
207  * @bus: SDW bus instance
208  *
209  * Scans Master ACPI node for SDW child Slave devices and registers it.
210  */
sdw_acpi_find_slaves(struct sdw_bus * bus)211 int sdw_acpi_find_slaves(struct sdw_bus *bus)
212 {
213 	struct acpi_device *parent;
214 
215 	parent = ACPI_COMPANION(bus->dev);
216 	if (!parent) {
217 		dev_err(bus->dev, "Can't find parent for acpi bind\n");
218 		return -ENODEV;
219 	}
220 
221 	return acpi_dev_for_each_child(parent, sdw_acpi_find_one, bus);
222 }
223 
224 #endif
225 
226 /*
227  * sdw_of_find_slaves() - Find Slave devices in master device tree node
228  * @bus: SDW bus instance
229  *
230  * Scans Master DT node for SDW child Slave devices and registers it.
231  */
sdw_of_find_slaves(struct sdw_bus * bus)232 int sdw_of_find_slaves(struct sdw_bus *bus)
233 {
234 	struct device *dev = bus->dev;
235 	struct device_node *node;
236 
237 	for_each_child_of_node(bus->dev->of_node, node) {
238 		int link_id, ret, len;
239 		unsigned int sdw_version;
240 		const char *compat = NULL;
241 		struct sdw_slave_id id;
242 		const __be32 *addr;
243 
244 		compat = of_get_property(node, "compatible", NULL);
245 		if (!compat)
246 			continue;
247 
248 		ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
249 			     &id.mfg_id, &id.part_id, &id.class_id);
250 
251 		if (ret != 4) {
252 			dev_err(dev, "Invalid compatible string found %s\n",
253 				compat);
254 			continue;
255 		}
256 
257 		addr = of_get_property(node, "reg", &len);
258 		if (!addr || (len < 2 * sizeof(u32))) {
259 			dev_err(dev, "Invalid Link and Instance ID\n");
260 			continue;
261 		}
262 
263 		link_id = be32_to_cpup(addr++);
264 		id.unique_id = be32_to_cpup(addr);
265 		id.sdw_version = sdw_version;
266 
267 		/* Check for link_id match */
268 		if (link_id != bus->link_id)
269 			continue;
270 
271 		sdw_slave_add(bus, &id, of_fwnode_handle(node));
272 	}
273 
274 	return 0;
275 }
276 
of_sdw_find_device_by_node(struct device_node * np)277 struct device *of_sdw_find_device_by_node(struct device_node *np)
278 {
279 	return bus_find_device_by_of_node(&sdw_bus_type, np);
280 }
281 EXPORT_SYMBOL_GPL(of_sdw_find_device_by_node);
282 
283 MODULE_IMPORT_NS("SND_SOC_SDCA");
284