xref: /openbmc/linux/sound/soc/intel/avs/boards/rt5682.c (revision e8254a8e)
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 avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
173 			       struct snd_soc_dai_link **dai_link)
174 {
175 	struct snd_soc_dai_link_component *platform;
176 	struct snd_soc_dai_link *dl;
177 
178 	dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
179 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
180 	if (!dl || !platform)
181 		return -ENOMEM;
182 
183 	platform->name = platform_name;
184 
185 	dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
186 	dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
187 	dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
188 	if (!dl->name || !dl->cpus || !dl->codecs)
189 		return -ENOMEM;
190 
191 	dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
192 	dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10EC5682:00");
193 	dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, AVS_RT5682_CODEC_DAI_NAME);
194 	if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name)
195 		return -ENOMEM;
196 
197 	dl->num_cpus = 1;
198 	dl->num_codecs = 1;
199 	dl->platforms = platform;
200 	dl->num_platforms = 1;
201 	dl->id = 0;
202 	dl->init = avs_rt5682_codec_init;
203 	dl->exit = avs_rt5682_codec_exit;
204 	dl->ops = &avs_rt5682_ops;
205 	dl->nonatomic = 1;
206 	dl->no_pcm = 1;
207 	dl->dpcm_capture = 1;
208 	dl->dpcm_playback = 1;
209 
210 	*dai_link = dl;
211 
212 	return 0;
213 }
214 
215 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
216 				  struct snd_soc_dapm_route **routes, int *num_routes)
217 {
218 	struct snd_soc_dapm_route *dr;
219 	const int num_base = ARRAY_SIZE(card_base_routes);
220 	const int num_dr = num_base + 2;
221 	int idx;
222 
223 	dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
224 	if (!dr)
225 		return -ENOMEM;
226 
227 	memcpy(dr, card_base_routes, num_base * sizeof(*dr));
228 
229 	idx = num_base;
230 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "AIF1 Playback");
231 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
232 	if (!dr[idx].sink || !dr[idx].source)
233 		return -ENOMEM;
234 
235 	idx++;
236 	dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
237 	dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "AIF1 Capture");
238 	if (!dr[idx].sink || !dr[idx].source)
239 		return -ENOMEM;
240 
241 	*routes = dr;
242 	*num_routes = num_dr;
243 
244 	return 0;
245 }
246 
247 static int avs_card_suspend_pre(struct snd_soc_card *card)
248 {
249 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, AVS_RT5682_CODEC_DAI_NAME);
250 
251 	return snd_soc_component_set_jack(codec_dai->component, NULL, NULL);
252 }
253 
254 static int avs_card_resume_post(struct snd_soc_card *card)
255 {
256 	struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, AVS_RT5682_CODEC_DAI_NAME);
257 	struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card);
258 
259 	return snd_soc_component_set_jack(codec_dai->component, jack, NULL);
260 }
261 
262 static int avs_rt5682_probe(struct platform_device *pdev)
263 {
264 	struct snd_soc_dapm_route *routes;
265 	struct snd_soc_dai_link *dai_link;
266 	struct snd_soc_acpi_mach *mach;
267 	struct snd_soc_card *card;
268 	struct snd_soc_jack *jack;
269 	struct device *dev = &pdev->dev;
270 	const char *pname;
271 	int num_routes, ssp_port, ret;
272 
273 	if (pdev->id_entry && pdev->id_entry->driver_data)
274 		avs_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
275 
276 	dmi_check_system(avs_rt5682_quirk_table);
277 	dev_dbg(dev, "avs_rt5682_quirk = %lx\n", avs_rt5682_quirk);
278 
279 	mach = dev_get_platdata(dev);
280 	pname = mach->mach_params.platform;
281 	ssp_port = __ffs(mach->mach_params.i2s_link_mask);
282 
283 	ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
284 	if (ret) {
285 		dev_err(dev, "Failed to create dai link: %d", ret);
286 		return ret;
287 	}
288 
289 	ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
290 	if (ret) {
291 		dev_err(dev, "Failed to create dapm routes: %d", ret);
292 		return ret;
293 	}
294 
295 	jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL);
296 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
297 	if (!jack || !card)
298 		return -ENOMEM;
299 
300 	card->name = "avs_rt5682";
301 	card->dev = dev;
302 	card->owner = THIS_MODULE;
303 	card->suspend_pre = avs_card_suspend_pre;
304 	card->resume_post = avs_card_resume_post;
305 	card->dai_link = dai_link;
306 	card->num_links = 1;
307 	card->controls = card_controls;
308 	card->num_controls = ARRAY_SIZE(card_controls);
309 	card->dapm_widgets = card_widgets;
310 	card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
311 	card->dapm_routes = routes;
312 	card->num_dapm_routes = num_routes;
313 	card->fully_routed = true;
314 	snd_soc_card_set_drvdata(card, jack);
315 
316 	ret = snd_soc_fixup_dai_links_platform_name(card, pname);
317 	if (ret)
318 		return ret;
319 
320 	return devm_snd_soc_register_card(dev, card);
321 }
322 
323 static struct platform_driver avs_rt5682_driver = {
324 	.probe = avs_rt5682_probe,
325 	.driver = {
326 		.name = "avs_rt5682",
327 		.pm = &snd_soc_pm_ops,
328 	},
329 };
330 
331 module_platform_driver(avs_rt5682_driver)
332 
333 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
334 MODULE_LICENSE("GPL");
335 MODULE_ALIAS("platform:avs_rt5682");
336