mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Simon Trimmer <simont@opensource.cirrus.com>
To: <broonie@kernel.org>, <lgirdwood@gmail.com>
Cc: <alsa-devel@alsa-project.org>, <patches@opensource.cirrus.com>,
	<linux-kernel@vger.kernel.org>,
	Simon Trimmer <simont@opensource.cirrus.com>
Subject: [PATCH 13/16] ASoC: wm_adsp: move firmware loading to client
Date: Mon, 13 Sep 2021 17:00:54 +0100	[thread overview]
Message-ID: <20210913160057.103842-14-simont@opensource.cirrus.com> (raw)
In-Reply-To: <20210913160057.103842-1-simont@opensource.cirrus.com>

This is preparation for moving the generic DSP support out of ASoC.
Passing the firmware as parameters into the power_up functions
simplifies the generic code that will be moved out of wm_adsp.

Signed-off-by: Simon Trimmer <simont@opensource.cirrus.com>
---
 sound/soc/codecs/wm_adsp.c | 163 +++++++++++++++++++++++++------------
 1 file changed, 112 insertions(+), 51 deletions(-)

diff --git a/sound/soc/codecs/wm_adsp.c b/sound/soc/codecs/wm_adsp.c
index c2e1eb8ff357..3b5b0cce7b35 100644
--- a/sound/soc/codecs/wm_adsp.c
+++ b/sound/soc/codecs/wm_adsp.c
@@ -29,6 +29,7 @@
 #include <sound/jack.h>
 #include <sound/initval.h>
 #include <sound/tlv.h>
+#include <linux/firmware.h>
 
 #include "wm_adsp.h"
 
@@ -1797,6 +1798,60 @@ static unsigned int cs_dsp_adsp1_parse_sizes(struct wm_adsp *dsp,
 	return pos + sizeof(*adsp1_sizes);
 }
 
+static void wm_adsp_release_firmware_files(struct wm_adsp *dsp,
+					   const struct firmware *wmfw_firmware,
+					   char *wmfw_filename,
+					   const struct firmware *coeff_firmware,
+					   char *coeff_filename)
+{
+	if (wmfw_firmware)
+		release_firmware(wmfw_firmware);
+	kfree(wmfw_filename);
+
+	if (coeff_firmware)
+		release_firmware(coeff_firmware);
+	kfree(coeff_filename);
+}
+
+static int wm_adsp_request_firmware_file(struct wm_adsp *dsp,
+					 const struct firmware **firmware,
+					 char **filename,
+					 char *suffix)
+{
+	int ret = 0;
+
+	*filename = kasprintf(GFP_KERNEL, "%s-%s-%s.%s", dsp->part, dsp->fwf_name,
+			      wm_adsp_fw[dsp->fw].file, suffix);
+	if (*filename == NULL)
+		return -ENOMEM;
+
+	ret = request_firmware(firmware, *filename, dsp->dev);
+	if (ret != 0) {
+		adsp_err(dsp, "Failed to request '%s'\n", *filename);
+		kfree(*filename);
+		*filename = NULL;
+	}
+
+	return ret;
+}
+
+static int wm_adsp_request_firmware_files(struct wm_adsp *dsp,
+					  const struct firmware **wmfw_firmware,
+					  char **wmfw_filename,
+					  const struct firmware **coeff_firmware,
+					  char **coeff_filename)
+{
+	int ret = 0;
+
+	ret = wm_adsp_request_firmware_file(dsp, wmfw_firmware, wmfw_filename, "wmfw");
+	if (ret != 0)
+		return ret;
+
+	wm_adsp_request_firmware_file(dsp, coeff_firmware, coeff_filename, "bin");
+
+	return 0;
+}
+
 static unsigned int cs_dsp_adsp2_parse_sizes(struct wm_adsp *dsp,
 					     const char * const file,
 					     unsigned int pos,
@@ -1837,10 +1892,10 @@ static bool cs_dsp_halo_validate_version(struct wm_adsp *dsp, unsigned int versi
 	}
 }
 
