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