1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright (c) 2019 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
6 #include <linux/bitfield.h>
8 #include <linux/module.h>
9 #include <sound/pcm_params.h>
10 #include <linux/regmap.h>
11 #include <sound/soc.h>
12 #include <sound/soc-dai.h>
14 #include <dt-bindings/sound/meson-g12a-tohdmitx.h>
15 #include "meson-codec-glue.h"
17 #define G12A_TOHDMITX_DRV_NAME "g12a-tohdmitx"
19 #define TOHDMITX_CTRL0 0x0
20 #define CTRL0_ENABLE_SHIFT 31
21 #define CTRL0_I2S_DAT_SEL_SHIFT 12
22 #define CTRL0_I2S_DAT_SEL (0x3 << CTRL0_I2S_DAT_SEL_SHIFT)
23 #define CTRL0_I2S_LRCLK_SEL GENMASK(9, 8)
24 #define CTRL0_I2S_BLK_CAP_INV BIT(7)
25 #define CTRL0_I2S_BCLK_O_INV BIT(6)
26 #define CTRL0_I2S_BCLK_SEL GENMASK(5, 4)
27 #define CTRL0_SPDIF_CLK_CAP_INV BIT(3)
28 #define CTRL0_SPDIF_CLK_O_INV BIT(2)
29 #define CTRL0_SPDIF_SEL_SHIFT 1
30 #define CTRL0_SPDIF_SEL (0x1 << CTRL0_SPDIF_SEL_SHIFT)
31 #define CTRL0_SPDIF_CLK_SEL BIT(0)
33 static const char * const g12a_tohdmitx_i2s_mux_texts[] = {
34 "I2S A", "I2S B", "I2S C",
37 static int g12a_tohdmitx_i2s_mux_put_enum(struct snd_kcontrol *kcontrol,
38 struct snd_ctl_elem_value *ucontrol)
40 struct snd_soc_component *component =
41 snd_soc_dapm_kcontrol_component(kcontrol);
42 struct snd_soc_dapm_context *dapm =
43 snd_soc_dapm_kcontrol_dapm(kcontrol);
44 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
45 unsigned int mux, changed;
47 mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
48 changed = snd_soc_component_test_bits(component, e->reg,
50 FIELD_PREP(CTRL0_I2S_DAT_SEL,
56 /* Force disconnect of the mux while updating */
57 snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
59 snd_soc_component_update_bits(component, e->reg,
63 FIELD_PREP(CTRL0_I2S_DAT_SEL, mux) |
64 FIELD_PREP(CTRL0_I2S_LRCLK_SEL, mux) |
65 FIELD_PREP(CTRL0_I2S_BCLK_SEL, mux));
67 snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
72 static SOC_ENUM_SINGLE_DECL(g12a_tohdmitx_i2s_mux_enum, TOHDMITX_CTRL0,
73 CTRL0_I2S_DAT_SEL_SHIFT,
74 g12a_tohdmitx_i2s_mux_texts);
76 static const struct snd_kcontrol_new g12a_tohdmitx_i2s_mux =
77 SOC_DAPM_ENUM_EXT("I2S Source", g12a_tohdmitx_i2s_mux_enum,
78 snd_soc_dapm_get_enum_double,
79 g12a_tohdmitx_i2s_mux_put_enum);
81 static const char * const g12a_tohdmitx_spdif_mux_texts[] = {
85 static int g12a_tohdmitx_spdif_mux_put_enum(struct snd_kcontrol *kcontrol,
86 struct snd_ctl_elem_value *ucontrol)
88 struct snd_soc_component *component =
89 snd_soc_dapm_kcontrol_component(kcontrol);
90 struct snd_soc_dapm_context *dapm =
91 snd_soc_dapm_kcontrol_dapm(kcontrol);
92 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
93 unsigned int mux, changed;
95 mux = snd_soc_enum_item_to_val(e, ucontrol->value.enumerated.item[0]);
96 changed = snd_soc_component_test_bits(component, TOHDMITX_CTRL0,
98 FIELD_PREP(CTRL0_SPDIF_SEL, mux));
103 /* Force disconnect of the mux while updating */
104 snd_soc_dapm_mux_update_power(dapm, kcontrol, 0, NULL, NULL);
106 snd_soc_component_update_bits(component, TOHDMITX_CTRL0,
109 FIELD_PREP(CTRL0_SPDIF_SEL, mux) |
110 FIELD_PREP(CTRL0_SPDIF_CLK_SEL, mux));
112 snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
117 static SOC_ENUM_SINGLE_DECL(g12a_tohdmitx_spdif_mux_enum, TOHDMITX_CTRL0,
118 CTRL0_SPDIF_SEL_SHIFT,
119 g12a_tohdmitx_spdif_mux_texts);
121 static const struct snd_kcontrol_new g12a_tohdmitx_spdif_mux =
122 SOC_DAPM_ENUM_EXT("SPDIF Source", g12a_tohdmitx_spdif_mux_enum,
123 snd_soc_dapm_get_enum_double,
124 g12a_tohdmitx_spdif_mux_put_enum);
126 static const struct snd_kcontrol_new g12a_tohdmitx_out_enable =
127 SOC_DAPM_SINGLE_AUTODISABLE("Switch", TOHDMITX_CTRL0,
128 CTRL0_ENABLE_SHIFT, 1, 0);
130 static const struct snd_soc_dapm_widget g12a_tohdmitx_widgets[] = {
131 SND_SOC_DAPM_MUX("I2S SRC", SND_SOC_NOPM, 0, 0,
132 &g12a_tohdmitx_i2s_mux),
133 SND_SOC_DAPM_SWITCH("I2S OUT EN", SND_SOC_NOPM, 0, 0,
134 &g12a_tohdmitx_out_enable),
135 SND_SOC_DAPM_MUX("SPDIF SRC", SND_SOC_NOPM, 0, 0,
136 &g12a_tohdmitx_spdif_mux),
137 SND_SOC_DAPM_SWITCH("SPDIF OUT EN", SND_SOC_NOPM, 0, 0,
138 &g12a_tohdmitx_out_enable),
141 static const struct snd_soc_dai_ops g12a_tohdmitx_input_ops = {
142 .hw_params = meson_codec_glue_input_hw_params,
143 .set_fmt = meson_codec_glue_input_set_fmt,
146 static const struct snd_soc_dai_ops g12a_tohdmitx_output_ops = {
147 .startup = meson_codec_glue_output_startup,
150 #define TOHDMITX_SPDIF_FORMATS \
151 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
152 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
154 #define TOHDMITX_I2S_FORMATS \
155 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
156 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE | \
157 SNDRV_PCM_FMTBIT_S32_LE)
159 #define TOHDMITX_STREAM(xname, xsuffix, xfmt, xchmax) \
161 .stream_name = xname " " xsuffix, \
163 .channels_max = (xchmax), \
165 .rate_max = 192000, \
169 #define TOHDMITX_IN(xname, xid, xfmt, xchmax) { \
172 .playback = TOHDMITX_STREAM(xname, "Playback", xfmt, xchmax), \
173 .ops = &g12a_tohdmitx_input_ops, \
174 .probe = meson_codec_glue_input_dai_probe, \
175 .remove = meson_codec_glue_input_dai_remove, \
178 #define TOHDMITX_OUT(xname, xid, xfmt, xchmax) { \
181 .capture = TOHDMITX_STREAM(xname, "Capture", xfmt, xchmax), \
182 .ops = &g12a_tohdmitx_output_ops, \
185 static struct snd_soc_dai_driver g12a_tohdmitx_dai_drv[] = {
186 TOHDMITX_IN("I2S IN A", TOHDMITX_I2S_IN_A,
187 TOHDMITX_I2S_FORMATS, 8),
188 TOHDMITX_IN("I2S IN B", TOHDMITX_I2S_IN_B,
189 TOHDMITX_I2S_FORMATS, 8),
190 TOHDMITX_IN("I2S IN C", TOHDMITX_I2S_IN_C,
191 TOHDMITX_I2S_FORMATS, 8),
192 TOHDMITX_OUT("I2S OUT", TOHDMITX_I2S_OUT,
193 TOHDMITX_I2S_FORMATS, 8),
194 TOHDMITX_IN("SPDIF IN A", TOHDMITX_SPDIF_IN_A,
195 TOHDMITX_SPDIF_FORMATS, 2),
196 TOHDMITX_IN("SPDIF IN B", TOHDMITX_SPDIF_IN_B,
197 TOHDMITX_SPDIF_FORMATS, 2),
198 TOHDMITX_OUT("SPDIF OUT", TOHDMITX_SPDIF_OUT,
199 TOHDMITX_SPDIF_FORMATS, 2),
202 static int g12a_tohdmi_component_probe(struct snd_soc_component *c)
204 /* Initialize the static clock parameters */
205 return snd_soc_component_write(c, TOHDMITX_CTRL0,
206 CTRL0_I2S_BLK_CAP_INV | CTRL0_SPDIF_CLK_CAP_INV);
209 static const struct snd_soc_dapm_route g12a_tohdmitx_routes[] = {
210 { "I2S SRC", "I2S A", "I2S IN A Playback" },
211 { "I2S SRC", "I2S B", "I2S IN B Playback" },
212 { "I2S SRC", "I2S C", "I2S IN C Playback" },
213 { "I2S OUT EN", "Switch", "I2S SRC" },
214 { "I2S OUT Capture", NULL, "I2S OUT EN" },
215 { "SPDIF SRC", "SPDIF A", "SPDIF IN A Playback" },
216 { "SPDIF SRC", "SPDIF B", "SPDIF IN B Playback" },
217 { "SPDIF OUT EN", "Switch", "SPDIF SRC" },
218 { "SPDIF OUT Capture", NULL, "SPDIF OUT EN" },
221 static const struct snd_soc_component_driver g12a_tohdmitx_component_drv = {
222 .probe = g12a_tohdmi_component_probe,
223 .dapm_widgets = g12a_tohdmitx_widgets,
224 .num_dapm_widgets = ARRAY_SIZE(g12a_tohdmitx_widgets),
225 .dapm_routes = g12a_tohdmitx_routes,
226 .num_dapm_routes = ARRAY_SIZE(g12a_tohdmitx_routes),
228 .non_legacy_dai_naming = 1,
231 static const struct regmap_config g12a_tohdmitx_regmap_cfg = {
237 static const struct of_device_id g12a_tohdmitx_of_match[] = {
238 { .compatible = "amlogic,g12a-tohdmitx", },
241 MODULE_DEVICE_TABLE(of, g12a_tohdmitx_of_match);
243 static int g12a_tohdmitx_probe(struct platform_device *pdev)
245 struct device *dev = &pdev->dev;
249 regs = devm_platform_ioremap_resource(pdev, 0);
251 return PTR_ERR(regs);
253 map = devm_regmap_init_mmio(dev, regs, &g12a_tohdmitx_regmap_cfg);
255 dev_err(dev, "failed to init regmap: %ld\n",
260 return devm_snd_soc_register_component(dev,
261 &g12a_tohdmitx_component_drv, g12a_tohdmitx_dai_drv,
262 ARRAY_SIZE(g12a_tohdmitx_dai_drv));
265 static struct platform_driver g12a_tohdmitx_pdrv = {
267 .name = G12A_TOHDMITX_DRV_NAME,
268 .of_match_table = g12a_tohdmitx_of_match,
270 .probe = g12a_tohdmitx_probe,
272 module_platform_driver(g12a_tohdmitx_pdrv);
274 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
275 MODULE_DESCRIPTION("Amlogic G12a To HDMI Tx Control Codec Driver");
276 MODULE_LICENSE("GPL v2");