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