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 /* 12 * Hardware interface for Renoir ACP block 13 */ 14 15 #include <linux/platform_device.h> 16 #include <linux/module.h> 17 #include <linux/err.h> 18 #include <linux/io.h> 19 #include <sound/pcm_params.h> 20 #include <sound/soc.h> 21 #include <sound/soc-dai.h> 22 #include <linux/dma-mapping.h> 23 #include <linux/pm_runtime.h> 24 25 #include "amd.h" 26 #include "acp-mach.h" 27 28 #define DRV_NAME "acp_asoc_renoir" 29 30 static struct acp_resource rsrc = { 31 .offset = 20, 32 .no_of_ctrls = 1, 33 .irqp_used = 0, 34 .irq_reg_offset = 0x1800, 35 .scratch_reg_offset = 0x12800, 36 .sram_pte_offset = 0x02052800, 37 }; 38 39 static struct snd_soc_acpi_codecs amp_rt1019 = { 40 .num_codecs = 1, 41 .codecs = {"10EC1019"} 42 }; 43 44 static struct snd_soc_acpi_codecs amp_max = { 45 .num_codecs = 1, 46 .codecs = {"MX98360A"} 47 }; 48 49 static struct snd_soc_acpi_mach snd_soc_acpi_amd_acp_machines[] = { 50 { 51 .id = "10EC5682", 52 .drv_name = "acp3xalc56821019", 53 .machine_quirk = snd_soc_acpi_codec_list, 54 .quirk_data = &_rt1019, 55 }, 56 { 57 .id = "RTL5682", 58 .drv_name = "acp3xalc5682sm98360", 59 .machine_quirk = snd_soc_acpi_codec_list, 60 .quirk_data = &_max, 61 }, 62 { 63 .id = "RTL5682", 64 .drv_name = "acp3xalc5682s1019", 65 .machine_quirk = snd_soc_acpi_codec_list, 66 .quirk_data = &_rt1019, 67 }, 68 { 69 .id = "AMDI1019", 70 .drv_name = "renoir-acp", 71 }, 72 { 73 .id = "ESSX8336", 74 .drv_name = "acp3x-es83xx", 75 }, 76 {}, 77 }; 78 79 static struct snd_soc_dai_driver acp_renoir_dai[] = { 80 { 81 .name = "acp-i2s-sp", 82 .id = I2S_SP_INSTANCE, 83 .playback = { 84 .stream_name = "I2S SP Playback", 85 .rates = SNDRV_PCM_RATE_8000_96000, 86 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | 87 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_LE, 88 .channels_min = 2, 89 .channels_max = 8, 90 .rate_min = 8000, 91 .rate_max = 96000, 92 }, 93 .capture = { 94 .stream_name = "I2S SP Capture", 95 .rates = SNDRV_PCM_RATE_8000_48000, 96 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | 97 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_LE, 98 .channels_min = 2, 99 .channels_max = 2, 100 .rate_min = 8000, 101 .rate_max = 48000, 102 }, 103 .ops = &asoc_acp_cpu_dai_ops, 104 }, 105 { 106 .name = "acp-i2s-bt", 107 .id = I2S_BT_INSTANCE, 108 .playback = { 109 .stream_name = "I2S BT Playback", 110 .rates = SNDRV_PCM_RATE_8000_96000, 111 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | 112 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_LE, 113 .channels_min = 2, 114 .channels_max = 8, 115 .rate_min = 8000, 116 .rate_max = 96000, 117 }, 118 .capture = { 119 .stream_name = "I2S BT Capture", 120 .rates = SNDRV_PCM_RATE_8000_48000, 121 .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | 122 SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S32_LE, 123 .channels_min = 2, 124 .channels_max = 2, 125 .rate_min = 8000, 126 .rate_max = 48000, 127 }, 128 .ops = &asoc_acp_cpu_dai_ops, 129 }, 130 { 131 .name = "acp-pdm-dmic", 132 .id = DMIC_INSTANCE, 133 .capture = { 134 .rates = SNDRV_PCM_RATE_8000_48000, 135 .formats = SNDRV_PCM_FMTBIT_S32_LE, 136 .channels_min = 2, 137 .channels_max = 2, 138 .rate_min = 8000, 139 .rate_max = 48000, 140 }, 141 .ops = &acp_dmic_dai_ops, 142 }, 143 }; 144 145 146 static int renoir_audio_probe(struct platform_device *pdev) 147 { 148 struct device *dev = &pdev->dev; 149 struct acp_chip_info *chip; 150 struct acp_dev_data *adata; 151 struct resource *res; 152 int ret; 153 154 chip = dev_get_platdata(&pdev->dev); 155 if (!chip || !chip->base) { 156 dev_err(&pdev->dev, "ACP chip data is NULL\n"); 157 return -ENODEV; 158 } 159 160 if (chip->acp_rev != ACP3X_DEV) { 161 dev_err(&pdev->dev, "Un-supported ACP Revision %d\n", chip->acp_rev); 162 return -ENODEV; 163 } 164 165 adata = devm_kzalloc(dev, sizeof(struct acp_dev_data), GFP_KERNEL); 166 if (!adata) 167 return -ENOMEM; 168 169 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "acp_mem"); 170 if (!res) { 171 dev_err(&pdev->dev, "IORESOURCE_MEM FAILED\n"); 172 return -ENODEV; 173 } 174 175 adata->acp_base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); 176 if (!adata->acp_base) 177 return -ENOMEM; 178 179 ret = platform_get_irq_byname(pdev, "acp_dai_irq"); 180 if (ret < 0) 181 return ret; 182 adata->i2s_irq = ret; 183 184 adata->dev = dev; 185 adata->dai_driver = acp_renoir_dai; 186 adata->num_dai = ARRAY_SIZE(acp_renoir_dai); 187 adata->rsrc = &rsrc; 188 adata->platform = RENOIR; 189 adata->flag = chip->flag; 190 191 adata->machines = snd_soc_acpi_amd_acp_machines; 192 acp_machine_select(adata); 193 194 dev_set_drvdata(dev, adata); 195 acp_enable_interrupts(adata); 196 acp_platform_register(dev); 197 198 pm_runtime_set_autosuspend_delay(&pdev->dev, ACP_SUSPEND_DELAY_MS); 199 pm_runtime_use_autosuspend(&pdev->dev); 200 pm_runtime_mark_last_busy(&pdev->dev); 201 pm_runtime_set_active(&pdev->dev); 202 pm_runtime_enable(&pdev->dev); 203 return 0; 204 } 205 206 static void renoir_audio_remove(struct platform_device *pdev) 207 { 208 struct device *dev = &pdev->dev; 209 struct acp_dev_data *adata = dev_get_drvdata(dev); 210 211 acp_disable_interrupts(adata); 212 acp_platform_unregister(dev); 213 } 214 215 static int __maybe_unused rn_pcm_resume(struct device *dev) 216 { 217 struct acp_dev_data *adata = dev_get_drvdata(dev); 218 struct acp_stream *stream; 219 struct snd_pcm_substream *substream; 220 snd_pcm_uframes_t buf_in_frames; 221 u64 buf_size; 222 223 spin_lock(&adata->acp_lock); 224 list_for_each_entry(stream, &adata->stream_list, list) { 225 substream = stream->substream; 226 if (substream && substream->runtime) { 227 buf_in_frames = (substream->runtime->buffer_size); 228 buf_size = frames_to_bytes(substream->runtime, buf_in_frames); 229 config_pte_for_stream(adata, stream); 230 config_acp_dma(adata, stream, buf_size); 231 if (stream->dai_id) 232 restore_acp_i2s_params(substream, adata, stream); 233 else 234 restore_acp_pdm_params(substream, adata); 235 } 236 } 237 spin_unlock(&adata->acp_lock); 238 return 0; 239 } 240 241 static const struct dev_pm_ops rn_dma_pm_ops = { 242 SET_SYSTEM_SLEEP_PM_OPS(NULL, rn_pcm_resume) 243 }; 244 245 static struct platform_driver renoir_driver = { 246 .probe = renoir_audio_probe, 247 .remove = renoir_audio_remove, 248 .driver = { 249 .name = "acp_asoc_renoir", 250 .pm = &rn_dma_pm_ops, 251 }, 252 }; 253 254 module_platform_driver(renoir_driver); 255 256 MODULE_DESCRIPTION("AMD ACP Renoir Driver"); 257 MODULE_IMPORT_NS(SND_SOC_ACP_COMMON); 258 MODULE_LICENSE("Dual BSD/GPL"); 259 MODULE_ALIAS("platform:" DRV_NAME); 260