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 "../codecs/rt5631.h"
27 #define DBG(x...) printk(KERN_INFO x)
32 static int rk29_hw_params(struct snd_pcm_substream *substream,
33 struct snd_pcm_hw_params *params)
35 struct snd_soc_pcm_runtime *rtd = substream->private_data;
36 struct snd_soc_dai *codec_dai = rtd->codec_dai;
37 struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
38 unsigned int pll_out = 0;
41 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
42 /*by Vincent Hsiung for EQ Vol Change*/
43 #define HW_PARAMS_FLAG_EQVOL_ON 0x21
44 #define HW_PARAMS_FLAG_EQVOL_OFF 0x22
45 if ((params->flags == HW_PARAMS_FLAG_EQVOL_ON)||(params->flags == HW_PARAMS_FLAG_EQVOL_OFF))
47 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); //by Vincent
48 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
53 /* set codec DAI configuration */
54 #if defined (CONFIG_SND_RK29_CODEC_SOC_SLAVE)
55 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
56 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
58 #if defined (CONFIG_SND_RK29_CODEC_SOC_MASTER)
59 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
60 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM );
65 /* set cpu DAI configuration */
66 #if defined (CONFIG_SND_RK29_CODEC_SOC_SLAVE)
67 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
68 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
70 #if defined (CONFIG_SND_RK29_CODEC_SOC_MASTER)
71 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
72 SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
80 switch(params_rate(params)) {
94 DBG("Enter:%s, %d, Error rate=%d\n",__FUNCTION__,__LINE__,params_rate(params));
98 DBG("Enter:%s, %d, rate=%d\n",__FUNCTION__,__LINE__,params_rate(params));
100 #if defined (CONFIG_SND_RK29_CODEC_SOC_SLAVE)
101 #if 0 //use pll from blck
102 /*Set the pll of rt5631,the Pll source from BITCLK on CPU is master mode*/
104 ret=snd_soc_dai_set_pll(codec_dai,0,params_rate(params)*64,pll_out);
107 DBG("rk29_hw_params_rt5631:failed to set the pll for codec side\n");
111 /*Set the system clk for codec*/
112 ret=snd_soc_dai_set_sysclk(codec_dai, 0,pll_out,SND_SOC_CLOCK_IN);
115 DBG("rk29_hw_params_rt5631:failed to set the sysclk for codec side\n");
121 #if defined (CONFIG_SND_RK29_CODEC_SOC_MASTER)
123 // snd_soc_dai_set_pll(codec_dai,0,pll_out, 22579200);
124 snd_soc_dai_set_sysclk(codec_dai,0,pll_out, SND_SOC_CLOCK_IN);
129 #if defined (CONFIG_SND_RK29_CODEC_SOC_SLAVE)
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);
135 DBG("Enter:%s, %d, LRCK=%d\n",__FUNCTION__,__LINE__,(pll_out/4)/params_rate(params));
140 static const struct snd_soc_dapm_widget rt5631_dapm_widgets[] = {
142 SND_SOC_DAPM_MIC("Mic Jack", NULL),
143 SND_SOC_DAPM_SPK("Ext Spk", NULL),
144 SND_SOC_DAPM_HP("Headphone Jack", NULL),
148 static const struct snd_soc_dapm_route audio_map[]={
150 /* Mic Jack --> MIC_IN*/
151 {"Mic Bias1", NULL, "Mic Jack"},
152 {"MIC1", NULL, "Mic Bias1"},
153 /* HP_OUT --> Headphone Jack */
154 {"Headphone Jack", NULL, "HPOL"},
155 {"Headphone Jack", NULL, "HPOR"},
156 /* LINE_OUT --> Ext Speaker */
157 {"Ext Spk", NULL, "SPOL"},
158 {"Ext Spk", NULL, "SPOR"},
163 * Logic for a rt5631 as connected on a rockchip board.
165 static int rk29_rt5631_init(struct snd_soc_pcm_runtime *rtd)
167 struct snd_soc_codec *codec = rtd->codec;
168 struct snd_soc_dapm_context *dapm = &codec->dapm;
170 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
172 /* Add specific widgets */
173 snd_soc_dapm_new_controls(dapm, rt5631_dapm_widgets,
174 ARRAY_SIZE(rt5631_dapm_widgets));
175 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
176 /* Set up specific audio path audio_mapnects */
177 snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
178 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
179 snd_soc_dapm_nc_pin(dapm, "HP_L");
180 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
181 snd_soc_dapm_nc_pin(dapm, "HP_R");
182 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
183 snd_soc_dapm_sync(dapm);
184 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
189 static struct snd_soc_ops rk29_ops = {
190 .hw_params = rk29_hw_params,
193 static struct snd_soc_dai_link rk29_dai = {
195 .stream_name = "RT5631 PCM",
196 .codec_name = "RT5631.0-001a",
197 .platform_name = "rockchip-audio",
198 #if defined(CONFIG_SND_RK29_SOC_I2S_8CH)
199 .cpu_dai_name = "rk29_i2s.0",
200 #elif defined(CONFIG_SND_RK29_SOC_I2S_2CH)
201 .cpu_dai_name = "rk29_i2s.1",
203 .cpu_dai_name = "rk29_i2s.2",
205 .codec_dai_name = "RT5631 HiFi",
206 .init = rk29_rt5631_init,
210 static struct snd_soc_card snd_soc_card_rk29 = {
211 .name = "RK29_RT5631",
212 .dai_link = &rk29_dai,
216 static struct platform_device *rk29_snd_device;
218 static int __init audio_card_init(void)
222 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
223 rk29_snd_device = platform_device_alloc("soc-audio", -1);
224 if (!rk29_snd_device) {
225 printk("platform device allocation failed\n");
229 platform_set_drvdata(rk29_snd_device, &snd_soc_card_rk29);
230 ret = platform_device_add(rk29_snd_device);
232 printk("platform device add failed\n");
233 platform_device_put(rk29_snd_device);
238 static void __exit audio_card_exit(void)
240 platform_device_unregister(rk29_snd_device);
243 module_init(audio_card_init);
244 module_exit(audio_card_exit);
245 /* Module information */
246 MODULE_AUTHOR("rockchip");
247 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
248 MODULE_LICENSE("GPL");