2 * rk_rk616.c -- SoC audio for rockchip
4 * Driver for rockchip rk616 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/rk616_codec.h"
28 #define DBG(x...) printk(KERN_INFO x)
33 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
34 SND_SOC_DAPM_MIC("Mic Jack", NULL),
35 SND_SOC_DAPM_MIC("Headset Jack", NULL),
36 SND_SOC_DAPM_SPK("Ext Spk", NULL),
37 SND_SOC_DAPM_HP("Headphone Jack", NULL),
40 static const struct snd_soc_dapm_route rk_audio_map[]={
42 /* Mic Jack --> MIC_IN*/
43 {"Mic1 Bias", NULL, "Mic Jack"},
44 {"MIC1P", NULL, "Mic1 Bias"},
45 {"MIC1N", NULL, "Mic1 Bias"},
48 {"Mic2 Bias", NULL, "Headset Jack"},
49 {"MIC2P", NULL, "Mic2 Bias"},
50 {"MIC2N", NULL, "Mic2 Bias"},
52 {"Ext Spk", NULL, "SPKOUTR"},
53 {"Ext Spk", NULL, "SPKOUTL"},
55 {"Headphone Jack", NULL, "HPOUTR"},
56 {"Headphone Jack", NULL, "HPOUTL"},
59 static const struct snd_kcontrol_new rk_controls[] = {
60 SOC_DAPM_PIN_SWITCH("Mic Jack"),
61 SOC_DAPM_PIN_SWITCH("Headset Jack"),
62 SOC_DAPM_PIN_SWITCH("Ext Spk"),
63 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
66 static int rk616_init(struct snd_soc_pcm_runtime *rtd)
68 struct snd_soc_codec *codec = rtd->codec;
69 struct snd_soc_dapm_context *dapm = &codec->dapm;
71 // if is for mid that using tiny alsa,
72 // it don't need this controls and route, so return.
73 if (rk616_get_for_mid())
76 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
78 snd_soc_add_codec_controls(codec, rk_controls,
79 ARRAY_SIZE(rk_controls));
81 /* Add specific widgets */
82 snd_soc_dapm_new_controls(dapm, rk_dapm_widgets,
83 ARRAY_SIZE(rk_dapm_widgets));
84 /* Set up specific audio path audio_mapnects */
85 snd_soc_dapm_add_routes(dapm, rk_audio_map, ARRAY_SIZE(rk_audio_map));
87 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
88 snd_soc_dapm_enable_pin(dapm, "Headset Jack");
89 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
90 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
92 snd_soc_dapm_sync(dapm);
97 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
98 struct snd_pcm_hw_params *params)
100 struct snd_soc_pcm_runtime *rtd = substream->private_data;
101 struct snd_soc_dai *codec_dai = rtd->codec_dai;
102 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
103 unsigned int pll_out = 0, div = 4;
106 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
108 /* set codec DAI configuration */
109 #if defined (CONFIG_SND_RK_CODEC_SOC_SLAVE)
111 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
112 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
114 #if defined (CONFIG_SND_RK_CODEC_SOC_MASTER)
116 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
117 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM );
122 /* set cpu DAI configuration */
123 #if defined (CONFIG_SND_RK_CODEC_SOC_SLAVE)
124 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
125 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
127 #if defined (CONFIG_SND_RK_CODEC_SOC_MASTER)
128 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
129 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
134 switch(params_rate(params)) {
151 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
156 DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
158 #if defined(CONFIG_RK616_USE_MCLK_12M)
159 /* MCLK must be 12M when RK616 HDMI is in */
160 if (get_hdmi_state() && pll_out != 12000000) {
161 DBG("%s : HDMI is in, don't set sys clk %u\n",__FUNCTION__, pll_out);
166 /* Set the system clk for codec
167 mclk will be setted in set_sysclk of codec_dai*/
168 ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
170 DBG("rk_hifi_hw_params:failed to set the sysclk for codec side\n");
173 #if defined(CONFIG_RK616_USE_MCLK_12M)
176 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out / div)/params_rate(params)-1);
177 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, div - 1);
179 DBG("Enter:%s, %d, pll_out/div/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/div)/params_rate(params));
184 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
185 struct snd_pcm_hw_params *params)
187 struct snd_soc_pcm_runtime *rtd = substream->private_data;
188 struct snd_soc_dai *codec_dai = rtd->codec_dai;
189 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
190 unsigned int pll_out = 0;
193 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
195 /* set codec DAI configuration */
196 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A |
197 SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS);
199 switch(params_rate(params)) {
213 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
218 /* MCLK must be 12M when RK616 HDMI is in */
219 #if defined(CONFIG_RK616_USE_MCLK_12M)
220 if (get_hdmi_state() && pll_out != 12000000) {
221 DBG("%s : HDMI is in, set mclk to 12Mn",__FUNCTION__);
226 //snd_soc_dai_set_pll(codec_dai, RT5625_PLL_MCLK_TO_VSYSCLK, 0, pll_out, 24576000);
228 /*Set the system clk for codec*/
229 ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
232 printk("rk_voice_hw_params:failed to set the sysclk for codec side\n");
236 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
241 static struct snd_soc_ops rk616_hifi_ops = {
242 .hw_params = rk_hifi_hw_params,
245 static struct snd_soc_ops rk616_voice_ops = {
246 .hw_params = rk_voice_hw_params,
249 static struct snd_soc_dai_link rk_dai[] = {
251 .name = "RK616 I2S1",
252 .stream_name = "RK616 PCM",
253 .codec_name = "rk616-codec.4-0050",
254 .platform_name = "rockchip-pcm",
255 #if defined(CONFIG_SND_RK_SOC_I2S_8CH)
256 .cpu_dai_name = "rockchip-i2s.0",
257 #elif defined(CONFIG_SND_RK_SOC_I2S_2CH)
258 .cpu_dai_name = "rockchip-i2s.1",
260 .codec_dai_name = "rk616-hifi",
262 .ops = &rk616_hifi_ops,
265 .name = "RK616 I2S2",
266 .stream_name = "RK616 PCM",
267 .codec_name = "rk616-codec.4-0050",
268 .platform_name = "rockchip-pcm",
269 #if defined(CONFIG_SND_RK_SOC_I2S_8CH)
270 .cpu_dai_name = "rockchip-i2s.0",
271 #elif defined(CONFIG_SND_RK_SOC_I2S_2CH)
272 .cpu_dai_name = "rockchip-i2s.1",
274 .codec_dai_name = "rk616-voice",
275 .ops = &rk616_voice_ops,
279 static struct snd_soc_card rockchip_rk616_snd_card = {
285 static int rockchip_rk616_audio_probe(struct platform_device *pdev)
288 struct snd_soc_card *card = &rockchip_rk616_snd_card;
290 card->dev = &pdev->dev;
292 ret = snd_soc_register_card(card);
295 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
300 static int rockchip_rk616_audio_remove(struct platform_device *pdev)
302 struct snd_soc_card *card = platform_get_drvdata(pdev);
304 snd_soc_unregister_card(card);
310 static const struct of_device_id rockchip_rk616_of_match[] = {
311 { .compatible = "rockchip-rk616", },
314 MODULE_DEVICE_TABLE(of, rockchip_rk616_of_match);
315 #endif /* CONFIG_OF */
317 static struct platform_driver rockchip_rk616_audio_driver = {
319 .name = "rockchip-rk616",
320 .owner = THIS_MODULE,
321 .of_match_table = of_match_ptr(rockchip_rk616_of_match),
323 .probe = rockchip_rk616_audio_probe,
324 .remove = rockchip_rk616_audio_remove,
327 module_platform_driver(rockchip_rk616_audio_driver);
329 /* Module information */
330 MODULE_AUTHOR("rockchip");
331 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
332 MODULE_LICENSE("GPL");