mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
To: Padmanabhan Rajanbabu <p.rajanbabu@samsung.com>,
	lgirdwood@gmail.com, broonie@kernel.org, robh+dt@kernel.org,
	krzysztof.kozlowski+dt@linaro.org, s.nawrocki@samsung.com,
	perex@perex.cz, tiwai@suse.com, pankaj.dubey@samsung.com,
	alim.akhtar@samsung.com, rcsekar@samsung.com,
	aswani.reddy@samsung.com
Cc: alsa-devel@alsa-project.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-samsung-soc@vger.kernel.org
Subject: Re: [PATCH 4/6] ASoC: samsung: fsd: Add FSD soundcard driver
Date: Sun, 16 Oct 2022 11:18:08 -0400	[thread overview]
Message-ID: <60620ca9-80cd-9b13-800b-130a3f75442a@linaro.org> (raw)
In-Reply-To: <20221014102151.108539-5-p.rajanbabu@samsung.com>

On 14/10/2022 06:21, Padmanabhan Rajanbabu wrote:
> Add a soundcard driver for FSD audio interface to bridge the
> CPU DAI of samsung I2S with the codec and platform driver.
> 

Thank you for your patch. There is something to discuss/improve.

> +
> +#define FSD_PREFIX		"tesla,"
> +#define FSD_DAI_SRC_PCLK	3
> +#define FSD_DAI_RFS_192		192
> +#define FSD_DAI_BFS_48		48
> +#define FSD_DAI_BFS_96		96
> +#define FSD_DAI_BFS_192		192
> +
> +struct fsd_card_priv {
> +	struct snd_soc_card card;
> +	struct snd_soc_dai_link *dai_link;
> +	u32 tdm_slots;
> +	u32 tdm_slot_width;
> +};
> +
> +static unsigned int fsd_card_get_rfs(unsigned int rate)
> +{
> +	return FSD_DAI_RFS_192;

This wrapper is a bit silly...

> +}
> +
> +static unsigned int fsd_card_get_bfs(unsigned int channels)
> +{
> +	switch (channels) {
> +	case 1:
> +	case 2:
> +		return FSD_DAI_BFS_48;
> +	case 3:
> +	case 4:
> +		return FSD_DAI_BFS_96;
> +	case 5:
> +	case 6:
> +	case 7:
> +	case 8:
> +		return FSD_DAI_BFS_192;
> +	default:
> +		return FSD_DAI_BFS_48;
> +	}
> +}
> +
> +static unsigned int fsd_card_get_psr(unsigned int rate)
> +{
> +	switch (rate) {
> +	case 8000:	return 43;
> +	case 11025:	return 31;
> +	case 16000:	return 21;
> +	case 22050:	return 16;
> +	case 32000:	return 11;
> +	case 44100:	return 8;
> +	case 48000:	return 7;
> +	case 64000:	return 5;
> +	case 88200:	return 4;
> +	case 96000:	return 4;
> +	case 192000:	return 2;
> +	default:	return 1;
> +	}
> +}
> +
> +static int fsd_card_hw_params(struct snd_pcm_substream *substream,
> +					struct snd_pcm_hw_params *params)
> +{
> +	struct snd_soc_pcm_runtime *rtd	= substream->private_data;
> +	struct snd_soc_card *card	= rtd->card;
> +	struct snd_soc_dai *cpu_dai	= asoc_rtd_to_cpu(rtd, 0);
> +	struct snd_soc_dai_link *link	= rtd->dai_link;
> +	struct fsd_card_priv *priv	= snd_soc_card_get_drvdata(card);
> +	unsigned int rfs, bfs, psr;
> +	int ret = 0, cdclk_dir;
> +
> +	rfs = fsd_card_get_rfs(params_rate(params));
> +	bfs = fsd_card_get_bfs(params_channels(params));
> +	psr = fsd_card_get_psr(params_rate(params));
> +
> +	/* Configure the Root Clock Source */
> +	ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_OPCLK,
> +					false, FSD_DAI_SRC_PCLK);
> +	if (ret < 0) {
> +		dev_err(card->dev, "Failed to set OPCLK: %d\n", ret);
> +		goto err;
> +	}
> +
> +	ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_RCLKSRC_0,
> +					false, false);
> +	if (ret < 0) {
> +		dev_err(card->dev, "Failed to set RCLKSRC: %d\n", ret);

Don't you need to cleanup on error paths?

> +		goto err;
> +	}
> +
> +	/* Set CPU DAI configuration */
> +	ret = snd_soc_dai_set_fmt(cpu_dai, link->dai_fmt);
> +	if (ret < 0) {
> +		dev_err(card->dev, "Failed to set DAIFMT: %d\n", ret);
> +		goto err;
> +	}
> +
> +	if (link->dai_fmt & SND_SOC_DAIFMT_CBC_CFC) {
> +		cdclk_dir = SND_SOC_CLOCK_OUT;
> +	} else if (link->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) {
> +		cdclk_dir = SND_SOC_CLOCK_IN;
> +	} else {
> +		dev_err(card->dev, "Missing Clock Master information\n");
> +		goto err;
> +	}
> +
> +	/* Set Clock Source for CDCLK */
> +	ret = snd_soc_dai_set_sysclk(cpu_dai, SAMSUNG_I2S_CDCLK,
> +					rfs, cdclk_dir);
> +	if (ret < 0) {
> +		dev_err(card->dev, "Failed to set CDCLK: %d\n", ret);
> +		goto err;
> +	}
> +
> +	ret = snd_soc_dai_set_clkdiv(cpu_dai, SAMSUNG_I2S_DIV_RCLK, psr);
> +	if (ret < 0) {
> +		dev_err(card->dev, "Failed to set PSR: %d\n", ret);
> +		goto err;
> +	}
> +
> +	ret = snd_soc_dai_set_clkdiv(cpu_dai, SAMSUNG_I2S_DIV_BCLK, bfs);
> +	if (ret < 0) {
> +		dev_err(card->dev, "Failed to set BCLK: %d\n", ret);
> +		goto err;
> +	}
> +
> +	if (priv->tdm_slots) {
> +		ret = snd_soc_dai_set_tdm_slot(cpu_dai, false, false,
> +				priv->tdm_slots, priv->tdm_slot_width);
> +		if (ret < 0) {
> +			dev_err(card->dev,
> +				"Failed to configure in TDM mode:%d\n", ret);
> +			goto err;
> +		}
> +	}
> +
> +err:
> +	return ret;
> +}
> +
> +static const struct snd_soc_ops fsd_card_ops = {
> +	.hw_params	= fsd_card_hw_params,
> +};
> +
> +static struct fsd_card_priv *fsd_card_parse_of(struct snd_soc_card *card)
> +{
> +	struct fsd_card_priv *priv;
> +	struct snd_soc_dai_link *link;
> +	struct device *dev = card->dev;
> +	struct device_node *node = dev->of_node;
> +	struct device_node *np, *cpu_node, *codec_node;
> +	struct snd_soc_dai_link_component *dlc;
> +	int ret, id = 0, num_links;
> +
> +	ret = snd_soc_of_parse_card_name(card, "model");
> +	if (ret) {
> +		dev_err(dev, "Error parsing card name: %d\n", ret);
> +		return ERR_PTR(ret);

return dev_err_probe

> +	}
> +
> +	if (of_property_read_bool(dev->of_node, "widgets")) {
> +		ret = snd_soc_of_parse_audio_simple_widgets(card, "widgets");
> +		if (ret)
> +			return ERR_PTR(ret);
> +	}
> +
> +	/* Add DAPM routes to the card */
> +	if (of_property_read_bool(node, "audio-routing")) {
> +		ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
> +		if (ret)
> +			return ERR_PTR(ret);
> +	}
> +
> +	num_links = of_get_child_count(node);
> +
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return ERR_PTR(-ENOMEM);
> +
> +	priv->dai_link = devm_kzalloc(dev, num_links * sizeof(*priv->dai_link),
> +								GFP_KERNEL);
> +	if (!priv->dai_link)
> +		return ERR_PTR(-ENOMEM);
> +
> +	priv->tdm_slots = 0;
> +	priv->tdm_slot_width = 0;
> +
> +	snd_soc_of_parse_tdm_slot(node, NULL, NULL, &priv->tdm_slots,
> +			&priv->tdm_slot_width);
> +
> +	link = priv->dai_link;
> +
> +	for_each_child_of_node(node, np) {
> +		dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
> +		if (!dlc)
> +			return ERR_PTR(-ENOMEM);
> +
> +		link->id		= id;
> +		link->cpus		= &dlc[0];
> +		link->platforms		= &dlc[1];
> +		link->num_cpus		= 1;
> +		link->num_codecs	= 1;
> +		link->num_platforms	= 1;
> +
> +		cpu_node = of_get_child_by_name(np, "cpu");
> +		if (!cpu_node) {
> +			dev_err(dev, "Missing CPU/Codec node\n");
> +			ret = -EINVAL;
> +			goto err_cpu_node;
> +		}
> +
> +		ret = snd_soc_of_get_dai_link_cpus(dev, cpu_node, link);
> +		if (ret < 0) {
> +			dev_err(dev, "Error Parsing CPU DAI name\n");
> +			goto err_cpu_name;
> +		}
> +
> +		link->platforms->of_node = link->cpus->of_node;
> +
> +		codec_node = of_get_child_by_name(np, "codec");
> +		if (codec_node) {
> +			ret = snd_soc_of_get_dai_link_codecs(dev, codec_node,
> +					link);
> +			if (ret < 0) {
> +				dev_err(dev, "Error Parsing Codec DAI name\n");
> +				goto err_codec_name;
> +			}
> +		} else {
> +			dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
> +			if (!dlc) {
> +				ret = -ENOMEM;
> +				goto err_cpu_name;
> +			}
> +
> +			link->codecs = dlc;
> +
> +			link->codecs->dai_name = "snd-soc-dummy-dai";
> +			link->codecs->name = "snd-soc-dummy";
> +			link->dynamic = 1;
> +
> +			snd_soc_dai_link_set_capabilities(link);
> +			link->ignore_suspend = 1;
> +			link->nonatomic = 1;
> +		}
> +
> +		ret = asoc_simple_parse_daifmt(dev, np, codec_node,
> +					FSD_PREFIX, &link->dai_fmt);
> +		if (ret)
> +			link->dai_fmt = SND_SOC_DAIFMT_NB_NF |
> +					SND_SOC_DAIFMT_CBC_CFC |
> +					SND_SOC_DAIFMT_I2S;
> +
> +		ret = of_property_read_string(np, "link-name", &link->name);
> +		if (ret) {
> +			dev_err(card->dev, "Error Parsing link name\n");
> +			goto err_codec_name;
> +		}
> +
> +		link->stream_name = link->name;
> +		link->ops = &fsd_card_ops;
> +
> +		link++;
> +		id++;
> +
> +		of_node_put(cpu_node);
> +		of_node_put(codec_node);
> +	}
> +
> +	card->dai_link = priv->dai_link;
> +	card->num_links = num_links;
> +
> +	return priv;
> +
> +err_codec_name:
> +	of_node_put(codec_node);
> +err_cpu_name:
> +	of_node_put(cpu_node);
> +err_cpu_node:
> +	of_node_put(np);
> +	return ERR_PTR(ret);
> +}
> +
> +static int fsd_platform_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct snd_soc_card *card;
> +	struct fsd_card_priv *fsd_priv;
> +
> +	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
> +	if (!card)
> +		return -ENOMEM;
> +
> +	card->dev	= dev;
> +	fsd_priv	= fsd_card_parse_of(card);

