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