xref: /linux/sound/soc/intel/boards/sof_cirrus_common.c (revision 8e1bb4a41aa78d6105e59186af3dcd545fc66e70)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This file defines data structures and functions used in Machine
4  * Driver for Intel platforms with Cirrus Logic Codecs.
5  *
6  * Copyright 2022 Intel Corporation.
7  */
8 #include <linux/module.h>
9 #include <sound/sof.h>
10 #include "../../codecs/cs35l41.h"
11 #include "sof_cirrus_common.h"
12 
13 #define CS35L41_HID "CSC3541"
14 #define CS35L41_MAX_AMPS 4
15 
16 /*
17  * Cirrus Logic CS35L41/CS35L53
18  */
19 static const struct snd_kcontrol_new cs35l41_kcontrols[] = {
20 	SOC_DAPM_PIN_SWITCH("WL Spk"),
21 	SOC_DAPM_PIN_SWITCH("WR Spk"),
22 	SOC_DAPM_PIN_SWITCH("TL Spk"),
23 	SOC_DAPM_PIN_SWITCH("TR Spk"),
24 };
25 
26 static const struct snd_soc_dapm_widget cs35l41_dapm_widgets[] = {
27 	SND_SOC_DAPM_SPK("WL Spk", NULL),
28 	SND_SOC_DAPM_SPK("WR Spk", NULL),
29 	SND_SOC_DAPM_SPK("TL Spk", NULL),
30 	SND_SOC_DAPM_SPK("TR Spk", NULL),
31 };
32 
33 static const struct snd_soc_dapm_route cs35l41_dapm_routes[] = {
34 	/* speaker */
35 	{"WL Spk", NULL, "WL SPK"},
36 	{"WR Spk", NULL, "WR SPK"},
37 	{"TL Spk", NULL, "TL SPK"},
38 	{"TR Spk", NULL, "TR SPK"},
39 };
40 
41 static struct snd_soc_dai_link_component cs35l41_components[CS35L41_MAX_AMPS];
42 
43 /*
44  * Mapping between ACPI instance id and speaker position.
45  */
46 static struct snd_soc_codec_conf cs35l41_codec_conf[CS35L41_MAX_AMPS];
47 
48 static int cs35l41_init(struct snd_soc_pcm_runtime *rtd)
49 {
50 	struct snd_soc_card *card = rtd->card;
51 	int ret;
52 
53 	ret = snd_soc_dapm_new_controls(&card->dapm, cs35l41_dapm_widgets,
54 					ARRAY_SIZE(cs35l41_dapm_widgets));
55 	if (ret) {
56 		dev_err(rtd->dev, "fail to add dapm controls, ret %d\n", ret);
57 		return ret;
58 	}
59 
60 	ret = snd_soc_add_card_controls(card, cs35l41_kcontrols,
61 					ARRAY_SIZE(cs35l41_kcontrols));
62 	if (ret) {
63 		dev_err(rtd->dev, "fail to add card controls, ret %d\n", ret);
64 		return ret;
65 	}
66 
67 	ret = snd_soc_dapm_add_routes(&card->dapm, cs35l41_dapm_routes,
68 				      ARRAY_SIZE(cs35l41_dapm_routes));
69 
70 	if (ret)
71 		dev_err(rtd->dev, "fail to add dapm routes, ret %d\n", ret);
72 
73 	return ret;
74 }
75 
76 /*
77  * Channel map:
78  *
79  * TL/WL: ASPRX1 on slot 0, ASPRX2 on slot 1 (default)
80  * TR/WR: ASPRX1 on slot 1, ASPRX2 on slot 0
81  */
82 static const struct {
83 	unsigned int rx[2];
84 } cs35l41_channel_map[] = {
85 	{.rx = {0, 1}}, /* WL */
86 	{.rx = {1, 0}}, /* WR */
87 	{.rx = {0, 1}}, /* TL */
88 	{.rx = {1, 0}}, /* TR */
89 };
90 
91 static int cs35l41_hw_params(struct snd_pcm_substream *substream,
92 			     struct snd_pcm_hw_params *params)
93 {
94 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
95 	struct snd_soc_dai *codec_dai;
96 	int clk_freq, i, ret;
97 
98 	clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
99 
100 	if (clk_freq <= 0) {
101 		dev_err(rtd->dev, "fail to get bclk freq, ret %d\n", clk_freq);
102 		return -EINVAL;
103 	}
104 
105 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
106 		/* call dai driver's set_sysclk() callback */
107 		ret = snd_soc_dai_set_sysclk(codec_dai, CS35L41_CLKID_SCLK,
108 					     clk_freq, SND_SOC_CLOCK_IN);
109 		if (ret < 0) {
110 			dev_err(codec_dai->dev, "fail to set sysclk, ret %d\n",
111 				ret);
112 			return ret;
113 		}
114 
115 		/* call component driver's set_sysclk() callback */
116 		ret = snd_soc_component_set_sysclk(codec_dai->component,
117 						   CS35L41_CLKID_SCLK, 0,
118 						   clk_freq, SND_SOC_CLOCK_IN);
119 		if (ret < 0) {
120 			dev_err(codec_dai->dev, "fail to set component sysclk, ret %d\n",
121 				ret);
122 			return ret;
123 		}
124 
125 		/* setup channel map */
126 		ret = snd_soc_dai_set_channel_map(codec_dai, 0, NULL,
127 						  ARRAY_SIZE(cs35l41_channel_map[i].rx),
128 						  (unsigned int *)cs35l41_channel_map[i].rx);
129 		if (ret < 0) {
130 			dev_err(codec_dai->dev, "fail to set channel map, ret %d\n",
131 				ret);
132 			return ret;
133 		}
134 	}
135 
136 	return 0;
137 }
138 
139 static const struct snd_soc_ops cs35l41_ops = {
140 	.hw_params = cs35l41_hw_params,
141 };
142 
143 static const char * const cs35l41_name_prefixes[] = { "WL", "WR", "TL", "TR" };
144 
145 /*
146  * Expected UIDs are integers (stored as strings).
147  * UID Mapping is fixed:
148  * UID 0x0 -> WL
149  * UID 0x1 -> WR
150  * UID 0x2 -> TL
151  * UID 0x3 -> TR
152  * Note: If there are less than 4 Amps, UIDs still map to WL/WR/TL/TR. Dynamic code will only create
153  * dai links for UIDs which exist, and ignore non-existant ones. Only 2 or 4 amps are expected.
154  * Return number of codecs found.
155  */
156 static int cs35l41_compute_codec_conf(void)
157 {
158 	static const char * const uid_strings[] = { "0", "1", "2", "3" };
159 	unsigned int uid, sz = 0;
160 	struct acpi_device *adev;
161 	struct device *physdev;
162 
163 	for (uid = 0; uid < CS35L41_MAX_AMPS; uid++) {
164 		adev = acpi_dev_get_first_match_dev(CS35L41_HID, uid_strings[uid], -1);
165 		if (!adev) {
166 			pr_devel("Cannot find match for HID %s UID %u (%s)\n", CS35L41_HID, uid,
167 				 cs35l41_name_prefixes[uid]);
168 			continue;
169 		}
170 		physdev = get_device(acpi_get_first_physical_node(adev));
171 		acpi_dev_put(adev);
172 		if (!physdev) {
173 			pr_devel("Cannot find physical node for HID %s UID %u (%s)\n", CS35L41_HID,
174 					uid, cs35l41_name_prefixes[uid]);
175 			return 0;
176 		}
177 		cs35l41_components[sz].name = dev_name(physdev);
178 		cs35l41_components[sz].dai_name = CS35L41_CODEC_DAI;
179 		cs35l41_codec_conf[sz].dlc.name = dev_name(physdev);
180 		cs35l41_codec_conf[sz].name_prefix = cs35l41_name_prefixes[uid];
181 		sz++;
182 	}
183 
184 	if (sz != 2 && sz != 4)
185 		pr_warn("Invalid number of cs35l41 amps found: %d, expected 2 or 4\n", sz);
186 	return sz;
187 }
188 
189 void cs35l41_set_dai_link(struct snd_soc_dai_link *link)
190 {
191 	link->num_codecs = cs35l41_compute_codec_conf();
192 	link->codecs = cs35l41_components;
193 	link->init = cs35l41_init;
194 	link->ops = &cs35l41_ops;
195 }
196 EXPORT_SYMBOL_NS(cs35l41_set_dai_link, SND_SOC_INTEL_SOF_CIRRUS_COMMON);
197 
198 void cs35l41_set_codec_conf(struct snd_soc_card *card)
199 {
200 	card->codec_conf = cs35l41_codec_conf;
201 	card->num_configs = ARRAY_SIZE(cs35l41_codec_conf);
202 }
203 EXPORT_SYMBOL_NS(cs35l41_set_codec_conf, SND_SOC_INTEL_SOF_CIRRUS_COMMON);
204 
205 MODULE_DESCRIPTION("ASoC Intel SOF Cirrus Logic helpers");
206 MODULE_LICENSE("GPL");
207