Merge remote-tracking branch 'origin/upstream/linux-linaro-lsk-v3.10-android' into...
[firefly-linux-kernel-4.4.55.git] / sound / soc / rockchip / rk_rk616.c
1 /*
2  * rk_rk616.c  --  SoC audio for rockchip
3  *
4  * Driver for rockchip rk616 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/rk616_codec.h"
24 #include "rk_pcm.h"
25 #include "rk29_i2s.h"
26
27 #if 1
28 #define DBG(x...)       printk(KERN_INFO x)
29 #else
30 #define DBG(x...)
31 #endif
32
33 static const struct snd_soc_dapm_widget rk_dapm_widgets[] = {
34         SND_SOC_DAPM_MIC("Mic Jack", NULL),
35         SND_SOC_DAPM_MIC("Headset Jack", NULL),
36         SND_SOC_DAPM_SPK("Ext Spk", NULL),
37         SND_SOC_DAPM_HP("Headphone Jack", NULL),
38 };
39
40 static const struct snd_soc_dapm_route rk_audio_map[]={
41
42         /* Mic Jack --> MIC_IN*/
43         {"Mic1 Bias", NULL, "Mic Jack"},
44         {"MIC1P", NULL, "Mic1 Bias"},
45         {"MIC1N", NULL, "Mic1 Bias"},
46
47         // HP MIC
48         {"Mic2 Bias", NULL, "Headset Jack"},
49         {"MIC2P", NULL, "Mic2 Bias"},
50         {"MIC2N", NULL, "Mic2 Bias"},
51
52         {"Ext Spk", NULL, "SPKOUTR"},
53         {"Ext Spk", NULL, "SPKOUTL"},
54
55         {"Headphone Jack", NULL, "HPOUTR"},
56         {"Headphone Jack", NULL, "HPOUTL"},
57 } ;
58
59 static const struct snd_kcontrol_new rk_controls[] = {
60         SOC_DAPM_PIN_SWITCH("Mic Jack"),
61         SOC_DAPM_PIN_SWITCH("Headset Jack"),
62         SOC_DAPM_PIN_SWITCH("Ext Spk"),
63         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
64 };
65
66 static int rk616_init(struct snd_soc_pcm_runtime *rtd)
67 {
68         struct snd_soc_codec *codec = rtd->codec;
69         struct snd_soc_dapm_context *dapm = &codec->dapm;
70
71         // if is for mid that using tiny alsa, 
72         // it don't need this controls and route, so return.
73         if (rk616_get_for_mid())
74                 return 0;
75
76         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
77
78         snd_soc_add_codec_controls(codec, rk_controls,
79                         ARRAY_SIZE(rk_controls));
80
81         /* Add specific widgets */
82         snd_soc_dapm_new_controls(dapm, rk_dapm_widgets,
83                                   ARRAY_SIZE(rk_dapm_widgets));
84         /* Set up specific audio path audio_mapnects */
85         snd_soc_dapm_add_routes(dapm, rk_audio_map, ARRAY_SIZE(rk_audio_map));
86
87         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
88         snd_soc_dapm_enable_pin(dapm, "Headset Jack");
89         snd_soc_dapm_enable_pin(dapm, "Ext Spk");
90         snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
91
92         snd_soc_dapm_sync(dapm);
93
94         return 0;
95 }
96
97 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
98         struct snd_pcm_hw_params *params)
99 {
100         struct snd_soc_pcm_runtime *rtd = substream->private_data;
101         struct snd_soc_dai *codec_dai = rtd->codec_dai;
102         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
103         unsigned int pll_out = 0, div = 4;
104         int ret;
105
106         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
107
108         /* set codec DAI configuration */
109         #if defined (CONFIG_SND_RK_CODEC_SOC_SLAVE)
110
111         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
112                         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
113         #endif
114         #if defined (CONFIG_SND_RK_CODEC_SOC_MASTER)
115
116         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
117                         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM );
118         #endif
119         if (ret < 0)
120                 return ret;
121
122         /* set cpu DAI configuration */
123         #if defined (CONFIG_SND_RK_CODEC_SOC_SLAVE)
124         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
125                         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
126         #endif
127         #if defined (CONFIG_SND_RK_CODEC_SOC_MASTER)
128         ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
129                         SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
130         #endif
131         if (ret < 0)
132                 return ret;
133
134         switch(params_rate(params)) {
135                 case 16000:
136                 case 24000:
137                 case 32000:
138                 case 48000:
139                         pll_out = 12288000;
140                         break;
141                 case 11025:
142                 case 22050:
143                 case 44100:
144                         pll_out = 11289600;
145                         break;
146                 case 8000:
147                         pll_out = 12000000;
148                         div = 6;
149                         break;
150                 default:
151                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
152                         return -EINVAL;
153                         break;
154         }
155
156         DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
157
158         #if defined(CONFIG_RK616_USE_MCLK_12M)
159         /* MCLK must be 12M when RK616 HDMI is in */
160         if (get_hdmi_state() && pll_out != 12000000) {
161                 DBG("%s : HDMI is in, don't set sys clk %u\n",__FUNCTION__, pll_out);
162                 goto __setdiv;
163         }
164         #endif
165
166         /* Set the system clk for codec
167            mclk will be setted in set_sysclk of codec_dai*/
168         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
169         if (ret < 0) {
170                 DBG("rk_hifi_hw_params:failed to set the sysclk for codec side\n");
171                 return ret;
172         }
173 #if defined(CONFIG_RK616_USE_MCLK_12M)
174 __setdiv:
175 #endif
176         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out / div)/params_rate(params)-1);
177         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, div - 1);
178
179         DBG("Enter:%s, %d, pll_out/div/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/div)/params_rate(params));
180
181         return 0;
182 }
183
184 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
185         struct snd_pcm_hw_params *params)
186 {
187         struct snd_soc_pcm_runtime *rtd = substream->private_data;
188         struct snd_soc_dai *codec_dai = rtd->codec_dai;
189         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
190         unsigned int pll_out = 0;
191         int ret;
192
193         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
194
195         /* set codec DAI configuration */
196         ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A |
197                                 SND_SOC_DAIFMT_IB_NF | SND_SOC_DAIFMT_CBS_CFS);
198
199         switch(params_rate(params)) {
200                 case 8000:
201                 case 16000:
202                 case 24000:
203                 case 32000:
204                 case 48000:
205                         pll_out = 12288000;
206                         break;
207                 case 11025:
208                 case 22050:
209                 case 44100:
210                         pll_out = 11289600;
211                         break;
212                 default:
213                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
214                         return -EINVAL;
215                         break;
216         }
217
218         /* MCLK must be 12M when RK616 HDMI is in */
219         #if defined(CONFIG_RK616_USE_MCLK_12M)
220         if (get_hdmi_state() && pll_out != 12000000) {
221                 DBG("%s : HDMI is in, set mclk to 12Mn",__FUNCTION__);
222                 pll_out = 12000000;
223         }
224         #endif
225
226         //snd_soc_dai_set_pll(codec_dai, RT5625_PLL_MCLK_TO_VSYSCLK, 0, pll_out, 24576000);
227
228         /*Set the system clk for codec*/
229         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
230
231         if (ret < 0) {
232                 printk("rk_voice_hw_params:failed to set the sysclk for codec side\n");
233                 return ret;
234         }
235
236         ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
237
238         return 0;
239 }
240
241 static struct snd_soc_ops rk616_hifi_ops = {
242         .hw_params = rk_hifi_hw_params,
243 };
244
245 static struct snd_soc_ops rk616_voice_ops = {
246         .hw_params = rk_voice_hw_params,
247 };
248
249 static struct snd_soc_dai_link rk_dai[] = {
250         {
251                 .name = "RK616 I2S1",
252                 .stream_name = "RK616 PCM",
253                 .codec_name = "rk616-codec.4-0050",
254                 .platform_name = "rockchip-pcm",
255 #if defined(CONFIG_SND_RK_SOC_I2S_8CH)
256                 .cpu_dai_name = "rockchip-i2s.0",
257 #elif defined(CONFIG_SND_RK_SOC_I2S_2CH)
258                 .cpu_dai_name = "rockchip-i2s.1",
259 #endif
260                 .codec_dai_name = "rk616-hifi",
261                 .init = rk616_init,
262                 .ops = &rk616_hifi_ops,
263         },
264         {
265                 .name = "RK616 I2S2",
266                 .stream_name = "RK616 PCM",
267                 .codec_name = "rk616-codec.4-0050",
268                 .platform_name = "rockchip-pcm",
269 #if defined(CONFIG_SND_RK_SOC_I2S_8CH)
270                 .cpu_dai_name = "rockchip-i2s.0",
271 #elif defined(CONFIG_SND_RK_SOC_I2S_2CH)
272                 .cpu_dai_name = "rockchip-i2s.1",
273 #endif
274                 .codec_dai_name = "rk616-voice",
275                 .ops = &rk616_voice_ops,
276         },
277 };
278
279 static struct snd_soc_card rockchip_rk616_snd_card = {
280         .name = "RK_RK616",
281         .dai_link = rk_dai,
282         .num_links = 2,
283 };
284
285 static int rockchip_rk616_audio_probe(struct platform_device *pdev)
286 {
287         int ret;
288         struct snd_soc_card *card = &rockchip_rk616_snd_card;
289
290         card->dev = &pdev->dev;
291
292         ret = snd_soc_register_card(card);
293
294         if (ret)
295                 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
296
297         return ret;
298 }
299
300 static int rockchip_rk616_audio_remove(struct platform_device *pdev)
301 {
302         struct snd_soc_card *card = platform_get_drvdata(pdev);
303
304         snd_soc_unregister_card(card);
305
306         return 0;
307 }
308
309 #ifdef CONFIG_OF
310 static const struct of_device_id rockchip_rk616_of_match[] = {
311         { .compatible = "rockchip-rk616", },
312         {},
313 };
314 MODULE_DEVICE_TABLE(of, rockchip_rk616_of_match);
315 #endif /* CONFIG_OF */
316
317 static struct platform_driver rockchip_rk616_audio_driver = {
318         .driver         = {
319                 .name   = "rockchip-rk616",
320                 .owner  = THIS_MODULE,
321                 .of_match_table = of_match_ptr(rockchip_rk616_of_match),
322         },
323         .probe          = rockchip_rk616_audio_probe,
324         .remove         = rockchip_rk616_audio_remove,
325 };
326
327 module_platform_driver(rockchip_rk616_audio_driver);
328
329 /* Module information */
330 MODULE_AUTHOR("rockchip");
331 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
332 MODULE_LICENSE("GPL");