xref: /linux/sound/soc/intel/boards/sof_cirrus_common.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
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 /*
14  * Cirrus Logic CS35L41/CS35L53
15  */
16 static const struct snd_kcontrol_new cs35l41_kcontrols[] = {
17 	SOC_DAPM_PIN_SWITCH("WL Spk"),
18 	SOC_DAPM_PIN_SWITCH("WR Spk"),
19 	SOC_DAPM_PIN_SWITCH("TL Spk"),
20 	SOC_DAPM_PIN_SWITCH("TR Spk"),
21 };
22 
23 static const struct snd_soc_dapm_widget cs35l41_dapm_widgets[] = {
24 	SND_SOC_DAPM_SPK("WL Spk", NULL),
25 	SND_SOC_DAPM_SPK("WR Spk", NULL),
26 	SND_SOC_DAPM_SPK("TL Spk", NULL),
27 	SND_SOC_DAPM_SPK("TR Spk", NULL),
28 };
29 
30 static const struct snd_soc_dapm_route cs35l41_dapm_routes[] = {
31 	/* speaker */
32 	{"WL Spk", NULL, "WL SPK"},
33 	{"WR Spk", NULL, "WR SPK"},
34 	{"TL Spk", NULL, "TL SPK"},
35 	{"TR Spk", NULL, "TR SPK"},
36 };
37 
38 static struct snd_soc_dai_link_component cs35l41_components[] = {
39 	{
40 		.name = CS35L41_DEV0_NAME,
41 		.dai_name = CS35L41_CODEC_DAI,
42 	},
43 	{
44 		.name = CS35L41_DEV1_NAME,
45 		.dai_name = CS35L41_CODEC_DAI,
46 	},
47 	{
48 		.name = CS35L41_DEV2_NAME,
49 		.dai_name = CS35L41_CODEC_DAI,
50 	},
51 	{
52 		.name = CS35L41_DEV3_NAME,
53 		.dai_name = CS35L41_CODEC_DAI,
54 	},
55 };
56 
57 /*
58  * Mapping between ACPI instance id and speaker position.
59  *
60  * Four speakers:
61  *         0: Tweeter left, 1: Woofer left
62  *         2: Tweeter right, 3: Woofer right
63  */
64 static struct snd_soc_codec_conf cs35l41_codec_conf[] = {
65 	{
66 		.dlc = COMP_CODEC_CONF(CS35L41_DEV0_NAME),
67 		.name_prefix = "TL",
68 	},
69 	{
70 		.dlc = COMP_CODEC_CONF(CS35L41_DEV1_NAME),
71 		.name_prefix = "WL",
72 	},
73 	{
74 		.dlc = COMP_CODEC_CONF(CS35L41_DEV2_NAME),
75 		.name_prefix = "TR",
76 	},
77 	{
78 		.dlc = COMP_CODEC_CONF(CS35L41_DEV3_NAME),
79 		.name_prefix = "WR",
80 	},
81 };
82 
83 static int cs35l41_init(struct snd_soc_pcm_runtime *rtd)
84 {
85 	struct snd_soc_card *card = rtd->card;
86 	int ret;
87 
88 	ret = snd_soc_dapm_new_controls(&card->dapm, cs35l41_dapm_widgets,
89 					ARRAY_SIZE(cs35l41_dapm_widgets));
90 	if (ret) {
91 		dev_err(rtd->dev, "fail to add dapm controls, ret %d\n", ret);
92 		return ret;
93 	}
94 
95 	ret = snd_soc_add_card_controls(card, cs35l41_kcontrols,
96 					ARRAY_SIZE(cs35l41_kcontrols));
97 	if (ret) {
98 		dev_err(rtd->dev, "fail to add card controls, ret %d\n", ret);
99 		return ret;
100 	}
101 
102 	ret = snd_soc_dapm_add_routes(&card->dapm, cs35l41_dapm_routes,
103 				      ARRAY_SIZE(cs35l41_dapm_routes));
104 
105 	if (ret)
106 		dev_err(rtd->dev, "fail to add dapm routes, ret %d\n", ret);
107 
108 	return ret;
109 }
110 
111 /*
112  * Channel map:
113  *
114  * TL/WL: ASPRX1 on slot 0, ASPRX2 on slot 1 (default)
115  * TR/WR: ASPRX1 on slot 1, ASPRX2 on slot 0
116  */
117 static const struct {
118 	unsigned int rx[2];
119 } cs35l41_channel_map[] = {
120 	{.rx = {0, 1}}, /* TL */
121 	{.rx = {0, 1}}, /* WL */
122 	{.rx = {1, 0}}, /* TR */
123 	{.rx = {1, 0}}, /* WR */
124 };
125 
126 static int cs35l41_hw_params(struct snd_pcm_substream *substream,
127 			     struct snd_pcm_hw_params *params)
128 {
129 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
130 	struct snd_soc_dai *codec_dai;
131 	int clk_freq, i, ret;
132 
133 	clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
134 
135 	if (clk_freq <= 0) {
136 		dev_err(rtd->dev, "fail to get bclk freq, ret %d\n", clk_freq);
137 		return -EINVAL;
138 	}
139 
140 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
141 		/* call dai driver's set_sysclk() callback */
142 		ret = snd_soc_dai_set_sysclk(codec_dai, CS35L41_CLKID_SCLK,
143 					     clk_freq, SND_SOC_CLOCK_IN);
144 		if (ret < 0) {
145 			dev_err(codec_dai->dev, "fail to set sysclk, ret %d\n",
146 				ret);
147 			return ret;
148 		}
149 
150 		/* call component driver's set_sysclk() callback */
151 		ret = snd_soc_component_set_sysclk(codec_dai->component,
152 						   CS35L41_CLKID_SCLK, 0,
153 						   clk_freq, SND_SOC_CLOCK_IN);
154 		if (ret < 0) {
155 			dev_err(codec_dai->dev, "fail to set component sysclk, ret %d\n",
156 				ret);
157 			return ret;
158 		}
159 
160 		/* setup channel map */
161 		ret = snd_soc_dai_set_channel_map(codec_dai, 0, NULL,
162 						  ARRAY_SIZE(cs35l41_channel_map[i].rx),
163 						  (unsigned int *)cs35l41_channel_map[i].rx);
164 		if (ret < 0) {
165 			dev_err(codec_dai->dev, "fail to set channel map, ret %d\n",
166 				ret);
167 			return ret;
168 		}
169 	}
170 
171 	return 0;
172 }
173 
174 static const struct snd_soc_ops cs35l41_ops = {
175 	.hw_params = cs35l41_hw_params,
176 };
177 
178 void cs35l41_set_dai_link(struct snd_soc_dai_link *link)
179 {
180 	link->codecs = cs35l41_components;
181 	link->num_codecs = ARRAY_SIZE(cs35l41_components);
182 	link->init = cs35l41_init;
183 	link->ops = &cs35l41_ops;
184 }
185 EXPORT_SYMBOL_NS(cs35l41_set_dai_link, SND_SOC_INTEL_SOF_CIRRUS_COMMON);
186 
187 void cs35l41_set_codec_conf(struct snd_soc_card *card)
188 {
189 	card->codec_conf = cs35l41_codec_conf;
190 	card->num_configs = ARRAY_SIZE(cs35l41_codec_conf);
191 }
192 EXPORT_SYMBOL_NS(cs35l41_set_codec_conf, SND_SOC_INTEL_SOF_CIRRUS_COMMON);
193 
194 MODULE_DESCRIPTION("ASoC Intel SOF Cirrus Logic helpers");
195 MODULE_LICENSE("GPL");
196