GNU Linux-libre 4.19.264-gnu1
[releases.git] / sound / soc / sh / rcar / ssi.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Renesas R-Car SSIU/SSI support
4 //
5 // Copyright (C) 2013 Renesas Solutions Corp.
6 // Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
7 //
8 // Based on fsi.c
9 // Kuninori Morimoto <morimoto.kuninori@renesas.com>
10
11 /*
12  * you can enable below define if you don't need
13  * SSI interrupt status debug message when debugging
14  * see rsnd_dbg_irq_status()
15  *
16  * #define RSND_DEBUG_NO_IRQ_STATUS 1
17  */
18
19 #include <sound/simple_card_utils.h>
20 #include <linux/delay.h>
21 #include "rsnd.h"
22 #define RSND_SSI_NAME_SIZE 16
23
24 /*
25  * SSICR
26  */
27 #define FORCE           (1 << 31)       /* Fixed */
28 #define DMEN            (1 << 28)       /* DMA Enable */
29 #define UIEN            (1 << 27)       /* Underflow Interrupt Enable */
30 #define OIEN            (1 << 26)       /* Overflow Interrupt Enable */
31 #define IIEN            (1 << 25)       /* Idle Mode Interrupt Enable */
32 #define DIEN            (1 << 24)       /* Data Interrupt Enable */
33 #define CHNL_4          (1 << 22)       /* Channels */
34 #define CHNL_6          (2 << 22)       /* Channels */
35 #define CHNL_8          (3 << 22)       /* Channels */
36 #define DWL_MASK        (7 << 19)       /* Data Word Length mask */
37 #define DWL_8           (0 << 19)       /* Data Word Length */
38 #define DWL_16          (1 << 19)       /* Data Word Length */
39 #define DWL_18          (2 << 19)       /* Data Word Length */
40 #define DWL_20          (3 << 19)       /* Data Word Length */
41 #define DWL_22          (4 << 19)       /* Data Word Length */
42 #define DWL_24          (5 << 19)       /* Data Word Length */
43 #define DWL_32          (6 << 19)       /* Data Word Length */
44
45 #define SWL_32          (3 << 16)       /* R/W System Word Length */
46 #define SCKD            (1 << 15)       /* Serial Bit Clock Direction */
47 #define SWSD            (1 << 14)       /* Serial WS Direction */
48 #define SCKP            (1 << 13)       /* Serial Bit Clock Polarity */
49 #define SWSP            (1 << 12)       /* Serial WS Polarity */
50 #define SDTA            (1 << 10)       /* Serial Data Alignment */
51 #define PDTA            (1 <<  9)       /* Parallel Data Alignment */
52 #define DEL             (1 <<  8)       /* Serial Data Delay */
53 #define CKDV(v)         (v <<  4)       /* Serial Clock Division Ratio */
54 #define TRMD            (1 <<  1)       /* Transmit/Receive Mode Select */
55 #define EN              (1 <<  0)       /* SSI Module Enable */
56
57 /*
58  * SSISR
59  */
60 #define UIRQ            (1 << 27)       /* Underflow Error Interrupt Status */
61 #define OIRQ            (1 << 26)       /* Overflow Error Interrupt Status */
62 #define IIRQ            (1 << 25)       /* Idle Mode Interrupt Status */
63 #define DIRQ            (1 << 24)       /* Data Interrupt Status Flag */
64
65 /*
66  * SSIWSR
67  */
68 #define CONT            (1 << 8)        /* WS Continue Function */
69 #define WS_MODE         (1 << 0)        /* WS Mode */
70
71 #define SSI_NAME "ssi"
72
73 struct rsnd_ssi {
74         struct rsnd_mod mod;
75
76         u32 flags;
77         u32 cr_own;
78         u32 cr_clk;
79         u32 cr_mode;
80         u32 cr_en;
81         u32 wsr;
82         int chan;
83         int rate;
84         int irq;
85         unsigned int usrcnt;
86
87         /* for PIO */
88         int byte_pos;
89         int byte_per_period;
90         int next_period_byte;
91 };
92
93 /* flags */
94 #define RSND_SSI_CLK_PIN_SHARE          (1 << 0)
95 #define RSND_SSI_NO_BUSIF               (1 << 1) /* SSI+DMA without BUSIF */
96 #define RSND_SSI_HDMI0                  (1 << 2) /* for HDMI0 */
97 #define RSND_SSI_HDMI1                  (1 << 3) /* for HDMI1 */
98 #define RSND_SSI_PROBED                 (1 << 4)
99
100 #define for_each_rsnd_ssi(pos, priv, i)                                 \
101         for (i = 0;                                                     \
102              (i < rsnd_ssi_nr(priv)) &&                                 \
103                 ((pos) = ((struct rsnd_ssi *)(priv)->ssi + i));         \
104              i++)
105
106 #define rsnd_ssi_get(priv, id) ((struct rsnd_ssi *)(priv->ssi) + id)
107 #define rsnd_ssi_nr(priv) ((priv)->ssi_nr)
108 #define rsnd_mod_to_ssi(_mod) container_of((_mod), struct rsnd_ssi, mod)
109 #define rsnd_ssi_is_parent(ssi, io) ((ssi) == rsnd_io_to_mod_ssip(io))
110 #define rsnd_ssi_is_multi_slave(mod, io) \
111         (rsnd_ssi_multi_slaves(io) & (1 << rsnd_mod_id(mod)))
112 #define rsnd_ssi_is_run_mods(mod, io) \
113         (rsnd_ssi_run_mods(io) & (1 << rsnd_mod_id(mod)))
114 #define rsnd_ssi_can_output_clk(mod) (!__rsnd_ssi_is_pin_sharing(mod))
115
116 int rsnd_ssi_hdmi_port(struct rsnd_dai_stream *io)
117 {
118         struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
119         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
120
121         if (rsnd_flags_has(ssi, RSND_SSI_HDMI0))
122                 return RSND_SSI_HDMI_PORT0;
123
124         if (rsnd_flags_has(ssi, RSND_SSI_HDMI1))
125                 return RSND_SSI_HDMI_PORT1;
126
127         return 0;
128 }
129
130 int rsnd_ssi_use_busif(struct rsnd_dai_stream *io)
131 {
132         struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
133         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
134         int use_busif = 0;
135
136         if (!rsnd_ssi_is_dma_mode(mod))
137                 return 0;
138
139         if (!(rsnd_flags_has(ssi, RSND_SSI_NO_BUSIF)))
140                 use_busif = 1;
141         if (rsnd_io_to_mod_src(io))
142                 use_busif = 1;
143
144         return use_busif;
145 }
146
147 static void rsnd_ssi_status_clear(struct rsnd_mod *mod)
148 {
149         rsnd_mod_write(mod, SSISR, 0);
150 }
151
152 static u32 rsnd_ssi_status_get(struct rsnd_mod *mod)
153 {
154         return rsnd_mod_read(mod, SSISR);
155 }
156
157 static void rsnd_ssi_status_check(struct rsnd_mod *mod,
158                                   u32 bit)
159 {
160         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
161         struct device *dev = rsnd_priv_to_dev(priv);
162         u32 status;
163         int i;
164
165         for (i = 0; i < 1024; i++) {
166                 status = rsnd_ssi_status_get(mod);
167                 if (status & bit)
168                         return;
169
170                 udelay(5);
171         }
172
173         dev_warn(dev, "%s[%d] status check failed\n",
174                  rsnd_mod_name(mod), rsnd_mod_id(mod));
175 }
176
177 static u32 rsnd_ssi_multi_slaves(struct rsnd_dai_stream *io)
178 {
179         struct rsnd_mod *mod;
180         enum rsnd_mod_type types[] = {
181                 RSND_MOD_SSIM1,
182                 RSND_MOD_SSIM2,
183                 RSND_MOD_SSIM3,
184         };
185         int i, mask;
186
187         mask = 0;
188         for (i = 0; i < ARRAY_SIZE(types); i++) {
189                 mod = rsnd_io_to_mod(io, types[i]);
190                 if (!mod)
191                         continue;
192
193                 mask |= 1 << rsnd_mod_id(mod);
194         }
195
196         return mask;
197 }
198
199 static u32 rsnd_ssi_run_mods(struct rsnd_dai_stream *io)
200 {
201         struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
202         struct rsnd_mod *ssi_parent_mod = rsnd_io_to_mod_ssip(io);
203         u32 mods;
204
205         mods = rsnd_ssi_multi_slaves_runtime(io) |
206                 1 << rsnd_mod_id(ssi_mod);
207
208         if (ssi_parent_mod)
209                 mods |= 1 << rsnd_mod_id(ssi_parent_mod);
210
211         return mods;
212 }
213
214 u32 rsnd_ssi_multi_slaves_runtime(struct rsnd_dai_stream *io)
215 {
216         if (rsnd_runtime_is_ssi_multi(io))
217                 return rsnd_ssi_multi_slaves(io);
218
219         return 0;
220 }
221
222 unsigned int rsnd_ssi_clk_query(struct rsnd_priv *priv,
223                        int param1, int param2, int *idx)
224 {
225         int ssi_clk_mul_table[] = {
226                 1, 2, 4, 8, 16, 6, 12,
227         };
228         int j, ret;
229         unsigned int main_rate;
230
231         for (j = 0; j < ARRAY_SIZE(ssi_clk_mul_table); j++) {
232
233                 /*
234                  * It will set SSIWSR.CONT here, but SSICR.CKDV = 000
235                  * with it is not allowed. (SSIWSR.WS_MODE with
236                  * SSICR.CKDV = 000 is not allowed either).
237                  * Skip it. See SSICR.CKDV
238                  */
239                 if (j == 0)
240                         continue;
241
242                 /*
243                  * this driver is assuming that
244                  * system word is 32bit x chan
245                  * see rsnd_ssi_init()
246                  */
247                 main_rate = 32 * param1 * param2 * ssi_clk_mul_table[j];
248
249                 ret = rsnd_adg_clk_query(priv, main_rate);
250                 if (ret < 0)
251                         continue;
252
253                 if (idx)
254                         *idx = j;
255
256                 return main_rate;
257         }
258
259         return 0;
260 }
261
262 static int rsnd_ssi_master_clk_start(struct rsnd_mod *mod,
263                                      struct rsnd_dai_stream *io)
264 {
265         struct rsnd_priv *priv = rsnd_io_to_priv(io);
266         struct device *dev = rsnd_priv_to_dev(priv);
267         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
268         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
269         int chan = rsnd_runtime_channel_for_ssi(io);
270         int idx, ret;
271         unsigned int main_rate;
272         unsigned int rate = rsnd_io_is_play(io) ?
273                 rsnd_src_get_out_rate(priv, io) :
274                 rsnd_src_get_in_rate(priv, io);
275
276         if (!rsnd_rdai_is_clk_master(rdai))
277                 return 0;
278
279         if (!rsnd_ssi_can_output_clk(mod))
280                 return 0;
281
282         if (rsnd_ssi_is_multi_slave(mod, io))
283                 return 0;
284
285         if (ssi->usrcnt > 0) {
286                 if (ssi->rate != rate) {
287                         dev_err(dev, "SSI parent/child should use same rate\n");
288                         return -EINVAL;
289                 }
290
291                 return 0;
292         }
293
294         main_rate = rsnd_ssi_clk_query(priv, rate, chan, &idx);
295         if (!main_rate) {
296                 dev_err(dev, "unsupported clock rate\n");
297                 return -EIO;
298         }
299
300         ret = rsnd_adg_ssi_clk_try_start(mod, main_rate);
301         if (ret < 0)
302                 return ret;
303
304         /*
305          * SSI clock will be output contiguously
306          * by below settings.
307          * This means, rsnd_ssi_master_clk_start()
308          * and rsnd_ssi_register_setup() are necessary
309          * for SSI parent
310          *
311          * SSICR  : FORCE, SCKD, SWSD
312          * SSIWSR : CONT
313          */
314         ssi->cr_clk = FORCE | SWL_32 | SCKD | SWSD | CKDV(idx);
315         ssi->wsr = CONT;
316         ssi->rate = rate;
317
318         dev_dbg(dev, "%s[%d] outputs %u Hz\n",
319                 rsnd_mod_name(mod),
320                 rsnd_mod_id(mod), rate);
321
322         return 0;
323 }
324
325 static void rsnd_ssi_master_clk_stop(struct rsnd_mod *mod,
326                                      struct rsnd_dai_stream *io)
327 {
328         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
329         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
330
331         if (!rsnd_rdai_is_clk_master(rdai))
332                 return;
333
334         if (!rsnd_ssi_can_output_clk(mod))
335                 return;
336
337         if (ssi->usrcnt > 1)
338                 return;
339
340         ssi->cr_clk     = 0;
341         ssi->rate       = 0;
342
343         rsnd_adg_ssi_clk_stop(mod);
344 }
345
346 static void rsnd_ssi_config_init(struct rsnd_mod *mod,
347                                 struct rsnd_dai_stream *io)
348 {
349         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
350         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
351         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
352         u32 cr_own      = ssi->cr_own;
353         u32 cr_mode     = ssi->cr_mode;
354         u32 wsr         = ssi->wsr;
355         int is_tdm;
356
357         is_tdm = rsnd_runtime_is_ssi_tdm(io);
358
359         /*
360          * always use 32bit system word.
361          * see also rsnd_ssi_master_clk_enable()
362          */
363         cr_own |= FORCE | SWL_32;
364
365         if (rdai->bit_clk_inv)
366                 cr_own |= SCKP;
367         if (rdai->frm_clk_inv ^ is_tdm)
368                 cr_own |= SWSP;
369         if (rdai->data_alignment)
370                 cr_own |= SDTA;
371         if (rdai->sys_delay)
372                 cr_own |= DEL;
373
374         /*
375          * We shouldn't exchange SWSP after running.
376          * This means, parent needs to care it.
377          */
378         if (rsnd_ssi_is_parent(mod, io))
379                 goto init_end;
380
381         if (rsnd_io_is_play(io))
382                 cr_own |= TRMD;
383
384         cr_own &= ~DWL_MASK;
385         switch (snd_pcm_format_width(runtime->format)) {
386         case 16:
387                 cr_own |= DWL_16;
388                 break;
389         case 24:
390                 cr_own |= DWL_24;
391                 break;
392         }
393
394         if (rsnd_ssi_is_dma_mode(mod)) {
395                 cr_mode = UIEN | OIEN | /* over/under run */
396                           DMEN;         /* DMA : enable DMA */
397         } else {
398                 cr_mode = DIEN;         /* PIO : enable Data interrupt */
399         }
400
401         /*
402          * TDM Extend Mode
403          * see
404          *      rsnd_ssiu_init_gen2()
405          */
406         wsr = ssi->wsr;
407         if (is_tdm) {
408                 wsr     |= WS_MODE;
409                 cr_own  |= CHNL_8;
410         }
411 init_end:
412         ssi->cr_own     = cr_own;
413         ssi->cr_mode    = cr_mode;
414         ssi->wsr        = wsr;
415 }
416
417 static void rsnd_ssi_register_setup(struct rsnd_mod *mod)
418 {
419         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
420
421         rsnd_mod_write(mod, SSIWSR,     ssi->wsr);
422         rsnd_mod_write(mod, SSICR,      ssi->cr_own     |
423                                         ssi->cr_clk     |
424                                         ssi->cr_mode    |
425                                         ssi->cr_en);
426 }
427
428 /*
429  *      SSI mod common functions
430  */
431 static int rsnd_ssi_init(struct rsnd_mod *mod,
432                          struct rsnd_dai_stream *io,
433                          struct rsnd_priv *priv)
434 {
435         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
436
437         if (!rsnd_ssi_is_run_mods(mod, io))
438                 return 0;
439
440         ssi->usrcnt++;
441
442         rsnd_mod_power_on(mod);
443
444         rsnd_ssi_config_init(mod, io);
445
446         rsnd_ssi_register_setup(mod);
447
448         /* clear error status */
449         rsnd_ssi_status_clear(mod);
450
451         return 0;
452 }
453
454 static int rsnd_ssi_quit(struct rsnd_mod *mod,
455                          struct rsnd_dai_stream *io,
456                          struct rsnd_priv *priv)
457 {
458         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
459         struct device *dev = rsnd_priv_to_dev(priv);
460
461         if (!rsnd_ssi_is_run_mods(mod, io))
462                 return 0;
463
464         if (!ssi->usrcnt) {
465                 dev_err(dev, "%s[%d] usrcnt error\n",
466                         rsnd_mod_name(mod), rsnd_mod_id(mod));
467                 return -EIO;
468         }
469
470         rsnd_ssi_master_clk_stop(mod, io);
471
472         rsnd_mod_power_off(mod);
473
474         ssi->usrcnt--;
475
476         if (!ssi->usrcnt) {
477                 ssi->cr_own     = 0;
478                 ssi->cr_mode    = 0;
479                 ssi->wsr        = 0;
480         }
481
482         return 0;
483 }
484
485 static int rsnd_ssi_hw_params(struct rsnd_mod *mod,
486                               struct rsnd_dai_stream *io,
487                               struct snd_pcm_substream *substream,
488                               struct snd_pcm_hw_params *params)
489 {
490         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
491         int chan = params_channels(params);
492
493         /*
494          * snd_pcm_ops::hw_params will be called *before*
495          * snd_soc_dai_ops::trigger. Thus, ssi->usrcnt is 0
496          * in 1st call.
497          */
498         if (ssi->usrcnt) {
499                 /*
500                  * Already working.
501                  * It will happen if SSI has parent/child connection.
502                  * it is error if child <-> parent SSI uses
503                  * different channels.
504                  */
505                 if (ssi->chan != chan)
506                         return -EIO;
507         }
508
509         ssi->chan = chan;
510
511         return 0;
512 }
513
514 static int rsnd_ssi_start(struct rsnd_mod *mod,
515                           struct rsnd_dai_stream *io,
516                           struct rsnd_priv *priv)
517 {
518         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
519
520         if (!rsnd_ssi_is_run_mods(mod, io))
521                 return 0;
522
523         /*
524          * EN will be set via SSIU :: SSI_CONTROL
525          * if Multi channel mode
526          */
527         if (rsnd_ssi_multi_slaves_runtime(io))
528                 return 0;
529
530         /*
531          * EN is for data output.
532          * SSI parent EN is not needed.
533          */
534         if (rsnd_ssi_is_parent(mod, io))
535                 return 0;
536
537         ssi->cr_en = EN;
538
539         rsnd_mod_write(mod, SSICR,      ssi->cr_own     |
540                                         ssi->cr_clk     |
541                                         ssi->cr_mode    |
542                                         ssi->cr_en);
543
544         return 0;
545 }
546
547 static int rsnd_ssi_stop(struct rsnd_mod *mod,
548                          struct rsnd_dai_stream *io,
549                          struct rsnd_priv *priv)
550 {
551         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
552         u32 cr;
553
554         if (!rsnd_ssi_is_run_mods(mod, io))
555                 return 0;
556
557         if (rsnd_ssi_is_parent(mod, io))
558                 return 0;
559
560         cr  =   ssi->cr_own     |
561                 ssi->cr_clk;
562
563         /*
564          * disable all IRQ,
565          * Playback: Wait all data was sent
566          * Capture:  It might not receave data. Do nothing
567          */
568         if (rsnd_io_is_play(io)) {
569                 rsnd_mod_write(mod, SSICR, cr | ssi->cr_en);
570                 rsnd_ssi_status_check(mod, DIRQ);
571         }
572
573         /* In multi-SSI mode, stop is performed by setting ssi0129 in
574          * SSI_CONTROL to 0 (in rsnd_ssio_stop_gen2). Do nothing here.
575          */
576         if (rsnd_ssi_multi_slaves_runtime(io))
577                 return 0;
578
579         /*
580          * disable SSI,
581          * and, wait idle state
582          */
583         rsnd_mod_write(mod, SSICR, cr); /* disabled all */
584         rsnd_ssi_status_check(mod, IIRQ);
585
586         ssi->cr_en = 0;
587
588         return 0;
589 }
590
591 static int rsnd_ssi_irq(struct rsnd_mod *mod,
592                         struct rsnd_dai_stream *io,
593                         struct rsnd_priv *priv,
594                         int enable)
595 {
596         u32 val = 0;
597
598         if (rsnd_is_gen1(priv))
599                 return 0;
600
601         if (rsnd_ssi_is_parent(mod, io))
602                 return 0;
603
604         if (!rsnd_ssi_is_run_mods(mod, io))
605                 return 0;
606
607         if (enable)
608                 val = rsnd_ssi_is_dma_mode(mod) ? 0x0e000000 : 0x0f000000;
609
610         rsnd_mod_write(mod, SSI_INT_ENABLE, val);
611
612         return 0;
613 }
614
615 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
616                                    struct rsnd_dai_stream *io);
617 static void __rsnd_ssi_interrupt(struct rsnd_mod *mod,
618                                  struct rsnd_dai_stream *io)
619 {
620         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
621         struct device *dev = rsnd_priv_to_dev(priv);
622         int is_dma = rsnd_ssi_is_dma_mode(mod);
623         u32 status;
624         bool elapsed = false;
625         bool stop = false;
626
627         spin_lock(&priv->lock);
628
629         /* ignore all cases if not working */
630         if (!rsnd_io_is_working(io))
631                 goto rsnd_ssi_interrupt_out;
632
633         status = rsnd_ssi_status_get(mod);
634
635         /* PIO only */
636         if (!is_dma && (status & DIRQ))
637                 elapsed = rsnd_ssi_pio_interrupt(mod, io);
638
639         /* DMA only */
640         if (is_dma && (status & (UIRQ | OIRQ))) {
641                 rsnd_dbg_irq_status(dev, "%s[%d] err status : 0x%08x\n",
642                         rsnd_mod_name(mod), rsnd_mod_id(mod), status);
643
644                 stop = true;
645         }
646
647         rsnd_ssi_status_clear(mod);
648 rsnd_ssi_interrupt_out:
649         spin_unlock(&priv->lock);
650
651         if (elapsed)
652                 rsnd_dai_period_elapsed(io);
653
654         if (stop)
655                 snd_pcm_stop_xrun(io->substream);
656
657 }
658
659 static irqreturn_t rsnd_ssi_interrupt(int irq, void *data)
660 {
661         struct rsnd_mod *mod = data;
662
663         rsnd_mod_interrupt(mod, __rsnd_ssi_interrupt);
664
665         return IRQ_HANDLED;
666 }
667
668 /*
669  *              SSI PIO
670  */
671 static void rsnd_ssi_parent_attach(struct rsnd_mod *mod,
672                                    struct rsnd_dai_stream *io)
673 {
674         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
675         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
676
677         if (!__rsnd_ssi_is_pin_sharing(mod))
678                 return;
679
680         if (!rsnd_rdai_is_clk_master(rdai))
681                 return;
682
683         if (rsnd_ssi_is_multi_slave(mod, io))
684                 return;
685
686         switch (rsnd_mod_id(mod)) {
687         case 1:
688         case 2:
689                 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 0), io, RSND_MOD_SSIP);
690                 break;
691         case 4:
692                 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 3), io, RSND_MOD_SSIP);
693                 break;
694         case 8:
695                 rsnd_dai_connect(rsnd_ssi_mod_get(priv, 7), io, RSND_MOD_SSIP);
696                 break;
697         }
698 }
699
700 static int rsnd_ssi_pcm_new(struct rsnd_mod *mod,
701                             struct rsnd_dai_stream *io,
702                             struct snd_soc_pcm_runtime *rtd)
703 {
704         /*
705          * rsnd_rdai_is_clk_master() will be enabled after set_fmt,
706          * and, pcm_new will be called after it.
707          * This function reuse pcm_new at this point.
708          */
709         rsnd_ssi_parent_attach(mod, io);
710
711         return 0;
712 }
713
714 static int rsnd_ssi_common_probe(struct rsnd_mod *mod,
715                                  struct rsnd_dai_stream *io,
716                                  struct rsnd_priv *priv)
717 {
718         struct device *dev = rsnd_priv_to_dev(priv);
719         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
720         int ret;
721
722         /*
723          * SSIP/SSIU/IRQ are not needed on
724          * SSI Multi slaves
725          */
726         if (rsnd_ssi_is_multi_slave(mod, io))
727                 return 0;
728
729         /*
730          * It can't judge ssi parent at this point
731          * see rsnd_ssi_pcm_new()
732          */
733
734         ret = rsnd_ssiu_attach(io, mod);
735         if (ret < 0)
736                 return ret;
737
738         /*
739          * SSI might be called again as PIO fallback
740          * It is easy to manual handling for IRQ request/free
741          *
742          * OTOH, this function might be called many times if platform is
743          * using MIX. It needs xxx_attach() many times on xxx_probe().
744          * Because of it, we can't control .probe/.remove calling count by
745          * mod->status.
746          * But it don't need to call request_irq() many times.
747          * Let's control it by RSND_SSI_PROBED flag.
748          */
749         if (!rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
750                 ret = request_irq(ssi->irq,
751                                   rsnd_ssi_interrupt,
752                                   IRQF_SHARED,
753                                   dev_name(dev), mod);
754
755                 rsnd_flags_set(ssi, RSND_SSI_PROBED);
756         }
757
758         return ret;
759 }
760
761 static int rsnd_ssi_common_remove(struct rsnd_mod *mod,
762                                   struct rsnd_dai_stream *io,
763                                   struct rsnd_priv *priv)
764 {
765         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
766         struct rsnd_mod *pure_ssi_mod = rsnd_io_to_mod_ssi(io);
767
768         /* Do nothing if non SSI (= SSI parent, multi SSI) mod */
769         if (pure_ssi_mod != mod)
770                 return 0;
771
772         /* PIO will request IRQ again */
773         if (rsnd_flags_has(ssi, RSND_SSI_PROBED)) {
774                 free_irq(ssi->irq, mod);
775
776                 rsnd_flags_del(ssi, RSND_SSI_PROBED);
777         }
778
779         return 0;
780 }
781
782 /*
783  *      SSI PIO functions
784  */
785 static bool rsnd_ssi_pio_interrupt(struct rsnd_mod *mod,
786                                    struct rsnd_dai_stream *io)
787 {
788         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
789         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
790         u32 *buf = (u32 *)(runtime->dma_area + ssi->byte_pos);
791         int shift = 0;
792         int byte_pos;
793         bool elapsed = false;
794
795         if (snd_pcm_format_width(runtime->format) == 24)
796                 shift = 8;
797
798         /*
799          * 8/16/32 data can be assesse to TDR/RDR register
800          * directly as 32bit data
801          * see rsnd_ssi_init()
802          */
803         if (rsnd_io_is_play(io))
804                 rsnd_mod_write(mod, SSITDR, (*buf) << shift);
805         else
806                 *buf = (rsnd_mod_read(mod, SSIRDR) >> shift);
807
808         byte_pos = ssi->byte_pos + sizeof(*buf);
809
810         if (byte_pos >= ssi->next_period_byte) {
811                 int period_pos = byte_pos / ssi->byte_per_period;
812
813                 if (period_pos >= runtime->periods) {
814                         byte_pos = 0;
815                         period_pos = 0;
816                 }
817
818                 ssi->next_period_byte = (period_pos + 1) * ssi->byte_per_period;
819
820                 elapsed = true;
821         }
822
823         WRITE_ONCE(ssi->byte_pos, byte_pos);
824
825         return elapsed;
826 }
827
828 static int rsnd_ssi_pio_init(struct rsnd_mod *mod,
829                              struct rsnd_dai_stream *io,
830                              struct rsnd_priv *priv)
831 {
832         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
833         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
834
835         if (!rsnd_ssi_is_parent(mod, io)) {
836                 ssi->byte_pos           = 0;
837                 ssi->byte_per_period    = runtime->period_size *
838                                           runtime->channels *
839                                           samples_to_bytes(runtime, 1);
840                 ssi->next_period_byte   = ssi->byte_per_period;
841         }
842
843         return rsnd_ssi_init(mod, io, priv);
844 }
845
846 static int rsnd_ssi_pio_pointer(struct rsnd_mod *mod,
847                             struct rsnd_dai_stream *io,
848                             snd_pcm_uframes_t *pointer)
849 {
850         struct rsnd_ssi *ssi = rsnd_mod_to_ssi(mod);
851         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
852
853         *pointer = bytes_to_frames(runtime, READ_ONCE(ssi->byte_pos));
854
855         return 0;
856 }
857
858 static int rsnd_ssi_prepare(struct rsnd_mod *mod,
859                             struct rsnd_dai_stream *io,
860                             struct rsnd_priv *priv)
861 {
862         return rsnd_ssi_master_clk_start(mod, io);
863 }
864
865 static struct rsnd_mod_ops rsnd_ssi_pio_ops = {
866         .name   = SSI_NAME,
867         .probe  = rsnd_ssi_common_probe,
868         .remove = rsnd_ssi_common_remove,
869         .init   = rsnd_ssi_pio_init,
870         .quit   = rsnd_ssi_quit,
871         .start  = rsnd_ssi_start,
872         .stop   = rsnd_ssi_stop,
873         .irq    = rsnd_ssi_irq,
874         .pointer = rsnd_ssi_pio_pointer,
875         .pcm_new = rsnd_ssi_pcm_new,
876         .hw_params = rsnd_ssi_hw_params,
877         .prepare = rsnd_ssi_prepare,
878 };
879
880 static int rsnd_ssi_dma_probe(struct rsnd_mod *mod,
881                               struct rsnd_dai_stream *io,
882                               struct rsnd_priv *priv)
883 {
884         int ret;
885
886         /*
887          * SSIP/SSIU/IRQ/DMA are not needed on
888          * SSI Multi slaves
889          */
890         if (rsnd_ssi_is_multi_slave(mod, io))
891                 return 0;
892
893         ret = rsnd_ssi_common_probe(mod, io, priv);
894         if (ret)
895                 return ret;
896
897         /* SSI probe might be called many times in MUX multi path */
898         ret = rsnd_dma_attach(io, mod, &io->dma);
899
900         return ret;
901 }
902
903 static int rsnd_ssi_fallback(struct rsnd_mod *mod,
904                              struct rsnd_dai_stream *io,
905                              struct rsnd_priv *priv)
906 {
907         struct device *dev = rsnd_priv_to_dev(priv);
908
909         /*
910          * fallback to PIO
911          *
912          * SSI .probe might be called again.
913          * see
914          *      rsnd_rdai_continuance_probe()
915          */
916         mod->ops = &rsnd_ssi_pio_ops;
917
918         dev_info(dev, "%s[%d] fallback to PIO mode\n",
919                  rsnd_mod_name(mod), rsnd_mod_id(mod));
920
921         return 0;
922 }
923
924 static struct dma_chan *rsnd_ssi_dma_req(struct rsnd_dai_stream *io,
925                                          struct rsnd_mod *mod)
926 {
927         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
928         int is_play = rsnd_io_is_play(io);
929         char *name;
930
931         if (rsnd_ssi_use_busif(io))
932                 name = is_play ? "rxu" : "txu";
933         else
934                 name = is_play ? "rx" : "tx";
935
936         return rsnd_dma_request_channel(rsnd_ssi_of_node(priv),
937                                         mod, name);
938 }
939
940 static struct rsnd_mod_ops rsnd_ssi_dma_ops = {
941         .name   = SSI_NAME,
942         .dma_req = rsnd_ssi_dma_req,
943         .probe  = rsnd_ssi_dma_probe,
944         .remove = rsnd_ssi_common_remove,
945         .init   = rsnd_ssi_init,
946         .quit   = rsnd_ssi_quit,
947         .start  = rsnd_ssi_start,
948         .stop   = rsnd_ssi_stop,
949         .irq    = rsnd_ssi_irq,
950         .pcm_new = rsnd_ssi_pcm_new,
951         .fallback = rsnd_ssi_fallback,
952         .hw_params = rsnd_ssi_hw_params,
953         .prepare = rsnd_ssi_prepare,
954 };
955
956 int rsnd_ssi_is_dma_mode(struct rsnd_mod *mod)
957 {
958         return mod->ops == &rsnd_ssi_dma_ops;
959 }
960
961
962 /*
963  *              ssi mod function
964  */
965 static void rsnd_ssi_connect(struct rsnd_mod *mod,
966                              struct rsnd_dai_stream *io)
967 {
968         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
969         enum rsnd_mod_type types[] = {
970                 RSND_MOD_SSI,
971                 RSND_MOD_SSIM1,
972                 RSND_MOD_SSIM2,
973                 RSND_MOD_SSIM3,
974         };
975         enum rsnd_mod_type type;
976         int i;
977
978         /* try SSI -> SSIM1 -> SSIM2 -> SSIM3 */
979         for (i = 0; i < ARRAY_SIZE(types); i++) {
980                 type = types[i];
981                 if (!rsnd_io_to_mod(io, type)) {
982                         rsnd_dai_connect(mod, io, type);
983                         rsnd_rdai_channels_set(rdai, (i + 1) * 2);
984                         rsnd_rdai_ssi_lane_set(rdai, (i + 1));
985                         return;
986                 }
987         }
988 }
989
990 void rsnd_parse_connect_ssi(struct rsnd_dai *rdai,
991                             struct device_node *playback,
992                             struct device_node *capture)
993 {
994         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
995         struct device_node *node;
996         struct device_node *np;
997         struct rsnd_mod *mod;
998         int i;
999
1000         node = rsnd_ssi_of_node(priv);
1001         if (!node)
1002                 return;
1003
1004         i = 0;
1005         for_each_child_of_node(node, np) {
1006                 mod = rsnd_ssi_mod_get(priv, i);
1007                 if (np == playback)
1008                         rsnd_ssi_connect(mod, &rdai->playback);
1009                 if (np == capture)
1010                         rsnd_ssi_connect(mod, &rdai->capture);
1011                 i++;
1012         }
1013
1014         of_node_put(node);
1015 }
1016
1017 static void __rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1018                                              struct rsnd_dai_stream *io,
1019                                              struct device_node *remote_ep)
1020 {
1021         struct device *dev = rsnd_priv_to_dev(priv);
1022         struct rsnd_mod *mod = rsnd_io_to_mod_ssi(io);
1023         struct rsnd_ssi *ssi;
1024         struct device_node *remote_node = of_graph_get_port_parent(remote_ep);
1025
1026         /* support Gen3 only */
1027         if (!rsnd_is_gen3(priv))
1028                 return;
1029
1030         if (!mod)
1031                 return;
1032
1033         ssi  = rsnd_mod_to_ssi(mod);
1034
1035         /* HDMI0 */
1036         if (strstr(remote_node->full_name, "hdmi@fead0000")) {
1037                 rsnd_flags_set(ssi, RSND_SSI_HDMI0);
1038                 dev_dbg(dev, "%s[%d] connected to HDMI0\n",
1039                          rsnd_mod_name(mod), rsnd_mod_id(mod));
1040         }
1041
1042         /* HDMI1 */
1043         if (strstr(remote_node->full_name, "hdmi@feae0000")) {
1044                 rsnd_flags_set(ssi, RSND_SSI_HDMI1);
1045                 dev_dbg(dev, "%s[%d] connected to HDMI1\n",
1046                         rsnd_mod_name(mod), rsnd_mod_id(mod));
1047         }
1048 }
1049
1050 void rsnd_ssi_parse_hdmi_connection(struct rsnd_priv *priv,
1051                                     struct device_node *endpoint,
1052                                     int dai_i)
1053 {
1054         struct rsnd_dai *rdai = rsnd_rdai_get(priv, dai_i);
1055         struct device_node *remote_ep;
1056
1057         remote_ep = of_graph_get_remote_endpoint(endpoint);
1058         if (!remote_ep)
1059                 return;
1060
1061         __rsnd_ssi_parse_hdmi_connection(priv, &rdai->playback, remote_ep);
1062         __rsnd_ssi_parse_hdmi_connection(priv, &rdai->capture,  remote_ep);
1063 }
1064
1065 struct rsnd_mod *rsnd_ssi_mod_get(struct rsnd_priv *priv, int id)
1066 {
1067         if (WARN_ON(id < 0 || id >= rsnd_ssi_nr(priv)))
1068                 id = 0;
1069
1070         return rsnd_mod_get(rsnd_ssi_get(priv, id));
1071 }
1072
1073 int __rsnd_ssi_is_pin_sharing(struct rsnd_mod *mod)
1074 {
1075         if (!mod)
1076                 return 0;
1077
1078         return !!(rsnd_flags_has(rsnd_mod_to_ssi(mod), RSND_SSI_CLK_PIN_SHARE));
1079 }
1080
1081 static u32 *rsnd_ssi_get_status(struct rsnd_dai_stream *io,
1082                                 struct rsnd_mod *mod,
1083                                 enum rsnd_mod_type type)
1084 {
1085         /*
1086          * SSIP (= SSI parent) needs to be special, otherwise,
1087          * 2nd SSI might doesn't start. see also rsnd_mod_call()
1088          *
1089          * We can't include parent SSI status on SSI, because we don't know
1090          * how many SSI requests parent SSI. Thus, it is localed on "io" now.
1091          * ex) trouble case
1092          *      Playback: SSI0
1093          *      Capture : SSI1 (needs SSI0)
1094          *
1095          * 1) start Capture  -> SSI0/SSI1 are started.
1096          * 2) start Playback -> SSI0 doesn't work, because it is already
1097          *                      marked as "started" on 1)
1098          *
1099          * OTOH, using each mod's status is good for MUX case.
1100          * It doesn't need to start in 2nd start
1101          * ex)
1102          *      IO-0: SRC0 -> CTU1 -+-> MUX -> DVC -> SSIU -> SSI0
1103          *                          |
1104          *      IO-1: SRC1 -> CTU2 -+
1105          *
1106          * 1) start IO-0 ->     start SSI0
1107          * 2) start IO-1 ->     SSI0 doesn't need to start, because it is
1108          *                      already started on 1)
1109          */
1110         if (type == RSND_MOD_SSIP)
1111                 return &io->parent_ssi_status;
1112
1113         return rsnd_mod_get_status(io, mod, type);
1114 }
1115
1116 int rsnd_ssi_probe(struct rsnd_priv *priv)
1117 {
1118         struct device_node *node;
1119         struct device_node *np;
1120         struct device *dev = rsnd_priv_to_dev(priv);
1121         struct rsnd_mod_ops *ops;
1122         struct clk *clk;
1123         struct rsnd_ssi *ssi;
1124         char name[RSND_SSI_NAME_SIZE];
1125         int i, nr, ret;
1126
1127         node = rsnd_ssi_of_node(priv);
1128         if (!node)
1129                 return -EINVAL;
1130
1131         nr = of_get_child_count(node);
1132         if (!nr) {
1133                 ret = -EINVAL;
1134                 goto rsnd_ssi_probe_done;
1135         }
1136
1137         ssi     = devm_kcalloc(dev, nr, sizeof(*ssi), GFP_KERNEL);
1138         if (!ssi) {
1139                 ret = -ENOMEM;
1140                 goto rsnd_ssi_probe_done;
1141         }
1142
1143         priv->ssi       = ssi;
1144         priv->ssi_nr    = nr;
1145
1146         i = 0;
1147         for_each_child_of_node(node, np) {
1148                 if (!of_device_is_available(np))
1149                         goto skip;
1150
1151                 ssi = rsnd_ssi_get(priv, i);
1152
1153                 snprintf(name, RSND_SSI_NAME_SIZE, "%s.%d",
1154                          SSI_NAME, i);
1155
1156                 clk = devm_clk_get(dev, name);
1157                 if (IS_ERR(clk)) {
1158                         ret = PTR_ERR(clk);
1159                         of_node_put(np);
1160                         goto rsnd_ssi_probe_done;
1161                 }
1162
1163                 if (of_get_property(np, "shared-pin", NULL))
1164                         rsnd_flags_set(ssi, RSND_SSI_CLK_PIN_SHARE);
1165
1166                 if (of_get_property(np, "no-busif", NULL))
1167                         rsnd_flags_set(ssi, RSND_SSI_NO_BUSIF);
1168
1169                 ssi->irq = irq_of_parse_and_map(np, 0);
1170                 if (!ssi->irq) {
1171                         ret = -EINVAL;
1172                         of_node_put(np);
1173                         goto rsnd_ssi_probe_done;
1174                 }
1175
1176                 if (of_property_read_bool(np, "pio-transfer"))
1177                         ops = &rsnd_ssi_pio_ops;
1178                 else
1179                         ops = &rsnd_ssi_dma_ops;
1180
1181                 ret = rsnd_mod_init(priv, rsnd_mod_get(ssi), ops, clk,
1182                                     rsnd_ssi_get_status, RSND_MOD_SSI, i);
1183                 if (ret) {
1184                         of_node_put(np);
1185                         goto rsnd_ssi_probe_done;
1186                 }
1187 skip:
1188                 i++;
1189         }
1190
1191         ret = 0;
1192
1193 rsnd_ssi_probe_done:
1194         of_node_put(node);
1195
1196         return ret;
1197 }
1198
1199 void rsnd_ssi_remove(struct rsnd_priv *priv)
1200 {
1201         struct rsnd_ssi *ssi;
1202         int i;
1203
1204         for_each_rsnd_ssi(ssi, priv, i) {
1205                 rsnd_mod_quit(rsnd_mod_get(ssi));
1206         }
1207 }