blob: a762ee24e6faee5c74df0c3caccfb76d7b0bcf71 [file] [log] [blame]
Vinod Koul7c3cd182017-12-14 11:19:34 +05301// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2// Copyright(c) 2015-17 Intel Corporation.
3
4#include <linux/acpi.h>
Srinivas Kandagatlaa2e48452019-08-29 17:35:12 +01005#include <linux/of.h>
Vinod Koul7c3cd182017-12-14 11:19:34 +05306#include <linux/soundwire/sdw.h>
7#include <linux/soundwire/sdw_type.h>
8#include "bus.h"
9
10static void sdw_slave_release(struct device *dev)
11{
12 struct sdw_slave *slave = dev_to_sdw_dev(dev);
13
14 kfree(slave);
15}
16
Pierre-Louis Bossart90acca12020-05-19 01:43:19 +080017struct device_type sdw_slave_type = {
18 .name = "sdw_slave",
19 .release = sdw_slave_release,
20 .uevent = sdw_slave_uevent,
21};
22
Vinod Koul7c3cd182017-12-14 11:19:34 +053023static int sdw_slave_add(struct sdw_bus *bus,
Pierre-Louis Bossartc0cbfb02019-05-01 10:57:36 -050024 struct sdw_slave_id *id, struct fwnode_handle *fwnode)
Vinod Koul7c3cd182017-12-14 11:19:34 +053025{
26 struct sdw_slave *slave;
27 int ret;
Pierre-Louis Bossart60737552020-08-31 21:43:18 +080028 int i;
Vinod Koul7c3cd182017-12-14 11:19:34 +053029
30 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
31 if (!slave)
32 return -ENOMEM;
33
34 /* Initialize data structure */
35 memcpy(&slave->id, id, sizeof(*id));
36 slave->dev.parent = bus->dev;
37 slave->dev.fwnode = fwnode;
38
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -050039 if (id->unique_id == SDW_IGNORED_UNIQUE_ID) {
40 /* name shall be sdw:link:mfg:part:class */
41 dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x",
42 bus->link_id, id->mfg_id, id->part_id,
43 id->class_id);
44 } else {
45 /* name shall be sdw:link:mfg:part:class:unique */
46 dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x:%x",
47 bus->link_id, id->mfg_id, id->part_id,
48 id->class_id, id->unique_id);
49 }
Vinod Koul7c3cd182017-12-14 11:19:34 +053050
Vinod Koul7c3cd182017-12-14 11:19:34 +053051 slave->dev.bus = &sdw_bus_type;
Srinivas Kandagatlaa2e48452019-08-29 17:35:12 +010052 slave->dev.of_node = of_node_get(to_of_node(fwnode));
Pierre-Louis Bossart90acca12020-05-19 01:43:19 +080053 slave->dev.type = &sdw_slave_type;
Vinod Koul7c3cd182017-12-14 11:19:34 +053054 slave->bus = bus;
55 slave->status = SDW_SLAVE_UNATTACHED;
Pierre-Louis Bossartfb9469e2020-01-14 18:08:36 -060056 init_completion(&slave->enumeration_complete);
Pierre-Louis Bossarta90def02020-01-14 18:08:37 -060057 init_completion(&slave->initialization_complete);
Vinod Koul7c3cd182017-12-14 11:19:34 +053058 slave->dev_num = 0;
Pierre-Louis Bossart2140b662020-01-14 18:08:35 -060059 init_completion(&slave->probe_complete);
60 slave->probed = false;
Vinod Koul7c3cd182017-12-14 11:19:34 +053061
Pierre-Louis Bossart60737552020-08-31 21:43:18 +080062 for (i = 0; i < SDW_MAX_PORTS; i++)
63 init_completion(&slave->port_ready[i]);
64
Vinod Koul7c3cd182017-12-14 11:19:34 +053065 mutex_lock(&bus->bus_lock);
66 list_add_tail(&slave->node, &bus->slaves);
67 mutex_unlock(&bus->bus_lock);
68
69 ret = device_register(&slave->dev);
70 if (ret) {
71 dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
72
73 /*
74 * On err, don't free but drop ref as this will be freed
75 * when release method is invoked.
76 */
77 mutex_lock(&bus->bus_lock);
78 list_del(&slave->node);
79 mutex_unlock(&bus->bus_lock);
80 put_device(&slave->dev);
Pierre-Louis Bossart8893ab52020-04-20 02:51:15 +080081
82 return ret;
Vinod Koul7c3cd182017-12-14 11:19:34 +053083 }
Pierre-Louis Bossartbf034732019-08-21 13:58:18 -050084 sdw_slave_debugfs_init(slave);
Vinod Koul7c3cd182017-12-14 11:19:34 +053085
86 return ret;
87}
88
89#if IS_ENABLED(CONFIG_ACPI)
Pierre-Louis Bossartde5b1742019-10-22 18:48:07 -050090
91static bool find_slave(struct sdw_bus *bus,
92 struct acpi_device *adev,
93 struct sdw_slave_id *id)
94{
95 unsigned long long addr;
96 unsigned int link_id;
97 acpi_status status;
98
99 status = acpi_evaluate_integer(adev->handle,
100 METHOD_NAME__ADR, NULL, &addr);
101
102 if (ACPI_FAILURE(status)) {
103 dev_err(bus->dev, "_ADR resolution failed: %x\n",
104 status);
105 return false;
106 }
107
108 /* Extract link id from ADR, Bit 51 to 48 (included) */
109 link_id = (addr >> 48) & GENMASK(3, 0);
110
111 /* Check for link_id match */
112 if (link_id != bus->link_id)
113 return false;
114
115 sdw_extract_slave_id(bus, addr, id);
116
117 return true;
118}
119
Vinod Koul7c3cd182017-12-14 11:19:34 +0530120/*
121 * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
122 * @bus: SDW bus instance
123 *
124 * Scans Master ACPI node for SDW child Slave devices and registers it.
125 */
126int sdw_acpi_find_slaves(struct sdw_bus *bus)
127{
128 struct acpi_device *adev, *parent;
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500129 struct acpi_device *adev2, *parent2;
Vinod Koul7c3cd182017-12-14 11:19:34 +0530130
131 parent = ACPI_COMPANION(bus->dev);
132 if (!parent) {
133 dev_err(bus->dev, "Can't find parent for acpi bind\n");
134 return -ENODEV;
135 }
136
137 list_for_each_entry(adev, &parent->children, node) {
Vinod Koul7c3cd182017-12-14 11:19:34 +0530138 struct sdw_slave_id id;
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500139 struct sdw_slave_id id2;
140 bool ignore_unique_id = true;
Vinod Koul7c3cd182017-12-14 11:19:34 +0530141
Pierre-Louis Bossartde5b1742019-10-22 18:48:07 -0500142 if (!find_slave(bus, adev, &id))
Vinod Koul7c3cd182017-12-14 11:19:34 +0530143 continue;
144
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500145 /* brute-force O(N^2) search for duplicates */
146 parent2 = parent;
147 list_for_each_entry(adev2, &parent2->children, node) {
148
149 if (adev == adev2)
150 continue;
151
152 if (!find_slave(bus, adev2, &id2))
153 continue;
154
155 if (id.sdw_version != id2.sdw_version ||
156 id.mfg_id != id2.mfg_id ||
157 id.part_id != id2.part_id ||
158 id.class_id != id2.class_id)
159 continue;
160
161 if (id.unique_id != id2.unique_id) {
162 dev_dbg(bus->dev,
163 "Valid unique IDs %x %x for Slave mfg %x part %d\n",
164 id.unique_id, id2.unique_id,
165 id.mfg_id, id.part_id);
166 ignore_unique_id = false;
167 } else {
168 dev_err(bus->dev,
169 "Invalid unique IDs %x %x for Slave mfg %x part %d\n",
170 id.unique_id, id2.unique_id,
171 id.mfg_id, id.part_id);
172 return -ENODEV;
173 }
174 }
175
176 if (ignore_unique_id)
177 id.unique_id = SDW_IGNORED_UNIQUE_ID;
178
Vinod Koul7c3cd182017-12-14 11:19:34 +0530179 /*
180 * don't error check for sdw_slave_add as we want to continue
181 * adding Slaves
182 */
183 sdw_slave_add(bus, &id, acpi_fwnode_handle(adev));
184 }
185
186 return 0;
187}
188
189#endif
Srinivas Kandagatlaa2e48452019-08-29 17:35:12 +0100190
191/*
192 * sdw_of_find_slaves() - Find Slave devices in master device tree node
193 * @bus: SDW bus instance
194 *
195 * Scans Master DT node for SDW child Slave devices and registers it.
196 */
197int sdw_of_find_slaves(struct sdw_bus *bus)
198{
199 struct device *dev = bus->dev;
200 struct device_node *node;
201
202 for_each_child_of_node(bus->dev->of_node, node) {
Pierre-Louis Bossart7b47ad32019-10-22 18:31:47 -0500203 int link_id, ret, len;
204 unsigned int sdw_version;
Srinivas Kandagatlaa2e48452019-08-29 17:35:12 +0100205 const char *compat = NULL;
206 struct sdw_slave_id id;
207 const __be32 *addr;
208
209 compat = of_get_property(node, "compatible", NULL);
210 if (!compat)
211 continue;
212
213 ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
214 &id.mfg_id, &id.part_id, &id.class_id);
215
216 if (ret != 4) {
217 dev_err(dev, "Invalid compatible string found %s\n",
218 compat);
219 continue;
220 }
221
222 addr = of_get_property(node, "reg", &len);
223 if (!addr || (len < 2 * sizeof(u32))) {
224 dev_err(dev, "Invalid Link and Instance ID\n");
225 continue;
226 }
227
228 link_id = be32_to_cpup(addr++);
229 id.unique_id = be32_to_cpup(addr);
230 id.sdw_version = sdw_version;
231
232 /* Check for link_id match */
233 if (link_id != bus->link_id)
234 continue;
235
236 sdw_slave_add(bus, &id, of_fwnode_handle(node));
237 }
238
239 return 0;
240}