blob: 4a250d33de5d0731e351ed286bec934847e5ba6e [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;
Pierre-Louis Bossartc2819e12020-09-08 21:45:17 +080061 slave->first_interrupt_done = false;
Vinod Koul7c3cd182017-12-14 11:19:34 +053062
Pierre-Louis Bossart60737552020-08-31 21:43:18 +080063 for (i = 0; i < SDW_MAX_PORTS; i++)
64 init_completion(&slave->port_ready[i]);
65
Vinod Koul7c3cd182017-12-14 11:19:34 +053066 mutex_lock(&bus->bus_lock);
67 list_add_tail(&slave->node, &bus->slaves);
68 mutex_unlock(&bus->bus_lock);
69
70 ret = device_register(&slave->dev);
71 if (ret) {
72 dev_err(bus->dev, "Failed to add slave: ret %d\n", ret);
73
74 /*
75 * On err, don't free but drop ref as this will be freed
76 * when release method is invoked.
77 */
78 mutex_lock(&bus->bus_lock);
79 list_del(&slave->node);
80 mutex_unlock(&bus->bus_lock);
81 put_device(&slave->dev);
Pierre-Louis Bossart8893ab52020-04-20 02:51:15 +080082
83 return ret;
Vinod Koul7c3cd182017-12-14 11:19:34 +053084 }
Pierre-Louis Bossartbf034732019-08-21 13:58:18 -050085 sdw_slave_debugfs_init(slave);
Vinod Koul7c3cd182017-12-14 11:19:34 +053086
87 return ret;
88}
89
90#if IS_ENABLED(CONFIG_ACPI)
Pierre-Louis Bossartde5b1742019-10-22 18:48:07 -050091
92static bool find_slave(struct sdw_bus *bus,
93 struct acpi_device *adev,
94 struct sdw_slave_id *id)
95{
96 unsigned long long addr;
97 unsigned int link_id;
98 acpi_status status;
99
100 status = acpi_evaluate_integer(adev->handle,
101 METHOD_NAME__ADR, NULL, &addr);
102
103 if (ACPI_FAILURE(status)) {
104 dev_err(bus->dev, "_ADR resolution failed: %x\n",
105 status);
106 return false;
107 }
108
109 /* Extract link id from ADR, Bit 51 to 48 (included) */
Vinod Koulbd6a0242020-09-03 17:14:58 +0530110 link_id = SDW_DISCO_LINK_ID(addr);
Pierre-Louis Bossartde5b1742019-10-22 18:48:07 -0500111
112 /* Check for link_id match */
113 if (link_id != bus->link_id)
114 return false;
115
116 sdw_extract_slave_id(bus, addr, id);
117
118 return true;
119}
120
Vinod Koul7c3cd182017-12-14 11:19:34 +0530121/*
122 * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node
123 * @bus: SDW bus instance
124 *
125 * Scans Master ACPI node for SDW child Slave devices and registers it.
126 */
127int sdw_acpi_find_slaves(struct sdw_bus *bus)
128{
129 struct acpi_device *adev, *parent;
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500130 struct acpi_device *adev2, *parent2;
Vinod Koul7c3cd182017-12-14 11:19:34 +0530131
132 parent = ACPI_COMPANION(bus->dev);
133 if (!parent) {
134 dev_err(bus->dev, "Can't find parent for acpi bind\n");
135 return -ENODEV;
136 }
137
138 list_for_each_entry(adev, &parent->children, node) {
Vinod Koul7c3cd182017-12-14 11:19:34 +0530139 struct sdw_slave_id id;
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500140 struct sdw_slave_id id2;
141 bool ignore_unique_id = true;
Vinod Koul7c3cd182017-12-14 11:19:34 +0530142
Pierre-Louis Bossartde5b1742019-10-22 18:48:07 -0500143 if (!find_slave(bus, adev, &id))
Vinod Koul7c3cd182017-12-14 11:19:34 +0530144 continue;
145
Pierre-Louis Bossart2e8c4ad2019-10-22 18:48:08 -0500146 /* brute-force O(N^2) search for duplicates */
147 parent2 = parent;
148 list_for_each_entry(adev2, &parent2->children, node) {
149
150 if (adev == adev2)
151 continue;
152
153 if (!find_slave(bus, adev2, &id2))
154 continue;
155
156 if (id.sdw_version != id2.sdw_version ||
157 id.mfg_id != id2.mfg_id ||
158 id.part_id != id2.part_id ||
159 id.class_id != id2.class_id)
160 continue;
161
162 if (id.unique_id != id2.unique_id) {
163 dev_dbg(bus->dev,
164 "Valid unique IDs %x %x for Slave mfg %x part %d\n",
165 id.unique_id, id2.unique_id,
166 id.mfg_id, id.part_id);
167 ignore_unique_id = false;
168 } else {
169 dev_err(bus->dev,
170 "Invalid unique IDs %x %x for Slave mfg %x part %d\n",
171 id.unique_id, id2.unique_id,
172 id.mfg_id, id.part_id);
173 return -ENODEV;
174 }
175 }
176
177 if (ignore_unique_id)
178 id.unique_id = SDW_IGNORED_UNIQUE_ID;
179
Vinod Koul7c3cd182017-12-14 11:19:34 +0530180 /*
181 * don't error check for sdw_slave_add as we want to continue
182 * adding Slaves
183 */
184 sdw_slave_add(bus, &id, acpi_fwnode_handle(adev));
185 }
186
187 return 0;
188}
189
190#endif
Srinivas Kandagatlaa2e48452019-08-29 17:35:12 +0100191
192/*
193 * sdw_of_find_slaves() - Find Slave devices in master device tree node
194 * @bus: SDW bus instance
195 *
196 * Scans Master DT node for SDW child Slave devices and registers it.
197 */
198int sdw_of_find_slaves(struct sdw_bus *bus)
199{
200 struct device *dev = bus->dev;
201 struct device_node *node;
202
203 for_each_child_of_node(bus->dev->of_node, node) {
Pierre-Louis Bossart7b47ad32019-10-22 18:31:47 -0500204 int link_id, ret, len;
205 unsigned int sdw_version;
Srinivas Kandagatlaa2e48452019-08-29 17:35:12 +0100206 const char *compat = NULL;
207 struct sdw_slave_id id;
208 const __be32 *addr;
209
210 compat = of_get_property(node, "compatible", NULL);
211 if (!compat)
212 continue;
213
214 ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version,
215 &id.mfg_id, &id.part_id, &id.class_id);
216
217 if (ret != 4) {
218 dev_err(dev, "Invalid compatible string found %s\n",
219 compat);
220 continue;
221 }
222
223 addr = of_get_property(node, "reg", &len);
224 if (!addr || (len < 2 * sizeof(u32))) {
225 dev_err(dev, "Invalid Link and Instance ID\n");
226 continue;
227 }
228
229 link_id = be32_to_cpup(addr++);
230 id.unique_id = be32_to_cpup(addr);
231 id.sdw_version = sdw_version;
232
233 /* Check for link_id match */
234 if (link_id != bus->link_id)
235 continue;
236
237 sdw_slave_add(bus, &id, of_fwnode_handle(node));
238 }
239
240 return 0;
241}