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