1 // SPDX-License-Identifier: GPL-2.0-only
2 // This file incorporates work covered by the following copyright notice:
3 // Copyright (c) 2023 Intel Corporation
4 // Copyright (c) 2024 Advanced Micro Devices, Inc.
5
6 /*
7 * soc_sdw_cs_amp - Helpers to handle CS35L56 from generic machine driver
8 */
9
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <sound/soc.h>
13 #include <sound/soc-acpi.h>
14 #include <sound/soc-dai.h>
15 #include <sound/soc_sdw_utils.h>
16
17 #define CODEC_NAME_SIZE 8
18 #define CS_AMP_CHANNELS_PER_AMP 4
19
asoc_sdw_cs_spk_rtd_init(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)20 int asoc_sdw_cs_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
21 {
22 const char *dai_name = rtd->dai_link->codecs->dai_name;
23 struct snd_soc_card *card = rtd->card;
24 char codec_name[CODEC_NAME_SIZE];
25 char widget_name[16];
26 struct snd_soc_dapm_route route = { "Speaker", NULL, widget_name };
27 struct snd_soc_dai *codec_dai;
28 int i, ret;
29
30 snprintf(codec_name, CODEC_NAME_SIZE, "%s", dai_name);
31 card->components = devm_kasprintf(card->dev, GFP_KERNEL,
32 "%s spk:%s",
33 card->components, codec_name);
34 if (!card->components)
35 return -ENOMEM;
36
37 for_each_rtd_codec_dais(rtd, i, codec_dai) {
38 if (!strstr(codec_dai->name, "cs35l56"))
39 continue;
40
41 snprintf(widget_name, sizeof(widget_name), "%s SPK",
42 codec_dai->component->name_prefix);
43 ret = snd_soc_dapm_add_routes(&card->dapm, &route, 1);
44 if (ret)
45 return ret;
46 }
47
48 return 0;
49 }
50 EXPORT_SYMBOL_NS(asoc_sdw_cs_spk_rtd_init, "SND_SOC_SDW_UTILS");
51
asoc_sdw_cs_spk_feedback_rtd_init(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)52 int asoc_sdw_cs_spk_feedback_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
53 {
54 const struct snd_soc_dai_link *dai_link = rtd->dai_link;
55 const struct snd_soc_dai_link_ch_map *ch_map;
56 const struct snd_soc_dai_link_component *codec_dlc;
57 struct snd_soc_dai *codec_dai;
58 u8 ch_slot[8] = {};
59 unsigned int amps_per_bus, ch_per_amp, mask;
60 int i, ret;
61
62 WARN_ON(dai_link->num_cpus > ARRAY_SIZE(ch_slot));
63
64 /*
65 * CS35L56 has 4 TX channels. When the capture is aggregated the
66 * same bus slots will be allocated to all the amps on a bus. Only
67 * one amp on that bus can be transmitting in each slot so divide
68 * the available 4 slots between all the amps on a bus.
69 */
70 amps_per_bus = dai_link->num_codecs / dai_link->num_cpus;
71 if ((amps_per_bus == 0) || (amps_per_bus > CS_AMP_CHANNELS_PER_AMP)) {
72 dev_err(rtd->card->dev, "Illegal num_codecs:%u / num_cpus:%u\n",
73 dai_link->num_codecs, dai_link->num_cpus);
74 return -EINVAL;
75 }
76
77 ch_per_amp = CS_AMP_CHANNELS_PER_AMP / amps_per_bus;
78
79 for_each_rtd_ch_maps(rtd, i, ch_map) {
80 codec_dlc = snd_soc_link_to_codec(rtd->dai_link, i);
81 codec_dai = snd_soc_find_dai(codec_dlc);
82 mask = GENMASK(ch_per_amp - 1, 0) << ch_slot[ch_map->cpu];
83
84 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0, mask, 4, 32);
85 if (ret < 0) {
86 dev_err(rtd->card->dev, "Failed to set TDM slot:%d\n", ret);
87 return ret;
88 }
89
90 ch_slot[ch_map->cpu] += ch_per_amp;
91 }
92
93 return 0;
94 }
95 EXPORT_SYMBOL_NS(asoc_sdw_cs_spk_feedback_rtd_init, "SND_SOC_SDW_UTILS");
96
asoc_sdw_cs_amp_init(struct snd_soc_card * card,struct snd_soc_dai_link * dai_links,struct asoc_sdw_codec_info * info,bool playback)97 int asoc_sdw_cs_amp_init(struct snd_soc_card *card,
98 struct snd_soc_dai_link *dai_links,
99 struct asoc_sdw_codec_info *info,
100 bool playback)
101 {
102 /* Do init on playback link only. */
103 if (!playback)
104 return 0;
105
106 info->amp_num++;
107
108 return 0;
109 }
110 EXPORT_SYMBOL_NS(asoc_sdw_cs_amp_init, "SND_SOC_SDW_UTILS");
111