xref: /linux/sound/soc/intel/boards/sof_sdw_cs_amp.c (revision e91c37f1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2023 Intel Corporation
3 
4 /*
5  *  sof_sdw_cs_amp - Helpers to handle CS35L56 from generic machine driver
6  */
7 
8 #include <linux/device.h>
9 #include <linux/errno.h>
10 #include <sound/soc.h>
11 #include <sound/soc-acpi.h>
12 #include <sound/soc-dai.h>
13 #include "sof_sdw_common.h"
14 
15 #define CODEC_NAME_SIZE	8
16 
17 static const struct snd_soc_dapm_widget sof_widgets[] = {
18 	SND_SOC_DAPM_SPK("Speakers", NULL),
19 };
20 
21 static int cs_spk_init(struct snd_soc_pcm_runtime *rtd)
22 {
23 	const char *dai_name = rtd->dai_link->codecs->dai_name;
24 	struct snd_soc_card *card = rtd->card;
25 	char codec_name[CODEC_NAME_SIZE];
26 	char widget_name[16];
27 	struct snd_soc_dapm_route route = { "Speakers", NULL, widget_name };
28 	struct snd_soc_dai *codec_dai;
29 	int i, ret;
30 
31 	snprintf(codec_name, CODEC_NAME_SIZE, "%s", dai_name);
32 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
33 					  "%s spk:%s",
34 					  card->components, codec_name);
35 	if (!card->components)
36 		return -ENOMEM;
37 
38 	ret = snd_soc_dapm_new_controls(&card->dapm, sof_widgets,
39 					ARRAY_SIZE(sof_widgets));
40 	if (ret) {
41 		dev_err(card->dev, "widgets addition failed: %d\n", ret);
42 		return ret;
43 	}
44 
45 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
46 		if (!strstr(codec_dai->name, "cs35l56"))
47 			continue;
48 
49 		snprintf(widget_name, sizeof(widget_name), "%s SPK",
50 			 codec_dai->component->name_prefix);
51 		ret = snd_soc_dapm_add_routes(&card->dapm, &route, 1);
52 		if (ret)
53 			return ret;
54 	}
55 
56 	return 0;
57 }
58 
59 int sof_sdw_cs_amp_init(struct snd_soc_card *card,
60 			const struct snd_soc_acpi_link_adr *link,
61 			struct snd_soc_dai_link *dai_links,
62 			struct sof_sdw_codec_info *info,
63 			bool playback)
64 {
65 	/* Do init on playback link only. */
66 	if (!playback)
67 		return 0;
68 
69 	info->amp_num++;
70 	dai_links->init = cs_spk_init;
71 
72 	return 0;
73 }
74