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