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 struct device_type sdw_slave_type = { 18 .name = "sdw_slave", 19 .release = sdw_slave_release, 20 .uevent = sdw_slave_uevent, 21 }; 22 23 static int sdw_slave_add(struct sdw_bus *bus, 24 struct sdw_slave_id *id, struct fwnode_handle *fwnode) 25 { 26 struct sdw_slave *slave; 27 int ret; 28 int i; 29 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 39 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 } 50 51 slave->dev.bus = &sdw_bus_type; 52 slave->dev.of_node = of_node_get(to_of_node(fwnode)); 53 slave->dev.type = &sdw_slave_type; 54 slave->bus = bus; 55 slave->status = SDW_SLAVE_UNATTACHED; 56 init_completion(&slave->enumeration_complete); 57 init_completion(&slave->initialization_complete); 58 slave->dev_num = 0; 59 init_completion(&slave->probe_complete); 60 slave->probed = false; 61 62 for (i = 0; i < SDW_MAX_PORTS; i++) 63 init_completion(&slave->port_ready[i]); 64 65 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); 81 82 return ret; 83 } 84 sdw_slave_debugfs_init(slave); 85 86 return ret; 87 } 88 89 #if IS_ENABLED(CONFIG_ACPI) 90 91 static 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 = SDW_DISCO_LINK_ID(addr); 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 120 /* 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 */ 126 int sdw_acpi_find_slaves(struct sdw_bus *bus) 127 { 128 struct acpi_device *adev, *parent; 129 struct acpi_device *adev2, *parent2; 130 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) { 138 struct sdw_slave_id id; 139 struct sdw_slave_id id2; 140 bool ignore_unique_id = true; 141 142 if (!find_slave(bus, adev, &id)) 143 continue; 144 145 /* 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 179 /* 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 190 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 */ 197 int 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) { 203 int link_id, ret, len; 204 unsigned int sdw_version; 205 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 } 241