Drop the indentation before =

> +
> +	if (IS_ERR(fsd_priv)) {
> +		dev_err(&pdev->dev, "Error Parsing DAI Link: %ld\n",
> +				PTR_ERR(fsd_priv));
> +		return PTR_ERR(fsd_priv);

return dev_err_probe

> +	}
> +
> +	snd_soc_card_set_drvdata(card, fsd_priv);
> +
> +	return devm_snd_soc_register_card(&pdev->dev, card);
> +}
> +
> +static const struct of_device_id fsd_device_id[] = {
> +	{ .compatible = "tesla,fsd-card" },
> +	{},
> +};
> +MODULE_DEVICE_TABLE(of, fsd_device_id);
> +
> +static struct platform_driver fsd_platform_driver = {
> +	.driver = {
> +		.name = "fsd-card",
> +		.of_match_table = of_match_ptr(fsd_device_id),

of_match_ptr comes with maybe_unused. Or drop it.



Best regards,
Krzysztof


  parent reply	other threads:[~2022-10-16 15:18 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20221014104843epcas5p47f6daaad2e67e0c9eedd68c2256c025b@epcas5p4.samsung.com>
2022-10-14 10:21 ` [PATCH 0/6] ASoC: samsung: fsd: audio support for FSD SoC Padmanabhan Rajanbabu
     [not found]   ` <CGME20221014104850epcas5p1a707b9d407a0947c3519077cf7fca5ff@epcas5p1.samsung.com>
2022-10-14 10:21     ` [PATCH 1/6] ASoC: samsung: i2s: TDM Support for CPU DAI driver Padmanabhan Rajanbabu
     [not found]   ` <CGME20221014104857epcas5p2a275a1d606ca066227228d13bcf5b120@epcas5p2.samsung.com>
2022-10-14 10:21     ` [PATCH 2/6] ASoC: samsung: i2s: configure PSR from sound card Padmanabhan Rajanbabu
2022-10-14 12:02       ` Mark Brown
2022-10-21  8:00         ` Padmanabhan Rajanbabu
2022-10-21 11:53           ` Mark Brown
2022-11-08  5:23             ` Padmanabhan Rajanbabu
2022-11-09 17:38               ` Mark Brown
2023-01-03  4:55                 ` Padmanabhan Rajanbabu
2022-10-23 13:12           ` Krzysztof Kozlowski
     [not found]   ` <CGME20221014104901epcas5p1a61ea81c3b1640bd8a064633c0b1e40d@epcas5p1.samsung.com>
2022-10-14 10:21     ` [PATCH 3/6] dt-bindings: sound: Add sound card bindings for Tesla FSD Padmanabhan Rajanbabu
2022-10-14 15:13       ` Rob Herring
2022-10-21  8:44         ` Padmanabhan Rajanbabu
2022-10-22 16:48           ` Krzysztof Kozlowski
2022-11-08  5:33             ` Padmanabhan Rajanbabu
2022-11-08 10:36               ` Krzysztof Kozlowski
     [not found]   ` <CGME20221014104904epcas5p4f458182cc9ac9c223d9a25566f3dd300@epcas5p4.samsung.com>
2022-10-14 10:21     ` [PATCH 4/6] ASoC: samsung: fsd: Add FSD soundcard driver Padmanabhan Rajanbabu
2022-10-14 12:23       ` Mark Brown
2022-10-21  8:09         ` Padmanabhan Rajanbabu
2022-10-16 15:18       ` Krzysztof Kozlowski [this message]
2022-10-21  9:04         ` Padmanabhan Rajanbabu
     [not found]   ` <CGME20221014104911epcas5p394100ff6ed53be32c4d64c7e23e48833@epcas5p3.samsung.com>
2022-10-14 10:21     ` [PATCH 5/6] arm64: dts: fsd: Add I2S DAI node for Tesla FSD Padmanabhan Rajanbabu
2022-10-14 13:24       ` Alim Akhtar
2022-10-21  8:09         ` Padmanabhan Rajanbabu
2022-10-16 15:14       ` Krzysztof Kozlowski
2022-10-21  8:49         ` Padmanabhan Rajanbabu
2022-10-21 13:01           ` Krzysztof Kozlowski
2022-11-08  5:26             ` Padmanabhan Rajanbabu
     [not found]   ` <CGME20221014104915epcas5p12414b87ea127b2d5bf521556bf841b00@epcas5p1.samsung.com>
2022-10-14 10:21     ` [PATCH 6/6] arm64: dts: fsd: Add sound card " Padmanabhan Rajanbabu
2022-10-14 13:29       ` Alim Akhtar
2022-10-21  8:12         ` Padmanabhan Rajanbabu
2022-10-21 12:53           ` Krzysztof Kozlowski
2022-11-08  5:25             ` Padmanabhan Rajanbabu

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=60620ca9-80cd-9b13-800b-130a3f75442a@linaro.org \
    --to=krzysztof.kozlowski@linaro.org \
    --cc=alim.akhtar@samsung.com \
    --cc=alsa-devel@alsa-project.org \
    --cc=aswani.reddy@samsung.com \
    --cc=broonie@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=krzysztof.kozlowski+dt@linaro.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-samsung-soc@vger.kernel.org \
    --cc=p.rajanbabu@samsung.com \
    --cc=pankaj.dubey@samsung.com \
    --cc=perex@perex.cz \
    --cc=rcsekar@samsung.com \
    --cc=robh+dt@kernel.org \
    --cc=s.nawrocki@samsung.com \
    --cc=tiwai@suse.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®