xref: /linux/sound/soc/intel/boards/sof_pcm512x.c (revision f22a351fe2193dac803fc919096b734ff2947958)
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))
2922414cadSPierre-Louis Bossart #define SOF_PCM512X_ENABLE_SSP_CAPTURE		BIT(4)
3022414cadSPierre-Louis Bossart #define SOF_PCM512X_ENABLE_DMIC			BIT(5)
31f40ed2e8SPierre-Louis Bossart 
3215a5a895SKai Vehmanen #define IDISP_CODEC_MASK	0x4
3315a5a895SKai Vehmanen 
34f40ed2e8SPierre-Louis Bossart /* Default: SSP5 */
3522414cadSPierre-Louis Bossart static unsigned long sof_pcm512x_quirk =
3622414cadSPierre-Louis Bossart 	SOF_PCM512X_SSP_CODEC(5) |
3722414cadSPierre-Louis Bossart 	SOF_PCM512X_ENABLE_SSP_CAPTURE |
3822414cadSPierre-Louis Bossart 	SOF_PCM512X_ENABLE_DMIC;
39f40ed2e8SPierre-Louis Bossart 
40f40ed2e8SPierre-Louis Bossart static bool is_legacy_cpu;
41f40ed2e8SPierre-Louis Bossart 
42f40ed2e8SPierre-Louis Bossart struct sof_hdmi_pcm {
43f40ed2e8SPierre-Louis Bossart 	struct list_head head;
44f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai *codec_dai;
45f40ed2e8SPierre-Louis Bossart 	int device;
46f40ed2e8SPierre-Louis Bossart };
47f40ed2e8SPierre-Louis Bossart 
48f40ed2e8SPierre-Louis Bossart struct sof_card_private {
49f40ed2e8SPierre-Louis Bossart 	struct list_head hdmi_pcm_list;
5015a5a895SKai Vehmanen 	bool idisp_codec;
51f40ed2e8SPierre-Louis Bossart };
52f40ed2e8SPierre-Louis Bossart 
53f40ed2e8SPierre-Louis Bossart static int sof_pcm512x_quirk_cb(const struct dmi_system_id *id)
54f40ed2e8SPierre-Louis Bossart {
55f40ed2e8SPierre-Louis Bossart 	sof_pcm512x_quirk = (unsigned long)id->driver_data;
56f40ed2e8SPierre-Louis Bossart 	return 1;
57f40ed2e8SPierre-Louis Bossart }
58f40ed2e8SPierre-Louis Bossart 
59f40ed2e8SPierre-Louis Bossart static const struct dmi_system_id sof_pcm512x_quirk_table[] = {
60f40ed2e8SPierre-Louis Bossart 	{
61f40ed2e8SPierre-Louis Bossart 		.callback = sof_pcm512x_quirk_cb,
62f40ed2e8SPierre-Louis Bossart 		.matches = {
63f40ed2e8SPierre-Louis Bossart 			DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
64f40ed2e8SPierre-Louis Bossart 			DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
65f40ed2e8SPierre-Louis Bossart 		},
66f40ed2e8SPierre-Louis Bossart 		.driver_data = (void *)(SOF_PCM512X_SSP_CODEC(2)),
67f40ed2e8SPierre-Louis Bossart 	},
68f40ed2e8SPierre-Louis Bossart 	{}
69f40ed2e8SPierre-Louis Bossart };
70f40ed2e8SPierre-Louis Bossart 
71f40ed2e8SPierre-Louis Bossart static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
72f40ed2e8SPierre-Louis Bossart {
73f40ed2e8SPierre-Louis Bossart 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
74a2c1125eSKuninori Morimoto 	struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0);
75f40ed2e8SPierre-Louis Bossart 	struct sof_hdmi_pcm *pcm;
76f40ed2e8SPierre-Louis Bossart 
77f40ed2e8SPierre-Louis Bossart 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
78f40ed2e8SPierre-Louis Bossart 	if (!pcm)
79f40ed2e8SPierre-Louis Bossart 		return -ENOMEM;
80f40ed2e8SPierre-Louis Bossart 
81f40ed2e8SPierre-Louis Bossart 	/* dai_link id is 1:1 mapped to the PCM device */
82f40ed2e8SPierre-Louis Bossart 	pcm->device = rtd->dai_link->id;
83f40ed2e8SPierre-Louis Bossart 	pcm->codec_dai = dai;
84f40ed2e8SPierre-Louis Bossart 
85f40ed2e8SPierre-Louis Bossart 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
86f40ed2e8SPierre-Louis Bossart 
87f40ed2e8SPierre-Louis Bossart 	return 0;
88f40ed2e8SPierre-Louis Bossart }
89f40ed2e8SPierre-Louis Bossart 
90f40ed2e8SPierre-Louis Bossart static int sof_pcm512x_codec_init(struct snd_soc_pcm_runtime *rtd)
91f40ed2e8SPierre-Louis Bossart {
92a2c1125eSKuninori Morimoto 	struct snd_soc_component *codec = snd_soc_rtd_to_codec(rtd, 0)->component;
93f40ed2e8SPierre-Louis Bossart 
94f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_EN, 0x08, 0x08);
95f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
96f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_CONTROL_1,
97f40ed2e8SPierre-Louis Bossart 				      0x08, 0x08);
98f40ed2e8SPierre-Louis Bossart 
99f40ed2e8SPierre-Louis Bossart 	return 0;
100f40ed2e8SPierre-Louis Bossart }
101f40ed2e8SPierre-Louis Bossart 
102f40ed2e8SPierre-Louis Bossart static int aif1_startup(struct snd_pcm_substream *substream)
103f40ed2e8SPierre-Louis Bossart {
104a2c1125eSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
105a2c1125eSKuninori Morimoto 	struct snd_soc_component *codec = snd_soc_rtd_to_codec(rtd, 0)->component;
106f40ed2e8SPierre-Louis Bossart 
107f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_CONTROL_1,
108f40ed2e8SPierre-Louis Bossart 				      0x08, 0x08);
109f40ed2e8SPierre-Louis Bossart 
110f40ed2e8SPierre-Louis Bossart 	return 0;
111f40ed2e8SPierre-Louis Bossart }
112f40ed2e8SPierre-Louis Bossart 
113f40ed2e8SPierre-Louis Bossart static void aif1_shutdown(struct snd_pcm_substream *substream)
114f40ed2e8SPierre-Louis Bossart {
115a2c1125eSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
116a2c1125eSKuninori Morimoto 	struct snd_soc_component *codec = snd_soc_rtd_to_codec(rtd, 0)->component;
117f40ed2e8SPierre-Louis Bossart 
118f40ed2e8SPierre-Louis Bossart 	snd_soc_component_update_bits(codec, PCM512x_GPIO_CONTROL_1,
119f40ed2e8SPierre-Louis Bossart 				      0x08, 0x00);
120f40ed2e8SPierre-Louis Bossart }
121f40ed2e8SPierre-Louis Bossart 
122f40ed2e8SPierre-Louis Bossart static const struct snd_soc_ops sof_pcm512x_ops = {
123f40ed2e8SPierre-Louis Bossart 	.startup = aif1_startup,
124f40ed2e8SPierre-Louis Bossart 	.shutdown = aif1_shutdown,
125f40ed2e8SPierre-Louis Bossart };
126f40ed2e8SPierre-Louis Bossart 
127f40ed2e8SPierre-Louis Bossart static struct snd_soc_dai_link_component platform_component[] = {
128f40ed2e8SPierre-Louis Bossart 	{
129f40ed2e8SPierre-Louis Bossart 		/* name might be overridden during probe */
130f40ed2e8SPierre-Louis Bossart 		.name = "0000:00:1f.3"
131f40ed2e8SPierre-Louis Bossart 	}
132f40ed2e8SPierre-Louis Bossart };
133f40ed2e8SPierre-Louis Bossart 
134f40ed2e8SPierre-Louis Bossart static int sof_card_late_probe(struct snd_soc_card *card)
135f40ed2e8SPierre-Louis Bossart {
136f40ed2e8SPierre-Louis Bossart 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
137f40ed2e8SPierre-Louis Bossart 	struct sof_hdmi_pcm *pcm;
138f40ed2e8SPierre-Louis Bossart 
139f40ed2e8SPierre-Louis Bossart 	/* HDMI is not supported by SOF on Baytrail/CherryTrail */
140f40ed2e8SPierre-Louis Bossart 	if (is_legacy_cpu)
141f40ed2e8SPierre-Louis Bossart 		return 0;
142f40ed2e8SPierre-Louis Bossart 
143f40ed2e8SPierre-Louis Bossart 	if (list_empty(&ctx->hdmi_pcm_list))
144f40ed2e8SPierre-Louis Bossart 		return -EINVAL;
145f40ed2e8SPierre-Louis Bossart 
14615a5a895SKai Vehmanen 	if (!ctx->idisp_codec)
14715a5a895SKai Vehmanen 		return 0;
14815a5a895SKai Vehmanen 
149f40ed2e8SPierre-Louis Bossart 	pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, head);
150f40ed2e8SPierre-Louis Bossart 
151f40ed2e8SPierre-Louis Bossart 	return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
152f40ed2e8SPierre-Louis Bossart }
153f40ed2e8SPierre-Louis Bossart 
154f40ed2e8SPierre-Louis Bossart static const struct snd_kcontrol_new sof_controls[] = {
155f40ed2e8SPierre-Louis Bossart 	SOC_DAPM_PIN_SWITCH("Ext Spk"),
156f40ed2e8SPierre-Louis Bossart };
157f40ed2e8SPierre-Louis Bossart 
158f40ed2e8SPierre-Louis Bossart static const struct snd_soc_dapm_widget sof_widgets[] = {
159f40ed2e8SPierre-Louis Bossart 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
160f40ed2e8SPierre-Louis Bossart };
161f40ed2e8SPierre-Louis Bossart 
162f40ed2e8SPierre-Louis Bossart static const struct snd_soc_dapm_widget dmic_widgets[] = {
163f40ed2e8SPierre-Louis Bossart 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
164f40ed2e8SPierre-Louis Bossart };
165f40ed2e8SPierre-Louis Bossart 
166f40ed2e8SPierre-Louis Bossart static const struct snd_soc_dapm_route sof_map[] = {
167f40ed2e8SPierre-Louis Bossart 	/* Speaker */
168f40ed2e8SPierre-Louis Bossart 	{"Ext Spk", NULL, "OUTR"},
169f40ed2e8SPierre-Louis Bossart 	{"Ext Spk", NULL, "OUTL"},
170f40ed2e8SPierre-Louis Bossart };
171f40ed2e8SPierre-Louis Bossart 
172f40ed2e8SPierre-Louis Bossart static const struct snd_soc_dapm_route dmic_map[] = {
173f40ed2e8SPierre-Louis Bossart 	/* digital mics */
174f40ed2e8SPierre-Louis Bossart 	{"DMic", NULL, "SoC DMIC"},
175f40ed2e8SPierre-Louis Bossart };
176f40ed2e8SPierre-Louis Bossart 
177f40ed2e8SPierre-Louis Bossart static int dmic_init(struct snd_soc_pcm_runtime *rtd)
178f40ed2e8SPierre-Louis Bossart {
179f40ed2e8SPierre-Louis Bossart 	struct snd_soc_card *card = rtd->card;
180f40ed2e8SPierre-Louis Bossart 	int ret;
181f40ed2e8SPierre-Louis Bossart 
182f40ed2e8SPierre-Louis Bossart 	ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
183f40ed2e8SPierre-Louis Bossart 					ARRAY_SIZE(dmic_widgets));
184f40ed2e8SPierre-Louis Bossart 	if (ret) {
185f40ed2e8SPierre-Louis Bossart 		dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
186f40ed2e8SPierre-Louis Bossart 		/* Don't need to add routes if widget addition failed */
187f40ed2e8SPierre-Louis Bossart 		return ret;
188f40ed2e8SPierre-Louis Bossart 	}
189f40ed2e8SPierre-Louis Bossart 
190f40ed2e8SPierre-Louis Bossart 	ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
191f40ed2e8SPierre-Louis Bossart 				      ARRAY_SIZE(dmic_map));
192f40ed2e8SPierre-Louis Bossart 
193f40ed2e8SPierre-Louis Bossart 	if (ret)
194f40ed2e8SPierre-Louis Bossart 		dev_err(card->dev, "DMic map addition failed: %d\n", ret);
195f40ed2e8SPierre-Louis Bossart 
196f40ed2e8SPierre-Louis Bossart 	return ret;
197f40ed2e8SPierre-Louis Bossart }
198f40ed2e8SPierre-Louis Bossart 
199f40ed2e8SPierre-Louis Bossart /* sof audio machine driver for pcm512x codec */
200f40ed2e8SPierre-Louis Bossart static struct snd_soc_card sof_audio_card_pcm512x = {
201f40ed2e8SPierre-Louis Bossart 	.name = "pcm512x",
202f40ed2e8SPierre-Louis Bossart 	.owner = THIS_MODULE,
203f40ed2e8SPierre-Louis Bossart 	.controls = sof_controls,
204f40ed2e8SPierre-Louis Bossart 	.num_controls = ARRAY_SIZE(sof_controls),
205f40ed2e8SPierre-Louis Bossart 	.dapm_widgets = sof_widgets,
206f40ed2e8SPierre-Louis Bossart 	.num_dapm_widgets = ARRAY_SIZE(sof_widgets),
207f40ed2e8SPierre-Louis Bossart 	.dapm_routes = sof_map,
208f40ed2e8SPierre-Louis Bossart 	.num_dapm_routes = ARRAY_SIZE(sof_map),
209f40ed2e8SPierre-Louis Bossart 	.fully_routed = true,
210f40ed2e8SPierre-Louis Bossart 	.late_probe = sof_card_late_probe,
211f40ed2e8SPierre-Louis Bossart };
212f40ed2e8SPierre-Louis Bossart 
213f40ed2e8SPierre-Louis Bossart SND_SOC_DAILINK_DEF(pcm512x_component,
214f40ed2e8SPierre-Louis Bossart 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-104C5122:00", "pcm512x-hifi")));
215f40ed2e8SPierre-Louis Bossart SND_SOC_DAILINK_DEF(dmic_component,
216f40ed2e8SPierre-Louis Bossart 	DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
217f40ed2e8SPierre-Louis Bossart 
218f40ed2e8SPierre-Louis Bossart static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
219f40ed2e8SPierre-Louis Bossart 							  int ssp_codec,
220f40ed2e8SPierre-Louis Bossart 							  int dmic_be_num,
22115a5a895SKai Vehmanen 							  int hdmi_num,
22215a5a895SKai Vehmanen 							  bool idisp_codec)
223f40ed2e8SPierre-Louis Bossart {
224f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai_link_component *idisp_components;
225f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai_link_component *cpus;
226f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai_link *links;
227f40ed2e8SPierre-Louis Bossart 	int i, id = 0;
228f40ed2e8SPierre-Louis Bossart 
229f40ed2e8SPierre-Louis Bossart 	links = devm_kcalloc(dev, sof_audio_card_pcm512x.num_links,
230f40ed2e8SPierre-Louis Bossart 			sizeof(struct snd_soc_dai_link), GFP_KERNEL);
231f40ed2e8SPierre-Louis Bossart 	cpus = devm_kcalloc(dev, sof_audio_card_pcm512x.num_links,
232f40ed2e8SPierre-Louis Bossart 			sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
233f40ed2e8SPierre-Louis Bossart 	if (!links || !cpus)
234f40ed2e8SPierre-Louis Bossart 		goto devm_err;
235f40ed2e8SPierre-Louis Bossart 
236f40ed2e8SPierre-Louis Bossart 	/* codec SSP */
237f40ed2e8SPierre-Louis Bossart 	links[id].name = devm_kasprintf(dev, GFP_KERNEL,
238f40ed2e8SPierre-Louis Bossart 					"SSP%d-Codec", ssp_codec);
239f40ed2e8SPierre-Louis Bossart 	if (!links[id].name)
240f40ed2e8SPierre-Louis Bossart 		goto devm_err;
241f40ed2e8SPierre-Louis Bossart 
242f40ed2e8SPierre-Louis Bossart 	links[id].id = id;
243f40ed2e8SPierre-Louis Bossart 	links[id].codecs = pcm512x_component;
244f40ed2e8SPierre-Louis Bossart 	links[id].num_codecs = ARRAY_SIZE(pcm512x_component);
245f40ed2e8SPierre-Louis Bossart 	links[id].platforms = platform_component;
246f40ed2e8SPierre-Louis Bossart 	links[id].num_platforms = ARRAY_SIZE(platform_component);
247f40ed2e8SPierre-Louis Bossart 	links[id].init = sof_pcm512x_codec_init;
248f40ed2e8SPierre-Louis Bossart 	links[id].ops = &sof_pcm512x_ops;
249f40ed2e8SPierre-Louis Bossart 	links[id].dpcm_playback = 1;
250f40ed2e8SPierre-Louis Bossart 	/*
251f40ed2e8SPierre-Louis Bossart 	 * capture only supported with specific versions of the Hifiberry DAC+
252f40ed2e8SPierre-Louis Bossart 	 */
25322414cadSPierre-Louis Bossart 	if (sof_pcm512x_quirk & SOF_PCM512X_ENABLE_SSP_CAPTURE)
25422414cadSPierre-Louis Bossart 		links[id].dpcm_capture = 1;
255f40ed2e8SPierre-Louis Bossart 	links[id].no_pcm = 1;
256f40ed2e8SPierre-Louis Bossart 	links[id].cpus = &cpus[id];
257f40ed2e8SPierre-Louis Bossart 	links[id].num_cpus = 1;
258f40ed2e8SPierre-Louis Bossart 	if (is_legacy_cpu) {
259f40ed2e8SPierre-Louis Bossart 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
260f40ed2e8SPierre-Louis Bossart 							  "ssp%d-port",
261f40ed2e8SPierre-Louis Bossart 							  ssp_codec);
262f40ed2e8SPierre-Louis Bossart 		if (!links[id].cpus->dai_name)
263f40ed2e8SPierre-Louis Bossart 			goto devm_err;
264f40ed2e8SPierre-Louis Bossart 	} else {
265f40ed2e8SPierre-Louis Bossart 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
266f40ed2e8SPierre-Louis Bossart 							  "SSP%d Pin",
267f40ed2e8SPierre-Louis Bossart 							  ssp_codec);
268f40ed2e8SPierre-Louis Bossart 		if (!links[id].cpus->dai_name)
269f40ed2e8SPierre-Louis Bossart 			goto devm_err;
270f40ed2e8SPierre-Louis Bossart 	}
271f40ed2e8SPierre-Louis Bossart 	id++;
272f40ed2e8SPierre-Louis Bossart 
273f40ed2e8SPierre-Louis Bossart 	/* dmic */
274f40ed2e8SPierre-Louis Bossart 	if (dmic_be_num > 0) {
275f40ed2e8SPierre-Louis Bossart 		/* at least we have dmic01 */
276f40ed2e8SPierre-Louis Bossart 		links[id].name = "dmic01";
277f40ed2e8SPierre-Louis Bossart 		links[id].cpus = &cpus[id];
278f40ed2e8SPierre-Louis Bossart 		links[id].cpus->dai_name = "DMIC01 Pin";
279f40ed2e8SPierre-Louis Bossart 		links[id].init = dmic_init;
280f40ed2e8SPierre-Louis Bossart 		if (dmic_be_num > 1) {
281f40ed2e8SPierre-Louis Bossart 			/* set up 2 BE links at most */
282f40ed2e8SPierre-Louis Bossart 			links[id + 1].name = "dmic16k";
283f40ed2e8SPierre-Louis Bossart 			links[id + 1].cpus = &cpus[id + 1];
284f40ed2e8SPierre-Louis Bossart 			links[id + 1].cpus->dai_name = "DMIC16k Pin";
285f40ed2e8SPierre-Louis Bossart 			dmic_be_num = 2;
286f40ed2e8SPierre-Louis Bossart 		}
287f40ed2e8SPierre-Louis Bossart 	}
288f40ed2e8SPierre-Louis Bossart 
289f40ed2e8SPierre-Louis Bossart 	for (i = 0; i < dmic_be_num; i++) {
290f40ed2e8SPierre-Louis Bossart 		links[id].id = id;
291f40ed2e8SPierre-Louis Bossart 		links[id].num_cpus = 1;
292f40ed2e8SPierre-Louis Bossart 		links[id].codecs = dmic_component;
293f40ed2e8SPierre-Louis Bossart 		links[id].num_codecs = ARRAY_SIZE(dmic_component);
294f40ed2e8SPierre-Louis Bossart 		links[id].platforms = platform_component;
295f40ed2e8SPierre-Louis Bossart 		links[id].num_platforms = ARRAY_SIZE(platform_component);
296f40ed2e8SPierre-Louis Bossart 		links[id].ignore_suspend = 1;
297f40ed2e8SPierre-Louis Bossart 		links[id].dpcm_capture = 1;
298f40ed2e8SPierre-Louis Bossart 		links[id].no_pcm = 1;
299f40ed2e8SPierre-Louis Bossart 		id++;
300f40ed2e8SPierre-Louis Bossart 	}
301f40ed2e8SPierre-Louis Bossart 
302f40ed2e8SPierre-Louis Bossart 	/* HDMI */
303f40ed2e8SPierre-Louis Bossart 	if (hdmi_num > 0) {
304f40ed2e8SPierre-Louis Bossart 		idisp_components = devm_kcalloc(dev, hdmi_num,
305f40ed2e8SPierre-Louis Bossart 				sizeof(struct snd_soc_dai_link_component),
306f40ed2e8SPierre-Louis Bossart 				GFP_KERNEL);
307f40ed2e8SPierre-Louis Bossart 		if (!idisp_components)
308f40ed2e8SPierre-Louis Bossart 			goto devm_err;
309f40ed2e8SPierre-Louis Bossart 	}
310f40ed2e8SPierre-Louis Bossart 	for (i = 1; i <= hdmi_num; i++) {
311f40ed2e8SPierre-Louis Bossart 		links[id].name = devm_kasprintf(dev, GFP_KERNEL,
312f40ed2e8SPierre-Louis Bossart 						"iDisp%d", i);
313f40ed2e8SPierre-Louis Bossart 		if (!links[id].name)
314f40ed2e8SPierre-Louis Bossart 			goto devm_err;
315f40ed2e8SPierre-Louis Bossart 
316f40ed2e8SPierre-Louis Bossart 		links[id].id = id;
317f40ed2e8SPierre-Louis Bossart 		links[id].cpus = &cpus[id];
318f40ed2e8SPierre-Louis Bossart 		links[id].num_cpus = 1;
319f40ed2e8SPierre-Louis Bossart 		links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
320f40ed2e8SPierre-Louis Bossart 							  "iDisp%d Pin", i);
321f40ed2e8SPierre-Louis Bossart 		if (!links[id].cpus->dai_name)
322f40ed2e8SPierre-Louis Bossart 			goto devm_err;
323f40ed2e8SPierre-Louis Bossart 
32415a5a895SKai Vehmanen 		/*
32515a5a895SKai Vehmanen 		 * topology cannot be loaded if codec is missing, so
32615a5a895SKai Vehmanen 		 * use the dummy codec if needed
32715a5a895SKai Vehmanen 		 */
32815a5a895SKai Vehmanen 		if (idisp_codec) {
329f40ed2e8SPierre-Louis Bossart 			idisp_components[i - 1].name = "ehdaudio0D2";
33015a5a895SKai Vehmanen 			idisp_components[i - 1].dai_name =
33115a5a895SKai Vehmanen 				devm_kasprintf(dev, GFP_KERNEL,
33215a5a895SKai Vehmanen 					       "intel-hdmi-hifi%d", i);
33315a5a895SKai Vehmanen 		} else {
334a2c1125eSKuninori Morimoto 			idisp_components[i - 1] = snd_soc_dummy_dlc;
33515a5a895SKai Vehmanen 		}
336f40ed2e8SPierre-Louis Bossart 		if (!idisp_components[i - 1].dai_name)
337f40ed2e8SPierre-Louis Bossart 			goto devm_err;
338f40ed2e8SPierre-Louis Bossart 
339f40ed2e8SPierre-Louis Bossart 		links[id].codecs = &idisp_components[i - 1];
340f40ed2e8SPierre-Louis Bossart 		links[id].num_codecs = 1;
341f40ed2e8SPierre-Louis Bossart 		links[id].platforms = platform_component;
342f40ed2e8SPierre-Louis Bossart 		links[id].num_platforms = ARRAY_SIZE(platform_component);
343f40ed2e8SPierre-Louis Bossart 		links[id].init = sof_hdmi_init;
344f40ed2e8SPierre-Louis Bossart 		links[id].dpcm_playback = 1;
345f40ed2e8SPierre-Louis Bossart 		links[id].no_pcm = 1;
346f40ed2e8SPierre-Louis Bossart 		id++;
347f40ed2e8SPierre-Louis Bossart 	}
348f40ed2e8SPierre-Louis Bossart 
349f40ed2e8SPierre-Louis Bossart 	return links;
350f40ed2e8SPierre-Louis Bossart devm_err:
351f40ed2e8SPierre-Louis Bossart 	return NULL;
352f40ed2e8SPierre-Louis Bossart }
353f40ed2e8SPierre-Louis Bossart 
354f40ed2e8SPierre-Louis Bossart static int sof_audio_probe(struct platform_device *pdev)
355f40ed2e8SPierre-Louis Bossart {
35615a5a895SKai Vehmanen 	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
357f40ed2e8SPierre-Louis Bossart 	struct snd_soc_dai_link *dai_links;
358f40ed2e8SPierre-Louis Bossart 	struct sof_card_private *ctx;
359f40ed2e8SPierre-Louis Bossart 	int dmic_be_num, hdmi_num;
360f40ed2e8SPierre-Louis Bossart 	int ret, ssp_codec;
361f40ed2e8SPierre-Louis Bossart 
362f40ed2e8SPierre-Louis Bossart 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
363f40ed2e8SPierre-Louis Bossart 	if (!ctx)
364f40ed2e8SPierre-Louis Bossart 		return -ENOMEM;
365f40ed2e8SPierre-Louis Bossart 
366f40ed2e8SPierre-Louis Bossart 	hdmi_num = 0;
367f40ed2e8SPierre-Louis Bossart 	if (soc_intel_is_byt() || soc_intel_is_cht()) {
368f40ed2e8SPierre-Louis Bossart 		is_legacy_cpu = true;
369f40ed2e8SPierre-Louis Bossart 		dmic_be_num = 0;
370f40ed2e8SPierre-Louis Bossart 		/* default quirk for legacy cpu */
371f40ed2e8SPierre-Louis Bossart 		sof_pcm512x_quirk = SOF_PCM512X_SSP_CODEC(2);
372f40ed2e8SPierre-Louis Bossart 	} else {
373f40ed2e8SPierre-Louis Bossart 		dmic_be_num = 2;
374*f22a351fSBrent Lu 		if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
37515a5a895SKai Vehmanen 			ctx->idisp_codec = true;
37615a5a895SKai Vehmanen 
37715a5a895SKai Vehmanen 		/* links are always present in topology */
378f40ed2e8SPierre-Louis Bossart 		hdmi_num = 3;
379f40ed2e8SPierre-Louis Bossart 	}
380f40ed2e8SPierre-Louis Bossart 
381f40ed2e8SPierre-Louis Bossart 	dmi_check_system(sof_pcm512x_quirk_table);
382f40ed2e8SPierre-Louis Bossart 
383f40ed2e8SPierre-Louis Bossart 	dev_dbg(&pdev->dev, "sof_pcm512x_quirk = %lx\n", sof_pcm512x_quirk);
384f40ed2e8SPierre-Louis Bossart 
385f40ed2e8SPierre-Louis Bossart 	ssp_codec = sof_pcm512x_quirk & SOF_PCM512X_SSP_CODEC_MASK;
386f40ed2e8SPierre-Louis Bossart 
38722414cadSPierre-Louis Bossart 	if (!(sof_pcm512x_quirk & SOF_PCM512X_ENABLE_DMIC))
38822414cadSPierre-Louis Bossart 		dmic_be_num = 0;
38922414cadSPierre-Louis Bossart 
390f40ed2e8SPierre-Louis Bossart 	/* compute number of dai links */
391f40ed2e8SPierre-Louis Bossart 	sof_audio_card_pcm512x.num_links = 1 + dmic_be_num + hdmi_num;
392f40ed2e8SPierre-Louis Bossart 
393f40ed2e8SPierre-Louis Bossart 	dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec,
39415a5a895SKai Vehmanen 					      dmic_be_num, hdmi_num,
39515a5a895SKai Vehmanen 					      ctx->idisp_codec);
396f40ed2e8SPierre-Louis Bossart 	if (!dai_links)
397f40ed2e8SPierre-Louis Bossart 		return -ENOMEM;
398f40ed2e8SPierre-Louis Bossart 
399f40ed2e8SPierre-Louis Bossart 	sof_audio_card_pcm512x.dai_link = dai_links;
400f40ed2e8SPierre-Louis Bossart 
401f40ed2e8SPierre-Louis Bossart 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
402f40ed2e8SPierre-Louis Bossart 
403f40ed2e8SPierre-Louis Bossart 	sof_audio_card_pcm512x.dev = &pdev->dev;
404f40ed2e8SPierre-Louis Bossart 
405f40ed2e8SPierre-Louis Bossart 	/* set platform name for each dailink */
406f40ed2e8SPierre-Louis Bossart 	ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_pcm512x,
407f40ed2e8SPierre-Louis Bossart 						    mach->mach_params.platform);
408f40ed2e8SPierre-Louis Bossart 	if (ret)
409f40ed2e8SPierre-Louis Bossart 		return ret;
410f40ed2e8SPierre-Louis Bossart 
411f40ed2e8SPierre-Louis Bossart 	snd_soc_card_set_drvdata(&sof_audio_card_pcm512x, ctx);
412f40ed2e8SPierre-Louis Bossart 
413f40ed2e8SPierre-Louis Bossart 	return devm_snd_soc_register_card(&pdev->dev,
414f40ed2e8SPierre-Louis Bossart 					  &sof_audio_card_pcm512x);
415f40ed2e8SPierre-Louis Bossart }
416f40ed2e8SPierre-Louis Bossart 
417b6c85e14SUwe Kleine-König static void sof_pcm512x_remove(struct platform_device *pdev)
418f40ed2e8SPierre-Louis Bossart {
419f40ed2e8SPierre-Louis Bossart 	struct snd_soc_card *card = platform_get_drvdata(pdev);
420f057852fSPierre-Louis Bossart 	struct snd_soc_component *component;
421f40ed2e8SPierre-Louis Bossart 
422f40ed2e8SPierre-Louis Bossart 	for_each_card_components(card, component) {
423f40ed2e8SPierre-Louis Bossart 		if (!strcmp(component->name, pcm512x_component[0].name)) {
424f40ed2e8SPierre-Louis Bossart 			snd_soc_component_set_jack(component, NULL, NULL);
425f40ed2e8SPierre-Louis Bossart 			break;
426f40ed2e8SPierre-Louis Bossart 		}
427f40ed2e8SPierre-Louis Bossart 	}
428f40ed2e8SPierre-Louis Bossart }
429f40ed2e8SPierre-Louis Bossart 
430f40ed2e8SPierre-Louis Bossart static struct platform_driver sof_audio = {
431f40ed2e8SPierre-Louis Bossart 	.probe = sof_audio_probe,
432130af75bSUwe Kleine-König 	.remove = sof_pcm512x_remove,
433f40ed2e8SPierre-Louis Bossart 	.driver = {
434f40ed2e8SPierre-Louis Bossart 		.name = "sof_pcm512x",
435f40ed2e8SPierre-Louis Bossart 		.pm = &snd_soc_pm_ops,
436f40ed2e8SPierre-Louis Bossart 	},
437f40ed2e8SPierre-Louis Bossart };
438f40ed2e8SPierre-Louis Bossart module_platform_driver(sof_audio)
439f40ed2e8SPierre-Louis Bossart 
440f40ed2e8SPierre-Louis Bossart MODULE_DESCRIPTION("ASoC Intel(R) SOF + PCM512x Machine driver");
441f40ed2e8SPierre-Louis Bossart MODULE_AUTHOR("Pierre-Louis Bossart");
442f40ed2e8SPierre-Louis Bossart MODULE_LICENSE("GPL v2");
443f40ed2e8SPierre-Louis Bossart MODULE_ALIAS("platform:sof_pcm512x");
444f6081af6SPierre-Louis Bossart MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
445