Version:  2.0.40 2.2.26 2.4.37 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16

Linux/sound/soc/sh/rcar/core.c

  1 /*
  2  * Renesas R-Car SRU/SCU/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 
 15 /*
 16  * Renesas R-Car sound device structure
 17  *
 18  * Gen1
 19  *
 20  * SRU          : Sound Routing Unit
 21  *  - SRC       : Sampling Rate Converter
 22  *  - CMD
 23  *    - CTU     : Channel Count Conversion Unit
 24  *    - MIX     : Mixer
 25  *    - DVC     : Digital Volume and Mute Function
 26  *  - SSI       : Serial Sound Interface
 27  *
 28  * Gen2
 29  *
 30  * SCU          : Sampling Rate Converter Unit
 31  *  - SRC       : Sampling Rate Converter
 32  *  - CMD
 33  *   - CTU      : Channel Count Conversion Unit
 34  *   - MIX      : Mixer
 35  *   - DVC      : Digital Volume and Mute Function
 36  * SSIU         : Serial Sound Interface Unit
 37  *  - SSI       : Serial Sound Interface
 38  */
 39 
 40 /*
 41  *      driver data Image
 42  *
 43  * rsnd_priv
 44  *   |
 45  *   | ** this depends on Gen1/Gen2
 46  *   |
 47  *   +- gen
 48  *   |
 49  *   | ** these depend on data path
 50  *   | ** gen and platform data control it
 51  *   |
 52  *   +- rdai[0]
 53  *   |   |               sru     ssiu      ssi
 54  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
 55  *   |   |
 56  *   |   |               sru     ssiu      ssi
 57  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
 58  *   |
 59  *   +- rdai[1]
 60  *   |   |               sru     ssiu      ssi
 61  *   |   +- playback -> [mod] -> [mod] -> [mod] -> ...
 62  *   |   |
 63  *   |   |               sru     ssiu      ssi
 64  *   |   +- capture  -> [mod] -> [mod] -> [mod] -> ...
 65  *   ...
 66  *   |
 67  *   | ** these control ssi
 68  *   |
 69  *   +- ssi
 70  *   |  |
 71  *   |  +- ssi[0]
 72  *   |  +- ssi[1]
 73  *   |  +- ssi[2]
 74  *   |  ...
 75  *   |
 76  *   | ** these control src
 77  *   |
 78  *   +- src
 79  *      |
 80  *      +- src[0]
 81  *      +- src[1]
 82  *      +- src[2]
 83  *      ...
 84  *
 85  *
 86  * for_each_rsnd_dai(xx, priv, xx)
 87  *  rdai[0] => rdai[1] => rdai[2] => ...
 88  *
 89  * for_each_rsnd_mod(xx, rdai, xx)
 90  *  [mod] => [mod] => [mod] => ...
 91  *
 92  * rsnd_dai_call(xxx, fn )
 93  *  [mod]->fn() -> [mod]->fn() -> [mod]->fn()...
 94  *
 95  */
 96 #include <linux/pm_runtime.h>
 97 #include <linux/shdma-base.h>
 98 #include "rsnd.h"
 99 
