1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2018-2020 Intel Corporation.
5 * Intel SOF Machine Driver for Intel platforms with TI PCM512x codec,
6 * e.g. Up or Up2 with Hifiberry DAC+ HAT
10 #include <linux/i2c.h>
11 #include <linux/input.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/types.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/soc.h>
20 #include <sound/soc-acpi.h>
21 #include "../../codecs/pcm512x.h"
22 #include "../common/soc-intel-quirks.h"
23 #include "hda_dsp_common.h"
27 #define SOF_PCM512X_SSP_CODEC(quirk) ((quirk) & GENMASK(3, 0))
28 #define SOF_PCM512X_SSP_CODEC_MASK (GENMASK(3, 0))
30 #define IDISP_CODEC_MASK 0x4
33 static unsigned long sof_pcm512x_quirk = SOF_PCM512X_SSP_CODEC(5);
35 static bool is_legacy_cpu;
38 struct list_head head;
39 struct snd_soc_dai *codec_dai;
43 struct sof_card_private {
44 struct list_head hdmi_pcm_list;
48 static int sof_pcm512x_quirk_cb(const struct dmi_system_id *id)
50 sof_pcm512x_quirk = (unsigned long)id->driver_data;
54 static const struct dmi_system_id sof_pcm512x_quirk_table[] = {
56 .callback = sof_pcm512x_quirk_cb,
58 DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
59 DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
61 .driver_data = (void *)(SOF_PCM512X_SSP_CODEC(2)),
66 static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
68 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
69 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
70 struct sof_hdmi_pcm *pcm;
72 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
76 /* dai_link id is 1:1 mapped to the PCM device */
77 pcm->device = rtd->dai_link->id;
80 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
85 static int sof_pcm512x_codec_init(struct snd_soc_pcm_runtime *rtd)
87 struct snd_soc_component *codec = asoc_rtd_to_codec(rtd, 0)->component;
89 snd_soc_component_update_bits(codec, PCM512x_GPIO_EN, 0x08, 0x08);
90 snd_soc_component_update_bits(codec, PCM512x_GPIO_OUTPUT_4, 0x0f, 0x02);
91 snd_soc_component_update_bits(codec, PCM512x_GPIO_CONTROL_1,
97 static int aif1_startup(struct snd_pcm_substream *substream)
99 struct snd_soc_pcm_runtime *rtd = substream->private_data;
100 struct snd_soc_component *codec = asoc_rtd_to_codec(rtd, 0)->component;
102 snd_soc_component_update_bits(codec, PCM512x_GPIO_CONTROL_1,
108 static void aif1_shutdown(struct snd_pcm_substream *substream)
110 struct snd_soc_pcm_runtime *rtd = substream->private_data;
111 struct snd_soc_component *codec = asoc_rtd_to_codec(rtd, 0)->component;
113 snd_soc_component_update_bits(codec, PCM512x_GPIO_CONTROL_1,
117 static const struct snd_soc_ops sof_pcm512x_ops = {
118 .startup = aif1_startup,
119 .shutdown = aif1_shutdown,
122 static struct snd_soc_dai_link_component platform_component[] = {
124 /* name might be overridden during probe */
125 .name = "0000:00:1f.3"
129 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
130 static int sof_card_late_probe(struct snd_soc_card *card)
132 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
133 struct sof_hdmi_pcm *pcm;
135 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
139 if (list_empty(&ctx->hdmi_pcm_list))
142 if (!ctx->idisp_codec)
145 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, head);
147 return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
150 static int sof_card_late_probe(struct snd_soc_card *card)
156 static const struct snd_kcontrol_new sof_controls[] = {
157 SOC_DAPM_PIN_SWITCH("Ext Spk"),
160 static const struct snd_soc_dapm_widget sof_widgets[] = {
161 SND_SOC_DAPM_SPK("Ext Spk", NULL),
164 static const struct snd_soc_dapm_widget dmic_widgets[] = {
165 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
168 static const struct snd_soc_dapm_route sof_map[] = {
170 {"Ext Spk", NULL, "OUTR"},
171 {"Ext Spk", NULL, "OUTL"},
174 static const struct snd_soc_dapm_route dmic_map[] = {
176 {"DMic", NULL, "SoC DMIC"},
179 static int dmic_init(struct snd_soc_pcm_runtime *rtd)
181 struct snd_soc_card *card = rtd->card;
184 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
185 ARRAY_SIZE(dmic_widgets));
187 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
188 /* Don't need to add routes if widget addition failed */
192 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
193 ARRAY_SIZE(dmic_map));
196 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
201 /* sof audio machine driver for pcm512x codec */
202 static struct snd_soc_card sof_audio_card_pcm512x = {
204 .owner = THIS_MODULE,
205 .controls = sof_controls,
206 .num_controls = ARRAY_SIZE(sof_controls),
207 .dapm_widgets = sof_widgets,
208 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
209 .dapm_routes = sof_map,
210 .num_dapm_routes = ARRAY_SIZE(sof_map),
211 .fully_routed = true,
212 .late_probe = sof_card_late_probe,
215 SND_SOC_DAILINK_DEF(pcm512x_component,
216 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-104C5122:00", "pcm512x-hifi")));
217 SND_SOC_DAILINK_DEF(dmic_component,
218 DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
220 static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
226 struct snd_soc_dai_link_component *idisp_components;
227 struct snd_soc_dai_link_component *cpus;
228 struct snd_soc_dai_link *links;
231 links = devm_kcalloc(dev, sof_audio_card_pcm512x.num_links,
232 sizeof(struct snd_soc_dai_link), GFP_KERNEL);
233 cpus = devm_kcalloc(dev, sof_audio_card_pcm512x.num_links,
234 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
239 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
240 "SSP%d-Codec", ssp_codec);
245 links[id].codecs = pcm512x_component;
246 links[id].num_codecs = ARRAY_SIZE(pcm512x_component);
247 links[id].platforms = platform_component;
248 links[id].num_platforms = ARRAY_SIZE(platform_component);
249 links[id].init = sof_pcm512x_codec_init;
250 links[id].ops = &sof_pcm512x_ops;
251 links[id].nonatomic = true;
252 links[id].dpcm_playback = 1;
254 * capture only supported with specific versions of the Hifiberry DAC+
255 * links[id].dpcm_capture = 1;
257 links[id].no_pcm = 1;
258 links[id].cpus = &cpus[id];
259 links[id].num_cpus = 1;
261 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
264 if (!links[id].cpus->dai_name)
267 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
270 if (!links[id].cpus->dai_name)
276 if (dmic_be_num > 0) {
277 /* at least we have dmic01 */
278 links[id].name = "dmic01";
279 links[id].cpus = &cpus[id];
280 links[id].cpus->dai_name = "DMIC01 Pin";
281 links[id].init = dmic_init;
282 if (dmic_be_num > 1) {
283 /* set up 2 BE links at most */
284 links[id + 1].name = "dmic16k";
285 links[id + 1].cpus = &cpus[id + 1];
286 links[id + 1].cpus->dai_name = "DMIC16k Pin";
291 for (i = 0; i < dmic_be_num; i++) {
293 links[id].num_cpus = 1;
294 links[id].codecs = dmic_component;
295 links[id].num_codecs = ARRAY_SIZE(dmic_component);
296 links[id].platforms = platform_component;
297 links[id].num_platforms = ARRAY_SIZE(platform_component);
298 links[id].ignore_suspend = 1;
299 links[id].dpcm_capture = 1;
300 links[id].no_pcm = 1;
306 idisp_components = devm_kcalloc(dev, hdmi_num,
307 sizeof(struct snd_soc_dai_link_component),
309 if (!idisp_components)
312 for (i = 1; i <= hdmi_num; i++) {
313 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
319 links[id].cpus = &cpus[id];
320 links[id].num_cpus = 1;
321 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
323 if (!links[id].cpus->dai_name)
327 * topology cannot be loaded if codec is missing, so
328 * use the dummy codec if needed
331 idisp_components[i - 1].name = "ehdaudio0D2";
332 idisp_components[i - 1].dai_name =
333 devm_kasprintf(dev, GFP_KERNEL,
334 "intel-hdmi-hifi%d", i);
336 idisp_components[i - 1].name = "snd-soc-dummy";
337 idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
339 if (!idisp_components[i - 1].dai_name)
342 links[id].codecs = &idisp_components[i - 1];
343 links[id].num_codecs = 1;
344 links[id].platforms = platform_component;
345 links[id].num_platforms = ARRAY_SIZE(platform_component);
346 links[id].init = sof_hdmi_init;
347 links[id].dpcm_playback = 1;
348 links[id].no_pcm = 1;
357 static int sof_audio_probe(struct platform_device *pdev)
359 struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
360 struct snd_soc_dai_link *dai_links;
361 struct sof_card_private *ctx;
362 int dmic_be_num, hdmi_num;
365 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
370 if (soc_intel_is_byt() || soc_intel_is_cht()) {
371 is_legacy_cpu = true;
373 /* default quirk for legacy cpu */
374 sof_pcm512x_quirk = SOF_PCM512X_SSP_CODEC(2);
377 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI)
378 if (mach->mach_params.common_hdmi_codec_drv &&
379 (mach->mach_params.codec_mask & IDISP_CODEC_MASK))
380 ctx->idisp_codec = true;
382 /* links are always present in topology */
387 dmi_check_system(sof_pcm512x_quirk_table);
389 dev_dbg(&pdev->dev, "sof_pcm512x_quirk = %lx\n", sof_pcm512x_quirk);
391 ssp_codec = sof_pcm512x_quirk & SOF_PCM512X_SSP_CODEC_MASK;
393 /* compute number of dai links */
394 sof_audio_card_pcm512x.num_links = 1 + dmic_be_num + hdmi_num;
396 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec,
397 dmic_be_num, hdmi_num,
402 sof_audio_card_pcm512x.dai_link = dai_links;
404 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
406 sof_audio_card_pcm512x.dev = &pdev->dev;
408 /* set platform name for each dailink */
409 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_pcm512x,
410 mach->mach_params.platform);
414 snd_soc_card_set_drvdata(&sof_audio_card_pcm512x, ctx);
416 return devm_snd_soc_register_card(&pdev->dev,
417 &sof_audio_card_pcm512x);
420 static int sof_pcm512x_remove(struct platform_device *pdev)
422 struct snd_soc_card *card = platform_get_drvdata(pdev);
423 struct snd_soc_component *component = NULL;
425 for_each_card_components(card, component) {
426 if (!strcmp(component->name, pcm512x_component[0].name)) {
427 snd_soc_component_set_jack(component, NULL, NULL);
435 static struct platform_driver sof_audio = {
436 .probe = sof_audio_probe,
437 .remove = sof_pcm512x_remove,
439 .name = "sof_pcm512x",
440 .pm = &snd_soc_pm_ops,
443 module_platform_driver(sof_audio)
445 MODULE_DESCRIPTION("ASoC Intel(R) SOF + PCM512x Machine driver");
446 MODULE_AUTHOR("Pierre-Louis Bossart");
447 MODULE_LICENSE("GPL v2");
448 MODULE_ALIAS("platform:sof_pcm512x");