xref: /linux/sound/soc/renesas/rcar/rsnd.h (revision ef19ecf042b448a69ee3bd9b3e35689b0b7892ac)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car
4 //
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 
8 #ifndef RSND_H
9 #define RSND_H
10 
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/io.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/reset.h>
19 #include <linux/sh_dma.h>
20 #include <linux/workqueue.h>
21 #include <sound/soc.h>
22 #include <sound/pcm_params.h>
23 
24 #define RSND_BASE_ADG	0
25 #define RSND_BASE_SSI	1
26 #define RSND_BASE_SSIU	2
27 #define RSND_BASE_SCU	3	// for Gen2/Gen3
28 #define RSND_BASE_SDMC	3	// for Gen4	reuse
29 #define RSND_BASE_MAX	4
30 
31 /*
32  *	pseudo register
33  *
34  * The register address offsets SRU/SCU/SSIU on Gen1/Gen2 are very different.
35  * This driver uses pseudo register in order to hide it.
36  * see gen1/gen2 for detail
37  */
38 enum rsnd_reg {
39 	/* SCU (MIX/CTU/DVC) */
40 	SRC_I_BUSIF_MODE,
41 	SRC_O_BUSIF_MODE,
42 	SRC_ROUTE_MODE0,
43 	SRC_SWRSR,
44 	SRC_SRCIR,
45 	SRC_ADINR,
46 	SRC_IFSCR,
47 	SRC_IFSVR,
48 	SRC_SRCCR,
49 	SRC_CTRL,
50 	SRC_BSDSR,
51 	SRC_BSISR,
52 	SRC_INT_ENABLE0,
53 	SRC_BUSIF_DALIGN,
54 	SRCIN_TIMSEL0,
55 	SRCIN_TIMSEL1,
56 	SRCIN_TIMSEL2,
57 	SRCIN_TIMSEL3,
58 	SRCIN_TIMSEL4,
59 	SRCOUT_TIMSEL0,
60 	SRCOUT_TIMSEL1,
61 	SRCOUT_TIMSEL2,
62 	SRCOUT_TIMSEL3,
63 	SRCOUT_TIMSEL4,
64 	SCU_SYS_STATUS0,
65 	SCU_SYS_STATUS1,
66 	SCU_SYS_INT_EN0,
67 	SCU_SYS_INT_EN1,
68 	CMD_CTRL,
69 	CMD_BUSIF_MODE,
70 	CMD_BUSIF_DALIGN,
71 	CMD_ROUTE_SLCT,
72 	CMDOUT_TIMSEL,
73 	CTU_SWRSR,
74 	CTU_CTUIR,
75 	CTU_ADINR,
76 	CTU_CPMDR,
77 	CTU_SCMDR,
78 	CTU_SV00R,
79 	CTU_SV01R,
80 	CTU_SV02R,
81 	CTU_SV03R,
82 	CTU_SV04R,
83 	CTU_SV05R,
84 	CTU_SV06R,
85 	CTU_SV07R,
86 	CTU_SV10R,
87 	CTU_SV11R,
88 	CTU_SV12R,
89 	CTU_SV13R,
90 	CTU_SV14R,
91 	CTU_SV15R,
92 	CTU_SV16R,
93 	CTU_SV17R,
94 	CTU_SV20R,
95 	CTU_SV21R,
96 	CTU_SV22R,
97 	CTU_SV23R,
98 	CTU_SV24R,
99 	CTU_SV25R,
100 	CTU_SV26R,
101 	CTU_SV27R,
102 	CTU_SV30R,
103 	CTU_SV31R,
104 	CTU_SV32R,
105 	CTU_SV33R,
106 	CTU_SV34R,
107 	CTU_SV35R,
108 	CTU_SV36R,
109 	CTU_SV37R,
110 	MIX_SWRSR,
111 	MIX_MIXIR,
112 	MIX_ADINR,
113 	MIX_MIXMR,
114 	MIX_MVPDR,
115 	MIX_MDBAR,
116 	MIX_MDBBR,
117 	MIX_MDBCR,
118 	MIX_MDBDR,
119 	MIX_MDBER,
120 	DVC_SWRSR,
121 	DVC_DVUIR,
122 	DVC_ADINR,
123 	DVC_DVUCR,
124 	DVC_ZCMCR,
125 	DVC_VOL0R,
126 	DVC_VOL1R,
127 	DVC_VOL2R,
128 	DVC_VOL3R,
129 	DVC_VOL4R,
130 	DVC_VOL5R,
131 	DVC_VOL6R,
132 	DVC_VOL7R,
133 	DVC_DVUER,
134 	DVC_VRCTR,
135 	DVC_VRPDR,
136 	DVC_VRDBR,
137 
138 	/* ADG */
139 	BRRA,
140 	BRRB,
141 	BRGCKR,
142 	DIV_EN,
143 	AUDIO_CLK_SEL0,
144 	AUDIO_CLK_SEL1,
145 	AUDIO_CLK_SEL2,
146 	AUDIO_CLK_SEL3,
147 
148 	/* SSIU */
149 	SSI_MODE,
150 	SSI_MODE0,
151 	SSI_MODE1,
152 	SSI_MODE2,
153 	SSI_MODE3,
154 	SSI_CONTROL,
155 	SSI_CONTROL2,
156 	SSI_CTRL,
157 	SSI_BUSIF0_MODE,
158 	SSI_BUSIF1_MODE,
159 	SSI_BUSIF2_MODE,
160 	SSI_BUSIF3_MODE,
161 	SSI_BUSIF4_MODE,
162 	SSI_BUSIF5_MODE,
163 	SSI_BUSIF6_MODE,
164 	SSI_BUSIF7_MODE,
165 	SSI_BUSIF0_ADINR,
166 	SSI_BUSIF1_ADINR,
167 	SSI_BUSIF2_ADINR,
168 	SSI_BUSIF3_ADINR,
169 	SSI_BUSIF4_ADINR,
170 	SSI_BUSIF5_ADINR,
171 	SSI_BUSIF6_ADINR,
172 	SSI_BUSIF7_ADINR,
173 	SSI_BUSIF0_DALIGN,
174 	SSI_BUSIF1_DALIGN,
175 	SSI_BUSIF2_DALIGN,
176 	SSI_BUSIF3_DALIGN,
177 	SSI_BUSIF4_DALIGN,
178 	SSI_BUSIF5_DALIGN,
179 	SSI_BUSIF6_DALIGN,
180 	SSI_BUSIF7_DALIGN,
181 	SSI_INT_ENABLE,
182 	SSI_SYS_STATUS0,
183 	SSI_SYS_STATUS1,
184 	SSI_SYS_STATUS2,
185 	SSI_SYS_STATUS3,
186 	SSI_SYS_STATUS4,
187 	SSI_SYS_STATUS5,
188 	SSI_SYS_STATUS6,
189 	SSI_SYS_STATUS7,
190 	SSI_SYS_INT_ENABLE0,
191 	SSI_SYS_INT_ENABLE1,
192 	SSI_SYS_INT_ENABLE2,
193 	SSI_SYS_INT_ENABLE3,
194 	SSI_SYS_INT_ENABLE4,
195 	SSI_SYS_INT_ENABLE5,
196 	SSI_SYS_INT_ENABLE6,
197 	SSI_SYS_INT_ENABLE7,
198 	HDMI0_SEL,
199 	HDMI1_SEL,
200 	SSI9_BUSIF0_MODE,
201 	SSI9_BUSIF1_MODE,
202 	SSI9_BUSIF2_MODE,
203 	SSI9_BUSIF3_MODE,
204 	SSI9_BUSIF4_MODE,
205 	SSI9_BUSIF5_MODE,
206 	SSI9_BUSIF6_MODE,
207 	SSI9_BUSIF7_MODE,
208 	SSI9_BUSIF0_ADINR,
209 	SSI9_BUSIF1_ADINR,
210 	SSI9_BUSIF2_ADINR,
211 	SSI9_BUSIF3_ADINR,
212 	SSI9_BUSIF4_ADINR,
213 	SSI9_BUSIF5_ADINR,
214 	SSI9_BUSIF6_ADINR,
215 	SSI9_BUSIF7_ADINR,
216 	SSI9_BUSIF0_DALIGN,
217 	SSI9_BUSIF1_DALIGN,
218 	SSI9_BUSIF2_DALIGN,
219 	SSI9_BUSIF3_DALIGN,
220 	SSI9_BUSIF4_DALIGN,
221 	SSI9_BUSIF5_DALIGN,
222 	SSI9_BUSIF6_DALIGN,
223 	SSI9_BUSIF7_DALIGN,
224 
225 	/* SSI */
226 	SSICR,
227 	SSISR,
228 	SSITDR,
229 	SSIRDR,
230 	SSIWSR,
231 
232 	REG_MAX,
233 };
234 #define SRCIN_TIMSEL(i)		(SRCIN_TIMSEL0 + (i))
235 #define SRCOUT_TIMSEL(i)	(SRCOUT_TIMSEL0 + (i))
236 #define CTU_SVxxR(i, j)		(CTU_SV00R + (i * 8) + (j))
237 #define DVC_VOLxR(i)		(DVC_VOL0R + (i))
238 #define AUDIO_CLK_SEL(i)	(AUDIO_CLK_SEL0 + (i))
239 #define SSI_BUSIF_MODE(i)	(SSI_BUSIF0_MODE + (i))
240 #define SSI_BUSIF_ADINR(i)	(SSI_BUSIF0_ADINR + (i))
241 #define SSI_BUSIF_DALIGN(i)	(SSI_BUSIF0_DALIGN + (i))
242 #define SSI9_BUSIF_MODE(i)	(SSI9_BUSIF0_MODE + (i))
243 #define SSI9_BUSIF_ADINR(i)	(SSI9_BUSIF0_ADINR + (i))
244 #define SSI9_BUSIF_DALIGN(i)	(SSI9_BUSIF0_DALIGN + (i))
245 #define SSI_SYS_STATUS(i)	(SSI_SYS_STATUS0 + (i))
246 #define SSI_SYS_INT_ENABLE(i) (SSI_SYS_INT_ENABLE0 + (i))
247 
248 
249 struct rsnd_priv;
250 struct rsnd_mod;
251 struct rsnd_dai;
252 struct rsnd_dai_stream;
253 
254 /*
255  *	R-Car basic functions
256  */
257 u32 rsnd_mod_read(struct rsnd_mod *mod, enum rsnd_reg reg);
258 void rsnd_mod_write(struct rsnd_mod *mod, enum rsnd_reg reg, u32 data);
259 void rsnd_mod_bset(struct rsnd_mod *mod, enum rsnd_reg reg, u32 mask, u32 data);
260 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io);
261 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io);
262 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod);
263 
264 /*
265  *	R-Car DMA
266  */
267 int rsnd_dma_attach(struct rsnd_dai_stream *io,
268 		    struct rsnd_mod *mod, struct rsnd_mod **dma_mod);
269 int rsnd_dma_probe(struct rsnd_priv *priv);
270 void rsnd_dma_suspend(struct rsnd_priv *priv);
271 void rsnd_dma_resume(struct rsnd_priv *priv);
272 struct dma_chan *rsnd_dma_request_channel(struct device_node *of_node, char *name,
273 					  struct rsnd_mod *mod, char *x);
274 
275 /*
276  *	R-Car sound mod
277  */
278 enum rsnd_mod_type {
279 	RSND_MOD_AUDMAPP,
280 	RSND_MOD_AUDMA,
281 	RSND_MOD_DVC,
282 	RSND_MOD_MIX,
283 	RSND_MOD_CTU,
284 	RSND_MOD_CMD,
285 	RSND_MOD_SRC,
286 	RSND_MOD_SSIM3,		/* SSI multi 3 */
287 	RSND_MOD_SSIM2,		/* SSI multi 2 */
288 	RSND_MOD_SSIM1,		/* SSI multi 1 */
289 	RSND_MOD_SSIP,		/* SSI parent */
290 	RSND_MOD_SSI,
291 	RSND_MOD_SSIU,
292 	RSND_MOD_MAX,
293 };
294 
295 struct rsnd_mod_ops {
296 	char *name;
297 	struct dma_chan* (*dma_req)(struct rsnd_dai_stream *io,
298 				    struct rsnd_mod *mod);
299 	int (*probe)(struct rsnd_mod *mod,
300 		     struct rsnd_dai_stream *io,
301 		     struct rsnd_priv *priv);
302 	int (*remove)(struct rsnd_mod *mod,
303 		      struct rsnd_dai_stream *io,
304 		      struct rsnd_priv *priv);
305 	int (*init)(struct rsnd_mod *mod,
306 		    struct rsnd_dai_stream *io,
307 		    struct rsnd_priv *priv);
308 	int (*quit)(struct rsnd_mod *mod,
309 		    struct rsnd_dai_stream *io,
310 		    struct rsnd_priv *priv);
311 	int (*start)(struct rsnd_mod *mod,
312 		     struct rsnd_dai_stream *io,
313 		     struct rsnd_priv *priv);
314 	int (*stop)(struct rsnd_mod *mod,
315 		    struct rsnd_dai_stream *io,
316 		    struct rsnd_priv *priv);
317 	int (*irq)(struct rsnd_mod *mod,
318 		   struct rsnd_dai_stream *io,
319 		   struct rsnd_priv *priv, int enable);
320 	int (*pcm_new)(struct rsnd_mod *mod,
321 		       struct rsnd_dai_stream *io,
322 		       struct snd_soc_pcm_runtime *rtd);
323 	int (*hw_params)(struct rsnd_mod *mod,
324 			 struct rsnd_dai_stream *io,
325 			 struct snd_pcm_substream *substream,
326 			 struct snd_pcm_hw_params *hw_params);
327 	int (*pointer)(struct rsnd_mod *mod,
328 		       struct rsnd_dai_stream *io,
329 		       snd_pcm_uframes_t *pointer);
330 	int (*fallback)(struct rsnd_mod *mod,
331 			struct rsnd_dai_stream *io,
332 			struct rsnd_priv *priv);
333 	int (*prepare)(struct rsnd_mod *mod,
334 		       struct rsnd_dai_stream *io,
335 		       struct rsnd_priv *priv);
336 	int (*cleanup)(struct rsnd_mod *mod,
337 		       struct rsnd_dai_stream *io,
338 		       struct rsnd_priv *priv);
339 	int (*hw_free)(struct rsnd_mod *mod,
340 		       struct rsnd_dai_stream *io,
341 		       struct snd_pcm_substream *substream);
342 	u32 *(*get_status)(struct rsnd_mod *mod,
343 			   struct rsnd_dai_stream *io,
344 			   enum rsnd_mod_type type);
345 	int (*id)(struct rsnd_mod *mod);
346 	int (*id_sub)(struct rsnd_mod *mod);
347 	int (*id_cmd)(struct rsnd_mod *mod);
348 
349 #ifdef CONFIG_DEBUG_FS
350 	void (*debug_info)(struct seq_file *m,
351 			   struct rsnd_dai_stream *io, struct rsnd_mod *mod);
352 #endif
353 };
354 
355 struct rsnd_dai_stream;
356 struct rsnd_mod {
357 	int id;
358 	enum rsnd_mod_type type;
359 	struct rsnd_mod_ops *ops;
360 	struct rsnd_priv *priv;
361 	struct clk *clk;
362 	struct reset_control *rstc;
363 	u32 status;
364 };
365 /*
366  * status
367  *
368  * 0xH000DCB0
369  *
370  * B	0: init		1: quit
371  * C	0: start	1: stop
372  * D	0: hw_params	1: hw_free
373  *
374  * H is always called (see __rsnd_mod_call)
375  */
376 #define __rsnd_mod_shift_init		4
377 #define __rsnd_mod_shift_quit		4
378 #define __rsnd_mod_shift_start		8
379 #define __rsnd_mod_shift_stop		8
380 #define __rsnd_mod_shift_hw_params	12
381 #define __rsnd_mod_shift_hw_free	12
382 #define __rsnd_mod_shift_probe		28 /* always called */
383 #define __rsnd_mod_shift_remove		28 /* always called */
384 #define __rsnd_mod_shift_irq		28 /* always called */
385 #define __rsnd_mod_shift_pcm_new	28 /* always called */
386 #define __rsnd_mod_shift_fallback	28 /* always called */
387 #define __rsnd_mod_shift_pointer	28 /* always called */
388 #define __rsnd_mod_shift_prepare	28 /* always called */
389 #define __rsnd_mod_shift_cleanup	28 /* always called */
390 
391 #define __rsnd_mod_add_probe		0
392 #define __rsnd_mod_add_remove		0
393 #define __rsnd_mod_add_prepare		0
394 #define __rsnd_mod_add_cleanup		0
395 #define __rsnd_mod_add_init		 1 /* needs protect */
396 #define __rsnd_mod_add_quit		-1 /* needs protect */
397 #define __rsnd_mod_add_start		 1 /* needs protect */
398 #define __rsnd_mod_add_stop		-1 /* needs protect */
399 #define __rsnd_mod_add_hw_params	 1 /* needs protect */
400 #define __rsnd_mod_add_hw_free		-1 /* needs protect */
401 #define __rsnd_mod_add_irq		0
402 #define __rsnd_mod_add_pcm_new		0
403 #define __rsnd_mod_add_fallback		0
404 #define __rsnd_mod_add_pointer		0
405 
406 #define __rsnd_mod_call_probe		0
407 #define __rsnd_mod_call_remove		0
408 #define __rsnd_mod_call_prepare		0
409 #define __rsnd_mod_call_cleanup		0
410 #define __rsnd_mod_call_init		0 /* needs protect */
411 #define __rsnd_mod_call_quit		1 /* needs protect */
412 #define __rsnd_mod_call_start		0 /* needs protect */
413 #define __rsnd_mod_call_stop		1 /* needs protect */
414 #define __rsnd_mod_call_hw_params	0 /* needs protect */
415 #define __rsnd_mod_call_hw_free		1 /* needs protect */
416 #define __rsnd_mod_call_irq		0
417 #define __rsnd_mod_call_pcm_new		0
418 #define __rsnd_mod_call_fallback	0
419 #define __rsnd_mod_call_pointer		0
420 
421 #define rsnd_mod_to_priv(mod)	((mod)->priv)
422 #define rsnd_mod_power_on(mod)	clk_enable((mod)->clk)
423 #define rsnd_mod_power_off(mod)	clk_disable((mod)->clk)
424 #define rsnd_mod_get(ip)	(&(ip)->mod)
425 
426 int rsnd_mod_init(struct rsnd_priv *priv,
427 		  struct rsnd_mod *mod,
428 		  struct rsnd_mod_ops *ops,
429 		  struct clk *clk,
430 		  struct reset_control *rstc,
431 		  enum rsnd_mod_type type,
432 		  int id);
433 void rsnd_mod_quit(struct rsnd_mod *mod);
434 void rsnd_suspend_clk_reset(struct clk *clk, struct reset_control *rstc);
435 void rsnd_resume_clk_reset(struct clk *clk, struct reset_control *rstc);
436 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
437 				  struct rsnd_mod *mod);
438 void rsnd_mod_interrupt(struct rsnd_mod *mod,
439 			void (*callback)(struct rsnd_mod *mod,
440 					 struct rsnd_dai_stream *io));
441 u32 *rsnd_mod_get_status(struct rsnd_mod *mod,
442 			 struct rsnd_dai_stream *io,
443 			 enum rsnd_mod_type type);
444 int rsnd_mod_id(struct rsnd_mod *mod);
445 int rsnd_mod_id_raw(struct rsnd_mod *mod);
446 int rsnd_mod_id_sub(struct rsnd_mod *mod);
447 char *rsnd_mod_name(struct rsnd_mod *mod);
448 struct rsnd_mod *rsnd_mod_next(int *iterator,
449 			       struct rsnd_dai_stream *io,
450 			       enum rsnd_mod_type *array,
451 			       int array_size);
452 #define for_each_rsnd_mod(iterator, pos, io)				\
453 	for (iterator = 0;						\
454 	     (pos = rsnd_mod_next(&iterator, io, NULL, 0)); iterator++)
455 #define for_each_rsnd_mod_arrays(iterator, pos, io, array, size)	\
456 	for (iterator = 0;						\
457 	     (pos = rsnd_mod_next(&iterator, io, array, size)); iterator++)
458 #define for_each_rsnd_mod_array(iterator, pos, io, array)		\
459 	for_each_rsnd_mod_arrays(iterator, pos, io, array, ARRAY_SIZE(array))
460 
461 void rsnd_parse_connect_common(struct rsnd_dai *rdai, char *name,
462 		struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
463 		struct device_node *node,
464 		struct device_node *playback,
465 		struct device_node *capture);
466 int rsnd_node_count(struct rsnd_priv *priv, struct device_node *node, char *name);
467 int rsnd_node_fixed_index(struct device *dev, struct device_node *node, char *name, int idx);
468 
469 int rsnd_channel_normalization(int chan);
470 #define rsnd_runtime_channel_original(io) \
471 	rsnd_runtime_channel_original_with_params(io, NULL)
472 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
473 				struct snd_pcm_hw_params *params);
474 #define rsnd_runtime_channel_after_ctu(io)			\
475 	rsnd_runtime_channel_after_ctu_with_params(io, NULL)
476 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
477 				struct snd_pcm_hw_params *params);
478 #define rsnd_runtime_channel_for_ssi(io) \
479 	rsnd_runtime_channel_for_ssi_with_params(io, NULL)
480 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
481 				 struct snd_pcm_hw_params *params);
482 int rsnd_runtime_is_multi_ssi(struct rsnd_dai_stream *io);
483 int rsnd_runtime_is_tdm(struct rsnd_dai_stream *io);
484 int rsnd_runtime_is_tdm_split(struct rsnd_dai_stream *io);
485 
486 /*
487  * Indexed clock and reset name helpers.
488  *
489  * Historically the rsnd driver has looked up per-instance clocks and
490  * resets using dot-separated names (e.g. "ssi.0", "src.0", "adg.ssi.0").
491  * Newer Renesas SoC bindings (RZ/G3E and later) use hyphen-separated
492  * names ("ssi-0", "src-0", ...) to follow the standard Device Tree
493  * naming convention. These helpers look up the hyphenated name first
494  * and transparently fall back to the dotted name, so a single driver
495  * build supports both conventions.
496  */
497 struct clk *rsnd_devm_clk_get_indexed(struct device *dev,
498 				      const char *base, int index);
499 struct clk *rsnd_devm_clk_get_optional_indexed(struct device *dev,
500 					       const char *base, int index);
501 struct reset_control *
502 rsnd_devm_reset_control_get_optional_indexed(struct device *dev,
503 					     const char *base, int index);
504 
505 /*
506  * DT
507  */
508 struct device_node *rsnd_parse_of_node(struct rsnd_priv *priv, const char *name);
509 
510 #define RSND_NODE_DAI	"rcar_sound,dai"
511 #define RSND_NODE_SSI	"rcar_sound,ssi"
512 #define RSND_NODE_SSIU	"rcar_sound,ssiu"
513 #define RSND_NODE_SRC	"rcar_sound,src"
514 #define RSND_NODE_CTU	"rcar_sound,ctu"
515 #define RSND_NODE_MIX	"rcar_sound,mix"
516 #define RSND_NODE_DVC	"rcar_sound,dvc"
517 
518 /*
519  *	R-Car sound DAI
520  */
521 #define RSND_DAI_NAME_SIZE	16
522 struct rsnd_dai_stream {
523 	char name[RSND_DAI_NAME_SIZE];
524 	struct snd_pcm_substream *substream;
525 	struct rsnd_mod *mod[RSND_MOD_MAX];
526 	struct rsnd_mod *dma;
527 	struct rsnd_dai *rdai;
528 	struct device *dmac_dev; /* for IPMMU */
529 	u32 converted_rate;      /* converted sampling rate */
530 	int converted_chan;      /* converted channels */
531 	u32 parent_ssi_status;
532 	u32 flags;
533 };
534 
535 /* flags */
536 #define RSND_STREAM_HDMI0	(1 << 0) /* for HDMI0 */
537 #define RSND_STREAM_HDMI1	(1 << 1) /* for HDMI1 */
538 #define RSND_STREAM_TDM_SPLIT	(1 << 2) /* for TDM split mode */
539 #define RSND_HW_RULE_ERR	(1 << 3) /* hw_rule error */
540 
541 #define rsnd_io_to_mod(io, i)	((i) < RSND_MOD_MAX ? (io)->mod[(i)] : NULL)
542 #define rsnd_io_to_mod_ssi(io)	rsnd_io_to_mod((io), RSND_MOD_SSI)
543 #define rsnd_io_to_mod_ssiu(io)	rsnd_io_to_mod((io), RSND_MOD_SSIU)
544 #define rsnd_io_to_mod_ssip(io)	rsnd_io_to_mod((io), RSND_MOD_SSIP)
545 #define rsnd_io_to_mod_src(io)	rsnd_io_to_mod((io), RSND_MOD_SRC)
546 #define rsnd_io_to_mod_ctu(io)	rsnd_io_to_mod((io), RSND_MOD_CTU)
547 #define rsnd_io_to_mod_mix(io)	rsnd_io_to_mod((io), RSND_MOD_MIX)
548 #define rsnd_io_to_mod_dvc(io)	rsnd_io_to_mod((io), RSND_MOD_DVC)
549 #define rsnd_io_to_mod_cmd(io)	rsnd_io_to_mod((io), RSND_MOD_CMD)
550 #define rsnd_io_to_rdai(io)	((io)->rdai)
551 #define rsnd_io_to_priv(io)	(rsnd_rdai_to_priv(rsnd_io_to_rdai(io)))
552 #define rsnd_io_is_play(io)	(&rsnd_io_to_rdai(io)->playback == io)
553 #define rsnd_io_to_runtime(io) ((io)->substream ? \
554 				(io)->substream->runtime : NULL)
555 #define rsnd_io_converted_rate(io)	((io)->converted_rate)
556 #define rsnd_io_converted_chan(io)	((io)->converted_chan)
557 int rsnd_io_is_working(struct rsnd_dai_stream *io);
558 
559 struct rsnd_dai {
560 	char name[RSND_DAI_NAME_SIZE];
561 	struct rsnd_dai_stream playback;
562 	struct rsnd_dai_stream capture;
563 	struct rsnd_priv *priv;
564 	struct snd_pcm_hw_constraint_list constraint;
565 	struct of_phandle_args dai_args;
566 
567 	int max_channels;	/* 2ch - 16ch */
568 	int ssi_lane;		/* 1lane - 4lane */
569 	int chan_width;		/* 16/24/32 bit width */
570 
571 	unsigned int clk_master:1;
572 	unsigned int bit_clk_inv:1;
573 	unsigned int frm_clk_inv:1;
574 	unsigned int sys_delay:1;
575 	unsigned int data_alignment:1;
576 };
577 
578 #define rsnd_rdai_nr(priv) ((priv)->rdai_nr)
579 #define rsnd_rdai_is_clk_master(rdai) ((rdai)->clk_master)
580 #define rsnd_rdai_to_priv(rdai) ((rdai)->priv)
581 #define for_each_rsnd_dai(rdai, priv, i)		\
582 	for (i = 0;					\
583 	     (i < rsnd_rdai_nr(priv)) &&		\
584 	     ((rdai) = rsnd_rdai_get(priv, i));		\
585 	     i++)
586 
587 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id);
588 
589 #define rsnd_rdai_channels_set(rdai, max_channels) \
590 	rsnd_rdai_channels_ctrl(rdai, max_channels)
591 #define rsnd_rdai_channels_get(rdai) \
592 	rsnd_rdai_channels_ctrl(rdai, 0)
593 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
594 			    int max_channels);
595 
596 #define rsnd_rdai_ssi_lane_set(rdai, ssi_lane) \
597 	rsnd_rdai_ssi_lane_ctrl(rdai, ssi_lane)
598 #define rsnd_rdai_ssi_lane_get(rdai) \
599 	rsnd_rdai_ssi_lane_ctrl(rdai, 0)
600 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
601 			    int ssi_lane);
602 
603 #define rsnd_rdai_width_set(rdai, width) \
604 	rsnd_rdai_width_ctrl(rdai, width)
605 #define rsnd_rdai_width_get(rdai) \
606 	rsnd_rdai_width_ctrl(rdai, 0)
607 int rsnd_rdai_width_ctrl(struct rsnd_dai *rdai, int width);
608 int rsnd_dai_connect(struct rsnd_mod *mod,
609 		     struct rsnd_dai_stream *io,
610 		     enum rsnd_mod_type type);
611 
612 /*
613  *	R-Car Gen1/Gen2
614  */
615 int rsnd_gen_probe(struct rsnd_priv *priv);
616 void __iomem *rsnd_gen_reg_get(struct rsnd_priv *priv,
617 			       struct rsnd_mod *mod,
618 			       enum rsnd_reg reg);
619 phys_addr_t rsnd_gen_get_phy_addr(struct rsnd_priv *priv, int reg_id);
620 #ifdef CONFIG_DEBUG_FS
621 void __iomem *rsnd_gen_get_base_addr(struct rsnd_priv *priv, int reg_id);
622 #endif
623 
624 /*
625  *	R-Car ADG
626  */
627 int rsnd_adg_clk_query(struct rsnd_priv *priv, unsigned int rate);
628 int rsnd_adg_ssi_clk_stop(struct rsnd_mod *ssi_mod);
629 int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *ssi_mod, unsigned int rate);
630 int rsnd_adg_probe(struct rsnd_priv *priv);
631 void rsnd_adg_remove(struct rsnd_priv *priv);
632 void rsnd_adg_suspend(struct rsnd_priv *priv);
633 void rsnd_adg_resume(struct rsnd_priv *priv);
634 int rsnd_adg_set_src_timesel_gen2(struct rsnd_mod *src_mod,
635 				  struct rsnd_dai_stream *io,
636 				  unsigned int in_rate,
637 				  unsigned int out_rate);
638 int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_mod *cmd_mod,
639 				 struct rsnd_dai_stream *io);
640 #define rsnd_adg_clk_enable(priv)	rsnd_adg_clk_control(priv, 1)
641 #define rsnd_adg_clk_disable(priv)	rsnd_adg_clk_control(priv, 0)
642 int rsnd_adg_clk_control(struct rsnd_priv *priv, int enable);
643 void rsnd_adg_clk_dbg_info(struct rsnd_priv *priv, struct seq_file *m);
644 
645 /*
646  *	R-Car sound priv
647  */
648 struct rsnd_priv {
649 
650 	struct platform_device *pdev;
651 	spinlock_t lock;
652 	unsigned long flags;
653 
654 	/*
655 	 * Flags layout: 0xDCBA
656 	 *
657 	 * A: R-Car generation (Gen1/Gen2/Gen3/Gen4)
658 	 * B: R-Car SoC variant (e.g. SOC_E for E1/E2/E3)
659 	 * C: RZ series generation
660 	 * D: RZ series SoC identifier (e.g. RZG3E)
661 	 *
662 	 * Bits 16+ are used for capability flags.
663 	 */
664 #define RSND_GEN_MASK	(0xF << 0)
665 #define RSND_GEN1	(1 << 0)
666 #define RSND_GEN2	(2 << 0)
667 #define RSND_GEN3	(3 << 0)
668 #define RSND_GEN4	(4 << 0)
669 #define RSND_SOC_MASK	(0xF << 4)  /* nibble B */
670 #define RSND_SOC_E	(1 << 4)    /* E1/E2/E3 */
671 #define RSND_RZ_MASK	(0xF << 8)  /* nibble C */
672 #define RSND_RZ3	(3 << 8)
673 #define RSND_RZ_ID_MASK	(0xF << 12) /* nibble D */
674 #define RSND_RZG3E	(1 << 12)
675 #define RSND_SSIU_BUSIF_STATUS_COUNT_2	BIT(16) /* Only 2 BUSIF error-status register pairs */
676 	/*
677 	 * below value will be filled on rsnd_gen_probe()
678 	 */
679 	void *gen;
680 
681 	/*
682 	 * below value will be filled on rsnd_adg_probe()
683 	 */
684 	void *adg;
685 
686 	/*
687 	 * below value will be filled on rsnd_dma_probe()
688 	 */
689 	void *dma;
690 
691 	/*
692 	 * below value will be filled on rsnd_ssi_probe()
693 	 */
694 	void *ssi;
695 	int ssi_nr;
696 
697 	/*
698 	 * below value will be filled on rsnd_ssiu_probe()
699 	 */
700 	void *ssiu_ctrl;
701 	void *ssiu;
702 	int ssiu_nr;
703 
704 	/*
705 	 * below value will be filled on rsnd_src_probe()
706 	 */
707 	void *src_ctrl;
708 	void *src;
709 	int src_nr;
710 
711 	/*
712 	 * below value will be filled on rsnd_ctu_probe()
713 	 */
714 	void *ctu;
715 	int ctu_nr;
716 
717 	/*
718 	 * below value will be filled on rsnd_mix_probe()
719 	 */
720 	void *mix;
721 	int mix_nr;
722 
723 	/*
724 	 * below value will be filled on rsnd_dvc_probe()
725 	 */
726 	void *dvc;
727 	int dvc_nr;
728 
729 	/*
730 	 * below value will be filled on rsnd_cmd_probe()
731 	 */
732 	void *cmd;
733 	int cmd_nr;
734 
735 	/*
736 	 * below value will be filled on rsnd_dai_probe()
737 	 */
738 	struct snd_soc_dai_driver *daidrv;
739 	struct rsnd_dai *rdai;
740 	int rdai_nr;
741 
742 #define RSND_MAX_COMPONENT 3
743 	int component_dais[RSND_MAX_COMPONENT];
744 };
745 
746 #define rsnd_priv_to_pdev(priv)	((priv)->pdev)
747 #define rsnd_priv_to_dev(priv)	(&(rsnd_priv_to_pdev(priv)->dev))
748 
749 #define rsnd_is_gen1(priv)	(((priv)->flags & RSND_GEN_MASK) == RSND_GEN1)
750 #define rsnd_is_gen2(priv)	(((priv)->flags & RSND_GEN_MASK) == RSND_GEN2)
751 #define rsnd_is_gen3(priv)	(((priv)->flags & RSND_GEN_MASK) == RSND_GEN3)
752 #define rsnd_is_gen4(priv)	(((priv)->flags & RSND_GEN_MASK) == RSND_GEN4)
753 #define rsnd_is_gen3_e3(priv)	(((priv)->flags & \
754 					(RSND_GEN_MASK | RSND_SOC_MASK)) == \
755 					(RSND_GEN3 | RSND_SOC_E))
756 #define rsnd_is_rzg3e(priv) (((priv)->flags & \
757 				(RSND_RZ_MASK | RSND_RZ_ID_MASK)) == \
758 					(RSND_RZ3 | RSND_RZG3E))
759 
760 #define rsnd_flags_has(p, f) ((p)->flags & (f))
761 #define rsnd_flags_set(p, f) ((p)->flags |= (f))
762 #define rsnd_flags_del(p, f) ((p)->flags &= ~(f))
763 
764 /*
765  *	rsnd_kctrl
766  */
767 struct rsnd_kctrl_cfg {
768 	unsigned int max;
769 	unsigned int size;
770 	u32 *val;
771 	const char * const *texts;
772 	int (*accept)(struct rsnd_dai_stream *io);
773 	void (*update)(struct rsnd_dai_stream *io, struct rsnd_mod *mod);
774 	struct rsnd_dai_stream *io;
775 	struct snd_card *card;
776 	struct snd_kcontrol *kctrl;
777 	struct rsnd_mod *mod;
778 };
779 
780 #define RSND_MAX_CHANNELS	8
781 struct rsnd_kctrl_cfg_m {
782 	struct rsnd_kctrl_cfg cfg;
783 	u32 val[RSND_MAX_CHANNELS];
784 };
785 
786 struct rsnd_kctrl_cfg_s {
787 	struct rsnd_kctrl_cfg cfg;
788 	u32 val;
789 };
790 #define rsnd_kctrl_size(x)	((x).cfg.size)
791 #define rsnd_kctrl_max(x)	((x).cfg.max)
792 #define rsnd_kctrl_valm(x, i)	((x).val[i])	/* = (x).cfg.val[i] */
793 #define rsnd_kctrl_vals(x)	((x).val)	/* = (x).cfg.val[0] */
794 
795 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io);
796 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg);
797 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg);
798 int rsnd_kctrl_new(struct rsnd_mod *mod,
799 		   struct rsnd_dai_stream *io,
800 		   struct snd_soc_pcm_runtime *rtd,
801 		   const unsigned char *name,
802 		   int (*accept)(struct rsnd_dai_stream *io),
803 		   void (*update)(struct rsnd_dai_stream *io,
804 				  struct rsnd_mod *mod),
805 		   struct rsnd_kctrl_cfg *cfg,
806 		   const char * const *texts,
807 		   int size,
808 		   u32 max);
809 
810 #define rsnd_kctrl_new_m(mod, io, rtd, name, accept, update, cfg, size, max) \
811 	rsnd_kctrl_new(mod, io, rtd, name, accept, update, rsnd_kctrl_init_m(cfg), \
812 		       NULL, size, max)
813 
814 #define rsnd_kctrl_new_s(mod, io, rtd, name, accept, update, cfg, max)	\
815 	rsnd_kctrl_new(mod, io, rtd, name, accept, update, rsnd_kctrl_init_s(cfg), \
816 		       NULL, 1, max)
817 
818 #define rsnd_kctrl_new_e(mod, io, rtd, name, accept, update, cfg, texts, size) \
819 	rsnd_kctrl_new(mod, io, rtd, name, accept, update, rsnd_kctrl_init_s(cfg), \
820 		       texts, 1, size)
821 
822 extern const char * const volume_ramp_rate[];
823 #define VOLUME_RAMP_MAX_DVC	(0x17 + 1)
824 #define VOLUME_RAMP_MAX_MIX	(0x0a + 1)
825 
826 /*
827  *	R-Car SSI
828  */
829 int rsnd_ssi_probe(struct rsnd_priv *priv);
830 void rsnd_ssi_remove(struct rsnd_priv *priv);
831 void rsnd_ssi_suspend(struct rsnd_priv *priv);
832 void rsnd_ssi_resume(struct rsnd_priv *priv);
833 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id);
834 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io);
835 u32 rsnd_ssi_multi_secondaries_runtime(struct rsnd_dai_stream *io);
836 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod);
837 
838 #define rsnd_ssi_is_pin_sharing(io)	\
839 	__rsnd_ssi_is_pin_sharing(rsnd_io_to_mod_ssi(io))
840 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod);
841 
842 #define rsnd_ssi_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_SSI)
843 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
844 			    struct device_node *playback,
845 			    struct device_node *capture);
846 unsigned int rsnd_ssi_clk_query(struct rsnd_dai *rdai,
847 		       int param1, int param2, int *idx);
848 
849 /*
850  *	R-Car SSIU
851  */
852 int rsnd_ssiu_attach(struct rsnd_dai_stream *io,
853 		     struct rsnd_mod *mod);
854 int rsnd_ssiu_probe(struct rsnd_priv *priv);
855 void rsnd_ssiu_remove(struct rsnd_priv *priv);
856 void rsnd_ssiu_suspend(struct rsnd_priv *priv);
857 void rsnd_ssiu_resume(struct rsnd_priv *priv);
858 void rsnd_parse_connect_ssiu(struct rsnd_dai *rdai,
859 			     struct device_node *playback,
860 			     struct device_node *capture);
861 #define rsnd_ssiu_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_SSIU)
862 bool rsnd_ssiu_busif_err_status_clear(struct rsnd_mod *mod);
863 
864 /*
865  *	R-Car SRC
866  */
867 int rsnd_src_probe(struct rsnd_priv *priv);
868 void rsnd_src_remove(struct rsnd_priv *priv);
869 void rsnd_src_suspend(struct rsnd_priv *priv);
870 void rsnd_src_resume(struct rsnd_priv *priv);
871 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id);
872 
873 #define rsnd_src_get_in_rate(priv, io) rsnd_src_get_rate(priv, io, 1)
874 #define rsnd_src_get_out_rate(priv, io) rsnd_src_get_rate(priv, io, 0)
875 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
876 			       struct rsnd_dai_stream *io,
877 			       int is_in);
878 
879 #define rsnd_src_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_SRC)
880 #define rsnd_parse_connect_src(rdai, playback, capture)			\
881 	rsnd_parse_connect_common(rdai, "src", rsnd_src_mod_get,	\
882 				  rsnd_src_of_node(rsnd_rdai_to_priv(rdai)), \
883 						   playback, capture)
884 
885 /*
886  *	R-Car CTU
887  */
888 int rsnd_ctu_probe(struct rsnd_priv *priv);
889 void rsnd_ctu_remove(struct rsnd_priv *priv);
890 void rsnd_ctu_suspend(struct rsnd_priv *priv);
891 void rsnd_ctu_resume(struct rsnd_priv *priv);
892 struct rsnd_mod *rsnd_ctu_mod_get(struct rsnd_priv *priv, int id);
893 #define rsnd_ctu_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_CTU)
894 #define rsnd_parse_connect_ctu(rdai, playback, capture)			\
895 	rsnd_parse_connect_common(rdai, "ctu", rsnd_ctu_mod_get,	\
896 				  rsnd_ctu_of_node(rsnd_rdai_to_priv(rdai)), \
897 						   playback, capture)
898 
899 /*
900  *	R-Car MIX
901  */
902 int rsnd_mix_probe(struct rsnd_priv *priv);
903 void rsnd_mix_remove(struct rsnd_priv *priv);
904 void rsnd_mix_suspend(struct rsnd_priv *priv);
905 void rsnd_mix_resume(struct rsnd_priv *priv);
906 struct rsnd_mod *rsnd_mix_mod_get(struct rsnd_priv *priv, int id);
907 #define rsnd_mix_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_MIX)
908 #define rsnd_parse_connect_mix(rdai, playback, capture)			\
909 	rsnd_parse_connect_common(rdai, "mix", rsnd_mix_mod_get,	\
910 				  rsnd_mix_of_node(rsnd_rdai_to_priv(rdai)), \
911 						   playback, capture)
912 
913 /*
914  *	R-Car DVC
915  */
916 int rsnd_dvc_probe(struct rsnd_priv *priv);
917 void rsnd_dvc_remove(struct rsnd_priv *priv);
918 void rsnd_dvc_suspend(struct rsnd_priv *priv);
919 void rsnd_dvc_resume(struct rsnd_priv *priv);
920 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id);
921 #define rsnd_dvc_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_DVC)
922 #define rsnd_parse_connect_dvc(rdai, playback, capture)			\
923 	rsnd_parse_connect_common(rdai, "dvc", rsnd_dvc_mod_get,	\
924 				  rsnd_dvc_of_node(rsnd_rdai_to_priv(rdai)), \
925 						   playback, capture)
926 
927 /*
928  *	R-Car CMD
929  */
930 int rsnd_cmd_probe(struct rsnd_priv *priv);
931 void rsnd_cmd_remove(struct rsnd_priv *priv);
932 int rsnd_cmd_attach(struct rsnd_dai_stream *io, int id);
933 
934 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type);
935 
936 /*
937  * If you don't need interrupt status debug message,
938  * define RSND_DEBUG_NO_IRQ_STATUS as 1 on top of src.c/ssi.c
939  *
940  * #define RSND_DEBUG_NO_IRQ_STATUS 1
941  */
942 #define rsnd_print_irq_status(dev, param...) do {	\
943 	if (!IS_BUILTIN(RSND_DEBUG_NO_IRQ_STATUS))	\
944 		dev_info(dev, param);			\
945 } while (0)
946 
947 #ifdef CONFIG_DEBUG_FS
948 int rsnd_debugfs_probe(struct snd_soc_component *component);
949 void rsnd_debugfs_reg_show(struct seq_file *m, phys_addr_t _addr,
950 			   void __iomem *base, int offset, int size);
951 void rsnd_debugfs_mod_reg_show(struct seq_file *m, struct rsnd_mod *mod,
952 			       int reg_id, int offset, int size);
953 
954 #else
955 #define rsnd_debugfs_probe  NULL
956 #endif
957 
958 #endif /* RSND_H */
959