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 int rsnd_dai_connect(struct rsnd_mod *mod,
580 		     struct rsnd_dai_stream *io,
581 		     enum rsnd_mod_type type);
582 
583 /*
584  *	R-Car Gen1/Gen2
585  */
586 int rsnd_gen_probe(struct rsnd_priv *priv);
587 void __iomem *rsnd_gen_reg_get(struct rsnd_priv *priv,
588 			       struct rsnd_mod *mod,
589 			       enum rsnd_reg reg);
590 phys_addr_t rsnd_gen_get_phy_addr(struct rsnd_priv *priv, int reg_id);
591 #ifdef CONFIG_DEBUG_FS
592 void __iomem *rsnd_gen_get_base_addr(struct rsnd_priv *priv, int reg_id);
593 #endif
594 
595 /*
596  *	R-Car ADG
597  */
598 int rsnd_adg_clk_query(struct rsnd_priv *priv, unsigned int rate);
599 int rsnd_adg_ssi_clk_stop(struct rsnd_mod *ssi_mod);
600 int rsnd_adg_ssi_clk_try_start(struct rsnd_mod *ssi_mod, unsigned int rate);
601 int rsnd_adg_probe(struct rsnd_priv *priv);
602 void rsnd_adg_remove(struct rsnd_priv *priv);
603 int rsnd_adg_set_src_timesel_gen2(struct rsnd_mod *src_mod,
604 				  struct rsnd_dai_stream *io,
605 				  unsigned int in_rate,
606 				  unsigned int out_rate);
607 int rsnd_adg_set_cmd_timsel_gen2(struct rsnd_mod *cmd_mod,
608 				 struct rsnd_dai_stream *io);
609 #define rsnd_adg_clk_enable(priv)	rsnd_adg_clk_control(priv, 1)
610 #define rsnd_adg_clk_disable(priv)	rsnd_adg_clk_control(priv, 0)
611 void rsnd_adg_clk_control(struct rsnd_priv *priv, int enable);
612 void rsnd_adg_clk_dbg_info(struct rsnd_priv *priv, struct seq_file *m);
613 
614 /*
615  *	R-Car sound priv
616  */
617 struct rsnd_priv {
618 
619 	struct platform_device *pdev;
620 	spinlock_t lock;
621 	unsigned long flags;
622 #define RSND_GEN_MASK	(0xF << 0)
623 #define RSND_GEN1	(1 << 0)
624 #define RSND_GEN2	(2 << 0)
625 #define RSND_GEN3	(3 << 0)
626 #define RSND_GEN4	(4 << 0)
627 #define RSND_SOC_MASK	(0xFF << 4)
628 #define RSND_SOC_E	(1 << 4) /* E1/E2/E3 */
629 
630 	/*
631 	 * below value will be filled on rsnd_gen_probe()
632 	 */
633 	void *gen;
634 
635 	/*
636 	 * below value will be filled on rsnd_adg_probe()
637 	 */
638 	void *adg;
639 
640 	/*
641 	 * below value will be filled on rsnd_dma_probe()
642 	 */
643 	void *dma;
644 
645 	/*
646 	 * below value will be filled on rsnd_ssi_probe()
647 	 */
648 	void *ssi;
649 	int ssi_nr;
650 
651 	/*
652 	 * below value will be filled on rsnd_ssiu_probe()
653 	 */
654 	void *ssiu;
655 	int ssiu_nr;
656 
657 	/*
658 	 * below value will be filled on rsnd_src_probe()
659 	 */
660 	void *src;
661 	int src_nr;
662 
663 	/*
664 	 * below value will be filled on rsnd_ctu_probe()
665 	 */
666 	void *ctu;
667 	int ctu_nr;
668 
669 	/*
670 	 * below value will be filled on rsnd_mix_probe()
671 	 */
672 	void *mix;
673 	int mix_nr;
674 
675 	/*
676 	 * below value will be filled on rsnd_dvc_probe()
677 	 */
678 	void *dvc;
679 	int dvc_nr;
680 
681 	/*
682 	 * below value will be filled on rsnd_cmd_probe()
683 	 */
684 	void *cmd;
685 	int cmd_nr;
686 
687 	/*
688 	 * below value will be filled on rsnd_dai_probe()
689 	 */
690 	struct snd_soc_dai_driver *daidrv;
691 	struct rsnd_dai *rdai;
692 	int rdai_nr;
693 
694 #define RSND_MAX_COMPONENT 3
695 	int component_dais[RSND_MAX_COMPONENT];
696 };
697 
698 #define rsnd_priv_to_pdev(priv)	((priv)->pdev)
699 #define rsnd_priv_to_dev(priv)	(&(rsnd_priv_to_pdev(priv)->dev))
700 
701 #define rsnd_is_gen1(priv)	(((priv)->flags & RSND_GEN_MASK) == RSND_GEN1)
702 #define rsnd_is_gen2(priv)	(((priv)->flags & RSND_GEN_MASK) == RSND_GEN2)
703 #define rsnd_is_gen3(priv)	(((priv)->flags & RSND_GEN_MASK) == RSND_GEN3)
704 #define rsnd_is_gen4(priv)	(((priv)->flags & RSND_GEN_MASK) == RSND_GEN4)
705 #define rsnd_is_gen3_e3(priv)	(((priv)->flags & \
706 					(RSND_GEN_MASK | RSND_SOC_MASK)) == \
707 					(RSND_GEN3 | RSND_SOC_E))
708 
709 #define rsnd_flags_has(p, f) ((p)->flags & (f))
710 #define rsnd_flags_set(p, f) ((p)->flags |= (f))
711 #define rsnd_flags_del(p, f) ((p)->flags &= ~(f))
712 
713 /*
714  *	rsnd_kctrl
715  */
716 struct rsnd_kctrl_cfg {
717 	unsigned int max;
718 	unsigned int size;
719 	u32 *val;
720 	const char * const *texts;
721 	int (*accept)(struct rsnd_dai_stream *io);
722 	void (*update)(struct rsnd_dai_stream *io, struct rsnd_mod *mod);
723 	struct rsnd_dai_stream *io;
724 	struct snd_card *card;
725 	struct snd_kcontrol *kctrl;
726 	struct rsnd_mod *mod;
727 };
728 
729 #define RSND_MAX_CHANNELS	8
730 struct rsnd_kctrl_cfg_m {
731 	struct rsnd_kctrl_cfg cfg;
732 	u32 val[RSND_MAX_CHANNELS];
733 };
734 
735 struct rsnd_kctrl_cfg_s {
736 	struct rsnd_kctrl_cfg cfg;
737 	u32 val;
738 };
739 #define rsnd_kctrl_size(x)	((x).cfg.size)
740 #define rsnd_kctrl_max(x)	((x).cfg.max)
741 #define rsnd_kctrl_valm(x, i)	((x).val[i])	/* = (x).cfg.val[i] */
742 #define rsnd_kctrl_vals(x)	((x).val)	/* = (x).cfg.val[0] */
743 
744 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io);
745 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io);
746 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg);
747 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg);
748 int rsnd_kctrl_new(struct rsnd_mod *mod,
749 		   struct rsnd_dai_stream *io,
750 		   struct snd_soc_pcm_runtime *rtd,
751 		   const unsigned char *name,
752 		   int (*accept)(struct rsnd_dai_stream *io),
753 		   void (*update)(struct rsnd_dai_stream *io,
754 				  struct rsnd_mod *mod),
755 		   struct rsnd_kctrl_cfg *cfg,
756 		   const char * const *texts,
757 		   int size,
758 		   u32 max);
759 
760 #define rsnd_kctrl_new_m(mod, io, rtd, name, accept, update, cfg, size, max) \
761 	rsnd_kctrl_new(mod, io, rtd, name, accept, update, rsnd_kctrl_init_m(cfg), \
762 		       NULL, size, max)
763 
764 #define rsnd_kctrl_new_s(mod, io, rtd, name, accept, update, cfg, max)	\
765 	rsnd_kctrl_new(mod, io, rtd, name, accept, update, rsnd_kctrl_init_s(cfg), \
766 		       NULL, 1, max)
767 
768 #define rsnd_kctrl_new_e(mod, io, rtd, name, accept, update, cfg, texts, size) \
769 	rsnd_kctrl_new(mod, io, rtd, name, accept, update, rsnd_kctrl_init_s(cfg), \
770 		       texts, 1, size)
771 
772 extern const char * const volume_ramp_rate[];
773 #define VOLUME_RAMP_MAX_DVC	(0x17 + 1)
774 #define VOLUME_RAMP_MAX_MIX	(0x0a + 1)
775 
776 /*
777  *	R-Car SSI
778  */
779 int rsnd_ssi_probe(struct rsnd_priv *priv);
780 void rsnd_ssi_remove(struct rsnd_priv *priv);
781 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id);
782 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io);
783 u32 rsnd_ssi_multi_secondaries_runtime(struct rsnd_dai_stream *io);
784 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod);
785 
786 #define rsnd_ssi_is_pin_sharing(io)	\
787 	__rsnd_ssi_is_pin_sharing(rsnd_io_to_mod_ssi(io))
788 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod);
789 
790 #define rsnd_ssi_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_SSI)
791 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
792 			    struct device_node *playback,
793 			    struct device_node *capture);
794 unsigned int rsnd_ssi_clk_query(struct rsnd_dai *rdai,
795 		       int param1, int param2, int *idx);
796 
797 /*
798  *	R-Car SSIU
799  */
800 int rsnd_ssiu_attach(struct rsnd_dai_stream *io,
801 		     struct rsnd_mod *mod);
802 int rsnd_ssiu_probe(struct rsnd_priv *priv);
803 void rsnd_ssiu_remove(struct rsnd_priv *priv);
804 void rsnd_parse_connect_ssiu(struct rsnd_dai *rdai,
805 			     struct device_node *playback,
806 			     struct device_node *capture);
807 #define rsnd_ssiu_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_SSIU)
808 bool rsnd_ssiu_busif_err_status_clear(struct rsnd_mod *mod);
809 
810 /*
811  *	R-Car SRC
812  */
813 int rsnd_src_probe(struct rsnd_priv *priv);
814 void rsnd_src_remove(struct rsnd_priv *priv);
815 struct rsnd_mod *rsnd_src_mod_get(struct rsnd_priv *priv, int id);
816 
817 #define rsnd_src_get_in_rate(priv, io) rsnd_src_get_rate(priv, io, 1)
818 #define rsnd_src_get_out_rate(priv, io) rsnd_src_get_rate(priv, io, 0)
819 unsigned int rsnd_src_get_rate(struct rsnd_priv *priv,
820 			       struct rsnd_dai_stream *io,
821 			       int is_in);
822 
823 #define rsnd_src_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_SRC)
824 #define rsnd_parse_connect_src(rdai, playback, capture)			\
825 	rsnd_parse_connect_common(rdai, "src", rsnd_src_mod_get,	\
826 				  rsnd_src_of_node(rsnd_rdai_to_priv(rdai)), \
827 						   playback, capture)
828 
829 /*
830  *	R-Car CTU
831  */
832 int rsnd_ctu_probe(struct rsnd_priv *priv);
833 void rsnd_ctu_remove(struct rsnd_priv *priv);
834 struct rsnd_mod *rsnd_ctu_mod_get(struct rsnd_priv *priv, int id);
835 #define rsnd_ctu_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_CTU)
836 #define rsnd_parse_connect_ctu(rdai, playback, capture)			\
837 	rsnd_parse_connect_common(rdai, "ctu", rsnd_ctu_mod_get,	\
838 				  rsnd_ctu_of_node(rsnd_rdai_to_priv(rdai)), \
839 						   playback, capture)
840 
841 /*
842  *	R-Car MIX
843  */
844 int rsnd_mix_probe(struct rsnd_priv *priv);
845 void rsnd_mix_remove(struct rsnd_priv *priv);
846 struct rsnd_mod *rsnd_mix_mod_get(struct rsnd_priv *priv, int id);
847 #define rsnd_mix_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_MIX)
848 #define rsnd_parse_connect_mix(rdai, playback, capture)			\
849 	rsnd_parse_connect_common(rdai, "mix", rsnd_mix_mod_get,	\
850 				  rsnd_mix_of_node(rsnd_rdai_to_priv(rdai)), \
851 						   playback, capture)
852 
853 /*
854  *	R-Car DVC
855  */
856 int rsnd_dvc_probe(struct rsnd_priv *priv);
857 void rsnd_dvc_remove(struct rsnd_priv *priv);
858 struct rsnd_mod *rsnd_dvc_mod_get(struct rsnd_priv *priv, int id);
859 #define rsnd_dvc_of_node(priv) rsnd_parse_of_node(priv, RSND_NODE_DVC)
860 #define rsnd_parse_connect_dvc(rdai, playback, capture)			\
861 	rsnd_parse_connect_common(rdai, "dvc", rsnd_dvc_mod_get,	\
862 				  rsnd_dvc_of_node(rsnd_rdai_to_priv(rdai)), \
863 						   playback, capture)
864 
865 /*
866  *	R-Car CMD
867  */
868 int rsnd_cmd_probe(struct rsnd_priv *priv);
869 void rsnd_cmd_remove(struct rsnd_priv *priv);
870 int rsnd_cmd_attach(struct rsnd_dai_stream *io, int id);
871 
872 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type);
873 
874 /*
875  * If you don't need interrupt status debug message,
876  * define RSND_DEBUG_NO_IRQ_STATUS as 1 on top of src.c/ssi.c
877  *
878  * #define RSND_DEBUG_NO_IRQ_STATUS 1
879  */
880 #define rsnd_print_irq_status(dev, param...) do {	\
881 	if (!IS_BUILTIN(RSND_DEBUG_NO_IRQ_STATUS))	\
882 		dev_info(dev, param);			\
883 } while (0)
884 
885 #ifdef CONFIG_DEBUG_FS
886 int rsnd_debugfs_probe(struct snd_soc_component *component);
887 void rsnd_debugfs_reg_show(struct seq_file *m, phys_addr_t _addr,
888 			   void __iomem *base, int offset, int size);
889 void rsnd_debugfs_mod_reg_show(struct seq_file *m, struct rsnd_mod *mod,
890 			       int reg_id, int offset, int size);
891 
892 #else
893 #define rsnd_debugfs_probe  NULL
894 #endif
895 
896 #endif /* RSND_H */
897