1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file defines data structures and functions used in Machine
4 * Driver for Intel platforms with Cirrus Logic Codecs.
6 * Copyright 2022 Intel Corporation.
8 #include <linux/module.h>
10 #include "../../codecs/cs35l41.h"
11 #include "sof_cirrus_common.h"
13 #define CS35L41_HID "CSC3541"
14 #define CS35L41_MAX_AMPS 4
17 * Cirrus Logic CS35L41/CS35L53
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"),
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),
33 static const struct snd_soc_dapm_route cs35l41_dapm_routes[] = {
35 {"WL Spk", NULL, "WL SPK"},
36 {"WR Spk", NULL, "WR SPK"},
37 {"TL Spk", NULL, "TL SPK"},
38 {"TR Spk", NULL, "TR SPK"},
41 static struct snd_soc_dai_link_component cs35l41_components[CS35L41_MAX_AMPS];
44 * Mapping between ACPI instance id and speaker position.
46 static struct snd_soc_codec_conf cs35l41_codec_conf[CS35L41_MAX_AMPS];
48 static int cs35l41_init(struct snd_soc_pcm_runtime *rtd)
50 struct snd_soc_card *card = rtd->card;
53 ret = snd_soc_dapm_new_controls(&card->dapm, cs35l41_dapm_widgets,
54 ARRAY_SIZE(cs35l41_dapm_widgets));
56 dev_err(rtd->dev, "fail to add dapm controls, ret %d\n", ret);
60 ret = snd_soc_add_card_controls(card, cs35l41_kcontrols,
61 ARRAY_SIZE(cs35l41_kcontrols));
63 dev_err(rtd->dev, "fail to add card controls, ret %d\n", ret);
67 ret = snd_soc_dapm_add_routes(&card->dapm, cs35l41_dapm_routes,
68 ARRAY_SIZE(cs35l41_dapm_routes));
71 dev_err(rtd->dev, "fail to add dapm routes, ret %d\n", ret);
79 * TL/WL: ASPRX1 on slot 0, ASPRX2 on slot 1 (default)
80 * TR/WR: ASPRX1 on slot 1, ASPRX2 on slot 0
84 } cs35l41_channel_map[] = {
85 {.rx = {0, 1}}, /* WL */
86 {.rx = {1, 0}}, /* WR */
87 {.rx = {0, 1}}, /* TL */
88 {.rx = {1, 0}}, /* TR */
91 static int cs35l41_hw_params(struct snd_pcm_substream *substream,
92 struct snd_pcm_hw_params *params)
94 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
95 struct snd_soc_dai *codec_dai;
98 clk_freq = sof_dai_get_bclk(rtd); /* BCLK freq */
101 dev_err(rtd->dev, "fail to get bclk freq, ret %d\n", clk_freq);
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);
110 dev_err(codec_dai->dev, "fail to set sysclk, ret %d\n",
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);
120 dev_err(codec_dai->dev, "fail to set component sysclk, ret %d\n",
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);
130 dev_err(codec_dai->dev, "fail to set channel map, ret %d\n",
139 static const struct snd_soc_ops cs35l41_ops = {
140 .hw_params = cs35l41_hw_params,
143 static const char * const cs35l41_name_prefixes[] = { "WL", "WR", "TL", "TR" };
146 * Expected UIDs are integers (stored as strings).
147 * UID Mapping is fixed:
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.
156 static int cs35l41_compute_codec_conf(void)
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;
163 for (uid = 0; uid < CS35L41_MAX_AMPS; uid++) {
164 adev = acpi_dev_get_first_match_dev(CS35L41_HID, uid_strings[uid], -1);
166 pr_devel("Cannot find match for HID %s UID %u (%s)\n", CS35L41_HID, uid,
167 cs35l41_name_prefixes[uid]);
170 physdev = get_device(acpi_get_first_physical_node(adev));
173 pr_devel("Cannot find physical node for HID %s UID %u (%s)\n", CS35L41_HID,
174 uid, cs35l41_name_prefixes[uid]);
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];
184 if (sz != 2 && sz != 4)
185 pr_warn("Invalid number of cs35l41 amps found: %d, expected 2 or 4\n", sz);
189 void cs35l41_set_dai_link(struct snd_soc_dai_link *link)
191 link->num_codecs = cs35l41_compute_codec_conf();
192 link->codecs = cs35l41_components;
193 link->init = cs35l41_init;
194 link->ops = &cs35l41_ops;
196 EXPORT_SYMBOL_NS(cs35l41_set_dai_link, SND_SOC_INTEL_SOF_CIRRUS_COMMON);
198 void cs35l41_set_codec_conf(struct snd_soc_card *card)
200 card->codec_conf = cs35l41_codec_conf;
201 card->num_configs = ARRAY_SIZE(cs35l41_codec_conf);
203 EXPORT_SYMBOL_NS(cs35l41_set_codec_conf, SND_SOC_INTEL_SOF_CIRRUS_COMMON);
205 MODULE_DESCRIPTION("ASoC Intel SOF Cirrus Logic helpers");
206 MODULE_LICENSE("GPL");