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"
24 #include "card_info.h"
29 #define DBG(x...) printk(KERN_INFO x)
35 extern int hdmi_is_insert(void);
38 #ifdef CONFIG_HDMI_RK30
39 extern int hdmi_get_hotplug(void);
42 static bool get_hdmi_state(void)
49 #ifdef CONFIG_HDMI_RK30
50 if(hdmi_get_hotplug() == 2/*HDMI_HPD_ACTIVED*/)
57 static const struct snd_soc_dapm_widget rk_rk616_dapm_widgets[] = {
58 SND_SOC_DAPM_MIC("Mic Jack", NULL),
59 SND_SOC_DAPM_MIC("Headset Jack", NULL),
60 SND_SOC_DAPM_SPK("Ext Spk", NULL),
61 SND_SOC_DAPM_HP("Headphone Jack", NULL),
64 static const struct snd_soc_dapm_route rk_rk616_audio_map[]={
66 /* Mic Jack --> MIC_IN*/
67 {"Mic1 Bias", NULL, "Mic Jack"},
68 {"MIC1P", NULL, "Mic1 Bias"},
69 {"MIC1N", NULL, "Mic1 Bias"},
72 {"Mic2 Bias", NULL, "Headset Jack"},
73 {"MIC2P", NULL, "Mic2 Bias"},
74 {"MIC2N", NULL, "Mic2 Bias"},
76 {"Ext Spk", NULL, "SPKOUTR"},
77 {"Ext Spk", NULL, "SPKOUTL"},
79 {"Headphone Jack", NULL, "HPOUTR"},
80 {"Headphone Jack", NULL, "HPOUTL"},
83 static const struct snd_kcontrol_new rk_rk616_controls[] = {
84 SOC_DAPM_PIN_SWITCH("Mic Jack"),
85 SOC_DAPM_PIN_SWITCH("Headset Jack"),
86 SOC_DAPM_PIN_SWITCH("Ext Spk"),
87 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
90 static int rk616_init(struct snd_soc_pcm_runtime *rtd)
92 struct snd_soc_codec *codec = rtd->codec;
93 struct snd_soc_dapm_context *dapm = &codec->dapm;
95 // if is for mid that using tiny alsa,
96 // it don't need this controls and route, so return.
97 if (rk616_get_for_mid())
100 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
102 mutex_lock(&dapm->card->dapm_mutex);
104 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
105 snd_soc_dapm_enable_pin(dapm, "Headset Jack");
106 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
107 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
109 mutex_unlock(&dapm->card->dapm_mutex);
111 snd_soc_dapm_sync(dapm);
116 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
117 struct snd_pcm_hw_params *params)
119 struct snd_soc_pcm_runtime *rtd = substream->private_data;
120 struct snd_soc_dai *codec_dai = rtd->codec_dai;
121 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
122 unsigned int pll_out = 0, div = 4, dai_fmt = rtd->dai_link->dai_fmt;
125 DBG("Enter::%s----%d\n", __FUNCTION__, __LINE__);
127 /* set codec DAI configuration */
128 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
130 printk("%s():failed to set the format for codec side\n", __FUNCTION__);
134 /* set cpu DAI configuration */
135 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
137 printk("%s():failed to set the format for cpu side\n", __FUNCTION__);
141 switch(params_rate(params)) {
158 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
163 DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
165 #if defined(CONFIG_RK616_USE_MCLK_12M)
166 /* MCLK must be 12M when RK616 HDMI is in */
167 if (get_hdmi_state() && pll_out != 12000000) {
168 DBG("%s : HDMI is in, don't set sys clk %u\n",__FUNCTION__, pll_out);
173 /* Set the system clk for codec
174 mclk will be setted in set_sysclk of codec_dai*/
175 ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
177 DBG("rk_hifi_hw_params:failed to set the sysclk for codec side\n");
180 #if defined(CONFIG_RK616_USE_MCLK_12M)
183 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out / div)/params_rate(params)-1);
184 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, div - 1);
186 DBG("Enter:%s, %d, pll_out/div/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/div)/params_rate(params));
191 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
192 struct snd_pcm_hw_params *params)
194 struct snd_soc_pcm_runtime *rtd = substream->private_data;
195 struct snd_soc_dai *codec_dai = rtd->codec_dai;
196 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
197 unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
200 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
202 /* set codec DAI configuration */
203 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
205 printk("rk_voice_hw_params:failed to set the format for codec side\n");
209 switch(params_rate(params)) {
223 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
228 /* MCLK must be 12M when RK616 HDMI is in */
229 #if defined(CONFIG_RK616_USE_MCLK_12M)
230 if (get_hdmi_state() && pll_out != 12000000) {
231 DBG("%s : HDMI is in, set mclk to 12Mn",__FUNCTION__);
236 //snd_soc_dai_set_pll(codec_dai, RT5625_PLL_MCLK_TO_VSYSCLK, 0, pll_out, 24576000);
238 /*Set the system clk for codec*/
239 ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
241 printk("rk_voice_hw_params:failed to set the sysclk for codec side\n");
245 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
247 printk("rk_voice_hw_params:failed to set the sysclk for cpu side\n");
254 static struct snd_soc_ops rk616_hifi_ops = {
255 .hw_params = rk_hifi_hw_params,
258 static struct snd_soc_ops rk616_voice_ops = {
259 .hw_params = rk_voice_hw_params,
262 static struct snd_soc_dai_link rk_dai[] = {
264 .name = "RK616 I2S1",
265 .stream_name = "RK616 PCM",
266 .codec_dai_name = "rk616-hifi",
268 .ops = &rk616_hifi_ops,
271 .name = "RK616 I2S2",
272 .stream_name = "RK616 PCM",
273 .codec_dai_name = "rk616-voice",
274 .ops = &rk616_voice_ops,
279 static struct snd_soc_card rockchip_rk616_snd_card = {
283 .controls = rk_rk616_controls,
284 .num_controls = ARRAY_SIZE(rk_rk616_controls),
285 .dapm_widgets = rk_rk616_dapm_widgets,
286 .num_dapm_widgets = ARRAY_SIZE(rk_rk616_dapm_widgets),
287 .dapm_routes = rk_rk616_audio_map,
288 .num_dapm_routes = ARRAY_SIZE(rk_rk616_audio_map),
293 compatible = "rockchip-rk616";
296 audio-codec = <&rk616>;
297 i2s-controller = <&i2s0>;
300 //bitclock-inversion;
307 audio-codec = <&rk616>;
308 i2s-controller = <&i2s0>;
319 static int rockchip_rk616_audio_probe(struct platform_device *pdev)
322 struct snd_soc_card *card = &rockchip_rk616_snd_card;
324 card->dev = &pdev->dev;
326 ret = rockchip_of_get_sound_card_info(card);
328 printk("%s() get sound card info failed:%d\n", __FUNCTION__, ret);
332 ret = snd_soc_register_card(card);
334 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
339 static int rockchip_rk616_audio_remove(struct platform_device *pdev)
341 struct snd_soc_card *card = platform_get_drvdata(pdev);
343 snd_soc_unregister_card(card);
349 static const struct of_device_id rockchip_rk616_of_match[] = {
350 { .compatible = "rockchip-rk616", },
353 MODULE_DEVICE_TABLE(of, rockchip_rk616_of_match);
354 #endif /* CONFIG_OF */
356 static struct platform_driver rockchip_rk616_audio_driver = {
358 .name = "rockchip-rk616",
359 .owner = THIS_MODULE,
360 .of_match_table = of_match_ptr(rockchip_rk616_of_match),
362 .probe = rockchip_rk616_audio_probe,
363 .remove = rockchip_rk616_audio_remove,
366 module_platform_driver(rockchip_rk616_audio_driver);
368 /* Module information */
369 MODULE_AUTHOR("rockchip");
370 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
371 MODULE_LICENSE("GPL");