xref: /linux/sound/soc/intel/boards/sof_cirrus_common.c (revision 2aa680df68062e4e0c356ec2aa7100c13654907b)
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 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);
52 	int ret;
53 
54 	ret = snd_soc_dapm_new_controls(dapm, cs35l41_dapm_widgets,
55 					ARRAY_SIZE(cs35l41_dapm_widgets));
56 	if (ret) {
57 		dev_err(rtd->dev, "fail to add dapm controls, ret %d\n", ret);
58 		return ret;
59 	}
60 
61 	ret = snd_soc_add_card_controls(card, cs35l41_kcontrols,
62 					ARRAY_SIZE(cs35l41_kcontrols));
63 	if (ret) {
64 		dev_err(rtd->dev, "fail to add card controls, ret %d\n", ret);
65 		return ret;
66 	}
67 
68 	ret = snd_soc_dapm_add_routes(dapm, cs35l41_dapm_routes,
69 				      ARRAY_SIZE(cs35l41_dapm_routes));
70 
71 	if (ret)
72 		dev_err(rtd->dev, "fail to add dapm routes, ret %d\n", ret);
73 
74 	return ret;
75 }
76 
77 /*
78  * Channel map:
79  *
80  * TL/WL: ASPRX1 on slot 0, ASPRX2 on slot 1 (default)
81  * TR/WR: ASPRX1 on slot 1, ASPRX2 on slot 0
82  */
83 static const struct {
84 	unsigned int rx[2];
85 } cs35l41_channel_map[] = {
86 	{.rx = {0, 1}}, /* WL */
87 	{.rx = {1, 0}}, /* WR */
88 	{.rx = {0, 1}}, /* TL */
89 	{.rx = {1, 0}}, /* TR */
90 };
91 
92 static int cs35l41_hw_params(struct snd_pcm_substream *substream,
93 			     struct snd_pcm_hw_params *params)
94 {
95 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
96 	struct snd_soc_dai *codec_dai;
97 	int clk_freq, i, ret;
98 
99 	clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
100 
101 	if (clk_freq <= 0) {
102 		dev_err(rtd->dev, "fail to get bclk freq, ret %d\n", clk_freq);
103 		return -EINVAL;
104 	}
105 
106 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
107 		/* call dai driver's set_sysclk() callback */
108 		ret = snd_soc_dai_set_sysclk(codec_dai, CS35L41_CLKID_SCLK,
109 					     clk_freq, SND_SOC_CLOCK_IN);
110 		if (ret < 0) {
111 			dev_err(codec_dai->dev, "fail to set sysclk, ret %d\n",
112 				ret);
113 			return ret;
114 		}
115 
116 		/* call component driver's set_sysclk() callback */
117 		ret = snd_soc_component_set_sysclk(codec_dai->component,
118 						   CS35L41_CLKID_SCLK, 0,
119 						   clk_freq, SND_SOC_CLOCK_IN);
120 		if (ret < 0) {
121 			dev_err(codec_dai->dev, "fail to set component sysclk, ret %d\n",
122 				ret);
123 			return ret;
124 		}
125 
126 		/* setup channel map */
127 		ret = snd_soc_dai_set_channel_map(codec_dai, 0, NULL,
128 						  ARRAY_SIZE(cs35l41_channel_map[i].rx),
129 						  (unsigned int *)cs35l41_channel_map[i].rx);
130 		if (ret < 0) {
131 			dev_err(codec_dai->dev, "fail to set channel map, ret %d\n",
132 				ret);
133 			return ret;
134 		}
135 	}
136 
137 	return 0;
138 }
139 
140 static const struct snd_soc_ops cs35l41_ops = {
141 	.hw_params = cs35l41_hw_params,
142 };
143 
144 static const char * const cs35l41_name_prefixes[] = { "WL", "WR", "TL", "TR" };
145 
146 /*
147  * Expected UIDs are integers (stored as strings).
148  * UID Mapping is fixed:
149  * UID 0x0 -> WL
150  * UID 0x1 -> WR
151  * UID 0x2 -> TL
152  * UID 0x3 -> TR
153  * Note: If there are less than 4 Amps, UIDs still map to WL/WR/TL/TR. Dynamic code will only create
154  * dai links for UIDs which exist, and ignore non-existant ones. Only 2 or 4 amps are expected.
155  * Return number of codecs found.
156  */
157 static int cs35l41_compute_codec_conf(void)
158 {
159 	static const char * const uid_strings[] = { "0", "1", "2", "3" };
160 	unsigned int uid, sz = 0;
161 	struct acpi_device *adev;
162 	struct device *physdev;
163 
164 	for (uid = 0; uid < CS35L41_MAX_AMPS; uid++) {
165 		adev = acpi_dev_get_first_match_dev(CS35L41_HID, uid_strings[uid], -1);
166 		if (!adev) {
167 			pr_devel("Cannot find match for HID %s UID %u (%s)\n", CS35L41_HID, uid,
168 				 cs35l41_name_prefixes[uid]);
169 			continue;
170 		}
171 		physdev = get_device(acpi_get_first_physical_node(adev));
172 		acpi_dev_put(adev);
173 		if (!physdev) {
174 			pr_devel("Cannot find physical node for HID %s UID %u (%s)\n", CS35L41_HID,
175 					uid, cs35l41_name_prefixes[uid]);
176 			return 0;
177 		}
178 		cs35l41_components[sz].name = dev_name(physdev);
179 		cs35l41_components[sz].dai_name = CS35L41_CODEC_DAI;
180 		cs35l41_codec_conf[sz].dlc.name = dev_name(physdev);
181 		cs35l41_codec_conf[sz].name_prefix = cs35l41_name_prefixes[uid];
182 		sz++;
183 	}
184 
185 	if (sz != 2 && sz != 4)
186 		pr_warn("Invalid number of cs35l41 amps found: %d, expected 2 or 4\n", sz);
187 	return sz;
188 }
189 
190 void cs35l41_set_dai_link(struct snd_soc_dai_link *link)
191 {
192 	link->num_codecs = cs35l41_compute_codec_conf();
193 	link->codecs = cs35l41_components;
194 	link->init = cs35l41_init;
195 	link->ops = &cs35l41_ops;
196 }
197 EXPORT_SYMBOL_NS(cs35l41_set_dai_link, "SND_SOC_INTEL_SOF_CIRRUS_COMMON");
198 
199 void cs35l41_set_codec_conf(struct snd_soc_card *card)
200 {
201 	card->codec_conf = cs35l41_codec_conf;
202 	card->num_configs = ARRAY_SIZE(cs35l41_codec_conf);
203 }
204 EXPORT_SYMBOL_NS(cs35l41_set_codec_conf, "SND_SOC_INTEL_SOF_CIRRUS_COMMON");
205 
206 MODULE_DESCRIPTION("ASoC Intel SOF Cirrus Logic helpers");
207 MODULE_LICENSE("GPL");
208