xref: /linux/sound/soc/intel/boards/sof_pcm512x.c (revision f6081af6cf2b4fda929638e8cec4cd2f9487dd9e)
1e149ca29SPierre-Louis Bossart // SPDX-License-Identifier: GPL-2.0-only
2f40ed2e8SPierre-Louis Bossart // Copyright(c) 2018-2020 Intel Corporation.
3f40ed2e8SPierre-Louis Bossart 
4f40ed2e8SPierre-Louis Bossart /*
5f40ed2e8SPierre-Louis Bossart  * Intel SOF Machine Driver for Intel platforms with TI PCM512x codec,
6f40ed2e8SPierre-Louis Bossart  * e.g. Up or Up2 with Hifiberry DAC+ HAT
7f40ed2e8SPierre-Louis Bossart  */
8f40ed2e8SPierre-Louis Bossart #include <linux/clk.h>
9f40ed2e8SPierre-Louis Bossart #include <linux/dmi.h>
10f40ed2e8SPierre-Louis Bossart #include <linux/i2c.h>
11f40ed2e8SPierre-Louis Bossart #include <linux/input.h>
12f40ed2e8SPierre-Louis Bossart #include <linux/module.h>
13f40ed2e8SPierre-Louis Bossart #include <linux/platform_device.h>
14f40ed2e8SPierre-Louis Bossart #include <linux/types.h>
15f40ed2e8SPierre-Louis Bossart #include <sound/core.h>
16f40ed2e8SPierre-Louis Bossart #include <sound/jack.h>
17f40ed2e8SPierre-Louis Bossart #include <sound/pcm.h>
18f40ed2e8SPierre-Louis Bossart #include <sound/pcm_params.h>
19f40ed2e8SPierre-Louis Bossart #include <sound/soc.h>
20f40ed2e8SPierre-Louis Bossart #include <sound/soc-acpi.h>
21f40ed2e8SPierre-Louis Bossart #include "../../codecs/pcm512x.h"
22f40ed2e8SPierre-Louis Bossart #include "../common/soc-intel-quirks.h"
23f40ed2e8SPierre-Louis Bossart #include "hda_dsp_common.h"
24f40ed2e8SPierre-Louis Bossart 
25f40ed2e8SPierre-Louis Bossart #define NAME_SIZE 32
26f40ed2e8SPierre-Louis Bossart 
27f40ed2e8SPierre-Louis Bossart #define SOF_PCM512X_SSP_CODEC(quirk)		((quirk) & GENMASK(3, 0))
28f40ed2e8SPierre-Louis Bossart #define SOF_PCM512X_SSP_CODEC_MASK			(GENMASK(3, 0))
29f40ed2e8SPierre-Louis Bossart 
3015a5a895SKai Vehmanen #define IDISP_CODEC_MASK	0x4
3115a5a895SKai Vehmanen 
32f40ed2e8SPierre-Louis Bossart /* Default: SSP5 */
33f40ed2e8SPierre-Louis Bossart static unsigned long sof_pcm512x_quirk = SOF_PCM512X_SSP_CODEC(5);
34f40ed2e8SPierre-Louis Bossart 
35f40ed2e8SPierre-Louis Bossart static bool is_legacy_cpu;
36f40ed2e8SPierre-Louis Bossart 
37f40ed2e8SPierre-Louis Bossart struct sof_hdmi_pcm {
38f40ed2e8SPierre-Louis Bossart 	struct list_head head;
39f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai *codec_dai;
40f40ed2e8SPierre-Louis Bossart 	int device;
41f40ed2e8SPierre-Louis Bossart };
42f40ed2e8SPierre-Louis Bossart 
43f40ed2e8SPierre-Louis Bossart struct sof_card_private {
44f40ed2e8SPierre-Louis Bossart 	struct list_head hdmi_pcm_list;
4515a5a895SKai Vehmanen 	bool idisp_codec;
46f40ed2e8SPierre-Louis Bossart };
47f40ed2e8SPierre-Louis Bossart 
48f40ed2e8SPierre-Louis Bossart static int sof_pcm512x_quirk_cb(const struct dmi_system_id *id)
49f40ed2e8SPierre-Louis Bossart {
50f40ed2e8SPierre-Louis Bossart 	sof_pcm512x_quirk = (unsigned long)id->driver_data;
51f40ed2e8SPierre-Louis Bossart 	return 1;
52f40ed2e8SPierre-Louis Bossart }
53f40ed2e8SPierre-Louis Bossart 
54f40ed2e8SPierre-Louis Bossart static const struct dmi_system_id sof_pcm512x_quirk_table[] = {
55f40ed2e8SPierre-Louis Bossart 	{
56f40ed2e8SPierre-Louis Bossart 		.callback = sof_pcm512x_quirk_cb,
57f40ed2e8SPierre-Louis Bossart 		.matches = {
58f40ed2e8SPierre-Louis Bossart 			DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
59f40ed2e8SPierre-Louis Bossart 			DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
60f40ed2e8SPierre-Louis Bossart 		},
61f40ed2e8SPierre-Louis Bossart 		.driver_data = (void *)(SOF_PCM512X_SSP_CODEC(2)),
62f40ed2e8SPierre-Louis Bossart 	},
63f40ed2e8SPierre-Louis Bossart 	{}
64f40ed2e8SPierre-Louis Bossart };
65f40ed2e8SPierre-Louis Bossart 
66f40ed2e8SPierre-Louis Bossart static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
67f40ed2e8SPierre-Louis Bossart {
68f40ed2e8SPierre-Louis Bossart 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
690d1571c1SKuninori Morimoto 	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
70f40ed2e8SPierre-Louis Bossart 	struct sof_hdmi_pcm *pcm;
71f40ed2e8SPierre-Louis Bossart 
72f40ed2e8SPierre-Louis Bossart 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
73f40ed2e8SPierre-Louis Bossart 	if (!pcm)
74f40ed2e8SPierre-Louis Bossart 		return -ENOMEM;
75f40ed2e8SPierre-Louis Bossart 
76f40ed2e8SPierre-Louis Bossart 	/* dai_link id is 1:1 mapped to the PCM device */
77f40ed2e8SPierre-Louis Bossart 	pcm->device = rtd->dai_link->id;
78f40ed2e8SPierre-Louis Bossart 	pcm->codec_dai = dai;
79f40ed2e8SPierre-Louis Bossart 
80f40ed2e8SPierre-Louis Bossart 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
81f40ed2e8SPierre-Louis Bossart 
82f40ed2e8SPierre-Louis Bossart 	return 0;
83f40ed2e8SPierre-Louis Bossart }
84f40ed2e8SPierre-Louis Bossart 
85f40ed2e8SPierre-Louis Bossart static int sof_pcm512x_codec_init(struct snd_soc_pcm_runtime *rtd)
86f40ed2e8SPierre-Louis Bossart {
870d1571c1SKuninori Morimoto 	struct snd_soc_component *codec = asoc_rtd_to_codec(rtd, 0)->component;
88f40ed2e8SPierre-Louis Bossart 
89f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_EN, 0x08, 0x08);
90f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
91f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_CONTROL_1,
92f40ed2e8SPierre-Louis Bossart 				      0x08, 0x08);
93f40ed2e8SPierre-Louis Bossart 
94f40ed2e8SPierre-Louis Bossart 	return 0;
95f40ed2e8SPierre-Louis Bossart }
96f40ed2e8SPierre-Louis Bossart 
97f40ed2e8SPierre-Louis Bossart static int aif1_startup(struct snd_pcm_substream *substream)
98f40ed2e8SPierre-Louis Bossart {
992207b93bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1000d1571c1SKuninori Morimoto 	struct snd_soc_component *codec = asoc_rtd_to_codec(rtd, 0)->component;
101f40ed2e8SPierre-Louis Bossart 
102f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_CONTROL_1,
103f40ed2e8SPierre-Louis Bossart 				      0x08, 0x08);
104f40ed2e8SPierre-Louis Bossart 
105f40ed2e8SPierre-Louis Bossart 	return 0;
106f40ed2e8SPierre-Louis Bossart }
107f40ed2e8SPierre-Louis Bossart 
108f40ed2e8SPierre-Louis Bossart static void aif1_shutdown(struct snd_pcm_substream *substream)
109f40ed2e8SPierre-Louis Bossart {
1102207b93bSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1110d1571c1SKuninori Morimoto 	struct snd_soc_component *codec = asoc_rtd_to_codec(rtd, 0)->component;
112f40ed2e8SPierre-Louis Bossart 
113f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_CONTROL_1,
114f40ed2e8SPierre-Louis Bossart 				      0x08, 0x00);
115f40ed2e8SPierre-Louis Bossart }
116f40ed2e8SPierre-Louis Bossart 
117f40ed2e8SPierre-Louis Bossart static const struct snd_soc_ops sof_pcm512x_ops = {
118f40ed2e8SPierre-Louis Bossart 	.startup = aif1_startup,
119f40ed2e8SPierre-Louis Bossart 	.shutdown = aif1_shutdown,
120f40ed2e8SPierre-Louis Bossart };
121f40ed2e8SPierre-Louis Bossart 
122f40ed2e8SPierre-Louis Bossart static struct snd_soc_dai_link_component platform_component[] = {
123f40ed2e8SPierre-Louis Bossart 	{
124f40ed2e8SPierre-Louis Bossart 		/* name might be overridden during probe */
125f40ed2e8SPierre-Louis Bossart 		.name = "0000:00:1f.3"
126f40ed2e8SPierre-Louis Bossart 	}
127f40ed2e8SPierre-Louis Bossart };
128f40ed2e8SPierre-Louis Bossart 
129f40ed2e8SPierre-Louis Bossart static int sof_card_late_probe(struct snd_soc_card *card)
130f40ed2e8SPierre-Louis Bossart {
131f40ed2e8SPierre-Louis Bossart 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
132f40ed2e8SPierre-Louis Bossart 	struct sof_hdmi_pcm *pcm;
133f40ed2e8SPierre-Louis Bossart 
134f40ed2e8SPierre-Louis Bossart 	/* HDMI is not supported by SOF on Baytrail/CherryTrail */
135f40ed2e8SPierre-Louis Bossart 	if (is_legacy_cpu)
136f40ed2e8SPierre-Louis Bossart 		return 0;
137f40ed2e8SPierre-Louis Bossart 
138f40ed2e8SPierre-Louis Bossart 	if (list_empty(&ctx->hdmi_pcm_list))
139f40ed2e8SPierre-Louis Bossart 		return -EINVAL;
140f40ed2e8SPierre-Louis Bossart 
14115a5a895SKai Vehmanen 	if (!ctx->idisp_codec)
14215a5a895SKai Vehmanen 		return 0;
14315a5a895SKai Vehmanen 
144f40ed2e8SPierre-Louis Bossart 	pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, head);
145f40ed2e8SPierre-Louis Bossart 
146f40ed2e8SPierre-Louis Bossart 	return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
147f40ed2e8SPierre-Louis Bossart }
148f40ed2e8SPierre-Louis Bossart 
149f40ed2e8SPierre-Louis Bossart static const struct snd_kcontrol_new sof_controls[] = {
150f40ed2e8SPierre-Louis Bossart 	SOC_DAPM_PIN_SWITCH("Ext Spk"),
151f40ed2e8SPierre-Louis Bossart };
152f40ed2e8SPierre-Louis Bossart 
153f40ed2e8SPierre-Louis Bossart static const struct snd_soc_dapm_widget sof_widgets[] = {
154f40ed2e8SPierre-Louis Bossart 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
155f40ed2e8SPierre-Louis Bossart };
156f40ed2e8SPierre-Louis Bossart 
157f40ed2e8SPierre-Louis Bossart static const struct snd_soc_dapm_widget dmic_widgets[] = {
158f40ed2e8SPierre-Louis Bossart 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
159f40ed2e8SPierre-Louis Bossart };
160f40ed2e8SPierre-Louis Bossart 
161f40ed2e8SPierre-Louis Bossart static const struct snd_soc_dapm_route sof_map[] = {
162f40ed2e8SPierre-Louis Bossart 	/* Speaker */
163f40ed2e8SPierre-Louis Bossart 	{"Ext Spk", NULL, "OUTR"},
164f40ed2e8SPierre-Louis Bossart 	{"Ext Spk", NULL, "OUTL"},
165f40ed2e8SPierre-Louis Bossart };
166f40ed2e8SPierre-Louis Bossart 
167f40ed2e8SPierre-Louis Bossart static const struct snd_soc_dapm_route dmic_map[] = {
168f40ed2e8SPierre-Louis Bossart 	/* digital mics */
169f40ed2e8SPierre-Louis Bossart 	{"DMic", NULL, "SoC DMIC"},
170f40ed2e8SPierre-Louis Bossart };
171f40ed2e8SPierre-Louis Bossart 
172f40ed2e8SPierre-Louis Bossart static int dmic_init(struct snd_soc_pcm_runtime *rtd)
173f40ed2e8SPierre-Louis Bossart {
174f40ed2e8SPierre-Louis Bossart 	struct snd_soc_card *card = rtd->card;
175f40ed2e8SPierre-Louis Bossart 	int ret;
176f40ed2e8SPierre-Louis Bossart 
177f40ed2e8SPierre-Louis Bossart 	ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
178f40ed2e8SPierre-Louis Bossart 					ARRAY_SIZE(dmic_widgets));
179f40ed2e8SPierre-Louis Bossart 	if (ret) {
180f40ed2e8SPierre-Louis Bossart 		dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
181f40ed2e8SPierre-Louis Bossart 		/* Don't need to add routes if widget addition failed */
182f40ed2e8SPierre-Louis Bossart 		return ret;
183f40ed2e8SPierre-Louis Bossart 	}
184f40ed2e8SPierre-Louis Bossart 
185f40ed2e8SPierre-Louis Bossart 	ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
186f40ed2e8SPierre-Louis Bossart 				      ARRAY_SIZE(dmic_map));
187f40ed2e8SPierre-Louis Bossart 
188f40ed2e8SPierre-Louis Bossart 	if (ret)
189f40ed2e8SPierre-Louis Bossart 		dev_err(card->dev, "DMic map addition failed: %d\n", ret);
190f40ed2e8SPierre-Louis Bossart 
191f40ed2e8SPierre-Louis Bossart 	return ret;
192f40ed2e8SPierre-Louis Bossart }
193f40ed2e8SPierre-Louis Bossart 
194f40ed2e8SPierre-Louis Bossart /* sof audio machine driver for pcm512x codec */
195f40ed2e8SPierre-Louis Bossart static struct snd_soc_card sof_audio_card_pcm512x = {
196f40ed2e8SPierre-Louis Bossart 	.name = "pcm512x",
197f40ed2e8SPierre-Louis Bossart 	.owner = THIS_MODULE,
198f40ed2e8SPierre-Louis Bossart 	.controls = sof_controls,
199f40ed2e8SPierre-Louis Bossart 	.num_controls = ARRAY_SIZE(sof_controls),
200f40ed2e8SPierre-Louis Bossart 	.dapm_widgets = sof_widgets,
201f40ed2e8SPierre-Louis Bossart 	.num_dapm_widgets = ARRAY_SIZE(sof_widgets),
202f40ed2e8SPierre-Louis Bossart 	.dapm_routes = sof_map,
203f40ed2e8SPierre-Louis Bossart 	.num_dapm_routes = ARRAY_SIZE(sof_map),
204f40ed2e8SPierre-Louis Bossart 	.fully_routed = true,
205f40ed2e8SPierre-Louis Bossart 	.late_probe = sof_card_late_probe,
206f40ed2e8SPierre-Louis Bossart };
207f40ed2e8SPierre-Louis Bossart 
208f40ed2e8SPierre-Louis Bossart SND_SOC_DAILINK_DEF(pcm512x_component,
209f40ed2e8SPierre-Louis Bossart 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-104C5122:00", "pcm512x-hifi")));
210f40ed2e8SPierre-Louis Bossart SND_SOC_DAILINK_DEF(dmic_component,
211f40ed2e8SPierre-Louis Bossart 	DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
212f40ed2e8SPierre-Louis Bossart 
213f40ed2e8SPierre-Louis Bossart static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
214f40ed2e8SPierre-Louis Bossart 							  int ssp_codec,
215f40ed2e8SPierre-Louis Bossart 							  int dmic_be_num,
21615a5a895SKai Vehmanen 							  int hdmi_num,
21715a5a895SKai Vehmanen 							  bool idisp_codec)
218f40ed2e8SPierre-Louis Bossart {
219f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai_link_component *idisp_components;
220f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai_link_component *cpus;
221f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai_link *links;
222f40ed2e8SPierre-Louis Bossart 	int i, id = 0;
223f40ed2e8SPierre-Louis Bossart 
224f40ed2e8SPierre-Louis Bossart 	links = devm_kcalloc(dev, sof_audio_card_pcm512x.num_links,
225f40ed2e8SPierre-Louis Bossart 			sizeof(struct snd_soc_dai_link), GFP_KERNEL);
226f40ed2e8SPierre-Louis Bossart 	cpus = devm_kcalloc(dev, sof_audio_card_pcm512x.num_links,
227f40ed2e8SPierre-Louis Bossart 			sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
228f40ed2e8SPierre-Louis Bossart 	if (!links || !cpus)
229f40ed2e8SPierre-Louis Bossart 		goto devm_err;
230f40ed2e8SPierre-Louis Bossart 
231f40ed2e8SPierre-Louis Bossart 	/* codec SSP */
232f40ed2e8SPierre-Louis Bossart 	links[id].name = devm_kasprintf(dev, GFP_KERNEL,
233f40ed2e8SPierre-Louis Bossart 					"SSP%d-Codec", ssp_codec);
234f40ed2e8SPierre-Louis Bossart 	if (!links[id].name)
235f40ed2e8SPierre-Louis Bossart 		goto devm_err;
236f40ed2e8SPierre-Louis Bossart 
237f40ed2e8SPierre-Louis Bossart 	links[id].id = id;
238f40ed2e8SPierre-Louis Bossart 	links[id].codecs = pcm512x_component;
239f40ed2e8SPierre-Louis Bossart 	links[id].num_codecs = ARRAY_SIZE(pcm512x_component);
240f40ed2e8SPierre-Louis Bossart 	links[id].platforms = platform_component;
241f40ed2e8SPierre-Louis Bossart 	links[id].num_platforms = ARRAY_SIZE(platform_component);
242f40ed2e8SPierre-Louis Bossart 	links[id].init = sof_pcm512x_codec_init;
243f40ed2e8SPierre-Louis Bossart 	links[id].ops = &sof_pcm512x_ops;
244f40ed2e8SPierre-Louis Bossart 	links[id].nonatomic = true;
245f40ed2e8SPierre-Louis Bossart 	links[id].dpcm_playback = 1;
246f40ed2e8SPierre-Louis Bossart 	/*
247f40ed2e8SPierre-Louis Bossart 	 * capture only supported with specific versions of the Hifiberry DAC+
248f40ed2e8SPierre-Louis Bossart 	 * links[id].dpcm_capture = 1;
249f40ed2e8SPierre-Louis Bossart 	 */
250f40ed2e8SPierre-Louis Bossart 	links[id].no_pcm = 1;
251f40ed2e8SPierre-Louis Bossart 	links[id].cpus = &cpus[id];
252f40ed2e8SPierre-Louis Bossart 	links[id].num_cpus = 1;
253f40ed2e8SPierre-Louis Bossart 	if (is_legacy_cpu) {
254f40ed2e8SPierre-Louis Bossart 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
255f40ed2e8SPierre-Louis Bossart 							  "ssp%d-port",
256f40ed2e8SPierre-Louis Bossart 							  ssp_codec);
257f40ed2e8SPierre-Louis Bossart 		if (!links[id].cpus->dai_name)
258f40ed2e8SPierre-Louis Bossart 			goto devm_err;
259f40ed2e8SPierre-Louis Bossart 	} else {
260f40ed2e8SPierre-Louis Bossart 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
261f40ed2e8SPierre-Louis Bossart 							  "SSP%d Pin",
262f40ed2e8SPierre-Louis Bossart 							  ssp_codec);
263f40ed2e8SPierre-Louis Bossart 		if (!links[id].cpus->dai_name)
264f40ed2e8SPierre-Louis Bossart 			goto devm_err;
265f40ed2e8SPierre-Louis Bossart 	}
266f40ed2e8SPierre-Louis Bossart 	id++;
267f40ed2e8SPierre-Louis Bossart 
268f40ed2e8SPierre-Louis Bossart 	/* dmic */
269f40ed2e8SPierre-Louis Bossart 	if (dmic_be_num > 0) {
270f40ed2e8SPierre-Louis Bossart 		/* at least we have dmic01 */
271f40ed2e8SPierre-Louis Bossart 		links[id].name = "dmic01";
272f40ed2e8SPierre-Louis Bossart 		links[id].cpus = &cpus[id];
273f40ed2e8SPierre-Louis Bossart 		links[id].cpus->dai_name = "DMIC01 Pin";
274f40ed2e8SPierre-Louis Bossart 		links[id].init = dmic_init;
275f40ed2e8SPierre-Louis Bossart 		if (dmic_be_num > 1) {
276f40ed2e8SPierre-Louis Bossart 			/* set up 2 BE links at most */
277f40ed2e8SPierre-Louis Bossart 			links[id + 1].name = "dmic16k";
278f40ed2e8SPierre-Louis Bossart 			links[id + 1].cpus = &cpus[id + 1];
279f40ed2e8SPierre-Louis Bossart 			links[id + 1].cpus->dai_name = "DMIC16k Pin";
280f40ed2e8SPierre-Louis Bossart 			dmic_be_num = 2;
281f40ed2e8SPierre-Louis Bossart 		}
282f40ed2e8SPierre-Louis Bossart 	}
283f40ed2e8SPierre-Louis Bossart 
284f40ed2e8SPierre-Louis Bossart 	for (i = 0; i < dmic_be_num; i++) {
285f40ed2e8SPierre-Louis Bossart 		links[id].id = id;
286f40ed2e8SPierre-Louis Bossart 		links[id].num_cpus = 1;
287f40ed2e8SPierre-Louis Bossart 		links[id].codecs = dmic_component;
288f40ed2e8SPierre-Louis Bossart 		links[id].num_codecs = ARRAY_SIZE(dmic_component);
289f40ed2e8SPierre-Louis Bossart 		links[id].platforms = platform_component;
290f40ed2e8SPierre-Louis Bossart 		links[id].num_platforms = ARRAY_SIZE(platform_component);
291f40ed2e8SPierre-Louis Bossart 		links[id].ignore_suspend = 1;
292f40ed2e8SPierre-Louis Bossart 		links[id].dpcm_capture = 1;
293f40ed2e8SPierre-Louis Bossart 		links[id].no_pcm = 1;
294f40ed2e8SPierre-Louis Bossart 		id++;
295f40ed2e8SPierre-Louis Bossart 	}
296f40ed2e8SPierre-Louis Bossart 
297f40ed2e8SPierre-Louis Bossart 	/* HDMI */
298f40ed2e8SPierre-Louis Bossart 	if (hdmi_num > 0) {
299f40ed2e8SPierre-Louis Bossart 		idisp_components = devm_kcalloc(dev, hdmi_num,
300f40ed2e8SPierre-Louis Bossart 				sizeof(struct snd_soc_dai_link_component),
301f40ed2e8SPierre-Louis Bossart 				GFP_KERNEL);
302f40ed2e8SPierre-Louis Bossart 		if (!idisp_components)
303f40ed2e8SPierre-Louis Bossart 			goto devm_err;
304f40ed2e8SPierre-Louis Bossart 	}
305f40ed2e8SPierre-Louis Bossart 	for (i = 1; i <= hdmi_num; i++) {
306f40ed2e8SPierre-Louis Bossart 		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
307f40ed2e8SPierre-Louis Bossart 						"iDisp%d", i);
308f40ed2e8SPierre-Louis Bossart 		if (!links[id].name)
309f40ed2e8SPierre-Louis Bossart 			goto devm_err;
310f40ed2e8SPierre-Louis Bossart 
311f40ed2e8SPierre-Louis Bossart 		links[id].id = id;
312f40ed2e8SPierre-Louis Bossart 		links[id].cpus = &cpus[id];
313f40ed2e8SPierre-Louis Bossart 		links[id].num_cpus = 1;
314f40ed2e8SPierre-Louis Bossart 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
315f40ed2e8SPierre-Louis Bossart 							  "iDisp%d Pin", i);
316f40ed2e8SPierre-Louis Bossart 		if (!links[id].cpus->dai_name)
317f40ed2e8SPierre-Louis Bossart 			goto devm_err;
318f40ed2e8SPierre-Louis Bossart 
31915a5a895SKai Vehmanen 		/*
32015a5a895SKai Vehmanen 		 * topology cannot be loaded if codec is missing, so
32115a5a895SKai Vehmanen 		 * use the dummy codec if needed
32215a5a895SKai Vehmanen 		 */
32315a5a895SKai Vehmanen 		if (idisp_codec) {
324f40ed2e8SPierre-Louis Bossart 			idisp_components[i - 1].name = "ehdaudio0D2";
32515a5a895SKai Vehmanen 			idisp_components[i - 1].dai_name =
32615a5a895SKai Vehmanen 				devm_kasprintf(dev, GFP_KERNEL,
32715a5a895SKai Vehmanen 					       "intel-hdmi-hifi%d", i);
32815a5a895SKai Vehmanen 		} else {
32915a5a895SKai Vehmanen 			idisp_components[i - 1].name = "snd-soc-dummy";
33015a5a895SKai Vehmanen 			idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
33115a5a895SKai Vehmanen 		}
332f40ed2e8SPierre-Louis Bossart 		if (!idisp_components[i - 1].dai_name)
333f40ed2e8SPierre-Louis Bossart 			goto devm_err;
334f40ed2e8SPierre-Louis Bossart 
335f40ed2e8SPierre-Louis Bossart 		links[id].codecs = &idisp_components[i - 1];
336f40ed2e8SPierre-Louis Bossart 		links[id].num_codecs = 1;
337f40ed2e8SPierre-Louis Bossart 		links[id].platforms = platform_component;
338f40ed2e8SPierre-Louis Bossart 		links[id].num_platforms = ARRAY_SIZE(platform_component);
339f40ed2e8SPierre-Louis Bossart 		links[id].init = sof_hdmi_init;
340f40ed2e8SPierre-Louis Bossart 		links[id].dpcm_playback = 1;
341f40ed2e8SPierre-Louis Bossart 		links[id].no_pcm = 1;
342f40ed2e8SPierre-Louis Bossart 		id++;
343f40ed2e8SPierre-Louis Bossart 	}
344f40ed2e8SPierre-Louis Bossart 
345f40ed2e8SPierre-Louis Bossart 	return links;
346f40ed2e8SPierre-Louis Bossart devm_err:
347f40ed2e8SPierre-Louis Bossart 	return NULL;
348f40ed2e8SPierre-Louis Bossart }
349f40ed2e8SPierre-Louis Bossart 
350f40ed2e8SPierre-Louis Bossart static int sof_audio_probe(struct platform_device *pdev)
351f40ed2e8SPierre-Louis Bossart {
35215a5a895SKai Vehmanen 	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
353f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai_link *dai_links;
354f40ed2e8SPierre-Louis Bossart 	struct sof_card_private *ctx;
355f40ed2e8SPierre-Louis Bossart 	int dmic_be_num, hdmi_num;
356f40ed2e8SPierre-Louis Bossart 	int ret, ssp_codec;
357f40ed2e8SPierre-Louis Bossart 
358f40ed2e8SPierre-Louis Bossart 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
359f40ed2e8SPierre-Louis Bossart 	if (!ctx)
360f40ed2e8SPierre-Louis Bossart 		return -ENOMEM;
361f40ed2e8SPierre-Louis Bossart 
362f40ed2e8SPierre-Louis Bossart 	hdmi_num = 0;
363f40ed2e8SPierre-Louis Bossart 	if (soc_intel_is_byt() || soc_intel_is_cht()) {
364f40ed2e8SPierre-Louis Bossart 		is_legacy_cpu = true;
365f40ed2e8SPierre-Louis Bossart 		dmic_be_num = 0;
366f40ed2e8SPierre-Louis Bossart 		/* default quirk for legacy cpu */
367f40ed2e8SPierre-Louis Bossart 		sof_pcm512x_quirk = SOF_PCM512X_SSP_CODEC(2);
368f40ed2e8SPierre-Louis Bossart 	} else {
369f40ed2e8SPierre-Louis Bossart 		dmic_be_num = 2;
37015a5a895SKai Vehmanen 		if (mach->mach_params.common_hdmi_codec_drv &&
37115a5a895SKai Vehmanen 		    (mach->mach_params.codec_mask & IDISP_CODEC_MASK))
37215a5a895SKai Vehmanen 			ctx->idisp_codec = true;
37315a5a895SKai Vehmanen 
37415a5a895SKai Vehmanen 		/* links are always present in topology */
375f40ed2e8SPierre-Louis Bossart 		hdmi_num = 3;
376f40ed2e8SPierre-Louis Bossart 	}
377f40ed2e8SPierre-Louis Bossart 
378f40ed2e8SPierre-Louis Bossart 	dmi_check_system(sof_pcm512x_quirk_table);
379f40ed2e8SPierre-Louis Bossart 
380f40ed2e8SPierre-Louis Bossart 	dev_dbg(&pdev->dev, "sof_pcm512x_quirk = %lx\n", sof_pcm512x_quirk);
381f40ed2e8SPierre-Louis Bossart 
382f40ed2e8SPierre-Louis Bossart 	ssp_codec = sof_pcm512x_quirk & SOF_PCM512X_SSP_CODEC_MASK;
383f40ed2e8SPierre-Louis Bossart 
384f40ed2e8SPierre-Louis Bossart 	/* compute number of dai links */
385f40ed2e8SPierre-Louis Bossart 	sof_audio_card_pcm512x.num_links = 1 + dmic_be_num + hdmi_num;
386f40ed2e8SPierre-Louis Bossart 
387f40ed2e8SPierre-Louis Bossart 	dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec,
38815a5a895SKai Vehmanen 					      dmic_be_num, hdmi_num,
38915a5a895SKai Vehmanen 					      ctx->idisp_codec);
390f40ed2e8SPierre-Louis Bossart 	if (!dai_links)
391f40ed2e8SPierre-Louis Bossart 		return -ENOMEM;
392f40ed2e8SPierre-Louis Bossart 
393f40ed2e8SPierre-Louis Bossart 	sof_audio_card_pcm512x.dai_link = dai_links;
394f40ed2e8SPierre-Louis Bossart 
395f40ed2e8SPierre-Louis Bossart 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
396f40ed2e8SPierre-Louis Bossart 
397f40ed2e8SPierre-Louis Bossart 	sof_audio_card_pcm512x.dev = &pdev->dev;
398f40ed2e8SPierre-Louis Bossart 
399f40ed2e8SPierre-Louis Bossart 	/* set platform name for each dailink */
400f40ed2e8SPierre-Louis Bossart 	ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_pcm512x,
401f40ed2e8SPierre-Louis Bossart 						    mach->mach_params.platform);
402f40ed2e8SPierre-Louis Bossart 	if (ret)
403f40ed2e8SPierre-Louis Bossart 		return ret;
404f40ed2e8SPierre-Louis Bossart 
405f40ed2e8SPierre-Louis Bossart 	snd_soc_card_set_drvdata(&sof_audio_card_pcm512x, ctx);
406f40ed2e8SPierre-Louis Bossart 
407f40ed2e8SPierre-Louis Bossart 	return devm_snd_soc_register_card(&pdev->dev,
408f40ed2e8SPierre-Louis Bossart 					  &sof_audio_card_pcm512x);
409f40ed2e8SPierre-Louis Bossart }
410f40ed2e8SPierre-Louis Bossart 
411f40ed2e8SPierre-Louis Bossart static int sof_pcm512x_remove(struct platform_device *pdev)
412f40ed2e8SPierre-Louis Bossart {
413f40ed2e8SPierre-Louis Bossart 	struct snd_soc_card *card = platform_get_drvdata(pdev);
414f40ed2e8SPierre-Louis Bossart 	struct snd_soc_component *component = NULL;
415f40ed2e8SPierre-Louis Bossart 
416f40ed2e8SPierre-Louis Bossart 	for_each_card_components(card, component) {
417f40ed2e8SPierre-Louis Bossart 		if (!strcmp(component->name, pcm512x_component[0].name)) {
418f40ed2e8SPierre-Louis Bossart 			snd_soc_component_set_jack(component, NULL, NULL);
419f40ed2e8SPierre-Louis Bossart 			break;
420f40ed2e8SPierre-Louis Bossart 		}
421f40ed2e8SPierre-Louis Bossart 	}
422f40ed2e8SPierre-Louis Bossart 
423f40ed2e8SPierre-Louis Bossart 	return 0;
424f40ed2e8SPierre-Louis Bossart }
425f40ed2e8SPierre-Louis Bossart 
426f40ed2e8SPierre-Louis Bossart static struct platform_driver sof_audio = {
427f40ed2e8SPierre-Louis Bossart 	.probe = sof_audio_probe,
428f40ed2e8SPierre-Louis Bossart 	.remove = sof_pcm512x_remove,
429f40ed2e8SPierre-Louis Bossart 	.driver = {
430f40ed2e8SPierre-Louis Bossart 		.name = "sof_pcm512x",
431f40ed2e8SPierre-Louis Bossart 		.pm = &snd_soc_pm_ops,
432f40ed2e8SPierre-Louis Bossart 	},
433f40ed2e8SPierre-Louis Bossart };
434f40ed2e8SPierre-Louis Bossart module_platform_driver(sof_audio)
435f40ed2e8SPierre-Louis Bossart 
436f40ed2e8SPierre-Louis Bossart MODULE_DESCRIPTION("ASoC Intel(R) SOF + PCM512x Machine driver");
437f40ed2e8SPierre-Louis Bossart MODULE_AUTHOR("Pierre-Louis Bossart");
438f40ed2e8SPierre-Louis Bossart MODULE_LICENSE("GPL v2");
439f40ed2e8SPierre-Louis Bossart MODULE_ALIAS("platform:sof_pcm512x");
440*f6081af6SPierre-Louis Bossart MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
441