1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // This file is provided under a dual BSD/GPLv2 license. When using or 4 // redistributing this file, you may do so under either license. 5 // 6 // Copyright(c) 2021 Advanced Micro Devices, Inc. 7 // 8 // Authors: Ajit Kumar Pandey <AjitKumar.Pandey@amd.com> 9 10 /* 11 * Generic interface for ACP audio blck PCM component 12 */ 13 14 #include <linux/platform_device.h> 15 #include <linux/module.h> 16 #include <linux/err.h> 17 #include <linux/io.h> 18 #include <sound/pcm_params.h> 19 #include <sound/soc.h> 20 #include <sound/soc-dai.h> 21 #include <linux/dma-mapping.h> 22 23 #include "amd.h" 24 #include "../mach-config.h" 25 #include "acp-mach.h" 26 27 #define DRV_NAME "acp_i2s_dma" 28 29 static const struct snd_pcm_hardware acp_pcm_hardware_playback = { 30 .info = SNDRV_PCM_INFO_INTERLEAVED | 31 SNDRV_PCM_INFO_BLOCK_TRANSFER | 32 SNDRV_PCM_INFO_BATCH | 33 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | 34 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, 35 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | 36 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE | 37 SNDRV_PCM_FMTBIT_S32_LE, 38 .channels_min = 2, 39 .channels_max = 8, 40 .rates = SNDRV_PCM_RATE_8000_96000, 41 .rate_min = 8000, 42 .rate_max = 96000, 43 .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE, 44 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE, 45 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE, 46 .periods_min = PLAYBACK_MIN_NUM_PERIODS, 47 .periods_max = PLAYBACK_MAX_NUM_PERIODS, 48 }; 49 50 static const struct snd_pcm_hardware acp_pcm_hardware_capture = { 51 .info = SNDRV_PCM_INFO_INTERLEAVED | 52 SNDRV_PCM_INFO_BLOCK_TRANSFER | 53 SNDRV_PCM_INFO_BATCH | 54 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | 55 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, 56 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | 57 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE | 58 SNDRV_PCM_FMTBIT_S32_LE, 59 .channels_min = 2, 60 .channels_max = 2, 61 .rates = SNDRV_PCM_RATE_8000_48000, 62 .rate_min = 8000, 63 .rate_max = 48000, 64 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE, 65 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE, 66 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE, 67 .periods_min = CAPTURE_MIN_NUM_PERIODS, 68 .periods_max = CAPTURE_MAX_NUM_PERIODS, 69 }; 70 71 static const struct snd_pcm_hardware acp6x_pcm_hardware_playback = { 72 .info = SNDRV_PCM_INFO_INTERLEAVED | 73 SNDRV_PCM_INFO_BLOCK_TRANSFER | 74 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | 75 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, 76 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | 77 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE | 78 SNDRV_PCM_FMTBIT_S32_LE, 79 .channels_min = 2, 80 .channels_max = 32, 81 .rates = SNDRV_PCM_RATE_8000_192000, 82 .rate_min = 8000, 83 .rate_max = 192000, 84 .buffer_bytes_max = PLAYBACK_MAX_NUM_PERIODS * PLAYBACK_MAX_PERIOD_SIZE, 85 .period_bytes_min = PLAYBACK_MIN_PERIOD_SIZE, 86 .period_bytes_max = PLAYBACK_MAX_PERIOD_SIZE, 87 .periods_min = PLAYBACK_MIN_NUM_PERIODS, 88 .periods_max = PLAYBACK_MAX_NUM_PERIODS, 89 }; 90 91 static const struct snd_pcm_hardware acp6x_pcm_hardware_capture = { 92 .info = SNDRV_PCM_INFO_INTERLEAVED | 93 SNDRV_PCM_INFO_BLOCK_TRANSFER | 94 SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_MMAP_VALID | 95 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME, 96 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | 97 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S24_LE | 98 SNDRV_PCM_FMTBIT_S32_LE, 99 .channels_min = 2, 100 .channels_max = 32, 101 .rates = SNDRV_PCM_RATE_8000_192000, 102 .rate_min = 8000, 103 .rate_max = 192000, 104 .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE, 105 .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE, 106 .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE, 107 .periods_min = CAPTURE_MIN_NUM_PERIODS, 108 .periods_max = CAPTURE_MAX_NUM_PERIODS, 109 }; 110 111 int acp_machine_select(struct acp_dev_data *adata) 112 { 113 struct snd_soc_acpi_mach *mach; 114 int size, platform; 115 116 if (adata->flag == FLAG_AMD_LEGACY_ONLY_DMIC) { 117 platform = adata->acp_rev; 118 adata->mach_dev = platform_device_register_data(adata->dev, "acp-pdm-mach", 119 PLATFORM_DEVID_NONE, &platform, 120 sizeof(platform)); 121 } else { 122 size = sizeof(*adata->machines); 123 mach = snd_soc_acpi_find_machine(adata->machines); 124 if (!mach) { 125 dev_err(adata->dev, "warning: No matching ASoC machine driver found\n"); 126 return -EINVAL; 127 } 128 mach->mach_params.subsystem_rev = adata->acp_rev; 129 adata->mach_dev = platform_device_register_data(adata->dev, mach->drv_name, 130 PLATFORM_DEVID_NONE, mach, size); 131 } 132 if (IS_ERR(adata->mach_dev)) 133 dev_warn(adata->dev, "Unable to register Machine device\n"); 134 return 0; 135 } 136 EXPORT_SYMBOL_NS_GPL(acp_machine_select, SND_SOC_ACP_COMMON); 137 138 static irqreturn_t i2s_irq_handler(int irq, void *data) 139 { 140 struct acp_dev_data *adata = data; 141 struct acp_resource *rsrc = adata->rsrc; 142 struct acp_stream *stream; 143 u16 i2s_flag = 0; 144 u32 ext_intr_stat, ext_intr_stat1; 145 146 if (adata->rsrc->no_of_ctrls == 2) 147 ext_intr_stat1 = readl(ACP_EXTERNAL_INTR_STAT(adata, (rsrc->irqp_used - 1))); 148 149 ext_intr_stat = readl(ACP_EXTERNAL_INTR_STAT(adata, rsrc->irqp_used)); 150 151 spin_lock(&adata->acp_lock); 152 list_for_each_entry(stream, &adata->stream_list, list) { 153 if (ext_intr_stat & stream->irq_bit) { 154 writel(stream->irq_bit, 155 ACP_EXTERNAL_INTR_STAT(adata, rsrc->irqp_used)); 156 snd_pcm_period_elapsed(stream->substream); 157 i2s_flag = 1; 158 } 159 if (adata->rsrc->no_of_ctrls == 2) { 160 if (ext_intr_stat1 & stream->irq_bit) { 161 writel(stream->irq_bit, ACP_EXTERNAL_INTR_STAT(adata, 162 (rsrc->irqp_used - 1))); 163 snd_pcm_period_elapsed(stream->substream); 164 i2s_flag = 1; 165 } 166 } 167 } 168 spin_unlock(&adata->acp_lock); 169 if (i2s_flag) 170 return IRQ_HANDLED; 171 172 return IRQ_NONE; 173 } 174 175 void config_pte_for_stream(struct acp_dev_data *adata, struct acp_stream *stream) 176 { 177 struct acp_resource *rsrc = adata->rsrc; 178 u32 reg_val; 179 180 reg_val = rsrc->sram_pte_offset; 181 stream->reg_offset = 0x02000000; 182 183 writel((reg_val + GRP1_OFFSET) | BIT(31), adata->acp_base + ACPAXI2AXI_ATU_BASE_ADDR_GRP_1); 184 writel(PAGE_SIZE_4K_ENABLE, adata->acp_base + ACPAXI2AXI_ATU_PAGE_SIZE_GRP_1); 185 186 writel((reg_val + GRP2_OFFSET) | BIT(31), adata->acp_base + ACPAXI2AXI_ATU_BASE_ADDR_GRP_2); 187 writel(PAGE_SIZE_4K_ENABLE, adata->acp_base + ACPAXI2AXI_ATU_PAGE_SIZE_GRP_2); 188 189 writel(reg_val | BIT(31), adata->acp_base + ACPAXI2AXI_ATU_BASE_ADDR_GRP_5); 190 writel(PAGE_SIZE_4K_ENABLE, adata->acp_base + ACPAXI2AXI_ATU_PAGE_SIZE_GRP_5); 191 192 writel(0x01, adata->acp_base + ACPAXI2AXI_ATU_CTRL); 193 } 194 EXPORT_SYMBOL_NS_GPL(config_pte_for_stream, SND_SOC_ACP_COMMON); 195 196 void config_acp_dma(struct acp_dev_data *adata, struct acp_stream *stream, int size) 197 { 198 struct snd_pcm_substream *substream = stream->substream; 199 struct acp_resource *rsrc = adata->rsrc; 200 dma_addr_t addr = substream->dma_buffer.addr; 201 int num_pages = (PAGE_ALIGN(size) >> PAGE_SHIFT); 202 u32 low, high, val; 203 u16 page_idx; 204 205 switch (adata->acp_rev) { 206 case ACP70_PCI_ID: 207 case ACP71_PCI_ID: 208 switch (stream->dai_id) { 209 case I2S_SP_INSTANCE: 210 if (stream->dir == SNDRV_PCM_STREAM_PLAYBACK) 211 val = 0x0; 212 else 213 val = 0x1000; 214 break; 215 case I2S_BT_INSTANCE: 216 if (stream->dir == SNDRV_PCM_STREAM_PLAYBACK) 217 val = 0x2000; 218 else 219 val = 0x3000; 220 break; 221 case I2S_HS_INSTANCE: 222 if (stream->dir == SNDRV_PCM_STREAM_PLAYBACK) 223 val = 0x4000; 224 else 225 val = 0x5000; 226 break; 227 case DMIC_INSTANCE: 228 val = 0x6000; 229 break; 230 default: 231 dev_err(adata->dev, "Invalid dai id %x\n", stream->dai_id); 232 return; 233 } 234 break; 235 default: 236 val = stream->pte_offset; 237 break; 238 } 239 240 for (page_idx = 0; page_idx < num_pages; page_idx++) { 241 /* Load the low address of page int ACP SRAM through SRBM */ 242 low = lower_32_bits(addr); 243 high = upper_32_bits(addr); 244 writel(low, adata->acp_base + rsrc->scratch_reg_offset + val); 245 high |= BIT(31); 246 writel(high, adata->acp_base + rsrc->scratch_reg_offset + val + 4); 247 248 /* Move to next physically contiguous page */ 249 val += 8; 250 addr += PAGE_SIZE; 251 } 252 } 253 EXPORT_SYMBOL_NS_GPL(config_acp_dma, SND_SOC_ACP_COMMON); 254 255 static int acp_dma_open(struct snd_soc_component *component, struct snd_pcm_substream *substream) 256 { 257 struct snd_pcm_runtime *runtime = substream->runtime; 258 struct device *dev = component->dev; 259 struct acp_dev_data *adata = dev_get_drvdata(dev); 260 struct acp_chip_info *chip; 261 struct acp_stream *stream; 262 int ret; 263 264 stream = kzalloc(sizeof(*stream), GFP_KERNEL); 265 if (!stream) 266 return -ENOMEM; 267 268 stream->substream = substream; 269 chip = dev_get_platdata(dev); 270 switch (chip->acp_rev) { 271 case ACP63_PCI_ID: 272 case ACP70_PCI_ID: 273 case ACP71_PCI_ID: 274 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 275 runtime->hw = acp6x_pcm_hardware_playback; 276 else 277 runtime->hw = acp6x_pcm_hardware_capture; 278 break; 279 default: 280 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 281 runtime->hw = acp_pcm_hardware_playback; 282 else 283 runtime->hw = acp_pcm_hardware_capture; 284 break; 285 } 286 287 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_BYTES, DMA_SIZE); 288 if (ret) { 289 dev_err(component->dev, "set hw constraint HW_PARAM_PERIOD_BYTES failed\n"); 290 kfree(stream); 291 return ret; 292 } 293 294 ret = snd_pcm_hw_constraint_step(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, DMA_SIZE); 295 if (ret) { 296 dev_err(component->dev, "set hw constraint HW_PARAM_BUFFER_BYTES failed\n"); 297 kfree(stream); 298 return ret; 299 } 300 301 ret = snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS); 302 if (ret < 0) { 303 dev_err(component->dev, "set integer constraint failed\n"); 304 kfree(stream); 305 return ret; 306 } 307 runtime->private_data = stream; 308 309 writel(1, ACP_EXTERNAL_INTR_ENB(adata)); 310 311 spin_lock_irq(&adata->acp_lock); 312 list_add_tail(&stream->list, &adata->stream_list); 313 spin_unlock_irq(&adata->acp_lock); 314 315 return ret; 316 } 317 318 static int acp_dma_hw_params(struct snd_soc_component *component, 319 struct snd_pcm_substream *substream, 320 struct snd_pcm_hw_params *params) 321 { 322 struct acp_dev_data *adata = snd_soc_component_get_drvdata(component); 323 struct acp_stream *stream = substream->runtime->private_data; 324 u64 size = params_buffer_bytes(params); 325 326 /* Configure ACP DMA block with params */ 327 config_pte_for_stream(adata, stream); 328 config_acp_dma(adata, stream, size); 329 330 return 0; 331 } 332 333 static snd_pcm_uframes_t acp_dma_pointer(struct snd_soc_component *component, 334 struct snd_pcm_substream *substream) 335 { 336 struct device *dev = component->dev; 337 struct acp_dev_data *adata = dev_get_drvdata(dev); 338 struct acp_stream *stream = substream->runtime->private_data; 339 u32 pos, buffersize; 340 u64 bytescount; 341 342 buffersize = frames_to_bytes(substream->runtime, 343 substream->runtime->buffer_size); 344 345 bytescount = acp_get_byte_count(adata, stream->dai_id, substream->stream); 346 347 if (bytescount > stream->bytescount) 348 bytescount -= stream->bytescount; 349 350 pos = do_div(bytescount, buffersize); 351 352 return bytes_to_frames(substream->runtime, pos); 353 } 354 355 static int acp_dma_new(struct snd_soc_component *component, 356 struct snd_soc_pcm_runtime *rtd) 357 { 358 struct device *parent = component->dev->parent; 359 360 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, 361 parent, MIN_BUFFER, MAX_BUFFER); 362 return 0; 363 } 364 365 static int acp_dma_close(struct snd_soc_component *component, 366 struct snd_pcm_substream *substream) 367 { 368 struct device *dev = component->dev; 369 struct acp_dev_data *adata = dev_get_drvdata(dev); 370 struct acp_stream *stream = substream->runtime->private_data; 371 372 /* Remove entry from list */ 373 spin_lock_irq(&adata->acp_lock); 374 list_del(&stream->list); 375 spin_unlock_irq(&adata->acp_lock); 376 kfree(stream); 377 378 return 0; 379 } 380 381 static const struct snd_soc_component_driver acp_pcm_component = { 382 .name = DRV_NAME, 383 .open = acp_dma_open, 384 .close = acp_dma_close, 385 .hw_params = acp_dma_hw_params, 386 .pointer = acp_dma_pointer, 387 .pcm_construct = acp_dma_new, 388 .legacy_dai_naming = 1, 389 }; 390 391 int acp_platform_register(struct device *dev) 392 { 393 struct acp_dev_data *adata = dev_get_drvdata(dev); 394 struct snd_soc_dai_driver; 395 unsigned int status; 396 397 status = devm_request_irq(dev, adata->i2s_irq, i2s_irq_handler, 398 IRQF_SHARED, "ACP_I2S_IRQ", adata); 399 if (status) { 400 dev_err(dev, "ACP I2S IRQ request failed\n"); 401 return status; 402 } 403 404 status = devm_snd_soc_register_component(dev, &acp_pcm_component, 405 adata->dai_driver, 406 adata->num_dai); 407 if (status) { 408 dev_err(dev, "Fail to register acp i2s component\n"); 409 return status; 410 } 411 412 INIT_LIST_HEAD(&adata->stream_list); 413 spin_lock_init(&adata->acp_lock); 414 415 return 0; 416 } 417 EXPORT_SYMBOL_NS_GPL(acp_platform_register, SND_SOC_ACP_COMMON); 418 419 int acp_platform_unregister(struct device *dev) 420 { 421 struct acp_dev_data *adata = dev_get_drvdata(dev); 422 423 if (adata->mach_dev) 424 platform_device_unregister(adata->mach_dev); 425 return 0; 426 } 427 EXPORT_SYMBOL_NS_GPL(acp_platform_unregister, SND_SOC_ACP_COMMON); 428 429 MODULE_DESCRIPTION("AMD ACP PCM Driver"); 430 MODULE_LICENSE("Dual BSD/GPL"); 431 MODULE_ALIAS(DRV_NAME); 432