]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/sh/rcar/ssi.c
Merge tag 'armsoc-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[karo-tx-linux.git] / sound / soc / sh / rcar / ssi.c
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 #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 */
37
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 */
49
50 /*
51  * SSISR
52  */
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 */
57
58 /*
59  * SSIWSR
60  */
61 #define CONT            (1 << 8)        /* WS Continue Function */
62 #define WS_MODE         (1 << 0)        /* WS Mode */
63
64 #define SSI_NAME "ssi"
65
66 struct rsnd_ssi {
67         struct rsnd_mod mod;
68         struct rsnd_mod *dma;
69
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;
79 };
80
81 /* flags */
82 #define RSND_SSI_CLK_PIN_SHARE          (1 << 0)
83 #define RSND_SSI_NO_BUSIF               (1 << 1) /* SSI+DMA without BUSIF */
84
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++)
90
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)))
101
102 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
103 {
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;
107
108         if (!rsnd_ssi_is_dma_mode(mod))
109                 return 0;
110
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;
115
116         return use_busif;
117 }
118
119 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
120 {
121         rsnd_mod_write(mod, SSISR, 0);
122 }
123
124 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
125 {
126         return rsnd_mod_read(mod, SSISR);
127 }
128
129 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
130                                   u32 bit)
131 {
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;
136
137         for (i = 0; i < 1024; i++) {
138                 status = rsnd_ssi_status_get(mod);
139                 if (status & bit)
140                         return;
141
142                 udelay(50);
143         }
144
145         dev_warn(dev, "%s[%d] status check failed\n",
146                  rsnd_mod_name(mod), rsnd_mod_id(mod));
147 }
148
149 static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
150 {
151         struct rsnd_mod *mod;
152         enum rsnd_mod_type types[] = {
153                 RSND_MOD_SSIM1,
154                 RSND_MOD_SSIM2,
155                 RSND_MOD_SSIM3,
156         };
157         int i, mask;
158
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;
164
165                 mask |= 1 << rsnd_mod_id(mod);
166         }
167
168         return mask;
169 }
170
171 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
172 {
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);
175
176         return rsnd_ssi_multi_slaves_runtime(io) |
177                 1 << rsnd_mod_id(ssi_mod) |
178                 1 << rsnd_mod_id(ssi_parent_mod);
179 }
180
181 u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
182 {
183         if (rsnd_runtime_is_ssi_multi(io))
184                 return rsnd_ssi_multi_slaves(io);
185
186         return 0;
187 }
188
189 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
190                                      struct rsnd_dai_stream *io)
191 {
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,
201         };
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);
206
207         if (!rsnd_rdai_is_clk_master(rdai))
208                 return 0;
209
210         if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
211                 return 0;
212
213         if (rsnd_ssi_is_multi_slave(mod, io))
214                 return 0;
215
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;
220                 }
221
222                 return 0;
223         }
224
225         /*
226          * Find best clock, and try to start ADG
227          */
228         for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
229
230                 /*
231                  * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
232                  * with it is not allowed. (SSIWSR.WS_MODE with
233                  * SSICR.CKDV = 000 is not allowed either).
234                  * Skip it. See SSICR.CKDV
235                  */
236                 if (j == 0)
237                         continue;
238
239                 /*
240                  * this driver is assuming that
241                  * system word is 32bit x chan
242                  * see rsnd_ssi_init()
243                  */
244                 main_rate = rate * 32 * chan * ssi_clk_mul_table[j];
245
246                 ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
247                 if (0 == ret) {
248                         ssi->cr_clk     = FORCE | SWL_32 |
249                                 SCKD | SWSD | CKDV(j);
250                         ssi->wsr = CONT;
251
252                         ssi->rate = rate;
253
254                         dev_dbg(dev, "%s[%d] outputs %u Hz\n",
255                                 rsnd_mod_name(mod),
256                                 rsnd_mod_id(mod), rate);
257
258                         return 0;
259                 }
260         }
261
262         dev_err(dev, "unsupported clock rate\n");
263         return -EIO;
264 }
265
266 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
267                                      struct rsnd_dai_stream *io)
268 {
269         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
270         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
271         struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
272
273         if (!rsnd_rdai_is_clk_master(rdai))
274                 return;
275
276         if (ssi_parent_mod && !rsnd_ssi_is_parent(mod, io))
277                 return;
278
279         if (ssi->usrcnt > 1)
280                 return;
281
282         ssi->cr_clk     = 0;
283         ssi->rate       = 0;
284
285         rsnd_adg_ssi_clk_stop(mod);
286 }
287
288 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
289                                 struct rsnd_dai_stream *io)
290 {
291         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
292         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
293         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
294         u32 cr_own;
295         u32 cr_mode;
296         u32 wsr;
297         int is_tdm;
298
299         is_tdm = rsnd_runtime_is_ssi_tdm(io);
300
301         /*
302          * always use 32bit system word.
303          * see also rsnd_ssi_master_clk_enable()
304          */
305         cr_own = FORCE | SWL_32;
306
307         if (rdai->bit_clk_inv)
308                 cr_own |= SCKP;
309         if (rdai->frm_clk_inv ^ is_tdm)
310                 cr_own |= SWSP;
311         if (rdai->data_alignment)
312                 cr_own |= SDTA;
313         if (rdai->sys_delay)
314                 cr_own |= DEL;
315         if (rsnd_io_is_play(io))
316                 cr_own |= TRMD;
317
318         switch (runtime->sample_bits) {
319         case 16:
320                 cr_own |= DWL_16;
321                 break;
322         case 32:
323                 cr_own |= DWL_24;
324                 break;
325         }
326
327         if (rsnd_ssi_is_dma_mode(mod)) {
328                 cr_mode = UIEN | OIEN | /* over/under run */
329                           DMEN;         /* DMA : enable DMA */
330         } else {
331                 cr_mode = DIEN;         /* PIO : enable Data interrupt */
332         }
333
334         /*
335          * TDM Extend Mode
336          * see
337          *      rsnd_ssiu_init_gen2()
338          */
339         wsr = ssi->wsr;
340         if (is_tdm) {
341                 wsr     |= WS_MODE;
342                 cr_own  |= CHNL_8;
343         }
344
345         ssi->cr_own     = cr_own;
346         ssi->cr_mode    = cr_mode;
347         ssi->wsr        = wsr;
348 }
349
350 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
351 {
352         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
353
354         rsnd_mod_write(mod, SSIWSR,     ssi->wsr);
355         rsnd_mod_write(mod, SSICR,      ssi->cr_own     |
356                                         ssi->cr_clk     |
357                                         ssi->cr_mode); /* without EN */
358 }
359
360 /*
361  *      SSI mod common functions
362  */
363 static int rsnd_ssi_init(struct rsnd_mod *mod,
364                          struct rsnd_dai_stream *io,
365                          struct rsnd_priv *priv)
366 {
367         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
368         int ret;
369
370         if (!rsnd_ssi_is_run_mods(mod, io))
371                 return 0;
372
373         ssi->usrcnt++;
374
375         rsnd_mod_power_on(mod);
376
377         ret = rsnd_ssi_master_clk_start(mod, io);
378         if (ret < 0)
379                 return ret;
380
381         if (!rsnd_ssi_is_parent(mod, io))
382                 rsnd_ssi_config_init(mod, io);
383
384         rsnd_ssi_register_setup(mod);
385
386         /* clear error status */
387         rsnd_ssi_status_clear(mod);
388
389         return 0;
390 }
391
392 static int rsnd_ssi_quit(struct rsnd_mod *mod,
393                          struct rsnd_dai_stream *io,
394                          struct rsnd_priv *priv)
395 {
396         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
397         struct device *dev = rsnd_priv_to_dev(priv);
398
399         if (!rsnd_ssi_is_run_mods(mod, io))
400                 return 0;
401
402         if (!ssi->usrcnt) {
403                 dev_err(dev, "%s[%d] usrcnt error\n",
404                         rsnd_mod_name(mod), rsnd_mod_id(mod));
405                 return -EIO;
406         }
407
408         if (!rsnd_ssi_is_parent(mod, io))
409                 ssi->cr_own     = 0;
410
411         rsnd_ssi_master_clk_stop(mod, io);
412
413         rsnd_mod_power_off(mod);
414
415         ssi->usrcnt--;
416
417         return 0;
418 }
419
420 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
421                               struct rsnd_dai_stream *io,
422                               struct snd_pcm_substream *substream,
423                               struct snd_pcm_hw_params *params)
424 {
425         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
426         int chan = params_channels(params);
427
428         /*
429          * snd_pcm_ops::hw_params will be called *before*
430          * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
431          * in 1st call.
432          */
433         if (ssi->usrcnt) {
434                 /*
435                  * Already working.
436                  * It will happen if SSI has parent/child connection.
437                  * it is error if child <-> parent SSI uses
438                  * different channels.
439                  */
440                 if (ssi->chan != chan)
441                         return -EIO;
442         }
443
444         ssi->chan = chan;
445
446         return 0;
447 }
448
449 static int rsnd_ssi_start(struct rsnd_mod *mod,
450                           struct rsnd_dai_stream *io,
451                           struct rsnd_priv *priv)
452 {
453         if (!rsnd_ssi_is_run_mods(mod, io))
454                 return 0;
455
456         /*
457          * EN will be set via SSIU :: SSI_CONTROL
458          * if Multi channel mode
459          */
460         if (rsnd_ssi_multi_slaves_runtime(io))
461                 return 0;
462
463         rsnd_mod_bset(mod, SSICR, EN, EN);
464
465         return 0;
466 }
467
468 static int rsnd_ssi_stop(struct rsnd_mod *mod,
469                          struct rsnd_dai_stream *io,
470                          struct rsnd_priv *priv)
471 {
472         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
473         u32 cr;
474
475         if (!rsnd_ssi_is_run_mods(mod, io))
476                 return 0;
477
478         /*
479          * don't stop if not last user
480          * see also
481          *      rsnd_ssi_start
482          *      rsnd_ssi_interrupt
483          */
484         if (ssi->usrcnt > 1)
485                 return 0;
486
487         /*
488          * disable all IRQ,
489          * and, wait all data was sent
490          */
491         cr  =   ssi->cr_own     |
492                 ssi->cr_clk;
493
494         rsnd_mod_write(mod, SSICR, cr | EN);
495         rsnd_ssi_status_check(mod, DIRQ);
496
497         /*
498          * disable SSI,
499          * and, wait idle state
500          */
501         rsnd_mod_write(mod, SSICR, cr); /* disabled all */
502         rsnd_ssi_status_check(mod, IIRQ);
503
504         return 0;
505 }
506
507 static int rsnd_ssi_irq(struct rsnd_mod *mod,
508                         struct rsnd_dai_stream *io,
509                         struct rsnd_priv *priv,
510                         int enable)
511 {
512         u32 val = 0;
513
514         if (rsnd_is_gen1(priv))
515                 return 0;
516
517         if (rsnd_ssi_is_parent(mod, io))
518                 return 0;
519
520         if (!rsnd_ssi_is_run_mods(mod, io))
521                 return 0;
522
523         if (enable)
524                 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
525
526         rsnd_mod_write(mod, SSI_INT_ENABLE, val);
527
528         return 0;
529 }
530
531 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
532                                  struct rsnd_dai_stream *io)
533 {
534         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
535         int is_dma = rsnd_ssi_is_dma_mode(mod);
536         u32 status;
537         bool elapsed = false;
538         bool stop = false;
539
540         spin_lock(&priv->lock);
541
542         /* ignore all cases if not working */
543         if (!rsnd_io_is_working(io))
544                 goto rsnd_ssi_interrupt_out;
545
546         status = rsnd_ssi_status_get(mod);
547
548         /* PIO only */
549         if (!is_dma && (status & DIRQ)) {
550                 struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
551                 u32 *buf = (u32 *)(runtime->dma_area +
552                                    rsnd_dai_pointer_offset(io, 0));
553                 int shift = 0;
554
555                 switch (runtime->sample_bits) {
556                 case 32:
557                         shift = 8;
558                         break;
559                 }
560
561                 /*
562                  * 8/16/32 data can be assesse to TDR/RDR register
563                  * directly as 32bit data
564                  * see rsnd_ssi_init()
565                  */
566                 if (rsnd_io_is_play(io))
567                         rsnd_mod_write(mod, SSITDR, (*buf) << shift);
568                 else
569                         *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
570
571                 elapsed = rsnd_dai_pointer_update(io, sizeof(*buf));
572         }
573
574         /* DMA only */
575         if (is_dma && (status & (UIRQ | OIRQ)))
576                 stop = true;
577
578         rsnd_ssi_status_clear(mod);
579 rsnd_ssi_interrupt_out:
580         spin_unlock(&priv->lock);
581
582         if (elapsed)
583                 rsnd_dai_period_elapsed(io);
584
585         if (stop)
586                 snd_pcm_stop_xrun(io->substream);
587
588 }
589
590 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
591 {
592         struct rsnd_mod *mod = data;
593
594         rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
595
596         return IRQ_HANDLED;
597 }
598
599 /*
600  *              SSI PIO
601  */
602 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
603                                    struct rsnd_dai_stream *io)
604 {
605         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
606         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
607
608         if (!__rsnd_ssi_is_pin_sharing(mod))
609                 return;
610
611         if (!rsnd_rdai_is_clk_master(rdai))
612                 return;
613
614         switch (rsnd_mod_id(mod)) {
615         case 1:
616         case 2:
617                 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
618                 break;
619         case 4:
620                 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
621                 break;
622         case 8:
623                 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
624                 break;
625         }
626 }
627
628 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
629                             struct rsnd_dai_stream *io,
630                             struct snd_soc_pcm_runtime *rtd)
631 {
632         /*
633          * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
634          * and, pcm_new will be called after it.
635          * This function reuse pcm_new at this point.
636          */
637         rsnd_ssi_parent_attach(mod, io);
638
639         return 0;
640 }
641
642 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
643                                  struct rsnd_dai_stream *io,
644                                  struct rsnd_priv *priv)
645 {
646         struct device *dev = rsnd_priv_to_dev(priv);
647         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
648         int ret;
649
650         /*
651          * SSIP/SSIU/IRQ are not needed on
652          * SSI Multi slaves
653          */
654         if (rsnd_ssi_is_multi_slave(mod, io))
655                 return 0;
656
657         /*
658          * It can't judge ssi parent at this point
659          * see rsnd_ssi_pcm_new()
660          */
661
662         ret = rsnd_ssiu_attach(io, mod);
663         if (ret < 0)
664                 return ret;
665
666         /*
667          * SSI might be called again as PIO fallback
668          * It is easy to manual handling for IRQ request/free
669          */
670         ret = request_irq(ssi->irq,
671                           rsnd_ssi_interrupt,
672                           IRQF_SHARED,
673                           dev_name(dev), mod);
674
675         return ret;
676 }
677
678 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
679         .name   = SSI_NAME,
680         .probe  = rsnd_ssi_common_probe,
681         .init   = rsnd_ssi_init,
682         .quit   = rsnd_ssi_quit,
683         .start  = rsnd_ssi_start,
684         .stop   = rsnd_ssi_stop,
685         .irq    = rsnd_ssi_irq,
686         .pcm_new = rsnd_ssi_pcm_new,
687         .hw_params = rsnd_ssi_hw_params,
688 };
689
690 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
691                               struct rsnd_dai_stream *io,
692                               struct rsnd_priv *priv)
693 {
694         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
695         int ret;
696
697         /*
698          * SSIP/SSIU/IRQ/DMA are not needed on
699          * SSI Multi slaves
700          */
701         if (rsnd_ssi_is_multi_slave(mod, io))
702                 return 0;
703
704         ret = rsnd_ssi_common_probe(mod, io, priv);
705         if (ret)
706                 return ret;
707
708         /* SSI probe might be called many times in MUX multi path */
709         ret = rsnd_dma_attach(io, mod, &ssi->dma);
710
711         return ret;
712 }
713
714 static int rsnd_ssi_dma_remove(struct rsnd_mod *mod,
715                                struct rsnd_dai_stream *io,
716                                struct rsnd_priv *priv)
717 {
718         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
719         struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
720
721         /* Do nothing for SSI parent mod */
722         if (ssi_parent_mod == mod)
723                 return 0;
724
725         /* PIO will request IRQ again */
726         free_irq(ssi->irq, mod);
727
728         return 0;
729 }
730
731 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
732                              struct rsnd_dai_stream *io,
733                              struct rsnd_priv *priv)
734 {
735         struct device *dev = rsnd_priv_to_dev(priv);
736
737         /*
738          * fallback to PIO
739          *
740          * SSI .probe might be called again.
741          * see
742          *      rsnd_rdai_continuance_probe()
743          */
744         mod->ops = &rsnd_ssi_pio_ops;
745
746         dev_info(dev, "%s[%d] fallback to PIO mode\n",
747                  rsnd_mod_name(mod), rsnd_mod_id(mod));
748
749         return 0;
750 }
751
752 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
753                                          struct rsnd_mod *mod)
754 {
755         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
756         int is_play = rsnd_io_is_play(io);
757         char *name;
758
759         if (rsnd_ssi_use_busif(io))
760                 name = is_play ? "rxu" : "txu";
761         else
762                 name = is_play ? "rx" : "tx";
763
764         return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
765                                         mod, name);
766 }
767
768 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
769         .name   = SSI_NAME,
770         .dma_req = rsnd_ssi_dma_req,
771         .probe  = rsnd_ssi_dma_probe,
772         .remove = rsnd_ssi_dma_remove,
773         .init   = rsnd_ssi_init,
774         .quit   = rsnd_ssi_quit,
775         .start  = rsnd_ssi_start,
776         .stop   = rsnd_ssi_stop,
777         .irq    = rsnd_ssi_irq,
778         .pcm_new = rsnd_ssi_pcm_new,
779         .fallback = rsnd_ssi_fallback,
780         .hw_params = rsnd_ssi_hw_params,
781 };
782
783 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
784 {
785         return mod->ops == &rsnd_ssi_dma_ops;
786 }
787
788
789 /*
790  *              Non SSI
791  */
792 static struct rsnd_mod_ops rsnd_ssi_non_ops = {
793         .name   = SSI_NAME,
794 };
795
796 /*
797  *              ssi mod function
798  */
799 static void rsnd_ssi_connect(struct rsnd_mod *mod,
800                              struct rsnd_dai_stream *io)
801 {
802         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
803         enum rsnd_mod_type types[] = {
804                 RSND_MOD_SSI,
805                 RSND_MOD_SSIM1,
806                 RSND_MOD_SSIM2,
807                 RSND_MOD_SSIM3,
808         };
809         enum rsnd_mod_type type;
810         int i;
811
812         /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
813         for (i = 0; i < ARRAY_SIZE(types); i++) {
814                 type = types[i];
815                 if (!rsnd_io_to_mod(io, type)) {
816                         rsnd_dai_connect(mod, io, type);
817                         rsnd_set_slot(rdai, 2 * (i + 1), (i + 1));
818                         return;
819                 }
820         }
821 }
822
823 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
824                             struct device_node *playback,
825                             struct device_node *capture)
826 {
827         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
828         struct device_node *node;
829         struct device_node *np;
830         struct rsnd_mod *mod;
831         int i;
832
833         node = rsnd_ssi_of_node(priv);
834         if (!node)
835                 return;
836
837         i = 0;
838         for_each_child_of_node(node, np) {
839                 mod = rsnd_ssi_mod_get(priv, i);
840                 if (np == playback)
841                         rsnd_ssi_connect(mod, &rdai->playback);
842                 if (np == capture)
843                         rsnd_ssi_connect(mod, &rdai->capture);
844                 i++;
845         }
846
847         of_node_put(node);
848 }
849
850 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
851 {
852         if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
853                 id = 0;
854
855         return rsnd_mod_get(rsnd_ssi_get(priv, id));
856 }
857
858 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
859 {
860         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
861
862         return !!(rsnd_ssi_mode_flags(ssi) & RSND_SSI_CLK_PIN_SHARE);
863 }
864
865 static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
866                                 struct rsnd_mod *mod,
867                                 enum rsnd_mod_type type)
868 {
869         /*
870          * SSIP (= SSI parent) needs to be special, otherwise,
871          * 2nd SSI might doesn't start. see also rsnd_mod_call()
872          *
873          * We can't include parent SSI status on SSI, because we don't know
874          * how many SSI requests parent SSI. Thus, it is localed on "io" now.
875          * ex) trouble case
876          *      Playback: SSI0
877          *      Capture : SSI1 (needs SSI0)
878          *
879          * 1) start Capture  -> SSI0/SSI1 are started.
880          * 2) start Playback -> SSI0 doesn't work, because it is already
881          *                      marked as "started" on 1)
882          *
883          * OTOH, using each mod's status is good for MUX case.
884          * It doesn't need to start in 2nd start
885          * ex)
886          *      IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
887          *                          |
888          *      IO-1: SRC1 -> CTU2 -+
889          *
890          * 1) start IO-0 ->     start SSI0
891          * 2) start IO-1 ->     SSI0 doesn't need to start, because it is
892          *                      already started on 1)
893          */
894         if (type == RSND_MOD_SSIP)
895                 return &io->parent_ssi_status;
896
897         return rsnd_mod_get_status(io, mod, type);
898 }
899
900 int rsnd_ssi_probe(struct rsnd_priv *priv)
901 {
902         struct device_node *node;
903         struct device_node *np;
904         struct device *dev = rsnd_priv_to_dev(priv);
905         struct rsnd_mod_ops *ops;
906         struct clk *clk;
907         struct rsnd_ssi *ssi;
908         char name[RSND_SSI_NAME_SIZE];
909         int i, nr, ret;
910
911         node = rsnd_ssi_of_node(priv);
912         if (!node)
913                 return -EINVAL;
914
915         nr = of_get_child_count(node);
916         if (!nr) {
917                 ret = -EINVAL;
918                 goto rsnd_ssi_probe_done;
919         }
920
921         ssi     = devm_kzalloc(dev, sizeof(*ssi) * nr, GFP_KERNEL);
922         if (!ssi) {
923                 ret = -ENOMEM;
924                 goto rsnd_ssi_probe_done;
925         }
926
927         priv->ssi       = ssi;
928         priv->ssi_nr    = nr;
929
930         i = 0;
931         for_each_child_of_node(node, np) {
932                 ssi = rsnd_ssi_get(priv, i);
933
934                 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
935                          SSI_NAME, i);
936
937                 clk = devm_clk_get(dev, name);
938                 if (IS_ERR(clk)) {
939                         ret = PTR_ERR(clk);
940                         goto rsnd_ssi_probe_done;
941                 }
942
943                 if (of_get_property(np, "shared-pin", NULL))
944                         ssi->flags |= RSND_SSI_CLK_PIN_SHARE;
945
946                 if (of_get_property(np, "no-busif", NULL))
947                         ssi->flags |= RSND_SSI_NO_BUSIF;
948
949                 ssi->irq = irq_of_parse_and_map(np, 0);
950                 if (!ssi->irq) {
951                         ret = -EINVAL;
952                         goto rsnd_ssi_probe_done;
953                 }
954
955                 ops = &rsnd_ssi_non_ops;
956                 if (of_property_read_bool(np, "pio-transfer"))
957                         ops = &rsnd_ssi_pio_ops;
958                 else
959                         ops = &rsnd_ssi_dma_ops;
960
961                 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
962                                     rsnd_ssi_get_status, RSND_MOD_SSI, i);
963                 if (ret)
964                         goto rsnd_ssi_probe_done;
965
966                 i++;
967         }
968
969         ret = 0;
970
971 rsnd_ssi_probe_done:
972         of_node_put(node);
973
974         return ret;
975 }
976
977 void rsnd_ssi_remove(struct rsnd_priv *priv)
978 {
979         struct rsnd_ssi *ssi;
980         int i;
981
982         for_each_rsnd_ssi(ssi, priv, i) {
983                 rsnd_mod_quit(rsnd_mod_get(ssi));
984         }
985 }