|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +// |
| 3 | +// Copyright(c) 2021-2022 Intel Corporation. All rights reserved. |
| 4 | +// |
| 5 | +// Authors: Cezary Rojewski <[email protected]> |
| 6 | +// Amadeusz Slawinski <[email protected]> |
| 7 | +// |
| 8 | + |
| 9 | +#include <linux/debugfs.h> |
| 10 | +#include <linux/device.h> |
| 11 | +#include <sound/hda_register.h> |
| 12 | +#include <sound/hdaudio_ext.h> |
| 13 | +#include <sound/soc-acpi.h> |
| 14 | +#include <sound/soc-acpi-intel-match.h> |
| 15 | +#include <sound/soc-component.h> |
| 16 | +#include "avs.h" |
| 17 | +#include "path.h" |
| 18 | +#include "topology.h" |
| 19 | + |
| 20 | +struct avs_dma_data { |
| 21 | + struct avs_tplg_path_template *template; |
| 22 | + struct avs_path *path; |
| 23 | + /* |
| 24 | + * link stream is stored within substream's runtime |
| 25 | + * private_data to fulfill the needs of codec BE path |
| 26 | + * |
| 27 | + * host stream assigned |
| 28 | + */ |
| 29 | + struct hdac_ext_stream *host_stream; |
| 30 | +}; |
| 31 | + |
| 32 | +static ssize_t topology_name_read(struct file *file, char __user *user_buf, size_t count, |
| 33 | + loff_t *ppos) |
| 34 | +{ |
| 35 | + struct snd_soc_component *component = file->private_data; |
| 36 | + struct snd_soc_card *card = component->card; |
| 37 | + struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev); |
| 38 | + char buf[64]; |
| 39 | + size_t len; |
| 40 | + |
| 41 | + len = snprintf(buf, sizeof(buf), "%s/%s\n", component->driver->topology_name_prefix, |
| 42 | + mach->tplg_filename); |
| 43 | + |
| 44 | + return simple_read_from_buffer(user_buf, count, ppos, buf, len); |
| 45 | +} |
| 46 | + |
| 47 | +static const struct file_operations topology_name_fops = { |
| 48 | + .open = simple_open, |
| 49 | + .read = topology_name_read, |
| 50 | + .llseek = default_llseek, |
| 51 | +}; |
| 52 | + |
| 53 | +static int avs_component_load_libraries(struct avs_soc_component *acomp) |
| 54 | +{ |
| 55 | + struct avs_tplg *tplg = acomp->tplg; |
| 56 | + struct avs_dev *adev = to_avs_dev(acomp->base.dev); |
| 57 | + int ret; |
| 58 | + |
| 59 | + if (!tplg->num_libs) |
| 60 | + return 0; |
| 61 | + |
| 62 | + /* Parent device may be asleep and library loading involves IPCs. */ |
| 63 | + ret = pm_runtime_resume_and_get(adev->dev); |
| 64 | + if (ret < 0) |
| 65 | + return ret; |
| 66 | + |
| 67 | + avs_hda_clock_gating_enable(adev, false); |
| 68 | + avs_hda_l1sen_enable(adev, false); |
| 69 | + |
| 70 | + ret = avs_dsp_load_libraries(adev, tplg->libs, tplg->num_libs); |
| 71 | + |
| 72 | + avs_hda_l1sen_enable(adev, true); |
| 73 | + avs_hda_clock_gating_enable(adev, true); |
| 74 | + |
| 75 | + if (!ret) |
| 76 | + ret = avs_module_info_init(adev, false); |
| 77 | + |
| 78 | + pm_runtime_mark_last_busy(adev->dev); |
| 79 | + pm_runtime_put_autosuspend(adev->dev); |
| 80 | + |
| 81 | + return ret; |
| 82 | +} |
| 83 | + |
| 84 | +static int avs_component_probe(struct snd_soc_component *component) |
| 85 | +{ |
| 86 | + struct snd_soc_card *card = component->card; |
| 87 | + struct snd_soc_acpi_mach *mach; |
| 88 | + struct avs_soc_component *acomp; |
| 89 | + struct avs_dev *adev; |
| 90 | + char *filename; |
| 91 | + int ret; |
| 92 | + |
| 93 | + dev_dbg(card->dev, "probing %s card %s\n", component->name, card->name); |
| 94 | + mach = dev_get_platdata(card->dev); |
| 95 | + acomp = to_avs_soc_component(component); |
| 96 | + adev = to_avs_dev(component->dev); |
| 97 | + |
| 98 | + acomp->tplg = avs_tplg_new(component); |
| 99 | + if (!acomp->tplg) |
| 100 | + return -ENOMEM; |
| 101 | + |
| 102 | + if (!mach->tplg_filename) |
| 103 | + goto finalize; |
| 104 | + |
| 105 | + /* Load specified topology and create debugfs for it. */ |
| 106 | + filename = kasprintf(GFP_KERNEL, "%s/%s", component->driver->topology_name_prefix, |
| 107 | + mach->tplg_filename); |
| 108 | + if (!filename) |
| 109 | + return -ENOMEM; |
| 110 | + |
| 111 | + ret = avs_load_topology(component, filename); |
| 112 | + kfree(filename); |
| 113 | + if (ret < 0) |
| 114 | + return ret; |
| 115 | + |
| 116 | + ret = avs_component_load_libraries(acomp); |
| 117 | + if (ret < 0) { |
| 118 | + dev_err(card->dev, "libraries loading failed: %d\n", ret); |
| 119 | + goto err_load_libs; |
| 120 | + } |
| 121 | + |
| 122 | +finalize: |
| 123 | + debugfs_create_file("topology_name", 0444, component->debugfs_root, component, |
| 124 | + &topology_name_fops); |
| 125 | + |
| 126 | + mutex_lock(&adev->comp_list_mutex); |
| 127 | + list_add_tail(&acomp->node, &adev->comp_list); |
| 128 | + mutex_unlock(&adev->comp_list_mutex); |
| 129 | + |
| 130 | + return 0; |
| 131 | + |
| 132 | +err_load_libs: |
| 133 | + avs_remove_topology(component); |
| 134 | + return ret; |
| 135 | +} |
| 136 | + |
| 137 | +static void avs_component_remove(struct snd_soc_component *component) |
| 138 | +{ |
| 139 | + struct avs_soc_component *acomp = to_avs_soc_component(component); |
| 140 | + struct snd_soc_acpi_mach *mach; |
| 141 | + struct avs_dev *adev = to_avs_dev(component->dev); |
| 142 | + int ret; |
| 143 | + |
| 144 | + mach = dev_get_platdata(component->card->dev); |
| 145 | + |
| 146 | + mutex_lock(&adev->comp_list_mutex); |
| 147 | + list_del(&acomp->node); |
| 148 | + mutex_unlock(&adev->comp_list_mutex); |
| 149 | + |
| 150 | + if (mach->tplg_filename) { |
| 151 | + ret = avs_remove_topology(component); |
| 152 | + if (ret < 0) |
| 153 | + dev_err(component->dev, "unload topology failed: %d\n", ret); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +static int avs_component_open(struct snd_soc_component *component, |
| 158 | + struct snd_pcm_substream *substream) |
| 159 | +{ |
| 160 | + struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream); |
| 161 | + struct snd_pcm_hardware hwparams; |
| 162 | + |
| 163 | + /* only FE DAI links are handled here */ |
| 164 | + if (rtd->dai_link->no_pcm) |
| 165 | + return 0; |
| 166 | + |
| 167 | + hwparams.info = SNDRV_PCM_INFO_MMAP | |
| 168 | + SNDRV_PCM_INFO_MMAP_VALID | |
| 169 | + SNDRV_PCM_INFO_INTERLEAVED | |
| 170 | + SNDRV_PCM_INFO_PAUSE | |
| 171 | + SNDRV_PCM_INFO_NO_PERIOD_WAKEUP; |
| 172 | + |
| 173 | + hwparams.formats = SNDRV_PCM_FMTBIT_S16_LE | |
| 174 | + SNDRV_PCM_FMTBIT_S24_LE | |
| 175 | + SNDRV_PCM_FMTBIT_S32_LE; |
| 176 | + hwparams.period_bytes_min = 128; |
| 177 | + hwparams.period_bytes_max = AZX_MAX_BUF_SIZE / 2; |
| 178 | + hwparams.periods_min = 2; |
| 179 | + hwparams.periods_max = AZX_MAX_FRAG; |
| 180 | + hwparams.buffer_bytes_max = AZX_MAX_BUF_SIZE; |
| 181 | + hwparams.fifo_size = 0; |
| 182 | + |
| 183 | + return snd_soc_set_runtime_hwparams(substream, &hwparams); |
| 184 | +} |
| 185 | + |
| 186 | +static unsigned int avs_hda_stream_dpib_read(struct hdac_ext_stream *stream) |
| 187 | +{ |
| 188 | + return readl(hdac_stream(stream)->bus->remap_addr + AZX_REG_VS_SDXDPIB_XBASE + |
| 189 | + (AZX_REG_VS_SDXDPIB_XINTERVAL * hdac_stream(stream)->index)); |
| 190 | +} |
| 191 | + |
| 192 | +static snd_pcm_uframes_t |
| 193 | +avs_component_pointer(struct snd_soc_component *component, struct snd_pcm_substream *substream) |
| 194 | +{ |
| 195 | + struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream); |
| 196 | + struct avs_dma_data *data; |
| 197 | + struct hdac_ext_stream *host_stream; |
| 198 | + unsigned int pos; |
| 199 | + |
| 200 | + data = snd_soc_dai_get_dma_data(asoc_rtd_to_cpu(rtd, 0), substream); |
| 201 | + if (!data->host_stream) |
| 202 | + return 0; |
| 203 | + |
| 204 | + host_stream = data->host_stream; |
| 205 | + pos = avs_hda_stream_dpib_read(host_stream); |
| 206 | + |
| 207 | + if (pos >= hdac_stream(host_stream)->bufsize) |
| 208 | + pos = 0; |
| 209 | + |
| 210 | + return bytes_to_frames(substream->runtime, pos); |
| 211 | +} |
| 212 | + |
| 213 | +static int avs_component_mmap(struct snd_soc_component *component, |
| 214 | + struct snd_pcm_substream *substream, |
| 215 | + struct vm_area_struct *vma) |
| 216 | +{ |
| 217 | + return snd_pcm_lib_default_mmap(substream, vma); |
| 218 | +} |
| 219 | + |
| 220 | +#define MAX_PREALLOC_SIZE (32 * 1024 * 1024) |
| 221 | + |
| 222 | +static int avs_component_construct(struct snd_soc_component *component, |
| 223 | + struct snd_soc_pcm_runtime *rtd) |
| 224 | +{ |
| 225 | + struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0); |
| 226 | + struct snd_pcm *pcm = rtd->pcm; |
| 227 | + |
| 228 | + if (dai->driver->playback.channels_min) |
| 229 | + snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream, |
| 230 | + SNDRV_DMA_TYPE_DEV_SG, component->dev, 0, |
| 231 | + MAX_PREALLOC_SIZE); |
| 232 | + |
| 233 | + if (dai->driver->capture.channels_min) |
| 234 | + snd_pcm_set_managed_buffer(pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream, |
| 235 | + SNDRV_DMA_TYPE_DEV_SG, component->dev, 0, |
| 236 | + MAX_PREALLOC_SIZE); |
| 237 | + |
| 238 | + return 0; |
| 239 | +} |
| 240 | + |
| 241 | +static const struct snd_soc_component_driver avs_component_driver = { |
| 242 | + .name = "avs-pcm", |
| 243 | + .probe = avs_component_probe, |
| 244 | + .remove = avs_component_remove, |
| 245 | + .open = avs_component_open, |
| 246 | + .pointer = avs_component_pointer, |
| 247 | + .mmap = avs_component_mmap, |
| 248 | + .pcm_construct = avs_component_construct, |
| 249 | + .module_get_upon_open = 1, /* increment refcount when a pcm is opened */ |
| 250 | + .topology_name_prefix = "intel/avs", |
| 251 | + .non_legacy_dai_naming = true, |
| 252 | +}; |
| 253 | + |
| 254 | +__maybe_unused |
| 255 | +static int avs_soc_component_register(struct device *dev, const char *name, |
| 256 | + const struct snd_soc_component_driver *drv, |
| 257 | + struct snd_soc_dai_driver *cpu_dais, int num_cpu_dais) |
| 258 | +{ |
| 259 | + struct avs_soc_component *acomp; |
| 260 | + int ret; |
| 261 | + |
| 262 | + acomp = devm_kzalloc(dev, sizeof(*acomp), GFP_KERNEL); |
| 263 | + if (!acomp) |
| 264 | + return -ENOMEM; |
| 265 | + |
| 266 | + ret = snd_soc_component_initialize(&acomp->base, drv, dev); |
| 267 | + if (ret < 0) |
| 268 | + return ret; |
| 269 | + |
| 270 | + /* force name change after ASoC is done with its init */ |
| 271 | + acomp->base.name = name; |
| 272 | + INIT_LIST_HEAD(&acomp->node); |
| 273 | + |
| 274 | + return snd_soc_add_component(&acomp->base, cpu_dais, num_cpu_dais); |
| 275 | +} |
0 commit comments