sdk-hwV1.3/lichee/linux-4.9/sound/soc/sunxi/sun50iw11/sunxi-sndspdif.c

190 lines
4.8 KiB
C

/*
* sound\soc\sunxi\sun50iw11\sunxi-sndspdif.c
* (C) Copyright 2019-2025
* AllWinner Technology Co., Ltd. <www.allwinnertech.com>
* yumingfeng <yumingfeng@allwinnertech.com>
*
* some simple description for this code
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
*/
#include <linux/module.h>
#include <linux/clk.h>
#include <linux/mutex.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/pcm_params.h>
#include <sound/soc-dapm.h>
#include <linux/io.h>
#include <linux/of.h>
#include "sunxi-spdif.h"
#include "sunxi-sndspdif.h"
static int sunxi_sndspdif_hw_params(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params)
{
struct snd_soc_pcm_runtime *rtd = substream->private_data;
struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
int ret, clk_div;
unsigned int freq;
switch (params_rate(params)) {
case 24000:
case 32000:
case 48000:
case 96000:
case 192000:
freq = 24576000;
break;
case 22050:
case 44100:
case 176400:
freq = 22579200;
break;
default:
dev_err(rtd->dev, "Invalid rate %d\n", params_rate(params));
return -EINVAL;
}
ret = snd_soc_dai_set_sysclk(cpu_dai, 0, freq, 0);
if (ret < 0) {
dev_err(rtd->dev, "set sysclk failed.\n");
return ret;
}
clk_div = freq/params_rate(params);
clk_div = clk_div >> 7; //fs = PLL/[(div+1)*64*2]
ret = snd_soc_dai_set_clkdiv(cpu_dai, 0, clk_div);
if (ret < 0) {
dev_err(rtd->dev, "set clkdiv failed.\n");
return ret;
}
return 0;
}
static struct snd_soc_ops sunxi_sndspdif_ops = {
.hw_params = sunxi_sndspdif_hw_params,
};
static struct snd_soc_dai_link sunxi_sndspdif_dai_link = {
.name = "SPDIF",
.stream_name = "SUNXI-SPDIF",
.cpu_dai_name = "sunxi-spdif",
.platform_name = "sunxi-spdif",
.codec_dai_name = "spdif-hifi",
.codec_name = "spdif-utils",
.ops = &sunxi_sndspdif_ops,
};
static struct snd_soc_card snd_soc_sunxi_sndspdif = {
.name = "sndspdif",
.owner = THIS_MODULE,
.dai_link = &sunxi_sndspdif_dai_link,
.num_links = 1,
};
static int sunxi_sndspdif_dev_probe(struct platform_device *pdev)
{
int ret = 0;
struct snd_soc_card *card = &snd_soc_sunxi_sndspdif;
struct device_node *np = pdev->dev.of_node;
struct sunxi_sndspdif_priv *sndspdif_priv;
sndspdif_priv = devm_kzalloc(&pdev->dev,
sizeof(struct sunxi_sndspdif_priv), GFP_KERNEL);
if (IS_ERR_OR_NULL(sndspdif_priv)) {
dev_err(card->dev, "Can't malloc the sunxi_snddmic_priv!\n");
return -ENOMEM;
}
sndspdif_priv->card = card;
card->dev = &pdev->dev;
sunxi_sndspdif_dai_link.cpu_dai_name = NULL;
sunxi_sndspdif_dai_link.cpu_of_node = of_parse_phandle(np,
"sunxi,spdif-controller", 0);
if (!sunxi_sndspdif_dai_link.cpu_of_node) {
dev_err(&pdev->dev,
"Property 'sunxi,spdif-controller' missing\n");
ret = -EINVAL;
goto err_of_get_cpu_node;
}
sunxi_sndspdif_dai_link.platform_name = NULL;
sunxi_sndspdif_dai_link.platform_of_node =
sunxi_sndspdif_dai_link.cpu_of_node;
sndspdif_priv->codec_device = platform_device_alloc("spdif-utils", -1);
if (sndspdif_priv->codec_device == NULL) {
dev_err(&pdev->dev, "spdif utils alloc failed\n");
ret = -ENOMEM;
goto err_platform_dev_alloc;
}
ret = platform_device_add(sndspdif_priv->codec_device);
if (ret) {
dev_err(&pdev->dev, "spdif utils add failed\n");
ret = -EBUSY;
goto err_platform_dev_add;
}
ret = snd_soc_register_card(card);
if (ret) {
dev_err(&pdev->dev, "snd_soc_register_card failed: %d\n", ret);
ret = -EBUSY;
goto err_register_card;
}
return ret;
err_register_card:
platform_device_del(sndspdif_priv->codec_device);
err_platform_dev_add:
platform_device_put(sndspdif_priv->codec_device);
err_platform_dev_alloc:
err_of_get_cpu_node:
devm_kfree(&pdev->dev, sndspdif_priv);
return ret;
}
static int sunxi_sndspdif_dev_remove(struct platform_device *pdev)
{
struct snd_soc_card *card = platform_get_drvdata(pdev);
struct sunxi_sndspdif_priv *sndspdif_priv =
snd_soc_card_get_drvdata(card);
snd_soc_unregister_card(card);
platform_device_del(sndspdif_priv->codec_device);
platform_device_put(sndspdif_priv->codec_device);
devm_kfree(&pdev->dev, sndspdif_priv);
return 0;
}
static const struct of_device_id sunxi_spdif_of_match[] = {
{ .compatible = "allwinner,sunxi-spdif-machine", },
{},
};
static struct platform_driver sunxi_spdif_driver = {
.driver = {
.name = "sndspdif",
.owner = THIS_MODULE,
.of_match_table = sunxi_spdif_of_match,
.pm = &snd_soc_pm_ops,
},
.probe = sunxi_sndspdif_dev_probe,
.remove = sunxi_sndspdif_dev_remove,
};
module_platform_driver(sunxi_spdif_driver);
MODULE_AUTHOR("yumingfeng <yumingfeng@allwinnertech.com>");
MODULE_DESCRIPTION("SUNXI SPDIF ASoC audio driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:sndspdif");