sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / sound / soc / sh / rcar / ssi.c
blob411bda2387adbcdab2dc510706986f5ac92b3cc1
1 /*
2 * Renesas R-Car SSIU/SSI support
4 * Copyright (C) 2013 Renesas Solutions Corp.
5 * Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 * Based on fsi.c
8 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
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.
14 #include <linux/delay.h>
15 #include "rsnd.h"
16 #define RSND_SSI_NAME_SIZE 16
19 * SSICR
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 #define CHNL_4 (1 << 22) /* Channels */
28 #define CHNL_6 (2 << 22) /* Channels */
29 #define CHNL_8 (3 << 22) /* Channels */
30 #define DWL_8 (0 << 19) /* Data Word Length */
31 #define DWL_16 (1 << 19) /* Data Word Length */
32 #define DWL_18 (2 << 19) /* Data Word Length */
33 #define DWL_20 (3 << 19) /* Data Word Length */
34 #define DWL_22 (4 << 19) /* Data Word Length */
35 #define DWL_24 (5 << 19) /* Data Word Length */
36 #define DWL_32 (6 << 19) /* Data Word Length */
38 #define SWL_32 (3 << 16) /* R/W System Word Length */
39 #define SCKD (1 << 15) /* Serial Bit Clock Direction */
40 #define SWSD (1 << 14) /* Serial WS Direction */
41 #define SCKP (1 << 13) /* Serial Bit Clock Polarity */
42 #define SWSP (1 << 12) /* Serial WS Polarity */
43 #define SDTA (1 << 10) /* Serial Data Alignment */
44 #define PDTA (1 << 9) /* Parallel Data Alignment */
45 #define DEL (1 << 8) /* Serial Data Delay */
46 #define CKDV(v) (v << 4) /* Serial Clock Division Ratio */
47 #define TRMD (1 << 1) /* Transmit/Receive Mode Select */
48 #define EN (1 << 0) /* SSI Module Enable */
51 * SSISR
53 #define UIRQ (1 << 27) /* Underflow Error Interrupt Status */
54 #define OIRQ (1 << 26) /* Overflow Error Interrupt Status */
55 #define IIRQ (1 << 25) /* Idle Mode Interrupt Status */
56 #define DIRQ (1 << 24) /* Data Interrupt Status Flag */
59 * SSIWSR
61 #define CONT (1 << 8) /* WS Continue Function */
62 #define WS_MODE (1 << 0) /* WS Mode */
64 #define SSI_NAME "ssi"
66 struct rsnd_ssi {
67 struct rsnd_mod mod;
68 struct rsnd_mod *dma;
70 u32 flags;
71 u32 cr_own;
72 u32 cr_clk;
73 u32 cr_mode;
74 u32 wsr;
75 int chan;
76 int rate;
77 int irq;
78 unsigned int usrcnt;
81 /* flags */
82 #define RSND_SSI_CLK_PIN_SHARE (1 << 0)
83 #define RSND_SSI_NO_BUSIF (1 << 1) /* SSI+DMA without BUSIF */
85 #define for_each_rsnd_ssi(pos, priv, i) \
86 for (i = 0; \
87 (i < rsnd_ssi_nr(priv)) && \
88 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i)); \
89 i++)
91 #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
92 #define rsnd_ssi_to_dma(mod) ((ssi)->dma)
93 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
94 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
95 #define rsnd_ssi_mode_flags(p) ((p)->flags)
96 #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
97 #define rsnd_ssi_is_multi_slave(mod, io) \
98 (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
99 #define rsnd_ssi_is_run_mods(mod, io) \
100 (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
102 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
104 struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
105 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
106 int use_busif = 0;
108 if (!rsnd_ssi_is_dma_mode(mod))
109 return 0;
111 if (!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_NO_BUSIF))
112 use_busif = 1;
113 if (rsnd_io_to_mod_src(io))
114 use_busif = 1;
116 return use_busif;
119 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
121 rsnd_mod_write(mod, SSISR, 0);
124 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
126 return rsnd_mod_read(mod, SSISR);
129 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
130 u32 bit)
132 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
133 struct device *dev = rsnd_priv_to_dev(priv);
134 u32 status;
135 int i;
137 for (i = 0; i < 1024; i++) {
138 status = rsnd_ssi_status_get(mod);
139 if (status & bit)
140 return;
142 udelay(50);
145 dev_warn(dev, "%s[%d] status check failed\n",
146 rsnd_mod_name(mod), rsnd_mod_id(mod));
149 static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
151 struct rsnd_mod *mod;
152 enum rsnd_mod_type types[] = {
153 RSND_MOD_SSIM1,
154 RSND_MOD_SSIM2,
155 RSND_MOD_SSIM3,
157 int i, mask;
159 mask = 0;
160 for (i = 0; i < ARRAY_SIZE(types); i++) {
161 mod = rsnd_io_to_mod(io, types[i]);
162 if (!mod)
163 continue;
165 mask |= 1 << rsnd_mod_id(mod);
168 return mask;
171 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
173 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
174 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
176 return rsnd_ssi_multi_slaves_runtime(io) |
177 1 << rsnd_mod_id(ssi_mod) |
178 1 << rsnd_mod_id(ssi_parent_mod);
181 u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
183 if (rsnd_runtime_is_ssi_multi(io))
184 return rsnd_ssi_multi_slaves(io);
186 return 0;
189 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
190 struct rsnd_dai_stream *io)
192 struct rsnd_priv *priv = rsnd_io_to_priv(io);
193 struct device *dev = rsnd_priv_to_dev(priv);
194 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
195 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
196 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
197 int chan = rsnd_runtime_channel_for_ssi(io);
198 int j, ret;
199 int ssi_clk_mul_table[] = {
200 1, 2, 4, 8, 16, 6, 12,
202 unsigned int main_rate;
203 unsigned int rate = rsnd_io_is_play(io) ?
204 rsnd_src_get_out_rate(priv, io) :
205 rsnd_src_get_in_rate(priv, io);
207 if (!rsnd_rdai_is_clk_master(rdai))
208 return 0;
210 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
211 return 0;
213 if (rsnd_ssi_is_multi_slave(mod, io))
214 return 0;
216 if (ssi->usrcnt > 1) {
217 if (ssi->rate != rate) {
218 dev_err(dev, "SSI parent/child should use same rate\n");
219 return -EINVAL;
222 return 0;
226 * Find best clock, and try to start ADG
228 for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
231 * this driver is assuming that
232 * system word is 32bit x chan
233 * see rsnd_ssi_init()
235 main_rate = rate * 32 * chan * ssi_clk_mul_table[j];
237 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
238 if (0 == ret) {
239 ssi->cr_clk = FORCE | SWL_32 |
240 SCKD | SWSD | CKDV(j);
241 ssi->wsr = CONT;
243 ssi->rate = rate;
245 dev_dbg(dev, "%s[%d] outputs %u Hz\n",
246 rsnd_mod_name(mod),
247 rsnd_mod_id(mod), rate);
249 return 0;
253 dev_err(dev, "unsupported clock rate\n");
254 return -EIO;
257 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
258 struct rsnd_dai_stream *io)
260 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
261 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
262 struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
264 if (!rsnd_rdai_is_clk_master(rdai))
265 return;
267 if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
268 return;
270 if (ssi->usrcnt > 1)
271 return;
273 ssi->cr_clk = 0;
274 ssi->rate = 0;
276 rsnd_adg_ssi_clk_stop(mod);
279 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
280 struct rsnd_dai_stream *io)
282 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
283 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
284 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
285 u32 cr_own;
286 u32 cr_mode;
287 u32 wsr;
288 int is_tdm;
290 is_tdm = rsnd_runtime_is_ssi_tdm(io);
293 * always use 32bit system word.
294 * see also rsnd_ssi_master_clk_enable()
296 cr_own = FORCE | SWL_32 | PDTA;
298 if (rdai->bit_clk_inv)
299 cr_own |= SCKP;
300 if (rdai->frm_clk_inv ^ is_tdm)
301 cr_own |= SWSP;
302 if (rdai->data_alignment)
303 cr_own |= SDTA;
304 if (rdai->sys_delay)
305 cr_own |= DEL;
306 if (rsnd_io_is_play(io))
307 cr_own |= TRMD;
309 switch (runtime->sample_bits) {
310 case 16:
311 cr_own |= DWL_16;
312 break;
313 case 32:
314 cr_own |= DWL_24;
315 break;
318 if (rsnd_ssi_is_dma_mode(mod)) {
319 cr_mode = UIEN | OIEN | /* over/under run */
320 DMEN; /* DMA : enable DMA */
321 } else {
322 cr_mode = DIEN; /* PIO : enable Data interrupt */
326 * TDM Extend Mode
327 * see
328 * rsnd_ssiu_init_gen2()
330 wsr = ssi->wsr;
331 if (is_tdm) {
332 wsr |= WS_MODE;
333 cr_own |= CHNL_8;
336 ssi->cr_own = cr_own;
337 ssi->cr_mode = cr_mode;
338 ssi->wsr = wsr;
341 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
343 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
345 rsnd_mod_write(mod, SSIWSR, ssi->wsr);
346 rsnd_mod_write(mod, SSICR, ssi->cr_own |
347 ssi->cr_clk |
348 ssi->cr_mode); /* without EN */
352 * SSI mod common functions
354 static int rsnd_ssi_init(struct rsnd_mod *mod,
355 struct rsnd_dai_stream *io,
356 struct rsnd_priv *priv)
358 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
359 int ret;
361 if (!rsnd_ssi_is_run_mods(mod, io))
362 return 0;
364 ssi->usrcnt++;
366 rsnd_mod_power_on(mod);
368 ret = rsnd_ssi_master_clk_start(mod, io);
369 if (ret < 0)
370 return ret;
372 if (!rsnd_ssi_is_parent(mod, io))
373 rsnd_ssi_config_init(mod, io);
375 rsnd_ssi_register_setup(mod);
377 /* clear error status */
378 rsnd_ssi_status_clear(mod);
380 return 0;
383 static int rsnd_ssi_quit(struct rsnd_mod *mod,
384 struct rsnd_dai_stream *io,
385 struct rsnd_priv *priv)
387 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
388 struct device *dev = rsnd_priv_to_dev(priv);
390 if (!rsnd_ssi_is_run_mods(mod, io))
391 return 0;
393 if (!ssi->usrcnt) {
394 dev_err(dev, "%s[%d] usrcnt error\n",
395 rsnd_mod_name(mod), rsnd_mod_id(mod));
396 return -EIO;
399 if (!rsnd_ssi_is_parent(mod, io))
400 ssi->cr_own = 0;
402 rsnd_ssi_master_clk_stop(mod, io);
404 rsnd_mod_power_off(mod);
406 ssi->usrcnt--;
408 return 0;
411 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
412 struct rsnd_dai_stream *io,
413 struct snd_pcm_substream *substream,
414 struct snd_pcm_hw_params *params)
416 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
417 int chan = params_channels(params);
420 * snd_pcm_ops::hw_params will be called *before*
421 * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
422 * in 1st call.
424 if (ssi->usrcnt) {
426 * Already working.
427 * It will happen if SSI has parent/child connection.
428 * it is error if child <-> parent SSI uses
429 * different channels.
431 if (ssi->chan != chan)
432 return -EIO;
435 ssi->chan = chan;
437 return 0;
440 static int rsnd_ssi_start(struct rsnd_mod *mod,
441 struct rsnd_dai_stream *io,
442 struct rsnd_priv *priv)
444 if (!rsnd_ssi_is_run_mods(mod, io))
445 return 0;
448 * EN will be set via SSIU :: SSI_CONTROL
449 * if Multi channel mode
451 if (rsnd_ssi_multi_slaves_runtime(io))
452 return 0;
454 rsnd_mod_bset(mod, SSICR, EN, EN);
456 return 0;
459 static int rsnd_ssi_stop(struct rsnd_mod *mod,
460 struct rsnd_dai_stream *io,
461 struct rsnd_priv *priv)
463 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
464 u32 cr;
466 if (!rsnd_ssi_is_run_mods(mod, io))
467 return 0;
470 * don't stop if not last user
471 * see also
472 * rsnd_ssi_start
473 * rsnd_ssi_interrupt
475 if (ssi->usrcnt > 1)
476 return 0;
479 * disable all IRQ,
480 * and, wait all data was sent
482 cr = ssi->cr_own |
483 ssi->cr_clk;
485 rsnd_mod_write(mod, SSICR, cr | EN);
486 rsnd_ssi_status_check(mod, DIRQ);
489 * disable SSI,
490 * and, wait idle state
492 rsnd_mod_write(mod, SSICR, cr); /* disabled all */
493 rsnd_ssi_status_check(mod, IIRQ);
495 return 0;
498 static int rsnd_ssi_irq(struct rsnd_mod *mod,
499 struct rsnd_dai_stream *io,
500 struct rsnd_priv *priv,
501 int enable)
503 u32 val = 0;
505 if (rsnd_is_gen1(priv))
506 return 0;
508 if (rsnd_ssi_is_parent(mod, io))
509 return 0;
511 if (!rsnd_ssi_is_run_mods(mod, io))
512 return 0;
514 if (enable)
515 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
517 rsnd_mod_write(mod, SSI_INT_ENABLE, val);
519 return 0;
522 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
523 struct rsnd_dai_stream *io)
525 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
526 int is_dma = rsnd_ssi_is_dma_mode(mod);
527 u32 status;
528 bool elapsed = false;
529 bool stop = false;
531 spin_lock(&priv->lock);
533 /* ignore all cases if not working */
534 if (!rsnd_io_is_working(io))
535 goto rsnd_ssi_interrupt_out;
537 status = rsnd_ssi_status_get(mod);
539 /* PIO only */
540 if (!is_dma && (status & DIRQ)) {
541 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
542 u32 *buf = (u32 *)(runtime->dma_area +
543 rsnd_dai_pointer_offset(io, 0));
546 * 8/16/32 data can be assesse to TDR/RDR register
547 * directly as 32bit data
548 * see rsnd_ssi_init()
550 if (rsnd_io_is_play(io))
551 rsnd_mod_write(mod, SSITDR, *buf);
552 else
553 *buf = rsnd_mod_read(mod, SSIRDR);
555 elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
558 /* DMA only */
559 if (is_dma && (status & (UIRQ | OIRQ)))
560 stop = true;
562 rsnd_ssi_status_clear(mod);
563 rsnd_ssi_interrupt_out:
564 spin_unlock(&priv->lock);
566 if (elapsed)
567 rsnd_dai_period_elapsed(io);
569 if (stop)
570 snd_pcm_stop_xrun(io->substream);
574 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
576 struct rsnd_mod *mod = data;
578 rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
580 return IRQ_HANDLED;
584 * SSI PIO
586 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
587 struct rsnd_dai_stream *io)
589 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
590 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
592 if (!__rsnd_ssi_is_pin_sharing(mod))
593 return;
595 if (!rsnd_rdai_is_clk_master(rdai))
596 return;
598 switch (rsnd_mod_id(mod)) {
599 case 1:
600 case 2:
601 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
602 break;
603 case 4:
604 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
605 break;
606 case 8:
607 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
608 break;
612 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
613 struct rsnd_dai_stream *io,
614 struct snd_soc_pcm_runtime *rtd)
617 * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
618 * and, pcm_new will be called after it.
619 * This function reuse pcm_new at this point.
621 rsnd_ssi_parent_attach(mod, io);
623 return 0;
626 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
627 struct rsnd_dai_stream *io,
628 struct rsnd_priv *priv)
630 struct device *dev = rsnd_priv_to_dev(priv);
631 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
632 int ret;
635 * SSIP/SSIU/IRQ are not needed on
636 * SSI Multi slaves
638 if (rsnd_ssi_is_multi_slave(mod, io))
639 return 0;
642 * It can't judge ssi parent at this point
643 * see rsnd_ssi_pcm_new()
646 ret = rsnd_ssiu_attach(io, mod);
647 if (ret < 0)
648 return ret;
651 * SSI might be called again as PIO fallback
652 * It is easy to manual handling for IRQ request/free
654 ret = request_irq(ssi->irq,
655 rsnd_ssi_interrupt,
656 IRQF_SHARED,
657 dev_name(dev), mod);
659 return ret;
662 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
663 .name = SSI_NAME,
664 .probe = rsnd_ssi_common_probe,
665 .init = rsnd_ssi_init,
666 .quit = rsnd_ssi_quit,
667 .start = rsnd_ssi_start,
668 .stop = rsnd_ssi_stop,
669 .irq = rsnd_ssi_irq,
670 .pcm_new = rsnd_ssi_pcm_new,
671 .hw_params = rsnd_ssi_hw_params,
674 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
675 struct rsnd_dai_stream *io,
676 struct rsnd_priv *priv)
678 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
679 int ret;
682 * SSIP/SSIU/IRQ/DMA are not needed on
683 * SSI Multi slaves
685 if (rsnd_ssi_is_multi_slave(mod, io))
686 return 0;
688 ret = rsnd_ssi_common_probe(mod, io, priv);
689 if (ret)
690 return ret;
692 /* SSI probe might be called many times in MUX multi path */
693 ret = rsnd_dma_attach(io, mod, &ssi->dma);
695 return ret;
698 static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
699 struct rsnd_dai_stream *io,
700 struct rsnd_priv *priv)
702 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
704 /* PIO will request IRQ again */
705 free_irq(ssi->irq, mod);
707 return 0;
710 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
711 struct rsnd_dai_stream *io,
712 struct rsnd_priv *priv)
714 struct device *dev = rsnd_priv_to_dev(priv);
717 * fallback to PIO
719 * SSI .probe might be called again.
720 * see
721 * rsnd_rdai_continuance_probe()
723 mod->ops = &rsnd_ssi_pio_ops;
725 dev_info(dev, "%s[%d] fallback to PIO mode\n",
726 rsnd_mod_name(mod), rsnd_mod_id(mod));
728 return 0;
731 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
732 struct rsnd_mod *mod)
734 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
735 int is_play = rsnd_io_is_play(io);
736 char *name;
738 if (rsnd_ssi_use_busif(io))
739 name = is_play ? "rxu" : "txu";
740 else
741 name = is_play ? "rx" : "tx";
743 return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
744 mod, name);
747 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
748 .name = SSI_NAME,
749 .dma_req = rsnd_ssi_dma_req,
750 .probe = rsnd_ssi_dma_probe,
751 .remove = rsnd_ssi_dma_remove,
752 .init = rsnd_ssi_init,
753 .quit = rsnd_ssi_quit,
754 .start = rsnd_ssi_start,
755 .stop = rsnd_ssi_stop,
756 .irq = rsnd_ssi_irq,
757 .pcm_new = rsnd_ssi_pcm_new,
758 .fallback = rsnd_ssi_fallback,
759 .hw_params = rsnd_ssi_hw_params,
762 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
764 return mod->ops == &rsnd_ssi_dma_ops;
769 * Non SSI
771 static struct rsnd_mod_ops rsnd_ssi_non_ops = {
772 .name = SSI_NAME,
776 * ssi mod function
778 static void rsnd_ssi_connect(struct rsnd_mod *mod,
779 struct rsnd_dai_stream *io)
781 struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
782 enum rsnd_mod_type types[] = {
783 RSND_MOD_SSI,
784 RSND_MOD_SSIM1,
785 RSND_MOD_SSIM2,
786 RSND_MOD_SSIM3,
788 enum rsnd_mod_type type;
789 int i;
791 /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
792 for (i = 0; i < ARRAY_SIZE(types); i++) {
793 type = types[i];
794 if (!rsnd_io_to_mod(io, type)) {
795 rsnd_dai_connect(mod, io, type);
796 rsnd_set_slot(rdai, 2 * (i + 1), (i + 1));
797 return;
802 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
803 struct device_node *playback,
804 struct device_node *capture)
806 struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
807 struct device_node *node;
808 struct device_node *np;
809 struct rsnd_mod *mod;
810 int i;
812 node = rsnd_ssi_of_node(priv);
813 if (!node)
814 return;
816 i = 0;
817 for_each_child_of_node(node, np) {
818 mod = rsnd_ssi_mod_get(priv, i);
819 if (np == playback)
820 rsnd_ssi_connect(mod, &rdai->playback);
821 if (np == capture)
822 rsnd_ssi_connect(mod, &rdai->capture);
823 i++;
826 of_node_put(node);
829 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
831 if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
832 id = 0;
834 return rsnd_mod_get(rsnd_ssi_get(priv, id));
837 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
839 struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
841 return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
844 static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
845 struct rsnd_mod *mod,
846 enum rsnd_mod_type type)
849 * SSIP (= SSI parent) needs to be special, otherwise,
850 * 2nd SSI might doesn't start. see also rsnd_mod_call()
852 * We can't include parent SSI status on SSI, because we don't know
853 * how many SSI requests parent SSI. Thus, it is localed on "io" now.
854 * ex) trouble case
855 * Playback: SSI0
856 * Capture : SSI1 (needs SSI0)
858 * 1) start Capture -> SSI0/SSI1 are started.
859 * 2) start Playback -> SSI0 doesn't work, because it is already
860 * marked as "started" on 1)
862 * OTOH, using each mod's status is good for MUX case.
863 * It doesn't need to start in 2nd start
864 * ex)
865 * IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
867 * IO-1: SRC1 -> CTU2 -+
869 * 1) start IO-0 -> start SSI0
870 * 2) start IO-1 -> SSI0 doesn't need to start, because it is
871 * already started on 1)
873 if (type == RSND_MOD_SSIP)
874 return &io->parent_ssi_status;
876 return rsnd_mod_get_status(io, mod, type);
879 int rsnd_ssi_probe(struct rsnd_priv *priv)
881 struct device_node *node;
882 struct device_node *np;
883 struct device *dev = rsnd_priv_to_dev(priv);
884 struct rsnd_mod_ops *ops;
885 struct clk *clk;
886 struct rsnd_ssi *ssi;
887 char name[RSND_SSI_NAME_SIZE];
888 int i, nr, ret;
890 node = rsnd_ssi_of_node(priv);
891 if (!node)
892 return -EINVAL;
894 nr = of_get_child_count(node);
895 if (!nr) {
896 ret = -EINVAL;
897 goto rsnd_ssi_probe_done;
900 ssi = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
901 if (!ssi) {
902 ret = -ENOMEM;
903 goto rsnd_ssi_probe_done;
906 priv->ssi = ssi;
907 priv->ssi_nr = nr;
909 i = 0;
910 for_each_child_of_node(node, np) {
911 ssi = rsnd_ssi_get(priv, i);
913 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
914 SSI_NAME, i);
916 clk = devm_clk_get(dev, name);
917 if (IS_ERR(clk)) {
918 ret = PTR_ERR(clk);
919 goto rsnd_ssi_probe_done;
922 if (of_get_property(np, "shared-pin", NULL))
923 ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
925 if (of_get_property(np, "no-busif", NULL))
926 ssi->flags |= RSND_SSI_NO_BUSIF;
928 ssi->irq = irq_of_parse_and_map(np, 0);
929 if (!ssi->irq) {
930 ret = -EINVAL;
931 goto rsnd_ssi_probe_done;
934 ops = &rsnd_ssi_non_ops;
935 if (of_property_read_bool(np, "pio-transfer"))
936 ops = &rsnd_ssi_pio_ops;
937 else
938 ops = &rsnd_ssi_dma_ops;
940 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
941 rsnd_ssi_get_status, RSND_MOD_SSI, i);
942 if (ret)
943 goto rsnd_ssi_probe_done;
945 i++;
948 ret = 0;
950 rsnd_ssi_probe_done:
951 of_node_put(node);
953 return ret;
956 void rsnd_ssi_remove(struct rsnd_priv *priv)
958 struct rsnd_ssi *ssi;
959 int i;
961 for_each_rsnd_ssi(ssi, priv, i) {
962 rsnd_mod_quit(rsnd_mod_get(ssi));