linux-omap.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jean-Francois Moine <moinejf@free.fr>
To: Jyri Sarha <jsarha@ti.com>
Cc: alsa-devel@alsa-project.org, linux-omap@vger.kernel.org,
	linux-arm-kernel@lists.infradead.org,
	dri-devel@lists.freedesktop.org
Subject: Re: [alsa-devel] [PATCH RFC v2 REPOST 3/8] ASoC: davinci-evm: HDMI audio support for TDA998x trough McASP I2S bus
Date: Fri, 24 Jan 2014 17:54:31 +0100	[thread overview]
Message-ID: <20140124175431.47a82d57@armhf> (raw)
In-Reply-To: <52E26325.4040203@ti.com>

On Fri, 24 Jan 2014 14:57:09 +0200
Jyri Sarha <jsarha@ti.com> wrote:

> Could you give me a link to a git repo with your tda998x codec code so I 
> could prepare to use that too?

I have no git repo. Instead, here is the source.

It goes into sound/soc/codecs and is selected with CONFIG_OF and
CONFIG_DRM_I2C_NXP_TDA998X.

It needs 2 exported functions of the tda998x driver: the first one to
select the audio port (tda998x_audio_update)  and the other one to get
the ELD (tda998x_audio_get_eld).

For these functions to receive the correct i2c_client, the codec
searches the tda998x driver in the DT by its compatible "nxp,tda998x".
If the tda998x is loaded by drm_i2c_encoder_init(), it should not be
declared in the DT, so, this raises a problem. I don't know what must
be done in this case.

The codec is used with the simple card as the sound card. For you, the
DT could be:

	hdmi_codec: hdmi-codec {
		compatible = "nxp,tda998x-codec";
		#sound-dai-cells = <1>;
		audio-ports = <0x03>, <0x04>;	/* i2s - s/pdif */
	};

	sound {
		compatible = "simple-audio-card";
		simple-audio-card,cpu {
			sound-dai = <&audio1 1>;
			format = "i2s";
		};
		simple-audio-card,codec {
			sound-dai = <&hdmi_codec 0>;	/* i2s */
		};
	};

('audio1' is the audio controller)

-------------------8<---------------- source sound/soc/codecs/tda998x.c
/*
 * ALSA SoC TDA998X driver
 *
 * This driver is used by the NXP TDA998x HDMI transmitter.
 *
 * Copyright (C) 2014 Jean-Francois Moine
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <sound/soc.h>
#include <sound/pcm.h>
#include <linux/of.h>
#include <linux/i2c.h>
#include <drm/drm_encoder_slave.h>
#include <drm/i2c/tda998x.h>

#define TDA998X_FORMATS	(SNDRV_PCM_FMTBIT_S16_LE | \
			SNDRV_PCM_FMTBIT_S20_3LE | \
			SNDRV_PCM_FMTBIT_S24_LE | \
			SNDRV_PCM_FMTBIT_S32_LE)

struct tda_priv {
	struct i2c_client *i2c_client;
	struct snd_soc_codec *codec;
	u32 ports[2];
	int dai_id;
	u8 *eld;
};

static void tda_get_encoder(struct tda_priv *priv)
{
	struct snd_soc_codec *codec = priv->codec;
	struct device_node *np;
	struct i2c_client *i2c_client;
	static const struct of_device_id tda_dt[] = {
		{ .compatible = "nxp,tda998x" },
		{ },
	};

	/* search the tda998x device (only one!) */
	np = of_find_matching_node_and_match(NULL, tda_dt, NULL);
	if (!np || !of_device_is_available(np)) {
		dev_err(codec->dev, "No tda998x in DT\n");
		return;
	}
	i2c_client = of_find_i2c_device_by_node(np);
	of_node_put(np);
	if (!i2c_client) {
		dev_err(codec->dev, "no tda998x i2c client\n");
		return;
	}
	if (!i2c_get_clientdata(i2c_client)) {
		dev_err(codec->dev, "tda998x not initialized\n");
		return;
	}

	priv->i2c_client = i2c_client;
}

static int tda_start_stop(struct tda_priv *priv,
			int start)
{
	int format;
	u32 port;

	if (!priv->i2c_client) {
		tda_get_encoder(priv);
		if (!priv->i2c_client)
			return -EINVAL;
	}

	/* give the audio input type and ports to the HDMI encoder */
	format = start ? priv->dai_id : 0;
	switch (format) {
	case AFMT_I2S:
		port = priv->ports[0];
		break;
	default:
	case AFMT_SPDIF:
		port = priv->ports[1];
		break;
	}
	tda998x_audio_update(priv->i2c_client, format, port);
	return 0;
}

