~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/soc/sh/rcar/core.c

Version: ~ [ linux-5.9-rc5 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 97 /*
 98  * you can enable below define if you don't need
 99  * DAI status debug message when debugging
100  * see rsnd_dbg_dai_call()
101  *
102  * #define RSND_DEBUG_NO_DAI_CALL 1
103  */
104 
105 #include <linux/pm_runtime.h>
106 #include "rsnd.h"
107 
108 #define RSND_RATES SNDRV_PCM_RATE_8000_192000
109 #define RSND_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
110 
111 static const struct of_device_id rsnd_of_match[] = {
112         { .compatible = "renesas,rcar_sound-gen1", .data = (void *)RSND_GEN1 },
113         { .compatible = "renesas,rcar_sound-gen2", .data = (void *)RSND_GEN2 },
114         { .compatible = "renesas,rcar_sound-gen3", .data = (void *)RSND_GEN3 },
115         {},
116 };
117 MODULE_DEVICE_TABLE(of, rsnd_of_match);
118 
119 /*
120  *      rsnd_mod functions
121  */
122 void rsnd_mod_make_sure(struct rsnd_mod *mod, enum rsnd_mod_type type)
123 {
124         if (mod->type != type) {
125                 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
126                 struct device *dev = rsnd_priv_to_dev(priv);
127 
128                 dev_warn(dev, "%s[%d] is not your expected module\n",
129                          rsnd_mod_name(mod), rsnd_mod_id(mod));
130         }
131 }
132 
133 struct dma_chan *rsnd_mod_dma_req(struct rsnd_dai_stream *io,
134                                   struct rsnd_mod *mod)
135 {
136         if (!mod || !mod->ops || !mod->ops->dma_req)
137                 return NULL;
138 
139         return mod->ops->dma_req(io, mod);
140 }
141 
142 u32 *rsnd_mod_get_status(struct rsnd_dai_stream *io,
143                          struct rsnd_mod *mod,
144                          enum rsnd_mod_type type)
145 {
146         return &mod->status;
147 }
148 
149 int rsnd_mod_init(struct rsnd_priv *priv,
150                   struct rsnd_mod *mod,
151                   struct rsnd_mod_ops *ops,
152                   struct clk *clk,
153                   u32* (*get_status)(struct rsnd_dai_stream *io,
154                                      struct rsnd_mod *mod,
155                                      enum rsnd_mod_type type),
156                   enum rsnd_mod_type type,
157                   int id)
158 {
159         int ret = clk_prepare(clk);
160 
161         if (ret)
162                 return ret;
163 
164         mod->id         = id;
165         mod->ops        = ops;
166         mod->type       = type;
167         mod->clk        = clk;
168         mod->priv       = priv;
169         mod->get_status = get_status;
170 
171         return ret;
172 }
173 
174 void rsnd_mod_quit(struct rsnd_mod *mod)
175 {
176         clk_unprepare(mod->clk);
177         mod->clk = NULL;
178 }
179 
180 void rsnd_mod_interrupt(struct rsnd_mod *mod,
181                         void (*callback)(struct rsnd_mod *mod,
182                                          struct rsnd_dai_stream *io))
183 {
184         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
185         struct rsnd_dai_stream *io;
186         struct rsnd_dai *rdai;
187         int i;
188 
189         for_each_rsnd_dai(rdai, priv, i) {
190                 io = &rdai->playback;
191                 if (mod == io->mod[mod->type])
192                         callback(mod, io);
193 
194                 io = &rdai->capture;
195                 if (mod == io->mod[mod->type])
196                         callback(mod, io);
197         }
198 }
199 
200 int rsnd_io_is_working(struct rsnd_dai_stream *io)
201 {
202         /* see rsnd_dai_stream_init/quit() */
203         if (io->substream)
204                 return snd_pcm_running(io->substream);
205 
206         return 0;
207 }
208 
209 int rsnd_runtime_channel_original_with_params(struct rsnd_dai_stream *io,
210                                               struct snd_pcm_hw_params *params)
211 {
212         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
213 
214         /*
215          * params will be added when refine
216          * see
217          *      __rsnd_soc_hw_rule_rate()
218          *      __rsnd_soc_hw_rule_channels()
219          */
220         if (params)
221                 return params_channels(params);
222         else
223                 return runtime->channels;
224 }
225 
226 int rsnd_runtime_channel_after_ctu_with_params(struct rsnd_dai_stream *io,
227                                                struct snd_pcm_hw_params *params)
228 {
229         int chan = rsnd_runtime_channel_original_with_params(io, params);
230         struct rsnd_mod *ctu_mod = rsnd_io_to_mod_ctu(io);
231 
232         if (ctu_mod) {
233                 u32 converted_chan = rsnd_ctu_converted_channel(ctu_mod);
234 
235                 if (converted_chan)
236                         return converted_chan;
237         }
238 
239         return chan;
240 }
241 
242 int rsnd_runtime_channel_for_ssi_with_params(struct rsnd_dai_stream *io,
243                                              struct snd_pcm_hw_params *params)
244 {
245         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
246         int chan = rsnd_io_is_play(io) ?
247                 rsnd_runtime_channel_after_ctu_with_params(io, params) :
248                 rsnd_runtime_channel_original_with_params(io, params);
249 
250         /* Use Multi SSI */
251         if (rsnd_runtime_is_ssi_multi(io))
252                 chan /= rsnd_rdai_ssi_lane_get(rdai);
253 
254         /* TDM Extend Mode needs 8ch */
255         if (chan == 6)
256                 chan = 8;
257 
258         return chan;
259 }
260 
261 int rsnd_runtime_is_ssi_multi(struct rsnd_dai_stream *io)
262 {
263         struct rsnd_dai *rdai = rsnd_io_to_rdai(io);
264         int lane = rsnd_rdai_ssi_lane_get(rdai);
265         int chan = rsnd_io_is_play(io) ?
266                 rsnd_runtime_channel_after_ctu(io) :
267                 rsnd_runtime_channel_original(io);
268 
269         return (chan > 2) && (lane > 1);
270 }
271 
272 int rsnd_runtime_is_ssi_tdm(struct rsnd_dai_stream *io)
273 {
274         return rsnd_runtime_channel_for_ssi(io) >= 6;
275 }
276 
277 /*
278  *      ADINR function
279  */
280 u32 rsnd_get_adinr_bit(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
281 {
282         struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
283         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
284         struct device *dev = rsnd_priv_to_dev(priv);
285 
286         switch (snd_pcm_format_width(runtime->format)) {
287         case 16:
288                 return 8 << 16;
289         case 24:
290                 return 0 << 16;
291         }
292 
293         dev_warn(dev, "not supported sample bits\n");
294 
295         return 0;
296 }
297 
298 /*
299  *      DALIGN function
300  */
301 u32 rsnd_get_dalign(struct rsnd_mod *mod, struct rsnd_dai_stream *io)
302 {
303         struct rsnd_mod *ssiu = rsnd_io_to_mod_ssiu(io);
304         struct rsnd_mod *target;
305         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
306 
307         /*
308          * *Hardware* L/R and *Software* L/R are inverted for 16bit data.
309          *          31..16 15...0
310          *      HW: [L ch] [R ch]
311          *      SW: [R ch] [L ch]
312          * We need to care about inversion timing to control
313          * Playback/Capture correctly.
314          * The point is [DVC] needs *Hardware* L/R, [MEM] needs *Software* L/R
315          *
316          * sL/R : software L/R
317          * hL/R : hardware L/R
318          * (*)  : conversion timing
319          *
320          * Playback
321          *           sL/R (*) hL/R     hL/R     hL/R      hL/R     hL/R
322          *      [MEM] -> [SRC] -> [DVC] -> [CMD] -> [SSIU] -> [SSI] -> codec
323          *
324          * Capture
325          *           hL/R     hL/R      hL/R     hL/R     hL/R (*) sL/R
326          *      codec -> [SSI] -> [SSIU] -> [SRC] -> [DVC] -> [CMD] -> [MEM]
327          */
328         if (rsnd_io_is_play(io)) {
329                 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
330 
331                 target = src ? src : ssiu;
332         } else {
333                 struct rsnd_mod *cmd = rsnd_io_to_mod_cmd(io);
334 
335                 target = cmd ? cmd : ssiu;
336         }
337 
338         /* Non target mod or 24bit data needs normal DALIGN */
339         if ((snd_pcm_format_width(runtime->format) != 16) ||
340             (mod != target))
341                 return 0x76543210;
342         /* Target mod needs inverted DALIGN when 16bit */
343         else
344                 return 0x67452301;
345 }
346 
347 u32 rsnd_get_busif_shift(struct rsnd_dai_stream *io, struct rsnd_mod *mod)
348 {
349         enum rsnd_mod_type playback_mods[] = {
350                 RSND_MOD_SRC,
351                 RSND_MOD_CMD,
352                 RSND_MOD_SSIU,
353         };
354         enum rsnd_mod_type capture_mods[] = {
355                 RSND_MOD_CMD,
356                 RSND_MOD_SRC,
357                 RSND_MOD_SSIU,
358         };
359         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
360         struct rsnd_mod *tmod = NULL;
361         enum rsnd_mod_type *mods =
362                 rsnd_io_is_play(io) ?
363                 playback_mods : capture_mods;
364         int i;
365 
366         /*
367          * This is needed for 24bit data
368          * We need to shift 8bit
369          *
370          * Linux 24bit data is located as 0x00******
371          * HW    24bit data is located as 0x******00
372          *
373          */
374         if (snd_pcm_format_width(runtime->format) == 16)
375                 return 0;
376 
377         for (i = 0; i < ARRAY_SIZE(playback_mods); i++) {
378                 tmod = rsnd_io_to_mod(io, mods[i]);
379                 if (tmod)
380                         break;
381         }
382 
383         if (tmod != mod)
384                 return 0;
385 
386         if (rsnd_io_is_play(io))
387                 return  (0 << 20) | /* shift to Left */
388                         (8 << 16);  /* 8bit */
389         else
390                 return  (1 << 20) | /* shift to Right */
391                         (8 << 16);  /* 8bit */
392 }
393 
394 /*
395  *      rsnd_dai functions
396  */
397 struct rsnd_mod *rsnd_mod_next(int *iterator,
398                                struct rsnd_dai_stream *io,
399                                enum rsnd_mod_type *array,
400                                int array_size)
401 {
402         struct rsnd_mod *mod;
403         enum rsnd_mod_type type;
404         int max = array ? array_size : RSND_MOD_MAX;
405 
406         for (; *iterator < max; (*iterator)++) {
407                 type = (array) ? array[*iterator] : *iterator;
408                 mod = rsnd_io_to_mod(io, type);
409                 if (mod)
410                         return mod;
411         }
412 
413         return NULL;
414 }
415 
416 static enum rsnd_mod_type rsnd_mod_sequence[][RSND_MOD_MAX] = {
417         {
418                 /* CAPTURE */
419                 RSND_MOD_AUDMAPP,
420                 RSND_MOD_AUDMA,
421                 RSND_MOD_DVC,
422                 RSND_MOD_MIX,
423                 RSND_MOD_CTU,
424                 RSND_MOD_CMD,
425                 RSND_MOD_SRC,
426                 RSND_MOD_SSIU,
427                 RSND_MOD_SSIM3,
428                 RSND_MOD_SSIM2,
429                 RSND_MOD_SSIM1,
430                 RSND_MOD_SSIP,
431                 RSND_MOD_SSI,
432         }, {
433                 /* PLAYBACK */
434                 RSND_MOD_AUDMAPP,
435                 RSND_MOD_AUDMA,
436                 RSND_MOD_SSIM3,
437                 RSND_MOD_SSIM2,
438                 RSND_MOD_SSIM1,
439                 RSND_MOD_SSIP,
440                 RSND_MOD_SSI,
441                 RSND_MOD_SSIU,
442                 RSND_MOD_DVC,
443                 RSND_MOD_MIX,
444                 RSND_MOD_CTU,
445                 RSND_MOD_CMD,
446                 RSND_MOD_SRC,
447         },
448 };
449 
450 static int rsnd_status_update(u32 *status,
451                               int shift, int add, int timing)
452 {
453         u32 mask        = 0xF << shift;
454         u8 val          = (*status >> shift) & 0xF;
455         u8 next_val     = (val + add) & 0xF;
456         int func_call   = (val == timing);
457 
458         if (next_val == 0xF) /* underflow case */
459                 func_call = 0;
460         else
461                 *status = (*status & ~mask) + (next_val << shift);
462 
463         return func_call;
464 }
465 
466 #define rsnd_dai_call(fn, io, param...)                                 \
467 ({                                                                      \
468         struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));     \
469         struct rsnd_mod *mod;                                           \
470         int is_play = rsnd_io_is_play(io);                              \
471         int ret = 0, i;                                                 \
472         enum rsnd_mod_type *types = rsnd_mod_sequence[is_play];         \
473         for_each_rsnd_mod_arrays(i, mod, io, types, RSND_MOD_MAX) {     \
474                 int tmp = 0;                                            \
475                 u32 *status = mod->get_status(io, mod, types[i]);       \
476                 int func_call = rsnd_status_update(status,              \
477                                                 __rsnd_mod_shift_##fn,  \
478                                                 __rsnd_mod_add_##fn,    \
479                                                 __rsnd_mod_call_##fn);  \
480                 rsnd_dbg_dai_call(dev, "%s[%d]\t0x%08x %s\n",           \
481                         rsnd_mod_name(mod), rsnd_mod_id(mod), *status,  \
482                         (func_call && (mod)->ops->fn) ? #fn : "");      \
483                 if (func_call && (mod)->ops->fn)                        \
484                         tmp = (mod)->ops->fn(mod, io, param);           \
485                 if (tmp && (tmp != -EPROBE_DEFER))                      \
486                         dev_err(dev, "%s[%d] : %s error %d\n",          \
487                                 rsnd_mod_name(mod), rsnd_mod_id(mod),   \
488                                                      #fn, tmp);         \
489                 ret |= tmp;                                             \
490         }                                                               \
491         ret;                                                            \
492 })
493 
494 int rsnd_dai_connect(struct rsnd_mod *mod,
495                      struct rsnd_dai_stream *io,
496                      enum rsnd_mod_type type)
497 {
498         struct rsnd_priv *priv;
499         struct device *dev;
500 
501         if (!mod)
502                 return -EIO;
503 
504         if (io->mod[type] == mod)
505                 return 0;
506 
507         if (io->mod[type])
508                 return -EINVAL;
509 
510         priv = rsnd_mod_to_priv(mod);
511         dev = rsnd_priv_to_dev(priv);
512 
513         io->mod[type] = mod;
514 
515         dev_dbg(dev, "%s[%d] is connected to io (%s)\n",
516                 rsnd_mod_name(mod), rsnd_mod_id(mod),
517                 rsnd_io_is_play(io) ? "Playback" : "Capture");
518 
519         return 0;
520 }
521 
522 static void rsnd_dai_disconnect(struct rsnd_mod *mod,
523                                 struct rsnd_dai_stream *io,
524                                 enum rsnd_mod_type type)
525 {
526         io->mod[type] = NULL;
527 }
528 
529 int rsnd_rdai_channels_ctrl(struct rsnd_dai *rdai,
530                             int max_channels)
531 {
532         if (max_channels > 0)
533                 rdai->max_channels = max_channels;
534 
535         return rdai->max_channels;
536 }
537 
538 int rsnd_rdai_ssi_lane_ctrl(struct rsnd_dai *rdai,
539                             int ssi_lane)
540 {
541         if (ssi_lane > 0)
542                 rdai->ssi_lane = ssi_lane;
543 
544         return rdai->ssi_lane;
545 }
546 
547 struct rsnd_dai *rsnd_rdai_get(struct rsnd_priv *priv, int id)
548 {
549         if ((id < 0) || (id >= rsnd_rdai_nr(priv)))
550                 return NULL;
551 
552         return priv->rdai + id;
553 }
554 
555 #define rsnd_dai_to_priv(dai) snd_soc_dai_get_drvdata(dai)
556 static struct rsnd_dai *rsnd_dai_to_rdai(struct snd_soc_dai *dai)
557 {
558         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
559 
560         return rsnd_rdai_get(priv, dai->id);
561 }
562 
563 /*
564  *      rsnd_soc_dai functions
565  */
566 void rsnd_dai_period_elapsed(struct rsnd_dai_stream *io)
567 {
568         struct snd_pcm_substream *substream = io->substream;
569 
570         /*
571          * this function should be called...
572          *
573          * - if rsnd_dai_pointer_update() returns true
574          * - without spin lock
575          */
576 
577         snd_pcm_period_elapsed(substream);
578 }
579 
580 static void rsnd_dai_stream_init(struct rsnd_dai_stream *io,
581                                 struct snd_pcm_substream *substream)
582 {
583         io->substream           = substream;
584 }
585 
586 static void rsnd_dai_stream_quit(struct rsnd_dai_stream *io)
587 {
588         io->substream           = NULL;
589 }
590 
591 static
592 struct snd_soc_dai *rsnd_substream_to_dai(struct snd_pcm_substream *substream)
593 {
594         struct snd_soc_pcm_runtime *rtd = substream->private_data;
595 
596         return  rtd->cpu_dai;
597 }
598 
599 static
600 struct rsnd_dai_stream *rsnd_rdai_to_io(struct rsnd_dai *rdai,
601                                         struct snd_pcm_substream *substream)
602 {
603         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
604                 return &rdai->playback;
605         else
606                 return &rdai->capture;
607 }
608 
609 static int rsnd_soc_dai_trigger(struct snd_pcm_substream *substream, int cmd,
610                             struct snd_soc_dai *dai)
611 {
612         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
613         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
614         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
615         int ret;
616         unsigned long flags;
617 
618         spin_lock_irqsave(&priv->lock, flags);
619 
620         switch (cmd) {
621         case SNDRV_PCM_TRIGGER_START:
622         case SNDRV_PCM_TRIGGER_RESUME:
623                 ret = rsnd_dai_call(init, io, priv);
624                 if (ret < 0)
625                         goto dai_trigger_end;
626 
627                 ret = rsnd_dai_call(start, io, priv);
628                 if (ret < 0)
629                         goto dai_trigger_end;
630 
631                 ret = rsnd_dai_call(irq, io, priv, 1);
632                 if (ret < 0)
633                         goto dai_trigger_end;
634 
635                 break;
636         case SNDRV_PCM_TRIGGER_STOP:
637         case SNDRV_PCM_TRIGGER_SUSPEND:
638                 ret = rsnd_dai_call(irq, io, priv, 0);
639 
640                 ret |= rsnd_dai_call(stop, io, priv);
641 
642                 ret |= rsnd_dai_call(quit, io, priv);
643 
644                 break;
645         default:
646                 ret = -EINVAL;
647         }
648 
649 dai_trigger_end:
650         spin_unlock_irqrestore(&priv->lock, flags);
651 
652         return ret;
653 }
654 
655 static int rsnd_soc_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
656 {
657         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
658 
659         /* set master/slave audio interface */
660         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
661         case SND_SOC_DAIFMT_CBM_CFM:
662                 rdai->clk_master = 0;
663                 break;
664         case SND_SOC_DAIFMT_CBS_CFS:
665                 rdai->clk_master = 1; /* codec is slave, cpu is master */
666                 break;
667         default:
668                 return -EINVAL;
669         }
670 
671         /* set format */
672         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
673         case SND_SOC_DAIFMT_I2S:
674                 rdai->sys_delay = 0;
675                 rdai->data_alignment = 0;
676                 rdai->frm_clk_inv = 0;
677                 break;
678         case SND_SOC_DAIFMT_LEFT_J:
679                 rdai->sys_delay = 1;
680                 rdai->data_alignment = 0;
681                 rdai->frm_clk_inv = 1;
682                 break;
683         case SND_SOC_DAIFMT_RIGHT_J:
684                 rdai->sys_delay = 1;
685                 rdai->data_alignment = 1;
686                 rdai->frm_clk_inv = 1;
687                 break;
688         }
689 
690         /* set clock inversion */
691         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
692         case SND_SOC_DAIFMT_NB_IF:
693                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
694                 break;
695         case SND_SOC_DAIFMT_IB_NF:
696                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
697                 break;
698         case SND_SOC_DAIFMT_IB_IF:
699                 rdai->bit_clk_inv = !rdai->bit_clk_inv;
700                 rdai->frm_clk_inv = !rdai->frm_clk_inv;
701                 break;
702         case SND_SOC_DAIFMT_NB_NF:
703         default:
704                 break;
705         }
706 
707         return 0;
708 }
709 
710 static int rsnd_soc_set_dai_tdm_slot(struct snd_soc_dai *dai,
711                                      u32 tx_mask, u32 rx_mask,
712                                      int slots, int slot_width)
713 {
714         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
715         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
716         struct device *dev = rsnd_priv_to_dev(priv);
717 
718         switch (slots) {
719         case 2:
720         case 6:
721         case 8:
722                 /* TDM Extend Mode */
723                 rsnd_rdai_channels_set(rdai, slots);
724                 rsnd_rdai_ssi_lane_set(rdai, 1);
725                 break;
726         default:
727                 dev_err(dev, "unsupported TDM slots (%d)\n", slots);
728                 return -EINVAL;
729         }
730 
731         return 0;
732 }
733 
734 static unsigned int rsnd_soc_hw_channels_list[] = {
735         2, 6, 8,
736 };
737 
738 static unsigned int rsnd_soc_hw_rate_list[] = {
739           8000,
740          11025,
741          16000,
742          22050,
743          32000,
744          44100,
745          48000,
746          64000,
747          88200,
748          96000,
749         176400,
750         192000,
751 };
752 
753 static int rsnd_soc_hw_rule(struct rsnd_priv *priv,
754                             unsigned int *list, int list_num,
755                             struct snd_interval *baseline, struct snd_interval *iv)
756 {
757         struct snd_interval p;
758         unsigned int rate;
759         int i;
760 
761         snd_interval_any(&p);
762         p.min = UINT_MAX;
763         p.max = 0;
764 
765         for (i = 0; i < list_num; i++) {
766 
767                 if (!snd_interval_test(iv, list[i]))
768                         continue;
769 
770                 rate = rsnd_ssi_clk_query(priv,
771                                           baseline->min, list[i], NULL);
772                 if (rate > 0) {
773                         p.min = min(p.min, list[i]);
774                         p.max = max(p.max, list[i]);
775                 }
776 
777                 rate = rsnd_ssi_clk_query(priv,
778                                           baseline->max, list[i], NULL);
779                 if (rate > 0) {
780                         p.min = min(p.min, list[i]);
781                         p.max = max(p.max, list[i]);
782                 }
783         }
784 
785         return snd_interval_refine(iv, &p);
786 }
787 
788 static int __rsnd_soc_hw_rule_rate(struct snd_pcm_hw_params *params,
789                                    struct snd_pcm_hw_rule *rule,
790                                    int is_play)
791 {
792         struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
793         struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
794         struct snd_interval ic;
795         struct snd_soc_dai *dai = rule->private;
796         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
797         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
798         struct rsnd_dai_stream *io = is_play ? &rdai->playback : &rdai->capture;
799 
800         /*
801          * possible sampling rate limitation is same as
802          * 2ch if it supports multi ssi
803          * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
804          */
805         ic = *ic_;
806         ic.min =
807         ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
808 
809         return rsnd_soc_hw_rule(priv, rsnd_soc_hw_rate_list,
810                                 ARRAY_SIZE(rsnd_soc_hw_rate_list),
811                                 &ic, ir);
812 }
813 
814 static int rsnd_soc_hw_rule_rate_playback(struct snd_pcm_hw_params *params,
815                                  struct snd_pcm_hw_rule *rule)
816 {
817         return __rsnd_soc_hw_rule_rate(params, rule, 1);
818 }
819 
820 static int rsnd_soc_hw_rule_rate_capture(struct snd_pcm_hw_params *params,
821                                           struct snd_pcm_hw_rule *rule)
822 {
823         return __rsnd_soc_hw_rule_rate(params, rule, 0);
824 }
825 
826 static int __rsnd_soc_hw_rule_channels(struct snd_pcm_hw_params *params,
827                                        struct snd_pcm_hw_rule *rule,
828                                        int is_play)
829 {
830         struct snd_interval *ic_ = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
831         struct snd_interval *ir = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
832         struct snd_interval ic;
833         struct snd_soc_dai *dai = rule->private;
834         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
835         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
836         struct rsnd_dai_stream *io = is_play ? &rdai->playback : &rdai->capture;
837 
838         /*
839          * possible sampling rate limitation is same as
840          * 2ch if it supports multi ssi
841          * and same as 8ch if TDM 6ch (see rsnd_ssi_config_init())
842          */
843         ic = *ic_;
844         ic.min =
845         ic.max = rsnd_runtime_channel_for_ssi_with_params(io, params);
846 
847         return rsnd_soc_hw_rule(priv, rsnd_soc_hw_channels_list,
848                                 ARRAY_SIZE(rsnd_soc_hw_channels_list),
849                                 ir, &ic);
850 }
851 
852 static int rsnd_soc_hw_rule_channels_playback(struct snd_pcm_hw_params *params,
853                                               struct snd_pcm_hw_rule *rule)
854 {
855         return __rsnd_soc_hw_rule_channels(params, rule, 1);
856 }
857 
858 static int rsnd_soc_hw_rule_channels_capture(struct snd_pcm_hw_params *params,
859                                              struct snd_pcm_hw_rule *rule)
860 {
861         return __rsnd_soc_hw_rule_channels(params, rule, 0);
862 }
863 
864 static const struct snd_pcm_hardware rsnd_pcm_hardware = {
865         .info =         SNDRV_PCM_INFO_INTERLEAVED      |
866                         SNDRV_PCM_INFO_MMAP             |
867                         SNDRV_PCM_INFO_MMAP_VALID,
868         .buffer_bytes_max       = 64 * 1024,
869         .period_bytes_min       = 32,
870         .period_bytes_max       = 8192,
871         .periods_min            = 1,
872         .periods_max            = 32,
873         .fifo_size              = 256,
874 };
875 
876 static int rsnd_soc_dai_startup(struct snd_pcm_substream *substream,
877                                 struct snd_soc_dai *dai)
878 {
879         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
880         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
881         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
882         struct snd_pcm_hw_constraint_list *constraint = &rdai->constraint;
883         struct snd_pcm_runtime *runtime = substream->runtime;
884         unsigned int max_channels = rsnd_rdai_channels_get(rdai);
885         int ret;
886         int i;
887 
888         rsnd_dai_stream_init(io, substream);
889 
890         /*
891          * Channel Limitation
892          * It depends on Platform design
893          */
894         constraint->list        = rsnd_soc_hw_channels_list;
895         constraint->count       = 0;
896         constraint->mask        = 0;
897 
898         for (i = 0; i < ARRAY_SIZE(rsnd_soc_hw_channels_list); i++) {
899                 if (rsnd_soc_hw_channels_list[i] > max_channels)
900                         break;
901                 constraint->count = i + 1;
902         }
903 
904         snd_soc_set_runtime_hwparams(substream, &rsnd_pcm_hardware);
905 
906         snd_pcm_hw_constraint_list(runtime, 0,
907                                    SNDRV_PCM_HW_PARAM_CHANNELS, constraint);
908 
909         snd_pcm_hw_constraint_integer(runtime,
910                                       SNDRV_PCM_HW_PARAM_PERIODS);
911 
912         /*
913          * Sampling Rate / Channel Limitation
914          * It depends on Clock Master Mode
915          */
916         if (rsnd_rdai_is_clk_master(rdai)) {
917                 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
918 
919                 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
920                                     is_play ? rsnd_soc_hw_rule_rate_playback :
921                                               rsnd_soc_hw_rule_rate_capture,
922                                     dai,
923                                     SNDRV_PCM_HW_PARAM_CHANNELS, -1);
924                 snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
925                                     is_play ? rsnd_soc_hw_rule_channels_playback :
926                                               rsnd_soc_hw_rule_channels_capture,
927                                     dai,
928                                     SNDRV_PCM_HW_PARAM_RATE, -1);
929         }
930 
931         /*
932          * call rsnd_dai_call without spinlock
933          */
934         ret = rsnd_dai_call(nolock_start, io, priv);
935         if (ret < 0)
936                 rsnd_dai_call(nolock_stop, io, priv);
937 
938         return ret;
939 }
940 
941 static void rsnd_soc_dai_shutdown(struct snd_pcm_substream *substream,
942                                   struct snd_soc_dai *dai)
943 {
944         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
945         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
946         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
947 
948         /*
949          * call rsnd_dai_call without spinlock
950          */
951         rsnd_dai_call(nolock_stop, io, priv);
952 
953         rsnd_dai_stream_quit(io);
954 }
955 
956 static int rsnd_soc_dai_prepare(struct snd_pcm_substream *substream,
957                                 struct snd_soc_dai *dai)
958 {
959         struct rsnd_priv *priv = rsnd_dai_to_priv(dai);
960         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
961         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
962 
963         return rsnd_dai_call(prepare, io, priv);
964 }
965 
966 static const struct snd_soc_dai_ops rsnd_soc_dai_ops = {
967         .startup        = rsnd_soc_dai_startup,
968         .shutdown       = rsnd_soc_dai_shutdown,
969         .trigger        = rsnd_soc_dai_trigger,
970         .set_fmt        = rsnd_soc_dai_set_fmt,
971         .set_tdm_slot   = rsnd_soc_set_dai_tdm_slot,
972         .prepare        = rsnd_soc_dai_prepare,
973 };
974 
975 void rsnd_parse_connect_common(struct rsnd_dai *rdai,
976                 struct rsnd_mod* (*mod_get)(struct rsnd_priv *priv, int id),
977                 struct device_node *node,
978                 struct device_node *playback,
979                 struct device_node *capture)
980 {
981         struct rsnd_priv *priv = rsnd_rdai_to_priv(rdai);
982         struct device_node *np;
983         struct rsnd_mod *mod;
984         int i;
985 
986         if (!node)
987                 return;
988 
989         i = 0;
990         for_each_child_of_node(node, np) {
991                 mod = mod_get(priv, i);
992                 if (np == playback)
993                         rsnd_dai_connect(mod, &rdai->playback, mod->type);
994                 if (np == capture)
995                         rsnd_dai_connect(mod, &rdai->capture, mod->type);
996                 i++;
997         }
998 
999         of_node_put(node);
1000 }
1001 
1002 static struct device_node *rsnd_dai_of_node(struct rsnd_priv *priv,
1003                                             int *is_graph)
1004 {
1005         struct device *dev = rsnd_priv_to_dev(priv);
1006         struct device_node *np = dev->of_node;
1007         struct device_node *dai_node;
1008         struct device_node *ret;
1009 
1010         *is_graph = 0;
1011 
1012         /*
1013          * parse both previous dai (= rcar_sound,dai), and
1014          * graph dai (= ports/port)
1015          */
1016         dai_node = of_get_child_by_name(np, RSND_NODE_DAI);
1017         if (dai_node) {
1018                 ret = dai_node;
1019                 goto of_node_compatible;
1020         }
1021 
1022         ret = np;
1023 
1024         dai_node = of_graph_get_next_endpoint(np, NULL);
1025         if (dai_node)
1026                 goto of_node_graph;
1027 
1028         return NULL;
1029 
1030 of_node_graph:
1031         *is_graph = 1;
1032 of_node_compatible:
1033         of_node_put(dai_node);
1034 
1035         return ret;
1036 }
1037 
1038 static void __rsnd_dai_probe(struct rsnd_priv *priv,
1039                              struct device_node *dai_np,
1040                              int dai_i)
1041 {
1042         struct device_node *playback, *capture;
1043         struct rsnd_dai_stream *io_playback;
1044         struct rsnd_dai_stream *io_capture;
1045         struct snd_soc_dai_driver *drv;
1046         struct rsnd_dai *rdai;
1047         struct device *dev = rsnd_priv_to_dev(priv);
1048         int io_i;
1049 
1050         rdai            = rsnd_rdai_get(priv, dai_i);
1051         drv             = priv->daidrv + dai_i;
1052         io_playback     = &rdai->playback;
1053         io_capture      = &rdai->capture;
1054 
1055         snprintf(rdai->name, RSND_DAI_NAME_SIZE, "rsnd-dai.%d", dai_i);
1056 
1057         rdai->priv      = priv;
1058         drv->name       = rdai->name;
1059         drv->ops        = &rsnd_soc_dai_ops;
1060 
1061         snprintf(rdai->playback.name, RSND_DAI_NAME_SIZE,
1062                  "DAI%d Playback", dai_i);
1063         drv->playback.rates             = RSND_RATES;
1064         drv->playback.formats           = RSND_FMTS;
1065         drv->playback.channels_min      = 2;
1066         drv->playback.channels_max      = 8;
1067         drv->playback.stream_name       = rdai->playback.name;
1068 
1069         snprintf(rdai->capture.name, RSND_DAI_NAME_SIZE,
1070                  "DAI%d Capture", dai_i);
1071         drv->capture.rates              = RSND_RATES;
1072         drv->capture.formats            = RSND_FMTS;
1073         drv->capture.channels_min       = 2;
1074         drv->capture.channels_max       = 8;
1075         drv->capture.stream_name        = rdai->capture.name;
1076 
1077         rdai->playback.rdai             = rdai;
1078         rdai->capture.rdai              = rdai;
1079         rsnd_rdai_channels_set(rdai, 2); /* default 2ch */
1080         rsnd_rdai_ssi_lane_set(rdai, 1); /* default 1lane */
1081 
1082         for (io_i = 0;; io_i++) {
1083                 playback = of_parse_phandle(dai_np, "playback", io_i);
1084                 capture  = of_parse_phandle(dai_np, "capture", io_i);
1085 
1086                 if (!playback && !capture)
1087                         break;
1088 
1089                 rsnd_parse_connect_ssi(rdai, playback, capture);
1090                 rsnd_parse_connect_src(rdai, playback, capture);
1091                 rsnd_parse_connect_ctu(rdai, playback, capture);
1092                 rsnd_parse_connect_mix(rdai, playback, capture);
1093                 rsnd_parse_connect_dvc(rdai, playback, capture);
1094 
1095                 of_node_put(playback);
1096                 of_node_put(capture);
1097         }
1098 
1099         dev_dbg(dev, "%s (%s/%s)\n", rdai->name,
1100                 rsnd_io_to_mod_ssi(io_playback) ? "play"    : " -- ",
1101                 rsnd_io_to_mod_ssi(io_capture) ? "capture" : "  --   ");
1102 }
1103 
1104 static int rsnd_dai_probe(struct rsnd_priv *priv)
1105 {
1106         struct device_node *dai_node;
1107         struct device_node *dai_np;
1108         struct snd_soc_dai_driver *rdrv;
1109         struct device *dev = rsnd_priv_to_dev(priv);
1110         struct rsnd_dai *rdai;
1111         int nr;
1112         int is_graph;
1113         int dai_i;
1114 
1115         dai_node = rsnd_dai_of_node(priv, &is_graph);
1116         if (is_graph)
1117                 nr = of_graph_get_endpoint_count(dai_node);
1118         else
1119                 nr = of_get_child_count(dai_node);
1120 
1121         if (!nr)
1122                 return -EINVAL;
1123 
1124         rdrv = devm_kcalloc(dev, nr, sizeof(*rdrv), GFP_KERNEL);
1125         rdai = devm_kcalloc(dev, nr, sizeof(*rdai), GFP_KERNEL);
1126         if (!rdrv || !rdai)
1127                 return -ENOMEM;
1128 
1129         priv->rdai_nr   = nr;
1130         priv->daidrv    = rdrv;
1131         priv->rdai      = rdai;
1132 
1133         /*
1134          * parse all dai
1135          */
1136         dai_i = 0;
1137         if (is_graph) {
1138                 for_each_endpoint_of_node(dai_node, dai_np) {
1139                         __rsnd_dai_probe(priv, dai_np, dai_i);
1140                         rsnd_ssi_parse_hdmi_connection(priv, dai_np, dai_i);
1141                         dai_i++;
1142                 }
1143         } else {
1144                 for_each_child_of_node(dai_node, dai_np)
1145                         __rsnd_dai_probe(priv, dai_np, dai_i++);
1146         }
1147 
1148         return 0;
1149 }
1150 
1151 /*
1152  *              pcm ops
1153  */
1154 static int rsnd_hw_params(struct snd_pcm_substream *substream,
1155                          struct snd_pcm_hw_params *hw_params)
1156 {
1157         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1158         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1159         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1160         int ret;
1161 
1162         ret = rsnd_dai_call(hw_params, io, substream, hw_params);
1163         if (ret)
1164                 return ret;
1165 
1166         return snd_pcm_lib_malloc_pages(substream,
1167                                         params_buffer_bytes(hw_params));
1168 }
1169 
1170 static snd_pcm_uframes_t rsnd_pointer(struct snd_pcm_substream *substream)
1171 {
1172         struct snd_soc_dai *dai = rsnd_substream_to_dai(substream);
1173         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1174         struct rsnd_dai_stream *io = rsnd_rdai_to_io(rdai, substream);
1175         snd_pcm_uframes_t pointer = 0;
1176 
1177         rsnd_dai_call(pointer, io, &pointer);
1178 
1179         return pointer;
1180 }
1181 
1182 static const struct snd_pcm_ops rsnd_pcm_ops = {
1183         .ioctl          = snd_pcm_lib_ioctl,
1184         .hw_params      = rsnd_hw_params,
1185         .hw_free        = snd_pcm_lib_free_pages,
1186         .pointer        = rsnd_pointer,
1187 };
1188 
1189 /*
1190  *              snd_kcontrol
1191  */
1192 static int rsnd_kctrl_info(struct snd_kcontrol *kctrl,
1193                            struct snd_ctl_elem_info *uinfo)
1194 {
1195         struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1196 
1197         if (cfg->texts) {
1198                 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1199                 uinfo->count = cfg->size;
1200                 uinfo->value.enumerated.items = cfg->max;
1201                 if (uinfo->value.enumerated.item >= cfg->max)
1202                         uinfo->value.enumerated.item = cfg->max - 1;
1203                 strlcpy(uinfo->value.enumerated.name,
1204                         cfg->texts[uinfo->value.enumerated.item],
1205                         sizeof(uinfo->value.enumerated.name));
1206         } else {
1207                 uinfo->count = cfg->size;
1208                 uinfo->value.integer.min = 0;
1209                 uinfo->value.integer.max = cfg->max;
1210                 uinfo->type = (cfg->max == 1) ?
1211                         SNDRV_CTL_ELEM_TYPE_BOOLEAN :
1212                         SNDRV_CTL_ELEM_TYPE_INTEGER;
1213         }
1214 
1215         return 0;
1216 }
1217 
1218 static int rsnd_kctrl_get(struct snd_kcontrol *kctrl,
1219                           struct snd_ctl_elem_value *uc)
1220 {
1221         struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1222         int i;
1223 
1224         for (i = 0; i < cfg->size; i++)
1225                 if (cfg->texts)
1226                         uc->value.enumerated.item[i] = cfg->val[i];
1227                 else
1228                         uc->value.integer.value[i] = cfg->val[i];
1229 
1230         return 0;
1231 }
1232 
1233 static int rsnd_kctrl_put(struct snd_kcontrol *kctrl,
1234                           struct snd_ctl_elem_value *uc)
1235 {
1236         struct rsnd_kctrl_cfg *cfg = snd_kcontrol_chip(kctrl);
1237         int i, change = 0;
1238 
1239         if (!cfg->accept(cfg->io))
1240                 return 0;
1241 
1242         for (i = 0; i < cfg->size; i++) {
1243                 if (cfg->texts) {
1244                         change |= (uc->value.enumerated.item[i] != cfg->val[i]);
1245                         cfg->val[i] = uc->value.enumerated.item[i];
1246                 } else {
1247                         change |= (uc->value.integer.value[i] != cfg->val[i]);
1248                         cfg->val[i] = uc->value.integer.value[i];
1249                 }
1250         }
1251 
1252         if (change && cfg->update)
1253                 cfg->update(cfg->io, cfg->mod);
1254 
1255         return change;
1256 }
1257 
1258 int rsnd_kctrl_accept_anytime(struct rsnd_dai_stream *io)
1259 {
1260         return 1;
1261 }
1262 
1263 int rsnd_kctrl_accept_runtime(struct rsnd_dai_stream *io)
1264 {
1265         struct snd_pcm_runtime *runtime = rsnd_io_to_runtime(io);
1266 
1267         return !!runtime;
1268 }
1269 
1270 struct rsnd_kctrl_cfg *rsnd_kctrl_init_m(struct rsnd_kctrl_cfg_m *cfg)
1271 {
1272         cfg->cfg.val = cfg->val;
1273 
1274         return &cfg->cfg;
1275 }
1276 
1277 struct rsnd_kctrl_cfg *rsnd_kctrl_init_s(struct rsnd_kctrl_cfg_s *cfg)
1278 {
1279         cfg->cfg.val = &cfg->val;
1280 
1281         return &cfg->cfg;
1282 }
1283 
1284 const char * const volume_ramp_rate[] = {
1285         "128 dB/1 step",         /* 00000 */
1286         "64 dB/1 step",          /* 00001 */
1287         "32 dB/1 step",          /* 00010 */
1288         "16 dB/1 step",          /* 00011 */
1289         "8 dB/1 step",           /* 00100 */
1290         "4 dB/1 step",           /* 00101 */
1291         "2 dB/1 step",           /* 00110 */
1292         "1 dB/1 step",           /* 00111 */
1293         "0.5 dB/1 step",         /* 01000 */
1294         "0.25 dB/1 step",        /* 01001 */
1295         "0.125 dB/1 step",       /* 01010 = VOLUME_RAMP_MAX_MIX */
1296         "0.125 dB/2 steps",      /* 01011 */
1297         "0.125 dB/4 steps",      /* 01100 */
1298         "0.125 dB/8 steps",      /* 01101 */
1299         "0.125 dB/16 steps",     /* 01110 */
1300         "0.125 dB/32 steps",     /* 01111 */
1301         "0.125 dB/64 steps",     /* 10000 */
1302         "0.125 dB/128 steps",    /* 10001 */
1303         "0.125 dB/256 steps",    /* 10010 */
1304         "0.125 dB/512 steps",    /* 10011 */
1305         "0.125 dB/1024 steps",   /* 10100 */
1306         "0.125 dB/2048 steps",   /* 10101 */
1307         "0.125 dB/4096 steps",   /* 10110 */
1308         "0.125 dB/8192 steps",   /* 10111 = VOLUME_RAMP_MAX_DVC */
1309 };
1310 
1311 int rsnd_kctrl_new(struct rsnd_mod *mod,
1312                    struct rsnd_dai_stream *io,
1313                    struct snd_soc_pcm_runtime *rtd,
1314                    const unsigned char *name,
1315                    int (*accept)(struct rsnd_dai_stream *io),
1316                    void (*update)(struct rsnd_dai_stream *io,
1317                                   struct rsnd_mod *mod),
1318                    struct rsnd_kctrl_cfg *cfg,
1319                    const char * const *texts,
1320                    int size,
1321                    u32 max)
1322 {
1323         struct snd_card *card = rtd->card->snd_card;
1324         struct snd_kcontrol *kctrl;
1325         struct snd_kcontrol_new knew = {
1326                 .iface          = SNDRV_CTL_ELEM_IFACE_MIXER,
1327                 .name           = name,
1328                 .info           = rsnd_kctrl_info,
1329                 .index          = rtd->num,
1330                 .get            = rsnd_kctrl_get,
1331                 .put            = rsnd_kctrl_put,
1332         };
1333         int ret;
1334 
1335         if (size > RSND_MAX_CHANNELS)
1336                 return -EINVAL;
1337 
1338         kctrl = snd_ctl_new1(&knew, cfg);
1339         if (!kctrl)
1340                 return -ENOMEM;
1341 
1342         ret = snd_ctl_add(card, kctrl);
1343         if (ret < 0)
1344                 return ret;
1345 
1346         cfg->texts      = texts;
1347         cfg->max        = max;
1348         cfg->size       = size;
1349         cfg->accept     = accept;
1350         cfg->update     = update;
1351         cfg->card       = card;
1352         cfg->kctrl      = kctrl;
1353         cfg->io         = io;
1354         cfg->mod        = mod;
1355 
1356         return 0;
1357 }
1358 
1359 /*
1360  *              snd_soc_component
1361  */
1362 
1363 #define PREALLOC_BUFFER         (32 * 1024)
1364 #define PREALLOC_BUFFER_MAX     (32 * 1024)
1365 
1366 static int rsnd_preallocate_pages(struct snd_soc_pcm_runtime *rtd,
1367                                   struct rsnd_dai_stream *io,
1368                                   int stream)
1369 {
1370         struct rsnd_priv *priv = rsnd_io_to_priv(io);
1371         struct device *dev = rsnd_priv_to_dev(priv);
1372         struct snd_pcm_substream *substream;
1373         int err;
1374 
1375         /*
1376          * use Audio-DMAC dev if we can use IPMMU
1377          * see
1378          *      rsnd_dmaen_attach()
1379          */
1380         if (io->dmac_dev)
1381                 dev = io->dmac_dev;
1382 
1383         for (substream = rtd->pcm->streams[stream].substream;
1384              substream;
1385              substream = substream->next) {
1386                 err = snd_pcm_lib_preallocate_pages(substream,
1387                                         SNDRV_DMA_TYPE_DEV,
1388                                         dev,
1389                                         PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1390                 if (err < 0)
1391                         return err;
1392         }
1393 
1394         return 0;
1395 }
1396 
1397 static int rsnd_pcm_new(struct snd_soc_pcm_runtime *rtd)
1398 {
1399         struct snd_soc_dai *dai = rtd->cpu_dai;
1400         struct rsnd_dai *rdai = rsnd_dai_to_rdai(dai);
1401         int ret;
1402 
1403         ret = rsnd_dai_call(pcm_new, &rdai->playback, rtd);
1404         if (ret)
1405                 return ret;
1406 
1407         ret = rsnd_dai_call(pcm_new, &rdai->capture, rtd);
1408         if (ret)
1409                 return ret;
1410 
1411         ret = rsnd_preallocate_pages(rtd, &rdai->playback,
1412                                      SNDRV_PCM_STREAM_PLAYBACK);
1413         if (ret)
1414                 return ret;
1415 
1416         ret = rsnd_preallocate_pages(rtd, &rdai->capture,
1417                                      SNDRV_PCM_STREAM_CAPTURE);
1418         if (ret)
1419                 return ret;
1420 
1421         return 0;
1422 }
1423 
1424 static const struct snd_soc_component_driver rsnd_soc_component = {
1425         .ops            = &rsnd_pcm_ops,
1426         .pcm_new        = rsnd_pcm_new,
1427         .name           = "rsnd",
1428 };
1429 
1430 static int rsnd_rdai_continuance_probe(struct rsnd_priv *priv,
1431                                        struct rsnd_dai_stream *io)
1432 {
1433         int ret;
1434 
1435         ret = rsnd_dai_call(probe, io, priv);
1436         if (ret == -EAGAIN) {
1437                 struct rsnd_mod *ssi_mod = rsnd_io_to_mod_ssi(io);
1438                 struct rsnd_mod *mod;
1439                 int i;
1440 
1441                 /*
1442                  * Fallback to PIO mode
1443                  */
1444 
1445                 /*
1446                  * call "remove" for SSI/SRC/DVC
1447                  * SSI will be switch to PIO mode if it was DMA mode
1448                  * see
1449                  *      rsnd_dma_init()
1450                  *      rsnd_ssi_fallback()
1451                  */
1452                 rsnd_dai_call(remove, io, priv);
1453 
1454                 /*
1455                  * remove all mod from io
1456                  * and, re connect ssi
1457                  */
1458                 for_each_rsnd_mod(i, mod, io)
1459                         rsnd_dai_disconnect(mod, io, i);
1460                 rsnd_dai_connect(ssi_mod, io, RSND_MOD_SSI);
1461 
1462                 /*
1463                  * fallback
1464                  */
1465                 rsnd_dai_call(fallback, io, priv);
1466 
1467                 /*
1468                  * retry to "probe".
1469                  * DAI has SSI which is PIO mode only now.
1470                  */
1471                 ret = rsnd_dai_call(probe, io, priv);
1472         }
1473 
1474         return ret;
1475 }
1476 
1477 /*
1478  *      rsnd probe
1479  */
1480 static int rsnd_probe(struct platform_device *pdev)
1481 {
1482         struct rsnd_priv *priv;
1483         struct device *dev = &pdev->dev;
1484         struct rsnd_dai *rdai;
1485         int (*probe_func[])(struct rsnd_priv *priv) = {
1486                 rsnd_gen_probe,
1487                 rsnd_dma_probe,
1488                 rsnd_ssi_probe,
1489                 rsnd_ssiu_probe,
1490                 rsnd_src_probe,
1491                 rsnd_ctu_probe,
1492                 rsnd_mix_probe,
1493                 rsnd_dvc_probe,
1494                 rsnd_cmd_probe,
1495                 rsnd_adg_probe,
1496                 rsnd_dai_probe,
1497         };
1498         int ret, i;
1499 
1500         /*
1501          *      init priv data
1502          */
1503         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
1504         if (!priv)
1505                 return -ENODEV;
1506 
1507         priv->pdev      = pdev;
1508         priv->flags     = (unsigned long)of_device_get_match_data(dev);
1509         spin_lock_init(&priv->lock);
1510 
1511         /*
1512          *      init each module
1513          */
1514         for (i = 0; i < ARRAY_SIZE(probe_func); i++) {
1515                 ret = probe_func[i](priv);
1516                 if (ret)
1517                         return ret;
1518         }
1519 
1520         for_each_rsnd_dai(rdai, priv, i) {
1521                 ret = rsnd_rdai_continuance_probe(priv, &rdai->playback);
1522                 if (ret)
1523                         goto exit_snd_probe;
1524 
1525                 ret = rsnd_rdai_continuance_probe(priv, &rdai->capture);
1526                 if (ret)
1527                         goto exit_snd_probe;
1528         }
1529 
1530         dev_set_drvdata(dev, priv);
1531 
1532         /*
1533          *      asoc register
1534          */
1535         ret = devm_snd_soc_register_component(dev, &rsnd_soc_component,
1536                                          priv->daidrv, rsnd_rdai_nr(priv));
1537         if (ret < 0) {
1538                 dev_err(dev, "cannot snd dai register\n");
1539                 goto exit_snd_probe;
1540         }
1541 
1542         pm_runtime_enable(dev);
1543 
1544         dev_info(dev, "probed\n");
1545         return ret;
1546 
1547 exit_snd_probe:
1548         for_each_rsnd_dai(rdai, priv, i) {
1549                 rsnd_dai_call(remove, &rdai->playback, priv);
1550                 rsnd_dai_call(remove, &rdai->capture, priv);
1551         }
1552 
1553         /*
1554          * adg is very special mod which can't use rsnd_dai_call(remove),
1555          * and it registers ADG clock on probe.
1556          * It should be unregister if probe failed.
1557          * Mainly it is assuming -EPROBE_DEFER case
1558          */
1559         rsnd_adg_remove(priv);
1560 
1561         return ret;
1562 }
1563 
1564 static int rsnd_remove(struct platform_device *pdev)
1565 {
1566         struct rsnd_priv *priv = dev_get_drvdata(&pdev->dev);
1567         struct rsnd_dai *rdai;
1568         void (*remove_func[])(struct rsnd_priv *priv) = {
1569                 rsnd_ssi_remove,
1570                 rsnd_ssiu_remove,
1571                 rsnd_src_remove,
1572                 rsnd_ctu_remove,
1573                 rsnd_mix_remove,
1574                 rsnd_dvc_remove,
1575                 rsnd_cmd_remove,
1576                 rsnd_adg_remove,
1577         };
1578         int ret = 0, i;
1579 
1580         snd_soc_disconnect_sync(&pdev->dev);
1581 
1582         pm_runtime_disable(&pdev->dev);
1583 
1584         for_each_rsnd_dai(rdai, priv, i) {
1585                 ret |= rsnd_dai_call(remove, &rdai->playback, priv);
1586                 ret |= rsnd_dai_call(remove, &rdai->capture, priv);
1587         }
1588 
1589         for (i = 0; i < ARRAY_SIZE(remove_func); i++)
1590                 remove_func[i](priv);
1591 
1592         return ret;
1593 }
1594 
1595 static int __maybe_unused rsnd_suspend(struct device *dev)
1596 {
1597         struct rsnd_priv *priv = dev_get_drvdata(dev);
1598 
1599         rsnd_adg_clk_disable(priv);
1600 
1601         return 0;
1602 }
1603 
1604 static int __maybe_unused rsnd_resume(struct device *dev)
1605 {
1606         struct rsnd_priv *priv = dev_get_drvdata(dev);
1607 
1608         rsnd_adg_clk_enable(priv);
1609 
1610         return 0;
1611 }
1612 
1613 static const struct dev_pm_ops rsnd_pm_ops = {
1614         SET_SYSTEM_SLEEP_PM_OPS(rsnd_suspend, rsnd_resume)
1615 };
1616 
1617 static struct platform_driver rsnd_driver = {
1618         .driver = {
1619                 .name   = "rcar_sound",
1620                 .pm     = &rsnd_pm_ops,
1621                 .of_match_table = rsnd_of_match,
1622         },
1623         .probe          = rsnd_probe,
1624         .remove         = rsnd_remove,
1625 };
1626 module_platform_driver(rsnd_driver);
1627 
1628 MODULE_LICENSE("GPL");
1629 MODULE_DESCRIPTION("Renesas R-Car audio driver");
1630 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
1631 MODULE_ALIAS("platform:rcar-pcm-audio");
1632 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp