xref: /linux/sound/soc/intel/avs/boards/rt5682.c (revision 55d0969c451159cff86949b38c39171cab962069)
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/clk.h>
10 #include <linux/dmi.h>
11 #include <linux/i2c.h>
12 #include <linux/input.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <sound/core.h>
16 #include <sound/jack.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/rt5682.h>
20 #include <sound/soc.h>
21 #include <sound/soc-acpi.h>
22 #include "../../common/soc-intel-quirks.h"
23 #include "../../../codecs/rt5682.h"
24 #include "../utils.h"
25 
26 #define AVS_RT5682_SSP_CODEC(quirk)	((quirk) & GENMASK(2, 0))
27 #define AVS_RT5682_SSP_CODEC_MASK	(GENMASK(2, 0))
28 #define AVS_RT5682_MCLK_EN		BIT(3)
29 #define AVS_RT5682_MCLK_24MHZ		BIT(4)
30 #define AVS_RT5682_CODEC_DAI_NAME	"rt5682-aif1"
31 
32 /* Default: MCLK on, MCLK 19.2M, SSP0 */
33 static unsigned long avs_rt5682_quirk = AVS_RT5682_MCLK_EN | AVS_RT5682_SSP_CODEC(0);
34 
35 static int avs_rt5682_quirk_cb(const struct dmi_system_id *id)
36 {
37 	avs_rt5682_quirk = (unsigned long)id->driver_data;
38 	return 1;
39 }
40 
41 static const struct dmi_system_id avs_rt5682_quirk_table[] = {
42 	{
43 		.callback = avs_rt5682_quirk_cb,
44 		.matches = {
45 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
46 			DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
47 		},
48 		.driver_data = (void *)(AVS_RT5682_MCLK_EN |
49 					AVS_RT5682_MCLK_24MHZ |
50 					AVS_RT5682_SSP_CODEC(1)),
51 	},
52 	{
53 		.callback = avs_rt5682_quirk_cb,
54 		.matches = {
55 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
56 			DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
57 		},
58 		.driver_data = (void *)(AVS_RT5682_MCLK_EN |
59 					AVS_RT5682_SSP_CODEC(0)),
60 	},
61 	{}
62 };
63 
64 static const struct snd_kcontrol_new card_controls[] = {
65 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
66 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
67 };
68 
69 static const struct snd_soc_dapm_widget card_widgets[] = {
70 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
71 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
72 };
73 
74 static const struct snd_soc_dapm_route card_base_routes[] = {
75 	/* HP jack connectors - unknown if we have jack detect */
76 	{ "Headphone Jack", NULL, "HPOL" },
77 	{ "Headphone Jack", NULL, "HPOR" },
78 
79 	/* other jacks */
80 	{ "IN1P", NULL, "Headset Mic" },
81 };
82 
83 static const struct snd_soc_jack_pin card_jack_pins[] = {
84 	{
85 		.pin = "Headphone Jack",
86 		.mask = SND_JACK_HEADPHONE,
87 	},
88 	{
89 		.pin = "Headset Mic",
90 		.mask = SND_JACK_MICROPHONE,
91 	},
92 };
93 
94 static int avs_rt5682_codec_init(struct snd_soc_pcm_runtime *runtime)
95 {
96 	struct snd_soc_component *component = snd_soc_rtd_to_codec(runtime, 0)->component;
97 	struct snd_soc_card *card = runtime->card;
98 	struct snd_soc_jack_pin *pins;
99 	struct snd_soc_jack *jack;
100 	int num_pins, ret;
101 
102 	jack = snd_soc_card_get_drvdata(card);
103 	num_pins = ARRAY_SIZE(card_jack_pins);
104 
105 	pins = devm_kmemdup(card->dev, card_jack_pins, sizeof(*pins) * num_pins, GFP_KERNEL);
106 	if (!pins)
107 		return -ENOMEM;
108 
109 	/* Need to enable ASRC function for 24MHz mclk rate */
110 	if ((avs_rt5682_quirk & AVS_RT5682_MCLK_EN) &&
111 	    (avs_rt5682_quirk & AVS_RT5682_MCLK_24MHZ)) {
112 		rt5682_sel_asrc_clk_src(component, RT5682_DA_STEREO1_FILTER |
113 					RT5682_AD_STEREO1_FILTER, RT5682_CLK_SEL_I2S1_ASRC);
114 	}
115 
116 
117 	ret = snd_soc_card_jack_new_pins(card, "Headset Jack", SND_JACK_HEADSET | SND_JACK_BTN_0 |
118 					 SND_JACK_BTN_1 | SND_JACK_BTN_2 | SND_JACK_BTN_3, jack,
119 					 pins, num_pins);
120 	if (ret) {
121 		dev_err(card->dev, "Headset Jack creation failed: %d\n", ret);
122 		return ret;
123 	}
124 
125 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
126 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
127 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
128 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
129 
130 	ret = snd_soc_component_set_jack(component, jack, NULL);
131 	if (ret) {
132 		dev_err(card->dev, "Headset Jack call-back failed: %d\n", ret);
133 		return ret;
134 	}
135 
136 	return 0;
137 };
138 
139 static void avs_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
140 {
141 	snd_soc_component_set_jack(snd_soc_rtd_to_codec(rtd, 0)->component, NULL, NULL);
142 }
143 
144 static int
145 avs_rt5682_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
146 {
147 	struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
148 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0);
149 	int pll_source, freq_in, freq_out;
150 	int ret;
151 
152 	if (avs_rt5682_quirk & AVS_RT5682_MCLK_EN) {
153 		pll_source = RT5682_PLL1_S_MCLK;
154 		if (avs_rt5682_quirk & AVS_RT5682_MCLK_24MHZ)
155 			freq_in = 24000000;
156 		else
157 			freq_in = 19200000;
158 	} else {
159 		pll_source = RT5682_PLL1_S_BCLK1;
160 		freq_in = params_rate(params) * 50;
161 	}
162 
163 	freq_out = params_rate(params) * 512;
164 
165 	ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL1, pll_source, freq_in, freq_out);
166 	if (ret < 0)
167 		dev_err(runtime->dev, "Set PLL failed: %d\n", ret);
168 
169 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1, freq_out, SND_SOC_CLOCK_IN);
170 	if (ret < 0)
171 		dev_err(runtime->dev, "Set sysclk failed: %d\n", ret);
172 
173 	/* slot_width should be equal or larger than data length. */
174 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2, params_width(params));
175 	if (ret < 0)
176 		dev_err(runtime->dev, "Set TDM slot failed: %d\n", ret);
177 
178 	return ret;
179 }
180 
181 static const struct snd_soc_ops avs_rt5682_ops = {
182 	.hw_params = avs_rt5682_hw_params,
183 };
184 
185 static int
186 avs_rt5682_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params)
187 {
188 	struct snd_interval *rate, *channels;
189 	struct snd_mask *fmt;
190 
191 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
192 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
193 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
194 
195 	/* The ADSP will convert the FE rate to 48k, stereo */
196 	rate->min = rate->max = 48000;
197 	channels->min = channels->max = 2;
198 
199 	/* set SSPN to 24 bit */
200 	snd_mask_none(fmt);
201 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
202 
203 	return 0;
204 }
205 
206 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
207 			       int tdm_slot, struct snd_soc_dai_link **dai_link)
208 {
209 	struct snd_soc_dai_link_component *platform;
210 	struct snd_soc_dai_link *dl;
211 
212 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
213 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
214 	if (!dl || !platform)
215 		return -ENOMEM;
216 
217 	platform->name = platform_name;
218 
219 	dl->name = devm_kasprintf(dev, GFP_KERNEL,
220 				  AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot));
221 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
222 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
223 	if (!dl->name || !dl->cpus || !dl->codecs)
224 		return -ENOMEM;
225 
226 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
227 					    AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot));
228 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10EC5682:00");
229 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, AVS_RT5682_CODEC_DAI_NAME);
230 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
231 		return -ENOMEM;
232 
233 	dl->num_cpus = 1;
234 	dl->num_codecs = 1;
235 	dl->platforms = platform;
236 	dl->num_platforms = 1;
237 	dl->id = 0;
238 	dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC;
239 	dl->init = avs_rt5682_codec_init;
240 	dl->exit = avs_rt5682_codec_exit;
241 	dl->be_hw_params_fixup = avs_rt5682_be_fixup;
242 	dl->ops = &avs_rt5682_ops;
243 	dl->nonatomic = 1;
244 	dl->no_pcm = 1;
245 	dl->dpcm_capture = 1;
246 	dl->dpcm_playback = 1;
247 
248 	*dai_link = dl;
249 
250 	return 0;
251 }
252 
253 static int avs_card_suspend_pre(struct snd_soc_card *card)
254 {
255 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, AVS_RT5682_CODEC_DAI_NAME);
256 
257 	return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
258 }
259 
260 static int avs_card_resume_post(struct snd_soc_card *card)
261 {
262 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, AVS_RT5682_CODEC_DAI_NAME);
263 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
264 
265 	return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
266 }
267 
268 static int avs_rt5682_probe(struct platform_device *pdev)
269 {
270 	struct snd_soc_dai_link *dai_link;
271 	struct snd_soc_acpi_mach *mach;
272 	struct snd_soc_card *card;
273 	struct snd_soc_jack *jack;
274 	struct device *dev = &pdev->dev;
275 	const char *pname;
276 	int ssp_port, tdm_slot, ret;
277 
278 	if (pdev->id_entry && pdev->id_entry->driver_data)
279 		avs_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
280 
281 	dmi_check_system(avs_rt5682_quirk_table);
282 	dev_dbg(dev, "avs_rt5682_quirk = %lx\n", avs_rt5682_quirk);
283 
284 	mach = dev_get_platdata(dev);
285 	pname = mach->mach_params.platform;
286 
287 	ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot);
288 	if (ret)
289 		return ret;
290 
291 	ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link);
292 	if (ret) {
293 		dev_err(dev, "Failed to create dai link: %d", ret);
294 		return ret;
295 	}
296 
297 	jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
298 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
299 	if (!jack || !card)
300 		return -ENOMEM;
301 
302 	card->name = "avs_rt5682";
303 	card->dev = dev;
304 	card->owner = THIS_MODULE;
305 	card->suspend_pre = avs_card_suspend_pre;
306 	card->resume_post = avs_card_resume_post;
307 	card->dai_link = dai_link;
308 	card->num_links = 1;
309 	card->controls = card_controls;
310 	card->num_controls = ARRAY_SIZE(card_controls);
311 	card->dapm_widgets = card_widgets;
312 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
313 	card->dapm_routes = card_base_routes;
314 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
315 	card->fully_routed = true;
316 	snd_soc_card_set_drvdata(card, jack);
317 
318 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
319 	if (ret)
320 		return ret;
321 
322 	return devm_snd_soc_register_card(dev, card);
323 }
324 
325 static const struct platform_device_id avs_rt5682_driver_ids[] = {
326 	{
327 		.name = "avs_rt5682",
328 	},
329 	{},
330 };
331 MODULE_DEVICE_TABLE(platform, avs_rt5682_driver_ids);
332 
333 static struct platform_driver avs_rt5682_driver = {
334 	.probe = avs_rt5682_probe,
335 	.driver = {
336 		.name = "avs_rt5682",
337 		.pm = &snd_soc_pm_ops,
338 	},
339 	.id_table = avs_rt5682_driver_ids,
340 };
341 
342 module_platform_driver(avs_rt5682_driver)
343 
344 MODULE_DESCRIPTION("Intel rt5682 machine driver");
345 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
346 MODULE_LICENSE("GPL");
347