2 * rk29_rt5640.c -- SoC audio for rockchip
4 * Driver for rockchip rt5640 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/rt5640.h"
24 #include "card_info.h"
30 #define DBG(x...) printk(KERN_INFO x)
35 static int rk29_hw_params(struct snd_pcm_substream *substream,
36 struct snd_pcm_hw_params *params)
38 struct snd_soc_pcm_runtime *rtd = substream->private_data;
39 struct snd_soc_dai *codec_dai = rtd->codec_dai;
40 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
41 unsigned int pll_out = 0;
44 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
46 switch(params_rate(params)) {
60 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
65 DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
67 /*Set the system clk for codec*/
68 ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
71 DBG("rk29_hw_params_rt5640:failed to set the sysclk for codec side\n");
75 snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
76 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out/4)/params_rate(params)-1);
77 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);
79 DBG("Enter:%s, %d, pll_out/4/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/4)/params_rate(params));
84 static int rt5640_voice_hw_params(struct snd_pcm_substream *substream,
85 struct snd_pcm_hw_params *params)
87 struct snd_soc_pcm_runtime *rtd = substream->private_data;
88 struct snd_soc_dai *codec_dai = rtd->codec_dai;
89 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
90 unsigned int pll_out = 0;
93 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
95 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A |
96 SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS );
98 switch(params_rate(params)) {
112 DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
117 DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
119 /*Set the system clk for codec*/
120 snd_soc_dai_set_pll(codec_dai, 0, RT5640_PLL1_S_MCLK, pll_out, 24576000);
122 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1, 24576000, SND_SOC_CLOCK_IN);
126 printk("rk29_hw_params_rt5640:failed to set the sysclk for codec side\n");
130 snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
131 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out/4)/params_rate(params)-1);
132 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);
134 DBG("Enter:%s, %d, pll_out/4/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/4)/params_rate(params));
139 static const struct snd_soc_dapm_widget rt5640_dapm_widgets[] = {
140 SND_SOC_DAPM_MIC("Mic Jack", NULL),
141 SND_SOC_DAPM_MIC("Headset Jack", NULL),
142 SND_SOC_DAPM_SPK("Ext Spk", NULL),
143 SND_SOC_DAPM_HP("Headphone Jack", NULL),
146 static const struct snd_soc_dapm_route audio_map[]={
148 /* Mic Jack --> MIC_IN*/
149 {"micbias1", NULL, "Mic Jack"},
150 {"MIC1", NULL, "micbias1"},
153 {"micbias1", NULL, "Headset Jack"},
154 {"MIC3", NULL, "micbias1"},
156 {"Ext Spk", NULL, "SPOLP"},
157 {"Ext Spk", NULL, "SPOLN"},
158 {"Ext Spk", NULL, "SPORP"},
159 {"Ext Spk", NULL, "SPORN"},
161 {"Headphone Jack", NULL, "HPOL"},
162 {"Headphone Jack", NULL, "HPOR"},
165 static const struct snd_kcontrol_new rk_controls[] = {
166 SOC_DAPM_PIN_SWITCH("Mic Jack"),
167 SOC_DAPM_PIN_SWITCH("Headset Jack"),
168 SOC_DAPM_PIN_SWITCH("Ext Spk"),
169 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
173 * Logic for a rt5640 as connected on a rockchip board.
175 static int rk29_rt5640_init(struct snd_soc_pcm_runtime *rtd)
177 struct snd_soc_codec *codec = rtd->codec;
178 struct snd_soc_dapm_context *dapm = &codec->dapm;
180 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
182 snd_soc_add_codec_controls(codec, rk_controls,
183 ARRAY_SIZE(rk_controls));
185 /* Add specific widgets */
186 snd_soc_dapm_new_controls(dapm, rt5640_dapm_widgets,
187 ARRAY_SIZE(rt5640_dapm_widgets));
188 /* Set up specific audio path audio_mapnects */
189 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
191 snd_soc_dapm_enable_pin(dapm, "Mic Jack");
192 snd_soc_dapm_enable_pin(dapm, "Headset Jack");
193 snd_soc_dapm_enable_pin(dapm, "Ext Spk");
194 snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
196 extern int hdmi_is_insert(void);
197 extern void codec_set_spk(bool on);
199 codec_set_spk(false);
202 #ifdef CONFIG_HDMI_RK30
203 extern int hdmi_get_hotplug(void);
204 if(hdmi_get_hotplug() == 2/*HDMI_HPD_ACTIVED*/)
205 codec_set_spk(false);
208 snd_soc_dapm_sync(dapm);
213 static struct snd_soc_ops rk29_ops = {
214 .hw_params = rk29_hw_params,
217 static struct snd_soc_ops rt5640_voice_ops = {
218 .hw_params = rt5640_voice_hw_params,
221 static struct snd_soc_dai_link rk29_dai[] = {
223 .name = "RT5640 I2S1",
224 .stream_name = "RT5640 PCM",
225 .codec_dai_name = "rt5640-aif1",
226 .init = rk29_rt5640_init,
230 .name = "RT5640 I2S2",
231 .stream_name = "RT5640 PCM",
232 .codec_dai_name = "rt5640-aif2",
233 .ops = &rt5640_voice_ops,
237 static struct snd_soc_card rockchip_rt5640_snd_card = {
239 .dai_link = rk29_dai,
243 static int rockchip_rt5640_audio_probe(struct platform_device *pdev)
246 struct snd_soc_card *card = &rockchip_rt5640_snd_card;
248 card->dev = &pdev->dev;
250 ret = rockchip_of_get_sound_card_info(card);
252 printk("%s() get sound card info failed:%d\n", __FUNCTION__, ret);
256 ret = snd_soc_register_card(card);
258 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
263 static int rockchip_rt5640_audio_remove(struct platform_device *pdev)
265 struct snd_soc_card *card = platform_get_drvdata(pdev);
267 snd_soc_unregister_card(card);
273 static const struct of_device_id rockchip_rt5640_of_match[] = {
274 { .compatible = "rockchip-rt5640", },
277 MODULE_DEVICE_TABLE(of, rockchip_rt5640_of_match);
278 #endif /* CONFIG_OF */
280 static struct platform_driver rockchip_rt5640_audio_driver = {
282 .name = "rockchip-rt5640",
283 .owner = THIS_MODULE,
284 .of_match_table = of_match_ptr(rockchip_rt5640_of_match),
286 .probe = rockchip_rt5640_audio_probe,
287 .remove = rockchip_rt5640_audio_remove,
290 module_platform_driver(rockchip_rt5640_audio_driver);
292 /* Module information */
293 MODULE_AUTHOR("rockchip");
294 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
295 MODULE_LICENSE("GPL");