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