diff -drupN a/sound/soc/sunxi/sun8iw18-sndcodec.c b/sound/soc/sunxi/sun8iw18-sndcodec.c --- a/sound/soc/sunxi/sun8iw18-sndcodec.c 1970-01-01 03:00:00.000000000 +0300 +++ b/sound/soc/sunxi/sun8iw18-sndcodec.c 2022-06-12 05:28:14.000000000 +0300 @@ -0,0 +1,416 @@ +/* + * sound\soc\sunxi\sunx8iw18-sndcodec.c + * (C) Copyright 2014-2018 + * allwinner Technology Co., Ltd. + * huangxin + * wolfgang huang + * yumingfeng + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "sunxi_rw_func.h" +#include "sun8iw18-sndcodec.h" + +/* enable the sndcodec suspend */ +static int sunxi_sndcodec_startup(struct snd_pcm_substream *substream) +{ +#ifdef SUNXI_CODEC_MAP_TO_DAUDIO +#ifdef CONFIG_SND_SUNXI_MAD + struct snd_soc_pcm_runtime *rtd = substream->private_data; + struct sunxi_sndcodec_priv *sndcodec_priv = + snd_soc_card_get_drvdata(rtd->card); + + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { + if (sndcodec_priv->mad_priv.mad_bind == 1) + rtd->dai_link->ignore_suspend = 1; + } +#endif +#endif + return 0; +} + +/* disable the sndcodec suspend */ +static void sunxi_sndcodec_shutdown(struct snd_pcm_substream *substream) +{ +#ifdef SUNXI_CODEC_MAP_TO_DAUDIO +#ifdef CONFIG_SND_SUNXI_MAD + struct snd_soc_pcm_runtime *rtd = substream->private_data; + struct sunxi_sndcodec_priv *sndcodec_priv = + snd_soc_card_get_drvdata(rtd->card); + + if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { + if (sndcodec_priv->mad_priv.mad_bind == 1) + rtd->dai_link->ignore_suspend = 0; + } +#endif +#endif +} + +#ifdef SUNXI_CODEC_MAP_TO_DAUDIO +#ifdef CONFIG_SND_SUNXI_MAD +/* mad_bind config*/ +static int sunxi_sndcodec_set_mad_bind(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); + struct sunxi_sndcodec_priv *sndcodec_priv = + snd_soc_card_get_drvdata(card); + + if (sunxi_mad_get_mad_info() == NULL) { + pr_alert("[%s] sunxi_mad driver maybe not be loaded\n", __func__); + sndcodec_priv->mad_priv.mad_bind = 0; + return 0; + } + sndcodec_priv->mad_priv.mad_bind = ucontrol->value.integer.value[0]; + + return 0; +} + +static int sunxi_sndcodec_get_mad_bind(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); + struct sunxi_sndcodec_priv *sndcodec_priv = + snd_soc_card_get_drvdata(card); + + ucontrol->value.integer.value[0] = sndcodec_priv->mad_priv.mad_bind; + + return 0; +} + +static const char *mad_bind_function[] = {"mad_unbind", "mad_bind"}; + +static const struct soc_enum mad_bind_enum[] = { + SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(mad_bind_function), + mad_bind_function), +}; + +/*lpsd channel sel*/ +static int sunxi_sndcodec_i2s_set_lpsd_chan(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); + struct sunxi_sndcodec_priv *sndcodec_priv = + snd_soc_card_get_drvdata(card); + struct sunxi_mad_priv *mad_priv = &(sndcodec_priv->mad_priv); + + mad_priv->lpsd_chan_sel = ucontrol->value.integer.value[0]; + + return 0; +} + +static int sunxi_sndcodec_i2s_get_lpsd_chan(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); + struct sunxi_sndcodec_priv *sndcodec_priv = + snd_soc_card_get_drvdata(card); + struct sunxi_mad_priv *mad_priv = &(sndcodec_priv->mad_priv); + + ucontrol->value.integer.value[0] = mad_priv->lpsd_chan_sel; + + return 0; +} + +static const char * const lpsd_chan_sel_function[] = {"0th_chan", "1st_chan", + "2nd_chan", "3rd_chan", "4th_chan", "5th_chan", + "6th_chan", "7th_chan", "8th_chan"}; + +static const struct soc_enum lpsd_chan_sel_enum[] = { + SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(lpsd_chan_sel_function), + lpsd_chan_sel_function), +}; + +/*mad_standby channel sel*/ +static int sunxi_sndcodec_i2s_set_mad_standby_chan(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); + struct sunxi_sndcodec_priv *sndcodec_priv = + snd_soc_card_get_drvdata(card); + struct sunxi_mad_priv *mad_priv = &(sndcodec_priv->mad_priv); + + mad_priv->mad_standby_chan_sel = ucontrol->value.integer.value[0]; + sunxi_mad_standby_chan_sel(mad_priv->mad_standby_chan_sel); + + return 0; +} + +static int sunxi_sndcodec_i2s_get_mad_standby_chan(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct snd_soc_card *card = snd_kcontrol_chip(kcontrol); + struct sunxi_sndcodec_priv *sndcodec_priv = + snd_soc_card_get_drvdata(card); + struct sunxi_mad_priv *mad_priv = &(sndcodec_priv->mad_priv); + + ucontrol->value.integer.value[0] = mad_priv->mad_standby_chan_sel; + + return 0; +} + +static const char * const mad_standby_chan_sel_function[] = { + "Zero_chan", "Two_chan", "Four_chan"}; + +static const struct soc_enum mad_standby_chan_sel_enum[] = { + SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(mad_standby_chan_sel_function), + mad_standby_chan_sel_function), +}; + +/* daudio machine kcontrols */ +static const struct snd_kcontrol_new sunxi_sndcodec_controls[] = { + +}; +#endif +#endif + +static const struct snd_kcontrol_new sunxi_card_controls[] = { + SOC_DAPM_PIN_SWITCH("External Speaker"), + SOC_DAPM_PIN_SWITCH("Lineout"), + +#ifdef SUNXI_CODEC_MAP_TO_DAUDIO +#ifdef CONFIG_SND_SUNXI_MAD + SOC_ENUM_EXT("sndcodec bind mad Function", mad_bind_enum[0], + sunxi_sndcodec_get_mad_bind, sunxi_sndcodec_set_mad_bind), + SOC_ENUM_EXT("lpsd channel sel Function", lpsd_chan_sel_enum[0], + sunxi_sndcodec_i2s_get_lpsd_chan, sunxi_sndcodec_i2s_set_lpsd_chan), + SOC_ENUM_EXT("mad_standby channel sel Function", + mad_standby_chan_sel_enum[0], + sunxi_sndcodec_i2s_get_mad_standby_chan, + sunxi_sndcodec_i2s_set_mad_standby_chan), +#endif +#endif +}; + +static const struct snd_soc_dapm_widget sunxi_card_dapm_widgets[] = { + SND_SOC_DAPM_MIC("Main Mic", NULL), +}; + +static const struct snd_soc_dapm_route sunxi_card_routes[] = { + {"MainMic Bias", NULL, "Main Mic"}, + + {"External Speaker", NULL, "LINEOUTL"}, + {"External Speaker", NULL, "LINEOUTR"}, + + {"Lineout", NULL, "LINEOUTL"}, + {"Lineout", NULL, "LINEOUTR"}, +}; + +/* + * 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_disable_pin(&codec->component.dapm, "Lineout"); + + snd_soc_dapm_sync(&codec->component.dapm); + 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; + unsigned int clk_div; + unsigned int div_id; + 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; + } + + dev_err(card->dev, "set codec dai sysclk:%d.\n", freq); + 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; + } + + /* set system clk div */ + clk_div = freq / params_rate(params); + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) + div_id = CODEC_DIV_PLAYBACK; + else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) + div_id = CODEC_DIV_CAPTURE; + + ret = snd_soc_dai_set_clkdiv(codec_dai, div_id, clk_div); + if (ret < 0) { + dev_warn(card->dev, "codec_dai set clkdiv failed\n"); + return ret; + } + + + return 0; +} + +static struct snd_soc_ops sunxi_card_ops = { + .hw_params = sunxi_card_hw_params, + .startup = sunxi_sndcodec_startup, + .shutdown = sunxi_sndcodec_shutdown, +}; + +static int sunxi_card_suspend(struct snd_soc_card *card) +{ + return 0; +} + +static int sunxi_card_resume(struct snd_soc_card *card) +{ + return 0; +} + +static struct snd_soc_dai_link sunxi_card_dai_link[] = { + { + .name = "audiocodec", + .stream_name = "SUNXI-CODEC", + .cpu_dai_name = "sunxi-internal-cpudai", + .codec_dai_name = "sun8iw18codec", + .platform_name = "sunxi-internal-cpudai", + .codec_name = "sunxi-internal-codec", + .init = sunxi_card_init, + .ops = &sunxi_card_ops, + }, +}; + +static struct snd_soc_card snd_soc_sunxi_card = { + .name = "audiocodec", + .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), + .suspend_post = sunxi_card_suspend, + .resume_post = sunxi_card_resume, +}; + +static int sunxi_card_dev_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct snd_soc_card *card = &snd_soc_sunxi_card; + struct sunxi_sndcodec_priv *sndcodec_priv; + int ret = 0; + + sndcodec_priv = devm_kzalloc(&pdev->dev, + sizeof(struct sunxi_sndcodec_priv), GFP_KERNEL); + if (!sndcodec_priv) + return -ENOMEM; + + sunxi_card_dai_link[0].cpu_dai_name = NULL; + sunxi_card_dai_link[0].cpu_of_node = of_parse_phandle(np, + "sunxi,cpudai-controller", 0); + if (!sunxi_card_dai_link[0].cpu_of_node) { + dev_err(&pdev->dev, "Property 'sunxi,cpudai-controller' missing or invalid\n"); + ret = -EINVAL; + goto err_devm_kfree; + } else { + sunxi_card_dai_link[0].platform_name = NULL; + sunxi_card_dai_link[0].platform_of_node = + sunxi_card_dai_link[0].cpu_of_node; + } + sunxi_card_dai_link[0].codec_name = NULL; + sunxi_card_dai_link[0].codec_of_node = of_parse_phandle(np, + "sunxi,audio-codec", 0); + if (!sunxi_card_dai_link[0].codec_of_node) { + dev_err(&pdev->dev, "Property 'sunxi,audio-codec' missing or invalid\n"); + ret = -EINVAL; + goto err_devm_kfree; + } + + /* register the soc card */ + card->dev = &pdev->dev; + + snd_soc_card_set_drvdata(card, sndcodec_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; + } + + return 0; + +err_devm_kfree: + devm_kfree(&pdev->dev, sndcodec_priv); + return ret; +} + +static int __exit sunxi_card_dev_remove(struct platform_device *pdev) +{ + struct sunxi_sndcodec_priv *sndcodec_priv = dev_get_drvdata(&pdev->dev); + + snd_soc_unregister_card(&snd_soc_sunxi_card); + devm_kfree(&pdev->dev, sndcodec_priv); + + 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("wolfgang huang "); +MODULE_DESCRIPTION("SUNXI Codec Machine ASoC driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:sunxi-codec-machine");