Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 1 | /* |
| 2 | * soc-compress.c -- ALSA SoC Compress |
| 3 | * |
| 4 | * Copyright (C) 2012 Intel Corp. |
| 5 | * |
| 6 | * Authors: Namarta Kohli <namartax.kohli@intel.com> |
| 7 | * Ramesh Babu K V <ramesh.babu@linux.intel.com> |
| 8 | * Vinod Koul <vinod.koul@linux.intel.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the |
| 12 | * Free Software Foundation; either version 2 of the License, or (at your |
| 13 | * option) any later version. |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <sound/core.h> |
| 23 | #include <sound/compress_params.h> |
| 24 | #include <sound/compress_driver.h> |
| 25 | #include <sound/soc.h> |
| 26 | #include <sound/initval.h> |
Liam Girdwood | 2a99ef0 | 2014-01-17 17:03:56 +0000 | [diff] [blame] | 27 | #include <sound/soc-dpcm.h> |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 28 | |
| 29 | static int soc_compr_open(struct snd_compr_stream *cstream) |
| 30 | { |
| 31 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 32 | struct snd_soc_platform *platform = rtd->platform; |
| 33 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 34 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 35 | int ret = 0; |
| 36 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 37 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 38 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 39 | if (platform->driver->compr_ops && platform->driver->compr_ops->open) { |
| 40 | ret = platform->driver->compr_ops->open(cstream); |
| 41 | if (ret < 0) { |
| 42 | pr_err("compress asoc: can't open platform %s\n", platform->name); |
| 43 | goto out; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) { |
| 48 | ret = rtd->dai_link->compr_ops->startup(cstream); |
| 49 | if (ret < 0) { |
| 50 | pr_err("compress asoc: %s startup failed\n", rtd->dai_link->name); |
| 51 | goto machine_err; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { |
| 56 | cpu_dai->playback_active++; |
| 57 | codec_dai->playback_active++; |
| 58 | } else { |
| 59 | cpu_dai->capture_active++; |
| 60 | codec_dai->capture_active++; |
| 61 | } |
| 62 | |
| 63 | cpu_dai->active++; |
| 64 | codec_dai->active++; |
| 65 | rtd->codec->active++; |
| 66 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 67 | mutex_unlock(&rtd->pcm_mutex); |
| 68 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 69 | return 0; |
| 70 | |
| 71 | machine_err: |
| 72 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) |
| 73 | platform->driver->compr_ops->free(cstream); |
| 74 | out: |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 75 | mutex_unlock(&rtd->pcm_mutex); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 76 | return ret; |
| 77 | } |
| 78 | |
Liam Girdwood | 2a99ef0 | 2014-01-17 17:03:56 +0000 | [diff] [blame] | 79 | static int soc_compr_open_fe(struct snd_compr_stream *cstream) |
| 80 | { |
| 81 | struct snd_soc_pcm_runtime *fe = cstream->private_data; |
| 82 | struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream; |
| 83 | struct snd_soc_platform *platform = fe->platform; |
| 84 | struct snd_soc_dai *cpu_dai = fe->cpu_dai; |
| 85 | struct snd_soc_dai *codec_dai = fe->codec_dai; |
| 86 | struct snd_soc_dpcm *dpcm; |
| 87 | struct snd_soc_dapm_widget_list *list; |
| 88 | int stream; |
| 89 | int ret = 0; |
| 90 | |
| 91 | if (cstream->direction == SND_COMPRESS_PLAYBACK) |
| 92 | stream = SNDRV_PCM_STREAM_PLAYBACK; |
| 93 | else |
| 94 | stream = SNDRV_PCM_STREAM_CAPTURE; |
| 95 | |
| 96 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 97 | |
| 98 | if (platform->driver->compr_ops && platform->driver->compr_ops->open) { |
| 99 | ret = platform->driver->compr_ops->open(cstream); |
| 100 | if (ret < 0) { |
| 101 | pr_err("compress asoc: can't open platform %s\n", platform->name); |
| 102 | goto out; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) { |
| 107 | ret = fe->dai_link->compr_ops->startup(cstream); |
| 108 | if (ret < 0) { |
| 109 | pr_err("compress asoc: %s startup failed\n", fe->dai_link->name); |
| 110 | goto machine_err; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | fe->dpcm[stream].runtime = fe_substream->runtime; |
| 115 | |
| 116 | if (dpcm_path_get(fe, stream, &list) <= 0) { |
| 117 | dev_dbg(fe->dev, "ASoC: %s no valid %s route\n", |
| 118 | fe->dai_link->name, stream ? "capture" : "playback"); |
| 119 | } |
| 120 | |
| 121 | /* calculate valid and active FE <-> BE dpcms */ |
| 122 | dpcm_process_paths(fe, stream, &list, 1); |
| 123 | |
| 124 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 125 | |
| 126 | ret = dpcm_be_dai_startup(fe, stream); |
| 127 | if (ret < 0) { |
| 128 | /* clean up all links */ |
| 129 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 130 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 131 | |
| 132 | dpcm_be_disconnect(fe, stream); |
| 133 | fe->dpcm[stream].runtime = NULL; |
| 134 | goto fe_err; |
| 135 | } |
| 136 | |
| 137 | dpcm_clear_pending_state(fe, stream); |
| 138 | dpcm_path_put(&list); |
| 139 | |
| 140 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; |
| 141 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 142 | |
| 143 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { |
| 144 | cpu_dai->playback_active++; |
| 145 | codec_dai->playback_active++; |
| 146 | } else { |
| 147 | cpu_dai->capture_active++; |
| 148 | codec_dai->capture_active++; |
| 149 | } |
| 150 | |
| 151 | cpu_dai->active++; |
| 152 | codec_dai->active++; |
| 153 | fe->codec->active++; |
| 154 | |
| 155 | mutex_unlock(&fe->card->mutex); |
| 156 | |
| 157 | return 0; |
| 158 | |
| 159 | fe_err: |
| 160 | if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown) |
| 161 | fe->dai_link->compr_ops->shutdown(cstream); |
| 162 | machine_err: |
| 163 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) |
| 164 | platform->driver->compr_ops->free(cstream); |
| 165 | out: |
| 166 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 167 | mutex_unlock(&fe->card->mutex); |
| 168 | return ret; |
| 169 | } |
| 170 | |
Charles Keepax | 202c8f7 | 2013-01-24 09:44:30 +0000 | [diff] [blame] | 171 | /* |
| 172 | * Power down the audio subsystem pmdown_time msecs after close is called. |
| 173 | * This is to ensure there are no pops or clicks in between any music tracks |
| 174 | * due to DAPM power cycling. |
| 175 | */ |
| 176 | static void close_delayed_work(struct work_struct *work) |
| 177 | { |
| 178 | struct snd_soc_pcm_runtime *rtd = |
| 179 | container_of(work, struct snd_soc_pcm_runtime, delayed_work.work); |
| 180 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 181 | |
| 182 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 183 | |
| 184 | dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n", |
| 185 | codec_dai->driver->playback.stream_name, |
| 186 | codec_dai->playback_active ? "active" : "inactive", |
| 187 | rtd->pop_wait ? "yes" : "no"); |
| 188 | |
| 189 | /* are we waiting on this codec DAI stream */ |
| 190 | if (rtd->pop_wait == 1) { |
| 191 | rtd->pop_wait = 0; |
| 192 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, |
| 193 | SND_SOC_DAPM_STREAM_STOP); |
| 194 | } |
| 195 | |
| 196 | mutex_unlock(&rtd->pcm_mutex); |
| 197 | } |
| 198 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 199 | static int soc_compr_free(struct snd_compr_stream *cstream) |
| 200 | { |
| 201 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 202 | struct snd_soc_platform *platform = rtd->platform; |
| 203 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 204 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 205 | struct snd_soc_codec *codec = rtd->codec; |
| 206 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 207 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 208 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 209 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { |
| 210 | cpu_dai->playback_active--; |
| 211 | codec_dai->playback_active--; |
| 212 | } else { |
| 213 | cpu_dai->capture_active--; |
| 214 | codec_dai->capture_active--; |
| 215 | } |
| 216 | |
Mark Brown | da18396 | 2013-02-06 15:44:07 +0000 | [diff] [blame] | 217 | snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction); |
| 218 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 219 | cpu_dai->active--; |
| 220 | codec_dai->active--; |
| 221 | codec->active--; |
| 222 | |
| 223 | if (!cpu_dai->active) |
| 224 | cpu_dai->rate = 0; |
| 225 | |
| 226 | if (!codec_dai->active) |
| 227 | codec_dai->rate = 0; |
| 228 | |
| 229 | |
| 230 | if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown) |
| 231 | rtd->dai_link->compr_ops->shutdown(cstream); |
| 232 | |
| 233 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) |
| 234 | platform->driver->compr_ops->free(cstream); |
Vinod Koul | 9d2667a | 2012-08-21 12:15:02 +0530 | [diff] [blame] | 235 | cpu_dai->runtime = NULL; |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 236 | |
| 237 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { |
| 238 | if (!rtd->pmdown_time || codec->ignore_pmdown_time || |
| 239 | rtd->dai_link->ignore_pmdown_time) { |
| 240 | snd_soc_dapm_stream_event(rtd, |
| 241 | SNDRV_PCM_STREAM_PLAYBACK, |
| 242 | SND_SOC_DAPM_STREAM_STOP); |
Charles Keepax | 8c3d2aa | 2013-01-24 09:44:28 +0000 | [diff] [blame] | 243 | } else { |
Misael Lopez Cruz | 9bffb1f | 2012-12-13 12:23:05 -0600 | [diff] [blame] | 244 | rtd->pop_wait = 1; |
Mark Brown | 3d24cfe | 2013-08-09 18:12:29 +0100 | [diff] [blame] | 245 | queue_delayed_work(system_power_efficient_wq, |
| 246 | &rtd->delayed_work, |
| 247 | msecs_to_jiffies(rtd->pmdown_time)); |
Charles Keepax | 8c3d2aa | 2013-01-24 09:44:28 +0000 | [diff] [blame] | 248 | } |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 249 | } else { |
| 250 | /* capture streams can be powered down now */ |
| 251 | snd_soc_dapm_stream_event(rtd, |
| 252 | SNDRV_PCM_STREAM_CAPTURE, |
| 253 | SND_SOC_DAPM_STREAM_STOP); |
| 254 | } |
| 255 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 256 | mutex_unlock(&rtd->pcm_mutex); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 257 | return 0; |
| 258 | } |
| 259 | |
Liam Girdwood | 2a99ef0 | 2014-01-17 17:03:56 +0000 | [diff] [blame] | 260 | static int soc_compr_free_fe(struct snd_compr_stream *cstream) |
| 261 | { |
| 262 | struct snd_soc_pcm_runtime *fe = cstream->private_data; |
| 263 | struct snd_soc_platform *platform = fe->platform; |
| 264 | struct snd_soc_dai *cpu_dai = fe->cpu_dai; |
| 265 | struct snd_soc_dai *codec_dai = fe->codec_dai; |
| 266 | struct snd_soc_dpcm *dpcm; |
| 267 | int stream, ret; |
| 268 | |
| 269 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 270 | |
| 271 | if (cstream->direction == SND_COMPRESS_PLAYBACK) { |
| 272 | stream = SNDRV_PCM_STREAM_PLAYBACK; |
| 273 | cpu_dai->playback_active--; |
| 274 | codec_dai->playback_active--; |
| 275 | } else { |
| 276 | stream = SNDRV_PCM_STREAM_CAPTURE; |
| 277 | cpu_dai->capture_active--; |
| 278 | codec_dai->capture_active--; |
| 279 | } |
| 280 | |
| 281 | cpu_dai->active--; |
| 282 | codec_dai->active--; |
| 283 | fe->codec->active--; |
| 284 | |
| 285 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 286 | |
| 287 | ret = dpcm_be_dai_hw_free(fe, stream); |
| 288 | if (ret < 0) |
| 289 | dev_err(fe->dev, "compressed hw_free failed %d\n", ret); |
| 290 | |
| 291 | ret = dpcm_be_dai_shutdown(fe, stream); |
| 292 | |
| 293 | /* mark FE's links ready to prune */ |
| 294 | list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be) |
| 295 | dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; |
| 296 | |
| 297 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 298 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); |
| 299 | else |
| 300 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); |
| 301 | |
| 302 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; |
| 303 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 304 | |
| 305 | dpcm_be_disconnect(fe, stream); |
| 306 | |
| 307 | fe->dpcm[stream].runtime = NULL; |
| 308 | |
| 309 | if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown) |
| 310 | fe->dai_link->compr_ops->shutdown(cstream); |
| 311 | |
| 312 | if (platform->driver->compr_ops && platform->driver->compr_ops->free) |
| 313 | platform->driver->compr_ops->free(cstream); |
| 314 | |
| 315 | mutex_unlock(&fe->card->mutex); |
| 316 | return 0; |
| 317 | } |
| 318 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 319 | static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd) |
| 320 | { |
| 321 | |
| 322 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 323 | struct snd_soc_platform *platform = rtd->platform; |
| 324 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 325 | int ret = 0; |
| 326 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 327 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 328 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 329 | if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) { |
| 330 | ret = platform->driver->compr_ops->trigger(cstream, cmd); |
| 331 | if (ret < 0) |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 332 | goto out; |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 333 | } |
| 334 | |
Mark Brown | da18396 | 2013-02-06 15:44:07 +0000 | [diff] [blame] | 335 | switch (cmd) { |
| 336 | case SNDRV_PCM_TRIGGER_START: |
| 337 | snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction); |
| 338 | break; |
| 339 | case SNDRV_PCM_TRIGGER_STOP: |
| 340 | snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction); |
| 341 | break; |
Mark Brown | e38b9b7 | 2013-02-06 13:52:42 +0000 | [diff] [blame] | 342 | } |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 343 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 344 | out: |
| 345 | mutex_unlock(&rtd->pcm_mutex); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 346 | return ret; |
| 347 | } |
| 348 | |
Liam Girdwood | 2a99ef0 | 2014-01-17 17:03:56 +0000 | [diff] [blame] | 349 | static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd) |
| 350 | { |
| 351 | struct snd_soc_pcm_runtime *fe = cstream->private_data; |
| 352 | struct snd_soc_platform *platform = fe->platform; |
| 353 | int ret = 0, stream; |
| 354 | |
| 355 | if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN || |
| 356 | cmd == SND_COMPR_TRIGGER_DRAIN) { |
| 357 | |
| 358 | if (platform->driver->compr_ops && |
| 359 | platform->driver->compr_ops->trigger) |
| 360 | return platform->driver->compr_ops->trigger(cstream, cmd); |
| 361 | } |
| 362 | |
| 363 | if (cstream->direction == SND_COMPRESS_PLAYBACK) |
| 364 | stream = SNDRV_PCM_STREAM_PLAYBACK; |
| 365 | else |
| 366 | stream = SNDRV_PCM_STREAM_CAPTURE; |
| 367 | |
| 368 | |
| 369 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 370 | |
| 371 | if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) { |
| 372 | ret = platform->driver->compr_ops->trigger(cstream, cmd); |
| 373 | if (ret < 0) |
| 374 | goto out; |
| 375 | } |
| 376 | |
| 377 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 378 | |
| 379 | ret = dpcm_be_dai_trigger(fe, stream, cmd); |
| 380 | |
| 381 | switch (cmd) { |
| 382 | case SNDRV_PCM_TRIGGER_START: |
| 383 | case SNDRV_PCM_TRIGGER_RESUME: |
| 384 | case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: |
| 385 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; |
| 386 | break; |
| 387 | case SNDRV_PCM_TRIGGER_STOP: |
| 388 | case SNDRV_PCM_TRIGGER_SUSPEND: |
| 389 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; |
| 390 | break; |
| 391 | case SNDRV_PCM_TRIGGER_PAUSE_PUSH: |
| 392 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; |
| 393 | break; |
| 394 | } |
| 395 | |
| 396 | out: |
| 397 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 398 | mutex_unlock(&fe->card->mutex); |
| 399 | return ret; |
| 400 | } |
| 401 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 402 | static int soc_compr_set_params(struct snd_compr_stream *cstream, |
| 403 | struct snd_compr_params *params) |
| 404 | { |
| 405 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 406 | struct snd_soc_platform *platform = rtd->platform; |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 407 | int ret = 0; |
| 408 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 409 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 410 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 411 | /* first we call set_params for the platform driver |
| 412 | * this should configure the soc side |
| 413 | * if the machine has compressed ops then we call that as well |
| 414 | * expectation is that platform and machine will configure everything |
| 415 | * for this compress path, like configuring pcm port for codec |
| 416 | */ |
| 417 | if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) { |
| 418 | ret = platform->driver->compr_ops->set_params(cstream, params); |
| 419 | if (ret < 0) |
Charles Keepax | fa40ef2 | 2013-03-27 16:39:01 +0000 | [diff] [blame] | 420 | goto err; |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) { |
| 424 | ret = rtd->dai_link->compr_ops->set_params(cstream); |
| 425 | if (ret < 0) |
Charles Keepax | fa40ef2 | 2013-03-27 16:39:01 +0000 | [diff] [blame] | 426 | goto err; |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 427 | } |
| 428 | |
Charles Keepax | 2c071ed | 2013-05-20 08:33:54 +0100 | [diff] [blame] | 429 | if (cstream->direction == SND_COMPRESS_PLAYBACK) |
| 430 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK, |
| 431 | SND_SOC_DAPM_STREAM_START); |
| 432 | else |
| 433 | snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE, |
| 434 | SND_SOC_DAPM_STREAM_START); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 435 | |
Charles Keepax | fa40ef2 | 2013-03-27 16:39:01 +0000 | [diff] [blame] | 436 | /* cancel any delayed stream shutdown that is pending */ |
| 437 | rtd->pop_wait = 0; |
| 438 | mutex_unlock(&rtd->pcm_mutex); |
| 439 | |
| 440 | cancel_delayed_work_sync(&rtd->delayed_work); |
| 441 | |
| 442 | return ret; |
| 443 | |
| 444 | err: |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 445 | mutex_unlock(&rtd->pcm_mutex); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 446 | return ret; |
| 447 | } |
| 448 | |
Liam Girdwood | 2a99ef0 | 2014-01-17 17:03:56 +0000 | [diff] [blame] | 449 | static int soc_compr_set_params_fe(struct snd_compr_stream *cstream, |
| 450 | struct snd_compr_params *params) |
| 451 | { |
| 452 | struct snd_soc_pcm_runtime *fe = cstream->private_data; |
| 453 | struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream; |
| 454 | struct snd_soc_platform *platform = fe->platform; |
| 455 | int ret = 0, stream; |
| 456 | |
| 457 | if (cstream->direction == SND_COMPRESS_PLAYBACK) |
| 458 | stream = SNDRV_PCM_STREAM_PLAYBACK; |
| 459 | else |
| 460 | stream = SNDRV_PCM_STREAM_CAPTURE; |
| 461 | |
| 462 | mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); |
| 463 | |
| 464 | if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) { |
| 465 | ret = platform->driver->compr_ops->set_params(cstream, params); |
| 466 | if (ret < 0) |
| 467 | goto out; |
| 468 | } |
| 469 | |
| 470 | if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) { |
| 471 | ret = fe->dai_link->compr_ops->set_params(cstream); |
| 472 | if (ret < 0) |
| 473 | goto out; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Create an empty hw_params for the BE as the machine driver must |
| 478 | * fix this up to match DSP decoder and ASRC configuration. |
| 479 | * I.e. machine driver fixup for compressed BE is mandatory. |
| 480 | */ |
| 481 | memset(&fe->dpcm[fe_substream->stream].hw_params, 0, |
| 482 | sizeof(struct snd_pcm_hw_params)); |
| 483 | |
| 484 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; |
| 485 | |
| 486 | ret = dpcm_be_dai_hw_params(fe, stream); |
| 487 | if (ret < 0) |
| 488 | goto out; |
| 489 | |
| 490 | ret = dpcm_be_dai_prepare(fe, stream); |
| 491 | if (ret < 0) |
| 492 | goto out; |
| 493 | |
| 494 | if (stream == SNDRV_PCM_STREAM_PLAYBACK) |
| 495 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); |
| 496 | else |
| 497 | dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); |
| 498 | |
| 499 | fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; |
| 500 | |
| 501 | out: |
| 502 | fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; |
| 503 | mutex_unlock(&fe->card->mutex); |
| 504 | return ret; |
| 505 | } |
| 506 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 507 | static int soc_compr_get_params(struct snd_compr_stream *cstream, |
| 508 | struct snd_codec *params) |
| 509 | { |
| 510 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 511 | struct snd_soc_platform *platform = rtd->platform; |
| 512 | int ret = 0; |
| 513 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 514 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 515 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 516 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_params) |
| 517 | ret = platform->driver->compr_ops->get_params(cstream, params); |
| 518 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 519 | mutex_unlock(&rtd->pcm_mutex); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | static int soc_compr_get_caps(struct snd_compr_stream *cstream, |
| 524 | struct snd_compr_caps *caps) |
| 525 | { |
| 526 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 527 | struct snd_soc_platform *platform = rtd->platform; |
| 528 | int ret = 0; |
| 529 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 530 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 531 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 532 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps) |
| 533 | ret = platform->driver->compr_ops->get_caps(cstream, caps); |
| 534 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 535 | mutex_unlock(&rtd->pcm_mutex); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 536 | return ret; |
| 537 | } |
| 538 | |
| 539 | static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream, |
| 540 | struct snd_compr_codec_caps *codec) |
| 541 | { |
| 542 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 543 | struct snd_soc_platform *platform = rtd->platform; |
| 544 | int ret = 0; |
| 545 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 546 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 547 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 548 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps) |
| 549 | ret = platform->driver->compr_ops->get_codec_caps(cstream, codec); |
| 550 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 551 | mutex_unlock(&rtd->pcm_mutex); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes) |
| 556 | { |
| 557 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 558 | struct snd_soc_platform *platform = rtd->platform; |
| 559 | int ret = 0; |
| 560 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 561 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 562 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 563 | if (platform->driver->compr_ops && platform->driver->compr_ops->ack) |
| 564 | ret = platform->driver->compr_ops->ack(cstream, bytes); |
| 565 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 566 | mutex_unlock(&rtd->pcm_mutex); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 567 | return ret; |
| 568 | } |
| 569 | |
| 570 | static int soc_compr_pointer(struct snd_compr_stream *cstream, |
| 571 | struct snd_compr_tstamp *tstamp) |
| 572 | { |
| 573 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 574 | struct snd_soc_platform *platform = rtd->platform; |
| 575 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 576 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 577 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 578 | if (platform->driver->compr_ops && platform->driver->compr_ops->pointer) |
| 579 | platform->driver->compr_ops->pointer(cstream, tstamp); |
| 580 | |
Charles Keepax | 15e2e61 | 2013-01-24 09:44:29 +0000 | [diff] [blame] | 581 | mutex_unlock(&rtd->pcm_mutex); |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 582 | return 0; |
| 583 | } |
| 584 | |
Charles Keepax | 1f88eb0 | 2013-02-05 10:41:47 +0000 | [diff] [blame] | 585 | static int soc_compr_copy(struct snd_compr_stream *cstream, |
Charles Keepax | 4daf891 | 2013-04-18 11:01:38 +0100 | [diff] [blame] | 586 | char __user *buf, size_t count) |
Charles Keepax | 1f88eb0 | 2013-02-05 10:41:47 +0000 | [diff] [blame] | 587 | { |
| 588 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 589 | struct snd_soc_platform *platform = rtd->platform; |
| 590 | int ret = 0; |
| 591 | |
| 592 | mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass); |
| 593 | |
| 594 | if (platform->driver->compr_ops && platform->driver->compr_ops->copy) |
| 595 | ret = platform->driver->compr_ops->copy(cstream, buf, count); |
| 596 | |
| 597 | mutex_unlock(&rtd->pcm_mutex); |
| 598 | return ret; |
| 599 | } |
| 600 | |
Vinod Koul | 02bd90e | 2013-07-28 20:06:15 +0530 | [diff] [blame] | 601 | static int soc_compr_set_metadata(struct snd_compr_stream *cstream, |
Jeeja KP | 36953d9 | 2013-03-26 21:22:28 +0530 | [diff] [blame] | 602 | struct snd_compr_metadata *metadata) |
| 603 | { |
| 604 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 605 | struct snd_soc_platform *platform = rtd->platform; |
| 606 | int ret = 0; |
| 607 | |
| 608 | if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata) |
| 609 | ret = platform->driver->compr_ops->set_metadata(cstream, metadata); |
| 610 | |
| 611 | return ret; |
| 612 | } |
| 613 | |
Vinod Koul | 02bd90e | 2013-07-28 20:06:15 +0530 | [diff] [blame] | 614 | static int soc_compr_get_metadata(struct snd_compr_stream *cstream, |
Jeeja KP | 36953d9 | 2013-03-26 21:22:28 +0530 | [diff] [blame] | 615 | struct snd_compr_metadata *metadata) |
| 616 | { |
| 617 | struct snd_soc_pcm_runtime *rtd = cstream->private_data; |
| 618 | struct snd_soc_platform *platform = rtd->platform; |
| 619 | int ret = 0; |
| 620 | |
| 621 | if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata) |
| 622 | ret = platform->driver->compr_ops->get_metadata(cstream, metadata); |
| 623 | |
| 624 | return ret; |
| 625 | } |
Liam Girdwood | 2a99ef0 | 2014-01-17 17:03:56 +0000 | [diff] [blame] | 626 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 627 | /* ASoC Compress operations */ |
| 628 | static struct snd_compr_ops soc_compr_ops = { |
| 629 | .open = soc_compr_open, |
| 630 | .free = soc_compr_free, |
| 631 | .set_params = soc_compr_set_params, |
Vinod Koul | 02bd90e | 2013-07-28 20:06:15 +0530 | [diff] [blame] | 632 | .set_metadata = soc_compr_set_metadata, |
| 633 | .get_metadata = soc_compr_get_metadata, |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 634 | .get_params = soc_compr_get_params, |
| 635 | .trigger = soc_compr_trigger, |
| 636 | .pointer = soc_compr_pointer, |
| 637 | .ack = soc_compr_ack, |
| 638 | .get_caps = soc_compr_get_caps, |
| 639 | .get_codec_caps = soc_compr_get_codec_caps |
| 640 | }; |
| 641 | |
Liam Girdwood | 2a99ef0 | 2014-01-17 17:03:56 +0000 | [diff] [blame] | 642 | /* ASoC Dynamic Compress operations */ |
| 643 | static struct snd_compr_ops soc_compr_dyn_ops = { |
| 644 | .open = soc_compr_open_fe, |
| 645 | .free = soc_compr_free_fe, |
| 646 | .set_params = soc_compr_set_params_fe, |
| 647 | .get_params = soc_compr_get_params, |
| 648 | .set_metadata = soc_compr_set_metadata, |
| 649 | .get_metadata = soc_compr_get_metadata, |
| 650 | .trigger = soc_compr_trigger_fe, |
| 651 | .pointer = soc_compr_pointer, |
| 652 | .ack = soc_compr_ack, |
| 653 | .get_caps = soc_compr_get_caps, |
| 654 | .get_codec_caps = soc_compr_get_codec_caps |
| 655 | }; |
| 656 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 657 | /* create a new compress */ |
| 658 | int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num) |
| 659 | { |
| 660 | struct snd_soc_codec *codec = rtd->codec; |
Charles Keepax | 1f88eb0 | 2013-02-05 10:41:47 +0000 | [diff] [blame] | 661 | struct snd_soc_platform *platform = rtd->platform; |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 662 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 663 | struct snd_soc_dai *cpu_dai = rtd->cpu_dai; |
| 664 | struct snd_compr *compr; |
Liam Girdwood | 2a99ef0 | 2014-01-17 17:03:56 +0000 | [diff] [blame] | 665 | struct snd_pcm *be_pcm; |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 666 | char new_name[64]; |
| 667 | int ret = 0, direction = 0; |
| 668 | |
| 669 | /* check client and interface hw capabilities */ |
| 670 | snprintf(new_name, sizeof(new_name), "%s %s-%d", |
| 671 | rtd->dai_link->stream_name, codec_dai->name, num); |
Charles Keepax | daa2db5 | 2013-04-18 11:02:38 +0100 | [diff] [blame] | 672 | |
| 673 | if (codec_dai->driver->playback.channels_min) |
| 674 | direction = SND_COMPRESS_PLAYBACK; |
| 675 | else if (codec_dai->driver->capture.channels_min) |
| 676 | direction = SND_COMPRESS_CAPTURE; |
| 677 | else |
| 678 | return -EINVAL; |
| 679 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 680 | compr = kzalloc(sizeof(*compr), GFP_KERNEL); |
| 681 | if (compr == NULL) { |
| 682 | snd_printk(KERN_ERR "Cannot allocate compr\n"); |
| 683 | return -ENOMEM; |
| 684 | } |
| 685 | |
Charles Keepax | 1f88eb0 | 2013-02-05 10:41:47 +0000 | [diff] [blame] | 686 | compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops), |
| 687 | GFP_KERNEL); |
| 688 | if (compr->ops == NULL) { |
| 689 | dev_err(rtd->card->dev, "Cannot allocate compressed ops\n"); |
| 690 | ret = -ENOMEM; |
| 691 | goto compr_err; |
| 692 | } |
Liam Girdwood | 2a99ef0 | 2014-01-17 17:03:56 +0000 | [diff] [blame] | 693 | |
| 694 | if (rtd->dai_link->dynamic) { |
| 695 | snprintf(new_name, sizeof(new_name), "(%s)", |
| 696 | rtd->dai_link->stream_name); |
| 697 | |
| 698 | ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, |
| 699 | 1, 0, &be_pcm); |
| 700 | if (ret < 0) { |
| 701 | dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n", |
| 702 | rtd->dai_link->name); |
| 703 | goto compr_err; |
| 704 | } |
| 705 | |
| 706 | rtd->pcm = be_pcm; |
| 707 | rtd->fe_compr = 1; |
| 708 | be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; |
| 709 | be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; |
| 710 | memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops)); |
| 711 | } else |
| 712 | memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops)); |
Charles Keepax | 1f88eb0 | 2013-02-05 10:41:47 +0000 | [diff] [blame] | 713 | |
| 714 | /* Add copy callback for not memory mapped DSPs */ |
| 715 | if (platform->driver->compr_ops && platform->driver->compr_ops->copy) |
| 716 | compr->ops->copy = soc_compr_copy; |
| 717 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 718 | mutex_init(&compr->lock); |
| 719 | ret = snd_compress_new(rtd->card->snd_card, num, direction, compr); |
| 720 | if (ret < 0) { |
| 721 | pr_err("compress asoc: can't create compress for codec %s\n", |
| 722 | codec->name); |
Charles Keepax | 1f88eb0 | 2013-02-05 10:41:47 +0000 | [diff] [blame] | 723 | goto compr_err; |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 724 | } |
| 725 | |
Charles Keepax | 202c8f7 | 2013-01-24 09:44:30 +0000 | [diff] [blame] | 726 | /* DAPM dai link stream work */ |
| 727 | INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work); |
| 728 | |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 729 | rtd->compr = compr; |
| 730 | compr->private_data = rtd; |
| 731 | |
| 732 | printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name, |
| 733 | cpu_dai->name); |
| 734 | return ret; |
Charles Keepax | 1f88eb0 | 2013-02-05 10:41:47 +0000 | [diff] [blame] | 735 | |
| 736 | compr_err: |
| 737 | kfree(compr); |
| 738 | return ret; |
Namarta Kohli | 1245b70 | 2012-08-16 17:10:41 +0530 | [diff] [blame] | 739 | } |