xref: /openbmc/linux/sound/soc/codecs/madera.h (revision 6279eb3d)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Cirrus Logic Madera class codecs common support
4  *
5  * Copyright (C) 2015-2018 Cirrus Logic, Inc. and
6  *                         Cirrus Logic International Semiconductor Ltd.
7  */
8 
9 #ifndef ASOC_MADERA_H
10 #define ASOC_MADERA_H
11 
12 #include <linux/completion.h>
13 #include <sound/soc.h>
14 #include <sound/madera-pdata.h>
15 
16 #include "wm_adsp.h"
17 
18 #define MADERA_FLL1_REFCLK		1
19 #define MADERA_FLL2_REFCLK		2
20 #define MADERA_FLL3_REFCLK		3
21 #define MADERA_FLLAO_REFCLK		4
22 #define MADERA_FLL1_SYNCCLK		5
23 #define MADERA_FLL2_SYNCCLK		6
24 #define MADERA_FLL3_SYNCCLK		7
25 #define MADERA_FLLAO_SYNCCLK		8
26 
27 #define MADERA_FLL_SRC_NONE		-1
28 #define MADERA_FLL_SRC_MCLK1		0
29 #define MADERA_FLL_SRC_MCLK2		1
30 #define MADERA_FLL_SRC_SLIMCLK		3
31 #define MADERA_FLL_SRC_FLL1		4
32 #define MADERA_FLL_SRC_FLL2		5
33 #define MADERA_FLL_SRC_AIF1BCLK		8
34 #define MADERA_FLL_SRC_AIF2BCLK		9
35 #define MADERA_FLL_SRC_AIF3BCLK		10
36 #define MADERA_FLL_SRC_AIF4BCLK		11
37 #define MADERA_FLL_SRC_AIF1LRCLK	12
38 #define MADERA_FLL_SRC_AIF2LRCLK	13
39 #define MADERA_FLL_SRC_AIF3LRCLK	14
40 #define MADERA_FLL_SRC_AIF4LRCLK	15
41 
42 #define MADERA_CLK_SYSCLK_1		1
43 #define MADERA_CLK_ASYNCCLK_1		2
44 #define MADERA_CLK_OPCLK		3
45 #define MADERA_CLK_ASYNC_OPCLK		4
46 #define MADERA_CLK_SYSCLK_2		5
47 #define MADERA_CLK_SYSCLK_3		6
48 #define MADERA_CLK_ASYNCCLK_2		7
49 #define MADERA_CLK_DSPCLK		8
50 #define MADERA_CLK_OUTCLK		9
51 
52 #define MADERA_CLK_SRC_MCLK1		0x0
53 #define MADERA_CLK_SRC_MCLK2		0x1
54 #define MADERA_CLK_SRC_FLL1		0x4
55 #define MADERA_CLK_SRC_FLL2		0x5
56 #define MADERA_CLK_SRC_FLL3		0x6
57 #define MADERA_CLK_SRC_FLLAO_HI		0x7
58 #define MADERA_CLK_SRC_FLL1_DIV6	0x7
59 #define MADERA_CLK_SRC_AIF1BCLK		0x8
60 #define MADERA_CLK_SRC_AIF2BCLK		0x9
61 #define MADERA_CLK_SRC_AIF3BCLK		0xA
62 #define MADERA_CLK_SRC_AIF4BCLK		0xB
63 #define MADERA_CLK_SRC_FLLAO		0xF
64 
65 #define MADERA_OUTCLK_SYSCLK		0
66 #define MADERA_OUTCLK_ASYNCCLK		1
67 #define MADERA_OUTCLK_MCLK1		4
68 #define MADERA_OUTCLK_MCLK2		5
69 #define MADERA_OUTCLK_MCLK3		6
70 
71 #define MADERA_MIXER_VOL_MASK		0x00FE
72 #define MADERA_MIXER_VOL_SHIFT		1
73 #define MADERA_MIXER_VOL_WIDTH		7
74 
75 #define MADERA_DOM_GRP_FX		0
76 #define MADERA_DOM_GRP_ASRC1		1
77 #define MADERA_DOM_GRP_ASRC2		2
78 #define MADERA_DOM_GRP_ISRC1		3
79 #define MADERA_DOM_GRP_ISRC2		4
80 #define MADERA_DOM_GRP_ISRC3		5
81 #define MADERA_DOM_GRP_ISRC4		6
82 #define MADERA_DOM_GRP_OUT		7
83 #define MADERA_DOM_GRP_SPD		8
84 #define MADERA_DOM_GRP_DSP1		9
85 #define MADERA_DOM_GRP_DSP2		10
86 #define MADERA_DOM_GRP_DSP3		11
87 #define MADERA_DOM_GRP_DSP4		12
88 #define MADERA_DOM_GRP_DSP5		13
89 #define MADERA_DOM_GRP_DSP6		14
90 #define MADERA_DOM_GRP_DSP7		15
91 #define MADERA_DOM_GRP_AIF1		16
92 #define MADERA_DOM_GRP_AIF2		17
93 #define MADERA_DOM_GRP_AIF3		18
94 #define MADERA_DOM_GRP_AIF4		19
95 #define MADERA_DOM_GRP_SLIMBUS		20
96 #define MADERA_DOM_GRP_PWM		21
97 #define MADERA_DOM_GRP_DFC		22
98 #define MADERA_N_DOM_GRPS		23
99 
100 #define MADERA_MAX_DAI			11
101 #define MADERA_MAX_ADSP			7
102 
103 #define MADERA_NUM_MIXER_INPUTS		148
104 
105 struct madera;
106 struct wm_adsp;
107 
108 struct madera_voice_trigger_info {
109 	/** Which core triggered, 1-based (1 = DSP1, ...) */
110 	int core_num;
111 };
112 
113 struct madera_dai_priv {
114 	int clk;
115 	struct snd_pcm_hw_constraint_list constraint;
116 };
117 
118 struct madera_priv {
119 	struct wm_adsp adsp[MADERA_MAX_ADSP];
120 	struct madera *madera;
121 	struct device *dev;
122 	int sysclk;
123 	int asyncclk;
124 	int dspclk;
125 	struct madera_dai_priv dai[MADERA_MAX_DAI];
126 
127 	int num_inputs;
128 
129 	unsigned int in_pending;
130 
131 	unsigned int out_up_pending;
132 	unsigned int out_up_delay;
133 	unsigned int out_down_pending;
134 	unsigned int out_down_delay;
135 
136 	unsigned int adsp_rate_cache[MADERA_MAX_ADSP];
137 
138 	struct mutex rate_lock;
139 
140 	int tdm_width[MADERA_MAX_AIF];
141 	int tdm_slots[MADERA_MAX_AIF];
142 
143 	int domain_group_ref[MADERA_N_DOM_GRPS];
144 };
145 
146 struct madera_fll_cfg {
147 	int n;
148 	unsigned int theta;
149 	unsigned int lambda;
150 	int refdiv;
151 	int fratio;
152 	int gain;
153 	int alt_gain;
154 };
155 
156 struct madera_fll {
157 	struct madera *madera;
158 	int id;
159 	unsigned int base;
160 
161 	unsigned int fout;
162 
163 	int sync_src;
164 	unsigned int sync_freq;
165 
166 	int ref_src;
167 	unsigned int ref_freq;
168 	struct madera_fll_cfg ref_cfg;
169 };
170 
171 struct madera_enum {
172 	struct soc_enum mixer_enum;
173 	int val;
174 };
175 
176 extern const unsigned int madera_ana_tlv[];
177 extern const unsigned int madera_eq_tlv[];
178 extern const unsigned int madera_digital_tlv[];
179 extern const unsigned int madera_noise_tlv[];
180 extern const unsigned int madera_ng_tlv[];
181 
182 extern const unsigned int madera_mixer_tlv[];
183 extern const char * const madera_mixer_texts[MADERA_NUM_MIXER_INPUTS];
184 extern const unsigned int madera_mixer_values[MADERA_NUM_MIXER_INPUTS];
185 
186 #define MADERA_GAINMUX_CONTROLS(name, base) \
187 	SOC_SINGLE_RANGE_TLV(name " Input Volume", base + 1,		\
188 			     MADERA_MIXER_VOL_SHIFT, 0x20, 0x50, 0,	\
189 			     madera_mixer_tlv)
190 
191 #define MADERA_MIXER_CONTROLS(name, base) \
192 	SOC_SINGLE_RANGE_TLV(name " Input 1 Volume", base + 1,		\
193 			     MADERA_MIXER_VOL_SHIFT, 0x20, 0x50, 0,	\
194 			     madera_mixer_tlv),			\
195 	SOC_SINGLE_RANGE_TLV(name " Input 2 Volume", base + 3,		\
196 			     MADERA_MIXER_VOL_SHIFT, 0x20, 0x50, 0,	\
197 			     madera_mixer_tlv),			\
198 	SOC_SINGLE_RANGE_TLV(name " Input 3 Volume", base + 5,		\
199 			     MADERA_MIXER_VOL_SHIFT, 0x20, 0x50, 0,	\
200 			     madera_mixer_tlv),			\
201 	SOC_SINGLE_RANGE_TLV(name " Input 4 Volume", base + 7,		\
202 			     MADERA_MIXER_VOL_SHIFT, 0x20, 0x50, 0,	\
203 			     madera_mixer_tlv)
204 
205 #define MADERA_MUX_ENUM_DECL(name, reg) \
206 	SOC_VALUE_ENUM_SINGLE_AUTODISABLE_DECL( \
207 		name, reg, 0, 0xff, madera_mixer_texts, madera_mixer_values)
208 
209 #define MADERA_MUX_CTL_DECL(name) \
210 	const struct snd_kcontrol_new name##_mux =	\
211 		SOC_DAPM_ENUM("Route", name##_enum)
212 
213 #define MADERA_MUX_ENUMS(name, base_reg) \
214 	static MADERA_MUX_ENUM_DECL(name##_enum, base_reg);	\
215 	static MADERA_MUX_CTL_DECL(name)
216 
217 #define MADERA_MIXER_ENUMS(name, base_reg) \
218 	MADERA_MUX_ENUMS(name##_in1, base_reg);     \
219 	MADERA_MUX_ENUMS(name##_in2, base_reg + 2); \
220 	MADERA_MUX_ENUMS(name##_in3, base_reg + 4); \
221 	MADERA_MUX_ENUMS(name##_in4, base_reg + 6)
222 
223 #define MADERA_DSP_AUX_ENUMS(name, base_reg) \
224 	MADERA_MUX_ENUMS(name##_aux1, base_reg);	\
225 	MADERA_MUX_ENUMS(name##_aux2, base_reg + 8);	\
226 	MADERA_MUX_ENUMS(name##_aux3, base_reg + 16);	\
227 	MADERA_MUX_ENUMS(name##_aux4, base_reg + 24);	\
228 	MADERA_MUX_ENUMS(name##_aux5, base_reg + 32);	\
229 	MADERA_MUX_ENUMS(name##_aux6, base_reg + 40)
230 
231 #define MADERA_MUX(name, ctrl) \
232 	SND_SOC_DAPM_MUX(name, SND_SOC_NOPM, 0, 0, ctrl)
233 
234 #define MADERA_MUX_WIDGETS(name, name_str) \
235 	MADERA_MUX(name_str " Input 1", &name##_mux)
236 
237 #define MADERA_MIXER_WIDGETS(name, name_str)	\
238 	MADERA_MUX(name_str " Input 1", &name##_in1_mux), \
239 	MADERA_MUX(name_str " Input 2", &name##_in2_mux), \
240 	MADERA_MUX(name_str " Input 3", &name##_in3_mux), \
241 	MADERA_MUX(name_str " Input 4", &name##_in4_mux), \
242 	SND_SOC_DAPM_MIXER(name_str " Mixer", SND_SOC_NOPM, 0, 0, NULL, 0)
243 
244 #define MADERA_DSP_WIDGETS(name, name_str)			\
245 	MADERA_MIXER_WIDGETS(name##L, name_str "L"),		\
246 	MADERA_MIXER_WIDGETS(name##R, name_str "R"),		\
247 	MADERA_MUX(name_str " Aux 1", &name##_aux1_mux),	\
248 	MADERA_MUX(name_str " Aux 2", &name##_aux2_mux),	\
249 	MADERA_MUX(name_str " Aux 3", &name##_aux3_mux),	\
250 	MADERA_MUX(name_str " Aux 4", &name##_aux4_mux),	\
251 	MADERA_MUX(name_str " Aux 5", &name##_aux5_mux),	\
252 	MADERA_MUX(name_str " Aux 6", &name##_aux6_mux)
253 
254 #define MADERA_MUX_ROUTES(widget, name) \
255 	{ widget, NULL, name " Input 1" }, \
256 	MADERA_MIXER_INPUT_ROUTES(name " Input 1")
257 
258 #define MADERA_MIXER_ROUTES(widget, name)		\
259 	{ widget, NULL, name " Mixer" },		\
260 	{ name " Mixer", NULL, name " Input 1" },	\
261 	{ name " Mixer", NULL, name " Input 2" },	\
262 	{ name " Mixer", NULL, name " Input 3" },	\
263 	{ name " Mixer", NULL, name " Input 4" },	\
264 	MADERA_MIXER_INPUT_ROUTES(name " Input 1"),	\
265 	MADERA_MIXER_INPUT_ROUTES(name " Input 2"),	\
266 	MADERA_MIXER_INPUT_ROUTES(name " Input 3"),	\
267 	MADERA_MIXER_INPUT_ROUTES(name " Input 4")
268 
269 #define MADERA_DSP_ROUTES(name)				\
270 	{ name, NULL, name " Preloader"},		\
271 	{ name " Preload", NULL, name " Preloader"},	\
272 	{ name, NULL, "SYSCLK"},			\
273 	{ name, NULL, "DSPCLK"},			\
274 	{ name, NULL, name " Aux 1" },			\
275 	{ name, NULL, name " Aux 2" },			\
276 	{ name, NULL, name " Aux 3" },			\
277 	{ name, NULL, name " Aux 4" },			\
278 	{ name, NULL, name " Aux 5" },			\
279 	{ name, NULL, name " Aux 6" },			\
280 	MADERA_MIXER_INPUT_ROUTES(name " Aux 1"),	\
281 	MADERA_MIXER_INPUT_ROUTES(name " Aux 2"),	\
282 	MADERA_MIXER_INPUT_ROUTES(name " Aux 3"),	\
283 	MADERA_MIXER_INPUT_ROUTES(name " Aux 4"),	\
284 	MADERA_MIXER_INPUT_ROUTES(name " Aux 5"),	\
285 	MADERA_MIXER_INPUT_ROUTES(name " Aux 6"),	\
286 	MADERA_MIXER_ROUTES(name, name "L"),		\
287 	MADERA_MIXER_ROUTES(name, name "R")
288 
289 #define MADERA_RATE_ENUM(xname, xenum) \
290 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,\
291 	.info = snd_soc_info_enum_double, \
292 	.get = snd_soc_get_enum_double, .put = madera_rate_put, \
293 	.private_value = (unsigned long)&xenum }
294 
295 #define MADERA_EQ_CONTROL(xname, xbase)				\
296 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
297 	.info = snd_soc_bytes_info, .get = snd_soc_bytes_get,	\
298 	.put = madera_eq_coeff_put, .private_value =		\
299 	((unsigned long)&(struct soc_bytes) { .base = xbase,	\
300 	 .num_regs = 20, .mask = ~MADERA_EQ1_B1_MODE }) }
301 
302 #define MADERA_LHPF_CONTROL(xname, xbase)			\
303 {	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname,	\
304 	.info = snd_soc_bytes_info, .get = snd_soc_bytes_get,	\
305 	.put = madera_lhpf_coeff_put, .private_value =		\
306 	((unsigned long)&(struct soc_bytes) { .base = xbase,	\
307 	 .num_regs = 1 }) }
308 
309 #define MADERA_RATES SNDRV_PCM_RATE_KNOT
310 
311 #define MADERA_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
312 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
313 
314 #define MADERA_OSR_ENUM_SIZE		5
315 #define MADERA_SYNC_RATE_ENUM_SIZE	3
316 #define MADERA_ASYNC_RATE_ENUM_SIZE	2
317 #define MADERA_RATE_ENUM_SIZE \
318 		(MADERA_SYNC_RATE_ENUM_SIZE + MADERA_ASYNC_RATE_ENUM_SIZE)
319 #define MADERA_SAMPLE_RATE_ENUM_SIZE	16
320 #define MADERA_DFC_TYPE_ENUM_SIZE	5
321 #define MADERA_DFC_WIDTH_ENUM_SIZE	5
322 
323 extern const struct snd_soc_dai_ops madera_dai_ops;
324 extern const struct snd_soc_dai_ops madera_simple_dai_ops;
325 
326 extern const struct snd_kcontrol_new madera_inmux[];
327 extern const struct snd_kcontrol_new madera_inmode[];
328 
329 extern const char * const madera_rate_text[MADERA_RATE_ENUM_SIZE];
330 extern const unsigned int madera_rate_val[MADERA_RATE_ENUM_SIZE];
331 
332 extern const struct soc_enum madera_sample_rate[];
333 extern const struct soc_enum madera_isrc_fsl[];
334 extern const struct soc_enum madera_isrc_fsh[];
335 extern const struct soc_enum madera_asrc1_rate[];
336 extern const struct soc_enum madera_asrc1_bidir_rate[];
337 extern const struct soc_enum madera_asrc2_rate[];
338 extern const struct soc_enum madera_dfc_width[];
339 extern const struct soc_enum madera_dfc_type[];
340 
341 extern const struct soc_enum madera_in_vi_ramp;
342 extern const struct soc_enum madera_in_vd_ramp;
343 
344 extern const struct soc_enum madera_out_vi_ramp;
345 extern const struct soc_enum madera_out_vd_ramp;
346 
347 extern const struct soc_enum madera_lhpf1_mode;
348 extern const struct soc_enum madera_lhpf2_mode;
349 extern const struct soc_enum madera_lhpf3_mode;
350 extern const struct soc_enum madera_lhpf4_mode;
351 
352 extern const struct soc_enum madera_ng_hold;
353 extern const struct soc_enum madera_in_hpf_cut_enum;
354 extern const struct soc_enum madera_in_dmic_osr[];
355 
356 extern const struct soc_enum madera_output_anc_src[];
357 extern const struct soc_enum madera_anc_input_src[];
358 extern const struct soc_enum madera_anc_ng_enum;
359 
360 extern const struct snd_kcontrol_new madera_dsp_trigger_output_mux[];
361 extern const struct snd_kcontrol_new madera_drc_activity_output_mux[];
362 
363 extern const struct snd_kcontrol_new madera_adsp_rate_controls[];
364 
365 int madera_dfc_put(struct snd_kcontrol *kcontrol,
366 		   struct snd_ctl_elem_value *ucontrol);
367 
368 int madera_lp_mode_put(struct snd_kcontrol *kcontrol,
369 		       struct snd_ctl_elem_value *ucontrol);
370 
371 int madera_out1_demux_put(struct snd_kcontrol *kcontrol,
372 			  struct snd_ctl_elem_value *ucontrol);
373 int madera_out1_demux_get(struct snd_kcontrol *kcontrol,
374 			  struct snd_ctl_elem_value *ucontrol);
375 
376 int madera_rate_put(struct snd_kcontrol *kcontrol,
377 		    struct snd_ctl_elem_value *ucontrol);
378 
379 int madera_eq_coeff_put(struct snd_kcontrol *kcontrol,
380 			struct snd_ctl_elem_value *ucontrol);
381 int madera_lhpf_coeff_put(struct snd_kcontrol *kcontrol,
382 			  struct snd_ctl_elem_value *ucontrol);
383 
384 int madera_sysclk_ev(struct snd_soc_dapm_widget *w,
385 		     struct snd_kcontrol *kcontrol, int event);
386 int madera_spk_ev(struct snd_soc_dapm_widget *w,
387 		  struct snd_kcontrol *kcontrol, int event);
388 int madera_in_ev(struct snd_soc_dapm_widget *w,
389 		 struct snd_kcontrol *kcontrol, int event);
390 int madera_out_ev(struct snd_soc_dapm_widget *w,
391 		  struct snd_kcontrol *kcontrol, int event);
392 int madera_hp_ev(struct snd_soc_dapm_widget *w,
393 		 struct snd_kcontrol *kcontrol, int event);
394 int madera_anc_ev(struct snd_soc_dapm_widget *w,
395 		  struct snd_kcontrol *kcontrol, int event);
396 int madera_domain_clk_ev(struct snd_soc_dapm_widget *w,
397 			 struct snd_kcontrol *kcontrol,
398 			 int event);
399 
400 int madera_set_adsp_clk(struct madera_priv *priv, int dsp_num,
401 			unsigned int freq);
402 
403 int madera_set_sysclk(struct snd_soc_component *component, int clk_id,
404 		      int source, unsigned int freq, int dir);
405 
406 int madera_init_fll(struct madera *madera, int id, int base,
407 		    struct madera_fll *fll);
408 int madera_set_fll_refclk(struct madera_fll *fll, int source,
409 			  unsigned int fref, unsigned int fout);
410 int madera_set_fll_syncclk(struct madera_fll *fll, int source,
411 			   unsigned int fref, unsigned int fout);
412 int madera_set_fll_ao_refclk(struct madera_fll *fll, int source,
413 			     unsigned int fin, unsigned int fout);
414 int madera_fllhj_set_refclk(struct madera_fll *fll, int source,
415 			    unsigned int fin, unsigned int fout);
416 
417 int madera_core_init(struct madera_priv *priv);
418 int madera_core_free(struct madera_priv *priv);
419 int madera_init_overheat(struct madera_priv *priv);
420 int madera_free_overheat(struct madera_priv *priv);
421 int madera_init_inputs(struct snd_soc_component *component);
422 int madera_init_outputs(struct snd_soc_component *component, int n_mono_routes);
423 int madera_init_bus_error_irq(struct madera_priv *priv, int dsp_num,
424 			      irq_handler_t handler);
425 void madera_free_bus_error_irq(struct madera_priv *priv, int dsp_num);
426 
427 int madera_init_dai(struct madera_priv *priv, int dai);
428 
429 int madera_set_output_mode(struct snd_soc_component *component, int output,
430 			   bool differential);
431 
432 /* Following functions are for use by machine drivers */
433 static inline int madera_register_notifier(struct snd_soc_component *component,
434 					   struct notifier_block *nb)
435 {
436 	struct madera_priv *priv = snd_soc_component_get_drvdata(component);
437 	struct madera *madera = priv->madera;
438 
439 	return blocking_notifier_chain_register(&madera->notifier, nb);
440 }
441 
442 static inline int
443 madera_unregister_notifier(struct snd_soc_component *component,
444 			   struct notifier_block *nb)
445 {
446 	struct madera_priv *priv = snd_soc_component_get_drvdata(component);
447 	struct madera *madera = priv->madera;
448 
449 	return blocking_notifier_chain_unregister(&madera->notifier, nb);
450 }
451 
452 #endif
453