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