Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 //
0003 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
0004 //
0005 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
0006 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
0007 //
0008 
0009 #include <linux/module.h>
0010 #include <linux/platform_device.h>
0011 #include <sound/core.h>
0012 #include <sound/pcm.h>
0013 #include <sound/pcm_params.h>
0014 #include <sound/soc.h>
0015 #include <sound/soc-acpi.h>
0016 #include "../../../codecs/nau8825.h"
0017 
0018 #define SKL_NUVOTON_CODEC_DAI   "nau8825-hifi"
0019 #define SKL_SSM_CODEC_DAI   "ssm4567-hifi"
0020 
0021 static struct snd_soc_codec_conf card_codec_conf[] = {
0022     {
0023         .dlc = COMP_CODEC_CONF("i2c-INT343B:00"),
0024         .name_prefix = "Left",
0025     },
0026     {
0027         .dlc = COMP_CODEC_CONF("i2c-INT343B:01"),
0028         .name_prefix = "Right",
0029     },
0030 };
0031 
0032 static const struct snd_kcontrol_new card_controls[] = {
0033     SOC_DAPM_PIN_SWITCH("Left Speaker"),
0034     SOC_DAPM_PIN_SWITCH("Right Speaker"),
0035 };
0036 
0037 static int
0038 platform_clock_control(struct snd_soc_dapm_widget *w, struct snd_kcontrol *control, int event)
0039 {
0040     struct snd_soc_dapm_context *dapm = w->dapm;
0041     struct snd_soc_card *card = dapm->card;
0042     struct snd_soc_dai *codec_dai;
0043     int ret;
0044 
0045     codec_dai = snd_soc_card_get_codec_dai(card, SKL_NUVOTON_CODEC_DAI);
0046     if (!codec_dai) {
0047         dev_err(card->dev, "Codec dai not found\n");
0048         return -EINVAL;
0049     }
0050 
0051     if (SND_SOC_DAPM_EVENT_ON(event)) {
0052         ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_MCLK, 24000000,
0053                          SND_SOC_CLOCK_IN);
0054         if (ret < 0)
0055             dev_err(card->dev, "set sysclk err = %d\n", ret);
0056     } else {
0057         ret = snd_soc_dai_set_sysclk(codec_dai, NAU8825_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN);
0058         if (ret < 0)
0059             dev_err(card->dev, "set sysclk err = %d\n", ret);
0060     }
0061 
0062     return ret;
0063 }
0064 
0065 static const struct snd_soc_dapm_widget card_widgets[] = {
0066     SND_SOC_DAPM_SPK("Left Speaker", NULL),
0067     SND_SOC_DAPM_SPK("Right Speaker", NULL),
0068     SND_SOC_DAPM_SPK("DP1", NULL),
0069     SND_SOC_DAPM_SPK("DP2", NULL),
0070     SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, platform_clock_control,
0071                 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
0072 };
0073 
0074 static const struct snd_soc_dapm_route card_base_routes[] = {
0075     {"Left Speaker", NULL, "Left OUT"},
0076     {"Right Speaker", NULL, "Right OUT"},
0077 };
0078 
0079 static int avs_ssm4567_codec_init(struct snd_soc_pcm_runtime *runtime)
0080 {
0081     int ret;
0082 
0083     /* Slot 1 for left */
0084     ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_codec(runtime, 0), 0x01, 0x01, 2, 48);
0085     if (ret < 0)
0086         return ret;
0087 
0088     /* Slot 2 for right */
0089     ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_codec(runtime, 1), 0x02, 0x02, 2, 48);
0090     if (ret < 0)
0091         return ret;
0092 
0093     return 0;
0094 }
0095 
0096 static int
0097 avs_ssm4567_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params)
0098 {
0099     struct snd_interval *rate, *channels;
0100     struct snd_mask *fmt;
0101 
0102     rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
0103     channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
0104     fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0105 
0106     /* The ADSP will covert the FE rate to 48k, stereo */
0107     rate->min = rate->max = 48000;
0108     channels->min = channels->max = 2;
0109 
0110     /* set SSP0 to 24 bit */
0111     snd_mask_none(fmt);
0112     snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
0113     return 0;
0114 }
0115 
0116 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port,
0117                    struct snd_soc_dai_link **dai_link)
0118 {
0119     struct snd_soc_dai_link_component *platform;
0120     struct snd_soc_dai_link *dl;
0121 
0122     dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL);
0123     platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
0124     if (!dl || !platform)
0125         return -ENOMEM;
0126 
0127     platform->name = platform_name;
0128 
0129     dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port);
0130     dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
0131     dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs) * 2, GFP_KERNEL);
0132     if (!dl->name || !dl->cpus || !dl->codecs)
0133         return -ENOMEM;
0134 
0135     dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port);
0136     dl->codecs[0].name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343B:00");
0137     dl->codecs[0].dai_name = devm_kasprintf(dev, GFP_KERNEL, "ssm4567-hifi");
0138     dl->codecs[1].name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343B:01");
0139     dl->codecs[1].dai_name = devm_kasprintf(dev, GFP_KERNEL, "ssm4567-hifi");
0140     if (!dl->cpus->dai_name || !dl->codecs[0].name || !dl->codecs[0].dai_name ||
0141         !dl->codecs[1].name || !dl->codecs[1].dai_name)
0142         return -ENOMEM;
0143 
0144     dl->num_cpus = 1;
0145     dl->num_codecs = 2;
0146     dl->platforms = platform;
0147     dl->num_platforms = 1;
0148     dl->id = 0;
0149     dl->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS;
0150     dl->init = avs_ssm4567_codec_init;
0151     dl->be_hw_params_fixup = avs_ssm4567_be_fixup;
0152     dl->nonatomic = 1;
0153     dl->no_pcm = 1;
0154     dl->dpcm_capture = 1;
0155     dl->dpcm_playback = 1;
0156     dl->ignore_pmdown_time = 1;
0157 
0158     *dai_link = dl;
0159 
0160     return 0;
0161 }
0162 
0163 static int avs_create_dapm_routes(struct device *dev, int ssp_port,
0164                   struct snd_soc_dapm_route **routes, int *num_routes)
0165 {
0166     struct snd_soc_dapm_route *dr;
0167     const int num_base = ARRAY_SIZE(card_base_routes);
0168     const int num_dr = num_base + 4;
0169     int idx;
0170 
0171     dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL);
0172     if (!dr)
0173         return -ENOMEM;
0174 
0175     memcpy(dr, card_base_routes, num_base * sizeof(*dr));
0176 
0177     idx = num_base;
0178     dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Left Playback");
0179     dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
0180     if (!dr[idx].sink || !dr[idx].source)
0181         return -ENOMEM;
0182 
0183     idx++;
0184     dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Right Playback");
0185     dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port);
0186     if (!dr[idx].sink || !dr[idx].source)
0187         return -ENOMEM;
0188 
0189     idx++;
0190     dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
0191     dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Left Capture Sense");
0192     if (!dr[idx].sink || !dr[idx].source)
0193         return -ENOMEM;
0194 
0195     idx++;
0196     dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port);
0197     dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Right Capture Sense");
0198     if (!dr[idx].sink || !dr[idx].source)
0199         return -ENOMEM;
0200 
0201     *routes = dr;
0202     *num_routes = num_dr;
0203 
0204     return 0;
0205 }
0206 
0207 static int avs_ssm4567_probe(struct platform_device *pdev)
0208 {
0209     struct snd_soc_dapm_route *routes;
0210     struct snd_soc_dai_link *dai_link;
0211     struct snd_soc_acpi_mach *mach;
0212     struct snd_soc_card *card;
0213     struct device *dev = &pdev->dev;
0214     const char *pname;
0215     int num_routes, ssp_port, ret;
0216 
0217     mach = dev_get_platdata(dev);
0218     pname = mach->mach_params.platform;
0219     ssp_port = __ffs(mach->mach_params.i2s_link_mask);
0220 
0221     ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link);
0222     if (ret) {
0223         dev_err(dev, "Failed to create dai link: %d", ret);
0224         return ret;
0225     }
0226 
0227     ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes);
0228     if (ret) {
0229         dev_err(dev, "Failed to create dapm routes: %d", ret);
0230         return ret;
0231     }
0232 
0233     card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
0234     if (!card)
0235         return -ENOMEM;
0236 
0237     card->name = "avs_ssm4567-adi";
0238     card->dev = dev;
0239     card->owner = THIS_MODULE;
0240     card->dai_link = dai_link;
0241     card->num_links = 1;
0242     card->codec_conf = card_codec_conf;
0243     card->num_configs = ARRAY_SIZE(card_codec_conf);
0244     card->controls = card_controls;
0245     card->num_controls = ARRAY_SIZE(card_controls);
0246     card->dapm_widgets = card_widgets;
0247     card->num_dapm_widgets = ARRAY_SIZE(card_widgets);
0248     card->dapm_routes = routes;
0249     card->num_dapm_routes = num_routes;
0250     card->fully_routed = true;
0251     card->disable_route_checks = true;
0252 
0253     ret = snd_soc_fixup_dai_links_platform_name(card, pname);
0254     if (ret)
0255         return ret;
0256 
0257     return devm_snd_soc_register_card(dev, card);
0258 }
0259 
0260 static struct platform_driver avs_ssm4567_driver = {
0261     .probe = avs_ssm4567_probe,
0262     .driver = {
0263         .name = "avs_ssm4567",
0264         .pm = &snd_soc_pm_ops,
0265     },
0266 };
0267 
0268 module_platform_driver(avs_ssm4567_driver)
0269 
0270 MODULE_LICENSE("GPL");
0271 MODULE_ALIAS("platform:avs_ssm4567");