blob: 26a60b4061cfcf2ded2073ff42ae8837cb935722 [file] [log] [blame]
Liam Girdwoodddee6272011-06-09 14:45:53 +01001/*
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>
Mark Brownd6652ef2011-12-03 20:14:31 +000022#include <linux/pm_runtime.h>
Liam Girdwoodddee6272011-06-09 14:45:53 +010023#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
Dong Aisheng17841022011-08-29 17:15:14 +080031static int soc_pcm_apply_symmetry(struct snd_pcm_substream *substream,
32 struct snd_soc_dai *soc_dai)
Liam Girdwoodddee6272011-06-09 14:45:53 +010033{
34 struct snd_soc_pcm_runtime *rtd = substream->private_data;
Liam Girdwoodddee6272011-06-09 14:45:53 +010035 int ret;
36
Dong Aisheng17841022011-08-29 17:15:14 +080037 if (!soc_dai->driver->symmetric_rates &&
Liam Girdwoodddee6272011-06-09 14:45:53 +010038 !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 */
Dong Aisheng17841022011-08-29 17:15:14 +080045 if (!soc_dai->rate) {
46 dev_warn(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +010047 "Not enforcing symmetric_rates due to race\n");
48 return 0;
49 }
50
Dong Aisheng17841022011-08-29 17:15:14 +080051 dev_dbg(soc_dai->dev, "Symmetry forces %dHz rate\n", soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010052
53 ret = snd_pcm_hw_constraint_minmax(substream->runtime,
54 SNDRV_PCM_HW_PARAM_RATE,
Dong Aisheng17841022011-08-29 17:15:14 +080055 soc_dai->rate, soc_dai->rate);
Liam Girdwoodddee6272011-06-09 14:45:53 +010056 if (ret < 0) {
Dong Aisheng17841022011-08-29 17:15:14 +080057 dev_err(soc_dai->dev,
Liam Girdwoodddee6272011-06-09 14:45:53 +010058 "Unable to apply rate symmetry constraint: %d\n", ret);
59 return ret;
60 }
61
62 return 0;
63}
64
65/*
Mark Brown58ba9b22012-01-16 18:38:51 +000066 * 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 */
70static int sample_sizes[] = {
Peter Ujfalusi88e33952012-01-25 10:09:41 +020071 24, 32,
Mark Brown58ba9b22012-01-16 18:38:51 +000072};
73
74static 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++) {
Mark Brown278047f2012-01-19 18:04:18 +000088 if (bits >= sample_sizes[i])
89 continue;
90
91 ret = snd_pcm_hw_constraint_msbits(substream->runtime, 0,
92 sample_sizes[i], bits);
Mark Brown58ba9b22012-01-16 18:38:51 +000093 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/*
Liam Girdwoodddee6272011-06-09 14:45:53 +0100101 * 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 */
105static 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
Mark Brownd6652ef2011-12-03 20:14:31 +0000116 pm_runtime_get_sync(cpu_dai->dev);
117 pm_runtime_get_sync(codec_dai->dev);
118 pm_runtime_get_sync(platform->dev);
119
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100120 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100121
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) {
Mark Brown25bfe662012-02-01 21:30:32 +0000126 dev_err(cpu_dai->dev, "can't open interface %s: %d\n",
127 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100128 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) {
Mark Brown25bfe662012-02-01 21:30:32 +0000135 dev_err(platform->dev, "can't open platform %s: %d\n",
136 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100137 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) {
Mark Brown25bfe662012-02-01 21:30:32 +0000144 dev_err(codec_dai->dev, "can't open codec %s: %d\n",
145 codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100146 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) {
Mark Brown25bfe662012-02-01 21:30:32 +0000153 pr_err("asoc: %s startup failed: %d\n",
154 rtd->dai_link->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100155 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
Mark Brown58ba9b22012-01-16 18:38:51 +0000227 soc_pcm_apply_msb(substream, codec_dai);
228 soc_pcm_apply_msb(substream, cpu_dai);
229
Liam Girdwoodddee6272011-06-09 14:45:53 +0100230 /* Symmetry only applies if we've already got an active stream. */
Dong Aisheng17841022011-08-29 17:15:14 +0800231 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);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100239 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++;
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100261 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100262 return 0;
263
264config_err:
265 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
266 rtd->dai_link->ops->shutdown(substream);
267
268machine_err:
269 if (codec_dai->driver->ops->shutdown)
270 codec_dai->driver->ops->shutdown(substream, codec_dai);
271
272codec_dai_err:
273 if (platform->driver->ops && platform->driver->ops->close)
274 platform->driver->ops->close(substream);
275
276platform_err:
277 if (cpu_dai->driver->ops->shutdown)
278 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
279out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100280 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000281
282 pm_runtime_put(platform->dev);
283 pm_runtime_put(codec_dai->dev);
284 pm_runtime_put(cpu_dai->dev);
285
Liam Girdwoodddee6272011-06-09 14:45:53 +0100286 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 */
294static 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
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100300 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100301
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;
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800310 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000311 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100312 }
313
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100314 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100315}
316
317/*
318 * Called by ALSA when a PCM substream is closed. Private data can be
319 * freed here. The cpu DAI, codec DAI, machine and platform are also
320 * shutdown.
321 */
Liam Girdwood91d5e6b2011-06-09 17:04:59 +0100322static int soc_pcm_close(struct snd_pcm_substream *substream)
Liam Girdwoodddee6272011-06-09 14:45:53 +0100323{
324 struct snd_soc_pcm_runtime *rtd = substream->private_data;
325 struct snd_soc_platform *platform = rtd->platform;
326 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
327 struct snd_soc_dai *codec_dai = rtd->codec_dai;
328 struct snd_soc_codec *codec = rtd->codec;
329
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100330 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100331
332 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
333 cpu_dai->playback_active--;
334 codec_dai->playback_active--;
335 } else {
336 cpu_dai->capture_active--;
337 codec_dai->capture_active--;
338 }
339
340 cpu_dai->active--;
341 codec_dai->active--;
342 codec->active--;
343
Dong Aisheng17841022011-08-29 17:15:14 +0800344 /* clear the corresponding DAIs rate when inactive */
345 if (!cpu_dai->active)
346 cpu_dai->rate = 0;
347
348 if (!codec_dai->active)
349 codec_dai->rate = 0;
Sascha Hauer25b76792011-08-17 09:20:01 +0200350
Liam Girdwoodddee6272011-06-09 14:45:53 +0100351 /* Muting the DAC suppresses artifacts caused during digital
352 * shutdown, for example from stopping clocks.
353 */
354 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
355 snd_soc_dai_digital_mute(codec_dai, 1);
356
357 if (cpu_dai->driver->ops->shutdown)
358 cpu_dai->driver->ops->shutdown(substream, cpu_dai);
359
360 if (codec_dai->driver->ops->shutdown)
361 codec_dai->driver->ops->shutdown(substream, codec_dai);
362
363 if (rtd->dai_link->ops && rtd->dai_link->ops->shutdown)
364 rtd->dai_link->ops->shutdown(substream);
365
366 if (platform->driver->ops && platform->driver->ops->close)
367 platform->driver->ops->close(substream);
368 cpu_dai->runtime = NULL;
369
370 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
Mark Brownb5d1d032012-02-08 20:10:56 +0000371 if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
Mark Brown5b6247a2011-10-27 12:11:26 +0200372 rtd->dai_link->ignore_pmdown_time) {
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300373 /* powered down playback stream now */
374 snd_soc_dapm_stream_event(rtd,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800375 SNDRV_PCM_STREAM_PLAYBACK,
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800376 SND_SOC_DAPM_STREAM_STOP);
Peter Ujfalusi1d69c5c2011-10-14 14:43:33 +0300377 } else {
378 /* start delayed pop wq here for playback streams */
379 codec_dai->pop_wait = 1;
380 schedule_delayed_work(&rtd->delayed_work,
381 msecs_to_jiffies(rtd->pmdown_time));
382 }
Liam Girdwoodddee6272011-06-09 14:45:53 +0100383 } else {
384 /* capture streams can be powered down now */
Mark Brown7bd3a6f2012-02-16 15:03:27 -0800385 snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000386 SND_SOC_DAPM_STREAM_STOP);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100387 }
388
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100389 mutex_unlock(&rtd->pcm_mutex);
Mark Brownd6652ef2011-12-03 20:14:31 +0000390
391 pm_runtime_put(platform->dev);
392 pm_runtime_put(codec_dai->dev);
393 pm_runtime_put(cpu_dai->dev);
394
Liam Girdwoodddee6272011-06-09 14:45:53 +0100395 return 0;
396}
397
398/*
399 * Called by ALSA when the PCM substream is prepared, can set format, sample
400 * rate, etc. This function is non atomic and can be called multiple times,
401 * it can refer to the runtime info.
402 */
403static int soc_pcm_prepare(struct snd_pcm_substream *substream)
404{
405 struct snd_soc_pcm_runtime *rtd = substream->private_data;
406 struct snd_soc_platform *platform = rtd->platform;
407 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
408 struct snd_soc_dai *codec_dai = rtd->codec_dai;
409 int ret = 0;
410
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100411 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100412
413 if (rtd->dai_link->ops && rtd->dai_link->ops->prepare) {
414 ret = rtd->dai_link->ops->prepare(substream);
415 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000416 pr_err("asoc: machine prepare error: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100417 goto out;
418 }
419 }
420
421 if (platform->driver->ops && platform->driver->ops->prepare) {
422 ret = platform->driver->ops->prepare(substream);
423 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000424 dev_err(platform->dev, "platform prepare error: %d\n",
425 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100426 goto out;
427 }
428 }
429
430 if (codec_dai->driver->ops->prepare) {
431 ret = codec_dai->driver->ops->prepare(substream, codec_dai);
432 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000433 dev_err(codec_dai->dev, "DAI prepare error: %d\n",
434 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100435 goto out;
436 }
437 }
438
439 if (cpu_dai->driver->ops->prepare) {
440 ret = cpu_dai->driver->ops->prepare(substream, cpu_dai);
441 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000442 dev_err(cpu_dai->dev, "DAI prepare error: %d\n",
443 ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100444 goto out;
445 }
446 }
447
448 /* cancel any delayed stream shutdown that is pending */
449 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
450 codec_dai->pop_wait) {
451 codec_dai->pop_wait = 0;
452 cancel_delayed_work(&rtd->delayed_work);
453 }
454
Liam Girdwoodd9b09512012-03-07 16:32:59 +0000455 snd_soc_dapm_stream_event(rtd, substream->stream,
456 SND_SOC_DAPM_STREAM_START);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100457
458 snd_soc_dai_digital_mute(codec_dai, 0);
459
460out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100461 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100462 return ret;
463}
464
465/*
466 * Called by ALSA when the hardware params are set by application. This
467 * function can also be called multiple times and can allocate buffers
468 * (using snd_pcm_lib_* ). It's non-atomic.
469 */
470static int soc_pcm_hw_params(struct snd_pcm_substream *substream,
471 struct snd_pcm_hw_params *params)
472{
473 struct snd_soc_pcm_runtime *rtd = substream->private_data;
474 struct snd_soc_platform *platform = rtd->platform;
475 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
476 struct snd_soc_dai *codec_dai = rtd->codec_dai;
477 int ret = 0;
478
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100479 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100480
481 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_params) {
482 ret = rtd->dai_link->ops->hw_params(substream, params);
483 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000484 pr_err("asoc: machine hw_params failed: %d\n", ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100485 goto out;
486 }
487 }
488
489 if (codec_dai->driver->ops->hw_params) {
490 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai);
491 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000492 dev_err(codec_dai->dev, "can't set %s hw params: %d\n",
493 codec_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100494 goto codec_err;
495 }
496 }
497
498 if (cpu_dai->driver->ops->hw_params) {
499 ret = cpu_dai->driver->ops->hw_params(substream, params, cpu_dai);
500 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000501 dev_err(cpu_dai->dev, "%s hw params failed: %d\n",
502 cpu_dai->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100503 goto interface_err;
504 }
505 }
506
507 if (platform->driver->ops && platform->driver->ops->hw_params) {
508 ret = platform->driver->ops->hw_params(substream, params);
509 if (ret < 0) {
Mark Brown25bfe662012-02-01 21:30:32 +0000510 dev_err(platform->dev, "%s hw params failed: %d\n",
511 platform->name, ret);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100512 goto platform_err;
513 }
514 }
515
Dong Aisheng17841022011-08-29 17:15:14 +0800516 /* store the rate for each DAIs */
517 cpu_dai->rate = params_rate(params);
518 codec_dai->rate = params_rate(params);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100519
520out:
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100521 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100522 return ret;
523
524platform_err:
525 if (cpu_dai->driver->ops->hw_free)
526 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
527
528interface_err:
529 if (codec_dai->driver->ops->hw_free)
530 codec_dai->driver->ops->hw_free(substream, codec_dai);
531
532codec_err:
533 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
534 rtd->dai_link->ops->hw_free(substream);
535
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100536 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100537 return ret;
538}
539
540/*
541 * Frees resources allocated by hw_params, can be called multiple times
542 */
543static int soc_pcm_hw_free(struct snd_pcm_substream *substream)
544{
545 struct snd_soc_pcm_runtime *rtd = substream->private_data;
546 struct snd_soc_platform *platform = rtd->platform;
547 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
548 struct snd_soc_dai *codec_dai = rtd->codec_dai;
549 struct snd_soc_codec *codec = rtd->codec;
550
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100551 mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100552
553 /* apply codec digital mute */
554 if (!codec->active)
555 snd_soc_dai_digital_mute(codec_dai, 1);
556
557 /* free any machine hw params */
558 if (rtd->dai_link->ops && rtd->dai_link->ops->hw_free)
559 rtd->dai_link->ops->hw_free(substream);
560
561 /* free any DMA resources */
562 if (platform->driver->ops && platform->driver->ops->hw_free)
563 platform->driver->ops->hw_free(substream);
564
565 /* now free hw params for the DAIs */
566 if (codec_dai->driver->ops->hw_free)
567 codec_dai->driver->ops->hw_free(substream, codec_dai);
568
569 if (cpu_dai->driver->ops->hw_free)
570 cpu_dai->driver->ops->hw_free(substream, cpu_dai);
571
Liam Girdwoodb8c0dab2011-06-09 17:04:39 +0100572 mutex_unlock(&rtd->pcm_mutex);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100573 return 0;
574}
575
576static int soc_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
577{
578 struct snd_soc_pcm_runtime *rtd = substream->private_data;
579 struct snd_soc_platform *platform = rtd->platform;
580 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
581 struct snd_soc_dai *codec_dai = rtd->codec_dai;
582 int ret;
583
584 if (codec_dai->driver->ops->trigger) {
585 ret = codec_dai->driver->ops->trigger(substream, cmd, codec_dai);
586 if (ret < 0)
587 return ret;
588 }
589
590 if (platform->driver->ops && platform->driver->ops->trigger) {
591 ret = platform->driver->ops->trigger(substream, cmd);
592 if (ret < 0)
593 return ret;
594 }
595
596 if (cpu_dai->driver->ops->trigger) {
597 ret = cpu_dai->driver->ops->trigger(substream, cmd, cpu_dai);
598 if (ret < 0)
599 return ret;
600 }
601 return 0;
602}
603
604/*
605 * soc level wrapper for pointer callback
606 * If cpu_dai, codec_dai, platform driver has the delay callback, than
607 * the runtime->delay will be updated accordingly.
608 */
609static snd_pcm_uframes_t soc_pcm_pointer(struct snd_pcm_substream *substream)
610{
611 struct snd_soc_pcm_runtime *rtd = substream->private_data;
612 struct snd_soc_platform *platform = rtd->platform;
613 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
614 struct snd_soc_dai *codec_dai = rtd->codec_dai;
615 struct snd_pcm_runtime *runtime = substream->runtime;
616 snd_pcm_uframes_t offset = 0;
617 snd_pcm_sframes_t delay = 0;
618
619 if (platform->driver->ops && platform->driver->ops->pointer)
620 offset = platform->driver->ops->pointer(substream);
621
622 if (cpu_dai->driver->ops->delay)
623 delay += cpu_dai->driver->ops->delay(substream, cpu_dai);
624
625 if (codec_dai->driver->ops->delay)
626 delay += codec_dai->driver->ops->delay(substream, codec_dai);
627
628 if (platform->driver->delay)
629 delay += platform->driver->delay(substream, codec_dai);
630
631 runtime->delay = delay;
632
633 return offset;
634}
635
Liam Girdwoodddee6272011-06-09 14:45:53 +0100636/* create a new pcm */
637int soc_new_pcm(struct snd_soc_pcm_runtime *rtd, int num)
638{
639 struct snd_soc_codec *codec = rtd->codec;
640 struct snd_soc_platform *platform = rtd->platform;
641 struct snd_soc_dai *codec_dai = rtd->codec_dai;
642 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
Sangsu Parka5002312012-01-02 17:15:10 +0900643 struct snd_pcm_ops *soc_pcm_ops = &rtd->ops;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100644 struct snd_pcm *pcm;
645 char new_name[64];
646 int ret = 0, playback = 0, capture = 0;
647
Sangsu Parka5002312012-01-02 17:15:10 +0900648 soc_pcm_ops->open = soc_pcm_open;
649 soc_pcm_ops->close = soc_pcm_close;
650 soc_pcm_ops->hw_params = soc_pcm_hw_params;
651 soc_pcm_ops->hw_free = soc_pcm_hw_free;
652 soc_pcm_ops->prepare = soc_pcm_prepare;
653 soc_pcm_ops->trigger = soc_pcm_trigger;
654 soc_pcm_ops->pointer = soc_pcm_pointer;
655
Liam Girdwoodddee6272011-06-09 14:45:53 +0100656 /* check client and interface hw capabilities */
657 snprintf(new_name, sizeof(new_name), "%s %s-%d",
658 rtd->dai_link->stream_name, codec_dai->name, num);
659
660 if (codec_dai->driver->playback.channels_min)
661 playback = 1;
662 if (codec_dai->driver->capture.channels_min)
663 capture = 1;
664
665 dev_dbg(rtd->card->dev, "registered pcm #%d %s\n",num,new_name);
666 ret = snd_pcm_new(rtd->card->snd_card, new_name,
667 num, playback, capture, &pcm);
668 if (ret < 0) {
669 printk(KERN_ERR "asoc: can't create pcm for codec %s\n", codec->name);
670 return ret;
671 }
672
673 /* DAPM dai link stream work */
674 INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
675
676 rtd->pcm = pcm;
677 pcm->private_data = rtd;
678 if (platform->driver->ops) {
Sangsu Parka5002312012-01-02 17:15:10 +0900679 soc_pcm_ops->mmap = platform->driver->ops->mmap;
680 soc_pcm_ops->pointer = platform->driver->ops->pointer;
681 soc_pcm_ops->ioctl = platform->driver->ops->ioctl;
682 soc_pcm_ops->copy = platform->driver->ops->copy;
683 soc_pcm_ops->silence = platform->driver->ops->silence;
684 soc_pcm_ops->ack = platform->driver->ops->ack;
685 soc_pcm_ops->page = platform->driver->ops->page;
Liam Girdwoodddee6272011-06-09 14:45:53 +0100686 }
687
688 if (playback)
Sangsu Parka5002312012-01-02 17:15:10 +0900689 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, soc_pcm_ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100690
691 if (capture)
Sangsu Parka5002312012-01-02 17:15:10 +0900692 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, soc_pcm_ops);
Liam Girdwoodddee6272011-06-09 14:45:53 +0100693
694 if (platform->driver->pcm_new) {
695 ret = platform->driver->pcm_new(rtd);
696 if (ret < 0) {
697 pr_err("asoc: platform pcm constructor failed\n");
698 return ret;
699 }
700 }
701
702 pcm->private_free = platform->driver->pcm_free;
703 printk(KERN_INFO "asoc: %s <-> %s mapping ok\n", codec_dai->name,
704 cpu_dai->name);
705 return ret;
706}