-static int cs_dsp_load(struct wm_adsp *dsp, const char *fw_file_name)
+static int cs_dsp_load(struct wm_adsp *dsp, const struct firmware *firmware,
+		       const char *file)
 {
 	LIST_HEAD(buf_list);
-	const struct firmware *firmware;
 	struct regmap *regmap = dsp->regmap;
 	unsigned int pos = 0;
 	const struct wmfw_header *header;
@@ -1849,25 +1904,12 @@ static int cs_dsp_load(struct wm_adsp *dsp, const char *fw_file_name)
 	const struct wmfw_region *region;
 	const struct cs_dsp_region *mem;
 	const char *region_name;
-	char *file, *text = NULL;
+	char *text = NULL;
 	struct cs_dsp_buf *buf;
 	unsigned int reg;
 	int regions = 0;
 	int ret, offset, type;
 
-	file = kzalloc(PAGE_SIZE, GFP_KERNEL);
-	if (file == NULL)
-		return -ENOMEM;
-
-	snprintf(file, PAGE_SIZE, "%s-%s-%s.wmfw", dsp->part, dsp->fwf_name,
-		 fw_file_name);
-	file[PAGE_SIZE - 1] = '\0';
-
-	ret = request_firmware(&firmware, file, dsp->dev);
-	if (ret != 0) {
-		cs_dsp_err(dsp, "Failed to request '%s'\n", file);
-		goto out;
-	}
 	ret = -EINVAL;
 
 	pos = sizeof(*header) + sizeof(*adsp1_sizes) + sizeof(*footer);
@@ -2031,10 +2073,7 @@ static int cs_dsp_load(struct wm_adsp *dsp, const char *fw_file_name)
 out_fw:
 	regmap_async_complete(regmap);
 	cs_dsp_buf_free(&buf_list);
-	release_firmware(firmware);
 	kfree(text);
-out:
-	kfree(file);
 
 	return ret;
 }
@@ -2582,45 +2621,33 @@ static int cs_dsp_halo_setup_algs(struct wm_adsp *dsp)
 	return ret;
 }
 
-static int cs_dsp_load_coeff(struct wm_adsp *dsp, const char *fw_file_name)
+static int cs_dsp_load_coeff(struct wm_adsp *dsp, const struct firmware *firmware,
+			     const char *file)
 {
 	LIST_HEAD(buf_list);
 	struct regmap *regmap = dsp->regmap;
 	struct wmfw_coeff_hdr *hdr;
 	struct wmfw_coeff_item *blk;
-	const struct firmware *firmware;
 	const struct cs_dsp_region *mem;
 	struct cs_dsp_alg_region *alg_region;
 	const char *region_name;
 	int ret, pos, blocks, type, offset, reg;
-	char *file;
 	struct cs_dsp_buf *buf;
 
-	file = kzalloc(PAGE_SIZE, GFP_KERNEL);
-	if (file == NULL)
-		return -ENOMEM;
+	if (!firmware)
+		return 0;
 
-	snprintf(file, PAGE_SIZE, "%s-%s-%s.bin", dsp->part, dsp->fwf_name,
-		 fw_file_name);
-	file[PAGE_SIZE - 1] = '\0';
-
-	ret = request_firmware(&firmware, file, dsp->dev);
-	if (ret != 0) {
-		cs_dsp_warn(dsp, "Failed to request '%s'\n", file);
-		ret = 0;
-		goto out;
-	}
 	ret = -EINVAL;
 
 	if (sizeof(*hdr) >= firmware->size) {
-		cs_dsp_err(dsp, "%s: file too short, %zu bytes\n",
+		cs_dsp_err(dsp, "%s: coefficient file too short, %zu bytes\n",
 			   file, firmware->size);
 		goto out_fw;
 	}
 
 	hdr = (void *)&firmware->data[0];
 	if (memcmp(hdr->magic, "WMDR", 4) != 0) {
-		cs_dsp_err(dsp, "%s: invalid magic\n", file);
+		cs_dsp_err(dsp, "%s: invalid coefficient magic\n", file);
 		goto out_fw;
 	}
 
@@ -2769,10 +2796,7 @@ static int cs_dsp_load_coeff(struct wm_adsp *dsp, const char *fw_file_name)
 
 out_fw:
 	regmap_async_complete(regmap);
-	release_firmware(firmware);
 	cs_dsp_buf_free(&buf_list);
-out:
-	kfree(file);
 	return ret;
 }
 
@@ -2837,7 +2861,10 @@ int wm_adsp1_init(struct wm_adsp *dsp)
 }
 EXPORT_SYMBOL_GPL(wm_adsp1_init);
 
