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