205 lines
4.9 KiB
C
205 lines
4.9 KiB
C
/*
|
|
* sound\soc\sunxi\sunx8iw19-sndcodec.c
|
|
* (C) Copyright 2014-2019
|
|
* allwinner Technology Co., Ltd. <www.allwinnertech.com>
|
|
* yumingfeng <yumingfeng@allwinnetech.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 <linux/gpio.h>
|
|
#include <linux/of_gpio.h>
|
|
#include <linux/io.h>
|
|
#include <linux/input.h>
|
|
#include <sound/pcm.h>
|
|
#include <sound/soc.h>
|
|
#include <sound/jack.h>
|
|
#include <linux/of.h>
|
|
#include <sound/pcm_params.h>
|
|
#include <sound/soc-dapm.h>
|
|
#include <linux/delay.h>
|
|
|
|
struct sunxi_card_priv {
|
|
struct snd_soc_card *card;
|
|
};
|
|
|
|
static const struct snd_kcontrol_new sunxi_card_controls[] = {
|
|
SOC_DAPM_PIN_SWITCH("External Speaker"),
|
|
};
|
|
|
|
static const struct snd_soc_dapm_widget sunxi_card_dapm_widgets[] = {
|
|
SND_SOC_DAPM_MIC("Main Mic", NULL),
|
|
SND_SOC_DAPM_LINE("LINEIN", NULL),
|
|
};
|
|
|
|
static const struct snd_soc_dapm_route sunxi_card_routes[] = {
|
|
{"MainMic Bias", NULL, "Main Mic"},
|
|
|
|
{"External Speaker", NULL, "LINEOUT"},
|
|
|
|
{"LINEINL", NULL, "LINEIN"},
|
|
};
|
|
|
|
/*
|
|
* Card initialization
|
|
*/
|
|
static int sunxi_card_init(struct snd_soc_pcm_runtime *rtd)
|
|
{
|
|
struct snd_soc_codec *codec = rtd->codec;
|
|
|
|
snd_soc_dapm_disable_pin(&codec->component.dapm, "External Speaker");
|
|
snd_soc_dapm_enable_pin(&codec->component.dapm, "LINEIN");
|
|
snd_soc_dapm_enable_pin(&codec->component.dapm, "LINEOUT");
|
|
|
|
snd_soc_dapm_sync(&codec->component.dapm);
|
|
|
|
pr_warn("[%s] card init finished.\n", __func__);
|
|
return 0;
|
|
}
|
|
|
|
static int sunxi_card_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 *codec_dai = rtd->codec_dai;
|
|
struct snd_soc_card *card = rtd->card;
|
|
unsigned int freq;
|
|
int ret;
|
|
|
|
switch (params_rate(params)) {
|
|
case 8000:
|
|
case 12000:
|
|
case 16000:
|
|
case 24000:
|
|
case 32000:
|
|
case 48000:
|
|
case 96000:
|
|
case 192000:
|
|
freq = 24576000;
|
|
break;
|
|
case 11025:
|
|
case 22050:
|
|
case 44100:
|
|
freq = 22579200;
|
|
break;
|
|
default:
|
|
dev_err(card->dev, "invalid rate setting\n");
|
|
return -EINVAL;
|
|
}
|
|
|
|
ret = snd_soc_dai_set_sysclk(codec_dai, 0, freq, 0);
|
|
if (ret < 0) {
|
|
dev_err(card->dev, "set codec dai sysclk faided.\n");
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static struct snd_soc_ops sunxi_card_ops = {
|
|
.hw_params = sunxi_card_hw_params,
|
|
};
|
|
|
|
static struct snd_soc_dai_link sunxi_card_dai_link[] = {
|
|
{
|
|
.name = "sun8iw19-codec",
|
|
.stream_name = "SUNXI-CODEC",
|
|
.codec_name = "codec",
|
|
.codec_dai_name = "sun8iw19codec",
|
|
.cpu_dai_name = "codec",
|
|
.platform_name = "codec",
|
|
.init = sunxi_card_init,
|
|
.ops = &sunxi_card_ops,
|
|
},
|
|
};
|
|
|
|
static struct snd_soc_card snd_soc_sunxi_card = {
|
|
.name = "sun8iw19-codec",
|
|
.owner = THIS_MODULE,
|
|
.dai_link = sunxi_card_dai_link,
|
|
.num_links = ARRAY_SIZE(sunxi_card_dai_link),
|
|
.controls = sunxi_card_controls,
|
|
.num_controls = ARRAY_SIZE(sunxi_card_controls),
|
|
.dapm_widgets = sunxi_card_dapm_widgets,
|
|
.num_dapm_widgets = ARRAY_SIZE(sunxi_card_dapm_widgets),
|
|
.dapm_routes = sunxi_card_routes,
|
|
.num_dapm_routes = ARRAY_SIZE(sunxi_card_routes),
|
|
};
|
|
|
|
static int sunxi_card_dev_probe(struct platform_device *pdev)
|
|
{
|
|
int ret = 0;
|
|
struct snd_soc_card *card = &snd_soc_sunxi_card;
|
|
struct sunxi_card_priv *priv;
|
|
|
|
/* register the soc card */
|
|
card->dev = &pdev->dev;
|
|
|
|
priv = devm_kzalloc(&pdev->dev,
|
|
sizeof(struct sunxi_card_priv), GFP_KERNEL);
|
|
if (!priv) {
|
|
dev_err(&pdev->dev, "devm_kzalloc failed %d\n", ret);
|
|
return -ENOMEM;
|
|
}
|
|
priv->card = card;
|
|
|
|
snd_soc_card_set_drvdata(card, priv);
|
|
ret = snd_soc_register_card(card);
|
|
if (ret) {
|
|
dev_err(&pdev->dev, "snd_soc_register_card failed %d\n", ret);
|
|
goto err_devm_kfree;
|
|
}
|
|
|
|
dev_warn(&pdev->dev, "[%s] register card finished.\n", __func__);
|
|
|
|
return 0;
|
|
|
|
err_devm_kfree:
|
|
devm_kfree(&pdev->dev, priv);
|
|
return ret;
|
|
}
|
|
|
|
static int __exit sunxi_card_dev_remove(struct platform_device *pdev)
|
|
{
|
|
struct snd_soc_card *card = platform_get_drvdata(pdev);
|
|
struct sunxi_card_priv *priv = snd_soc_card_get_drvdata(card);
|
|
|
|
snd_soc_unregister_card(card);
|
|
devm_kfree(&pdev->dev, priv);
|
|
|
|
dev_warn(&pdev->dev, "[%s] unregister card finished.\n", __func__);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static const struct of_device_id sunxi_card_of_match[] = {
|
|
{ .compatible = "allwinner,sunxi-codec-machine", },
|
|
{},
|
|
};
|
|
|
|
static struct platform_driver sunxi_machine_driver = {
|
|
.driver = {
|
|
.name = "sunxi-codec-machine",
|
|
.owner = THIS_MODULE,
|
|
.pm = &snd_soc_pm_ops,
|
|
.of_match_table = sunxi_card_of_match,
|
|
},
|
|
.probe = sunxi_card_dev_probe,
|
|
.remove = __exit_p(sunxi_card_dev_remove),
|
|
};
|
|
module_platform_driver(sunxi_machine_driver);
|
|
|
|
MODULE_AUTHOR("yumingfeng <yumingfeng@allwinnertech.com>");
|
|
MODULE_DESCRIPTION("SUNXI Codec Machine ASoC driver");
|
|
MODULE_LICENSE("GPL");
|
|
MODULE_ALIAS("platform:sunxi-codec-machine");
|