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