xref: /openbmc/linux/sound/soc/intel/avs/boards/rt5682.c (revision fedf429e)
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 int avs_rt5682_codec_init(struct snd_soc_pcm_runtime *runtime)
83 {
84 	struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
85 	struct snd_soc_jack *jack;
86 	struct snd_soc_card *card = runtime->card;
87 	int ret;
88 
89 	jack = snd_soc_card_get_drvdata(card);
90 
91 	/* Need to enable ASRC function for 24MHz mclk rate */
92 	if ((avs_rt5682_quirk & AVS_RT5682_MCLK_EN) &&
93 	    (avs_rt5682_quirk & AVS_RT5682_MCLK_24MHZ)) {
94 		rt5682_sel_asrc_clk_src(component, RT5682_DA_STEREO1_FILTER |
95 					RT5682_AD_STEREO1_FILTER, RT5682_CLK_SEL_I2S1_ASRC);
96 	}
97 
98 	/*
99 	 * Headset buttons map to the google Reference headset.
100 	 * These can be configured by userspace.
101 	 */
102 	ret = snd_soc_card_jack_new(card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0 |
103 				    SND_JACK_BTN_1 | SND_JACK_BTN_2 | SND_JACK_BTN_3, jack);
104 	if (ret) {
105 		dev_err(card->dev, "Headset Jack creation failed: %d\n", ret);
106 		return ret;
107 	}
108 
109 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
110 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
111 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
112 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
113 
114 	ret = snd_soc_component_set_jack(component, jack, NULL);
115 	if (ret) {
116 		dev_err(card->dev, "Headset Jack call-back failed: %d\n", ret);
117 		return ret;
118 	}
119 
120 	return 0;
121 };
122 
123 static void avs_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
124 {
125 	snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component, NULL, NULL);
126 }
127 
128 static int
129 avs_rt5682_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params)
130 {
131 	struct snd_soc_pcm_runtime *runtime = asoc_substream_to_rtd(substream);
132 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, 0);
133 	int clk_id, clk_freq;
134 	int pll_out, ret;
135 
136 	if (avs_rt5682_quirk & AVS_RT5682_MCLK_EN) {
137 		clk_id = RT5682_PLL1_S_MCLK;
138 		if (avs_rt5682_quirk & AVS_RT5682_MCLK_24MHZ)
139 			clk_freq = 24000000;
140 		else
141 			clk_freq = 19200000;
142 	} else {
143 		clk_id = RT5682_PLL1_S_BCLK1;
144 		clk_freq = params_rate(params) * 50;
145 	}
146 
147 	pll_out = params_rate(params) * 512;
148 
149 	ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
150 	if (ret < 0)
151 		dev_err(runtime->dev, "snd_soc_dai_set_pll err = %d\n", ret);
152 
153 	/* Configure sysclk for codec */
154 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1, pll_out, SND_SOC_CLOCK_IN);
155 	if (ret < 0)
156 		dev_err(runtime->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
157 
158 	/* slot_width should equal or large than data length, set them be the same */
159 	ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2, params_width(params));
160 	if (ret < 0) {
161 		dev_err(runtime->dev, "set TDM slot err:%d\n", ret);
162 		return ret;
163 	}
164 
165 	return 0;
166 }
167 
168 static const struct snd_soc_ops avs_rt5682_ops = {
169 	.hw_params = avs_rt5682_hw_params,
170 };
171 
172 static int
173 avs_rt5682_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params)
174 {
175 	struct snd_interval *rate, *channels;
176 	struct snd_mask *fmt;
177 
178 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
179 	channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
180 	fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
181 
182 	/* The ADSP will convert the FE rate to 48k, stereo */
183 	rate->min = rate->max = 48000;
184 	channels->min = channels->max = 2;
185 
186 	/* set SSPN to 24 bit */
187 	snd_mask_none(fmt);
188 	snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
189 
190 	return 0;
191 }
192 
193 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
194 			       struct snd_soc_dai_link **dai_link)
195 {
196 	struct snd_soc_dai_link_component *platform;
197 	struct snd_soc_dai_link *dl;
198 
199 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
200 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
201 	if (!dl || !platform)
202 		return -ENOMEM;
203 
204 	platform->name = platform_name;
205 
206 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
207 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
208 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
209 	if (!dl->name || !dl->cpus || !dl->codecs)
210 		return -ENOMEM;
211 
212 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
213 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10EC5682:00");
214 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, AVS_RT5682_CODEC_DAI_NAME);
215 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
216 		return -ENOMEM;
217 
218 	dl->num_cpus = 1;
219 	dl->num_codecs = 1;
220 	dl->platforms = platform;
221 	dl->num_platforms = 1;
222 	dl->id = 0;
223 	dl->init = avs_rt5682_codec_init;
224 	dl->exit = avs_rt5682_codec_exit;
225 	dl->be_hw_params_fixup = avs_rt5682_be_fixup;
226 	dl->ops = &avs_rt5682_ops;
227 	dl->nonatomic = 1;
228 	dl->no_pcm = 1;
229 	dl->dpcm_capture = 1;
230 	dl->dpcm_playback = 1;
231 
232 	*dai_link = dl;
233 
234 	return 0;
235 }
236 
237 static int avs_card_suspend_pre(struct snd_soc_card *card)
238 {
239 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, AVS_RT5682_CODEC_DAI_NAME);
240 
241 	return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
242 }
243 
244 static int avs_card_resume_post(struct snd_soc_card *card)
245 {
246 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, AVS_RT5682_CODEC_DAI_NAME);
247 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
248 
249 	return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
250 }
251 
252 static int avs_rt5682_probe(struct platform_device *pdev)
253 {
254 	struct snd_soc_dai_link *dai_link;
255 	struct snd_soc_acpi_mach *mach;
256 	struct snd_soc_card *card;
257 	struct snd_soc_jack *jack;
258 	struct device *dev = &pdev->dev;
259 	const char *pname;
260 	int ssp_port, ret;
261 
262 	if (pdev->id_entry && pdev->id_entry->driver_data)
263 		avs_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
264 
265 	dmi_check_system(avs_rt5682_quirk_table);
266 	dev_dbg(dev, "avs_rt5682_quirk = %lx\n", avs_rt5682_quirk);
267 
268 	mach = dev_get_platdata(dev);
269 	pname = mach->mach_params.platform;
270 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
271 
272 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
273 	if (ret) {
274 		dev_err(dev, "Failed to create dai link: %d", ret);
275 		return ret;
276 	}
277 
278 	jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
279 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
280 	if (!jack || !card)
281 		return -ENOMEM;
282 
283 	card->name = "avs_rt5682";
284 	card->dev = dev;
285 	card->owner = THIS_MODULE;
286 	card->suspend_pre = avs_card_suspend_pre;
287 	card->resume_post = avs_card_resume_post;
288 	card->dai_link = dai_link;
289 	card->num_links = 1;
290 	card->controls = card_controls;
291 	card->num_controls = ARRAY_SIZE(card_controls);
292 	card->dapm_widgets = card_widgets;
293 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
294 	card->dapm_routes = card_base_routes;
295 	card->num_dapm_routes = ARRAY_SIZE(card_base_routes);
296 	card->fully_routed = true;
297 	snd_soc_card_set_drvdata(card, jack);
298 
299 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
300 	if (ret)
301 		return ret;
302 
303 	return devm_snd_soc_register_card(dev, card);
304 }
305 
306 static struct platform_driver avs_rt5682_driver = {
307 	.probe = avs_rt5682_probe,
308 	.driver = {
309 		.name = "avs_rt5682",
310 		.pm = &snd_soc_pm_ops,
311 	},
312 };
313 
314 module_platform_driver(avs_rt5682_driver)
315 
316 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
317 MODULE_LICENSE("GPL");
318 MODULE_ALIAS("platform:avs_rt5682");
319