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