]> git.karo-electronics.de Git - karo-tx-linux.git/blob - sound/soc/soc-pcm.c
Merge tag 'v3.3-rc3' as we've got several bugfixes in there which are
[karo-tx-linux.git] / sound / soc / soc-pcm.c
1 /*
2  * soc-pcm.c  --  ALSA SoC PCM
3  *
4  * Copyright 2005 Wolfson Microelectronics PLC.
5  * Copyright 2005 Openedhand Ltd.
6  * Copyright (C) 2010 Slimlogic Ltd.
7  * Copyright (C) 2010 Texas Instruments Inc.
8  *
9  * Authors: Liam Girdwood <lrg@ti.com>
10  *          Mark Brown <broonie@opensource.wolfsonmicro.com>       
11  *
12  *  This program is free software; you can redistribute  it and/or modify it
13  *  under  the terms of  the GNU General  Public License as published by the
14  *  Free Software Foundation;  either version 2 of the  License, or (at your
15  *  option) any later version.
16  *
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/init.h>
21 #include <linux/delay.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <sound/core.h>
26 #include <sound/pcm.h>
27 #include <sound/pcm_params.h>
28 #include <sound/soc.h>
29 #include <sound/initval.h>
30
31 static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
32                                         struct snd_soc_dai *soc_dai)
33 {
34         struct snd_soc_pcm_runtime *rtd = substream->private_data;
35         int ret;
36
37         if (!soc_dai->driver->symmetric_rates &&
38             !rtd->dai_link->symmetric_rates)
39                 return 0;
40
41         /* This can happen if multiple streams are starting simultaneously -
42          * the second can need to get its constraints before the first has
43          * picked a rate.  Complain and allow the application to carry on.
44          */
45         if (!soc_dai->rate) {
46                 dev_warn(soc_dai->dev,
47                          "Not enforcing symmetric_rates due to race\n");
48                 return 0;
49         }
50
51         dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
52
53         ret = snd_pcm_hw_constraint_minmax(substream->runtime,
54                                            SNDRV_PCM_HW_PARAM_RATE,
55                                            soc_dai->rate, soc_dai->rate);
56         if (ret < 0) {
57                 dev_err(soc_dai->dev,
58                         "Unable to apply rate symmetry constraint: %d\n", ret);
59                 return ret;
60         }
61
62         return 0;
63 }
64
65 /*
66  * List of sample sizes that might go over the bus for parameter
67  * application.  There ought to be a wildcard sample size for things
68  * like the DAC/ADC resolution to use but there isn't right now.
69  */
70 static int sample_sizes[] = {
71         24, 32,
72 };
73
74 static void soc_pcm_apply_msb(struct snd_pcm_substream *substream,
75                               struct snd_soc_dai *dai)
76 {
77         int ret, i, bits;
78
79         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
80                 bits = dai->driver->playback.sig_bits;
81         else
82                 bits = dai->driver->capture.sig_bits;
83
84         if (!bits)
85                 return;
86
87         for (i = 0; i < ARRAY_SIZE(sample_sizes); i++) {
88                 if (bits >= sample_sizes[i])
89                         continue;
90
91                 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
92                                                    sample_sizes[i], bits);
93                 if (ret != 0)
94                         dev_warn(dai->dev,
95                                  "Failed to set MSB %d/%d: %d\n",
96                                  bits, sample_sizes[i], ret);
97         }
98 }
99
100 /*
101  * Called by ALSA when a PCM substream is opened, the runtime->hw record is
102  * then initialized and any private data can be allocated. This also calls
103  * startup for the cpu DAI, platform, machine and codec DAI.
104  */
105 static int soc_pcm_open(struct snd_pcm_substream *substream)
106 {
107         struct snd_soc_pcm_runtime *rtd = substream->private_data;
108         struct snd_pcm_runtime *runtime = substream->runtime;
109         struct snd_soc_platform *platform = rtd->platform;
110         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
111         struct snd_soc_dai *codec_dai = rtd->codec_dai;
112         struct snd_soc_dai_driver *cpu_dai_drv = cpu_dai->driver;
113         struct snd_soc_dai_driver *codec_dai_drv = codec_dai->driver;
114         int ret = 0;
115
116         pm_runtime_get_sync(cpu_dai->dev);
117         pm_runtime_get_sync(codec_dai->dev);
118         pm_runtime_get_sync(platform->dev);
119
120         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
121
122         /* startup the audio subsystem */
123         if (cpu_dai->driver->ops->startup) {
124                 ret = cpu_dai->driver->ops->startup(substream, cpu_dai);
125                 if (ret < 0) {
126                         dev_err(cpu_dai->dev, "can't open interface %s: %d\n",
127                                 cpu_dai->name, ret);
128                         goto out;
129                 }
130         }
131
132         if (platform->driver->ops && platform->driver->ops->open) {
133                 ret = platform->driver->ops->open(substream);
134                 if (ret < 0) {
135                         dev_err(platform->dev, "can't open platform %s: %d\n",
136                                 platform->name, ret);
137                         goto platform_err;
138                 }
139         }
140
141         if (codec_dai->driver->ops->startup) {
142                 ret = codec_dai->driver->ops->startup(substream, codec_dai);
143                 if (ret < 0) {
144                         dev_err(codec_dai->dev, "can't open codec %s: %d\n",
145                                 codec_dai->name, ret);
146                         goto codec_dai_err;
147                 }
148         }
149
150         if (rtd->dai_link->ops && rtd->dai_link->ops->startup) {
151                 ret = rtd->dai_link->ops->startup(substream);
152                 if (ret < 0) {
153                         pr_err("asoc: %s startup failed: %d\n",
154                                rtd->dai_link->name, ret);
155                         goto machine_err;
156                 }
157         }
158
159         /* Check that the codec and cpu DAIs are compatible */
160         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
161                 runtime->hw.rate_min =
162                         max(codec_dai_drv->playback.rate_min,
163                             cpu_dai_drv->playback.rate_min);
164                 runtime->hw.rate_max =
165                         min(codec_dai_drv->playback.rate_max,
166                             cpu_dai_drv->playback.rate_max);
167                 runtime->hw.channels_min =
168                         max(codec_dai_drv->playback.channels_min,
169                                 cpu_dai_drv->playback.channels_min);
170                 runtime->hw.channels_max =
171                         min(codec_dai_drv->playback.channels_max,
172                                 cpu_dai_drv->playback.channels_max);
173                 runtime->hw.formats =
174                         codec_dai_drv->playback.formats & cpu_dai_drv->playback.formats;
175                 runtime->hw.rates =
176                         codec_dai_drv->playback.rates & cpu_dai_drv->playback.rates;
177                 if (codec_dai_drv->playback.rates
178                            & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
179                         runtime->hw.rates |= cpu_dai_drv->playback.rates;
180                 if (cpu_dai_drv->playback.rates
181                            & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
182                         runtime->hw.rates |= codec_dai_drv->playback.rates;
183         } else {
184                 runtime->hw.rate_min =
185                         max(codec_dai_drv->capture.rate_min,
186                             cpu_dai_drv->capture.rate_min);
187                 runtime->hw.rate_max =
188                         min(codec_dai_drv->capture.rate_max,
189                             cpu_dai_drv->capture.rate_max);
190                 runtime->hw.channels_min =
191                         max(codec_dai_drv->capture.channels_min,
192                                 cpu_dai_drv->capture.channels_min);
193                 runtime->hw.channels_max =
194                         min(codec_dai_drv->capture.channels_max,
195                                 cpu_dai_drv->capture.channels_max);
196                 runtime->hw.formats =
197                         codec_dai_drv->capture.formats & cpu_dai_drv->capture.formats;
198                 runtime->hw.rates =
199                         codec_dai_drv->capture.rates & cpu_dai_drv->capture.rates;
200                 if (codec_dai_drv->capture.rates
201                            & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
202                         runtime->hw.rates |= cpu_dai_drv->capture.rates;
203                 if (cpu_dai_drv->capture.rates
204                            & (SNDRV_PCM_RATE_KNOT | SNDRV_PCM_RATE_CONTINUOUS))
205                         runtime->hw.rates |= codec_dai_drv->capture.rates;
206         }
207
208         ret = -EINVAL;
209         snd_pcm_limit_hw_rates(runtime);
210         if (!runtime->hw.rates) {
211                 printk(KERN_ERR "asoc: %s <-> %s No matching rates\n",
212                         codec_dai->name, cpu_dai->name);
213                 goto config_err;
214         }
215         if (!runtime->hw.formats) {
216                 printk(KERN_ERR "asoc: %s <-> %s No matching formats\n",
217                         codec_dai->name, cpu_dai->name);
218                 goto config_err;
219         }
220         if (!runtime->hw.channels_min || !runtime->hw.channels_max ||
221             runtime->hw.channels_min > runtime->hw.channels_max) {
222                 printk(KERN_ERR "asoc: %s <-> %s No matching channels\n",
223                                 codec_dai->name, cpu_dai->name);
224                 goto config_err;
225         }
226
227         soc_pcm_apply_msb(substream, codec_dai);
228         soc_pcm_apply_msb(substream, cpu_dai);
229
230         /* Symmetry only applies if we've already got an active stream. */
231         if (cpu_dai->active) {
232                 ret = soc_pcm_apply_symmetry(substream, cpu_dai);
233                 if (ret != 0)
234                         goto config_err;
235         }
236
237         if (codec_dai->active) {
238                 ret = soc_pcm_apply_symmetry(substream, codec_dai);
239                 if (ret != 0)
240                         goto config_err;
241         }
242
243         pr_debug("asoc: %s <-> %s info:\n",
244                         codec_dai->name, cpu_dai->name);
245         pr_debug("asoc: rate mask 0x%x\n", runtime->hw.rates);
246         pr_debug("asoc: min ch %d max ch %d\n", runtime->hw.channels_min,
247                  runtime->hw.channels_max);
248         pr_debug("asoc: min rate %d max rate %d\n", runtime->hw.rate_min,
249                  runtime->hw.rate_max);
250
251         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
252                 cpu_dai->playback_active++;
253                 codec_dai->playback_active++;
254         } else {
255                 cpu_dai->capture_active++;
256                 codec_dai->capture_active++;
257         }
258         cpu_dai->active++;
259         codec_dai->active++;
260         rtd->codec->active++;
261         mutex_unlock(&rtd->pcm_mutex);
262         return 0;
263
264 config_err:
265         if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
266                 rtd->dai_link->ops->shutdown(substream);
267
268 machine_err:
269         if (codec_dai->driver->ops->shutdown)
270                 codec_dai->driver->ops->shutdown(substream, codec_dai);
271
272 codec_dai_err:
273         if (platform->driver->ops && platform->driver->ops->close)
274                 platform->driver->ops->close(substream);
275
276 platform_err:
277         if (cpu_dai->driver->ops->shutdown)
278                 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
279 out:
280         mutex_unlock(&rtd->pcm_mutex);
281
282         pm_runtime_put(platform->dev);
283         pm_runtime_put(codec_dai->dev);
284         pm_runtime_put(cpu_dai->dev);
285
286         return ret;
287 }
288
289 /*
290  * Power down the audio subsystem pmdown_time msecs after close is called.
291  * This is to ensure there are no pops or clicks in between any music tracks
292  * due to DAPM power cycling.
293  */
294 static void close_delayed_work(struct work_struct *work)
295 {
296         struct snd_soc_pcm_runtime *rtd =
297                         container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
298         struct snd_soc_dai *codec_dai = rtd->codec_dai;
299
300         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
301
302         pr_debug("pop wq checking: %s status: %s waiting: %s\n",
303                  codec_dai->driver->playback.stream_name,
304                  codec_dai->playback_active ? "active" : "inactive",
305                  codec_dai->pop_wait ? "yes" : "no");
306
307         /* are we waiting on this codec DAI stream */
308         if (codec_dai->pop_wait == 1) {
309                 codec_dai->pop_wait = 0;
310                 snd_soc_dapm_stream_event(rtd,
311                         codec_dai->driver->playback.stream_name,
312                         SND_SOC_DAPM_STREAM_STOP);
313         }
314
315         mutex_unlock(&rtd->pcm_mutex);
316 }
317
318 /*
319  * Called by ALSA when a PCM substream is closed. Private data can be
320  * freed here. The cpu DAI, codec DAI, machine and platform are also
321  * shutdown.
322  */
323 static int soc_pcm_close(struct snd_pcm_substream *substream)
324 {
325         struct snd_soc_pcm_runtime *rtd = substream->private_data;
326         struct snd_soc_platform *platform = rtd->platform;
327         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
328         struct snd_soc_dai *codec_dai = rtd->codec_dai;
329         struct snd_soc_codec *codec = rtd->codec;
330
331         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
332
333         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
334                 cpu_dai->playback_active--;
335                 codec_dai->playback_active--;
336         } else {
337                 cpu_dai->capture_active--;
338                 codec_dai->capture_active--;
339         }
340
341         cpu_dai->active--;
342         codec_dai->active--;
343         codec->active--;
344
345         /* clear the corresponding DAIs rate when inactive */
346         if (!cpu_dai->active)
347                 cpu_dai->rate = 0;
348
349         if (!codec_dai->active)
350                 codec_dai->rate = 0;
351
352         /* Muting the DAC suppresses artifacts caused during digital
353          * shutdown, for example from stopping clocks.
354          */
355         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
356                 snd_soc_dai_digital_mute(codec_dai, 1);
357
358         if (cpu_dai->driver->ops->shutdown)
359                 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
360
361         if (codec_dai->driver->ops->shutdown)
362                 codec_dai->driver->ops->shutdown(substream, codec_dai);
363
364         if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
365                 rtd->dai_link->ops->shutdown(substream);
366
367         if (platform->driver->ops && platform->driver->ops->close)
368                 platform->driver->ops->close(substream);
369         cpu_dai->runtime = NULL;
370
371         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
372                 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
373                     rtd->dai_link->ignore_pmdown_time) {
374                         /* powered down playback stream now */
375                         snd_soc_dapm_stream_event(rtd,
376                                 codec_dai->driver->playback.stream_name,
377                                 SND_SOC_DAPM_STREAM_STOP);
378                 } else {
379                         /* start delayed pop wq here for playback streams */
380                         codec_dai->pop_wait = 1;
381                         schedule_delayed_work(&rtd->delayed_work,
382                                 msecs_to_jiffies(rtd->pmdown_time));
383                 }
384         } else {
385                 /* capture streams can be powered down now */
386                 snd_soc_dapm_stream_event(rtd,
387                         codec_dai->driver->capture.stream_name,
388                         SND_SOC_DAPM_STREAM_STOP);
389         }
390
391         mutex_unlock(&rtd->pcm_mutex);
392
393         pm_runtime_put(platform->dev);
394         pm_runtime_put(codec_dai->dev);
395         pm_runtime_put(cpu_dai->dev);
396
397         return 0;
398 }
399
400 /*
401  * Called by ALSA when the PCM substream is prepared, can set format, sample
402  * rate, etc.  This function is non atomic and can be called multiple times,
403  * it can refer to the runtime info.
404  */
405 static int soc_pcm_prepare(struct snd_pcm_substream *substream)
406 {
407         struct snd_soc_pcm_runtime *rtd = substream->private_data;
408         struct snd_soc_platform *platform = rtd->platform;
409         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
410         struct snd_soc_dai *codec_dai = rtd->codec_dai;
411         int ret = 0;
412
413         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
414
415         if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
416                 ret = rtd->dai_link->ops->prepare(substream);
417                 if (ret < 0) {
418                         pr_err("asoc: machine prepare error: %d\n", ret);
419                         goto out;
420                 }
421         }
422
423         if (platform->driver->ops && platform->driver->ops->prepare) {
424                 ret = platform->driver->ops->prepare(substream);
425                 if (ret < 0) {
426                         dev_err(platform->dev, "platform prepare error: %d\n",
427                                 ret);
428                         goto out;
429                 }
430         }
431
432         if (codec_dai->driver->ops->prepare) {
433                 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
434                 if (ret < 0) {
435                         dev_err(codec_dai->dev, "DAI prepare error: %d\n",
436                                 ret);
437                         goto out;
438                 }
439         }
440
441         if (cpu_dai->driver->ops->prepare) {
442                 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
443                 if (ret < 0) {
444                         dev_err(cpu_dai->dev, "DAI prepare error: %d\n",
445                                 ret);
446                         goto out;
447                 }
448         }
449
450         /* cancel any delayed stream shutdown that is pending */
451         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
452             codec_dai->pop_wait) {
453                 codec_dai->pop_wait = 0;
454                 cancel_delayed_work(&rtd->delayed_work);
455         }
456
457         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
458                 snd_soc_dapm_stream_event(rtd,
459                                           codec_dai->driver->playback.stream_name,
460                                           SND_SOC_DAPM_STREAM_START);
461         else
462                 snd_soc_dapm_stream_event(rtd,
463                                           codec_dai->driver->capture.stream_name,
464                                           SND_SOC_DAPM_STREAM_START);
465
466         snd_soc_dai_digital_mute(codec_dai, 0);
467
468 out:
469         mutex_unlock(&rtd->pcm_mutex);
470         return ret;
471 }
472
473 /*
474  * Called by ALSA when the hardware params are set by application. This
475  * function can also be called multiple times and can allocate buffers
476  * (using snd_pcm_lib_* ). It's non-atomic.
477  */
478 static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
479                                 struct snd_pcm_hw_params *params)
480 {
481         struct snd_soc_pcm_runtime *rtd = substream->private_data;
482         struct snd_soc_platform *platform = rtd->platform;
483         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
484         struct snd_soc_dai *codec_dai = rtd->codec_dai;
485         int ret = 0;
486
487         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
488
489         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
490                 ret = rtd->dai_link->ops->hw_params(substream, params);
491                 if (ret < 0) {
492                         pr_err("asoc: machine hw_params failed: %d\n", ret);
493                         goto out;
494                 }
495         }
496
497         if (codec_dai->driver->ops->hw_params) {
498                 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
499                 if (ret < 0) {
500                         dev_err(codec_dai->dev, "can't set %s hw params: %d\n",
501                                 codec_dai->name, ret);
502                         goto codec_err;
503                 }
504         }
505
506         if (cpu_dai->driver->ops->hw_params) {
507                 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
508                 if (ret < 0) {
509                         dev_err(cpu_dai->dev, "%s hw params failed: %d\n",
510                                 cpu_dai->name, ret);
511                         goto interface_err;
512                 }
513         }
514
515         if (platform->driver->ops && platform->driver->ops->hw_params) {
516                 ret = platform->driver->ops->hw_params(substream, params);
517                 if (ret < 0) {
518                         dev_err(platform->dev, "%s hw params failed: %d\n",
519                                platform->name, ret);
520                         goto platform_err;
521                 }
522         }
523
524         /* store the rate for each DAIs */
525         cpu_dai->rate = params_rate(params);
526         codec_dai->rate = params_rate(params);
527
528 out:
529         mutex_unlock(&rtd->pcm_mutex);
530         return ret;
531
532 platform_err:
533         if (cpu_dai->driver->ops->hw_free)
534                 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
535
536 interface_err:
537         if (codec_dai->driver->ops->hw_free)
538                 codec_dai->driver->ops->hw_free(substream, codec_dai);
539
540 codec_err:
541         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
542                 rtd->dai_link->ops->hw_free(substream);
543
544         mutex_unlock(&rtd->pcm_mutex);
545         return ret;
546 }
547
548 /*
549  * Frees resources allocated by hw_params, can be called multiple times
550  */
551 static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
552 {
553         struct snd_soc_pcm_runtime *rtd = substream->private_data;
554         struct snd_soc_platform *platform = rtd->platform;
555         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
556         struct snd_soc_dai *codec_dai = rtd->codec_dai;
557         struct snd_soc_codec *codec = rtd->codec;
558
559         mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
560
561         /* apply codec digital mute */
562         if (!codec->active)
563                 snd_soc_dai_digital_mute(codec_dai, 1);
564
565         /* free any machine hw params */
566         if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
567                 rtd->dai_link->ops->hw_free(substream);
568
569         /* free any DMA resources */
570         if (platform->driver->ops && platform->driver->ops->hw_free)
571                 platform->driver->ops->hw_free(substream);
572
573         /* now free hw params for the DAIs  */
574         if (codec_dai->driver->ops->hw_free)
575                 codec_dai->driver->ops->hw_free(substream, codec_dai);
576
577         if (cpu_dai->driver->ops->hw_free)
578                 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
579
580         mutex_unlock(&rtd->pcm_mutex);
581         return 0;
582 }
583
584 static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
585 {
586         struct snd_soc_pcm_runtime *rtd = substream->private_data;
587         struct snd_soc_platform *platform = rtd->platform;
588         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
589         struct snd_soc_dai *codec_dai = rtd->codec_dai;
590         int ret;
591
592         if (codec_dai->driver->ops->trigger) {
593                 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
594                 if (ret < 0)
595                         return ret;
596         }
597
598         if (platform->driver->ops && platform->driver->ops->trigger) {
599                 ret = platform->driver->ops->trigger(substream, cmd);
600                 if (ret < 0)
601                         return ret;
602         }
603
604         if (cpu_dai->driver->ops->trigger) {
605                 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
606                 if (ret < 0)
607                         return ret;
608         }
609         return 0;
610 }
611
612 /*
613  * soc level wrapper for pointer callback
614  * If cpu_dai, codec_dai, platform driver has the delay callback, than
615  * the runtime->delay will be updated accordingly.
616  */
617 static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
618 {
619         struct snd_soc_pcm_runtime *rtd = substream->private_data;
620         struct snd_soc_platform *platform = rtd->platform;
621         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
622         struct snd_soc_dai *codec_dai = rtd->codec_dai;
623         struct snd_pcm_runtime *runtime = substream->runtime;
624         snd_pcm_uframes_t offset = 0;
625         snd_pcm_sframes_t delay = 0;
626
627         if (platform->driver->ops && platform->driver->ops->pointer)
628                 offset = platform->driver->ops->pointer(substream);
629
630         if (cpu_dai->driver->ops->delay)
631                 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
632
633         if (codec_dai->driver->ops->delay)
634                 delay += codec_dai->driver->ops->delay(substream, codec_dai);
635
636         if (platform->driver->delay)
637                 delay += platform->driver->delay(substream, codec_dai);
638
639         runtime->delay = delay;
640
641         return offset;
642 }
643
644 /* create a new pcm */
645 int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
646 {
647         struct snd_soc_codec *codec = rtd->codec;
648         struct snd_soc_platform *platform = rtd->platform;
649         struct snd_soc_dai *codec_dai = rtd->codec_dai;
650         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
651         struct snd_pcm_ops *soc_pcm_ops = &rtd->ops;
652         struct snd_pcm *pcm;
653         char new_name[64];
654         int ret = 0, playback = 0, capture = 0;
655
656         soc_pcm_ops->open       = soc_pcm_open;
657         soc_pcm_ops->close      = soc_pcm_close;
658         soc_pcm_ops->hw_params  = soc_pcm_hw_params;
659         soc_pcm_ops->hw_free    = soc_pcm_hw_free;
660         soc_pcm_ops->prepare    = soc_pcm_prepare;
661         soc_pcm_ops->trigger    = soc_pcm_trigger;
662         soc_pcm_ops->pointer    = soc_pcm_pointer;
663
664         /* check client and interface hw capabilities */
665         snprintf(new_name, sizeof(new_name), "%s %s-%d",
666                         rtd->dai_link->stream_name, codec_dai->name, num);
667
668         if (codec_dai->driver->playback.channels_min)
669                 playback = 1;
670         if (codec_dai->driver->capture.channels_min)
671                 capture = 1;
672
673         dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
674         ret = snd_pcm_new(rtd->card->snd_card, new_name,
675                         num, playback, capture, &pcm);
676         if (ret < 0) {
677                 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
678                 return ret;
679         }
680
681         /* DAPM dai link stream work */
682         INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
683
684         rtd->pcm = pcm;
685         pcm->private_data = rtd;
686         if (platform->driver->ops) {
687                 soc_pcm_ops->mmap = platform->driver->ops->mmap;
688                 soc_pcm_ops->pointer = platform->driver->ops->pointer;
689                 soc_pcm_ops->ioctl = platform->driver->ops->ioctl;
690                 soc_pcm_ops->copy = platform->driver->ops->copy;
691                 soc_pcm_ops->silence = platform->driver->ops->silence;
692                 soc_pcm_ops->ack = platform->driver->ops->ack;
693                 soc_pcm_ops->page = platform->driver->ops->page;
694         }
695
696         if (playback)
697                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, soc_pcm_ops);
698
699         if (capture)
700                 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, soc_pcm_ops);
701
702         if (platform->driver->pcm_new) {
703                 ret = platform->driver->pcm_new(rtd);
704                 if (ret < 0) {
705                         pr_err("asoc: platform pcm constructor failed\n");
706                         return ret;
707                 }
708         }
709
710         pcm->private_free = platform->driver->pcm_free;
711         printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
712                 cpu_dai->name);
713         return ret;
714 }