1// SPDX-License-Identifier: GPL-2.0-only
2//
3// Copyright(c) 2022 Intel Corporation. All rights reserved.
4
5/*
6 * sof_ssp_amp.c - ASoc Machine driver for Intel platforms
7 * with RT1308/CS35L41 codec.
8 */
9
10#include <linux/acpi.h>
11#include <linux/delay.h>
12#include <linux/dmi.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/sof.h>
20#include "../../codecs/hdac_hdmi.h"
21#include "hda_dsp_common.h"
22#include "sof_realtek_common.h"
23#include "sof_cirrus_common.h"
24
25#define NAME_SIZE 32
26
27/* SSP port ID for speaker amplifier */
28#define SOF_AMPLIFIER_SSP(quirk)		((quirk) & GENMASK(3, 0))
29#define SOF_AMPLIFIER_SSP_MASK			(GENMASK(3, 0))
30
31/* HDMI capture*/
32#define SOF_SSP_HDMI_CAPTURE_PRESENT		BIT(4)
33#define SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT		5
34#define SOF_NO_OF_HDMI_CAPTURE_SSP_MASK		(GENMASK(6, 5))
35#define SOF_NO_OF_HDMI_CAPTURE_SSP(quirk)	\
36	(((quirk) << SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT) & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK)
37
38#define SOF_HDMI_CAPTURE_1_SSP_SHIFT		7
39#define SOF_HDMI_CAPTURE_1_SSP_MASK		(GENMASK(9, 7))
40#define SOF_HDMI_CAPTURE_1_SSP(quirk)	\
41	(((quirk) << SOF_HDMI_CAPTURE_1_SSP_SHIFT) & SOF_HDMI_CAPTURE_1_SSP_MASK)
42
43#define SOF_HDMI_CAPTURE_2_SSP_SHIFT		10
44#define SOF_HDMI_CAPTURE_2_SSP_MASK		(GENMASK(12, 10))
45#define SOF_HDMI_CAPTURE_2_SSP(quirk)	\
46	(((quirk) << SOF_HDMI_CAPTURE_2_SSP_SHIFT) & SOF_HDMI_CAPTURE_2_SSP_MASK)
47
48/* HDMI playback */
49#define SOF_HDMI_PLAYBACK_PRESENT		BIT(13)
50#define SOF_NO_OF_HDMI_PLAYBACK_SHIFT		14
51#define SOF_NO_OF_HDMI_PLAYBACK_MASK		(GENMASK(16, 14))
52#define SOF_NO_OF_HDMI_PLAYBACK(quirk)	\
53	(((quirk) << SOF_NO_OF_HDMI_PLAYBACK_SHIFT) & SOF_NO_OF_HDMI_PLAYBACK_MASK)
54
55/* BT audio offload */
56#define SOF_SSP_BT_OFFLOAD_PRESENT		BIT(17)
57#define SOF_BT_OFFLOAD_SSP_SHIFT		18
58#define SOF_BT_OFFLOAD_SSP_MASK			(GENMASK(20, 18))
59#define SOF_BT_OFFLOAD_SSP(quirk)	\
60	(((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK)
61
62/* Speaker amplifiers */
63#define SOF_RT1308_SPEAKER_AMP_PRESENT		BIT(21)
64#define SOF_CS35L41_SPEAKER_AMP_PRESENT		BIT(22)
65
66/* Default: SSP2  */
67static unsigned long sof_ssp_amp_quirk = SOF_AMPLIFIER_SSP(2);
68
69struct sof_hdmi_pcm {
70	struct list_head head;
71	struct snd_soc_jack sof_hdmi;
72	struct snd_soc_dai *codec_dai;
73	int device;
74};
75
76struct sof_card_private {
77	struct list_head hdmi_pcm_list;
78	bool common_hdmi_codec_drv;
79	bool idisp_codec;
80};
81
82static const struct dmi_system_id chromebook_platforms[] = {
83	{
84		.ident = "Google Chromebooks",
85		.matches = {
86			DMI_MATCH(DMI_SYS_VENDOR, "Google"),
87		}
88	},
89	{},
90};
91
92static const struct snd_soc_dapm_widget sof_ssp_amp_dapm_widgets[] = {
93	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
94};
95
96static const struct snd_soc_dapm_route sof_ssp_amp_dapm_routes[] = {
97	/* digital mics */
98	{"DMic", NULL, "SoC DMIC"},
99};
100
101static int sof_card_late_probe(struct snd_soc_card *card)
102{
103	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
104	struct snd_soc_component *component = NULL;
105	char jack_name[NAME_SIZE];
106	struct sof_hdmi_pcm *pcm;
107	int err;
108
109	if (!(sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT))
110		return 0;
111
112	/* HDMI is not supported by SOF on Baytrail/CherryTrail */
113	if (!ctx->idisp_codec)
114		return 0;
115
116	if (list_empty(&ctx->hdmi_pcm_list))
117		return -EINVAL;
118
119	if (ctx->common_hdmi_codec_drv) {
120		pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
121				       head);
122		component = pcm->codec_dai->component;
123		return hda_dsp_hdmi_build_controls(card, component);
124	}
125
126	list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
127		component = pcm->codec_dai->component;
128		snprintf(jack_name, sizeof(jack_name),
129			 "HDMI/DP, pcm=%d Jack", pcm->device);
130		err = snd_soc_card_jack_new(card, jack_name,
131					    SND_JACK_AVOUT, &pcm->sof_hdmi);
132
133		if (err)
134			return err;
135
136		err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
137					  &pcm->sof_hdmi);
138		if (err < 0)
139			return err;
140	}
141
142	return hdac_hdmi_jack_port_init(component, &card->dapm);
143}
144
145static struct snd_soc_card sof_ssp_amp_card = {
146	.name         = "ssp_amp",
147	.owner        = THIS_MODULE,
148	.dapm_widgets = sof_ssp_amp_dapm_widgets,
149	.num_dapm_widgets = ARRAY_SIZE(sof_ssp_amp_dapm_widgets),
150	.dapm_routes = sof_ssp_amp_dapm_routes,
151	.num_dapm_routes = ARRAY_SIZE(sof_ssp_amp_dapm_routes),
152	.fully_routed = true,
153	.late_probe = sof_card_late_probe,
154};
155
156static struct snd_soc_dai_link_component platform_component[] = {
157	{
158		/* name might be overridden during probe */
159		.name = "0000:00:1f.3"
160	}
161};
162
163static struct snd_soc_dai_link_component dmic_component[] = {
164	{
165		.name = "dmic-codec",
166		.dai_name = "dmic-hifi",
167	}
168};
169
170static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
171{
172	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
173	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
174	struct sof_hdmi_pcm *pcm;
175
176	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
177	if (!pcm)
178		return -ENOMEM;
179
180	/* dai_link id is 1:1 mapped to the PCM device */
181	pcm->device = rtd->dai_link->id;
182	pcm->codec_dai = dai;
183
184	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
185
186	return 0;
187}
188
189#define IDISP_CODEC_MASK	0x4
190
191static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
192							  int ssp_codec,
193							  int dmic_be_num,
194							  int hdmi_num,
195							  bool idisp_codec)
196{
197	struct snd_soc_dai_link_component *idisp_components;
198	struct snd_soc_dai_link_component *cpus;
199	struct snd_soc_dai_link *links;
200	int i, id = 0;
201
202	links = devm_kcalloc(dev, sof_ssp_amp_card.num_links,
203					sizeof(struct snd_soc_dai_link), GFP_KERNEL);
204	cpus = devm_kcalloc(dev, sof_ssp_amp_card.num_links,
205					sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
206	if (!links || !cpus)
207		return NULL;
208
209	/* HDMI-In SSP */
210	if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT) {
211		int num_of_hdmi_ssp = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
212				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
213
214		for (i = 1; i <= num_of_hdmi_ssp; i++) {
215			int port = (i == 1 ? (sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_1_SSP_MASK) >>
216						SOF_HDMI_CAPTURE_1_SSP_SHIFT :
217						(sof_ssp_amp_quirk & SOF_HDMI_CAPTURE_2_SSP_MASK) >>
218						SOF_HDMI_CAPTURE_2_SSP_SHIFT);
219
220			links[id].cpus = &cpus[id];
221			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
222								  "SSP%d Pin", port);
223			if (!links[id].cpus->dai_name)
224				return NULL;
225			links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", port);
226			if (!links[id].name)
227				return NULL;
228			links[id].id = id;
229			links[id].codecs = &asoc_dummy_dlc;
230			links[id].num_codecs = 1;
231			links[id].platforms = platform_component;
232			links[id].num_platforms = ARRAY_SIZE(platform_component);
233			links[id].dpcm_capture = 1;
234			links[id].no_pcm = 1;
235			links[id].num_cpus = 1;
236			id++;
237		}
238	}
239
240	/* codec SSP */
241	links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_codec);
242	if (!links[id].name)
243		return NULL;
244
245	links[id].id = id;
246	if (sof_ssp_amp_quirk & SOF_RT1308_SPEAKER_AMP_PRESENT) {
247		sof_rt1308_dai_link(&links[id]);
248	} else if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) {
249		cs35l41_set_dai_link(&links[id]);
250	}
251	links[id].platforms = platform_component;
252	links[id].num_platforms = ARRAY_SIZE(platform_component);
253	links[id].dpcm_playback = 1;
254	/* feedback from amplifier or firmware-generated echo reference */
255	links[id].dpcm_capture = 1;
256	links[id].no_pcm = 1;
257	links[id].cpus = &cpus[id];
258	links[id].num_cpus = 1;
259	links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_codec);
260	if (!links[id].cpus->dai_name)
261		return NULL;
262
263	id++;
264
265	/* dmic */
266	if (dmic_be_num > 0) {
267		/* at least we have dmic01 */
268		links[id].name = "dmic01";
269		links[id].cpus = &cpus[id];
270		links[id].cpus->dai_name = "DMIC01 Pin";
271		if (dmic_be_num > 1) {
272			/* set up 2 BE links at most */
273			links[id + 1].name = "dmic16k";
274			links[id + 1].cpus = &cpus[id + 1];
275			links[id + 1].cpus->dai_name = "DMIC16k Pin";
276			dmic_be_num = 2;
277		}
278	}
279
280	for (i = 0; i < dmic_be_num; i++) {
281		links[id].id = id;
282		links[id].num_cpus = 1;
283		links[id].codecs = dmic_component;
284		links[id].num_codecs = ARRAY_SIZE(dmic_component);
285		links[id].platforms = platform_component;
286		links[id].num_platforms = ARRAY_SIZE(platform_component);
287		links[id].ignore_suspend = 1;
288		links[id].dpcm_capture = 1;
289		links[id].no_pcm = 1;
290		id++;
291	}
292
293	/* HDMI playback */
294	if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) {
295		/* HDMI */
296		if (hdmi_num > 0) {
297			idisp_components = devm_kcalloc(dev,
298					   hdmi_num,
299					   sizeof(struct snd_soc_dai_link_component),
300					   GFP_KERNEL);
301			if (!idisp_components)
302				goto devm_err;
303		}
304		for (i = 1; i <= hdmi_num; i++) {
305			links[id].name = devm_kasprintf(dev, GFP_KERNEL,
306							"iDisp%d", i);
307			if (!links[id].name)
308				goto devm_err;
309
310			links[id].id = id;
311			links[id].cpus = &cpus[id];
312			links[id].num_cpus = 1;
313			links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
314								  "iDisp%d Pin", i);
315			if (!links[id].cpus->dai_name)
316				goto devm_err;
317
318			if (idisp_codec) {
319				idisp_components[i - 1].name = "ehdaudio0D2";
320				idisp_components[i - 1].dai_name = devm_kasprintf(dev,
321										  GFP_KERNEL,
322										  "intel-hdmi-hifi%d",
323										  i);
324				if (!idisp_components[i - 1].dai_name)
325					goto devm_err;
326			} else {
327				idisp_components[i - 1] = asoc_dummy_dlc;
328			}
329
330			links[id].codecs = &idisp_components[i - 1];
331			links[id].num_codecs = 1;
332			links[id].platforms = platform_component;
333			links[id].num_platforms = ARRAY_SIZE(platform_component);
334			links[id].init = sof_hdmi_init;
335			links[id].dpcm_playback = 1;
336			links[id].no_pcm = 1;
337			id++;
338		}
339	}
340
341	/* BT audio offload */
342	if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
343		int port = (sof_ssp_amp_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
344				SOF_BT_OFFLOAD_SSP_SHIFT;
345
346		links[id].id = id;
347		links[id].cpus = &cpus[id];
348		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
349							  "SSP%d Pin", port);
350		if (!links[id].cpus->dai_name)
351			goto devm_err;
352		links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
353		if (!links[id].name)
354			goto devm_err;
355		links[id].codecs = &asoc_dummy_dlc;
356		links[id].num_codecs = 1;
357		links[id].platforms = platform_component;
358		links[id].num_platforms = ARRAY_SIZE(platform_component);
359		links[id].dpcm_playback = 1;
360		links[id].dpcm_capture = 1;
361		links[id].no_pcm = 1;
362		links[id].num_cpus = 1;
363		id++;
364	}
365
366	return links;
367devm_err:
368	return NULL;
369}
370
371static int sof_ssp_amp_probe(struct platform_device *pdev)
372{
373	struct snd_soc_dai_link *dai_links;
374	struct snd_soc_acpi_mach *mach;
375	struct sof_card_private *ctx;
376	int dmic_be_num = 0, hdmi_num = 0;
377	int ret, ssp_codec;
378
379	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
380	if (!ctx)
381		return -ENOMEM;
382
383	if (pdev->id_entry && pdev->id_entry->driver_data)
384		sof_ssp_amp_quirk = (unsigned long)pdev->id_entry->driver_data;
385
386	mach = pdev->dev.platform_data;
387
388	if (dmi_check_system(chromebook_platforms) || mach->mach_params.dmic_num > 0)
389		dmic_be_num = 2;
390
391	ssp_codec = sof_ssp_amp_quirk & SOF_AMPLIFIER_SSP_MASK;
392
393	/* set number of dai links */
394	sof_ssp_amp_card.num_links = 1 + dmic_be_num;
395
396	if (sof_ssp_amp_quirk & SOF_SSP_HDMI_CAPTURE_PRESENT)
397		sof_ssp_amp_card.num_links += (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_CAPTURE_SSP_MASK) >>
398				SOF_NO_OF_HDMI_CAPTURE_SSP_SHIFT;
399
400	if (sof_ssp_amp_quirk & SOF_HDMI_PLAYBACK_PRESENT) {
401		hdmi_num = (sof_ssp_amp_quirk & SOF_NO_OF_HDMI_PLAYBACK_MASK) >>
402				SOF_NO_OF_HDMI_PLAYBACK_SHIFT;
403		/* default number of HDMI DAI's */
404		if (!hdmi_num)
405			hdmi_num = 3;
406
407		if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
408			ctx->idisp_codec = true;
409
410		sof_ssp_amp_card.num_links += hdmi_num;
411	}
412
413	if (sof_ssp_amp_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
414		sof_ssp_amp_card.num_links++;
415
416	dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, dmic_be_num, hdmi_num, ctx->idisp_codec);
417	if (!dai_links)
418		return -ENOMEM;
419
420	sof_ssp_amp_card.dai_link = dai_links;
421
422	/* update codec_conf */
423	if (sof_ssp_amp_quirk & SOF_CS35L41_SPEAKER_AMP_PRESENT) {
424		cs35l41_set_codec_conf(&sof_ssp_amp_card);
425	}
426
427	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
428
429	sof_ssp_amp_card.dev = &pdev->dev;
430
431	/* set platform name for each dailink */
432	ret = snd_soc_fixup_dai_links_platform_name(&sof_ssp_amp_card,
433						    mach->mach_params.platform);
434	if (ret)
435		return ret;
436
437	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
438
439	snd_soc_card_set_drvdata(&sof_ssp_amp_card, ctx);
440
441	return devm_snd_soc_register_card(&pdev->dev, &sof_ssp_amp_card);
442}
443
444static const struct platform_device_id board_ids[] = {
445	{
446		.name = "sof_ssp_amp",
447	},
448	{
449		.name = "tgl_rt1308_hdmi_ssp",
450		.driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(2) |
451					SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
452					SOF_HDMI_CAPTURE_1_SSP(1) |
453					SOF_HDMI_CAPTURE_2_SSP(5) |
454					SOF_SSP_HDMI_CAPTURE_PRESENT |
455					SOF_RT1308_SPEAKER_AMP_PRESENT),
456	},
457	{
458		.name = "adl_cs35l41",
459		.driver_data = (kernel_ulong_t)(SOF_AMPLIFIER_SSP(1) |
460					SOF_NO_OF_HDMI_PLAYBACK(4) |
461					SOF_HDMI_PLAYBACK_PRESENT |
462					SOF_BT_OFFLOAD_SSP(2) |
463					SOF_SSP_BT_OFFLOAD_PRESENT |
464					SOF_CS35L41_SPEAKER_AMP_PRESENT),
465	},
466	{
467		.name = "adl_lt6911_hdmi_ssp",
468		.driver_data = (kernel_ulong_t)(SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
469					SOF_HDMI_CAPTURE_1_SSP(0) |
470					SOF_HDMI_CAPTURE_2_SSP(2) |
471					SOF_SSP_HDMI_CAPTURE_PRESENT |
472					SOF_NO_OF_HDMI_PLAYBACK(3) |
473					SOF_HDMI_PLAYBACK_PRESENT),
474	},
475	{
476		.name = "rpl_lt6911_hdmi_ssp",
477		.driver_data = (kernel_ulong_t)(SOF_NO_OF_HDMI_CAPTURE_SSP(2) |
478					SOF_HDMI_CAPTURE_1_SSP(0) |
479					SOF_HDMI_CAPTURE_2_SSP(2) |
480					SOF_SSP_HDMI_CAPTURE_PRESENT |
481					SOF_NO_OF_HDMI_PLAYBACK(3) |
482					SOF_HDMI_PLAYBACK_PRESENT),
483	},
484	{ }
485};
486MODULE_DEVICE_TABLE(platform, board_ids);
487
488static struct platform_driver sof_ssp_amp_driver = {
489	.probe          = sof_ssp_amp_probe,
490	.driver = {
491		.name   = "sof_ssp_amp",
492		.pm = &snd_soc_pm_ops,
493	},
494	.id_table = board_ids,
495};
496module_platform_driver(sof_ssp_amp_driver);
497
498MODULE_DESCRIPTION("ASoC Intel(R) SOF Amplifier Machine driver");
499MODULE_AUTHOR("Balamurugan C <balamurugan.c@intel.com>");
500MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
501MODULE_LICENSE("GPL");
502MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
503MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);
504MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_CIRRUS_COMMON);
505