Merge tag 'lsk-android-14.04' into develop-3.10
[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 "card_info.h"
25 #include "rk_pcm.h"
26 #include "rk_i2s.h"
27
28 #if 1
29 #define DBG(x...)       printk(KERN_INFO x)
30 #else
31 #define DBG(x...)
32 #endif
33
34 #ifdef CONFIG_HDMI
35         extern int hdmi_is_insert(void);
36 #endif
37
38 #ifdef CONFIG_HDMI_RK30
39         extern int hdmi_get_hotplug(void);
40 #endif
41
42 static bool get_hdmi_state(void)
43 {
44 #ifdef CONFIG_HDMI
45         if(hdmi_is_insert())
46                 return true;
47 #endif
48
49 #ifdef CONFIG_HDMI_RK30
50         if(hdmi_get_hotplug() == 2/*HDMI_HPD_ACTIVED*/)
51                 return true;
52 #endif
53
54                         return false;
55 }
56
57 static const struct snd_soc_dapm_widget rk_rk616_dapm_widgets[] = {
58         SND_SOC_DAPM_MIC("Mic Jack", NULL),
59         SND_SOC_DAPM_MIC("Headset Jack", NULL),
60         SND_SOC_DAPM_SPK("Ext Spk", NULL),
61         SND_SOC_DAPM_HP("Headphone Jack", NULL),
62 };
63
64 static const struct snd_soc_dapm_route rk_rk616_audio_map[]={
65
66         /* Mic Jack --> MIC_IN*/
67         {"Mic1 Bias", NULL, "Mic Jack"},
68         {"MIC1P", NULL, "Mic1 Bias"},
69         {"MIC1N", NULL, "Mic1 Bias"},
70
71         // HP MIC
72         {"Mic2 Bias", NULL, "Headset Jack"},
73         {"MIC2P", NULL, "Mic2 Bias"},
74         {"MIC2N", NULL, "Mic2 Bias"},
75
76         {"Ext Spk", NULL, "SPKOUTR"},
77         {"Ext Spk", NULL, "SPKOUTL"},
78
79         {"Headphone Jack", NULL, "HPOUTR"},
80         {"Headphone Jack", NULL, "HPOUTL"},
81 } ;
82
83 static const struct snd_kcontrol_new rk_rk616_controls[] = {
84         SOC_DAPM_PIN_SWITCH("Mic Jack"),
85         SOC_DAPM_PIN_SWITCH("Headset Jack"),
86         SOC_DAPM_PIN_SWITCH("Ext Spk"),
87         SOC_DAPM_PIN_SWITCH("Headphone Jack"),
88 };
89
90 static int rk616_init(struct snd_soc_pcm_runtime *rtd)
91 {
92         struct snd_soc_codec *codec = rtd->codec;
93         struct snd_soc_dapm_context *dapm = &codec->dapm;
94
95         // if is for mid that using tiny alsa, 
96         // it don't need this controls and route, so return.
97         if (rk616_get_for_mid())
98                 return 0;
99
100         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
101
102         mutex_lock(&dapm->card->dapm_mutex);
103
104         snd_soc_dapm_enable_pin(dapm, "Mic Jack");
105         snd_soc_dapm_enable_pin(dapm, "Headset Jack");
106         snd_soc_dapm_enable_pin(dapm, "Ext Spk");
107         snd_soc_dapm_enable_pin(dapm, "Headphone Jack");
108
109         mutex_unlock(&dapm->card->dapm_mutex);
110
111         snd_soc_dapm_sync(dapm);
112
113         return 0;
114 }
115
116 static int rk_hifi_hw_params(struct snd_pcm_substream *substream,
117         struct snd_pcm_hw_params *params)
118 {
119         struct snd_soc_pcm_runtime *rtd = substream->private_data;
120         struct snd_soc_dai *codec_dai = rtd->codec_dai;
121         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
122         unsigned int pll_out = 0, div = 4, dai_fmt = rtd->dai_link->dai_fmt;
123         int ret;
124
125         DBG("Enter::%s----%d\n", __FUNCTION__, __LINE__);
126
127         /* set codec DAI configuration */
128         ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
129         if (ret < 0) {
130                 printk("%s():failed to set the format for codec side\n", __FUNCTION__);
131                 return ret;
132         }
133
134         /* set cpu DAI configuration */
135         ret = snd_soc_dai_set_fmt(cpu_dai, dai_fmt);
136         if (ret < 0) {
137                 printk("%s():failed to set the format for cpu side\n", __FUNCTION__);
138                 return ret;
139         }
140
141         switch(params_rate(params)) {
142                 case 16000:
143                 case 24000:
144                 case 32000:
145                 case 48000:
146                         pll_out = 12288000;
147                         break;
148                 case 11025:
149                 case 22050:
150                 case 44100:
151                         pll_out = 11289600;
152                         break;
153                 case 8000:
154                         pll_out = 12000000;
155                         div = 6;
156                         break;
157                 default:
158                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
159                         return -EINVAL;
160                         break;
161         }
162
163         DBG("Enter:%s, %d, rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
164
165         #if defined(CONFIG_RK616_USE_MCLK_12M)
166         /* MCLK must be 12M when RK616 HDMI is in */
167         if (get_hdmi_state() && pll_out != 12000000) {
168                 DBG("%s : HDMI is in, don't set sys clk %u\n",__FUNCTION__, pll_out);
169                 goto __setdiv;
170         }
171         #endif
172
173         /* Set the system clk for codec
174            mclk will be setted in set_sysclk of codec_dai*/
175         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
176         if (ret < 0) {
177                 DBG("rk_hifi_hw_params:failed to set the sysclk for codec side\n");
178                 return ret;
179         }
180 #if defined(CONFIG_RK616_USE_MCLK_12M)
181 __setdiv:
182 #endif
183         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_BCLK, (pll_out / div)/params_rate(params)-1);
184         snd_soc_dai_set_clkdiv(cpu_dai, ROCKCHIP_DIV_MCLK, div - 1);
185
186         DBG("Enter:%s, %d, pll_out/div/params_rate(params) = %d \n", __FUNCTION__, __LINE__, (pll_out/div)/params_rate(params));
187
188         return 0;
189 }
190
191 static int rk_voice_hw_params(struct snd_pcm_substream *substream,
192         struct snd_pcm_hw_params *params)
193 {
194         struct snd_soc_pcm_runtime *rtd = substream->private_data;
195         struct snd_soc_dai *codec_dai = rtd->codec_dai;
196         struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
197         unsigned int pll_out = 0, dai_fmt = rtd->dai_link->dai_fmt;
198         int ret;
199
200         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
201
202         /* set codec DAI configuration */
203         ret = snd_soc_dai_set_fmt(codec_dai, dai_fmt);
204         if (ret < 0) {
205                 printk("rk_voice_hw_params:failed to set the format for codec side\n");
206                 return ret;
207         }
208
209         switch(params_rate(params)) {
210                 case 8000:
211                 case 16000:
212                 case 24000:
213                 case 32000:
214                 case 48000:
215                         pll_out = 12288000;
216                         break;
217                 case 11025:
218                 case 22050:
219                 case 44100:
220                         pll_out = 11289600;
221                         break;
222                 default:
223                         DBG("Enter:%s, %d, Error rate=%d\n", __FUNCTION__, __LINE__, params_rate(params));
224                         return -EINVAL;
225                         break;
226         }
227
228         /* MCLK must be 12M when RK616 HDMI is in */
229         #if defined(CONFIG_RK616_USE_MCLK_12M)
230         if (get_hdmi_state() && pll_out != 12000000) {
231                 DBG("%s : HDMI is in, set mclk to 12Mn",__FUNCTION__);
232                 pll_out = 12000000;
233         }
234         #endif
235
236         //snd_soc_dai_set_pll(codec_dai, RT5625_PLL_MCLK_TO_VSYSCLK, 0, pll_out, 24576000);
237
238         /*Set the system clk for codec*/
239         ret = snd_soc_dai_set_sysclk(codec_dai, 0, pll_out, SND_SOC_CLOCK_IN);
240         if (ret < 0) {
241                 printk("rk_voice_hw_params:failed to set the sysclk for codec side\n");
242                 return ret;
243         }
244
245         ret = snd_soc_dai_set_sysclk(cpu_dai, 0, pll_out, 0);
246         if (ret < 0) {
247                 printk("rk_voice_hw_params:failed to set the sysclk for cpu side\n");
248                 return ret;
249         }
250
251         return 0;
252 }
253
254 static struct snd_soc_ops rk616_hifi_ops = {
255         .hw_params = rk_hifi_hw_params,
256 };
257
258 static struct snd_soc_ops rk616_voice_ops = {
259         .hw_params = rk_voice_hw_params,
260 };
261
262 static struct snd_soc_dai_link rk_dai[] = {
263         {
264                 .name = "RK616 I2S1",
265                 .stream_name = "RK616 PCM",
266                 .codec_dai_name = "rk616-hifi",
267                 .init = rk616_init,
268                 .ops = &rk616_hifi_ops,
269         },
270         {
271                 .name = "RK616 I2S2",
272                 .stream_name = "RK616 PCM",
273                 .codec_dai_name = "rk616-voice",
274                 .ops = &rk616_voice_ops,
275                 //.no_pcm = 1,
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         .controls = rk_rk616_controls,
284         .num_controls = ARRAY_SIZE(rk_rk616_controls),
285         .dapm_widgets    = rk_rk616_dapm_widgets,
286         .num_dapm_widgets = ARRAY_SIZE(rk_rk616_dapm_widgets),
287         .dapm_routes    = rk_rk616_audio_map,
288         .num_dapm_routes = ARRAY_SIZE(rk_rk616_audio_map),
289 };
290 /*
291 dts:
292         rockchip-rk616 {
293                 compatible = "rockchip-rk616";
294                 dais {
295                         dai0 {
296                                 audio-codec = <&rk616>;
297                                 i2s-controller = <&i2s0>;
298                                 format = "i2s";
299                                 //continuous-clock;
300                                 //bitclock-inversion;
301                                 //frame-inversion;
302                                 //bitclock-master;
303                                 //frame-master;
304                         };
305
306                         dai1 {
307                                 audio-codec = <&rk616>;
308                                 i2s-controller = <&i2s0>;
309                                 format = "dsp_a";
310                                 //continuous-clock;
311                                 bitclock-inversion;
312                                 //frame-inversion;
313                                 //bitclock-master;
314                                 //frame-master;
315                         };
316                 };
317         };
318 */
319 static int rockchip_rk616_audio_probe(struct platform_device *pdev)
320 {
321         int ret;
322         struct snd_soc_card *card = &rockchip_rk616_snd_card;
323
324         card->dev = &pdev->dev;
325
326         ret = rockchip_of_get_sound_card_info(card);
327         if (ret) {
328                 printk("%s() get sound card info failed:%d\n", __FUNCTION__, ret);
329                 return ret;
330         }
331
332         ret = snd_soc_register_card(card);
333         if (ret)
334                 printk("%s() register card failed:%d\n", __FUNCTION__, ret);
335
336         return ret;
337 }
338
339 static int rockchip_rk616_audio_remove(struct platform_device *pdev)
340 {
341         struct snd_soc_card *card = platform_get_drvdata(pdev);
342
343         snd_soc_unregister_card(card);
344
345         return 0;
346 }
347
348 #ifdef CONFIG_OF
349 static const struct of_device_id rockchip_rk616_of_match[] = {
350         { .compatible = "rockchip-rk616", },
351         {},
352 };
353 MODULE_DEVICE_TABLE(of, rockchip_rk616_of_match);
354 #endif /* CONFIG_OF */
355
356 static struct platform_driver rockchip_rk616_audio_driver = {
357         .driver         = {
358                 .name   = "rockchip-rk616",
359                 .owner  = THIS_MODULE,
360                 .pm = &snd_soc_pm_ops,
361                 .of_match_table = of_match_ptr(rockchip_rk616_of_match),
362         },
363         .probe          = rockchip_rk616_audio_probe,
364         .remove         = rockchip_rk616_audio_remove,
365 };
366
367 module_platform_driver(rockchip_rk616_audio_driver);
368
369 /* Module information */
370 MODULE_AUTHOR("rockchip");
371 MODULE_DESCRIPTION("ROCKCHIP i2s ASoC Interface");
372 MODULE_LICENSE("GPL");