Merge remote-tracking branch 'stable/linux-3.0.y' into develop-3.0-jb
[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 "../codecs/rt5631.h"
23 #include "rk29_pcm.h"
24 #include "rk29_i2s.h"
25
26 #if 0
27 #define DBG(x...)       printk(KERN_INFO x)
28 #else
29 #define DBG(x...)
30 #endif
31
32 static int rk29_hw_params(struct snd_pcm_substream *substream,
33         struct snd_pcm_hw_params *params)
34 {
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; 
39         int ret;
40
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))
46         {
47                 ret = codec_dai->driver->ops->hw_params(substream, params, codec_dai); //by Vincent
48                 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
49         }
50         else
51         {
52                 
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);
57                 #endif  
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 ); 
61                 #endif
62                 if (ret < 0)
63                   return ret; 
64
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);
69                 #endif  
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); 
73                 #endif          
74                 if (ret < 0)
75                   return ret;
76
77         }
78
79
80         switch(params_rate(params)) {
81         case 8000:
82         case 16000:
83         case 24000:
84         case 32000:
85         case 48000:
86                 pll_out = 12288000;
87                 break;
88         case 11025:
89         case 22050:
90         case 44100:
91                 pll_out = 11289600;
92                 break;
93         default:
94                 DBG("Enter:%s, %d, Error rate=%d\n",__FUNCTION__,__LINE__,params_rate(params));
95                 return -EINVAL;
96                 break;
97         }
98         DBG("Enter:%s, %d, rate=%d\n",__FUNCTION__,__LINE__,params_rate(params));
99
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*/
103          //bitclk is 64fs           
104                     ret=snd_soc_dai_set_pll(codec_dai,0,params_rate(params)*64,pll_out);
105                     if (ret < 0)
106                     { 
107                        DBG("rk29_hw_params_rt5631:failed to set the pll for codec side\n"); 
108                            return ret;
109                     }
110 #endif      
111                    /*Set the system clk for codec*/
112                     ret=snd_soc_dai_set_sysclk(codec_dai, 0,pll_out,SND_SOC_CLOCK_IN);
113                     if (ret < 0)
114                     {
115                        DBG("rk29_hw_params_rt5631:failed to set the sysclk for codec side\n"); 
116                            return ret;
117                         }           
118                 #endif
119   
120
121         #if defined (CONFIG_SND_RK29_CODEC_SOC_MASTER) 
122                 
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);                                          
125       
126         #endif
127
128
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);
133         #endif
134
135         DBG("Enter:%s, %d, LRCK=%d\n",__FUNCTION__,__LINE__,(pll_out/4)/params_rate(params));
136         
137         return 0;
138 }
139
140 static const struct snd_soc_dapm_widget rt5631_dapm_widgets[] = {
141         
142         SND_SOC_DAPM_MIC("Mic Jack", NULL),
143         SND_SOC_DAPM_SPK("Ext Spk", NULL),
144         SND_SOC_DAPM_HP("Headphone Jack", NULL),
145
146 };
147
148 static const struct snd_soc_dapm_route audio_map[]={
149
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"},
159
160 } ;
161
162 /*
163  * Logic for a rt5631 as connected on a rockchip board.
164  */
165 static int rk29_rt5631_init(struct snd_soc_pcm_runtime *rtd)
166 {
167         struct snd_soc_codec *codec = rtd->codec;
168         struct snd_soc_dapm_context *dapm = &codec->dapm;
169
170         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
171
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__);
185
186         return 0;
187 }
188
189 static struct snd_soc_ops rk29_ops = {
190           .hw_params = rk29_hw_params,
191 };
192
193 static struct snd_soc_dai_link rk29_dai = {
194         .name = "RT5631",
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",
202 #else
203         .cpu_dai_name = "rk29_i2s.2",
204 #endif
205         .codec_dai_name = "RT5631 HiFi",
206         .init = rk29_rt5631_init,
207         .ops = &rk29_ops,
208 };
209
210 static struct snd_soc_card snd_soc_card_rk29 = {
211         .name = "RK29_RT5631",
212         .dai_link = &rk29_dai,
213         .num_links = 1,
214 };
215
216 static struct platform_device *rk29_snd_device;
217
218 static int __init audio_card_init(void)
219 {
220         int ret =0;
221
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");
226                   ret = -ENOMEM;
227                   return ret;
228         }
229         platform_set_drvdata(rk29_snd_device, &snd_soc_card_rk29);
230         ret = platform_device_add(rk29_snd_device);
231         if (ret) {
232                 printk("platform device add failed\n");
233                 platform_device_put(rk29_snd_device);
234         }
235         return ret;
236 }
237
238 static void __exit audio_card_exit(void)
239 {
240         platform_device_unregister(rk29_snd_device);
241 }
242
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");
249