1b3ed4c86SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0+ 2b3ed4c86SKuninori Morimoto // 3b3ed4c86SKuninori Morimoto // soc-compress.c -- ALSA SoC Compress 4b3ed4c86SKuninori Morimoto // 5b3ed4c86SKuninori Morimoto // Copyright (C) 2012 Intel Corp. 6b3ed4c86SKuninori Morimoto // 7b3ed4c86SKuninori Morimoto // Authors: Namarta Kohli <namartax.kohli@intel.com> 8b3ed4c86SKuninori Morimoto // Ramesh Babu K V <ramesh.babu@linux.intel.com> 9b3ed4c86SKuninori Morimoto // Vinod Koul <vinod.koul@linux.intel.com> 101245b700SNamarta Kohli 111245b700SNamarta Kohli #include <linux/kernel.h> 121245b700SNamarta Kohli #include <linux/init.h> 131245b700SNamarta Kohli #include <linux/delay.h> 141245b700SNamarta Kohli #include <linux/slab.h> 151245b700SNamarta Kohli #include <linux/workqueue.h> 161245b700SNamarta Kohli #include <sound/core.h> 171245b700SNamarta Kohli #include <sound/compress_params.h> 181245b700SNamarta Kohli #include <sound/compress_driver.h> 191245b700SNamarta Kohli #include <sound/soc.h> 201245b700SNamarta Kohli #include <sound/initval.h> 212a99ef0fSLiam Girdwood #include <sound/soc-dpcm.h> 229ab711cbSKuninori Morimoto #include <sound/soc-link.h> 234137f4b6SCezary Rojewski #include <linux/pm_runtime.h> 241245b700SNamarta Kohli 25453d32c2SKuninori Morimoto static int soc_compr_clean(struct snd_compr_stream *cstream, int rollback) 2615a7b8c1SKuninori Morimoto { 2715a7b8c1SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = cstream->private_data; 2815a7b8c1SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 2915a7b8c1SKuninori Morimoto struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 3015a7b8c1SKuninori Morimoto int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */ 3115a7b8c1SKuninori Morimoto 3215a7b8c1SKuninori Morimoto mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 3315a7b8c1SKuninori Morimoto 34453d32c2SKuninori Morimoto if (!rollback) 3515a7b8c1SKuninori Morimoto snd_soc_runtime_deactivate(rtd, stream); 3615a7b8c1SKuninori Morimoto 3715a7b8c1SKuninori Morimoto snd_soc_dai_digital_mute(codec_dai, 1, stream); 3815a7b8c1SKuninori Morimoto 3915a7b8c1SKuninori Morimoto if (!snd_soc_dai_active(cpu_dai)) 4015a7b8c1SKuninori Morimoto cpu_dai->rate = 0; 4115a7b8c1SKuninori Morimoto 4215a7b8c1SKuninori Morimoto if (!snd_soc_dai_active(codec_dai)) 4315a7b8c1SKuninori Morimoto codec_dai->rate = 0; 4415a7b8c1SKuninori Morimoto 45453d32c2SKuninori Morimoto snd_soc_link_compr_shutdown(cstream, rollback); 4615a7b8c1SKuninori Morimoto 47453d32c2SKuninori Morimoto snd_soc_component_compr_free(cstream, rollback); 4815a7b8c1SKuninori Morimoto 49453d32c2SKuninori Morimoto snd_soc_dai_compr_shutdown(cpu_dai, cstream, rollback); 5015a7b8c1SKuninori Morimoto 51453d32c2SKuninori Morimoto if (!rollback) 5215a7b8c1SKuninori Morimoto snd_soc_dapm_stream_stop(rtd, stream); 5315a7b8c1SKuninori Morimoto 5415a7b8c1SKuninori Morimoto mutex_unlock(&rtd->card->pcm_mutex); 5515a7b8c1SKuninori Morimoto 56453d32c2SKuninori Morimoto snd_soc_pcm_component_pm_runtime_put(rtd, cstream, rollback); 5715a7b8c1SKuninori Morimoto 5815a7b8c1SKuninori Morimoto return 0; 5915a7b8c1SKuninori Morimoto } 6015a7b8c1SKuninori Morimoto 61453d32c2SKuninori Morimoto static int soc_compr_free(struct snd_compr_stream *cstream) 62453d32c2SKuninori Morimoto { 63453d32c2SKuninori Morimoto return soc_compr_clean(cstream, 0); 64453d32c2SKuninori Morimoto } 65453d32c2SKuninori Morimoto 661e57b828SCharles Keepax static int soc_compr_open(struct snd_compr_stream *cstream) 671e57b828SCharles Keepax { 681e57b828SCharles Keepax struct snd_soc_pcm_runtime *rtd = cstream->private_data; 69c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 707428d8c8SKuninori Morimoto int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */ 71939a5cfbSKuninori Morimoto int ret; 721245b700SNamarta Kohli 73939a5cfbSKuninori Morimoto ret = snd_soc_pcm_component_pm_runtime_get(rtd, cstream); 74939a5cfbSKuninori Morimoto if (ret < 0) 75453d32c2SKuninori Morimoto goto err_no_lock; 764137f4b6SCezary Rojewski 7772b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 7815e2e619SCharles Keepax 79b5ae4cceSKuninori Morimoto ret = snd_soc_dai_compr_startup(cpu_dai, cstream); 80b5ae4cceSKuninori Morimoto if (ret < 0) 81453d32c2SKuninori Morimoto goto err; 822e622ae4SVinod Koul 83f94ba9acSKuninori Morimoto ret = snd_soc_component_compr_open(cstream); 841e57b828SCharles Keepax if (ret < 0) 85453d32c2SKuninori Morimoto goto err; 869e7e3738SKuninori Morimoto 879ab711cbSKuninori Morimoto ret = snd_soc_link_compr_startup(cstream); 889ab711cbSKuninori Morimoto if (ret < 0) 89453d32c2SKuninori Morimoto goto err; 901245b700SNamarta Kohli 91eb84959aSKuninori Morimoto snd_soc_runtime_activate(rtd, stream); 92453d32c2SKuninori Morimoto err: 9372b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 94453d32c2SKuninori Morimoto err_no_lock: 95453d32c2SKuninori Morimoto if (ret < 0) 96453d32c2SKuninori Morimoto soc_compr_clean(cstream, 1); 974137f4b6SCezary Rojewski 981245b700SNamarta Kohli return ret; 991245b700SNamarta Kohli } 1001245b700SNamarta Kohli 1012a99ef0fSLiam Girdwood static int soc_compr_open_fe(struct snd_compr_stream *cstream) 1022a99ef0fSLiam Girdwood { 1032a99ef0fSLiam Girdwood struct snd_soc_pcm_runtime *fe = cstream->private_data; 10401b8cedfSSatish Babu Patakokila struct snd_pcm_substream *fe_substream = 10501b8cedfSSatish Babu Patakokila fe->pcm->streams[cstream->direction].substream; 106c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0); 1072a99ef0fSLiam Girdwood struct snd_soc_dpcm *dpcm; 1082a99ef0fSLiam Girdwood struct snd_soc_dapm_widget_list *list; 1097428d8c8SKuninori Morimoto int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */ 110572e6c8dSCharles Keepax int ret; 1112a99ef0fSLiam Girdwood 1122a99ef0fSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 1130b0722e1SSrinivas Kandagatla fe->dpcm[stream].runtime = fe_substream->runtime; 1140b0722e1SSrinivas Kandagatla 1150b0722e1SSrinivas Kandagatla ret = dpcm_path_get(fe, stream, &list); 1160b0722e1SSrinivas Kandagatla if (ret < 0) 1170b0722e1SSrinivas Kandagatla goto be_err; 118d479f00bSKuninori Morimoto 1190b0722e1SSrinivas Kandagatla /* calculate valid and active FE <-> BE dpcms */ 1200b0722e1SSrinivas Kandagatla dpcm_process_paths(fe, stream, &list, 1); 1210b0722e1SSrinivas Kandagatla fe->dpcm[stream].runtime = fe_substream->runtime; 1220b0722e1SSrinivas Kandagatla 1230b0722e1SSrinivas Kandagatla fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 1240b0722e1SSrinivas Kandagatla 1250b0722e1SSrinivas Kandagatla ret = dpcm_be_dai_startup(fe, stream); 1260b0722e1SSrinivas Kandagatla if (ret < 0) { 1270b0722e1SSrinivas Kandagatla /* clean up all links */ 1288d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 1290b0722e1SSrinivas Kandagatla dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 1300b0722e1SSrinivas Kandagatla 1310b0722e1SSrinivas Kandagatla dpcm_be_disconnect(fe, stream); 1320b0722e1SSrinivas Kandagatla fe->dpcm[stream].runtime = NULL; 1330b0722e1SSrinivas Kandagatla goto out; 1340b0722e1SSrinivas Kandagatla } 1352a99ef0fSLiam Girdwood 136b5ae4cceSKuninori Morimoto ret = snd_soc_dai_compr_startup(cpu_dai, cstream); 137b5ae4cceSKuninori Morimoto if (ret < 0) 1382e622ae4SVinod Koul goto out; 1392e622ae4SVinod Koul 140f94ba9acSKuninori Morimoto ret = snd_soc_component_compr_open(cstream); 1411e57b828SCharles Keepax if (ret < 0) 1420b0722e1SSrinivas Kandagatla goto open_err; 1439e7e3738SKuninori Morimoto 1449ab711cbSKuninori Morimoto ret = snd_soc_link_compr_startup(cstream); 1459ab711cbSKuninori Morimoto if (ret < 0) 1462a99ef0fSLiam Girdwood goto machine_err; 1472a99ef0fSLiam Girdwood 1482a99ef0fSLiam Girdwood dpcm_clear_pending_state(fe, stream); 1492a99ef0fSLiam Girdwood dpcm_path_put(&list); 1502a99ef0fSLiam Girdwood 1512a99ef0fSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN; 1522a99ef0fSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1532a99ef0fSLiam Girdwood 15424894b76SLars-Peter Clausen snd_soc_runtime_activate(fe, stream); 1552a99ef0fSLiam Girdwood 1562a99ef0fSLiam Girdwood mutex_unlock(&fe->card->mutex); 1572a99ef0fSLiam Girdwood 1582a99ef0fSLiam Girdwood return 0; 1592a99ef0fSLiam Girdwood 1602a99ef0fSLiam Girdwood machine_err: 161f94ba9acSKuninori Morimoto snd_soc_component_compr_free(cstream, 1); 1620b0722e1SSrinivas Kandagatla open_err: 1631e6a93cfSKuninori Morimoto snd_soc_dai_compr_shutdown(cpu_dai, cstream, 1); 1642a99ef0fSLiam Girdwood out: 1650b0722e1SSrinivas Kandagatla dpcm_path_put(&list); 1660b0722e1SSrinivas Kandagatla be_err: 1672a99ef0fSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1682a99ef0fSLiam Girdwood mutex_unlock(&fe->card->mutex); 1692a99ef0fSLiam Girdwood return ret; 1702a99ef0fSLiam Girdwood } 1712a99ef0fSLiam Girdwood 1722a99ef0fSLiam Girdwood static int soc_compr_free_fe(struct snd_compr_stream *cstream) 1732a99ef0fSLiam Girdwood { 1742a99ef0fSLiam Girdwood struct snd_soc_pcm_runtime *fe = cstream->private_data; 175c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0); 1762a99ef0fSLiam Girdwood struct snd_soc_dpcm *dpcm; 1777428d8c8SKuninori Morimoto int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */ 1782a99ef0fSLiam Girdwood 1792a99ef0fSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 1802a99ef0fSLiam Girdwood 18124894b76SLars-Peter Clausen snd_soc_runtime_deactivate(fe, stream); 1822a99ef0fSLiam Girdwood 1832a99ef0fSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 1842a99ef0fSLiam Girdwood 185*f52366e6SKuninori Morimoto dpcm_be_dai_hw_free(fe, stream); 1862a99ef0fSLiam Girdwood 187531590bbSKuninori Morimoto dpcm_be_dai_shutdown(fe, stream); 1882a99ef0fSLiam Girdwood 1892a99ef0fSLiam Girdwood /* mark FE's links ready to prune */ 1908d6258a4SKuninori Morimoto for_each_dpcm_be(fe, stream, dpcm) 1912a99ef0fSLiam Girdwood dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE; 1922a99ef0fSLiam Girdwood 1931c531230SKuninori Morimoto dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP); 1942a99ef0fSLiam Girdwood 1952a99ef0fSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE; 1962a99ef0fSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 1972a99ef0fSLiam Girdwood 1982a99ef0fSLiam Girdwood dpcm_be_disconnect(fe, stream); 1992a99ef0fSLiam Girdwood 2002a99ef0fSLiam Girdwood fe->dpcm[stream].runtime = NULL; 2012a99ef0fSLiam Girdwood 202cd7c7d10SKuninori Morimoto snd_soc_link_compr_shutdown(cstream, 0); 2032a99ef0fSLiam Girdwood 204f94ba9acSKuninori Morimoto snd_soc_component_compr_free(cstream, 0); 2059e7e3738SKuninori Morimoto 2061e6a93cfSKuninori Morimoto snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0); 2072e622ae4SVinod Koul 2082a99ef0fSLiam Girdwood mutex_unlock(&fe->card->mutex); 2092a99ef0fSLiam Girdwood return 0; 2102a99ef0fSLiam Girdwood } 2112a99ef0fSLiam Girdwood 2124ef0ecb8SCharles Keepax static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd) 2134ef0ecb8SCharles Keepax { 2144ef0ecb8SCharles Keepax struct snd_soc_pcm_runtime *rtd = cstream->private_data; 215c2233a26SKuninori Morimoto struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 216c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 2177428d8c8SKuninori Morimoto int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */ 2184ef0ecb8SCharles Keepax int ret; 2194ef0ecb8SCharles Keepax 22072b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 2214ef0ecb8SCharles Keepax 22208aee251SKuninori Morimoto ret = snd_soc_component_compr_trigger(cstream, cmd); 2234ef0ecb8SCharles Keepax if (ret < 0) 2244ef0ecb8SCharles Keepax goto out; 2254ef0ecb8SCharles Keepax 226eb08411bSKuninori Morimoto ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd); 227eb08411bSKuninori Morimoto if (ret < 0) 228eb08411bSKuninori Morimoto goto out; 2292e622ae4SVinod Koul 230e38b9b74SMark Brown switch (cmd) { 231e38b9b74SMark Brown case SNDRV_PCM_TRIGGER_START: 232eb84959aSKuninori Morimoto snd_soc_dai_digital_mute(codec_dai, 0, stream); 233e38b9b74SMark Brown break; 234e38b9b74SMark Brown case SNDRV_PCM_TRIGGER_STOP: 235eb84959aSKuninori Morimoto snd_soc_dai_digital_mute(codec_dai, 1, stream); 236e38b9b74SMark Brown break; 237e38b9b74SMark Brown } 2381245b700SNamarta Kohli 23915e2e619SCharles Keepax out: 24072b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 2411245b700SNamarta Kohli return ret; 2421245b700SNamarta Kohli } 2431245b700SNamarta Kohli 2442a99ef0fSLiam Girdwood static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd) 2452a99ef0fSLiam Girdwood { 2462a99ef0fSLiam Girdwood struct snd_soc_pcm_runtime *fe = cstream->private_data; 247c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0); 2487428d8c8SKuninori Morimoto int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */ 2497428d8c8SKuninori Morimoto int ret; 2502a99ef0fSLiam Girdwood 2512a99ef0fSLiam Girdwood if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN || 2524ef0ecb8SCharles Keepax cmd == SND_COMPR_TRIGGER_DRAIN) 25308aee251SKuninori Morimoto return snd_soc_component_compr_trigger(cstream, cmd); 2542a99ef0fSLiam Girdwood 2552a99ef0fSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 2562a99ef0fSLiam Girdwood 257eb08411bSKuninori Morimoto ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd); 2582e622ae4SVinod Koul if (ret < 0) 2592e622ae4SVinod Koul goto out; 2602e622ae4SVinod Koul 26108aee251SKuninori Morimoto ret = snd_soc_component_compr_trigger(cstream, cmd); 2629e7e3738SKuninori Morimoto if (ret < 0) 2639e7e3738SKuninori Morimoto goto out; 2649e7e3738SKuninori Morimoto 2652a99ef0fSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 2662a99ef0fSLiam Girdwood 2672a99ef0fSLiam Girdwood ret = dpcm_be_dai_trigger(fe, stream, cmd); 2682a99ef0fSLiam Girdwood 2692a99ef0fSLiam Girdwood switch (cmd) { 2702a99ef0fSLiam Girdwood case SNDRV_PCM_TRIGGER_START: 2712a99ef0fSLiam Girdwood case SNDRV_PCM_TRIGGER_RESUME: 2722a99ef0fSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2732a99ef0fSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START; 2742a99ef0fSLiam Girdwood break; 2752a99ef0fSLiam Girdwood case SNDRV_PCM_TRIGGER_STOP: 2762a99ef0fSLiam Girdwood case SNDRV_PCM_TRIGGER_SUSPEND: 2772a99ef0fSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP; 2782a99ef0fSLiam Girdwood break; 2792a99ef0fSLiam Girdwood case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2802a99ef0fSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED; 2812a99ef0fSLiam Girdwood break; 2822a99ef0fSLiam Girdwood } 2832a99ef0fSLiam Girdwood 2842a99ef0fSLiam Girdwood out: 2852a99ef0fSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 2862a99ef0fSLiam Girdwood mutex_unlock(&fe->card->mutex); 2872a99ef0fSLiam Girdwood return ret; 2882a99ef0fSLiam Girdwood } 2892a99ef0fSLiam Girdwood 2904ef0ecb8SCharles Keepax static int soc_compr_set_params(struct snd_compr_stream *cstream, 2914ef0ecb8SCharles Keepax struct snd_compr_params *params) 2924ef0ecb8SCharles Keepax { 2934ef0ecb8SCharles Keepax struct snd_soc_pcm_runtime *rtd = cstream->private_data; 294c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 2957428d8c8SKuninori Morimoto int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */ 29652cadf1fSCharles Keepax int ret; 2971245b700SNamarta Kohli 29872b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 29915e2e619SCharles Keepax 300ef050becSCharles Keepax /* 301ef050becSCharles Keepax * First we call set_params for the CPU DAI, then the component 302ef050becSCharles Keepax * driver this should configure the SoC side. If the machine has 303ef050becSCharles Keepax * compressed ops then we call that as well. The expectation is 304ef050becSCharles Keepax * that these callbacks will configure everything for this compress 305ef050becSCharles Keepax * path, like configuring a PCM port for a CODEC. 3061245b700SNamarta Kohli */ 3078dfedafbSKuninori Morimoto ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params); 3082e622ae4SVinod Koul if (ret < 0) 3092e622ae4SVinod Koul goto err; 3102e622ae4SVinod Koul 311ff08cf80SKuninori Morimoto ret = snd_soc_component_compr_set_params(cstream, params); 3129e7e3738SKuninori Morimoto if (ret < 0) 3139e7e3738SKuninori Morimoto goto err; 3149e7e3738SKuninori Morimoto 315eab810f3SKuninori Morimoto ret = snd_soc_link_compr_set_params(cstream); 3161245b700SNamarta Kohli if (ret < 0) 317fa40ef20SCharles Keepax goto err; 3181245b700SNamarta Kohli 3197428d8c8SKuninori Morimoto snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START); 3201245b700SNamarta Kohli 321fa40ef20SCharles Keepax /* cancel any delayed stream shutdown that is pending */ 322fa40ef20SCharles Keepax rtd->pop_wait = 0; 32372b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 324fa40ef20SCharles Keepax 325fa40ef20SCharles Keepax cancel_delayed_work_sync(&rtd->delayed_work); 326fa40ef20SCharles Keepax 32752cadf1fSCharles Keepax return 0; 328fa40ef20SCharles Keepax 329fa40ef20SCharles Keepax err: 33072b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 3311245b700SNamarta Kohli return ret; 3321245b700SNamarta Kohli } 3331245b700SNamarta Kohli 3342a99ef0fSLiam Girdwood static int soc_compr_set_params_fe(struct snd_compr_stream *cstream, 3352a99ef0fSLiam Girdwood struct snd_compr_params *params) 3362a99ef0fSLiam Girdwood { 3372a99ef0fSLiam Girdwood struct snd_soc_pcm_runtime *fe = cstream->private_data; 33801b8cedfSSatish Babu Patakokila struct snd_pcm_substream *fe_substream = 33901b8cedfSSatish Babu Patakokila fe->pcm->streams[cstream->direction].substream; 340c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(fe, 0); 3417428d8c8SKuninori Morimoto int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */ 3427428d8c8SKuninori Morimoto int ret; 3432a99ef0fSLiam Girdwood 3442a99ef0fSLiam Girdwood mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME); 3452a99ef0fSLiam Girdwood 3460b0722e1SSrinivas Kandagatla /* 3470b0722e1SSrinivas Kandagatla * Create an empty hw_params for the BE as the machine driver must 3480b0722e1SSrinivas Kandagatla * fix this up to match DSP decoder and ASRC configuration. 3490b0722e1SSrinivas Kandagatla * I.e. machine driver fixup for compressed BE is mandatory. 3500b0722e1SSrinivas Kandagatla */ 3510b0722e1SSrinivas Kandagatla memset(&fe->dpcm[fe_substream->stream].hw_params, 0, 3520b0722e1SSrinivas Kandagatla sizeof(struct snd_pcm_hw_params)); 3530b0722e1SSrinivas Kandagatla 3540b0722e1SSrinivas Kandagatla fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE; 3550b0722e1SSrinivas Kandagatla 3560b0722e1SSrinivas Kandagatla ret = dpcm_be_dai_hw_params(fe, stream); 3570b0722e1SSrinivas Kandagatla if (ret < 0) 3580b0722e1SSrinivas Kandagatla goto out; 3590b0722e1SSrinivas Kandagatla 3600b0722e1SSrinivas Kandagatla ret = dpcm_be_dai_prepare(fe, stream); 3610b0722e1SSrinivas Kandagatla if (ret < 0) 3620b0722e1SSrinivas Kandagatla goto out; 3630b0722e1SSrinivas Kandagatla 3648dfedafbSKuninori Morimoto ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params); 3652e622ae4SVinod Koul if (ret < 0) 3662e622ae4SVinod Koul goto out; 3672e622ae4SVinod Koul 368ff08cf80SKuninori Morimoto ret = snd_soc_component_compr_set_params(cstream, params); 3699e7e3738SKuninori Morimoto if (ret < 0) 3709e7e3738SKuninori Morimoto goto out; 3719e7e3738SKuninori Morimoto 372eab810f3SKuninori Morimoto ret = snd_soc_link_compr_set_params(cstream); 3732a99ef0fSLiam Girdwood if (ret < 0) 3742a99ef0fSLiam Girdwood goto out; 3752a99ef0fSLiam Girdwood 3762a99ef0fSLiam Girdwood dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START); 3772a99ef0fSLiam Girdwood fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE; 3782a99ef0fSLiam Girdwood 3792a99ef0fSLiam Girdwood out: 3802a99ef0fSLiam Girdwood fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO; 3812a99ef0fSLiam Girdwood mutex_unlock(&fe->card->mutex); 3822a99ef0fSLiam Girdwood return ret; 3832a99ef0fSLiam Girdwood } 3842a99ef0fSLiam Girdwood 3851245b700SNamarta Kohli static int soc_compr_get_params(struct snd_compr_stream *cstream, 3861245b700SNamarta Kohli struct snd_codec *params) 3871245b700SNamarta Kohli { 3881245b700SNamarta Kohli struct snd_soc_pcm_runtime *rtd = cstream->private_data; 389c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 39077c221ecSKuninori Morimoto int ret = 0; 3911245b700SNamarta Kohli 39272b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 39315e2e619SCharles Keepax 394adbef543SKuninori Morimoto ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params); 3952e622ae4SVinod Koul if (ret < 0) 3962e622ae4SVinod Koul goto err; 3972e622ae4SVinod Koul 39877c221ecSKuninori Morimoto ret = snd_soc_component_compr_get_params(cstream, params); 3992e622ae4SVinod Koul err: 40072b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 4011245b700SNamarta Kohli return ret; 4021245b700SNamarta Kohli } 4031245b700SNamarta Kohli 4041245b700SNamarta Kohli static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes) 4051245b700SNamarta Kohli { 4061245b700SNamarta Kohli struct snd_soc_pcm_runtime *rtd = cstream->private_data; 407c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 4080506b885SKuninori Morimoto int ret; 4091245b700SNamarta Kohli 41072b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 41115e2e619SCharles Keepax 41253294353SKuninori Morimoto ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes); 4132e622ae4SVinod Koul if (ret < 0) 4142e622ae4SVinod Koul goto err; 4152e622ae4SVinod Koul 4160506b885SKuninori Morimoto ret = snd_soc_component_compr_ack(cstream, bytes); 4172e622ae4SVinod Koul err: 41872b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 4191245b700SNamarta Kohli return ret; 4201245b700SNamarta Kohli } 4211245b700SNamarta Kohli 4221245b700SNamarta Kohli static int soc_compr_pointer(struct snd_compr_stream *cstream, 4231245b700SNamarta Kohli struct snd_compr_tstamp *tstamp) 4241245b700SNamarta Kohli { 4251245b700SNamarta Kohli struct snd_soc_pcm_runtime *rtd = cstream->private_data; 42603ecea64SKuninori Morimoto int ret; 427c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 4281245b700SNamarta Kohli 42972b745e3SPeter Ujfalusi mutex_lock_nested(&rtd->card->pcm_mutex, rtd->card->pcm_subclass); 43015e2e619SCharles Keepax 431ed38cc59SKuninori Morimoto ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp); 432ed38cc59SKuninori Morimoto if (ret < 0) 433ed38cc59SKuninori Morimoto goto out; 4342e622ae4SVinod Koul 43503ecea64SKuninori Morimoto ret = snd_soc_component_compr_pointer(cstream, tstamp); 436ed38cc59SKuninori Morimoto out: 43772b745e3SPeter Ujfalusi mutex_unlock(&rtd->card->pcm_mutex); 4387c9190f7SCharles Keepax return ret; 4391245b700SNamarta Kohli } 4401245b700SNamarta Kohli 44102bd90e8SVinod Koul static int soc_compr_set_metadata(struct snd_compr_stream *cstream, 44236953d98SJeeja KP struct snd_compr_metadata *metadata) 44336953d98SJeeja KP { 44436953d98SJeeja KP struct snd_soc_pcm_runtime *rtd = cstream->private_data; 445c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 4461b308fb1SKuninori Morimoto int ret; 44736953d98SJeeja KP 44888b3a7dfSKuninori Morimoto ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata); 4492e622ae4SVinod Koul if (ret < 0) 4502e622ae4SVinod Koul return ret; 4512e622ae4SVinod Koul 4521b308fb1SKuninori Morimoto return snd_soc_component_compr_set_metadata(cstream, metadata); 45336953d98SJeeja KP } 45436953d98SJeeja KP 45502bd90e8SVinod Koul static int soc_compr_get_metadata(struct snd_compr_stream *cstream, 45636953d98SJeeja KP struct snd_compr_metadata *metadata) 45736953d98SJeeja KP { 45836953d98SJeeja KP struct snd_soc_pcm_runtime *rtd = cstream->private_data; 459c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 460bab78c23SKuninori Morimoto int ret; 46136953d98SJeeja KP 46294d72819SKuninori Morimoto ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata); 4632e622ae4SVinod Koul if (ret < 0) 4642e622ae4SVinod Koul return ret; 4652e622ae4SVinod Koul 466bab78c23SKuninori Morimoto return snd_soc_component_compr_get_metadata(cstream, metadata); 46736953d98SJeeja KP } 4682a99ef0fSLiam Girdwood 4691245b700SNamarta Kohli /* ASoC Compress operations */ 4701245b700SNamarta Kohli static struct snd_compr_ops soc_compr_ops = { 4711245b700SNamarta Kohli .open = soc_compr_open, 4721245b700SNamarta Kohli .free = soc_compr_free, 4731245b700SNamarta Kohli .set_params = soc_compr_set_params, 47402bd90e8SVinod Koul .set_metadata = soc_compr_set_metadata, 47502bd90e8SVinod Koul .get_metadata = soc_compr_get_metadata, 4761245b700SNamarta Kohli .get_params = soc_compr_get_params, 4771245b700SNamarta Kohli .trigger = soc_compr_trigger, 4781245b700SNamarta Kohli .pointer = soc_compr_pointer, 4791245b700SNamarta Kohli .ack = soc_compr_ack, 480d67fcb2dSKuninori Morimoto .get_caps = snd_soc_component_compr_get_caps, 4810f6fe097SKuninori Morimoto .get_codec_caps = snd_soc_component_compr_get_codec_caps, 4821245b700SNamarta Kohli }; 4831245b700SNamarta Kohli 4842a99ef0fSLiam Girdwood /* ASoC Dynamic Compress operations */ 4852a99ef0fSLiam Girdwood static struct snd_compr_ops soc_compr_dyn_ops = { 4862a99ef0fSLiam Girdwood .open = soc_compr_open_fe, 4872a99ef0fSLiam Girdwood .free = soc_compr_free_fe, 4882a99ef0fSLiam Girdwood .set_params = soc_compr_set_params_fe, 4892a99ef0fSLiam Girdwood .get_params = soc_compr_get_params, 4902a99ef0fSLiam Girdwood .set_metadata = soc_compr_set_metadata, 4912a99ef0fSLiam Girdwood .get_metadata = soc_compr_get_metadata, 4922a99ef0fSLiam Girdwood .trigger = soc_compr_trigger_fe, 4932a99ef0fSLiam Girdwood .pointer = soc_compr_pointer, 4942a99ef0fSLiam Girdwood .ack = soc_compr_ack, 495d67fcb2dSKuninori Morimoto .get_caps = snd_soc_component_compr_get_caps, 4960f6fe097SKuninori Morimoto .get_codec_caps = snd_soc_component_compr_get_codec_caps, 4972a99ef0fSLiam Girdwood }; 4982a99ef0fSLiam Girdwood 4996f0c4226SJie Yang /** 5006f0c4226SJie Yang * snd_soc_new_compress - create a new compress. 5016f0c4226SJie Yang * 5026f0c4226SJie Yang * @rtd: The runtime for which we will create compress 5036f0c4226SJie Yang * @num: the device index number (zero based - shared with normal PCMs) 5046f0c4226SJie Yang * 5056f0c4226SJie Yang * Return: 0 for success, else error. 5066f0c4226SJie Yang */ 5076f0c4226SJie Yang int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num) 5081245b700SNamarta Kohli { 5099e7e3738SKuninori Morimoto struct snd_soc_component *component; 510c2233a26SKuninori Morimoto struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 511c2233a26SKuninori Morimoto struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0); 5121245b700SNamarta Kohli struct snd_compr *compr; 5132a99ef0fSLiam Girdwood struct snd_pcm *be_pcm; 5141245b700SNamarta Kohli char new_name[64]; 5151245b700SNamarta Kohli int ret = 0, direction = 0; 516a1068045SVinod Koul int playback = 0, capture = 0; 517613fb500SKuninori Morimoto int i; 5181245b700SNamarta Kohli 5197428d8c8SKuninori Morimoto /* 5207428d8c8SKuninori Morimoto * make sure these are same value, 5217428d8c8SKuninori Morimoto * and then use these as equally 5227428d8c8SKuninori Morimoto */ 5237428d8c8SKuninori Morimoto BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK); 5247428d8c8SKuninori Morimoto BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE); 5257428d8c8SKuninori Morimoto 5266e1276a5SBard Liao if (rtd->num_cpus > 1 || 5276e1276a5SBard Liao rtd->num_codecs > 1) { 528141dfc9eSCharles Keepax dev_err(rtd->card->dev, 5296e1276a5SBard Liao "Compress ASoC: Multi CPU/Codec not supported\n"); 5308151d5e6SBenoit Cousson return -EINVAL; 5318151d5e6SBenoit Cousson } 5328151d5e6SBenoit Cousson 5331245b700SNamarta Kohli /* check client and interface hw capabilities */ 534467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) && 535467fece8SKuninori Morimoto snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK)) 536a1068045SVinod Koul playback = 1; 537467fece8SKuninori Morimoto if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) && 538467fece8SKuninori Morimoto snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE)) 539a1068045SVinod Koul capture = 1; 540a1068045SVinod Koul 541a1068045SVinod Koul /* 542a1068045SVinod Koul * Compress devices are unidirectional so only one of the directions 543a1068045SVinod Koul * should be set, check for that (xor) 544a1068045SVinod Koul */ 545a1068045SVinod Koul if (playback + capture != 1) { 546141dfc9eSCharles Keepax dev_err(rtd->card->dev, 547141dfc9eSCharles Keepax "Compress ASoC: Invalid direction for P %d, C %d\n", 548a1068045SVinod Koul playback, capture); 549daa2db59SCharles Keepax return -EINVAL; 550a1068045SVinod Koul } 551a1068045SVinod Koul 552a1068045SVinod Koul if (playback) 553a1068045SVinod Koul direction = SND_COMPRESS_PLAYBACK; 554a1068045SVinod Koul else 555a1068045SVinod Koul direction = SND_COMPRESS_CAPTURE; 556daa2db59SCharles Keepax 55709f448a4SAmadeusz Sławiński compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL); 5587a0cf42eSMarkus Elfring if (!compr) 5591245b700SNamarta Kohli return -ENOMEM; 5601245b700SNamarta Kohli 5611f88eb0fSCharles Keepax compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops), 5621f88eb0fSCharles Keepax GFP_KERNEL); 56309f448a4SAmadeusz Sławiński if (!compr->ops) 56409f448a4SAmadeusz Sławiński return -ENOMEM; 5652a99ef0fSLiam Girdwood 5662a99ef0fSLiam Girdwood if (rtd->dai_link->dynamic) { 5672a99ef0fSLiam Girdwood snprintf(new_name, sizeof(new_name), "(%s)", 5682a99ef0fSLiam Girdwood rtd->dai_link->stream_name); 5692a99ef0fSLiam Girdwood 5702a99ef0fSLiam Girdwood ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num, 571d3268a40SQais Yousef rtd->dai_link->dpcm_playback, 572d3268a40SQais Yousef rtd->dai_link->dpcm_capture, &be_pcm); 5732a99ef0fSLiam Girdwood if (ret < 0) { 574141dfc9eSCharles Keepax dev_err(rtd->card->dev, 575141dfc9eSCharles Keepax "Compress ASoC: can't create compressed for %s: %d\n", 576141dfc9eSCharles Keepax rtd->dai_link->name, ret); 57709f448a4SAmadeusz Sławiński return ret; 5782a99ef0fSLiam Girdwood } 5792a99ef0fSLiam Girdwood 5802a99ef0fSLiam Girdwood rtd->pcm = be_pcm; 5812a99ef0fSLiam Girdwood rtd->fe_compr = 1; 582d3268a40SQais Yousef if (rtd->dai_link->dpcm_playback) 5832a99ef0fSLiam Girdwood be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd; 584d3268a40SQais Yousef else if (rtd->dai_link->dpcm_capture) 5852a99ef0fSLiam Girdwood be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd; 5862a99ef0fSLiam Girdwood memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops)); 587aeb6fa0fSPeng Donglin } else { 588aeb6fa0fSPeng Donglin snprintf(new_name, sizeof(new_name), "%s %s-%d", 589aeb6fa0fSPeng Donglin rtd->dai_link->stream_name, codec_dai->name, num); 590aeb6fa0fSPeng Donglin 5911f88eb0fSCharles Keepax memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops)); 592aeb6fa0fSPeng Donglin } 5931f88eb0fSCharles Keepax 594613fb500SKuninori Morimoto for_each_rtd_components(rtd, i, component) { 595c6cb522cSKuninori Morimoto if (!component->driver->compress_ops || 596c6cb522cSKuninori Morimoto !component->driver->compress_ops->copy) 597c6cb522cSKuninori Morimoto continue; 598c6cb522cSKuninori Morimoto 599b5852e66SKuninori Morimoto compr->ops->copy = snd_soc_component_compr_copy; 600c6cb522cSKuninori Morimoto break; 601c6cb522cSKuninori Morimoto } 602c6cb522cSKuninori Morimoto 6031245b700SNamarta Kohli mutex_init(&compr->lock); 604e5241a8cSRichard Fitzgerald ret = snd_compress_new(rtd->card->snd_card, num, direction, 605e5241a8cSRichard Fitzgerald new_name, compr); 6061245b700SNamarta Kohli if (ret < 0) { 607c2233a26SKuninori Morimoto component = asoc_rtd_to_codec(rtd, 0)->component; 608141dfc9eSCharles Keepax dev_err(component->dev, 609141dfc9eSCharles Keepax "Compress ASoC: can't create compress for codec %s: %d\n", 610141dfc9eSCharles Keepax component->name, ret); 61109f448a4SAmadeusz Sławiński return ret; 6121245b700SNamarta Kohli } 6131245b700SNamarta Kohli 614202c8f70SCharles Keepax /* DAPM dai link stream work */ 61583f94a2eSKuninori Morimoto rtd->close_delayed_work_func = snd_soc_close_delayed_work; 616202c8f70SCharles Keepax 6171245b700SNamarta Kohli rtd->compr = compr; 6181245b700SNamarta Kohli compr->private_data = rtd; 6191245b700SNamarta Kohli 6201d5cd525SPierre-Louis Bossart dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n", 621141dfc9eSCharles Keepax codec_dai->name, cpu_dai->name); 6221f88eb0fSCharles Keepax 62309f448a4SAmadeusz Sławiński return 0; 6241245b700SNamarta Kohli } 6256f0c4226SJie Yang EXPORT_SYMBOL_GPL(snd_soc_new_compress); 626