Debian Codegraph @

sound/soc/sdca/sdca_fdl.c

raw
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/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <linux/sprintf.h>
20 #include <linux/soundwire/sdw.h>
21 #include <linux/soundwire/sdw_registers.h>
22 #include <sound/sdca.h>
23 #include <sound/sdca_fdl.h>
24 #include <sound/sdca_function.h>
25 #include <sound/sdca_interrupts.h>
26 #include <sound/sdca_ump.h>
27
28 /**
29 * sdca_reset_function - send an SDCA function reset
30 * @dev: Device pointer for error messages.
31 * @function: Pointer to the SDCA Function.
32 * @regmap: Pointer to the SDCA Function regmap.
33 *
34 * Return: Zero on success or a negative error code.
35 */
36 int sdca_reset_function(struct device *dev, struct sdca_function_data *function,
37 struct regmap *regmap)
38 {
39 unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
40 SDCA_ENTITY_TYPE_ENTITY_0,
41 SDCA_CTL_ENTITY_0_FUNCTION_ACTION, 0);
42 unsigned int val, poll_us;
43 int ret;
44
45 ret = regmap_write(regmap, reg, SDCA_CTL_ENTITY_0_RESET_FUNCTION_NOW);
46 if (ret) // Allowed for function reset to not be implemented
47 return 0;
48
49 /*
50 * Poll up to 16 times but no more than once per ms, these are just
51 * arbitrarily selected values, so may be fine tuned in future.
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 /**
67 * sdca_fdl_sync - wait for a function to finish FDL
68 * @dev: Device pointer for error messages.
69 * @function: Pointer to the SDCA Function.
70 * @info: Pointer to the SDCA interrupt info for this device.
71 *
72 * Return: Zero on success or a negative error code.
73 */
74 int sdca_fdl_sync(struct device *dev, struct sdca_function_data *function,
75 struct sdca_interrupt_info *info)
76 {
77 static const int fdl_retries = 6;
78 unsigned long begin_timeout = msecs_to_jiffies(100);
79 unsigned long done_timeout = msecs_to_jiffies(4000);
80 int nfdl;
81 int i, j;
82
83 for (i = 0; i < fdl_retries; i++) {
84 nfdl = 0;
85
86 for (j = 0; j < SDCA_MAX_INTERRUPTS; j++) {
87 struct sdca_interrupt *interrupt = &info->irqs[j];
88 struct fdl_state *fdl_state;
89 unsigned long time;
90
91 if (interrupt->function != function ||
92 !interrupt->entity || !interrupt->control ||
93 interrupt->entity->type != SDCA_ENTITY_TYPE_XU ||
94 interrupt->control->sel != SDCA_CTL_XU_FDL_CURRENTOWNER)
95 continue;
96
97 fdl_state = interrupt->priv;
98 nfdl++;
99
100 /*
101 * Looking for timeout without any new FDL requests
102 * to imply the device has completed initial
103 * firmware setup. Alas the specification doesn't
104 * have any mechanism to detect this.
105 */
106 time = wait_for_completion_timeout(&fdl_state->begin,
107 begin_timeout);
108 if (!time) {
109 dev_dbg(dev, "no new FDL starts\n");
110 nfdl--;
111 continue;
112 }
113
114 time = wait_for_completion_timeout(&fdl_state->done,
115 done_timeout);
116 if (!time) {
117 dev_err(dev, "timed out waiting for FDL to complete\n");
118 goto error;
119 }
120 }
121
122 if (!nfdl)
123 return 0;
124 }
125
126 dev_err(dev, "too many FDL requests\n");
127
128 error:
129 for (j = 0; j < SDCA_MAX_INTERRUPTS; j++) {
130 struct sdca_interrupt *interrupt = &info->irqs[j];
131 struct fdl_state *fdl_state;
132
133 if (interrupt->function != function ||
134 !interrupt->entity || !interrupt->control ||
135 interrupt->entity->type != SDCA_ENTITY_TYPE_XU ||
136 interrupt->control->sel != SDCA_CTL_XU_FDL_CURRENTOWNER)
137 continue;
138
139 disable_irq(interrupt->irq);
140
141 fdl_state = interrupt->priv;
142
143 sdca_ump_cancel_timeout(&fdl_state->timeout);
144 }
145
146 return -ETIMEDOUT;
147 }
148 EXPORT_SYMBOL_NS_GPL(sdca_fdl_sync, "SND_SOC_SDCA");
149
150 static char *fdl_get_sku_filename(struct device *dev,
151 struct sdca_fdl_file *fdl_file)
152 {
153 struct device *parent = dev;
154 const char *product_vendor;
155 const char *product_sku;
156
157 /*
158 * Try to find pci_dev manually because the card may not be ready to be
159 * used for snd_soc_card_get_pci_ssid yet
160 */
161 while (parent) {
162 if (dev_is_pci(parent)) {
163 struct pci_dev *pci_dev = to_pci_dev(parent);
164
165 return kasprintf(GFP_KERNEL, "sdca/%x/%x/%x/%x.bin",
166 fdl_file->vendor_id,
167 pci_dev->subsystem_vendor,
168 pci_dev->subsystem_device,
169 fdl_file->file_id);
170 } else {
171 parent = parent->parent;
172 }
173 }
174
175 product_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
176 if (!product_vendor || !strcmp(product_vendor, "Default string"))
177 product_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
178 if (!product_vendor || !strcmp(product_vendor, "Default string"))
179 product_vendor = dmi_get_system_info(DMI_CHASSIS_VENDOR);
180 if (!product_vendor)
181 product_vendor = "unknown";
182
183 product_sku = dmi_get_system_info(DMI_PRODUCT_SKU);
184 if (!product_sku || !strcmp(product_sku, "Default string"))
185 product_sku = dmi_get_system_info(DMI_PRODUCT_NAME);
186 if (!product_sku)
187 product_sku = "unknown";
188
189 return kasprintf(GFP_KERNEL, "sdca/%x/%s/%s/%x.bin", fdl_file->vendor_id,
190 product_vendor, product_sku, fdl_file->file_id);
191 }
192
193 static int fdl_load_file(struct sdca_interrupt *interrupt,
194 struct sdca_fdl_set *set, int file_index)
195 {
196 struct device *dev = interrupt->dev;
197 struct sdca_fdl_data *fdl_data = &interrupt->function->fdl_data;
198 const struct firmware *firmware = NULL;
199 struct acpi_sw_file *swf = NULL, *tmp;
200 struct sdca_fdl_file *fdl_file;
201 char *disk_filename;
202 int ret;
203 int i;
204
205 if (!set) {
206 dev_err(dev, "request to load SWF with no set\n");
207 return -EINVAL;
208 }
209
210 fdl_file = &set->files[file_index];
211
212 if (fdl_data->swft) {
213 tmp = fdl_data->swft->files;
214 for (i = 0; i < fdl_data->swft->header.length; i += tmp->file_length,
215 tmp = ACPI_ADD_PTR(struct acpi_sw_file, tmp, tmp->file_length)) {
216 if (tmp->vendor_id == fdl_file->vendor_id &&
217 tmp->file_id == fdl_file->file_id) {
218 dev_dbg(dev, "located SWF in ACPI: %x-%x-%x\n",
219 tmp->vendor_id, tmp->file_id,
220 tmp->file_version);
221 swf = tmp;
222 break;
223 }
224 }
225 }
226
227 disk_filename = fdl_get_sku_filename(dev, fdl_file);
228 if (!disk_filename)
229 return -ENOMEM;
230
231 dev_dbg(dev, "FDL disk filename: %s\n", disk_filename);
232
233 ret = firmware_request_nowarn(&firmware, disk_filename, dev);
234 kfree(disk_filename);
235 if (ret) {
236 disk_filename = kasprintf(GFP_KERNEL, "sdca/%x/%x.bin",
237 fdl_file->vendor_id, fdl_file->file_id);
238 if (!disk_filename)
239 return -ENOMEM;
240
241 dev_dbg(dev, "FDL disk filename: %s\n", disk_filename);
242
243 ret = firmware_request_nowarn(&firmware, disk_filename, dev);
244 kfree(disk_filename);
245 }
246
247 if (!ret) {
248 tmp = (struct acpi_sw_file *)&firmware->data[0];
249
250 if (firmware->size < sizeof(*tmp) ||
251 tmp->file_length != firmware->size) {
252 dev_err(dev, "bad disk SWF size\n");
253 } else if (!swf || swf->file_version <= tmp->file_version) {
254 dev_dbg(dev, "using SWF from disk\n");
255 swf = tmp;
256 }
257 }
258
259 if (!swf) {
260 dev_err(dev, "failed to locate SWF\n");
261 return -ENOENT;
262 }
263
264 dev_info(dev, "loading SWF: %x-%x-%x\n",
265 swf->vendor_id, swf->file_id, swf->file_version);
266
267 ret = sdca_ump_write_message(dev, interrupt->device_regmap,
268 interrupt->function_regmap,
269 interrupt->function, interrupt->entity,
270 SDCA_CTL_XU_FDL_MESSAGEOFFSET, fdl_file->fdl_offset,
271 SDCA_CTL_XU_FDL_MESSAGELENGTH, swf->data,
272 swf->file_length - offsetof(struct acpi_sw_file, data));
273 release_firmware(firmware);
274 return ret;
275 }
276
277 static struct sdca_fdl_set *fdl_get_set(struct sdca_interrupt *interrupt)
278 {
279 struct device *dev = interrupt->dev;
280 struct sdca_fdl_data *fdl_data = &interrupt->function->fdl_data;
281 struct sdca_entity *xu = interrupt->entity;
282 struct sdca_control_range *range;
283 unsigned int val;
284 int i, ret;
285
286 ret = regmap_read(interrupt->function_regmap,
287 SDW_SDCA_CTL(interrupt->function->desc->adr, xu->id,
288 SDCA_CTL_XU_FDL_SET_INDEX, 0),
289 &val);
290 if (ret < 0) {
291 dev_err(dev, "failed to read FDL set index: %d\n", ret);
292 return NULL;
293 }
294
295 range = sdca_selector_find_range(dev, xu, SDCA_CTL_XU_FDL_SET_INDEX,
296 SDCA_FDL_SET_INDEX_NCOLS, 0);
297
298 val = sdca_range_search(range, SDCA_FDL_SET_INDEX_SET_NUMBER,
299 val, SDCA_FDL_SET_INDEX_FILE_SET_ID);
300
301 for (i = 0; i < fdl_data->num_sets; i++) {
302 if (fdl_data->sets[i].id == val)
303 return &fdl_data->sets[i];
304 }
305
306 dev_err(dev, "invalid fileset id: %d\n", val);
307 return NULL;
308 }
309
310 static void fdl_end(struct sdca_interrupt *interrupt)
311 {
312 struct fdl_state *fdl_state = interrupt->priv;
313
314 if (!fdl_state->set)
315 return;
316
317 fdl_state->set = NULL;
318
319 pm_runtime_put(interrupt->dev);
320 complete(&fdl_state->done);
321
322 dev_dbg(interrupt->dev, "completed FDL process\n");
323 }
324
325 static void sdca_fdl_timeout_work(struct work_struct *work)
326 {
327 struct fdl_state *fdl_state = container_of(work, struct fdl_state,
328 timeout.work);
329 struct sdca_interrupt *interrupt = fdl_state->interrupt;
330 struct device *dev = interrupt->dev;
331
332 dev_err(dev, "FDL transaction timed out\n");
333
334 guard(mutex)(&fdl_state->lock);
335
336 fdl_end(interrupt);
337 sdca_reset_function(dev, interrupt->function, interrupt->function_regmap);
338 }
339
340 static int fdl_status_process(struct sdca_interrupt *interrupt, unsigned int status)
341 {
342 struct fdl_state *fdl_state = interrupt->priv;
343 int ret;
344
345 switch (status) {
346 case SDCA_CTL_XU_FDLD_NEEDS_SET:
347 dev_dbg(interrupt->dev, "starting FDL process...\n");
348
349 pm_runtime_get(interrupt->dev);
350 complete(&fdl_state->begin);
351
352 fdl_state->file_index = 0;
353 fdl_state->set = fdl_get_set(interrupt);
354 fallthrough;
355 case SDCA_CTL_XU_FDLD_MORE_FILES_OK:
356 ret = fdl_load_file(interrupt, fdl_state->set, fdl_state->file_index);
357 if (ret) {
358 fdl_end(interrupt);
359 return SDCA_CTL_XU_FDLH_REQ_ABORT;
360 }
361
362 return SDCA_CTL_XU_FDLH_FILE_AVAILABLE;
363 case SDCA_CTL_XU_FDLD_FILE_OK:
364 if (!fdl_state->set) {
365 fdl_end(interrupt);
366 return SDCA_CTL_XU_FDLH_REQ_ABORT;
367 }
368
369 fdl_state->file_index++;
370
371 if (fdl_state->file_index < fdl_state->set->num_files)
372 return SDCA_CTL_XU_FDLH_MORE_FILES;
373 fallthrough;
374 case SDCA_CTL_XU_FDLD_COMPLETE:
375 fdl_end(interrupt);
376 return SDCA_CTL_XU_FDLH_COMPLETE;
377 default:
378 fdl_end(interrupt);
379
380 if (status & SDCA_CTL_XU_FDLD_REQ_RESET)
381 return SDCA_CTL_XU_FDLH_RESET_ACK;
382 else if (status & SDCA_CTL_XU_FDLD_REQ_ABORT)
383 return SDCA_CTL_XU_FDLH_COMPLETE;
384
385 dev_err(interrupt->dev, "invalid FDL status: %x\n", status);
386 return -EINVAL;
387 }
388 }
389
390 /**
391 * sdca_fdl_process - Process the FDL state machine
392 * @interrupt: SDCA interrupt structure
393 *
394 * Based on section 13.2.5 Flow Diagram for File Download, Host side.
395 *
396 * Return: Zero on success or a negative error code.
397 */
398 int sdca_fdl_process(struct sdca_interrupt *interrupt)
399 {
400 struct device *dev = interrupt->dev;
401 struct sdca_entity_xu *xu = &interrupt->entity->xu;
402 struct fdl_state *fdl_state = interrupt->priv;
403 unsigned int reg, status;
404 int response, ret;
405
406 ret = sdca_ump_get_owner_host(dev, interrupt->function_regmap,
407 interrupt->function, interrupt->entity,
408 interrupt->control);
409 if (ret)
410 goto reset_function;
411
412 sdca_ump_cancel_timeout(&fdl_state->timeout);
413
414 scoped_guard(mutex, &fdl_state->lock) {
415 reg = SDW_SDCA_CTL(interrupt->function->desc->adr,
416 interrupt->entity->id, SDCA_CTL_XU_FDL_STATUS, 0);
417 ret = regmap_read(interrupt->function_regmap, reg, &status);
418 if (ret < 0) {
419 dev_err(dev, "failed to read FDL status: %d\n", ret);
420 return ret;
421 }
422
423 dev_dbg(dev, "FDL status: %#x\n", status);
424
425 ret = fdl_status_process(interrupt, status);
426 if (ret < 0)
427 goto reset_function;
428
429 response = ret;
430
431 dev_dbg(dev, "FDL response: %#x\n", response);
432
433 ret = regmap_write(interrupt->function_regmap, reg,
434 response | (status & ~SDCA_CTL_XU_FDLH_MASK));
435 if (ret < 0) {
436 dev_err(dev, "failed to set FDL status signal: %d\n", ret);
437 return ret;
438 }
439
440 ret = sdca_ump_set_owner_device(dev, interrupt->function_regmap,
441 interrupt->function,
442 interrupt->entity,
443 interrupt->control);
444 if (ret)
445 return ret;
446
447 switch (response) {
448 case SDCA_CTL_XU_FDLH_RESET_ACK:
449 dev_dbg(dev, "FDL request reset\n");
450
451 switch (xu->reset_mechanism) {
452 default:
453 dev_warn(dev, "Requested reset mechanism not implemented\n");
454 fallthrough;
455 case SDCA_XU_RESET_FUNCTION:
456 goto reset_function;
457 }
458 case SDCA_CTL_XU_FDLH_COMPLETE:
459 if (status & SDCA_CTL_XU_FDLD_REQ_ABORT ||
460 status == SDCA_CTL_XU_FDLD_COMPLETE)
461 return 0;
462 fallthrough;
463 default:
464 sdca_ump_schedule_timeout(&fdl_state->timeout, xu->max_delay);
465 return 0;
466 }
467 }
468
469 reset_function:
470 sdca_reset_function(dev, interrupt->function, interrupt->function_regmap);
471
472 return ret;
473 }
474 EXPORT_SYMBOL_NS_GPL(sdca_fdl_process, "SND_SOC_SDCA");
475
476 /**
477 * sdca_fdl_alloc_state - allocate state for an FDL interrupt
478 * @interrupt: SDCA interrupt structure.
479 *
480 * Return: Zero on success or a negative error code.
481 */
482 int sdca_fdl_alloc_state(struct sdca_interrupt *interrupt)
483 {
484 struct device *dev = interrupt->dev;
485 struct fdl_state *fdl_state;
486
487 fdl_state = devm_kzalloc(dev, sizeof(*fdl_state), GFP_KERNEL);
488 if (!fdl_state)
489 return -ENOMEM;
490
491 INIT_DELAYED_WORK(&fdl_state->timeout, sdca_fdl_timeout_work);
492 init_completion(&fdl_state->begin);
493 init_completion(&fdl_state->done);
494 mutex_init(&fdl_state->lock);
495 fdl_state->interrupt = interrupt;
496
497 interrupt->priv = fdl_state;
498
499 return 0;
500 }
501 EXPORT_SYMBOL_NS_GPL(sdca_fdl_alloc_state, "SND_SOC_SDCA");