Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 // Copyright (c) 2020 Intel Corporation
0003 
0004 /*
0005  *  sof_sdw_rt1316 - Helpers to handle RT1316 from generic machine driver
0006  */
0007 
0008 #include <linux/device.h>
0009 #include <linux/errno.h>
0010 #include <sound/control.h>
0011 #include <sound/soc.h>
0012 #include <sound/soc-acpi.h>
0013 #include <sound/soc-dapm.h>
0014 #include "sof_sdw_common.h"
0015 
0016 static const struct snd_soc_dapm_widget rt1316_widgets[] = {
0017     SND_SOC_DAPM_SPK("Speaker", NULL),
0018 };
0019 
0020 /*
0021  * dapm routes for rt1316 will be registered dynamically according
0022  * to the number of rt1316 used. The first two entries will be registered
0023  * for one codec case, and the last two entries are also registered
0024  * if two 1316s are used.
0025  */
0026 static const struct snd_soc_dapm_route rt1316_map[] = {
0027     { "Speaker", NULL, "rt1316-1 SPOL" },
0028     { "Speaker", NULL, "rt1316-1 SPOR" },
0029     { "Speaker", NULL, "rt1316-2 SPOL" },
0030     { "Speaker", NULL, "rt1316-2 SPOR" },
0031 };
0032 
0033 static const struct snd_kcontrol_new rt1316_controls[] = {
0034     SOC_DAPM_PIN_SWITCH("Speaker"),
0035 };
0036 
0037 static int first_spk_init(struct snd_soc_pcm_runtime *rtd)
0038 {
0039     struct snd_soc_card *card = rtd->card;
0040     int ret;
0041 
0042     card->components = devm_kasprintf(card->dev, GFP_KERNEL,
0043                       "%s spk:rt1316",
0044                       card->components);
0045     if (!card->components)
0046         return -ENOMEM;
0047 
0048     ret = snd_soc_add_card_controls(card, rt1316_controls,
0049                     ARRAY_SIZE(rt1316_controls));
0050     if (ret) {
0051         dev_err(card->dev, "rt1316 controls addition failed: %d\n", ret);
0052         return ret;
0053     }
0054 
0055     ret = snd_soc_dapm_new_controls(&card->dapm, rt1316_widgets,
0056                     ARRAY_SIZE(rt1316_widgets));
0057     if (ret) {
0058         dev_err(card->dev, "rt1316 widgets addition failed: %d\n", ret);
0059         return ret;
0060     }
0061 
0062     ret = snd_soc_dapm_add_routes(&card->dapm, rt1316_map, 2);
0063     if (ret)
0064         dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
0065 
0066     return ret;
0067 }
0068 
0069 static int second_spk_init(struct snd_soc_pcm_runtime *rtd)
0070 {
0071     struct snd_soc_card *card = rtd->card;
0072     int ret;
0073 
0074     ret = snd_soc_dapm_add_routes(&card->dapm, rt1316_map + 2, 2);
0075     if (ret)
0076         dev_err(rtd->dev, "failed to add second SPK map: %d\n", ret);
0077 
0078     return ret;
0079 }
0080 
0081 static int all_spk_init(struct snd_soc_pcm_runtime *rtd)
0082 {
0083     int ret;
0084 
0085     ret = first_spk_init(rtd);
0086     if (ret)
0087         return ret;
0088 
0089     return second_spk_init(rtd);
0090 }
0091 
0092 int sof_sdw_rt1316_init(struct snd_soc_card *card,
0093             const struct snd_soc_acpi_link_adr *link,
0094             struct snd_soc_dai_link *dai_links,
0095             struct sof_sdw_codec_info *info,
0096             bool playback)
0097 {
0098     /* Count amp number and do init on playback link only. */
0099     if (!playback)
0100         return 0;
0101 
0102     info->amp_num++;
0103     if (info->amp_num == 1)
0104         dai_links->init = first_spk_init;
0105 
0106     if (info->amp_num == 2) {
0107         /*
0108          * if two 1316s are in one dai link, the init function
0109          * in this dai link will be first set for the first speaker,
0110          * and it should be reset to initialize all speakers when
0111          * the second speaker is found.
0112          */
0113         if (dai_links->init)
0114             dai_links->init = all_spk_init;
0115         else
0116             dai_links->init = second_spk_init;
0117     }
0118 
0119     return 0;
0120 }