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