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