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 #include "sysfs_local.h" 10 11 static void sdw_slave_release(struct device *dev) 12 { 13 struct sdw_slave *slave = dev_to_sdw_dev(dev); 14 15 kfree(slave); 16 } 17 18 struct device_type sdw_slave_type = { 19 .name = "sdw_slave", 20 .release = sdw_slave_release, 21 .uevent = sdw_slave_uevent, 22 }; 23 24 int sdw_slave_add(struct sdw_bus *bus, 25 struct sdw_slave_id *id, struct fwnode_handle *fwnode) 26 { 27 struct sdw_slave *slave; 28 int ret; 29 int i; 30 31 slave = kzalloc(sizeof(*slave), GFP_KERNEL); 32 if (!slave) 33 return -ENOMEM; 34 35 /* Initialize data structure */ 36 memcpy(&slave->id, id, sizeof(*id)); 37 slave->dev.parent = bus->dev; 38 slave->dev.fwnode = fwnode; 39 40 if (id->unique_id == SDW_IGNORED_UNIQUE_ID) { 41 /* name shall be sdw:link:mfg:part:class */ 42 dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x", 43 bus->link_id, id->mfg_id, id->part_id, 44 id->class_id); 45 } else { 46 /* name shall be sdw:link:mfg:part:class:unique */ 47 dev_set_name(&slave->dev, "sdw:%x:%x:%x:%x:%x", 48 bus->link_id, id->mfg_id, id->part_id, 49 id->class_id, id->unique_id); 50 } 51 52 slave->dev.bus = &sdw_bus_type; 53 slave->dev.of_node = of_node_get(to_of_node(fwnode)); 54 slave->dev.type = &sdw_slave_type; 55 slave->dev.groups = sdw_slave_status_attr_groups; 56 slave->bus = bus; 57 slave->status = SDW_SLAVE_UNATTACHED; 58 init_completion(&slave->enumeration_complete); 59 init_completion(&slave->initialization_complete); 60 slave->dev_num = 0; 61 init_completion(&slave->probe_complete); 62 slave->probed = false; 63 slave->first_interrupt_done = false; 64 65 for (i = 0; i < SDW_MAX_PORTS; i++) 66 init_completion(&slave->port_ready[i]); 67 68 mutex_lock(&bus->bus_lock); 69 list_add_tail(&slave->node, &bus->slaves); 70 mutex_unlock(&bus->bus_lock); 71 72 ret = device_register(&slave->dev); 73 if (ret) { 74 dev_err(bus->dev, "Failed to add slave: ret %d\n", ret); 75 76 /* 77 * On err, don't free but drop ref as this will be freed 78 * when release method is invoked. 79 */ 80 mutex_lock(&bus->bus_lock); 81 list_del(&slave->node); 82 mutex_unlock(&bus->bus_lock); 83 put_device(&slave->dev); 84 85 return ret; 86 } 87 sdw_slave_debugfs_init(slave); 88 89 return ret; 90 } 91 92 #if IS_ENABLED(CONFIG_ACPI) 93 94 static bool find_slave(struct sdw_bus *bus, 95 struct acpi_device *adev, 96 struct sdw_slave_id *id) 97 { 98 u64 addr; 99 unsigned int link_id; 100 acpi_status status; 101 102 status = acpi_evaluate_integer(adev->handle, 103 METHOD_NAME__ADR, NULL, &addr); 104 105 if (ACPI_FAILURE(status)) { 106 dev_err(bus->dev, "_ADR resolution failed: %x\n", 107 status); 108 return false; 109 } 110 111 if (bus->ops->override_adr) 112 addr = bus->ops->override_adr(bus, addr); 113 114 if (!addr) 115 return false; 116 117 /* Extract link id from ADR, Bit 51 to 48 (included) */ 118 link_id = SDW_DISCO_LINK_ID(addr); 119 120 /* Check for link_id match */ 121 if (link_id != bus->link_id) 122 return false; 123 124 sdw_extract_slave_id(bus, addr, id); 125 126 return true; 127 } 128 129 /* 130 * sdw_acpi_find_slaves() - Find Slave devices in Master ACPI node 131 * @bus: SDW bus instance 132 * 133 * Scans Master ACPI node for SDW child Slave devices and registers it. 134 */ 135 int sdw_acpi_find_slaves(struct sdw_bus *bus) 136 { 137 struct acpi_device *adev, *parent; 138 struct acpi_device *adev2, *parent2; 139 140 parent = ACPI_COMPANION(bus->dev); 141 if (!parent) { 142 dev_err(bus->dev, "Can't find parent for acpi bind\n"); 143 return -ENODEV; 144 } 145 146 list_for_each_entry(adev, &parent->children, node) { 147 struct sdw_slave_id id; 148 struct sdw_slave_id id2; 149 bool ignore_unique_id = true; 150 151 if (!find_slave(bus, adev, &id)) 152 continue; 153 154 /* brute-force O(N^2) search for duplicates */ 155 parent2 = parent; 156 list_for_each_entry(adev2, &parent2->children, node) { 157 158 if (adev == adev2) 159 continue; 160 161 if (!find_slave(bus, adev2, &id2)) 162 continue; 163 164 if (id.sdw_version != id2.sdw_version || 165 id.mfg_id != id2.mfg_id || 166 id.part_id != id2.part_id || 167 id.class_id != id2.class_id) 168 continue; 169 170 if (id.unique_id != id2.unique_id) { 171 dev_dbg(bus->dev, 172 "Valid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n", 173 id.unique_id, id2.unique_id, id.mfg_id, id.part_id); 174 ignore_unique_id = false; 175 } else { 176 dev_err(bus->dev, 177 "Invalid unique IDs 0x%x 0x%x for Slave mfg_id 0x%04x, part_id 0x%04x\n", 178 id.unique_id, id2.unique_id, id.mfg_id, id.part_id); 179 return -ENODEV; 180 } 181 } 182 183 if (ignore_unique_id) 184 id.unique_id = SDW_IGNORED_UNIQUE_ID; 185 186 /* 187 * don't error check for sdw_slave_add as we want to continue 188 * adding Slaves 189 */ 190 sdw_slave_add(bus, &id, acpi_fwnode_handle(adev)); 191 } 192 193 return 0; 194 } 195 196 #endif 197 198 /* 199 * sdw_of_find_slaves() - Find Slave devices in master device tree node 200 * @bus: SDW bus instance 201 * 202 * Scans Master DT node for SDW child Slave devices and registers it. 203 */ 204 int sdw_of_find_slaves(struct sdw_bus *bus) 205 { 206 struct device *dev = bus->dev; 207 struct device_node *node; 208 209 for_each_child_of_node(bus->dev->of_node, node) { 210 int link_id, ret, len; 211 unsigned int sdw_version; 212 const char *compat = NULL; 213 struct sdw_slave_id id; 214 const __be32 *addr; 215 216 compat = of_get_property(node, "compatible", NULL); 217 if (!compat) 218 continue; 219 220 ret = sscanf(compat, "sdw%01x%04hx%04hx%02hhx", &sdw_version, 221 &id.mfg_id, &id.part_id, &id.class_id); 222 223 if (ret != 4) { 224 dev_err(dev, "Invalid compatible string found %s\n", 225 compat); 226 continue; 227 } 228 229 addr = of_get_property(node, "reg", &len); 230 if (!addr || (len < 2 * sizeof(u32))) { 231 dev_err(dev, "Invalid Link and Instance ID\n"); 232 continue; 233 } 234 235 link_id = be32_to_cpup(addr++); 236 id.unique_id = be32_to_cpup(addr); 237 id.sdw_version = sdw_version; 238 239 /* Check for link_id match */ 240 if (link_id != bus->link_id) 241 continue; 242 243 sdw_slave_add(bus, &id, of_fwnode_handle(node)); 244 } 245 246 return 0; 247 } 248