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