xref: /openbmc/linux/sound/soc/sh/rcar/ssi.c (revision 36db6e8484ed455bbb320d89a119378897ae991c)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car SSIU/SSI support
4 //
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // Based on fsi.c
9 // Kuninori Morimoto <morimoto.kuninori@renesas.com>
10 
11 /*
12  * you can enable below define if you don't need
13  * SSI interrupt status debug message when debugging
14  * see rsnd_print_irq_status()
15  *
16  * #define RSND_DEBUG_NO_IRQ_STATUS 1
17  */
18 
19 #include <sound/simple_card_utils.h>
20 #include <linux/delay.h>
21 #include "rsnd.h"
22 #define RSND_SSI_NAME_SIZE 16
23 
24 /*
25  * SSICR
26  */
27 #define	FORCE		(1u << 31)	/* Fixed */
28 #define	DMEN		(1u << 28)	/* DMA Enable */
29 #define	UIEN		(1u << 27)	/* Underflow Interrupt Enable */
30 #define	OIEN		(1u << 26)	/* Overflow Interrupt Enable */
31 #define	IIEN		(1u << 25)	/* Idle Mode Interrupt Enable */
32 #define	DIEN		(1u << 24)	/* Data Interrupt Enable */
33 #define	CHNL_4		(1u << 22)	/* Channels */
34 #define	CHNL_6		(2u << 22)	/* Channels */
35 #define	CHNL_8		(3u << 22)	/* Channels */
36 #define DWL_MASK	(7u << 19)	/* Data Word Length mask */
37 #define	DWL_8		(0u << 19)	/* Data Word Length */
38 #define	DWL_16		(1u << 19)	/* Data Word Length */
39 #define	DWL_18		(2u << 19)	/* Data Word Length */
40 #define	DWL_20		(3u << 19)	/* Data Word Length */
41 #define	DWL_22		(4u << 19)	/* Data Word Length */
42 #define	DWL_24		(5u << 19)	/* Data Word Length */
43 #define	DWL_32		(6u << 19)	/* Data Word Length */
44 
45 /*
46  * System word length
47  */
48 #define	SWL_16		(1 << 16)	/* R/W System Word Length */
49 #define	SWL_24		(2 << 16)	/* R/W System Word Length */
50 #define	SWL_32		(3 << 16)	/* R/W System Word Length */
51 
52 #define	SCKD		(1 << 15)	/* Serial Bit Clock Direction */
53 #define	SWSD		(1 << 14)	/* Serial WS Direction */
54 #define	SCKP		(1 << 13)	/* Serial Bit Clock Polarity */
55 #define	SWSP		(1 << 12)	/* Serial WS Polarity */
56 #define	SDTA		(1 << 10)	/* Serial Data Alignment */
57 #define	PDTA		(1 <<  9)	/* Parallel Data Alignment */
58 #define	DEL		(1 <<  8)	/* Serial Data Delay */
59 #define	CKDV(v)		(v <<  4)	/* Serial Clock Division Ratio */
60 #define	TRMD		(1 <<  1)	/* Transmit/Receive Mode Select */
61 #define	EN		(1 <<  0)	/* SSI Module Enable */
62 
63 /*
64  * SSISR
65  */
66 #define	UIRQ		(1 << 27)	/* Underflow Error Interrupt Status */
67 #define	OIRQ		(1 << 26)	/* Overflow Error Interrupt Status */
68 #define	IIRQ		(1 << 25)	/* Idle Mode Interrupt Status */
69 #define	DIRQ		(1 << 24)	/* Data Interrupt Status Flag */
70 
71 /*
72  * SSIWSR
73  */
74 #define CONT		(1 << 8)	/* WS Continue Function */
75 #define WS_MODE		(1 << 0)	/* WS Mode */
76 
77 #define SSI_NAME "ssi"
78 
79 struct rsnd_ssi {
80 	struct rsnd_mod mod;
81 
82 	u32 flags;
83 	u32 cr_own;
84 	u32 cr_clk;
85 	u32 cr_mode;
86 	u32 cr_en;
87 	u32 wsr;
88 	int chan;
89 	int rate;
90 	int irq;
91 	unsigned int usrcnt;
92 
93 	/* for PIO */
94 	int byte_pos;
95 	int byte_per_period;
96 	int next_period_byte;
97 };
98 
99 /* flags */
100 #define RSND_SSI_CLK_PIN_SHARE		(1 << 0)
101 #define RSND_SSI_NO_BUSIF		(1 << 1) /* SSI+DMA without BUSIF */
102 #define RSND_SSI_PROBED			(1 << 2)
103 
104 #define for_each_rsnd_ssi(pos, priv, i)					\
105 	for (i = 0;							\
106 	     (i < rsnd_ssi_nr(priv)) &&					\
107 		((pos) = ((struct rsnd_ssi *)(priv)->ssi + i));		\
108 	     i++)
109 
110 #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
111 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
112 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
113 #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
114 #define rsnd_ssi_is_multi_secondary(mod, io)				\
115 	(rsnd_ssi_multi_secondaries(io) & (1 << rsnd_mod_id(mod)))
116 #define rsnd_ssi_is_run_mods(mod, io) \
117 	(rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
118 #define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(mod))
119 
rsnd_ssi_use_busif(struct rsnd_dai_stream * io)120 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
121 {
122 	struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
123 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
124 	int use_busif = 0;
125 
126 	if (!rsnd_ssi_is_dma_mode(mod))
127 		return 0;
128 
129 	if (!(rsnd_flags_has(ssi, RSND_SSI_NO_BUSIF)))
130 		use_busif = 1;
131 	if (rsnd_io_to_mod_src(io))
132 		use_busif = 1;
133 
134 	return use_busif;
135 }
136 
rsnd_ssi_status_clear(struct rsnd_mod * mod)137 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
138 {
139 	rsnd_mod_write(mod, SSISR, 0);
140 }
141 
rsnd_ssi_status_get(struct rsnd_mod * mod)142 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
143 {
144 	return rsnd_mod_read(mod, SSISR);
145 }
146 
rsnd_ssi_status_check(struct rsnd_mod * mod,u32 bit)147 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
148 				  u32 bit)
149 {
150 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
151 	struct device *dev = rsnd_priv_to_dev(priv);
152 	u32 status;
153 	int i;
154 
155 	for (i = 0; i < 1024; i++) {
156 		status = rsnd_ssi_status_get(mod);
157 		if (status & bit)
158 			return;
159 
160 		udelay(5);
161 	}
162 
163 	dev_warn(dev, "%s status check failed\n", rsnd_mod_name(mod));
164 }
165 
rsnd_ssi_multi_secondaries(struct rsnd_dai_stream * io)166 static u32 rsnd_ssi_multi_secondaries(struct rsnd_dai_stream *io)
167 {
168 	static const enum rsnd_mod_type types[] = {
169 		RSND_MOD_SSIM1,
170 		RSND_MOD_SSIM2,
171 		RSND_MOD_SSIM3,
172 	};
173 	int i, mask;
174 
175 	mask = 0;
176 	for (i = 0; i < ARRAY_SIZE(types); i++) {
177 		struct rsnd_mod *mod = rsnd_io_to_mod(io, types[i]);
178 
179 		if (!mod)
180 			continue;
181 
182 		mask |= 1 << rsnd_mod_id(mod);
183 	}
184 
185 	return mask;
186 }
187 
rsnd_ssi_run_mods(struct rsnd_dai_stream * io)188 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
189 {
190 	struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
191 	struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
192 	u32 mods;
193 
194 	mods = rsnd_ssi_multi_secondaries_runtime(io) |
195 		1 << rsnd_mod_id(ssi_mod);
196 
197 	if (ssi_parent_mod)
198 		mods |= 1 << rsnd_mod_id(ssi_parent_mod);
199 
200 	return mods;
201 }
202 
rsnd_ssi_multi_secondaries_runtime(struct rsnd_dai_stream * io)203 u32 rsnd_ssi_multi_secondaries_runtime(struct rsnd_dai_stream *io)
204 {
205 	if (rsnd_runtime_is_multi_ssi(io))
206 		return rsnd_ssi_multi_secondaries(io);
207 
208 	return 0;
209 }
210 
rsnd_rdai_width_to_swl(struct rsnd_dai * rdai)211 static u32 rsnd_rdai_width_to_swl(struct rsnd_dai *rdai)
212 {
213 	struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
214 	struct device *dev = rsnd_priv_to_dev(priv);
215 	int width = rsnd_rdai_width_get(rdai);
216 
217 	switch (width) {
218 	case 32: return SWL_32;
219 	case 24: return SWL_24;
220 	case 16: return SWL_16;
221 	}
222 
223 	dev_err(dev, "unsupported slot width value: %d\n", width);
224 	return 0;
225 }
226 
rsnd_ssi_clk_query(struct rsnd_dai * rdai,int param1,int param2,int * idx)227 unsigned int rsnd_ssi_clk_query(struct rsnd_dai *rdai,
228 		       int param1, int param2, int *idx)
229 {
230 	struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
231 	static const int ssi_clk_mul_table[] = {
232 		1, 2, 4, 8, 16, 6, 12,
233 	};
234 	int j, ret;
235 	unsigned int main_rate;
236 	int width = rsnd_rdai_width_get(rdai);
237 
238 	for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
239 
240 		/*
241 		 * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
242 		 * with it is not allowed. (SSIWSR.WS_MODE with
243 		 * SSICR.CKDV = 000 is not allowed either).
244 		 * Skip it. See SSICR.CKDV
245 		 */
246 		if (j == 0)
247 			continue;
248 
249 		main_rate = width * param1 * param2 * ssi_clk_mul_table[j];
250 
251 		ret = rsnd_adg_clk_query(priv, main_rate);
252 		if (ret < 0)
253 			continue;
254 
255 		if (idx)
256 			*idx = j;
257 
258 		return main_rate;
259 	}
260 
261 	return 0;
262 }
263 
rsnd_ssi_master_clk_start(struct rsnd_mod * mod,struct rsnd_dai_stream * io)264 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
265 				     struct rsnd_dai_stream *io)
266 {
267 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
268 	struct device *dev = rsnd_priv_to_dev(priv);
269 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
270 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
271 	int chan = rsnd_runtime_channel_for_ssi(io);
272 	int idx, ret;
273 	unsigned int main_rate;
274 	unsigned int rate = rsnd_io_is_play(io) ?
275 		rsnd_src_get_out_rate(priv, io) :
276 		rsnd_src_get_in_rate(priv, io);
277 
278 	if (!rsnd_rdai_is_clk_master(rdai))
279 		return 0;
280 
281 	if (!rsnd_ssi_can_output_clk(mod))
282 		return 0;
283 
284 	if (rsnd_ssi_is_multi_secondary(mod, io))
285 		return 0;
286 
287 	if (rsnd_runtime_is_tdm_split(io))
288 		chan = rsnd_io_converted_chan(io);
289 
290 	chan = rsnd_channel_normalization(chan);
291 
292 	if (ssi->usrcnt > 0) {
293 		if (ssi->rate != rate) {
294 			dev_err(dev, "SSI parent/child should use same rate\n");
295 			return -EINVAL;
296 		}
297 
298 		if (ssi->chan != chan) {
299 			dev_err(dev, "SSI parent/child should use same chan\n");
300 			return -EINVAL;
301 		}
302 
303 		return 0;
304 	}
305 
306 	ret = -EIO;
307 	main_rate = rsnd_ssi_clk_query(rdai, rate, chan, &idx);
308 	if (!main_rate)
309 		goto rate_err;
310 
311 	ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
312 	if (ret < 0)
313 		goto rate_err;
314 
315 	/*
316 	 * SSI clock will be output contiguously
317 	 * by below settings.
318 	 * This means, rsnd_ssi_master_clk_start()
319 	 * and rsnd_ssi_register_setup() are necessary
320 	 * for SSI parent
321 	 *
322 	 * SSICR  : FORCE, SCKD, SWSD
323 	 * SSIWSR : CONT
324 	 */
325 	ssi->cr_clk = FORCE | rsnd_rdai_width_to_swl(rdai) |
326 			SCKD | SWSD | CKDV(idx);
327 	ssi->wsr = CONT;
328 	ssi->rate = rate;
329 	ssi->chan = chan;
330 
331 	dev_dbg(dev, "%s outputs %d chan %u Hz\n",
332 		rsnd_mod_name(mod), chan, rate);
333 
334 	return 0;
335 
336 rate_err:
337 	dev_err(dev, "unsupported clock rate (%d)\n", rate);
338 
339 	return ret;
340 }
341 
rsnd_ssi_master_clk_stop(struct rsnd_mod * mod,struct rsnd_dai_stream * io)342 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
343 				     struct rsnd_dai_stream *io)
344 {
345 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
346 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
347 
348 	if (!rsnd_rdai_is_clk_master(rdai))
349 		return;
350 
351 	if (!rsnd_ssi_can_output_clk(mod))
352 		return;
353 
354 	if (ssi->usrcnt > 1)
355 		return;
356 
357 	ssi->cr_clk	= 0;
358 	ssi->rate	= 0;
359 	ssi->chan	= 0;
360 
361 	rsnd_adg_ssi_clk_stop(mod);
362 }
363 
rsnd_ssi_config_init(struct rsnd_mod * mod,struct rsnd_dai_stream * io)364 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
365 				struct rsnd_dai_stream *io)
366 {
367 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
368 	struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
369 	struct device *dev = rsnd_priv_to_dev(priv);
370 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
371 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
372 	u32 cr_own	= ssi->cr_own;
373 	u32 cr_mode	= ssi->cr_mode;
374 	u32 wsr		= ssi->wsr;
375 	int width;
376 	int is_tdm, is_tdm_split;
377 
378 	is_tdm		= rsnd_runtime_is_tdm(io);
379 	is_tdm_split	= rsnd_runtime_is_tdm_split(io);
380 
381 	if (is_tdm)
382 		dev_dbg(dev, "TDM mode\n");
383 	if (is_tdm_split)
384 		dev_dbg(dev, "TDM Split mode\n");
385 
386 	cr_own |= FORCE | rsnd_rdai_width_to_swl(rdai);
387 
388 	if (rdai->bit_clk_inv)
389 		cr_own |= SCKP;
390 	if (rdai->frm_clk_inv && !is_tdm)
391 		cr_own |= SWSP;
392 	if (rdai->data_alignment)
393 		cr_own |= SDTA;
394 	if (rdai->sys_delay)
395 		cr_own |= DEL;
396 
397 	/*
398 	 * TDM Mode
399 	 * see
400 	 *	rsnd_ssiu_init_gen2()
401 	 */
402 	if (is_tdm || is_tdm_split) {
403 		wsr	|= WS_MODE;
404 		cr_own	|= CHNL_8;
405 	}
406 
407 	/*
408 	 * We shouldn't exchange SWSP after running.
409 	 * This means, parent needs to care it.
410 	 */
411 	if (rsnd_ssi_is_parent(mod, io))
412 		goto init_end;
413 
414 	if (rsnd_io_is_play(io))
415 		cr_own |= TRMD;
416 
417 	cr_own &= ~DWL_MASK;
418 	width = snd_pcm_format_width(runtime->format);
419 	if (is_tdm_split) {
420 		/*
421 		 * The SWL and DWL bits in SSICR should be fixed at 32-bit
422 		 * setting when TDM split mode.
423 		 * see datasheet
424 		 *	Operation :: TDM Format Split Function (TDM Split Mode)
425 		 */
426 		width = 32;
427 	}
428 
429 	switch (width) {
430 	case 8:
431 		cr_own |= DWL_8;
432 		break;
433 	case 16:
434 		cr_own |= DWL_16;
435 		break;
436 	case 24:
437 		cr_own |= DWL_24;
438 		break;
439 	case 32:
440 		cr_own |= DWL_32;
441 		break;
442 	}
443 
444 	if (rsnd_ssi_is_dma_mode(mod)) {
445 		cr_mode = UIEN | OIEN |	/* over/under run */
446 			  DMEN;		/* DMA : enable DMA */
447 	} else {
448 		cr_mode = DIEN;		/* PIO : enable Data interrupt */
449 	}
450 
451 init_end:
452 	ssi->cr_own	= cr_own;
453 	ssi->cr_mode	= cr_mode;
454 	ssi->wsr	= wsr;
455 }
456 
rsnd_ssi_register_setup(struct rsnd_mod * mod)457 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
458 {
459 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
460 
461 	rsnd_mod_write(mod, SSIWSR,	ssi->wsr);
462 	rsnd_mod_write(mod, SSICR,	ssi->cr_own	|
463 					ssi->cr_clk	|
464 					ssi->cr_mode	|
465 					ssi->cr_en);
466 }
467 
468 /*
469  *	SSI mod common functions
470  */
rsnd_ssi_init(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)471 static int rsnd_ssi_init(struct rsnd_mod *mod,
472 			 struct rsnd_dai_stream *io,
473 			 struct rsnd_priv *priv)
474 {
475 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
476 	int ret;
477 
478 	if (!rsnd_ssi_is_run_mods(mod, io))
479 		return 0;
480 
481 	ret = rsnd_ssi_master_clk_start(mod, io);
482 	if (ret < 0)
483 		return ret;
484 
485 	ssi->usrcnt++;
486 
487 	ret = rsnd_mod_power_on(mod);
488 	if (ret < 0)
489 		return ret;
490 
491 	rsnd_ssi_config_init(mod, io);
492 
493 	rsnd_ssi_register_setup(mod);
494 
495 	/* clear error status */
496 	rsnd_ssi_status_clear(mod);
497 
498 	return 0;
499 }
500 
rsnd_ssi_quit(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)501 static int rsnd_ssi_quit(struct rsnd_mod *mod,
502 			 struct rsnd_dai_stream *io,
503 			 struct rsnd_priv *priv)
504 {
505 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
506 	struct device *dev = rsnd_priv_to_dev(priv);
507 
508 	if (!rsnd_ssi_is_run_mods(mod, io))
509 		return 0;
510 
511 	if (!ssi->usrcnt) {
512 		dev_err(dev, "%s usrcnt error\n", rsnd_mod_name(mod));
513 		return -EIO;
514 	}
515 
516 	rsnd_ssi_master_clk_stop(mod, io);
517 
518 	rsnd_mod_power_off(mod);
519 
520 	ssi->usrcnt--;
521 
522 	if (!ssi->usrcnt) {
523 		ssi->cr_own	= 0;
524 		ssi->cr_mode	= 0;
525 		ssi->wsr	= 0;
526 	}
527 
528 	return 0;
529 }
530 
rsnd_ssi_hw_params(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)531 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
532 			      struct rsnd_dai_stream *io,
533 			      struct snd_pcm_substream *substream,
534 			      struct snd_pcm_hw_params *params)
535 {
536 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
537 	unsigned int fmt_width = snd_pcm_format_width(params_format(params));
538 
539 	if (fmt_width > rdai->chan_width) {
540 		struct rsnd_priv *priv = rsnd_io_to_priv(io);
541 		struct device *dev = rsnd_priv_to_dev(priv);
542 
543 		dev_err(dev, "invalid combination of slot-width and format-data-width\n");
544 		return -EINVAL;
545 	}
546 
547 	return 0;
548 }
549 
rsnd_ssi_start(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)550 static int rsnd_ssi_start(struct rsnd_mod *mod,
551 			  struct rsnd_dai_stream *io,
552 			  struct rsnd_priv *priv)
553 {
554 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
555 
556 	if (!rsnd_ssi_is_run_mods(mod, io))
557 		return 0;
558 
559 	/*
560 	 * EN will be set via SSIU :: SSI_CONTROL
561 	 * if Multi channel mode
562 	 */
563 	if (rsnd_ssi_multi_secondaries_runtime(io))
564 		return 0;
565 
566 	/*
567 	 * EN is for data output.
568 	 * SSI parent EN is not needed.
569 	 */
570 	if (rsnd_ssi_is_parent(mod, io))
571 		return 0;
572 
573 	ssi->cr_en = EN;
574 
575 	rsnd_mod_write(mod, SSICR,	ssi->cr_own	|
576 					ssi->cr_clk	|
577 					ssi->cr_mode	|
578 					ssi->cr_en);
579 
580 	return 0;
581 }
582 
rsnd_ssi_stop(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)583 static int rsnd_ssi_stop(struct rsnd_mod *mod,
584 			 struct rsnd_dai_stream *io,
585 			 struct rsnd_priv *priv)
586 {
587 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
588 	u32 cr;
589 
590 	if (!rsnd_ssi_is_run_mods(mod, io))
591 		return 0;
592 
593 	if (rsnd_ssi_is_parent(mod, io))
594 		return 0;
595 
596 	cr  =	ssi->cr_own	|
597 		ssi->cr_clk;
598 
599 	/*
600 	 * disable all IRQ,
601 	 * Playback: Wait all data was sent
602 	 * Capture:  It might not receave data. Do nothing
603 	 */
604 	if (rsnd_io_is_play(io)) {
605 		rsnd_mod_write(mod, SSICR, cr | ssi->cr_en);
606 		rsnd_ssi_status_check(mod, DIRQ);
607 	}
608 
609 	/* In multi-SSI mode, stop is performed by setting ssi0129 in
610 	 * SSI_CONTROL to 0 (in rsnd_ssio_stop_gen2). Do nothing here.
611 	 */
612 	if (rsnd_ssi_multi_secondaries_runtime(io))
613 		return 0;
614 
615 	/*
616 	 * disable SSI,
617 	 * and, wait idle state
618 	 */
619 	rsnd_mod_write(mod, SSICR, cr);	/* disabled all */
620 	rsnd_ssi_status_check(mod, IIRQ);
621 
622 	ssi->cr_en = 0;
623 
624 	return 0;
625 }
626 
rsnd_ssi_irq(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv,int enable)627 static int rsnd_ssi_irq(struct rsnd_mod *mod,
628 			struct rsnd_dai_stream *io,
629 			struct rsnd_priv *priv,
630 			int enable)
631 {
632 	u32 val = 0;
633 	int is_tdm, is_tdm_split;
634 	int id = rsnd_mod_id(mod);
635 
636 	is_tdm		= rsnd_runtime_is_tdm(io);
637 	is_tdm_split	= rsnd_runtime_is_tdm_split(io);
638 
639 	if (rsnd_is_gen1(priv))
640 		return 0;
641 
642 	if (rsnd_ssi_is_parent(mod, io))
643 		return 0;
644 
645 	if (!rsnd_ssi_is_run_mods(mod, io))
646 		return 0;
647 
648 	if (enable)
649 		val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
650 
651 	if (is_tdm || is_tdm_split) {
652 		switch (id) {
653 		case 0:
654 		case 1:
655 		case 2:
656 		case 3:
657 		case 4:
658 		case 9:
659 			val |= 0x0000ff00;
660 			break;
661 		}
662 	}
663 
664 	rsnd_mod_write(mod, SSI_INT_ENABLE, val);
665 
666 	return 0;
667 }
668 
669 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
670 				   struct rsnd_dai_stream *io);
__rsnd_ssi_interrupt(struct rsnd_mod * mod,struct rsnd_dai_stream * io)671 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
672 				 struct rsnd_dai_stream *io)
673 {
674 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
675 	struct device *dev = rsnd_priv_to_dev(priv);
676 	int is_dma = rsnd_ssi_is_dma_mode(mod);
677 	u32 status;
678 	bool elapsed = false;
679 	bool stop = false;
680 
681 	spin_lock(&priv->lock);
682 
683 	/* ignore all cases if not working */
684 	if (!rsnd_io_is_working(io))
685 		goto rsnd_ssi_interrupt_out;
686 
687 	status = rsnd_ssi_status_get(mod);
688 
689 	/* PIO only */
690 	if (!is_dma && (status & DIRQ))
691 		elapsed = rsnd_ssi_pio_interrupt(mod, io);
692 
693 	/* DMA only */
694 	if (is_dma && (status & (UIRQ | OIRQ))) {
695 		rsnd_print_irq_status(dev, "%s err status : 0x%08x\n",
696 				      rsnd_mod_name(mod), status);
697 
698 		stop = true;
699 	}
700 
701 	stop |= rsnd_ssiu_busif_err_status_clear(mod);
702 
703 	rsnd_ssi_status_clear(mod);
704 rsnd_ssi_interrupt_out:
705 	spin_unlock(&priv->lock);
706 
707 	if (elapsed)
708 		rsnd_dai_period_elapsed(io);
709 
710 	if (stop)
711 		snd_pcm_stop_xrun(io->substream);
712 
713 }
714 
rsnd_ssi_interrupt(int irq,void * data)715 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
716 {
717 	struct rsnd_mod *mod = data;
718 
719 	rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
720 
721 	return IRQ_HANDLED;
722 }
723 
rsnd_ssi_get_status(struct rsnd_mod * mod,struct rsnd_dai_stream * io,enum rsnd_mod_type type)724 static u32 *rsnd_ssi_get_status(struct rsnd_mod *mod,
725 				struct rsnd_dai_stream *io,
726 				enum rsnd_mod_type type)
727 {
728 	/*
729 	 * SSIP (= SSI parent) needs to be special, otherwise,
730 	 * 2nd SSI might doesn't start. see also rsnd_mod_call()
731 	 *
732 	 * We can't include parent SSI status on SSI, because we don't know
733 	 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
734 	 * ex) trouble case
735 	 *	Playback: SSI0
736 	 *	Capture : SSI1 (needs SSI0)
737 	 *
738 	 * 1) start Capture  ->	SSI0/SSI1 are started.
739 	 * 2) start Playback ->	SSI0 doesn't work, because it is already
740 	 *			marked as "started" on 1)
741 	 *
742 	 * OTOH, using each mod's status is good for MUX case.
743 	 * It doesn't need to start in 2nd start
744 	 * ex)
745 	 *	IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
746 	 *			    |
747 	 *	IO-1: SRC1 -> CTU2 -+
748 	 *
749 	 * 1) start IO-0 ->	start SSI0
750 	 * 2) start IO-1 ->	SSI0 doesn't need to start, because it is
751 	 *			already started on 1)
752 	 */
753 	if (type == RSND_MOD_SSIP)
754 		return &io->parent_ssi_status;
755 
756 	return rsnd_mod_get_status(mod, io, type);
757 }
758 
759 /*
760  *		SSI PIO
761  */
rsnd_ssi_parent_attach(struct rsnd_mod * mod,struct rsnd_dai_stream * io)762 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
763 				   struct rsnd_dai_stream *io)
764 {
765 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
766 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
767 
768 	if (!__rsnd_ssi_is_pin_sharing(mod))
769 		return;
770 
771 	if (!rsnd_rdai_is_clk_master(rdai))
772 		return;
773 
774 	if (rsnd_ssi_is_multi_secondary(mod, io))
775 		return;
776 
777 	switch (rsnd_mod_id(mod)) {
778 	case 1:
779 	case 2:
780 	case 9:
781 		rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
782 		break;
783 	case 4:
784 		rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
785 		break;
786 	case 8:
787 		rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
788 		break;
789 	}
790 }
791 
rsnd_ssi_pcm_new(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct snd_soc_pcm_runtime * rtd)792 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
793 			    struct rsnd_dai_stream *io,
794 			    struct snd_soc_pcm_runtime *rtd)
795 {
796 	/*
797 	 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
798 	 * and, pcm_new will be called after it.
799 	 * This function reuse pcm_new at this point.
800 	 */
801 	rsnd_ssi_parent_attach(mod, io);
802 
803 	return 0;
804 }
805 
rsnd_ssi_common_probe(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)806 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
807 				 struct rsnd_dai_stream *io,
808 				 struct rsnd_priv *priv)
809 {
810 	struct device *dev = rsnd_priv_to_dev(priv);
811 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
812 	int ret = 0;
813 
814 	/*
815 	 * SSIP/SSIU/IRQ are not needed on
816 	 * SSI Multi secondaries
817 	 */
818 	if (rsnd_ssi_is_multi_secondary(mod, io))
819 		return 0;
820 
821 	/*
822 	 * It can't judge ssi parent at this point
823 	 * see rsnd_ssi_pcm_new()
824 	 */
825 
826 	/*
827 	 * SSI might be called again as PIO fallback
828 	 * It is easy to manual handling for IRQ request/free
829 	 *
830 	 * OTOH, this function might be called many times if platform is
831 	 * using MIX. It needs xxx_attach() many times on xxx_probe().
832 	 * Because of it, we can't control .probe/.remove calling count by
833 	 * mod->status.
834 	 * But it don't need to call request_irq() many times.
835 	 * Let's control it by RSND_SSI_PROBED flag.
836 	 */
837 	if (!rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
838 		ret = request_irq(ssi->irq,
839 				  rsnd_ssi_interrupt,
840 				  IRQF_SHARED,
841 				  dev_name(dev), mod);
842 
843 		rsnd_flags_set(ssi, RSND_SSI_PROBED);
844 	}
845 
846 	return ret;
847 }
848 
rsnd_ssi_common_remove(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)849 static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
850 				  struct rsnd_dai_stream *io,
851 				  struct rsnd_priv *priv)
852 {
853 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
854 	struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
855 
856 	/* Do nothing if non SSI (= SSI parent, multi SSI) mod */
857 	if (pure_ssi_mod != mod)
858 		return 0;
859 
860 	/* PIO will request IRQ again */
861 	if (rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
862 		free_irq(ssi->irq, mod);
863 
864 		rsnd_flags_del(ssi, RSND_SSI_PROBED);
865 	}
866 
867 	return 0;
868 }
869 
870 /*
871  *	SSI PIO functions
872  */
rsnd_ssi_pio_interrupt(struct rsnd_mod * mod,struct rsnd_dai_stream * io)873 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
874 				   struct rsnd_dai_stream *io)
875 {
876 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
877 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
878 	u32 *buf = (u32 *)(runtime->dma_area + ssi->byte_pos);
879 	int shift = 0;
880 	int byte_pos;
881 	bool elapsed = false;
882 
883 	if (snd_pcm_format_width(runtime->format) == 24)
884 		shift = 8;
885 
886 	/*
887 	 * 8/16/32 data can be assesse to TDR/RDR register
888 	 * directly as 32bit data
889 	 * see rsnd_ssi_init()
890 	 */
891 	if (rsnd_io_is_play(io))
892 		rsnd_mod_write(mod, SSITDR, (*buf) << shift);
893 	else
894 		*buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
895 
896 	byte_pos = ssi->byte_pos + sizeof(*buf);
897 
898 	if (byte_pos >= ssi->next_period_byte) {
899 		int period_pos = byte_pos / ssi->byte_per_period;
900 
901 		if (period_pos >= runtime->periods) {
902 			byte_pos = 0;
903 			period_pos = 0;
904 		}
905 
906 		ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period;
907 
908 		elapsed = true;
909 	}
910 
911 	WRITE_ONCE(ssi->byte_pos, byte_pos);
912 
913 	return elapsed;
914 }
915 
rsnd_ssi_pio_init(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)916 static int rsnd_ssi_pio_init(struct rsnd_mod *mod,
917 			     struct rsnd_dai_stream *io,
918 			     struct rsnd_priv *priv)
919 {
920 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
921 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
922 
923 	if (!rsnd_ssi_is_parent(mod, io)) {
924 		ssi->byte_pos		= 0;
925 		ssi->byte_per_period	= runtime->period_size *
926 					  runtime->channels *
927 					  samples_to_bytes(runtime, 1);
928 		ssi->next_period_byte	= ssi->byte_per_period;
929 	}
930 
931 	return rsnd_ssi_init(mod, io, priv);
932 }
933 
rsnd_ssi_pio_pointer(struct rsnd_mod * mod,struct rsnd_dai_stream * io,snd_pcm_uframes_t * pointer)934 static int rsnd_ssi_pio_pointer(struct rsnd_mod *mod,
935 			    struct rsnd_dai_stream *io,
936 			    snd_pcm_uframes_t *pointer)
937 {
938 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
939 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
940 
941 	*pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
942 
943 	return 0;
944 }
945 
946 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
947 	.name		= SSI_NAME,
948 	.probe		= rsnd_ssi_common_probe,
949 	.remove		= rsnd_ssi_common_remove,
950 	.init		= rsnd_ssi_pio_init,
951 	.quit		= rsnd_ssi_quit,
952 	.start		= rsnd_ssi_start,
953 	.stop		= rsnd_ssi_stop,
954 	.irq		= rsnd_ssi_irq,
955 	.pointer	= rsnd_ssi_pio_pointer,
956 	.pcm_new	= rsnd_ssi_pcm_new,
957 	.hw_params	= rsnd_ssi_hw_params,
958 	.get_status	= rsnd_ssi_get_status,
959 };
960 
rsnd_ssi_dma_probe(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)961 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
962 			      struct rsnd_dai_stream *io,
963 			      struct rsnd_priv *priv)
964 {
965 	int ret;
966 
967 	/*
968 	 * SSIP/SSIU/IRQ/DMA are not needed on
969 	 * SSI Multi secondaries
970 	 */
971 	if (rsnd_ssi_is_multi_secondary(mod, io))
972 		return 0;
973 
974 	ret = rsnd_ssi_common_probe(mod, io, priv);
975 	if (ret)
976 		return ret;
977 
978 	/* SSI probe might be called many times in MUX multi path */
979 	ret = rsnd_dma_attach(io, mod, &io->dma);
980 
981 	return ret;
982 }
983 
rsnd_ssi_fallback(struct rsnd_mod * mod,struct rsnd_dai_stream * io,struct rsnd_priv * priv)984 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
985 			     struct rsnd_dai_stream *io,
986 			     struct rsnd_priv *priv)
987 {
988 	struct device *dev = rsnd_priv_to_dev(priv);
989 
990 	/*
991 	 * fallback to PIO
992 	 *
993 	 * SSI .probe might be called again.
994 	 * see
995 	 *	rsnd_rdai_continuance_probe()
996 	 */
997 	mod->ops = &rsnd_ssi_pio_ops;
998 
999 	dev_info(dev, "%s fallback to PIO mode\n", rsnd_mod_name(mod));
1000 
1001 	return 0;
1002 }
1003 
rsnd_ssi_dma_req(struct rsnd_dai_stream * io,struct rsnd_mod * mod)1004 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
1005 					 struct rsnd_mod *mod)
1006 {
1007 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
1008 	int is_play = rsnd_io_is_play(io);
1009 	char *name;
1010 
1011 	/*
1012 	 * It should use "rcar_sound,ssiu" on DT.
1013 	 * But, we need to keep compatibility for old version.
1014 	 *
1015 	 * If it has "rcar_sound.ssiu", it will be used.
1016 	 * If not, "rcar_sound.ssi" will be used.
1017 	 * see
1018 	 *	rsnd_ssiu_dma_req()
1019 	 *	rsnd_dma_of_path()
1020 	 */
1021 
1022 	if (rsnd_ssi_use_busif(io))
1023 		name = is_play ? "rxu" : "txu";
1024 	else
1025 		name = is_play ? "rx" : "tx";
1026 
1027 	return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
1028 					SSI_NAME, mod, name);
1029 }
1030 
1031 #ifdef CONFIG_DEBUG_FS
rsnd_ssi_debug_info(struct seq_file * m,struct rsnd_dai_stream * io,struct rsnd_mod * mod)1032 static void rsnd_ssi_debug_info(struct seq_file *m,
1033 				struct rsnd_dai_stream *io,
1034 				struct rsnd_mod *mod)
1035 {
1036 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
1037 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
1038 
1039 	seq_printf(m, "clock:           %s\n",		rsnd_rdai_is_clk_master(rdai) ?
1040 								"provider" : "consumer");
1041 	seq_printf(m, "bit_clk_inv:     %d\n",		rdai->bit_clk_inv);
1042 	seq_printf(m, "frm_clk_inv:     %d\n",		rdai->frm_clk_inv);
1043 	seq_printf(m, "pin share:       %d\n",		__rsnd_ssi_is_pin_sharing(mod));
1044 	seq_printf(m, "can out clk:     %d\n",		rsnd_ssi_can_output_clk(mod));
1045 	seq_printf(m, "multi secondary: %d\n",		rsnd_ssi_is_multi_secondary(mod, io));
1046 	seq_printf(m, "tdm:             %d, %d\n",	rsnd_runtime_is_tdm(io),
1047 							rsnd_runtime_is_tdm_split(io));
1048 	seq_printf(m, "chan:            %d\n",		ssi->chan);
1049 	seq_printf(m, "user:            %d\n",		ssi->usrcnt);
1050 
1051 	rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SSI,
1052 				  rsnd_mod_id(mod) * 0x40, 0x40);
1053 }
1054 #define DEBUG_INFO .debug_info = rsnd_ssi_debug_info
1055 #else
1056 #define DEBUG_INFO
1057 #endif
1058 
1059 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
1060 	.name		= SSI_NAME,
1061 	.dma_req	= rsnd_ssi_dma_req,
1062 	.probe		= rsnd_ssi_dma_probe,
1063 	.remove		= rsnd_ssi_common_remove,
1064 	.init		= rsnd_ssi_init,
1065 	.quit		= rsnd_ssi_quit,
1066 	.start		= rsnd_ssi_start,
1067 	.stop		= rsnd_ssi_stop,
1068 	.irq		= rsnd_ssi_irq,
1069 	.pcm_new	= rsnd_ssi_pcm_new,
1070 	.fallback	= rsnd_ssi_fallback,
1071 	.hw_params	= rsnd_ssi_hw_params,
1072 	.get_status	= rsnd_ssi_get_status,
1073 	DEBUG_INFO
1074 };
1075 
rsnd_ssi_is_dma_mode(struct rsnd_mod * mod)1076 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
1077 {
1078 	return mod->ops == &rsnd_ssi_dma_ops;
1079 }
1080 
1081 /*
1082  *		ssi mod function
1083  */
rsnd_ssi_connect(struct rsnd_mod * mod,struct rsnd_dai_stream * io)1084 static void rsnd_ssi_connect(struct rsnd_mod *mod,
1085 			     struct rsnd_dai_stream *io)
1086 {
1087 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
1088 	static const enum rsnd_mod_type types[] = {
1089 		RSND_MOD_SSI,
1090 		RSND_MOD_SSIM1,
1091 		RSND_MOD_SSIM2,
1092 		RSND_MOD_SSIM3,
1093 	};
1094 	enum rsnd_mod_type type;
1095 	int i;
1096 
1097 	/* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
1098 	for (i = 0; i < ARRAY_SIZE(types); i++) {
1099 		type = types[i];
1100 		if (!rsnd_io_to_mod(io, type)) {
1101 			rsnd_dai_connect(mod, io, type);
1102 			rsnd_rdai_channels_set(rdai, (i + 1) * 2);
1103 			rsnd_rdai_ssi_lane_set(rdai, (i + 1));
1104 			return;
1105 		}
1106 	}
1107 }
1108 
rsnd_parse_connect_ssi(struct rsnd_dai * rdai,struct device_node * playback,struct device_node * capture)1109 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
1110 			    struct device_node *playback,
1111 			    struct device_node *capture)
1112 {
1113 	struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1114 	struct device *dev = rsnd_priv_to_dev(priv);
1115 	struct device_node *node;
1116 	struct device_node *np;
1117 	int i;
1118 
1119 	node = rsnd_ssi_of_node(priv);
1120 	if (!node)
1121 		return;
1122 
1123 	i = 0;
1124 	for_each_child_of_node(node, np) {
1125 		struct rsnd_mod *mod;
1126 
1127 		i = rsnd_node_fixed_index(dev, np, SSI_NAME, i);
1128 		if (i < 0) {
1129 			of_node_put(np);
1130 			break;
1131 		}
1132 
1133 		mod = rsnd_ssi_mod_get(priv, i);
1134 
1135 		if (np == playback)
1136 			rsnd_ssi_connect(mod, &rdai->playback);
1137 		if (np == capture)
1138 			rsnd_ssi_connect(mod, &rdai->capture);
1139 		i++;
1140 	}
1141 
1142 	of_node_put(node);
1143 }
1144 
rsnd_ssi_mod_get(struct rsnd_priv * priv,int id)1145 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1146 {
1147 	if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1148 		id = 0;
1149 
1150 	return rsnd_mod_get(rsnd_ssi_get(priv, id));
1151 }
1152 
__rsnd_ssi_is_pin_sharing(struct rsnd_mod * mod)1153 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1154 {
1155 	if (!mod)
1156 		return 0;
1157 
1158 	return !!(rsnd_flags_has(rsnd_mod_to_ssi(mod), RSND_SSI_CLK_PIN_SHARE));
1159 }
1160 
rsnd_ssi_probe(struct rsnd_priv * priv)1161 int rsnd_ssi_probe(struct rsnd_priv *priv)
1162 {
1163 	struct device_node *node;
1164 	struct device_node *np;
1165 	struct device *dev = rsnd_priv_to_dev(priv);
1166 	struct rsnd_mod_ops *ops;
1167 	struct clk *clk;
1168 	struct rsnd_ssi *ssi;
1169 	char name[RSND_SSI_NAME_SIZE];
1170 	int i, nr, ret;
1171 
1172 	node = rsnd_ssi_of_node(priv);
1173 	if (!node)
1174 		return -EINVAL;
1175 
1176 	nr = rsnd_node_count(priv, node, SSI_NAME);
1177 	if (!nr) {
1178 		ret = -EINVAL;
1179 		goto rsnd_ssi_probe_done;
1180 	}
1181 
1182 	ssi	= devm_kcalloc(dev, nr, sizeof(*ssi), GFP_KERNEL);
1183 	if (!ssi) {
1184 		ret = -ENOMEM;
1185 		goto rsnd_ssi_probe_done;
1186 	}
1187 
1188 	priv->ssi	= ssi;
1189 	priv->ssi_nr	= nr;
1190 
1191 	i = 0;
1192 	for_each_child_of_node(node, np) {
1193 		if (!of_device_is_available(np))
1194 			goto skip;
1195 
1196 		i = rsnd_node_fixed_index(dev, np, SSI_NAME, i);
1197 		if (i < 0) {
1198 			ret = -EINVAL;
1199 			of_node_put(np);
1200 			goto rsnd_ssi_probe_done;
1201 		}
1202 
1203 		ssi = rsnd_ssi_get(priv, i);
1204 
1205 		snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1206 			 SSI_NAME, i);
1207 
1208 		clk = devm_clk_get(dev, name);
1209 		if (IS_ERR(clk)) {
1210 			ret = PTR_ERR(clk);
1211 			of_node_put(np);
1212 			goto rsnd_ssi_probe_done;
1213 		}
1214 
1215 		if (of_property_read_bool(np, "shared-pin"))
1216 			rsnd_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
1217 
1218 		if (of_property_read_bool(np, "no-busif"))
1219 			rsnd_flags_set(ssi, RSND_SSI_NO_BUSIF);
1220 
1221 		ssi->irq = irq_of_parse_and_map(np, 0);
1222 		if (!ssi->irq) {
1223 			ret = -EINVAL;
1224 			of_node_put(np);
1225 			goto rsnd_ssi_probe_done;
1226 		}
1227 
1228 		if (of_property_read_bool(np, "pio-transfer"))
1229 			ops = &rsnd_ssi_pio_ops;
1230 		else
1231 			ops = &rsnd_ssi_dma_ops;
1232 
1233 		ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
1234 				    RSND_MOD_SSI, i);
1235 		if (ret) {
1236 			of_node_put(np);
1237 			goto rsnd_ssi_probe_done;
1238 		}
1239 skip:
1240 		i++;
1241 	}
1242 
1243 	ret = 0;
1244 
1245 rsnd_ssi_probe_done:
1246 	of_node_put(node);
1247 
1248 	return ret;
1249 }
1250 
rsnd_ssi_remove(struct rsnd_priv * priv)1251 void rsnd_ssi_remove(struct rsnd_priv *priv)
1252 {
1253 	struct rsnd_ssi *ssi;
1254 	int i;
1255 
1256 	for_each_rsnd_ssi(ssi, priv, i) {
1257 		rsnd_mod_quit(rsnd_mod_get(ssi));
1258 	}
1259 }
1260