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