xref: /openbmc/linux/sound/soc/atmel/atmel_wm8904.c (revision 0278eb32)
137fa65d0SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
252f19b14SBo Shen /*
352f19b14SBo Shen  * atmel_wm8904 - Atmel ASoC driver for boards with WM8904 codec.
452f19b14SBo Shen  *
552f19b14SBo Shen  * Copyright (C) 2012 Atmel
652f19b14SBo Shen  *
752f19b14SBo Shen  * Author: Bo Shen <voice.shen@atmel.com>
852f19b14SBo Shen  */
952f19b14SBo Shen 
1052f19b14SBo Shen #include <linux/clk.h>
1152f19b14SBo Shen #include <linux/module.h>
1252f19b14SBo Shen #include <linux/of.h>
1352f19b14SBo Shen #include <linux/of_device.h>
1452f19b14SBo Shen 
1552f19b14SBo Shen #include <sound/soc.h>
1652f19b14SBo Shen 
1752f19b14SBo Shen #include "../codecs/wm8904.h"
1852f19b14SBo Shen #include "atmel_ssc_dai.h"
1952f19b14SBo Shen 
2052f19b14SBo Shen static const struct snd_soc_dapm_widget atmel_asoc_wm8904_dapm_widgets[] = {
2152f19b14SBo Shen 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
2252f19b14SBo Shen 	SND_SOC_DAPM_MIC("Mic", NULL),
2352f19b14SBo Shen 	SND_SOC_DAPM_LINE("Line In Jack", NULL),
2452f19b14SBo Shen };
2552f19b14SBo Shen 
atmel_asoc_wm8904_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)2652f19b14SBo Shen static int atmel_asoc_wm8904_hw_params(struct snd_pcm_substream *substream,
2752f19b14SBo Shen 		struct snd_pcm_hw_params *params)
2852f19b14SBo Shen {
29b1839ebfSKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
30b434d707SKuninori Morimoto 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
3152f19b14SBo Shen 	int ret;
3252f19b14SBo Shen 
3352f19b14SBo Shen 	ret = snd_soc_dai_set_pll(codec_dai, WM8904_FLL_MCLK, WM8904_FLL_MCLK,
3452f19b14SBo Shen 		32768, params_rate(params) * 256);
3552f19b14SBo Shen 	if (ret < 0) {
3652f19b14SBo Shen 		pr_err("%s - failed to set wm8904 codec PLL.", __func__);
3752f19b14SBo Shen 		return ret;
3852f19b14SBo Shen 	}
3952f19b14SBo Shen 
4052f19b14SBo Shen 	/*
4152f19b14SBo Shen 	 * As here wm8904 use FLL output as its system clock
4252f19b14SBo Shen 	 * so calling set_sysclk won't care freq parameter
4352f19b14SBo Shen 	 * then we pass 0
4452f19b14SBo Shen 	 */
4552f19b14SBo Shen 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8904_CLK_FLL,
4652f19b14SBo Shen 			0, SND_SOC_CLOCK_IN);
4752f19b14SBo Shen 	if (ret < 0) {
4852f19b14SBo Shen 		pr_err("%s -failed to set wm8904 SYSCLK\n", __func__);
4952f19b14SBo Shen 		return ret;
5052f19b14SBo Shen 	}
5152f19b14SBo Shen 
5252f19b14SBo Shen 	return 0;
5352f19b14SBo Shen }
5452f19b14SBo Shen 
5528549313SJulia Lawall static const struct snd_soc_ops atmel_asoc_wm8904_ops = {
5652f19b14SBo Shen 	.hw_params = atmel_asoc_wm8904_hw_params,
5752f19b14SBo Shen };
5852f19b14SBo Shen 
59079878beSKuninori Morimoto SND_SOC_DAILINK_DEFS(pcm,
60079878beSKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
6116589b77SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8904-hifi")),
6216589b77SKuninori Morimoto 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
63079878beSKuninori Morimoto 
6452f19b14SBo Shen static struct snd_soc_dai_link atmel_asoc_wm8904_dailink = {
6552f19b14SBo Shen 	.name = "WM8904",
6652f19b14SBo Shen 	.stream_name = "WM8904 PCM",
6752f19b14SBo Shen 	.dai_fmt = SND_SOC_DAIFMT_I2S
6852f19b14SBo Shen 		| SND_SOC_DAIFMT_NB_NF
694a8cf938SMark Brown 		| SND_SOC_DAIFMT_CBP_CFP,
7052f19b14SBo Shen 	.ops = &atmel_asoc_wm8904_ops,
71079878beSKuninori Morimoto 	SND_SOC_DAILINK_REG(pcm),
7252f19b14SBo Shen };
7352f19b14SBo Shen 
7452f19b14SBo Shen static struct snd_soc_card atmel_asoc_wm8904_card = {
7552f19b14SBo Shen 	.name = "atmel_asoc_wm8904",
7652f19b14SBo Shen 	.owner = THIS_MODULE,
7752f19b14SBo Shen 	.dai_link = &atmel_asoc_wm8904_dailink,
7852f19b14SBo Shen 	.num_links = 1,
7952f19b14SBo Shen 	.dapm_widgets = atmel_asoc_wm8904_dapm_widgets,
8052f19b14SBo Shen 	.num_dapm_widgets = ARRAY_SIZE(atmel_asoc_wm8904_dapm_widgets),
8152f19b14SBo Shen 	.fully_routed = true,
8252f19b14SBo Shen };
8352f19b14SBo Shen 
atmel_asoc_wm8904_dt_init(struct platform_device * pdev)8452f19b14SBo Shen static int atmel_asoc_wm8904_dt_init(struct platform_device *pdev)
8552f19b14SBo Shen {
8652f19b14SBo Shen 	struct device_node *np = pdev->dev.of_node;
8752f19b14SBo Shen 	struct device_node *codec_np, *cpu_np;
8852f19b14SBo Shen 	struct snd_soc_card *card = &atmel_asoc_wm8904_card;
8952f19b14SBo Shen 	struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
9052f19b14SBo Shen 	int ret;
9152f19b14SBo Shen 
9252f19b14SBo Shen 	if (!np) {
9352f19b14SBo Shen 		dev_err(&pdev->dev, "only device tree supported\n");
9452f19b14SBo Shen 		return -EINVAL;
9552f19b14SBo Shen 	}
9652f19b14SBo Shen 
9752f19b14SBo Shen 	ret = snd_soc_of_parse_card_name(card, "atmel,model");
9852f19b14SBo Shen 	if (ret) {
9952f19b14SBo Shen 		dev_err(&pdev->dev, "failed to parse card name\n");
10052f19b14SBo Shen 		return ret;
10152f19b14SBo Shen 	}
10252f19b14SBo Shen 
10352f19b14SBo Shen 	ret = snd_soc_of_parse_audio_routing(card, "atmel,audio-routing");
10452f19b14SBo Shen 	if (ret) {
10552f19b14SBo Shen 		dev_err(&pdev->dev, "failed to parse audio routing\n");
10652f19b14SBo Shen 		return ret;
10752f19b14SBo Shen 	}
10852f19b14SBo Shen 
10952f19b14SBo Shen 	cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
11052f19b14SBo Shen 	if (!cpu_np) {
11152f19b14SBo Shen 		dev_err(&pdev->dev, "failed to get dai and pcm info\n");
11252f19b14SBo Shen 		ret = -EINVAL;
11352f19b14SBo Shen 		return ret;
11452f19b14SBo Shen 	}
115079878beSKuninori Morimoto 	dailink->cpus->of_node = cpu_np;
11616589b77SKuninori Morimoto 	dailink->platforms->of_node = cpu_np;
11752f19b14SBo Shen 	of_node_put(cpu_np);
11852f19b14SBo Shen 
11952f19b14SBo Shen 	codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
12052f19b14SBo Shen 	if (!codec_np) {
12152f19b14SBo Shen 		dev_err(&pdev->dev, "failed to get codec info\n");
12252f19b14SBo Shen 		ret = -EINVAL;
12352f19b14SBo Shen 		return ret;
12452f19b14SBo Shen 	}
125079878beSKuninori Morimoto 	dailink->codecs->of_node = codec_np;
12652f19b14SBo Shen 	of_node_put(codec_np);
12752f19b14SBo Shen 
12852f19b14SBo Shen 	return 0;
12952f19b14SBo Shen }
13052f19b14SBo Shen 
atmel_asoc_wm8904_probe(struct platform_device * pdev)13152f19b14SBo Shen static int atmel_asoc_wm8904_probe(struct platform_device *pdev)
13252f19b14SBo Shen {
13352f19b14SBo Shen 	struct snd_soc_card *card = &atmel_asoc_wm8904_card;
13452f19b14SBo Shen 	struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
13552f19b14SBo Shen 	int id, ret;
13652f19b14SBo Shen 
13752f19b14SBo Shen 	card->dev = &pdev->dev;
13852f19b14SBo Shen 	ret = atmel_asoc_wm8904_dt_init(pdev);
13952f19b14SBo Shen 	if (ret) {
14052f19b14SBo Shen 		dev_err(&pdev->dev, "failed to init dt info\n");
14152f19b14SBo Shen 		return ret;
14252f19b14SBo Shen 	}
14352f19b14SBo Shen 
144079878beSKuninori Morimoto 	id = of_alias_get_id((struct device_node *)dailink->cpus->of_node, "ssc");
14552f19b14SBo Shen 	ret = atmel_ssc_set_audio(id);
14652f19b14SBo Shen 	if (ret != 0) {
14752f19b14SBo Shen 		dev_err(&pdev->dev, "failed to set SSC %d for audio\n", id);
14852f19b14SBo Shen 		return ret;
14952f19b14SBo Shen 	}
15052f19b14SBo Shen 
15152f19b14SBo Shen 	ret = snd_soc_register_card(card);
15252f19b14SBo Shen 	if (ret) {
15352f19b14SBo Shen 		dev_err(&pdev->dev, "snd_soc_register_card failed\n");
15452f19b14SBo Shen 		goto err_set_audio;
15552f19b14SBo Shen 	}
15652f19b14SBo Shen 
15752f19b14SBo Shen 	return 0;
15852f19b14SBo Shen 
15952f19b14SBo Shen err_set_audio:
16052f19b14SBo Shen 	atmel_ssc_put_audio(id);
16152f19b14SBo Shen 	return ret;
16252f19b14SBo Shen }
16352f19b14SBo Shen 
atmel_asoc_wm8904_remove(struct platform_device * pdev)164*0278eb32SUwe Kleine-König static void atmel_asoc_wm8904_remove(struct platform_device *pdev)
16552f19b14SBo Shen {
16652f19b14SBo Shen 	struct snd_soc_card *card = platform_get_drvdata(pdev);
16752f19b14SBo Shen 	struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
16852f19b14SBo Shen 	int id;
16952f19b14SBo Shen 
170079878beSKuninori Morimoto 	id = of_alias_get_id((struct device_node *)dailink->cpus->of_node, "ssc");
17152f19b14SBo Shen 
17252f19b14SBo Shen 	snd_soc_unregister_card(card);
17352f19b14SBo Shen 	atmel_ssc_put_audio(id);
17452f19b14SBo Shen }
17552f19b14SBo Shen 
17652f19b14SBo Shen #ifdef CONFIG_OF
17752f19b14SBo Shen static const struct of_device_id atmel_asoc_wm8904_dt_ids[] = {
17852f19b14SBo Shen 	{ .compatible = "atmel,asoc-wm8904", },
17952f19b14SBo Shen 	{ }
18052f19b14SBo Shen };
181aab7826fSLuis de Bethencourt MODULE_DEVICE_TABLE(of, atmel_asoc_wm8904_dt_ids);
18252f19b14SBo Shen #endif
18352f19b14SBo Shen 
18452f19b14SBo Shen static struct platform_driver atmel_asoc_wm8904_driver = {
18552f19b14SBo Shen 	.driver = {
18652f19b14SBo Shen 		.name = "atmel-wm8904-audio",
18752f19b14SBo Shen 		.of_match_table = of_match_ptr(atmel_asoc_wm8904_dt_ids),
18850860e1dSSongjun Wu 		.pm		= &snd_soc_pm_ops,
18952f19b14SBo Shen 	},
19052f19b14SBo Shen 	.probe = atmel_asoc_wm8904_probe,
191*0278eb32SUwe Kleine-König 	.remove_new = atmel_asoc_wm8904_remove,
19252f19b14SBo Shen };
19352f19b14SBo Shen 
19452f19b14SBo Shen module_platform_driver(atmel_asoc_wm8904_driver);
19552f19b14SBo Shen 
19652f19b14SBo Shen /* Module information */
19752f19b14SBo Shen MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
19852f19b14SBo Shen MODULE_DESCRIPTION("ALSA SoC machine driver for Atmel EK with WM8904 codec");
19952f19b14SBo Shen MODULE_LICENSE("GPL");
200