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