2 * atmel_wm8904 - Atmel ASoC driver for boards with WM8904 codec.
4 * Copyright (C) 2012 Atmel
6 * Author: Bo Shen <voice.shen@atmel.com>
11 #include <linux/clk.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/pinctrl/consumer.h>
17 #include <sound/soc.h>
19 #include "../codecs/wm8904.h"
20 #include "atmel_ssc_dai.h"
22 #define MCLK_RATE 32768
24 static struct clk
*mclk
;
26 static const struct snd_soc_dapm_widget atmel_asoc_wm8904_dapm_widgets
[] = {
27 SND_SOC_DAPM_HP("Headphone Jack", NULL
),
28 SND_SOC_DAPM_MIC("Mic", NULL
),
29 SND_SOC_DAPM_LINE("Line In Jack", NULL
),
32 static int atmel_asoc_wm8904_hw_params(struct snd_pcm_substream
*substream
,
33 struct snd_pcm_hw_params
*params
)
35 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
36 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
39 ret
= snd_soc_dai_set_pll(codec_dai
, WM8904_FLL_MCLK
, WM8904_FLL_MCLK
,
40 32768, params_rate(params
) * 256);
42 pr_err("%s - failed to set wm8904 codec PLL.", __func__
);
47 * As here wm8904 use FLL output as its system clock
48 * so calling set_sysclk won't care freq parameter
51 ret
= snd_soc_dai_set_sysclk(codec_dai
, WM8904_CLK_FLL
,
54 pr_err("%s -failed to set wm8904 SYSCLK\n", __func__
);
61 static struct snd_soc_ops atmel_asoc_wm8904_ops
= {
62 .hw_params
= atmel_asoc_wm8904_hw_params
,
65 static int atmel_set_bias_level(struct snd_soc_card
*card
,
66 struct snd_soc_dapm_context
*dapm
,
67 enum snd_soc_bias_level level
)
69 if (dapm
->bias_level
== SND_SOC_BIAS_STANDBY
) {
71 case SND_SOC_BIAS_PREPARE
:
72 clk_prepare_enable(mclk
);
74 case SND_SOC_BIAS_OFF
:
75 clk_disable_unprepare(mclk
);
85 static struct snd_soc_dai_link atmel_asoc_wm8904_dailink
= {
87 .stream_name
= "WM8904 PCM",
88 .codec_dai_name
= "wm8904-hifi",
89 .dai_fmt
= SND_SOC_DAIFMT_I2S
90 | SND_SOC_DAIFMT_NB_NF
91 | SND_SOC_DAIFMT_CBM_CFM
,
92 .ops
= &atmel_asoc_wm8904_ops
,
95 static struct snd_soc_card atmel_asoc_wm8904_card
= {
96 .name
= "atmel_asoc_wm8904",
98 .set_bias_level
= atmel_set_bias_level
,
99 .dai_link
= &atmel_asoc_wm8904_dailink
,
101 .dapm_widgets
= atmel_asoc_wm8904_dapm_widgets
,
102 .num_dapm_widgets
= ARRAY_SIZE(atmel_asoc_wm8904_dapm_widgets
),
103 .fully_routed
= true,
106 static int atmel_asoc_wm8904_dt_init(struct platform_device
*pdev
)
108 struct device_node
*np
= pdev
->dev
.of_node
;
109 struct device_node
*codec_np
, *cpu_np
;
110 struct snd_soc_card
*card
= &atmel_asoc_wm8904_card
;
111 struct snd_soc_dai_link
*dailink
= &atmel_asoc_wm8904_dailink
;
115 dev_err(&pdev
->dev
, "only device tree supported\n");
119 ret
= snd_soc_of_parse_card_name(card
, "atmel,model");
121 dev_err(&pdev
->dev
, "failed to parse card name\n");
125 ret
= snd_soc_of_parse_audio_routing(card
, "atmel,audio-routing");
127 dev_err(&pdev
->dev
, "failed to parse audio routing\n");
131 cpu_np
= of_parse_phandle(np
, "atmel,ssc-controller", 0);
133 dev_err(&pdev
->dev
, "failed to get dai and pcm info\n");
137 dailink
->cpu_of_node
= cpu_np
;
138 dailink
->platform_of_node
= cpu_np
;
141 codec_np
= of_parse_phandle(np
, "atmel,audio-codec", 0);
143 dev_err(&pdev
->dev
, "failed to get codec info\n");
147 dailink
->codec_of_node
= codec_np
;
148 of_node_put(codec_np
);
153 static int atmel_asoc_wm8904_probe(struct platform_device
*pdev
)
155 struct snd_soc_card
*card
= &atmel_asoc_wm8904_card
;
156 struct snd_soc_dai_link
*dailink
= &atmel_asoc_wm8904_dailink
;
158 struct pinctrl
*pinctrl
;
161 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
162 if (IS_ERR(pinctrl
)) {
163 dev_err(&pdev
->dev
, "failed to request pinctrl\n");
164 return PTR_ERR(pinctrl
);
167 card
->dev
= &pdev
->dev
;
168 ret
= atmel_asoc_wm8904_dt_init(pdev
);
170 dev_err(&pdev
->dev
, "failed to init dt info\n");
174 id
= of_alias_get_id((struct device_node
*)dailink
->cpu_of_node
, "ssc");
175 ret
= atmel_ssc_set_audio(id
);
177 dev_err(&pdev
->dev
, "failed to set SSC %d for audio\n", id
);
181 mclk
= clk_get(NULL
, "pck0");
183 dev_err(&pdev
->dev
, "failed to get pck0\n");
188 clk_src
= clk_get(NULL
, "clk32k");
189 if (IS_ERR(clk_src
)) {
190 dev_err(&pdev
->dev
, "failed to get clk32k\n");
191 ret
= PTR_ERR(clk_src
);
195 ret
= clk_set_parent(mclk
, clk_src
);
198 dev_err(&pdev
->dev
, "failed to set MCLK parent\n");
202 dev_info(&pdev
->dev
, "setting pck0 to %dHz\n", MCLK_RATE
);
203 clk_set_rate(mclk
, MCLK_RATE
);
205 ret
= snd_soc_register_card(card
);
207 dev_err(&pdev
->dev
, "snd_soc_register_card failed\n");
214 atmel_ssc_put_audio(id
);
218 static int atmel_asoc_wm8904_remove(struct platform_device
*pdev
)
220 struct snd_soc_card
*card
= platform_get_drvdata(pdev
);
221 struct snd_soc_dai_link
*dailink
= &atmel_asoc_wm8904_dailink
;
224 id
= of_alias_get_id((struct device_node
*)dailink
->cpu_of_node
, "ssc");
226 snd_soc_unregister_card(card
);
227 atmel_ssc_put_audio(id
);
233 static const struct of_device_id atmel_asoc_wm8904_dt_ids
[] = {
234 { .compatible
= "atmel,asoc-wm8904", },
239 static struct platform_driver atmel_asoc_wm8904_driver
= {
241 .name
= "atmel-wm8904-audio",
242 .owner
= THIS_MODULE
,
243 .of_match_table
= of_match_ptr(atmel_asoc_wm8904_dt_ids
),
245 .probe
= atmel_asoc_wm8904_probe
,
246 .remove
= atmel_asoc_wm8904_remove
,
249 module_platform_driver(atmel_asoc_wm8904_driver
);
251 /* Module information */
252 MODULE_AUTHOR("Bo Shen <voice.shen@atmel.com>");
253 MODULE_DESCRIPTION("ALSA SoC machine driver for Atmel EK with WM8904 codec");
254 MODULE_LICENSE("GPL");