xref: /openbmc/linux/drivers/soundwire/slave.c (revision 12a5b00a)
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 "bus.h"
9 
10 static void sdw_slave_release(struct device *dev)
11 {
12 	struct sdw_slave *slave = dev_to_sdw_dev(dev);
13 
14 	kfree(slave);
15 }
16 
17 static int sdw_slave_add(struct sdw_bus *bus,
18 			 struct sdw_slave_id *id, struct fwnode_handle *fwnode)
19 {
20 	struct sdw_slave *slave;
21 	int ret;
22 
23 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
24 	if (!slave)
25 		return -ENOMEM;
26 
27 	/* Initialize data structure */
28 	memcpy(&slave->id, id, sizeof(*id));
29 	slave->dev.parent = bus->dev;
30 	slave->dev.fwnode = fwnode;
31 
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);
42 	}
43 
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;
54 
55 	mutex_lock(&bus->bus_lock);
56 	list_add_tail(&slave->node, &bus->slaves);
57 	mutex_unlock(&bus->bus_lock);
58 
59 	ret = device_register(&slave->dev);
60 	if (ret) {
61 		dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
62 
63 		/*
64 		 * On err, don't free but drop ref as this will be freed
65 		 * when release method is invoked.
66 		 */
67 		mutex_lock(&bus->bus_lock);
68 		list_del(&slave->node);
69 		mutex_unlock(&bus->bus_lock);
70 		put_device(&slave->dev);
71 	}
72 	sdw_slave_debugfs_init(slave);
73 
74 	return ret;
75 }
76 
77 #if IS_ENABLED(CONFIG_ACPI)
78 
79 static bool find_slave(struct sdw_bus *bus,
80 		       struct acpi_device *adev,
81 		       struct sdw_slave_id *id)
82 {
83 	unsigned long long addr;
84 	unsigned int link_id;
85 	acpi_status status;
86 
87 	status = acpi_evaluate_integer(adev->handle,
88 				       METHOD_NAME__ADR, NULL, &addr);
89 
90 	if (ACPI_FAILURE(status)) {
91 		dev_err(bus->dev, "_ADR resolution failed: %x\n",
92 			status);
93 		return false;
94 	}
95 
96 	/* Extract link id from ADR, Bit 51 to 48 (included) */
97 	link_id = (addr >> 48) & GENMASK(3, 0);
98 
99 	/* Check for link_id match */
100 	if (link_id != bus->link_id)
101 		return false;
102 
103 	sdw_extract_slave_id(bus, addr, id);
104 
105 	return true;
106 }
107 
108 /*
109  * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
110  * @bus: SDW bus instance
111  *
112  * Scans Master ACPI node for SDW child Slave devices and registers it.
113  */
114 int sdw_acpi_find_slaves(struct sdw_bus *bus)
115 {
116 	struct acpi_device *adev, *parent;
117 	struct acpi_device *adev2, *parent2;
118 
119 	parent = ACPI_COMPANION(bus->dev);
120 	if (!parent) {
121 		dev_err(bus->dev, "Can't find parent for acpi bind\n");
122 		return -ENODEV;
123 	}
124 
125 	list_for_each_entry(adev, &parent->children, node) {
126 		struct sdw_slave_id id;
127 		struct sdw_slave_id id2;
128 		bool ignore_unique_id = true;
129 
130 		if (!find_slave(bus, adev, &id))
131 			continue;
132 
133 		/* brute-force O(N^2) search for duplicates */
134 		parent2 = parent;
135 		list_for_each_entry(adev2, &parent2->children, node) {
136 
137 			if (adev == adev2)
138 				continue;
139 
140 			if (!find_slave(bus, adev2, &id2))
141 				continue;
142 
143 			if (id.sdw_version != id2.sdw_version ||
144 			    id.mfg_id != id2.mfg_id ||
145 			    id.part_id != id2.part_id ||
146 			    id.class_id != id2.class_id)
147 				continue;
148 
149 			if (id.unique_id != id2.unique_id) {
150 				dev_dbg(bus->dev,
151 					"Valid unique IDs %x %x for Slave mfg %x part %d\n",
152 					id.unique_id, id2.unique_id,
153 					id.mfg_id, id.part_id);
154 				ignore_unique_id = false;
155 			} else {
156 				dev_err(bus->dev,
157 					"Invalid unique IDs %x %x for Slave mfg %x part %d\n",
158 					id.unique_id, id2.unique_id,
159 					id.mfg_id, id.part_id);
160 				return -ENODEV;
161 			}
162 		}
163 
164 		if (ignore_unique_id)
165 			id.unique_id = SDW_IGNORED_UNIQUE_ID;
166 
167 		/*
168 		 * don't error check for sdw_slave_add as we want to continue
169 		 * adding Slaves
170 		 */
171 		sdw_slave_add(bus, &id, acpi_fwnode_handle(adev));
172 	}
173 
174 	return 0;
175 }
176 
177 #endif
178 
179 /*
180  * sdw_of_find_slaves() - Find Slave devices in master device tree node
181  * @bus: SDW bus instance
182  *
183  * Scans Master DT node for SDW child Slave devices and registers it.
184  */
185 int sdw_of_find_slaves(struct sdw_bus *bus)
186 {
187 	struct device *dev = bus->dev;
188 	struct device_node *node;
189 
190 	for_each_child_of_node(bus->dev->of_node, node) {
191 		int link_id, ret, len;
192 		unsigned int sdw_version;
193 		const char *compat = NULL;
194 		struct sdw_slave_id id;
195 		const __be32 *addr;
196 
197 		compat = of_get_property(node, "compatible", NULL);
198 		if (!compat)
199 			continue;
200 
201 		ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
202 			     &id.mfg_id, &id.part_id, &id.class_id);
203 
204 		if (ret != 4) {
205 			dev_err(dev, "Invalid compatible string found %s\n",
206 				compat);
207 			continue;
208 		}
209 
210 		addr = of_get_property(node, "reg", &len);
211 		if (!addr || (len < 2 * sizeof(u32))) {
212 			dev_err(dev, "Invalid Link and Instance ID\n");
213 			continue;
214 		}
215 
216 		link_id = be32_to_cpup(addr++);
217 		id.unique_id = be32_to_cpup(addr);
218 		id.sdw_version = sdw_version;
219 
220 		/* Check for link_id match */
221 		if (link_id != bus->link_id)
222 			continue;
223 
224 		sdw_slave_add(bus, &id, of_fwnode_handle(node));
225 	}
226 
227 	return 0;
228 }
229