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