Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) |
| 2 | // Copyright(c) 2015-17 Intel Corporation. |
| 3 | |
| 4 | #include <linux/acpi.h> |
Srinivas Kandagatla | a2e4845 | 2019-08-29 17:35:12 +0100 | [diff] [blame] | 5 | #include <linux/of.h> |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 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 | |
Pierre-Louis Bossart | 90acca1 | 2020-05-19 01:43:19 +0800 | [diff] [blame] | 17 | struct device_type sdw_slave_type = { |
| 18 | .name = "sdw_slave", |
| 19 | .release = sdw_slave_release, |
| 20 | .uevent = sdw_slave_uevent, |
| 21 | }; |
| 22 | |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 23 | static int sdw_slave_add(struct sdw_bus *bus, |
Pierre-Louis Bossart | c0cbfb0 | 2019-05-01 10:57:36 -0500 | [diff] [blame] | 24 | struct sdw_slave_id *id, struct fwnode_handle *fwnode) |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 25 | { |
| 26 | struct sdw_slave *slave; |
| 27 | int ret; |
Pierre-Louis Bossart | 6073755 | 2020-08-31 21:43:18 +0800 | [diff] [blame^] | 28 | int i; |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 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 | |
Pierre-Louis Bossart | 2e8c4ad | 2019-10-22 18:48:08 -0500 | [diff] [blame] | 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 | } |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 50 | |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 51 | slave->dev.bus = &sdw_bus_type; |
Srinivas Kandagatla | a2e4845 | 2019-08-29 17:35:12 +0100 | [diff] [blame] | 52 | slave->dev.of_node = of_node_get(to_of_node(fwnode)); |
Pierre-Louis Bossart | 90acca1 | 2020-05-19 01:43:19 +0800 | [diff] [blame] | 53 | slave->dev.type = &sdw_slave_type; |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 54 | slave->bus = bus; |
| 55 | slave->status = SDW_SLAVE_UNATTACHED; |
Pierre-Louis Bossart | fb9469e | 2020-01-14 18:08:36 -0600 | [diff] [blame] | 56 | init_completion(&slave->enumeration_complete); |
Pierre-Louis Bossart | a90def0 | 2020-01-14 18:08:37 -0600 | [diff] [blame] | 57 | init_completion(&slave->initialization_complete); |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 58 | slave->dev_num = 0; |
Pierre-Louis Bossart | 2140b66 | 2020-01-14 18:08:35 -0600 | [diff] [blame] | 59 | init_completion(&slave->probe_complete); |
| 60 | slave->probed = false; |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 61 | |
Pierre-Louis Bossart | 6073755 | 2020-08-31 21:43:18 +0800 | [diff] [blame^] | 62 | for (i = 0; i < SDW_MAX_PORTS; i++) |
| 63 | init_completion(&slave->port_ready[i]); |
| 64 | |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 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); |
Pierre-Louis Bossart | 8893ab5 | 2020-04-20 02:51:15 +0800 | [diff] [blame] | 81 | |
| 82 | return ret; |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 83 | } |
Pierre-Louis Bossart | bf03473 | 2019-08-21 13:58:18 -0500 | [diff] [blame] | 84 | sdw_slave_debugfs_init(slave); |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | #if IS_ENABLED(CONFIG_ACPI) |
Pierre-Louis Bossart | de5b174 | 2019-10-22 18:48:07 -0500 | [diff] [blame] | 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 = (addr >> 48) & GENMASK(3, 0); |
| 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 | |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 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; |
Pierre-Louis Bossart | 2e8c4ad | 2019-10-22 18:48:08 -0500 | [diff] [blame] | 129 | struct acpi_device *adev2, *parent2; |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 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) { |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 138 | struct sdw_slave_id id; |
Pierre-Louis Bossart | 2e8c4ad | 2019-10-22 18:48:08 -0500 | [diff] [blame] | 139 | struct sdw_slave_id id2; |
| 140 | bool ignore_unique_id = true; |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 141 | |
Pierre-Louis Bossart | de5b174 | 2019-10-22 18:48:07 -0500 | [diff] [blame] | 142 | if (!find_slave(bus, adev, &id)) |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 143 | continue; |
| 144 | |
Pierre-Louis Bossart | 2e8c4ad | 2019-10-22 18:48:08 -0500 | [diff] [blame] | 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 | |
Vinod Koul | 7c3cd18 | 2017-12-14 11:19:34 +0530 | [diff] [blame] | 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 |
Srinivas Kandagatla | a2e4845 | 2019-08-29 17:35:12 +0100 | [diff] [blame] | 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) { |
Pierre-Louis Bossart | 7b47ad3 | 2019-10-22 18:31:47 -0500 | [diff] [blame] | 203 | int link_id, ret, len; |
| 204 | unsigned int sdw_version; |
Srinivas Kandagatla | a2e4845 | 2019-08-29 17:35:12 +0100 | [diff] [blame] | 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 | } |