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