static int tda_startup(struct snd_pcm_substream *substream,
			struct snd_soc_dai *dai)
{
	struct tda_priv *priv = snd_soc_codec_get_drvdata(dai->codec);
	u8 *eld = NULL;
	static unsigned rates_mask[] = {
		SNDRV_PCM_RATE_32000,
		SNDRV_PCM_RATE_44100,
		SNDRV_PCM_RATE_48000,
		SNDRV_PCM_RATE_88200,
		SNDRV_PCM_RATE_96000,
		SNDRV_PCM_RATE_176400,
		SNDRV_PCM_RATE_192000,
	};

	/* memorize the used DAI */
	priv->dai_id = dai->id;

	/* get the ELD from the tda998x driver */
	if (!priv->i2c_client)
		tda_get_encoder(priv);
	if (priv->i2c_client)
		eld = tda998x_audio_get_eld(priv->i2c_client);

	/* limit the hw params from the ELD (EDID) */
	if (eld) {
		struct snd_soc_dai_driver *dai_drv = dai->driver;
		struct snd_soc_pcm_stream *stream = &dai_drv->playback;
		u8 *sad;
		int sad_count;
		unsigned eld_ver, mnl, rates, rate_mask, i;
		unsigned max_channels;

		eld_ver = eld[0] >> 3;
		if (eld_ver != 2 && eld_ver != 31)
			return 0;

		mnl = eld[4] & 0x1f;
		if (mnl > 16)
			return 0;

		sad_count = eld[5] >> 4;
		sad = eld + 20 + mnl;

		/* Start from the basic audio settings */
		max_channels = 2;
		rates = 0;
		while (sad_count--) {
			switch (sad[0] & 0x78) {
			case 0x08: /* PCM */
				max_channels = max(max_channels, (sad[0] & 7) + 1u);
				rates |= sad[1];
				break;
			}
			sad += 3;
		}

		for (rate_mask = i = 0; i < ARRAY_SIZE(rates_mask); i++)
			if (rates & 1 << i)
				rate_mask |= rates_mask[i];

		/* change the snd_soc_pcm_stream values of the driver */
		stream->rates = rate_mask;
		stream->channels_max = max_channels;
	}

	/* start the TDA998x audio */
	return tda_start_stop(priv, 1);
}

static void tda_shutdown(struct snd_pcm_substream *substream,
			struct snd_soc_dai *dai)
{
	struct tda_priv *priv = snd_soc_codec_get_drvdata(dai->codec);

	priv->dai_id = 0;
	tda_start_stop(priv, 0);
}

static const struct snd_soc_dai_ops tda_ops = {
	.startup = tda_startup,
	.shutdown = tda_shutdown,
};

static const struct snd_soc_dai_driver tda998x_dai[] = {
    {
	.name = "i2s-hifi",
	.id = AFMT_I2S,
	.playback = {
		.stream_name	= "HDMI I2S Playback",
		.channels_min	= 1,
		.channels_max	= 8,
		.rates		= SNDRV_PCM_RATE_CONTINUOUS,
		.rate_min	= 5512,
		.rate_max	= 192000,
		.formats	= TDA998X_FORMATS,

	},
	.ops = &tda_ops,
    },
    {
	.name = "spdif-hifi",
	.id = AFMT_SPDIF,
	.playback = {
		.stream_name	= "HDMI SPDIF Playback",
		.channels_min	= 1,
		.channels_max	= 2,
		.rates		= SNDRV_PCM_RATE_CONTINUOUS,
		.rate_min	= 22050,
		.rate_max	= 192000,
		.formats	= TDA998X_FORMATS,
	},
	.ops = &tda_ops,
    },
};

static const struct snd_soc_dapm_widget tda_widgets[] = {
	SND_SOC_DAPM_OUTPUT("hdmi-out"),
};
static const struct snd_soc_dapm_route tda_routes[] = {
	{ "hdmi-out", NULL, "HDMI I2S Playback" },
	{ "hdmi-out", NULL, "HDMI SPDIF Playback" },
};

