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