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