xref: /openbmc/linux/sound/soc/intel/avs/boards/rt274.c (revision f2d8e15b)
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 int avs_rt274_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params)
123 {
124 	struct snd_interval *rate, *channels;
125 	struct snd_mask *fmt;
126 
127 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
128 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
129 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
130 
131 	/* The ADSP will convert the FE rate to 48k, stereo */
132 	rate->min = rate->max = AVS_RT274_BE_FIXUP_RATE;
133 	channels->min = channels->max = 2;
134 
135 	/* set SSPN to 24 bit */
136 	snd_mask_none(fmt);
137 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
138 
139 	return 0;
140 }
141 
142 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
143 			       struct snd_soc_dai_link **dai_link)
144 {
145 	struct snd_soc_dai_link_component *platform;
146 	struct snd_soc_dai_link *dl;
147 
148 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
149 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
150 	if (!dl || !platform)
151 		return -ENOMEM;
152 
153 	platform->name = platform_name;
154 
155 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
156 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
157 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
158 	if (!dl->name || !dl->cpus || !dl->codecs)
159 		return -ENOMEM;
160 
161 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
162 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT34C2:00");
163 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, "rt274-aif1");
164 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
165 		return -ENOMEM;
166 
167 	dl->num_cpus = 1;
168 	dl->num_codecs = 1;
169 	dl->platforms = platform;
170 	dl->num_platforms = 1;
171 	dl->id = 0;
172 	dl->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS;
173 	dl->init = avs_rt274_codec_init;
174 	dl->be_hw_params_fixup = avs_rt274_be_fixup;
175 	dl->nonatomic = 1;
176 	dl->no_pcm = 1;
177 	dl->dpcm_capture = 1;
178 	dl->dpcm_playback = 1;
179 
180 	*dai_link = dl;
181 
182 	return 0;
183 }
184 
185 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
186 				  struct snd_soc_dapm_route **routes, int *num_routes)
187 {
188 	struct snd_soc_dapm_route *dr;
189 	const int num_base = ARRAY_SIZE(card_base_routes);
190 	const int num_dr = num_base + 2;
191 	int idx;
192 
193 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
194 	if (!dr)
195 		return -ENOMEM;
196 
197 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
198 
199 	idx = num_base;
200 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "AIF1 Playback");
201 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
202 	if (!dr[idx].sink || !dr[idx].source)
203 		return -ENOMEM;
204 
205 	idx++;
206 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
207 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "AIF1 Capture");
208 	if (!dr[idx].sink || !dr[idx].source)
209 		return -ENOMEM;
210 
211 	*routes = dr;
212 	*num_routes = num_dr;
213 
214 	return 0;
215 }
216 
217 static int avs_card_set_jack(struct snd_soc_card *card, struct snd_soc_jack *jack)
218 {
219 	struct snd_soc_component *component;
220 
221 	for_each_card_components(card, component)
222 		snd_soc_component_set_jack(component, jack, NULL);
223 	return 0;
224 }
225 
226 static int avs_card_remove(struct snd_soc_card *card)
227 {
228 	return avs_card_set_jack(card, NULL);
229 }
230 
231 static int avs_card_suspend_pre(struct snd_soc_card *card)
232 {
233 	return avs_card_set_jack(card, NULL);
234 }
235 
236 static int avs_card_resume_post(struct snd_soc_card *card)
237 {
238 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
239 
240 	return avs_card_set_jack(card, jack);
241 }
242 
243 static int avs_rt274_probe(struct platform_device *pdev)
244 {
245 	struct snd_soc_dapm_route *routes;
246 	struct snd_soc_dai_link *dai_link;
247 	struct snd_soc_acpi_mach *mach;
248 	struct snd_soc_card *card;
249 	struct snd_soc_jack *jack;
250 	struct device *dev = &pdev->dev;
251 	const char *pname;
252 	int num_routes, ssp_port, ret;
253 
254 	mach = dev_get_platdata(dev);
255 	pname = mach->mach_params.platform;
256 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
257 
258 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
259 	if (ret) {
260 		dev_err(dev, "Failed to create dai link: %d", ret);
261 		return ret;
262 	}
263 
264 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
265 	if (ret) {
266 		dev_err(dev, "Failed to create dapm routes: %d", ret);
267 		return ret;
268 	}
269 
270 	jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
271 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
272 	if (!jack || !card)
273 		return -ENOMEM;
274 
275 	card->name = "avs_rt274";
276 	card->dev = dev;
277 	card->owner = THIS_MODULE;
278 	card->remove = avs_card_remove;
279 	card->suspend_pre = avs_card_suspend_pre;
280 	card->resume_post = avs_card_resume_post;
281 	card->dai_link = dai_link;
282 	card->num_links = 1;
283 	card->controls = card_controls;
284 	card->num_controls = ARRAY_SIZE(card_controls);
285 	card->dapm_widgets = card_widgets;
286 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
287 	card->dapm_routes = routes;
288 	card->num_dapm_routes = num_routes;
289 	card->fully_routed = true;
290 	snd_soc_card_set_drvdata(card, jack);
291 
292 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
293 	if (ret)
294 		return ret;
295 
296 	return devm_snd_soc_register_card(dev, card);
297 }
298 
299 static struct platform_driver avs_rt274_driver = {
300 	.probe = avs_rt274_probe,
301 	.driver = {
302 		.name = "avs_rt274",
303 		.pm = &snd_soc_pm_ops,
304 	},
305 };
306 
307 module_platform_driver(avs_rt274_driver);
308 
309 MODULE_LICENSE("GPL");
310 MODULE_ALIAS("platform:avs_rt274");
311