linux/sound/soc/intel/boards/sof_sdw_max98373.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2// Copyright (c) 2020 Intel Corporation
   3//
   4// sof_sdw_max98373 - Helpers to handle 2x MAX98373
   5// codec devices from generic machine driver
   6
   7#include <linux/device.h>
   8#include <linux/errno.h>
   9#include <sound/control.h>
  10#include <sound/soc.h>
  11#include <sound/soc-acpi.h>
  12#include <sound/soc-dapm.h>
  13#include "sof_sdw_common.h"
  14#include "sof_maxim_common.h"
  15
  16static const struct snd_soc_dapm_widget mx8373_widgets[] = {
  17        SND_SOC_DAPM_SPK("Left Spk", NULL),
  18        SND_SOC_DAPM_SPK("Right Spk", NULL),
  19};
  20
  21static const struct snd_kcontrol_new mx8373_controls[] = {
  22        SOC_DAPM_PIN_SWITCH("Left Spk"),
  23        SOC_DAPM_PIN_SWITCH("Right Spk"),
  24};
  25
  26static int spk_init(struct snd_soc_pcm_runtime *rtd)
  27{
  28        struct snd_soc_card *card = rtd->card;
  29        int ret;
  30
  31        card->components = devm_kasprintf(card->dev, GFP_KERNEL,
  32                                          "%s spk:mx8373",
  33                                          card->components);
  34        if (!card->components)
  35                return -ENOMEM;
  36
  37        ret = snd_soc_add_card_controls(card, mx8373_controls,
  38                                        ARRAY_SIZE(mx8373_controls));
  39        if (ret) {
  40                dev_err(card->dev, "mx8373 ctrls addition failed: %d\n", ret);
  41                return ret;
  42        }
  43
  44        ret = snd_soc_dapm_new_controls(&card->dapm, mx8373_widgets,
  45                                        ARRAY_SIZE(mx8373_widgets));
  46        if (ret) {
  47                dev_err(card->dev, "mx8373 widgets addition failed: %d\n", ret);
  48                return ret;
  49        }
  50
  51        ret = snd_soc_dapm_add_routes(&card->dapm, max_98373_dapm_routes, 2);
  52        if (ret)
  53                dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
  54
  55        return ret;
  56}
  57
  58static int mx8373_enable_spk_pin(struct snd_pcm_substream *substream, bool enable)
  59{
  60        struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  61        struct snd_soc_dai *codec_dai;
  62        struct snd_soc_dai *cpu_dai;
  63        int ret;
  64        int j;
  65
  66        /* set spk pin by playback only */
  67        if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  68                return 0;
  69
  70        cpu_dai = asoc_rtd_to_cpu(rtd, 0);
  71        for_each_rtd_codec_dais(rtd, j, codec_dai) {
  72                struct snd_soc_dapm_context *dapm =
  73                                snd_soc_component_get_dapm(cpu_dai->component);
  74                char pin_name[16];
  75
  76                snprintf(pin_name, ARRAY_SIZE(pin_name), "%s Spk",
  77                         codec_dai->component->name_prefix);
  78
  79                if (enable)
  80                        ret = snd_soc_dapm_enable_pin(dapm, pin_name);
  81                else
  82                        ret = snd_soc_dapm_disable_pin(dapm, pin_name);
  83
  84                if (!ret)
  85                        snd_soc_dapm_sync(dapm);
  86        }
  87
  88        return 0;
  89}
  90
  91static int mx8373_sdw_prepare(struct snd_pcm_substream *substream)
  92{
  93        int ret;
  94
  95        /* according to soc_pcm_prepare dai link prepare is called first */
  96        ret = sdw_prepare(substream);
  97        if (ret < 0)
  98                return ret;
  99
 100        return mx8373_enable_spk_pin(substream, true);
 101}
 102
 103static int mx8373_sdw_hw_free(struct snd_pcm_substream *substream)
 104{
 105        int ret;
 106
 107        /* according to soc_pcm_hw_free dai link free is called first */
 108        ret = sdw_hw_free(substream);
 109        if (ret < 0)
 110                return ret;
 111
 112        return mx8373_enable_spk_pin(substream, false);
 113}
 114
 115static const struct snd_soc_ops max_98373_sdw_ops = {
 116        .startup = sdw_startup,
 117        .prepare = mx8373_sdw_prepare,
 118        .trigger = sdw_trigger,
 119        .hw_free = mx8373_sdw_hw_free,
 120        .shutdown = sdw_shutdown,
 121};
 122
 123int sof_sdw_mx8373_init(struct snd_soc_card *card,
 124                        const struct snd_soc_acpi_link_adr *link,
 125                        struct snd_soc_dai_link *dai_links,
 126                        struct sof_sdw_codec_info *info,
 127                        bool playback)
 128{
 129        info->amp_num++;
 130        if (info->amp_num == 2)
 131                dai_links->init = spk_init;
 132
 133        info->late_probe = true;
 134
 135        dai_links->ops = &max_98373_sdw_ops;
 136
 137        return 0;
 138}
 139
 140int sof_sdw_mx8373_late_probe(struct snd_soc_card *card)
 141{
 142        struct snd_soc_dapm_context *dapm = &card->dapm;
 143
 144        /* Disable Left and Right Spk pin after boot */
 145        snd_soc_dapm_disable_pin(dapm, "Left Spk");
 146        snd_soc_dapm_disable_pin(dapm, "Right Spk");
 147        return snd_soc_dapm_sync(dapm);
 148}
 149