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_rt1308 - Helpers to handle RT1308 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 #include "../../codecs/rt1308.h"
0016 
0017 static const struct snd_soc_dapm_widget rt1308_widgets[] = {
0018     SND_SOC_DAPM_SPK("Speaker", NULL),
0019 };
0020 
0021 /*
0022  * dapm routes for rt1308 will be registered dynamically according
0023  * to the number of rt1308 used. The first two entries will be registered
0024  * for one codec case, and the last two entries are also registered
0025  * if two 1308s are used.
0026  */
0027 static const struct snd_soc_dapm_route rt1308_map[] = {
0028     { "Speaker", NULL, "rt1308-1 SPOL" },
0029     { "Speaker", NULL, "rt1308-1 SPOR" },
0030     { "Speaker", NULL, "rt1308-2 SPOL" },
0031     { "Speaker", NULL, "rt1308-2 SPOR" },
0032 };
0033 
0034 static const struct snd_kcontrol_new rt1308_controls[] = {
0035     SOC_DAPM_PIN_SWITCH("Speaker"),
0036 };
0037 
0038 static int first_spk_init(struct snd_soc_pcm_runtime *rtd)
0039 {
0040     struct snd_soc_card *card = rtd->card;
0041     int ret;
0042 
0043     card->components = devm_kasprintf(card->dev, GFP_KERNEL,
0044                       "%s spk:rt1308",
0045                       card->components);
0046     if (!card->components)
0047         return -ENOMEM;
0048 
0049     ret = snd_soc_add_card_controls(card, rt1308_controls,
0050                     ARRAY_SIZE(rt1308_controls));
0051     if (ret) {
0052         dev_err(card->dev, "rt1308 controls addition failed: %d\n", ret);
0053         return ret;
0054     }
0055 
0056     ret = snd_soc_dapm_new_controls(&card->dapm, rt1308_widgets,
0057                     ARRAY_SIZE(rt1308_widgets));
0058     if (ret) {
0059         dev_err(card->dev, "rt1308 widgets addition failed: %d\n", ret);
0060         return ret;
0061     }
0062 
0063     ret = snd_soc_dapm_add_routes(&card->dapm, rt1308_map, 2);
0064     if (ret)
0065         dev_err(rtd->dev, "failed to add first SPK map: %d\n", ret);
0066 
0067     return ret;
0068 }
0069 
0070 static int second_spk_init(struct snd_soc_pcm_runtime *rtd)
0071 {
0072     struct snd_soc_card *card = rtd->card;
0073     int ret;
0074 
0075     ret = snd_soc_dapm_add_routes(&card->dapm, rt1308_map + 2, 2);
0076     if (ret)
0077         dev_err(rtd->dev, "failed to add second SPK map: %d\n", ret);
0078 
0079     return ret;
0080 }
0081 
0082 static int all_spk_init(struct snd_soc_pcm_runtime *rtd)
0083 {
0084     int ret;
0085 
0086     ret = first_spk_init(rtd);
0087     if (ret)
0088         return ret;
0089 
0090     return second_spk_init(rtd);
0091 }
0092 
0093 static int rt1308_i2s_hw_params(struct snd_pcm_substream *substream,
0094                 struct snd_pcm_hw_params *params)
0095 {
0096     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0097     struct snd_soc_card *card = rtd->card;
0098     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0099     int clk_id, clk_freq, pll_out;
0100     int err;
0101 
0102     clk_id = RT1308_PLL_S_MCLK;
0103     clk_freq = 38400000;
0104 
0105     pll_out = params_rate(params) * 512;
0106 
0107     /* Set rt1308 pll */
0108     err = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
0109     if (err < 0) {
0110         dev_err(card->dev, "Failed to set RT1308 PLL: %d\n", err);
0111         return err;
0112     }
0113 
0114     /* Set rt1308 sysclk */
0115     err = snd_soc_dai_set_sysclk(codec_dai, RT1308_FS_SYS_S_PLL, pll_out,
0116                      SND_SOC_CLOCK_IN);
0117     if (err < 0) {
0118         dev_err(card->dev, "Failed to set RT1308 SYSCLK: %d\n", err);
0119         return err;
0120     }
0121 
0122     return 0;
0123 }
0124 
0125 /* machine stream operations */
0126 struct snd_soc_ops sof_sdw_rt1308_i2s_ops = {
0127     .hw_params = rt1308_i2s_hw_params,
0128 };
0129 
0130 int sof_sdw_rt1308_init(struct snd_soc_card *card,
0131             const struct snd_soc_acpi_link_adr *link,
0132             struct snd_soc_dai_link *dai_links,
0133             struct sof_sdw_codec_info *info,
0134             bool playback)
0135 {
0136     /* Count amp number and do init on playback link only. */
0137     if (!playback)
0138         return 0;
0139 
0140     info->amp_num++;
0141     if (info->amp_num == 1)
0142         dai_links->init = first_spk_init;
0143 
0144     if (info->amp_num == 2) {
0145         /*
0146          * if two 1308s are in one dai link, the init function
0147          * in this dai link will be first set for the first speaker,
0148          * and it should be reset to initialize all speakers when
0149          * the second speaker is found.
0150          */
0151         if (dai_links->init)
0152             dai_links->init = all_spk_init;
0153         else
0154             dai_links->init = second_spk_init;
0155     }
0156 
0157     return 0;
0158 }