1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Copyright (c) 2020 BayLibre, SAS. 4 // Author: Jerome Brunet <jbrunet@baylibre.com> 5 6 #include <linux/bitfield.h> 7 #include <sound/pcm_params.h> 8 #include <sound/soc.h> 9 #include <sound/soc-dai.h> 10 11 #include <dt-bindings/sound/meson-aiu.h> 12 #include "aiu.h" 13 #include "meson-codec-glue.h" 14 15 #define CTRL_DIN_EN 15 16 #define CTRL_CLK_INV BIT(14) 17 #define CTRL_LRCLK_INV BIT(13) 18 #define CTRL_I2S_IN_BCLK_SRC BIT(11) 19 #define CTRL_DIN_LRCLK_SRC_SHIFT 6 20 #define CTRL_DIN_LRCLK_SRC (0x3 << CTRL_DIN_LRCLK_SRC_SHIFT) 21 #define CTRL_BCLK_MCLK_SRC GENMASK(5, 4) 22 #define CTRL_DIN_SKEW GENMASK(3, 2) 23 #define CTRL_I2S_OUT_LANE_SRC 0 24 25 #define AIU_ACODEC_OUT_CHMAX 2 26 27 static const char * const aiu_acodec_ctrl_mux_texts[] = { 28 "DISABLED", "I2S", "PCM", 29 }; 30 31 static int aiu_acodec_ctrl_mux_put_enum(struct snd_kcontrol *kcontrol, 32 struct snd_ctl_elem_value *ucontrol) 33 { 34 struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol); 35 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol); 36 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value; 37 unsigned int mux, changed; 38 39 mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]); 40 changed = snd_soc_component_test_bits(component, e->reg, 41 CTRL_DIN_LRCLK_SRC, 42 FIELD_PREP(CTRL_DIN_LRCLK_SRC, 43 mux)); 44 45 if (!changed) 46 return 0; 47 48 /* Force disconnect of the mux while updating */ 49 snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL); 50 51 snd_soc_component_update_bits(component, e->reg, 52 CTRL_DIN_LRCLK_SRC | 53 CTRL_BCLK_MCLK_SRC, 54 FIELD_PREP(CTRL_DIN_LRCLK_SRC, mux) | 55 FIELD_PREP(CTRL_BCLK_MCLK_SRC, mux)); 56 57 snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL); 58 59 return 1; 60 } 61 62 static SOC_ENUM_SINGLE_DECL(aiu_acodec_ctrl_mux_enum, AIU_ACODEC_CTRL, 63 CTRL_DIN_LRCLK_SRC_SHIFT, 64 aiu_acodec_ctrl_mux_texts); 65 66 static const struct snd_kcontrol_new aiu_acodec_ctrl_mux = 67 SOC_DAPM_ENUM_EXT("ACodec Source", aiu_acodec_ctrl_mux_enum, 68 snd_soc_dapm_get_enum_double, 69 aiu_acodec_ctrl_mux_put_enum); 70 71 static const struct snd_kcontrol_new aiu_acodec_ctrl_out_enable = 72 SOC_DAPM_SINGLE_AUTODISABLE("Switch", AIU_ACODEC_CTRL, 73 CTRL_DIN_EN, 1, 0); 74 75 static const struct snd_soc_dapm_widget aiu_acodec_ctrl_widgets[] = { 76 SND_SOC_DAPM_MUX("ACODEC SRC", SND_SOC_NOPM, 0, 0, 77 &aiu_acodec_ctrl_mux), 78 SND_SOC_DAPM_SWITCH("ACODEC OUT EN", SND_SOC_NOPM, 0, 0, 79 &aiu_acodec_ctrl_out_enable), 80 }; 81 82 static int aiu_acodec_ctrl_input_hw_params(struct snd_pcm_substream *substream, 83 struct snd_pcm_hw_params *params, 84 struct snd_soc_dai *dai) 85 { 86 struct meson_codec_glue_input *data; 87 int ret; 88 89 ret = meson_codec_glue_input_hw_params(substream, params, dai); 90 if (ret) 91 return ret; 92 93 /* The glue will provide 1 lane out of the 4 to the output */ 94 data = meson_codec_glue_input_get_data(dai); 95 data->params.channels_min = min_t(unsigned int, AIU_ACODEC_OUT_CHMAX, 96 data->params.channels_min); 97 data->params.channels_max = min_t(unsigned int, AIU_ACODEC_OUT_CHMAX, 98 data->params.channels_max); 99 100 return 0; 101 } 102 103 static const struct snd_soc_dai_ops aiu_acodec_ctrl_input_ops = { 104 .probe = meson_codec_glue_input_dai_probe, 105 .remove = meson_codec_glue_input_dai_remove, 106 .hw_params = aiu_acodec_ctrl_input_hw_params, 107 .set_fmt = meson_codec_glue_input_set_fmt, 108 }; 109 110 static const struct snd_soc_dai_ops aiu_acodec_ctrl_output_ops = { 111 .startup = meson_codec_glue_output_startup, 112 }; 113 114 #define AIU_ACODEC_CTRL_FORMATS \ 115 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 116 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE | \ 117 SNDRV_PCM_FMTBIT_S32_LE) 118 119 #define AIU_ACODEC_STREAM(xname, xsuffix, xchmax) \ 120 { \ 121 .stream_name = xname " " xsuffix, \ 122 .channels_min = 1, \ 123 .channels_max = (xchmax), \ 124 .rate_min = 5512, \ 125 .rate_max = 192000, \ 126 .formats = AIU_ACODEC_CTRL_FORMATS, \ 127 } 128 129 #define AIU_ACODEC_INPUT(xname) { \ 130 .name = "ACODEC CTRL " xname, \ 131 .playback = AIU_ACODEC_STREAM(xname, "Playback", 8), \ 132 .ops = &aiu_acodec_ctrl_input_ops, \ 133 } 134 135 #define AIU_ACODEC_OUTPUT(xname) { \ 136 .name = "ACODEC CTRL " xname, \ 137 .capture = AIU_ACODEC_STREAM(xname, "Capture", AIU_ACODEC_OUT_CHMAX), \ 138 .ops = &aiu_acodec_ctrl_output_ops, \ 139 } 140 141 static struct snd_soc_dai_driver aiu_acodec_ctrl_dai_drv[] = { 142 [CTRL_I2S] = AIU_ACODEC_INPUT("ACODEC I2S IN"), 143 [CTRL_PCM] = AIU_ACODEC_INPUT("ACODEC PCM IN"), 144 [CTRL_OUT] = AIU_ACODEC_OUTPUT("ACODEC OUT"), 145 }; 146 147 static const struct snd_soc_dapm_route aiu_acodec_ctrl_routes[] = { 148 { "ACODEC SRC", "I2S", "ACODEC I2S IN Playback" }, 149 { "ACODEC SRC", "PCM", "ACODEC PCM IN Playback" }, 150 { "ACODEC OUT EN", "Switch", "ACODEC SRC" }, 151 { "ACODEC OUT Capture", NULL, "ACODEC OUT EN" }, 152 }; 153 154 static const struct snd_kcontrol_new aiu_acodec_ctrl_controls[] = { 155 SOC_SINGLE("ACODEC I2S Lane Select", AIU_ACODEC_CTRL, 156 CTRL_I2S_OUT_LANE_SRC, 3, 0), 157 }; 158 159 static int aiu_acodec_of_xlate_dai_name(struct snd_soc_component *component, 160 const struct of_phandle_args *args, 161 const char **dai_name) 162 { 163 return aiu_of_xlate_dai_name(component, args, dai_name, AIU_ACODEC); 164 } 165 166 static int aiu_acodec_ctrl_component_probe(struct snd_soc_component *component) 167 { 168 /* 169 * NOTE: Din Skew setting 170 * According to the documentation, the following update adds one delay 171 * to the din line. Without this, the output saturates. This happens 172 * regardless of the link format (i2s or left_j) so it is not clear what 173 * it actually does but it seems to be required 174 */ 175 snd_soc_component_update_bits(component, AIU_ACODEC_CTRL, 176 CTRL_DIN_SKEW, 177 FIELD_PREP(CTRL_DIN_SKEW, 2)); 178 179 return 0; 180 } 181 182 static const struct snd_soc_component_driver aiu_acodec_ctrl_component = { 183 .name = "AIU Internal DAC Codec Control", 184 .probe = aiu_acodec_ctrl_component_probe, 185 .controls = aiu_acodec_ctrl_controls, 186 .num_controls = ARRAY_SIZE(aiu_acodec_ctrl_controls), 187 .dapm_widgets = aiu_acodec_ctrl_widgets, 188 .num_dapm_widgets = ARRAY_SIZE(aiu_acodec_ctrl_widgets), 189 .dapm_routes = aiu_acodec_ctrl_routes, 190 .num_dapm_routes = ARRAY_SIZE(aiu_acodec_ctrl_routes), 191 .of_xlate_dai_name = aiu_acodec_of_xlate_dai_name, 192 .endianness = 1, 193 #ifdef CONFIG_DEBUG_FS 194 .debugfs_prefix = "acodec", 195 #endif 196 }; 197 198 int aiu_acodec_ctrl_register_component(struct device *dev) 199 { 200 return snd_soc_register_component(dev, &aiu_acodec_ctrl_component, 201 aiu_acodec_ctrl_dai_drv, 202 ARRAY_SIZE(aiu_acodec_ctrl_dai_drv)); 203 } 204