xref: /linux/sound/soc/meson/g12a-toacodec.c (revision 2aa680df68062e4e0c356ec2aa7100c13654907b)
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 <linux/clk.h>
8 #include <linux/module.h>
9 #include <sound/pcm_params.h>
10 #include <linux/regmap.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/reset.h>
13 #include <sound/soc.h>
14 #include <sound/soc-dai.h>
15 
16 #include <dt-bindings/sound/meson-g12a-toacodec.h>
17 #include "axg-tdm.h"
18 #include "meson-codec-glue.h"
19 
20 #define G12A_TOACODEC_DRV_NAME "g12a-toacodec"
21 
22 #define TOACODEC_CTRL0			0x0
23 #define  CTRL0_ENABLE_SHIFT		31
24 #define  CTRL0_DAT_SEL_SM1_MSB		19
25 #define  CTRL0_DAT_SEL_SM1_LSB		18
26 #define  CTRL0_DAT_SEL_MSB		15
27 #define  CTRL0_DAT_SEL_LSB		14
28 #define  CTRL0_LANE_SEL_SM1		16
29 #define  CTRL0_LANE_SEL			12
30 #define  CTRL0_LRCLK_SEL_SM1_MSB	14
31 #define  CTRL0_LRCLK_SEL_SM1_LSB	12
32 #define  CTRL0_LRCLK_SEL_MSB		9
33 #define  CTRL0_LRCLK_SEL_LSB		8
34 #define  CTRL0_LRCLK_INV_SM1		BIT(10)
35 #define  CTRL0_BLK_CAP_INV_SM1		BIT(9)
36 #define  CTRL0_BLK_CAP_INV		BIT(7)
37 #define  CTRL0_BCLK_O_INV_SM1		BIT(8)
38 #define  CTRL0_BCLK_O_INV		BIT(6)
39 #define  CTRL0_BCLK_SEL_SM1_MSB		6
40 #define  CTRL0_BCLK_SEL_MSB		5
41 #define  CTRL0_BCLK_SEL_LSB		4
42 #define  CTRL0_MCLK_SEL			GENMASK(2, 0)
43 
44 #define TOACODEC_OUT_CHMAX		2
45 
46 struct g12a_toacodec {
47 	struct regmap_field *field_dat_sel;
48 	struct regmap_field *field_lrclk_sel;
49 	struct regmap_field *field_bclk_sel;
50 };
51 
52 struct g12a_toacodec_match_data {
53 	const struct snd_soc_component_driver *component_drv;
54 	struct reg_field field_dat_sel;
55 	struct reg_field field_lrclk_sel;
56 	struct reg_field field_bclk_sel;
57 };
58 
59 static const char * const g12a_toacodec_mux_texts[] = {
60 	"I2S A", "I2S B", "I2S C",
61 };
62 
63 static int g12a_toacodec_mux_put_enum(struct snd_kcontrol *kcontrol,
64 				      struct snd_ctl_elem_value *ucontrol)
65 {
66 	struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol);
67 	struct g12a_toacodec *priv = snd_soc_component_get_drvdata(component);
68 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
69 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
70 	unsigned int mux, reg;
71 
72 	if (ucontrol->value.enumerated.item[0] >= e->items)
73 		return -EINVAL;
74 
75 	mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
76 	regmap_field_read(priv->field_dat_sel, &reg);
77 
78 	if (mux == reg)
79 		return 0;
80 
81 	/* Force disconnect of the mux while updating */
82 	snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
83 
84 	regmap_field_write(priv->field_dat_sel, mux);
85 	regmap_field_write(priv->field_lrclk_sel, mux);
86 	regmap_field_write(priv->field_bclk_sel, mux);
87 
88 	/*
89 	 * FIXME:
90 	 * On this soc, the glue gets the MCLK directly from the clock
91 	 * controller instead of going the through the TDM interface.
92 	 *
93 	 * Here we assume interface A uses clock A, etc ... While it is
94 	 * true for now, it could be different. Instead the glue should
95 	 * find out the clock used by the interface and select the same
96 	 * source. For that, we will need regmap backed clock mux which
97 	 * is a work in progress
98 	 */
99 	snd_soc_component_update_bits(component, e->reg,
100 				      CTRL0_MCLK_SEL,
101 				      FIELD_PREP(CTRL0_MCLK_SEL, mux));
102 
103 	snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
104 
105 	return 1;
106 }
107 
108 static SOC_ENUM_SINGLE_DECL(g12a_toacodec_mux_enum, TOACODEC_CTRL0,
109 			    CTRL0_DAT_SEL_LSB,
110 			    g12a_toacodec_mux_texts);
111 
112 static SOC_ENUM_SINGLE_DECL(sm1_toacodec_mux_enum, TOACODEC_CTRL0,
113 			    CTRL0_DAT_SEL_SM1_LSB,
114 			    g12a_toacodec_mux_texts);
115 
116 static const struct snd_kcontrol_new g12a_toacodec_mux =
117 	SOC_DAPM_ENUM_EXT("Source", g12a_toacodec_mux_enum,
118 			  snd_soc_dapm_get_enum_double,
119 			  g12a_toacodec_mux_put_enum);
120 
121 static const struct snd_kcontrol_new sm1_toacodec_mux =
122 	SOC_DAPM_ENUM_EXT("Source", sm1_toacodec_mux_enum,
123 			  snd_soc_dapm_get_enum_double,
124 			  g12a_toacodec_mux_put_enum);
125 
126 static const struct snd_kcontrol_new g12a_toacodec_out_enable =
127 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", TOACODEC_CTRL0,
128 				    CTRL0_ENABLE_SHIFT, 1, 0);
129 
130 static const struct snd_soc_dapm_widget g12a_toacodec_widgets[] = {
131 	SND_SOC_DAPM_MUX("SRC", SND_SOC_NOPM, 0, 0,
132 			 &g12a_toacodec_mux),
133 	SND_SOC_DAPM_SWITCH("OUT EN", SND_SOC_NOPM, 0, 0,
134 			    &g12a_toacodec_out_enable),
135 };
136 
137 static const struct snd_soc_dapm_widget sm1_toacodec_widgets[] = {
138 	SND_SOC_DAPM_MUX("SRC", SND_SOC_NOPM, 0, 0,
139 			 &sm1_toacodec_mux),
140 	SND_SOC_DAPM_SWITCH("OUT EN", SND_SOC_NOPM, 0, 0,
141 			    &g12a_toacodec_out_enable),
142 };
143 
144 static int g12a_toacodec_input_hw_params(struct snd_pcm_substream *substream,
145 					 struct snd_pcm_hw_params *params,
146 					 struct snd_soc_dai *dai)
147 {
148 	struct meson_codec_glue_input *data;
149 	int ret;
150 
151 	ret = meson_codec_glue_input_hw_params(substream, params, dai);
152 	if (ret)
153 		return ret;
154 
155 	/* The glue will provide 1 lane out of the 4 to the output */
156 	data = meson_codec_glue_input_get_data(dai);
157 	data->params.channels_min = min_t(unsigned int, TOACODEC_OUT_CHMAX,
158 					data->params.channels_min);
159 	data->params.channels_max = min_t(unsigned int, TOACODEC_OUT_CHMAX,
160 					data->params.channels_max);
161 
162 	return 0;
163 }
164 
165 static const struct snd_soc_dai_ops g12a_toacodec_input_ops = {
166 	.probe		= meson_codec_glue_input_dai_probe,
167 	.remove		= meson_codec_glue_input_dai_remove,
168 	.hw_params	= g12a_toacodec_input_hw_params,
169 	.set_fmt	= meson_codec_glue_input_set_fmt,
170 };
171 
172 static const struct snd_soc_dai_ops g12a_toacodec_output_ops = {
173 	.startup	= meson_codec_glue_output_startup,
174 };
175 
176 #define TOACODEC_STREAM(xname, xsuffix, xchmax)			\
177 {								\
178 	.stream_name	= xname " " xsuffix,			\
179 	.channels_min	= 1,					\
180 	.channels_max	= (xchmax),				\
181 	.rate_min       = 5512,					\
182 	.rate_max	= 192000,				\
183 	.formats	= AXG_TDM_FORMATS,			\
184 }
185 
186 #define TOACODEC_INPUT(xname, xid) {					\
187 	.name = xname,							\
188 	.id = (xid),							\
189 	.playback = TOACODEC_STREAM(xname, "Playback", 8),		\
190 	.ops = &g12a_toacodec_input_ops,				\
191 }
192 
193 #define TOACODEC_OUTPUT(xname, xid) {					\
194 	.name = xname,							\
195 	.id = (xid),							\
196 	.capture = TOACODEC_STREAM(xname, "Capture", TOACODEC_OUT_CHMAX), \
197 	.ops = &g12a_toacodec_output_ops,				\
198 }
199 
200 static struct snd_soc_dai_driver g12a_toacodec_dai_drv[] = {
201 	TOACODEC_INPUT("IN A", TOACODEC_IN_A),
202 	TOACODEC_INPUT("IN B", TOACODEC_IN_B),
203 	TOACODEC_INPUT("IN C", TOACODEC_IN_C),
204 	TOACODEC_OUTPUT("OUT", TOACODEC_OUT),
205 };
206 
207 static int g12a_toacodec_component_probe(struct snd_soc_component *c)
208 {
209 	/* Initialize the static clock parameters */
210 	return snd_soc_component_write(c, TOACODEC_CTRL0,
211 				       CTRL0_BLK_CAP_INV);
212 }
213 
214 static int sm1_toacodec_component_probe(struct snd_soc_component *c)
215 {
216 	/* Initialize the static clock parameters */
217 	return snd_soc_component_write(c, TOACODEC_CTRL0,
218 				       CTRL0_BLK_CAP_INV_SM1);
219 }
220 
221 static const struct snd_soc_dapm_route g12a_toacodec_routes[] = {
222 	{ "SRC", "I2S A", "IN A Playback" },
223 	{ "SRC", "I2S B", "IN B Playback" },
224 	{ "SRC", "I2S C", "IN C Playback" },
225 	{ "OUT EN", "Switch", "SRC" },
226 	{ "OUT Capture", NULL, "OUT EN" },
227 };
228 
229 static const struct snd_kcontrol_new g12a_toacodec_controls[] = {
230 	SOC_SINGLE("Lane Select", TOACODEC_CTRL0, CTRL0_LANE_SEL, 3, 0),
231 };
232 
233 static const struct snd_kcontrol_new sm1_toacodec_controls[] = {
234 	SOC_SINGLE("Lane Select", TOACODEC_CTRL0, CTRL0_LANE_SEL_SM1, 3, 0),
235 };
236 
237 static const struct snd_soc_component_driver g12a_toacodec_component_drv = {
238 	.probe			= g12a_toacodec_component_probe,
239 	.controls		= g12a_toacodec_controls,
240 	.num_controls		= ARRAY_SIZE(g12a_toacodec_controls),
241 	.dapm_widgets		= g12a_toacodec_widgets,
242 	.num_dapm_widgets	= ARRAY_SIZE(g12a_toacodec_widgets),
243 	.dapm_routes		= g12a_toacodec_routes,
244 	.num_dapm_routes	= ARRAY_SIZE(g12a_toacodec_routes),
245 	.endianness		= 1,
246 };
247 
248 static const struct snd_soc_component_driver sm1_toacodec_component_drv = {
249 	.probe			= sm1_toacodec_component_probe,
250 	.controls		= sm1_toacodec_controls,
251 	.num_controls		= ARRAY_SIZE(sm1_toacodec_controls),
252 	.dapm_widgets		= sm1_toacodec_widgets,
253 	.num_dapm_widgets	= ARRAY_SIZE(sm1_toacodec_widgets),
254 	.dapm_routes		= g12a_toacodec_routes,
255 	.num_dapm_routes	= ARRAY_SIZE(g12a_toacodec_routes),
256 	.endianness		= 1,
257 };
258 
259 static const struct regmap_config g12a_toacodec_regmap_cfg = {
260 	.reg_bits	= 32,
261 	.val_bits	= 32,
262 	.reg_stride	= 4,
263 };
264 
265 static const struct g12a_toacodec_match_data g12a_toacodec_match_data = {
266 	.component_drv	= &g12a_toacodec_component_drv,
267 	.field_dat_sel	= REG_FIELD(TOACODEC_CTRL0, 14, 15),
268 	.field_lrclk_sel = REG_FIELD(TOACODEC_CTRL0, 8, 9),
269 	.field_bclk_sel	= REG_FIELD(TOACODEC_CTRL0, 4, 5),
270 };
271 
272 static const struct g12a_toacodec_match_data sm1_toacodec_match_data = {
273 	.component_drv	= &sm1_toacodec_component_drv,
274 	.field_dat_sel	= REG_FIELD(TOACODEC_CTRL0, 18, 19),
275 	.field_lrclk_sel = REG_FIELD(TOACODEC_CTRL0, 12, 14),
276 	.field_bclk_sel	= REG_FIELD(TOACODEC_CTRL0, 4, 6),
277 };
278 
279 static const struct of_device_id g12a_toacodec_of_match[] = {
280 	{
281 		.compatible = "amlogic,g12a-toacodec",
282 		.data = &g12a_toacodec_match_data,
283 	},
284 	{
285 		.compatible = "amlogic,sm1-toacodec",
286 		.data = &sm1_toacodec_match_data,
287 	},
288 	{}
289 };
290 MODULE_DEVICE_TABLE(of, g12a_toacodec_of_match);
291 
292 static int g12a_toacodec_probe(struct platform_device *pdev)
293 {
294 	const struct g12a_toacodec_match_data *data;
295 	struct device *dev = &pdev->dev;
296 	struct g12a_toacodec *priv;
297 	void __iomem *regs;
298 	struct regmap *map;
299 	int ret;
300 
301 	data = device_get_match_data(dev);
302 	if (!data) {
303 		dev_err(dev, "failed to match device\n");
304 		return -ENODEV;
305 	}
306 
307 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
308 	if (!priv)
309 		return -ENOMEM;
310 
311 	platform_set_drvdata(pdev, priv);
312 
313 	ret = device_reset(dev);
314 	if (ret)
315 		return ret;
316 
317 	regs = devm_platform_ioremap_resource(pdev, 0);
318 	if (IS_ERR(regs))
319 		return PTR_ERR(regs);
320 
321 	map = devm_regmap_init_mmio(dev, regs, &g12a_toacodec_regmap_cfg);
322 	if (IS_ERR(map)) {
323 		dev_err(dev, "failed to init regmap: %ld\n",
324 			PTR_ERR(map));
325 		return PTR_ERR(map);
326 	}
327 
328 	priv->field_dat_sel = devm_regmap_field_alloc(dev, map, data->field_dat_sel);
329 	if (IS_ERR(priv->field_dat_sel))
330 		return PTR_ERR(priv->field_dat_sel);
331 
332 	priv->field_lrclk_sel = devm_regmap_field_alloc(dev, map, data->field_lrclk_sel);
333 	if (IS_ERR(priv->field_lrclk_sel))
334 		return PTR_ERR(priv->field_lrclk_sel);
335 
336 	priv->field_bclk_sel = devm_regmap_field_alloc(dev, map, data->field_bclk_sel);
337 	if (IS_ERR(priv->field_bclk_sel))
338 		return PTR_ERR(priv->field_bclk_sel);
339 
340 	return devm_snd_soc_register_component(dev,
341 			data->component_drv, g12a_toacodec_dai_drv,
342 			ARRAY_SIZE(g12a_toacodec_dai_drv));
343 }
344 
345 static struct platform_driver g12a_toacodec_pdrv = {
346 	.driver = {
347 		.name = G12A_TOACODEC_DRV_NAME,
348 		.of_match_table = g12a_toacodec_of_match,
349 	},
350 	.probe = g12a_toacodec_probe,
351 };
352 module_platform_driver(g12a_toacodec_pdrv);
353 
354 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
355 MODULE_DESCRIPTION("Amlogic G12a To Internal DAC Codec Driver");
356 MODULE_LICENSE("GPL v2");
357