1// SPDX-License-Identifier: GPL-2.0
2//
3// Copyright (c) 2019 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/reset.h>
12#include <sound/soc.h>
13#include <sound/soc-dai.h>
14
15#include <dt-bindings/sound/meson-g12a-tohdmitx.h>
16#include "meson-codec-glue.h"
17
18#define G12A_TOHDMITX_DRV_NAME "g12a-tohdmitx"
19
20#define TOHDMITX_CTRL0 0x0
21#define CTRL0_ENABLE_SHIFT 31
22#define CTRL0_I2S_DAT_SEL_SHIFT 12
23#define CTRL0_I2S_DAT_SEL (0x3 << CTRL0_I2S_DAT_SEL_SHIFT)
24#define CTRL0_I2S_LRCLK_SEL GENMASK(9, 8)
25#define CTRL0_I2S_BLK_CAP_INV BIT(7)
26#define CTRL0_I2S_BCLK_O_INV BIT(6)
27#define CTRL0_I2S_BCLK_SEL GENMASK(5, 4)
28#define CTRL0_SPDIF_CLK_CAP_INV BIT(3)
29#define CTRL0_SPDIF_CLK_O_INV BIT(2)
30#define CTRL0_SPDIF_SEL_SHIFT 1
31#define CTRL0_SPDIF_SEL (0x1 << CTRL0_SPDIF_SEL_SHIFT)
32#define CTRL0_SPDIF_CLK_SEL BIT(0)
33
34static const char * const g12a_tohdmitx_i2s_mux_texts[] = {
35 "I2S A", "I2S B", "I2S C",
36};
37
38static int g12a_tohdmitx_i2s_mux_put_enum(struct snd_kcontrol *kcontrol,
39 struct snd_ctl_elem_value *ucontrol)
40{
41 struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol);
42 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
43 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
44 unsigned int mux, changed;
45
46 if (ucontrol->value.enumerated.item[0] >= e->items)
47 return -EINVAL;
48
49 mux = snd_soc_enum_item_to_val(e, item: ucontrol->value.enumerated.item[0]);
50 changed = snd_soc_component_test_bits(component, reg: e->reg,
51 CTRL0_I2S_DAT_SEL,
52 FIELD_PREP(CTRL0_I2S_DAT_SEL,
53 mux));
54
55 if (!changed)
56 return 0;
57
58 /* Force disconnect of the mux while updating */
59 snd_soc_dapm_mux_update_power(dapm, kcontrol, mux: 0, NULL, NULL);
60
61 snd_soc_component_update_bits(component, reg: e->reg,
62 CTRL0_I2S_DAT_SEL |
63 CTRL0_I2S_LRCLK_SEL |
64 CTRL0_I2S_BCLK_SEL,
65 FIELD_PREP(CTRL0_I2S_DAT_SEL, mux) |
66 FIELD_PREP(CTRL0_I2S_LRCLK_SEL, mux) |
67 FIELD_PREP(CTRL0_I2S_BCLK_SEL, mux));
68
69 snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
70
71 return 1;
72}
73
74static SOC_ENUM_SINGLE_DECL(g12a_tohdmitx_i2s_mux_enum, TOHDMITX_CTRL0,
75 CTRL0_I2S_DAT_SEL_SHIFT,
76 g12a_tohdmitx_i2s_mux_texts);
77
78static const struct snd_kcontrol_new g12a_tohdmitx_i2s_mux =
79 SOC_DAPM_ENUM_EXT("I2S Source", g12a_tohdmitx_i2s_mux_enum,
80 snd_soc_dapm_get_enum_double,
81 g12a_tohdmitx_i2s_mux_put_enum);
82
83static const char * const g12a_tohdmitx_spdif_mux_texts[] = {
84 "SPDIF A", "SPDIF B",
85};
86
87static int g12a_tohdmitx_spdif_mux_put_enum(struct snd_kcontrol *kcontrol,
88 struct snd_ctl_elem_value *ucontrol)
89{
90 struct snd_soc_component *component = snd_soc_dapm_kcontrol_to_component(kcontrol);
91 struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_to_dapm(kcontrol);
92 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
93 unsigned int mux, changed;
94
95 if (ucontrol->value.enumerated.item[0] >= e->items)
96 return -EINVAL;
97
98 mux = snd_soc_enum_item_to_val(e, item: ucontrol->value.enumerated.item[0]);
99 changed = snd_soc_component_test_bits(component, TOHDMITX_CTRL0,
100 CTRL0_SPDIF_SEL,
101 FIELD_PREP(CTRL0_SPDIF_SEL, mux));
102
103 if (!changed)
104 return 0;
105
106 /* Force disconnect of the mux while updating */
107 snd_soc_dapm_mux_update_power(dapm, kcontrol, mux: 0, NULL, NULL);
108
109 snd_soc_component_update_bits(component, TOHDMITX_CTRL0,
110 CTRL0_SPDIF_SEL |
111 CTRL0_SPDIF_CLK_SEL,
112 FIELD_PREP(CTRL0_SPDIF_SEL, mux) |
113 FIELD_PREP(CTRL0_SPDIF_CLK_SEL, mux));
114
115 snd_soc_dapm_mux_update_power(dapm, kcontrol, mux, e, NULL);
116
117 return 1;
118}
119
120static SOC_ENUM_SINGLE_DECL(g12a_tohdmitx_spdif_mux_enum, TOHDMITX_CTRL0,
121 CTRL0_SPDIF_SEL_SHIFT,
122 g12a_tohdmitx_spdif_mux_texts);
123
124static const struct snd_kcontrol_new g12a_tohdmitx_spdif_mux =
125 SOC_DAPM_ENUM_EXT("SPDIF Source", g12a_tohdmitx_spdif_mux_enum,
126 snd_soc_dapm_get_enum_double,
127 g12a_tohdmitx_spdif_mux_put_enum);
128
129static const struct snd_kcontrol_new g12a_tohdmitx_out_enable =
130 SOC_DAPM_SINGLE_AUTODISABLE("Switch", TOHDMITX_CTRL0,
131 CTRL0_ENABLE_SHIFT, 1, 0);
132
133static const struct snd_soc_dapm_widget g12a_tohdmitx_widgets[] = {
134 SND_SOC_DAPM_MUX("I2S SRC", SND_SOC_NOPM, 0, 0,
135 &g12a_tohdmitx_i2s_mux),
136 SND_SOC_DAPM_SWITCH("I2S OUT EN", SND_SOC_NOPM, 0, 0,
137 &g12a_tohdmitx_out_enable),
138 SND_SOC_DAPM_MUX("SPDIF SRC", SND_SOC_NOPM, 0, 0,
139 &g12a_tohdmitx_spdif_mux),
140 SND_SOC_DAPM_SWITCH("SPDIF OUT EN", SND_SOC_NOPM, 0, 0,
141 &g12a_tohdmitx_out_enable),
142};
143
144static const struct snd_soc_dai_ops g12a_tohdmitx_input_ops = {
145 .probe = meson_codec_glue_input_dai_probe,
146 .remove = meson_codec_glue_input_dai_remove,
147 .hw_params = meson_codec_glue_input_hw_params,
148 .set_fmt = meson_codec_glue_input_set_fmt,
149};
150
151static const struct snd_soc_dai_ops g12a_tohdmitx_output_ops = {
152 .startup = meson_codec_glue_output_startup,
153};
154
155#define TOHDMITX_SPDIF_FORMATS \
156 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
157 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE)
158
159#define TOHDMITX_I2S_FORMATS \
160 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
161 SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S24_LE | \
162 SNDRV_PCM_FMTBIT_S32_LE)
163
164#define TOHDMITX_STREAM(xname, xsuffix, xfmt, xchmax) \
165{ \
166 .stream_name = xname " " xsuffix, \
167 .channels_min = 1, \
168 .channels_max = (xchmax), \
169 .rate_min = 8000, \
170 .rate_max = 192000, \
171 .formats = (xfmt), \
172}
173
174#define TOHDMITX_IN(xname, xid, xfmt, xchmax) { \
175 .name = xname, \
176 .id = (xid), \
177 .playback = TOHDMITX_STREAM(xname, "Playback", xfmt, xchmax), \
178 .ops = &g12a_tohdmitx_input_ops, \
179}
180
181#define TOHDMITX_OUT(xname, xid, xfmt, xchmax) { \
182 .name = xname, \
183 .id = (xid), \
184 .capture = TOHDMITX_STREAM(xname, "Capture", xfmt, xchmax), \
185 .ops = &g12a_tohdmitx_output_ops, \
186}
187
188static struct snd_soc_dai_driver g12a_tohdmitx_dai_drv[] = {
189 TOHDMITX_IN("I2S IN A", TOHDMITX_I2S_IN_A,
190 TOHDMITX_I2S_FORMATS, 8),
191 TOHDMITX_IN("I2S IN B", TOHDMITX_I2S_IN_B,
192 TOHDMITX_I2S_FORMATS, 8),
193 TOHDMITX_IN("I2S IN C", TOHDMITX_I2S_IN_C,
194 TOHDMITX_I2S_FORMATS, 8),
195 TOHDMITX_OUT("I2S OUT", TOHDMITX_I2S_OUT,
196 TOHDMITX_I2S_FORMATS, 8),
197 TOHDMITX_IN("SPDIF IN A", TOHDMITX_SPDIF_IN_A,
198 TOHDMITX_SPDIF_FORMATS, 2),
199 TOHDMITX_IN("SPDIF IN B", TOHDMITX_SPDIF_IN_B,
200 TOHDMITX_SPDIF_FORMATS, 2),
201 TOHDMITX_OUT("SPDIF OUT", TOHDMITX_SPDIF_OUT,
202 TOHDMITX_SPDIF_FORMATS, 2),
203};
204
205static int g12a_tohdmi_component_probe(struct snd_soc_component *c)
206{
207 /* Initialize the static clock parameters */
208 return snd_soc_component_write(component: c, TOHDMITX_CTRL0,
209 CTRL0_I2S_BLK_CAP_INV | CTRL0_SPDIF_CLK_CAP_INV);
210}
211
212static const struct snd_soc_dapm_route g12a_tohdmitx_routes[] = {
213 { "I2S SRC", "I2S A", "I2S IN A Playback" },
214 { "I2S SRC", "I2S B", "I2S IN B Playback" },
215 { "I2S SRC", "I2S C", "I2S IN C Playback" },
216 { "I2S OUT EN", "Switch", "I2S SRC" },
217 { "I2S OUT Capture", NULL, "I2S OUT EN" },
218 { "SPDIF SRC", "SPDIF A", "SPDIF IN A Playback" },
219 { "SPDIF SRC", "SPDIF B", "SPDIF IN B Playback" },
220 { "SPDIF OUT EN", "Switch", "SPDIF SRC" },
221 { "SPDIF OUT Capture", NULL, "SPDIF OUT EN" },
222};
223
224static const struct snd_soc_component_driver g12a_tohdmitx_component_drv = {
225 .probe = g12a_tohdmi_component_probe,
226 .dapm_widgets = g12a_tohdmitx_widgets,
227 .num_dapm_widgets = ARRAY_SIZE(g12a_tohdmitx_widgets),
228 .dapm_routes = g12a_tohdmitx_routes,
229 .num_dapm_routes = ARRAY_SIZE(g12a_tohdmitx_routes),
230 .endianness = 1,
231};
232
233static const struct regmap_config g12a_tohdmitx_regmap_cfg = {
234 .reg_bits = 32,
235 .val_bits = 32,
236 .reg_stride = 4,
237};
238
239static const struct of_device_id g12a_tohdmitx_of_match[] = {
240 { .compatible = "amlogic,g12a-tohdmitx", },
241 {}
242};
243MODULE_DEVICE_TABLE(of, g12a_tohdmitx_of_match);
244
245static int g12a_tohdmitx_probe(struct platform_device *pdev)
246{
247 struct device *dev = &pdev->dev;
248 void __iomem *regs;
249 struct regmap *map;
250 int ret;
251
252 ret = device_reset(dev);
253 if (ret)
254 return ret;
255
256 regs = devm_platform_ioremap_resource(pdev, index: 0);
257 if (IS_ERR(ptr: regs))
258 return PTR_ERR(ptr: regs);
259
260 map = devm_regmap_init_mmio(dev, regs, &g12a_tohdmitx_regmap_cfg);
261 if (IS_ERR(ptr: map)) {
262 dev_err(dev, "failed to init regmap: %ld\n",
263 PTR_ERR(map));
264 return PTR_ERR(ptr: map);
265 }
266
267 return devm_snd_soc_register_component(dev,
268 component_driver: &g12a_tohdmitx_component_drv, dai_drv: g12a_tohdmitx_dai_drv,
269 ARRAY_SIZE(g12a_tohdmitx_dai_drv));
270}
271
272static struct platform_driver g12a_tohdmitx_pdrv = {
273 .driver = {
274 .name = G12A_TOHDMITX_DRV_NAME,
275 .of_match_table = g12a_tohdmitx_of_match,
276 },
277 .probe = g12a_tohdmitx_probe,
278};
279module_platform_driver(g12a_tohdmitx_pdrv);
280
281MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
282MODULE_DESCRIPTION("Amlogic G12a To HDMI Tx Control Codec Driver");
283MODULE_LICENSE("GPL v2");
284

source code of linux/sound/soc/meson/g12a-tohdmitx.c