2 * rk_rk312x.c -- SoC audio for rockchip
4 * Driver for rockchip rk312x audio
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/device.h>
17 #include <linux/of_gpio.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/soc.h>
21 #include <sound/soc-dapm.h>
23 #include "../codecs/rk312x_codec.h"
24 #include "card_info.h"
29 #define DBG(x...) pr_info("rk_rk312x" x)
34 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
35 SND_SOC_DAPM_MIC("Mic Jack", NULL),
36 SND_SOC_DAPM_MIC("Headset Jack", NULL),
37 SND_SOC_DAPM_SPK("Ext Spk", NULL),
38 SND_SOC_DAPM_HP("Headphone Jack", NULL),
41 static const struct snd_soc_dapm_route rk_audio_map[] = {
42 /* Mic Jack --> MIC_IN*/
43 {"Mic Bias", NULL, "Mic Jack"},
44 {"MICP", NULL, "Mic Bias"},
45 {"MICN", NULL, "Mic Bias"},
48 {"Mic Bias", NULL, "Headset Jack"},
50 {"Ext Spk", NULL, "HPOUTR"},
51 {"Ext Spk", NULL, "HPOUTL"},
53 {"Headphone Jack", NULL, "HPOUTR"},
54 {"Headphone Jack", NULL, "HPOUTL"},
57 static const struct snd_kcontrol_new rk_controls[] = {
58 SOC_DAPM_PIN_SWITCH("Mic Jack"),
59 SOC_DAPM_PIN_SWITCH("Headset Jack"),
60 SOC_DAPM_PIN_SWITCH("Ext Spk"),
61 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
64 static int rk312x_init(struct snd_soc_pcm_runtime *rtd)
66 struct snd_soc_codec *codec = rtd->codec;
67 struct snd_soc_dapm_context *dapm = &codec->dapm;
69 DBG("Enter::%s----%d\n", __func__, __LINE__);
71 mutex_lock(&dapm->card->dapm_mutex);
73 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
74 snd_soc_dapm_enable_pin(dapm, "Headset Jack");
75 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
76 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
78 mutex_unlock(&dapm->card->dapm_mutex);
80 snd_soc_dapm_sync(dapm);
85 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
86 struct snd_pcm_hw_params *params)
88 struct snd_soc_pcm_runtime *rtd = substream->private_data;
89 struct snd_soc_dai *codec_dai = rtd->codec_dai;
90 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
91 unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
94 DBG("Enter::%s----%d\n", __func__, __LINE__);
96 /* set codec DAI configuration */
97 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
99 DBG("%s():failed to set the format for codec side\n",
104 /* set cpu DAI configuration */
105 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
107 DBG("%s():failed to set the format for cpu side\n",
112 switch (params_rate(params)) {
126 DBG("Enter:%s, %d, Error rate=%d\n",
127 __func__, __LINE__, params_rate(params));
132 DBG("Enter:%s, %d, rate=%d\n",
133 __func__, __LINE__, params_rate(params));
135 /*Set the system clk for codec*/
136 ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
138 DBG("rk_hifi_hw_params:failed to set the sysclk for codec\n");
142 snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
143 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK,
144 (pll_out/4)/params_rate(params)-1);
145 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);
146 DBG("Enter:%s, %d, pll_out/4/params_rate(params) = %d \n",
147 __func__, __LINE__, (pll_out/4)/params_rate(params));
152 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
153 struct snd_pcm_hw_params *params)
155 struct snd_soc_pcm_runtime *rtd = substream->private_data;
156 struct snd_soc_dai *codec_dai = rtd->codec_dai;
157 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
158 unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
161 DBG("Enter::%s----%d\n", __func__, __LINE__);
163 /* set codec DAI configuration */
164 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
166 DBG("%s():failed to set the format for codec side\n",
171 /* set codec DAI configuration */
172 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A |
173 SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS);
175 switch (params_rate(params)) {
189 DBG("Enter:%s, %d, Error rate=%d\n",
191 params_rate(params));
196 /*Set the system clk for codec*/
197 ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
200 DBG("rk_voice_hw_params:failed to set codec side\n");
204 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
209 static struct snd_soc_ops rk312x_hifi_ops = {
210 .hw_params = rk_hifi_hw_params,
213 static struct snd_soc_ops rk312x_voice_ops = {
214 .hw_params = rk_voice_hw_params,
217 static struct snd_soc_dai_link rk_dai[] = {
219 .name = "RK312X I2S1",
220 .stream_name = "RK312X PCM",
221 .codec_dai_name = "rk312x-hifi",
223 .ops = &rk312x_hifi_ops,
226 .name = "RK312X I2S2",
227 .stream_name = "RK312X PCM",
228 .codec_dai_name = "rk312x-voice",
229 .ops = &rk312x_voice_ops,
233 static struct snd_soc_card rockchip_rk312x_snd_card = {
237 .controls = rk_controls,
238 .num_controls = ARRAY_SIZE(rk_controls),
239 .dapm_widgets = rk_dapm_widgets,
240 .num_dapm_widgets = ARRAY_SIZE(rk_dapm_widgets),
241 .dapm_routes = rk_audio_map,
242 .num_dapm_routes = ARRAY_SIZE(rk_audio_map),
245 static int rockchip_rk312x_audio_probe(struct platform_device *pdev)
248 struct snd_soc_card *card = &rockchip_rk312x_snd_card;
250 card->dev = &pdev->dev;
251 ret = rockchip_of_get_sound_card_info(card);
253 DBG("%s() get sound card info failed:%d\n", __func__, ret);
257 ret = snd_soc_register_card(card);
259 DBG("%s() register card failed:%d\n", __func__, ret);
264 static int rockchip_rk312x_audio_remove(struct platform_device *pdev)
266 struct snd_soc_card *card = platform_get_drvdata(pdev);
268 snd_soc_unregister_card(card);
274 static const struct of_device_id rockchip_rk312x_of_match[] = {
275 { .compatible = "audio-rk312x", },
278 MODULE_DEVICE_TABLE(of, rockchip_rk312x_of_match);
279 #endif /* CONFIG_OF */
281 static struct platform_driver rockchip_rk312x_audio_driver = {
283 .name = "audio-rk312x",
284 .owner = THIS_MODULE,
285 .pm = &snd_soc_pm_ops,
286 .of_match_table = of_match_ptr(rockchip_rk312x_of_match),
288 .probe = rockchip_rk312x_audio_probe,
289 .remove = rockchip_rk312x_audio_remove,
292 module_platform_driver(rockchip_rk312x_audio_driver);
294 /* Module information */
295 MODULE_AUTHOR("rockchip");
296 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
297 MODULE_LICENSE("GPL");