1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2023 Intel Corporation
3 
4 /*
5  *  sof_sdw_rt712_sdca - Helpers to handle RT712-SDCA from generic machine driver
6  */
7 
8 #include <linux/device.h>
9 #include <linux/errno.h>
10 #include <linux/soundwire/sdw.h>
11 #include <linux/soundwire/sdw_type.h>
12 #include <sound/control.h>
13 #include <sound/soc.h>
14 #include <sound/soc-acpi.h>
15 #include <sound/soc-dapm.h>
16 #include "sof_sdw_common.h"
17 
18 static const struct snd_soc_dapm_widget rt712_spk_widgets[] = {
19 	SND_SOC_DAPM_SPK("Speaker", NULL),
20 };
21 
22 /*
23  * dapm routes for rt712 spk will be registered dynamically according
24  * to the number of rt712 spk used. The first two entries will be registered
25  * for one codec case, and the last two entries are also registered
26  * if two rt712s are used.
27  */
28 static const struct snd_soc_dapm_route rt712_spk_map[] = {
29 	{ "Speaker", NULL, "rt712 SPOL" },
30 	{ "Speaker", NULL, "rt712 SPOR" },
31 };
32 
33 static const struct snd_kcontrol_new rt712_spk_controls[] = {
34 	SOC_DAPM_PIN_SWITCH("Speaker"),
35 };
36 
rt712_spk_init(struct snd_soc_pcm_runtime * rtd)37 static int rt712_spk_init(struct snd_soc_pcm_runtime *rtd)
38 {
39 	struct snd_soc_card *card = rtd->card;
40 	int ret;
41 
42 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
43 					  "%s spk:rt712",
44 					  card->components);
45 	if (!card->components)
46 		return -ENOMEM;
47 
48 	ret = snd_soc_add_card_controls(card, rt712_spk_controls,
49 					ARRAY_SIZE(rt712_spk_controls));
50 	if (ret) {
51 		dev_err(card->dev, "rt712 spk controls addition failed: %d\n", ret);
52 		return ret;
53 	}
54 
55 	ret = snd_soc_dapm_new_controls(&card->dapm, rt712_spk_widgets,
56 					ARRAY_SIZE(rt712_spk_widgets));
57 	if (ret) {
58 		dev_err(card->dev, "rt712 spk widgets addition failed: %d\n", ret);
59 		return ret;
60 	}
61 
62 	ret = snd_soc_dapm_add_routes(&card->dapm, rt712_spk_map, ARRAY_SIZE(rt712_spk_map));
63 	if (ret)
64 		dev_err(rtd->dev, "failed to add SPK map: %d\n", ret);
65 
66 	return ret;
67 }
68 
sof_sdw_rt712_spk_init(struct snd_soc_card * card,const struct snd_soc_acpi_link_adr * link,struct snd_soc_dai_link * dai_links,struct sof_sdw_codec_info * info,bool playback)69 int sof_sdw_rt712_spk_init(struct snd_soc_card *card,
70 			   const struct snd_soc_acpi_link_adr *link,
71 			   struct snd_soc_dai_link *dai_links,
72 			   struct sof_sdw_codec_info *info,
73 			   bool playback)
74 {
75 	dai_links->init = rt712_spk_init;
76 
77 	return 0;
78 }
79 
rt712_sdca_dmic_rtd_init(struct snd_soc_pcm_runtime * rtd)80 static int rt712_sdca_dmic_rtd_init(struct snd_soc_pcm_runtime *rtd)
81 {
82 	struct snd_soc_card *card = rtd->card;
83 
84 	card->components = devm_kasprintf(card->dev, GFP_KERNEL,
85 					  "%s mic:rt712-sdca-dmic",
86 					  card->components);
87 	if (!card->components)
88 		return -ENOMEM;
89 
90 	return 0;
91 }
92 
sof_sdw_rt712_sdca_dmic_init(struct snd_soc_card * card,const struct snd_soc_acpi_link_adr * link,struct snd_soc_dai_link * dai_links,struct sof_sdw_codec_info * info,bool playback)93 int sof_sdw_rt712_sdca_dmic_init(struct snd_soc_card *card,
94 				 const struct snd_soc_acpi_link_adr *link,
95 				 struct snd_soc_dai_link *dai_links,
96 				 struct sof_sdw_codec_info *info,
97 				 bool playback)
98 {
99 	dai_links->init = rt712_sdca_dmic_rtd_init;
100 
101 	return 0;
102 }
103