1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) 2 // Copyright(c) 2015-17 Intel Corporation. 3 4 /* 5 * SDW Intel Init Routines 6 * 7 * Initializes and creates SDW devices based on ACPI and Hardware values 8 */ 9 10 #include <linux/acpi.h> 11 #include <linux/export.h> 12 #include <linux/io.h> 13 #include <linux/module.h> 14 #include <linux/platform_device.h> 15 #include <linux/soundwire/sdw_intel.h> 16 #include "cadence_master.h" 17 #include "intel.h" 18 19 #define SDW_LINK_TYPE 4 /* from Intel ACPI documentation */ 20 #define SDW_MAX_LINKS 4 21 #define SDW_SHIM_LCAP 0x0 22 #define SDW_SHIM_BASE 0x2C000 23 #define SDW_ALH_BASE 0x2C800 24 #define SDW_LINK_BASE 0x30000 25 #define SDW_LINK_SIZE 0x10000 26 27 static int ctrl_link_mask; 28 module_param_named(sdw_link_mask, ctrl_link_mask, int, 0444); 29 MODULE_PARM_DESC(sdw_link_mask, "Intel link mask (one bit per link)"); 30 31 static bool is_link_enabled(struct fwnode_handle *fw_node, int i) 32 { 33 struct fwnode_handle *link; 34 char name[32]; 35 u32 quirk_mask = 0; 36 37 /* Find master handle */ 38 snprintf(name, sizeof(name), 39 "mipi-sdw-link-%d-subproperties", i); 40 41 link = fwnode_get_named_child_node(fw_node, name); 42 if (!link) 43 return false; 44 45 fwnode_property_read_u32(link, 46 "intel-quirk-mask", 47 &quirk_mask); 48 49 if (quirk_mask & SDW_INTEL_QUIRK_MASK_BUS_DISABLE) 50 return false; 51 52 return true; 53 } 54 55 static int sdw_intel_cleanup(struct sdw_intel_ctx *ctx) 56 { 57 struct sdw_intel_link_res *link = ctx->links; 58 u32 link_mask; 59 int i; 60 61 if (!link) 62 return 0; 63 64 link_mask = ctx->link_mask; 65 66 for (i = 0; i < ctx->count; i++, link++) { 67 if (!(link_mask & BIT(i))) 68 continue; 69 70 if (link->pdev) 71 platform_device_unregister(link->pdev); 72 } 73 74 return 0; 75 } 76 77 static int 78 sdw_intel_scan_controller(struct sdw_intel_acpi_info *info) 79 { 80 struct acpi_device *adev; 81 int ret, i; 82 u8 count; 83 84 if (acpi_bus_get_device(info->handle, &adev)) 85 return -EINVAL; 86 87 /* Found controller, find links supported */ 88 count = 0; 89 ret = fwnode_property_read_u8_array(acpi_fwnode_handle(adev), 90 "mipi-sdw-master-count", &count, 1); 91 92 /* 93 * In theory we could check the number of links supported in 94 * hardware, but in that step we cannot assume SoundWire IP is 95 * powered. 96 * 97 * In addition, if the BIOS doesn't even provide this 98 * 'master-count' property then all the inits based on link 99 * masks will fail as well. 100 * 101 * We will check the hardware capabilities in the startup() step 102 */ 103 104 if (ret) { 105 dev_err(&adev->dev, 106 "Failed to read mipi-sdw-master-count: %d\n", ret); 107 return -EINVAL; 108 } 109 110 /* Check count is within bounds */ 111 if (count > SDW_MAX_LINKS) { 112 dev_err(&adev->dev, "Link count %d exceeds max %d\n", 113 count, SDW_MAX_LINKS); 114 return -EINVAL; 115 } 116 117 if (!count) { 118 dev_warn(&adev->dev, "No SoundWire links detected\n"); 119 return -EINVAL; 120 } 121 dev_dbg(&adev->dev, "ACPI reports %d SDW Link devices\n", count); 122 123 info->count = count; 124 info->link_mask = 0; 125 126 for (i = 0; i < count; i++) { 127 if (ctrl_link_mask && !(ctrl_link_mask & BIT(i))) { 128 dev_dbg(&adev->dev, 129 "Link %d masked, will not be enabled\n", i); 130 continue; 131 } 132 133 if (!is_link_enabled(acpi_fwnode_handle(adev), i)) { 134 dev_dbg(&adev->dev, 135 "Link %d not selected in firmware\n", i); 136 continue; 137 } 138 139 info->link_mask |= BIT(i); 140 } 141 142 return 0; 143 } 144 145 static struct sdw_intel_ctx 146 *sdw_intel_probe_controller(struct sdw_intel_res *res) 147 { 148 struct platform_device_info pdevinfo; 149 struct platform_device *pdev; 150 struct sdw_intel_link_res *link; 151 struct sdw_intel_ctx *ctx; 152 struct acpi_device *adev; 153 u32 link_mask; 154 int count; 155 int i; 156 157 if (!res) 158 return NULL; 159 160 if (acpi_bus_get_device(res->handle, &adev)) 161 return NULL; 162 163 if (!res->count) 164 return NULL; 165 166 count = res->count; 167 dev_dbg(&adev->dev, "Creating %d SDW Link devices\n", count); 168 169 ctx = devm_kzalloc(&adev->dev, sizeof(*ctx), GFP_KERNEL); 170 if (!ctx) 171 return NULL; 172 173 ctx->count = count; 174 ctx->links = devm_kcalloc(&adev->dev, ctx->count, 175 sizeof(*ctx->links), GFP_KERNEL); 176 if (!ctx->links) 177 return NULL; 178 179 ctx->count = count; 180 ctx->mmio_base = res->mmio_base; 181 ctx->link_mask = res->link_mask; 182 ctx->handle = res->handle; 183 184 link = ctx->links; 185 link_mask = ctx->link_mask; 186 187 /* Create SDW Master devices */ 188 for (i = 0; i < count; i++, link++) { 189 if (!(link_mask & BIT(i))) { 190 dev_dbg(&adev->dev, 191 "Link %d masked, will not be enabled\n", i); 192 continue; 193 } 194 195 link->mmio_base = res->mmio_base; 196 link->registers = res->mmio_base + SDW_LINK_BASE 197 + (SDW_LINK_SIZE * i); 198 link->shim = res->mmio_base + SDW_SHIM_BASE; 199 link->alh = res->mmio_base + SDW_ALH_BASE; 200 201 link->ops = res->ops; 202 link->dev = res->dev; 203 204 memset(&pdevinfo, 0, sizeof(pdevinfo)); 205 206 pdevinfo.parent = res->parent; 207 pdevinfo.name = "intel-sdw"; 208 pdevinfo.id = i; 209 pdevinfo.fwnode = acpi_fwnode_handle(adev); 210 pdevinfo.data = link; 211 pdevinfo.size_data = sizeof(*link); 212 213 pdev = platform_device_register_full(&pdevinfo); 214 if (IS_ERR(pdev)) { 215 dev_err(&adev->dev, 216 "platform device creation failed: %ld\n", 217 PTR_ERR(pdev)); 218 goto err; 219 } 220 link->pdev = pdev; 221 } 222 223 return ctx; 224 225 err: 226 ctx->count = i; 227 sdw_intel_cleanup(ctx); 228 return NULL; 229 } 230 231 static int 232 sdw_intel_startup_controller(struct sdw_intel_ctx *ctx) 233 { 234 struct acpi_device *adev; 235 struct sdw_intel_link_res *link; 236 u32 caps; 237 u32 link_mask; 238 int i; 239 240 if (acpi_bus_get_device(ctx->handle, &adev)) 241 return -EINVAL; 242 243 /* Check SNDWLCAP.LCOUNT */ 244 caps = ioread32(ctx->mmio_base + SDW_SHIM_BASE + SDW_SHIM_LCAP); 245 caps &= GENMASK(2, 0); 246 247 /* Check HW supported vs property value */ 248 if (caps < ctx->count) { 249 dev_err(&adev->dev, 250 "BIOS master count is larger than hardware capabilities\n"); 251 return -EINVAL; 252 } 253 254 if (!ctx->links) 255 return -EINVAL; 256 257 link = ctx->links; 258 link_mask = ctx->link_mask; 259 260 /* Startup SDW Master devices */ 261 for (i = 0; i < ctx->count; i++, link++) { 262 if (!(link_mask & BIT(i))) 263 continue; 264 265 intel_master_startup(link->pdev); 266 } 267 268 return 0; 269 } 270 271 static acpi_status sdw_intel_acpi_cb(acpi_handle handle, u32 level, 272 void *cdata, void **return_value) 273 { 274 struct sdw_intel_acpi_info *info = cdata; 275 struct acpi_device *adev; 276 acpi_status status; 277 u64 adr; 278 279 status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &adr); 280 if (ACPI_FAILURE(status)) 281 return AE_OK; /* keep going */ 282 283 if (acpi_bus_get_device(handle, &adev)) { 284 pr_err("%s: Couldn't find ACPI handle\n", __func__); 285 return AE_NOT_FOUND; 286 } 287 288 info->handle = handle; 289 290 /* 291 * On some Intel platforms, multiple children of the HDAS 292 * device can be found, but only one of them is the SoundWire 293 * controller. The SNDW device is always exposed with 294 * Name(_ADR, 0x40000000), with bits 31..28 representing the 295 * SoundWire link so filter accordingly 296 */ 297 if ((adr & GENMASK(31, 28)) >> 28 != SDW_LINK_TYPE) 298 return AE_OK; /* keep going */ 299 300 /* device found, stop namespace walk */ 301 return AE_CTRL_TERMINATE; 302 } 303 304 /** 305 * sdw_intel_acpi_scan() - SoundWire Intel init routine 306 * @parent_handle: ACPI parent handle 307 * @info: description of what firmware/DSDT tables expose 308 * 309 * This scans the namespace and queries firmware to figure out which 310 * links to enable. A follow-up use of sdw_intel_probe() and 311 * sdw_intel_startup() is required for creation of devices and bus 312 * startup 313 */ 314 int sdw_intel_acpi_scan(acpi_handle *parent_handle, 315 struct sdw_intel_acpi_info *info) 316 { 317 acpi_status status; 318 319 status = acpi_walk_namespace(ACPI_TYPE_DEVICE, 320 parent_handle, 1, 321 sdw_intel_acpi_cb, 322 NULL, info, NULL); 323 if (ACPI_FAILURE(status)) 324 return -ENODEV; 325 326 return sdw_intel_scan_controller(info); 327 } 328 EXPORT_SYMBOL(sdw_intel_acpi_scan); 329 330 /** 331 * sdw_intel_probe() - SoundWire Intel probe routine 332 * @res: resource data 333 * 334 * This registers a platform device for each Master handled by the controller, 335 * and SoundWire Master and Slave devices will be created by the platform 336 * device probe. All the information necessary is stored in the context, and 337 * the res argument pointer can be freed after this step. 338 * This function will be called after sdw_intel_acpi_scan() by SOF probe. 339 */ 340 struct sdw_intel_ctx 341 *sdw_intel_probe(struct sdw_intel_res *res) 342 { 343 return sdw_intel_probe_controller(res); 344 } 345 EXPORT_SYMBOL(sdw_intel_probe); 346 347 /** 348 * sdw_intel_startup() - SoundWire Intel startup 349 * @ctx: SoundWire context allocated in the probe 350 * 351 * Startup Intel SoundWire controller. This function will be called after 352 * Intel Audio DSP is powered up. 353 */ 354 int sdw_intel_startup(struct sdw_intel_ctx *ctx) 355 { 356 return sdw_intel_startup_controller(ctx); 357 } 358 EXPORT_SYMBOL(sdw_intel_startup); 359 /** 360 * sdw_intel_exit() - SoundWire Intel exit 361 * @ctx: SoundWire context allocated in the probe 362 * 363 * Delete the controller instances created and cleanup 364 */ 365 void sdw_intel_exit(struct sdw_intel_ctx *ctx) 366 { 367 sdw_intel_cleanup(ctx); 368 } 369 EXPORT_SYMBOL(sdw_intel_exit); 370 371 MODULE_LICENSE("Dual BSD/GPL"); 372 MODULE_DESCRIPTION("Intel Soundwire Init Library"); 373