xref: /linux/sound/soc/sh/rcar/ssi.c (revision c0e297dc61f8d4453e07afbea1fa8d0e67cd4a34)
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 <linux/delay.h>
15 #include "rsnd.h"
16 #define RSND_SSI_NAME_SIZE 16
17 
18 /*
19  * SSICR
20  */
21 #define	FORCE		(1 << 31)	/* Fixed */
22 #define	DMEN		(1 << 28)	/* DMA Enable */
23 #define	UIEN		(1 << 27)	/* Underflow Interrupt Enable */
24 #define	OIEN		(1 << 26)	/* Overflow Interrupt Enable */
25 #define	IIEN		(1 << 25)	/* Idle Mode Interrupt Enable */
26 #define	DIEN		(1 << 24)	/* Data Interrupt Enable */
27 
28 #define	DWL_8		(0 << 19)	/* Data Word Length */
29 #define	DWL_16		(1 << 19)	/* Data Word Length */
30 #define	DWL_18		(2 << 19)	/* Data Word Length */
31 #define	DWL_20		(3 << 19)	/* Data Word Length */
32 #define	DWL_22		(4 << 19)	/* Data Word Length */
33 #define	DWL_24		(5 << 19)	/* Data Word Length */
34 #define	DWL_32		(6 << 19)	/* Data Word Length */
35 
36 #define	SWL_32		(3 << 16)	/* R/W System Word Length */
37 #define	SCKD		(1 << 15)	/* Serial Bit Clock Direction */
38 #define	SWSD		(1 << 14)	/* Serial WS Direction */
39 #define	SCKP		(1 << 13)	/* Serial Bit Clock Polarity */
40 #define	SWSP		(1 << 12)	/* Serial WS Polarity */
41 #define	SDTA		(1 << 10)	/* Serial Data Alignment */
42 #define	DEL		(1 <<  8)	/* Serial Data Delay */
43 #define	CKDV(v)		(v <<  4)	/* Serial Clock Division Ratio */
44 #define	TRMD		(1 <<  1)	/* Transmit/Receive Mode Select */
45 #define	EN		(1 <<  0)	/* SSI Module Enable */
46 
47 /*
48  * SSISR
49  */
50 #define	UIRQ		(1 << 27)	/* Underflow Error Interrupt Status */
51 #define	OIRQ		(1 << 26)	/* Overflow Error Interrupt Status */
52 #define	IIRQ		(1 << 25)	/* Idle Mode Interrupt Status */
53 #define	DIRQ		(1 << 24)	/* Data Interrupt Status Flag */
54 
55 /*
56  * SSIWSR
57  */
58 #define CONT		(1 << 8)	/* WS Continue Function */
59 
60 #define SSI_NAME "ssi"
61 
62 struct rsnd_ssi {
63 	struct rsnd_ssi_platform_info *info; /* rcar_snd.h */
64 	struct rsnd_ssi *parent;
65 	struct rsnd_mod mod;
66 
67 	u32 cr_own;
68 	u32 cr_clk;
69 	int chan;
70 	int err;
71 	unsigned int usrcnt;
72 };
73 
74 #define for_each_rsnd_ssi(pos, priv, i)					\
75 	for (i = 0;							\
76 	     (i < rsnd_ssi_nr(priv)) &&					\
77 		((pos) = ((struct rsnd_ssi *)(priv)->ssi + i));		\
78 	     i++)
79 
80 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
81 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
82 #define rsnd_dma_to_ssi(dma)  rsnd_mod_to_ssi(rsnd_dma_to_mod(dma))
83 #define rsnd_ssi_pio_available(ssi) ((ssi)->info->irq > 0)
84 #define rsnd_ssi_parent(ssi) ((ssi)->parent)
85 #define rsnd_ssi_mode_flags(p) ((p)->info->flags)
86 #define rsnd_ssi_dai_id(ssi) ((ssi)->info->dai_id)
87 #define rsnd_ssi_of_node(priv) \
88 	of_get_child_by_name(rsnd_priv_to_dev(priv)->of_node, "rcar_sound,ssi")
89 
90 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
91 {
92 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
93 	int use_busif = 0;
94 
95 	if (!rsnd_ssi_is_dma_mode(mod))
96 		return 0;
97 
98 	if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
99 		use_busif = 1;
100 	if (rsnd_io_to_mod_src(io))
101 		use_busif = 1;
102 
103 	return use_busif;
104 }
105 
106 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
107 				  u32 bit)
108 {
109 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
110 	struct device *dev = rsnd_priv_to_dev(priv);
111 	u32 status;
112 	int i;
113 
114 	for (i = 0; i < 1024; i++) {
115 		status = rsnd_mod_read(mod, SSISR);
116 		if (status & bit)
117 			return;
118 
119 		udelay(50);
120 	}
121 
122 	dev_warn(dev, "status check failed\n");
123 }
124 
125 static int rsnd_ssi_master_clk_start(struct rsnd_ssi *ssi,
126 				     struct rsnd_dai_stream *io)
127 {
128 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
129 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
130 	struct device *dev = rsnd_priv_to_dev(priv);
131 	int i, j, ret;
132 	int adg_clk_div_table[] = {
133 		1, 6, /* see adg.c */
134 	};
135 	int ssi_clk_mul_table[] = {
136 		1, 2, 4, 8, 16, 6, 12,
137 	};
138 	unsigned int main_rate;
139 	unsigned int rate = rsnd_src_get_ssi_rate(priv, io, runtime);
140 
141 	/*
142 	 * Find best clock, and try to start ADG
143 	 */
144 	for (i = 0; i < ARRAY_SIZE(adg_clk_div_table); i++) {
145 		for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
146 
147 			/*
148 			 * this driver is assuming that
149 			 * system word is 64fs (= 2 x 32bit)
150 			 * see rsnd_ssi_init()
151 			 */
152 			main_rate = rate / adg_clk_div_table[i]
153 				* 32 * 2 * ssi_clk_mul_table[j];
154 
155 			ret = rsnd_adg_ssi_clk_try_start(&ssi->mod, main_rate);
156 			if (0 == ret) {
157 				ssi->cr_clk	= FORCE | SWL_32 |
158 						  SCKD | SWSD | CKDV(j);
159 
160 				dev_dbg(dev, "%s[%d] outputs %u Hz\n",
161 					rsnd_mod_name(&ssi->mod),
162 					rsnd_mod_id(&ssi->mod), rate);
163 
164 				return 0;
165 			}
166 		}
167 	}
168 
169 	dev_err(dev, "unsupported clock rate\n");
170 	return -EIO;
171 }
172 
173 static void rsnd_ssi_master_clk_stop(struct rsnd_ssi *ssi)
174 {
175 	ssi->cr_clk = 0;
176 	rsnd_adg_ssi_clk_stop(&ssi->mod);
177 }
178 
179 static void rsnd_ssi_hw_start(struct rsnd_ssi *ssi,
180 			      struct rsnd_dai_stream *io)
181 {
182 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
183 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
184 	struct device *dev = rsnd_priv_to_dev(priv);
185 	u32 cr_mode;
186 	u32 cr;
187 
188 	if (0 == ssi->usrcnt) {
189 		rsnd_mod_hw_start(&ssi->mod);
190 
191 		if (rsnd_rdai_is_clk_master(rdai)) {
192 			struct rsnd_ssi *ssi_parent = rsnd_ssi_parent(ssi);
193 
194 			if (ssi_parent)
195 				rsnd_ssi_hw_start(ssi_parent, io);
196 			else
197 				rsnd_ssi_master_clk_start(ssi, io);
198 		}
199 	}
200 
201 	if (rsnd_ssi_is_dma_mode(&ssi->mod)) {
202 		cr_mode = UIEN | OIEN |	/* over/under run */
203 			  DMEN;		/* DMA : enable DMA */
204 	} else {
205 		cr_mode = DIEN;		/* PIO : enable Data interrupt */
206 	}
207 
208 	cr  =	ssi->cr_own	|
209 		ssi->cr_clk	|
210 		cr_mode		|
211 		EN;
212 
213 	rsnd_mod_write(&ssi->mod, SSICR, cr);
214 
215 	/* enable WS continue */
216 	if (rsnd_rdai_is_clk_master(rdai))
217 		rsnd_mod_write(&ssi->mod, SSIWSR, CONT);
218 
219 	/* clear error status */
220 	rsnd_mod_write(&ssi->mod, SSISR, 0);
221 
222 	ssi->usrcnt++;
223 
224 	dev_dbg(dev, "%s[%d] hw started\n",
225 		rsnd_mod_name(&ssi->mod), rsnd_mod_id(&ssi->mod));
226 }
227 
228 static void rsnd_ssi_hw_stop(struct rsnd_dai_stream *io, struct rsnd_ssi *ssi)
229 {
230 	struct rsnd_priv *priv = rsnd_mod_to_priv(&ssi->mod);
231 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
232 	struct device *dev = rsnd_priv_to_dev(priv);
233 	u32 cr;
234 
235 	if (0 == ssi->usrcnt) {
236 		dev_err(dev, "%s called without starting\n", __func__);
237 		return;
238 	}
239 
240 	ssi->usrcnt--;
241 
242 	if (0 == ssi->usrcnt) {
243 		/*
244 		 * disable all IRQ,
245 		 * and, wait all data was sent
246 		 */
247 		cr  =	ssi->cr_own	|
248 			ssi->cr_clk;
249 
250 		rsnd_mod_write(&ssi->mod, SSICR, cr | EN);
251 		rsnd_ssi_status_check(&ssi->mod, DIRQ);
252 
253 		/*
254 		 * disable SSI,
255 		 * and, wait idle state
256 		 */
257 		rsnd_mod_write(&ssi->mod, SSICR, cr);	/* disabled all */
258 		rsnd_ssi_status_check(&ssi->mod, IIRQ);
259 
260 		if (rsnd_rdai_is_clk_master(rdai)) {
261 			struct rsnd_ssi *ssi_parent = rsnd_ssi_parent(ssi);
262 
263 			if (ssi_parent)
264 				rsnd_ssi_hw_stop(io, ssi_parent);
265 			else
266 				rsnd_ssi_master_clk_stop(ssi);
267 		}
268 
269 		rsnd_mod_hw_stop(&ssi->mod);
270 
271 		ssi->chan = 0;
272 	}
273 
274 	dev_dbg(dev, "%s[%d] hw stopped\n",
275 		rsnd_mod_name(&ssi->mod), rsnd_mod_id(&ssi->mod));
276 }
277 
278 /*
279  *	SSI mod common functions
280  */
281 static int rsnd_ssi_init(struct rsnd_mod *mod,
282 			 struct rsnd_dai_stream *io,
283 			 struct rsnd_priv *priv)
284 {
285 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
286 	struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
287 	struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
288 	u32 cr;
289 
290 	cr = FORCE;
291 
292 	/*
293 	 * always use 32bit system word for easy clock calculation.
294 	 * see also rsnd_ssi_master_clk_enable()
295 	 */
296 	cr |= SWL_32;
297 
298 	/*
299 	 * init clock settings for SSICR
300 	 */
301 	switch (runtime->sample_bits) {
302 	case 16:
303 		cr |= DWL_16;
304 		break;
305 	case 32:
306 		cr |= DWL_24;
307 		break;
308 	default:
309 		return -EIO;
310 	}
311 
312 	if (rdai->bit_clk_inv)
313 		cr |= SCKP;
314 	if (rdai->frm_clk_inv)
315 		cr |= SWSP;
316 	if (rdai->data_alignment)
317 		cr |= SDTA;
318 	if (rdai->sys_delay)
319 		cr |= DEL;
320 	if (rsnd_io_is_play(io))
321 		cr |= TRMD;
322 
323 	/*
324 	 * set ssi parameter
325 	 */
326 	ssi->cr_own	= cr;
327 	ssi->err	= -1; /* ignore 1st error */
328 
329 	return 0;
330 }
331 
332 static int rsnd_ssi_quit(struct rsnd_mod *mod,
333 			 struct rsnd_dai_stream *io,
334 			 struct rsnd_priv *priv)
335 {
336 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
337 	struct device *dev = rsnd_priv_to_dev(priv);
338 
339 	if (ssi->err > 0)
340 		dev_warn(dev, "%s[%d] under/over flow err = %d\n",
341 			 rsnd_mod_name(mod), rsnd_mod_id(mod), ssi->err);
342 
343 	ssi->cr_own	= 0;
344 	ssi->err	= 0;
345 
346 	return 0;
347 }
348 
349 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
350 			      struct rsnd_dai_stream *io,
351 			      struct snd_pcm_substream *substream,
352 			      struct snd_pcm_hw_params *params)
353 {
354 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
355 	struct rsnd_ssi *ssi_parent = rsnd_ssi_parent(ssi);
356 	int chan = params_channels(params);
357 
358 	/*
359 	 * Already working.
360 	 * It will happen if SSI has parent/child connection.
361 	 */
362 	if (ssi->usrcnt) {
363 		/*
364 		 * it is error if child <-> parent SSI uses
365 		 * different channels.
366 		 */
367 		if (ssi->chan != chan)
368 			return -EIO;
369 	}
370 
371 	/* It will be removed on rsnd_ssi_hw_stop */
372 	ssi->chan = chan;
373 	if (ssi_parent)
374 		return rsnd_ssi_hw_params(&ssi_parent->mod, io,
375 					  substream, params);
376 
377 	return 0;
378 }
379 
380 static void rsnd_ssi_record_error(struct rsnd_ssi *ssi, u32 status)
381 {
382 	/* under/over flow error */
383 	if (status & (UIRQ | OIRQ)) {
384 		ssi->err++;
385 
386 		/* clear error status */
387 		rsnd_mod_write(&ssi->mod, SSISR, 0);
388 	}
389 }
390 
391 static int rsnd_ssi_start(struct rsnd_mod *mod,
392 			  struct rsnd_dai_stream *io,
393 			  struct rsnd_priv *priv)
394 {
395 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
396 
397 	rsnd_src_ssiu_start(mod, io, rsnd_ssi_use_busif(io, mod));
398 
399 	rsnd_ssi_hw_start(ssi, io);
400 
401 	rsnd_src_ssi_irq_enable(mod);
402 
403 	return 0;
404 }
405 
406 static int rsnd_ssi_stop(struct rsnd_mod *mod,
407 			 struct rsnd_dai_stream *io,
408 			 struct rsnd_priv *priv)
409 {
410 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
411 
412 	rsnd_src_ssi_irq_disable(mod);
413 
414 	rsnd_ssi_record_error(ssi, rsnd_mod_read(mod, SSISR));
415 
416 	rsnd_ssi_hw_stop(io, ssi);
417 
418 	rsnd_src_ssiu_stop(mod, io);
419 
420 	return 0;
421 }
422 
423 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
424 				 struct rsnd_dai_stream *io)
425 {
426 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
427 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
428 	int is_dma = rsnd_ssi_is_dma_mode(mod);
429 	u32 status;
430 	bool elapsed = false;
431 
432 	spin_lock(&priv->lock);
433 
434 	/* ignore all cases if not working */
435 	if (!rsnd_io_is_working(io))
436 		goto rsnd_ssi_interrupt_out;
437 
438 	status = rsnd_mod_read(mod, SSISR);
439 
440 	/* PIO only */
441 	if (!is_dma && (status & DIRQ)) {
442 		struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
443 		u32 *buf = (u32 *)(runtime->dma_area +
444 				   rsnd_dai_pointer_offset(io, 0));
445 
446 		/*
447 		 * 8/16/32 data can be assesse to TDR/RDR register
448 		 * directly as 32bit data
449 		 * see rsnd_ssi_init()
450 		 */
451 		if (rsnd_io_is_play(io))
452 			rsnd_mod_write(mod, SSITDR, *buf);
453 		else
454 			*buf = rsnd_mod_read(mod, SSIRDR);
455 
456 		elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
457 	}
458 
459 	/* DMA only */
460 	if (is_dma && (status & (UIRQ | OIRQ))) {
461 		struct device *dev = rsnd_priv_to_dev(priv);
462 
463 		/*
464 		 * restart SSI
465 		 */
466 		dev_dbg(dev, "%s[%d] restart\n",
467 			rsnd_mod_name(mod), rsnd_mod_id(mod));
468 
469 		rsnd_ssi_stop(mod, io, priv);
470 		if (ssi->err < 1024)
471 			rsnd_ssi_start(mod, io, priv);
472 		else
473 			dev_warn(dev, "no more SSI restart\n");
474 	}
475 
476 	rsnd_ssi_record_error(ssi, status);
477 
478 rsnd_ssi_interrupt_out:
479 	spin_unlock(&priv->lock);
480 
481 	if (elapsed)
482 		rsnd_dai_period_elapsed(io);
483 }
484 
485 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
486 {
487 	struct rsnd_mod *mod = data;
488 
489 	rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
490 
491 	return IRQ_HANDLED;
492 }
493 
494 /*
495  *		SSI PIO
496  */
497 static int rsnd_ssi_pio_probe(struct rsnd_mod *mod,
498 			      struct rsnd_dai_stream *io,
499 			      struct rsnd_priv *priv)
500 {
501 	struct device *dev = rsnd_priv_to_dev(priv);
502 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
503 	int ret;
504 
505 	ret = devm_request_irq(dev, ssi->info->irq,
506 			       rsnd_ssi_interrupt,
507 			       IRQF_SHARED,
508 			       dev_name(dev), mod);
509 
510 	return ret;
511 }
512 
513 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
514 	.name	= SSI_NAME,
515 	.probe	= rsnd_ssi_pio_probe,
516 	.init	= rsnd_ssi_init,
517 	.quit	= rsnd_ssi_quit,
518 	.start	= rsnd_ssi_start,
519 	.stop	= rsnd_ssi_stop,
520 	.hw_params = rsnd_ssi_hw_params,
521 };
522 
523 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
524 			      struct rsnd_dai_stream *io,
525 			      struct rsnd_priv *priv)
526 {
527 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
528 	struct device *dev = rsnd_priv_to_dev(priv);
529 	int dma_id = ssi->info->dma_id;
530 	int ret;
531 
532 	ret = devm_request_irq(dev, ssi->info->irq,
533 			       rsnd_ssi_interrupt,
534 			       IRQF_SHARED,
535 			       dev_name(dev), mod);
536 	if (ret)
537 		return ret;
538 
539 	ret = rsnd_dma_init(
540 		io, rsnd_mod_to_dma(mod),
541 		dma_id);
542 
543 	return ret;
544 }
545 
546 static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
547 			       struct rsnd_dai_stream *io,
548 			       struct rsnd_priv *priv)
549 {
550 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
551 	struct device *dev = rsnd_priv_to_dev(priv);
552 	int irq = ssi->info->irq;
553 
554 	rsnd_dma_quit(io, rsnd_mod_to_dma(mod));
555 
556 	/* PIO will request IRQ again */
557 	devm_free_irq(dev, irq, ssi);
558 
559 	return 0;
560 }
561 
562 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
563 			     struct rsnd_dai_stream *io,
564 			     struct rsnd_priv *priv)
565 {
566 	struct device *dev = rsnd_priv_to_dev(priv);
567 
568 	/*
569 	 * fallback to PIO
570 	 *
571 	 * SSI .probe might be called again.
572 	 * see
573 	 *	rsnd_rdai_continuance_probe()
574 	 */
575 	mod->ops = &rsnd_ssi_pio_ops;
576 
577 	dev_info(dev, "%s[%d] fallback to PIO mode\n",
578 		 rsnd_mod_name(mod), rsnd_mod_id(mod));
579 
580 	return 0;
581 }
582 
583 static int rsnd_ssi_dma_start(struct rsnd_mod *mod,
584 			      struct rsnd_dai_stream *io,
585 			      struct rsnd_priv *priv)
586 {
587 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
588 
589 	rsnd_dma_start(io, dma);
590 
591 	rsnd_ssi_start(mod, io, priv);
592 
593 	return 0;
594 }
595 
596 static int rsnd_ssi_dma_stop(struct rsnd_mod *mod,
597 			     struct rsnd_dai_stream *io,
598 			     struct rsnd_priv *priv)
599 {
600 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
601 
602 	rsnd_ssi_stop(mod, io, priv);
603 
604 	rsnd_dma_stop(io, dma);
605 
606 	return 0;
607 }
608 
609 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
610 					 struct rsnd_mod *mod)
611 {
612 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
613 	int is_play = rsnd_io_is_play(io);
614 	char *name;
615 
616 	if (rsnd_ssi_use_busif(io, mod))
617 		name = is_play ? "rxu" : "txu";
618 	else
619 		name = is_play ? "rx" : "tx";
620 
621 	return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
622 					mod, name);
623 }
624 
625 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
626 	.name	= SSI_NAME,
627 	.dma_req = rsnd_ssi_dma_req,
628 	.probe	= rsnd_ssi_dma_probe,
629 	.remove	= rsnd_ssi_dma_remove,
630 	.init	= rsnd_ssi_init,
631 	.quit	= rsnd_ssi_quit,
632 	.start	= rsnd_ssi_dma_start,
633 	.stop	= rsnd_ssi_dma_stop,
634 	.fallback = rsnd_ssi_fallback,
635 	.hw_params = rsnd_ssi_hw_params,
636 };
637 
638 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
639 {
640 	return mod->ops == &rsnd_ssi_dma_ops;
641 }
642 
643 
644 /*
645  *		Non SSI
646  */
647 static struct rsnd_mod_ops rsnd_ssi_non_ops = {
648 	.name	= SSI_NAME,
649 };
650 
651 /*
652  *		ssi mod function
653  */
654 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
655 {
656 	if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
657 		id = 0;
658 
659 	return &((struct rsnd_ssi *)(priv->ssi) + id)->mod;
660 }
661 
662 int rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
663 {
664 	struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
665 
666 	return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
667 }
668 
669 static void rsnd_ssi_parent_setup(struct rsnd_priv *priv, struct rsnd_ssi *ssi)
670 {
671 	if (!rsnd_ssi_is_pin_sharing(&ssi->mod))
672 		return;
673 
674 	switch (rsnd_mod_id(&ssi->mod)) {
675 	case 1:
676 	case 2:
677 		ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 0));
678 		break;
679 	case 4:
680 		ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 3));
681 		break;
682 	case 8:
683 		ssi->parent = rsnd_mod_to_ssi(rsnd_ssi_mod_get(priv, 7));
684 		break;
685 	}
686 }
687 
688 
689 static void rsnd_of_parse_ssi(struct platform_device *pdev,
690 			      const struct rsnd_of_data *of_data,
691 			      struct rsnd_priv *priv)
692 {
693 	struct device_node *node;
694 	struct device_node *np;
695 	struct rsnd_ssi_platform_info *ssi_info;
696 	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
697 	struct device *dev = &pdev->dev;
698 	int nr, i;
699 
700 	if (!of_data)
701 		return;
702 
703 	node = rsnd_ssi_of_node(priv);
704 	if (!node)
705 		return;
706 
707 	nr = of_get_child_count(node);
708 	if (!nr)
709 		goto rsnd_of_parse_ssi_end;
710 
711 	ssi_info = devm_kzalloc(dev,
712 				sizeof(struct rsnd_ssi_platform_info) * nr,
713 				GFP_KERNEL);
714 	if (!ssi_info) {
715 		dev_err(dev, "ssi info allocation error\n");
716 		goto rsnd_of_parse_ssi_end;
717 	}
718 
719 	info->ssi_info		= ssi_info;
720 	info->ssi_info_nr	= nr;
721 
722 	i = -1;
723 	for_each_child_of_node(node, np) {
724 		i++;
725 
726 		ssi_info = info->ssi_info + i;
727 
728 		/*
729 		 * pin settings
730 		 */
731 		if (of_get_property(np, "shared-pin", NULL))
732 			ssi_info->flags |= RSND_SSI_CLK_PIN_SHARE;
733 
734 		/*
735 		 * irq
736 		 */
737 		ssi_info->irq = irq_of_parse_and_map(np, 0);
738 
739 		/*
740 		 * DMA
741 		 */
742 		ssi_info->dma_id = of_get_property(np, "pio-transfer", NULL) ?
743 			0 : 1;
744 
745 		if (of_get_property(np, "no-busif", NULL))
746 			ssi_info->flags |= RSND_SSI_NO_BUSIF;
747 	}
748 
749 rsnd_of_parse_ssi_end:
750 	of_node_put(node);
751 }
752 
753 int rsnd_ssi_probe(struct platform_device *pdev,
754 		   const struct rsnd_of_data *of_data,
755 		   struct rsnd_priv *priv)
756 {
757 	struct rcar_snd_info *info = rsnd_priv_to_info(priv);
758 	struct rsnd_ssi_platform_info *pinfo;
759 	struct device *dev = rsnd_priv_to_dev(priv);
760 	struct rsnd_mod_ops *ops;
761 	struct clk *clk;
762 	struct rsnd_ssi *ssi;
763 	char name[RSND_SSI_NAME_SIZE];
764 	int i, nr, ret;
765 
766 	rsnd_of_parse_ssi(pdev, of_data, priv);
767 
768 	/*
769 	 *	init SSI
770 	 */
771 	nr	= info->ssi_info_nr;
772 	ssi	= devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
773 	if (!ssi) {
774 		dev_err(dev, "SSI allocate failed\n");
775 		return -ENOMEM;
776 	}
777 
778 	priv->ssi	= ssi;
779 	priv->ssi_nr	= nr;
780 
781 	for_each_rsnd_ssi(ssi, priv, i) {
782 		pinfo = &info->ssi_info[i];
783 
784 		snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
785 			 SSI_NAME, i);
786 
787 		clk = devm_clk_get(dev, name);
788 		if (IS_ERR(clk))
789 			return PTR_ERR(clk);
790 
791 		ssi->info	= pinfo;
792 
793 		ops = &rsnd_ssi_non_ops;
794 		if (pinfo->dma_id > 0)
795 			ops = &rsnd_ssi_dma_ops;
796 		else if (rsnd_ssi_pio_available(ssi))
797 			ops = &rsnd_ssi_pio_ops;
798 
799 		ret = rsnd_mod_init(priv, &ssi->mod, ops, clk, RSND_MOD_SSI, i);
800 		if (ret)
801 			return ret;
802 
803 		rsnd_ssi_parent_setup(priv, ssi);
804 	}
805 
806 	return 0;
807 }
808 
809 void rsnd_ssi_remove(struct platform_device *pdev,
810 		     struct rsnd_priv *priv)
811 {
812 	struct rsnd_ssi *ssi;
813 	int i;
814 
815 	for_each_rsnd_ssi(ssi, priv, i) {
816 		rsnd_mod_quit(&ssi->mod);
817 	}
818 }
819