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