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