Merge tag 'platform-drivers-x86-v6.9-3' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / sound / soc / intel / avs / boards / rt5514.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2023 Intel Corporation. All rights reserved.
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8
9 #include <linux/clk.h>
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"
18 #include "../utils.h"
19
20 #define RT5514_CODEC_DAI        "rt5514-aif1"
21
22 static const struct snd_soc_dapm_widget card_widgets[] = {
23         SND_SOC_DAPM_MIC("DMIC", NULL),
24 };
25
26 static const struct snd_soc_dapm_route card_base_routes[] = {
27         /* DMIC */
28         { "DMIC1L", NULL, "DMIC" },
29         { "DMIC1R", NULL, "DMIC" },
30         { "DMIC2L", NULL, "DMIC" },
31         { "DMIC2R", NULL, "DMIC" },
32 };
33
34 static int avs_rt5514_codec_init(struct snd_soc_pcm_runtime *runtime)
35 {
36         int ret = snd_soc_dapm_ignore_suspend(&runtime->card->dapm, "DMIC");
37
38         if (ret)
39                 dev_err(runtime->dev, "DMIC - Ignore suspend failed = %d\n", ret);
40
41         return ret;
42 }
43
44 static int avs_rt5514_be_fixup(struct snd_soc_pcm_runtime *runtime,
45                                struct snd_pcm_hw_params *params)
46 {
47         struct snd_interval *rate, *channels;
48         struct snd_mask *fmt;
49
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);
53
54         rate->min = rate->max = 48000;
55         channels->min = channels->max = 4;
56
57         snd_mask_none(fmt);
58         snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
59
60         return 0;
61 }
62
63 static int avs_rt5514_hw_params(struct snd_pcm_substream *substream,
64                                 struct snd_pcm_hw_params *params)
65 {
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);
68         int ret;
69
70         ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0, 8, 16);
71         if (ret < 0) {
72                 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
73                 return ret;
74         }
75
76         ret = snd_soc_dai_set_sysclk(codec_dai, RT5514_SCLK_S_MCLK, 24576000, SND_SOC_CLOCK_IN);
77         if (ret < 0)
78                 dev_err(rtd->dev, "set sysclk err: %d\n", ret);
79
80         return ret;
81 }
82
83 static const struct snd_soc_ops avs_rt5514_ops = {
84         .hw_params = avs_rt5514_hw_params,
85 };
86
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)
89 {
90         struct snd_soc_dai_link_component *platform;
91         struct snd_soc_dai_link *dl;
92
93         dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
94         platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
95         if (!dl || !platform)
96                 return -ENOMEM;
97
98         platform->name = platform_name;
99
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)
105                 return -ENOMEM;
106
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)
112                 return -ENOMEM;
113
114         dl->num_cpus = 1;
115         dl->num_codecs = 1;
116         dl->platforms = platform;
117         dl->num_platforms = 1;
118         dl->id = 0;
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;
122         dl->nonatomic = 1;
123         dl->no_pcm = 1;
124         dl->dpcm_capture = 1;
125         dl->ops = &avs_rt5514_ops;
126
127         *dai_link = dl;
128
129         return 0;
130 }
131
132 static int avs_rt5514_probe(struct platform_device *pdev)
133 {
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;
138         const char *pname;
139         int ssp_port, tdm_slot, ret;
140
141         mach = dev_get_platdata(dev);
142         pname = mach->mach_params.platform;
143
144         ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
145         if (ret)
146                 return ret;
147
148         ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
149         if (ret) {
150                 dev_err(dev, "Failed to create dai link: %d", ret);
151                 return ret;
152         }
153
154         card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
155         if (!card)
156                 return -ENOMEM;
157
158         card->name = "avs_rt5514";
159         card->dev = dev;
160         card->owner = THIS_MODULE;
161         card->dai_link = dai_link;
162         card->num_links = 1;
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;
168
169         ret = snd_soc_fixup_dai_links_platform_name(card, pname);
170         if (ret)
171                 return ret;
172
173         return devm_snd_soc_register_card(dev, card);
174 }
175
176 static const struct platform_device_id avs_rt5514_driver_ids[] = {
177         {
178                 .name = "avs_rt5514",
179         },
180         {},
181 };
182 MODULE_DEVICE_TABLE(platform, avs_rt5514_driver_ids);
183
184 static struct platform_driver avs_rt5514_driver = {
185         .probe = avs_rt5514_probe,
186         .driver = {
187                 .name = "avs_rt5514",
188                 .pm = &snd_soc_pm_ops,
189         },
190         .id_table = avs_rt5514_driver_ids,
191 };
192
193 module_platform_driver(avs_rt5514_driver);
194
195 MODULE_DESCRIPTION("Intel rt5514 machine driver");
196 MODULE_LICENSE("GPL");