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