xref: /openbmc/linux/sound/soc/sh/rcar/ssi.c (revision 65844828)
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 
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 
137 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
138 {
139 	rsnd_mod_write(mod, SSISR, 0);
140 }
141 
142 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
143 {
144 	return rsnd_mod_read(mod, SSISR);
145 }
146 
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 
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 
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 
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 
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 
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 
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 	main_rate = rsnd_ssi_clk_query(rdai, rate, chan, &idx);
307 	if (!main_rate) {
308 		dev_err(dev, "unsupported clock rate\n");
309 		return -EIO;
310 	}
311 
312 	ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
313 	if (ret < 0)
314 		return ret;
315 
316 	/*
317 	 * SSI clock will be output contiguously
318 	 * by below settings.
319 	 * This means, rsnd_ssi_master_clk_start()
320 	 * and rsnd_ssi_register_setup() are necessary
321 	 * for SSI parent
322 	 *
323 	 * SSICR  : FORCE, SCKD, SWSD
324 	 * SSIWSR : CONT
325 	 */
326 	ssi->cr_clk = FORCE | rsnd_rdai_width_to_swl(rdai) |
327 			SCKD | SWSD | CKDV(idx);
328 	ssi->wsr = CONT;
329 	ssi->rate = rate;
330 	ssi->chan = chan;
331 
332 	dev_dbg(dev, "%s outputs %d chan %u Hz\n",
333 		rsnd_mod_name(mod), chan, rate);
334 
335 	return 0;
336 }
337 
338 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
339 				     struct rsnd_dai_stream *io)
340 {
341 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
342 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
343 
344 	if (!rsnd_rdai_is_clk_master(rdai))
345 		return;
346 
347 	if (!rsnd_ssi_can_output_clk(mod))
348 		return;
349 
350 	if (ssi->usrcnt > 1)
351 		return;
352 
353 	ssi->cr_clk	= 0;
354 	ssi->rate	= 0;
355 	ssi->chan	= 0;
356 
357 	rsnd_adg_ssi_clk_stop(mod);
358 }
359 
360 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
361 				struct rsnd_dai_stream *io)
362 {
363 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
364 	struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
365 	struct device *dev = rsnd_priv_to_dev(priv);
366 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
367 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
368 	u32 cr_own	= ssi->cr_own;
369 	u32 cr_mode	= ssi->cr_mode;
370 	u32 wsr		= ssi->wsr;
371 	int width;
372 	int is_tdm, is_tdm_split;
373 
374 	is_tdm		= rsnd_runtime_is_tdm(io);
375 	is_tdm_split	= rsnd_runtime_is_tdm_split(io);
376 
377 	if (is_tdm)
378 		dev_dbg(dev, "TDM mode\n");
379 	if (is_tdm_split)
380 		dev_dbg(dev, "TDM Split mode\n");
381 
382 	cr_own |= FORCE | rsnd_rdai_width_to_swl(rdai);
383 
384 	if (rdai->bit_clk_inv)
385 		cr_own |= SCKP;
386 	if (rdai->frm_clk_inv && !is_tdm)
387 		cr_own |= SWSP;
388 	if (rdai->data_alignment)
389 		cr_own |= SDTA;
390 	if (rdai->sys_delay)
391 		cr_own |= DEL;
392 
393 	/*
394 	 * TDM Mode
395 	 * see
396 	 *	rsnd_ssiu_init_gen2()
397 	 */
398 	if (is_tdm || is_tdm_split) {
399 		wsr	|= WS_MODE;
400 		cr_own	|= CHNL_8;
401 	}
402 
403 	/*
404 	 * We shouldn't exchange SWSP after running.
405 	 * This means, parent needs to care it.
406 	 */
407 	if (rsnd_ssi_is_parent(mod, io))
408 		goto init_end;
409 
410 	if (rsnd_io_is_play(io))
411 		cr_own |= TRMD;
412 
413 	cr_own &= ~DWL_MASK;
414 	width = snd_pcm_format_width(runtime->format);
415 	if (is_tdm_split) {
416 		/*
417 		 * The SWL and DWL bits in SSICR should be fixed at 32-bit
418 		 * setting when TDM split mode.
419 		 * see datasheet
420 		 *	Operation :: TDM Format Split Function (TDM Split Mode)
421 		 */
422 		width = 32;
423 	}
424 
425 	switch (width) {
426 	case 8:
427 		cr_own |= DWL_8;
428 		break;
429 	case 16:
430 		cr_own |= DWL_16;
431 		break;
432 	case 24:
433 		cr_own |= DWL_24;
434 		break;
435 	case 32:
436 		cr_own |= DWL_32;
437 		break;
438 	}
439 
440 	if (rsnd_ssi_is_dma_mode(mod)) {
441 		cr_mode = UIEN | OIEN |	/* over/under run */
442 			  DMEN;		/* DMA : enable DMA */
443 	} else {
444 		cr_mode = DIEN;		/* PIO : enable Data interrupt */
445 	}
446 
447 init_end:
448 	ssi->cr_own	= cr_own;
449 	ssi->cr_mode	= cr_mode;
450 	ssi->wsr	= wsr;
451 }
452 
453 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
454 {
455 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
456 
457 	rsnd_mod_write(mod, SSIWSR,	ssi->wsr);
458 	rsnd_mod_write(mod, SSICR,	ssi->cr_own	|
459 					ssi->cr_clk	|
460 					ssi->cr_mode	|
461 					ssi->cr_en);
462 }
463 
464 /*
465  *	SSI mod common functions
466  */
467 static int rsnd_ssi_init(struct rsnd_mod *mod,
468 			 struct rsnd_dai_stream *io,
469 			 struct rsnd_priv *priv)
470 {
471 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
472 	int ret;
473 
474 	if (!rsnd_ssi_is_run_mods(mod, io))
475 		return 0;
476 
477 	ret = rsnd_ssi_master_clk_start(mod, io);
478 	if (ret < 0)
479 		return ret;
480 
481 	ssi->usrcnt++;
482 
483 	ret = rsnd_mod_power_on(mod);
484 	if (ret < 0)
485 		return ret;
486 
487 	rsnd_ssi_config_init(mod, io);
488 
489 	rsnd_ssi_register_setup(mod);
490 
491 	/* clear error status */
492 	rsnd_ssi_status_clear(mod);
493 
494 	return 0;
495 }
496 
497 static int rsnd_ssi_quit(struct rsnd_mod *mod,
498 			 struct rsnd_dai_stream *io,
499 			 struct rsnd_priv *priv)
500 {
501 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
502 	struct device *dev = rsnd_priv_to_dev(priv);
503 
504 	if (!rsnd_ssi_is_run_mods(mod, io))
505 		return 0;
506 
507 	if (!ssi->usrcnt) {
508 		dev_err(dev, "%s usrcnt error\n", rsnd_mod_name(mod));
509 		return -EIO;
510 	}
511 
512 	rsnd_ssi_master_clk_stop(mod, io);
513 
514 	rsnd_mod_power_off(mod);
515 
516 	ssi->usrcnt--;
517 
518 	if (!ssi->usrcnt) {
519 		ssi->cr_own	= 0;
520 		ssi->cr_mode	= 0;
521 		ssi->wsr	= 0;
522 	}
523 
524 	return 0;
525 }
526 
527 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
528 			      struct rsnd_dai_stream *io,
529 			      struct snd_pcm_substream *substream,
530 			      struct snd_pcm_hw_params *params)
531 {
532 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
533 	unsigned int fmt_width = snd_pcm_format_width(params_format(params));
534 
535 	if (fmt_width > rdai->chan_width) {
536 		struct rsnd_priv *priv = rsnd_io_to_priv(io);
537 		struct device *dev = rsnd_priv_to_dev(priv);
538 
539 		dev_err(dev, "invalid combination of slot-width and format-data-width\n");
540 		return -EINVAL;
541 	}
542 
543 	return 0;
544 }
545 
546 static int rsnd_ssi_start(struct rsnd_mod *mod,
547 			  struct rsnd_dai_stream *io,
548 			  struct rsnd_priv *priv)
549 {
550 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
551 
552 	if (!rsnd_ssi_is_run_mods(mod, io))
553 		return 0;
554 
555 	/*
556 	 * EN will be set via SSIU :: SSI_CONTROL
557 	 * if Multi channel mode
558 	 */
559 	if (rsnd_ssi_multi_secondaries_runtime(io))
560 		return 0;
561 
562 	/*
563 	 * EN is for data output.
564 	 * SSI parent EN is not needed.
565 	 */
566 	if (rsnd_ssi_is_parent(mod, io))
567 		return 0;
568 
569 	ssi->cr_en = EN;
570 
571 	rsnd_mod_write(mod, SSICR,	ssi->cr_own	|
572 					ssi->cr_clk	|
573 					ssi->cr_mode	|
574 					ssi->cr_en);
575 
576 	return 0;
577 }
578 
579 static int rsnd_ssi_stop(struct rsnd_mod *mod,
580 			 struct rsnd_dai_stream *io,
581 			 struct rsnd_priv *priv)
582 {
583 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
584 	u32 cr;
585 
586 	if (!rsnd_ssi_is_run_mods(mod, io))
587 		return 0;
588 
589 	if (rsnd_ssi_is_parent(mod, io))
590 		return 0;
591 
592 	cr  =	ssi->cr_own	|
593 		ssi->cr_clk;
594 
595 	/*
596 	 * disable all IRQ,
597 	 * Playback: Wait all data was sent
598 	 * Capture:  It might not receave data. Do nothing
599 	 */
600 	if (rsnd_io_is_play(io)) {
601 		rsnd_mod_write(mod, SSICR, cr | ssi->cr_en);
602 		rsnd_ssi_status_check(mod, DIRQ);
603 	}
604 
605 	/* In multi-SSI mode, stop is performed by setting ssi0129 in
606 	 * SSI_CONTROL to 0 (in rsnd_ssio_stop_gen2). Do nothing here.
607 	 */
608 	if (rsnd_ssi_multi_secondaries_runtime(io))
609 		return 0;
610 
611 	/*
612 	 * disable SSI,
613 	 * and, wait idle state
614 	 */
615 	rsnd_mod_write(mod, SSICR, cr);	/* disabled all */
616 	rsnd_ssi_status_check(mod, IIRQ);
617 
618 	ssi->cr_en = 0;
619 
620 	return 0;
621 }
622 
623 static int rsnd_ssi_irq(struct rsnd_mod *mod,
624 			struct rsnd_dai_stream *io,
625 			struct rsnd_priv *priv,
626 			int enable)
627 {
628 	u32 val = 0;
629 	int is_tdm, is_tdm_split;
630 	int id = rsnd_mod_id(mod);
631 
632 	is_tdm		= rsnd_runtime_is_tdm(io);
633 	is_tdm_split	= rsnd_runtime_is_tdm_split(io);
634 
635 	if (rsnd_is_gen1(priv))
636 		return 0;
637 
638 	if (rsnd_ssi_is_parent(mod, io))
639 		return 0;
640 
641 	if (!rsnd_ssi_is_run_mods(mod, io))
642 		return 0;
643 
644 	if (enable)
645 		val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
646 
647 	if (is_tdm || is_tdm_split) {
648 		switch (id) {
649 		case 0:
650 		case 1:
651 		case 2:
652 		case 3:
653 		case 4:
654 		case 9:
655 			val |= 0x0000ff00;
656 			break;
657 		}
658 	}
659 
660 	rsnd_mod_write(mod, SSI_INT_ENABLE, val);
661 
662 	return 0;
663 }
664 
665 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
666 				   struct rsnd_dai_stream *io);
667 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
668 				 struct rsnd_dai_stream *io)
669 {
670 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
671 	struct device *dev = rsnd_priv_to_dev(priv);
672 	int is_dma = rsnd_ssi_is_dma_mode(mod);
673 	u32 status;
674 	bool elapsed = false;
675 	bool stop = false;
676 
677 	spin_lock(&priv->lock);
678 
679 	/* ignore all cases if not working */
680 	if (!rsnd_io_is_working(io))
681 		goto rsnd_ssi_interrupt_out;
682 
683 	status = rsnd_ssi_status_get(mod);
684 
685 	/* PIO only */
686 	if (!is_dma && (status & DIRQ))
687 		elapsed = rsnd_ssi_pio_interrupt(mod, io);
688 
689 	/* DMA only */
690 	if (is_dma && (status & (UIRQ | OIRQ))) {
691 		rsnd_print_irq_status(dev, "%s err status : 0x%08x\n",
692 				      rsnd_mod_name(mod), status);
693 
694 		stop = true;
695 	}
696 
697 	stop |= rsnd_ssiu_busif_err_status_clear(mod);
698 
699 	rsnd_ssi_status_clear(mod);
700 rsnd_ssi_interrupt_out:
701 	spin_unlock(&priv->lock);
702 
703 	if (elapsed)
704 		rsnd_dai_period_elapsed(io);
705 
706 	if (stop)
707 		snd_pcm_stop_xrun(io->substream);
708 
709 }
710 
711 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
712 {
713 	struct rsnd_mod *mod = data;
714 
715 	rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
716 
717 	return IRQ_HANDLED;
718 }
719 
720 static u32 *rsnd_ssi_get_status(struct rsnd_mod *mod,
721 				struct rsnd_dai_stream *io,
722 				enum rsnd_mod_type type)
723 {
724 	/*
725 	 * SSIP (= SSI parent) needs to be special, otherwise,
726 	 * 2nd SSI might doesn't start. see also rsnd_mod_call()
727 	 *
728 	 * We can't include parent SSI status on SSI, because we don't know
729 	 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
730 	 * ex) trouble case
731 	 *	Playback: SSI0
732 	 *	Capture : SSI1 (needs SSI0)
733 	 *
734 	 * 1) start Capture  ->	SSI0/SSI1 are started.
735 	 * 2) start Playback ->	SSI0 doesn't work, because it is already
736 	 *			marked as "started" on 1)
737 	 *
738 	 * OTOH, using each mod's status is good for MUX case.
739 	 * It doesn't need to start in 2nd start
740 	 * ex)
741 	 *	IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
742 	 *			    |
743 	 *	IO-1: SRC1 -> CTU2 -+
744 	 *
745 	 * 1) start IO-0 ->	start SSI0
746 	 * 2) start IO-1 ->	SSI0 doesn't need to start, because it is
747 	 *			already started on 1)
748 	 */
749 	if (type == RSND_MOD_SSIP)
750 		return &io->parent_ssi_status;
751 
752 	return rsnd_mod_get_status(mod, io, type);
753 }
754 
755 /*
756  *		SSI PIO
757  */
758 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
759 				   struct rsnd_dai_stream *io)
760 {
761 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
762 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
763 
764 	if (!__rsnd_ssi_is_pin_sharing(mod))
765 		return;
766 
767 	if (!rsnd_rdai_is_clk_master(rdai))
768 		return;
769 
770 	if (rsnd_ssi_is_multi_secondary(mod, io))
771 		return;
772 
773 	switch (rsnd_mod_id(mod)) {
774 	case 1:
775 	case 2:
776 	case 9:
777 		rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
778 		break;
779 	case 4:
780 		rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
781 		break;
782 	case 8:
783 		rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
784 		break;
785 	}
786 }
787 
788 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
789 			    struct rsnd_dai_stream *io,
790 			    struct snd_soc_pcm_runtime *rtd)
791 {
792 	/*
793 	 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
794 	 * and, pcm_new will be called after it.
795 	 * This function reuse pcm_new at this point.
796 	 */
797 	rsnd_ssi_parent_attach(mod, io);
798 
799 	return 0;
800 }
801 
802 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
803 				 struct rsnd_dai_stream *io,
804 				 struct rsnd_priv *priv)
805 {
806 	struct device *dev = rsnd_priv_to_dev(priv);
807 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
808 	int ret = 0;
809 
810 	/*
811 	 * SSIP/SSIU/IRQ are not needed on
812 	 * SSI Multi secondaries
813 	 */
814 	if (rsnd_ssi_is_multi_secondary(mod, io))
815 		return 0;
816 
817 	/*
818 	 * It can't judge ssi parent at this point
819 	 * see rsnd_ssi_pcm_new()
820 	 */
821 
822 	/*
823 	 * SSI might be called again as PIO fallback
824 	 * It is easy to manual handling for IRQ request/free
825 	 *
826 	 * OTOH, this function might be called many times if platform is
827 	 * using MIX. It needs xxx_attach() many times on xxx_probe().
828 	 * Because of it, we can't control .probe/.remove calling count by
829 	 * mod->status.
830 	 * But it don't need to call request_irq() many times.
831 	 * Let's control it by RSND_SSI_PROBED flag.
832 	 */
833 	if (!rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
834 		ret = request_irq(ssi->irq,
835 				  rsnd_ssi_interrupt,
836 				  IRQF_SHARED,
837 				  dev_name(dev), mod);
838 
839 		rsnd_flags_set(ssi, RSND_SSI_PROBED);
840 	}
841 
842 	return ret;
843 }
844 
845 static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
846 				  struct rsnd_dai_stream *io,
847 				  struct rsnd_priv *priv)
848 {
849 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
850 	struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
851 
852 	/* Do nothing if non SSI (= SSI parent, multi SSI) mod */
853 	if (pure_ssi_mod != mod)
854 		return 0;
855 
856 	/* PIO will request IRQ again */
857 	if (rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
858 		free_irq(ssi->irq, mod);
859 
860 		rsnd_flags_del(ssi, RSND_SSI_PROBED);
861 	}
862 
863 	return 0;
864 }
865 
866 /*
867  *	SSI PIO functions
868  */
869 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
870 				   struct rsnd_dai_stream *io)
871 {
872 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
873 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
874 	u32 *buf = (u32 *)(runtime->dma_area + ssi->byte_pos);
875 	int shift = 0;
876 	int byte_pos;
877 	bool elapsed = false;
878 
879 	if (snd_pcm_format_width(runtime->format) == 24)
880 		shift = 8;
881 
882 	/*
883 	 * 8/16/32 data can be assesse to TDR/RDR register
884 	 * directly as 32bit data
885 	 * see rsnd_ssi_init()
886 	 */
887 	if (rsnd_io_is_play(io))
888 		rsnd_mod_write(mod, SSITDR, (*buf) << shift);
889 	else
890 		*buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
891 
892 	byte_pos = ssi->byte_pos + sizeof(*buf);
893 
894 	if (byte_pos >= ssi->next_period_byte) {
895 		int period_pos = byte_pos / ssi->byte_per_period;
896 
897 		if (period_pos >= runtime->periods) {
898 			byte_pos = 0;
899 			period_pos = 0;
900 		}
901 
902 		ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period;
903 
904 		elapsed = true;
905 	}
906 
907 	WRITE_ONCE(ssi->byte_pos, byte_pos);
908 
909 	return elapsed;
910 }
911 
912 static int rsnd_ssi_pio_init(struct rsnd_mod *mod,
913 			     struct rsnd_dai_stream *io,
914 			     struct rsnd_priv *priv)
915 {
916 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
917 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
918 
919 	if (!rsnd_ssi_is_parent(mod, io)) {
920 		ssi->byte_pos		= 0;
921 		ssi->byte_per_period	= runtime->period_size *
922 					  runtime->channels *
923 					  samples_to_bytes(runtime, 1);
924 		ssi->next_period_byte	= ssi->byte_per_period;
925 	}
926 
927 	return rsnd_ssi_init(mod, io, priv);
928 }
929 
930 static int rsnd_ssi_pio_pointer(struct rsnd_mod *mod,
931 			    struct rsnd_dai_stream *io,
932 			    snd_pcm_uframes_t *pointer)
933 {
934 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
935 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
936 
937 	*pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
938 
939 	return 0;
940 }
941 
942 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
943 	.name		= SSI_NAME,
944 	.probe		= rsnd_ssi_common_probe,
945 	.remove		= rsnd_ssi_common_remove,
946 	.init		= rsnd_ssi_pio_init,
947 	.quit		= rsnd_ssi_quit,
948 	.start		= rsnd_ssi_start,
949 	.stop		= rsnd_ssi_stop,
950 	.irq		= rsnd_ssi_irq,
951 	.pointer	= rsnd_ssi_pio_pointer,
952 	.pcm_new	= rsnd_ssi_pcm_new,
953 	.hw_params	= rsnd_ssi_hw_params,
954 	.get_status	= rsnd_ssi_get_status,
955 };
956 
957 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
958 			      struct rsnd_dai_stream *io,
959 			      struct rsnd_priv *priv)
960 {
961 	int ret;
962 
963 	/*
964 	 * SSIP/SSIU/IRQ/DMA are not needed on
965 	 * SSI Multi secondaries
966 	 */
967 	if (rsnd_ssi_is_multi_secondary(mod, io))
968 		return 0;
969 
970 	ret = rsnd_ssi_common_probe(mod, io, priv);
971 	if (ret)
972 		return ret;
973 
974 	/* SSI probe might be called many times in MUX multi path */
975 	ret = rsnd_dma_attach(io, mod, &io->dma);
976 
977 	return ret;
978 }
979 
980 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
981 			     struct rsnd_dai_stream *io,
982 			     struct rsnd_priv *priv)
983 {
984 	struct device *dev = rsnd_priv_to_dev(priv);
985 
986 	/*
987 	 * fallback to PIO
988 	 *
989 	 * SSI .probe might be called again.
990 	 * see
991 	 *	rsnd_rdai_continuance_probe()
992 	 */
993 	mod->ops = &rsnd_ssi_pio_ops;
994 
995 	dev_info(dev, "%s fallback to PIO mode\n", rsnd_mod_name(mod));
996 
997 	return 0;
998 }
999 
1000 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
1001 					 struct rsnd_mod *mod)
1002 {
1003 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
1004 	int is_play = rsnd_io_is_play(io);
1005 	char *name;
1006 
1007 	/*
1008 	 * It should use "rcar_sound,ssiu" on DT.
1009 	 * But, we need to keep compatibility for old version.
1010 	 *
1011 	 * If it has "rcar_sound.ssiu", it will be used.
1012 	 * If not, "rcar_sound.ssi" will be used.
1013 	 * see
1014 	 *	rsnd_ssiu_dma_req()
1015 	 *	rsnd_dma_of_path()
1016 	 */
1017 
1018 	if (rsnd_ssi_use_busif(io))
1019 		name = is_play ? "rxu" : "txu";
1020 	else
1021 		name = is_play ? "rx" : "tx";
1022 
1023 	return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
1024 					SSI_NAME, mod, name);
1025 }
1026 
1027 #ifdef CONFIG_DEBUG_FS
1028 static void rsnd_ssi_debug_info(struct seq_file *m,
1029 				struct rsnd_dai_stream *io,
1030 				struct rsnd_mod *mod)
1031 {
1032 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
1033 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
1034 
1035 	seq_printf(m, "clock:           %s\n",		rsnd_rdai_is_clk_master(rdai) ?
1036 								"provider" : "consumer");
1037 	seq_printf(m, "bit_clk_inv:     %d\n",		rdai->bit_clk_inv);
1038 	seq_printf(m, "frm_clk_inv:     %d\n",		rdai->frm_clk_inv);
1039 	seq_printf(m, "pin share:       %d\n",		__rsnd_ssi_is_pin_sharing(mod));
1040 	seq_printf(m, "can out clk:     %d\n",		rsnd_ssi_can_output_clk(mod));
1041 	seq_printf(m, "multi secondary: %d\n",		rsnd_ssi_is_multi_secondary(mod, io));
1042 	seq_printf(m, "tdm:             %d, %d\n",	rsnd_runtime_is_tdm(io),
1043 							rsnd_runtime_is_tdm_split(io));
1044 	seq_printf(m, "chan:            %d\n",		ssi->chan);
1045 	seq_printf(m, "user:            %d\n",		ssi->usrcnt);
1046 
1047 	rsnd_debugfs_mod_reg_show(m, mod, RSND_GEN2_SSI,
1048 				  rsnd_mod_id(mod) * 0x40, 0x40);
1049 }
1050 #define DEBUG_INFO .debug_info = rsnd_ssi_debug_info
1051 #else
1052 #define DEBUG_INFO
1053 #endif
1054 
1055 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
1056 	.name		= SSI_NAME,
1057 	.dma_req	= rsnd_ssi_dma_req,
1058 	.probe		= rsnd_ssi_dma_probe,
1059 	.remove		= rsnd_ssi_common_remove,
1060 	.init		= rsnd_ssi_init,
1061 	.quit		= rsnd_ssi_quit,
1062 	.start		= rsnd_ssi_start,
1063 	.stop		= rsnd_ssi_stop,
1064 	.irq		= rsnd_ssi_irq,
1065 	.pcm_new	= rsnd_ssi_pcm_new,
1066 	.fallback	= rsnd_ssi_fallback,
1067 	.hw_params	= rsnd_ssi_hw_params,
1068 	.get_status	= rsnd_ssi_get_status,
1069 	DEBUG_INFO
1070 };
1071 
1072 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
1073 {
1074 	return mod->ops == &rsnd_ssi_dma_ops;
1075 }
1076 
1077 /*
1078  *		ssi mod function
1079  */
1080 static void rsnd_ssi_connect(struct rsnd_mod *mod,
1081 			     struct rsnd_dai_stream *io)
1082 {
1083 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
1084 	static const enum rsnd_mod_type types[] = {
1085 		RSND_MOD_SSI,
1086 		RSND_MOD_SSIM1,
1087 		RSND_MOD_SSIM2,
1088 		RSND_MOD_SSIM3,
1089 	};
1090 	enum rsnd_mod_type type;
1091 	int i;
1092 
1093 	/* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
1094 	for (i = 0; i < ARRAY_SIZE(types); i++) {
1095 		type = types[i];
1096 		if (!rsnd_io_to_mod(io, type)) {
1097 			rsnd_dai_connect(mod, io, type);
1098 			rsnd_rdai_channels_set(rdai, (i + 1) * 2);
1099 			rsnd_rdai_ssi_lane_set(rdai, (i + 1));
1100 			return;
1101 		}
1102 	}
1103 }
1104 
1105 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
1106 			    struct device_node *playback,
1107 			    struct device_node *capture)
1108 {
1109 	struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
1110 	struct device *dev = rsnd_priv_to_dev(priv);
1111 	struct device_node *node;
1112 	struct device_node *np;
1113 	int i;
1114 
1115 	node = rsnd_ssi_of_node(priv);
1116 	if (!node)
1117 		return;
1118 
1119 	i = 0;
1120 	for_each_child_of_node(node, np) {
1121 		struct rsnd_mod *mod;
1122 
1123 		i = rsnd_node_fixed_index(dev, np, SSI_NAME, i);
1124 		if (i < 0) {
1125 			of_node_put(np);
1126 			break;
1127 		}
1128 
1129 		mod = rsnd_ssi_mod_get(priv, i);
1130 
1131 		if (np == playback)
1132 			rsnd_ssi_connect(mod, &rdai->playback);
1133 		if (np == capture)
1134 			rsnd_ssi_connect(mod, &rdai->capture);
1135 		i++;
1136 	}
1137 
1138 	of_node_put(node);
1139 }
1140 
1141 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1142 {
1143 	if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1144 		id = 0;
1145 
1146 	return rsnd_mod_get(rsnd_ssi_get(priv, id));
1147 }
1148 
1149 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1150 {
1151 	if (!mod)
1152 		return 0;
1153 
1154 	return !!(rsnd_flags_has(rsnd_mod_to_ssi(mod), RSND_SSI_CLK_PIN_SHARE));
1155 }
1156 
1157 int rsnd_ssi_probe(struct rsnd_priv *priv)
1158 {
1159 	struct device_node *node;
1160 	struct device_node *np;
1161 	struct device *dev = rsnd_priv_to_dev(priv);
1162 	struct rsnd_mod_ops *ops;
1163 	struct clk *clk;
1164 	struct rsnd_ssi *ssi;
1165 	char name[RSND_SSI_NAME_SIZE];
1166 	int i, nr, ret;
1167 
1168 	node = rsnd_ssi_of_node(priv);
1169 	if (!node)
1170 		return -EINVAL;
1171 
1172 	nr = rsnd_node_count(priv, node, SSI_NAME);
1173 	if (!nr) {
1174 		ret = -EINVAL;
1175 		goto rsnd_ssi_probe_done;
1176 	}
1177 
1178 	ssi	= devm_kcalloc(dev, nr, sizeof(*ssi), GFP_KERNEL);
1179 	if (!ssi) {
1180 		ret = -ENOMEM;
1181 		goto rsnd_ssi_probe_done;
1182 	}
1183 
1184 	priv->ssi	= ssi;
1185 	priv->ssi_nr	= nr;
1186 
1187 	i = 0;
1188 	for_each_child_of_node(node, np) {
1189 		if (!of_device_is_available(np))
1190 			goto skip;
1191 
1192 		i = rsnd_node_fixed_index(dev, np, SSI_NAME, i);
1193 		if (i < 0) {
1194 			ret = -EINVAL;
1195 			of_node_put(np);
1196 			goto rsnd_ssi_probe_done;
1197 		}
1198 
1199 		ssi = rsnd_ssi_get(priv, i);
1200 
1201 		snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1202 			 SSI_NAME, i);
1203 
1204 		clk = devm_clk_get(dev, name);
1205 		if (IS_ERR(clk)) {
1206 			ret = PTR_ERR(clk);
1207 			of_node_put(np);
1208 			goto rsnd_ssi_probe_done;
1209 		}
1210 
1211 		if (of_get_property(np, "shared-pin", NULL))
1212 			rsnd_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
1213 
1214 		if (of_get_property(np, "no-busif", NULL))
1215 			rsnd_flags_set(ssi, RSND_SSI_NO_BUSIF);
1216 
1217 		ssi->irq = irq_of_parse_and_map(np, 0);
1218 		if (!ssi->irq) {
1219 			ret = -EINVAL;
1220 			of_node_put(np);
1221 			goto rsnd_ssi_probe_done;
1222 		}
1223 
1224 		if (of_property_read_bool(np, "pio-transfer"))
1225 			ops = &rsnd_ssi_pio_ops;
1226 		else
1227 			ops = &rsnd_ssi_dma_ops;
1228 
1229 		ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
1230 				    RSND_MOD_SSI, i);
1231 		if (ret) {
1232 			of_node_put(np);
1233 			goto rsnd_ssi_probe_done;
1234 		}
1235 skip:
1236 		i++;
1237 	}
1238 
1239 	ret = 0;
1240 
1241 rsnd_ssi_probe_done:
1242 	of_node_put(node);
1243 
1244 	return ret;
1245 }
1246 
1247 void rsnd_ssi_remove(struct rsnd_priv *priv)
1248 {
1249 	struct rsnd_ssi *ssi;
1250 	int i;
1251 
1252 	for_each_rsnd_ssi(ssi, priv, i) {
1253 		rsnd_mod_quit(rsnd_mod_get(ssi));
1254 	}
1255 }
1256