xref: /linux/sound/soc/meson/aiu-codec-ctrl.c (revision 7d69804a35103a50852eae41bfe6a2e0061c68fd)
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_CLK_SEL		GENMASK(1, 0)
16 #define CTRL_DATA_SEL_SHIFT	4
17 #define CTRL_DATA_SEL		(0x3 << CTRL_DATA_SEL_SHIFT)
18 
19 static const char * const aiu_codec_ctrl_mux_texts[] = {
20 	"DISABLED", "PCM", "I2S",
21 };
22 
23 static int aiu_codec_ctrl_mux_put_enum(struct snd_kcontrol *kcontrol,
24 				       struct snd_ctl_elem_value *ucontrol)
25 {
26 	struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol);
27 	struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
28 	struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
29 	unsigned int mux, changed;
30 
31 	if (ucontrol->value.enumerated.item[0] >= e->items)
32 		return -EINVAL;
33 
34 	mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
35 	changed = snd_soc_component_test_bits(component, e->reg,
36 					      CTRL_DATA_SEL,
37 					      FIELD_PREP(CTRL_DATA_SEL, mux));
38 
39 	if (!changed)
40 		return 0;
41 
42 	/* Force disconnect of the mux while updating */
43 	snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
44 
45 	/* Reset the source first */
46 	snd_soc_component_update_bits(component, e->reg,
47 				      CTRL_CLK_SEL |
48 				      CTRL_DATA_SEL,
49 				      FIELD_PREP(CTRL_CLK_SEL, 0) |
50 				      FIELD_PREP(CTRL_DATA_SEL, 0));
51 
52 	/* Set the appropriate source */
53 	snd_soc_component_update_bits(component, e->reg,
54 				      CTRL_CLK_SEL |
55 				      CTRL_DATA_SEL,
56 				      FIELD_PREP(CTRL_CLK_SEL, mux) |
57 				      FIELD_PREP(CTRL_DATA_SEL, mux));
58 
59 	snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
60 
61 	return 1;
62 }
63 
64 static SOC_ENUM_SINGLE_DECL(aiu_hdmi_ctrl_mux_enum, AIU_HDMI_CLK_DATA_CTRL,
65 			    CTRL_DATA_SEL_SHIFT,
66 			    aiu_codec_ctrl_mux_texts);
67 
68 static const struct snd_kcontrol_new aiu_hdmi_ctrl_mux =
69 	SOC_DAPM_ENUM_EXT("HDMI Source", aiu_hdmi_ctrl_mux_enum,
70 			  snd_soc_dapm_get_enum_double,
71 			  aiu_codec_ctrl_mux_put_enum);
72 
73 static const struct snd_soc_dapm_widget aiu_hdmi_ctrl_widgets[] = {
74 	SND_SOC_DAPM_MUX("HDMI CTRL SRC", SND_SOC_NOPM, 0, 0,
75 			 &aiu_hdmi_ctrl_mux),
76 };
77 
78 static const struct snd_soc_dai_ops aiu_codec_ctrl_input_ops = {
79 	.probe		= meson_codec_glue_input_dai_probe,
80 	.remove		= meson_codec_glue_input_dai_remove,
81 	.hw_params	= meson_codec_glue_input_hw_params,
82 	.set_fmt	= meson_codec_glue_input_set_fmt,
83 };
84 
85 static const struct snd_soc_dai_ops aiu_codec_ctrl_output_ops = {
86 	.startup	= meson_codec_glue_output_startup,
87 };
88 
89 #define AIU_CODEC_CTRL_FORMATS					\
90 	(SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |	\
91 	 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE |	\
92 	 SNDRV_PCM_FMTBIT_S32_LE)
93 
94 #define AIU_CODEC_CTRL_STREAM(xname, xsuffix)			\
95 {								\
96 	.stream_name	= xname " " xsuffix,			\
97 	.channels_min	= 1,					\
98 	.channels_max	= 8,					\
99 	.rate_min       = 5512,					\
100 	.rate_max	= 192000,				\
101 	.formats	= AIU_CODEC_CTRL_FORMATS,		\
102 }
103 
104 #define AIU_CODEC_CTRL_INPUT(xname) {				\
105 	.name = "CODEC CTRL " xname,				\
106 	.playback = AIU_CODEC_CTRL_STREAM(xname, "Playback"),	\
107 	.ops = &aiu_codec_ctrl_input_ops,			\
108 }
109 
110 #define AIU_CODEC_CTRL_OUTPUT(xname) {				\
111 	.name = "CODEC CTRL " xname,				\
112 	.capture = AIU_CODEC_CTRL_STREAM(xname, "Capture"),	\
113 	.ops = &aiu_codec_ctrl_output_ops,			\
114 }
115 
116 static struct snd_soc_dai_driver aiu_hdmi_ctrl_dai_drv[] = {
117 	[CTRL_I2S] = AIU_CODEC_CTRL_INPUT("HDMI I2S IN"),
118 	[CTRL_PCM] = AIU_CODEC_CTRL_INPUT("HDMI PCM IN"),
119 	[CTRL_OUT] = AIU_CODEC_CTRL_OUTPUT("HDMI OUT"),
120 };
121 
122 static const struct snd_soc_dapm_route aiu_hdmi_ctrl_routes[] = {
123 	{ "HDMI CTRL SRC", "I2S", "HDMI I2S IN Playback" },
124 	{ "HDMI CTRL SRC", "PCM", "HDMI PCM IN Playback" },
125 	{ "HDMI OUT Capture", NULL, "HDMI CTRL SRC" },
126 };
127 
128 static int aiu_hdmi_of_xlate_dai_name(struct snd_soc_component *component,
129 				      const struct of_phandle_args *args,
130 				      const char **dai_name)
131 {
132 	return aiu_of_xlate_dai_name(component, args, dai_name, AIU_HDMI);
133 }
134 
135 static const struct snd_soc_component_driver aiu_hdmi_ctrl_component = {
136 	.name			= "AIU HDMI Codec Control",
137 	.dapm_widgets		= aiu_hdmi_ctrl_widgets,
138 	.num_dapm_widgets	= ARRAY_SIZE(aiu_hdmi_ctrl_widgets),
139 	.dapm_routes		= aiu_hdmi_ctrl_routes,
140 	.num_dapm_routes	= ARRAY_SIZE(aiu_hdmi_ctrl_routes),
141 	.of_xlate_dai_name	= aiu_hdmi_of_xlate_dai_name,
142 	.endianness		= 1,
143 #ifdef CONFIG_DEBUG_FS
144 	.debugfs_prefix		= "hdmi",
145 #endif
146 };
147 
148 int aiu_hdmi_ctrl_register_component(struct device *dev)
149 {
150 	return snd_soc_register_component(dev, &aiu_hdmi_ctrl_component,
151 					  aiu_hdmi_ctrl_dai_drv,
152 					  ARRAY_SIZE(aiu_hdmi_ctrl_dai_drv));
153 }
154 
155