xref: /linux/sound/soc/sdw_utils/soc_sdw_cs42l43.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Based on sof_sdw_rt5682.c
3 // This file incorporates work covered by the following copyright notice:
4 // Copyright (c) 2023 Intel Corporation
5 // Copyright (c) 2024 Advanced Micro Devices, Inc.
6 
7 /*
8  *  soc_sdw_cs42l43 - Helpers to handle CS42L43 from generic machine driver
9  */
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/input.h>
13 #include <sound/jack.h>
14 #include <linux/soundwire/sdw.h>
15 #include <linux/soundwire/sdw_type.h>
16 #include <sound/cs42l43.h>
17 #include <sound/control.h>
18 #include <sound/soc.h>
19 #include <sound/soc-acpi.h>
20 #include <sound/soc-dapm.h>
21 #include <sound/soc_sdw_utils.h>
22 
23 static const struct snd_soc_dapm_route cs42l43_hs_map[] = {
24 	{ "Headphone", NULL, "cs42l43 AMP3_OUT" },
25 	{ "Headphone", NULL, "cs42l43 AMP4_OUT" },
26 	{ "cs42l43 ADC1_IN1_P", NULL, "Headset Mic" },
27 	{ "cs42l43 ADC1_IN1_N", NULL, "Headset Mic" },
28 };
29 
30 static const struct snd_soc_dapm_route cs42l43_spk_map[] = {
31 	{ "Speaker", NULL, "cs42l43 AMP1_OUT_P", },
32 	{ "Speaker", NULL, "cs42l43 AMP1_OUT_N", },
33 	{ "Speaker", NULL, "cs42l43 AMP2_OUT_P", },
34 	{ "Speaker", NULL, "cs42l43 AMP2_OUT_N", },
35 };
36 
37 static const struct snd_soc_dapm_route cs42l43_dmic_map[] = {
38 	{ "cs42l43 PDM1_DIN", NULL, "DMIC" },
39 	{ "cs42l43 PDM2_DIN", NULL, "DMIC" },
40 };
41 
42 static struct snd_soc_jack_pin soc_jack_pins[] = {
43 	{
44 		.pin    = "Headphone",
45 		.mask   = SND_JACK_HEADPHONE,
46 	},
47 	{
48 		.pin    = "Headset Mic",
49 		.mask   = SND_JACK_MICROPHONE,
50 	},
51 };
52 
asoc_sdw_cs42l43_hs_rtd_init(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)53 int asoc_sdw_cs42l43_hs_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
54 {
55 	struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
56 	struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(rtd->card);
57 	struct snd_soc_jack *jack = &ctx->sdw_headset;
58 	struct snd_soc_card *card = rtd->card;
59 	int ret;
60 
61 	card->components = devm_kasprintf(card->dev, GFP_KERNEL, "%s hs:cs42l43",
62 					  card->components);
63 	if (!card->components)
64 		return -ENOMEM;
65 
66 	ret = snd_soc_dapm_add_routes(&card->dapm, cs42l43_hs_map,
67 				      ARRAY_SIZE(cs42l43_hs_map));
68 	if (ret) {
69 		dev_err(card->dev, "cs42l43 hs map addition failed: %d\n", ret);
70 		return ret;
71 	}
72 
73 	ret = snd_soc_card_jack_new_pins(card, "Jack",
74 					 SND_JACK_MECHANICAL | SND_JACK_AVOUT |
75 					 SND_JACK_HEADSET | SND_JACK_LINEOUT |
76 					 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
77 					 SND_JACK_BTN_2 | SND_JACK_BTN_3,
78 					 jack, soc_jack_pins,
79 					 ARRAY_SIZE(soc_jack_pins));
80 	if (ret) {
81 		dev_err(card->dev, "Failed to create jack: %d\n", ret);
82 		return ret;
83 	}
84 
85 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
86 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
87 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
88 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
89 
90 	ret = snd_soc_component_set_jack(component, jack, NULL);
91 	if (ret) {
92 		dev_err(card->dev, "Failed to register jack: %d\n", ret);
93 		return ret;
94 	}
95 
96 	ret = snd_soc_component_set_sysclk(component, CS42L43_SYSCLK, CS42L43_SYSCLK_SDW,
97 					   0, SND_SOC_CLOCK_IN);
98 	if (ret)
99 		dev_err(card->dev, "Failed to set sysclk: %d\n", ret);
100 
101 	return ret;
102 }
103 EXPORT_SYMBOL_NS(asoc_sdw_cs42l43_hs_rtd_init, SND_SOC_SDW_UTILS);
104 
asoc_sdw_cs42l43_spk_rtd_init(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)105 int asoc_sdw_cs42l43_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
106 {
107 	struct snd_soc_card *card = rtd->card;
108 	struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);
109 	int ret;
110 
111 	if (!(ctx->mc_quirk & SOC_SDW_SIDECAR_AMPS)) {
112 		/* Will be set by the bridge code in this case */
113 		card->components = devm_kasprintf(card->dev, GFP_KERNEL,
114 						  "%s spk:cs42l43-spk",
115 						  card->components);
116 		if (!card->components)
117 			return -ENOMEM;
118 	}
119 
120 	ret = snd_soc_dapm_add_routes(&card->dapm, cs42l43_spk_map,
121 				      ARRAY_SIZE(cs42l43_spk_map));
122 	if (ret)
123 		dev_err(card->dev, "cs42l43 speaker map addition failed: %d\n", ret);
124 
125 	return ret;
126 }
127 EXPORT_SYMBOL_NS(asoc_sdw_cs42l43_spk_rtd_init, SND_SOC_SDW_UTILS);
128 
asoc_sdw_cs42l43_spk_init(struct snd_soc_card * card,struct snd_soc_dai_link * dai_links,struct asoc_sdw_codec_info * info,bool playback)129 int asoc_sdw_cs42l43_spk_init(struct snd_soc_card *card,
130 			      struct snd_soc_dai_link *dai_links,
131 			      struct asoc_sdw_codec_info *info,
132 			      bool playback)
133 {
134 	/* Do init on playback link only. */
135 	if (!playback)
136 		return 0;
137 
138 	info->amp_num++;
139 
140 	return asoc_sdw_bridge_cs35l56_spk_init(card, dai_links, info, playback);
141 }
142 EXPORT_SYMBOL_NS(asoc_sdw_cs42l43_spk_init, SND_SOC_SDW_UTILS);
143 
asoc_sdw_cs42l43_dmic_rtd_init(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)144 int asoc_sdw_cs42l43_dmic_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
145 {
146 	struct snd_soc_card *card = rtd->card;
147 	int ret;
148 
149 	card->components = devm_kasprintf(card->dev, GFP_KERNEL, "%s mic:cs42l43-dmic",
150 					  card->components);
151 	if (!card->components)
152 		return -ENOMEM;
153 
154 	ret = snd_soc_dapm_add_routes(&card->dapm, cs42l43_dmic_map,
155 				      ARRAY_SIZE(cs42l43_dmic_map));
156 	if (ret)
157 		dev_err(card->dev, "cs42l43 dmic map addition failed: %d\n", ret);
158 
159 	return ret;
160 }
161 EXPORT_SYMBOL_NS(asoc_sdw_cs42l43_dmic_rtd_init, SND_SOC_SDW_UTILS);
162