blob: 04c7b3e8562b39b42f688ab41581191295218ba0 [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 <sound/soc-dapm.h>
#include <linux/pinctrl/consumer.h>
#include "../codecs/wm8962.h"
#include "fsl_sai.h"
#define DAI_NAME_SIZE 32
struct imx_tfa9890_data {
struct snd_soc_dai_link dai_link;
struct snd_soc_card card;
};
struct imx_priv {
struct snd_soc_codec *codec;
struct platform_device *pdev;
struct snd_pcm_substream *first_stream;
struct snd_card *snd_card;
};
static struct imx_priv card_priv;
static const struct snd_soc_dapm_widget imx_tfa9890_dapm_widgets[] = {
SND_SOC_DAPM_SPK("Ext Spk", NULL),
};
static int imx_mono_spkr_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 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;
if (!priv->first_stream) {
priv->first_stream = substream;
}
dai_format = SND_SOC_DAIFMT_I2S | 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;
}
ret = snd_soc_dai_set_fmt(cpu_dai, dai_format);
if (ret) {
dev_err(dev, "failed to set codec dai fmt: %d\n", ret);
return ret;
}
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 */
dev_err(dev, "Unhandled format\n");
break;
}
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: %d\n", ret);
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: %d\n", ret);
return ret;
}
return 0;
}
static int imx_mono_spkr_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 snd_soc_dai *cpu_dai = rtd->cpu_dai;
struct imx_priv *priv = &card_priv;
struct device *dev = &priv->pdev->dev;
int ret;
/* We don't need to handle anything if there's no substream running */
if (!priv->first_stream)
return 0;
if (!priv->first_stream) {
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
{
snd_soc_dai_digital_mute(codec_dai, 1, substream->stream);
snd_soc_dai_digital_mute(cpu_dai, 1, substream->stream);
}
ret = snd_soc_dai_set_sysclk(codec_dai, FSL_SAI_CLK_MAST1 /*ignored*/,
0, SND_SOC_CLOCK_IN /*ignored*/);
if (ret < 0) {
dev_err(dev, "failed to switch away from FLL: %d\n", ret);
return ret;
}
ret = snd_soc_dai_set_sysclk(cpu_dai, FSL_SAI_CLK_MAST1,
0, SND_SOC_CLOCK_OUT);
if (ret < 0) {
dev_err(dev, "failed to switch away from FLL: %d\n", ret);
return ret;
}
}
return 0;
}
static struct snd_soc_ops imx_mono_spkr_ops = {
.hw_params = imx_mono_spkr_hw_params,
.hw_free = imx_mono_spkr_hw_free,
};
static int imx_tfa9890_probe(struct platform_device *pdev)
{
struct device_node *cpu_np, *codec_np;
struct platform_device *cpu_pdev;
struct imx_priv *priv = &card_priv;
struct i2c_client *codec_dev;
struct imx_tfa9890_data *data;
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;
}
codec_np = of_parse_phandle(pdev->dev.of_node, "audio-codec", 0);
if (!codec_np) {
dev_err(&pdev->dev, "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 SAI platform device\n");
ret = -EINVAL;
goto fail;
}
codec_dev = of_find_i2c_device_by_node(codec_np);
if (!codec_dev || !codec_dev->driver) {
dev_err(&pdev->dev, "failed to find codec platform device\n");
ret = -EINVAL;
goto fail;
}
priv->first_stream = NULL;
data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
if (!data) {
ret = -ENOMEM;
goto fail;
}
data->dai_link.name = "tfa9890_codec";
data->dai_link.stream_name = "MONO_SPKR";
data->dai_link.codec_dai_name = "tfa9890_codec";
data->dai_link.codec_of_node = codec_np;
data->dai_link.cpu_dai_name = dev_name(&cpu_pdev->dev);
data->dai_link.platform_of_node = cpu_np;
data->dai_link.ops = &imx_mono_spkr_ops;
data->dai_link.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
SND_SOC_DAIFMT_CBS_CFS;
data->card.dev = &pdev->dev;
ret = snd_soc_of_parse_card_name(&data->card, "model");
if (ret)
goto fail;
data->card.num_links = 1;
data->card.dai_link = &data->dai_link;
data->card.dapm_widgets = imx_tfa9890_dapm_widgets;
data->card.num_dapm_widgets = ARRAY_SIZE(imx_tfa9890_dapm_widgets);
platform_set_drvdata(pdev, &data->card);
snd_soc_card_set_drvdata(&data->card, data);
ret = snd_soc_register_card(&data->card);
if (ret) {
dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
goto fail;
}
priv->snd_card = data->card.snd_card;
fail:
if (cpu_np)
of_node_put(cpu_np);
if (codec_np)
of_node_put(codec_np);
return ret;
}
static int imx_tfa9890_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_tfa9890_dt_ids[] = {
{ .compatible = "fsl,imx-audio-tfa9890", },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, imx_tfa9890_dt_ids);
static struct platform_driver imx_tfa9890_driver = {
.driver = {
.name = "imx-tfa9890",
.owner = THIS_MODULE,
.pm = &snd_soc_pm_ops,
.of_match_table = imx_tfa9890_dt_ids,
},
.probe = imx_tfa9890_probe,
.remove = imx_tfa9890_remove,
};
module_platform_driver(imx_tfa9890_driver);
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("Freescale i.MX TFA9890 ASoC machine driver");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:imx-tfa9890");