Merge remote-tracking branch 'remotes/tegra/android-tegra-2.6.36-honeycomb-mr1' into...
[firefly-linux-kernel-4.4.55.git] / sound / soc / rk29 / rk29_rt5631.c
1 /*
2  * rk29_rt5631.c  --  SoC audio for rockchip
3  *
4  * Driver for rockchip rt5631 audio
5  *
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.
10  *
11  *
12  */
13
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>
20 #include <asm/io.h>
21 #include <mach/hardware.h>
22 #include <mach/rk29_iomap.h>
23 #include "../codecs/rt5631.h"
24 #include "rk29_pcm.h"
25 #include "rk29_i2s.h"
26
27 #if 0
28 #define DBG(x...)       printk(KERN_INFO x)
29 #else
30 #define DBG(x...)
31 #endif
32
33 static int rk29_hw_params(struct snd_pcm_substream *substream,
34         struct snd_pcm_hw_params *params)
35 {
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;
41         int ret;
42           
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))
48         {
49                 ret = codec_dai->ops->hw_params(substream, params, codec_dai); //by Vincent
50                 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
51         }
52         else
53         {
54                 
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);
59                 #endif  
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 ); 
63                 #endif
64                 if (ret < 0)
65                   return ret; 
66
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);
71                 #endif  
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); 
75                 #endif          
76                 if (ret < 0)
77                   return ret;
78
79         }
80
81
82         switch(params_rate(params)) {
83         case 8000:
84         case 16000:
85         case 24000:
86         case 32000:
87         case 48000:
88                 pll_out = 12288000;
89                 break;
90         case 11025:
91         case 22050:
92         case 44100:
93                 pll_out = 11289600;
94                 break;
95         default:
96                 DBG("Enter:%s, %d, Error rate=%d\n",__FUNCTION__,__LINE__,params_rate(params));
97                 return -EINVAL;
98                 break;
99         }
100         DBG("Enter:%s, %d, rate=%d\n",__FUNCTION__,__LINE__,params_rate(params));
101
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*/
105          //bitclk is 64fs           
106                     ret=snd_soc_dai_set_pll(codec_dai,0,params_rate(params)*64,pll_out);
107                     if (ret < 0)
108                     { 
109                        DBG("rk29_hw_params_rt5631:failed to set the pll for codec side\n"); 
110                            return ret;
111                     }
112 #endif      
113                    /*Set the system clk for codec*/
114                     ret=snd_soc_dai_set_sysclk(codec_dai, 0,pll_out,SND_SOC_CLOCK_IN);
115                     if (ret < 0)
116                     {
117                        DBG("rk29_hw_params_rt5631:failed to set the sysclk for codec side\n"); 
118                            return ret;
119                         }           
120                 #endif
121   
122 /*
123         #if defined (CONFIG_SND_RK29_CODEC_SOC_MASTER) 
124
125                    if((24576000%params_rate(params))==0)        //for 8k,16k,32k,48k
126                         {
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);                        
129                         }
130                    else if((22579200%params_rate(params))==0)   //for 11k,22k,44k
131                         {
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);                        
134                         }
135       
136         #endif
137 */
138
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);
143         #endif
144
145         DBG("Enter:%s, %d, LRCK=%d\n",__FUNCTION__,__LINE__,(pll_out/4)/params_rate(params));
146         
147         return 0;
148 }
149
150 static const struct snd_soc_dapm_widget rt5631_dapm_widgets[] = {
151         
152         SND_SOC_DAPM_MIC("Mic Jack", NULL),
153         SND_SOC_DAPM_SPK("Ext Spk", NULL),
154         SND_SOC_DAPM_HP("Headphone Jack", NULL),
155
156 };
157
158 static const struct snd_soc_dapm_route audio_map[]={
159
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"},
169
170 } ;
171
172 /*
173  * Logic for a rt5631 as connected on a rockchip board.
174  */
175 static int rk29_rt5631_init(struct snd_soc_codec *codec)
176 {
177         struct snd_soc_dai *codec_dai = &codec->dai[0];
178         int ret;
179           
180         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
181
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__);
195
196         return 0;
197 }
198
199 static struct snd_soc_ops rk29_ops = {
200           .hw_params = rk29_hw_params,
201 };
202
203 static struct snd_soc_dai_link rk29_dai = {
204           .name = "RT5631",
205           .stream_name = "RT5631 PCM",
206           .cpu_dai = &rk29_i2s_dai[0],
207           .codec_dai = &rt5631_dai,
208           .init = rk29_rt5631_init,
209           .ops = &rk29_ops,
210 };
211
212 static struct snd_soc_card snd_soc_card_rk29 = {
213           .name = "RK29_RT5631",
214           .platform = &rk29_soc_platform,
215           .dai_link = &rk29_dai,
216           .num_links = 1,
217 };
218
219
220 static struct snd_soc_device rk29_snd_devdata = {
221           .card = &snd_soc_card_rk29,
222           .codec_dev = &soc_codec_dev_rt5631,
223 };
224
225 static struct platform_device *rk29_snd_device;
226
227 static int __init audio_card_init(void)
228 {
229         int ret =0;     
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");
234                   ret = -ENOMEM;
235                   return ret;
236         }
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);
240         if (ret) {
241                 DBG("platform device add failed\n");
242                 platform_device_put(rk29_snd_device);
243         }
244         return ret;
245 }
246
247 static void __exit audio_card_exit(void)
248 {
249         platform_device_unregister(rk29_snd_device);
250 }
251
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");
258 \r