1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 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/dmi.h>
10 #include <linux/module.h>
11 #include <sound/jack.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include "../../../codecs/rt298.h"
17
18 static const struct dmi_system_id kblr_dmi_table[] = {
19 {
20 .matches = {
21 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
22 DMI_MATCH(DMI_BOARD_NAME, "Kabylake R DDR4 RVP"),
23 },
24 },
25 {}
26 };
27
28 static const struct snd_kcontrol_new card_controls[] = {
29 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
30 SOC_DAPM_PIN_SWITCH("Mic Jack"),
31 SOC_DAPM_PIN_SWITCH("Speaker"),
32 };
33
34 static const struct snd_soc_dapm_widget card_widgets[] = {
35 SND_SOC_DAPM_HP("Headphone Jack", NULL),
36 SND_SOC_DAPM_MIC("Mic Jack", NULL),
37 SND_SOC_DAPM_SPK("Speaker", NULL),
38 };
39
40 static const struct snd_soc_dapm_route card_base_routes[] = {
41 /* HP jack connectors - unknown if we have jack detect */
42 {"Headphone Jack", NULL, "HPO Pin"},
43 {"MIC1", NULL, "Mic Jack"},
44
45 {"Speaker", NULL, "SPOR"},
46 {"Speaker", NULL, "SPOL"},
47 };
48
49 static struct snd_soc_jack_pin card_headset_pins[] = {
50 {
51 .pin = "Headphone Jack",
52 .mask = SND_JACK_HEADPHONE,
53 },
54 {
55 .pin = "Mic Jack",
56 .mask = SND_JACK_MICROPHONE,
57 },
58 };
59
avs_rt298_codec_init(struct snd_soc_pcm_runtime * runtime)60 static int avs_rt298_codec_init(struct snd_soc_pcm_runtime *runtime)
61 {
62 struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
63 struct snd_soc_jack_pin *pins;
64 struct snd_soc_jack *jack;
65 struct snd_soc_card *card = runtime->card;
66 int num_pins, ret;
67
68 jack = snd_soc_card_get_drvdata(card);
69 num_pins = ARRAY_SIZE(card_headset_pins);
70
71 pins = devm_kmemdup(card->dev, card_headset_pins, sizeof(*pins) * num_pins, GFP_KERNEL);
72 if (!pins)
73 return -ENOMEM;
74
75 ret = snd_soc_card_jack_new_pins(card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0, jack,
76 pins, num_pins);
77 if (ret)
78 return ret;
79
80 snd_soc_component_set_jack(component, jack, NULL);
81
82 return 0;
83 }
84
avs_rt298_be_fixup(struct snd_soc_pcm_runtime * runtime,struct snd_pcm_hw_params * params)85 static int avs_rt298_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params)
86 {
87 struct snd_interval *rate, *channels;
88 struct snd_mask *fmt;
89
90 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
91 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
92 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
93
94 /* The ADSP will convert the FE rate to 48k, stereo */
95 rate->min = rate->max = 48000;
96 channels->min = channels->max = 2;
97
98 /* set SSP0 to 24 bit */
99 snd_mask_none(fmt);
100 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
101
102 return 0;
103 }
104
105 static int
avs_rt298_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)106 avs_rt298_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
107 {
108 struct snd_soc_pcm_runtime *rtd = substream->private_data;
109 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
110 unsigned int clk_freq;
111 int ret;
112
113 if (dmi_first_match(kblr_dmi_table))
114 clk_freq = 24000000;
115 else
116 clk_freq = 19200000;
117
118 ret = snd_soc_dai_set_sysclk(codec_dai, RT298_SCLK_S_PLL, clk_freq, SND_SOC_CLOCK_IN);
119 if (ret < 0)
120 dev_err(rtd->dev, "Set codec sysclk failed: %d\n", ret);
121
122 return ret;
123 }
124
125 static const struct snd_soc_ops avs_rt298_ops = {
126 .hw_params = avs_rt298_hw_params,
127 };
128
avs_create_dai_link(struct device * dev,const char * platform_name,int ssp_port,struct snd_soc_dai_link ** dai_link)129 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
130 struct snd_soc_dai_link **dai_link)
131 {
132 struct snd_soc_dai_link_component *platform;
133 struct snd_soc_dai_link *dl;
134
135 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
136 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
137 if (!dl || !platform)
138 return -ENOMEM;
139
140 platform->name = platform_name;
141
142 dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
143 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
144 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
145 if (!dl->name || !dl->cpus || !dl->codecs)
146 return -ENOMEM;
147
148 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
149 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343A:00");
150 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, "rt298-aif1");
151 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
152 return -ENOMEM;
153
154 dl->num_cpus = 1;
155 dl->num_codecs = 1;
156 dl->platforms = platform;
157 dl->num_platforms = 1;
158 dl->id = 0;
159 if (dmi_first_match(kblr_dmi_table))
160 dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
161 else
162 dl->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
163 dl->init = avs_rt298_codec_init;
164 dl->be_hw_params_fixup = avs_rt298_be_fixup;
165 dl->ops = &avs_rt298_ops;
166 dl->nonatomic = 1;
167 dl->no_pcm = 1;
168 dl->dpcm_capture = 1;
169 dl->dpcm_playback = 1;
170
171 *dai_link = dl;
172
173 return 0;
174 }
175
avs_create_dapm_routes(struct device * dev,int ssp_port,struct snd_soc_dapm_route ** routes,int * num_routes)176 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
177 struct snd_soc_dapm_route **routes, int *num_routes)
178 {
179 struct snd_soc_dapm_route *dr;
180 const int num_base = ARRAY_SIZE(card_base_routes);
181 const int num_dr = num_base + 2;
182 int idx;
183
184 dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
185 if (!dr)
186 return -ENOMEM;
187
188 memcpy(dr, card_base_routes, num_base * sizeof(*dr));
189
190 idx = num_base;
191 dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "AIF1 Playback");
192 dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
193 if (!dr[idx].sink || !dr[idx].source)
194 return -ENOMEM;
195
196 idx++;
197 dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
198 dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "AIF1 Capture");
199 if (!dr[idx].sink || !dr[idx].source)
200 return -ENOMEM;
201
202 *routes = dr;
203 *num_routes = num_dr;
204
205 return 0;
206 }
207
avs_card_set_jack(struct snd_soc_card * card,struct snd_soc_jack * jack)208 static int avs_card_set_jack(struct snd_soc_card *card, struct snd_soc_jack *jack)
209 {
210 struct snd_soc_component *component;
211
212 for_each_card_components(card, component)
213 snd_soc_component_set_jack(component, jack, NULL);
214 return 0;
215 }
216
avs_card_remove(struct snd_soc_card * card)217 static int avs_card_remove(struct snd_soc_card *card)
218 {
219 return avs_card_set_jack(card, NULL);
220 }
221
avs_card_suspend_pre(struct snd_soc_card * card)222 static int avs_card_suspend_pre(struct snd_soc_card *card)
223 {
224 return avs_card_set_jack(card, NULL);
225 }
226
avs_card_resume_post(struct snd_soc_card * card)227 static int avs_card_resume_post(struct snd_soc_card *card)
228 {
229 struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
230
231 return avs_card_set_jack(card, jack);
232 }
233
avs_rt298_probe(struct platform_device * pdev)234 static int avs_rt298_probe(struct platform_device *pdev)
235 {
236 struct snd_soc_dapm_route *routes;
237 struct snd_soc_dai_link *dai_link;
238 struct snd_soc_acpi_mach *mach;
239 struct snd_soc_card *card;
240 struct snd_soc_jack *jack;
241 struct device *dev = &pdev->dev;
242 const char *pname;
243 int num_routes, ssp_port, ret;
244
245 mach = dev_get_platdata(dev);
246 pname = mach->mach_params.platform;
247 ssp_port = __ffs(mach->mach_params.i2s_link_mask);
248
249 ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
250 if (ret) {
251 dev_err(dev, "Failed to create dai link: %d", ret);
252 return ret;
253 }
254
255 ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
256 if (ret) {
257 dev_err(dev, "Failed to create dapm routes: %d", ret);
258 return ret;
259 }
260
261 jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
262 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
263 if (!jack || !card)
264 return -ENOMEM;
265
266 card->name = "avs_rt298";
267 card->dev = dev;
268 card->owner = THIS_MODULE;
269 card->remove = avs_card_remove;
270 card->suspend_pre = avs_card_suspend_pre;
271 card->resume_post = avs_card_resume_post;
272 card->dai_link = dai_link;
273 card->num_links = 1;
274 card->controls = card_controls;
275 card->num_controls = ARRAY_SIZE(card_controls);
276 card->dapm_widgets = card_widgets;
277 card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
278 card->dapm_routes = routes;
279 card->num_dapm_routes = num_routes;
280 card->fully_routed = true;
281 snd_soc_card_set_drvdata(card, jack);
282
283 ret = snd_soc_fixup_dai_links_platform_name(card, pname);
284 if (ret)
285 return ret;
286
287 return devm_snd_soc_register_card(dev, card);
288 }
289
290 static struct platform_driver avs_rt298_driver = {
291 .probe = avs_rt298_probe,
292 .driver = {
293 .name = "avs_rt298",
294 .pm = &snd_soc_pm_ops,
295 },
296 };
297
298 module_platform_driver(avs_rt298_driver);
299
300 MODULE_LICENSE("GPL");
301 MODULE_ALIAS("platform:avs_rt298");
302