xref: /OK3568_Linux_fs/kernel/sound/soc/atmel/atmel_wm8904.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * atmel_wm8904 - Atmel ASoC driver for boards with WM8904 codec.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2012 Atmel
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Author: Bo Shen <voice.shen@atmel.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/clk.h>
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/of.h>
13*4882a593Smuzhiyun #include <linux/of_device.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include <sound/soc.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "../codecs/wm8904.h"
18*4882a593Smuzhiyun #include "atmel_ssc_dai.h"
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun static const struct snd_soc_dapm_widget atmel_asoc_wm8904_dapm_widgets[] = {
21*4882a593Smuzhiyun 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
22*4882a593Smuzhiyun 	SND_SOC_DAPM_MIC("Mic", NULL),
23*4882a593Smuzhiyun 	SND_SOC_DAPM_LINE("Line In Jack", NULL),
24*4882a593Smuzhiyun };
25*4882a593Smuzhiyun 
atmel_asoc_wm8904_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)26*4882a593Smuzhiyun static int atmel_asoc_wm8904_hw_params(struct snd_pcm_substream *substream,
27*4882a593Smuzhiyun 		struct snd_pcm_hw_params *params)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
30*4882a593Smuzhiyun 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
31*4882a593Smuzhiyun 	int ret;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	ret = snd_soc_dai_set_pll(codec_dai, WM8904_FLL_MCLK, WM8904_FLL_MCLK,
34*4882a593Smuzhiyun 		32768, params_rate(params) * 256);
35*4882a593Smuzhiyun 	if (ret < 0) {
36*4882a593Smuzhiyun 		pr_err("%s - failed to set wm8904 codec PLL.", __func__);
37*4882a593Smuzhiyun 		return ret;
38*4882a593Smuzhiyun 	}
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun 	/*
41*4882a593Smuzhiyun 	 * As here wm8904 use FLL output as its system clock
42*4882a593Smuzhiyun 	 * so calling set_sysclk won't care freq parameter
43*4882a593Smuzhiyun 	 * then we pass 0
44*4882a593Smuzhiyun 	 */
45*4882a593Smuzhiyun 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8904_CLK_FLL,
46*4882a593Smuzhiyun 			0, SND_SOC_CLOCK_IN);
47*4882a593Smuzhiyun 	if (ret < 0) {
48*4882a593Smuzhiyun 		pr_err("%s -failed to set wm8904 SYSCLK\n", __func__);
49*4882a593Smuzhiyun 		return ret;
50*4882a593Smuzhiyun 	}
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	return 0;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun static const struct snd_soc_ops atmel_asoc_wm8904_ops = {
56*4882a593Smuzhiyun 	.hw_params = atmel_asoc_wm8904_hw_params,
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun SND_SOC_DAILINK_DEFS(pcm,
60*4882a593Smuzhiyun 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
61*4882a593Smuzhiyun 	DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "wm8904-hifi")),
62*4882a593Smuzhiyun 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun static struct snd_soc_dai_link atmel_asoc_wm8904_dailink = {
65*4882a593Smuzhiyun 	.name = "WM8904",
66*4882a593Smuzhiyun 	.stream_name = "WM8904 PCM",
67*4882a593Smuzhiyun 	.dai_fmt = SND_SOC_DAIFMT_I2S
68*4882a593Smuzhiyun 		| SND_SOC_DAIFMT_NB_NF
69*4882a593Smuzhiyun 		| SND_SOC_DAIFMT_CBM_CFM,
70*4882a593Smuzhiyun 	.ops = &atmel_asoc_wm8904_ops,
71*4882a593Smuzhiyun 	SND_SOC_DAILINK_REG(pcm),
72*4882a593Smuzhiyun };
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun static struct snd_soc_card atmel_asoc_wm8904_card = {
75*4882a593Smuzhiyun 	.name = "atmel_asoc_wm8904",
76*4882a593Smuzhiyun 	.owner = THIS_MODULE,
77*4882a593Smuzhiyun 	.dai_link = &atmel_asoc_wm8904_dailink,
78*4882a593Smuzhiyun 	.num_links = 1,
79*4882a593Smuzhiyun 	.dapm_widgets = atmel_asoc_wm8904_dapm_widgets,
80*4882a593Smuzhiyun 	.num_dapm_widgets = ARRAY_SIZE(atmel_asoc_wm8904_dapm_widgets),
81*4882a593Smuzhiyun 	.fully_routed = true,
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
atmel_asoc_wm8904_dt_init(struct platform_device * pdev)84*4882a593Smuzhiyun static int atmel_asoc_wm8904_dt_init(struct platform_device *pdev)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	struct device_node *np = pdev->dev.of_node;
87*4882a593Smuzhiyun 	struct device_node *codec_np, *cpu_np;
88*4882a593Smuzhiyun 	struct snd_soc_card *card = &atmel_asoc_wm8904_card;
89*4882a593Smuzhiyun 	struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
90*4882a593Smuzhiyun 	int ret;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	if (!np) {
93*4882a593Smuzhiyun 		dev_err(&pdev->dev, "only device tree supported\n");
94*4882a593Smuzhiyun 		return -EINVAL;
95*4882a593Smuzhiyun 	}
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	ret = snd_soc_of_parse_card_name(card, "atmel,model");
98*4882a593Smuzhiyun 	if (ret) {
99*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to parse card name\n");
100*4882a593Smuzhiyun 		return ret;
101*4882a593Smuzhiyun 	}
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	ret = snd_soc_of_parse_audio_routing(card, "atmel,audio-routing");
104*4882a593Smuzhiyun 	if (ret) {
105*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to parse audio routing\n");
106*4882a593Smuzhiyun 		return ret;
107*4882a593Smuzhiyun 	}
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
110*4882a593Smuzhiyun 	if (!cpu_np) {
111*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to get dai and pcm info\n");
112*4882a593Smuzhiyun 		ret = -EINVAL;
113*4882a593Smuzhiyun 		return ret;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 	dailink->cpus->of_node = cpu_np;
116*4882a593Smuzhiyun 	dailink->platforms->of_node = cpu_np;
117*4882a593Smuzhiyun 	of_node_put(cpu_np);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
120*4882a593Smuzhiyun 	if (!codec_np) {
121*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to get codec info\n");
122*4882a593Smuzhiyun 		ret = -EINVAL;
123*4882a593Smuzhiyun 		return ret;
124*4882a593Smuzhiyun 	}
125*4882a593Smuzhiyun 	dailink->codecs->of_node = codec_np;
126*4882a593Smuzhiyun 	of_node_put(codec_np);
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	return 0;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
atmel_asoc_wm8904_probe(struct platform_device * pdev)131*4882a593Smuzhiyun static int atmel_asoc_wm8904_probe(struct platform_device *pdev)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	struct snd_soc_card *card = &atmel_asoc_wm8904_card;
134*4882a593Smuzhiyun 	struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
135*4882a593Smuzhiyun 	int id, ret;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	card->dev = &pdev->dev;
138*4882a593Smuzhiyun 	ret = atmel_asoc_wm8904_dt_init(pdev);
139*4882a593Smuzhiyun 	if (ret) {
140*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to init dt info\n");
141*4882a593Smuzhiyun 		return ret;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	id = of_alias_get_id((struct device_node *)dailink->cpus->of_node, "ssc");
145*4882a593Smuzhiyun 	ret = atmel_ssc_set_audio(id);
146*4882a593Smuzhiyun 	if (ret != 0) {
147*4882a593Smuzhiyun 		dev_err(&pdev->dev, "failed to set SSC %d for audio\n", id);
148*4882a593Smuzhiyun 		return ret;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	ret = snd_soc_register_card(card);
152*4882a593Smuzhiyun 	if (ret) {
153*4882a593Smuzhiyun 		dev_err(&pdev->dev, "snd_soc_register_card failed\n");
154*4882a593Smuzhiyun 		goto err_set_audio;
155*4882a593Smuzhiyun 	}
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	return 0;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun err_set_audio:
160*4882a593Smuzhiyun 	atmel_ssc_put_audio(id);
161*4882a593Smuzhiyun 	return ret;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
atmel_asoc_wm8904_remove(struct platform_device * pdev)164*4882a593Smuzhiyun static int atmel_asoc_wm8904_remove(struct platform_device *pdev)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun 	struct snd_soc_card *card = platform_get_drvdata(pdev);
167*4882a593Smuzhiyun 	struct snd_soc_dai_link *dailink = &atmel_asoc_wm8904_dailink;
168*4882a593Smuzhiyun 	int id;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	id = of_alias_get_id((struct device_node *)dailink->cpus->of_node, "ssc");
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	snd_soc_unregister_card(card);
173*4882a593Smuzhiyun 	atmel_ssc_put_audio(id);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	return 0;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun #ifdef CONFIG_OF
179*4882a593Smuzhiyun static const struct of_device_id atmel_asoc_wm8904_dt_ids[] = {
180*4882a593Smuzhiyun 	{ .compatible = "atmel,asoc-wm8904", },
181*4882a593Smuzhiyun 	{ }
182*4882a593Smuzhiyun };
183*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, atmel_asoc_wm8904_dt_ids);
184*4882a593Smuzhiyun #endif
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun static struct platform_driver atmel_asoc_wm8904_driver = {
187*4882a593Smuzhiyun 	.driver = {
188*4882a593Smuzhiyun 		.name = "atmel-wm8904-audio",
189*4882a593Smuzhiyun 		.of_match_table = of_match_ptr(atmel_asoc_wm8904_dt_ids),
190*4882a593Smuzhiyun 		.pm		= &snd_soc_pm_ops,
191*4882a593Smuzhiyun 	},
192*4882a593Smuzhiyun 	.probe = atmel_asoc_wm8904_probe,
193*4882a593Smuzhiyun 	.remove = atmel_asoc_wm8904_remove,
194*4882a593Smuzhiyun };
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun module_platform_driver(atmel_asoc_wm8904_driver);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun /* Module information */
199*4882a593Smuzhiyun MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
200*4882a593Smuzhiyun MODULE_DESCRIPTION("ALSA SoC machine driver for Atmel EK with WM8904 codec");
201*4882a593Smuzhiyun MODULE_LICENSE("GPL");
202