-static int cs_dsp_adsp1_power_up(struct wm_adsp *dsp, const char *fw_file_name, const char *fw_name)
+static int cs_dsp_adsp1_power_up(struct wm_adsp *dsp,
+				 const struct firmware *wmfw_firmware, char *wmfw_filename,
+				 const struct firmware *coeff_firmware, char *coeff_filename,
+				 const char *fw_name)
 {
 	unsigned int val;
 	int ret;
@@ -2871,7 +2898,7 @@ static int cs_dsp_adsp1_power_up(struct wm_adsp *dsp, const char *fw_file_name,
 		}
 	}
 
-	ret = cs_dsp_load(dsp, fw_file_name);
+	ret = cs_dsp_load(dsp, wmfw_firmware, wmfw_filename);
 	if (ret != 0)
 		goto err_ena;
 
@@ -2879,7 +2906,7 @@ static int cs_dsp_adsp1_power_up(struct wm_adsp *dsp, const char *fw_file_name,
 	if (ret != 0)
 		goto err_ena;
 
-	ret = cs_dsp_load_coeff(dsp, fw_file_name);
+	ret = cs_dsp_load_coeff(dsp, coeff_firmware, coeff_filename);
 	if (ret != 0)
 		goto err_ena;
 
@@ -2949,14 +2976,29 @@ int wm_adsp1_event(struct snd_soc_dapm_widget *w,
 	struct wm_adsp *dsps = snd_soc_component_get_drvdata(component);
 	struct wm_adsp *dsp = &dsps[w->shift];
 	int ret = 0;
+	char *wmfw_filename = NULL;
+	const struct firmware *wmfw_firmware = NULL;
+	char *coeff_filename = NULL;
+	const struct firmware *coeff_firmware = NULL;
 
 	dsp->component = component;
 
 	switch (event) {
 	case SND_SOC_DAPM_POST_PMU:
+		ret = wm_adsp_request_firmware_files(dsp,
+						     &wmfw_firmware, &wmfw_filename,
+						     &coeff_firmware, &coeff_filename);
+		if (ret)
+			break;
+
 		ret = cs_dsp_adsp1_power_up(dsp,
-					    wm_adsp_fw[dsp->fw].file,
+					    wmfw_firmware, wmfw_filename,
+					    coeff_firmware, coeff_filename,
 					    wm_adsp_fw_text[dsp->fw]);
+
+		wm_adsp_release_firmware_files(dsp,
+					       wmfw_firmware, wmfw_filename,
+					       coeff_firmware, coeff_filename);
 		break;
 	case SND_SOC_DAPM_PRE_PMD:
 		cs_dsp_adsp1_power_down(dsp);
@@ -3176,8 +3218,10 @@ static void cs_dsp_halo_stop_watchdog(struct wm_adsp *dsp)
 			   HALO_WDT_EN_MASK, 0);
 }
 
-static void cs_dsp_power_up(struct wm_adsp *dsp, const char *fw_file_name,
-			    const char *fw_name)
+static int cs_dsp_power_up(struct wm_adsp *dsp,
+			   const struct firmware *wmfw_firmware, char *wmfw_filename,
+			   const struct firmware *coeff_firmware, char *coeff_filename,
+			   const char *fw_name)
 {
 	int ret;
 
@@ -3197,7 +3241,7 @@ static void cs_dsp_power_up(struct wm_adsp *dsp, const char *fw_file_name,
 			goto err_mem;
 	}
 
-	ret = cs_dsp_load(dsp, fw_file_name);
+	ret = cs_dsp_load(dsp, wmfw_firmware, wmfw_filename);
 	if (ret != 0)
 		goto err_ena;
 
@@ -3205,7 +3249,7 @@ static void cs_dsp_power_up(struct wm_adsp *dsp, const char *fw_file_name,
 	if (ret != 0)
 		goto err_ena;
 
-	ret = cs_dsp_load_coeff(dsp, fw_file_name);
+	ret = cs_dsp_load_coeff(dsp, coeff_firmware, coeff_filename);
 	if (ret != 0)
 		goto err_ena;
 
@@ -3221,8 +3265,7 @@ static void cs_dsp_power_up(struct wm_adsp *dsp, const char *fw_file_name,
 
 	mutex_unlock(&dsp->pwr_lock);
 
-	return;
-
+	return 0;
 err_ena:
 	if (dsp->ops->disable_core)
 		dsp->ops->disable_core(dsp);
@@ -3231,6 +3274,8 @@ static void cs_dsp_power_up(struct wm_adsp *dsp, const char *fw_file_name,
 		dsp->ops->disable_memory(dsp);
 err_mutex:
 	mutex_unlock(&dsp->pwr_lock);
+
+	return ret;
 }
 
 static void cs_dsp_power_down(struct wm_adsp *dsp)
@@ -3264,10 +3309,26 @@ static void wm_adsp_boot_work(struct work_struct *work)
 	struct wm_adsp *dsp = container_of(work,
 					   struct wm_adsp,
 					   boot_work);
+	int ret = 0;
+	char *wmfw_filename = NULL;
+	const struct firmware *wmfw_firmware = NULL;
+	char *coeff_filename = NULL;
+	const struct firmware *coeff_firmware = NULL;
+
+	ret = wm_adsp_request_firmware_files(dsp,
+					     &wmfw_firmware, &wmfw_filename,
+					     &coeff_firmware, &coeff_filename);
+	if (ret)
+		return;
 
 	cs_dsp_power_up(dsp,
-			wm_adsp_fw[dsp->fw].file,
+			wmfw_firmware, wmfw_filename,
+			coeff_firmware, coeff_filename,
 			wm_adsp_fw_text[dsp->fw]);
+
+	wm_adsp_release_firmware_files(dsp,
+				       wmfw_firmware, wmfw_filename,
+				       coeff_firmware, coeff_filename);
 }
 
 int wm_adsp_early_event(struct snd_soc_dapm_widget *w,
-- 
2.33.0


  parent reply	other threads:[~2021-09-13 16:01 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-13 16:00 [PATCH 00/16] add driver to support firmware loading on Cirrus Logic DSPs Simon Trimmer
2021-09-13 16:00 ` [PATCH 01/16] ASoC: wm_adsp: Remove use of snd_ctl_elem_type_t Simon Trimmer
2021-09-13 16:00 ` [PATCH 02/16] ASoC: wm_adsp: Move check for control existence Simon Trimmer
2021-09-13 16:00 ` [PATCH 03/16] ASoC: wm_adsp: Switch to using wm_coeff_read_ctrl for compressed buffers Simon Trimmer
2021-09-13 16:00 ` [PATCH 04/16] ASoC: wm_adsp: Cancel ongoing work when removing controls Simon Trimmer
2021-09-13 16:00 ` [PATCH 05/16] ASoC: wm_adsp: Rename generic DSP support Simon Trimmer
2021-09-13 16:00 ` [PATCH 06/16] ASoC: wm_adsp: Introduce cs_dsp logging macros Simon Trimmer
2021-09-13 16:00 ` [PATCH 07/16] ASoC: wm_adsp: Separate some ASoC and generic functions Simon Trimmer
2021-09-13 16:00 ` [PATCH 08/16] ASoC: wm_adsp: Split DSP power operations into helper functions Simon Trimmer
2021-09-13 16:00 ` [PATCH 09/16] ASoC: wm_adsp: Move sys_config_size to wm_adsp Simon Trimmer
2021-09-13 16:00 ` [PATCH 10/16] ASoC: wm_adsp: Separate generic cs_dsp_coeff_ctl handling Simon Trimmer
2021-09-13 16:00 ` [PATCH 11/16] ASoC: wm_adsp: Move check of dsp->running to better place Simon Trimmer
2021-09-13 16:00 ` [PATCH 12/16] ASoC: wm_adsp: Pass firmware names as parameters when starting DSP core Simon Trimmer
2021-09-13 16:00 ` Simon Trimmer [this message]
2021-09-13 16:00 ` [PATCH 14/16] ASoC: wm_adsp: Split out struct cs_dsp from struct wm_adsp Simon Trimmer
2021-09-13 16:00 ` [PATCH 15/16] ASoC: wm_adsp: Separate wm_adsp specifics in cs_dsp_client_ops Simon Trimmer
2021-09-13 16:00 ` [PATCH 16/16] firmware: cs_dsp: add driver to support firmware loading on Cirrus Logic DSPs Simon Trimmer
2021-09-27 17:45 ` [PATCH 00/16] " Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210913160057.103842-14-simont@opensource.cirrus.com \
    --to=simont@opensource.cirrus.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=patches@opensource.cirrus.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®