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