Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Copyright (c) 2020 BayLibre, SAS.
0004 // Author: Jerome Brunet <jbrunet@baylibre.com>
0005 
0006 #include <linux/bitfield.h>
0007 #include <sound/pcm_params.h>
0008 #include <sound/soc.h>
0009 #include <sound/soc-dai.h>
0010 
0011 #include <dt-bindings/sound/meson-aiu.h>
0012 #include "aiu.h"
0013 #include "meson-codec-glue.h"
0014 
0015 #define CTRL_DIN_EN         15
0016 #define CTRL_CLK_INV            BIT(14)
0017 #define CTRL_LRCLK_INV          BIT(13)
0018 #define CTRL_I2S_IN_BCLK_SRC        BIT(11)
0019 #define CTRL_DIN_LRCLK_SRC_SHIFT    6
0020 #define CTRL_DIN_LRCLK_SRC      (0x3 << CTRL_DIN_LRCLK_SRC_SHIFT)
0021 #define CTRL_BCLK_MCLK_SRC      GENMASK(5, 4)
0022 #define CTRL_DIN_SKEW           GENMASK(3, 2)
0023 #define CTRL_I2S_OUT_LANE_SRC       0
0024 
0025 #define AIU_ACODEC_OUT_CHMAX        2
0026 
0027 static const char * const aiu_acodec_ctrl_mux_texts[] = {
0028     "DISABLED", "I2S", "PCM",
0029 };
0030 
0031 static int aiu_acodec_ctrl_mux_put_enum(struct snd_kcontrol *kcontrol,
0032                     struct snd_ctl_elem_value *ucontrol)
0033 {
0034     struct snd_soc_component *component =
0035         snd_soc_dapm_kcontrol_component(kcontrol);
0036     struct snd_soc_dapm_context *dapm =
0037         snd_soc_dapm_kcontrol_dapm(kcontrol);
0038     struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0039     unsigned int mux, changed;
0040 
0041     mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
0042     changed = snd_soc_component_test_bits(component, e->reg,
0043                           CTRL_DIN_LRCLK_SRC,
0044                           FIELD_PREP(CTRL_DIN_LRCLK_SRC,
0045                              mux));
0046 
0047     if (!changed)
0048         return 0;
0049 
0050     /* Force disconnect of the mux while updating */
0051     snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
0052 
0053     snd_soc_component_update_bits(component, e->reg,
0054                       CTRL_DIN_LRCLK_SRC |
0055                       CTRL_BCLK_MCLK_SRC,
0056                       FIELD_PREP(CTRL_DIN_LRCLK_SRC, mux) |
0057                       FIELD_PREP(CTRL_BCLK_MCLK_SRC, mux));
0058 
0059     snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
0060 
0061     return 1;
0062 }
0063 
0064 static SOC_ENUM_SINGLE_DECL(aiu_acodec_ctrl_mux_enum, AIU_ACODEC_CTRL,
0065                 CTRL_DIN_LRCLK_SRC_SHIFT,
0066                 aiu_acodec_ctrl_mux_texts);
0067 
0068 static const struct snd_kcontrol_new aiu_acodec_ctrl_mux =
0069     SOC_DAPM_ENUM_EXT("ACodec Source", aiu_acodec_ctrl_mux_enum,
0070               snd_soc_dapm_get_enum_double,
0071               aiu_acodec_ctrl_mux_put_enum);
0072 
0073 static const struct snd_kcontrol_new aiu_acodec_ctrl_out_enable =
0074     SOC_DAPM_SINGLE_AUTODISABLE("Switch", AIU_ACODEC_CTRL,
0075                     CTRL_DIN_EN, 1, 0);
0076 
0077 static const struct snd_soc_dapm_widget aiu_acodec_ctrl_widgets[] = {
0078     SND_SOC_DAPM_MUX("ACODEC SRC", SND_SOC_NOPM, 0, 0,
0079              &aiu_acodec_ctrl_mux),
0080     SND_SOC_DAPM_SWITCH("ACODEC OUT EN", SND_SOC_NOPM, 0, 0,
0081                 &aiu_acodec_ctrl_out_enable),
0082 };
0083 
0084 static int aiu_acodec_ctrl_input_hw_params(struct snd_pcm_substream *substream,
0085                        struct snd_pcm_hw_params *params,
0086                        struct snd_soc_dai *dai)
0087 {
0088     struct meson_codec_glue_input *data;
0089     int ret;
0090 
0091     ret = meson_codec_glue_input_hw_params(substream, params, dai);
0092     if (ret)
0093         return ret;
0094 
0095     /* The glue will provide 1 lane out of the 4 to the output */
0096     data = meson_codec_glue_input_get_data(dai);
0097     data->params.channels_min = min_t(unsigned int, AIU_ACODEC_OUT_CHMAX,
0098                       data->params.channels_min);
0099     data->params.channels_max = min_t(unsigned int, AIU_ACODEC_OUT_CHMAX,
0100                       data->params.channels_max);
0101 
0102     return 0;
0103 }
0104 
0105 static const struct snd_soc_dai_ops aiu_acodec_ctrl_input_ops = {
0106     .hw_params  = aiu_acodec_ctrl_input_hw_params,
0107     .set_fmt    = meson_codec_glue_input_set_fmt,
0108 };
0109 
0110 static const struct snd_soc_dai_ops aiu_acodec_ctrl_output_ops = {
0111     .startup    = meson_codec_glue_output_startup,
0112 };
0113 
0114 #define AIU_ACODEC_CTRL_FORMATS                 \
0115     (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |   \
0116      SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE |   \
0117      SNDRV_PCM_FMTBIT_S32_LE)
0118 
0119 #define AIU_ACODEC_STREAM(xname, xsuffix, xchmax)       \
0120 {                               \
0121     .stream_name    = xname " " xsuffix,            \
0122     .channels_min   = 1,                    \
0123     .channels_max   = (xchmax),             \
0124     .rate_min       = 5512,                 \
0125     .rate_max   = 192000,               \
0126     .formats    = AIU_ACODEC_CTRL_FORMATS,      \
0127 }
0128 
0129 #define AIU_ACODEC_INPUT(xname) {               \
0130     .name = "ACODEC CTRL " xname,               \
0131     .playback = AIU_ACODEC_STREAM(xname, "Playback", 8),    \
0132     .ops = &aiu_acodec_ctrl_input_ops,          \
0133     .probe = meson_codec_glue_input_dai_probe,      \
0134     .remove = meson_codec_glue_input_dai_remove,        \
0135 }
0136 
0137 #define AIU_ACODEC_OUTPUT(xname) {              \
0138     .name = "ACODEC CTRL " xname,               \
0139     .capture = AIU_ACODEC_STREAM(xname, "Capture", AIU_ACODEC_OUT_CHMAX), \
0140     .ops = &aiu_acodec_ctrl_output_ops,         \
0141 }
0142 
0143 static struct snd_soc_dai_driver aiu_acodec_ctrl_dai_drv[] = {
0144     [CTRL_I2S] = AIU_ACODEC_INPUT("ACODEC I2S IN"),
0145     [CTRL_PCM] = AIU_ACODEC_INPUT("ACODEC PCM IN"),
0146     [CTRL_OUT] = AIU_ACODEC_OUTPUT("ACODEC OUT"),
0147 };
0148 
0149 static const struct snd_soc_dapm_route aiu_acodec_ctrl_routes[] = {
0150     { "ACODEC SRC", "I2S", "ACODEC I2S IN Playback" },
0151     { "ACODEC SRC", "PCM", "ACODEC PCM IN Playback" },
0152     { "ACODEC OUT EN", "Switch", "ACODEC SRC" },
0153     { "ACODEC OUT Capture", NULL, "ACODEC OUT EN" },
0154 };
0155 
0156 static const struct snd_kcontrol_new aiu_acodec_ctrl_controls[] = {
0157     SOC_SINGLE("ACODEC I2S Lane Select", AIU_ACODEC_CTRL,
0158            CTRL_I2S_OUT_LANE_SRC, 3, 0),
0159 };
0160 
0161 static int aiu_acodec_of_xlate_dai_name(struct snd_soc_component *component,
0162                     const struct of_phandle_args *args,
0163                     const char **dai_name)
0164 {
0165     return aiu_of_xlate_dai_name(component, args, dai_name, AIU_ACODEC);
0166 }
0167 
0168 static int aiu_acodec_ctrl_component_probe(struct snd_soc_component *component)
0169 {
0170     /*
0171      * NOTE: Din Skew setting
0172      * According to the documentation, the following update adds one delay
0173      * to the din line. Without this, the output saturates. This happens
0174      * regardless of the link format (i2s or left_j) so it is not clear what
0175      * it actually does but it seems to be required
0176      */
0177     snd_soc_component_update_bits(component, AIU_ACODEC_CTRL,
0178                       CTRL_DIN_SKEW,
0179                       FIELD_PREP(CTRL_DIN_SKEW, 2));
0180 
0181     return 0;
0182 }
0183 
0184 static const struct snd_soc_component_driver aiu_acodec_ctrl_component = {
0185     .name           = "AIU Internal DAC Codec Control",
0186     .probe          = aiu_acodec_ctrl_component_probe,
0187     .controls       = aiu_acodec_ctrl_controls,
0188     .num_controls       = ARRAY_SIZE(aiu_acodec_ctrl_controls),
0189     .dapm_widgets       = aiu_acodec_ctrl_widgets,
0190     .num_dapm_widgets   = ARRAY_SIZE(aiu_acodec_ctrl_widgets),
0191     .dapm_routes        = aiu_acodec_ctrl_routes,
0192     .num_dapm_routes    = ARRAY_SIZE(aiu_acodec_ctrl_routes),
0193     .of_xlate_dai_name  = aiu_acodec_of_xlate_dai_name,
0194     .endianness     = 1,
0195 #ifdef CONFIG_DEBUG_FS
0196     .debugfs_prefix     = "acodec",
0197 #endif
0198 };
0199 
0200 int aiu_acodec_ctrl_register_component(struct device *dev)
0201 {
0202     return snd_soc_register_component(dev, &aiu_acodec_ctrl_component,
0203                       aiu_acodec_ctrl_dai_drv,
0204                       ARRAY_SIZE(aiu_acodec_ctrl_dai_drv));
0205 }