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