Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright 2017-2020 NXP
0003 
0004 #include <linux/module.h>
0005 #include <linux/of_platform.h>
0006 #include <sound/jack.h>
0007 #include <sound/pcm_params.h>
0008 #include <sound/hdmi-codec.h>
0009 #include "fsl_sai.h"
0010 
0011 /**
0012  * struct cpu_priv - CPU private data
0013  * @sysclk_id: SYSCLK ids for set_sysclk()
0014  * @slot_width: Slot width of each frame
0015  *
0016  * Note: [1] for tx and [0] for rx
0017  */
0018 struct cpu_priv {
0019     u32 sysclk_id[2];
0020     u32 slot_width;
0021 };
0022 
0023 struct imx_hdmi_data {
0024     struct snd_soc_dai_link dai;
0025     struct snd_soc_card card;
0026     struct snd_soc_jack hdmi_jack;
0027     struct snd_soc_jack_pin hdmi_jack_pin;
0028     struct cpu_priv cpu_priv;
0029     u32 dai_fmt;
0030 };
0031 
0032 static int imx_hdmi_hw_params(struct snd_pcm_substream *substream,
0033                   struct snd_pcm_hw_params *params)
0034 {
0035     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0036     struct imx_hdmi_data *data = snd_soc_card_get_drvdata(rtd->card);
0037     bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
0038     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0039     struct snd_soc_card *card = rtd->card;
0040     struct device *dev = card->dev;
0041     u32 slot_width = data->cpu_priv.slot_width;
0042     int ret;
0043 
0044     /* MCLK always is (256 or 192) * rate. */
0045     ret = snd_soc_dai_set_sysclk(cpu_dai, data->cpu_priv.sysclk_id[tx],
0046                      8 * slot_width * params_rate(params),
0047                      tx ? SND_SOC_CLOCK_OUT : SND_SOC_CLOCK_IN);
0048     if (ret && ret != -ENOTSUPP) {
0049         dev_err(dev, "failed to set cpu sysclk: %d\n", ret);
0050         return ret;
0051     }
0052 
0053     ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0, 0, 2, slot_width);
0054     if (ret && ret != -ENOTSUPP) {
0055         dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
0056         return ret;
0057     }
0058 
0059     return 0;
0060 }
0061 
0062 static const struct snd_soc_ops imx_hdmi_ops = {
0063     .hw_params = imx_hdmi_hw_params,
0064 };
0065 
0066 static const struct snd_soc_dapm_widget imx_hdmi_widgets[] = {
0067     SND_SOC_DAPM_LINE("HDMI Jack", NULL),
0068 };
0069 
0070 static int imx_hdmi_init(struct snd_soc_pcm_runtime *rtd)
0071 {
0072     struct snd_soc_card *card = rtd->card;
0073     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0074     struct snd_soc_component *component = codec_dai->component;
0075     struct imx_hdmi_data *data = snd_soc_card_get_drvdata(card);
0076     int ret;
0077 
0078     data->hdmi_jack_pin.pin = "HDMI Jack";
0079     data->hdmi_jack_pin.mask = SND_JACK_LINEOUT;
0080     /* enable jack detection */
0081     ret = snd_soc_card_jack_new_pins(card, "HDMI Jack", SND_JACK_LINEOUT,
0082                      &data->hdmi_jack,
0083                      &data->hdmi_jack_pin, 1);
0084     if (ret) {
0085         dev_err(card->dev, "Can't new HDMI Jack %d\n", ret);
0086         return ret;
0087     }
0088 
0089     ret = snd_soc_component_set_jack(component, &data->hdmi_jack, NULL);
0090     if (ret && ret != -ENOTSUPP) {
0091         dev_err(card->dev, "Can't set HDMI Jack %d\n", ret);
0092         return ret;
0093     }
0094 
0095     return 0;
0096 };
0097 
0098 static int imx_hdmi_probe(struct platform_device *pdev)
0099 {
0100     struct device_node *np = pdev->dev.of_node;
0101     bool hdmi_out = of_property_read_bool(np, "hdmi-out");
0102     bool hdmi_in = of_property_read_bool(np, "hdmi-in");
0103     struct snd_soc_dai_link_component *dlc;
0104     struct platform_device *cpu_pdev;
0105     struct device_node *cpu_np;
0106     struct imx_hdmi_data *data;
0107     int ret;
0108 
0109     dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
0110     if (!dlc)
0111         return -ENOMEM;
0112 
0113     cpu_np = of_parse_phandle(np, "audio-cpu", 0);
0114     if (!cpu_np) {
0115         dev_err(&pdev->dev, "cpu dai phandle missing or invalid\n");
0116         ret = -EINVAL;
0117         goto fail;
0118     }
0119 
0120     cpu_pdev = of_find_device_by_node(cpu_np);
0121     if (!cpu_pdev) {
0122         dev_err(&pdev->dev, "failed to find SAI platform device\n");
0123         ret = -EINVAL;
0124         goto fail;
0125     }
0126 
0127     data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
0128     if (!data) {
0129         ret = -ENOMEM;
0130         put_device(&cpu_pdev->dev);
0131         goto fail;
0132     }
0133 
0134     data->dai.cpus = &dlc[0];
0135     data->dai.num_cpus = 1;
0136     data->dai.platforms = &dlc[1];
0137     data->dai.num_platforms = 1;
0138     data->dai.codecs = &dlc[2];
0139     data->dai.num_codecs = 1;
0140 
0141     data->dai.name = "i.MX HDMI";
0142     data->dai.stream_name = "i.MX HDMI";
0143     data->dai.cpus->dai_name = dev_name(&cpu_pdev->dev);
0144     data->dai.platforms->of_node = cpu_np;
0145     data->dai.ops = &imx_hdmi_ops;
0146     data->dai.playback_only = true;
0147     data->dai.capture_only = false;
0148     data->dai.init = imx_hdmi_init;
0149 
0150     put_device(&cpu_pdev->dev);
0151 
0152     if (of_node_name_eq(cpu_np, "sai")) {
0153         data->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
0154         data->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
0155     }
0156 
0157     if (of_device_is_compatible(np, "fsl,imx-audio-sii902x")) {
0158         data->dai_fmt = SND_SOC_DAIFMT_LEFT_J;
0159         data->cpu_priv.slot_width = 24;
0160     } else {
0161         data->dai_fmt = SND_SOC_DAIFMT_I2S;
0162         data->cpu_priv.slot_width = 32;
0163     }
0164 
0165     if ((hdmi_out && hdmi_in) || (!hdmi_out && !hdmi_in)) {
0166         dev_err(&pdev->dev, "Invalid HDMI DAI link\n");
0167         ret = -EINVAL;
0168         goto fail;
0169     }
0170 
0171     if (hdmi_out) {
0172         data->dai.playback_only = true;
0173         data->dai.capture_only = false;
0174         data->dai.codecs->dai_name = "i2s-hifi";
0175         data->dai.codecs->name = "hdmi-audio-codec.1";
0176         data->dai.dai_fmt = data->dai_fmt |
0177                     SND_SOC_DAIFMT_NB_NF |
0178                     SND_SOC_DAIFMT_CBC_CFC;
0179     }
0180 
0181     if (hdmi_in) {
0182         data->dai.playback_only = false;
0183         data->dai.capture_only = true;
0184         data->dai.codecs->dai_name = "i2s-hifi";
0185         data->dai.codecs->name = "hdmi-audio-codec.2";
0186         data->dai.dai_fmt = data->dai_fmt |
0187                     SND_SOC_DAIFMT_NB_NF |
0188                     SND_SOC_DAIFMT_CBP_CFP;
0189     }
0190 
0191     data->card.dapm_widgets = imx_hdmi_widgets;
0192     data->card.num_dapm_widgets = ARRAY_SIZE(imx_hdmi_widgets);
0193     data->card.dev = &pdev->dev;
0194     data->card.owner = THIS_MODULE;
0195     ret = snd_soc_of_parse_card_name(&data->card, "model");
0196     if (ret)
0197         goto fail;
0198 
0199     data->card.num_links = 1;
0200     data->card.dai_link = &data->dai;
0201 
0202     snd_soc_card_set_drvdata(&data->card, data);
0203     ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
0204     if (ret) {
0205         dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
0206         goto fail;
0207     }
0208 
0209 fail:
0210     of_node_put(cpu_np);
0211 
0212     return ret;
0213 }
0214 
0215 static const struct of_device_id imx_hdmi_dt_ids[] = {
0216     { .compatible = "fsl,imx-audio-hdmi", },
0217     { .compatible = "fsl,imx-audio-sii902x", },
0218     { /* sentinel */ }
0219 };
0220 MODULE_DEVICE_TABLE(of, imx_hdmi_dt_ids);
0221 
0222 static struct platform_driver imx_hdmi_driver = {
0223     .driver = {
0224         .name = "imx-hdmi",
0225         .pm = &snd_soc_pm_ops,
0226         .of_match_table = imx_hdmi_dt_ids,
0227     },
0228     .probe = imx_hdmi_probe,
0229 };
0230 module_platform_driver(imx_hdmi_driver);
0231 
0232 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
0233 MODULE_DESCRIPTION("Freescale i.MX hdmi audio ASoC machine driver");
0234 MODULE_LICENSE("GPL v2");
0235 MODULE_ALIAS("platform:imx-hdmi");