2 * rk29_rt5631.c -- SoC audio for rockchip
4 * Driver for rockchip rt5631 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>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/soc.h>
19 #include <sound/soc-dapm.h>
21 #include <mach/hardware.h>
22 #include <mach/rk29_iomap.h>
23 #include "../codecs/rt5631.h"
28 #define DBG(x...) printk(KERN_INFO x)
33 static int rk29_hw_params(struct snd_pcm_substream *substream,
34 struct snd_pcm_hw_params *params)
36 struct snd_soc_pcm_runtime *rtd = substream->private_data;
37 struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
38 struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
39 unsigned int pll_out = 0;
40 unsigned int lrclk = 0;
43 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
44 /*by Vincent Hsiung for EQ Vol Change*/
45 #define HW_PARAMS_FLAG_EQVOL_ON 0x21
46 #define HW_PARAMS_FLAG_EQVOL_OFF 0x22
47 if ((params->flags == HW_PARAMS_FLAG_EQVOL_ON)||(params->flags == HW_PARAMS_FLAG_EQVOL_OFF))
49 ret = codec_dai->ops->hw_params(substream, params, codec_dai); //by Vincent
50 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
55 /* set codec DAI configuration */
56 #if defined (CONFIG_SND_RK29_CODEC_SOC_SLAVE)
57 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
58 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
60 #if defined (CONFIG_SND_RK29_CODEC_SOC_MASTER)
61 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
62 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM );
67 /* set cpu DAI configuration */
68 #if defined (CONFIG_SND_RK29_CODEC_SOC_SLAVE)
69 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
70 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
72 #if defined (CONFIG_SND_RK29_CODEC_SOC_MASTER)
73 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
74 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
82 switch(params_rate(params)) {
96 DBG("Enter:%s, %d, Error rate=%d\n",__FUNCTION__,__LINE__,params_rate(params));
100 DBG("Enter:%s, %d, rate=%d\n",__FUNCTION__,__LINE__,params_rate(params));
102 #if defined (CONFIG_SND_RK29_CODEC_SOC_SLAVE)
103 #if 0 //use pll from blck
104 /*Set the pll of rt5631,the Pll source from BITCLK on CPU is master mode*/
106 ret=snd_soc_dai_set_pll(codec_dai,0,params_rate(params)*64,pll_out);
109 DBG("rk29_hw_params_rt5631:failed to set the pll for codec side\n");
113 /*Set the system clk for codec*/
114 ret=snd_soc_dai_set_sysclk(codec_dai, 0,pll_out,SND_SOC_CLOCK_IN);
117 DBG("rk29_hw_params_rt5631:failed to set the sysclk for codec side\n");
123 #if defined (CONFIG_SND_RK29_CODEC_SOC_MASTER)
125 if((24576000%params_rate(params))==0) //for 8k,16k,32k,48k
127 snd_soc_dai_set_pll(codec_dai,0,pll_out, 24576000);
128 snd_soc_dai_set_sysclk(codec_dai,0, 24576000, SND_SOC_CLOCK_IN);
130 else if((22579200%params_rate(params))==0) //for 11k,22k,44k
132 snd_soc_dai_set_pll(codec_dai,0,pll_out, 22579200);
133 snd_soc_dai_set_sysclk(codec_dai,0, 22579200, SND_SOC_CLOCK_IN);
139 #if defined (CONFIG_SND_RK29_CODEC_SOC_SLAVE)
140 snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
141 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out/4)/params_rate(params)-1);
142 snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);
145 DBG("Enter:%s, %d, LRCK=%d\n",__FUNCTION__,__LINE__,(pll_out/4)/params_rate(params));
150 static const struct snd_soc_dapm_widget rt5631_dapm_widgets[] = {
152 SND_SOC_DAPM_MIC("Mic Jack", NULL),
153 SND_SOC_DAPM_SPK("Ext Spk", NULL),
154 SND_SOC_DAPM_HP("Headphone Jack", NULL),
158 static const struct snd_soc_dapm_route audio_map[]={
160 /* Mic Jack --> MIC_IN*/
161 {"Mic Bias1", NULL, "Mic Jack"},
162 {"MIC1", NULL, "Mic Bias1"},
163 /* HP_OUT --> Headphone Jack */
164 {"Headphone Jack", NULL, "HPOL"},
165 {"Headphone Jack", NULL, "HPOR"},
166 /* LINE_OUT --> Ext Speaker */
167 {"Ext Spk", NULL, "SPOL"},
168 {"Ext Spk", NULL, "SPOR"},
173 * Logic for a rt5631 as connected on a rockchip board.
175 static int rk29_rt5631_init(struct snd_soc_codec *codec)
177 struct snd_soc_dai *codec_dai = &codec->dai[0];
180 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
182 /* Add specific widgets */
183 snd_soc_dapm_new_controls(codec, rt5631_dapm_widgets,
184 ARRAY_SIZE(rt5631_dapm_widgets));
185 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
186 /* Set up specific audio path audio_mapnects */
187 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
188 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
189 // snd_soc_dapm_nc_pin(codec, "HP_L");
190 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
191 // snd_soc_dapm_nc_pin(codec, "HP_R");
192 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
193 snd_soc_dapm_sync(codec);
194 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
199 static struct snd_soc_ops rk29_ops = {
200 .hw_params = rk29_hw_params,
203 static struct snd_soc_dai_link rk29_dai = {
205 .stream_name = "RT5631 PCM",
206 .cpu_dai = &rk29_i2s_dai[0],
207 .codec_dai = &rt5631_dai,
208 .init = rk29_rt5631_init,
212 static struct snd_soc_card snd_soc_card_rk29 = {
213 .name = "RK29_RT5631",
214 .platform = &rk29_soc_platform,
215 .dai_link = &rk29_dai,
220 static struct snd_soc_device rk29_snd_devdata = {
221 .card = &snd_soc_card_rk29,
222 .codec_dev = &soc_codec_dev_rt5631,
225 static struct platform_device *rk29_snd_device;
227 static int __init audio_card_init(void)
230 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
231 rk29_snd_device = platform_device_alloc("soc-audio", -1);
232 if (!rk29_snd_device) {
233 DBG("platform device allocation failed\n");
237 platform_set_drvdata(rk29_snd_device, &rk29_snd_devdata);
238 rk29_snd_devdata.dev = &rk29_snd_device->dev;
239 ret = platform_device_add(rk29_snd_device);
241 DBG("platform device add failed\n");
242 platform_device_put(rk29_snd_device);
247 static void __exit audio_card_exit(void)
249 platform_device_unregister(rk29_snd_device);
252 module_init(audio_card_init);
253 module_exit(audio_card_exit);
254 /* Module information */
255 MODULE_AUTHOR("rockchip");
256 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
257 MODULE_LICENSE("GPL");