static int tda_probe(struct snd_soc_codec *codec)
{
	struct tda_priv *priv;
	struct device_node *np;
	int i, ret;

	priv = devm_kzalloc(codec->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
	snd_soc_codec_set_drvdata(codec, priv);
	priv->codec = codec;

	/* get the audio input ports (I2s and S/PDIF) */
	np = codec->dev->of_node;
	for (i = 0; i < 2; i++) {
		ret = of_property_read_u32_index(np, "audio-ports", i,
						&priv->ports[i]);
		if (ret) {
			if (i == 0)
				dev_err(codec->dev,
					"bad or missing audio-ports\n");
			break;
		}
	}

	return 0;
}

static const struct snd_soc_codec_driver soc_codec_tda998x = {
	.probe = tda_probe,
	.dapm_widgets = tda_widgets,
	.num_dapm_widgets = ARRAY_SIZE(tda_widgets),
	.dapm_routes = tda_routes,
	.num_dapm_routes = ARRAY_SIZE(tda_routes),
};

static int tda998x_dev_probe(struct platform_device *pdev)
{
	struct snd_soc_dai_driver *dai_drv;

	/* copy the DAI driver to a writable area */
	dai_drv = devm_kzalloc(&pdev->dev, sizeof(tda998x_dai), GFP_KERNEL);
	if (!dai_drv)
		return -ENOMEM;
	memcpy(dai_drv, tda998x_dai, sizeof(tda998x_dai));

	return snd_soc_register_codec(&pdev->dev,
				&soc_codec_tda998x,
				dai_drv, ARRAY_SIZE(tda998x_dai));
}

static int tda998x_dev_remove(struct platform_device *pdev)
{
	snd_soc_unregister_codec(&pdev->dev);
	return 0;
}

static const struct of_device_id tda998x_codec_ids[] = {
	{ .compatible = "nxp,tda998x-codec", },
	{ }
};
MODULE_DEVICE_TABLE(of, tda998x_codec_ids);

static struct platform_driver tda998x_driver = {
	.probe		= tda998x_dev_probe,
	.remove		= tda998x_dev_remove,
	.driver		= {
		.name	= "tda998x-codec",
		.owner	= THIS_MODULE,
		.of_match_table = tda998x_codec_ids,
	},
};

module_platform_driver(tda998x_driver);

MODULE_AUTHOR("Jean-Francois Moine");
MODULE_DESCRIPTION("TDA998x codec driver");
MODULE_LICENSE("GPL");
-------------------8<----------------

-- 
Ken ar c'hentañ	|	      ** Breizh ha Linux atav! **
Jef		|		http://moinejf.free.fr/
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel

  reply	other threads:[~2014-01-24 16:54 UTC|newest]

Thread overview: 59+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-19 12:12 [PATCH RFC 0/9] Beaglebone-Black HDMI audio Jyri Sarha
2013-11-19 12:12 ` [PATCH RFC 1/9] clk: add gpio controlled clock Jyri Sarha
2013-11-19 12:12 ` [PATCH RFC 2/9] ASoC: davinci-evm: Add named clock reference to DT bindings Jyri Sarha
2013-11-19 18:18   ` Mark Brown
2013-11-19 12:12 ` [PATCH RFC 3/9] ASoC: davinci-evm: HDMI audio support for TDA998x trough McASP I2S bus Jyri Sarha
2013-11-19 15:07   ` Thomas Petazzoni
2013-11-19 12:12 ` [PATCH RFC 4/9] ASoC: hdmi-codec: Add devicetree binding with documentation Jyri Sarha
2013-11-19 17:50   ` Mark Brown
2013-11-20  9:23   ` Jean-Francois Moine
2013-11-20 10:09     ` Mark Brown
2013-11-20 12:34       ` Jean-Francois Moine
2013-11-20 13:33         ` Mark Brown
2013-11-19 12:12 ` [PATCH RFC 5/9] ASoC: hdmi-codec: Add SNDRV_PCM_FMTBIT_32_LE playback format Jyri Sarha
2013-11-19 18:21   ` Mark Brown
2013-11-19 12:12 ` [PATCH RFC 6/9] ASoC: davinci: HDMI audio build for AM33XX and TDA998x Jyri Sarha
2013-11-19 12:12 ` [PATCH RFC 7/9] drm/tilcdc: Add I2C HDMI audio config for tda998x Jyri Sarha
2013-11-19 12:12 ` [PATCH RFC 8/9] ARM: OMAP2+: omap2plus_defconfig: Enable tilcdc and TDA998X HDMI support Jyri Sarha
2013-11-19 12:12 ` [PATCH RFC 9/9] ARM: OMAP2+: omap2plus_defconfig: Enable BeagleBone Black HDMI audio support Jyri Sarha
2013-11-19 12:21 ` [PATCH RFC] DTS Changes for Beaglebone-Black HDMI audio Jyri Sarha
2013-11-19 12:21   ` [PATCH RFC] ARM/dts: am335x-boneblack: Add HDMI audio support Jyri Sarha
2013-11-19 13:02     ` Benoit Cousson
2013-11-19 13:29       ` Jyri Sarha
2013-12-08 12:16 ` [RFC v2 0/8] Beaglebone-Black HDMI audio Jyri Sarha
2013-12-08 12:16   ` [RFC v2 1/8] clk: add gpio controlled clock Jyri Sarha
2013-12-08 12:16   ` [RFC v2 2/8] ASoC: davinci-evm: Add named clock reference to DT bindings Jyri Sarha
2013-12-08 12:16   ` [RFC v2 3/8] ASoC: davinci-evm: HDMI audio support for TDA998x trough McASP I2S bus Jyri Sarha
2013-12-08 12:16   ` [RFC v2 4/8] ASoC: hdmi-codec: Add devicetree binding with documentation Jyri Sarha
2013-12-08 12:16   ` [RFC v2 5/8] ASoC: davinci: HDMI audio build for AM33XX and TDA998x Jyri Sarha
2013-12-08 12:16   ` [RFC v2 6/8] drm/tilcdc: Add I2C HDMI audio config for tda998x Jyri Sarha
2013-12-08 12:16   ` [RFC v2 7/8] ARM: OMAP2+: omap2plus_defconfig: Enable tilcdc and TDA998X HDMI support Jyri Sarha
2013-12-08 12:16   ` [RFC v2 8/8] ARM: OMAP2+: omap2plus_defconfig: Enable BeagleBone Black HDMI audio support Jyri Sarha
2013-12-08 12:20 ` [RFC v2] Beaglebone-Black HDMI audio Jyri Sarha
2013-12-08 12:20   ` [RFC v2] ARM/dts: am335x-boneblack: Add HDMI audio support Jyri Sarha
2013-12-20 10:36 ` [PATCH RFC v2 REPOST 0/8] Beaglebone-Black HDMI audio Jyri Sarha
2013-12-20 10:37   ` [PATCH RFC v2 REPOST 1/8] clk: add gpio controlled clock Jyri Sarha
2013-12-20 10:38   ` [PATCH RFC v2 REPOST 2/8] ASoC: davinci-evm: Add named clock reference to DT bindings Jyri Sarha
2013-12-31 13:16     ` Mark Brown
2014-01-15 11:12       ` Jyri Sarha
2013-12-20 10:39   ` [PATCH RFC v2 REPOST 3/8] ASoC: davinci-evm: HDMI audio support for TDA998x trough McASP I2S bus Jyri Sarha
2013-12-31 13:25     ` Mark Brown
2014-01-15 11:27       ` Jyri Sarha
2014-01-15 13:48         ` Anssi Hannula
2014-01-15 16:28           ` Jyri Sarha
2014-01-15 15:51         ` [alsa-devel] " Jean-Francois Moine
2014-01-22  9:20           ` Jyri Sarha
2014-01-22 10:19             ` [alsa-devel] " Jean-Francois Moine
2014-01-22 10:46               ` Jean-Francois Moine
2014-01-24 12:57                 ` Jyri Sarha
2014-01-24 16:54                   ` Jean-Francois Moine [this message]
2014-01-21 19:15         ` Mark Brown
2014-01-24 13:01           ` Jyri Sarha
2013-12-20 10:40   ` [PATCH RFC v2 REPOST 4/8] ASoC: hdmi-codec: Add devicetree binding with documentation Jyri Sarha
2013-12-31 13:26     ` Mark Brown
2013-12-20 10:40   ` [PATCH RFC v2 REPOST 5/8] ASoC: davinci: HDMI audio build for AM33XX and TDA998x Jyri Sarha
2013-12-20 10:41   ` [PATCH RFC v2 REPOST 6/8] drm/tilcdc: Add I2C HDMI audio config for tda998x Jyri Sarha
2013-12-20 10:42   ` [PATCH RFC v2 REPOST 7/8] ARM: OMAP2+: omap2plus_defconfig: Enable tilcdc and TDA998X HDMI support Jyri Sarha
2013-12-20 10:43   ` [PATCH RFC v2 REPOST 8/8] ARM: OMAP2+: omap2plus_defconfig: Enable BeagleBone Black HDMI audio support Jyri Sarha
2013-12-20 11:30   ` [alsa-devel] [PATCH RFC v2 REPOST 0/8] Beaglebone-Black HDMI audio Mark Brown
2013-12-20 11:51     ` Jyri Sarha

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=20140124175431.47a82d57@armhf \
    --to=moinejf@free.fr \
    --cc=alsa-devel@alsa-project.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=jsarha@ti.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-omap@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).