blob: 40702e0af9a3356cc33fe644f449d0f156cda2a1 [file] [log] [blame]
/*
* Copyright (C) 2014 Freescale Semiconductor, Inc.
*
* Based on imx-wm8962.c
* Copyright (C) 2012 Freescale Semiconductor, Inc.
* Copyright (C) 2012 Linaro Ltd.
*
* The code contained herein is licensed under the GNU General Public
* License. You may obtain a copy of the GNU General Public License
* Version 2 or later at the following locations:
*
* http://www.opensource.org/licenses/gpl-license.html
* http://www.gnu.org/copyleft/gpl.html
*/
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/of_i2c.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/clk.h>
#include <sound/soc.h>
#include <sound/jack.h>
#include <sound/control.h>
#include <sound/pcm_params.h>
#include <linux/pinctrl/consumer.h>
#include <linux/spi/spi.h>
#include "../codecs/tfa98xx-core.h"
#include "../codecs/wm8940.h"
#include "fsl_sai.h"
#define DAI_NAME_SIZE 32
struct imx_priv {
struct snd_soc_card card;
struct snd_soc_dai *spkr_codec_dai;
struct platform_device *pdev;
};
static struct imx_priv card_priv;
static int pll_clock_rate(snd_pcm_format_t sample_format, unsigned int sample_rate)
{
u32 pll_out;
switch(sample_format)
{
case SNDRV_PCM_FORMAT_S16_LE:
case SNDRV_PCM_FORMAT_S16_BE:
case SNDRV_PCM_FORMAT_U16_LE:
case SNDRV_PCM_FORMAT_U16_BE:
pll_out = 32 * sample_rate;
break;
case SNDRV_PCM_FORMAT_S24_LE:
case SNDRV_PCM_FORMAT_S24_BE:
case SNDRV_PCM_FORMAT_U24_LE:
case SNDRV_PCM_FORMAT_U24_BE:
pll_out = 48 * sample_rate;
break;
case SNDRV_PCM_FORMAT_S32_LE:
case SNDRV_PCM_FORMAT_S32_BE:
case SNDRV_PCM_FORMAT_U32_LE:
case SNDRV_PCM_FORMAT_U32_BE:
pll_out = 64 * sample_rate;
break;
default:
pll_out = 0; /* Just to get rid of the warning during compilation */
break;
}
return pll_out;
}
static int imx_audio_fe_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_dai *cpu_dai = rtd->cpu_dai;
struct imx_priv *priv = &card_priv;
struct device *dev = &priv->pdev->dev;
unsigned int sample_rate = params_rate(params);
snd_pcm_format_t sample_format = params_format(params);
u32 dai_format, pll_out;
int ret = 0;
dai_format = SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF |
SND_SOC_DAIFMT_CBS_CFS;
ret = snd_soc_dai_set_fmt(cpu_dai, dai_format);
if (ret) {
dev_err(dev, "failed to set cpu dai fmt: %d\n", ret);
return ret;
}
pll_out = pll_clock_rate(sample_format, sample_rate);
if (!pll_out) {
dev_err(dev, "failed to get pll clock rate\n");
return ret;
}
ret = snd_soc_dai_set_sysclk(cpu_dai, FSL_SAI_CLK_MAST1,
pll_out, SND_SOC_CLOCK_OUT);
if (ret) {
dev_err(dev, "failed to set SYSCLK on cpu: %d\n", ret);
return ret;
}
return 0;
}
static int imx_audio_be_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_dai *codec_dai = rtd->codec_dai;
struct imx_priv *priv = &card_priv;
struct device *dev = &priv->pdev->dev;
unsigned int sample_rate = params_rate(params);
snd_pcm_format_t sample_format = params_format(params);
u32 dai_format, pll_out;
int ret = 0;
dai_format = SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF |
SND_SOC_DAIFMT_CBS_CFS;
/* set codec DAI configuration */
ret = snd_soc_dai_set_fmt(codec_dai, dai_format);
if (ret) {
dev_err(dev, "failed to set codec dai fmt: %d\n", ret);
return ret;
}
pll_out = pll_clock_rate(sample_format, sample_rate);
if (!pll_out) {
dev_err(dev, "failed to get pll clock rate\n");
return ret;
}
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
ret = snd_soc_dai_set_sysclk(codec_dai, FSL_SAI_CLK_MAST1 /* ignored */,
pll_out, SND_SOC_CLOCK_IN /* ignored */);
if (ret) {
dev_err(dev, "failed to set SYSCLK on codec: %d\n", ret);
return ret;
}
priv->spkr_codec_dai = codec_dai;
} else {
/* Set the PLL required for the codec to operate */
ret = snd_soc_dai_set_pll(codec_dai, 0 /* ignored */,
0 /* ignored */, 24576000 /*24.576 MHz */,
256 * sample_rate);
if (ret) {
dev_err(dev, "failed to set PLL on codec: %d\n", ret);
return ret;
}
}
return 0;
}
static int imx_audio_fe_hw_free(struct snd_pcm_substream *substream)
{
struct imx_priv *priv = &card_priv;
struct device *dev = &priv->pdev->dev;
struct snd_soc_dai *spkr_codec_dai = priv->spkr_codec_dai;
int ret = 0;
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
if (spkr_codec_dai) {
/* Speaker codec must be muted before the I2S clocks are
* turned off. The I2S clocks are supplied by the CPU
* and turned off during hw_free operation. So store a
* handle to the spkr_codec_dai in hw_params and use
* that here to turn off the speaker amplifier
*/
ret = snd_soc_dai_digital_mute(spkr_codec_dai, 1, substream->stream);
if (ret) {
dev_err(dev, "failed to mute speaker codec: %d\n", ret);
return ret;
}
priv->spkr_codec_dai = NULL;
}
}
return 0;
}
static int imx_audio_be_hw_free(struct snd_pcm_substream *substream)
{
struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct snd_soc_dai *codec_dai = rtd->codec_dai;
struct imx_priv *priv = &card_priv;
struct device *dev = &priv->pdev->dev;
int ret = 0;
if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
/* Set the PLL required for the codec to operate */
ret = snd_soc_dai_set_pll(codec_dai, 0 /* ignored */,
0 /* ignored */, 0,
0);
if (ret) {
dev_err(dev, "failed to set PLL on codec: %d\n", ret);
return ret;
}
}
return 0;
}
static struct snd_soc_ops imx_audio_fe_ops = {
.hw_params = imx_audio_fe_hw_params,
.hw_free = imx_audio_fe_hw_free,
};
static struct snd_soc_ops imx_audio_be_ops = {
.hw_params = imx_audio_be_hw_params,
.hw_free = imx_audio_be_hw_free,
};
/* dai link definitions */
#define SPKR_DAI_FE_LINK 0
#define MIC_DAI_FE_LINK 1
#define SPKR_DAI_BE_LINK 2
#define MIC_DAI_BE_LINK 3
static struct snd_soc_dai_link imx_audio_dai_link[] = {
/* Speaker DAI FE Link */
{
.name = "Class-D FE",
.stream_name = "Class-D FE",
.codec_name = "snd-soc-dummy",
.codec_dai_name = "snd-soc-dummy-dai",
.ops = &imx_audio_fe_ops,
.dai_fmt = SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS,
.dynamic = 1,
.ignore_pmdown_time = 1,
},
/* Mic DAI FE Link */
{
.name = "HiFi FE",
.stream_name = "HiFi FE",
.codec_name = "snd-soc-dummy",
.codec_dai_name = "snd-soc-dummy-dai",
.ops = &imx_audio_fe_ops,
.dai_fmt = SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS,
.dynamic = 1,
.ignore_pmdown_time = 1,
},
/* Speaker DAI BE Link */
{
.name = "Class-D BE",
.stream_name = "Class-D BE",
.platform_name = "snd-soc-dummy",
.cpu_dai_name = "cs48l10-dai0",
.codec_dai_name = "tfa98xx_codec",
.ops = &imx_audio_be_ops,
.dai_fmt = SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS,
.no_pcm = 1,
.ignore_pmdown_time = 1,
},
/* Mic DAI BE Link */
{
.name = "HiFi BE",
.stream_name = "HiFi BE",
.platform_name = "snd-soc-dummy",
.cpu_dai_name = "cs48l10-dao1",
.codec_dai_name = "wm8940-hifi",
.ops = &imx_audio_be_ops,
.dai_fmt = SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS,
.no_pcm = 1,
.ignore_pmdown_time = 1,
},
};
static const struct snd_soc_dapm_widget imx_audio_dapm_widgets[] = {
SND_SOC_DAPM_MIC("AMic", NULL),
SND_SOC_DAPM_SPK("Ext Spkr", NULL),
};
static const struct snd_soc_dapm_route imx_audio_routes[] = {
/* Connections to Mic */
{"MICP", NULL, "Mic Bias"},
{"Mic Bias", NULL, "AMic"},
/* Connections to the CS48L10 DSP - Capture path */
{"DAI1 Playback", NULL, "HiFi Capture"},
{"SAI Capture", NULL, "DAO1 Capture"},
/* Connections to the CS48L10 DSP - Playback path */
{"DAI0 Playback", NULL, "SAI Playback"},
{"Ext Spkr", NULL, "DAO0 Capture"},
};
static int dev_node_match(struct device *dev, void *data)
{
return dev->of_node == data;
}
struct spi_device *find_spi_device_by_node(struct device_node *np)
{
struct device *dev;
dev = bus_find_device(&spi_bus_type, NULL, np, dev_node_match);
return dev ? to_spi_device(dev) : NULL;
}
static int imx_audio_probe(struct platform_device *pdev)
{
struct device_node *cpu_np, *spkr_codec_np, *mic_codec_np, *dsp_np;
struct platform_device *cpu_pdev;
struct spi_device *dsp_dev;
struct imx_priv *priv = &card_priv;
struct i2c_client *spkr_codec_dev, *mic_codec_dev;
int ret;
priv->pdev = pdev;
cpu_np = of_parse_phandle(pdev->dev.of_node, "cpu-dai", 0);
if (!cpu_np) {
dev_err(&pdev->dev, "cpu dai phandle missing or invalid\n");
ret = -EINVAL;
goto fail;
}
cpu_pdev = of_find_device_by_node(cpu_np);
if (!cpu_pdev) {
dev_err(&pdev->dev, "failed to find platform device\n");
ret = -EINVAL;
goto fail;
}
dsp_np = of_parse_phandle(pdev->dev.of_node, "dsp", 0);
if (!dsp_np) {
dev_err(&pdev->dev, "speaker codec phandle missing or invalid\n");
ret = -EINVAL;
goto fail;
}
spkr_codec_np = of_parse_phandle(pdev->dev.of_node, "spkr-codec", 0);
if (!spkr_codec_np) {
dev_err(&pdev->dev, "speaker codec phandle missing or invalid\n");
ret = -EINVAL;
goto fail;
}
mic_codec_np = of_parse_phandle(pdev->dev.of_node, "mic-codec", 0);
if (!mic_codec_np) {
dev_err(&pdev->dev, "mic codec phandle missing or invalid\n");
ret = -EINVAL;
goto fail;
}
dsp_dev = find_spi_device_by_node(dsp_np);
if (!dsp_dev) {
dev_err(&pdev->dev, "failed to find dsp device\n");
ret = -EINVAL;
goto fail;
}
spkr_codec_dev = of_find_i2c_device_by_node(spkr_codec_np);
if (!spkr_codec_dev || !spkr_codec_dev->driver) {
dev_err(&pdev->dev, "failed to find speaker codec device\n");
ret = -EINVAL;
goto fail;
}
mic_codec_dev = of_find_i2c_device_by_node(mic_codec_np);
if (!mic_codec_dev || !mic_codec_dev->driver) {
dev_err(&pdev->dev, "failed to find mic codec device\n");
ret = -EINVAL;
goto fail;
}
/* Setup the dai links */
imx_audio_dai_link[MIC_DAI_FE_LINK].cpu_dai_name = dev_name(&cpu_pdev->dev);
imx_audio_dai_link[MIC_DAI_FE_LINK].platform_of_node = cpu_np;
imx_audio_dai_link[MIC_DAI_BE_LINK].codec_of_node = mic_codec_np;
imx_audio_dai_link[SPKR_DAI_FE_LINK].cpu_dai_name = dev_name(&cpu_pdev->dev);
imx_audio_dai_link[SPKR_DAI_FE_LINK].platform_of_node = cpu_np;
imx_audio_dai_link[SPKR_DAI_BE_LINK].codec_of_node = spkr_codec_np;
priv->card.dev = &pdev->dev;
ret = snd_soc_of_parse_card_name(&priv->card, "model");
if (ret)
goto fail;
priv->card.num_links = ARRAY_SIZE(imx_audio_dai_link);
priv->card.dai_link = imx_audio_dai_link;
/* Add widgets */
priv->card.dapm_widgets = imx_audio_dapm_widgets;
priv->card.num_dapm_widgets = ARRAY_SIZE(imx_audio_dapm_widgets);
/* Add routes */
priv->card.dapm_routes = imx_audio_routes;
priv->card.num_dapm_routes = ARRAY_SIZE(imx_audio_routes);
platform_set_drvdata(pdev, &priv->card);
ret = snd_soc_register_card(&priv->card);
if (ret) {
dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
goto fail;
}
fail:
if (cpu_np)
of_node_put(cpu_np);
if (dsp_np)
of_node_put(dsp_np);
if (spkr_codec_np)
of_node_put(spkr_codec_np);
if (mic_codec_np)
of_node_put(mic_codec_np);
return ret;
}
static int imx_audio_remove(struct platform_device *pdev)
{
struct snd_soc_card *card = platform_get_drvdata(pdev);
snd_soc_unregister_card(card);
return 0;
}
static const struct of_device_id imx_audio_dt_ids[] = {
{ .compatible = "fsl,imx-audio", },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, imx_audio_dt_ids);
static struct platform_driver imx_audio_driver = {
.driver = {
.name = "imx-audio",
.owner = THIS_MODULE,
.pm = &snd_soc_pm_ops,
.of_match_table = imx_audio_dt_ids,
},
.probe = imx_audio_probe,
.remove = imx_audio_remove,
};
module_platform_driver(imx_audio_driver);
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("Freescale i.MX Audio ASoC machine driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:imx-audio");