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 #define CS35L56_SPK_VOLUME_0DB 400 /* 0dB Max */ 20 21 int asoc_sdw_cs35l56_volume_limit(struct snd_soc_card *card, const char *name_prefix) 22 { 23 char *volume_ctl_name; 24 int ret; 25 26 volume_ctl_name = kasprintf(GFP_KERNEL, "%s Speaker Volume", name_prefix); 27 if (!volume_ctl_name) 28 return -ENOMEM; 29 30 ret = snd_soc_limit_volume(card, volume_ctl_name, CS35L56_SPK_VOLUME_0DB); 31 if (ret) 32 dev_err(card->dev, "%s limit set failed: %d\n", volume_ctl_name, ret); 33 34 kfree(volume_ctl_name); 35 return ret; 36 } 37 EXPORT_SYMBOL_NS(asoc_sdw_cs35l56_volume_limit, "SND_SOC_SDW_UTILS"); 38 39 int asoc_sdw_cs_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai) 40 { 41 const char *dai_name = rtd->dai_link->codecs->dai_name; 42 struct snd_soc_card *card = rtd->card; 43 char codec_name[CODEC_NAME_SIZE]; 44 char widget_name[16]; 45 struct snd_soc_dapm_route route = { "Speaker", NULL, widget_name }; 46 struct snd_soc_dai *codec_dai; 47 int i, ret; 48 49 snprintf(codec_name, CODEC_NAME_SIZE, "%s", dai_name); 50 card->components = devm_kasprintf(card->dev, GFP_KERNEL, 51 "%s spk:%s", 52 card->components, codec_name); 53 if (!card->components) 54 return -ENOMEM; 55 56 for_each_rtd_codec_dais(rtd, i, codec_dai) { 57 if (!strstr(codec_dai->name, "cs35l56")) 58 continue; 59 60 snprintf(widget_name, sizeof(widget_name), "%s SPK", 61 codec_dai->component->name_prefix); 62 63 ret = asoc_sdw_cs35l56_volume_limit(card, codec_dai->component->name_prefix); 64 if (ret) 65 return ret; 66 67 ret = snd_soc_dapm_add_routes(&card->dapm, &route, 1); 68 if (ret) 69 return ret; 70 } 71 72 return 0; 73 } 74 EXPORT_SYMBOL_NS(asoc_sdw_cs_spk_rtd_init, "SND_SOC_SDW_UTILS"); 75 76 int asoc_sdw_cs_spk_feedback_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai) 77 { 78 const struct snd_soc_dai_link *dai_link = rtd->dai_link; 79 const struct snd_soc_dai_link_ch_map *ch_map; 80 const struct snd_soc_dai_link_component *codec_dlc; 81 struct snd_soc_dai *codec_dai; 82 u8 ch_slot[8] = {}; 83 unsigned int amps_per_bus, ch_per_amp, mask; 84 int i, ret; 85 86 WARN_ON(dai_link->num_cpus > ARRAY_SIZE(ch_slot)); 87 88 /* 89 * CS35L56 has 4 TX channels. When the capture is aggregated the 90 * same bus slots will be allocated to all the amps on a bus. Only 91 * one amp on that bus can be transmitting in each slot so divide 92 * the available 4 slots between all the amps on a bus. 93 */ 94 amps_per_bus = dai_link->num_codecs / dai_link->num_cpus; 95 if ((amps_per_bus == 0) || (amps_per_bus > CS_AMP_CHANNELS_PER_AMP)) { 96 dev_err(rtd->card->dev, "Illegal num_codecs:%u / num_cpus:%u\n", 97 dai_link->num_codecs, dai_link->num_cpus); 98 return -EINVAL; 99 } 100 101 ch_per_amp = CS_AMP_CHANNELS_PER_AMP / amps_per_bus; 102 103 for_each_rtd_ch_maps(rtd, i, ch_map) { 104 codec_dlc = snd_soc_link_to_codec(rtd->dai_link, i); 105 codec_dai = snd_soc_find_dai(codec_dlc); 106 mask = GENMASK(ch_per_amp - 1, 0) << ch_slot[ch_map->cpu]; 107 108 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0, mask, 4, 32); 109 if (ret < 0) { 110 dev_err(rtd->card->dev, "Failed to set TDM slot:%d\n", ret); 111 return ret; 112 } 113 114 ch_slot[ch_map->cpu] += ch_per_amp; 115 } 116 117 return 0; 118 } 119 EXPORT_SYMBOL_NS(asoc_sdw_cs_spk_feedback_rtd_init, "SND_SOC_SDW_UTILS"); 120 121 int asoc_sdw_cs_amp_init(struct snd_soc_card *card, 122 struct snd_soc_dai_link *dai_links, 123 struct asoc_sdw_codec_info *info, 124 bool playback) 125 { 126 /* Do init on playback link only. */ 127 if (!playback) 128 return 0; 129 130 info->amp_num++; 131 132 return 0; 133 } 134 EXPORT_SYMBOL_NS(asoc_sdw_cs_amp_init, "SND_SOC_SDW_UTILS"); 135