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