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