xref: /openbmc/linux/sound/soc/meson/axg-tdmout.c (revision b830f94f)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 //
3 // Copyright (c) 2018 BayLibre, SAS.
4 // Author: Jerome Brunet <jbrunet@baylibre.com>
5 
6 #include <linux/module.h>
7 #include <linux/of_platform.h>
8 #include <linux/regmap.h>
9 #include <sound/soc.h>
10 #include <sound/soc-dai.h>
11 
12 #include "axg-tdm-formatter.h"
13 
14 #define TDMOUT_CTRL0			0x00
15 #define  TDMOUT_CTRL0_BITNUM_MASK	GENMASK(4, 0)
16 #define  TDMOUT_CTRL0_BITNUM(x)		((x) << 0)
17 #define  TDMOUT_CTRL0_SLOTNUM_MASK	GENMASK(9, 5)
18 #define  TDMOUT_CTRL0_SLOTNUM(x)	((x) << 5)
19 #define  TDMOUT_CTRL0_INIT_BITNUM_MASK	GENMASK(19, 15)
20 #define  TDMOUT_CTRL0_INIT_BITNUM(x)	((x) << 15)
21 #define  TDMOUT_CTRL0_ENABLE		BIT(31)
22 #define  TDMOUT_CTRL0_RST_OUT		BIT(29)
23 #define  TDMOUT_CTRL0_RST_IN		BIT(28)
24 #define TDMOUT_CTRL1			0x04
25 #define  TDMOUT_CTRL1_TYPE_MASK		GENMASK(6, 4)
26 #define  TDMOUT_CTRL1_TYPE(x)		((x) << 4)
27 #define  TDMOUT_CTRL1_MSB_POS_MASK	GENMASK(12, 8)
28 #define  TDMOUT_CTRL1_MSB_POS(x)	((x) << 8)
29 #define  TDMOUT_CTRL1_SEL_SHIFT		24
30 #define  TDMOUT_CTRL1_GAIN_EN		26
31 #define  TDMOUT_CTRL1_WS_INV		BIT(28)
32 #define TDMOUT_SWAP			0x08
33 #define TDMOUT_MASK0			0x0c
34 #define TDMOUT_MASK1			0x10
35 #define TDMOUT_MASK2			0x14
36 #define TDMOUT_MASK3			0x18
37 #define TDMOUT_STAT			0x1c
38 #define TDMOUT_GAIN0			0x20
39 #define TDMOUT_GAIN1			0x24
40 #define TDMOUT_MUTE_VAL			0x28
41 #define TDMOUT_MUTE0			0x2c
42 #define TDMOUT_MUTE1			0x30
43 #define TDMOUT_MUTE2			0x34
44 #define TDMOUT_MUTE3			0x38
45 #define TDMOUT_MASK_VAL			0x3c
46 
47 static const struct regmap_config axg_tdmout_regmap_cfg = {
48 	.reg_bits	= 32,
49 	.val_bits	= 32,
50 	.reg_stride	= 4,
51 	.max_register	= TDMOUT_MASK_VAL,
52 };
53 
54 static const struct snd_kcontrol_new axg_tdmout_controls[] = {
55 	SOC_DOUBLE("Lane 0 Volume", TDMOUT_GAIN0,  0,  8, 255, 0),
56 	SOC_DOUBLE("Lane 1 Volume", TDMOUT_GAIN0, 16, 24, 255, 0),
57 	SOC_DOUBLE("Lane 2 Volume", TDMOUT_GAIN1,  0,  8, 255, 0),
58 	SOC_DOUBLE("Lane 3 Volume", TDMOUT_GAIN1, 16, 24, 255, 0),
59 	SOC_SINGLE("Gain Enable Switch", TDMOUT_CTRL1,
60 		   TDMOUT_CTRL1_GAIN_EN, 1, 0),
61 };
62 
63 static const char * const tdmout_sel_texts[] = {
64 	"IN 0", "IN 1", "IN 2",
65 };
66 
67 static SOC_ENUM_SINGLE_DECL(axg_tdmout_sel_enum, TDMOUT_CTRL1,
68 			    TDMOUT_CTRL1_SEL_SHIFT, tdmout_sel_texts);
69 
70 static const struct snd_kcontrol_new axg_tdmout_in_mux =
71 	SOC_DAPM_ENUM("Input Source", axg_tdmout_sel_enum);
72 
73 static struct snd_soc_dai *
74 axg_tdmout_get_be(struct snd_soc_dapm_widget *w)
75 {
76 	struct snd_soc_dapm_path *p = NULL;
77 	struct snd_soc_dai *be;
78 
79 	snd_soc_dapm_widget_for_each_sink_path(w, p) {
80 		if (!p->connect)
81 			continue;
82 
83 		if (p->sink->id == snd_soc_dapm_dai_in)
84 			return (struct snd_soc_dai *)p->sink->priv;
85 
86 		be = axg_tdmout_get_be(p->sink);
87 		if (be)
88 			return be;
89 	}
90 
91 	return NULL;
92 }
93 
94 static struct axg_tdm_stream *
95 axg_tdmout_get_tdm_stream(struct snd_soc_dapm_widget *w)
96 {
97 	struct snd_soc_dai *be = axg_tdmout_get_be(w);
98 
99 	if (!be)
100 		return NULL;
101 
102 	return be->playback_dma_data;
103 }
104 
105 static void axg_tdmout_enable(struct regmap *map)
106 {
107 	/* Apply both reset */
108 	regmap_update_bits(map, TDMOUT_CTRL0,
109 			   TDMOUT_CTRL0_RST_OUT | TDMOUT_CTRL0_RST_IN, 0);
110 
111 	/* Clear out reset before in reset */
112 	regmap_update_bits(map, TDMOUT_CTRL0,
113 			   TDMOUT_CTRL0_RST_OUT, TDMOUT_CTRL0_RST_OUT);
114 	regmap_update_bits(map, TDMOUT_CTRL0,
115 			   TDMOUT_CTRL0_RST_IN,  TDMOUT_CTRL0_RST_IN);
116 
117 	/* Actually enable tdmout */
118 	regmap_update_bits(map, TDMOUT_CTRL0,
119 			   TDMOUT_CTRL0_ENABLE, TDMOUT_CTRL0_ENABLE);
120 }
121 
122 static void axg_tdmout_disable(struct regmap *map)
123 {
124 	regmap_update_bits(map, TDMOUT_CTRL0, TDMOUT_CTRL0_ENABLE, 0);
125 }
126 
127 static int axg_tdmout_prepare(struct regmap *map,
128 			      const struct axg_tdm_formatter_hw *quirks,
129 			      struct axg_tdm_stream *ts)
130 {
131 	unsigned int val, skew = quirks->skew_offset;
132 
133 	/* Set the stream skew */
134 	switch (ts->iface->fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
135 	case SND_SOC_DAIFMT_I2S:
136 	case SND_SOC_DAIFMT_DSP_A:
137 		break;
138 
139 	case SND_SOC_DAIFMT_LEFT_J:
140 	case SND_SOC_DAIFMT_DSP_B:
141 		skew += 1;
142 		break;
143 
144 	default:
145 		pr_err("Unsupported format: %u\n",
146 		       ts->iface->fmt & SND_SOC_DAIFMT_FORMAT_MASK);
147 		return -EINVAL;
148 	}
149 
150 	val = TDMOUT_CTRL0_INIT_BITNUM(skew);
151 
152 	/* Set the slot width */
153 	val |= TDMOUT_CTRL0_BITNUM(ts->iface->slot_width - 1);
154 
155 	/* Set the slot number */
156 	val |= TDMOUT_CTRL0_SLOTNUM(ts->iface->slots - 1);
157 
158 	regmap_update_bits(map, TDMOUT_CTRL0,
159 			   TDMOUT_CTRL0_INIT_BITNUM_MASK |
160 			   TDMOUT_CTRL0_BITNUM_MASK |
161 			   TDMOUT_CTRL0_SLOTNUM_MASK, val);
162 
163 	/* Set the sample width */
164 	val = TDMOUT_CTRL1_MSB_POS(ts->width - 1);
165 
166 	/* FIFO data are arranged in chunks of 64bits */
167 	switch (ts->physical_width) {
168 	case 8:
169 		/* 8 samples of 8 bits */
170 		val |= TDMOUT_CTRL1_TYPE(0);
171 		break;
172 	case 16:
173 		/* 4 samples of 16 bits - right justified */
174 		val |= TDMOUT_CTRL1_TYPE(2);
175 		break;
176 	case 32:
177 		/* 2 samples of 32 bits - right justified */
178 		val |= TDMOUT_CTRL1_TYPE(4);
179 		break;
180 	default:
181 		pr_err("Unsupported physical width: %u\n",
182 		       ts->physical_width);
183 		return -EINVAL;
184 	}
185 
186 	/* If the sample clock is inverted, invert it back for the formatter */
187 	if (axg_tdm_lrclk_invert(ts->iface->fmt))
188 		val |= TDMOUT_CTRL1_WS_INV;
189 
190 	regmap_update_bits(map, TDMOUT_CTRL1,
191 			   (TDMOUT_CTRL1_TYPE_MASK | TDMOUT_CTRL1_MSB_POS_MASK |
192 			    TDMOUT_CTRL1_WS_INV), val);
193 
194 	/* Set static swap mask configuration */
195 	regmap_write(map, TDMOUT_SWAP, 0x76543210);
196 
197 	return axg_tdm_formatter_set_channel_masks(map, ts, TDMOUT_MASK0);
198 }
199 
200 static const struct snd_soc_dapm_widget axg_tdmout_dapm_widgets[] = {
201 	SND_SOC_DAPM_AIF_IN("IN 0", NULL, 0, SND_SOC_NOPM, 0, 0),
202 	SND_SOC_DAPM_AIF_IN("IN 1", NULL, 0, SND_SOC_NOPM, 0, 0),
203 	SND_SOC_DAPM_AIF_IN("IN 2", NULL, 0, SND_SOC_NOPM, 0, 0),
204 	SND_SOC_DAPM_MUX("SRC SEL", SND_SOC_NOPM, 0, 0, &axg_tdmout_in_mux),
205 	SND_SOC_DAPM_PGA_E("ENC", SND_SOC_NOPM, 0, 0, NULL, 0,
206 			   axg_tdm_formatter_event,
207 			   (SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_PRE_PMD)),
208 	SND_SOC_DAPM_AIF_OUT("OUT", NULL, 0, SND_SOC_NOPM, 0, 0),
209 };
210 
211 static const struct snd_soc_dapm_route axg_tdmout_dapm_routes[] = {
212 	{ "SRC SEL", "IN 0", "IN 0" },
213 	{ "SRC SEL", "IN 1", "IN 1" },
214 	{ "SRC SEL", "IN 2", "IN 2" },
215 	{ "ENC", NULL, "SRC SEL" },
216 	{ "OUT", NULL, "ENC" },
217 };
218 
219 static const struct snd_soc_component_driver axg_tdmout_component_drv = {
220 	.controls		= axg_tdmout_controls,
221 	.num_controls		= ARRAY_SIZE(axg_tdmout_controls),
222 	.dapm_widgets		= axg_tdmout_dapm_widgets,
223 	.num_dapm_widgets	= ARRAY_SIZE(axg_tdmout_dapm_widgets),
224 	.dapm_routes		= axg_tdmout_dapm_routes,
225 	.num_dapm_routes	= ARRAY_SIZE(axg_tdmout_dapm_routes),
226 };
227 
228 static const struct axg_tdm_formatter_ops axg_tdmout_ops = {
229 	.get_stream	= axg_tdmout_get_tdm_stream,
230 	.prepare	= axg_tdmout_prepare,
231 	.enable		= axg_tdmout_enable,
232 	.disable	= axg_tdmout_disable,
233 };
234 
235 static const struct axg_tdm_formatter_driver axg_tdmout_drv = {
236 	.component_drv	= &axg_tdmout_component_drv,
237 	.regmap_cfg	= &axg_tdmout_regmap_cfg,
238 	.ops		= &axg_tdmout_ops,
239 	.quirks		= &(const struct axg_tdm_formatter_hw) {
240 		.invert_sclk = true,
241 		.skew_offset = 1,
242 	},
243 };
244 
245 static const struct axg_tdm_formatter_driver g12a_tdmout_drv = {
246 	.component_drv	= &axg_tdmout_component_drv,
247 	.regmap_cfg	= &axg_tdmout_regmap_cfg,
248 	.ops		= &axg_tdmout_ops,
249 	.quirks		= &(const struct axg_tdm_formatter_hw) {
250 		.invert_sclk = true,
251 		.skew_offset = 2,
252 	},
253 };
254 
255 static const struct of_device_id axg_tdmout_of_match[] = {
256 	{
257 		.compatible = "amlogic,axg-tdmout",
258 		.data = &axg_tdmout_drv,
259 	}, {
260 		.compatible = "amlogic,g12a-tdmout",
261 		.data = &g12a_tdmout_drv,
262 	}, {}
263 };
264 MODULE_DEVICE_TABLE(of, axg_tdmout_of_match);
265 
266 static struct platform_driver axg_tdmout_pdrv = {
267 	.probe = axg_tdm_formatter_probe,
268 	.driver = {
269 		.name = "axg-tdmout",
270 		.of_match_table = axg_tdmout_of_match,
271 	},
272 };
273 module_platform_driver(axg_tdmout_pdrv);
274 
275 MODULE_DESCRIPTION("Amlogic AXG TDM output formatter driver");
276 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
277 MODULE_LICENSE("GPL v2");
278