Merge branch 'develop-3.10' of ssh://10.10.10.29/rk/kernel into develop-3.10
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rk_rt5640.c
1 /*
2  * rk29_rt5640.c  --  SoC audio for rockchip
3  *
4  * Driver for rockchip rt5640 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 <linux/of.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>
22
23 #include "../codecs/rt5640.h"
24 #include "card_info.h"
25 #include "rk_pcm.h"
26 #include "rk_i2s.h"
27
28
29 #if 0
30 #define DBG(x...)       printk(KERN_INFO x)
31 #else
32 #define DBG(x...)
33 #endif
34
35 static int rk29_hw_params(struct snd_pcm_substream *substream,
36         struct snd_pcm_hw_params *params)
37 {
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, dai_fmt = rtd->dai_link->dai_fmt;
42         int ret;
43
44         DBG("Enter::%s----%d\n", __FUNCTION__, __LINE__);
45
46         /* set codec DAI configuration */
47         ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
48         if (ret < 0) {
49                 printk("%s():failed to set the format for codec side\n", __FUNCTION__);
50                 return ret;
51         }
52
53         /* set cpu DAI configuration */
54         ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
55         if (ret < 0) {
56                 printk("%s():failed to set the format for cpu side\n", __FUNCTION__);
57                 return ret;
58         }
59
60         switch(params_rate(params)) {
61                 case 8000:
62                 case 16000:
63                 case 24000:
64                 case 32000:
65                 case 48000:
66                         pll_out = 12288000;
67                         break;
68                 case 11025:
69                 case 22050:
70                 case 44100:
71                         pll_out = 11289600;
72                         break;
73                 default:
74                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
75                         return -EINVAL;
76                         break;
77         }
78
79         DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
80
81         /*Set the system clk for codec*/
82         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
83         if (ret < 0)
84         {
85                 DBG("rk29_hw_params_rt5640:failed to set the sysclk for codec side\n"); 
86                 return ret;
87         }
88
89         snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
90         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out/4)/params_rate(params)-1);
91         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);
92
93         DBG("Enter:%s, %d, pll_out/4/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/4)/params_rate(params));
94  
95         return 0;
96 }
97
98 static int rt5640_voice_hw_params(struct snd_pcm_substream *substream,
99         struct snd_pcm_hw_params *params)
100 {
101         struct snd_soc_pcm_runtime *rtd = substream->private_data;
102         struct snd_soc_dai *codec_dai = rtd->codec_dai;
103         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
104         unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
105         int ret;
106
107         DBG("Enter::%s----%d\n", __FUNCTION__, __LINE__);
108
109         /* set codec DAI configuration */
110         ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
111         if (ret < 0) {
112                 printk("%s():failed to set the format for codec side\n", __FUNCTION__);
113                 return ret;
114         }
115
116         switch(params_rate(params)) {
117                 case 8000:
118                 case 16000:
119                 case 24000:
120                 case 32000:
121                 case 48000:
122                         pll_out = 12288000;
123                         break;
124                 case 11025:
125                 case 22050:
126                 case 44100:
127                         pll_out = 11289600;
128                         break;
129                 default:
130                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
131                         return -EINVAL;
132                         break;
133         }
134
135         DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
136
137         /*Set the system clk for codec*/
138         snd_soc_dai_set_pll(codec_dai, 0, RT5640_PLL1_S_MCLK, pll_out, 24576000);
139
140         ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1, 24576000, SND_SOC_CLOCK_IN);
141
142
143         if (ret < 0) {
144                 printk("rk29_hw_params_rt5640:failed to set the sysclk for codec side\n"); 
145                 return ret;
146         }
147
148         snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
149         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out/4)/params_rate(params)-1);
150         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, 3);
151
152         DBG("Enter:%s, %d, pll_out/4/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/4)/params_rate(params));
153  
154         return 0;
155 }
156
157 static const struct snd_soc_dapm_widget rt5640_dapm_widgets[] = {
158         SND_SOC_DAPM_MIC("Mic Jack", NULL),
159         SND_SOC_DAPM_MIC("Headset Jack", NULL), 
160         SND_SOC_DAPM_SPK("Ext Spk", NULL),
161         SND_SOC_DAPM_HP("Headphone Jack", NULL),
162 };
163
164 static const struct snd_soc_dapm_route audio_map[]={
165
166         /* Mic Jack --> MIC_IN*/
167         {"micbias1", NULL, "Mic Jack"},
168         {"MIC1", NULL, "micbias1"},
169         
170         // HP MIC
171         {"micbias1", NULL, "Headset Jack"},
172         {"MIC3", NULL, "micbias1"},
173
174         {"Ext Spk", NULL, "SPOLP"},
175         {"Ext Spk", NULL, "SPOLN"},
176         {"Ext Spk", NULL, "SPORP"},
177         {"Ext Spk", NULL, "SPORN"},
178
179         {"Headphone Jack", NULL, "HPOL"},
180         {"Headphone Jack", NULL, "HPOR"},
181 } ;
182
183 static const struct snd_kcontrol_new rk_controls[] = {
184         SOC_DAPM_PIN_SWITCH("Mic Jack"),
185         SOC_DAPM_PIN_SWITCH("Headset Jack"),
186         SOC_DAPM_PIN_SWITCH("Ext Spk"),
187         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
188 };
189
190 /*
191  * Logic for a rt5640 as connected on a rockchip board.
192  */
193 static int rk29_rt5640_init(struct snd_soc_pcm_runtime *rtd)
194 {
195         struct snd_soc_codec *codec = rtd->codec;
196         struct snd_soc_dapm_context *dapm = &codec->dapm;
197
198         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
199
200         snd_soc_add_codec_controls(codec, rk_controls,
201                         ARRAY_SIZE(rk_controls));
202
203         /* Add specific widgets */
204         snd_soc_dapm_new_controls(dapm, rt5640_dapm_widgets,
205                                   ARRAY_SIZE(rt5640_dapm_widgets));
206         /* Set up specific audio path audio_mapnects */
207         snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
208
209         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
210         snd_soc_dapm_enable_pin(dapm, "Headset Jack");
211         snd_soc_dapm_enable_pin(dapm, "Ext Spk");
212         snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
213
214         snd_soc_dapm_sync(dapm);
215
216         return 0;
217 }
218
219 static struct snd_soc_ops rk29_ops = {
220         .hw_params = rk29_hw_params,
221 };
222
223 static struct snd_soc_ops rt5640_voice_ops = {
224         .hw_params = rt5640_voice_hw_params,
225 };
226
227 static struct snd_soc_dai_link rk29_dai[] = {
228         {
229                 .name = "RT5640 I2S1",
230                 .stream_name = "RT5640 PCM",
231                 .codec_dai_name = "rt5640-aif1",
232                 .init = rk29_rt5640_init,
233                 .ops = &rk29_ops,
234         },
235         {
236                 .name = "RT5640 I2S2",
237                 .stream_name = "RT5640 PCM",
238                 .codec_dai_name = "rt5640-aif2",
239                 .ops = &rt5640_voice_ops,
240         },
241 };
242
243 static struct snd_soc_card rockchip_rt5640_snd_card = {
244         .name = "RK_RT5640",
245         .dai_link = rk29_dai,
246         .num_links = 2,
247 };
248
249 static int rockchip_rt5640_audio_probe(struct platform_device *pdev)
250 {
251         int ret;
252         struct snd_soc_card *card = &rockchip_rt5640_snd_card;
253
254         card->dev = &pdev->dev;
255
256         ret = rockchip_of_get_sound_card_info(card);
257         if (ret) {
258                 printk("%s() get sound card info failed:%d\n", __FUNCTION__, ret);
259                 return ret;
260         }
261
262         ret = snd_soc_register_card(card);
263         if (ret)
264                 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
265
266         return ret;
267 }
268
269 static int rockchip_rt5640_audio_remove(struct platform_device *pdev)
270 {
271         struct snd_soc_card *card = platform_get_drvdata(pdev);
272
273         snd_soc_unregister_card(card);
274
275         return 0;
276 }
277
278 #ifdef CONFIG_OF
279 static const struct of_device_id rockchip_rt5640_of_match[] = {
280         { .compatible = "rockchip-rt5640", },
281         {},
282 };
283 MODULE_DEVICE_TABLE(of, rockchip_rt5640_of_match);
284 #endif /* CONFIG_OF */
285
286 static struct platform_driver rockchip_rt5640_audio_driver = {
287         .driver         = {
288                 .name   = "rockchip-rt5640",
289                 .owner  = THIS_MODULE,
290                 .of_match_table = of_match_ptr(rockchip_rt5640_of_match),
291         },
292         .probe          = rockchip_rt5640_audio_probe,
293         .remove         = rockchip_rt5640_audio_remove,
294 };
295
296 module_platform_driver(rockchip_rt5640_audio_driver);
297
298 /* Module information */
299 MODULE_AUTHOR("rockchip");
300 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
301 MODULE_LICENSE("GPL");