Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ASoC: SDCA: Add FDL library for XU entities

Some instances of the XU Entity have a need for Files to be downloaded
from the Host. In these XUs, there is one instance of a Host to Device
(Consumer) UMP, identified by the FDL_CurrentOwner Control. FDL Library
introduced here implements the FDL flow triggered by FDL_CurrentOwner
irq, which sends a file from SoundWire File Table (SWFT) or from the
firmware directory in specific cases, to the Device FDL UMP.

Currently only Direct method of FDL is implemented.

Reviewed-by: Bard Liao <yung-chuan.liao@linux.intel.com>
Signed-off-by: Maciej Strozek <mstrozek@opensource.cirrus.com>
Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com>
Reviewed-by: Pierre-Louis Bossart <pierre-louis.bossart@linux.dev>
Link: https://patch.msgid.link/20251020155512.353774-15-ckeepax@opensource.cirrus.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Maciej Strozek and committed by
Mark Brown
71f7990a c4d096c3

+467
+58
include/sound/sdca_fdl.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * The MIPI SDCA specification is available for public downloads at 4 + * https://www.mipi.org/mipi-sdca-v1-0-download 5 + * 6 + * Copyright (C) 2025 Cirrus Logic, Inc. and 7 + * Cirrus Logic International Semiconductor Ltd. 8 + */ 9 + 10 + #ifndef __SDCA_FDL_H__ 11 + #define __SDCA_FDL_H__ 12 + 13 + struct device; 14 + struct regmap; 15 + struct sdca_fdl_set; 16 + struct sdca_function_data; 17 + struct sdca_interrupt; 18 + 19 + /** 20 + * struct fdl_state - FDL state structure to keep data between interrupts 21 + * @set: Pointer to the FDL set currently being downloaded. 22 + * @file_index: Index of the current file being processed. 23 + */ 24 + struct fdl_state { 25 + struct sdca_fdl_set *set; 26 + int file_index; 27 + }; 28 + 29 + #define SDCA_CTL_XU_FDLH_COMPLETE 0 30 + #define SDCA_CTL_XU_FDLH_MORE_FILES SDCA_CTL_XU_FDLH_SET_IN_PROGRESS 31 + #define SDCA_CTL_XU_FDLH_FILE_AVAILABLE (SDCA_CTL_XU_FDLH_TRANSFERRED_FILE | \ 32 + SDCA_CTL_XU_FDLH_SET_IN_PROGRESS) 33 + #define SDCA_CTL_XU_FDLH_MASK (SDCA_CTL_XU_FDLH_TRANSFERRED_CHUNK | \ 34 + SDCA_CTL_XU_FDLH_TRANSFERRED_FILE | \ 35 + SDCA_CTL_XU_FDLH_SET_IN_PROGRESS | \ 36 + SDCA_CTL_XU_FDLH_RESET_ACK | \ 37 + SDCA_CTL_XU_FDLH_REQ_ABORT) 38 + 39 + #define SDCA_CTL_XU_FDLD_COMPLETE 0 40 + #define SDCA_CTL_XU_FDLD_FILE_OK (SDCA_CTL_XU_FDLH_TRANSFERRED_FILE | \ 41 + SDCA_CTL_XU_FDLH_SET_IN_PROGRESS | \ 42 + SDCA_CTL_XU_FDLD_ACK_TRANSFER | \ 43 + SDCA_CTL_XU_FDLD_NEEDS_SET) 44 + #define SDCA_CTL_XU_FDLD_MORE_FILES_OK (SDCA_CTL_XU_FDLH_SET_IN_PROGRESS | \ 45 + SDCA_CTL_XU_FDLD_ACK_TRANSFER | \ 46 + SDCA_CTL_XU_FDLD_NEEDS_SET) 47 + #define SDCA_CTL_XU_FDLD_MASK (SDCA_CTL_XU_FDLD_REQ_RESET | \ 48 + SDCA_CTL_XU_FDLD_REQ_ABORT | \ 49 + SDCA_CTL_XU_FDLD_ACK_TRANSFER | \ 50 + SDCA_CTL_XU_FDLD_NEEDS_SET) 51 + 52 + int sdca_fdl_alloc_state(struct sdca_interrupt *interrupt); 53 + int sdca_fdl_process(struct sdca_interrupt *interrupt); 54 + 55 + int sdca_reset_function(struct device *dev, struct sdca_function_data *function, 56 + struct regmap *regmap); 57 + 58 + #endif // __SDCA_FDL_H__
+24
include/sound/sdca_function.h
··· 285 285 SDCA_CTL_XU_FDL_STATUS = 0x14, 286 286 SDCA_CTL_XU_FDL_SET_INDEX = 0x15, 287 287 SDCA_CTL_XU_FDL_HOST_REQUEST = 0x16, 288 + 289 + /* FDL Status Host->Device bit definitions */ 290 + SDCA_CTL_XU_FDLH_TRANSFERRED_CHUNK = BIT(0), 291 + SDCA_CTL_XU_FDLH_TRANSFERRED_FILE = BIT(1), 292 + SDCA_CTL_XU_FDLH_SET_IN_PROGRESS = BIT(2), 293 + SDCA_CTL_XU_FDLH_RESET_ACK = BIT(4), 294 + SDCA_CTL_XU_FDLH_REQ_ABORT = BIT(5), 295 + /* FDL Status Device->Host bit definitions */ 296 + SDCA_CTL_XU_FDLD_REQ_RESET = BIT(4), 297 + SDCA_CTL_XU_FDLD_REQ_ABORT = BIT(5), 298 + SDCA_CTL_XU_FDLD_ACK_TRANSFER = BIT(6), 299 + SDCA_CTL_XU_FDLD_NEEDS_SET = BIT(7), 300 + }; 301 + 302 + /** 303 + * enum sdca_set_index_range - Column definitions UMP SetIndex 304 + */ 305 + enum sdca_fdl_set_index_range { 306 + SDCA_FDL_SET_INDEX_SET_NUMBER = 0, 307 + SDCA_FDL_SET_INDEX_FILE_SET_ID = 1, 308 + SDCA_FDL_SET_INDEX_NCOLS = 2, 288 309 }; 289 310 290 311 /** ··· 590 569 SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION = BIT(5), 591 570 SDCA_CTL_ENTITY_0_FUNCTION_HAS_BEEN_RESET = BIT(6), 592 571 SDCA_CTL_ENTITY_0_FUNCTION_BUSY = BIT(7), 572 + 573 + /* Function Action Bits */ 574 + SDCA_CTL_ENTITY_0_RESET_FUNCTION_NOW = BIT(0), 593 575 }; 594 576 595 577 #define SDCA_CTL_MIC_BIAS_NAME "Mic Bias"
+8
sound/soc/sdca/Kconfig
··· 25 25 help 26 26 This option enables support for SDCA IRQs. 27 27 28 + config SND_SOC_SDCA_FDL 29 + bool "SDCA FDL (File DownLoad) support" 30 + depends on SND_SOC_SDCA 31 + default y 32 + help 33 + This option enables support for the File Download using UMP, 34 + typically used for downloading firmware to devices. 35 + 28 36 config SND_SOC_SDCA_OPTIONAL 29 37 def_tristate SND_SOC_SDCA || !SND_SOC_SDCA 30 38
+1
sound/soc/sdca/Makefile
··· 4 4 sdca_ump.o 5 5 snd-soc-sdca-$(CONFIG_SND_SOC_SDCA_HID) += sdca_hid.o 6 6 snd-soc-sdca-$(CONFIG_SND_SOC_SDCA_IRQ) += sdca_interrupts.o 7 + snd-soc-sdca-$(CONFIG_SND_SOC_SDCA_FDL) += sdca_fdl.o 7 8 8 9 obj-$(CONFIG_SND_SOC_SDCA) += snd-soc-sdca.o
+376
sound/soc/sdca/sdca_fdl.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + // Copyright (C) 2025 Cirrus Logic, Inc. and 3 + // Cirrus Logic International Semiconductor Ltd. 4 + 5 + /* 6 + * The MIPI SDCA specification is available for public downloads at 7 + * https://www.mipi.org/mipi-sdca-v1-0-download 8 + */ 9 + 10 + #include <linux/acpi.h> 11 + #include <linux/device.h> 12 + #include <linux/dev_printk.h> 13 + #include <linux/dmi.h> 14 + #include <linux/firmware.h> 15 + #include <linux/module.h> 16 + #include <linux/pci.h> 17 + #include <linux/regmap.h> 18 + #include <linux/sprintf.h> 19 + #include <linux/soundwire/sdw.h> 20 + #include <linux/soundwire/sdw_registers.h> 21 + #include <sound/sdca.h> 22 + #include <sound/sdca_fdl.h> 23 + #include <sound/sdca_function.h> 24 + #include <sound/sdca_interrupts.h> 25 + #include <sound/sdca_ump.h> 26 + 27 + /** 28 + * sdca_reset_function - send an SDCA function reset 29 + * @dev: Device pointer for error messages. 30 + * @function: Pointer to the SDCA Function. 31 + * @regmap: Pointer to the SDCA Function regmap. 32 + * 33 + * Return: Zero on success or a negative error code. 34 + */ 35 + int sdca_reset_function(struct device *dev, struct sdca_function_data *function, 36 + struct regmap *regmap) 37 + { 38 + unsigned int reg = SDW_SDCA_CTL(function->desc->adr, 39 + SDCA_ENTITY_TYPE_ENTITY_0, 40 + SDCA_CTL_ENTITY_0_FUNCTION_ACTION, 0); 41 + unsigned int val, poll_us; 42 + int ret; 43 + 44 + ret = regmap_write(regmap, reg, SDCA_CTL_ENTITY_0_RESET_FUNCTION_NOW); 45 + if (ret) // Allowed for function reset to not be implemented 46 + return 0; 47 + 48 + if (!function->reset_max_delay) { 49 + dev_err(dev, "No reset delay specified in DisCo\n"); 50 + return -EINVAL; 51 + } 52 + 53 + poll_us = umin(function->reset_max_delay >> 4, 1000); 54 + 55 + ret = regmap_read_poll_timeout(regmap, reg, val, !val, poll_us, 56 + function->reset_max_delay); 57 + if (ret) { 58 + dev_err(dev, "Failed waiting for function reset: %d\n", ret); 59 + return ret; 60 + } 61 + 62 + return 0; 63 + } 64 + EXPORT_SYMBOL_NS(sdca_reset_function, "SND_SOC_SDCA"); 65 + 66 + static char *fdl_get_sku_filename(struct device *dev, 67 + struct sdca_fdl_file *fdl_file) 68 + { 69 + struct device *parent = dev; 70 + const char *product_vendor; 71 + const char *product_sku; 72 + 73 + /* 74 + * Try to find pci_dev manually because the card may not be ready to be 75 + * used for snd_soc_card_get_pci_ssid yet 76 + */ 77 + while (parent) { 78 + if (dev_is_pci(parent)) { 79 + struct pci_dev *pci_dev = to_pci_dev(parent); 80 + 81 + return kasprintf(GFP_KERNEL, "sdca/%x/%x/%x/%x.bin", 82 + fdl_file->vendor_id, 83 + pci_dev->subsystem_vendor, 84 + pci_dev->subsystem_device, 85 + fdl_file->file_id); 86 + } else { 87 + parent = parent->parent; 88 + } 89 + } 90 + 91 + product_vendor = dmi_get_system_info(DMI_SYS_VENDOR); 92 + if (!product_vendor || !strcmp(product_vendor, "Default string")) 93 + product_vendor = dmi_get_system_info(DMI_BOARD_VENDOR); 94 + if (!product_vendor || !strcmp(product_vendor, "Default string")) 95 + product_vendor = dmi_get_system_info(DMI_CHASSIS_VENDOR); 96 + if (!product_vendor) 97 + product_vendor = "unknown"; 98 + 99 + product_sku = dmi_get_system_info(DMI_PRODUCT_SKU); 100 + if (!product_sku || !strcmp(product_sku, "Default string")) 101 + product_sku = dmi_get_system_info(DMI_PRODUCT_NAME); 102 + if (!product_sku) 103 + product_sku = "unknown"; 104 + 105 + return kasprintf(GFP_KERNEL, "sdca/%x/%s/%s/%x.bin", fdl_file->vendor_id, 106 + product_vendor, product_sku, fdl_file->file_id); 107 + } 108 + 109 + static int fdl_load_file(struct sdca_interrupt *interrupt, 110 + struct sdca_fdl_set *set, int file_index) 111 + { 112 + struct device *dev = interrupt->dev; 113 + struct sdca_fdl_data *fdl_data = &interrupt->function->fdl_data; 114 + const struct firmware *firmware = NULL; 115 + struct acpi_sw_file *swf = NULL, *tmp; 116 + struct sdca_fdl_file *fdl_file; 117 + char *disk_filename; 118 + int ret; 119 + int i; 120 + 121 + if (!set) { 122 + dev_err(dev, "request to load SWF with no set\n"); 123 + return -EINVAL; 124 + } 125 + 126 + fdl_file = &set->files[file_index]; 127 + 128 + if (fdl_data->swft) { 129 + tmp = fdl_data->swft->files; 130 + for (i = 0; i < fdl_data->swft->header.length; i += tmp->file_length, 131 + tmp = ACPI_ADD_PTR(struct acpi_sw_file, tmp, tmp->file_length)) { 132 + if (tmp->vendor_id == fdl_file->vendor_id && 133 + tmp->file_id == fdl_file->file_id) { 134 + dev_dbg(dev, "located SWF in ACPI: %x-%x-%x\n", 135 + tmp->vendor_id, tmp->file_id, 136 + tmp->file_version); 137 + swf = tmp; 138 + break; 139 + } 140 + } 141 + } 142 + 143 + disk_filename = fdl_get_sku_filename(dev, fdl_file); 144 + if (!disk_filename) 145 + return -ENOMEM; 146 + 147 + dev_dbg(dev, "FDL disk filename: %s\n", disk_filename); 148 + 149 + ret = firmware_request_nowarn(&firmware, disk_filename, dev); 150 + kfree(disk_filename); 151 + if (ret) { 152 + disk_filename = kasprintf(GFP_KERNEL, "sdca/%x/%x.bin", 153 + fdl_file->vendor_id, fdl_file->file_id); 154 + if (!disk_filename) 155 + return -ENOMEM; 156 + 157 + dev_dbg(dev, "FDL disk filename: %s\n", disk_filename); 158 + 159 + ret = firmware_request_nowarn(&firmware, disk_filename, dev); 160 + kfree(disk_filename); 161 + } 162 + 163 + if (!ret) { 164 + tmp = (struct acpi_sw_file *)&firmware->data[0]; 165 + 166 + if (firmware->size < sizeof(*tmp) || 167 + tmp->file_length != firmware->size) { 168 + dev_err(dev, "bad disk SWF size\n"); 169 + } else if (!swf || swf->file_version <= tmp->file_version) { 170 + dev_dbg(dev, "using SWF from disk: %x-%x-%x\n", 171 + tmp->vendor_id, tmp->file_id, tmp->file_version); 172 + swf = tmp; 173 + } 174 + } 175 + 176 + if (!swf) { 177 + dev_err(dev, "failed to locate SWF\n"); 178 + return -ENOENT; 179 + } 180 + 181 + ret = sdca_ump_write_message(dev, interrupt->device_regmap, 182 + interrupt->function_regmap, 183 + interrupt->function, interrupt->entity, 184 + SDCA_CTL_XU_FDL_MESSAGEOFFSET, fdl_file->fdl_offset, 185 + SDCA_CTL_XU_FDL_MESSAGELENGTH, swf->data, 186 + swf->file_length - offsetof(struct acpi_sw_file, data)); 187 + release_firmware(firmware); 188 + return ret; 189 + } 190 + 191 + static struct sdca_fdl_set *fdl_get_set(struct sdca_interrupt *interrupt) 192 + { 193 + struct device *dev = interrupt->dev; 194 + struct sdca_fdl_data *fdl_data = &interrupt->function->fdl_data; 195 + struct sdca_entity *xu = interrupt->entity; 196 + struct sdca_control_range *range; 197 + unsigned int val; 198 + int i, ret; 199 + 200 + ret = regmap_read(interrupt->function_regmap, 201 + SDW_SDCA_CTL(interrupt->function->desc->adr, xu->id, 202 + SDCA_CTL_XU_FDL_SET_INDEX, 0), 203 + &val); 204 + if (ret < 0) { 205 + dev_err(dev, "failed to read FDL set index: %d\n", ret); 206 + return NULL; 207 + } 208 + 209 + range = sdca_selector_find_range(dev, xu, SDCA_CTL_XU_FDL_SET_INDEX, 210 + SDCA_FDL_SET_INDEX_NCOLS, 0); 211 + 212 + val = sdca_range_search(range, SDCA_FDL_SET_INDEX_SET_NUMBER, 213 + val, SDCA_FDL_SET_INDEX_FILE_SET_ID); 214 + 215 + for (i = 0; i < fdl_data->num_sets; i++) { 216 + if (fdl_data->sets[i].id == val) 217 + return &fdl_data->sets[i]; 218 + } 219 + 220 + dev_err(dev, "invalid fileset id: %d\n", val); 221 + return NULL; 222 + } 223 + 224 + static void fdl_end(struct sdca_interrupt *interrupt) 225 + { 226 + struct fdl_state *fdl_state = interrupt->priv; 227 + 228 + if (!fdl_state->set) 229 + return; 230 + 231 + fdl_state->set = NULL; 232 + 233 + dev_dbg(interrupt->dev, "completed FDL process\n"); 234 + } 235 + 236 + static int fdl_status_process(struct sdca_interrupt *interrupt, unsigned int status) 237 + { 238 + struct fdl_state *fdl_state = interrupt->priv; 239 + int ret; 240 + 241 + switch (status) { 242 + case SDCA_CTL_XU_FDLD_NEEDS_SET: 243 + dev_dbg(interrupt->dev, "starting FDL process...\n"); 244 + 245 + fdl_state->file_index = 0; 246 + fdl_state->set = fdl_get_set(interrupt); 247 + fallthrough; 248 + case SDCA_CTL_XU_FDLD_MORE_FILES_OK: 249 + ret = fdl_load_file(interrupt, fdl_state->set, fdl_state->file_index); 250 + if (ret) { 251 + fdl_end(interrupt); 252 + return SDCA_CTL_XU_FDLH_REQ_ABORT; 253 + } 254 + 255 + return SDCA_CTL_XU_FDLH_FILE_AVAILABLE; 256 + case SDCA_CTL_XU_FDLD_FILE_OK: 257 + if (!fdl_state->set) { 258 + fdl_end(interrupt); 259 + return SDCA_CTL_XU_FDLH_REQ_ABORT; 260 + } 261 + 262 + fdl_state->file_index++; 263 + 264 + if (fdl_state->file_index < fdl_state->set->num_files) 265 + return SDCA_CTL_XU_FDLH_MORE_FILES; 266 + fallthrough; 267 + case SDCA_CTL_XU_FDLD_COMPLETE: 268 + fdl_end(interrupt); 269 + return SDCA_CTL_XU_FDLH_COMPLETE; 270 + default: 271 + fdl_end(interrupt); 272 + 273 + if (status & SDCA_CTL_XU_FDLD_REQ_RESET) 274 + return SDCA_CTL_XU_FDLH_RESET_ACK; 275 + else if (status & SDCA_CTL_XU_FDLD_REQ_ABORT) 276 + return SDCA_CTL_XU_FDLH_COMPLETE; 277 + 278 + dev_err(interrupt->dev, "invalid FDL status: %x\n", status); 279 + return -EINVAL; 280 + } 281 + } 282 + 283 + /** 284 + * sdca_fdl_process - Process the FDL state machine 285 + * @interrupt: SDCA interrupt structure 286 + * 287 + * Based on section 13.2.5 Flow Diagram for File Download, Host side. 288 + * 289 + * Return: Zero on success or a negative error code. 290 + */ 291 + int sdca_fdl_process(struct sdca_interrupt *interrupt) 292 + { 293 + struct device *dev = interrupt->dev; 294 + struct sdca_entity_xu *xu = &interrupt->entity->xu; 295 + unsigned int reg, status; 296 + int response, ret; 297 + 298 + ret = sdca_ump_get_owner_host(dev, interrupt->function_regmap, 299 + interrupt->function, interrupt->entity, 300 + interrupt->control); 301 + if (ret) 302 + goto reset_function; 303 + 304 + reg = SDW_SDCA_CTL(interrupt->function->desc->adr, interrupt->entity->id, 305 + SDCA_CTL_XU_FDL_STATUS, 0); 306 + ret = regmap_read(interrupt->function_regmap, reg, &status); 307 + if (ret < 0) { 308 + dev_err(dev, "failed to read FDL status: %d\n", ret); 309 + return ret; 310 + } 311 + 312 + dev_dbg(dev, "FDL status: %#x\n", status); 313 + 314 + ret = fdl_status_process(interrupt, status); 315 + if (ret < 0) 316 + goto reset_function; 317 + 318 + response = ret; 319 + 320 + dev_dbg(dev, "FDL response: %#x\n", response); 321 + 322 + ret = regmap_write(interrupt->function_regmap, reg, 323 + response | (status & ~SDCA_CTL_XU_FDLH_MASK)); 324 + if (ret < 0) { 325 + dev_err(dev, "failed to set FDL status signal: %d\n", ret); 326 + return ret; 327 + } 328 + 329 + ret = sdca_ump_set_owner_device(dev, interrupt->function_regmap, 330 + interrupt->function, interrupt->entity, 331 + interrupt->control); 332 + if (ret) 333 + return ret; 334 + 335 + switch (response) { 336 + case SDCA_CTL_XU_FDLH_RESET_ACK: 337 + dev_dbg(dev, "FDL request reset\n"); 338 + 339 + switch (xu->reset_mechanism) { 340 + default: 341 + dev_warn(dev, "Requested reset mechanism not implemented\n"); 342 + fallthrough; 343 + case SDCA_XU_RESET_FUNCTION: 344 + goto reset_function; 345 + } 346 + default: 347 + return 0; 348 + } 349 + 350 + reset_function: 351 + sdca_reset_function(dev, interrupt->function, interrupt->function_regmap); 352 + 353 + return ret; 354 + } 355 + EXPORT_SYMBOL_NS_GPL(sdca_fdl_process, "SND_SOC_SDCA"); 356 + 357 + /** 358 + * sdca_fdl_alloc_state - allocate state for an FDL interrupt 359 + * @interrupt: SDCA interrupt structure. 360 + * 361 + * Return: Zero on success or a negative error code. 362 + */ 363 + int sdca_fdl_alloc_state(struct sdca_interrupt *interrupt) 364 + { 365 + struct device *dev = interrupt->dev; 366 + struct fdl_state *fdl_state; 367 + 368 + fdl_state = devm_kzalloc(dev, sizeof(struct fdl_state), GFP_KERNEL); 369 + if (!fdl_state) 370 + return -ENOMEM; 371 + 372 + interrupt->priv = fdl_state; 373 + 374 + return 0; 375 + } 376 + EXPORT_SYMBOL_NS_GPL(sdca_fdl_alloc_state, "SND_SOC_SDCA");