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