1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2021-2023 Intel Corporation. All rights reserved.
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
10 #include <linux/input.h>
11 #include <linux/module.h>
12 #include <sound/jack.h>
13 #include <sound/pcm.h>
14 #include <sound/pcm_params.h>
15 #include <sound/soc.h>
16 #include <sound/soc-acpi.h>
17 #include "../../../codecs/rt5514.h"
20 #define RT5514_CODEC_DAI "rt5514-aif1"
22 static const struct snd_soc_dapm_widget card_widgets[] = {
23 SND_SOC_DAPM_MIC("DMIC", NULL),
26 static const struct snd_soc_dapm_route card_base_routes[] = {
28 { "DMIC1L", NULL, "DMIC" },
29 { "DMIC1R", NULL, "DMIC" },
30 { "DMIC2L", NULL, "DMIC" },
31 { "DMIC2R", NULL, "DMIC" },
34 static int avs_rt5514_codec_init(struct snd_soc_pcm_runtime *runtime)
36 int ret = snd_soc_dapm_ignore_suspend(&runtime->card->dapm, "DMIC");
39 dev_err(runtime->dev, "DMIC - Ignore suspend failed = %d\n", ret);
44 static int avs_rt5514_be_fixup(struct snd_soc_pcm_runtime *runtime,
45 struct snd_pcm_hw_params *params)
47 struct snd_interval *rate, *channels;
50 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
51 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
52 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
54 rate->min = rate->max = 48000;
55 channels->min = channels->max = 4;
58 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
63 static int avs_rt5514_hw_params(struct snd_pcm_substream *substream,
64 struct snd_pcm_hw_params *params)
66 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
67 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
70 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0, 8, 16);
72 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
76 ret = snd_soc_dai_set_sysclk(codec_dai, RT5514_SCLK_S_MCLK, 24576000, SND_SOC_CLOCK_IN);
78 dev_err(rtd->dev, "set sysclk err: %d\n", ret);
83 static const struct snd_soc_ops avs_rt5514_ops = {
84 .hw_params = avs_rt5514_hw_params,
87 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
88 int tdm_slot, struct snd_soc_dai_link **dai_link)
90 struct snd_soc_dai_link_component *platform;
91 struct snd_soc_dai_link *dl;
93 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
94 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
98 platform->name = platform_name;
100 dl->name = devm_kasprintf(dev, GFP_KERNEL,
101 AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
102 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
103 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
104 if (!dl->name || !dl->cpus || !dl->codecs)
107 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
108 AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
109 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10EC5514:00");
110 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, RT5514_CODEC_DAI);
111 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
116 dl->platforms = platform;
117 dl->num_platforms = 1;
119 dl->dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
120 dl->init = avs_rt5514_codec_init;
121 dl->be_hw_params_fixup = avs_rt5514_be_fixup;
124 dl->dpcm_capture = 1;
125 dl->ops = &avs_rt5514_ops;
132 static int avs_rt5514_probe(struct platform_device *pdev)
134 struct snd_soc_dai_link *dai_link;
135 struct snd_soc_acpi_mach *mach;
136 struct snd_soc_card *card;
137 struct device *dev = &pdev->dev;
139 int ssp_port, tdm_slot, ret;
141 mach = dev_get_platdata(dev);
142 pname = mach->mach_params.platform;
144 ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
148 ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
150 dev_err(dev, "Failed to create dai link: %d", ret);
154 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
158 card->name = "avs_rt5514";
160 card->owner = THIS_MODULE;
161 card->dai_link = dai_link;
163 card->dapm_widgets = card_widgets;
164 card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
165 card->dapm_routes = card_base_routes;
166 card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
167 card->fully_routed = true;
169 ret = snd_soc_fixup_dai_links_platform_name(card, pname);
173 return devm_snd_soc_register_card(dev, card);
176 static const struct platform_device_id avs_rt5514_driver_ids[] = {
178 .name = "avs_rt5514",
182 MODULE_DEVICE_TABLE(platform, avs_rt5514_driver_ids);
184 static struct platform_driver avs_rt5514_driver = {
185 .probe = avs_rt5514_probe,
187 .name = "avs_rt5514",
188 .pm = &snd_soc_pm_ops,
190 .id_table = avs_rt5514_driver_ids,
193 module_platform_driver(avs_rt5514_driver);
195 MODULE_DESCRIPTION("Intel rt5514 machine driver");
196 MODULE_LICENSE("GPL");