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_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_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_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 snd_soc_add_codec_controls(codec, rk_controls,
103 ARRAY_SIZE(rk_controls));
105 /* Add specific widgets */
106 snd_soc_dapm_new_controls(dapm, rk_dapm_widgets,
107 ARRAY_SIZE(rk_dapm_widgets));
108 /* Set up specific audio path audio_mapnects */
109 snd_soc_dapm_add_routes(dapm, rk_audio_map, ARRAY_SIZE(rk_audio_map));
111 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
112 snd_soc_dapm_enable_pin(dapm, "Headset Jack");
113 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
114 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
116 snd_soc_dapm_sync(dapm);
121 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
122 struct snd_pcm_hw_params *params)
124 struct snd_soc_pcm_runtime *rtd = substream->private_data;
125 struct snd_soc_dai *codec_dai = rtd->codec_dai;
126 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
127 unsigned int pll_out = 0, div = 4, dai_fmt = rtd->dai_link->dai_fmt;
130 DBG("Enter::%s----%d\n", __FUNCTION__, __LINE__);
132 /* set codec DAI configuration */
133 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
135 printk("%s():failed to set the format for codec side\n", __FUNCTION__);
139 /* set cpu DAI configuration */
140 ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
142 printk("%s():failed to set the format for cpu side\n", __FUNCTION__);
146 switch(params_rate(params)) {
163 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
168 DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
170 #if defined(CONFIG_RK616_USE_MCLK_12M)
171 /* MCLK must be 12M when RK616 HDMI is in */
172 if (get_hdmi_state() && pll_out != 12000000) {
173 DBG("%s : HDMI is in, don't set sys clk %u\n",__FUNCTION__, pll_out);
178 /* Set the system clk for codec
179 mclk will be setted in set_sysclk of codec_dai*/
180 ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
182 DBG("rk_hifi_hw_params:failed to set the sysclk for codec side\n");
185 #if defined(CONFIG_RK616_USE_MCLK_12M)
188 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out / div)/params_rate(params)-1);
189 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, div - 1);
191 DBG("Enter:%s, %d, pll_out/div/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/div)/params_rate(params));
196 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
197 struct snd_pcm_hw_params *params)
199 struct snd_soc_pcm_runtime *rtd = substream->private_data;
200 struct snd_soc_dai *codec_dai = rtd->codec_dai;
201 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
202 unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
205 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
207 /* set codec DAI configuration */
208 ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
210 printk("rk_voice_hw_params:failed to set the format for codec side\n");
214 switch(params_rate(params)) {
228 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
233 /* MCLK must be 12M when RK616 HDMI is in */
234 #if defined(CONFIG_RK616_USE_MCLK_12M)
235 if (get_hdmi_state() && pll_out != 12000000) {
236 DBG("%s : HDMI is in, set mclk to 12Mn",__FUNCTION__);
241 //snd_soc_dai_set_pll(codec_dai, RT5625_PLL_MCLK_TO_VSYSCLK, 0, pll_out, 24576000);
243 /*Set the system clk for codec*/
244 ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
246 printk("rk_voice_hw_params:failed to set the sysclk for codec side\n");
250 ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
252 printk("rk_voice_hw_params:failed to set the sysclk for cpu side\n");
259 static struct snd_soc_ops rk616_hifi_ops = {
260 .hw_params = rk_hifi_hw_params,
263 static struct snd_soc_ops rk616_voice_ops = {
264 .hw_params = rk_voice_hw_params,
267 static struct snd_soc_dai_link rk_dai[] = {
269 .name = "RK616 I2S1",
270 .stream_name = "RK616 PCM",
271 .codec_dai_name = "rk616-hifi",
273 .ops = &rk616_hifi_ops,
276 .name = "RK616 I2S2",
277 .stream_name = "RK616 PCM",
278 .codec_dai_name = "rk616-voice",
279 .ops = &rk616_voice_ops,
284 static struct snd_soc_card rockchip_rk616_snd_card = {
292 compatible = "rockchip-rk616";
295 audio-codec = <&rk616>;
296 i2s-controller = <&i2s0>;
299 //bitclock-inversion;
306 audio-codec = <&rk616>;
307 i2s-controller = <&i2s0>;
318 static int rockchip_rk616_audio_probe(struct platform_device *pdev)
321 struct snd_soc_card *card = &rockchip_rk616_snd_card;
323 card->dev = &pdev->dev;
325 ret = rockchip_of_get_sound_card_info(card);
327 printk("%s() get sound card info failed:%d\n", __FUNCTION__, ret);
331 ret = snd_soc_register_card(card);
333 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
338 static int rockchip_rk616_audio_remove(struct platform_device *pdev)
340 struct snd_soc_card *card = platform_get_drvdata(pdev);
342 snd_soc_unregister_card(card);
348 static const struct of_device_id rockchip_rk616_of_match[] = {
349 { .compatible = "rockchip-rk616", },
352 MODULE_DEVICE_TABLE(of, rockchip_rk616_of_match);
353 #endif /* CONFIG_OF */
355 static struct platform_driver rockchip_rk616_audio_driver = {
357 .name = "rockchip-rk616",
358 .owner = THIS_MODULE,
359 .of_match_table = of_match_ptr(rockchip_rk616_of_match),
361 .probe = rockchip_rk616_audio_probe,
362 .remove = rockchip_rk616_audio_remove,
365 module_platform_driver(rockchip_rk616_audio_driver);
367 /* Module information */
368 MODULE_AUTHOR("rockchip");
369 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
370 MODULE_LICENSE("GPL");