| /* |
| * 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/tfa9890-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_codec *codec; |
| struct platform_device *pdev; |
| }; |
| static struct imx_priv card_priv; |
| |
| static int imx_audio_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; |
| |
| 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; |
| } |
| 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; |
| } |
| |
| 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; |
| } |
| } 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; |
| } |
| } |
| 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 struct snd_soc_ops imx_audio_ops = { |
| .hw_params = imx_audio_hw_params, |
| }; |
| |
| /* dai link definitions */ |
| #define SPKR_DAI_LINK 0 |
| #define MIC_DAI_LINK 1 |
| |
| static struct snd_soc_dai_link imx_audio_dai_link[] = { |
| /* Speaker DAI Link */ |
| { |
| .name = "Class-D", |
| .stream_name = "Class-D", |
| .codec_dai_name = "tfa9890_codec", |
| .ops = &imx_audio_ops, |
| .dai_fmt = SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS, |
| }, |
| /* Mic DAI Link */ |
| { |
| .name = "HiFi", |
| .stream_name = "HiFi", |
| .codec_dai_name = "wm8940-hifi", |
| .ops = &imx_audio_ops, |
| .dai_fmt = SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS, |
| }, |
| }; |
| |
| static struct snd_soc_aux_dev cs48l10_aux_devs[] = { |
| { |
| .name = "cs48l10", |
| }, |
| }; |
| |
| 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 */ |
| {"DAO_Mixer", NULL, "ADC"}, |
| {"Capture", NULL, "DAO_Mixer"}, |
| /* Connections to the CS48L10 DSP - Playback path */ |
| {"DAI_Mixer", NULL, "Playback"}, |
| {"Ext Spkr", NULL, "DAI_Mixer"}, |
| }; |
| |
| 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; |
| } |
| cs48l10_aux_devs[0].codec_name = dev_name(&dsp_dev->dev); |
| |
| 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_LINK].cpu_dai_name = dev_name(&cpu_pdev->dev); |
| imx_audio_dai_link[MIC_DAI_LINK].platform_of_node = cpu_np; |
| imx_audio_dai_link[MIC_DAI_LINK].codec_of_node = mic_codec_np; |
| imx_audio_dai_link[SPKR_DAI_LINK].cpu_dai_name = dev_name(&cpu_pdev->dev); |
| imx_audio_dai_link[SPKR_DAI_LINK].platform_of_node = cpu_np; |
| imx_audio_dai_link[SPKR_DAI_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); |
| /* Add Aux devices */ |
| priv->card.aux_dev = cs48l10_aux_devs; |
| priv->card.num_aux_devs = ARRAY_SIZE(cs48l10_aux_devs); |
| |
| 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 (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"); |