xref: /linux/sound/soc/meson/g12a-tohdmitx.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1c8609f38SJerome Brunet // SPDX-License-Identifier: GPL-2.0
2c8609f38SJerome Brunet //
3c8609f38SJerome Brunet // Copyright (c) 2019 BayLibre, SAS.
4c8609f38SJerome Brunet // Author: Jerome Brunet <jbrunet@baylibre.com>
5c8609f38SJerome Brunet 
6c8609f38SJerome Brunet #include <linux/bitfield.h>
7c8609f38SJerome Brunet #include <linux/clk.h>
8c8609f38SJerome Brunet #include <linux/module.h>
9c8609f38SJerome Brunet #include <sound/pcm_params.h>
10c8609f38SJerome Brunet #include <linux/regmap.h>
1122946f37SJerome Brunet #include <linux/reset.h>
12c8609f38SJerome Brunet #include <sound/soc.h>
13c8609f38SJerome Brunet #include <sound/soc-dai.h>
14c8609f38SJerome Brunet 
15c8609f38SJerome Brunet #include <dt-bindings/sound/meson-g12a-tohdmitx.h>
169c29fd9bSJerome Brunet #include "meson-codec-glue.h"
17c8609f38SJerome Brunet 
18c8609f38SJerome Brunet #define G12A_TOHDMITX_DRV_NAME "g12a-tohdmitx"
19c8609f38SJerome Brunet 
20c8609f38SJerome Brunet #define TOHDMITX_CTRL0			0x0
21c8609f38SJerome Brunet #define  CTRL0_ENABLE_SHIFT		31
229c29fd9bSJerome Brunet #define  CTRL0_I2S_DAT_SEL_SHIFT	12
239c29fd9bSJerome Brunet #define  CTRL0_I2S_DAT_SEL		(0x3 << CTRL0_I2S_DAT_SEL_SHIFT)
24c8609f38SJerome Brunet #define  CTRL0_I2S_LRCLK_SEL		GENMASK(9, 8)
25c8609f38SJerome Brunet #define  CTRL0_I2S_BLK_CAP_INV		BIT(7)
26c8609f38SJerome Brunet #define  CTRL0_I2S_BCLK_O_INV		BIT(6)
27c8609f38SJerome Brunet #define  CTRL0_I2S_BCLK_SEL		GENMASK(5, 4)
28c8609f38SJerome Brunet #define  CTRL0_SPDIF_CLK_CAP_INV	BIT(3)
29c8609f38SJerome Brunet #define  CTRL0_SPDIF_CLK_O_INV		BIT(2)
309c29fd9bSJerome Brunet #define  CTRL0_SPDIF_SEL_SHIFT		1
319c29fd9bSJerome Brunet #define  CTRL0_SPDIF_SEL		(0x1 << CTRL0_SPDIF_SEL_SHIFT)
32c8609f38SJerome Brunet #define  CTRL0_SPDIF_CLK_SEL		BIT(0)
33c8609f38SJerome Brunet 
34c8609f38SJerome Brunet static const char * const g12a_tohdmitx_i2s_mux_texts[] = {
35c8609f38SJerome Brunet 	"I2S A", "I2S B", "I2S C",
36c8609f38SJerome Brunet };
37c8609f38SJerome Brunet 
g12a_tohdmitx_i2s_mux_put_enum(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)38c8609f38SJerome Brunet static int g12a_tohdmitx_i2s_mux_put_enum(struct snd_kcontrol *kcontrol,
39c8609f38SJerome Brunet 				   struct snd_ctl_elem_value *ucontrol)
40c8609f38SJerome Brunet {
41c8609f38SJerome Brunet 	struct snd_soc_component *component =
42c8609f38SJerome Brunet 		snd_soc_dapm_kcontrol_component(kcontrol);
43c8609f38SJerome Brunet 	struct snd_soc_dapm_context *dapm =
44c8609f38SJerome Brunet 		snd_soc_dapm_kcontrol_dapm(kcontrol);
45c8609f38SJerome Brunet 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
469c29fd9bSJerome Brunet 	unsigned int mux, changed;
479c29fd9bSJerome Brunet 
481e001206SMark Brown 	if (ucontrol->value.enumerated.item[0] >= e->items)
491e001206SMark Brown 		return -EINVAL;
501e001206SMark Brown 
519c29fd9bSJerome Brunet 	mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
529c29fd9bSJerome Brunet 	changed = snd_soc_component_test_bits(component, e->reg,
539c29fd9bSJerome Brunet 					      CTRL0_I2S_DAT_SEL,
549c29fd9bSJerome Brunet 					      FIELD_PREP(CTRL0_I2S_DAT_SEL,
559c29fd9bSJerome Brunet 							 mux));
569c29fd9bSJerome Brunet 
579c29fd9bSJerome Brunet 	if (!changed)
589c29fd9bSJerome Brunet 		return 0;
59c8609f38SJerome Brunet 
60c8609f38SJerome Brunet 	/* Force disconnect of the mux while updating */
61c8609f38SJerome Brunet 	snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
62c8609f38SJerome Brunet 
639c29fd9bSJerome Brunet 	snd_soc_component_update_bits(component, e->reg,
64c8609f38SJerome Brunet 				      CTRL0_I2S_DAT_SEL |
65c8609f38SJerome Brunet 				      CTRL0_I2S_LRCLK_SEL |
66c8609f38SJerome Brunet 				      CTRL0_I2S_BCLK_SEL,
67c8609f38SJerome Brunet 				      FIELD_PREP(CTRL0_I2S_DAT_SEL, mux) |
68c8609f38SJerome Brunet 				      FIELD_PREP(CTRL0_I2S_LRCLK_SEL, mux) |
69c8609f38SJerome Brunet 				      FIELD_PREP(CTRL0_I2S_BCLK_SEL, mux));
70c8609f38SJerome Brunet 
71c8609f38SJerome Brunet 	snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
72c8609f38SJerome Brunet 
7312131008SMark Brown 	return 1;
74c8609f38SJerome Brunet }
75c8609f38SJerome Brunet 
769c29fd9bSJerome Brunet static SOC_ENUM_SINGLE_DECL(g12a_tohdmitx_i2s_mux_enum, TOHDMITX_CTRL0,
779c29fd9bSJerome Brunet 			    CTRL0_I2S_DAT_SEL_SHIFT,
789c29fd9bSJerome Brunet 			    g12a_tohdmitx_i2s_mux_texts);
799c29fd9bSJerome Brunet 
80c8609f38SJerome Brunet static const struct snd_kcontrol_new g12a_tohdmitx_i2s_mux =
81c8609f38SJerome Brunet 	SOC_DAPM_ENUM_EXT("I2S Source", g12a_tohdmitx_i2s_mux_enum,
829c29fd9bSJerome Brunet 			  snd_soc_dapm_get_enum_double,
83c8609f38SJerome Brunet 			  g12a_tohdmitx_i2s_mux_put_enum);
84c8609f38SJerome Brunet 
85c8609f38SJerome Brunet static const char * const g12a_tohdmitx_spdif_mux_texts[] = {
86c8609f38SJerome Brunet 	"SPDIF A", "SPDIF B",
87c8609f38SJerome Brunet };
88c8609f38SJerome Brunet 
g12a_tohdmitx_spdif_mux_put_enum(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)89c8609f38SJerome Brunet static int g12a_tohdmitx_spdif_mux_put_enum(struct snd_kcontrol *kcontrol,
90c8609f38SJerome Brunet 					    struct snd_ctl_elem_value *ucontrol)
91c8609f38SJerome Brunet {
92c8609f38SJerome Brunet 	struct snd_soc_component *component =
93c8609f38SJerome Brunet 		snd_soc_dapm_kcontrol_component(kcontrol);
94c8609f38SJerome Brunet 	struct snd_soc_dapm_context *dapm =
95c8609f38SJerome Brunet 		snd_soc_dapm_kcontrol_dapm(kcontrol);
96c8609f38SJerome Brunet 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
979c29fd9bSJerome Brunet 	unsigned int mux, changed;
989c29fd9bSJerome Brunet 
991e001206SMark Brown 	if (ucontrol->value.enumerated.item[0] >= e->items)
1001e001206SMark Brown 		return -EINVAL;
1011e001206SMark Brown 
1029c29fd9bSJerome Brunet 	mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
1039c29fd9bSJerome Brunet 	changed = snd_soc_component_test_bits(component, TOHDMITX_CTRL0,
1049c29fd9bSJerome Brunet 					      CTRL0_SPDIF_SEL,
1059c29fd9bSJerome Brunet 					      FIELD_PREP(CTRL0_SPDIF_SEL, mux));
1069c29fd9bSJerome Brunet 
1079c29fd9bSJerome Brunet 	if (!changed)
1089c29fd9bSJerome Brunet 		return 0;
109c8609f38SJerome Brunet 
110c8609f38SJerome Brunet 	/* Force disconnect of the mux while updating */
111c8609f38SJerome Brunet 	snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
112c8609f38SJerome Brunet 
113c8609f38SJerome Brunet 	snd_soc_component_update_bits(component, TOHDMITX_CTRL0,
114c8609f38SJerome Brunet 				      CTRL0_SPDIF_SEL |
115c8609f38SJerome Brunet 				      CTRL0_SPDIF_CLK_SEL,
116c8609f38SJerome Brunet 				      FIELD_PREP(CTRL0_SPDIF_SEL, mux) |
117c8609f38SJerome Brunet 				      FIELD_PREP(CTRL0_SPDIF_CLK_SEL, mux));
118c8609f38SJerome Brunet 
119c8609f38SJerome Brunet 	snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
120c8609f38SJerome Brunet 
121*b036d8efSMark Brown 	return 1;
122c8609f38SJerome Brunet }
123c8609f38SJerome Brunet 
1249c29fd9bSJerome Brunet static SOC_ENUM_SINGLE_DECL(g12a_tohdmitx_spdif_mux_enum, TOHDMITX_CTRL0,
1259c29fd9bSJerome Brunet 			    CTRL0_SPDIF_SEL_SHIFT,
1269c29fd9bSJerome Brunet 			    g12a_tohdmitx_spdif_mux_texts);
1279c29fd9bSJerome Brunet 
128c8609f38SJerome Brunet static const struct snd_kcontrol_new g12a_tohdmitx_spdif_mux =
129c8609f38SJerome Brunet 	SOC_DAPM_ENUM_EXT("SPDIF Source", g12a_tohdmitx_spdif_mux_enum,
1309c29fd9bSJerome Brunet 			  snd_soc_dapm_get_enum_double,
131c8609f38SJerome Brunet 			  g12a_tohdmitx_spdif_mux_put_enum);
132c8609f38SJerome Brunet 
133c8609f38SJerome Brunet static const struct snd_kcontrol_new g12a_tohdmitx_out_enable =
134c8609f38SJerome Brunet 	SOC_DAPM_SINGLE_AUTODISABLE("Switch", TOHDMITX_CTRL0,
135c8609f38SJerome Brunet 				    CTRL0_ENABLE_SHIFT, 1, 0);
136c8609f38SJerome Brunet 
137c8609f38SJerome Brunet static const struct snd_soc_dapm_widget g12a_tohdmitx_widgets[] = {
138c8609f38SJerome Brunet 	SND_SOC_DAPM_MUX("I2S SRC", SND_SOC_NOPM, 0, 0,
139c8609f38SJerome Brunet 			 &g12a_tohdmitx_i2s_mux),
140c8609f38SJerome Brunet 	SND_SOC_DAPM_SWITCH("I2S OUT EN", SND_SOC_NOPM, 0, 0,
141c8609f38SJerome Brunet 			    &g12a_tohdmitx_out_enable),
142c8609f38SJerome Brunet 	SND_SOC_DAPM_MUX("SPDIF SRC", SND_SOC_NOPM, 0, 0,
143c8609f38SJerome Brunet 			 &g12a_tohdmitx_spdif_mux),
144c8609f38SJerome Brunet 	SND_SOC_DAPM_SWITCH("SPDIF OUT EN", SND_SOC_NOPM, 0, 0,
145c8609f38SJerome Brunet 			    &g12a_tohdmitx_out_enable),
146c8609f38SJerome Brunet };
147c8609f38SJerome Brunet 
148c8609f38SJerome Brunet static const struct snd_soc_dai_ops g12a_tohdmitx_input_ops = {
1492d3155a9SKuninori Morimoto 	.probe		= meson_codec_glue_input_dai_probe,
1502d3155a9SKuninori Morimoto 	.remove		= meson_codec_glue_input_dai_remove,
1519c29fd9bSJerome Brunet 	.hw_params	= meson_codec_glue_input_hw_params,
1529c29fd9bSJerome Brunet 	.set_fmt	= meson_codec_glue_input_set_fmt,
153c8609f38SJerome Brunet };
154c8609f38SJerome Brunet 
155c8609f38SJerome Brunet static const struct snd_soc_dai_ops g12a_tohdmitx_output_ops = {
1569c29fd9bSJerome Brunet 	.startup	= meson_codec_glue_output_startup,
157c8609f38SJerome Brunet };
158c8609f38SJerome Brunet 
159c8609f38SJerome Brunet #define TOHDMITX_SPDIF_FORMATS					\
160c8609f38SJerome Brunet 	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |	\
161c8609f38SJerome Brunet 	 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
162c8609f38SJerome Brunet 
163c8609f38SJerome Brunet #define TOHDMITX_I2S_FORMATS					\
164c8609f38SJerome Brunet 	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |	\
165c8609f38SJerome Brunet 	 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE |	\
166c8609f38SJerome Brunet 	 SNDRV_PCM_FMTBIT_S32_LE)
167c8609f38SJerome Brunet 
168c8609f38SJerome Brunet #define TOHDMITX_STREAM(xname, xsuffix, xfmt, xchmax)		\
169c8609f38SJerome Brunet {								\
170c8609f38SJerome Brunet 	.stream_name	= xname " " xsuffix,			\
171c8609f38SJerome Brunet 	.channels_min	= 1,					\
172c8609f38SJerome Brunet 	.channels_max	= (xchmax),				\
173c8609f38SJerome Brunet 	.rate_min       = 8000,					\
174c8609f38SJerome Brunet 	.rate_max	= 192000,				\
175c8609f38SJerome Brunet 	.formats	= (xfmt),				\
176c8609f38SJerome Brunet }
177c8609f38SJerome Brunet 
178c8609f38SJerome Brunet #define TOHDMITX_IN(xname, xid, xfmt, xchmax) {				\
179c8609f38SJerome Brunet 	.name = xname,							\
180c8609f38SJerome Brunet 	.id = (xid),							\
181c8609f38SJerome Brunet 	.playback = TOHDMITX_STREAM(xname, "Playback", xfmt, xchmax),	\
182c8609f38SJerome Brunet 	.ops = &g12a_tohdmitx_input_ops,				\
183c8609f38SJerome Brunet }
184c8609f38SJerome Brunet 
185c8609f38SJerome Brunet #define TOHDMITX_OUT(xname, xid, xfmt, xchmax) {			\
186c8609f38SJerome Brunet 	.name = xname,							\
187c8609f38SJerome Brunet 	.id = (xid),							\
188c8609f38SJerome Brunet 	.capture = TOHDMITX_STREAM(xname, "Capture", xfmt, xchmax),	\
189c8609f38SJerome Brunet 	.ops = &g12a_tohdmitx_output_ops,				\
190c8609f38SJerome Brunet }
191c8609f38SJerome Brunet 
192c8609f38SJerome Brunet static struct snd_soc_dai_driver g12a_tohdmitx_dai_drv[] = {
193c8609f38SJerome Brunet 	TOHDMITX_IN("I2S IN A", TOHDMITX_I2S_IN_A,
194c8609f38SJerome Brunet 		    TOHDMITX_I2S_FORMATS, 8),
195c8609f38SJerome Brunet 	TOHDMITX_IN("I2S IN B", TOHDMITX_I2S_IN_B,
196c8609f38SJerome Brunet 		    TOHDMITX_I2S_FORMATS, 8),
197c8609f38SJerome Brunet 	TOHDMITX_IN("I2S IN C", TOHDMITX_I2S_IN_C,
198c8609f38SJerome Brunet 		    TOHDMITX_I2S_FORMATS, 8),
199c8609f38SJerome Brunet 	TOHDMITX_OUT("I2S OUT", TOHDMITX_I2S_OUT,
200c8609f38SJerome Brunet 		     TOHDMITX_I2S_FORMATS, 8),
201c8609f38SJerome Brunet 	TOHDMITX_IN("SPDIF IN A", TOHDMITX_SPDIF_IN_A,
202c8609f38SJerome Brunet 		    TOHDMITX_SPDIF_FORMATS, 2),
203c8609f38SJerome Brunet 	TOHDMITX_IN("SPDIF IN B", TOHDMITX_SPDIF_IN_B,
204c8609f38SJerome Brunet 		    TOHDMITX_SPDIF_FORMATS, 2),
205c8609f38SJerome Brunet 	TOHDMITX_OUT("SPDIF OUT", TOHDMITX_SPDIF_OUT,
206c8609f38SJerome Brunet 		     TOHDMITX_SPDIF_FORMATS, 2),
207c8609f38SJerome Brunet };
208c8609f38SJerome Brunet 
g12a_tohdmi_component_probe(struct snd_soc_component * c)209c8609f38SJerome Brunet static int g12a_tohdmi_component_probe(struct snd_soc_component *c)
210c8609f38SJerome Brunet {
211c8609f38SJerome Brunet 	/* Initialize the static clock parameters */
212c8609f38SJerome Brunet 	return snd_soc_component_write(c, TOHDMITX_CTRL0,
213c8609f38SJerome Brunet 		     CTRL0_I2S_BLK_CAP_INV | CTRL0_SPDIF_CLK_CAP_INV);
214c8609f38SJerome Brunet }
215c8609f38SJerome Brunet 
216c8609f38SJerome Brunet static const struct snd_soc_dapm_route g12a_tohdmitx_routes[] = {
217c8609f38SJerome Brunet 	{ "I2S SRC", "I2S A", "I2S IN A Playback" },
218c8609f38SJerome Brunet 	{ "I2S SRC", "I2S B", "I2S IN B Playback" },
219c8609f38SJerome Brunet 	{ "I2S SRC", "I2S C", "I2S IN C Playback" },
220c8609f38SJerome Brunet 	{ "I2S OUT EN", "Switch", "I2S SRC" },
221c8609f38SJerome Brunet 	{ "I2S OUT Capture", NULL, "I2S OUT EN" },
222c8609f38SJerome Brunet 	{ "SPDIF SRC", "SPDIF A", "SPDIF IN A Playback" },
223c8609f38SJerome Brunet 	{ "SPDIF SRC", "SPDIF B", "SPDIF IN B Playback" },
224c8609f38SJerome Brunet 	{ "SPDIF OUT EN", "Switch", "SPDIF SRC" },
225c8609f38SJerome Brunet 	{ "SPDIF OUT Capture", NULL, "SPDIF OUT EN" },
226c8609f38SJerome Brunet };
227c8609f38SJerome Brunet 
228c8609f38SJerome Brunet static const struct snd_soc_component_driver g12a_tohdmitx_component_drv = {
229c8609f38SJerome Brunet 	.probe			= g12a_tohdmi_component_probe,
230c8609f38SJerome Brunet 	.dapm_widgets		= g12a_tohdmitx_widgets,
231c8609f38SJerome Brunet 	.num_dapm_widgets	= ARRAY_SIZE(g12a_tohdmitx_widgets),
232c8609f38SJerome Brunet 	.dapm_routes		= g12a_tohdmitx_routes,
233c8609f38SJerome Brunet 	.num_dapm_routes	= ARRAY_SIZE(g12a_tohdmitx_routes),
234c8609f38SJerome Brunet 	.endianness		= 1,
235c8609f38SJerome Brunet };
236c8609f38SJerome Brunet 
237c8609f38SJerome Brunet static const struct regmap_config g12a_tohdmitx_regmap_cfg = {
238c8609f38SJerome Brunet 	.reg_bits	= 32,
239c8609f38SJerome Brunet 	.val_bits	= 32,
240c8609f38SJerome Brunet 	.reg_stride	= 4,
241c8609f38SJerome Brunet };
242c8609f38SJerome Brunet 
243c8609f38SJerome Brunet static const struct of_device_id g12a_tohdmitx_of_match[] = {
244c8609f38SJerome Brunet 	{ .compatible = "amlogic,g12a-tohdmitx", },
245c8609f38SJerome Brunet 	{}
246c8609f38SJerome Brunet };
247c8609f38SJerome Brunet MODULE_DEVICE_TABLE(of, g12a_tohdmitx_of_match);
248c8609f38SJerome Brunet 
g12a_tohdmitx_probe(struct platform_device * pdev)249c8609f38SJerome Brunet static int g12a_tohdmitx_probe(struct platform_device *pdev)
250c8609f38SJerome Brunet {
251c8609f38SJerome Brunet 	struct device *dev = &pdev->dev;
252c8609f38SJerome Brunet 	void __iomem *regs;
253c8609f38SJerome Brunet 	struct regmap *map;
25422946f37SJerome Brunet 	int ret;
25522946f37SJerome Brunet 
25622946f37SJerome Brunet 	ret = device_reset(dev);
25722946f37SJerome Brunet 	if (ret)
25822946f37SJerome Brunet 		return ret;
259c8609f38SJerome Brunet 
260a95c9012SYueHaibing 	regs = devm_platform_ioremap_resource(pdev, 0);
261c8609f38SJerome Brunet 	if (IS_ERR(regs))
262c8609f38SJerome Brunet 		return PTR_ERR(regs);
263c8609f38SJerome Brunet 
264c8609f38SJerome Brunet 	map = devm_regmap_init_mmio(dev, regs, &g12a_tohdmitx_regmap_cfg);
265c8609f38SJerome Brunet 	if (IS_ERR(map)) {
266c8609f38SJerome Brunet 		dev_err(dev, "failed to init regmap: %ld\n",
267c8609f38SJerome Brunet 			PTR_ERR(map));
268c8609f38SJerome Brunet 		return PTR_ERR(map);
269c8609f38SJerome Brunet 	}
270c8609f38SJerome Brunet 
271c8609f38SJerome Brunet 	return devm_snd_soc_register_component(dev,
272c8609f38SJerome Brunet 			&g12a_tohdmitx_component_drv, g12a_tohdmitx_dai_drv,
273c8609f38SJerome Brunet 			ARRAY_SIZE(g12a_tohdmitx_dai_drv));
274c8609f38SJerome Brunet }
275c8609f38SJerome Brunet 
276c8609f38SJerome Brunet static struct platform_driver g12a_tohdmitx_pdrv = {
277c8609f38SJerome Brunet 	.driver = {
278c8609f38SJerome Brunet 		.name = G12A_TOHDMITX_DRV_NAME,
279c8609f38SJerome Brunet 		.of_match_table = g12a_tohdmitx_of_match,
280c8609f38SJerome Brunet 	},
281c8609f38SJerome Brunet 	.probe = g12a_tohdmitx_probe,
282c8609f38SJerome Brunet };
283c8609f38SJerome Brunet module_platform_driver(g12a_tohdmitx_pdrv);
284c8609f38SJerome Brunet 
285c8609f38SJerome Brunet MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
286c8609f38SJerome Brunet MODULE_DESCRIPTION("Amlogic G12a To HDMI Tx Control Codec Driver");
287c8609f38SJerome Brunet MODULE_LICENSE("GPL v2");
288