xref: /linux-6.15/drivers/soundwire/slave.c (revision de5b174b)
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 	/* name shall be sdw:link:mfg:part:class:unique */
33 	dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x:%x",
34 		     bus->link_id, id->mfg_id, id->part_id,
35 		     id->class_id, id->unique_id);
36 
37 	slave->dev.release = sdw_slave_release;
38 	slave->dev.bus = &sdw_bus_type;
39 	slave->dev.of_node = of_node_get(to_of_node(fwnode));
40 	slave->bus = bus;
41 	slave->status = SDW_SLAVE_UNATTACHED;
42 	slave->dev_num = 0;
43 
44 	mutex_lock(&bus->bus_lock);
45 	list_add_tail(&slave->node, &bus->slaves);
46 	mutex_unlock(&bus->bus_lock);
47 
48 	ret = device_register(&slave->dev);
49 	if (ret) {
50 		dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
51 
52 		/*
53 		 * On err, don't free but drop ref as this will be freed
54 		 * when release method is invoked.
55 		 */
56 		mutex_lock(&bus->bus_lock);
57 		list_del(&slave->node);
58 		mutex_unlock(&bus->bus_lock);
59 		put_device(&slave->dev);
60 	}
61 	sdw_slave_debugfs_init(slave);
62 
63 	return ret;
64 }
65 
66 #if IS_ENABLED(CONFIG_ACPI)
67 
68 static bool find_slave(struct sdw_bus *bus,
69 		       struct acpi_device *adev,
70 		       struct sdw_slave_id *id)
71 {
72 	unsigned long long addr;
73 	unsigned int link_id;
74 	acpi_status status;
75 
76 	status = acpi_evaluate_integer(adev->handle,
77 				       METHOD_NAME__ADR, NULL, &addr);
78 
79 	if (ACPI_FAILURE(status)) {
80 		dev_err(bus->dev, "_ADR resolution failed: %x\n",
81 			status);
82 		return false;
83 	}
84 
85 	/* Extract link id from ADR, Bit 51 to 48 (included) */
86 	link_id = (addr >> 48) & GENMASK(3, 0);
87 
88 	/* Check for link_id match */
89 	if (link_id != bus->link_id)
90 		return false;
91 
92 	sdw_extract_slave_id(bus, addr, id);
93 
94 	return true;
95 }
96 
97 /*
98  * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
99  * @bus: SDW bus instance
100  *
101  * Scans Master ACPI node for SDW child Slave devices and registers it.
102  */
103 int sdw_acpi_find_slaves(struct sdw_bus *bus)
104 {
105 	struct acpi_device *adev, *parent;
106 
107 	parent = ACPI_COMPANION(bus->dev);
108 	if (!parent) {
109 		dev_err(bus->dev, "Can't find parent for acpi bind\n");
110 		return -ENODEV;
111 	}
112 
113 	list_for_each_entry(adev, &parent->children, node) {
114 		struct sdw_slave_id id;
115 
116 		if (!find_slave(bus, adev, &id))
117 			continue;
118 
119 		/*
120 		 * don't error check for sdw_slave_add as we want to continue
121 		 * adding Slaves
122 		 */
123 		sdw_slave_add(bus, &id, acpi_fwnode_handle(adev));
124 	}
125 
126 	return 0;
127 }
128 
129 #endif
130 
131 /*
132  * sdw_of_find_slaves() - Find Slave devices in master device tree node
133  * @bus: SDW bus instance
134  *
135  * Scans Master DT node for SDW child Slave devices and registers it.
136  */
137 int sdw_of_find_slaves(struct sdw_bus *bus)
138 {
139 	struct device *dev = bus->dev;
140 	struct device_node *node;
141 
142 	for_each_child_of_node(bus->dev->of_node, node) {
143 		int link_id, sdw_version, ret, len;
144 		const char *compat = NULL;
145 		struct sdw_slave_id id;
146 		const __be32 *addr;
147 
148 		compat = of_get_property(node, "compatible", NULL);
149 		if (!compat)
150 			continue;
151 
152 		ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
153 			     &id.mfg_id, &id.part_id, &id.class_id);
154 
155 		if (ret != 4) {
156 			dev_err(dev, "Invalid compatible string found %s\n",
157 				compat);
158 			continue;
159 		}
160 
161 		addr = of_get_property(node, "reg", &len);
162 		if (!addr || (len < 2 * sizeof(u32))) {
163 			dev_err(dev, "Invalid Link and Instance ID\n");
164 			continue;
165 		}
166 
167 		link_id = be32_to_cpup(addr++);
168 		id.unique_id = be32_to_cpup(addr);
169 		id.sdw_version = sdw_version;
170 
171 		/* Check for link_id match */
172 		if (link_id != bus->link_id)
173 			continue;
174 
175 		sdw_slave_add(bus, &id, of_fwnode_handle(node));
176 	}
177 
178 	return 0;
179 }
180