100 #define RSND_RATES SNDRV_PCM_RATE_8000_96000
101 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
102 
103 static struct rsnd_of_data rsnd_of_data_gen1 = {
104         .flags = RSND_GEN1,
105 };
106 
107 static struct rsnd_of_data rsnd_of_data_gen2 = {
108         .flags = RSND_GEN2,
109 };
110 
111 static struct of_device_id rsnd_of_match[] = {
112         { .compatible = "renesas,rcar_sound-gen1", .data = &rsnd_of_data_gen1 },
113         { .compatible = "renesas,rcar_sound-gen2", .data = &rsnd_of_data_gen2 },
114         {},
115 };
116 MODULE_DEVICE_TABLE(of, rsnd_of_match);
117 
118 /*
119  *      rsnd_platform functions
120  */
121 #define rsnd_platform_call(priv, dai, func, param...)   \
122         (!(priv->info->func) ? 0 :              \
123          priv->info->func(param))
124 
125 #define rsnd_is_enable_path(io, name) \
126         ((io)->info ? (io)->info->name : NULL)
127 #define rsnd_info_id(priv, io, name) \
128         ((io)->info->name - priv->info->name##_info)
129 
130 /*
131  *      rsnd_mod functions
132  */
133 char *rsnd_mod_name(struct rsnd_mod *mod)
134 {
135         if (!mod || !mod->ops)
136                 return "unknown";
137 
138         return mod->ops->name;
139 }
140 
141 void rsnd_mod_init(struct rsnd_priv *priv,
142                    struct rsnd_mod *mod,
143                    struct rsnd_mod_ops *ops,
144                    enum rsnd_mod_type type,
145                    int id)
146 {
147         mod->priv       = priv;
148         mod->id         = id;
149         mod->ops        = ops;
150         mod->type       = type;
151 }
152 
153 /*
154  *      rsnd_dma functions
155  */
156 static void __rsnd_dma_start(struct rsnd_dma *dma);
157 static void rsnd_dma_continue(struct rsnd_dma *dma)
158 {
159         /* push next A or B plane */
160         dma->submit_loop = 1;
161         schedule_work(&dma->work);
162 }
163 
164 void rsnd_dma_start(struct rsnd_dma *dma)
165 {
166         /* push both A and B plane*/
167         dma->offset = 0;
168         dma->submit_loop = 2;
169         __rsnd_dma_start(dma);
170 }
171 
172 void rsnd_dma_stop(struct rsnd_dma *dma)
173 {
174         dma->submit_loop = 0;
175         cancel_work_sync(&dma->work);
176         dmaengine_terminate_all(dma->chan);
177 }
178 
179 static void rsnd_dma_complete(void *data)
180 {
181         struct rsnd_dma *dma = (struct rsnd_dma *)data;
182         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
183         struct rsnd_priv *priv = rsnd_mod_to_priv(rsnd_dma_to_mod(dma));
184         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
185         unsigned long flags;
186 
187         rsnd_lock(priv, flags);
188 
189         /*
190          * Renesas sound Gen1 needs 1 DMAC,
191          * Gen2 needs 2 DMAC.
192          * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
193          * But, Audio-DMAC-peri-peri doesn't have interrupt,
194          * and this driver is assuming that here.
195          *
196          * If Audio-DMAC-peri-peri has interrpt,
197          * rsnd_dai_pointer_update() will be called twice,
198          * ant it will breaks io->byte_pos
199          */
200         if (dma->submit_loop)
201                 rsnd_dma_continue(dma);
202 
203         rsnd_unlock(priv, flags);
204 
205         rsnd_dai_pointer_update(io, io->byte_per_period);
206 }
207 
208 static void __rsnd_dma_start(struct rsnd_dma *dma)
209 {
210         struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
211         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
212         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
213         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
214         struct device *dev = rsnd_priv_to_dev(priv);
215         struct dma_async_tx_descriptor *desc;
216         dma_addr_t buf;
217         size_t len = io->byte_per_period;
218         int i;
219 
220         for (i = 0; i < dma->submit_loop; i++) {
221 
222                 buf = runtime->dma_addr +
223                         rsnd_dai_pointer_offset(io, dma->offset + len);
224                 dma->offset = len;
225 
226                 desc = dmaengine_prep_slave_single(
227                         dma->chan, buf, len, dma->dir,
228                         DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
229                 if (!desc) {
230                         dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
231                         return;
232                 }
233 
234                 desc->callback          = rsnd_dma_complete;
235                 desc->callback_param    = dma;
236 
237                 if (dmaengine_submit(desc) < 0) {
238                         dev_err(dev, "dmaengine_submit() fail\n");
239                         return;
240                 }
241 
242                 dma_async_issue_pending(dma->chan);
243         }
244 }
245 
246 static void rsnd_dma_do_work(struct work_struct *work)
247 {
248         struct rsnd_dma *dma = container_of(work, struct rsnd_dma, work);
249 
250         __rsnd_dma_start(dma);
251 }
252 
253 int rsnd_dma_available(struct rsnd_dma *dma)
254 {
255         return !!dma->chan;
256 }
257 
258 #define DMA_NAME_SIZE 16
259 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
260 static int _rsnd_dma_of_name(char *dma_name, struct rsnd_mod *mod)
261 {
262         if (mod)
263                 return snprintf(dma_name, DMA_NAME_SIZE / 2, "%s%d",
264                          rsnd_mod_name(mod), rsnd_mod_id(mod));
265         else
266                 return snprintf(dma_name, DMA_NAME_SIZE / 2, "mem");
267 
268 }
269 
270 static void rsnd_dma_of_name(struct rsnd_dma *dma,
271                              int is_play, char *dma_name)
272 {
273         struct rsnd_mod *this = rsnd_dma_to_mod(dma);
274         struct rsnd_dai_stream *io = rsnd_mod_to_io(this);
275         struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
276         struct rsnd_mod *src = rsnd_io_to_mod_src(io);
277         struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
278         struct rsnd_mod *mod[MOD_MAX];
279         struct rsnd_mod *src_mod, *dst_mod;
280         int i, index;
281 
282 
283         for (i = 0; i < MOD_MAX; i++)
284                 mod[i] = NULL;
285 
286         /*
287          * in play case...
288          *
289          * src -> dst
290          *
291          * mem -> SSI
292          * mem -> SRC -> SSI
293          * mem -> SRC -> DVC -> SSI
294          */
295         mod[0] = NULL; /* for "mem" */
296         index = 1;
297         for (i = 1; i < MOD_MAX; i++) {
298                 if (!src) {
299                         mod[i] = ssi;
300                         break;
301                 } else if (!dvc) {
302                         mod[i] = src;
303                         src = NULL;
304                 } else {
305                         mod[i] = dvc;
306                         dvc = NULL;
307                 }
308 
309                 if (mod[i] == this)
310                         index = i;
311         }
312 
313         if (is_play) {
314                 src_mod = mod[index - 1];
315                 dst_mod = mod[index];
316         } else {
317                 src_mod = mod[index];
318                 dst_mod = mod[index - 1];
319         }
320 
321         index = 0;
322         index = _rsnd_dma_of_name(dma_name + index, src_mod);
323         *(dma_name + index++) = '_';
324         index = _rsnd_dma_of_name(dma_name + index, dst_mod);
325 }
326 
327 int rsnd_dma_init(struct rsnd_priv *priv, struct rsnd_dma *dma,
328                   int is_play, int id)
329 {
330         struct device *dev = rsnd_priv_to_dev(priv);
331         struct dma_slave_config cfg;
332         char dma_name[DMA_NAME_SIZE];
333         dma_cap_mask_t mask;
334         int ret;
335 
336         if (dma->chan) {
337                 dev_err(dev, "it already has dma channel\n");
338                 return -EIO;
339         }
340 
341         dma_cap_zero(mask);
342         dma_cap_set(DMA_SLAVE, mask);
343 
344         if (dev->of_node)
345                 rsnd_dma_of_name(dma, is_play, dma_name);
346         else
347                 snprintf(dma_name, DMA_NAME_SIZE,
348                          is_play ? "tx" : "rx");
349 
350         dev_dbg(dev, "dma name : %s\n", dma_name);
351 
352         dma->chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
353                                                      (void *)id, dev,
354                                                      dma_name);
355         if (!dma->chan) {
356                 dev_err(dev, "can't get dma channel\n");
357                 return -EIO;
358         }
359 
360         rsnd_gen_dma_addr(priv, dma, &cfg, is_play, id);
361 
362         ret = dmaengine_slave_config(dma->chan, &cfg);
363         if (ret < 0)
364                 goto rsnd_dma_init_err;
365 
366         dma->dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
367         INIT_WORK(&dma->work, rsnd_dma_do_work);
368 
369         return 0;
370 
371 rsnd_dma_init_err:
372         rsnd_dma_quit(priv, dma);
373 
374         return ret;
375 }
376 
377 void  rsnd_dma_quit(struct rsnd_priv *priv,
378                     struct rsnd_dma *dma)
379 {
380         if (dma->chan)
381                 dma_release_channel(dma->chan);
382 
383         dma->chan = NULL;
384 }
385 
386 /*
387  *      settting function
388  */
389 u32 rsnd_get_adinr(struct rsnd_mod *mod)
390 {
391         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
392         struct rsnd_dai_stream *io = rsnd_mod_to_io(mod);
393         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
394         struct device *dev = rsnd_priv_to_dev(priv);
395         u32 adinr = runtime->channels;
396 
397         switch (runtime->sample_bits) {
398         case 16:
399                 adinr |= (8 << 16);
400                 break;
401         case 32:
402                 adinr |= (0 << 16);
403                 break;
404         default:
405                 dev_warn(dev, "not supported sample bits\n");
406                 return 0;
407         }
408 
409         return adinr;
410 }
411 
412 /*
413  *      rsnd_dai functions
414  */
415 #define __rsnd_mod_call(mod, func, rdai...)                     \
416 ({                                                              \
417         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);         \
418         struct device *dev = rsnd_priv_to_dev(priv);            \
419         dev_dbg(dev, "%s [%d] %s\n",                            \
420                 rsnd_mod_name(mod), rsnd_mod_id(mod), #func);   \
421         (mod)->ops->func(mod, rdai);                            \
422 })
423 
424 #define rsnd_mod_call(mod, func, rdai...)       \
425         (!(mod) ? -ENODEV :                     \
426          !((mod)->ops->func) ? 0 :              \
427          __rsnd_mod_call(mod, func, rdai))
428 
429 #define rsnd_dai_call(fn, io, rdai...)                          \
430 ({                                                              \
431         struct rsnd_mod *mod;                                   \
432         int ret = 0, i;                                         \
433         for (i = 0; i < RSND_MOD_MAX; i++) {                    \
434                 mod = (io)->mod[i];                             \
435                 if (!mod)                                       \
436                         continue;                               \
437                 ret = rsnd_mod_call(mod, fn, rdai);             \
438                 if (ret < 0)                                    \
439                         break;                                  \
440         }                                                       \
441         ret;                                                    \
442 })
443 
444 static int rsnd_dai_connect(struct rsnd_mod *mod,
445                             struct rsnd_dai_stream *io)
446 {
447         if (!mod)
448                 return -EIO;
449 
450         if (io->mod[mod->type]) {
451                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
452                 struct device *dev = rsnd_priv_to_dev(priv);
453 
454                 dev_err(dev, "%s%d is not empty\n",
455                         rsnd_mod_name(mod),
456                         rsnd_mod_id(mod));
457                 return -EIO;
458         }
459 
460         io->mod[mod->type] = mod;
461         mod->io = io;
462 
463         return 0;
464 }
465 
466 int rsnd_dai_id(struct rsnd_priv *priv, struct rsnd_dai *rdai)
467 {
468         int id = rdai - priv->rdai;
469 
470         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
471                 return -EINVAL;
472 
473         return id;
474 }
475 
476 struct rsnd_dai *rsnd_dai_get(struct rsnd_priv *priv, int id)
477 {
478         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
479                 return NULL;
480 
481         return priv->rdai + id;
482 }
483 
484 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
485 {
486         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
487 
488         return rsnd_dai_get(priv, dai->id);
489 }
490 
491 int rsnd_dai_is_play(struct rsnd_dai *rdai, struct rsnd_dai_stream *io)
492 {
493         return &rdai->playback == io;
494 }
495 
496 /*
497  *      rsnd_soc_dai functions
498  */
499 int rsnd_dai_pointer_offset(struct rsnd_dai_stream *io, int additional)
500 {
501         struct snd_pcm_substream *substream = io->substream;
502         struct snd_pcm_runtime *runtime = substream->runtime;
503         int pos = io->byte_pos + additional;
504 
505         pos %= (runtime->periods * io->byte_per_period);
506 
507         return pos;
508 }
509 
510 void rsnd_dai_pointer_update(struct rsnd_dai_stream *io, int byte)
511 {
512         io->byte_pos += byte;
513 
514         if (io->byte_pos >= io->next_period_byte) {
515                 struct snd_pcm_substream *substream = io->substream;
516                 struct snd_pcm_runtime *runtime = substream->runtime;
517 
518                 io->period_pos++;
519                 io->next_period_byte += io->byte_per_period;
520 
521                 if (io->period_pos >= runtime->periods) {
522                         io->byte_pos = 0;
523                         io->period_pos = 0;
524                         io->next_period_byte = io->byte_per_period;
525                 }
526 
527                 snd_pcm_period_elapsed(substream);
528         }
529 }
530 
531 static int rsnd_dai_stream_init(struct rsnd_dai_stream *io,
532                                 struct snd_pcm_substream *substream)
533 {
534         struct snd_pcm_runtime *runtime = substream->runtime;
535 
536         io->substream           = substream;
537         io->byte_pos            = 0;
538         io->period_pos          = 0;
539         io->byte_per_period     = runtime->period_size *
540                                   runtime->channels *
541                                   samples_to_bytes(runtime, 1);
542         io->next_period_byte    = io->byte_per_period;
543 
544         return 0;
545 }
546 
547 static
548 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
549 {
550         struct snd_soc_pcm_runtime *rtd = substream->private_data;
551 
552         return  rtd->cpu_dai;
553 }
554 
555 static
556 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
557                                         struct snd_pcm_substream *substream)
558 {
559         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
560                 return &rdai->playback;
561         else
562                 return &rdai->capture;
563 }
564 
565 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
566                             struct snd_soc_dai *dai)
567 {
568         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(dai);
569         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
570         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
571         int ssi_id = rsnd_mod_id(rsnd_io_to_mod_ssi(io));
572         int ret;
573         unsigned long flags;
574 
575         rsnd_lock(priv, flags);
576 
577         switch (cmd) {
578         case SNDRV_PCM_TRIGGER_START:
579                 ret = rsnd_dai_stream_init(io, substream);
580                 if (ret < 0)
581                         goto dai_trigger_end;
582 
583                 ret = rsnd_platform_call(priv, dai, start, ssi_id);
584                 if (ret < 0)
585                         goto dai_trigger_end;
586 
587                 ret = rsnd_dai_call(init, io, rdai);
588                 if (ret < 0)
589                         goto dai_trigger_end;
590 
591                 ret = rsnd_dai_call(start, io, rdai);
592                 if (ret < 0)
593                         goto dai_trigger_end;
594                 break;
595         case SNDRV_PCM_TRIGGER_STOP:
596                 ret = rsnd_dai_call(stop, io, rdai);
597                 if (ret < 0)
598                         goto dai_trigger_end;
599 
600                 ret = rsnd_dai_call(quit, io, rdai);
601                 if (ret < 0)
602                         goto dai_trigger_end;
603 
604                 ret = rsnd_platform_call(priv, dai, stop, ssi_id);
605                 if (ret < 0)
606                         goto dai_trigger_end;
607                 break;
608         default:
609                 ret = -EINVAL;
610         }
611 
612 dai_trigger_end:
613         rsnd_unlock(priv, flags);
614 
615         return ret;
616 }
617 
618 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
619 {
620         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
621 
622         /* set master/slave audio interface */
623         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
624         case SND_SOC_DAIFMT_CBM_CFM:
625                 rdai->clk_master = 0;
626                 break;
627         case SND_SOC_DAIFMT_CBS_CFS:
628                 rdai->clk_master = 1; /* codec is slave, cpu is master */
629                 break;
630         default:
631                 return -EINVAL;
632         }
633 
634         /* set clock inversion */
635         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
636         case SND_SOC_DAIFMT_NB_IF:
637                 rdai->bit_clk_inv = 0;
638                 rdai->frm_clk_inv = 1;
639                 break;
640         case SND_SOC_DAIFMT_IB_NF:
641                 rdai->bit_clk_inv = 1;
642                 rdai->frm_clk_inv = 0;
643                 break;
644         case SND_SOC_DAIFMT_IB_IF:
645                 rdai->bit_clk_inv = 1;
646                 rdai->frm_clk_inv = 1;
647                 break;
648         case SND_SOC_DAIFMT_NB_NF:
649         default:
650                 rdai->bit_clk_inv = 0;
651                 rdai->frm_clk_inv = 0;
652                 break;
653         }
654 
655         /* set format */
656         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
657         case SND_SOC_DAIFMT_I2S:
658                 rdai->sys_delay = 0;
659                 rdai->data_alignment = 0;
660                 break;
661         case SND_SOC_DAIFMT_LEFT_J:
662                 rdai->sys_delay = 1;
663                 rdai->data_alignment = 0;
664                 break;
665         case SND_SOC_DAIFMT_RIGHT_J:
666                 rdai->sys_delay = 1;
667                 rdai->data_alignment = 1;
668                 break;
669         }
670 
671         return 0;
672 }
673 
674 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
675         .trigger        = rsnd_soc_dai_trigger,
676         .set_fmt        = rsnd_soc_dai_set_fmt,
677 };
678 
679 #define rsnd_path_parse(priv, io, type)                         \
680 ({                                                              \
681         struct rsnd_mod *mod;                                   \
682         int ret = 0;                                            \
683         int id = -1;                                            \
684                                                                 \
685         if (rsnd_is_enable_path(io, type)) {                    \
686                 id = rsnd_info_id(priv, io, type);              \
687                 if (id >= 0) {                                  \
688                         mod = rsnd_##type##_mod_get(priv, id);  \
689                         ret = rsnd_dai_connect(mod, io);        \
690                 }                                               \
691         }                                                       \
692         ret;                                                    \
693 })
694 
695 static int rsnd_path_init(struct rsnd_priv *priv,
696                           struct rsnd_dai *rdai,
697                           struct rsnd_dai_stream *io)
698 {
699         int ret;
700 
701         /*
702          * Gen1 is created by SRU/SSI, and this SRU is base module of
703          * Gen2's SCU/SSIU/SSI. (Gen2 SCU/SSIU came from SRU)
704          *
705          * Easy image is..
706          *      Gen1 SRU = Gen2 SCU + SSIU + etc
707          *
708          * Gen2 SCU path is very flexible, but, Gen1 SRU (SCU parts) is
709          * using fixed path.
710          */
711 
712         /* SRC */
713         ret = rsnd_path_parse(priv, io, src);
714         if (ret < 0)
715                 return ret;
716 
717         /* SSI */
718         ret = rsnd_path_parse(priv, io, ssi);
719         if (ret < 0)
720                 return ret;
721 
722         /* DVC */
723         ret = rsnd_path_parse(priv, io, dvc);
724         if (ret < 0)
725                 return ret;
726 
727         return ret;
728 }
729 
730 static void rsnd_of_parse_dai(struct platform_device *pdev,
731                               const struct rsnd_of_data *of_data,
732                               struct rsnd_priv *priv)
733 {
734         struct device_node *dai_node,   *dai_np;
735         struct device_node *ssi_node,   *ssi_np;
736         struct device_node *src_node,   *src_np;
737         struct device_node *playback, *capture;
738         struct rsnd_dai_platform_info *dai_info;
739         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
740         struct device *dev = &pdev->dev;
741         int nr, i;
742         int dai_i, ssi_i, src_i;
743 
744         if (!of_data)
745                 return;
746 
747         dai_node = of_get_child_by_name(dev->of_node, "rcar_sound,dai");
748         if (!dai_node)
749                 return;
750 
751         nr = of_get_child_count(dai_node);
752         if (!nr)
753                 return;
754 
755         dai_info = devm_kzalloc(dev,
756                                 sizeof(struct rsnd_dai_platform_info) * nr,
757                                 GFP_KERNEL);
758         if (!dai_info) {
759                 dev_err(dev, "dai info allocation error\n");
760                 return;
761         }
762 
763         info->dai_info_nr       = nr;
764         info->dai_info          = dai_info;
765 
766         ssi_node = of_get_child_by_name(dev->of_node, "rcar_sound,ssi");
767         src_node = of_get_child_by_name(dev->of_node, "rcar_sound,src");
768 
769 #define mod_parse(name)                                                 \
770 if (name##_node) {                                                      \
771         struct rsnd_##name##_platform_info *name##_info;                \
772                                                                         \
773         name##_i = 0;                                                   \
774         for_each_child_of_node(name##_node, name##_np) {                \
775                 name##_info = info->name##_info + name##_i;             \
776                                                                         \
777                 if (name##_np == playback)                              \
778                         dai_info->playback.name = name##_info;          \
779                 if (name##_np == capture)                               \
780                         dai_info->capture.name = name##_info;           \
781                                                                         \
782                 name##_i++;                                             \
783         }                                                               \
784 }
785 
786         /*
787          * parse all dai
788          */
789         dai_i = 0;
790         for_each_child_of_node(dai_node, dai_np) {
791                 dai_info = info->dai_info + dai_i;
792 
793                 for (i = 0;; i++) {
794 
795                         playback = of_parse_phandle(dai_np, "playback", i);
796                         capture  = of_parse_phandle(dai_np, "capture", i);
797 
798                         if (!playback && !capture)
799                                 break;
800 
801                         mod_parse(ssi);
802                         mod_parse(src);
803 
804                         if (playback)
805                                 of_node_put(playback);
806                         if (capture)
807                                 of_node_put(capture);
808                 }
809 
810                 dai_i++;
811         }
812 }
813 
814 static int rsnd_dai_probe(struct platform_device *pdev,
815                           const struct rsnd_of_data *of_data,
816                           struct rsnd_priv *priv)
817 {
818         struct snd_soc_dai_driver *drv;
819         struct rcar_snd_info *info = rsnd_priv_to_info(priv);
820         struct rsnd_dai *rdai;
821         struct rsnd_ssi_platform_info *pmod, *cmod;
822         struct device *dev = rsnd_priv_to_dev(priv);
823         int dai_nr;
824         int i;
825 
826         rsnd_of_parse_dai(pdev, of_data, priv);
827 
828         dai_nr = info->dai_info_nr;
829         if (!dai_nr) {
830                 dev_err(dev, "no dai\n");
831                 return -EIO;
832         }
833 
834         drv  = devm_kzalloc(dev, sizeof(*drv)  * dai_nr, GFP_KERNEL);
835         rdai = devm_kzalloc(dev, sizeof(*rdai) * dai_nr, GFP_KERNEL);
836         if (!drv || !rdai) {
837                 dev_err(dev, "dai allocate failed\n");
838                 return -ENOMEM;
839         }
840 
841         priv->rdai_nr   = dai_nr;
842         priv->daidrv    = drv;
843         priv->rdai      = rdai;
844 
845         for (i = 0; i < dai_nr; i++) {
846                 rdai[i].info = &info->dai_info[i];
847 
848                 pmod = rdai[i].info->playback.ssi;
849                 cmod = rdai[i].info->capture.ssi;
850 
851                 /*
852                  *      init rsnd_dai
853                  */
854                 snprintf(rdai[i].name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", i);
855 
856                 /*
857                  *      init snd_soc_dai_driver
858                  */
859                 drv[i].name     = rdai[i].name;
860                 drv[i].ops      = &rsnd_soc_dai_ops;
861                 if (pmod) {
862                         drv[i].playback.rates           = RSND_RATES;
863                         drv[i].playback.formats         = RSND_FMTS;
864                         drv[i].playback.channels_min    = 2;
865                         drv[i].playback.channels_max    = 2;
866 
867                         rdai[i].playback.info = &info->dai_info[i].playback;
868                         rsnd_path_init(priv, &rdai[i], &rdai[i].playback);
869                 }
870                 if (cmod) {
871                         drv[i].capture.rates            = RSND_RATES;
872                         drv[i].capture.formats          = RSND_FMTS;
873                         drv[i].capture.channels_min     = 2;
874                         drv[i].capture.channels_max     = 2;
875 
876                         rdai[i].capture.info = &info->dai_info[i].capture;
877                         rsnd_path_init(priv, &rdai[i], &rdai[i].capture);
878                 }
879 
880                 dev_dbg(dev, "%s (%s/%s)\n", rdai[i].name,
881                         pmod ? "play"    : " -- ",
882                         cmod ? "capture" : "  --   ");
883         }
884 
885         return 0;
886 }
887 
888 /*
889  *              pcm ops
890  */
891 static struct snd_pcm_hardware rsnd_pcm_hardware = {
892         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
893                         SNDRV_PCM_INFO_MMAP             |
894                         SNDRV_PCM_INFO_MMAP_VALID       |
895                         SNDRV_PCM_INFO_PAUSE,
896         .buffer_bytes_max       = 64 * 1024,
897         .period_bytes_min       = 32,
898         .period_bytes_max       = 8192,
899         .periods_min            = 1,
900         .periods_max            = 32,
901         .fifo_size              = 256,
902 };
903 
904 static int rsnd_pcm_open(struct snd_pcm_substream *substream)
905 {
906         struct snd_pcm_runtime *runtime = substream->runtime;
907         int ret = 0;
908 
909         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
910 
911         ret = snd_pcm_hw_constraint_integer(runtime,
912                                             SNDRV_PCM_HW_PARAM_PERIODS);
913 
914         return ret;
915 }
916 
917 static int rsnd_hw_params(struct snd_pcm_substream *substream,
918                          struct snd_pcm_hw_params *hw_params)
919 {
920         return snd_pcm_lib_malloc_pages(substream,
921                                         params_buffer_bytes(hw_params));
922 }
923 
924 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
925 {
926         struct snd_pcm_runtime *runtime = substream->runtime;
927         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
928         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
929         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
930 
931         return bytes_to_frames(runtime, io->byte_pos);
932 }
933 
934 static struct snd_pcm_ops rsnd_pcm_ops = {
935         .open           = rsnd_pcm_open,
936         .ioctl          = snd_pcm_lib_ioctl,
937         .hw_params      = rsnd_hw_params,
938         .hw_free        = snd_pcm_lib_free_pages,
939         .pointer        = rsnd_pointer,
940 };
941 
942 /*
943  *              snd_soc_platform
944  */
945 
946 #define PREALLOC_BUFFER         (32 * 1024)
947 #define PREALLOC_BUFFER_MAX     (32 * 1024)
948 
949 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
950 {
951         struct rsnd_priv *priv = snd_soc_dai_get_drvdata(rtd->cpu_dai);
952         struct rsnd_dai *rdai;
953         int i, ret;
954 
955         for_each_rsnd_dai(rdai, priv, i) {
956                 ret = rsnd_dai_call(pcm_new, &rdai->playback, rdai, rtd);
957                 if (ret)
958                         return ret;
959 
960                 ret = rsnd_dai_call(pcm_new, &rdai->capture, rdai, rtd);
961                 if (ret)
962                         return ret;
963         }
964 
965         return snd_pcm_lib_preallocate_pages_for_all(
966                 rtd->pcm,
967                 SNDRV_DMA_TYPE_DEV,
968                 rtd->card->snd_card->dev,
969                 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
970 }
971 
972 static void rsnd_pcm_free(struct snd_pcm *pcm)
973 {
974         snd_pcm_lib_preallocate_free_for_all(pcm);
975 }
976 
977 static struct snd_soc_platform_driver rsnd_soc_platform = {
978         .ops            = &rsnd_pcm_ops,
979         .pcm_new        = rsnd_pcm_new,
980         .pcm_free       = rsnd_pcm_free,
981 };
982 
983 static const struct snd_soc_component_driver rsnd_soc_component = {
984         .name           = "rsnd",
985 };
986 
987 /*
988  *      rsnd probe
989  */
990 static int rsnd_probe(struct platform_device *pdev)
991 {
992         struct rcar_snd_info *info;
993         struct rsnd_priv *priv;
994         struct device *dev = &pdev->dev;
995         struct rsnd_dai *rdai;
996         const struct of_device_id *of_id = of_match_device(rsnd_of_match, dev);
997         const struct rsnd_of_data *of_data;
998         int (*probe_func[])(struct platform_device *pdev,
999                             const struct rsnd_of_data *of_data,
1000                             struct rsnd_priv *priv) = {
1001                 rsnd_gen_probe,
1002                 rsnd_ssi_probe,
1003                 rsnd_src_probe,
1004                 rsnd_dvc_probe,
1005                 rsnd_adg_probe,
1006                 rsnd_dai_probe,
1007         };
1008         int ret, i;
1009 
1010         info = NULL;
1011         of_data = NULL;
1012         if (of_id) {
1013                 info = devm_kzalloc(&pdev->dev,
1014                                     sizeof(struct rcar_snd_info), GFP_KERNEL);
1015                 of_data = of_id->data;
1016         } else {
1017                 info = pdev->dev.platform_data;
1018         }
1019 
1020         if (!info) {
1021                 dev_err(dev, "driver needs R-Car sound information\n");
1022                 return -ENODEV;
1023         }
1024 
1025         /*
1026          *      init priv data
1027          */
1028         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1029         if (!priv) {
1030                 dev_err(dev, "priv allocate failed\n");
1031                 return -ENODEV;
1032         }
1033 
1034         priv->pdev      = pdev;
1035         priv->info      = info;
1036         spin_lock_init(&priv->lock);
1037 
1038         /*
1039          *      init each module
1040          */
1041         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1042                 ret = probe_func[i](pdev, of_data, priv);
1043                 if (ret)
1044                         return ret;
1045         }
1046 
1047         for_each_rsnd_dai(rdai, priv, i) {
1048                 ret = rsnd_dai_call(probe, &rdai->playback, rdai);
1049                 if (ret)
1050                         return ret;
1051 
1052                 ret = rsnd_dai_call(probe, &rdai->capture, rdai);
1053                 if (ret)
1054                         return ret;
1055         }
1056 
1057         /*
1058          *      asoc register
1059          */
1060         ret = snd_soc_register_platform(dev, &rsnd_soc_platform);
1061         if (ret < 0) {
1062                 dev_err(dev, "cannot snd soc register\n");
1063                 return ret;
1064         }
1065 
1066         ret = snd_soc_register_component(dev, &rsnd_soc_component,
1067                                          priv->daidrv, rsnd_rdai_nr(priv));
1068         if (ret < 0) {
1069                 dev_err(dev, "cannot snd dai register\n");
1070                 goto exit_snd_soc;
1071         }
1072 
1073         dev_set_drvdata(dev, priv);
1074 
1075         pm_runtime_enable(dev);
1076 
1077         dev_info(dev, "probed\n");
1078         return ret;
1079 
1080 exit_snd_soc:
1081         snd_soc_unregister_platform(dev);
1082 
1083         return ret;
1084 }
1085 
1086 static int rsnd_remove(struct platform_device *pdev)
1087 {
1088         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1089         struct rsnd_dai *rdai;
1090         int ret, i;
1091 
1092         pm_runtime_disable(&pdev->dev);
1093 
1094         for_each_rsnd_dai(rdai, priv, i) {
1095                 ret = rsnd_dai_call(remove, &rdai->playback, rdai);
1096                 if (ret)
1097                         return ret;
1098 
1099                 ret = rsnd_dai_call(remove, &rdai->capture, rdai);
1100                 if (ret)
1101                         return ret;
1102         }
1103 
1104         return 0;
1105 }
1106 
1107 static struct platform_driver rsnd_driver = {
1108         .driver = {
1109                 .name   = "rcar_sound",
1110                 .of_match_table = rsnd_of_match,
1111         },
1112         .probe          = rsnd_probe,
1113         .remove         = rsnd_remove,
1114 };
1115 module_platform_driver(rsnd_driver);
1116 
1117 MODULE_LICENSE("GPL");
1118 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1119 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1120 MODULE_ALIAS("platform:rcar-pcm-audio");
1121 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us