sdca_fdl.c 14 KB

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