|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright 2017 NXP |
| 4 | + * |
| 5 | + * The code contained herein is licensed under the GNU General Public |
| 6 | + * License. You may obtain a copy of the GNU General Public License |
| 7 | + * Version 2 or later at the following locations: |
| 8 | + * |
| 9 | + * http://www.opensource.org/licenses/gpl-license.html |
| 10 | + * http://www.gnu.org/copyleft/gpl.html |
| 11 | + */ |
| 12 | + |
| 13 | +#include <linux/module.h> |
| 14 | +#include <linux/of_platform.h> |
| 15 | +#include <linux/clk.h> |
| 16 | +#include <sound/soc.h> |
| 17 | +#include <sound/soc-dapm.h> |
| 18 | +#include <linux/pm_runtime.h> |
| 19 | +#include "fsl_sai.h" |
| 20 | +#include "fsl_audmix.h" |
| 21 | + |
| 22 | +struct imx_audmix { |
| 23 | + struct platform_device *pdev; |
| 24 | + struct snd_soc_card card; |
| 25 | + struct platform_device *audmix_pdev; |
| 26 | + struct platform_device *out_pdev; |
| 27 | + struct clk *cpu_mclk; |
| 28 | + int num_dai; |
| 29 | + struct snd_soc_dai_link *dai; |
| 30 | + int num_dai_conf; |
| 31 | + struct snd_soc_codec_conf *dai_conf; |
| 32 | + int num_dapm_routes; |
| 33 | + struct snd_soc_dapm_route *dapm_routes; |
| 34 | +}; |
| 35 | + |
| 36 | +static const u32 imx_audmix_rates[] = { |
| 37 | + 8000, 12000, 16000, 24000, 32000, 48000, 64000, 96000, |
| 38 | +}; |
| 39 | + |
| 40 | +static const struct snd_pcm_hw_constraint_list imx_audmix_rate_constraints = { |
| 41 | + .count = ARRAY_SIZE(imx_audmix_rates), |
| 42 | + .list = imx_audmix_rates, |
| 43 | +}; |
| 44 | + |
| 45 | +static int imx_audmix_fe_startup(struct snd_pcm_substream *substream) |
| 46 | +{ |
| 47 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 48 | + struct imx_audmix *priv = snd_soc_card_get_drvdata(rtd->card); |
| 49 | + struct snd_pcm_runtime *runtime = substream->runtime; |
| 50 | + struct device *dev = rtd->card->dev; |
| 51 | + unsigned long clk_rate = clk_get_rate(priv->cpu_mclk); |
| 52 | + int ret; |
| 53 | + |
| 54 | + if (clk_rate % 24576000 == 0) { |
| 55 | + ret = snd_pcm_hw_constraint_list(runtime, 0, |
| 56 | + SNDRV_PCM_HW_PARAM_RATE, |
| 57 | + &imx_audmix_rate_constraints); |
| 58 | + if (ret < 0) |
| 59 | + return ret; |
| 60 | + } else { |
| 61 | + dev_warn(dev, "mclk may be not supported %lu\n", clk_rate); |
| 62 | + } |
| 63 | + |
| 64 | + ret = snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS, |
| 65 | + 1, 8); |
| 66 | + if (ret < 0) |
| 67 | + return ret; |
| 68 | + |
| 69 | + return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, |
| 70 | + FSL_AUDMIX_FORMATS); |
| 71 | +} |
| 72 | + |
| 73 | +static int imx_audmix_fe_hw_params(struct snd_pcm_substream *substream, |
| 74 | + struct snd_pcm_hw_params *params) |
| 75 | +{ |
| 76 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 77 | + struct device *dev = rtd->card->dev; |
| 78 | + bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 79 | + unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF; |
| 80 | + u32 channels = params_channels(params); |
| 81 | + int ret, dir; |
| 82 | + |
| 83 | + /* For playback the AUDMIX is slave, and for record is master */ |
| 84 | + fmt |= tx ? SND_SOC_DAIFMT_CBS_CFS : SND_SOC_DAIFMT_CBM_CFM; |
| 85 | + dir = tx ? SND_SOC_CLOCK_OUT : SND_SOC_CLOCK_IN; |
| 86 | + |
| 87 | + /* set DAI configuration */ |
| 88 | + ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt); |
| 89 | + if (ret) { |
| 90 | + dev_err(dev, "failed to set cpu dai fmt: %d\n", ret); |
| 91 | + return ret; |
| 92 | + } |
| 93 | + |
| 94 | + ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, FSL_SAI_CLK_MAST1, 0, dir); |
| 95 | + if (ret) { |
| 96 | + dev_err(dev, "failed to set cpu sysclk: %d\n", ret); |
| 97 | + return ret; |
| 98 | + } |
| 99 | + |
| 100 | + /* |
| 101 | + * Per datasheet, AUDMIX expects 8 slots and 32 bits |
| 102 | + * for every slot in TDM mode. |
| 103 | + */ |
| 104 | + ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, BIT(channels) - 1, |
| 105 | + BIT(channels) - 1, 8, 32); |
| 106 | + if (ret) |
| 107 | + dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret); |
| 108 | + |
| 109 | + return ret; |
| 110 | +} |
| 111 | + |
| 112 | +static int imx_audmix_be_hw_params(struct snd_pcm_substream *substream, |
| 113 | + struct snd_pcm_hw_params *params) |
| 114 | +{ |
| 115 | + struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 116 | + struct device *dev = rtd->card->dev; |
| 117 | + bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; |
| 118 | + unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF; |
| 119 | + int ret; |
| 120 | + |
| 121 | + if (!tx) |
| 122 | + return 0; |
| 123 | + |
| 124 | + /* For playback the AUDMIX is slave */ |
| 125 | + fmt |= SND_SOC_DAIFMT_CBM_CFM; |
| 126 | + |
| 127 | + /* set AUDMIX DAI configuration */ |
| 128 | + ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt); |
| 129 | + if (ret) |
| 130 | + dev_err(dev, "failed to set AUDMIX DAI fmt: %d\n", ret); |
| 131 | + |
| 132 | + return ret; |
| 133 | +} |
| 134 | + |
| 135 | +static struct snd_soc_ops imx_audmix_fe_ops = { |
| 136 | + .startup = imx_audmix_fe_startup, |
| 137 | + .hw_params = imx_audmix_fe_hw_params, |
| 138 | +}; |
| 139 | + |
| 140 | +static struct snd_soc_ops imx_audmix_be_ops = { |
| 141 | + .hw_params = imx_audmix_be_hw_params, |
| 142 | +}; |
| 143 | + |
| 144 | +static int imx_audmix_probe(struct platform_device *pdev) |
| 145 | +{ |
| 146 | + struct device_node *np = pdev->dev.of_node; |
| 147 | + struct device_node *audmix_np = NULL, *out_cpu_np = NULL; |
| 148 | + struct platform_device *audmix_pdev = NULL; |
| 149 | + struct platform_device *cpu_pdev; |
| 150 | + struct of_phandle_args args; |
| 151 | + struct imx_audmix *priv; |
| 152 | + int i, num_dai, ret; |
| 153 | + const char *fe_name_pref = "HiFi-AUDMIX-FE-"; |
| 154 | + char *be_name, *be_pb, *be_cp, *dai_name, *capture_dai_name; |
| 155 | + |
| 156 | + if (pdev->dev.parent) { |
| 157 | + audmix_np = pdev->dev.parent->of_node; |
| 158 | + } else { |
| 159 | + dev_err(&pdev->dev, "Missing parent device.\n"); |
| 160 | + return -EINVAL; |
| 161 | + } |
| 162 | + |
| 163 | + if (!audmix_np) { |
| 164 | + dev_err(&pdev->dev, "Missign DT node for parent device.\n"); |
| 165 | + return -EINVAL; |
| 166 | + } |
| 167 | + |
| 168 | + audmix_pdev = of_find_device_by_node(audmix_np); |
| 169 | + if (!audmix_pdev) { |
| 170 | + dev_err(&pdev->dev, "Missing AUDMIX platform device for %s\n", |
| 171 | + np->full_name); |
| 172 | + return -EINVAL; |
| 173 | + } |
| 174 | + |
| 175 | + num_dai = of_count_phandle_with_args(audmix_np, "dais", NULL); |
| 176 | + if (num_dai != FSL_AUDMIX_MAX_DAIS) { |
| 177 | + dev_err(&pdev->dev, "Need 2 dais to be provided for %s\n", |
| 178 | + audmix_np->full_name); |
| 179 | + return -EINVAL; |
| 180 | + } |
| 181 | + |
| 182 | + priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); |
| 183 | + if (!priv) |
| 184 | + return -ENOMEM; |
| 185 | + |
| 186 | + priv->num_dai = 2 * num_dai; |
| 187 | + priv->dai = devm_kzalloc(&pdev->dev, priv->num_dai * |
| 188 | + sizeof(struct snd_soc_dai_link), GFP_KERNEL); |
| 189 | + if (!priv->dai) |
| 190 | + return -ENOMEM; |
| 191 | + |
| 192 | + priv->num_dai_conf = num_dai; |
| 193 | + priv->dai_conf = devm_kzalloc(&pdev->dev, priv->num_dai_conf * |
| 194 | + sizeof(struct snd_soc_codec_conf), |
| 195 | + GFP_KERNEL); |
| 196 | + if (!priv->dai_conf) |
| 197 | + return -ENOMEM; |
| 198 | + |
| 199 | + priv->num_dapm_routes = 3 * num_dai; |
| 200 | + priv->dapm_routes = devm_kzalloc(&pdev->dev, priv->num_dapm_routes * |
| 201 | + sizeof(struct snd_soc_dapm_route), |
| 202 | + GFP_KERNEL); |
| 203 | + if (!priv->dapm_routes) |
| 204 | + return -ENOMEM; |
| 205 | + |
| 206 | + for (i = 0; i < num_dai; i++) { |
| 207 | + ret = of_parse_phandle_with_args(audmix_np, "dais", NULL, i, |
| 208 | + &args); |
| 209 | + if (ret < 0) { |
| 210 | + dev_err(&pdev->dev, "of_parse_phandle_with_args failed\n"); |
| 211 | + return ret; |
| 212 | + } |
| 213 | + |
| 214 | + cpu_pdev = of_find_device_by_node(args.np); |
| 215 | + if (!cpu_pdev) { |
| 216 | + dev_err(&pdev->dev, "failed to find SAI platform device\n"); |
| 217 | + return -EINVAL; |
| 218 | + } |
| 219 | + |
| 220 | + dai_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s%s", |
| 221 | + fe_name_pref, args.np->full_name + 1); |
| 222 | + |
| 223 | + dev_info(pdev->dev.parent, "DAI FE name:%s\n", dai_name); |
| 224 | + |
| 225 | + if (i == 0) { |
| 226 | + out_cpu_np = args.np; |
| 227 | + capture_dai_name = |
| 228 | + devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s", |
| 229 | + dai_name, "CPU-Capture"); |
| 230 | + } |
| 231 | + |
| 232 | + priv->dai[i].name = dai_name; |
| 233 | + priv->dai[i].stream_name = "HiFi-AUDMIX-FE"; |
| 234 | + priv->dai[i].codec_dai_name = "snd-soc-dummy-dai"; |
| 235 | + priv->dai[i].codec_name = "snd-soc-dummy"; |
| 236 | + priv->dai[i].cpu_of_node = args.np; |
| 237 | + priv->dai[i].cpu_dai_name = dev_name(&cpu_pdev->dev); |
| 238 | + priv->dai[i].platform_of_node = args.np; |
| 239 | + priv->dai[i].dynamic = 1; |
| 240 | + priv->dai[i].dpcm_playback = 1; |
| 241 | + priv->dai[i].dpcm_capture = (i == 0 ? 1 : 0); |
| 242 | + priv->dai[i].ignore_pmdown_time = 1; |
| 243 | + priv->dai[i].ops = &imx_audmix_fe_ops; |
| 244 | + |
| 245 | + /* Add AUDMIX Backend */ |
| 246 | + be_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, |
| 247 | + "audmix-%d", i); |
| 248 | + be_pb = devm_kasprintf(&pdev->dev, GFP_KERNEL, |
| 249 | + "AUDMIX-Playback-%d", i); |
| 250 | + be_cp = devm_kasprintf(&pdev->dev, GFP_KERNEL, |
| 251 | + "AUDMIX-Capture-%d", i); |
| 252 | + |
| 253 | + priv->dai[num_dai + i].name = be_name; |
| 254 | + priv->dai[num_dai + i].codec_dai_name = "snd-soc-dummy-dai"; |
| 255 | + priv->dai[num_dai + i].codec_name = "snd-soc-dummy"; |
| 256 | + priv->dai[num_dai + i].cpu_of_node = audmix_np; |
| 257 | + priv->dai[num_dai + i].cpu_dai_name = be_name; |
| 258 | + priv->dai[num_dai + i].platform_name = "snd-soc-dummy"; |
| 259 | + priv->dai[num_dai + i].no_pcm = 1; |
| 260 | + priv->dai[num_dai + i].dpcm_playback = 1; |
| 261 | + priv->dai[num_dai + i].dpcm_capture = 1; |
| 262 | + priv->dai[num_dai + i].ignore_pmdown_time = 1; |
| 263 | + priv->dai[num_dai + i].ops = &imx_audmix_be_ops; |
| 264 | + |
| 265 | + priv->dai_conf[i].of_node = args.np; |
| 266 | + priv->dai_conf[i].name_prefix = dai_name; |
| 267 | + |
| 268 | + priv->dapm_routes[i].source = |
| 269 | + devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s %s", |
| 270 | + dai_name, "CPU-Playback"); |
| 271 | + priv->dapm_routes[i].sink = be_pb; |
| 272 | + priv->dapm_routes[num_dai + i].source = be_pb; |
| 273 | + priv->dapm_routes[num_dai + i].sink = be_cp; |
| 274 | + priv->dapm_routes[2 * num_dai + i].source = be_cp; |
| 275 | + priv->dapm_routes[2 * num_dai + i].sink = capture_dai_name; |
| 276 | + } |
| 277 | + |
| 278 | + cpu_pdev = of_find_device_by_node(out_cpu_np); |
| 279 | + if (!cpu_pdev) { |
| 280 | + dev_err(&pdev->dev, "failed to find SAI platform device\n"); |
| 281 | + return -EINVAL; |
| 282 | + } |
| 283 | + priv->cpu_mclk = devm_clk_get(&cpu_pdev->dev, "mclk1"); |
| 284 | + if (IS_ERR(priv->cpu_mclk)) { |
| 285 | + ret = PTR_ERR(priv->cpu_mclk); |
| 286 | + dev_err(&cpu_pdev->dev, "failed to get DAI mclk1: %d\n", ret); |
| 287 | + return -EINVAL; |
| 288 | + } |
| 289 | + |
| 290 | + priv->audmix_pdev = audmix_pdev; |
| 291 | + priv->out_pdev = cpu_pdev; |
| 292 | + |
| 293 | + priv->card.dai_link = priv->dai; |
| 294 | + priv->card.num_links = priv->num_dai; |
| 295 | + priv->card.codec_conf = priv->dai_conf; |
| 296 | + priv->card.num_configs = priv->num_dai_conf; |
| 297 | + priv->card.dapm_routes = priv->dapm_routes; |
| 298 | + priv->card.num_dapm_routes = priv->num_dapm_routes; |
| 299 | + priv->card.dev = pdev->dev.parent; |
| 300 | + priv->card.owner = THIS_MODULE; |
| 301 | + priv->card.name = "imx-audmix"; |
| 302 | + |
| 303 | + platform_set_drvdata(pdev, &priv->card); |
| 304 | + snd_soc_card_set_drvdata(&priv->card, priv); |
| 305 | + |
| 306 | + ret = devm_snd_soc_register_card(pdev->dev.parent, &priv->card); |
| 307 | + if (ret) { |
| 308 | + dev_err(&pdev->dev, "snd_soc_register_card failed\n"); |
| 309 | + return ret; |
| 310 | + } |
| 311 | + |
| 312 | + return ret; |
| 313 | +} |
| 314 | + |
| 315 | +static struct platform_driver imx_audmix_driver = { |
| 316 | + .probe = imx_audmix_probe, |
| 317 | + .driver = { |
| 318 | + .name = "imx-audmix", |
| 319 | + .pm = &snd_soc_pm_ops, |
| 320 | + }, |
| 321 | +}; |
| 322 | +module_platform_driver(imx_audmix_driver); |
| 323 | + |
| 324 | +MODULE_DESCRIPTION("NXP AUDMIX ASoC machine driver"); |
| 325 | +MODULE_AUTHOR( "Viorel Suman <[email protected]>"); |
| 326 | +MODULE_ALIAS("platform:imx-audmix"); |
| 327 | +MODULE_LICENSE("GPL v2"); |
0 commit comments