16a5f850aSShengjiu Wang // SPDX-License-Identifier: GPL-2.0
26a5f850aSShengjiu Wang // Copyright 2017-2020 NXP
36a5f850aSShengjiu Wang
46a5f850aSShengjiu Wang #include <linux/module.h>
56a5f850aSShengjiu Wang #include <linux/of_platform.h>
66a5f850aSShengjiu Wang #include <sound/jack.h>
76a5f850aSShengjiu Wang #include <sound/pcm_params.h>
86a5f850aSShengjiu Wang #include <sound/hdmi-codec.h>
96a5f850aSShengjiu Wang #include "fsl_sai.h"
106a5f850aSShengjiu Wang
116a5f850aSShengjiu Wang /**
126a5f850aSShengjiu Wang * struct cpu_priv - CPU private data
136a5f850aSShengjiu Wang * @sysclk_id: SYSCLK ids for set_sysclk()
146a5f850aSShengjiu Wang * @slot_width: Slot width of each frame
156a5f850aSShengjiu Wang *
166a5f850aSShengjiu Wang * Note: [1] for tx and [0] for rx
176a5f850aSShengjiu Wang */
186a5f850aSShengjiu Wang struct cpu_priv {
196a5f850aSShengjiu Wang u32 sysclk_id[2];
206a5f850aSShengjiu Wang u32 slot_width;
216a5f850aSShengjiu Wang };
226a5f850aSShengjiu Wang
236a5f850aSShengjiu Wang struct imx_hdmi_data {
246a5f850aSShengjiu Wang struct snd_soc_dai_link dai;
256a5f850aSShengjiu Wang struct snd_soc_card card;
266a5f850aSShengjiu Wang struct snd_soc_jack hdmi_jack;
276a5f850aSShengjiu Wang struct snd_soc_jack_pin hdmi_jack_pin;
286a5f850aSShengjiu Wang struct cpu_priv cpu_priv;
296a5f850aSShengjiu Wang u32 dai_fmt;
306a5f850aSShengjiu Wang };
316a5f850aSShengjiu Wang
imx_hdmi_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)326a5f850aSShengjiu Wang static int imx_hdmi_hw_params(struct snd_pcm_substream *substream,
336a5f850aSShengjiu Wang struct snd_pcm_hw_params *params)
346a5f850aSShengjiu Wang {
356a5f850aSShengjiu Wang struct snd_soc_pcm_runtime *rtd = substream->private_data;
366a5f850aSShengjiu Wang struct imx_hdmi_data *data = snd_soc_card_get_drvdata(rtd->card);
376a5f850aSShengjiu Wang bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
386a5f850aSShengjiu Wang struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
396a5f850aSShengjiu Wang struct snd_soc_card *card = rtd->card;
406a5f850aSShengjiu Wang struct device *dev = card->dev;
416a5f850aSShengjiu Wang u32 slot_width = data->cpu_priv.slot_width;
426a5f850aSShengjiu Wang int ret;
436a5f850aSShengjiu Wang
446a5f850aSShengjiu Wang /* MCLK always is (256 or 192) * rate. */
456a5f850aSShengjiu Wang ret = snd_soc_dai_set_sysclk(cpu_dai, data->cpu_priv.sysclk_id[tx],
466a5f850aSShengjiu Wang 8 * slot_width * params_rate(params),
476a5f850aSShengjiu Wang tx ? SND_SOC_CLOCK_OUT : SND_SOC_CLOCK_IN);
486a5f850aSShengjiu Wang if (ret && ret != -ENOTSUPP) {
496a5f850aSShengjiu Wang dev_err(dev, "failed to set cpu sysclk: %d\n", ret);
506a5f850aSShengjiu Wang return ret;
516a5f850aSShengjiu Wang }
526a5f850aSShengjiu Wang
536a5f850aSShengjiu Wang ret = snd_soc_dai_set_tdm_slot(cpu_dai, 0, 0, 2, slot_width);
546a5f850aSShengjiu Wang if (ret && ret != -ENOTSUPP) {
556a5f850aSShengjiu Wang dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
566a5f850aSShengjiu Wang return ret;
576a5f850aSShengjiu Wang }
586a5f850aSShengjiu Wang
596a5f850aSShengjiu Wang return 0;
606a5f850aSShengjiu Wang }
616a5f850aSShengjiu Wang
62815b55e1SRikard Falkeborn static const struct snd_soc_ops imx_hdmi_ops = {
636a5f850aSShengjiu Wang .hw_params = imx_hdmi_hw_params,
646a5f850aSShengjiu Wang };
656a5f850aSShengjiu Wang
666a5f850aSShengjiu Wang static const struct snd_soc_dapm_widget imx_hdmi_widgets[] = {
676a5f850aSShengjiu Wang SND_SOC_DAPM_LINE("HDMI Jack", NULL),
686a5f850aSShengjiu Wang };
696a5f850aSShengjiu Wang
imx_hdmi_init(struct snd_soc_pcm_runtime * rtd)706a5f850aSShengjiu Wang static int imx_hdmi_init(struct snd_soc_pcm_runtime *rtd)
716a5f850aSShengjiu Wang {
726a5f850aSShengjiu Wang struct snd_soc_card *card = rtd->card;
736a5f850aSShengjiu Wang struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
746a5f850aSShengjiu Wang struct snd_soc_component *component = codec_dai->component;
756a5f850aSShengjiu Wang struct imx_hdmi_data *data = snd_soc_card_get_drvdata(card);
766a5f850aSShengjiu Wang int ret;
776a5f850aSShengjiu Wang
786a5f850aSShengjiu Wang data->hdmi_jack_pin.pin = "HDMI Jack";
796a5f850aSShengjiu Wang data->hdmi_jack_pin.mask = SND_JACK_LINEOUT;
806a5f850aSShengjiu Wang /* enable jack detection */
8119aed2d6SAkihiko Odaki ret = snd_soc_card_jack_new_pins(card, "HDMI Jack", SND_JACK_LINEOUT,
8219aed2d6SAkihiko Odaki &data->hdmi_jack,
8319aed2d6SAkihiko Odaki &data->hdmi_jack_pin, 1);
846a5f850aSShengjiu Wang if (ret) {
856a5f850aSShengjiu Wang dev_err(card->dev, "Can't new HDMI Jack %d\n", ret);
866a5f850aSShengjiu Wang return ret;
876a5f850aSShengjiu Wang }
886a5f850aSShengjiu Wang
896a5f850aSShengjiu Wang ret = snd_soc_component_set_jack(component, &data->hdmi_jack, NULL);
902a0435dfSStephan Gerhold if (ret && ret != -ENOTSUPP) {
916a5f850aSShengjiu Wang dev_err(card->dev, "Can't set HDMI Jack %d\n", ret);
926a5f850aSShengjiu Wang return ret;
936a5f850aSShengjiu Wang }
946a5f850aSShengjiu Wang
956a5f850aSShengjiu Wang return 0;
966a5f850aSShengjiu Wang };
976a5f850aSShengjiu Wang
imx_hdmi_probe(struct platform_device * pdev)986a5f850aSShengjiu Wang static int imx_hdmi_probe(struct platform_device *pdev)
996a5f850aSShengjiu Wang {
1006a5f850aSShengjiu Wang struct device_node *np = pdev->dev.of_node;
1016a5f850aSShengjiu Wang bool hdmi_out = of_property_read_bool(np, "hdmi-out");
1026a5f850aSShengjiu Wang bool hdmi_in = of_property_read_bool(np, "hdmi-in");
1036a5f850aSShengjiu Wang struct snd_soc_dai_link_component *dlc;
1046a5f850aSShengjiu Wang struct platform_device *cpu_pdev;
1056a5f850aSShengjiu Wang struct device_node *cpu_np;
1066a5f850aSShengjiu Wang struct imx_hdmi_data *data;
1076a5f850aSShengjiu Wang int ret;
1086a5f850aSShengjiu Wang
1096a5f850aSShengjiu Wang dlc = devm_kzalloc(&pdev->dev, 3 * sizeof(*dlc), GFP_KERNEL);
1106a5f850aSShengjiu Wang if (!dlc)
1116a5f850aSShengjiu Wang return -ENOMEM;
1126a5f850aSShengjiu Wang
1136a5f850aSShengjiu Wang cpu_np = of_parse_phandle(np, "audio-cpu", 0);
1146a5f850aSShengjiu Wang if (!cpu_np) {
1156a5f850aSShengjiu Wang dev_err(&pdev->dev, "cpu dai phandle missing or invalid\n");
1166a5f850aSShengjiu Wang ret = -EINVAL;
1176a5f850aSShengjiu Wang goto fail;
1186a5f850aSShengjiu Wang }
1196a5f850aSShengjiu Wang
1206a5f850aSShengjiu Wang cpu_pdev = of_find_device_by_node(cpu_np);
1216a5f850aSShengjiu Wang if (!cpu_pdev) {
1226a5f850aSShengjiu Wang dev_err(&pdev->dev, "failed to find SAI platform device\n");
1236a5f850aSShengjiu Wang ret = -EINVAL;
1246a5f850aSShengjiu Wang goto fail;
1256a5f850aSShengjiu Wang }
1266a5f850aSShengjiu Wang
1276a5f850aSShengjiu Wang data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
1286a5f850aSShengjiu Wang if (!data) {
1296a5f850aSShengjiu Wang ret = -ENOMEM;
130ed46731dSMiaoqian Lin put_device(&cpu_pdev->dev);
1316a5f850aSShengjiu Wang goto fail;
1326a5f850aSShengjiu Wang }
1336a5f850aSShengjiu Wang
1346a5f850aSShengjiu Wang data->dai.cpus = &dlc[0];
1356a5f850aSShengjiu Wang data->dai.num_cpus = 1;
1366a5f850aSShengjiu Wang data->dai.platforms = &dlc[1];
1376a5f850aSShengjiu Wang data->dai.num_platforms = 1;
1386a5f850aSShengjiu Wang data->dai.codecs = &dlc[2];
1396a5f850aSShengjiu Wang data->dai.num_codecs = 1;
1406a5f850aSShengjiu Wang
1416a5f850aSShengjiu Wang data->dai.name = "i.MX HDMI";
1426a5f850aSShengjiu Wang data->dai.stream_name = "i.MX HDMI";
1436a5f850aSShengjiu Wang data->dai.cpus->dai_name = dev_name(&cpu_pdev->dev);
1446a5f850aSShengjiu Wang data->dai.platforms->of_node = cpu_np;
1456a5f850aSShengjiu Wang data->dai.ops = &imx_hdmi_ops;
1466a5f850aSShengjiu Wang data->dai.playback_only = true;
1476a5f850aSShengjiu Wang data->dai.capture_only = false;
1486a5f850aSShengjiu Wang data->dai.init = imx_hdmi_init;
1496a5f850aSShengjiu Wang
150f670b274SYe Guojin put_device(&cpu_pdev->dev);
151f670b274SYe Guojin
1526a5f850aSShengjiu Wang if (of_node_name_eq(cpu_np, "sai")) {
1536a5f850aSShengjiu Wang data->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
1546a5f850aSShengjiu Wang data->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
1556a5f850aSShengjiu Wang }
1566a5f850aSShengjiu Wang
1576a5f850aSShengjiu Wang if (of_device_is_compatible(np, "fsl,imx-audio-sii902x")) {
1586a5f850aSShengjiu Wang data->dai_fmt = SND_SOC_DAIFMT_LEFT_J;
1596a5f850aSShengjiu Wang data->cpu_priv.slot_width = 24;
1606a5f850aSShengjiu Wang } else {
1616a5f850aSShengjiu Wang data->dai_fmt = SND_SOC_DAIFMT_I2S;
1626a5f850aSShengjiu Wang data->cpu_priv.slot_width = 32;
1636a5f850aSShengjiu Wang }
1646a5f850aSShengjiu Wang
1656a5f850aSShengjiu Wang if ((hdmi_out && hdmi_in) || (!hdmi_out && !hdmi_in)) {
1666a5f850aSShengjiu Wang dev_err(&pdev->dev, "Invalid HDMI DAI link\n");
167acd894aeSshengjiu wang ret = -EINVAL;
1686a5f850aSShengjiu Wang goto fail;
1696a5f850aSShengjiu Wang }
1706a5f850aSShengjiu Wang
1716a5f850aSShengjiu Wang if (hdmi_out) {
1726a5f850aSShengjiu Wang data->dai.playback_only = true;
1736a5f850aSShengjiu Wang data->dai.capture_only = false;
1746a5f850aSShengjiu Wang data->dai.codecs->dai_name = "i2s-hifi";
1756a5f850aSShengjiu Wang data->dai.codecs->name = "hdmi-audio-codec.1";
1766a5f850aSShengjiu Wang data->dai.dai_fmt = data->dai_fmt |
1776a5f850aSShengjiu Wang SND_SOC_DAIFMT_NB_NF |
178a90f847aSMark Brown SND_SOC_DAIFMT_CBC_CFC;
1796a5f850aSShengjiu Wang }
1806a5f850aSShengjiu Wang
1816a5f850aSShengjiu Wang if (hdmi_in) {
1826a5f850aSShengjiu Wang data->dai.playback_only = false;
1836a5f850aSShengjiu Wang data->dai.capture_only = true;
1846a5f850aSShengjiu Wang data->dai.codecs->dai_name = "i2s-hifi";
1856a5f850aSShengjiu Wang data->dai.codecs->name = "hdmi-audio-codec.2";
1866a5f850aSShengjiu Wang data->dai.dai_fmt = data->dai_fmt |
1876a5f850aSShengjiu Wang SND_SOC_DAIFMT_NB_NF |
188a90f847aSMark Brown SND_SOC_DAIFMT_CBP_CFP;
1896a5f850aSShengjiu Wang }
1906a5f850aSShengjiu Wang
1916a5f850aSShengjiu Wang data->card.dapm_widgets = imx_hdmi_widgets;
1926a5f850aSShengjiu Wang data->card.num_dapm_widgets = ARRAY_SIZE(imx_hdmi_widgets);
1936a5f850aSShengjiu Wang data->card.dev = &pdev->dev;
1946a5f850aSShengjiu Wang data->card.owner = THIS_MODULE;
1956a5f850aSShengjiu Wang ret = snd_soc_of_parse_card_name(&data->card, "model");
1966a5f850aSShengjiu Wang if (ret)
1976a5f850aSShengjiu Wang goto fail;
1986a5f850aSShengjiu Wang
1996a5f850aSShengjiu Wang data->card.num_links = 1;
2006a5f850aSShengjiu Wang data->card.dai_link = &data->dai;
2016a5f850aSShengjiu Wang
2026a5f850aSShengjiu Wang snd_soc_card_set_drvdata(&data->card, data);
2036a5f850aSShengjiu Wang ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
2046a5f850aSShengjiu Wang if (ret) {
205*362e8d0fSAlexander Stein dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
2066a5f850aSShengjiu Wang goto fail;
2076a5f850aSShengjiu Wang }
2086a5f850aSShengjiu Wang
2096a5f850aSShengjiu Wang fail:
2106a5f850aSShengjiu Wang of_node_put(cpu_np);
2116a5f850aSShengjiu Wang
2126a5f850aSShengjiu Wang return ret;
2136a5f850aSShengjiu Wang }
2146a5f850aSShengjiu Wang
2156a5f850aSShengjiu Wang static const struct of_device_id imx_hdmi_dt_ids[] = {
2166a5f850aSShengjiu Wang { .compatible = "fsl,imx-audio-hdmi", },
2176a5f850aSShengjiu Wang { .compatible = "fsl,imx-audio-sii902x", },
2186a5f850aSShengjiu Wang { /* sentinel */ }
2196a5f850aSShengjiu Wang };
2206a5f850aSShengjiu Wang MODULE_DEVICE_TABLE(of, imx_hdmi_dt_ids);
2216a5f850aSShengjiu Wang
2226a5f850aSShengjiu Wang static struct platform_driver imx_hdmi_driver = {
2236a5f850aSShengjiu Wang .driver = {
2246a5f850aSShengjiu Wang .name = "imx-hdmi",
2256a5f850aSShengjiu Wang .pm = &snd_soc_pm_ops,
2266a5f850aSShengjiu Wang .of_match_table = imx_hdmi_dt_ids,
2276a5f850aSShengjiu Wang },
2286a5f850aSShengjiu Wang .probe = imx_hdmi_probe,
2296a5f850aSShengjiu Wang };
2306a5f850aSShengjiu Wang module_platform_driver(imx_hdmi_driver);
2316a5f850aSShengjiu Wang
2326a5f850aSShengjiu Wang MODULE_AUTHOR("Freescale Semiconductor, Inc.");
2336a5f850aSShengjiu Wang MODULE_DESCRIPTION("Freescale i.MX hdmi audio ASoC machine driver");
2346a5f850aSShengjiu Wang MODULE_LICENSE("GPL v2");
2356a5f850aSShengjiu Wang MODULE_ALIAS("platform:imx-hdmi");
236